wm8750.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * wm8750.c -- WM8750 ALSA SoC audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on WM8753.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/slab.h>
  23. #include <linux/of_device.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/initval.h>
  29. #include "wm8750.h"
  30. /*
  31. * wm8750 register cache
  32. * We can't read the WM8750 register space when we
  33. * are using 2 wire for device control, so we cache them instead.
  34. */
  35. static const struct reg_default wm8750_reg_defaults[] = {
  36. { 0, 0x0097 },
  37. { 1, 0x0097 },
  38. { 2, 0x0079 },
  39. { 3, 0x0079 },
  40. { 4, 0x0000 },
  41. { 5, 0x0008 },
  42. { 6, 0x0000 },
  43. { 7, 0x000a },
  44. { 8, 0x0000 },
  45. { 9, 0x0000 },
  46. { 10, 0x00ff },
  47. { 11, 0x00ff },
  48. { 12, 0x000f },
  49. { 13, 0x000f },
  50. { 14, 0x0000 },
  51. { 15, 0x0000 },
  52. { 16, 0x0000 },
  53. { 17, 0x007b },
  54. { 18, 0x0000 },
  55. { 19, 0x0032 },
  56. { 20, 0x0000 },
  57. { 21, 0x00c3 },
  58. { 22, 0x00c3 },
  59. { 23, 0x00c0 },
  60. { 24, 0x0000 },
  61. { 25, 0x0000 },
  62. { 26, 0x0000 },
  63. { 27, 0x0000 },
  64. { 28, 0x0000 },
  65. { 29, 0x0000 },
  66. { 30, 0x0000 },
  67. { 31, 0x0000 },
  68. { 32, 0x0000 },
  69. { 33, 0x0000 },
  70. { 34, 0x0050 },
  71. { 35, 0x0050 },
  72. { 36, 0x0050 },
  73. { 37, 0x0050 },
  74. { 38, 0x0050 },
  75. { 39, 0x0050 },
  76. { 40, 0x0079 },
  77. { 41, 0x0079 },
  78. { 42, 0x0079 },
  79. };
  80. /* codec private data */
  81. struct wm8750_priv {
  82. unsigned int sysclk;
  83. };
  84. #define wm8750_reset(c) snd_soc_write(c, WM8750_RESET, 0)
  85. /*
  86. * WM8750 Controls
  87. */
  88. static const char *wm8750_bass[] = {"Linear Control", "Adaptive Boost"};
  89. static const char *wm8750_bass_filter[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
  90. static const char *wm8750_treble[] = {"8kHz", "4kHz"};
  91. static const char *wm8750_3d_lc[] = {"200Hz", "500Hz"};
  92. static const char *wm8750_3d_uc[] = {"2.2kHz", "1.5kHz"};
  93. static const char *wm8750_3d_func[] = {"Capture", "Playback"};
  94. static const char *wm8750_alc_func[] = {"Off", "Right", "Left", "Stereo"};
  95. static const char *wm8750_ng_type[] = {"Constant PGA Gain",
  96. "Mute ADC Output"};
  97. static const char *wm8750_line_mux[] = {"Line 1", "Line 2", "Line 3", "PGA",
  98. "Differential"};
  99. static const char *wm8750_pga_sel[] = {"Line 1", "Line 2", "Line 3",
  100. "Differential"};
  101. static const char *wm8750_out3[] = {"VREF", "ROUT1 + Vol", "MonoOut",
  102. "ROUT1"};
  103. static const char *wm8750_diff_sel[] = {"Line 1", "Line 2"};
  104. static const char *wm8750_adcpol[] = {"Normal", "L Invert", "R Invert",
  105. "L + R Invert"};
  106. static const char *wm8750_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  107. static const char *wm8750_mono_mux[] = {"Stereo", "Mono (Left)",
  108. "Mono (Right)", "Digital Mono"};
  109. static const struct soc_enum wm8750_enum[] = {
  110. SOC_ENUM_SINGLE(WM8750_BASS, 7, 2, wm8750_bass),
  111. SOC_ENUM_SINGLE(WM8750_BASS, 6, 2, wm8750_bass_filter),
  112. SOC_ENUM_SINGLE(WM8750_TREBLE, 6, 2, wm8750_treble),
  113. SOC_ENUM_SINGLE(WM8750_3D, 5, 2, wm8750_3d_lc),
  114. SOC_ENUM_SINGLE(WM8750_3D, 6, 2, wm8750_3d_uc),
  115. SOC_ENUM_SINGLE(WM8750_3D, 7, 2, wm8750_3d_func),
  116. SOC_ENUM_SINGLE(WM8750_ALC1, 7, 4, wm8750_alc_func),
  117. SOC_ENUM_SINGLE(WM8750_NGATE, 1, 2, wm8750_ng_type),
  118. SOC_ENUM_SINGLE(WM8750_LOUTM1, 0, 5, wm8750_line_mux),
  119. SOC_ENUM_SINGLE(WM8750_ROUTM1, 0, 5, wm8750_line_mux),
  120. SOC_ENUM_SINGLE(WM8750_LADCIN, 6, 4, wm8750_pga_sel), /* 10 */
  121. SOC_ENUM_SINGLE(WM8750_RADCIN, 6, 4, wm8750_pga_sel),
  122. SOC_ENUM_SINGLE(WM8750_ADCTL2, 7, 4, wm8750_out3),
  123. SOC_ENUM_SINGLE(WM8750_ADCIN, 8, 2, wm8750_diff_sel),
  124. SOC_ENUM_SINGLE(WM8750_ADCDAC, 5, 4, wm8750_adcpol),
  125. SOC_ENUM_SINGLE(WM8750_ADCDAC, 1, 4, wm8750_deemph),
  126. SOC_ENUM_SINGLE(WM8750_ADCIN, 6, 4, wm8750_mono_mux), /* 16 */
  127. };
  128. static const struct snd_kcontrol_new wm8750_snd_controls[] = {
  129. SOC_DOUBLE_R("Capture Volume", WM8750_LINVOL, WM8750_RINVOL, 0, 63, 0),
  130. SOC_DOUBLE_R("Capture ZC Switch", WM8750_LINVOL, WM8750_RINVOL, 6, 1, 0),
  131. SOC_DOUBLE_R("Capture Switch", WM8750_LINVOL, WM8750_RINVOL, 7, 1, 1),
  132. SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8750_LOUT1V,
  133. WM8750_ROUT1V, 7, 1, 0),
  134. SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8750_LOUT2V,
  135. WM8750_ROUT2V, 7, 1, 0),
  136. SOC_ENUM("Playback De-emphasis", wm8750_enum[15]),
  137. SOC_ENUM("Capture Polarity", wm8750_enum[14]),
  138. SOC_SINGLE("Playback 6dB Attenuate", WM8750_ADCDAC, 7, 1, 0),
  139. SOC_SINGLE("Capture 6dB Attenuate", WM8750_ADCDAC, 8, 1, 0),
  140. SOC_DOUBLE_R("PCM Volume", WM8750_LDAC, WM8750_RDAC, 0, 255, 0),
  141. SOC_ENUM("Bass Boost", wm8750_enum[0]),
  142. SOC_ENUM("Bass Filter", wm8750_enum[1]),
  143. SOC_SINGLE("Bass Volume", WM8750_BASS, 0, 15, 1),
  144. SOC_SINGLE("Treble Volume", WM8750_TREBLE, 0, 15, 1),
  145. SOC_ENUM("Treble Cut-off", wm8750_enum[2]),
  146. SOC_SINGLE("3D Switch", WM8750_3D, 0, 1, 0),
  147. SOC_SINGLE("3D Volume", WM8750_3D, 1, 15, 0),
  148. SOC_ENUM("3D Lower Cut-off", wm8750_enum[3]),
  149. SOC_ENUM("3D Upper Cut-off", wm8750_enum[4]),
  150. SOC_ENUM("3D Mode", wm8750_enum[5]),
  151. SOC_SINGLE("ALC Capture Target Volume", WM8750_ALC1, 0, 7, 0),
  152. SOC_SINGLE("ALC Capture Max Volume", WM8750_ALC1, 4, 7, 0),
  153. SOC_ENUM("ALC Capture Function", wm8750_enum[6]),
  154. SOC_SINGLE("ALC Capture ZC Switch", WM8750_ALC2, 7, 1, 0),
  155. SOC_SINGLE("ALC Capture Hold Time", WM8750_ALC2, 0, 15, 0),
  156. SOC_SINGLE("ALC Capture Decay Time", WM8750_ALC3, 4, 15, 0),
  157. SOC_SINGLE("ALC Capture Attack Time", WM8750_ALC3, 0, 15, 0),
  158. SOC_SINGLE("ALC Capture NG Threshold", WM8750_NGATE, 3, 31, 0),
  159. SOC_ENUM("ALC Capture NG Type", wm8750_enum[4]),
  160. SOC_SINGLE("ALC Capture NG Switch", WM8750_NGATE, 0, 1, 0),
  161. SOC_SINGLE("Left ADC Capture Volume", WM8750_LADC, 0, 255, 0),
  162. SOC_SINGLE("Right ADC Capture Volume", WM8750_RADC, 0, 255, 0),
  163. SOC_SINGLE("ZC Timeout Switch", WM8750_ADCTL1, 0, 1, 0),
  164. SOC_SINGLE("Playback Invert Switch", WM8750_ADCTL1, 1, 1, 0),
  165. SOC_SINGLE("Right Speaker Playback Invert Switch", WM8750_ADCTL2, 4, 1, 0),
  166. /* Unimplemented */
  167. /* ADCDAC Bit 0 - ADCHPD */
  168. /* ADCDAC Bit 4 - HPOR */
  169. /* ADCTL1 Bit 2,3 - DATSEL */
  170. /* ADCTL1 Bit 4,5 - DMONOMIX */
  171. /* ADCTL1 Bit 6,7 - VSEL */
  172. /* ADCTL2 Bit 2 - LRCM */
  173. /* ADCTL2 Bit 3 - TRI */
  174. /* ADCTL3 Bit 5 - HPFLREN */
  175. /* ADCTL3 Bit 6 - VROI */
  176. /* ADCTL3 Bit 7,8 - ADCLRM */
  177. /* ADCIN Bit 4 - LDCM */
  178. /* ADCIN Bit 5 - RDCM */
  179. SOC_DOUBLE_R("Mic Boost", WM8750_LADCIN, WM8750_RADCIN, 4, 3, 0),
  180. SOC_DOUBLE_R("Bypass Left Playback Volume", WM8750_LOUTM1,
  181. WM8750_LOUTM2, 4, 7, 1),
  182. SOC_DOUBLE_R("Bypass Right Playback Volume", WM8750_ROUTM1,
  183. WM8750_ROUTM2, 4, 7, 1),
  184. SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8750_MOUTM1,
  185. WM8750_MOUTM2, 4, 7, 1),
  186. SOC_SINGLE("Mono Playback ZC Switch", WM8750_MOUTV, 7, 1, 0),
  187. SOC_DOUBLE_R("Headphone Playback Volume", WM8750_LOUT1V, WM8750_ROUT1V,
  188. 0, 127, 0),
  189. SOC_DOUBLE_R("Speaker Playback Volume", WM8750_LOUT2V, WM8750_ROUT2V,
  190. 0, 127, 0),
  191. SOC_SINGLE("Mono Playback Volume", WM8750_MOUTV, 0, 127, 0),
  192. };
  193. /*
  194. * DAPM Controls
  195. */
  196. /* Left Mixer */
  197. static const struct snd_kcontrol_new wm8750_left_mixer_controls[] = {
  198. SOC_DAPM_SINGLE("Playback Switch", WM8750_LOUTM1, 8, 1, 0),
  199. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_LOUTM1, 7, 1, 0),
  200. SOC_DAPM_SINGLE("Right Playback Switch", WM8750_LOUTM2, 8, 1, 0),
  201. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_LOUTM2, 7, 1, 0),
  202. };
  203. /* Right Mixer */
  204. static const struct snd_kcontrol_new wm8750_right_mixer_controls[] = {
  205. SOC_DAPM_SINGLE("Left Playback Switch", WM8750_ROUTM1, 8, 1, 0),
  206. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_ROUTM1, 7, 1, 0),
  207. SOC_DAPM_SINGLE("Playback Switch", WM8750_ROUTM2, 8, 1, 0),
  208. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_ROUTM2, 7, 1, 0),
  209. };
  210. /* Mono Mixer */
  211. static const struct snd_kcontrol_new wm8750_mono_mixer_controls[] = {
  212. SOC_DAPM_SINGLE("Left Playback Switch", WM8750_MOUTM1, 8, 1, 0),
  213. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_MOUTM1, 7, 1, 0),
  214. SOC_DAPM_SINGLE("Right Playback Switch", WM8750_MOUTM2, 8, 1, 0),
  215. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_MOUTM2, 7, 1, 0),
  216. };
  217. /* Left Line Mux */
  218. static const struct snd_kcontrol_new wm8750_left_line_controls =
  219. SOC_DAPM_ENUM("Route", wm8750_enum[8]);
  220. /* Right Line Mux */
  221. static const struct snd_kcontrol_new wm8750_right_line_controls =
  222. SOC_DAPM_ENUM("Route", wm8750_enum[9]);
  223. /* Left PGA Mux */
  224. static const struct snd_kcontrol_new wm8750_left_pga_controls =
  225. SOC_DAPM_ENUM("Route", wm8750_enum[10]);
  226. /* Right PGA Mux */
  227. static const struct snd_kcontrol_new wm8750_right_pga_controls =
  228. SOC_DAPM_ENUM("Route", wm8750_enum[11]);
  229. /* Out 3 Mux */
  230. static const struct snd_kcontrol_new wm8750_out3_controls =
  231. SOC_DAPM_ENUM("Route", wm8750_enum[12]);
  232. /* Differential Mux */
  233. static const struct snd_kcontrol_new wm8750_diffmux_controls =
  234. SOC_DAPM_ENUM("Route", wm8750_enum[13]);
  235. /* Mono ADC Mux */
  236. static const struct snd_kcontrol_new wm8750_monomux_controls =
  237. SOC_DAPM_ENUM("Route", wm8750_enum[16]);
  238. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  239. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  240. &wm8750_left_mixer_controls[0],
  241. ARRAY_SIZE(wm8750_left_mixer_controls)),
  242. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  243. &wm8750_right_mixer_controls[0],
  244. ARRAY_SIZE(wm8750_right_mixer_controls)),
  245. SND_SOC_DAPM_MIXER("Mono Mixer", WM8750_PWR2, 2, 0,
  246. &wm8750_mono_mixer_controls[0],
  247. ARRAY_SIZE(wm8750_mono_mixer_controls)),
  248. SND_SOC_DAPM_PGA("Right Out 2", WM8750_PWR2, 3, 0, NULL, 0),
  249. SND_SOC_DAPM_PGA("Left Out 2", WM8750_PWR2, 4, 0, NULL, 0),
  250. SND_SOC_DAPM_PGA("Right Out 1", WM8750_PWR2, 5, 0, NULL, 0),
  251. SND_SOC_DAPM_PGA("Left Out 1", WM8750_PWR2, 6, 0, NULL, 0),
  252. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8750_PWR2, 7, 0),
  253. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8750_PWR2, 8, 0),
  254. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8750_PWR1, 1, 0),
  255. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8750_PWR1, 2, 0),
  256. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8750_PWR1, 3, 0),
  257. SND_SOC_DAPM_MUX("Left PGA Mux", WM8750_PWR1, 5, 0,
  258. &wm8750_left_pga_controls),
  259. SND_SOC_DAPM_MUX("Right PGA Mux", WM8750_PWR1, 4, 0,
  260. &wm8750_right_pga_controls),
  261. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  262. &wm8750_left_line_controls),
  263. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  264. &wm8750_right_line_controls),
  265. SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8750_out3_controls),
  266. SND_SOC_DAPM_PGA("Out 3", WM8750_PWR2, 1, 0, NULL, 0),
  267. SND_SOC_DAPM_PGA("Mono Out 1", WM8750_PWR2, 2, 0, NULL, 0),
  268. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  269. &wm8750_diffmux_controls),
  270. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  271. &wm8750_monomux_controls),
  272. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  273. &wm8750_monomux_controls),
  274. SND_SOC_DAPM_OUTPUT("LOUT1"),
  275. SND_SOC_DAPM_OUTPUT("ROUT1"),
  276. SND_SOC_DAPM_OUTPUT("LOUT2"),
  277. SND_SOC_DAPM_OUTPUT("ROUT2"),
  278. SND_SOC_DAPM_OUTPUT("MONO1"),
  279. SND_SOC_DAPM_OUTPUT("OUT3"),
  280. SND_SOC_DAPM_VMID("VREF"),
  281. SND_SOC_DAPM_INPUT("LINPUT1"),
  282. SND_SOC_DAPM_INPUT("LINPUT2"),
  283. SND_SOC_DAPM_INPUT("LINPUT3"),
  284. SND_SOC_DAPM_INPUT("RINPUT1"),
  285. SND_SOC_DAPM_INPUT("RINPUT2"),
  286. SND_SOC_DAPM_INPUT("RINPUT3"),
  287. };
  288. static const struct snd_soc_dapm_route wm8750_dapm_routes[] = {
  289. /* left mixer */
  290. {"Left Mixer", "Playback Switch", "Left DAC"},
  291. {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
  292. {"Left Mixer", "Right Playback Switch", "Right DAC"},
  293. {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
  294. /* right mixer */
  295. {"Right Mixer", "Left Playback Switch", "Left DAC"},
  296. {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
  297. {"Right Mixer", "Playback Switch", "Right DAC"},
  298. {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
  299. /* left out 1 */
  300. {"Left Out 1", NULL, "Left Mixer"},
  301. {"LOUT1", NULL, "Left Out 1"},
  302. /* left out 2 */
  303. {"Left Out 2", NULL, "Left Mixer"},
  304. {"LOUT2", NULL, "Left Out 2"},
  305. /* right out 1 */
  306. {"Right Out 1", NULL, "Right Mixer"},
  307. {"ROUT1", NULL, "Right Out 1"},
  308. /* right out 2 */
  309. {"Right Out 2", NULL, "Right Mixer"},
  310. {"ROUT2", NULL, "Right Out 2"},
  311. /* mono mixer */
  312. {"Mono Mixer", "Left Playback Switch", "Left DAC"},
  313. {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
  314. {"Mono Mixer", "Right Playback Switch", "Right DAC"},
  315. {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
  316. /* mono out */
  317. {"Mono Out 1", NULL, "Mono Mixer"},
  318. {"MONO1", NULL, "Mono Out 1"},
  319. /* out 3 */
  320. {"Out3 Mux", "VREF", "VREF"},
  321. {"Out3 Mux", "ROUT1 + Vol", "ROUT1"},
  322. {"Out3 Mux", "ROUT1", "Right Mixer"},
  323. {"Out3 Mux", "MonoOut", "MONO1"},
  324. {"Out 3", NULL, "Out3 Mux"},
  325. {"OUT3", NULL, "Out 3"},
  326. /* Left Line Mux */
  327. {"Left Line Mux", "Line 1", "LINPUT1"},
  328. {"Left Line Mux", "Line 2", "LINPUT2"},
  329. {"Left Line Mux", "Line 3", "LINPUT3"},
  330. {"Left Line Mux", "PGA", "Left PGA Mux"},
  331. {"Left Line Mux", "Differential", "Differential Mux"},
  332. /* Right Line Mux */
  333. {"Right Line Mux", "Line 1", "RINPUT1"},
  334. {"Right Line Mux", "Line 2", "RINPUT2"},
  335. {"Right Line Mux", "Line 3", "RINPUT3"},
  336. {"Right Line Mux", "PGA", "Right PGA Mux"},
  337. {"Right Line Mux", "Differential", "Differential Mux"},
  338. /* Left PGA Mux */
  339. {"Left PGA Mux", "Line 1", "LINPUT1"},
  340. {"Left PGA Mux", "Line 2", "LINPUT2"},
  341. {"Left PGA Mux", "Line 3", "LINPUT3"},
  342. {"Left PGA Mux", "Differential", "Differential Mux"},
  343. /* Right PGA Mux */
  344. {"Right PGA Mux", "Line 1", "RINPUT1"},
  345. {"Right PGA Mux", "Line 2", "RINPUT2"},
  346. {"Right PGA Mux", "Line 3", "RINPUT3"},
  347. {"Right PGA Mux", "Differential", "Differential Mux"},
  348. /* Differential Mux */
  349. {"Differential Mux", "Line 1", "LINPUT1"},
  350. {"Differential Mux", "Line 1", "RINPUT1"},
  351. {"Differential Mux", "Line 2", "LINPUT2"},
  352. {"Differential Mux", "Line 2", "RINPUT2"},
  353. /* Left ADC Mux */
  354. {"Left ADC Mux", "Stereo", "Left PGA Mux"},
  355. {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
  356. {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
  357. /* Right ADC Mux */
  358. {"Right ADC Mux", "Stereo", "Right PGA Mux"},
  359. {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
  360. {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
  361. /* ADC */
  362. {"Left ADC", NULL, "Left ADC Mux"},
  363. {"Right ADC", NULL, "Right ADC Mux"},
  364. };
  365. struct _coeff_div {
  366. u32 mclk;
  367. u32 rate;
  368. u16 fs;
  369. u8 sr:5;
  370. u8 usb:1;
  371. };
  372. /* codec hifi mclk clock divider coefficients */
  373. static const struct _coeff_div coeff_div[] = {
  374. /* 8k */
  375. {12288000, 8000, 1536, 0x6, 0x0},
  376. {11289600, 8000, 1408, 0x16, 0x0},
  377. {18432000, 8000, 2304, 0x7, 0x0},
  378. {16934400, 8000, 2112, 0x17, 0x0},
  379. {12000000, 8000, 1500, 0x6, 0x1},
  380. /* 11.025k */
  381. {11289600, 11025, 1024, 0x18, 0x0},
  382. {16934400, 11025, 1536, 0x19, 0x0},
  383. {12000000, 11025, 1088, 0x19, 0x1},
  384. /* 16k */
  385. {12288000, 16000, 768, 0xa, 0x0},
  386. {18432000, 16000, 1152, 0xb, 0x0},
  387. {12000000, 16000, 750, 0xa, 0x1},
  388. /* 22.05k */
  389. {11289600, 22050, 512, 0x1a, 0x0},
  390. {16934400, 22050, 768, 0x1b, 0x0},
  391. {12000000, 22050, 544, 0x1b, 0x1},
  392. /* 32k */
  393. {12288000, 32000, 384, 0xc, 0x0},
  394. {18432000, 32000, 576, 0xd, 0x0},
  395. {12000000, 32000, 375, 0xa, 0x1},
  396. /* 44.1k */
  397. {11289600, 44100, 256, 0x10, 0x0},
  398. {16934400, 44100, 384, 0x11, 0x0},
  399. {12000000, 44100, 272, 0x11, 0x1},
  400. /* 48k */
  401. {12288000, 48000, 256, 0x0, 0x0},
  402. {18432000, 48000, 384, 0x1, 0x0},
  403. {12000000, 48000, 250, 0x0, 0x1},
  404. /* 88.2k */
  405. {11289600, 88200, 128, 0x1e, 0x0},
  406. {16934400, 88200, 192, 0x1f, 0x0},
  407. {12000000, 88200, 136, 0x1f, 0x1},
  408. /* 96k */
  409. {12288000, 96000, 128, 0xe, 0x0},
  410. {18432000, 96000, 192, 0xf, 0x0},
  411. {12000000, 96000, 125, 0xe, 0x1},
  412. };
  413. static inline int get_coeff(int mclk, int rate)
  414. {
  415. int i;
  416. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  417. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  418. return i;
  419. }
  420. printk(KERN_ERR "wm8750: could not get coeff for mclk %d @ rate %d\n",
  421. mclk, rate);
  422. return -EINVAL;
  423. }
  424. static int wm8750_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  425. int clk_id, unsigned int freq, int dir)
  426. {
  427. struct snd_soc_codec *codec = codec_dai->codec;
  428. struct wm8750_priv *wm8750 = snd_soc_codec_get_drvdata(codec);
  429. switch (freq) {
  430. case 11289600:
  431. case 12000000:
  432. case 12288000:
  433. case 16934400:
  434. case 18432000:
  435. wm8750->sysclk = freq;
  436. return 0;
  437. }
  438. return -EINVAL;
  439. }
  440. static int wm8750_set_dai_fmt(struct snd_soc_dai *codec_dai,
  441. unsigned int fmt)
  442. {
  443. struct snd_soc_codec *codec = codec_dai->codec;
  444. u16 iface = 0;
  445. /* set master/slave audio interface */
  446. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  447. case SND_SOC_DAIFMT_CBM_CFM:
  448. iface = 0x0040;
  449. break;
  450. case SND_SOC_DAIFMT_CBS_CFS:
  451. break;
  452. default:
  453. return -EINVAL;
  454. }
  455. /* interface format */
  456. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  457. case SND_SOC_DAIFMT_I2S:
  458. iface |= 0x0002;
  459. break;
  460. case SND_SOC_DAIFMT_RIGHT_J:
  461. break;
  462. case SND_SOC_DAIFMT_LEFT_J:
  463. iface |= 0x0001;
  464. break;
  465. case SND_SOC_DAIFMT_DSP_A:
  466. iface |= 0x0003;
  467. break;
  468. case SND_SOC_DAIFMT_DSP_B:
  469. iface |= 0x0013;
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. /* clock inversion */
  475. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  476. case SND_SOC_DAIFMT_NB_NF:
  477. break;
  478. case SND_SOC_DAIFMT_IB_IF:
  479. iface |= 0x0090;
  480. break;
  481. case SND_SOC_DAIFMT_IB_NF:
  482. iface |= 0x0080;
  483. break;
  484. case SND_SOC_DAIFMT_NB_IF:
  485. iface |= 0x0010;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. snd_soc_write(codec, WM8750_IFACE, iface);
  491. return 0;
  492. }
  493. static int wm8750_pcm_hw_params(struct snd_pcm_substream *substream,
  494. struct snd_pcm_hw_params *params,
  495. struct snd_soc_dai *dai)
  496. {
  497. struct snd_soc_codec *codec = dai->codec;
  498. struct wm8750_priv *wm8750 = snd_soc_codec_get_drvdata(codec);
  499. u16 iface = snd_soc_read(codec, WM8750_IFACE) & 0x1f3;
  500. u16 srate = snd_soc_read(codec, WM8750_SRATE) & 0x1c0;
  501. int coeff = get_coeff(wm8750->sysclk, params_rate(params));
  502. /* bit size */
  503. switch (params_width(params)) {
  504. case 16:
  505. break;
  506. case 20:
  507. iface |= 0x0004;
  508. break;
  509. case 24:
  510. iface |= 0x0008;
  511. break;
  512. case 32:
  513. iface |= 0x000c;
  514. break;
  515. }
  516. /* set iface & srate */
  517. snd_soc_write(codec, WM8750_IFACE, iface);
  518. if (coeff >= 0)
  519. snd_soc_write(codec, WM8750_SRATE, srate |
  520. (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
  521. return 0;
  522. }
  523. static int wm8750_mute(struct snd_soc_dai *dai, int mute)
  524. {
  525. struct snd_soc_codec *codec = dai->codec;
  526. u16 mute_reg = snd_soc_read(codec, WM8750_ADCDAC) & 0xfff7;
  527. if (mute)
  528. snd_soc_write(codec, WM8750_ADCDAC, mute_reg | 0x8);
  529. else
  530. snd_soc_write(codec, WM8750_ADCDAC, mute_reg);
  531. return 0;
  532. }
  533. static int wm8750_set_bias_level(struct snd_soc_codec *codec,
  534. enum snd_soc_bias_level level)
  535. {
  536. u16 pwr_reg = snd_soc_read(codec, WM8750_PWR1) & 0xfe3e;
  537. switch (level) {
  538. case SND_SOC_BIAS_ON:
  539. /* set vmid to 50k and unmute dac */
  540. snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x00c0);
  541. break;
  542. case SND_SOC_BIAS_PREPARE:
  543. break;
  544. case SND_SOC_BIAS_STANDBY:
  545. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  546. snd_soc_cache_sync(codec);
  547. /* Set VMID to 5k */
  548. snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x01c1);
  549. /* ...and ramp */
  550. msleep(1000);
  551. }
  552. /* mute dac and set vmid to 500k, enable VREF */
  553. snd_soc_write(codec, WM8750_PWR1, pwr_reg | 0x0141);
  554. break;
  555. case SND_SOC_BIAS_OFF:
  556. snd_soc_write(codec, WM8750_PWR1, 0x0001);
  557. break;
  558. }
  559. return 0;
  560. }
  561. #define WM8750_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  562. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
  563. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  564. #define WM8750_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  565. SNDRV_PCM_FMTBIT_S24_LE)
  566. static const struct snd_soc_dai_ops wm8750_dai_ops = {
  567. .hw_params = wm8750_pcm_hw_params,
  568. .digital_mute = wm8750_mute,
  569. .set_fmt = wm8750_set_dai_fmt,
  570. .set_sysclk = wm8750_set_dai_sysclk,
  571. };
  572. static struct snd_soc_dai_driver wm8750_dai = {
  573. .name = "wm8750-hifi",
  574. .playback = {
  575. .stream_name = "Playback",
  576. .channels_min = 1,
  577. .channels_max = 2,
  578. .rates = WM8750_RATES,
  579. .formats = WM8750_FORMATS,},
  580. .capture = {
  581. .stream_name = "Capture",
  582. .channels_min = 1,
  583. .channels_max = 2,
  584. .rates = WM8750_RATES,
  585. .formats = WM8750_FORMATS,},
  586. .ops = &wm8750_dai_ops,
  587. };
  588. static int wm8750_probe(struct snd_soc_codec *codec)
  589. {
  590. int ret;
  591. ret = wm8750_reset(codec);
  592. if (ret < 0) {
  593. printk(KERN_ERR "wm8750: failed to reset: %d\n", ret);
  594. return ret;
  595. }
  596. /* set the update bits */
  597. snd_soc_update_bits(codec, WM8750_LDAC, 0x0100, 0x0100);
  598. snd_soc_update_bits(codec, WM8750_RDAC, 0x0100, 0x0100);
  599. snd_soc_update_bits(codec, WM8750_LOUT1V, 0x0100, 0x0100);
  600. snd_soc_update_bits(codec, WM8750_ROUT1V, 0x0100, 0x0100);
  601. snd_soc_update_bits(codec, WM8750_LOUT2V, 0x0100, 0x0100);
  602. snd_soc_update_bits(codec, WM8750_ROUT2V, 0x0100, 0x0100);
  603. snd_soc_update_bits(codec, WM8750_LINVOL, 0x0100, 0x0100);
  604. snd_soc_update_bits(codec, WM8750_RINVOL, 0x0100, 0x0100);
  605. return ret;
  606. }
  607. static const struct snd_soc_codec_driver soc_codec_dev_wm8750 = {
  608. .probe = wm8750_probe,
  609. .set_bias_level = wm8750_set_bias_level,
  610. .suspend_bias_off = true,
  611. .component_driver = {
  612. .controls = wm8750_snd_controls,
  613. .num_controls = ARRAY_SIZE(wm8750_snd_controls),
  614. .dapm_widgets = wm8750_dapm_widgets,
  615. .num_dapm_widgets = ARRAY_SIZE(wm8750_dapm_widgets),
  616. .dapm_routes = wm8750_dapm_routes,
  617. .num_dapm_routes = ARRAY_SIZE(wm8750_dapm_routes),
  618. },
  619. };
  620. static const struct of_device_id wm8750_of_match[] = {
  621. { .compatible = "wlf,wm8750", },
  622. { .compatible = "wlf,wm8987", },
  623. { }
  624. };
  625. MODULE_DEVICE_TABLE(of, wm8750_of_match);
  626. static const struct regmap_config wm8750_regmap = {
  627. .reg_bits = 7,
  628. .val_bits = 9,
  629. .max_register = WM8750_MOUTV,
  630. .reg_defaults = wm8750_reg_defaults,
  631. .num_reg_defaults = ARRAY_SIZE(wm8750_reg_defaults),
  632. .cache_type = REGCACHE_RBTREE,
  633. };
  634. #if defined(CONFIG_SPI_MASTER)
  635. static int wm8750_spi_probe(struct spi_device *spi)
  636. {
  637. struct wm8750_priv *wm8750;
  638. struct regmap *regmap;
  639. int ret;
  640. wm8750 = devm_kzalloc(&spi->dev, sizeof(struct wm8750_priv),
  641. GFP_KERNEL);
  642. if (wm8750 == NULL)
  643. return -ENOMEM;
  644. regmap = devm_regmap_init_spi(spi, &wm8750_regmap);
  645. if (IS_ERR(regmap))
  646. return PTR_ERR(regmap);
  647. spi_set_drvdata(spi, wm8750);
  648. ret = snd_soc_register_codec(&spi->dev,
  649. &soc_codec_dev_wm8750, &wm8750_dai, 1);
  650. return ret;
  651. }
  652. static int wm8750_spi_remove(struct spi_device *spi)
  653. {
  654. snd_soc_unregister_codec(&spi->dev);
  655. return 0;
  656. }
  657. static const struct spi_device_id wm8750_spi_ids[] = {
  658. { "wm8750", 0 },
  659. { "wm8987", 0 },
  660. { },
  661. };
  662. MODULE_DEVICE_TABLE(spi, wm8750_spi_ids);
  663. static struct spi_driver wm8750_spi_driver = {
  664. .driver = {
  665. .name = "wm8750",
  666. .of_match_table = wm8750_of_match,
  667. },
  668. .id_table = wm8750_spi_ids,
  669. .probe = wm8750_spi_probe,
  670. .remove = wm8750_spi_remove,
  671. };
  672. #endif /* CONFIG_SPI_MASTER */
  673. #if IS_ENABLED(CONFIG_I2C)
  674. static int wm8750_i2c_probe(struct i2c_client *i2c,
  675. const struct i2c_device_id *id)
  676. {
  677. struct wm8750_priv *wm8750;
  678. struct regmap *regmap;
  679. int ret;
  680. wm8750 = devm_kzalloc(&i2c->dev, sizeof(struct wm8750_priv),
  681. GFP_KERNEL);
  682. if (wm8750 == NULL)
  683. return -ENOMEM;
  684. i2c_set_clientdata(i2c, wm8750);
  685. regmap = devm_regmap_init_i2c(i2c, &wm8750_regmap);
  686. if (IS_ERR(regmap))
  687. return PTR_ERR(regmap);
  688. ret = snd_soc_register_codec(&i2c->dev,
  689. &soc_codec_dev_wm8750, &wm8750_dai, 1);
  690. return ret;
  691. }
  692. static int wm8750_i2c_remove(struct i2c_client *client)
  693. {
  694. snd_soc_unregister_codec(&client->dev);
  695. return 0;
  696. }
  697. static const struct i2c_device_id wm8750_i2c_id[] = {
  698. { "wm8750", 0 },
  699. { "wm8987", 0 },
  700. { }
  701. };
  702. MODULE_DEVICE_TABLE(i2c, wm8750_i2c_id);
  703. static struct i2c_driver wm8750_i2c_driver = {
  704. .driver = {
  705. .name = "wm8750",
  706. .of_match_table = wm8750_of_match,
  707. },
  708. .probe = wm8750_i2c_probe,
  709. .remove = wm8750_i2c_remove,
  710. .id_table = wm8750_i2c_id,
  711. };
  712. #endif
  713. static int __init wm8750_modinit(void)
  714. {
  715. int ret = 0;
  716. #if IS_ENABLED(CONFIG_I2C)
  717. ret = i2c_add_driver(&wm8750_i2c_driver);
  718. if (ret != 0) {
  719. printk(KERN_ERR "Failed to register wm8750 I2C driver: %d\n",
  720. ret);
  721. }
  722. #endif
  723. #if defined(CONFIG_SPI_MASTER)
  724. ret = spi_register_driver(&wm8750_spi_driver);
  725. if (ret != 0) {
  726. printk(KERN_ERR "Failed to register wm8750 SPI driver: %d\n",
  727. ret);
  728. }
  729. #endif
  730. return ret;
  731. }
  732. module_init(wm8750_modinit);
  733. static void __exit wm8750_exit(void)
  734. {
  735. #if IS_ENABLED(CONFIG_I2C)
  736. i2c_del_driver(&wm8750_i2c_driver);
  737. #endif
  738. #if defined(CONFIG_SPI_MASTER)
  739. spi_unregister_driver(&wm8750_spi_driver);
  740. #endif
  741. }
  742. module_exit(wm8750_exit);
  743. MODULE_DESCRIPTION("ASoC WM8750 driver");
  744. MODULE_AUTHOR("Liam Girdwood");
  745. MODULE_LICENSE("GPL");