wm8971.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm8971.c -- WM8971 ALSA SoC Audio driver
  4. *
  5. * Copyright 2005 Lab126, Inc.
  6. *
  7. * Author: Kenneth Kiraly <kiraly@lab126.com>
  8. *
  9. * Based on wm8753.c by Liam Girdwood
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/pm.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/initval.h>
  24. #include "wm8971.h"
  25. #define WM8971_REG_COUNT 43
  26. /* codec private data */
  27. struct wm8971_priv {
  28. unsigned int sysclk;
  29. struct delayed_work charge_work;
  30. struct regmap *regmap;
  31. };
  32. /*
  33. * wm8971 register cache
  34. * We can't read the WM8971 register space when we
  35. * are using 2 wire for device control, so we cache them instead.
  36. */
  37. static const struct reg_default wm8971_reg_defaults[] = {
  38. { 0, 0x0097 },
  39. { 1, 0x0097 },
  40. { 2, 0x0079 },
  41. { 3, 0x0079 },
  42. { 4, 0x0000 },
  43. { 5, 0x0008 },
  44. { 6, 0x0000 },
  45. { 7, 0x000a },
  46. { 8, 0x0000 },
  47. { 9, 0x0000 },
  48. { 10, 0x00ff },
  49. { 11, 0x00ff },
  50. { 12, 0x000f },
  51. { 13, 0x000f },
  52. { 14, 0x0000 },
  53. { 15, 0x0000 },
  54. { 16, 0x0000 },
  55. { 17, 0x007b },
  56. { 18, 0x0000 },
  57. { 19, 0x0032 },
  58. { 20, 0x0000 },
  59. { 21, 0x00c3 },
  60. { 22, 0x00c3 },
  61. { 23, 0x00c0 },
  62. { 24, 0x0000 },
  63. { 25, 0x0000 },
  64. { 26, 0x0000 },
  65. { 27, 0x0000 },
  66. { 28, 0x0000 },
  67. { 29, 0x0000 },
  68. { 30, 0x0000 },
  69. { 31, 0x0000 },
  70. { 32, 0x0000 },
  71. { 33, 0x0000 },
  72. { 34, 0x0050 },
  73. { 35, 0x0050 },
  74. { 36, 0x0050 },
  75. { 37, 0x0050 },
  76. { 38, 0x0050 },
  77. { 39, 0x0050 },
  78. { 40, 0x0079 },
  79. { 41, 0x0079 },
  80. { 42, 0x0079 },
  81. };
  82. #define wm8971_reset(c) snd_soc_component_write(c, WM8971_RESET, 0)
  83. /* WM8971 Controls */
  84. static const char *wm8971_bass[] = { "Linear Control", "Adaptive Boost" };
  85. static const char *wm8971_bass_filter[] = { "130Hz @ 48kHz",
  86. "200Hz @ 48kHz" };
  87. static const char *wm8971_treble[] = { "8kHz", "4kHz" };
  88. static const char *wm8971_alc_func[] = { "Off", "Right", "Left", "Stereo" };
  89. static const char *wm8971_ng_type[] = { "Constant PGA Gain",
  90. "Mute ADC Output" };
  91. static const char *wm8971_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
  92. static const char *wm8971_mono_mux[] = {"Stereo", "Mono (Left)",
  93. "Mono (Right)", "Digital Mono"};
  94. static const char *wm8971_dac_phase[] = { "Non Inverted", "Inverted" };
  95. static const char *wm8971_lline_mux[] = {"Line", "NC", "NC", "PGA",
  96. "Differential"};
  97. static const char *wm8971_rline_mux[] = {"Line", "Mic", "NC", "PGA",
  98. "Differential"};
  99. static const char *wm8971_lpga_sel[] = {"Line", "NC", "NC", "Differential"};
  100. static const char *wm8971_rpga_sel[] = {"Line", "Mic", "NC", "Differential"};
  101. static const char *wm8971_adcpol[] = {"Normal", "L Invert", "R Invert",
  102. "L + R Invert"};
  103. static const struct soc_enum wm8971_enum[] = {
  104. SOC_ENUM_SINGLE(WM8971_BASS, 7, 2, wm8971_bass), /* 0 */
  105. SOC_ENUM_SINGLE(WM8971_BASS, 6, 2, wm8971_bass_filter),
  106. SOC_ENUM_SINGLE(WM8971_TREBLE, 6, 2, wm8971_treble),
  107. SOC_ENUM_SINGLE(WM8971_ALC1, 7, 4, wm8971_alc_func),
  108. SOC_ENUM_SINGLE(WM8971_NGATE, 1, 2, wm8971_ng_type), /* 4 */
  109. SOC_ENUM_SINGLE(WM8971_ADCDAC, 1, 4, wm8971_deemp),
  110. SOC_ENUM_SINGLE(WM8971_ADCTL1, 4, 4, wm8971_mono_mux),
  111. SOC_ENUM_SINGLE(WM8971_ADCTL1, 1, 2, wm8971_dac_phase),
  112. SOC_ENUM_SINGLE(WM8971_LOUTM1, 0, 5, wm8971_lline_mux), /* 8 */
  113. SOC_ENUM_SINGLE(WM8971_ROUTM1, 0, 5, wm8971_rline_mux),
  114. SOC_ENUM_SINGLE(WM8971_LADCIN, 6, 4, wm8971_lpga_sel),
  115. SOC_ENUM_SINGLE(WM8971_RADCIN, 6, 4, wm8971_rpga_sel),
  116. SOC_ENUM_SINGLE(WM8971_ADCDAC, 5, 4, wm8971_adcpol), /* 12 */
  117. SOC_ENUM_SINGLE(WM8971_ADCIN, 6, 4, wm8971_mono_mux),
  118. };
  119. static const struct snd_kcontrol_new wm8971_snd_controls[] = {
  120. SOC_DOUBLE_R("Capture Volume", WM8971_LINVOL, WM8971_RINVOL, 0, 63, 0),
  121. SOC_DOUBLE_R("Capture ZC Switch", WM8971_LINVOL, WM8971_RINVOL,
  122. 6, 1, 0),
  123. SOC_DOUBLE_R("Capture Switch", WM8971_LINVOL, WM8971_RINVOL, 7, 1, 1),
  124. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8971_LOUT1V,
  125. WM8971_ROUT1V, 7, 1, 0),
  126. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8971_LOUT2V,
  127. WM8971_ROUT2V, 7, 1, 0),
  128. SOC_SINGLE("Mono Playback ZC Switch", WM8971_MOUTV, 7, 1, 0),
  129. SOC_DOUBLE_R("PCM Volume", WM8971_LDAC, WM8971_RDAC, 0, 255, 0),
  130. SOC_DOUBLE_R("Bypass Left Playback Volume", WM8971_LOUTM1,
  131. WM8971_LOUTM2, 4, 7, 1),
  132. SOC_DOUBLE_R("Bypass Right Playback Volume", WM8971_ROUTM1,
  133. WM8971_ROUTM2, 4, 7, 1),
  134. SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8971_MOUTM1,
  135. WM8971_MOUTM2, 4, 7, 1),
  136. SOC_DOUBLE_R("Headphone Playback Volume", WM8971_LOUT1V,
  137. WM8971_ROUT1V, 0, 127, 0),
  138. SOC_DOUBLE_R("Speaker Playback Volume", WM8971_LOUT2V,
  139. WM8971_ROUT2V, 0, 127, 0),
  140. SOC_ENUM("Bass Boost", wm8971_enum[0]),
  141. SOC_ENUM("Bass Filter", wm8971_enum[1]),
  142. SOC_SINGLE("Bass Volume", WM8971_BASS, 0, 7, 1),
  143. SOC_SINGLE("Treble Volume", WM8971_TREBLE, 0, 7, 0),
  144. SOC_ENUM("Treble Cut-off", wm8971_enum[2]),
  145. SOC_SINGLE("Capture Filter Switch", WM8971_ADCDAC, 0, 1, 1),
  146. SOC_SINGLE("ALC Target Volume", WM8971_ALC1, 0, 7, 0),
  147. SOC_SINGLE("ALC Max Volume", WM8971_ALC1, 4, 7, 0),
  148. SOC_SINGLE("ALC Capture Target Volume", WM8971_ALC1, 0, 7, 0),
  149. SOC_SINGLE("ALC Capture Max Volume", WM8971_ALC1, 4, 7, 0),
  150. SOC_ENUM("ALC Capture Function", wm8971_enum[3]),
  151. SOC_SINGLE("ALC Capture ZC Switch", WM8971_ALC2, 7, 1, 0),
  152. SOC_SINGLE("ALC Capture Hold Time", WM8971_ALC2, 0, 15, 0),
  153. SOC_SINGLE("ALC Capture Decay Time", WM8971_ALC3, 4, 15, 0),
  154. SOC_SINGLE("ALC Capture Attack Time", WM8971_ALC3, 0, 15, 0),
  155. SOC_SINGLE("ALC Capture NG Threshold", WM8971_NGATE, 3, 31, 0),
  156. SOC_ENUM("ALC Capture NG Type", wm8971_enum[4]),
  157. SOC_SINGLE("ALC Capture NG Switch", WM8971_NGATE, 0, 1, 0),
  158. SOC_SINGLE("Capture 6dB Attenuate", WM8971_ADCDAC, 8, 1, 0),
  159. SOC_SINGLE("Playback 6dB Attenuate", WM8971_ADCDAC, 7, 1, 0),
  160. SOC_ENUM("Playback De-emphasis", wm8971_enum[5]),
  161. SOC_ENUM("Playback Function", wm8971_enum[6]),
  162. SOC_ENUM("Playback Phase", wm8971_enum[7]),
  163. SOC_DOUBLE_R("Mic Boost", WM8971_LADCIN, WM8971_RADCIN, 4, 3, 0),
  164. };
  165. /*
  166. * DAPM Controls
  167. */
  168. /* Left Mixer */
  169. static const struct snd_kcontrol_new wm8971_left_mixer_controls[] = {
  170. SOC_DAPM_SINGLE("Playback Switch", WM8971_LOUTM1, 8, 1, 0),
  171. SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_LOUTM1, 7, 1, 0),
  172. SOC_DAPM_SINGLE("Right Playback Switch", WM8971_LOUTM2, 8, 1, 0),
  173. SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_LOUTM2, 7, 1, 0),
  174. };
  175. /* Right Mixer */
  176. static const struct snd_kcontrol_new wm8971_right_mixer_controls[] = {
  177. SOC_DAPM_SINGLE("Left Playback Switch", WM8971_ROUTM1, 8, 1, 0),
  178. SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_ROUTM1, 7, 1, 0),
  179. SOC_DAPM_SINGLE("Playback Switch", WM8971_ROUTM2, 8, 1, 0),
  180. SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_ROUTM2, 7, 1, 0),
  181. };
  182. /* Mono Mixer */
  183. static const struct snd_kcontrol_new wm8971_mono_mixer_controls[] = {
  184. SOC_DAPM_SINGLE("Left Playback Switch", WM8971_MOUTM1, 8, 1, 0),
  185. SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_MOUTM1, 7, 1, 0),
  186. SOC_DAPM_SINGLE("Right Playback Switch", WM8971_MOUTM2, 8, 1, 0),
  187. SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_MOUTM2, 7, 1, 0),
  188. };
  189. /* Left Line Mux */
  190. static const struct snd_kcontrol_new wm8971_left_line_controls =
  191. SOC_DAPM_ENUM("Route", wm8971_enum[8]);
  192. /* Right Line Mux */
  193. static const struct snd_kcontrol_new wm8971_right_line_controls =
  194. SOC_DAPM_ENUM("Route", wm8971_enum[9]);
  195. /* Left PGA Mux */
  196. static const struct snd_kcontrol_new wm8971_left_pga_controls =
  197. SOC_DAPM_ENUM("Route", wm8971_enum[10]);
  198. /* Right PGA Mux */
  199. static const struct snd_kcontrol_new wm8971_right_pga_controls =
  200. SOC_DAPM_ENUM("Route", wm8971_enum[11]);
  201. /* Mono ADC Mux */
  202. static const struct snd_kcontrol_new wm8971_monomux_controls =
  203. SOC_DAPM_ENUM("Route", wm8971_enum[13]);
  204. static const struct snd_soc_dapm_widget wm8971_dapm_widgets[] = {
  205. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  206. &wm8971_left_mixer_controls[0],
  207. ARRAY_SIZE(wm8971_left_mixer_controls)),
  208. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  209. &wm8971_right_mixer_controls[0],
  210. ARRAY_SIZE(wm8971_right_mixer_controls)),
  211. SND_SOC_DAPM_MIXER("Mono Mixer", WM8971_PWR2, 2, 0,
  212. &wm8971_mono_mixer_controls[0],
  213. ARRAY_SIZE(wm8971_mono_mixer_controls)),
  214. SND_SOC_DAPM_PGA("Right Out 2", WM8971_PWR2, 3, 0, NULL, 0),
  215. SND_SOC_DAPM_PGA("Left Out 2", WM8971_PWR2, 4, 0, NULL, 0),
  216. SND_SOC_DAPM_PGA("Right Out 1", WM8971_PWR2, 5, 0, NULL, 0),
  217. SND_SOC_DAPM_PGA("Left Out 1", WM8971_PWR2, 6, 0, NULL, 0),
  218. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8971_PWR2, 7, 0),
  219. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8971_PWR2, 8, 0),
  220. SND_SOC_DAPM_PGA("Mono Out 1", WM8971_PWR2, 2, 0, NULL, 0),
  221. SND_SOC_DAPM_SUPPLY("Mic Bias", WM8971_PWR1, 1, 0, NULL, 0),
  222. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8971_PWR1, 2, 0),
  223. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8971_PWR1, 3, 0),
  224. SND_SOC_DAPM_MUX("Left PGA Mux", WM8971_PWR1, 5, 0,
  225. &wm8971_left_pga_controls),
  226. SND_SOC_DAPM_MUX("Right PGA Mux", WM8971_PWR1, 4, 0,
  227. &wm8971_right_pga_controls),
  228. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  229. &wm8971_left_line_controls),
  230. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  231. &wm8971_right_line_controls),
  232. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  233. &wm8971_monomux_controls),
  234. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  235. &wm8971_monomux_controls),
  236. SND_SOC_DAPM_OUTPUT("LOUT1"),
  237. SND_SOC_DAPM_OUTPUT("ROUT1"),
  238. SND_SOC_DAPM_OUTPUT("LOUT2"),
  239. SND_SOC_DAPM_OUTPUT("ROUT2"),
  240. SND_SOC_DAPM_OUTPUT("MONO"),
  241. SND_SOC_DAPM_INPUT("LINPUT1"),
  242. SND_SOC_DAPM_INPUT("RINPUT1"),
  243. SND_SOC_DAPM_INPUT("MIC"),
  244. };
  245. static const struct snd_soc_dapm_route wm8971_dapm_routes[] = {
  246. /* left mixer */
  247. {"Left Mixer", "Playback Switch", "Left DAC"},
  248. {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
  249. {"Left Mixer", "Right Playback Switch", "Right DAC"},
  250. {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
  251. /* right mixer */
  252. {"Right Mixer", "Left Playback Switch", "Left DAC"},
  253. {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
  254. {"Right Mixer", "Playback Switch", "Right DAC"},
  255. {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
  256. /* left out 1 */
  257. {"Left Out 1", NULL, "Left Mixer"},
  258. {"LOUT1", NULL, "Left Out 1"},
  259. /* left out 2 */
  260. {"Left Out 2", NULL, "Left Mixer"},
  261. {"LOUT2", NULL, "Left Out 2"},
  262. /* right out 1 */
  263. {"Right Out 1", NULL, "Right Mixer"},
  264. {"ROUT1", NULL, "Right Out 1"},
  265. /* right out 2 */
  266. {"Right Out 2", NULL, "Right Mixer"},
  267. {"ROUT2", NULL, "Right Out 2"},
  268. /* mono mixer */
  269. {"Mono Mixer", "Left Playback Switch", "Left DAC"},
  270. {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
  271. {"Mono Mixer", "Right Playback Switch", "Right DAC"},
  272. {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
  273. /* mono out */
  274. {"Mono Out", NULL, "Mono Mixer"},
  275. {"MONO1", NULL, "Mono Out"},
  276. /* Left Line Mux */
  277. {"Left Line Mux", "Line", "LINPUT1"},
  278. {"Left Line Mux", "PGA", "Left PGA Mux"},
  279. {"Left Line Mux", "Differential", "Differential Mux"},
  280. /* Right Line Mux */
  281. {"Right Line Mux", "Line", "RINPUT1"},
  282. {"Right Line Mux", "Mic", "MIC"},
  283. {"Right Line Mux", "PGA", "Right PGA Mux"},
  284. {"Right Line Mux", "Differential", "Differential Mux"},
  285. /* Left PGA Mux */
  286. {"Left PGA Mux", "Line", "LINPUT1"},
  287. {"Left PGA Mux", "Differential", "Differential Mux"},
  288. /* Right PGA Mux */
  289. {"Right PGA Mux", "Line", "RINPUT1"},
  290. {"Right PGA Mux", "Differential", "Differential Mux"},
  291. /* Differential Mux */
  292. {"Differential Mux", "Line", "LINPUT1"},
  293. {"Differential Mux", "Line", "RINPUT1"},
  294. /* Left ADC Mux */
  295. {"Left ADC Mux", "Stereo", "Left PGA Mux"},
  296. {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
  297. {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
  298. /* Right ADC Mux */
  299. {"Right ADC Mux", "Stereo", "Right PGA Mux"},
  300. {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
  301. {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
  302. /* ADC */
  303. {"Left ADC", NULL, "Left ADC Mux"},
  304. {"Right ADC", NULL, "Right ADC Mux"},
  305. };
  306. struct _coeff_div {
  307. u32 mclk;
  308. u32 rate;
  309. u16 fs;
  310. u8 sr:5;
  311. u8 usb:1;
  312. };
  313. /* codec hifi mclk clock divider coefficients */
  314. static const struct _coeff_div coeff_div[] = {
  315. /* 8k */
  316. {12288000, 8000, 1536, 0x6, 0x0},
  317. {11289600, 8000, 1408, 0x16, 0x0},
  318. {18432000, 8000, 2304, 0x7, 0x0},
  319. {16934400, 8000, 2112, 0x17, 0x0},
  320. {12000000, 8000, 1500, 0x6, 0x1},
  321. /* 11.025k */
  322. {11289600, 11025, 1024, 0x18, 0x0},
  323. {16934400, 11025, 1536, 0x19, 0x0},
  324. {12000000, 11025, 1088, 0x19, 0x1},
  325. /* 16k */
  326. {12288000, 16000, 768, 0xa, 0x0},
  327. {18432000, 16000, 1152, 0xb, 0x0},
  328. {12000000, 16000, 750, 0xa, 0x1},
  329. /* 22.05k */
  330. {11289600, 22050, 512, 0x1a, 0x0},
  331. {16934400, 22050, 768, 0x1b, 0x0},
  332. {12000000, 22050, 544, 0x1b, 0x1},
  333. /* 32k */
  334. {12288000, 32000, 384, 0xc, 0x0},
  335. {18432000, 32000, 576, 0xd, 0x0},
  336. {12000000, 32000, 375, 0xa, 0x1},
  337. /* 44.1k */
  338. {11289600, 44100, 256, 0x10, 0x0},
  339. {16934400, 44100, 384, 0x11, 0x0},
  340. {12000000, 44100, 272, 0x11, 0x1},
  341. /* 48k */
  342. {12288000, 48000, 256, 0x0, 0x0},
  343. {18432000, 48000, 384, 0x1, 0x0},
  344. {12000000, 48000, 250, 0x0, 0x1},
  345. /* 88.2k */
  346. {11289600, 88200, 128, 0x1e, 0x0},
  347. {16934400, 88200, 192, 0x1f, 0x0},
  348. {12000000, 88200, 136, 0x1f, 0x1},
  349. /* 96k */
  350. {12288000, 96000, 128, 0xe, 0x0},
  351. {18432000, 96000, 192, 0xf, 0x0},
  352. {12000000, 96000, 125, 0xe, 0x1},
  353. };
  354. static int get_coeff(int mclk, int rate)
  355. {
  356. int i;
  357. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  358. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  359. return i;
  360. }
  361. return -EINVAL;
  362. }
  363. static int wm8971_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  364. int clk_id, unsigned int freq, int dir)
  365. {
  366. struct snd_soc_component *component = codec_dai->component;
  367. struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
  368. switch (freq) {
  369. case 11289600:
  370. case 12000000:
  371. case 12288000:
  372. case 16934400:
  373. case 18432000:
  374. wm8971->sysclk = freq;
  375. return 0;
  376. }
  377. return -EINVAL;
  378. }
  379. static int wm8971_set_dai_fmt(struct snd_soc_dai *codec_dai,
  380. unsigned int fmt)
  381. {
  382. struct snd_soc_component *component = codec_dai->component;
  383. u16 iface = 0;
  384. /* set master/slave audio interface */
  385. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  386. case SND_SOC_DAIFMT_CBM_CFM:
  387. iface = 0x0040;
  388. break;
  389. case SND_SOC_DAIFMT_CBS_CFS:
  390. break;
  391. default:
  392. return -EINVAL;
  393. }
  394. /* interface format */
  395. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  396. case SND_SOC_DAIFMT_I2S:
  397. iface |= 0x0002;
  398. break;
  399. case SND_SOC_DAIFMT_RIGHT_J:
  400. break;
  401. case SND_SOC_DAIFMT_LEFT_J:
  402. iface |= 0x0001;
  403. break;
  404. case SND_SOC_DAIFMT_DSP_A:
  405. iface |= 0x0003;
  406. break;
  407. case SND_SOC_DAIFMT_DSP_B:
  408. iface |= 0x0013;
  409. break;
  410. default:
  411. return -EINVAL;
  412. }
  413. /* clock inversion */
  414. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  415. case SND_SOC_DAIFMT_NB_NF:
  416. break;
  417. case SND_SOC_DAIFMT_IB_IF:
  418. iface |= 0x0090;
  419. break;
  420. case SND_SOC_DAIFMT_IB_NF:
  421. iface |= 0x0080;
  422. break;
  423. case SND_SOC_DAIFMT_NB_IF:
  424. iface |= 0x0010;
  425. break;
  426. default:
  427. return -EINVAL;
  428. }
  429. snd_soc_component_write(component, WM8971_IFACE, iface);
  430. return 0;
  431. }
  432. static int wm8971_pcm_hw_params(struct snd_pcm_substream *substream,
  433. struct snd_pcm_hw_params *params,
  434. struct snd_soc_dai *dai)
  435. {
  436. struct snd_soc_component *component = dai->component;
  437. struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
  438. u16 iface = snd_soc_component_read(component, WM8971_IFACE) & 0x1f3;
  439. u16 srate = snd_soc_component_read(component, WM8971_SRATE) & 0x1c0;
  440. int coeff = get_coeff(wm8971->sysclk, params_rate(params));
  441. /* bit size */
  442. switch (params_width(params)) {
  443. case 16:
  444. break;
  445. case 20:
  446. iface |= 0x0004;
  447. break;
  448. case 24:
  449. iface |= 0x0008;
  450. break;
  451. case 32:
  452. iface |= 0x000c;
  453. break;
  454. }
  455. /* set iface & srate */
  456. snd_soc_component_write(component, WM8971_IFACE, iface);
  457. if (coeff >= 0)
  458. snd_soc_component_write(component, WM8971_SRATE, srate |
  459. (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
  460. return 0;
  461. }
  462. static int wm8971_mute(struct snd_soc_dai *dai, int mute, int direction)
  463. {
  464. struct snd_soc_component *component = dai->component;
  465. u16 mute_reg = snd_soc_component_read(component, WM8971_ADCDAC) & 0xfff7;
  466. if (mute)
  467. snd_soc_component_write(component, WM8971_ADCDAC, mute_reg | 0x8);
  468. else
  469. snd_soc_component_write(component, WM8971_ADCDAC, mute_reg);
  470. return 0;
  471. }
  472. static void wm8971_charge_work(struct work_struct *work)
  473. {
  474. struct wm8971_priv *wm8971 =
  475. container_of(work, struct wm8971_priv, charge_work.work);
  476. /* Set to 500k */
  477. regmap_update_bits(wm8971->regmap, WM8971_PWR1, 0x0180, 0x0100);
  478. }
  479. static int wm8971_set_bias_level(struct snd_soc_component *component,
  480. enum snd_soc_bias_level level)
  481. {
  482. struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
  483. u16 pwr_reg = snd_soc_component_read(component, WM8971_PWR1) & 0xfe3e;
  484. switch (level) {
  485. case SND_SOC_BIAS_ON:
  486. /* set vmid to 50k and unmute dac */
  487. snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x00c1);
  488. break;
  489. case SND_SOC_BIAS_PREPARE:
  490. /* Wait until fully charged */
  491. flush_delayed_work(&wm8971->charge_work);
  492. break;
  493. case SND_SOC_BIAS_STANDBY:
  494. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
  495. snd_soc_component_cache_sync(component);
  496. /* charge output caps - set vmid to 5k for quick power up */
  497. snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x01c0);
  498. queue_delayed_work(system_power_efficient_wq,
  499. &wm8971->charge_work, msecs_to_jiffies(1000));
  500. } else {
  501. /* mute dac and set vmid to 500k, enable VREF */
  502. snd_soc_component_write(component, WM8971_PWR1, pwr_reg | 0x0140);
  503. }
  504. break;
  505. case SND_SOC_BIAS_OFF:
  506. cancel_delayed_work_sync(&wm8971->charge_work);
  507. snd_soc_component_write(component, WM8971_PWR1, 0x0001);
  508. break;
  509. }
  510. return 0;
  511. }
  512. #define WM8971_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  513. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  514. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  515. #define WM8971_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  516. SNDRV_PCM_FMTBIT_S24_LE)
  517. static const struct snd_soc_dai_ops wm8971_dai_ops = {
  518. .hw_params = wm8971_pcm_hw_params,
  519. .mute_stream = wm8971_mute,
  520. .set_fmt = wm8971_set_dai_fmt,
  521. .set_sysclk = wm8971_set_dai_sysclk,
  522. .no_capture_mute = 1,
  523. };
  524. static struct snd_soc_dai_driver wm8971_dai = {
  525. .name = "wm8971-hifi",
  526. .playback = {
  527. .stream_name = "Playback",
  528. .channels_min = 1,
  529. .channels_max = 2,
  530. .rates = WM8971_RATES,
  531. .formats = WM8971_FORMATS,},
  532. .capture = {
  533. .stream_name = "Capture",
  534. .channels_min = 1,
  535. .channels_max = 2,
  536. .rates = WM8971_RATES,
  537. .formats = WM8971_FORMATS,},
  538. .ops = &wm8971_dai_ops,
  539. };
  540. static int wm8971_probe(struct snd_soc_component *component)
  541. {
  542. struct wm8971_priv *wm8971 = snd_soc_component_get_drvdata(component);
  543. INIT_DELAYED_WORK(&wm8971->charge_work, wm8971_charge_work);
  544. wm8971_reset(component);
  545. /* set the update bits */
  546. snd_soc_component_update_bits(component, WM8971_LDAC, 0x0100, 0x0100);
  547. snd_soc_component_update_bits(component, WM8971_RDAC, 0x0100, 0x0100);
  548. snd_soc_component_update_bits(component, WM8971_LOUT1V, 0x0100, 0x0100);
  549. snd_soc_component_update_bits(component, WM8971_ROUT1V, 0x0100, 0x0100);
  550. snd_soc_component_update_bits(component, WM8971_LOUT2V, 0x0100, 0x0100);
  551. snd_soc_component_update_bits(component, WM8971_ROUT2V, 0x0100, 0x0100);
  552. snd_soc_component_update_bits(component, WM8971_LINVOL, 0x0100, 0x0100);
  553. snd_soc_component_update_bits(component, WM8971_RINVOL, 0x0100, 0x0100);
  554. return 0;
  555. }
  556. static const struct snd_soc_component_driver soc_component_dev_wm8971 = {
  557. .probe = wm8971_probe,
  558. .set_bias_level = wm8971_set_bias_level,
  559. .controls = wm8971_snd_controls,
  560. .num_controls = ARRAY_SIZE(wm8971_snd_controls),
  561. .dapm_widgets = wm8971_dapm_widgets,
  562. .num_dapm_widgets = ARRAY_SIZE(wm8971_dapm_widgets),
  563. .dapm_routes = wm8971_dapm_routes,
  564. .num_dapm_routes = ARRAY_SIZE(wm8971_dapm_routes),
  565. .suspend_bias_off = 1,
  566. .idle_bias_on = 1,
  567. .use_pmdown_time = 1,
  568. .endianness = 1,
  569. .non_legacy_dai_naming = 1,
  570. };
  571. static const struct regmap_config wm8971_regmap = {
  572. .reg_bits = 7,
  573. .val_bits = 9,
  574. .max_register = WM8971_MOUTV,
  575. .reg_defaults = wm8971_reg_defaults,
  576. .num_reg_defaults = ARRAY_SIZE(wm8971_reg_defaults),
  577. .cache_type = REGCACHE_RBTREE,
  578. };
  579. static int wm8971_i2c_probe(struct i2c_client *i2c,
  580. const struct i2c_device_id *id)
  581. {
  582. struct wm8971_priv *wm8971;
  583. int ret;
  584. wm8971 = devm_kzalloc(&i2c->dev, sizeof(struct wm8971_priv),
  585. GFP_KERNEL);
  586. if (wm8971 == NULL)
  587. return -ENOMEM;
  588. wm8971->regmap = devm_regmap_init_i2c(i2c, &wm8971_regmap);
  589. if (IS_ERR(wm8971->regmap))
  590. return PTR_ERR(wm8971->regmap);
  591. i2c_set_clientdata(i2c, wm8971);
  592. ret = devm_snd_soc_register_component(&i2c->dev,
  593. &soc_component_dev_wm8971, &wm8971_dai, 1);
  594. return ret;
  595. }
  596. static const struct i2c_device_id wm8971_i2c_id[] = {
  597. { "wm8971", 0 },
  598. { }
  599. };
  600. MODULE_DEVICE_TABLE(i2c, wm8971_i2c_id);
  601. static struct i2c_driver wm8971_i2c_driver = {
  602. .driver = {
  603. .name = "wm8971",
  604. },
  605. .probe = wm8971_i2c_probe,
  606. .id_table = wm8971_i2c_id,
  607. };
  608. module_i2c_driver(wm8971_i2c_driver);
  609. MODULE_DESCRIPTION("ASoC WM8971 driver");
  610. MODULE_AUTHOR("Lab126");
  611. MODULE_LICENSE("GPL");