ak4642.c 13 KB

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