da7210.c 18 KB

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