wm8510.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * wm8510.c -- WM8510 ALSA Soc Audio driver
  3. *
  4. * Copyright 2006 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/slab.h>
  21. #include <linux/of_device.h>
  22. #include <linux/regmap.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/initval.h>
  28. #include "wm8510.h"
  29. /*
  30. * wm8510 register cache
  31. * We can't read the WM8510 register space when we are
  32. * using 2 wire for device control, so we cache them instead.
  33. */
  34. static const struct reg_default wm8510_reg_defaults[] = {
  35. { 1, 0x0000 },
  36. { 2, 0x0000 },
  37. { 3, 0x0000 },
  38. { 4, 0x0050 },
  39. { 5, 0x0000 },
  40. { 6, 0x0140 },
  41. { 7, 0x0000 },
  42. { 8, 0x0000 },
  43. { 9, 0x0000 },
  44. { 10, 0x0000 },
  45. { 11, 0x00ff },
  46. { 12, 0x0000 },
  47. { 13, 0x0000 },
  48. { 14, 0x0100 },
  49. { 15, 0x00ff },
  50. { 16, 0x0000 },
  51. { 17, 0x0000 },
  52. { 18, 0x012c },
  53. { 19, 0x002c },
  54. { 20, 0x002c },
  55. { 21, 0x002c },
  56. { 22, 0x002c },
  57. { 23, 0x0000 },
  58. { 24, 0x0032 },
  59. { 25, 0x0000 },
  60. { 26, 0x0000 },
  61. { 27, 0x0000 },
  62. { 28, 0x0000 },
  63. { 29, 0x0000 },
  64. { 30, 0x0000 },
  65. { 31, 0x0000 },
  66. { 32, 0x0038 },
  67. { 33, 0x000b },
  68. { 34, 0x0032 },
  69. { 35, 0x0000 },
  70. { 36, 0x0008 },
  71. { 37, 0x000c },
  72. { 38, 0x0093 },
  73. { 39, 0x00e9 },
  74. { 40, 0x0000 },
  75. { 41, 0x0000 },
  76. { 42, 0x0000 },
  77. { 43, 0x0000 },
  78. { 44, 0x0003 },
  79. { 45, 0x0010 },
  80. { 46, 0x0000 },
  81. { 47, 0x0000 },
  82. { 48, 0x0000 },
  83. { 49, 0x0002 },
  84. { 50, 0x0001 },
  85. { 51, 0x0000 },
  86. { 52, 0x0000 },
  87. { 53, 0x0000 },
  88. { 54, 0x0039 },
  89. { 55, 0x0000 },
  90. { 56, 0x0001 },
  91. };
  92. static bool wm8510_volatile(struct device *dev, unsigned int reg)
  93. {
  94. switch (reg) {
  95. case WM8510_RESET:
  96. return true;
  97. default:
  98. return false;
  99. }
  100. }
  101. #define WM8510_POWER1_BIASEN 0x08
  102. #define WM8510_POWER1_BUFIOEN 0x10
  103. #define wm8510_reset(c) snd_soc_write(c, WM8510_RESET, 0)
  104. /* codec private data */
  105. struct wm8510_priv {
  106. struct regmap *regmap;
  107. };
  108. static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
  109. static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
  110. static const char *wm8510_alc[] = { "ALC", "Limiter" };
  111. static const struct soc_enum wm8510_enum[] = {
  112. SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
  113. SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
  114. SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp),
  115. SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc),
  116. };
  117. static const struct snd_kcontrol_new wm8510_snd_controls[] = {
  118. SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
  119. SOC_ENUM("DAC Companding", wm8510_enum[1]),
  120. SOC_ENUM("ADC Companding", wm8510_enum[0]),
  121. SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
  122. SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
  123. SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
  124. SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
  125. SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
  126. SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
  127. SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
  128. SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
  129. SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
  130. SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
  131. SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
  132. SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
  133. SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
  134. SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
  135. SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
  136. SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
  137. SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
  138. SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
  139. SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
  140. SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
  141. SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
  142. SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
  143. SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
  144. SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
  145. SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
  146. SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
  147. SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
  148. SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
  149. SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
  150. SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
  151. SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
  152. };
  153. /* Speaker Output Mixer */
  154. static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
  155. SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
  156. SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
  157. SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
  158. };
  159. /* Mono Output Mixer */
  160. static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
  161. SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
  162. SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
  163. SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
  164. };
  165. static const struct snd_kcontrol_new wm8510_boost_controls[] = {
  166. SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
  167. SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
  168. SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
  169. };
  170. static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
  171. SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
  172. SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
  173. SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
  174. };
  175. static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
  176. SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
  177. &wm8510_speaker_mixer_controls[0],
  178. ARRAY_SIZE(wm8510_speaker_mixer_controls)),
  179. SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
  180. &wm8510_mono_mixer_controls[0],
  181. ARRAY_SIZE(wm8510_mono_mixer_controls)),
  182. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
  183. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
  184. SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
  185. SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
  186. SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
  187. SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
  188. SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
  189. &wm8510_micpga_controls[0],
  190. ARRAY_SIZE(wm8510_micpga_controls)),
  191. SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
  192. &wm8510_boost_controls[0],
  193. ARRAY_SIZE(wm8510_boost_controls)),
  194. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
  195. SND_SOC_DAPM_INPUT("MICN"),
  196. SND_SOC_DAPM_INPUT("MICP"),
  197. SND_SOC_DAPM_INPUT("AUX"),
  198. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  199. SND_SOC_DAPM_OUTPUT("SPKOUTP"),
  200. SND_SOC_DAPM_OUTPUT("SPKOUTN"),
  201. };
  202. static const struct snd_soc_dapm_route wm8510_dapm_routes[] = {
  203. /* Mono output mixer */
  204. {"Mono Mixer", "PCM Playback Switch", "DAC"},
  205. {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
  206. {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
  207. /* Speaker output mixer */
  208. {"Speaker Mixer", "PCM Playback Switch", "DAC"},
  209. {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
  210. {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
  211. /* Outputs */
  212. {"Mono Out", NULL, "Mono Mixer"},
  213. {"MONOOUT", NULL, "Mono Out"},
  214. {"SpkN Out", NULL, "Speaker Mixer"},
  215. {"SpkP Out", NULL, "Speaker Mixer"},
  216. {"SPKOUTN", NULL, "SpkN Out"},
  217. {"SPKOUTP", NULL, "SpkP Out"},
  218. /* Microphone PGA */
  219. {"Mic PGA", "MICN Switch", "MICN"},
  220. {"Mic PGA", "MICP Switch", "MICP"},
  221. { "Mic PGA", "AUX Switch", "Aux Input" },
  222. /* Boost Mixer */
  223. {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
  224. {"Boost Mixer", "Mic Volume", "MICP"},
  225. {"Boost Mixer", "Aux Volume", "Aux Input"},
  226. {"ADC", NULL, "Boost Mixer"},
  227. };
  228. struct pll_ {
  229. unsigned int pre_div:4; /* prescale - 1 */
  230. unsigned int n:4;
  231. unsigned int k;
  232. };
  233. static struct pll_ pll_div;
  234. /* The size in bits of the pll divide multiplied by 10
  235. * to allow rounding later */
  236. #define FIXED_PLL_SIZE ((1 << 24) * 10)
  237. static void pll_factors(unsigned int target, unsigned int source)
  238. {
  239. unsigned long long Kpart;
  240. unsigned int K, Ndiv, Nmod;
  241. Ndiv = target / source;
  242. if (Ndiv < 6) {
  243. source >>= 1;
  244. pll_div.pre_div = 1;
  245. Ndiv = target / source;
  246. } else
  247. pll_div.pre_div = 0;
  248. if ((Ndiv < 6) || (Ndiv > 12))
  249. printk(KERN_WARNING
  250. "WM8510 N value %u outwith recommended range!d\n",
  251. Ndiv);
  252. pll_div.n = Ndiv;
  253. Nmod = target % source;
  254. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  255. do_div(Kpart, source);
  256. K = Kpart & 0xFFFFFFFF;
  257. /* Check if we need to round */
  258. if ((K % 10) >= 5)
  259. K += 5;
  260. /* Move down to proper range now rounding is done */
  261. K /= 10;
  262. pll_div.k = K;
  263. }
  264. static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  265. int source, unsigned int freq_in, unsigned int freq_out)
  266. {
  267. struct snd_soc_codec *codec = codec_dai->codec;
  268. u16 reg;
  269. if (freq_in == 0 || freq_out == 0) {
  270. /* Clock CODEC directly from MCLK */
  271. reg = snd_soc_read(codec, WM8510_CLOCK);
  272. snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
  273. /* Turn off PLL */
  274. reg = snd_soc_read(codec, WM8510_POWER1);
  275. snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
  276. return 0;
  277. }
  278. pll_factors(freq_out*4, freq_in);
  279. snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
  280. snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
  281. snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
  282. snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
  283. reg = snd_soc_read(codec, WM8510_POWER1);
  284. snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
  285. /* Run CODEC from PLL instead of MCLK */
  286. reg = snd_soc_read(codec, WM8510_CLOCK);
  287. snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
  288. return 0;
  289. }
  290. /*
  291. * Configure WM8510 clock dividers.
  292. */
  293. static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  294. int div_id, int div)
  295. {
  296. struct snd_soc_codec *codec = codec_dai->codec;
  297. u16 reg;
  298. switch (div_id) {
  299. case WM8510_OPCLKDIV:
  300. reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
  301. snd_soc_write(codec, WM8510_GPIO, reg | div);
  302. break;
  303. case WM8510_MCLKDIV:
  304. reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
  305. snd_soc_write(codec, WM8510_CLOCK, reg | div);
  306. break;
  307. case WM8510_ADCCLK:
  308. reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
  309. snd_soc_write(codec, WM8510_ADC, reg | div);
  310. break;
  311. case WM8510_DACCLK:
  312. reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
  313. snd_soc_write(codec, WM8510_DAC, reg | div);
  314. break;
  315. case WM8510_BCLKDIV:
  316. reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
  317. snd_soc_write(codec, WM8510_CLOCK, reg | div);
  318. break;
  319. default:
  320. return -EINVAL;
  321. }
  322. return 0;
  323. }
  324. static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
  325. unsigned int fmt)
  326. {
  327. struct snd_soc_codec *codec = codec_dai->codec;
  328. u16 iface = 0;
  329. u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
  330. /* set master/slave audio interface */
  331. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  332. case SND_SOC_DAIFMT_CBM_CFM:
  333. clk |= 0x0001;
  334. break;
  335. case SND_SOC_DAIFMT_CBS_CFS:
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. /* interface format */
  341. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  342. case SND_SOC_DAIFMT_I2S:
  343. iface |= 0x0010;
  344. break;
  345. case SND_SOC_DAIFMT_RIGHT_J:
  346. break;
  347. case SND_SOC_DAIFMT_LEFT_J:
  348. iface |= 0x0008;
  349. break;
  350. case SND_SOC_DAIFMT_DSP_A:
  351. iface |= 0x00018;
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. /* clock inversion */
  357. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  358. case SND_SOC_DAIFMT_NB_NF:
  359. break;
  360. case SND_SOC_DAIFMT_IB_IF:
  361. iface |= 0x0180;
  362. break;
  363. case SND_SOC_DAIFMT_IB_NF:
  364. iface |= 0x0100;
  365. break;
  366. case SND_SOC_DAIFMT_NB_IF:
  367. iface |= 0x0080;
  368. break;
  369. default:
  370. return -EINVAL;
  371. }
  372. snd_soc_write(codec, WM8510_IFACE, iface);
  373. snd_soc_write(codec, WM8510_CLOCK, clk);
  374. return 0;
  375. }
  376. static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
  377. struct snd_pcm_hw_params *params,
  378. struct snd_soc_dai *dai)
  379. {
  380. struct snd_soc_codec *codec = dai->codec;
  381. u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
  382. u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
  383. /* bit size */
  384. switch (params_width(params)) {
  385. case 16:
  386. break;
  387. case 20:
  388. iface |= 0x0020;
  389. break;
  390. case 24:
  391. iface |= 0x0040;
  392. break;
  393. case 32:
  394. iface |= 0x0060;
  395. break;
  396. }
  397. /* filter coefficient */
  398. switch (params_rate(params)) {
  399. case 8000:
  400. adn |= 0x5 << 1;
  401. break;
  402. case 11025:
  403. adn |= 0x4 << 1;
  404. break;
  405. case 16000:
  406. adn |= 0x3 << 1;
  407. break;
  408. case 22050:
  409. adn |= 0x2 << 1;
  410. break;
  411. case 32000:
  412. adn |= 0x1 << 1;
  413. break;
  414. case 44100:
  415. case 48000:
  416. break;
  417. }
  418. snd_soc_write(codec, WM8510_IFACE, iface);
  419. snd_soc_write(codec, WM8510_ADD, adn);
  420. return 0;
  421. }
  422. static int wm8510_mute(struct snd_soc_dai *dai, int mute)
  423. {
  424. struct snd_soc_codec *codec = dai->codec;
  425. u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
  426. if (mute)
  427. snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
  428. else
  429. snd_soc_write(codec, WM8510_DAC, mute_reg);
  430. return 0;
  431. }
  432. /* liam need to make this lower power with dapm */
  433. static int wm8510_set_bias_level(struct snd_soc_codec *codec,
  434. enum snd_soc_bias_level level)
  435. {
  436. struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
  437. u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
  438. switch (level) {
  439. case SND_SOC_BIAS_ON:
  440. case SND_SOC_BIAS_PREPARE:
  441. power1 |= 0x1; /* VMID 50k */
  442. snd_soc_write(codec, WM8510_POWER1, power1);
  443. break;
  444. case SND_SOC_BIAS_STANDBY:
  445. power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
  446. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  447. regcache_sync(wm8510->regmap);
  448. /* Initial cap charge at VMID 5k */
  449. snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
  450. mdelay(100);
  451. }
  452. power1 |= 0x2; /* VMID 500k */
  453. snd_soc_write(codec, WM8510_POWER1, power1);
  454. break;
  455. case SND_SOC_BIAS_OFF:
  456. snd_soc_write(codec, WM8510_POWER1, 0);
  457. snd_soc_write(codec, WM8510_POWER2, 0);
  458. snd_soc_write(codec, WM8510_POWER3, 0);
  459. break;
  460. }
  461. return 0;
  462. }
  463. #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  464. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  465. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  466. #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  467. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  468. static const struct snd_soc_dai_ops wm8510_dai_ops = {
  469. .hw_params = wm8510_pcm_hw_params,
  470. .digital_mute = wm8510_mute,
  471. .set_fmt = wm8510_set_dai_fmt,
  472. .set_clkdiv = wm8510_set_dai_clkdiv,
  473. .set_pll = wm8510_set_dai_pll,
  474. };
  475. static struct snd_soc_dai_driver wm8510_dai = {
  476. .name = "wm8510-hifi",
  477. .playback = {
  478. .stream_name = "Playback",
  479. .channels_min = 2,
  480. .channels_max = 2,
  481. .rates = WM8510_RATES,
  482. .formats = WM8510_FORMATS,},
  483. .capture = {
  484. .stream_name = "Capture",
  485. .channels_min = 2,
  486. .channels_max = 2,
  487. .rates = WM8510_RATES,
  488. .formats = WM8510_FORMATS,},
  489. .ops = &wm8510_dai_ops,
  490. .symmetric_rates = 1,
  491. };
  492. static int wm8510_probe(struct snd_soc_codec *codec)
  493. {
  494. wm8510_reset(codec);
  495. return 0;
  496. }
  497. static const struct snd_soc_codec_driver soc_codec_dev_wm8510 = {
  498. .probe = wm8510_probe,
  499. .set_bias_level = wm8510_set_bias_level,
  500. .suspend_bias_off = true,
  501. .component_driver = {
  502. .controls = wm8510_snd_controls,
  503. .num_controls = ARRAY_SIZE(wm8510_snd_controls),
  504. .dapm_widgets = wm8510_dapm_widgets,
  505. .num_dapm_widgets = ARRAY_SIZE(wm8510_dapm_widgets),
  506. .dapm_routes = wm8510_dapm_routes,
  507. .num_dapm_routes = ARRAY_SIZE(wm8510_dapm_routes),
  508. },
  509. };
  510. static const struct of_device_id wm8510_of_match[] = {
  511. { .compatible = "wlf,wm8510" },
  512. { },
  513. };
  514. MODULE_DEVICE_TABLE(of, wm8510_of_match);
  515. static const struct regmap_config wm8510_regmap = {
  516. .reg_bits = 7,
  517. .val_bits = 9,
  518. .max_register = WM8510_MONOMIX,
  519. .reg_defaults = wm8510_reg_defaults,
  520. .num_reg_defaults = ARRAY_SIZE(wm8510_reg_defaults),
  521. .cache_type = REGCACHE_RBTREE,
  522. .volatile_reg = wm8510_volatile,
  523. };
  524. #if defined(CONFIG_SPI_MASTER)
  525. static int wm8510_spi_probe(struct spi_device *spi)
  526. {
  527. struct wm8510_priv *wm8510;
  528. int ret;
  529. wm8510 = devm_kzalloc(&spi->dev, sizeof(struct wm8510_priv),
  530. GFP_KERNEL);
  531. if (wm8510 == NULL)
  532. return -ENOMEM;
  533. wm8510->regmap = devm_regmap_init_spi(spi, &wm8510_regmap);
  534. if (IS_ERR(wm8510->regmap))
  535. return PTR_ERR(wm8510->regmap);
  536. spi_set_drvdata(spi, wm8510);
  537. ret = snd_soc_register_codec(&spi->dev,
  538. &soc_codec_dev_wm8510, &wm8510_dai, 1);
  539. return ret;
  540. }
  541. static int wm8510_spi_remove(struct spi_device *spi)
  542. {
  543. snd_soc_unregister_codec(&spi->dev);
  544. return 0;
  545. }
  546. static struct spi_driver wm8510_spi_driver = {
  547. .driver = {
  548. .name = "wm8510",
  549. .of_match_table = wm8510_of_match,
  550. },
  551. .probe = wm8510_spi_probe,
  552. .remove = wm8510_spi_remove,
  553. };
  554. #endif /* CONFIG_SPI_MASTER */
  555. #if IS_ENABLED(CONFIG_I2C)
  556. static int wm8510_i2c_probe(struct i2c_client *i2c,
  557. const struct i2c_device_id *id)
  558. {
  559. struct wm8510_priv *wm8510;
  560. int ret;
  561. wm8510 = devm_kzalloc(&i2c->dev, sizeof(struct wm8510_priv),
  562. GFP_KERNEL);
  563. if (wm8510 == NULL)
  564. return -ENOMEM;
  565. wm8510->regmap = devm_regmap_init_i2c(i2c, &wm8510_regmap);
  566. if (IS_ERR(wm8510->regmap))
  567. return PTR_ERR(wm8510->regmap);
  568. i2c_set_clientdata(i2c, wm8510);
  569. ret = snd_soc_register_codec(&i2c->dev,
  570. &soc_codec_dev_wm8510, &wm8510_dai, 1);
  571. return ret;
  572. }
  573. static int wm8510_i2c_remove(struct i2c_client *client)
  574. {
  575. snd_soc_unregister_codec(&client->dev);
  576. return 0;
  577. }
  578. static const struct i2c_device_id wm8510_i2c_id[] = {
  579. { "wm8510", 0 },
  580. { }
  581. };
  582. MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
  583. static struct i2c_driver wm8510_i2c_driver = {
  584. .driver = {
  585. .name = "wm8510",
  586. .of_match_table = wm8510_of_match,
  587. },
  588. .probe = wm8510_i2c_probe,
  589. .remove = wm8510_i2c_remove,
  590. .id_table = wm8510_i2c_id,
  591. };
  592. #endif
  593. static int __init wm8510_modinit(void)
  594. {
  595. int ret = 0;
  596. #if IS_ENABLED(CONFIG_I2C)
  597. ret = i2c_add_driver(&wm8510_i2c_driver);
  598. if (ret != 0) {
  599. printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n",
  600. ret);
  601. }
  602. #endif
  603. #if defined(CONFIG_SPI_MASTER)
  604. ret = spi_register_driver(&wm8510_spi_driver);
  605. if (ret != 0) {
  606. printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n",
  607. ret);
  608. }
  609. #endif
  610. return ret;
  611. }
  612. module_init(wm8510_modinit);
  613. static void __exit wm8510_exit(void)
  614. {
  615. #if IS_ENABLED(CONFIG_I2C)
  616. i2c_del_driver(&wm8510_i2c_driver);
  617. #endif
  618. #if defined(CONFIG_SPI_MASTER)
  619. spi_unregister_driver(&wm8510_spi_driver);
  620. #endif
  621. }
  622. module_exit(wm8510_exit);
  623. MODULE_DESCRIPTION("ASoC WM8510 driver");
  624. MODULE_AUTHOR("Liam Girdwood");
  625. MODULE_LICENSE("GPL");