da7210.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*
  2. * DA7210 ALSA Soc codec driver
  3. *
  4. * Copyright (c) 2009 Dialog Semiconductor
  5. * Written by David Chen <Dajun.chen@diasemi.com>
  6. *
  7. * Copyright (C) 2009 Renesas Solutions Corp.
  8. * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
  9. *
  10. * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/i2c.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. /* DA7210 register space */
  29. #define DA7210_PAGE_CONTROL 0x00
  30. #define DA7210_CONTROL 0x01
  31. #define DA7210_STATUS 0x02
  32. #define DA7210_STARTUP1 0x03
  33. #define DA7210_STARTUP2 0x04
  34. #define DA7210_STARTUP3 0x05
  35. #define DA7210_MIC_L 0x07
  36. #define DA7210_MIC_R 0x08
  37. #define DA7210_AUX1_L 0x09
  38. #define DA7210_AUX1_R 0x0A
  39. #define DA7210_AUX2 0x0B
  40. #define DA7210_IN_GAIN 0x0C
  41. #define DA7210_INMIX_L 0x0D
  42. #define DA7210_INMIX_R 0x0E
  43. #define DA7210_ADC_HPF 0x0F
  44. #define DA7210_ADC 0x10
  45. #define DA7210_ADC_EQ1_2 0X11
  46. #define DA7210_ADC_EQ3_4 0x12
  47. #define DA7210_ADC_EQ5 0x13
  48. #define DA7210_DAC_HPF 0x14
  49. #define DA7210_DAC_L 0x15
  50. #define DA7210_DAC_R 0x16
  51. #define DA7210_DAC_SEL 0x17
  52. #define DA7210_SOFTMUTE 0x18
  53. #define DA7210_DAC_EQ1_2 0x19
  54. #define DA7210_DAC_EQ3_4 0x1A
  55. #define DA7210_DAC_EQ5 0x1B
  56. #define DA7210_OUTMIX_L 0x1C
  57. #define DA7210_OUTMIX_R 0x1D
  58. #define DA7210_OUT1_L 0x1E
  59. #define DA7210_OUT1_R 0x1F
  60. #define DA7210_OUT2 0x20
  61. #define DA7210_HP_L_VOL 0x21
  62. #define DA7210_HP_R_VOL 0x22
  63. #define DA7210_HP_CFG 0x23
  64. #define DA7210_ZERO_CROSS 0x24
  65. #define DA7210_DAI_SRC_SEL 0x25
  66. #define DA7210_DAI_CFG1 0x26
  67. #define DA7210_DAI_CFG3 0x28
  68. #define DA7210_PLL_DIV1 0x29
  69. #define DA7210_PLL_DIV2 0x2A
  70. #define DA7210_PLL_DIV3 0x2B
  71. #define DA7210_PLL 0x2C
  72. #define DA7210_ALC_MAX 0x83
  73. #define DA7210_ALC_MIN 0x84
  74. #define DA7210_ALC_NOIS 0x85
  75. #define DA7210_ALC_ATT 0x86
  76. #define DA7210_ALC_REL 0x87
  77. #define DA7210_ALC_DEL 0x88
  78. #define DA7210_A_HID_UNLOCK 0x8A
  79. #define DA7210_A_TEST_UNLOCK 0x8B
  80. #define DA7210_A_PLL1 0x90
  81. #define DA7210_A_CP_MODE 0xA7
  82. /* STARTUP1 bit fields */
  83. #define DA7210_SC_MST_EN (1 << 0)
  84. /* MIC_L bit fields */
  85. #define DA7210_MICBIAS_EN (1 << 6)
  86. #define DA7210_MIC_L_EN (1 << 7)
  87. /* MIC_R bit fields */
  88. #define DA7210_MIC_R_EN (1 << 7)
  89. /* INMIX_L bit fields */
  90. #define DA7210_IN_L_EN (1 << 7)
  91. /* INMIX_R bit fields */
  92. #define DA7210_IN_R_EN (1 << 7)
  93. /* ADC bit fields */
  94. #define DA7210_ADC_ALC_EN (1 << 0)
  95. #define DA7210_ADC_L_EN (1 << 3)
  96. #define DA7210_ADC_R_EN (1 << 7)
  97. /* DAC/ADC HPF fields */
  98. #define DA7210_VOICE_F0_MASK (0x7 << 4)
  99. #define DA7210_VOICE_F0_25 (1 << 4)
  100. #define DA7210_VOICE_EN (1 << 7)
  101. /* DAC_SEL bit fields */
  102. #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
  103. #define DA7210_DAC_L_EN (1 << 3)
  104. #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
  105. #define DA7210_DAC_R_EN (1 << 7)
  106. /* OUTMIX_L bit fields */
  107. #define DA7210_OUT_L_EN (1 << 7)
  108. /* OUTMIX_R bit fields */
  109. #define DA7210_OUT_R_EN (1 << 7)
  110. /* HP_CFG bit fields */
  111. #define DA7210_HP_2CAP_MODE (1 << 1)
  112. #define DA7210_HP_SENSE_EN (1 << 2)
  113. #define DA7210_HP_L_EN (1 << 3)
  114. #define DA7210_HP_MODE (1 << 6)
  115. #define DA7210_HP_R_EN (1 << 7)
  116. /* DAI_SRC_SEL bit fields */
  117. #define DA7210_DAI_OUT_L_SRC (6 << 0)
  118. #define DA7210_DAI_OUT_R_SRC (7 << 4)
  119. /* DAI_CFG1 bit fields */
  120. #define DA7210_DAI_WORD_S16_LE (0 << 0)
  121. #define DA7210_DAI_WORD_S20_3LE (1 << 0)
  122. #define DA7210_DAI_WORD_S24_LE (2 << 0)
  123. #define DA7210_DAI_WORD_S32_LE (3 << 0)
  124. #define DA7210_DAI_FLEN_64BIT (1 << 2)
  125. #define DA7210_DAI_MODE_SLAVE (0 << 7)
  126. #define DA7210_DAI_MODE_MASTER (1 << 7)
  127. /* DAI_CFG3 bit fields */
  128. #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
  129. #define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
  130. #define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
  131. #define DA7210_DAI_OE (1 << 3)
  132. #define DA7210_DAI_EN (1 << 7)
  133. /*PLL_DIV3 bit fields */
  134. #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
  135. #define DA7210_PLL_BYP (1 << 6)
  136. /* PLL bit fields */
  137. #define DA7210_PLL_FS_MASK (0xF << 0)
  138. #define DA7210_PLL_FS_8000 (0x1 << 0)
  139. #define DA7210_PLL_FS_11025 (0x2 << 0)
  140. #define DA7210_PLL_FS_12000 (0x3 << 0)
  141. #define DA7210_PLL_FS_16000 (0x5 << 0)
  142. #define DA7210_PLL_FS_22050 (0x6 << 0)
  143. #define DA7210_PLL_FS_24000 (0x7 << 0)
  144. #define DA7210_PLL_FS_32000 (0x9 << 0)
  145. #define DA7210_PLL_FS_44100 (0xA << 0)
  146. #define DA7210_PLL_FS_48000 (0xB << 0)
  147. #define DA7210_PLL_FS_88200 (0xE << 0)
  148. #define DA7210_PLL_FS_96000 (0xF << 0)
  149. #define DA7210_PLL_EN (0x1 << 7)
  150. /* SOFTMUTE bit fields */
  151. #define DA7210_RAMP_EN (1 << 6)
  152. /* CONTROL bit fields */
  153. #define DA7210_NOISE_SUP_EN (1 << 3)
  154. /* IN_GAIN bit fields */
  155. #define DA7210_INPGA_L_VOL (0x0F << 0)
  156. #define DA7210_INPGA_R_VOL (0xF0 << 0)
  157. /* ZERO_CROSS bit fields */
  158. #define DA7210_AUX1_L_ZC (1 << 0)
  159. #define DA7210_AUX1_R_ZC (1 << 1)
  160. #define DA7210_HP_L_ZC (1 << 6)
  161. #define DA7210_HP_R_ZC (1 << 7)
  162. /* AUX1_L bit fields */
  163. #define DA7210_AUX1_L_VOL (0x3F << 0)
  164. #define DA7210_AUX1_L_EN (1 << 7)
  165. /* AUX1_R bit fields */
  166. #define DA7210_AUX1_R_VOL (0x3F << 0)
  167. #define DA7210_AUX1_R_EN (1 << 7)
  168. /* AUX2 bit fields */
  169. #define DA7210_AUX2_EN (1 << 3)
  170. /* Minimum INPGA and AUX1 volume to enable noise suppression */
  171. #define DA7210_INPGA_MIN_VOL_NS 0x0A /* 10.5dB */
  172. #define DA7210_AUX1_MIN_VOL_NS 0x35 /* 6dB */
  173. /* OUT1_L bit fields */
  174. #define DA7210_OUT1_L_EN (1 << 7)
  175. /* OUT1_R bit fields */
  176. #define DA7210_OUT1_R_EN (1 << 7)
  177. /* OUT2 bit fields */
  178. #define DA7210_OUT2_OUTMIX_R (1 << 5)
  179. #define DA7210_OUT2_OUTMIX_L (1 << 6)
  180. #define DA7210_OUT2_EN (1 << 7)
  181. #define DA7210_VERSION "0.0.1"
  182. /*
  183. * Playback Volume
  184. *
  185. * max : 0x3F (+15.0 dB)
  186. * (1.5 dB step)
  187. * min : 0x11 (-54.0 dB)
  188. * mute : 0x10
  189. * reserved : 0x00 - 0x0F
  190. *
  191. * Reserved area are considered as "mute".
  192. */
  193. static const unsigned int hp_out_tlv[] = {
  194. TLV_DB_RANGE_HEAD(2),
  195. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  196. /* -54 dB to +15 dB */
  197. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
  198. };
  199. static const unsigned int lineout_vol_tlv[] = {
  200. TLV_DB_RANGE_HEAD(2),
  201. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  202. /* -54dB to 15dB */
  203. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
  204. };
  205. static const unsigned int mono_vol_tlv[] = {
  206. TLV_DB_RANGE_HEAD(2),
  207. 0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
  208. /* -18dB to 6dB */
  209. 0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
  210. };
  211. static const unsigned int aux1_vol_tlv[] = {
  212. TLV_DB_RANGE_HEAD(2),
  213. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  214. /* -48dB to 21dB */
  215. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
  216. };
  217. static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
  218. static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
  219. static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -7725, 75, 0);
  220. static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
  221. static const DECLARE_TLV_DB_SCALE(aux2_vol_tlv, -600, 600, 0);
  222. static const DECLARE_TLV_DB_SCALE(inpga_gain_tlv, -450, 150, 0);
  223. /* ADC and DAC high pass filter f0 value */
  224. static const char * const da7210_hpf_cutoff_txt[] = {
  225. "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
  226. };
  227. static const struct soc_enum da7210_dac_hpf_cutoff =
  228. SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
  229. static const struct soc_enum da7210_adc_hpf_cutoff =
  230. SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
  231. /* ADC and DAC voice (8kHz) high pass cutoff value */
  232. static const char * const da7210_vf_cutoff_txt[] = {
  233. "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  234. };
  235. static const struct soc_enum da7210_dac_vf_cutoff =
  236. SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
  237. static const struct soc_enum da7210_adc_vf_cutoff =
  238. SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
  239. static const char *da7210_hp_mode_txt[] = {
  240. "Class H", "Class G"
  241. };
  242. static const struct soc_enum da7210_hp_mode_sel =
  243. SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
  244. /* ALC can be enabled only if noise suppression is disabled */
  245. static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  249. if (ucontrol->value.integer.value[0]) {
  250. /* Check if noise suppression is enabled */
  251. if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
  252. dev_dbg(codec->dev,
  253. "Disable noise suppression to enable ALC\n");
  254. return -EINVAL;
  255. }
  256. }
  257. /* If all conditions are met or we are actually disabling ALC */
  258. return snd_soc_put_volsw(kcontrol, ucontrol);
  259. }
  260. /* Noise suppression can be enabled only if following conditions are met
  261. * ALC disabled
  262. * ZC enabled for HP and AUX1 PGA
  263. * INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
  264. * AUX1_L_VOL and AUX1_R_VOL >= 6 dB
  265. */
  266. static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
  267. struct snd_ctl_elem_value *ucontrol)
  268. {
  269. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  270. u8 val;
  271. if (ucontrol->value.integer.value[0]) {
  272. /* Check if ALC is enabled */
  273. if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
  274. goto err;
  275. /* Check ZC for HP and AUX1 PGA */
  276. if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
  277. (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
  278. DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
  279. DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
  280. goto err;
  281. /* Check INPGA_L_VOL and INPGA_R_VOL */
  282. val = snd_soc_read(codec, DA7210_IN_GAIN);
  283. if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
  284. (((val & DA7210_INPGA_R_VOL) >> 4) <
  285. DA7210_INPGA_MIN_VOL_NS))
  286. goto err;
  287. /* Check AUX1_L_VOL and AUX1_R_VOL */
  288. if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
  289. DA7210_AUX1_MIN_VOL_NS) ||
  290. ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
  291. DA7210_AUX1_MIN_VOL_NS))
  292. goto err;
  293. }
  294. /* If all conditions are met or we are actually disabling Noise sup */
  295. return snd_soc_put_volsw(kcontrol, ucontrol);
  296. err:
  297. return -EINVAL;
  298. }
  299. static const struct snd_kcontrol_new da7210_snd_controls[] = {
  300. SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
  301. DA7210_HP_L_VOL, DA7210_HP_R_VOL,
  302. 0, 0x3F, 0, hp_out_tlv),
  303. SOC_DOUBLE_R_TLV("Digital Playback Volume",
  304. DA7210_DAC_L, DA7210_DAC_R,
  305. 0, 0x77, 1, dac_gain_tlv),
  306. SOC_DOUBLE_R_TLV("Lineout Playback Volume",
  307. DA7210_OUT1_L, DA7210_OUT1_R,
  308. 0, 0x3f, 0, lineout_vol_tlv),
  309. SOC_SINGLE_TLV("Mono Playback Volume", DA7210_OUT2, 0, 0x7, 0,
  310. mono_vol_tlv),
  311. SOC_DOUBLE_R_TLV("Mic Capture Volume",
  312. DA7210_MIC_L, DA7210_MIC_R,
  313. 0, 0x5, 0, mic_vol_tlv),
  314. SOC_DOUBLE_R_TLV("Aux1 Capture Volume",
  315. DA7210_AUX1_L, DA7210_AUX1_R,
  316. 0, 0x3f, 0, aux1_vol_tlv),
  317. SOC_SINGLE_TLV("Aux2 Capture Volume", DA7210_AUX2, 0, 0x3, 0,
  318. aux2_vol_tlv),
  319. SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
  320. inpga_gain_tlv),
  321. /* DAC Equalizer controls */
  322. SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
  323. SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
  324. eq_gain_tlv),
  325. SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
  326. eq_gain_tlv),
  327. SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
  328. eq_gain_tlv),
  329. SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
  330. eq_gain_tlv),
  331. SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
  332. eq_gain_tlv),
  333. /* ADC Equalizer controls */
  334. SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
  335. SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
  336. 1, adc_eq_master_gain_tlv),
  337. SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
  338. eq_gain_tlv),
  339. SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
  340. eq_gain_tlv),
  341. SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
  342. eq_gain_tlv),
  343. SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
  344. eq_gain_tlv),
  345. SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
  346. eq_gain_tlv),
  347. SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
  348. SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
  349. SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
  350. SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
  351. SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
  352. SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
  353. SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
  354. SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
  355. /* Mute controls */
  356. SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
  357. SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
  358. SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
  359. SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
  360. SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
  361. /* Zero cross controls */
  362. SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
  363. SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
  364. SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
  365. SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
  366. SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
  367. /* ALC controls */
  368. SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
  369. snd_soc_get_volsw, da7210_put_alc_sw),
  370. SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
  371. SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
  372. SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
  373. SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
  374. SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
  375. SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
  376. SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
  377. 0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
  378. };
  379. /*
  380. * DAPM Controls
  381. *
  382. * Current DAPM implementation covers almost all codec components e.g. IOs,
  383. * mixers, PGAs,ADC and DAC.
  384. */
  385. /* In Mixer Left */
  386. static const struct snd_kcontrol_new da7210_dapm_inmixl_controls[] = {
  387. SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_L, 0, 1, 0),
  388. SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_L, 1, 1, 0),
  389. SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_INMIX_L, 2, 1, 0),
  390. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_L, 3, 1, 0),
  391. SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_INMIX_L, 4, 1, 0),
  392. };
  393. /* In Mixer Right */
  394. static const struct snd_kcontrol_new da7210_dapm_inmixr_controls[] = {
  395. SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_R, 0, 1, 0),
  396. SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_R, 1, 1, 0),
  397. SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_INMIX_R, 2, 1, 0),
  398. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_R, 3, 1, 0),
  399. SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_INMIX_R, 4, 1, 0),
  400. };
  401. /* Out Mixer Left */
  402. static const struct snd_kcontrol_new da7210_dapm_outmixl_controls[] = {
  403. SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_OUTMIX_L, 0, 1, 0),
  404. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_L, 1, 1, 0),
  405. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_L, 2, 1, 0),
  406. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_L, 3, 1, 0),
  407. SOC_DAPM_SINGLE("DAC Left Switch", DA7210_OUTMIX_L, 4, 1, 0),
  408. };
  409. /* Out Mixer Right */
  410. static const struct snd_kcontrol_new da7210_dapm_outmixr_controls[] = {
  411. SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_OUTMIX_R, 0, 1, 0),
  412. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_R, 1, 1, 0),
  413. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_R, 2, 1, 0),
  414. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_R, 3, 1, 0),
  415. SOC_DAPM_SINGLE("DAC Right Switch", DA7210_OUTMIX_R, 4, 1, 0),
  416. };
  417. /* Mono Mixer */
  418. static const struct snd_kcontrol_new da7210_dapm_monomix_controls[] = {
  419. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUT2, 3, 1, 0),
  420. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUT2, 4, 1, 0),
  421. SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_OUT2, 5, 1, 0),
  422. SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_OUT2, 6, 1, 0),
  423. };
  424. /* DAPM widgets */
  425. static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
  426. /* Input Side */
  427. /* Input Lines */
  428. SND_SOC_DAPM_INPUT("MICL"),
  429. SND_SOC_DAPM_INPUT("MICR"),
  430. SND_SOC_DAPM_INPUT("AUX1L"),
  431. SND_SOC_DAPM_INPUT("AUX1R"),
  432. SND_SOC_DAPM_INPUT("AUX2"),
  433. /* Input PGAs */
  434. SND_SOC_DAPM_PGA("Mic Left", DA7210_STARTUP3, 0, 1, NULL, 0),
  435. SND_SOC_DAPM_PGA("Mic Right", DA7210_STARTUP3, 1, 1, NULL, 0),
  436. SND_SOC_DAPM_PGA("Aux1 Left", DA7210_STARTUP3, 2, 1, NULL, 0),
  437. SND_SOC_DAPM_PGA("Aux1 Right", DA7210_STARTUP3, 3, 1, NULL, 0),
  438. SND_SOC_DAPM_PGA("Aux2 Mono", DA7210_STARTUP3, 4, 1, NULL, 0),
  439. SND_SOC_DAPM_PGA("INPGA Left", DA7210_INMIX_L, 7, 0, NULL, 0),
  440. SND_SOC_DAPM_PGA("INPGA Right", DA7210_INMIX_R, 7, 0, NULL, 0),
  441. /* MICBIAS */
  442. SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
  443. /* Input Mixers */
  444. SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
  445. &da7210_dapm_inmixl_controls[0],
  446. ARRAY_SIZE(da7210_dapm_inmixl_controls)),
  447. SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
  448. &da7210_dapm_inmixr_controls[0],
  449. ARRAY_SIZE(da7210_dapm_inmixr_controls)),
  450. /* ADCs */
  451. SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
  452. SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
  453. /* Output Side */
  454. /* DACs */
  455. SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
  456. SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
  457. /* Output Mixers */
  458. SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
  459. &da7210_dapm_outmixl_controls[0],
  460. ARRAY_SIZE(da7210_dapm_outmixl_controls)),
  461. SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
  462. &da7210_dapm_outmixr_controls[0],
  463. ARRAY_SIZE(da7210_dapm_outmixr_controls)),
  464. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
  465. &da7210_dapm_monomix_controls[0],
  466. ARRAY_SIZE(da7210_dapm_monomix_controls)),
  467. /* Output PGAs */
  468. SND_SOC_DAPM_PGA("OUTPGA Left Enable", DA7210_OUTMIX_L, 7, 0, NULL, 0),
  469. SND_SOC_DAPM_PGA("OUTPGA Right Enable", DA7210_OUTMIX_R, 7, 0, NULL, 0),
  470. SND_SOC_DAPM_PGA("Out1 Left", DA7210_STARTUP2, 0, 1, NULL, 0),
  471. SND_SOC_DAPM_PGA("Out1 Right", DA7210_STARTUP2, 1, 1, NULL, 0),
  472. SND_SOC_DAPM_PGA("Out2 Mono", DA7210_STARTUP2, 2, 1, NULL, 0),
  473. SND_SOC_DAPM_PGA("Headphone Left", DA7210_STARTUP2, 3, 1, NULL, 0),
  474. SND_SOC_DAPM_PGA("Headphone Right", DA7210_STARTUP2, 4, 1, NULL, 0),
  475. /* Output Lines */
  476. SND_SOC_DAPM_OUTPUT("OUT1L"),
  477. SND_SOC_DAPM_OUTPUT("OUT1R"),
  478. SND_SOC_DAPM_OUTPUT("HPL"),
  479. SND_SOC_DAPM_OUTPUT("HPR"),
  480. SND_SOC_DAPM_OUTPUT("OUT2"),
  481. };
  482. /* DAPM audio route definition */
  483. static const struct snd_soc_dapm_route da7210_audio_map[] = {
  484. /* Dest Connecting Widget source */
  485. /* Input path */
  486. {"Mic Left", NULL, "MICL"},
  487. {"Mic Right", NULL, "MICR"},
  488. {"Aux1 Left", NULL, "AUX1L"},
  489. {"Aux1 Right", NULL, "AUX1R"},
  490. {"Aux2 Mono", NULL, "AUX2"},
  491. {"In Mixer Left", "Mic Left Switch", "Mic Left"},
  492. {"In Mixer Left", "Mic Right Switch", "Mic Right"},
  493. {"In Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
  494. {"In Mixer Left", "Aux2 Switch", "Aux2 Mono"},
  495. {"In Mixer Left", "Outmix Left Switch", "Out Mixer Left"},
  496. {"In Mixer Right", "Mic Right Switch", "Mic Right"},
  497. {"In Mixer Right", "Mic Left Switch", "Mic Left"},
  498. {"In Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
  499. {"In Mixer Right", "Aux2 Switch", "Aux2 Mono"},
  500. {"In Mixer Right", "Outmix Right Switch", "Out Mixer Right"},
  501. {"INPGA Left", NULL, "In Mixer Left"},
  502. {"ADC Left", NULL, "INPGA Left"},
  503. {"INPGA Right", NULL, "In Mixer Right"},
  504. {"ADC Right", NULL, "INPGA Right"},
  505. /* Output path */
  506. {"Out Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
  507. {"Out Mixer Left", "Aux2 Switch", "Aux2 Mono"},
  508. {"Out Mixer Left", "INPGA Left Switch", "INPGA Left"},
  509. {"Out Mixer Left", "INPGA Right Switch", "INPGA Right"},
  510. {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
  511. {"Out Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
  512. {"Out Mixer Right", "Aux2 Switch", "Aux2 Mono"},
  513. {"Out Mixer Right", "INPGA Right Switch", "INPGA Right"},
  514. {"Out Mixer Right", "INPGA Left Switch", "INPGA Left"},
  515. {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
  516. {"Mono Mixer", "INPGA Right Switch", "INPGA Right"},
  517. {"Mono Mixer", "INPGA Left Switch", "INPGA Left"},
  518. {"Mono Mixer", "Outmix Right Switch", "Out Mixer Right"},
  519. {"Mono Mixer", "Outmix Left Switch", "Out Mixer Left"},
  520. {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
  521. {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
  522. {"Out1 Left", NULL, "OUTPGA Left Enable"},
  523. {"OUT1L", NULL, "Out1 Left"},
  524. {"Out1 Right", NULL, "OUTPGA Right Enable"},
  525. {"OUT1R", NULL, "Out1 Right"},
  526. {"Headphone Left", NULL, "OUTPGA Left Enable"},
  527. {"HPL", NULL, "Headphone Left"},
  528. {"Headphone Right", NULL, "OUTPGA Right Enable"},
  529. {"HPR", NULL, "Headphone Right"},
  530. {"Out2 Mono", NULL, "Mono Mixer"},
  531. {"OUT2", NULL, "Out2 Mono"},
  532. };
  533. /* Codec private data */
  534. struct da7210_priv {
  535. struct regmap *regmap;
  536. };
  537. static struct reg_default da7210_reg_defaults[] = {
  538. { 0x00, 0x00 },
  539. { 0x01, 0x11 },
  540. { 0x03, 0x00 },
  541. { 0x04, 0x00 },
  542. { 0x05, 0x00 },
  543. { 0x06, 0x00 },
  544. { 0x07, 0x00 },
  545. { 0x08, 0x00 },
  546. { 0x09, 0x00 },
  547. { 0x0a, 0x00 },
  548. { 0x0b, 0x00 },
  549. { 0x0c, 0x00 },
  550. { 0x0d, 0x00 },
  551. { 0x0e, 0x00 },
  552. { 0x0f, 0x08 },
  553. { 0x10, 0x00 },
  554. { 0x11, 0x00 },
  555. { 0x12, 0x00 },
  556. { 0x13, 0x00 },
  557. { 0x14, 0x08 },
  558. { 0x15, 0x10 },
  559. { 0x16, 0x10 },
  560. { 0x17, 0x54 },
  561. { 0x18, 0x40 },
  562. { 0x19, 0x00 },
  563. { 0x1a, 0x00 },
  564. { 0x1b, 0x00 },
  565. { 0x1c, 0x00 },
  566. { 0x1d, 0x00 },
  567. { 0x1e, 0x00 },
  568. { 0x1f, 0x00 },
  569. { 0x20, 0x00 },
  570. { 0x21, 0x00 },
  571. { 0x22, 0x00 },
  572. { 0x23, 0x02 },
  573. { 0x24, 0x00 },
  574. { 0x25, 0x76 },
  575. { 0x26, 0x00 },
  576. { 0x27, 0x00 },
  577. { 0x28, 0x04 },
  578. { 0x29, 0x00 },
  579. { 0x2a, 0x00 },
  580. { 0x2b, 0x30 },
  581. { 0x2c, 0x2A },
  582. { 0x83, 0x00 },
  583. { 0x84, 0x00 },
  584. { 0x85, 0x00 },
  585. { 0x86, 0x00 },
  586. { 0x87, 0x00 },
  587. { 0x88, 0x00 },
  588. };
  589. static bool da7210_readable_register(struct device *dev, unsigned int reg)
  590. {
  591. switch (reg) {
  592. case DA7210_A_HID_UNLOCK:
  593. case DA7210_A_TEST_UNLOCK:
  594. case DA7210_A_PLL1:
  595. case DA7210_A_CP_MODE:
  596. return false;
  597. default:
  598. return true;
  599. }
  600. }
  601. static bool da7210_volatile_register(struct device *dev,
  602. unsigned int reg)
  603. {
  604. switch (reg) {
  605. case DA7210_STATUS:
  606. return true;
  607. default:
  608. return false;
  609. }
  610. }
  611. /*
  612. * Set PCM DAI word length.
  613. */
  614. static int da7210_hw_params(struct snd_pcm_substream *substream,
  615. struct snd_pcm_hw_params *params,
  616. struct snd_soc_dai *dai)
  617. {
  618. struct snd_soc_codec *codec = dai->codec;
  619. u32 dai_cfg1;
  620. u32 fs, bypass;
  621. /* set DAI source to Left and Right ADC */
  622. snd_soc_write(codec, DA7210_DAI_SRC_SEL,
  623. DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
  624. /* Enable DAI */
  625. snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
  626. dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
  627. switch (params_format(params)) {
  628. case SNDRV_PCM_FORMAT_S16_LE:
  629. dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
  630. break;
  631. case SNDRV_PCM_FORMAT_S20_3LE:
  632. dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
  633. break;
  634. case SNDRV_PCM_FORMAT_S24_LE:
  635. dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
  636. break;
  637. case SNDRV_PCM_FORMAT_S32_LE:
  638. dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
  639. break;
  640. default:
  641. return -EINVAL;
  642. }
  643. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  644. switch (params_rate(params)) {
  645. case 8000:
  646. fs = DA7210_PLL_FS_8000;
  647. bypass = DA7210_PLL_BYP;
  648. break;
  649. case 11025:
  650. fs = DA7210_PLL_FS_11025;
  651. bypass = 0;
  652. break;
  653. case 12000:
  654. fs = DA7210_PLL_FS_12000;
  655. bypass = DA7210_PLL_BYP;
  656. break;
  657. case 16000:
  658. fs = DA7210_PLL_FS_16000;
  659. bypass = DA7210_PLL_BYP;
  660. break;
  661. case 22050:
  662. fs = DA7210_PLL_FS_22050;
  663. bypass = 0;
  664. break;
  665. case 32000:
  666. fs = DA7210_PLL_FS_32000;
  667. bypass = DA7210_PLL_BYP;
  668. break;
  669. case 44100:
  670. fs = DA7210_PLL_FS_44100;
  671. bypass = 0;
  672. break;
  673. case 48000:
  674. fs = DA7210_PLL_FS_48000;
  675. bypass = DA7210_PLL_BYP;
  676. break;
  677. case 88200:
  678. fs = DA7210_PLL_FS_88200;
  679. bypass = 0;
  680. break;
  681. case 96000:
  682. fs = DA7210_PLL_FS_96000;
  683. bypass = DA7210_PLL_BYP;
  684. break;
  685. default:
  686. return -EINVAL;
  687. }
  688. /* Disable active mode */
  689. snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
  690. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
  691. snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
  692. /* Enable active mode */
  693. snd_soc_update_bits(codec, DA7210_STARTUP1,
  694. DA7210_SC_MST_EN, DA7210_SC_MST_EN);
  695. return 0;
  696. }
  697. /*
  698. * Set DAI mode and Format
  699. */
  700. static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
  701. {
  702. struct snd_soc_codec *codec = codec_dai->codec;
  703. u32 dai_cfg1;
  704. u32 dai_cfg3;
  705. dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
  706. dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
  707. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  708. case SND_SOC_DAIFMT_CBM_CFM:
  709. dai_cfg1 |= DA7210_DAI_MODE_MASTER;
  710. break;
  711. case SND_SOC_DAIFMT_CBS_CFS:
  712. dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. /* FIXME
  718. *
  719. * It support I2S only now
  720. */
  721. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  722. case SND_SOC_DAIFMT_I2S:
  723. dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
  724. break;
  725. case SND_SOC_DAIFMT_LEFT_J:
  726. dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
  727. break;
  728. case SND_SOC_DAIFMT_RIGHT_J:
  729. dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
  730. break;
  731. default:
  732. return -EINVAL;
  733. }
  734. /* FIXME
  735. *
  736. * It support 64bit data transmission only now
  737. */
  738. dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
  739. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  740. snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
  741. return 0;
  742. }
  743. static int da7210_mute(struct snd_soc_dai *dai, int mute)
  744. {
  745. struct snd_soc_codec *codec = dai->codec;
  746. u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
  747. if (mute)
  748. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
  749. else
  750. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
  751. return 0;
  752. }
  753. #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  754. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  755. /* DAI operations */
  756. static const struct snd_soc_dai_ops da7210_dai_ops = {
  757. .hw_params = da7210_hw_params,
  758. .set_fmt = da7210_set_dai_fmt,
  759. .digital_mute = da7210_mute,
  760. };
  761. static struct snd_soc_dai_driver da7210_dai = {
  762. .name = "da7210-hifi",
  763. /* playback capabilities */
  764. .playback = {
  765. .stream_name = "Playback",
  766. .channels_min = 1,
  767. .channels_max = 2,
  768. .rates = SNDRV_PCM_RATE_8000_96000,
  769. .formats = DA7210_FORMATS,
  770. },
  771. /* capture capabilities */
  772. .capture = {
  773. .stream_name = "Capture",
  774. .channels_min = 1,
  775. .channels_max = 2,
  776. .rates = SNDRV_PCM_RATE_8000_96000,
  777. .formats = DA7210_FORMATS,
  778. },
  779. .ops = &da7210_dai_ops,
  780. .symmetric_rates = 1,
  781. };
  782. static int da7210_probe(struct snd_soc_codec *codec)
  783. {
  784. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  785. int ret;
  786. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  787. codec->control_data = da7210->regmap;
  788. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  789. if (ret < 0) {
  790. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  791. return ret;
  792. }
  793. /* FIXME
  794. *
  795. * This driver use fixed value here
  796. * And below settings expects MCLK = 12.288MHz
  797. *
  798. * When you select different MCLK, please check...
  799. * DA7210_PLL_DIV1 val
  800. * DA7210_PLL_DIV2 val
  801. * DA7210_PLL_DIV3 val
  802. * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
  803. */
  804. /*
  805. * ADC settings
  806. */
  807. /* Enable Left & Right MIC PGA and Mic Bias */
  808. snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
  809. snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
  810. /* Enable Left and Right input PGA */
  811. snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
  812. snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
  813. /* Enable Left and Right ADC */
  814. snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
  815. /*
  816. * DAC settings
  817. */
  818. /* Enable Left and Right DAC */
  819. snd_soc_write(codec, DA7210_DAC_SEL,
  820. DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
  821. DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
  822. /* Enable Left and Right out PGA */
  823. snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
  824. snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
  825. /* Enable Left and Right HeadPhone PGA */
  826. snd_soc_write(codec, DA7210_HP_CFG,
  827. DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
  828. DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
  829. /* Enable ramp mode for DAC gain update */
  830. snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
  831. /*
  832. * For DA7210 codec, there are two ways to enable/disable analog IOs
  833. * and ADC/DAC,
  834. * (1) Using "Enable Bit" of register associated with that IO
  835. * (or ADC/DAC)
  836. * e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
  837. *
  838. * (2) Using "Standby Bit" of STARTUP2 or STARTUP3 register
  839. * e.g. Mic left can be put to STANDBY using bit 0 of STARTUP3(0x5)
  840. *
  841. * Out of these two methods, the one using STANDBY bits is preferred
  842. * way to enable/disable individual blocks. This is because STANDBY
  843. * registers are part of system controller which allows system power
  844. * up/down in a controlled, pop-free manner. Also, as per application
  845. * note of DA7210, STANDBY register bits are only effective if a
  846. * particular IO (or ADC/DAC) is already enabled using enable/disable
  847. * register bits. Keeping these things in mind, current DAPM
  848. * implementation manipulates only STANDBY bits.
  849. *
  850. * Overall implementation can be outlined as below,
  851. *
  852. * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
  853. * - "STANDBY bit" is controlled by DAPM
  854. */
  855. /* Enable Line out amplifiers */
  856. snd_soc_write(codec, DA7210_OUT1_L, DA7210_OUT1_L_EN);
  857. snd_soc_write(codec, DA7210_OUT1_R, DA7210_OUT1_R_EN);
  858. snd_soc_write(codec, DA7210_OUT2, DA7210_OUT2_EN |
  859. DA7210_OUT2_OUTMIX_L | DA7210_OUT2_OUTMIX_R);
  860. /* Enable Aux1 */
  861. snd_soc_write(codec, DA7210_AUX1_L, DA7210_AUX1_L_EN);
  862. snd_soc_write(codec, DA7210_AUX1_R, DA7210_AUX1_R_EN);
  863. /* Enable Aux2 */
  864. snd_soc_write(codec, DA7210_AUX2, DA7210_AUX2_EN);
  865. /* Diable PLL and bypass it */
  866. snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
  867. /*
  868. * If 48kHz sound came, it use bypass mode,
  869. * and when it is 44.1kHz, it use PLL.
  870. *
  871. * This time, this driver sets PLL always ON
  872. * and controls bypass/PLL mode by switching
  873. * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
  874. * see da7210_hw_params
  875. */
  876. snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
  877. snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
  878. snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
  879. DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
  880. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
  881. /* Activate all enabled subsystem */
  882. snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
  883. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  884. return 0;
  885. }
  886. static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
  887. .probe = da7210_probe,
  888. .controls = da7210_snd_controls,
  889. .num_controls = ARRAY_SIZE(da7210_snd_controls),
  890. .dapm_widgets = da7210_dapm_widgets,
  891. .num_dapm_widgets = ARRAY_SIZE(da7210_dapm_widgets),
  892. .dapm_routes = da7210_audio_map,
  893. .num_dapm_routes = ARRAY_SIZE(da7210_audio_map),
  894. };
  895. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  896. static struct reg_default da7210_regmap_i2c_patch[] = {
  897. /* System controller master disable */
  898. { DA7210_STARTUP1, 0x00 },
  899. /* make sure that DA7210 use bypass mode before start up */
  900. { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP },
  901. /* to unlock */
  902. { DA7210_A_HID_UNLOCK, 0x8B},
  903. { DA7210_A_TEST_UNLOCK, 0xB4},
  904. { DA7210_A_PLL1, 0x01},
  905. { DA7210_A_CP_MODE, 0x7C},
  906. /* to re-lock */
  907. { DA7210_A_HID_UNLOCK, 0x00},
  908. { DA7210_A_TEST_UNLOCK, 0x00},
  909. };
  910. static const struct regmap_config da7210_regmap_config_i2c = {
  911. .reg_bits = 8,
  912. .val_bits = 8,
  913. .reg_defaults = da7210_reg_defaults,
  914. .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
  915. .volatile_reg = da7210_volatile_register,
  916. .readable_reg = da7210_readable_register,
  917. .cache_type = REGCACHE_RBTREE,
  918. };
  919. static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
  920. const struct i2c_device_id *id)
  921. {
  922. struct da7210_priv *da7210;
  923. int ret;
  924. da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
  925. GFP_KERNEL);
  926. if (!da7210)
  927. return -ENOMEM;
  928. i2c_set_clientdata(i2c, da7210);
  929. da7210->regmap = regmap_init_i2c(i2c, &da7210_regmap_config_i2c);
  930. if (IS_ERR(da7210->regmap)) {
  931. ret = PTR_ERR(da7210->regmap);
  932. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  933. return ret;
  934. }
  935. ret = regmap_register_patch(da7210->regmap, da7210_regmap_i2c_patch,
  936. ARRAY_SIZE(da7210_regmap_i2c_patch));
  937. if (ret != 0)
  938. dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
  939. ret = snd_soc_register_codec(&i2c->dev,
  940. &soc_codec_dev_da7210, &da7210_dai, 1);
  941. if (ret < 0) {
  942. dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
  943. goto err_regmap;
  944. }
  945. return ret;
  946. err_regmap:
  947. regmap_exit(da7210->regmap);
  948. return ret;
  949. }
  950. static int __devexit da7210_i2c_remove(struct i2c_client *client)
  951. {
  952. struct da7210_priv *da7210 = i2c_get_clientdata(client);
  953. snd_soc_unregister_codec(&client->dev);
  954. regmap_exit(da7210->regmap);
  955. return 0;
  956. }
  957. static const struct i2c_device_id da7210_i2c_id[] = {
  958. { "da7210", 0 },
  959. { }
  960. };
  961. MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
  962. /* I2C codec control layer */
  963. static struct i2c_driver da7210_i2c_driver = {
  964. .driver = {
  965. .name = "da7210",
  966. .owner = THIS_MODULE,
  967. },
  968. .probe = da7210_i2c_probe,
  969. .remove = __devexit_p(da7210_i2c_remove),
  970. .id_table = da7210_i2c_id,
  971. };
  972. #endif
  973. #if defined(CONFIG_SPI_MASTER)
  974. static struct reg_default da7210_regmap_spi_patch[] = {
  975. /* Dummy read to give two pulses over nCS for SPI */
  976. { DA7210_AUX2, 0x00 },
  977. { DA7210_AUX2, 0x00 },
  978. /* System controller master disable */
  979. { DA7210_STARTUP1, 0x00 },
  980. /* make sure that DA7210 use bypass mode before start up */
  981. { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP },
  982. /* to set PAGE1 of SPI register space */
  983. { DA7210_PAGE_CONTROL, 0x80 },
  984. /* to unlock */
  985. { DA7210_A_HID_UNLOCK, 0x8B},
  986. { DA7210_A_TEST_UNLOCK, 0xB4},
  987. { DA7210_A_PLL1, 0x01},
  988. { DA7210_A_CP_MODE, 0x7C},
  989. /* to re-lock */
  990. { DA7210_A_HID_UNLOCK, 0x00},
  991. { DA7210_A_TEST_UNLOCK, 0x00},
  992. /* to set back PAGE0 of SPI register space */
  993. { DA7210_PAGE_CONTROL, 0x00 },
  994. };
  995. static const struct regmap_config da7210_regmap_config_spi = {
  996. .reg_bits = 8,
  997. .val_bits = 8,
  998. .read_flag_mask = 0x01,
  999. .write_flag_mask = 0x00,
  1000. .reg_defaults = da7210_reg_defaults,
  1001. .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
  1002. .volatile_reg = da7210_volatile_register,
  1003. .readable_reg = da7210_readable_register,
  1004. .cache_type = REGCACHE_RBTREE,
  1005. };
  1006. static int __devinit da7210_spi_probe(struct spi_device *spi)
  1007. {
  1008. struct da7210_priv *da7210;
  1009. int ret;
  1010. da7210 = devm_kzalloc(&spi->dev, sizeof(struct da7210_priv),
  1011. GFP_KERNEL);
  1012. if (!da7210)
  1013. return -ENOMEM;
  1014. spi_set_drvdata(spi, da7210);
  1015. da7210->regmap = devm_regmap_init_spi(spi, &da7210_regmap_config_spi);
  1016. if (IS_ERR(da7210->regmap)) {
  1017. ret = PTR_ERR(da7210->regmap);
  1018. dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
  1019. return ret;
  1020. }
  1021. ret = regmap_register_patch(da7210->regmap, da7210_regmap_spi_patch,
  1022. ARRAY_SIZE(da7210_regmap_spi_patch));
  1023. if (ret != 0)
  1024. dev_warn(&spi->dev, "Failed to apply regmap patch: %d\n", ret);
  1025. ret = snd_soc_register_codec(&spi->dev,
  1026. &soc_codec_dev_da7210, &da7210_dai, 1);
  1027. if (ret < 0)
  1028. goto err_regmap;
  1029. return ret;
  1030. err_regmap:
  1031. regmap_exit(da7210->regmap);
  1032. return ret;
  1033. }
  1034. static int __devexit da7210_spi_remove(struct spi_device *spi)
  1035. {
  1036. struct da7210_priv *da7210 = spi_get_drvdata(spi);
  1037. snd_soc_unregister_codec(&spi->dev);
  1038. regmap_exit(da7210->regmap);
  1039. return 0;
  1040. }
  1041. static struct spi_driver da7210_spi_driver = {
  1042. .driver = {
  1043. .name = "da7210",
  1044. .owner = THIS_MODULE,
  1045. },
  1046. .probe = da7210_spi_probe,
  1047. .remove = __devexit_p(da7210_spi_remove)
  1048. };
  1049. #endif
  1050. static int __init da7210_modinit(void)
  1051. {
  1052. int ret = 0;
  1053. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1054. ret = i2c_add_driver(&da7210_i2c_driver);
  1055. #endif
  1056. #if defined(CONFIG_SPI_MASTER)
  1057. ret = spi_register_driver(&da7210_spi_driver);
  1058. if (ret) {
  1059. printk(KERN_ERR "Failed to register da7210 SPI driver: %d\n",
  1060. ret);
  1061. }
  1062. #endif
  1063. return ret;
  1064. }
  1065. module_init(da7210_modinit);
  1066. static void __exit da7210_exit(void)
  1067. {
  1068. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1069. i2c_del_driver(&da7210_i2c_driver);
  1070. #endif
  1071. #if defined(CONFIG_SPI_MASTER)
  1072. spi_unregister_driver(&da7210_spi_driver);
  1073. #endif
  1074. }
  1075. module_exit(da7210_exit);
  1076. MODULE_DESCRIPTION("ASoC DA7210 driver");
  1077. MODULE_AUTHOR("David Chen, Kuninori Morimoto");
  1078. MODULE_LICENSE("GPL");