wm8971.c 20 KB

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