wm8731.c 20 KB

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