wm8510.c 18 KB

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