wm8731.c 20 KB

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