wm8770.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * wm8770.c -- WM8770 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  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/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.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 <sound/tlv.h>
  28. #include "wm8770.h"
  29. #define WM8770_NUM_SUPPLIES 3
  30. static const char *wm8770_supply_names[WM8770_NUM_SUPPLIES] = {
  31. "AVDD1",
  32. "AVDD2",
  33. "DVDD"
  34. };
  35. static const struct reg_default wm8770_reg_defaults[] = {
  36. { 0, 0x7f },
  37. { 1, 0x7f },
  38. { 2, 0x7f },
  39. { 3, 0x7f },
  40. { 4, 0x7f },
  41. { 5, 0x7f },
  42. { 6, 0x7f },
  43. { 7, 0x7f },
  44. { 8, 0x7f },
  45. { 9, 0xff },
  46. { 10, 0xff },
  47. { 11, 0xff },
  48. { 12, 0xff },
  49. { 13, 0xff },
  50. { 14, 0xff },
  51. { 15, 0xff },
  52. { 16, 0xff },
  53. { 17, 0xff },
  54. { 18, 0 },
  55. { 19, 0x90 },
  56. { 20, 0 },
  57. { 21, 0 },
  58. { 22, 0x22 },
  59. { 23, 0x22 },
  60. { 24, 0x3e },
  61. { 25, 0xc },
  62. { 26, 0xc },
  63. { 27, 0x100 },
  64. { 28, 0x189 },
  65. { 29, 0x189 },
  66. { 30, 0x8770 },
  67. };
  68. static bool wm8770_volatile_reg(struct device *dev, unsigned int reg)
  69. {
  70. switch (reg) {
  71. case WM8770_RESET:
  72. return true;
  73. default:
  74. return false;
  75. }
  76. }
  77. struct wm8770_priv {
  78. struct regmap *regmap;
  79. struct regulator_bulk_data supplies[WM8770_NUM_SUPPLIES];
  80. struct notifier_block disable_nb[WM8770_NUM_SUPPLIES];
  81. struct snd_soc_codec *codec;
  82. int sysclk;
  83. };
  84. static int vout12supply_event(struct snd_soc_dapm_widget *w,
  85. struct snd_kcontrol *kcontrol, int event);
  86. static int vout34supply_event(struct snd_soc_dapm_widget *w,
  87. struct snd_kcontrol *kcontrol, int event);
  88. /*
  89. * We can't use the same notifier block for more than one supply and
  90. * there's no way I can see to get from a callback to the caller
  91. * except container_of().
  92. */
  93. #define WM8770_REGULATOR_EVENT(n) \
  94. static int wm8770_regulator_event_##n(struct notifier_block *nb, \
  95. unsigned long event, void *data) \
  96. { \
  97. struct wm8770_priv *wm8770 = container_of(nb, struct wm8770_priv, \
  98. disable_nb[n]); \
  99. if (event & REGULATOR_EVENT_DISABLE) { \
  100. regcache_mark_dirty(wm8770->regmap); \
  101. } \
  102. return 0; \
  103. }
  104. WM8770_REGULATOR_EVENT(0)
  105. WM8770_REGULATOR_EVENT(1)
  106. WM8770_REGULATOR_EVENT(2)
  107. static const DECLARE_TLV_DB_SCALE(adc_tlv, -1200, 100, 0);
  108. static const DECLARE_TLV_DB_SCALE(dac_dig_tlv, -12750, 50, 1);
  109. static const DECLARE_TLV_DB_SCALE(dac_alg_tlv, -12700, 100, 1);
  110. static const char *dac_phase_text[][2] = {
  111. { "DAC1 Normal", "DAC1 Inverted" },
  112. { "DAC2 Normal", "DAC2 Inverted" },
  113. { "DAC3 Normal", "DAC3 Inverted" },
  114. { "DAC4 Normal", "DAC4 Inverted" },
  115. };
  116. static const struct soc_enum dac_phase[] = {
  117. SOC_ENUM_DOUBLE(WM8770_DACPHASE, 0, 1, 2, dac_phase_text[0]),
  118. SOC_ENUM_DOUBLE(WM8770_DACPHASE, 2, 3, 2, dac_phase_text[1]),
  119. SOC_ENUM_DOUBLE(WM8770_DACPHASE, 4, 5, 2, dac_phase_text[2]),
  120. SOC_ENUM_DOUBLE(WM8770_DACPHASE, 6, 7, 2, dac_phase_text[3]),
  121. };
  122. static const struct snd_kcontrol_new wm8770_snd_controls[] = {
  123. /* global DAC playback controls */
  124. SOC_SINGLE_TLV("DAC Playback Volume", WM8770_MSDIGVOL, 0, 255, 0,
  125. dac_dig_tlv),
  126. SOC_SINGLE("DAC Playback Switch", WM8770_DACMUTE, 4, 1, 1),
  127. SOC_SINGLE("DAC Playback ZC Switch", WM8770_DACCTRL1, 0, 1, 0),
  128. /* global VOUT playback controls */
  129. SOC_SINGLE_TLV("VOUT Playback Volume", WM8770_MSALGVOL, 0, 127, 0,
  130. dac_alg_tlv),
  131. SOC_SINGLE("VOUT Playback ZC Switch", WM8770_MSALGVOL, 7, 1, 0),
  132. /* VOUT1/2/3/4 specific controls */
  133. SOC_DOUBLE_R_TLV("VOUT1 Playback Volume", WM8770_VOUT1LVOL,
  134. WM8770_VOUT1RVOL, 0, 127, 0, dac_alg_tlv),
  135. SOC_DOUBLE_R("VOUT1 Playback ZC Switch", WM8770_VOUT1LVOL,
  136. WM8770_VOUT1RVOL, 7, 1, 0),
  137. SOC_DOUBLE_R_TLV("VOUT2 Playback Volume", WM8770_VOUT2LVOL,
  138. WM8770_VOUT2RVOL, 0, 127, 0, dac_alg_tlv),
  139. SOC_DOUBLE_R("VOUT2 Playback ZC Switch", WM8770_VOUT2LVOL,
  140. WM8770_VOUT2RVOL, 7, 1, 0),
  141. SOC_DOUBLE_R_TLV("VOUT3 Playback Volume", WM8770_VOUT3LVOL,
  142. WM8770_VOUT3RVOL, 0, 127, 0, dac_alg_tlv),
  143. SOC_DOUBLE_R("VOUT3 Playback ZC Switch", WM8770_VOUT3LVOL,
  144. WM8770_VOUT3RVOL, 7, 1, 0),
  145. SOC_DOUBLE_R_TLV("VOUT4 Playback Volume", WM8770_VOUT4LVOL,
  146. WM8770_VOUT4RVOL, 0, 127, 0, dac_alg_tlv),
  147. SOC_DOUBLE_R("VOUT4 Playback ZC Switch", WM8770_VOUT4LVOL,
  148. WM8770_VOUT4RVOL, 7, 1, 0),
  149. /* DAC1/2/3/4 specific controls */
  150. SOC_DOUBLE_R_TLV("DAC1 Playback Volume", WM8770_DAC1LVOL,
  151. WM8770_DAC1RVOL, 0, 255, 0, dac_dig_tlv),
  152. SOC_SINGLE("DAC1 Deemphasis Switch", WM8770_DACCTRL2, 0, 1, 0),
  153. SOC_ENUM("DAC1 Phase", dac_phase[0]),
  154. SOC_DOUBLE_R_TLV("DAC2 Playback Volume", WM8770_DAC2LVOL,
  155. WM8770_DAC2RVOL, 0, 255, 0, dac_dig_tlv),
  156. SOC_SINGLE("DAC2 Deemphasis Switch", WM8770_DACCTRL2, 1, 1, 0),
  157. SOC_ENUM("DAC2 Phase", dac_phase[1]),
  158. SOC_DOUBLE_R_TLV("DAC3 Playback Volume", WM8770_DAC3LVOL,
  159. WM8770_DAC3RVOL, 0, 255, 0, dac_dig_tlv),
  160. SOC_SINGLE("DAC3 Deemphasis Switch", WM8770_DACCTRL2, 2, 1, 0),
  161. SOC_ENUM("DAC3 Phase", dac_phase[2]),
  162. SOC_DOUBLE_R_TLV("DAC4 Playback Volume", WM8770_DAC4LVOL,
  163. WM8770_DAC4RVOL, 0, 255, 0, dac_dig_tlv),
  164. SOC_SINGLE("DAC4 Deemphasis Switch", WM8770_DACCTRL2, 3, 1, 0),
  165. SOC_ENUM("DAC4 Phase", dac_phase[3]),
  166. /* ADC specific controls */
  167. SOC_DOUBLE_R_TLV("Capture Volume", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
  168. 0, 31, 0, adc_tlv),
  169. SOC_DOUBLE_R("Capture Switch", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
  170. 5, 1, 1),
  171. /* other controls */
  172. SOC_SINGLE("ADC 128x Oversampling Switch", WM8770_MSTRCTRL, 3, 1, 0),
  173. SOC_SINGLE("ADC Highpass Filter Switch", WM8770_IFACECTRL, 8, 1, 1)
  174. };
  175. static const char *ain_text[] = {
  176. "AIN1", "AIN2", "AIN3", "AIN4",
  177. "AIN5", "AIN6", "AIN7", "AIN8"
  178. };
  179. static SOC_ENUM_DOUBLE_DECL(ain_enum,
  180. WM8770_ADCMUX, 0, 4, ain_text);
  181. static const struct snd_kcontrol_new ain_mux =
  182. SOC_DAPM_ENUM("Capture Mux", ain_enum);
  183. static const struct snd_kcontrol_new vout1_mix_controls[] = {
  184. SOC_DAPM_SINGLE("DAC1 Switch", WM8770_OUTMUX1, 0, 1, 0),
  185. SOC_DAPM_SINGLE("AUX1 Switch", WM8770_OUTMUX1, 1, 1, 0),
  186. SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 2, 1, 0)
  187. };
  188. static const struct snd_kcontrol_new vout2_mix_controls[] = {
  189. SOC_DAPM_SINGLE("DAC2 Switch", WM8770_OUTMUX1, 3, 1, 0),
  190. SOC_DAPM_SINGLE("AUX2 Switch", WM8770_OUTMUX1, 4, 1, 0),
  191. SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 5, 1, 0)
  192. };
  193. static const struct snd_kcontrol_new vout3_mix_controls[] = {
  194. SOC_DAPM_SINGLE("DAC3 Switch", WM8770_OUTMUX2, 0, 1, 0),
  195. SOC_DAPM_SINGLE("AUX3 Switch", WM8770_OUTMUX2, 1, 1, 0),
  196. SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 2, 1, 0)
  197. };
  198. static const struct snd_kcontrol_new vout4_mix_controls[] = {
  199. SOC_DAPM_SINGLE("DAC4 Switch", WM8770_OUTMUX2, 3, 1, 0),
  200. SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 4, 1, 0)
  201. };
  202. static const struct snd_soc_dapm_widget wm8770_dapm_widgets[] = {
  203. SND_SOC_DAPM_INPUT("AUX1"),
  204. SND_SOC_DAPM_INPUT("AUX2"),
  205. SND_SOC_DAPM_INPUT("AUX3"),
  206. SND_SOC_DAPM_INPUT("AIN1"),
  207. SND_SOC_DAPM_INPUT("AIN2"),
  208. SND_SOC_DAPM_INPUT("AIN3"),
  209. SND_SOC_DAPM_INPUT("AIN4"),
  210. SND_SOC_DAPM_INPUT("AIN5"),
  211. SND_SOC_DAPM_INPUT("AIN6"),
  212. SND_SOC_DAPM_INPUT("AIN7"),
  213. SND_SOC_DAPM_INPUT("AIN8"),
  214. SND_SOC_DAPM_MUX("Capture Mux", WM8770_ADCMUX, 8, 1, &ain_mux),
  215. SND_SOC_DAPM_ADC("ADC", "Capture", WM8770_PWDNCTRL, 1, 1),
  216. SND_SOC_DAPM_DAC("DAC1", "Playback", WM8770_PWDNCTRL, 2, 1),
  217. SND_SOC_DAPM_DAC("DAC2", "Playback", WM8770_PWDNCTRL, 3, 1),
  218. SND_SOC_DAPM_DAC("DAC3", "Playback", WM8770_PWDNCTRL, 4, 1),
  219. SND_SOC_DAPM_DAC("DAC4", "Playback", WM8770_PWDNCTRL, 5, 1),
  220. SND_SOC_DAPM_SUPPLY("VOUT12 Supply", SND_SOC_NOPM, 0, 0,
  221. vout12supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  222. SND_SOC_DAPM_SUPPLY("VOUT34 Supply", SND_SOC_NOPM, 0, 0,
  223. vout34supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  224. SND_SOC_DAPM_MIXER("VOUT1 Mixer", SND_SOC_NOPM, 0, 0,
  225. vout1_mix_controls, ARRAY_SIZE(vout1_mix_controls)),
  226. SND_SOC_DAPM_MIXER("VOUT2 Mixer", SND_SOC_NOPM, 0, 0,
  227. vout2_mix_controls, ARRAY_SIZE(vout2_mix_controls)),
  228. SND_SOC_DAPM_MIXER("VOUT3 Mixer", SND_SOC_NOPM, 0, 0,
  229. vout3_mix_controls, ARRAY_SIZE(vout3_mix_controls)),
  230. SND_SOC_DAPM_MIXER("VOUT4 Mixer", SND_SOC_NOPM, 0, 0,
  231. vout4_mix_controls, ARRAY_SIZE(vout4_mix_controls)),
  232. SND_SOC_DAPM_OUTPUT("VOUT1"),
  233. SND_SOC_DAPM_OUTPUT("VOUT2"),
  234. SND_SOC_DAPM_OUTPUT("VOUT3"),
  235. SND_SOC_DAPM_OUTPUT("VOUT4")
  236. };
  237. static const struct snd_soc_dapm_route wm8770_intercon[] = {
  238. { "Capture Mux", "AIN1", "AIN1" },
  239. { "Capture Mux", "AIN2", "AIN2" },
  240. { "Capture Mux", "AIN3", "AIN3" },
  241. { "Capture Mux", "AIN4", "AIN4" },
  242. { "Capture Mux", "AIN5", "AIN5" },
  243. { "Capture Mux", "AIN6", "AIN6" },
  244. { "Capture Mux", "AIN7", "AIN7" },
  245. { "Capture Mux", "AIN8", "AIN8" },
  246. { "ADC", NULL, "Capture Mux" },
  247. { "VOUT1 Mixer", NULL, "VOUT12 Supply" },
  248. { "VOUT1 Mixer", "DAC1 Switch", "DAC1" },
  249. { "VOUT1 Mixer", "AUX1 Switch", "AUX1" },
  250. { "VOUT1 Mixer", "Bypass Switch", "Capture Mux" },
  251. { "VOUT2 Mixer", NULL, "VOUT12 Supply" },
  252. { "VOUT2 Mixer", "DAC2 Switch", "DAC2" },
  253. { "VOUT2 Mixer", "AUX2 Switch", "AUX2" },
  254. { "VOUT2 Mixer", "Bypass Switch", "Capture Mux" },
  255. { "VOUT3 Mixer", NULL, "VOUT34 Supply" },
  256. { "VOUT3 Mixer", "DAC3 Switch", "DAC3" },
  257. { "VOUT3 Mixer", "AUX3 Switch", "AUX3" },
  258. { "VOUT3 Mixer", "Bypass Switch", "Capture Mux" },
  259. { "VOUT4 Mixer", NULL, "VOUT34 Supply" },
  260. { "VOUT4 Mixer", "DAC4 Switch", "DAC4" },
  261. { "VOUT4 Mixer", "Bypass Switch", "Capture Mux" },
  262. { "VOUT1", NULL, "VOUT1 Mixer" },
  263. { "VOUT2", NULL, "VOUT2 Mixer" },
  264. { "VOUT3", NULL, "VOUT3 Mixer" },
  265. { "VOUT4", NULL, "VOUT4 Mixer" }
  266. };
  267. static int vout12supply_event(struct snd_soc_dapm_widget *w,
  268. struct snd_kcontrol *kcontrol, int event)
  269. {
  270. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  271. switch (event) {
  272. case SND_SOC_DAPM_PRE_PMU:
  273. snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0);
  274. break;
  275. case SND_SOC_DAPM_POST_PMD:
  276. snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0x180);
  277. break;
  278. }
  279. return 0;
  280. }
  281. static int vout34supply_event(struct snd_soc_dapm_widget *w,
  282. struct snd_kcontrol *kcontrol, int event)
  283. {
  284. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  285. switch (event) {
  286. case SND_SOC_DAPM_PRE_PMU:
  287. snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0);
  288. break;
  289. case SND_SOC_DAPM_POST_PMD:
  290. snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0x180);
  291. break;
  292. }
  293. return 0;
  294. }
  295. static int wm8770_reset(struct snd_soc_codec *codec)
  296. {
  297. return snd_soc_write(codec, WM8770_RESET, 0);
  298. }
  299. static int wm8770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  300. {
  301. struct snd_soc_codec *codec;
  302. int iface, master;
  303. codec = dai->codec;
  304. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  305. case SND_SOC_DAIFMT_CBM_CFM:
  306. master = 0x100;
  307. break;
  308. case SND_SOC_DAIFMT_CBS_CFS:
  309. master = 0;
  310. break;
  311. default:
  312. return -EINVAL;
  313. }
  314. iface = 0;
  315. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  316. case SND_SOC_DAIFMT_I2S:
  317. iface |= 0x2;
  318. break;
  319. case SND_SOC_DAIFMT_RIGHT_J:
  320. break;
  321. case SND_SOC_DAIFMT_LEFT_J:
  322. iface |= 0x1;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  328. case SND_SOC_DAIFMT_NB_NF:
  329. break;
  330. case SND_SOC_DAIFMT_IB_IF:
  331. iface |= 0xc;
  332. break;
  333. case SND_SOC_DAIFMT_IB_NF:
  334. iface |= 0x8;
  335. break;
  336. case SND_SOC_DAIFMT_NB_IF:
  337. iface |= 0x4;
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. snd_soc_update_bits(codec, WM8770_IFACECTRL, 0xf, iface);
  343. snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x100, master);
  344. return 0;
  345. }
  346. static const int mclk_ratios[] = {
  347. 128,
  348. 192,
  349. 256,
  350. 384,
  351. 512,
  352. 768
  353. };
  354. static int wm8770_hw_params(struct snd_pcm_substream *substream,
  355. struct snd_pcm_hw_params *params,
  356. struct snd_soc_dai *dai)
  357. {
  358. struct snd_soc_codec *codec;
  359. struct wm8770_priv *wm8770;
  360. int i;
  361. int iface;
  362. int shift;
  363. int ratio;
  364. codec = dai->codec;
  365. wm8770 = snd_soc_codec_get_drvdata(codec);
  366. iface = 0;
  367. switch (params_width(params)) {
  368. case 16:
  369. break;
  370. case 20:
  371. iface |= 0x10;
  372. break;
  373. case 24:
  374. iface |= 0x20;
  375. break;
  376. case 32:
  377. iface |= 0x30;
  378. break;
  379. }
  380. switch (substream->stream) {
  381. case SNDRV_PCM_STREAM_PLAYBACK:
  382. i = 0;
  383. shift = 4;
  384. break;
  385. case SNDRV_PCM_STREAM_CAPTURE:
  386. i = 2;
  387. shift = 0;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. /* Only need to set MCLK/LRCLK ratio if we're master */
  393. if (snd_soc_read(codec, WM8770_MSTRCTRL) & 0x100) {
  394. for (; i < ARRAY_SIZE(mclk_ratios); ++i) {
  395. ratio = wm8770->sysclk / params_rate(params);
  396. if (ratio == mclk_ratios[i])
  397. break;
  398. }
  399. if (i == ARRAY_SIZE(mclk_ratios)) {
  400. dev_err(codec->dev,
  401. "Unable to configure MCLK ratio %d/%d\n",
  402. wm8770->sysclk, params_rate(params));
  403. return -EINVAL;
  404. }
  405. dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
  406. snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x7 << shift,
  407. i << shift);
  408. }
  409. snd_soc_update_bits(codec, WM8770_IFACECTRL, 0x30, iface);
  410. return 0;
  411. }
  412. static int wm8770_mute(struct snd_soc_dai *dai, int mute)
  413. {
  414. struct snd_soc_codec *codec;
  415. codec = dai->codec;
  416. return snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10,
  417. !!mute << 4);
  418. }
  419. static int wm8770_set_sysclk(struct snd_soc_dai *dai,
  420. int clk_id, unsigned int freq, int dir)
  421. {
  422. struct snd_soc_codec *codec;
  423. struct wm8770_priv *wm8770;
  424. codec = dai->codec;
  425. wm8770 = snd_soc_codec_get_drvdata(codec);
  426. wm8770->sysclk = freq;
  427. return 0;
  428. }
  429. static int wm8770_set_bias_level(struct snd_soc_codec *codec,
  430. enum snd_soc_bias_level level)
  431. {
  432. int ret;
  433. struct wm8770_priv *wm8770;
  434. wm8770 = snd_soc_codec_get_drvdata(codec);
  435. switch (level) {
  436. case SND_SOC_BIAS_ON:
  437. break;
  438. case SND_SOC_BIAS_PREPARE:
  439. break;
  440. case SND_SOC_BIAS_STANDBY:
  441. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  442. ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
  443. wm8770->supplies);
  444. if (ret) {
  445. dev_err(codec->dev,
  446. "Failed to enable supplies: %d\n",
  447. ret);
  448. return ret;
  449. }
  450. regcache_sync(wm8770->regmap);
  451. /* global powerup */
  452. snd_soc_write(codec, WM8770_PWDNCTRL, 0);
  453. }
  454. break;
  455. case SND_SOC_BIAS_OFF:
  456. /* global powerdown */
  457. snd_soc_write(codec, WM8770_PWDNCTRL, 1);
  458. regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies),
  459. wm8770->supplies);
  460. break;
  461. }
  462. return 0;
  463. }
  464. #define WM8770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  465. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  466. static const struct snd_soc_dai_ops wm8770_dai_ops = {
  467. .digital_mute = wm8770_mute,
  468. .hw_params = wm8770_hw_params,
  469. .set_fmt = wm8770_set_fmt,
  470. .set_sysclk = wm8770_set_sysclk,
  471. };
  472. static struct snd_soc_dai_driver wm8770_dai = {
  473. .name = "wm8770-hifi",
  474. .playback = {
  475. .stream_name = "Playback",
  476. .channels_min = 2,
  477. .channels_max = 2,
  478. .rates = SNDRV_PCM_RATE_8000_192000,
  479. .formats = WM8770_FORMATS
  480. },
  481. .capture = {
  482. .stream_name = "Capture",
  483. .channels_min = 2,
  484. .channels_max = 2,
  485. .rates = SNDRV_PCM_RATE_8000_96000,
  486. .formats = WM8770_FORMATS
  487. },
  488. .ops = &wm8770_dai_ops,
  489. .symmetric_rates = 1
  490. };
  491. static int wm8770_probe(struct snd_soc_codec *codec)
  492. {
  493. struct wm8770_priv *wm8770;
  494. int ret;
  495. wm8770 = snd_soc_codec_get_drvdata(codec);
  496. wm8770->codec = codec;
  497. ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
  498. wm8770->supplies);
  499. if (ret) {
  500. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  501. return ret;
  502. }
  503. ret = wm8770_reset(codec);
  504. if (ret < 0) {
  505. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  506. goto err_reg_enable;
  507. }
  508. /* latch the volume update bits */
  509. snd_soc_update_bits(codec, WM8770_MSDIGVOL, 0x100, 0x100);
  510. snd_soc_update_bits(codec, WM8770_MSALGVOL, 0x100, 0x100);
  511. snd_soc_update_bits(codec, WM8770_VOUT1RVOL, 0x100, 0x100);
  512. snd_soc_update_bits(codec, WM8770_VOUT2RVOL, 0x100, 0x100);
  513. snd_soc_update_bits(codec, WM8770_VOUT3RVOL, 0x100, 0x100);
  514. snd_soc_update_bits(codec, WM8770_VOUT4RVOL, 0x100, 0x100);
  515. snd_soc_update_bits(codec, WM8770_DAC1RVOL, 0x100, 0x100);
  516. snd_soc_update_bits(codec, WM8770_DAC2RVOL, 0x100, 0x100);
  517. snd_soc_update_bits(codec, WM8770_DAC3RVOL, 0x100, 0x100);
  518. snd_soc_update_bits(codec, WM8770_DAC4RVOL, 0x100, 0x100);
  519. /* mute all DACs */
  520. snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10, 0x10);
  521. err_reg_enable:
  522. regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
  523. return ret;
  524. }
  525. static const struct snd_soc_codec_driver soc_codec_dev_wm8770 = {
  526. .probe = wm8770_probe,
  527. .set_bias_level = wm8770_set_bias_level,
  528. .idle_bias_off = true,
  529. .component_driver = {
  530. .controls = wm8770_snd_controls,
  531. .num_controls = ARRAY_SIZE(wm8770_snd_controls),
  532. .dapm_widgets = wm8770_dapm_widgets,
  533. .num_dapm_widgets = ARRAY_SIZE(wm8770_dapm_widgets),
  534. .dapm_routes = wm8770_intercon,
  535. .num_dapm_routes = ARRAY_SIZE(wm8770_intercon),
  536. },
  537. };
  538. static const struct of_device_id wm8770_of_match[] = {
  539. { .compatible = "wlf,wm8770", },
  540. { }
  541. };
  542. MODULE_DEVICE_TABLE(of, wm8770_of_match);
  543. static const struct regmap_config wm8770_regmap = {
  544. .reg_bits = 7,
  545. .val_bits = 9,
  546. .max_register = WM8770_RESET,
  547. .reg_defaults = wm8770_reg_defaults,
  548. .num_reg_defaults = ARRAY_SIZE(wm8770_reg_defaults),
  549. .cache_type = REGCACHE_RBTREE,
  550. .volatile_reg = wm8770_volatile_reg,
  551. };
  552. static int wm8770_spi_probe(struct spi_device *spi)
  553. {
  554. struct wm8770_priv *wm8770;
  555. int ret, i;
  556. wm8770 = devm_kzalloc(&spi->dev, sizeof(struct wm8770_priv),
  557. GFP_KERNEL);
  558. if (!wm8770)
  559. return -ENOMEM;
  560. for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++)
  561. wm8770->supplies[i].supply = wm8770_supply_names[i];
  562. ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8770->supplies),
  563. wm8770->supplies);
  564. if (ret) {
  565. dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
  566. return ret;
  567. }
  568. wm8770->disable_nb[0].notifier_call = wm8770_regulator_event_0;
  569. wm8770->disable_nb[1].notifier_call = wm8770_regulator_event_1;
  570. wm8770->disable_nb[2].notifier_call = wm8770_regulator_event_2;
  571. /* This should really be moved into the regulator core */
  572. for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++) {
  573. ret = regulator_register_notifier(wm8770->supplies[i].consumer,
  574. &wm8770->disable_nb[i]);
  575. if (ret) {
  576. dev_err(&spi->dev,
  577. "Failed to register regulator notifier: %d\n",
  578. ret);
  579. }
  580. }
  581. wm8770->regmap = devm_regmap_init_spi(spi, &wm8770_regmap);
  582. if (IS_ERR(wm8770->regmap))
  583. return PTR_ERR(wm8770->regmap);
  584. spi_set_drvdata(spi, wm8770);
  585. ret = snd_soc_register_codec(&spi->dev,
  586. &soc_codec_dev_wm8770, &wm8770_dai, 1);
  587. return ret;
  588. }
  589. static int wm8770_spi_remove(struct spi_device *spi)
  590. {
  591. struct wm8770_priv *wm8770 = spi_get_drvdata(spi);
  592. int i;
  593. for (i = 0; i < ARRAY_SIZE(wm8770->supplies); ++i)
  594. regulator_unregister_notifier(wm8770->supplies[i].consumer,
  595. &wm8770->disable_nb[i]);
  596. snd_soc_unregister_codec(&spi->dev);
  597. return 0;
  598. }
  599. static struct spi_driver wm8770_spi_driver = {
  600. .driver = {
  601. .name = "wm8770",
  602. .of_match_table = wm8770_of_match,
  603. },
  604. .probe = wm8770_spi_probe,
  605. .remove = wm8770_spi_remove
  606. };
  607. module_spi_driver(wm8770_spi_driver);
  608. MODULE_DESCRIPTION("ASoC WM8770 driver");
  609. MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
  610. MODULE_LICENSE("GPL");