wm8731.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. * wm8731.c -- WM8731 ALSA SoC Audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on wm8753.c by Liam Girdwood
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spi/spi.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "wm8731.h"
  30. static struct snd_soc_codec *wm8731_codec;
  31. struct snd_soc_codec_device soc_codec_dev_wm8731;
  32. /* codec private data */
  33. struct wm8731_priv {
  34. struct snd_soc_codec codec;
  35. u16 reg_cache[WM8731_CACHEREGNUM];
  36. unsigned int sysclk;
  37. };
  38. #ifdef CONFIG_SPI_MASTER
  39. static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
  40. #endif
  41. /*
  42. * wm8731 register cache
  43. * We can't read the WM8731 register space when we are
  44. * using 2 wire for device control, so we cache them instead.
  45. * There is no point in caching the reset register
  46. */
  47. static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
  48. 0x0097, 0x0097, 0x0079, 0x0079,
  49. 0x000a, 0x0008, 0x009f, 0x000a,
  50. 0x0000, 0x0000
  51. };
  52. #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
  53. static const char *wm8731_input_select[] = {"Line In", "Mic"};
  54. static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  55. static const struct soc_enum wm8731_enum[] = {
  56. SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
  57. SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
  58. };
  59. static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
  60. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
  61. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  62. static const struct snd_kcontrol_new wm8731_snd_controls[] = {
  63. SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
  64. 0, 127, 0, out_tlv),
  65. SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
  66. 7, 1, 0),
  67. SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
  68. in_tlv),
  69. SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
  70. SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
  71. SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
  72. SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
  73. sidetone_tlv),
  74. SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
  75. SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
  76. SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
  77. };
  78. /* Output Mixer */
  79. static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
  80. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  81. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  82. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  83. };
  84. /* Input mux */
  85. static const struct snd_kcontrol_new wm8731_input_mux_controls =
  86. SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
  87. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  88. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
  89. &wm8731_output_mixer_controls[0],
  90. ARRAY_SIZE(wm8731_output_mixer_controls)),
  91. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
  92. SND_SOC_DAPM_OUTPUT("LOUT"),
  93. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  94. SND_SOC_DAPM_OUTPUT("ROUT"),
  95. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  96. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
  97. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
  98. SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
  99. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
  100. SND_SOC_DAPM_INPUT("MICIN"),
  101. SND_SOC_DAPM_INPUT("RLINEIN"),
  102. SND_SOC_DAPM_INPUT("LLINEIN"),
  103. };
  104. static const struct snd_soc_dapm_route intercon[] = {
  105. /* output mixer */
  106. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  107. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  108. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  109. /* outputs */
  110. {"RHPOUT", NULL, "Output Mixer"},
  111. {"ROUT", NULL, "Output Mixer"},
  112. {"LHPOUT", NULL, "Output Mixer"},
  113. {"LOUT", NULL, "Output Mixer"},
  114. /* input mux */
  115. {"Input Mux", "Line In", "Line Input"},
  116. {"Input Mux", "Mic", "Mic Bias"},
  117. {"ADC", NULL, "Input Mux"},
  118. /* inputs */
  119. {"Line Input", NULL, "LLINEIN"},
  120. {"Line Input", NULL, "RLINEIN"},
  121. {"Mic Bias", NULL, "MICIN"},
  122. };
  123. static int wm8731_add_widgets(struct snd_soc_codec *codec)
  124. {
  125. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  126. ARRAY_SIZE(wm8731_dapm_widgets));
  127. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  128. snd_soc_dapm_new_widgets(codec);
  129. return 0;
  130. }
  131. struct _coeff_div {
  132. u32 mclk;
  133. u32 rate;
  134. u16 fs;
  135. u8 sr:4;
  136. u8 bosr:1;
  137. u8 usb:1;
  138. };
  139. /* codec mclk clock divider coefficients */
  140. static const struct _coeff_div coeff_div[] = {
  141. /* 48k */
  142. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  143. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  144. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  145. /* 32k */
  146. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  147. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  148. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  149. /* 8k */
  150. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  151. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  152. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  153. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  154. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  155. /* 96k */
  156. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  157. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  158. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  159. /* 44.1k */
  160. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  161. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  162. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  163. /* 88.2k */
  164. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  165. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  166. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  167. };
  168. static inline int get_coeff(int mclk, int rate)
  169. {
  170. int i;
  171. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  172. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  173. return i;
  174. }
  175. return 0;
  176. }
  177. static int wm8731_hw_params(struct snd_pcm_substream *substream,
  178. struct snd_pcm_hw_params *params,
  179. struct snd_soc_dai *dai)
  180. {
  181. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  182. struct snd_soc_device *socdev = rtd->socdev;
  183. struct snd_soc_codec *codec = socdev->card->codec;
  184. struct wm8731_priv *wm8731 = codec->private_data;
  185. u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
  186. int i = get_coeff(wm8731->sysclk, params_rate(params));
  187. u16 srate = (coeff_div[i].sr << 2) |
  188. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  189. snd_soc_write(codec, WM8731_SRATE, srate);
  190. /* bit size */
  191. switch (params_format(params)) {
  192. case SNDRV_PCM_FORMAT_S16_LE:
  193. break;
  194. case SNDRV_PCM_FORMAT_S20_3LE:
  195. iface |= 0x0004;
  196. break;
  197. case SNDRV_PCM_FORMAT_S24_LE:
  198. iface |= 0x0008;
  199. break;
  200. }
  201. snd_soc_write(codec, WM8731_IFACE, iface);
  202. return 0;
  203. }
  204. static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
  205. struct snd_soc_dai *dai)
  206. {
  207. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  208. struct snd_soc_device *socdev = rtd->socdev;
  209. struct snd_soc_codec *codec = socdev->card->codec;
  210. /* set active */
  211. snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
  212. return 0;
  213. }
  214. static void wm8731_shutdown(struct snd_pcm_substream *substream,
  215. struct snd_soc_dai *dai)
  216. {
  217. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  218. struct snd_soc_device *socdev = rtd->socdev;
  219. struct snd_soc_codec *codec = socdev->card->codec;
  220. /* deactivate */
  221. if (!codec->active) {
  222. udelay(50);
  223. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  224. }
  225. }
  226. static int wm8731_mute(struct snd_soc_dai *dai, int mute)
  227. {
  228. struct snd_soc_codec *codec = dai->codec;
  229. u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
  230. if (mute)
  231. snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
  232. else
  233. snd_soc_write(codec, WM8731_APDIGI, mute_reg);
  234. return 0;
  235. }
  236. static int wm8731_set_dai_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. struct wm8731_priv *wm8731 = codec->private_data;
  241. switch (freq) {
  242. case 11289600:
  243. case 12000000:
  244. case 12288000:
  245. case 16934400:
  246. case 18432000:
  247. wm8731->sysclk = freq;
  248. return 0;
  249. }
  250. return -EINVAL;
  251. }
  252. static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
  253. unsigned int fmt)
  254. {
  255. struct snd_soc_codec *codec = codec_dai->codec;
  256. u16 iface = 0;
  257. /* set master/slave audio interface */
  258. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  259. case SND_SOC_DAIFMT_CBM_CFM:
  260. iface |= 0x0040;
  261. break;
  262. case SND_SOC_DAIFMT_CBS_CFS:
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. /* interface format */
  268. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  269. case SND_SOC_DAIFMT_I2S:
  270. iface |= 0x0002;
  271. break;
  272. case SND_SOC_DAIFMT_RIGHT_J:
  273. break;
  274. case SND_SOC_DAIFMT_LEFT_J:
  275. iface |= 0x0001;
  276. break;
  277. case SND_SOC_DAIFMT_DSP_A:
  278. iface |= 0x0003;
  279. break;
  280. case SND_SOC_DAIFMT_DSP_B:
  281. iface |= 0x0013;
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. /* clock inversion */
  287. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  288. case SND_SOC_DAIFMT_NB_NF:
  289. break;
  290. case SND_SOC_DAIFMT_IB_IF:
  291. iface |= 0x0090;
  292. break;
  293. case SND_SOC_DAIFMT_IB_NF:
  294. iface |= 0x0080;
  295. break;
  296. case SND_SOC_DAIFMT_NB_IF:
  297. iface |= 0x0010;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. /* set iface */
  303. snd_soc_write(codec, WM8731_IFACE, iface);
  304. return 0;
  305. }
  306. static int wm8731_set_bias_level(struct snd_soc_codec *codec,
  307. enum snd_soc_bias_level level)
  308. {
  309. u16 reg;
  310. switch (level) {
  311. case SND_SOC_BIAS_ON:
  312. break;
  313. case SND_SOC_BIAS_PREPARE:
  314. break;
  315. case SND_SOC_BIAS_STANDBY:
  316. /* Clear PWROFF, gate CLKOUT, everything else as-is */
  317. reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
  318. snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
  319. break;
  320. case SND_SOC_BIAS_OFF:
  321. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  322. snd_soc_write(codec, WM8731_PWR, 0xffff);
  323. break;
  324. }
  325. codec->bias_level = level;
  326. return 0;
  327. }
  328. #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  329. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  330. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  331. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  332. SNDRV_PCM_RATE_96000)
  333. #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  334. SNDRV_PCM_FMTBIT_S24_LE)
  335. static struct snd_soc_dai_ops wm8731_dai_ops = {
  336. .prepare = wm8731_pcm_prepare,
  337. .hw_params = wm8731_hw_params,
  338. .shutdown = wm8731_shutdown,
  339. .digital_mute = wm8731_mute,
  340. .set_sysclk = wm8731_set_dai_sysclk,
  341. .set_fmt = wm8731_set_dai_fmt,
  342. };
  343. struct snd_soc_dai wm8731_dai = {
  344. .name = "WM8731",
  345. .playback = {
  346. .stream_name = "Playback",
  347. .channels_min = 1,
  348. .channels_max = 2,
  349. .rates = WM8731_RATES,
  350. .formats = WM8731_FORMATS,},
  351. .capture = {
  352. .stream_name = "Capture",
  353. .channels_min = 1,
  354. .channels_max = 2,
  355. .rates = WM8731_RATES,
  356. .formats = WM8731_FORMATS,},
  357. .ops = &wm8731_dai_ops,
  358. .symmetric_rates = 1,
  359. };
  360. EXPORT_SYMBOL_GPL(wm8731_dai);
  361. #ifdef CONFIG_PM
  362. static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
  363. {
  364. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  365. struct snd_soc_codec *codec = socdev->card->codec;
  366. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  367. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  368. return 0;
  369. }
  370. static int wm8731_resume(struct platform_device *pdev)
  371. {
  372. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  373. struct snd_soc_codec *codec = socdev->card->codec;
  374. int i;
  375. u8 data[2];
  376. u16 *cache = codec->reg_cache;
  377. /* Sync reg_cache with the hardware */
  378. for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
  379. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  380. data[1] = cache[i] & 0x00ff;
  381. codec->hw_write(codec->control_data, data, 2);
  382. }
  383. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  384. wm8731_set_bias_level(codec, codec->suspend_bias_level);
  385. return 0;
  386. }
  387. #else
  388. #define wm8731_suspend NULL
  389. #define wm8731_resume NULL
  390. #endif
  391. static int wm8731_probe(struct platform_device *pdev)
  392. {
  393. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  394. struct snd_soc_codec *codec;
  395. int ret = 0;
  396. if (wm8731_codec == NULL) {
  397. dev_err(&pdev->dev, "Codec device not registered\n");
  398. return -ENODEV;
  399. }
  400. socdev->card->codec = wm8731_codec;
  401. codec = wm8731_codec;
  402. /* register pcms */
  403. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  404. if (ret < 0) {
  405. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  406. goto pcm_err;
  407. }
  408. snd_soc_add_controls(codec, wm8731_snd_controls,
  409. ARRAY_SIZE(wm8731_snd_controls));
  410. wm8731_add_widgets(codec);
  411. ret = snd_soc_init_card(socdev);
  412. if (ret < 0) {
  413. dev_err(codec->dev, "failed to register card: %d\n", ret);
  414. goto card_err;
  415. }
  416. return ret;
  417. card_err:
  418. snd_soc_free_pcms(socdev);
  419. snd_soc_dapm_free(socdev);
  420. pcm_err:
  421. return ret;
  422. }
  423. /* power down chip */
  424. static int wm8731_remove(struct platform_device *pdev)
  425. {
  426. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  427. snd_soc_free_pcms(socdev);
  428. snd_soc_dapm_free(socdev);
  429. return 0;
  430. }
  431. struct snd_soc_codec_device soc_codec_dev_wm8731 = {
  432. .probe = wm8731_probe,
  433. .remove = wm8731_remove,
  434. .suspend = wm8731_suspend,
  435. .resume = wm8731_resume,
  436. };
  437. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
  438. static int wm8731_register(struct wm8731_priv *wm8731,
  439. enum snd_soc_control_type control)
  440. {
  441. int ret;
  442. struct snd_soc_codec *codec = &wm8731->codec;
  443. if (wm8731_codec) {
  444. dev_err(codec->dev, "Another WM8731 is registered\n");
  445. ret = -EINVAL;
  446. goto err;
  447. }
  448. mutex_init(&codec->mutex);
  449. INIT_LIST_HEAD(&codec->dapm_widgets);
  450. INIT_LIST_HEAD(&codec->dapm_paths);
  451. codec->private_data = wm8731;
  452. codec->name = "WM8731";
  453. codec->owner = THIS_MODULE;
  454. codec->bias_level = SND_SOC_BIAS_OFF;
  455. codec->set_bias_level = wm8731_set_bias_level;
  456. codec->dai = &wm8731_dai;
  457. codec->num_dai = 1;
  458. codec->reg_cache_size = WM8731_CACHEREGNUM;
  459. codec->reg_cache = &wm8731->reg_cache;
  460. memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
  461. ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
  462. if (ret < 0) {
  463. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  464. goto err;
  465. }
  466. ret = wm8731_reset(codec);
  467. if (ret < 0) {
  468. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  469. goto err;
  470. }
  471. wm8731_dai.dev = codec->dev;
  472. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  473. /* Latch the update bits */
  474. snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
  475. snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
  476. snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
  477. snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
  478. /* Disable bypass path by default */
  479. snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
  480. wm8731_codec = codec;
  481. ret = snd_soc_register_codec(codec);
  482. if (ret != 0) {
  483. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  484. goto err;
  485. }
  486. ret = snd_soc_register_dai(&wm8731_dai);
  487. if (ret != 0) {
  488. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  489. snd_soc_unregister_codec(codec);
  490. goto err_codec;
  491. }
  492. return 0;
  493. err_codec:
  494. snd_soc_unregister_codec(codec);
  495. err:
  496. kfree(wm8731);
  497. return ret;
  498. }
  499. static void wm8731_unregister(struct wm8731_priv *wm8731)
  500. {
  501. wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
  502. snd_soc_unregister_dai(&wm8731_dai);
  503. snd_soc_unregister_codec(&wm8731->codec);
  504. kfree(wm8731);
  505. wm8731_codec = NULL;
  506. }
  507. #if defined(CONFIG_SPI_MASTER)
  508. static int __devinit wm8731_spi_probe(struct spi_device *spi)
  509. {
  510. struct snd_soc_codec *codec;
  511. struct wm8731_priv *wm8731;
  512. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  513. if (wm8731 == NULL)
  514. return -ENOMEM;
  515. codec = &wm8731->codec;
  516. codec->control_data = spi;
  517. codec->dev = &spi->dev;
  518. dev_set_drvdata(&spi->dev, wm8731);
  519. return wm8731_register(wm8731, SND_SOC_SPI);
  520. }
  521. static int __devexit wm8731_spi_remove(struct spi_device *spi)
  522. {
  523. struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
  524. wm8731_unregister(wm8731);
  525. return 0;
  526. }
  527. #ifdef CONFIG_PM
  528. static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg)
  529. {
  530. return snd_soc_suspend_device(&spi->dev);
  531. }
  532. static int wm8731_spi_resume(struct spi_device *spi)
  533. {
  534. return snd_soc_resume_device(&spi->dev);
  535. }
  536. #else
  537. #define wm8731_spi_suspend NULL
  538. #define wm8731_spi_resume NULL
  539. #endif
  540. static struct spi_driver wm8731_spi_driver = {
  541. .driver = {
  542. .name = "wm8731",
  543. .bus = &spi_bus_type,
  544. .owner = THIS_MODULE,
  545. },
  546. .probe = wm8731_spi_probe,
  547. .suspend = wm8731_spi_suspend,
  548. .resume = wm8731_spi_resume,
  549. .remove = __devexit_p(wm8731_spi_remove),
  550. };
  551. #endif /* CONFIG_SPI_MASTER */
  552. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  553. static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
  554. const struct i2c_device_id *id)
  555. {
  556. struct wm8731_priv *wm8731;
  557. struct snd_soc_codec *codec;
  558. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  559. if (wm8731 == NULL)
  560. return -ENOMEM;
  561. codec = &wm8731->codec;
  562. i2c_set_clientdata(i2c, wm8731);
  563. codec->control_data = i2c;
  564. codec->dev = &i2c->dev;
  565. return wm8731_register(wm8731, SND_SOC_I2C);
  566. }
  567. static __devexit int wm8731_i2c_remove(struct i2c_client *client)
  568. {
  569. struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
  570. wm8731_unregister(wm8731);
  571. return 0;
  572. }
  573. #ifdef CONFIG_PM
  574. static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg)
  575. {
  576. return snd_soc_suspend_device(&i2c->dev);
  577. }
  578. static int wm8731_i2c_resume(struct i2c_client *i2c)
  579. {
  580. return snd_soc_resume_device(&i2c->dev);
  581. }
  582. #else
  583. #define wm8731_i2c_suspend NULL
  584. #define wm8731_i2c_resume NULL
  585. #endif
  586. static const struct i2c_device_id wm8731_i2c_id[] = {
  587. { "wm8731", 0 },
  588. { }
  589. };
  590. MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
  591. static struct i2c_driver wm8731_i2c_driver = {
  592. .driver = {
  593. .name = "WM8731 I2C Codec",
  594. .owner = THIS_MODULE,
  595. },
  596. .probe = wm8731_i2c_probe,
  597. .remove = __devexit_p(wm8731_i2c_remove),
  598. .suspend = wm8731_i2c_suspend,
  599. .resume = wm8731_i2c_resume,
  600. .id_table = wm8731_i2c_id,
  601. };
  602. #endif
  603. static int __init wm8731_modinit(void)
  604. {
  605. int ret;
  606. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  607. ret = i2c_add_driver(&wm8731_i2c_driver);
  608. if (ret != 0) {
  609. printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
  610. ret);
  611. }
  612. #endif
  613. #if defined(CONFIG_SPI_MASTER)
  614. ret = spi_register_driver(&wm8731_spi_driver);
  615. if (ret != 0) {
  616. printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
  617. ret);
  618. }
  619. #endif
  620. return 0;
  621. }
  622. module_init(wm8731_modinit);
  623. static void __exit wm8731_exit(void)
  624. {
  625. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  626. i2c_del_driver(&wm8731_i2c_driver);
  627. #endif
  628. #if defined(CONFIG_SPI_MASTER)
  629. spi_unregister_driver(&wm8731_spi_driver);
  630. #endif
  631. }
  632. module_exit(wm8731_exit);
  633. MODULE_DESCRIPTION("ASoC WM8731 driver");
  634. MODULE_AUTHOR("Richard Purdie");
  635. MODULE_LICENSE("GPL");