wm8770.c 20 KB

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