wm8731.c 18 KB

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