da7210.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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_PLL_DIV_L_MASK (0xF << 0)
  135. #define DA7210_MCLK_RANGE_10_20_MHZ (1 << 4)
  136. #define DA7210_PLL_BYP (1 << 6)
  137. /* PLL bit fields */
  138. #define DA7210_PLL_FS_MASK (0xF << 0)
  139. #define DA7210_PLL_FS_8000 (0x1 << 0)
  140. #define DA7210_PLL_FS_11025 (0x2 << 0)
  141. #define DA7210_PLL_FS_12000 (0x3 << 0)
  142. #define DA7210_PLL_FS_16000 (0x5 << 0)
  143. #define DA7210_PLL_FS_22050 (0x6 << 0)
  144. #define DA7210_PLL_FS_24000 (0x7 << 0)
  145. #define DA7210_PLL_FS_32000 (0x9 << 0)
  146. #define DA7210_PLL_FS_44100 (0xA << 0)
  147. #define DA7210_PLL_FS_48000 (0xB << 0)
  148. #define DA7210_PLL_FS_88200 (0xE << 0)
  149. #define DA7210_PLL_FS_96000 (0xF << 0)
  150. #define DA7210_MCLK_DET_EN (0x1 << 5)
  151. #define DA7210_MCLK_SRM_EN (0x1 << 6)
  152. #define DA7210_PLL_EN (0x1 << 7)
  153. /* SOFTMUTE bit fields */
  154. #define DA7210_RAMP_EN (1 << 6)
  155. /* CONTROL bit fields */
  156. #define DA7210_REG_EN (1 << 0)
  157. #define DA7210_BIAS_EN (1 << 2)
  158. #define DA7210_NOISE_SUP_EN (1 << 3)
  159. /* IN_GAIN bit fields */
  160. #define DA7210_INPGA_L_VOL (0x0F << 0)
  161. #define DA7210_INPGA_R_VOL (0xF0 << 0)
  162. /* ZERO_CROSS bit fields */
  163. #define DA7210_AUX1_L_ZC (1 << 0)
  164. #define DA7210_AUX1_R_ZC (1 << 1)
  165. #define DA7210_HP_L_ZC (1 << 6)
  166. #define DA7210_HP_R_ZC (1 << 7)
  167. /* AUX1_L bit fields */
  168. #define DA7210_AUX1_L_VOL (0x3F << 0)
  169. #define DA7210_AUX1_L_EN (1 << 7)
  170. /* AUX1_R bit fields */
  171. #define DA7210_AUX1_R_VOL (0x3F << 0)
  172. #define DA7210_AUX1_R_EN (1 << 7)
  173. /* AUX2 bit fields */
  174. #define DA7210_AUX2_EN (1 << 3)
  175. /* Minimum INPGA and AUX1 volume to enable noise suppression */
  176. #define DA7210_INPGA_MIN_VOL_NS 0x0A /* 10.5dB */
  177. #define DA7210_AUX1_MIN_VOL_NS 0x35 /* 6dB */
  178. /* OUT1_L bit fields */
  179. #define DA7210_OUT1_L_EN (1 << 7)
  180. /* OUT1_R bit fields */
  181. #define DA7210_OUT1_R_EN (1 << 7)
  182. /* OUT2 bit fields */
  183. #define DA7210_OUT2_OUTMIX_R (1 << 5)
  184. #define DA7210_OUT2_OUTMIX_L (1 << 6)
  185. #define DA7210_OUT2_EN (1 << 7)
  186. struct pll_div {
  187. int fref;
  188. int fout;
  189. u8 div1;
  190. u8 div2;
  191. u8 div3;
  192. u8 mode; /* 0 = slave, 1 = master */
  193. };
  194. /* PLL dividers table */
  195. static const struct pll_div da7210_pll_div[] = {
  196. /* for MASTER mode, fs = 44.1Khz */
  197. { 12000000, 2822400, 0xE8, 0x6C, 0x2, 1}, /* MCLK=12Mhz */
  198. { 13000000, 2822400, 0xDF, 0x28, 0xC, 1}, /* MCLK=13Mhz */
  199. { 13500000, 2822400, 0xDB, 0x0A, 0xD, 1}, /* MCLK=13.5Mhz */
  200. { 14400000, 2822400, 0xD4, 0x5A, 0x2, 1}, /* MCLK=14.4Mhz */
  201. { 19200000, 2822400, 0xBB, 0x43, 0x9, 1}, /* MCLK=19.2Mhz */
  202. { 19680000, 2822400, 0xB9, 0x6D, 0xA, 1}, /* MCLK=19.68Mhz */
  203. { 19800000, 2822400, 0xB8, 0xFB, 0xB, 1}, /* MCLK=19.8Mhz */
  204. /* for MASTER mode, fs = 48Khz */
  205. { 12000000, 3072000, 0xF3, 0x12, 0x7, 1}, /* MCLK=12Mhz */
  206. { 13000000, 3072000, 0xE8, 0xFD, 0x5, 1}, /* MCLK=13Mhz */
  207. { 13500000, 3072000, 0xE4, 0x82, 0x3, 1}, /* MCLK=13.5Mhz */
  208. { 14400000, 3072000, 0xDD, 0x3A, 0x0, 1}, /* MCLK=14.4Mhz */
  209. { 19200000, 3072000, 0xC1, 0xEB, 0x8, 1}, /* MCLK=19.2Mhz */
  210. { 19680000, 3072000, 0xBF, 0xEC, 0x0, 1}, /* MCLK=19.68Mhz */
  211. { 19800000, 3072000, 0xBF, 0x70, 0x0, 1}, /* MCLK=19.8Mhz */
  212. /* for SLAVE mode with SRM */
  213. { 12000000, 2822400, 0xED, 0xBF, 0x5, 0}, /* MCLK=12Mhz */
  214. { 13000000, 2822400, 0xE4, 0x13, 0x0, 0}, /* MCLK=13Mhz */
  215. { 13500000, 2822400, 0xDF, 0xC6, 0x8, 0}, /* MCLK=13.5Mhz */
  216. { 14400000, 2822400, 0xD8, 0xCA, 0x1, 0}, /* MCLK=14.4Mhz */
  217. { 19200000, 2822400, 0xBE, 0x97, 0x9, 0}, /* MCLK=19.2Mhz */
  218. { 19680000, 2822400, 0xBC, 0xAC, 0xD, 0}, /* MCLK=19.68Mhz */
  219. { 19800000, 2822400, 0xBC, 0x35, 0xE, 0}, /* MCLK=19.8Mhz */
  220. };
  221. enum clk_src {
  222. DA7210_CLKSRC_MCLK
  223. };
  224. #define DA7210_VERSION "0.0.1"
  225. /*
  226. * Playback Volume
  227. *
  228. * max : 0x3F (+15.0 dB)
  229. * (1.5 dB step)
  230. * min : 0x11 (-54.0 dB)
  231. * mute : 0x10
  232. * reserved : 0x00 - 0x0F
  233. *
  234. * Reserved area are considered as "mute".
  235. */
  236. static const DECLARE_TLV_DB_RANGE(hp_out_tlv,
  237. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  238. /* -54 dB to +15 dB */
  239. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
  240. );
  241. static const DECLARE_TLV_DB_RANGE(lineout_vol_tlv,
  242. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  243. /* -54dB to 15dB */
  244. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
  245. );
  246. static const DECLARE_TLV_DB_RANGE(mono_vol_tlv,
  247. 0x0, 0x2, TLV_DB_SCALE_ITEM(-1800, 0, 1),
  248. /* -18dB to 6dB */
  249. 0x3, 0x7, TLV_DB_SCALE_ITEM(-1800, 600, 0)
  250. );
  251. static const DECLARE_TLV_DB_RANGE(aux1_vol_tlv,
  252. 0x0, 0x10, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  253. /* -48dB to 21dB */
  254. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-4800, 150, 0)
  255. );
  256. static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
  257. static const DECLARE_TLV_DB_SCALE(adc_eq_master_gain_tlv, -1800, 600, 1);
  258. static const DECLARE_TLV_DB_SCALE(dac_gain_tlv, -7725, 75, 0);
  259. static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
  260. static const DECLARE_TLV_DB_SCALE(aux2_vol_tlv, -600, 600, 0);
  261. static const DECLARE_TLV_DB_SCALE(inpga_gain_tlv, -450, 150, 0);
  262. /* ADC and DAC high pass filter f0 value */
  263. static const char * const da7210_hpf_cutoff_txt[] = {
  264. "Fs/8192*pi", "Fs/4096*pi", "Fs/2048*pi", "Fs/1024*pi"
  265. };
  266. static SOC_ENUM_SINGLE_DECL(da7210_dac_hpf_cutoff,
  267. DA7210_DAC_HPF, 0, da7210_hpf_cutoff_txt);
  268. static SOC_ENUM_SINGLE_DECL(da7210_adc_hpf_cutoff,
  269. DA7210_ADC_HPF, 0, da7210_hpf_cutoff_txt);
  270. /* ADC and DAC voice (8kHz) high pass cutoff value */
  271. static const char * const da7210_vf_cutoff_txt[] = {
  272. "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  273. };
  274. static SOC_ENUM_SINGLE_DECL(da7210_dac_vf_cutoff,
  275. DA7210_DAC_HPF, 4, da7210_vf_cutoff_txt);
  276. static SOC_ENUM_SINGLE_DECL(da7210_adc_vf_cutoff,
  277. DA7210_ADC_HPF, 4, da7210_vf_cutoff_txt);
  278. static const char *da7210_hp_mode_txt[] = {
  279. "Class H", "Class G"
  280. };
  281. static SOC_ENUM_SINGLE_DECL(da7210_hp_mode_sel,
  282. DA7210_HP_CFG, 0, da7210_hp_mode_txt);
  283. /* ALC can be enabled only if noise suppression is disabled */
  284. static int da7210_put_alc_sw(struct snd_kcontrol *kcontrol,
  285. struct snd_ctl_elem_value *ucontrol)
  286. {
  287. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  288. if (ucontrol->value.integer.value[0]) {
  289. /* Check if noise suppression is enabled */
  290. if (snd_soc_read(codec, DA7210_CONTROL) & DA7210_NOISE_SUP_EN) {
  291. dev_dbg(codec->dev,
  292. "Disable noise suppression to enable ALC\n");
  293. return -EINVAL;
  294. }
  295. }
  296. /* If all conditions are met or we are actually disabling ALC */
  297. return snd_soc_put_volsw(kcontrol, ucontrol);
  298. }
  299. /* Noise suppression can be enabled only if following conditions are met
  300. * ALC disabled
  301. * ZC enabled for HP and AUX1 PGA
  302. * INPGA_L_VOL and INPGA_R_VOL >= 10.5 dB
  303. * AUX1_L_VOL and AUX1_R_VOL >= 6 dB
  304. */
  305. static int da7210_put_noise_sup_sw(struct snd_kcontrol *kcontrol,
  306. struct snd_ctl_elem_value *ucontrol)
  307. {
  308. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  309. u8 val;
  310. if (ucontrol->value.integer.value[0]) {
  311. /* Check if ALC is enabled */
  312. if (snd_soc_read(codec, DA7210_ADC) & DA7210_ADC_ALC_EN)
  313. goto err;
  314. /* Check ZC for HP and AUX1 PGA */
  315. if ((snd_soc_read(codec, DA7210_ZERO_CROSS) &
  316. (DA7210_AUX1_L_ZC | DA7210_AUX1_R_ZC | DA7210_HP_L_ZC |
  317. DA7210_HP_R_ZC)) != (DA7210_AUX1_L_ZC |
  318. DA7210_AUX1_R_ZC | DA7210_HP_L_ZC | DA7210_HP_R_ZC))
  319. goto err;
  320. /* Check INPGA_L_VOL and INPGA_R_VOL */
  321. val = snd_soc_read(codec, DA7210_IN_GAIN);
  322. if (((val & DA7210_INPGA_L_VOL) < DA7210_INPGA_MIN_VOL_NS) ||
  323. (((val & DA7210_INPGA_R_VOL) >> 4) <
  324. DA7210_INPGA_MIN_VOL_NS))
  325. goto err;
  326. /* Check AUX1_L_VOL and AUX1_R_VOL */
  327. if (((snd_soc_read(codec, DA7210_AUX1_L) & DA7210_AUX1_L_VOL) <
  328. DA7210_AUX1_MIN_VOL_NS) ||
  329. ((snd_soc_read(codec, DA7210_AUX1_R) & DA7210_AUX1_R_VOL) <
  330. DA7210_AUX1_MIN_VOL_NS))
  331. goto err;
  332. }
  333. /* If all conditions are met or we are actually disabling Noise sup */
  334. return snd_soc_put_volsw(kcontrol, ucontrol);
  335. err:
  336. return -EINVAL;
  337. }
  338. static const struct snd_kcontrol_new da7210_snd_controls[] = {
  339. SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
  340. DA7210_HP_L_VOL, DA7210_HP_R_VOL,
  341. 0, 0x3F, 0, hp_out_tlv),
  342. SOC_DOUBLE_R_TLV("Digital Playback Volume",
  343. DA7210_DAC_L, DA7210_DAC_R,
  344. 0, 0x77, 1, dac_gain_tlv),
  345. SOC_DOUBLE_R_TLV("Lineout Playback Volume",
  346. DA7210_OUT1_L, DA7210_OUT1_R,
  347. 0, 0x3f, 0, lineout_vol_tlv),
  348. SOC_SINGLE_TLV("Mono Playback Volume", DA7210_OUT2, 0, 0x7, 0,
  349. mono_vol_tlv),
  350. SOC_DOUBLE_R_TLV("Mic Capture Volume",
  351. DA7210_MIC_L, DA7210_MIC_R,
  352. 0, 0x5, 0, mic_vol_tlv),
  353. SOC_DOUBLE_R_TLV("Aux1 Capture Volume",
  354. DA7210_AUX1_L, DA7210_AUX1_R,
  355. 0, 0x3f, 0, aux1_vol_tlv),
  356. SOC_SINGLE_TLV("Aux2 Capture Volume", DA7210_AUX2, 0, 0x3, 0,
  357. aux2_vol_tlv),
  358. SOC_DOUBLE_TLV("In PGA Capture Volume", DA7210_IN_GAIN, 0, 4, 0xF, 0,
  359. inpga_gain_tlv),
  360. /* DAC Equalizer controls */
  361. SOC_SINGLE("DAC EQ Switch", DA7210_DAC_EQ5, 7, 1, 0),
  362. SOC_SINGLE_TLV("DAC EQ1 Volume", DA7210_DAC_EQ1_2, 0, 0xf, 1,
  363. eq_gain_tlv),
  364. SOC_SINGLE_TLV("DAC EQ2 Volume", DA7210_DAC_EQ1_2, 4, 0xf, 1,
  365. eq_gain_tlv),
  366. SOC_SINGLE_TLV("DAC EQ3 Volume", DA7210_DAC_EQ3_4, 0, 0xf, 1,
  367. eq_gain_tlv),
  368. SOC_SINGLE_TLV("DAC EQ4 Volume", DA7210_DAC_EQ3_4, 4, 0xf, 1,
  369. eq_gain_tlv),
  370. SOC_SINGLE_TLV("DAC EQ5 Volume", DA7210_DAC_EQ5, 0, 0xf, 1,
  371. eq_gain_tlv),
  372. /* ADC Equalizer controls */
  373. SOC_SINGLE("ADC EQ Switch", DA7210_ADC_EQ5, 7, 1, 0),
  374. SOC_SINGLE_TLV("ADC EQ Master Volume", DA7210_ADC_EQ5, 4, 0x3,
  375. 1, adc_eq_master_gain_tlv),
  376. SOC_SINGLE_TLV("ADC EQ1 Volume", DA7210_ADC_EQ1_2, 0, 0xf, 1,
  377. eq_gain_tlv),
  378. SOC_SINGLE_TLV("ADC EQ2 Volume", DA7210_ADC_EQ1_2, 4, 0xf, 1,
  379. eq_gain_tlv),
  380. SOC_SINGLE_TLV("ADC EQ3 Volume", DA7210_ADC_EQ3_4, 0, 0xf, 1,
  381. eq_gain_tlv),
  382. SOC_SINGLE_TLV("ADC EQ4 Volume", DA7210_ADC_EQ3_4, 4, 0xf, 1,
  383. eq_gain_tlv),
  384. SOC_SINGLE_TLV("ADC EQ5 Volume", DA7210_ADC_EQ5, 0, 0xf, 1,
  385. eq_gain_tlv),
  386. SOC_SINGLE("DAC HPF Switch", DA7210_DAC_HPF, 3, 1, 0),
  387. SOC_ENUM("DAC HPF Cutoff", da7210_dac_hpf_cutoff),
  388. SOC_SINGLE("DAC Voice Mode Switch", DA7210_DAC_HPF, 7, 1, 0),
  389. SOC_ENUM("DAC Voice Cutoff", da7210_dac_vf_cutoff),
  390. SOC_SINGLE("ADC HPF Switch", DA7210_ADC_HPF, 3, 1, 0),
  391. SOC_ENUM("ADC HPF Cutoff", da7210_adc_hpf_cutoff),
  392. SOC_SINGLE("ADC Voice Mode Switch", DA7210_ADC_HPF, 7, 1, 0),
  393. SOC_ENUM("ADC Voice Cutoff", da7210_adc_vf_cutoff),
  394. /* Mute controls */
  395. SOC_DOUBLE_R("Mic Capture Switch", DA7210_MIC_L, DA7210_MIC_R, 3, 1, 0),
  396. SOC_SINGLE("Aux2 Capture Switch", DA7210_AUX2, 2, 1, 0),
  397. SOC_DOUBLE("ADC Capture Switch", DA7210_ADC, 2, 6, 1, 0),
  398. SOC_SINGLE("Digital Soft Mute Switch", DA7210_SOFTMUTE, 7, 1, 0),
  399. SOC_SINGLE("Digital Soft Mute Rate", DA7210_SOFTMUTE, 0, 0x7, 0),
  400. /* Zero cross controls */
  401. SOC_DOUBLE("Aux1 ZC Switch", DA7210_ZERO_CROSS, 0, 1, 1, 0),
  402. SOC_DOUBLE("In PGA ZC Switch", DA7210_ZERO_CROSS, 2, 3, 1, 0),
  403. SOC_DOUBLE("Lineout ZC Switch", DA7210_ZERO_CROSS, 4, 5, 1, 0),
  404. SOC_DOUBLE("Headphone ZC Switch", DA7210_ZERO_CROSS, 6, 7, 1, 0),
  405. SOC_ENUM("Headphone Class", da7210_hp_mode_sel),
  406. /* ALC controls */
  407. SOC_SINGLE_EXT("ALC Enable Switch", DA7210_ADC, 0, 1, 0,
  408. snd_soc_get_volsw, da7210_put_alc_sw),
  409. SOC_SINGLE("ALC Capture Max Volume", DA7210_ALC_MAX, 0, 0x3F, 0),
  410. SOC_SINGLE("ALC Capture Min Volume", DA7210_ALC_MIN, 0, 0x3F, 0),
  411. SOC_SINGLE("ALC Capture Noise Volume", DA7210_ALC_NOIS, 0, 0x3F, 0),
  412. SOC_SINGLE("ALC Capture Attack Rate", DA7210_ALC_ATT, 0, 0xFF, 0),
  413. SOC_SINGLE("ALC Capture Release Rate", DA7210_ALC_REL, 0, 0xFF, 0),
  414. SOC_SINGLE("ALC Capture Release Delay", DA7210_ALC_DEL, 0, 0xFF, 0),
  415. SOC_SINGLE_EXT("Noise Suppression Enable Switch", DA7210_CONTROL, 3, 1,
  416. 0, snd_soc_get_volsw, da7210_put_noise_sup_sw),
  417. };
  418. /*
  419. * DAPM Controls
  420. *
  421. * Current DAPM implementation covers almost all codec components e.g. IOs,
  422. * mixers, PGAs,ADC and DAC.
  423. */
  424. /* In Mixer Left */
  425. static const struct snd_kcontrol_new da7210_dapm_inmixl_controls[] = {
  426. SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_L, 0, 1, 0),
  427. SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_L, 1, 1, 0),
  428. SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_INMIX_L, 2, 1, 0),
  429. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_L, 3, 1, 0),
  430. SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_INMIX_L, 4, 1, 0),
  431. };
  432. /* In Mixer Right */
  433. static const struct snd_kcontrol_new da7210_dapm_inmixr_controls[] = {
  434. SOC_DAPM_SINGLE("Mic Right Switch", DA7210_INMIX_R, 0, 1, 0),
  435. SOC_DAPM_SINGLE("Mic Left Switch", DA7210_INMIX_R, 1, 1, 0),
  436. SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_INMIX_R, 2, 1, 0),
  437. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_INMIX_R, 3, 1, 0),
  438. SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_INMIX_R, 4, 1, 0),
  439. };
  440. /* Out Mixer Left */
  441. static const struct snd_kcontrol_new da7210_dapm_outmixl_controls[] = {
  442. SOC_DAPM_SINGLE("Aux1 Left Switch", DA7210_OUTMIX_L, 0, 1, 0),
  443. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_L, 1, 1, 0),
  444. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_L, 2, 1, 0),
  445. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_L, 3, 1, 0),
  446. SOC_DAPM_SINGLE("DAC Left Switch", DA7210_OUTMIX_L, 4, 1, 0),
  447. };
  448. /* Out Mixer Right */
  449. static const struct snd_kcontrol_new da7210_dapm_outmixr_controls[] = {
  450. SOC_DAPM_SINGLE("Aux1 Right Switch", DA7210_OUTMIX_R, 0, 1, 0),
  451. SOC_DAPM_SINGLE("Aux2 Switch", DA7210_OUTMIX_R, 1, 1, 0),
  452. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUTMIX_R, 2, 1, 0),
  453. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUTMIX_R, 3, 1, 0),
  454. SOC_DAPM_SINGLE("DAC Right Switch", DA7210_OUTMIX_R, 4, 1, 0),
  455. };
  456. /* Mono Mixer */
  457. static const struct snd_kcontrol_new da7210_dapm_monomix_controls[] = {
  458. SOC_DAPM_SINGLE("INPGA Right Switch", DA7210_OUT2, 3, 1, 0),
  459. SOC_DAPM_SINGLE("INPGA Left Switch", DA7210_OUT2, 4, 1, 0),
  460. SOC_DAPM_SINGLE("Outmix Right Switch", DA7210_OUT2, 5, 1, 0),
  461. SOC_DAPM_SINGLE("Outmix Left Switch", DA7210_OUT2, 6, 1, 0),
  462. };
  463. /* DAPM widgets */
  464. static const struct snd_soc_dapm_widget da7210_dapm_widgets[] = {
  465. /* Input Side */
  466. /* Input Lines */
  467. SND_SOC_DAPM_INPUT("MICL"),
  468. SND_SOC_DAPM_INPUT("MICR"),
  469. SND_SOC_DAPM_INPUT("AUX1L"),
  470. SND_SOC_DAPM_INPUT("AUX1R"),
  471. SND_SOC_DAPM_INPUT("AUX2"),
  472. /* Input PGAs */
  473. SND_SOC_DAPM_PGA("Mic Left", DA7210_STARTUP3, 0, 1, NULL, 0),
  474. SND_SOC_DAPM_PGA("Mic Right", DA7210_STARTUP3, 1, 1, NULL, 0),
  475. SND_SOC_DAPM_PGA("Aux1 Left", DA7210_STARTUP3, 2, 1, NULL, 0),
  476. SND_SOC_DAPM_PGA("Aux1 Right", DA7210_STARTUP3, 3, 1, NULL, 0),
  477. SND_SOC_DAPM_PGA("Aux2 Mono", DA7210_STARTUP3, 4, 1, NULL, 0),
  478. SND_SOC_DAPM_PGA("INPGA Left", DA7210_INMIX_L, 7, 0, NULL, 0),
  479. SND_SOC_DAPM_PGA("INPGA Right", DA7210_INMIX_R, 7, 0, NULL, 0),
  480. /* MICBIAS */
  481. SND_SOC_DAPM_SUPPLY("Mic Bias", DA7210_MIC_L, 6, 0, NULL, 0),
  482. /* Input Mixers */
  483. SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
  484. &da7210_dapm_inmixl_controls[0],
  485. ARRAY_SIZE(da7210_dapm_inmixl_controls)),
  486. SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
  487. &da7210_dapm_inmixr_controls[0],
  488. ARRAY_SIZE(da7210_dapm_inmixr_controls)),
  489. /* ADCs */
  490. SND_SOC_DAPM_ADC("ADC Left", "Capture", DA7210_STARTUP3, 5, 1),
  491. SND_SOC_DAPM_ADC("ADC Right", "Capture", DA7210_STARTUP3, 6, 1),
  492. /* Output Side */
  493. /* DACs */
  494. SND_SOC_DAPM_DAC("DAC Left", "Playback", DA7210_STARTUP2, 5, 1),
  495. SND_SOC_DAPM_DAC("DAC Right", "Playback", DA7210_STARTUP2, 6, 1),
  496. /* Output Mixers */
  497. SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
  498. &da7210_dapm_outmixl_controls[0],
  499. ARRAY_SIZE(da7210_dapm_outmixl_controls)),
  500. SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
  501. &da7210_dapm_outmixr_controls[0],
  502. ARRAY_SIZE(da7210_dapm_outmixr_controls)),
  503. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0,
  504. &da7210_dapm_monomix_controls[0],
  505. ARRAY_SIZE(da7210_dapm_monomix_controls)),
  506. /* Output PGAs */
  507. SND_SOC_DAPM_PGA("OUTPGA Left Enable", DA7210_OUTMIX_L, 7, 0, NULL, 0),
  508. SND_SOC_DAPM_PGA("OUTPGA Right Enable", DA7210_OUTMIX_R, 7, 0, NULL, 0),
  509. SND_SOC_DAPM_PGA("Out1 Left", DA7210_STARTUP2, 0, 1, NULL, 0),
  510. SND_SOC_DAPM_PGA("Out1 Right", DA7210_STARTUP2, 1, 1, NULL, 0),
  511. SND_SOC_DAPM_PGA("Out2 Mono", DA7210_STARTUP2, 2, 1, NULL, 0),
  512. SND_SOC_DAPM_PGA("Headphone Left", DA7210_STARTUP2, 3, 1, NULL, 0),
  513. SND_SOC_DAPM_PGA("Headphone Right", DA7210_STARTUP2, 4, 1, NULL, 0),
  514. /* Output Lines */
  515. SND_SOC_DAPM_OUTPUT("OUT1L"),
  516. SND_SOC_DAPM_OUTPUT("OUT1R"),
  517. SND_SOC_DAPM_OUTPUT("HPL"),
  518. SND_SOC_DAPM_OUTPUT("HPR"),
  519. SND_SOC_DAPM_OUTPUT("OUT2"),
  520. };
  521. /* DAPM audio route definition */
  522. static const struct snd_soc_dapm_route da7210_audio_map[] = {
  523. /* Dest Connecting Widget source */
  524. /* Input path */
  525. {"Mic Left", NULL, "MICL"},
  526. {"Mic Right", NULL, "MICR"},
  527. {"Aux1 Left", NULL, "AUX1L"},
  528. {"Aux1 Right", NULL, "AUX1R"},
  529. {"Aux2 Mono", NULL, "AUX2"},
  530. {"In Mixer Left", "Mic Left Switch", "Mic Left"},
  531. {"In Mixer Left", "Mic Right Switch", "Mic Right"},
  532. {"In Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
  533. {"In Mixer Left", "Aux2 Switch", "Aux2 Mono"},
  534. {"In Mixer Left", "Outmix Left Switch", "Out Mixer Left"},
  535. {"In Mixer Right", "Mic Right Switch", "Mic Right"},
  536. {"In Mixer Right", "Mic Left Switch", "Mic Left"},
  537. {"In Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
  538. {"In Mixer Right", "Aux2 Switch", "Aux2 Mono"},
  539. {"In Mixer Right", "Outmix Right Switch", "Out Mixer Right"},
  540. {"INPGA Left", NULL, "In Mixer Left"},
  541. {"ADC Left", NULL, "INPGA Left"},
  542. {"INPGA Right", NULL, "In Mixer Right"},
  543. {"ADC Right", NULL, "INPGA Right"},
  544. /* Output path */
  545. {"Out Mixer Left", "Aux1 Left Switch", "Aux1 Left"},
  546. {"Out Mixer Left", "Aux2 Switch", "Aux2 Mono"},
  547. {"Out Mixer Left", "INPGA Left Switch", "INPGA Left"},
  548. {"Out Mixer Left", "INPGA Right Switch", "INPGA Right"},
  549. {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
  550. {"Out Mixer Right", "Aux1 Right Switch", "Aux1 Right"},
  551. {"Out Mixer Right", "Aux2 Switch", "Aux2 Mono"},
  552. {"Out Mixer Right", "INPGA Right Switch", "INPGA Right"},
  553. {"Out Mixer Right", "INPGA Left Switch", "INPGA Left"},
  554. {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
  555. {"Mono Mixer", "INPGA Right Switch", "INPGA Right"},
  556. {"Mono Mixer", "INPGA Left Switch", "INPGA Left"},
  557. {"Mono Mixer", "Outmix Right Switch", "Out Mixer Right"},
  558. {"Mono Mixer", "Outmix Left Switch", "Out Mixer Left"},
  559. {"OUTPGA Left Enable", NULL, "Out Mixer Left"},
  560. {"OUTPGA Right Enable", NULL, "Out Mixer Right"},
  561. {"Out1 Left", NULL, "OUTPGA Left Enable"},
  562. {"OUT1L", NULL, "Out1 Left"},
  563. {"Out1 Right", NULL, "OUTPGA Right Enable"},
  564. {"OUT1R", NULL, "Out1 Right"},
  565. {"Headphone Left", NULL, "OUTPGA Left Enable"},
  566. {"HPL", NULL, "Headphone Left"},
  567. {"Headphone Right", NULL, "OUTPGA Right Enable"},
  568. {"HPR", NULL, "Headphone Right"},
  569. {"Out2 Mono", NULL, "Mono Mixer"},
  570. {"OUT2", NULL, "Out2 Mono"},
  571. };
  572. /* Codec private data */
  573. struct da7210_priv {
  574. struct regmap *regmap;
  575. unsigned int mclk_rate;
  576. int master;
  577. };
  578. static const struct reg_default da7210_reg_defaults[] = {
  579. { 0x00, 0x00 },
  580. { 0x01, 0x11 },
  581. { 0x03, 0x00 },
  582. { 0x04, 0x00 },
  583. { 0x05, 0x00 },
  584. { 0x06, 0x00 },
  585. { 0x07, 0x00 },
  586. { 0x08, 0x00 },
  587. { 0x09, 0x00 },
  588. { 0x0a, 0x00 },
  589. { 0x0b, 0x00 },
  590. { 0x0c, 0x00 },
  591. { 0x0d, 0x00 },
  592. { 0x0e, 0x00 },
  593. { 0x0f, 0x08 },
  594. { 0x10, 0x00 },
  595. { 0x11, 0x00 },
  596. { 0x12, 0x00 },
  597. { 0x13, 0x00 },
  598. { 0x14, 0x08 },
  599. { 0x15, 0x10 },
  600. { 0x16, 0x10 },
  601. { 0x17, 0x54 },
  602. { 0x18, 0x40 },
  603. { 0x19, 0x00 },
  604. { 0x1a, 0x00 },
  605. { 0x1b, 0x00 },
  606. { 0x1c, 0x00 },
  607. { 0x1d, 0x00 },
  608. { 0x1e, 0x00 },
  609. { 0x1f, 0x00 },
  610. { 0x20, 0x00 },
  611. { 0x21, 0x00 },
  612. { 0x22, 0x00 },
  613. { 0x23, 0x02 },
  614. { 0x24, 0x00 },
  615. { 0x25, 0x76 },
  616. { 0x26, 0x00 },
  617. { 0x27, 0x00 },
  618. { 0x28, 0x04 },
  619. { 0x29, 0x00 },
  620. { 0x2a, 0x00 },
  621. { 0x2b, 0x30 },
  622. { 0x2c, 0x2A },
  623. { 0x83, 0x00 },
  624. { 0x84, 0x00 },
  625. { 0x85, 0x00 },
  626. { 0x86, 0x00 },
  627. { 0x87, 0x00 },
  628. { 0x88, 0x00 },
  629. };
  630. static bool da7210_readable_register(struct device *dev, unsigned int reg)
  631. {
  632. switch (reg) {
  633. case DA7210_A_HID_UNLOCK:
  634. case DA7210_A_TEST_UNLOCK:
  635. case DA7210_A_PLL1:
  636. case DA7210_A_CP_MODE:
  637. return false;
  638. default:
  639. return true;
  640. }
  641. }
  642. static bool da7210_volatile_register(struct device *dev,
  643. unsigned int reg)
  644. {
  645. switch (reg) {
  646. case DA7210_STATUS:
  647. return true;
  648. default:
  649. return false;
  650. }
  651. }
  652. /*
  653. * Set PCM DAI word length.
  654. */
  655. static int da7210_hw_params(struct snd_pcm_substream *substream,
  656. struct snd_pcm_hw_params *params,
  657. struct snd_soc_dai *dai)
  658. {
  659. struct snd_soc_codec *codec = dai->codec;
  660. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  661. u32 dai_cfg1;
  662. u32 fs, sysclk;
  663. /* set DAI source to Left and Right ADC */
  664. snd_soc_write(codec, DA7210_DAI_SRC_SEL,
  665. DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
  666. /* Enable DAI */
  667. snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
  668. dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
  669. switch (params_width(params)) {
  670. case 16:
  671. dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
  672. break;
  673. case 20:
  674. dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
  675. break;
  676. case 24:
  677. dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
  678. break;
  679. case 32:
  680. dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
  681. break;
  682. default:
  683. return -EINVAL;
  684. }
  685. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  686. switch (params_rate(params)) {
  687. case 8000:
  688. fs = DA7210_PLL_FS_8000;
  689. sysclk = 3072000;
  690. break;
  691. case 11025:
  692. fs = DA7210_PLL_FS_11025;
  693. sysclk = 2822400;
  694. break;
  695. case 12000:
  696. fs = DA7210_PLL_FS_12000;
  697. sysclk = 3072000;
  698. break;
  699. case 16000:
  700. fs = DA7210_PLL_FS_16000;
  701. sysclk = 3072000;
  702. break;
  703. case 22050:
  704. fs = DA7210_PLL_FS_22050;
  705. sysclk = 2822400;
  706. break;
  707. case 32000:
  708. fs = DA7210_PLL_FS_32000;
  709. sysclk = 3072000;
  710. break;
  711. case 44100:
  712. fs = DA7210_PLL_FS_44100;
  713. sysclk = 2822400;
  714. break;
  715. case 48000:
  716. fs = DA7210_PLL_FS_48000;
  717. sysclk = 3072000;
  718. break;
  719. case 88200:
  720. fs = DA7210_PLL_FS_88200;
  721. sysclk = 2822400;
  722. break;
  723. case 96000:
  724. fs = DA7210_PLL_FS_96000;
  725. sysclk = 3072000;
  726. break;
  727. default:
  728. return -EINVAL;
  729. }
  730. /* Disable active mode */
  731. snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
  732. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
  733. if (da7210->mclk_rate && (da7210->mclk_rate != sysclk)) {
  734. /* PLL mode, disable PLL bypass */
  735. snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, 0);
  736. if (!da7210->master) {
  737. /* PLL slave mode, also enable SRM */
  738. snd_soc_update_bits(codec, DA7210_PLL,
  739. (DA7210_MCLK_SRM_EN |
  740. DA7210_MCLK_DET_EN),
  741. (DA7210_MCLK_SRM_EN |
  742. DA7210_MCLK_DET_EN));
  743. }
  744. } else {
  745. /* PLL bypass mode, enable PLL bypass and Auto Detection */
  746. snd_soc_update_bits(codec, DA7210_PLL, DA7210_MCLK_DET_EN,
  747. DA7210_MCLK_DET_EN);
  748. snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP,
  749. DA7210_PLL_BYP);
  750. }
  751. /* Enable active mode */
  752. snd_soc_update_bits(codec, DA7210_STARTUP1,
  753. DA7210_SC_MST_EN, DA7210_SC_MST_EN);
  754. return 0;
  755. }
  756. /*
  757. * Set DAI mode and Format
  758. */
  759. static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
  760. {
  761. struct snd_soc_codec *codec = codec_dai->codec;
  762. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  763. u32 dai_cfg1;
  764. u32 dai_cfg3;
  765. dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
  766. dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
  767. if ((snd_soc_read(codec, DA7210_PLL) & DA7210_PLL_EN) &&
  768. (!(snd_soc_read(codec, DA7210_PLL_DIV3) & DA7210_PLL_BYP)))
  769. return -EINVAL;
  770. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  771. case SND_SOC_DAIFMT_CBM_CFM:
  772. da7210->master = 1;
  773. dai_cfg1 |= DA7210_DAI_MODE_MASTER;
  774. break;
  775. case SND_SOC_DAIFMT_CBS_CFS:
  776. da7210->master = 0;
  777. dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
  778. break;
  779. default:
  780. return -EINVAL;
  781. }
  782. /* FIXME
  783. *
  784. * It support I2S only now
  785. */
  786. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  787. case SND_SOC_DAIFMT_I2S:
  788. dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
  789. break;
  790. case SND_SOC_DAIFMT_LEFT_J:
  791. dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
  792. break;
  793. case SND_SOC_DAIFMT_RIGHT_J:
  794. dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
  795. break;
  796. default:
  797. return -EINVAL;
  798. }
  799. /* FIXME
  800. *
  801. * It support 64bit data transmission only now
  802. */
  803. dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
  804. snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
  805. snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
  806. return 0;
  807. }
  808. static int da7210_mute(struct snd_soc_dai *dai, int mute)
  809. {
  810. struct snd_soc_codec *codec = dai->codec;
  811. u8 mute_reg = snd_soc_read(codec, DA7210_DAC_HPF) & 0xFB;
  812. if (mute)
  813. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg | 0x4);
  814. else
  815. snd_soc_write(codec, DA7210_DAC_HPF, mute_reg);
  816. return 0;
  817. }
  818. #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  819. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  820. static int da7210_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  821. int clk_id, unsigned int freq, int dir)
  822. {
  823. struct snd_soc_codec *codec = codec_dai->codec;
  824. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  825. switch (clk_id) {
  826. case DA7210_CLKSRC_MCLK:
  827. switch (freq) {
  828. case 12000000:
  829. case 13000000:
  830. case 13500000:
  831. case 14400000:
  832. case 19200000:
  833. case 19680000:
  834. case 19800000:
  835. da7210->mclk_rate = freq;
  836. return 0;
  837. default:
  838. dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
  839. freq);
  840. return -EINVAL;
  841. }
  842. break;
  843. default:
  844. dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
  845. return -EINVAL;
  846. }
  847. }
  848. /**
  849. * da7210_set_dai_pll :Configure the codec PLL
  850. * @param codec_dai : pointer to codec DAI
  851. * @param pll_id : da7210 has only one pll, so pll_id is always zero
  852. * @param fref : MCLK frequency, should be < 20MHz
  853. * @param fout : FsDM value, Refer page 44 & 45 of datasheet
  854. * @return int : Zero for success, negative error code for error
  855. *
  856. * Note: Supported PLL input frequencies are 12MHz, 13MHz, 13.5MHz, 14.4MHz,
  857. * 19.2MHz, 19.6MHz and 19.8MHz
  858. */
  859. static int da7210_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  860. int source, unsigned int fref, unsigned int fout)
  861. {
  862. struct snd_soc_codec *codec = codec_dai->codec;
  863. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  864. u8 pll_div1, pll_div2, pll_div3, cnt;
  865. /* In slave mode, there is only one set of divisors */
  866. if (!da7210->master)
  867. fout = 2822400;
  868. /* Search pll div array for correct divisors */
  869. for (cnt = 0; cnt < ARRAY_SIZE(da7210_pll_div); cnt++) {
  870. /* check fref, mode and fout */
  871. if ((fref == da7210_pll_div[cnt].fref) &&
  872. (da7210->master == da7210_pll_div[cnt].mode) &&
  873. (fout == da7210_pll_div[cnt].fout)) {
  874. /* all match, pick up divisors */
  875. pll_div1 = da7210_pll_div[cnt].div1;
  876. pll_div2 = da7210_pll_div[cnt].div2;
  877. pll_div3 = da7210_pll_div[cnt].div3;
  878. break;
  879. }
  880. }
  881. if (cnt >= ARRAY_SIZE(da7210_pll_div))
  882. goto err;
  883. /* Disable active mode */
  884. snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
  885. /* Write PLL dividers */
  886. snd_soc_write(codec, DA7210_PLL_DIV1, pll_div1);
  887. snd_soc_write(codec, DA7210_PLL_DIV2, pll_div2);
  888. snd_soc_update_bits(codec, DA7210_PLL_DIV3,
  889. DA7210_PLL_DIV_L_MASK, pll_div3);
  890. /* Enable PLL */
  891. snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
  892. /* Enable active mode */
  893. snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN,
  894. DA7210_SC_MST_EN);
  895. return 0;
  896. err:
  897. dev_err(codec_dai->dev, "Unsupported PLL input frequency %d\n", fref);
  898. return -EINVAL;
  899. }
  900. /* DAI operations */
  901. static const struct snd_soc_dai_ops da7210_dai_ops = {
  902. .hw_params = da7210_hw_params,
  903. .set_fmt = da7210_set_dai_fmt,
  904. .set_sysclk = da7210_set_dai_sysclk,
  905. .set_pll = da7210_set_dai_pll,
  906. .digital_mute = da7210_mute,
  907. };
  908. static struct snd_soc_dai_driver da7210_dai = {
  909. .name = "da7210-hifi",
  910. /* playback capabilities */
  911. .playback = {
  912. .stream_name = "Playback",
  913. .channels_min = 1,
  914. .channels_max = 2,
  915. .rates = SNDRV_PCM_RATE_8000_96000,
  916. .formats = DA7210_FORMATS,
  917. },
  918. /* capture capabilities */
  919. .capture = {
  920. .stream_name = "Capture",
  921. .channels_min = 1,
  922. .channels_max = 2,
  923. .rates = SNDRV_PCM_RATE_8000_96000,
  924. .formats = DA7210_FORMATS,
  925. },
  926. .ops = &da7210_dai_ops,
  927. .symmetric_rates = 1,
  928. };
  929. static int da7210_probe(struct snd_soc_codec *codec)
  930. {
  931. struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
  932. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  933. da7210->mclk_rate = 0; /* This will be set from set_sysclk() */
  934. da7210->master = 0; /* This will be set from set_fmt() */
  935. /* Enable internal regulator & bias current */
  936. snd_soc_write(codec, DA7210_CONTROL, DA7210_REG_EN | DA7210_BIAS_EN);
  937. /*
  938. * ADC settings
  939. */
  940. /* Enable Left & Right MIC PGA and Mic Bias */
  941. snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
  942. snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
  943. /* Enable Left and Right input PGA */
  944. snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
  945. snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
  946. /* Enable Left and Right ADC */
  947. snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
  948. /*
  949. * DAC settings
  950. */
  951. /* Enable Left and Right DAC */
  952. snd_soc_write(codec, DA7210_DAC_SEL,
  953. DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
  954. DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
  955. /* Enable Left and Right out PGA */
  956. snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
  957. snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
  958. /* Enable Left and Right HeadPhone PGA */
  959. snd_soc_write(codec, DA7210_HP_CFG,
  960. DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
  961. DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
  962. /* Enable ramp mode for DAC gain update */
  963. snd_soc_write(codec, DA7210_SOFTMUTE, DA7210_RAMP_EN);
  964. /*
  965. * For DA7210 codec, there are two ways to enable/disable analog IOs
  966. * and ADC/DAC,
  967. * (1) Using "Enable Bit" of register associated with that IO
  968. * (or ADC/DAC)
  969. * e.g. Mic Left can be enabled using bit 7 of MIC_L(0x7) reg
  970. *
  971. * (2) Using "Standby Bit" of STARTUP2 or STARTUP3 register
  972. * e.g. Mic left can be put to STANDBY using bit 0 of STARTUP3(0x5)
  973. *
  974. * Out of these two methods, the one using STANDBY bits is preferred
  975. * way to enable/disable individual blocks. This is because STANDBY
  976. * registers are part of system controller which allows system power
  977. * up/down in a controlled, pop-free manner. Also, as per application
  978. * note of DA7210, STANDBY register bits are only effective if a
  979. * particular IO (or ADC/DAC) is already enabled using enable/disable
  980. * register bits. Keeping these things in mind, current DAPM
  981. * implementation manipulates only STANDBY bits.
  982. *
  983. * Overall implementation can be outlined as below,
  984. *
  985. * - "Enable bit" of an IO or ADC/DAC is used to enable it in probe()
  986. * - "STANDBY bit" is controlled by DAPM
  987. */
  988. /* Enable Line out amplifiers */
  989. snd_soc_write(codec, DA7210_OUT1_L, DA7210_OUT1_L_EN);
  990. snd_soc_write(codec, DA7210_OUT1_R, DA7210_OUT1_R_EN);
  991. snd_soc_write(codec, DA7210_OUT2, DA7210_OUT2_EN |
  992. DA7210_OUT2_OUTMIX_L | DA7210_OUT2_OUTMIX_R);
  993. /* Enable Aux1 */
  994. snd_soc_write(codec, DA7210_AUX1_L, DA7210_AUX1_L_EN);
  995. snd_soc_write(codec, DA7210_AUX1_R, DA7210_AUX1_R_EN);
  996. /* Enable Aux2 */
  997. snd_soc_write(codec, DA7210_AUX2, DA7210_AUX2_EN);
  998. /* Set PLL Master clock range 10-20 MHz, enable PLL bypass */
  999. snd_soc_write(codec, DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ |
  1000. DA7210_PLL_BYP);
  1001. /* Diable PLL and bypass it */
  1002. snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
  1003. /* Activate all enabled subsystem */
  1004. snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
  1005. dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
  1006. return 0;
  1007. }
  1008. static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
  1009. .probe = da7210_probe,
  1010. .component_driver = {
  1011. .controls = da7210_snd_controls,
  1012. .num_controls = ARRAY_SIZE(da7210_snd_controls),
  1013. .dapm_widgets = da7210_dapm_widgets,
  1014. .num_dapm_widgets = ARRAY_SIZE(da7210_dapm_widgets),
  1015. .dapm_routes = da7210_audio_map,
  1016. .num_dapm_routes = ARRAY_SIZE(da7210_audio_map),
  1017. },
  1018. };
  1019. #if IS_ENABLED(CONFIG_I2C)
  1020. static const struct reg_sequence da7210_regmap_i2c_patch[] = {
  1021. /* System controller master disable */
  1022. { DA7210_STARTUP1, 0x00 },
  1023. /* Set PLL Master clock range 10-20 MHz */
  1024. { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ },
  1025. /* to unlock */
  1026. { DA7210_A_HID_UNLOCK, 0x8B},
  1027. { DA7210_A_TEST_UNLOCK, 0xB4},
  1028. { DA7210_A_PLL1, 0x01},
  1029. { DA7210_A_CP_MODE, 0x7C},
  1030. /* to re-lock */
  1031. { DA7210_A_HID_UNLOCK, 0x00},
  1032. { DA7210_A_TEST_UNLOCK, 0x00},
  1033. };
  1034. static const struct regmap_config da7210_regmap_config_i2c = {
  1035. .reg_bits = 8,
  1036. .val_bits = 8,
  1037. .reg_defaults = da7210_reg_defaults,
  1038. .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
  1039. .volatile_reg = da7210_volatile_register,
  1040. .readable_reg = da7210_readable_register,
  1041. .cache_type = REGCACHE_RBTREE,
  1042. };
  1043. static int da7210_i2c_probe(struct i2c_client *i2c,
  1044. const struct i2c_device_id *id)
  1045. {
  1046. struct da7210_priv *da7210;
  1047. int ret;
  1048. da7210 = devm_kzalloc(&i2c->dev, sizeof(struct da7210_priv),
  1049. GFP_KERNEL);
  1050. if (!da7210)
  1051. return -ENOMEM;
  1052. i2c_set_clientdata(i2c, da7210);
  1053. da7210->regmap = devm_regmap_init_i2c(i2c, &da7210_regmap_config_i2c);
  1054. if (IS_ERR(da7210->regmap)) {
  1055. ret = PTR_ERR(da7210->regmap);
  1056. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1057. return ret;
  1058. }
  1059. ret = regmap_register_patch(da7210->regmap, da7210_regmap_i2c_patch,
  1060. ARRAY_SIZE(da7210_regmap_i2c_patch));
  1061. if (ret != 0)
  1062. dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
  1063. ret = snd_soc_register_codec(&i2c->dev,
  1064. &soc_codec_dev_da7210, &da7210_dai, 1);
  1065. if (ret < 0)
  1066. dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
  1067. return ret;
  1068. }
  1069. static int da7210_i2c_remove(struct i2c_client *client)
  1070. {
  1071. snd_soc_unregister_codec(&client->dev);
  1072. return 0;
  1073. }
  1074. static const struct i2c_device_id da7210_i2c_id[] = {
  1075. { "da7210", 0 },
  1076. { }
  1077. };
  1078. MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
  1079. /* I2C codec control layer */
  1080. static struct i2c_driver da7210_i2c_driver = {
  1081. .driver = {
  1082. .name = "da7210",
  1083. },
  1084. .probe = da7210_i2c_probe,
  1085. .remove = da7210_i2c_remove,
  1086. .id_table = da7210_i2c_id,
  1087. };
  1088. #endif
  1089. #if defined(CONFIG_SPI_MASTER)
  1090. static const struct reg_sequence da7210_regmap_spi_patch[] = {
  1091. /* Dummy read to give two pulses over nCS for SPI */
  1092. { DA7210_AUX2, 0x00 },
  1093. { DA7210_AUX2, 0x00 },
  1094. /* System controller master disable */
  1095. { DA7210_STARTUP1, 0x00 },
  1096. /* Set PLL Master clock range 10-20 MHz */
  1097. { DA7210_PLL_DIV3, DA7210_MCLK_RANGE_10_20_MHZ },
  1098. /* to set PAGE1 of SPI register space */
  1099. { DA7210_PAGE_CONTROL, 0x80 },
  1100. /* to unlock */
  1101. { DA7210_A_HID_UNLOCK, 0x8B},
  1102. { DA7210_A_TEST_UNLOCK, 0xB4},
  1103. { DA7210_A_PLL1, 0x01},
  1104. { DA7210_A_CP_MODE, 0x7C},
  1105. /* to re-lock */
  1106. { DA7210_A_HID_UNLOCK, 0x00},
  1107. { DA7210_A_TEST_UNLOCK, 0x00},
  1108. /* to set back PAGE0 of SPI register space */
  1109. { DA7210_PAGE_CONTROL, 0x00 },
  1110. };
  1111. static const struct regmap_config da7210_regmap_config_spi = {
  1112. .reg_bits = 8,
  1113. .val_bits = 8,
  1114. .read_flag_mask = 0x01,
  1115. .write_flag_mask = 0x00,
  1116. .reg_defaults = da7210_reg_defaults,
  1117. .num_reg_defaults = ARRAY_SIZE(da7210_reg_defaults),
  1118. .volatile_reg = da7210_volatile_register,
  1119. .readable_reg = da7210_readable_register,
  1120. .cache_type = REGCACHE_RBTREE,
  1121. };
  1122. static int da7210_spi_probe(struct spi_device *spi)
  1123. {
  1124. struct da7210_priv *da7210;
  1125. int ret;
  1126. da7210 = devm_kzalloc(&spi->dev, sizeof(struct da7210_priv),
  1127. GFP_KERNEL);
  1128. if (!da7210)
  1129. return -ENOMEM;
  1130. spi_set_drvdata(spi, da7210);
  1131. da7210->regmap = devm_regmap_init_spi(spi, &da7210_regmap_config_spi);
  1132. if (IS_ERR(da7210->regmap)) {
  1133. ret = PTR_ERR(da7210->regmap);
  1134. dev_err(&spi->dev, "Failed to register regmap: %d\n", ret);
  1135. return ret;
  1136. }
  1137. ret = regmap_register_patch(da7210->regmap, da7210_regmap_spi_patch,
  1138. ARRAY_SIZE(da7210_regmap_spi_patch));
  1139. if (ret != 0)
  1140. dev_warn(&spi->dev, "Failed to apply regmap patch: %d\n", ret);
  1141. ret = snd_soc_register_codec(&spi->dev,
  1142. &soc_codec_dev_da7210, &da7210_dai, 1);
  1143. return ret;
  1144. }
  1145. static int da7210_spi_remove(struct spi_device *spi)
  1146. {
  1147. snd_soc_unregister_codec(&spi->dev);
  1148. return 0;
  1149. }
  1150. static struct spi_driver da7210_spi_driver = {
  1151. .driver = {
  1152. .name = "da7210",
  1153. },
  1154. .probe = da7210_spi_probe,
  1155. .remove = da7210_spi_remove
  1156. };
  1157. #endif
  1158. static int __init da7210_modinit(void)
  1159. {
  1160. int ret = 0;
  1161. #if IS_ENABLED(CONFIG_I2C)
  1162. ret = i2c_add_driver(&da7210_i2c_driver);
  1163. #endif
  1164. #if defined(CONFIG_SPI_MASTER)
  1165. ret = spi_register_driver(&da7210_spi_driver);
  1166. if (ret) {
  1167. printk(KERN_ERR "Failed to register da7210 SPI driver: %d\n",
  1168. ret);
  1169. }
  1170. #endif
  1171. return ret;
  1172. }
  1173. module_init(da7210_modinit);
  1174. static void __exit da7210_exit(void)
  1175. {
  1176. #if IS_ENABLED(CONFIG_I2C)
  1177. i2c_del_driver(&da7210_i2c_driver);
  1178. #endif
  1179. #if defined(CONFIG_SPI_MASTER)
  1180. spi_unregister_driver(&da7210_spi_driver);
  1181. #endif
  1182. }
  1183. module_exit(da7210_exit);
  1184. MODULE_DESCRIPTION("ASoC DA7210 driver");
  1185. MODULE_AUTHOR("David Chen, Kuninori Morimoto");
  1186. MODULE_LICENSE("GPL");