ak4642.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. struct snd_soc_codec_device soc_codec_dev_ak4642;
  77. /* codec private data */
  78. struct ak4642_priv {
  79. struct snd_soc_codec codec;
  80. unsigned int sysclk;
  81. };
  82. static struct snd_soc_codec *ak4642_codec;
  83. /*
  84. * ak4642 register cache
  85. */
  86. static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
  87. 0x0000, 0x0000, 0x0001, 0x0000,
  88. 0x0002, 0x0000, 0x0000, 0x0000,
  89. 0x00e1, 0x00e1, 0x0018, 0x0000,
  90. 0x00e1, 0x0018, 0x0011, 0x0008,
  91. 0x0000, 0x0000, 0x0000, 0x0000,
  92. 0x0000, 0x0000, 0x0000, 0x0000,
  93. 0x0000, 0x0000, 0x0000, 0x0000,
  94. 0x0000, 0x0000, 0x0000, 0x0000,
  95. 0x0000, 0x0000, 0x0000, 0x0000,
  96. 0x0000,
  97. };
  98. /*
  99. * read ak4642 register cache
  100. */
  101. static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
  102. unsigned int reg)
  103. {
  104. u16 *cache = codec->reg_cache;
  105. if (reg >= AK4642_CACHEREGNUM)
  106. return -1;
  107. return cache[reg];
  108. }
  109. /*
  110. * write ak4642 register cache
  111. */
  112. static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
  113. u16 reg, unsigned int value)
  114. {
  115. u16 *cache = codec->reg_cache;
  116. if (reg >= AK4642_CACHEREGNUM)
  117. return;
  118. cache[reg] = value;
  119. }
  120. /*
  121. * write to the AK4642 register space
  122. */
  123. static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
  124. unsigned int value)
  125. {
  126. u8 data[2];
  127. /* data is
  128. * D15..D8 AK4642 register offset
  129. * D7...D0 register data
  130. */
  131. data[0] = reg & 0xff;
  132. data[1] = value & 0xff;
  133. if (codec->hw_write(codec->control_data, data, 2) == 2) {
  134. ak4642_write_reg_cache(codec, reg, value);
  135. return 0;
  136. } else
  137. return -EIO;
  138. }
  139. static int ak4642_sync(struct snd_soc_codec *codec)
  140. {
  141. u16 *cache = codec->reg_cache;
  142. int i, r = 0;
  143. for (i = 0; i < AK4642_CACHEREGNUM; i++)
  144. r |= ak4642_write(codec, i, cache[i]);
  145. return r;
  146. };
  147. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  148. struct snd_soc_dai *dai)
  149. {
  150. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  151. struct snd_soc_codec *codec = dai->codec;
  152. if (is_play) {
  153. /*
  154. * start headphone output
  155. *
  156. * PLL, Master Mode
  157. * Audio I/F Format :MSB justified (ADC & DAC)
  158. * Sampling Frequency: 44.1kHz
  159. * Digital Volume: −8dB
  160. * Bass Boost Level : Middle
  161. *
  162. * This operation came from example code of
  163. * "ASAHI KASEI AK4642" (japanese) manual p97.
  164. *
  165. * Example code use 0x39, 0x79 value for 0x01 address,
  166. * But we need MCKO (0x02) bit now
  167. */
  168. ak4642_write(codec, 0x05, 0x27);
  169. ak4642_write(codec, 0x0f, 0x09);
  170. ak4642_write(codec, 0x0e, 0x19);
  171. ak4642_write(codec, 0x09, 0x91);
  172. ak4642_write(codec, 0x0c, 0x91);
  173. ak4642_write(codec, 0x0a, 0x28);
  174. ak4642_write(codec, 0x0d, 0x28);
  175. ak4642_write(codec, 0x00, 0x64);
  176. ak4642_write(codec, 0x01, 0x3b); /* + MCKO bit */
  177. ak4642_write(codec, 0x01, 0x7b); /* + MCKO bit */
  178. } else {
  179. /*
  180. * start stereo input
  181. *
  182. * PLL Master Mode
  183. * Audio I/F Format:MSB justified (ADC & DAC)
  184. * Sampling Frequency:44.1kHz
  185. * Pre MIC AMP:+20dB
  186. * MIC Power On
  187. * ALC setting:Refer to Table 35
  188. * ALC bit=“1”
  189. *
  190. * This operation came from example code of
  191. * "ASAHI KASEI AK4642" (japanese) manual p94.
  192. */
  193. ak4642_write(codec, 0x05, 0x27);
  194. ak4642_write(codec, 0x02, 0x05);
  195. ak4642_write(codec, 0x06, 0x3c);
  196. ak4642_write(codec, 0x08, 0xe1);
  197. ak4642_write(codec, 0x0b, 0x00);
  198. ak4642_write(codec, 0x07, 0x21);
  199. ak4642_write(codec, 0x00, 0x41);
  200. ak4642_write(codec, 0x10, 0x01);
  201. }
  202. return 0;
  203. }
  204. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  205. struct snd_soc_dai *dai)
  206. {
  207. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  208. struct snd_soc_codec *codec = dai->codec;
  209. if (is_play) {
  210. /* stop headphone output */
  211. ak4642_write(codec, 0x01, 0x3b);
  212. ak4642_write(codec, 0x01, 0x0b);
  213. ak4642_write(codec, 0x00, 0x40);
  214. ak4642_write(codec, 0x0e, 0x11);
  215. ak4642_write(codec, 0x0f, 0x08);
  216. } else {
  217. /* stop stereo input */
  218. ak4642_write(codec, 0x00, 0x40);
  219. ak4642_write(codec, 0x10, 0x00);
  220. ak4642_write(codec, 0x07, 0x01);
  221. }
  222. }
  223. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  224. int clk_id, unsigned int freq, int dir)
  225. {
  226. struct snd_soc_codec *codec = codec_dai->codec;
  227. struct ak4642_priv *ak4642 = codec->private_data;
  228. ak4642->sysclk = freq;
  229. return 0;
  230. }
  231. static struct snd_soc_dai_ops ak4642_dai_ops = {
  232. .startup = ak4642_dai_startup,
  233. .shutdown = ak4642_dai_shutdown,
  234. .set_sysclk = ak4642_dai_set_sysclk,
  235. };
  236. struct snd_soc_dai ak4642_dai = {
  237. .name = "AK4642",
  238. .playback = {
  239. .stream_name = "Playback",
  240. .channels_min = 1,
  241. .channels_max = 2,
  242. .rates = SNDRV_PCM_RATE_8000_48000,
  243. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  244. .capture = {
  245. .stream_name = "Capture",
  246. .channels_min = 1,
  247. .channels_max = 2,
  248. .rates = SNDRV_PCM_RATE_8000_48000,
  249. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  250. .ops = &ak4642_dai_ops,
  251. };
  252. EXPORT_SYMBOL_GPL(ak4642_dai);
  253. static int ak4642_resume(struct platform_device *pdev)
  254. {
  255. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  256. struct snd_soc_codec *codec = socdev->card->codec;
  257. ak4642_sync(codec);
  258. return 0;
  259. }
  260. /*
  261. * initialise the AK4642 driver
  262. * register the mixer and dsp interfaces with the kernel
  263. */
  264. static int ak4642_init(struct ak4642_priv *ak4642)
  265. {
  266. struct snd_soc_codec *codec = &ak4642->codec;
  267. int ret = 0;
  268. if (ak4642_codec) {
  269. dev_err(codec->dev, "Another ak4642 is registered\n");
  270. return -EINVAL;
  271. }
  272. mutex_init(&codec->mutex);
  273. INIT_LIST_HEAD(&codec->dapm_widgets);
  274. INIT_LIST_HEAD(&codec->dapm_paths);
  275. codec->private_data = ak4642;
  276. codec->name = "AK4642";
  277. codec->owner = THIS_MODULE;
  278. codec->read = ak4642_read_reg_cache;
  279. codec->write = ak4642_write;
  280. codec->dai = &ak4642_dai;
  281. codec->num_dai = 1;
  282. codec->hw_write = (hw_write_t)i2c_master_send;
  283. codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
  284. codec->reg_cache = kmemdup(ak4642_reg,
  285. sizeof(ak4642_reg), GFP_KERNEL);
  286. if (!codec->reg_cache)
  287. return -ENOMEM;
  288. ak4642_dai.dev = codec->dev;
  289. ak4642_codec = codec;
  290. ret = snd_soc_register_codec(codec);
  291. if (ret) {
  292. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  293. goto reg_cache_err;
  294. }
  295. ret = snd_soc_register_dai(&ak4642_dai);
  296. if (ret) {
  297. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  298. snd_soc_unregister_codec(codec);
  299. goto reg_cache_err;
  300. }
  301. /*
  302. * clock setting
  303. *
  304. * Audio I/F Format: MSB justified (ADC & DAC)
  305. * BICK frequency at Master Mode: 64fs
  306. * Input Master Clock Select at PLL Mode: 11.2896MHz
  307. * MCKO: Enable
  308. * Sampling Frequency: 44.1kHz
  309. *
  310. * This operation came from example code of
  311. * "ASAHI KASEI AK4642" (japanese) manual p89.
  312. *
  313. * please fix-me
  314. */
  315. ak4642_write(codec, 0x01, 0x08);
  316. ak4642_write(codec, 0x04, 0x4a);
  317. ak4642_write(codec, 0x05, 0x27);
  318. ak4642_write(codec, 0x00, 0x40);
  319. ak4642_write(codec, 0x01, 0x0b);
  320. return ret;
  321. reg_cache_err:
  322. kfree(codec->reg_cache);
  323. codec->reg_cache = NULL;
  324. return ret;
  325. }
  326. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  327. static int ak4642_i2c_probe(struct i2c_client *i2c,
  328. const struct i2c_device_id *id)
  329. {
  330. struct ak4642_priv *ak4642;
  331. struct snd_soc_codec *codec;
  332. int ret;
  333. ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
  334. if (!ak4642)
  335. return -ENOMEM;
  336. codec = &ak4642->codec;
  337. codec->dev = &i2c->dev;
  338. i2c_set_clientdata(i2c, ak4642);
  339. codec->control_data = i2c;
  340. ret = ak4642_init(ak4642);
  341. if (ret < 0)
  342. printk(KERN_ERR "failed to initialise AK4642\n");
  343. return ret;
  344. }
  345. static int ak4642_i2c_remove(struct i2c_client *client)
  346. {
  347. struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
  348. snd_soc_unregister_dai(&ak4642_dai);
  349. snd_soc_unregister_codec(&ak4642->codec);
  350. kfree(ak4642->codec.reg_cache);
  351. kfree(ak4642);
  352. ak4642_codec = NULL;
  353. return 0;
  354. }
  355. static const struct i2c_device_id ak4642_i2c_id[] = {
  356. { "ak4642", 0 },
  357. { "ak4643", 0 },
  358. { }
  359. };
  360. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  361. static struct i2c_driver ak4642_i2c_driver = {
  362. .driver = {
  363. .name = "AK4642 I2C Codec",
  364. .owner = THIS_MODULE,
  365. },
  366. .probe = ak4642_i2c_probe,
  367. .remove = ak4642_i2c_remove,
  368. .id_table = ak4642_i2c_id,
  369. };
  370. #endif
  371. static int ak4642_probe(struct platform_device *pdev)
  372. {
  373. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  374. int ret;
  375. if (!ak4642_codec) {
  376. dev_err(&pdev->dev, "Codec device not registered\n");
  377. return -ENODEV;
  378. }
  379. socdev->card->codec = ak4642_codec;
  380. /* register pcms */
  381. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  382. if (ret < 0) {
  383. printk(KERN_ERR "ak4642: failed to create pcms\n");
  384. goto pcm_err;
  385. }
  386. ret = snd_soc_init_card(socdev);
  387. if (ret < 0) {
  388. printk(KERN_ERR "ak4642: failed to register card\n");
  389. goto card_err;
  390. }
  391. dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
  392. return ret;
  393. card_err:
  394. snd_soc_free_pcms(socdev);
  395. snd_soc_dapm_free(socdev);
  396. pcm_err:
  397. return ret;
  398. }
  399. /* power down chip */
  400. static int ak4642_remove(struct platform_device *pdev)
  401. {
  402. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  403. snd_soc_free_pcms(socdev);
  404. snd_soc_dapm_free(socdev);
  405. return 0;
  406. }
  407. struct snd_soc_codec_device soc_codec_dev_ak4642 = {
  408. .probe = ak4642_probe,
  409. .remove = ak4642_remove,
  410. .resume = ak4642_resume,
  411. };
  412. EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
  413. static int __init ak4642_modinit(void)
  414. {
  415. int ret;
  416. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  417. ret = i2c_add_driver(&ak4642_i2c_driver);
  418. #endif
  419. return ret;
  420. }
  421. module_init(ak4642_modinit);
  422. static void __exit ak4642_exit(void)
  423. {
  424. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  425. i2c_del_driver(&ak4642_i2c_driver);
  426. #endif
  427. }
  428. module_exit(ak4642_exit);
  429. MODULE_DESCRIPTION("Soc AK4642 driver");
  430. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  431. MODULE_LICENSE("GPL");