da7210.c 40 KB

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