da7210.c 18 KB

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