wm8971.c 21 KB

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