da7210.c 19 KB

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