wm8731.c 19 KB

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