ak4642.c 13 KB

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