da7210.c 18 KB

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