da7210.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. /* DA7210 register space */
  27. #define DA7210_CONTROL 0x01
  28. #define DA7210_STATUS 0x02
  29. #define DA7210_STARTUP1 0x03
  30. #define DA7210_MIC_L 0x07
  31. #define DA7210_MIC_R 0x08
  32. #define DA7210_AUX1_L 0x09
  33. #define DA7210_AUX1_R 0x0A
  34. #define DA7210_AUX2 0x0B
  35. #define DA7210_IN_GAIN 0x0C
  36. #define DA7210_INMIX_L 0x0D
  37. #define DA7210_INMIX_R 0x0E
  38. #define DA7210_ADC_HPF 0x0F
  39. #define DA7210_ADC 0x10
  40. #define DA7210_ADC_EQ1_2 0X11
  41. #define DA7210_ADC_EQ3_4 0x12
  42. #define DA7210_ADC_EQ5 0x13
  43. #define DA7210_DAC_HPF 0x14
  44. #define DA7210_DAC_L 0x15
  45. #define DA7210_DAC_R 0x16
  46. #define DA7210_DAC_SEL 0x17
  47. #define DA7210_SOFTMUTE 0x18
  48. #define DA7210_DAC_EQ1_2 0x19
  49. #define DA7210_DAC_EQ3_4 0x1A
  50. #define DA7210_DAC_EQ5 0x1B
  51. #define DA7210_OUTMIX_L 0x1C
  52. #define DA7210_OUTMIX_R 0x1D
  53. #define DA7210_HP_L_VOL 0x21
  54. #define DA7210_HP_R_VOL 0x22
  55. #define DA7210_HP_CFG 0x23
  56. #define DA7210_ZERO_CROSS 0x24
  57. #define DA7210_DAI_SRC_SEL 0x25
  58. #define DA7210_DAI_CFG1 0x26
  59. #define DA7210_DAI_CFG3 0x28
  60. #define DA7210_PLL_DIV1 0x29
  61. #define DA7210_PLL_DIV2 0x2A
  62. #define DA7210_PLL_DIV3 0x2B
  63. #define DA7210_PLL 0x2C
  64. #define DA7210_ALC_MAX 0x83
  65. #define DA7210_ALC_MIN 0x84
  66. #define DA7210_ALC_NOIS 0x85
  67. #define DA7210_ALC_ATT 0x86
  68. #define DA7210_ALC_REL 0x87
  69. #define DA7210_ALC_DEL 0x88
  70. #define DA7210_A_HID_UNLOCK 0x8A
  71. #define DA7210_A_TEST_UNLOCK 0x8B
  72. #define DA7210_A_PLL1 0x90
  73. #define DA7210_A_CP_MODE 0xA7
  74. /* STARTUP1 bit fields */
  75. #define DA7210_SC_MST_EN (1 << 0)
  76. /* MIC_L bit fields */
  77. #define DA7210_MICBIAS_EN (1 << 6)
  78. #define DA7210_MIC_L_EN (1 << 7)
  79. /* MIC_R bit fields */
  80. #define DA7210_MIC_R_EN (1 << 7)
  81. /* INMIX_L bit fields */
  82. #define DA7210_IN_L_EN (1 << 7)
  83. /* INMIX_R bit fields */
  84. #define DA7210_IN_R_EN (1 << 7)
  85. /* ADC bit fields */
  86. #define DA7210_ADC_ALC_EN (1 << 0)
  87. #define DA7210_ADC_L_EN (1 << 3)
  88. #define DA7210_ADC_R_EN (1 << 7)
  89. /* DAC/ADC HPF fields */
  90. #define DA7210_VOICE_F0_MASK (0x7 << 4)
  91. #define DA7210_VOICE_F0_25 (1 << 4)
  92. #define DA7210_VOICE_EN (1 << 7)
  93. /* DAC_SEL bit fields */
  94. #define DA7210_DAC_L_SRC_DAI_L (4 << 0)
  95. #define DA7210_DAC_L_EN (1 << 3)
  96. #define DA7210_DAC_R_SRC_DAI_R (5 << 4)
  97. #define DA7210_DAC_R_EN (1 << 7)
  98. /* OUTMIX_L bit fields */
  99. #define DA7210_OUT_L_EN (1 << 7)
  100. /* OUTMIX_R bit fields */
  101. #define DA7210_OUT_R_EN (1 << 7)
  102. /* HP_CFG bit fields */
  103. #define DA7210_HP_2CAP_MODE (1 << 1)
  104. #define DA7210_HP_SENSE_EN (1 << 2)
  105. #define DA7210_HP_L_EN (1 << 3)
  106. #define DA7210_HP_MODE (1 << 6)
  107. #define DA7210_HP_R_EN (1 << 7)
  108. /* DAI_SRC_SEL bit fields */
  109. #define DA7210_DAI_OUT_L_SRC (6 << 0)
  110. #define DA7210_DAI_OUT_R_SRC (7 << 4)
  111. /* DAI_CFG1 bit fields */
  112. #define DA7210_DAI_WORD_S16_LE (0 << 0)
  113. #define DA7210_DAI_WORD_S20_3LE (1 << 0)
  114. #define DA7210_DAI_WORD_S24_LE (2 << 0)
  115. #define DA7210_DAI_WORD_S32_LE (3 << 0)
  116. #define DA7210_DAI_FLEN_64BIT (1 << 2)
  117. #define DA7210_DAI_MODE_SLAVE (0 << 7)
  118. #define DA7210_DAI_MODE_MASTER (1 << 7)
  119. /* DAI_CFG3 bit fields */
  120. #define DA7210_DAI_FORMAT_I2SMODE (0 << 0)
  121. #define DA7210_DAI_FORMAT_LEFT_J (1 << 0)
  122. #define DA7210_DAI_FORMAT_RIGHT_J (2 << 0)
  123. #define DA7210_DAI_OE (1 << 3)
  124. #define DA7210_DAI_EN (1 << 7)
  125. /*PLL_DIV3 bit fields */
  126. #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
  127. #define DA7210_PLL_BYP (1 << 6)
  128. /* PLL bit fields */
  129. #define DA7210_PLL_FS_MASK (0xF << 0)
  130. #define DA7210_PLL_FS_8000 (0x1 << 0)
  131. #define DA7210_PLL_FS_11025 (0x2 << 0)
  132. #define DA7210_PLL_FS_12000 (0x3 << 0)
  133. #define DA7210_PLL_FS_16000 (0x5 << 0)
  134. #define DA7210_PLL_FS_22050 (0x6 << 0)
  135. #define DA7210_PLL_FS_24000 (0x7 << 0)
  136. #define DA7210_PLL_FS_32000 (0x9 << 0)
  137. #define DA7210_PLL_FS_44100 (0xA << 0)
  138. #define DA7210_PLL_FS_48000 (0xB << 0)
  139. #define DA7210_PLL_FS_88200 (0xE << 0)
  140. #define DA7210_PLL_FS_96000 (0xF << 0)
  141. #define DA7210_PLL_EN (0x1 << 7)
  142. /* SOFTMUTE bit fields */
  143. #define DA7210_RAMP_EN (1 << 6)
  144. /* CONTROL bit fields */
  145. #define DA7210_NOISE_SUP_EN (1 << 3)
  146. /* IN_GAIN bit fields */
  147. #define DA7210_INPGA_L_VOL (0x0F << 0)
  148. #define DA7210_INPGA_R_VOL (0xF0 << 0)
  149. /* ZERO_CROSS bit fields */
  150. #define DA7210_AUX1_L_ZC (1 << 0)
  151. #define DA7210_AUX1_R_ZC (1 << 1)
  152. #define DA7210_HP_L_ZC (1 << 6)
  153. #define DA7210_HP_R_ZC (1 << 7)
  154. /* AUX1_L bit fields */
  155. #define DA7210_AUX1_L_VOL (0x3F << 0)
  156. /* AUX1_R bit fields */
  157. #define DA7210_AUX1_R_VOL (0x3F << 0)
  158. /* Minimum INPGA and AUX1 volume to enable noise suppression */
  159. #define DA7210_INPGA_MIN_VOL_NS 0x0A /* 10.5dB */
  160. #define DA7210_AUX1_MIN_VOL_NS 0x35 /* 6dB */
  161. #define DA7210_VERSION "0.0.1"
  162. /*
  163. * Playback Volume
  164. *
  165. * max : 0x3F (+15.0 dB)
  166. * (1.5 dB step)
  167. * min : 0x11 (-54.0 dB)
  168. * mute : 0x10
  169. * reserved : 0x00 - 0x0F
  170. *
  171. * Reserved area are considered as "mute".
  172. */
  173. static const unsigned int hp_out_tlv[] = {
  174. TLV_DB_RANGE_HEAD(2),
  175. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  176. /* -54 dB to +15 dB */
  177. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0),
  178. };
  179. static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
  180. static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
  181. /* ADC and DAC high pass filter f0 value */
  182. static const char const *da7210_hpf_cutoff_txt[] = {
  183. "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
  184. };
  185. static const struct soc_enum da7210_dac_hpf_cutoff =
  186. SOC_ENUM_SINGLE(DA7210_DAC_HPF, 0, 4, da7210_hpf_cutoff_txt);
  187. static const struct soc_enum da7210_adc_hpf_cutoff =
  188. SOC_ENUM_SINGLE(DA7210_ADC_HPF, 0, 4, da7210_hpf_cutoff_txt);
  189. /* ADC and DAC voice (8kHz) high pass cutoff value */
  190. static const char const *da7210_vf_cutoff_txt[] = {
  191. "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  192. };
  193. static const struct soc_enum da7210_dac_vf_cutoff =
  194. SOC_ENUM_SINGLE(DA7210_DAC_HPF, 4, 8, da7210_vf_cutoff_txt);
  195. static const struct soc_enum da7210_adc_vf_cutoff =
  196. SOC_ENUM_SINGLE(DA7210_ADC_HPF, 4, 8, da7210_vf_cutoff_txt);
  197. static const char *da7210_hp_mode_txt[] = {
  198. "Class H", "Class G"
  199. };
  200. static const struct soc_enum da7210_hp_mode_sel =
  201. SOC_ENUM_SINGLE(DA7210_HP_CFG, 0, 2, da7210_hp_mode_txt);
  202. /* ALC can be enabled only if noise suppression is disabled */
  203. static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
  204. struct snd_ctl_elem_value *ucontrol)
  205. {
  206. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  207. if (ucontrol->value.integer.value[0]) {
  208. /* Check if noise suppression is enabled */
  209. if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
  210. dev_dbg(codec->dev,
  211. "Disable noise suppression to enable ALC\n");
  212. return -EINVAL;
  213. }
  214. }
  215. /* If all conditions are met or we are actually disabling ALC */
  216. return snd_soc_put_volsw(kcontrol, ucontrol);
  217. }
  218. /* Noise suppression can be enabled only if following conditions are met
  219. * ALC disabled
  220. * ZC enabled for HP and AUX1 PGA
  221. * INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
  222. * AUX1_L_VOL and AUX1_R_VOL >= 6 dB
  223. */
  224. static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
  225. struct snd_ctl_elem_value *ucontrol)
  226. {
  227. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  228. u8 val;
  229. if (ucontrol->value.integer.value[0]) {
  230. /* Check if ALC is enabled */
  231. if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
  232. goto err;
  233. /* Check ZC for HP and AUX1 PGA */
  234. if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
  235. (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
  236. DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
  237. DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
  238. goto err;
  239. /* Check INPGA_L_VOL and INPGA_R_VOL */
  240. val = snd_soc_read(codec, DA7210_IN_GAIN);
  241. if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
  242. (((val & DA7210_INPGA_R_VOL) >> 4) <
  243. DA7210_INPGA_MIN_VOL_NS))
  244. goto err;
  245. /* Check AUX1_L_VOL and AUX1_R_VOL */
  246. if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
  247. DA7210_AUX1_MIN_VOL_NS) ||
  248. ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
  249. DA7210_AUX1_MIN_VOL_NS))
  250. goto err;
  251. }
  252. /* If all conditions are met or we are actually disabling Noise sup */
  253. return snd_soc_put_volsw(kcontrol, ucontrol);
  254. err:
  255. return -EINVAL;
  256. }
  257. static const struct snd_kcontrol_new da7210_snd_controls[] = {
  258. SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
  259. DA7210_HP_L_VOL, DA7210_HP_R_VOL,
  260. 0, 0x3F, 0, hp_out_tlv),
  261. /* DAC Equalizer controls */
  262. SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
  263. SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
  264. eq_gain_tlv),
  265. SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
  266. eq_gain_tlv),
  267. SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
  268. eq_gain_tlv),
  269. SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
  270. eq_gain_tlv),
  271. SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
  272. eq_gain_tlv),
  273. /* ADC Equalizer controls */
  274. SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
  275. SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
  276. 1, adc_eq_master_gain_tlv),
  277. SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
  278. eq_gain_tlv),
  279. SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
  280. eq_gain_tlv),
  281. SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
  282. eq_gain_tlv),
  283. SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
  284. eq_gain_tlv),
  285. SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
  286. eq_gain_tlv),
  287. SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
  288. SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
  289. SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
  290. SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
  291. SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
  292. SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
  293. SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
  294. SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
  295. /* Mute controls */
  296. SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
  297. SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
  298. SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
  299. SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
  300. SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
  301. /* Zero cross controls */
  302. SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
  303. SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
  304. SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
  305. SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
  306. SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
  307. /* ALC controls */
  308. SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
  309. snd_soc_get_volsw, da7210_put_alc_sw),
  310. SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
  311. SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
  312. SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
  313. SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
  314. SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
  315. SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
  316. SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
  317. 0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
  318. };
  319. /* Codec private data */
  320. struct da7210_priv {
  321. enum snd_soc_control_type control_type;
  322. };
  323. /*
  324. * Register cache
  325. */
  326. static const u8 da7210_reg[] = {
  327. 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R0 - R7 */
  328. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, /* R8 - RF */
  329. 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54, /* R10 - R17 */
  330. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R18 - R1F */
  331. 0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00, /* R20 - R27 */
  332. 0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00, /* R28 - R2F */
  333. 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, /* R30 - R37 */
  334. 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00, /* R38 - R3F */
  335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R40 - R4F */
  336. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R48 - R4F */
  337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R50 - R57 */
  338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R58 - R5F */
  339. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R60 - R67 */
  340. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R68 - R6F */
  341. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* R70 - R77 */
  342. 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00, /* R78 - R7F */
  343. 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, /* R80 - R87 */
  344. 0x00, /* R88 */
  345. };
  346. static int da7210_volatile_register(struct snd_soc_codec *codec,
  347. unsigned int reg)
  348. {
  349. switch (reg) {
  350. case DA7210_STATUS:
  351. return 1;
  352. default:
  353. return 0;
  354. }
  355. }
  356. static int da7210_startup(struct snd_pcm_substream *substream,
  357. struct snd_soc_dai *dai)
  358. {
  359. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  360. struct snd_soc_codec *codec = dai->codec;
  361. if (is_play) {
  362. /* Enable Out */
  363. snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
  364. snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
  365. } else {
  366. /* Volume 7 */
  367. snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
  368. snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
  369. /* Enable Mic */
  370. snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
  371. snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
  372. }
  373. return 0;
  374. }
  375. /*
  376. * Set PCM DAI word length.
  377. */
  378. static int da7210_hw_params(struct snd_pcm_substream *substream,
  379. struct snd_pcm_hw_params *params,
  380. struct snd_soc_dai *dai)
  381. {
  382. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  383. struct snd_soc_codec *codec = rtd->codec;
  384. u32 dai_cfg1;
  385. u32 fs, bypass;
  386. /* set DAI source to Left and Right ADC */
  387. snd_soc_write(codec, DA7210_DAI_SRC_SEL,
  388. DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
  389. /* Enable DAI */
  390. snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
  391. dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
  392. switch (params_format(params)) {
  393. case SNDRV_PCM_FORMAT_S16_LE:
  394. dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
  395. break;
  396. case SNDRV_PCM_FORMAT_S20_3LE:
  397. dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
  398. break;
  399. case SNDRV_PCM_FORMAT_S24_LE:
  400. dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
  401. break;
  402. case SNDRV_PCM_FORMAT_S32_LE:
  403. dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  409. switch (params_rate(params)) {
  410. case 8000:
  411. fs = DA7210_PLL_FS_8000;
  412. bypass = DA7210_PLL_BYP;
  413. break;
  414. case 11025:
  415. fs = DA7210_PLL_FS_11025;
  416. bypass = 0;
  417. break;
  418. case 12000:
  419. fs = DA7210_PLL_FS_12000;
  420. bypass = DA7210_PLL_BYP;
  421. break;
  422. case 16000:
  423. fs = DA7210_PLL_FS_16000;
  424. bypass = DA7210_PLL_BYP;
  425. break;
  426. case 22050:
  427. fs = DA7210_PLL_FS_22050;
  428. bypass = 0;
  429. break;
  430. case 32000:
  431. fs = DA7210_PLL_FS_32000;
  432. bypass = DA7210_PLL_BYP;
  433. break;
  434. case 44100:
  435. fs = DA7210_PLL_FS_44100;
  436. bypass = 0;
  437. break;
  438. case 48000:
  439. fs = DA7210_PLL_FS_48000;
  440. bypass = DA7210_PLL_BYP;
  441. break;
  442. case 88200:
  443. fs = DA7210_PLL_FS_88200;
  444. bypass = 0;
  445. break;
  446. case 96000:
  447. fs = DA7210_PLL_FS_96000;
  448. bypass = DA7210_PLL_BYP;
  449. break;
  450. default:
  451. return -EINVAL;
  452. }
  453. /* Disable active mode */
  454. snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
  455. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
  456. snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
  457. /* Enable active mode */
  458. snd_soc_update_bits(codec, DA7210_STARTUP1,
  459. DA7210_SC_MST_EN, DA7210_SC_MST_EN);
  460. return 0;
  461. }
  462. /*
  463. * Set DAI mode and Format
  464. */
  465. static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
  466. {
  467. struct snd_soc_codec *codec = codec_dai->codec;
  468. u32 dai_cfg1;
  469. u32 dai_cfg3;
  470. dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
  471. dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
  472. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  473. case SND_SOC_DAIFMT_CBM_CFM:
  474. dai_cfg1 |= DA7210_DAI_MODE_MASTER;
  475. break;
  476. case SND_SOC_DAIFMT_CBS_CFS:
  477. dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. /* FIXME
  483. *
  484. * It support I2S only now
  485. */
  486. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  487. case SND_SOC_DAIFMT_I2S:
  488. dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
  489. break;
  490. case SND_SOC_DAIFMT_LEFT_J:
  491. dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
  492. break;
  493. case SND_SOC_DAIFMT_RIGHT_J:
  494. dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. /* FIXME
  500. *
  501. * It support 64bit data transmission only now
  502. */
  503. dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
  504. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  505. snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
  506. return 0;
  507. }
  508. static int da7210_mute(struct snd_soc_dai *dai, int mute)
  509. {
  510. struct snd_soc_codec *codec = dai->codec;
  511. u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
  512. if (mute)
  513. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
  514. else
  515. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
  516. return 0;
  517. }
  518. #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  519. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  520. /* DAI operations */
  521. static struct snd_soc_dai_ops da7210_dai_ops = {
  522. .startup = da7210_startup,
  523. .hw_params = da7210_hw_params,
  524. .set_fmt = da7210_set_dai_fmt,
  525. .digital_mute = da7210_mute,
  526. };
  527. static struct snd_soc_dai_driver da7210_dai = {
  528. .name = "da7210-hifi",
  529. /* playback capabilities */
  530. .playback = {
  531. .stream_name = "Playback",
  532. .channels_min = 1,
  533. .channels_max = 2,
  534. .rates = SNDRV_PCM_RATE_8000_96000,
  535. .formats = DA7210_FORMATS,
  536. },
  537. /* capture capabilities */
  538. .capture = {
  539. .stream_name = "Capture",
  540. .channels_min = 1,
  541. .channels_max = 2,
  542. .rates = SNDRV_PCM_RATE_8000_96000,
  543. .formats = DA7210_FORMATS,
  544. },
  545. .ops = &da7210_dai_ops,
  546. .symmetric_rates = 1,
  547. };
  548. static int da7210_probe(struct snd_soc_codec *codec)
  549. {
  550. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  551. int ret;
  552. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  553. ret = snd_soc_codec_set_cache_io(codec, 8, 8, da7210->control_type);
  554. if (ret < 0) {
  555. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  556. return ret;
  557. }
  558. /* FIXME
  559. *
  560. * This driver use fixed value here
  561. * And below settings expects MCLK = 12.288MHz
  562. *
  563. * When you select different MCLK, please check...
  564. * DA7210_PLL_DIV1 val
  565. * DA7210_PLL_DIV2 val
  566. * DA7210_PLL_DIV3 val
  567. * DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
  568. */
  569. /*
  570. * make sure that DA7210 use bypass mode before start up
  571. */
  572. snd_soc_write(codec, DA7210_STARTUP1, 0);
  573. snd_soc_write(codec, DA7210_PLL_DIV3,
  574. DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
  575. /*
  576. * ADC settings
  577. */
  578. /* Enable Left & Right MIC PGA and Mic Bias */
  579. snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
  580. snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
  581. /* Enable Left and Right input PGA */
  582. snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
  583. snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
  584. /* Enable Left and Right ADC */
  585. snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
  586. /*
  587. * DAC settings
  588. */
  589. /* Enable Left and Right DAC */
  590. snd_soc_write(codec, DA7210_DAC_SEL,
  591. DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
  592. DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
  593. /* Enable Left and Right out PGA */
  594. snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
  595. snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
  596. /* Enable Left and Right HeadPhone PGA */
  597. snd_soc_write(codec, DA7210_HP_CFG,
  598. DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
  599. DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
  600. /* Enable ramp mode for DAC gain update */
  601. snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
  602. /* Diable PLL and bypass it */
  603. snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
  604. /*
  605. * If 48kHz sound came, it use bypass mode,
  606. * and when it is 44.1kHz, it use PLL.
  607. *
  608. * This time, this driver sets PLL always ON
  609. * and controls bypass/PLL mode by switching
  610. * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
  611. * see da7210_hw_params
  612. */
  613. snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
  614. snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
  615. snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
  616. DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
  617. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
  618. /* As suggested by Dialog */
  619. snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x8B); /* unlock */
  620. snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0xB4);
  621. snd_soc_write(codec, DA7210_A_PLL1, 0x01);
  622. snd_soc_write(codec, DA7210_A_CP_MODE, 0x7C);
  623. snd_soc_write(codec, DA7210_A_HID_UNLOCK, 0x00); /* re-lock */
  624. snd_soc_write(codec, DA7210_A_TEST_UNLOCK, 0x00);
  625. /* Activate all enabled subsystem */
  626. snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
  627. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  628. return 0;
  629. }
  630. static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
  631. .probe = da7210_probe,
  632. .reg_cache_size = ARRAY_SIZE(da7210_reg),
  633. .reg_word_size = sizeof(u8),
  634. .reg_cache_default = da7210_reg,
  635. .volatile_register = da7210_volatile_register,
  636. .controls = da7210_snd_controls,
  637. .num_controls = ARRAY_SIZE(da7210_snd_controls),
  638. };
  639. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  640. static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
  641. const struct i2c_device_id *id)
  642. {
  643. struct da7210_priv *da7210;
  644. int ret;
  645. da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
  646. if (!da7210)
  647. return -ENOMEM;
  648. i2c_set_clientdata(i2c, da7210);
  649. da7210->control_type = SND_SOC_I2C;
  650. ret = snd_soc_register_codec(&i2c->dev,
  651. &soc_codec_dev_da7210, &da7210_dai, 1);
  652. if (ret < 0)
  653. kfree(da7210);
  654. return ret;
  655. }
  656. static int __devexit da7210_i2c_remove(struct i2c_client *client)
  657. {
  658. snd_soc_unregister_codec(&client->dev);
  659. kfree(i2c_get_clientdata(client));
  660. return 0;
  661. }
  662. static const struct i2c_device_id da7210_i2c_id[] = {
  663. { "da7210", 0 },
  664. { }
  665. };
  666. MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
  667. /* I2C codec control layer */
  668. static struct i2c_driver da7210_i2c_driver = {
  669. .driver = {
  670. .name = "da7210-codec",
  671. .owner = THIS_MODULE,
  672. },
  673. .probe = da7210_i2c_probe,
  674. .remove = __devexit_p(da7210_i2c_remove),
  675. .id_table = da7210_i2c_id,
  676. };
  677. #endif
  678. static int __init da7210_modinit(void)
  679. {
  680. int ret = 0;
  681. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  682. ret = i2c_add_driver(&da7210_i2c_driver);
  683. #endif
  684. return ret;
  685. }
  686. module_init(da7210_modinit);
  687. static void __exit da7210_exit(void)
  688. {
  689. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  690. i2c_del_driver(&da7210_i2c_driver);
  691. #endif
  692. }
  693. module_exit(da7210_exit);
  694. MODULE_DESCRIPTION("ASoC DA7210 driver");
  695. MODULE_AUTHOR("David Chen, Kuninori Morimoto");
  696. MODULE_LICENSE("GPL");