wm8750.c 25 KB

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