da7210.c 18 KB

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