max98504.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * max98504.c -- MAX98504 ALSA SoC Audio driver
  3. *
  4. * Copyright 2011-2012 Maxim Integrated Products
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <sound/pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/soc.h>
  17. #include <sound/tlv.h>
  18. #include <sound/max98504.h>
  19. #include "max98504.h"
  20. #include <linux/regulator/consumer.h>
  21. #ifdef DEBUG_MAX98504
  22. #define msg_maxim(format, args...) \
  23. printk(KERN_INFO "[MAX98504_DEBUG] %s " format, __func__, ## args)
  24. #else
  25. #define msg_maxim(format, args...)
  26. #endif
  27. static const u8 max98504_reg_def[MAX98504_REG_CNT] = {
  28. [MAX98504_REG_01_INTERRUPT_STATUS] = 0,
  29. [MAX98504_REG_02_INTERRUPT_FLAGS] = 0,
  30. [MAX98504_REG_03_INTERRUPT_ENABLES] = 0,
  31. [MAX98504_REG_04_INTERRUPT_FLAG_CLEARS] = 0,
  32. [MAX98504_REG_10_GPIO_ENABLE] = 0,
  33. [MAX98504_REG_11_GPIO_CONFIG] = 0,
  34. [MAX98504_REG_12_WATCHDOG_ENABLE] = 0,
  35. [MAX98504_REG_13_WATCHDOG_CONFIG] = 0,
  36. [MAX98504_REG_14_WATCHDOG_CLEAR] = 0,
  37. [MAX98504_REG_15_CLOCK_MONITOR_ENABLE] = 0,
  38. [MAX98504_REG_16_PVDD_BROWNOUT_ENABLE] = 0,
  39. [MAX98504_REG_17_PVDD_BROWNOUT_CONFIG_1] = 0,
  40. [MAX98504_REG_18_PVDD_BROWNOUT_CONFIG_2] = 0,
  41. [MAX98504_REG_19_PVDD_BROWNOUT_CONFIG_3] = 0,
  42. [MAX98504_REG_1A_PVDD_BROWNOUT_CONFIG_4] = 0,
  43. [MAX98504_REG_20_PCM_RX_ENABLES] = 0,
  44. [MAX98504_REG_21_PCM_TX_ENABLES] = 0,
  45. [MAX98504_REG_22_PCM_TX_HIZ_CONTROL] = 0,
  46. [MAX98504_REG_23_PCM_TX_CHANNEL_SOURCES] = 0,
  47. [MAX98504_REG_24_PCM_MODE_CONFIG] = 0,
  48. [MAX98504_REG_25_PCM_DSP_CONFIG] = 0,
  49. [MAX98504_REG_26_PCM_CLOCK_SETUP] = 0,
  50. [MAX98504_REG_27_PCM_SAMPLE_RATE_SETUP] = 0,
  51. [MAX98504_REG_28_PCM_TO_SPEAKER_MONOMIX] = 0,
  52. [MAX98504_REG_30_PDM_TX_ENABLES] = 0,
  53. [MAX98504_REG_31_PDM_TX_HIZ_CONTROL] = 0,
  54. [MAX98504_REG_32_PDM_TX_CONTROL] = 0,
  55. [MAX98504_REG_33_PDM_RX_ENABLE] = 0,
  56. [MAX98504_REG_34_SPEAKER_ENABLE] = 0,
  57. [MAX98504_REG_35_SPEAKER_SOURCE_SELECT] = 0,
  58. [MAX98504_REG_36_MEASUREMENT_ENABLES] = 0,
  59. [MAX98504_REG_37_ANALOGUE_INPUT_GAIN] = 0,
  60. [MAX98504_REG_38_TEMPERATURE_LIMIT_CONFIG] = 0,
  61. [MAX98504_REG_39_ANALOGUE_SPARE] = 0,
  62. [MAX98504_REG_40_GLOBAL_ENABLE] = 0,
  63. [MAX98504_REG_41_SOFTWARE_RESET] = 0,
  64. };
  65. static struct {
  66. u8 read;
  67. u8 write;
  68. u8 vol;
  69. } max98504_reg_access[MAX98504_REG_CNT] = {
  70. [MAX98504_REG_01_INTERRUPT_STATUS] = { 0xFF, 0x00, 0xFF },
  71. [MAX98504_REG_02_INTERRUPT_FLAGS] = { 0xFF, 0x00, 0xFF },
  72. [MAX98504_REG_03_INTERRUPT_ENABLES] = { 0xFF, 0xFF, 0x00 },
  73. [MAX98504_REG_04_INTERRUPT_FLAG_CLEARS] = { 0x00, 0xFF, 0xFF },
  74. [MAX98504_REG_10_GPIO_ENABLE] = { 0xFF, 0xFF, 0x00 },
  75. [MAX98504_REG_11_GPIO_CONFIG] = { 0xFF, 0xFF, 0x00 },
  76. [MAX98504_REG_12_WATCHDOG_ENABLE] = { 0xFF, 0xFF, 0x00 },
  77. [MAX98504_REG_13_WATCHDOG_CONFIG] = { 0xFF, 0xFF, 0x00 },
  78. [MAX98504_REG_14_WATCHDOG_CLEAR] = { 0x00, 0xFF, 0xFF },
  79. [MAX98504_REG_15_CLOCK_MONITOR_ENABLE] = { 0xFF, 0xFF, 0x00 },
  80. [MAX98504_REG_16_PVDD_BROWNOUT_ENABLE] = { 0xFF, 0xFF, 0x00 },
  81. [MAX98504_REG_17_PVDD_BROWNOUT_CONFIG_1] = { 0xFF, 0xFF, 0x00 },
  82. [MAX98504_REG_18_PVDD_BROWNOUT_CONFIG_2] = { 0xFF, 0xFF, 0x00 },
  83. [MAX98504_REG_19_PVDD_BROWNOUT_CONFIG_3] = { 0xFF, 0xFF, 0x00 },
  84. [MAX98504_REG_1A_PVDD_BROWNOUT_CONFIG_4] = { 0xFF, 0xFF, 0x00 },
  85. [MAX98504_REG_20_PCM_RX_ENABLES] = { 0xFF, 0xFF, 0x00 },
  86. [MAX98504_REG_21_PCM_TX_ENABLES] = { 0xFF, 0xFF, 0x00 },
  87. [MAX98504_REG_22_PCM_TX_HIZ_CONTROL] = { 0xFF, 0xFF, 0x00 },
  88. [MAX98504_REG_23_PCM_TX_CHANNEL_SOURCES] = { 0xFF, 0xFF, 0x00 },
  89. [MAX98504_REG_24_PCM_MODE_CONFIG] = { 0xFF, 0xFF, 0x00 },
  90. [MAX98504_REG_25_PCM_DSP_CONFIG] = { 0xFF, 0xFF, 0x00 },
  91. [MAX98504_REG_26_PCM_CLOCK_SETUP] = { 0xFF, 0xFF, 0x00 },
  92. [MAX98504_REG_27_PCM_SAMPLE_RATE_SETUP] = { 0xFF, 0xFF, 0x00 },
  93. [MAX98504_REG_28_PCM_TO_SPEAKER_MONOMIX] = { 0xFF, 0xFF, 0x00 },
  94. [MAX98504_REG_30_PDM_TX_ENABLES] = { 0xFF, 0xFF, 0x00 },
  95. [MAX98504_REG_31_PDM_TX_HIZ_CONTROL] = { 0xFF, 0xFF, 0x00 },
  96. [MAX98504_REG_32_PDM_TX_CONTROL] = { 0xFF, 0xFF, 0x00 },
  97. [MAX98504_REG_33_PDM_RX_ENABLE] = { 0xFF, 0xFF, 0x00 },
  98. [MAX98504_REG_34_SPEAKER_ENABLE] = { 0xFF, 0xFF, 0x00 },
  99. [MAX98504_REG_35_SPEAKER_SOURCE_SELECT] = { 0xFF, 0xFF, 0x00 },
  100. [MAX98504_REG_36_MEASUREMENT_ENABLES] = { 0xFF, 0xFF, 0x00 },
  101. [MAX98504_REG_37_ANALOGUE_INPUT_GAIN] = { 0xFF, 0xFF, 0x00 },
  102. [MAX98504_REG_38_TEMPERATURE_LIMIT_CONFIG] = { 0xFF, 0xFF, 0x00 },
  103. [MAX98504_REG_39_ANALOGUE_SPARE] = { 0xFF, 0xFF, 0x00 },
  104. [MAX98504_REG_40_GLOBAL_ENABLE] = { 0xFF, 0xFF, 0xFF },
  105. [MAX98504_REG_41_SOFTWARE_RESET] = { 0x00, 0xFF, 0xFF },
  106. };
  107. static int max98504_volatile_register
  108. (struct snd_soc_codec *codec, unsigned int reg)
  109. {
  110. if (max98504_reg_access[reg].vol) {
  111. return 1;
  112. } else {
  113. /* Mark all volatile for 2nd Ev Kit i2c master */
  114. return 0;
  115. }
  116. }
  117. static int max98504_readable(struct snd_soc_codec *codec, unsigned int reg)
  118. {
  119. if (reg >= MAX98504_REG_CNT)
  120. return 0;
  121. return max98504_reg_access[reg].read != 0;
  122. }
  123. static int max98504_reset(struct snd_soc_codec *codec)
  124. {
  125. int ret;
  126. msg_maxim("\n");
  127. /* Reset the codec by writing to this write-only reset register */
  128. ret = snd_soc_write(codec, MAX98504_REG_41_SOFTWARE_RESET,
  129. M98504_SOFTWARE_RESET_MASK);
  130. if (ret < 0) {
  131. dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
  132. return ret;
  133. }
  134. msleep(20);
  135. return ret;
  136. }
  137. #ifdef USE_MAX98504_IRQ
  138. static irqreturn_t max98504_interrupt(int irq, void *data)
  139. {
  140. struct max98504_priv *max98504 = (struct max98504_priv *) data;
  141. unsigned int mask;
  142. unsigned int flag;
  143. regmap_read(max98504->regmap, MAX98504_REG_03_INTERRUPT_ENABLES, &mask);
  144. regmap_read(max98504->regmap, MAX98504_REG_02_INTERRUPT_FLAGS, &flag);
  145. msg_maxim("flag=0x%02x mask=0x%02x -> flag=0x%02x\n",
  146. flag, mask, flag & mask);
  147. flag &= mask;
  148. if (!flag)
  149. return IRQ_NONE;
  150. /* Send work to be scheduled */
  151. if (flag & M98504_INT_GENFAIL_EN_MASK)
  152. msg_maxim("M98504_INT_GENFAIL_EN_MASK active!");
  153. if (flag & M98504_INT_AUTHDONE_EN_MASK)
  154. msg_maxim("M98504_INT_AUTHDONE_EN_MASK active!");
  155. if (flag & M98504_INT_VBATBROWN_EN_MASK)
  156. msg_maxim("M98504_INT_VBATBROWN_EN_MASK active!");
  157. if (flag & M98504_INT_WATCHFAIL_EN_MASK)
  158. msg_maxim("M98504_INT_WATCHFAIL_EN_MASK active!");
  159. if (flag & M98504_INT_THERMWARN_END_EN_MASK)
  160. msg_maxim("M98504_INT_THERMWARN_END_EN_MASK active!");
  161. if (flag & M98504_INT_THERMWARN_BGN_EN_MASK)
  162. msg_maxim("M98504_INT_THERMWARN_BGN_EN_MASK active!\n");
  163. if (flag & M98504_INT_THERMSHDN_END_EN_MASK)
  164. msg_maxim("M98504_INT_THERMSHDN_END_EN_MASK active!\n");
  165. if (flag & M98504_INT_THERMSHDN_BGN_FLAG_MASK)
  166. msg_maxim("M98504_INT_THERMSHDN_BGN_FLAG_MASK active!\n");
  167. regmap_write(max98504->regmap, MAX98504_REG_04_INTERRUPT_FLAG_CLEARS,
  168. flag&0xff);
  169. return IRQ_HANDLED;
  170. }
  171. #endif
  172. static int max98504_rxpcm_gain_set(struct snd_kcontrol *kcontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  176. unsigned int sel = ucontrol->value.integer.value[0];
  177. msg_maxim("val=%d\n", sel);
  178. snd_soc_update_bits(codec, MAX98504_REG_25_PCM_DSP_CONFIG,
  179. M98504_PCM_DSP_CFG_RX_GAIN_MASK,
  180. sel << M98504_PCM_DSP_CFG_RX_GAIN_SHIFT);
  181. return 0;
  182. }
  183. static int max98504_rxpcm_gain_get(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  187. unsigned int val = snd_soc_read(codec, MAX98504_REG_25_PCM_DSP_CONFIG);
  188. val = (val & M98504_PCM_DSP_CFG_RX_GAIN_MASK) \
  189. >> M98504_PCM_DSP_CFG_RX_GAIN_SHIFT;
  190. ucontrol->value.integer.value[0] = val;
  191. msg_maxim("val=%d\n", val);
  192. return 0;
  193. }
  194. static int max98504_ain_gain_set(struct snd_kcontrol *kcontrol,
  195. struct snd_ctl_elem_value *ucontrol)
  196. {
  197. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  198. unsigned int sel = ucontrol->value.integer.value[0];
  199. msg_maxim("val=%d\n", sel);
  200. snd_soc_update_bits(codec,
  201. MAX98504_REG_37_ANALOGUE_INPUT_GAIN,
  202. M98504_ANALOG_INPUT_GAIN_MASK,
  203. sel << M98504_ANALOG_INPUT_GAIN_SHIFT);
  204. return 0;
  205. }
  206. static int max98504_ain_gain_get(struct snd_kcontrol *kcontrol,
  207. struct snd_ctl_elem_value *ucontrol)
  208. {
  209. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  210. unsigned int val =
  211. snd_soc_read(codec, MAX98504_REG_37_ANALOGUE_INPUT_GAIN);
  212. val = (val & M98504_ANALOG_INPUT_GAIN_MASK) \
  213. >> M98504_ANALOG_INPUT_GAIN_SHIFT;
  214. ucontrol->value.integer.value[0] = val;
  215. msg_maxim("val=%d\n", val);
  216. return 0;
  217. }
  218. static const unsigned int max98504_rxpcm_gain_tlv[] = {
  219. TLV_DB_RANGE_HEAD(1),
  220. 0, 12, TLV_DB_SCALE_ITEM(0, 100, 0),
  221. };
  222. static const unsigned int max98504_ain_gain_tlv[] = {
  223. TLV_DB_RANGE_HEAD(1),
  224. 0, 1, TLV_DB_SCALE_ITEM(1200, 600, 0),
  225. };
  226. static const char * const max98504_enableddisabled_text[] =\
  227. {"Disabled", "Enabled"};
  228. static const struct soc_enum max98504_ispken_enum =
  229. SOC_ENUM_SINGLE(MAX98504_REG_36_MEASUREMENT_ENABLES,
  230. M98504_MEAS_I_EN_MASK,
  231. ARRAY_SIZE(max98504_enableddisabled_text),
  232. max98504_enableddisabled_text);
  233. static const struct soc_enum max98504_vspken_enum =
  234. SOC_ENUM_SINGLE(MAX98504_REG_36_MEASUREMENT_ENABLES,
  235. M98504_MEAS_V_EN_MASK,
  236. ARRAY_SIZE(max98504_enableddisabled_text),
  237. max98504_enableddisabled_text);
  238. static const char * const max98504_vbatbrown_code_text[] = \
  239. {"2.6V", "2.65V", "Reserved", "Reserved",
  240. "Reserved", "Reserved", "Reserved", "Reserved",
  241. "Reserved", "Reserved", "Reserved", "Reserved",
  242. "Reserved", "Reserved", "Reserved", "Reserved",
  243. "Reserved", "Reserved", "Reserved", "Reserved",
  244. "Reserved", "Reserved", "Reserved", "Reserved",
  245. "Reserved", "Reserved", "Reserved", "Reserved",
  246. "Reserved", "Reserved", "Reserved", "3.7V"};
  247. static const struct soc_enum max98504_brownout_code_enum =
  248. SOC_ENUM_SINGLE(MAX98504_REG_17_PVDD_BROWNOUT_CONFIG_1,
  249. M98504_PVDD_BROWNOUT_CFG1_CODE_SHIFT, 31, max98504_vbatbrown_code_text);
  250. static const char * const max98504_vbatbrown_max_atten_text[] =\
  251. {"0dB", "1dB", "2dB", "3dB", "4dB", "5dB", "6dB"};
  252. static const struct soc_enum max98504_brownout_max_atten_enum =
  253. SOC_ENUM_SINGLE(MAX98504_REG_17_PVDD_BROWNOUT_CONFIG_1,
  254. M98504_PVDD_BROWNOUT_CFG1_MAX_ATTEN_SHIFT,
  255. 6, max98504_vbatbrown_max_atten_text);
  256. static const char * const max98504_flt_mode_text[] = {"Voice", "Music"};
  257. static const struct soc_enum max98504_pcm_rx_flt_mode_enum =
  258. SOC_ENUM_SINGLE(MAX98504_REG_25_PCM_DSP_CONFIG,
  259. M98504_PCM_DSP_CFG_RX_FLT_MODE_SHIFT,
  260. 1, max98504_flt_mode_text);
  261. static const char * const max98504_pcm_bsel_text[] =\
  262. {"Reserved", "Reserved", "32", "48", "64",\
  263. "Reserved", "128", "Reserved", "256"};
  264. static const struct soc_enum max98504_pcm_bsel_enum =
  265. SOC_ENUM_SINGLE(MAX98504_REG_26_PCM_CLOCK_SETUP,
  266. M98504_PCM_CLK_SETUP_BSEL_SHIFT, 8, max98504_pcm_bsel_text);
  267. static int max98504_set_speaker(struct snd_kcontrol *kcontrol,
  268. struct snd_ctl_elem_value *ucontrol) {
  269. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  270. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  271. uint32_t OnOff;
  272. OnOff = ucontrol->value.integer.value[0];
  273. msg_maxim("%s, OnOff:%d\n", __func__, OnOff);
  274. if (OnOff) {
  275. snd_soc_update_bits(codec, MAX98504_REG_34_SPEAKER_ENABLE,
  276. M98504_SPK_EN_MASK, M98504_SPK_EN_MASK);
  277. snd_soc_update_bits(codec, MAX98504_REG_40_GLOBAL_ENABLE,
  278. M98504_GLOBAL_EN_MASK, M98504_GLOBAL_EN_MASK);
  279. max98504->status = 1;
  280. } else {
  281. snd_soc_update_bits(codec, MAX98504_REG_40_GLOBAL_ENABLE,
  282. M98504_GLOBAL_EN_MASK, 0);
  283. snd_soc_update_bits(codec, MAX98504_REG_34_SPEAKER_ENABLE,
  284. M98504_SPK_EN_MASK, 0);
  285. max98504->status = 0;
  286. }
  287. return 0;
  288. }
  289. static int max98504_get_speaker(struct snd_kcontrol *kcontrol,
  290. struct snd_ctl_elem_value *ucontrol) {
  291. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  292. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  293. if (max98504->status > 0)
  294. ucontrol->value.integer.value[0] = 1;
  295. else
  296. ucontrol->value.integer.value[0] = 0;
  297. msg_maxim("%s, OnOff:%d\n", __func__,
  298. (int)ucontrol->value.integer.value[0]);
  299. return 0;
  300. }
  301. static const char * const spk_state_text[] = {"Disable", "Enable"};
  302. static const struct soc_enum spk_state_enum[] = {
  303. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_state_text), spk_state_text),
  304. };
  305. #ifdef USE_DSM_LOG
  306. #define DEFAULT_LOG_CLASS_NAME "dsm"
  307. static const char *class_name_log;
  308. static int max98504_get_dump_status(struct snd_kcontrol *kcontrol,
  309. struct snd_ctl_elem_value *ucontrol)
  310. {
  311. ucontrol->value.integer.value[0] = maxdsm_get_dump_status();
  312. return 0;
  313. }
  314. static int max98504_set_dump_status(struct snd_kcontrol *kcontrol,
  315. struct snd_ctl_elem_value *ucontrol)
  316. {
  317. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  318. unsigned int val = snd_soc_read(codec, MAX98504_REG_40_GLOBAL_ENABLE);
  319. if (val != 0)
  320. maxdsm_update_param();
  321. else
  322. msg_maxim("val:%d\n", val);
  323. return 0;
  324. }
  325. static ssize_t max98504_log_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. return maxdsm_log_prepare(buf);
  329. }
  330. static DEVICE_ATTR(dsm_log, S_IRUGO, max98504_log_show, NULL);
  331. static struct attribute *max98504_attributes[] = {
  332. &dev_attr_dsm_log.attr,
  333. NULL
  334. };
  335. static struct attribute_group max98504_attribute_group = {
  336. .attrs = max98504_attributes
  337. };
  338. #endif
  339. static const struct snd_kcontrol_new max98504_snd_controls[] = {
  340. SOC_SINGLE("GPIO Pin Switch", MAX98504_REG_10_GPIO_ENABLE
  341. , M98504_GPIO_ENALBE_SHIFT, 1, 0),
  342. SOC_SINGLE("Watchdog Enable Switch", MAX98504_REG_12_WATCHDOG_ENABLE
  343. , M98504_WDOG_ENABLE_SHIFT, 1, 0),
  344. SOC_SINGLE("Watchdog Config Switch", MAX98504_REG_13_WATCHDOG_CONFIG
  345. , M98504_WDOG_CONFIG_SHIFT, 3, 0),
  346. SOC_SINGLE("Watchdog Clear Switch", MAX98504_REG_14_WATCHDOG_CLEAR
  347. , M98504_WDOG_CLEAR_SHIFT, 0xe9, 0),
  348. SOC_SINGLE("Clock Monitor Switch", MAX98504_REG_15_CLOCK_MONITOR_ENABLE
  349. , M98504_CMON_ENA_SHIFT, 1, 0),
  350. SOC_SINGLE("Brownout Protection Switch",
  351. MAX98504_REG_16_PVDD_BROWNOUT_ENABLE,
  352. M98504_CMON_ENA_SHIFT, 1, 0),
  353. SOC_ENUM("Brownout Threshold", max98504_brownout_code_enum),
  354. SOC_ENUM("Brownout Attenuation Value",
  355. max98504_brownout_max_atten_enum),
  356. SOC_SINGLE("Brownout Attack Hold Time",
  357. MAX98504_REG_18_PVDD_BROWNOUT_CONFIG_2,
  358. M98504_PVDD_BROWNOUT_CFG2_ATTK_HOLD_SHIFT, 255, 0),
  359. SOC_SINGLE("Brownout Timed Hold",
  360. MAX98504_REG_19_PVDD_BROWNOUT_CONFIG_3,
  361. M98504_PVDD_BROWNOUT_CFG3_TIMED_HOLD_SHIFT, 255, 0),
  362. SOC_SINGLE("Brownout Release", MAX98504_REG_1A_PVDD_BROWNOUT_CONFIG_4,
  363. M98504_PVDD_BROWNOUT_CFG4_RELEASE_SHIFT, 255, 0),
  364. SOC_SINGLE("PCM BCLK Edge", MAX98504_REG_24_PCM_MODE_CONFIG,
  365. M98504_PCM_MODE_CFG_BCLKEDGE_SHIFT, 1, 0),
  366. SOC_SINGLE("PCM Channel Select", MAX98504_REG_24_PCM_MODE_CONFIG,
  367. M98504_PCM_MODE_CFG_CHSEL_SHIFT, 1, 0),
  368. SOC_SINGLE("PCM Transmit Extra HiZ Switch",
  369. MAX98504_REG_24_PCM_MODE_CONFIG,
  370. M98504_PCM_MODE_CFG_TX_EXTRA_HIZ_SHIFT, 1, 0),
  371. SOC_SINGLE("PCM Output Dither Switch", MAX98504_REG_25_PCM_DSP_CONFIG,
  372. M98504_PCM_DSP_CFG_TX_DITH_EN_SHIFT, 1, 0),
  373. SOC_SINGLE("PCM Measurement DC Blocking Filter Switch",
  374. MAX98504_REG_25_PCM_DSP_CONFIG,
  375. M98504_PCM_DSP_CFG_MEAS_DCBLK_EN_SHIFT, 1, 0),
  376. SOC_SINGLE("PCM Input Dither Switch", MAX98504_REG_25_PCM_DSP_CONFIG,
  377. M98504_PCM_DSP_CFG_RX_DITH_EN_SHIFT, 1, 0),
  378. SOC_ENUM("PCM Output Filter Mode", max98504_pcm_rx_flt_mode_enum),
  379. SOC_SINGLE_EXT_TLV("PCM Rx Gain", MAX98504_REG_25_PCM_DSP_CONFIG,
  380. M98504_PCM_DSP_CFG_RX_GAIN_SHIFT,
  381. M98504_PCM_DSP_CFG_RX_GAIN_WIDTH - 1,
  382. 1, max98504_rxpcm_gain_get, max98504_rxpcm_gain_set,
  383. max98504_rxpcm_gain_tlv),
  384. SOC_ENUM("PCM BCLK rate", max98504_pcm_bsel_enum),
  385. SOC_ENUM("Speaker Current Sense Enable", max98504_ispken_enum),
  386. SOC_ENUM("Speaker Voltage Sense Enable", max98504_vspken_enum),
  387. SOC_SINGLE_EXT_TLV("AIN Gain", MAX98504_REG_37_ANALOGUE_INPUT_GAIN,
  388. M98504_ANALOG_INPUT_GAIN_SHIFT,
  389. M98504_ANALOG_INPUT_GAIN_WIDTH - 1,
  390. 1, max98504_ain_gain_get, max98504_ain_gain_set,
  391. max98504_ain_gain_tlv),
  392. SOC_SINGLE("AUTH_STATUS", MAX98504_REG_01_INTERRUPT_STATUS,
  393. 0, M98504_INT_INTERRUPT_STATUS_MASK, 0),
  394. SOC_ENUM_EXT("SPK out", spk_state_enum[0],
  395. max98504_get_speaker, max98504_set_speaker),
  396. #ifdef USE_DSM_LOG
  397. SOC_SINGLE_EXT("DSM LOG", SND_SOC_NOPM, 0, 3, 0,
  398. max98504_get_dump_status, max98504_set_dump_status),
  399. #endif
  400. };
  401. #ifdef MAX98504_USE_DAPM
  402. static const char * const spk_src_mux_text[] =\
  403. {"PCM", "AIN", "PDM_CH0", "PDM_CH1"};
  404. static const struct soc_enum spk_src_mux_enum =
  405. SOC_ENUM_SINGLE(MAX98504_REG_35_SPEAKER_SOURCE_SELECT,
  406. M98504_SPK_SRC_SEL_SHIFT,
  407. ARRAY_SIZE(spk_src_mux_text), spk_src_mux_text);
  408. static const struct snd_kcontrol_new max98504_spk_src_mux =
  409. SOC_DAPM_ENUM("SPK_SRC Mux", spk_src_mux_enum);
  410. static const char * const digital_mono_mux_text[] = {"CH0", "CH1", "CHMIX"};
  411. static const struct soc_enum digital_mono_mux_enum =
  412. SOC_ENUM_SINGLE(MAX98504_REG_28_PCM_TO_SPEAKER_MONOMIX,
  413. M98504_PCM_TO_SPK_MONOMIX_CFG_SHIFT,
  414. ARRAY_SIZE(digital_mono_mux_text), digital_mono_mux_text);
  415. static const struct snd_kcontrol_new max98504_digital_mono_mux =
  416. SOC_DAPM_ENUM("DAC_MONOMIX Mux", digital_mono_mux_enum);
  417. static const struct snd_soc_dapm_widget max98504_dapm_widgets[] = {
  418. SND_SOC_DAPM_SUPPLY("SHDN", MAX98504_REG_40_GLOBAL_ENABLE,
  419. M98504_GLOBAL_EN_SHIFT, 0, NULL, 0),
  420. SND_SOC_DAPM_INPUT("Voltage Data"),
  421. SND_SOC_DAPM_INPUT("Current Data"),
  422. SND_SOC_DAPM_INPUT("Analog Input"),
  423. SND_SOC_DAPM_ADC("ADCL", NULL, MAX98504_REG_36_MEASUREMENT_ENABLES,
  424. M98504_MEAS_V_EN_SHIFT, 0),
  425. SND_SOC_DAPM_ADC("ADCR", NULL, MAX98504_REG_36_MEASUREMENT_ENABLES,
  426. M98504_MEAS_I_EN_SHIFT, 0),
  427. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  428. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "HiFi Capture", 1, SND_SOC_NOPM, 0, 0),
  429. SND_SOC_DAPM_AIF_IN("AIFINL", "HiFi Playback", 0, SND_SOC_NOPM, 0, 0),
  430. SND_SOC_DAPM_AIF_IN("AIFINR", "HiFi Playback", 1, SND_SOC_NOPM, 0, 0),
  431. SND_SOC_DAPM_DAC("DACL", NULL, MAX98504_REG_20_PCM_RX_ENABLES,
  432. M98504_PCM_RX_EN_CH0_SHIFT, 0),
  433. SND_SOC_DAPM_DAC("DACR", NULL, MAX98504_REG_20_PCM_RX_ENABLES,
  434. M98504_PCM_RX_EN_CH1_SHIFT, 0),
  435. SND_SOC_DAPM_MUX("DAC Mono Mux", SND_SOC_NOPM, 0, 0,
  436. &max98504_digital_mono_mux),
  437. SND_SOC_DAPM_MUX("SPK Source Mux", SND_SOC_NOPM, 0, 0,
  438. &max98504_spk_src_mux),
  439. SND_SOC_DAPM_PGA("SPK Mono Out", MAX98504_REG_34_SPEAKER_ENABLE,
  440. M98504_SPK_EN_SHIFT, 0, NULL, 0),
  441. SND_SOC_DAPM_OUTPUT("SPKOUT"),
  442. };
  443. static const struct snd_soc_dapm_route max98504_audio_map[] = {
  444. {"ADCL", NULL, "Voltage Data"},
  445. {"ADCR", NULL, "Current Data"},
  446. {"AIFOUTL", NULL, "ADCL"},
  447. {"AIFOUTR", NULL, "ADCR"},
  448. {"AIFOUTL", NULL, "SHDN"},
  449. {"AIFOUTR", NULL, "SHDN"},
  450. {"AIFINL", NULL, "SHDN"},
  451. {"AIFINR", NULL, "SHDN"},
  452. {"DAC Mono Mux", "CH0", "DACL"},
  453. {"DAC Mono Mux", "CH1", "DACR"},
  454. {"DAC Mono Mux", "CHMIX", "DACL"},
  455. {"DAC Mono Mux", "CHMIX", "DACR"},
  456. {"SPK Source Mux", "PCM", "DAC Mono Mux"},
  457. {"SPK Source Mux", "AIN", "Analog Input"},
  458. {"SPK Mono Out", NULL, "SPK Source Mux"},
  459. {"SPKOUT", NULL, "SPK Mono Out"},
  460. };
  461. #endif
  462. static int max98504_add_widgets(struct snd_soc_codec *codec)
  463. {
  464. msg_maxim("\n");
  465. snd_soc_add_codec_controls(codec, max98504_snd_controls,
  466. ARRAY_SIZE(max98504_snd_controls));
  467. return 0;
  468. }
  469. /* codec sample rate config parameter table */
  470. static const struct {
  471. u32 rate;
  472. u8 sr;
  473. } rate_table[] = {
  474. {8000, (0)},
  475. {11025, (1)},
  476. {12000, (2)},
  477. {16000, (3)},
  478. {22050, (4)},
  479. {24000, (5)},
  480. {32000, (6)},
  481. {44100, (7)},
  482. {48000, (8)},
  483. };
  484. static inline int rate_value(int rate, u8 *value)
  485. {
  486. int i;
  487. for (i = 0; i < ARRAY_SIZE(rate_table); i++) {
  488. if (rate_table[i].rate >= rate) {
  489. *value = rate_table[i].sr;
  490. return 0;
  491. }
  492. }
  493. *value = rate_table[0].sr;
  494. return -EINVAL;
  495. }
  496. /* #define TDM */
  497. static int max98504_set_tdm_slot(struct snd_soc_dai *codec_dai,
  498. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  499. {
  500. return 0;
  501. }
  502. static int max98504_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  503. {
  504. struct snd_soc_codec *codec = codec_dai->codec;
  505. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  506. struct max98504_cdata *cdata;
  507. u8 regval;
  508. msg_maxim("\n");
  509. cdata = &max98504->dai[0];
  510. if (fmt != cdata->fmt) {
  511. cdata->fmt = fmt;
  512. regval = 0;
  513. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  514. case SND_SOC_DAIFMT_CBS_CFS:
  515. case SND_SOC_DAIFMT_CBM_CFM:
  516. break;
  517. case SND_SOC_DAIFMT_CBS_CFM:
  518. case SND_SOC_DAIFMT_CBM_CFS:
  519. default:
  520. dev_err(codec->dev,
  521. "DAI clock mode unsupported");
  522. return -EINVAL;
  523. }
  524. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  525. case SND_SOC_DAIFMT_I2S:
  526. snd_soc_update_bits(codec,
  527. MAX98504_REG_24_PCM_MODE_CONFIG,
  528. M98504_PCM_MODE_CFG_FORMAT_MASK,
  529. M98504_PCM_MODE_CFG_FORMAT_I2S_MASK);
  530. break;
  531. case SND_SOC_DAIFMT_LEFT_J:
  532. snd_soc_update_bits(codec,
  533. MAX98504_REG_24_PCM_MODE_CONFIG,
  534. M98504_PCM_MODE_CFG_FORMAT_MASK,
  535. M98504_PCM_MODE_CFG_FORMAT_LJ_MASK);
  536. break;
  537. case SND_SOC_DAIFMT_RIGHT_J:
  538. snd_soc_update_bits(codec,
  539. MAX98504_REG_24_PCM_MODE_CONFIG,
  540. M98504_PCM_MODE_CFG_FORMAT_MASK,
  541. M98504_PCM_MODE_CFG_FORMAT_RJ_MASK);
  542. break;
  543. case SND_SOC_DAIFMT_DSP_A:
  544. /* Not supported mode */
  545. default:
  546. dev_err(codec->dev,
  547. "DAI format unsupported, fmt:0x%d"
  548. , fmt);
  549. return -EINVAL;
  550. }
  551. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  552. case SND_SOC_DAIFMT_NB_NF:
  553. case SND_SOC_DAIFMT_NB_IF:
  554. break;
  555. case SND_SOC_DAIFMT_IB_NF:
  556. case SND_SOC_DAIFMT_IB_IF:
  557. break;
  558. default:
  559. dev_err(codec->dev,
  560. "DAI invert mode unsupported");
  561. return -EINVAL;
  562. }
  563. snd_soc_write(codec, MAX98504_REG_26_PCM_CLOCK_SETUP, 0);
  564. }
  565. return 0;
  566. }
  567. #ifdef MAX98504_USE_DAPM
  568. static int max98504_set_bias_level(struct snd_soc_codec *codec,
  569. enum snd_soc_bias_level level)
  570. {
  571. int ret;
  572. msg_maxim("level=%d\n", level);
  573. switch (level) {
  574. case SND_SOC_BIAS_ON:
  575. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  576. ret = snd_soc_cache_sync(codec);
  577. if (ret != 0) {
  578. dev_err(codec->dev,
  579. "Failed to sync cache: %d\n"
  580. , ret);
  581. return ret;
  582. }
  583. }
  584. snd_soc_update_bits(codec,
  585. MAX98504_REG_40_GLOBAL_ENABLE,
  586. M98504_GLOBAL_EN_MASK, M98504_GLOBAL_EN_MASK);
  587. break;
  588. case SND_SOC_BIAS_PREPARE:
  589. break;
  590. case SND_SOC_BIAS_STANDBY:
  591. case SND_SOC_BIAS_OFF:
  592. snd_soc_update_bits(codec,
  593. MAX98504_REG_40_GLOBAL_ENABLE,
  594. M98504_GLOBAL_EN_MASK, 0x00);
  595. codec->cache_sync = 1;
  596. break;
  597. }
  598. codec->dapm.bias_level = level;
  599. return 0;
  600. }
  601. #endif
  602. static int max98504_dai_hw_params(struct snd_pcm_substream *substream,
  603. struct snd_pcm_hw_params *params,
  604. struct snd_soc_dai *dai)
  605. {
  606. struct snd_soc_codec *codec = dai->codec;
  607. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  608. struct max98504_cdata *cdata;
  609. unsigned int rate;
  610. u8 regval;
  611. msg_maxim("\n");
  612. cdata = &max98504->dai[0];
  613. rate = params_rate(params);
  614. switch (params_format(params)) {
  615. case SNDRV_PCM_FORMAT_S8:
  616. snd_soc_update_bits(codec,
  617. MAX98504_REG_24_PCM_MODE_CONFIG,
  618. M98504_PCM_MODE_CFG_CH_SIZE_MASK,
  619. M98504_PCM_MODE_CFG_CH_SIZE_8_MASK);
  620. break;
  621. case SNDRV_PCM_FORMAT_S16_LE:
  622. snd_soc_update_bits(codec,
  623. MAX98504_REG_24_PCM_MODE_CONFIG,
  624. M98504_PCM_MODE_CFG_CH_SIZE_MASK,
  625. M98504_PCM_MODE_CFG_CH_SIZE_16_MASK);
  626. break;
  627. case SNDRV_PCM_FORMAT_S24_LE:
  628. snd_soc_update_bits(codec,
  629. MAX98504_REG_24_PCM_MODE_CONFIG,
  630. M98504_PCM_MODE_CFG_CH_SIZE_MASK,
  631. M98504_PCM_MODE_CFG_CH_SIZE_24_MASK);
  632. break;
  633. case SNDRV_PCM_FORMAT_S32_LE:
  634. snd_soc_update_bits(codec,
  635. MAX98504_REG_24_PCM_MODE_CONFIG,
  636. M98504_PCM_MODE_CFG_CH_SIZE_MASK,
  637. M98504_PCM_MODE_CFG_CH_SIZE_32_MASK);
  638. break;
  639. default:
  640. return -EINVAL;
  641. }
  642. if (rate_value(rate, &regval))
  643. return -EINVAL;
  644. /* Update sample rate mode */
  645. snd_soc_update_bits(codec, MAX98504_REG_27_PCM_SAMPLE_RATE_SETUP,
  646. M98504_PCM_SR_SETUP_SPK_SR_MASK,
  647. regval<<M98504_PCM_SR_SETUP_SPK_SR_SHIFT);
  648. snd_soc_update_bits(codec, MAX98504_REG_27_PCM_SAMPLE_RATE_SETUP,
  649. M98504_PCM_SR_SETUP_MEAS_SR_MASK,
  650. regval<<M98504_PCM_SR_SETUP_MEAS_SR_SHIFT);
  651. return 0;
  652. }
  653. static int max98504_dai_set_sysclk(struct snd_soc_dai *dai,
  654. int clk_id, unsigned int freq, int dir)
  655. {
  656. struct snd_soc_codec *codec = dai->codec;
  657. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  658. msg_maxim("clk_id;%d, freq:%d, dir:%d\n", clk_id, freq, dir);
  659. /* Requested clock frequency is already setup */
  660. if (freq == max98504->sysclk)
  661. return 0;
  662. max98504->sysclk = freq;
  663. return 0;
  664. }
  665. #ifdef MAX98504_USE_DAPM
  666. static int max98504_dai_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  667. {
  668. struct snd_soc_codec *codec = codec_dai->codec;
  669. msg_maxim("- mute:%d\n", mute);
  670. if (mute) {
  671. snd_soc_update_bits(codec, MAX98504_REG_34_SPEAKER_ENABLE,
  672. M98504_SPK_EN_MASK, 0);
  673. } else {
  674. snd_soc_update_bits(codec, MAX98504_REG_34_SPEAKER_ENABLE,
  675. M98504_SPK_EN_MASK, M98504_SPK_EN_MASK);
  676. }
  677. return 0;
  678. }
  679. #endif
  680. #define MAX98504_RATES SNDRV_PCM_RATE_8000_48000
  681. #define MAX98504_FORMATS \
  682. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE\
  683. | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  684. static struct snd_soc_dai_ops max98504_dai_ops = {
  685. .set_sysclk = max98504_dai_set_sysclk,
  686. .set_fmt = max98504_dai_set_fmt,
  687. .set_tdm_slot = max98504_set_tdm_slot,
  688. .hw_params = max98504_dai_hw_params,
  689. #ifdef MAX98504_USE_DAPM
  690. .digital_mute = max98504_dai_digital_mute,
  691. #endif
  692. };
  693. static struct snd_soc_dai_driver max98504_dai[] = {
  694. {
  695. .name = "max98504-aif1",
  696. .playback = {
  697. .stream_name = "HiFi Playback",
  698. .channels_min = 1,
  699. .channels_max = 2,
  700. .rates = MAX98504_RATES,
  701. .formats = MAX98504_FORMATS,
  702. },
  703. .capture = {
  704. .stream_name = "HiFi Capture",
  705. .channels_min = 1,
  706. .channels_max = 2,
  707. .rates = MAX98504_RATES,
  708. .formats = MAX98504_FORMATS,
  709. },
  710. .ops = &max98504_dai_ops,
  711. }
  712. };
  713. static void max98504_handle_pdata(struct snd_soc_codec *codec)
  714. {
  715. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  716. struct max98504_pdata *pdata = max98504->pdata;
  717. struct max98504_cfg_data *cfg_data = &pdata->cfg_data;
  718. u8 regval;
  719. msg_maxim("\n");
  720. if (!pdata) {
  721. dev_dbg(codec->dev, "No platform data\n");
  722. return;
  723. }
  724. /* Configure Rx Mode */
  725. if (pdata->rx_mode == MODE_RX_PCM) {
  726. regval = 0;
  727. if (cfg_data->rx_dither_en)
  728. regval |= M98504_PCM_DSP_CFG_RX_DITH_EN_MASK;
  729. if (cfg_data->rx_flt_mode)
  730. regval |= M98504_PCM_DSP_CFG_RX_FLT_MODE_MASK;
  731. snd_soc_update_bits(codec, MAX98504_REG_25_PCM_DSP_CONFIG,
  732. M98504_PCM_DSP_CFG_RX_DITH_EN_MASK|\
  733. M98504_PCM_DSP_CFG_RX_FLT_MODE_MASK, regval);
  734. snd_soc_write(codec, MAX98504_REG_20_PCM_RX_ENABLES,
  735. (u8)cfg_data->rx_ch_en);
  736. } else if (pdata->rx_mode == MODE_RX_PDM0 || \
  737. pdata->rx_mode == MODE_RX_PDM1) {
  738. snd_soc_write(codec, MAX98504_REG_33_PDM_RX_ENABLE,
  739. M98504_PDM_RX_EN_MASK);
  740. }
  741. snd_soc_write(codec, MAX98504_REG_35_SPEAKER_SOURCE_SELECT,
  742. (u8) (M98504_SPK_SRC_SEL_MASK & pdata->rx_mode));
  743. /* Configure Tx Mode */
  744. if (pdata->tx_mode == MODE_TX_PCM) {
  745. regval = 0;
  746. if (cfg_data->tx_dither_en)
  747. regval |= M98504_PCM_DSP_CFG_TX_DITH_EN_MASK;
  748. if (cfg_data->meas_dc_block_en)
  749. regval |= M98504_PCM_DSP_CFG_MEAS_DCBLK_EN_MASK;
  750. snd_soc_update_bits(codec, MAX98504_REG_25_PCM_DSP_CONFIG,
  751. M98504_PCM_DSP_CFG_TX_DITH_EN_MASK|\
  752. M98504_PCM_DSP_CFG_MEAS_DCBLK_EN_MASK, regval);
  753. snd_soc_write(codec, MAX98504_REG_21_PCM_TX_ENABLES,
  754. (u8)cfg_data->tx_ch_en);
  755. snd_soc_write(codec, MAX98504_REG_22_PCM_TX_HIZ_CONTROL,
  756. (u8)cfg_data->tx_hiz_ch_en);
  757. snd_soc_write(codec, MAX98504_REG_23_PCM_TX_CHANNEL_SOURCES,
  758. (u8)cfg_data->tx_ch_src);
  759. } else {
  760. snd_soc_write(codec, MAX98504_REG_30_PDM_TX_ENABLES,
  761. (u8)cfg_data->tx_ch_en);
  762. snd_soc_write(codec, MAX98504_REG_31_PDM_TX_HIZ_CONTROL,
  763. (u8)cfg_data->tx_hiz_ch_en);
  764. snd_soc_write(codec, MAX98504_REG_32_PDM_TX_CONTROL,
  765. (u8)cfg_data->tx_ch_src);
  766. }
  767. #ifndef MAX98504_USE_DAPM
  768. snd_soc_write(codec, MAX98504_REG_36_MEASUREMENT_ENABLES,
  769. M98504_MEAS_I_EN_MASK | M98504_MEAS_V_EN_MASK);
  770. #endif
  771. }
  772. static int max98504_suspend(struct snd_soc_codec *codec)
  773. {
  774. msg_maxim("\n");
  775. return 0;
  776. }
  777. static int max98504_resume(struct snd_soc_codec *codec)
  778. {
  779. msg_maxim("\n");
  780. return 0;
  781. }
  782. static int max98504_probe(struct snd_soc_codec *codec)
  783. {
  784. struct max98504_priv *max98504 = snd_soc_codec_get_drvdata(codec);
  785. struct max98504_cdata *cdata;
  786. int ret = 0;
  787. msg_maxim("\n");
  788. max98504->codec = codec;
  789. codec->cache_sync = 1;
  790. ret = snd_soc_codec_set_cache_io(codec, 16, 8, SND_SOC_I2C);
  791. if (ret != 0) {
  792. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  793. return ret;
  794. }
  795. /* reset the codec, the DSP core, and disable all interrupts */
  796. ret = max98504_reset(codec);
  797. if (ret < 0)
  798. goto err_access;
  799. /* initialize private data */
  800. max98504->sysclk = (unsigned)-1;
  801. cdata = &max98504->dai[0];
  802. cdata->rate = (unsigned)-1;
  803. cdata->fmt = (unsigned)-1;
  804. ret = snd_soc_read(codec, MAX98504_REG_7FFF_REV_ID);
  805. if (ret < 0) {
  806. dev_err(codec->dev, "Failed to read device revision: %d\n",
  807. ret);
  808. goto err_access;
  809. }
  810. msg_maxim("REV ID=0x%d\n", ret);
  811. snd_soc_write(codec, MAX98504_REG_16_PVDD_BROWNOUT_ENABLE, 0x1);
  812. snd_soc_write(codec, MAX98504_REG_17_PVDD_BROWNOUT_CONFIG_1, 0x3);
  813. snd_soc_write(codec, MAX98504_REG_18_PVDD_BROWNOUT_CONFIG_2, 0x64);
  814. snd_soc_write(codec, MAX98504_REG_19_PVDD_BROWNOUT_CONFIG_3, 0xff);
  815. snd_soc_write(codec, MAX98504_REG_1A_PVDD_BROWNOUT_CONFIG_4, 0xff);
  816. max98504_handle_pdata(codec);
  817. max98504_add_widgets(codec);
  818. #ifdef USE_DSM_LOG
  819. if (class_name_log != NULL)
  820. max98504->dev_log_class =
  821. class_create(THIS_MODULE, class_name_log);
  822. if (max98504->dev_log_class == NULL) {
  823. pr_err("%s: class_create fail.\n", __func__);
  824. } else {
  825. max98504->dev_log = device_create(max98504->dev_log_class, NULL,
  826. 1, NULL, "max98504");
  827. if (IS_ERR(max98504->dev_log)) {
  828. ret = sysfs_create_group(&codec->dev->kobj,
  829. &max98504_attribute_group);
  830. if (ret)
  831. msg_maxim(\
  832. "failed to create sysfs group [%d]", ret);
  833. } else {
  834. ret = sysfs_create_group(&max98504->dev_log->kobj,
  835. &max98504_attribute_group);
  836. if (ret)
  837. msg_maxim("failed to create sysfs group [%d]",
  838. ret);
  839. }
  840. }
  841. #endif
  842. msg_maxim("done.");
  843. err_access:
  844. return ret;
  845. }
  846. static int max98504_remove(struct snd_soc_codec *codec)
  847. {
  848. msg_maxim("\n");
  849. return 0;
  850. }
  851. static struct snd_soc_codec_driver soc_codec_dev_max98504 = {
  852. .probe = max98504_probe,
  853. .remove = max98504_remove,
  854. .suspend = max98504_suspend,
  855. .resume = max98504_resume,
  856. #ifdef MAX98504_USE_DAPM
  857. .set_bias_level = max98504_set_bias_level,
  858. #endif
  859. .reg_cache_size = ARRAY_SIZE(max98504_reg_def),
  860. .reg_word_size = sizeof(u8),
  861. .reg_cache_default = max98504_reg_def,
  862. .readable_register = max98504_readable,
  863. .volatile_register = max98504_volatile_register,
  864. #ifdef MAX98504_USE_DAPM
  865. .dapm_widgets = max98504_dapm_widgets,
  866. .num_dapm_widgets = ARRAY_SIZE(max98504_dapm_widgets),
  867. .dapm_routes = max98504_audio_map,
  868. .num_dapm_routes = ARRAY_SIZE(max98504_audio_map),
  869. #endif
  870. };
  871. static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
  872. {
  873. return (regulator_count_voltages(reg) > 0) ?
  874. regulator_set_optimum_mode(reg, load_uA) : 0;
  875. }
  876. static int max98504_regulator_config(struct i2c_client *i2c,
  877. bool pullup, bool on)
  878. {
  879. struct regulator *max98504_vcc_i2c;
  880. int rc;
  881. #define VCC_I2C_MIN_UV 1800000
  882. #define VCC_I2C_MAX_UV 1800000
  883. #define I2C_LOAD_UA 300000
  884. msg_maxim("pullup=%d\n", pullup);
  885. if (pullup) {
  886. max98504_vcc_i2c = regulator_get(&i2c->dev, "vcc_i2c");
  887. if (IS_ERR(max98504_vcc_i2c)) {
  888. rc = PTR_ERR(max98504_vcc_i2c);
  889. pr_err("Regulator get failed rc=%d\n", rc);
  890. return rc;
  891. }
  892. if (regulator_count_voltages(max98504_vcc_i2c) > 0) {
  893. rc = regulator_set_voltage(max98504_vcc_i2c,
  894. VCC_I2C_MIN_UV, VCC_I2C_MAX_UV);
  895. if (rc) {
  896. pr_err("regulator set_vtg failed rc=%d\n", rc);
  897. goto error_set_vtg_i2c;
  898. }
  899. }
  900. rc = reg_set_optimum_mode_check(max98504_vcc_i2c, I2C_LOAD_UA);
  901. if (rc < 0) {
  902. pr_err("Regulator vcc_i2c set_opt failed rc=%d\n", rc);
  903. goto error_reg_opt_i2c;
  904. }
  905. rc = regulator_enable(max98504_vcc_i2c);
  906. if (rc) {
  907. pr_err("Regulator vcc_i2c enable failed rc=%d\n", rc);
  908. goto error_reg_en_vcc_i2c;
  909. }
  910. }
  911. return 0;
  912. error_reg_en_vcc_i2c:
  913. if (pullup)
  914. reg_set_optimum_mode_check(max98504_vcc_i2c, 0);
  915. error_reg_opt_i2c:
  916. regulator_disable(max98504_vcc_i2c);
  917. error_set_vtg_i2c:
  918. regulator_put(max98504_vcc_i2c);
  919. return rc;
  920. }
  921. static int max98504_i2c_probe(struct i2c_client *i2c,
  922. const struct i2c_device_id *id)
  923. {
  924. struct max98504_priv *max98504;
  925. struct max98504_pdata *pdata;
  926. int ret;
  927. msg_maxim("\n");
  928. max98504_regulator_config(i2c, of_property_read_bool(i2c->dev.of_node,
  929. "max98504,i2c-pull-up"), 1);
  930. max98504 = kzalloc(sizeof(struct max98504_priv), GFP_KERNEL);
  931. if (max98504 == NULL)
  932. return -ENOMEM;
  933. max98504->devtype = id->driver_data;
  934. i2c_set_clientdata(i2c, max98504);
  935. max98504->control_data = i2c;
  936. max98504->pdata = devm_kzalloc(&i2c->dev,
  937. sizeof(struct max98504_pdata), GFP_KERNEL);
  938. if (!max98504->pdata) {
  939. dev_err(&i2c->dev, "Failed to allocate memory\n");
  940. return -ENOMEM;
  941. } else
  942. pdata = max98504->pdata;
  943. if (i2c->dev.of_node) {
  944. #ifdef USE_MAX98504_IRQ
  945. pdata->irq = of_get_named_gpio_flags(i2c->dev.of_node,
  946. "max98504,irq-gpio",
  947. 0, NULL);
  948. #endif
  949. ret = of_property_read_u32(i2c->dev.of_node, "max98504,rx_mode",
  950. &pdata->rx_mode);
  951. if (ret) {
  952. dev_err(&i2c->dev, "Failed to read rx_mode.\n");
  953. return -EINVAL;
  954. }
  955. ret = of_property_read_u32(i2c->dev.of_node, "max98504,tx_mode",
  956. &pdata->tx_mode);
  957. if (ret) {
  958. dev_err(&i2c->dev, "Failed to read tx_mode.\n");
  959. return -EINVAL;
  960. }
  961. ret = of_property_read_u32_array(i2c->dev.of_node,
  962. "max98504,cfg_data",
  963. (u32 *)&pdata->cfg_data,
  964. sizeof(struct max98504_cfg_data)/sizeof(u32));
  965. if (ret) {
  966. dev_err(&i2c->dev, "Failed to read cfg_data.\n");
  967. return -EINVAL;
  968. }
  969. #ifdef USE_DSM_LOG
  970. class_name_log = NULL;
  971. ret = of_property_read_string(i2c->dev.of_node,
  972. "max98504,log_class", &class_name_log);
  973. if (ret) {
  974. dev_err(&i2c->dev, "Failed to read log_class.\n");
  975. class_name_log = DEFAULT_LOG_CLASS_NAME;
  976. }
  977. #endif
  978. msg_maxim("rx_mode:%d, tx_mode:%d, tx_dither_en:%d, "\
  979. "rx_dither_en:%d, meas_dc_block_en:%d, "\
  980. "rx_flt_mode:%d, rx_ch_en:%d\n",
  981. pdata->rx_mode,
  982. pdata->tx_mode,
  983. pdata->cfg_data.tx_dither_en,
  984. pdata->cfg_data.rx_dither_en,
  985. pdata->cfg_data.meas_dc_block_en,
  986. pdata->cfg_data.rx_flt_mode,
  987. pdata->cfg_data.rx_ch_en);
  988. msg_maxim("tx_ch_en:%d, tx_hiz_ch_en:%d, tx_ch_src:%d, "\
  989. "auth_en:%d, wdog_time_out:%d\n",
  990. pdata->cfg_data.tx_ch_en,
  991. pdata->cfg_data.tx_hiz_ch_en,
  992. pdata->cfg_data.tx_ch_src,
  993. pdata->cfg_data.auth_en,
  994. pdata->cfg_data.wdog_time_out);
  995. } else
  996. max98504->pdata = i2c->dev.platform_data;
  997. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_max98504,
  998. max98504_dai, ARRAY_SIZE(max98504_dai));
  999. if (ret < 0)
  1000. kfree(max98504);
  1001. msg_maxim("ret=%d\n", ret);
  1002. #ifdef USE_MAX98504_IRQ
  1003. if (gpio_is_valid(pdata->irq)) {
  1004. /* configure touchscreen irq gpio */
  1005. ret = gpio_request(pdata->irq, "max98504_irq_gpio");
  1006. if (ret) {
  1007. dev_err(&i2c->dev, "unable to request gpio [%d]\n",
  1008. pdata->irq);
  1009. goto err_irq_gpio_req;
  1010. }
  1011. ret = gpio_direction_input(pdata->irq);
  1012. if (ret) {
  1013. dev_err(&i2c->dev,
  1014. "unable to set direction for gpio [%d]\n",
  1015. pdata->irq);
  1016. goto err_irq_gpio_req;
  1017. }
  1018. i2c->irq = gpio_to_irq(pdata->irq);
  1019. } else {
  1020. dev_err(&i2c->dev, "irq gpio not provided\n");
  1021. }
  1022. ret = request_threaded_irq(i2c->irq, NULL, max98504_interrupt,
  1023. IRQF_TRIGGER_FALLING, "max98504_interrupt", max98504);
  1024. if (ret)
  1025. dev_err(&i2c->dev, "Failed to register interrupt\n");
  1026. err_irq_gpio_req:
  1027. if (gpio_is_valid(pdata->irq))
  1028. gpio_free(pdata->irq);
  1029. #endif
  1030. return ret;
  1031. }
  1032. static int __devexit max98504_i2c_remove(struct i2c_client *client)
  1033. {
  1034. snd_soc_unregister_codec(&client->dev);
  1035. kfree(i2c_get_clientdata(client));
  1036. msg_maxim("\n");
  1037. return 0;
  1038. }
  1039. static const struct i2c_device_id max98504_i2c_id[] = {
  1040. { "max98504", MAX98504 },
  1041. { }
  1042. };
  1043. MODULE_DEVICE_TABLE(i2c, max98504_i2c_id);
  1044. static struct i2c_driver max98504_i2c_driver = {
  1045. .driver = {
  1046. .name = "max98504",
  1047. .owner = THIS_MODULE,
  1048. },
  1049. .probe = max98504_i2c_probe,
  1050. .remove = __devexit_p(max98504_i2c_remove),
  1051. .id_table = max98504_i2c_id,
  1052. };
  1053. static int __init max98504_init(void)
  1054. {
  1055. int ret;
  1056. msg_maxim("%s\n", __func__);
  1057. ret = i2c_add_driver(&max98504_i2c_driver);
  1058. if (ret)
  1059. pr_err("Failed to register MAX98504 I2C driver: %d\n", ret);
  1060. else
  1061. pr_info("MAX98504 driver built on %s at %s\n",
  1062. __DATE__, __TIME__);
  1063. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  1064. maxdsm_init();
  1065. #endif
  1066. return ret;
  1067. }
  1068. module_init(max98504_init);
  1069. static void __exit max98504_exit(void)
  1070. {
  1071. i2c_del_driver(&max98504_i2c_driver);
  1072. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  1073. maxdsm_deinit();
  1074. #endif
  1075. }
  1076. module_exit(max98504_exit);
  1077. MODULE_DESCRIPTION("ALSA SoC MAX98504 driver");
  1078. MODULE_AUTHOR("Ryan Lee");
  1079. MODULE_LICENSE("GPL");