da7210.c 17 KB

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