da7210.c 33 KB

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