wm8728.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * wm8728.c -- WM8728 ALSA SoC Audio driver
  3. *
  4. * Copyright 2008 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spi/spi.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8728.h"
  28. struct snd_soc_codec_device soc_codec_dev_wm8728;
  29. /*
  30. * We can't read the WM8728 register space so we cache them instead.
  31. * Note that the defaults here aren't the physical defaults, we latch
  32. * the volume update bits, mute the output and enable infinite zero
  33. * detect.
  34. */
  35. static const u16 wm8728_reg_defaults[] = {
  36. 0x1ff,
  37. 0x1ff,
  38. 0x001,
  39. 0x100,
  40. };
  41. static inline unsigned int wm8728_read_reg_cache(struct snd_soc_codec *codec,
  42. unsigned int reg)
  43. {
  44. u16 *cache = codec->reg_cache;
  45. BUG_ON(reg >= ARRAY_SIZE(wm8728_reg_defaults));
  46. return cache[reg];
  47. }
  48. static inline void wm8728_write_reg_cache(struct snd_soc_codec *codec,
  49. u16 reg, unsigned int value)
  50. {
  51. u16 *cache = codec->reg_cache;
  52. BUG_ON(reg >= ARRAY_SIZE(wm8728_reg_defaults));
  53. cache[reg] = value;
  54. }
  55. /*
  56. * write to the WM8728 register space
  57. */
  58. static int wm8728_write(struct snd_soc_codec *codec, unsigned int reg,
  59. unsigned int value)
  60. {
  61. u8 data[2];
  62. /* data is
  63. * D15..D9 WM8728 register offset
  64. * D8...D0 register data
  65. */
  66. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  67. data[1] = value & 0x00ff;
  68. wm8728_write_reg_cache(codec, reg, value);
  69. if (codec->hw_write(codec->control_data, data, 2) == 2)
  70. return 0;
  71. else
  72. return -EIO;
  73. }
  74. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  75. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  76. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  77. 0, 255, 0, wm8728_tlv),
  78. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  79. };
  80. /*
  81. * DAPM controls.
  82. */
  83. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  84. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  85. SND_SOC_DAPM_OUTPUT("VOUTL"),
  86. SND_SOC_DAPM_OUTPUT("VOUTR"),
  87. };
  88. static const struct snd_soc_dapm_route intercon[] = {
  89. {"VOUTL", NULL, "DAC"},
  90. {"VOUTR", NULL, "DAC"},
  91. };
  92. static int wm8728_add_widgets(struct snd_soc_codec *codec)
  93. {
  94. snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
  95. ARRAY_SIZE(wm8728_dapm_widgets));
  96. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  97. snd_soc_dapm_new_widgets(codec);
  98. return 0;
  99. }
  100. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  101. {
  102. struct snd_soc_codec *codec = dai->codec;
  103. u16 mute_reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  104. if (mute)
  105. wm8728_write(codec, WM8728_DACCTL, mute_reg | 1);
  106. else
  107. wm8728_write(codec, WM8728_DACCTL, mute_reg & ~1);
  108. return 0;
  109. }
  110. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  111. struct snd_pcm_hw_params *params,
  112. struct snd_soc_dai *dai)
  113. {
  114. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  115. struct snd_soc_device *socdev = rtd->socdev;
  116. struct snd_soc_codec *codec = socdev->card->codec;
  117. u16 dac = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  118. dac &= ~0x18;
  119. switch (params_format(params)) {
  120. case SNDRV_PCM_FORMAT_S16_LE:
  121. break;
  122. case SNDRV_PCM_FORMAT_S20_3LE:
  123. dac |= 0x10;
  124. break;
  125. case SNDRV_PCM_FORMAT_S24_LE:
  126. dac |= 0x08;
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. wm8728_write(codec, WM8728_DACCTL, dac);
  132. return 0;
  133. }
  134. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  135. unsigned int fmt)
  136. {
  137. struct snd_soc_codec *codec = codec_dai->codec;
  138. u16 iface = wm8728_read_reg_cache(codec, WM8728_IFCTL);
  139. /* Currently only I2S is supported by the driver, though the
  140. * hardware is more flexible.
  141. */
  142. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  143. case SND_SOC_DAIFMT_I2S:
  144. iface |= 1;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. /* The hardware only support full slave mode */
  150. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  151. case SND_SOC_DAIFMT_CBS_CFS:
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  157. case SND_SOC_DAIFMT_NB_NF:
  158. iface &= ~0x22;
  159. break;
  160. case SND_SOC_DAIFMT_IB_NF:
  161. iface |= 0x20;
  162. iface &= ~0x02;
  163. break;
  164. case SND_SOC_DAIFMT_NB_IF:
  165. iface |= 0x02;
  166. iface &= ~0x20;
  167. break;
  168. case SND_SOC_DAIFMT_IB_IF:
  169. iface |= 0x22;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. wm8728_write(codec, WM8728_IFCTL, iface);
  175. return 0;
  176. }
  177. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  178. enum snd_soc_bias_level level)
  179. {
  180. u16 reg;
  181. int i;
  182. switch (level) {
  183. case SND_SOC_BIAS_ON:
  184. case SND_SOC_BIAS_PREPARE:
  185. case SND_SOC_BIAS_STANDBY:
  186. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  187. /* Power everything up... */
  188. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  189. wm8728_write(codec, WM8728_DACCTL, reg & ~0x4);
  190. /* ..then sync in the register cache. */
  191. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  192. wm8728_write(codec, i,
  193. wm8728_read_reg_cache(codec, i));
  194. }
  195. break;
  196. case SND_SOC_BIAS_OFF:
  197. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  198. wm8728_write(codec, WM8728_DACCTL, reg | 0x4);
  199. break;
  200. }
  201. codec->bias_level = level;
  202. return 0;
  203. }
  204. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  205. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  206. SNDRV_PCM_FMTBIT_S24_LE)
  207. static struct snd_soc_dai_ops wm8728_dai_ops = {
  208. .hw_params = wm8728_hw_params,
  209. .digital_mute = wm8728_mute,
  210. .set_fmt = wm8728_set_dai_fmt,
  211. };
  212. struct snd_soc_dai wm8728_dai = {
  213. .name = "WM8728",
  214. .playback = {
  215. .stream_name = "Playback",
  216. .channels_min = 2,
  217. .channels_max = 2,
  218. .rates = WM8728_RATES,
  219. .formats = WM8728_FORMATS,
  220. },
  221. .ops = &wm8728_dai_ops,
  222. };
  223. EXPORT_SYMBOL_GPL(wm8728_dai);
  224. static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
  225. {
  226. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  227. struct snd_soc_codec *codec = socdev->card->codec;
  228. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  229. return 0;
  230. }
  231. static int wm8728_resume(struct platform_device *pdev)
  232. {
  233. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  234. struct snd_soc_codec *codec = socdev->card->codec;
  235. wm8728_set_bias_level(codec, codec->suspend_bias_level);
  236. return 0;
  237. }
  238. /*
  239. * initialise the WM8728 driver
  240. * register the mixer and dsp interfaces with the kernel
  241. */
  242. static int wm8728_init(struct snd_soc_device *socdev)
  243. {
  244. struct snd_soc_codec *codec = socdev->card->codec;
  245. int ret = 0;
  246. codec->name = "WM8728";
  247. codec->owner = THIS_MODULE;
  248. codec->read = wm8728_read_reg_cache;
  249. codec->write = wm8728_write;
  250. codec->set_bias_level = wm8728_set_bias_level;
  251. codec->dai = &wm8728_dai;
  252. codec->num_dai = 1;
  253. codec->bias_level = SND_SOC_BIAS_OFF;
  254. codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
  255. codec->reg_cache = kmemdup(wm8728_reg_defaults,
  256. sizeof(wm8728_reg_defaults),
  257. GFP_KERNEL);
  258. if (codec->reg_cache == NULL)
  259. return -ENOMEM;
  260. /* register pcms */
  261. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  262. if (ret < 0) {
  263. printk(KERN_ERR "wm8728: failed to create pcms\n");
  264. goto pcm_err;
  265. }
  266. /* power on device */
  267. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  268. snd_soc_add_controls(codec, wm8728_snd_controls,
  269. ARRAY_SIZE(wm8728_snd_controls));
  270. wm8728_add_widgets(codec);
  271. ret = snd_soc_init_card(socdev);
  272. if (ret < 0) {
  273. printk(KERN_ERR "wm8728: failed to register card\n");
  274. goto card_err;
  275. }
  276. return ret;
  277. card_err:
  278. snd_soc_free_pcms(socdev);
  279. snd_soc_dapm_free(socdev);
  280. pcm_err:
  281. kfree(codec->reg_cache);
  282. return ret;
  283. }
  284. static struct snd_soc_device *wm8728_socdev;
  285. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  286. /*
  287. * WM8728 2 wire address is determined by GPIO5
  288. * state during powerup.
  289. * low = 0x1a
  290. * high = 0x1b
  291. */
  292. static int wm8728_i2c_probe(struct i2c_client *i2c,
  293. const struct i2c_device_id *id)
  294. {
  295. struct snd_soc_device *socdev = wm8728_socdev;
  296. struct snd_soc_codec *codec = socdev->card->codec;
  297. int ret;
  298. i2c_set_clientdata(i2c, codec);
  299. codec->control_data = i2c;
  300. ret = wm8728_init(socdev);
  301. if (ret < 0)
  302. pr_err("failed to initialise WM8728\n");
  303. return ret;
  304. }
  305. static int wm8728_i2c_remove(struct i2c_client *client)
  306. {
  307. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  308. kfree(codec->reg_cache);
  309. return 0;
  310. }
  311. static const struct i2c_device_id wm8728_i2c_id[] = {
  312. { "wm8728", 0 },
  313. { }
  314. };
  315. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  316. static struct i2c_driver wm8728_i2c_driver = {
  317. .driver = {
  318. .name = "WM8728 I2C Codec",
  319. .owner = THIS_MODULE,
  320. },
  321. .probe = wm8728_i2c_probe,
  322. .remove = wm8728_i2c_remove,
  323. .id_table = wm8728_i2c_id,
  324. };
  325. static int wm8728_add_i2c_device(struct platform_device *pdev,
  326. const struct wm8728_setup_data *setup)
  327. {
  328. struct i2c_board_info info;
  329. struct i2c_adapter *adapter;
  330. struct i2c_client *client;
  331. int ret;
  332. ret = i2c_add_driver(&wm8728_i2c_driver);
  333. if (ret != 0) {
  334. dev_err(&pdev->dev, "can't add i2c driver\n");
  335. return ret;
  336. }
  337. memset(&info, 0, sizeof(struct i2c_board_info));
  338. info.addr = setup->i2c_address;
  339. strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
  340. adapter = i2c_get_adapter(setup->i2c_bus);
  341. if (!adapter) {
  342. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  343. setup->i2c_bus);
  344. goto err_driver;
  345. }
  346. client = i2c_new_device(adapter, &info);
  347. i2c_put_adapter(adapter);
  348. if (!client) {
  349. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  350. (unsigned int)info.addr);
  351. goto err_driver;
  352. }
  353. return 0;
  354. err_driver:
  355. i2c_del_driver(&wm8728_i2c_driver);
  356. return -ENODEV;
  357. }
  358. #endif
  359. #if defined(CONFIG_SPI_MASTER)
  360. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  361. {
  362. struct snd_soc_device *socdev = wm8728_socdev;
  363. struct snd_soc_codec *codec = socdev->card->codec;
  364. int ret;
  365. codec->control_data = spi;
  366. ret = wm8728_init(socdev);
  367. if (ret < 0)
  368. dev_err(&spi->dev, "failed to initialise WM8728\n");
  369. return ret;
  370. }
  371. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  372. {
  373. return 0;
  374. }
  375. static struct spi_driver wm8728_spi_driver = {
  376. .driver = {
  377. .name = "wm8728",
  378. .bus = &spi_bus_type,
  379. .owner = THIS_MODULE,
  380. },
  381. .probe = wm8728_spi_probe,
  382. .remove = __devexit_p(wm8728_spi_remove),
  383. };
  384. static int wm8728_spi_write(struct spi_device *spi, const char *data, int len)
  385. {
  386. struct spi_transfer t;
  387. struct spi_message m;
  388. u8 msg[2];
  389. if (len <= 0)
  390. return 0;
  391. msg[0] = data[0];
  392. msg[1] = data[1];
  393. spi_message_init(&m);
  394. memset(&t, 0, (sizeof t));
  395. t.tx_buf = &msg[0];
  396. t.len = len;
  397. spi_message_add_tail(&t, &m);
  398. spi_sync(spi, &m);
  399. return len;
  400. }
  401. #endif /* CONFIG_SPI_MASTER */
  402. static int wm8728_probe(struct platform_device *pdev)
  403. {
  404. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  405. struct wm8728_setup_data *setup;
  406. struct snd_soc_codec *codec;
  407. int ret = 0;
  408. setup = socdev->codec_data;
  409. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  410. if (codec == NULL)
  411. return -ENOMEM;
  412. socdev->card->codec = codec;
  413. mutex_init(&codec->mutex);
  414. INIT_LIST_HEAD(&codec->dapm_widgets);
  415. INIT_LIST_HEAD(&codec->dapm_paths);
  416. wm8728_socdev = socdev;
  417. ret = -ENODEV;
  418. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  419. if (setup->i2c_address) {
  420. codec->hw_write = (hw_write_t)i2c_master_send;
  421. ret = wm8728_add_i2c_device(pdev, setup);
  422. }
  423. #endif
  424. #if defined(CONFIG_SPI_MASTER)
  425. if (setup->spi) {
  426. codec->hw_write = (hw_write_t)wm8728_spi_write;
  427. ret = spi_register_driver(&wm8728_spi_driver);
  428. if (ret != 0)
  429. printk(KERN_ERR "can't add spi driver");
  430. }
  431. #endif
  432. if (ret != 0)
  433. kfree(codec);
  434. return ret;
  435. }
  436. /* power down chip */
  437. static int wm8728_remove(struct platform_device *pdev)
  438. {
  439. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  440. struct snd_soc_codec *codec = socdev->card->codec;
  441. if (codec->control_data)
  442. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  443. snd_soc_free_pcms(socdev);
  444. snd_soc_dapm_free(socdev);
  445. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  446. i2c_unregister_device(codec->control_data);
  447. i2c_del_driver(&wm8728_i2c_driver);
  448. #endif
  449. #if defined(CONFIG_SPI_MASTER)
  450. spi_unregister_driver(&wm8728_spi_driver);
  451. #endif
  452. kfree(codec);
  453. return 0;
  454. }
  455. struct snd_soc_codec_device soc_codec_dev_wm8728 = {
  456. .probe = wm8728_probe,
  457. .remove = wm8728_remove,
  458. .suspend = wm8728_suspend,
  459. .resume = wm8728_resume,
  460. };
  461. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
  462. static int __init wm8728_modinit(void)
  463. {
  464. return snd_soc_register_dai(&wm8728_dai);
  465. }
  466. module_init(wm8728_modinit);
  467. static void __exit wm8728_exit(void)
  468. {
  469. snd_soc_unregister_dai(&wm8728_dai);
  470. }
  471. module_exit(wm8728_exit);
  472. MODULE_DESCRIPTION("ASoC WM8728 driver");
  473. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  474. MODULE_LICENSE("GPL");