wm8731.c 19 KB

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