ak4642.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
  3. *
  4. * Copyright (C) 2009 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on wm8731.c by Richard Purdie
  8. * Based on ak4535.c by Richard Purdie
  9. * Based on wm8753.c by Liam Girdwood
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. /* ** CAUTION **
  16. *
  17. * This is very simple driver.
  18. * It can use headphone output / stereo input only
  19. *
  20. * AK4642 is not tested.
  21. * AK4643 is tested.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/soc.h>
  34. #include <sound/soc-dapm.h>
  35. #include <sound/initval.h>
  36. #include "ak4642.h"
  37. #define AK4642_VERSION "0.0.1"
  38. #define PW_MGMT1 0x00
  39. #define PW_MGMT2 0x01
  40. #define SG_SL1 0x02
  41. #define SG_SL2 0x03
  42. #define MD_CTL1 0x04
  43. #define MD_CTL2 0x05
  44. #define TIMER 0x06
  45. #define ALC_CTL1 0x07
  46. #define ALC_CTL2 0x08
  47. #define L_IVC 0x09
  48. #define L_DVC 0x0a
  49. #define ALC_CTL3 0x0b
  50. #define R_IVC 0x0c
  51. #define R_DVC 0x0d
  52. #define MD_CTL3 0x0e
  53. #define MD_CTL4 0x0f
  54. #define PW_MGMT3 0x10
  55. #define DF_S 0x11
  56. #define FIL3_0 0x12
  57. #define FIL3_1 0x13
  58. #define FIL3_2 0x14
  59. #define FIL3_3 0x15
  60. #define EQ_0 0x16
  61. #define EQ_1 0x17
  62. #define EQ_2 0x18
  63. #define EQ_3 0x19
  64. #define EQ_4 0x1a
  65. #define EQ_5 0x1b
  66. #define FIL1_0 0x1c
  67. #define FIL1_1 0x1d
  68. #define FIL1_2 0x1e
  69. #define FIL1_3 0x1f
  70. #define PW_MGMT4 0x20
  71. #define MD_CTL5 0x21
  72. #define LO_MS 0x22
  73. #define HP_MS 0x23
  74. #define SPK_MS 0x24
  75. #define AK4642_CACHEREGNUM 0x25
  76. /* PW_MGMT2 */
  77. #define HPMTN (1 << 6)
  78. #define PMHPL (1 << 5)
  79. #define PMHPR (1 << 4)
  80. #define MS (1 << 3) /* master/slave select */
  81. #define MCKO (1 << 1)
  82. #define PMPLL (1 << 0)
  83. #define PMHP_MASK (PMHPL | PMHPR)
  84. #define PMHP PMHP_MASK
  85. /* MD_CTL1 */
  86. #define PLL3 (1 << 7)
  87. #define PLL2 (1 << 6)
  88. #define PLL1 (1 << 5)
  89. #define PLL0 (1 << 4)
  90. #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
  91. #define BCKO_MASK (1 << 3)
  92. #define BCKO_64 BCKO_MASK
  93. struct snd_soc_codec_device soc_codec_dev_ak4642;
  94. /* codec private data */
  95. struct ak4642_priv {
  96. struct snd_soc_codec codec;
  97. };
  98. static struct snd_soc_codec *ak4642_codec;
  99. /*
  100. * ak4642 register cache
  101. */
  102. static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
  103. 0x0000, 0x0000, 0x0001, 0x0000,
  104. 0x0002, 0x0000, 0x0000, 0x0000,
  105. 0x00e1, 0x00e1, 0x0018, 0x0000,
  106. 0x00e1, 0x0018, 0x0011, 0x0008,
  107. 0x0000, 0x0000, 0x0000, 0x0000,
  108. 0x0000, 0x0000, 0x0000, 0x0000,
  109. 0x0000, 0x0000, 0x0000, 0x0000,
  110. 0x0000, 0x0000, 0x0000, 0x0000,
  111. 0x0000, 0x0000, 0x0000, 0x0000,
  112. 0x0000,
  113. };
  114. /*
  115. * read ak4642 register cache
  116. */
  117. static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
  118. unsigned int reg)
  119. {
  120. u16 *cache = codec->reg_cache;
  121. if (reg >= AK4642_CACHEREGNUM)
  122. return -1;
  123. return cache[reg];
  124. }
  125. /*
  126. * write ak4642 register cache
  127. */
  128. static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
  129. u16 reg, unsigned int value)
  130. {
  131. u16 *cache = codec->reg_cache;
  132. if (reg >= AK4642_CACHEREGNUM)
  133. return;
  134. cache[reg] = value;
  135. }
  136. /*
  137. * write to the AK4642 register space
  138. */
  139. static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
  140. unsigned int value)
  141. {
  142. u8 data[2];
  143. /* data is
  144. * D15..D8 AK4642 register offset
  145. * D7...D0 register data
  146. */
  147. data[0] = reg & 0xff;
  148. data[1] = value & 0xff;
  149. if (codec->hw_write(codec->control_data, data, 2) == 2) {
  150. ak4642_write_reg_cache(codec, reg, value);
  151. return 0;
  152. } else
  153. return -EIO;
  154. }
  155. static int ak4642_sync(struct snd_soc_codec *codec)
  156. {
  157. u16 *cache = codec->reg_cache;
  158. int i, r = 0;
  159. for (i = 0; i < AK4642_CACHEREGNUM; i++)
  160. r |= ak4642_write(codec, i, cache[i]);
  161. return r;
  162. };
  163. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  164. struct snd_soc_dai *dai)
  165. {
  166. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  167. struct snd_soc_codec *codec = dai->codec;
  168. if (is_play) {
  169. /*
  170. * start headphone output
  171. *
  172. * PLL, Master Mode
  173. * Audio I/F Format :MSB justified (ADC & DAC)
  174. * Sampling Frequency: 44.1kHz
  175. * Digital Volume: −8dB
  176. * Bass Boost Level : Middle
  177. *
  178. * This operation came from example code of
  179. * "ASAHI KASEI AK4642" (japanese) manual p97.
  180. */
  181. ak4642_write(codec, 0x05, 0x27);
  182. ak4642_write(codec, 0x0f, 0x09);
  183. ak4642_write(codec, 0x0e, 0x19);
  184. ak4642_write(codec, 0x09, 0x91);
  185. ak4642_write(codec, 0x0c, 0x91);
  186. ak4642_write(codec, 0x0a, 0x28);
  187. ak4642_write(codec, 0x0d, 0x28);
  188. ak4642_write(codec, 0x00, 0x64);
  189. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
  190. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
  191. } else {
  192. /*
  193. * start stereo input
  194. *
  195. * PLL Master Mode
  196. * Audio I/F Format:MSB justified (ADC & DAC)
  197. * Sampling Frequency:44.1kHz
  198. * Pre MIC AMP:+20dB
  199. * MIC Power On
  200. * ALC setting:Refer to Table 35
  201. * ALC bit=“1”
  202. *
  203. * This operation came from example code of
  204. * "ASAHI KASEI AK4642" (japanese) manual p94.
  205. */
  206. ak4642_write(codec, 0x05, 0x27);
  207. ak4642_write(codec, 0x02, 0x05);
  208. ak4642_write(codec, 0x06, 0x3c);
  209. ak4642_write(codec, 0x08, 0xe1);
  210. ak4642_write(codec, 0x0b, 0x00);
  211. ak4642_write(codec, 0x07, 0x21);
  212. ak4642_write(codec, 0x00, 0x41);
  213. ak4642_write(codec, 0x10, 0x01);
  214. }
  215. return 0;
  216. }
  217. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  218. struct snd_soc_dai *dai)
  219. {
  220. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  221. struct snd_soc_codec *codec = dai->codec;
  222. if (is_play) {
  223. /* stop headphone output */
  224. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
  225. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
  226. ak4642_write(codec, 0x00, 0x40);
  227. ak4642_write(codec, 0x0e, 0x11);
  228. ak4642_write(codec, 0x0f, 0x08);
  229. } else {
  230. /* stop stereo input */
  231. ak4642_write(codec, 0x00, 0x40);
  232. ak4642_write(codec, 0x10, 0x00);
  233. ak4642_write(codec, 0x07, 0x01);
  234. }
  235. }
  236. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  237. int clk_id, unsigned int freq, int dir)
  238. {
  239. struct snd_soc_codec *codec = codec_dai->codec;
  240. u8 pll;
  241. switch (freq) {
  242. case 11289600:
  243. pll = PLL2;
  244. break;
  245. case 12288000:
  246. pll = PLL2 | PLL0;
  247. break;
  248. case 12000000:
  249. pll = PLL2 | PLL1;
  250. break;
  251. case 24000000:
  252. pll = PLL2 | PLL1 | PLL0;
  253. break;
  254. case 13500000:
  255. pll = PLL3 | PLL2;
  256. break;
  257. case 27000000:
  258. pll = PLL3 | PLL2 | PLL0;
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
  264. return 0;
  265. }
  266. static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  267. {
  268. struct snd_soc_codec *codec = dai->codec;
  269. u8 data;
  270. u8 bcko;
  271. data = MCKO | PMPLL; /* use MCKO */
  272. bcko = 0;
  273. /* set master/slave audio interface */
  274. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  275. case SND_SOC_DAIFMT_CBM_CFM:
  276. data |= MS;
  277. bcko = BCKO_64;
  278. break;
  279. case SND_SOC_DAIFMT_CBS_CFS:
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. snd_soc_update_bits(codec, PW_MGMT2, MS, data);
  285. snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
  286. return 0;
  287. }
  288. static struct snd_soc_dai_ops ak4642_dai_ops = {
  289. .startup = ak4642_dai_startup,
  290. .shutdown = ak4642_dai_shutdown,
  291. .set_sysclk = ak4642_dai_set_sysclk,
  292. .set_fmt = ak4642_dai_set_fmt,
  293. };
  294. struct snd_soc_dai ak4642_dai = {
  295. .name = "AK4642",
  296. .playback = {
  297. .stream_name = "Playback",
  298. .channels_min = 1,
  299. .channels_max = 2,
  300. .rates = SNDRV_PCM_RATE_8000_48000,
  301. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  302. .capture = {
  303. .stream_name = "Capture",
  304. .channels_min = 1,
  305. .channels_max = 2,
  306. .rates = SNDRV_PCM_RATE_8000_48000,
  307. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  308. .ops = &ak4642_dai_ops,
  309. };
  310. EXPORT_SYMBOL_GPL(ak4642_dai);
  311. static int ak4642_resume(struct platform_device *pdev)
  312. {
  313. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  314. struct snd_soc_codec *codec = socdev->card->codec;
  315. ak4642_sync(codec);
  316. return 0;
  317. }
  318. /*
  319. * initialise the AK4642 driver
  320. * register the mixer and dsp interfaces with the kernel
  321. */
  322. static int ak4642_init(struct ak4642_priv *ak4642)
  323. {
  324. struct snd_soc_codec *codec = &ak4642->codec;
  325. int ret = 0;
  326. if (ak4642_codec) {
  327. dev_err(codec->dev, "Another ak4642 is registered\n");
  328. return -EINVAL;
  329. }
  330. mutex_init(&codec->mutex);
  331. INIT_LIST_HEAD(&codec->dapm_widgets);
  332. INIT_LIST_HEAD(&codec->dapm_paths);
  333. codec->private_data = ak4642;
  334. codec->name = "AK4642";
  335. codec->owner = THIS_MODULE;
  336. codec->read = ak4642_read_reg_cache;
  337. codec->write = ak4642_write;
  338. codec->dai = &ak4642_dai;
  339. codec->num_dai = 1;
  340. codec->hw_write = (hw_write_t)i2c_master_send;
  341. codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
  342. codec->reg_cache = kmemdup(ak4642_reg,
  343. sizeof(ak4642_reg), GFP_KERNEL);
  344. if (!codec->reg_cache)
  345. return -ENOMEM;
  346. ak4642_dai.dev = codec->dev;
  347. ak4642_codec = codec;
  348. ret = snd_soc_register_codec(codec);
  349. if (ret) {
  350. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  351. goto reg_cache_err;
  352. }
  353. ret = snd_soc_register_dai(&ak4642_dai);
  354. if (ret) {
  355. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  356. snd_soc_unregister_codec(codec);
  357. goto reg_cache_err;
  358. }
  359. return ret;
  360. reg_cache_err:
  361. kfree(codec->reg_cache);
  362. codec->reg_cache = NULL;
  363. return ret;
  364. }
  365. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  366. static int ak4642_i2c_probe(struct i2c_client *i2c,
  367. const struct i2c_device_id *id)
  368. {
  369. struct ak4642_priv *ak4642;
  370. struct snd_soc_codec *codec;
  371. int ret;
  372. ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
  373. if (!ak4642)
  374. return -ENOMEM;
  375. codec = &ak4642->codec;
  376. codec->dev = &i2c->dev;
  377. i2c_set_clientdata(i2c, ak4642);
  378. codec->control_data = i2c;
  379. ret = ak4642_init(ak4642);
  380. if (ret < 0)
  381. printk(KERN_ERR "failed to initialise AK4642\n");
  382. return ret;
  383. }
  384. static int ak4642_i2c_remove(struct i2c_client *client)
  385. {
  386. struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
  387. snd_soc_unregister_dai(&ak4642_dai);
  388. snd_soc_unregister_codec(&ak4642->codec);
  389. kfree(ak4642->codec.reg_cache);
  390. kfree(ak4642);
  391. ak4642_codec = NULL;
  392. return 0;
  393. }
  394. static const struct i2c_device_id ak4642_i2c_id[] = {
  395. { "ak4642", 0 },
  396. { "ak4643", 0 },
  397. { }
  398. };
  399. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  400. static struct i2c_driver ak4642_i2c_driver = {
  401. .driver = {
  402. .name = "AK4642 I2C Codec",
  403. .owner = THIS_MODULE,
  404. },
  405. .probe = ak4642_i2c_probe,
  406. .remove = ak4642_i2c_remove,
  407. .id_table = ak4642_i2c_id,
  408. };
  409. #endif
  410. static int ak4642_probe(struct platform_device *pdev)
  411. {
  412. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  413. int ret;
  414. if (!ak4642_codec) {
  415. dev_err(&pdev->dev, "Codec device not registered\n");
  416. return -ENODEV;
  417. }
  418. socdev->card->codec = ak4642_codec;
  419. /* register pcms */
  420. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  421. if (ret < 0) {
  422. printk(KERN_ERR "ak4642: failed to create pcms\n");
  423. goto pcm_err;
  424. }
  425. dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
  426. return ret;
  427. pcm_err:
  428. return ret;
  429. }
  430. /* power down chip */
  431. static int ak4642_remove(struct platform_device *pdev)
  432. {
  433. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  434. snd_soc_free_pcms(socdev);
  435. snd_soc_dapm_free(socdev);
  436. return 0;
  437. }
  438. struct snd_soc_codec_device soc_codec_dev_ak4642 = {
  439. .probe = ak4642_probe,
  440. .remove = ak4642_remove,
  441. .resume = ak4642_resume,
  442. };
  443. EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
  444. static int __init ak4642_modinit(void)
  445. {
  446. int ret = 0;
  447. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  448. ret = i2c_add_driver(&ak4642_i2c_driver);
  449. #endif
  450. return ret;
  451. }
  452. module_init(ak4642_modinit);
  453. static void __exit ak4642_exit(void)
  454. {
  455. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  456. i2c_del_driver(&ak4642_i2c_driver);
  457. #endif
  458. }
  459. module_exit(ak4642_exit);
  460. MODULE_DESCRIPTION("Soc AK4642 driver");
  461. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  462. MODULE_LICENSE("GPL");