wm8988.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * wm8988.c -- WM8988 ALSA SoC audio driver
  3. *
  4. * Copyright 2009 Wolfson Microelectronics plc
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/platform_device.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/tlv.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include "wm8988.h"
  29. /*
  30. * wm8988 register cache
  31. * We can't read the WM8988 register space when we
  32. * are using 2 wire for device control, so we cache them instead.
  33. */
  34. static const u16 wm8988_reg[] = {
  35. 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
  36. 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
  37. 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
  38. 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
  39. 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
  40. 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
  41. 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
  42. 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
  43. 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
  44. 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
  45. 0x0079, 0x0079, 0x0079, /* 40 */
  46. };
  47. /* codec private data */
  48. struct wm8988_priv {
  49. unsigned int sysclk;
  50. struct snd_soc_codec codec;
  51. struct snd_pcm_hw_constraint_list *sysclk_constraints;
  52. u16 reg_cache[WM8988_NUM_REG];
  53. };
  54. #define wm8988_reset(c) snd_soc_write(c, WM8988_RESET, 0)
  55. /*
  56. * WM8988 Controls
  57. */
  58. static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"};
  59. static const struct soc_enum bass_boost =
  60. SOC_ENUM_SINGLE(WM8988_BASS, 7, 2, bass_boost_txt);
  61. static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
  62. static const struct soc_enum bass_filter =
  63. SOC_ENUM_SINGLE(WM8988_BASS, 6, 2, bass_filter_txt);
  64. static const char *treble_txt[] = {"8kHz", "4kHz"};
  65. static const struct soc_enum treble =
  66. SOC_ENUM_SINGLE(WM8988_TREBLE, 6, 2, treble_txt);
  67. static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"};
  68. static const struct soc_enum stereo_3d_lc =
  69. SOC_ENUM_SINGLE(WM8988_3D, 5, 2, stereo_3d_lc_txt);
  70. static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"};
  71. static const struct soc_enum stereo_3d_uc =
  72. SOC_ENUM_SINGLE(WM8988_3D, 6, 2, stereo_3d_uc_txt);
  73. static const char *stereo_3d_func_txt[] = {"Capture", "Playback"};
  74. static const struct soc_enum stereo_3d_func =
  75. SOC_ENUM_SINGLE(WM8988_3D, 7, 2, stereo_3d_func_txt);
  76. static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
  77. static const struct soc_enum alc_func =
  78. SOC_ENUM_SINGLE(WM8988_ALC1, 7, 4, alc_func_txt);
  79. static const char *ng_type_txt[] = {"Constant PGA Gain",
  80. "Mute ADC Output"};
  81. static const struct soc_enum ng_type =
  82. SOC_ENUM_SINGLE(WM8988_NGATE, 1, 2, ng_type_txt);
  83. static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  84. static const struct soc_enum deemph =
  85. SOC_ENUM_SINGLE(WM8988_ADCDAC, 1, 4, deemph_txt);
  86. static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert",
  87. "L + R Invert"};
  88. static const struct soc_enum adcpol =
  89. SOC_ENUM_SINGLE(WM8988_ADCDAC, 5, 4, adcpol_txt);
  90. static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
  91. static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
  92. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
  93. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  94. static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
  95. static const struct snd_kcontrol_new wm8988_snd_controls[] = {
  96. SOC_ENUM("Bass Boost", bass_boost),
  97. SOC_ENUM("Bass Filter", bass_filter),
  98. SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1),
  99. SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0),
  100. SOC_ENUM("Treble Cut-off", treble),
  101. SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0),
  102. SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0),
  103. SOC_ENUM("3D Lower Cut-off", stereo_3d_lc),
  104. SOC_ENUM("3D Upper Cut-off", stereo_3d_uc),
  105. SOC_ENUM("3D Mode", stereo_3d_func),
  106. SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0),
  107. SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0),
  108. SOC_ENUM("ALC Capture Function", alc_func),
  109. SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0),
  110. SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0),
  111. SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0),
  112. SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0),
  113. SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0),
  114. SOC_ENUM("ALC Capture NG Type", ng_type),
  115. SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0),
  116. SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0),
  117. SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC,
  118. 0, 255, 0, adc_tlv),
  119. SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL,
  120. 0, 63, 0, pga_tlv),
  121. SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0),
  122. SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1),
  123. SOC_ENUM("Playback De-emphasis", deemph),
  124. SOC_ENUM("Capture Polarity", adcpol),
  125. SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0),
  126. SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0),
  127. SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv),
  128. SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1,
  129. bypass_tlv),
  130. SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1,
  131. bypass_tlv),
  132. SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1,
  133. bypass_tlv),
  134. SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1,
  135. bypass_tlv),
  136. SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V,
  137. WM8988_ROUT1V, 7, 1, 0),
  138. SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V,
  139. 0, 127, 0, out_tlv),
  140. SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V,
  141. WM8988_ROUT2V, 7, 1, 0),
  142. SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V,
  143. 0, 127, 0, out_tlv),
  144. };
  145. /*
  146. * DAPM Controls
  147. */
  148. static int wm8988_lrc_control(struct snd_soc_dapm_widget *w,
  149. struct snd_kcontrol *kcontrol, int event)
  150. {
  151. struct snd_soc_codec *codec = w->codec;
  152. u16 adctl2 = snd_soc_read(codec, WM8988_ADCTL2);
  153. /* Use the DAC to gate LRC if active, otherwise use ADC */
  154. if (snd_soc_read(codec, WM8988_PWR2) & 0x180)
  155. adctl2 &= ~0x4;
  156. else
  157. adctl2 |= 0x4;
  158. return snd_soc_write(codec, WM8988_ADCTL2, adctl2);
  159. }
  160. static const char *wm8988_line_texts[] = {
  161. "Line 1", "Line 2", "PGA", "Differential"};
  162. static const unsigned int wm8988_line_values[] = {
  163. 0, 1, 3, 4};
  164. static const struct soc_enum wm8988_lline_enum =
  165. SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7,
  166. ARRAY_SIZE(wm8988_line_texts),
  167. wm8988_line_texts,
  168. wm8988_line_values);
  169. static const struct snd_kcontrol_new wm8988_left_line_controls =
  170. SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
  171. static const struct soc_enum wm8988_rline_enum =
  172. SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7,
  173. ARRAY_SIZE(wm8988_line_texts),
  174. wm8988_line_texts,
  175. wm8988_line_values);
  176. static const struct snd_kcontrol_new wm8988_right_line_controls =
  177. SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
  178. /* Left Mixer */
  179. static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = {
  180. SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0),
  181. SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0),
  182. SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0),
  183. SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0),
  184. };
  185. /* Right Mixer */
  186. static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = {
  187. SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0),
  188. SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0),
  189. SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0),
  190. SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0),
  191. };
  192. static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"};
  193. static const unsigned int wm8988_pga_val[] = { 0, 1, 3 };
  194. /* Left PGA Mux */
  195. static const struct soc_enum wm8988_lpga_enum =
  196. SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3,
  197. ARRAY_SIZE(wm8988_pga_sel),
  198. wm8988_pga_sel,
  199. wm8988_pga_val);
  200. static const struct snd_kcontrol_new wm8988_left_pga_controls =
  201. SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum);
  202. /* Right PGA Mux */
  203. static const struct soc_enum wm8988_rpga_enum =
  204. SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3,
  205. ARRAY_SIZE(wm8988_pga_sel),
  206. wm8988_pga_sel,
  207. wm8988_pga_val);
  208. static const struct snd_kcontrol_new wm8988_right_pga_controls =
  209. SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum);
  210. /* Differential Mux */
  211. static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"};
  212. static const struct soc_enum diffmux =
  213. SOC_ENUM_SINGLE(WM8988_ADCIN, 8, 2, wm8988_diff_sel);
  214. static const struct snd_kcontrol_new wm8988_diffmux_controls =
  215. SOC_DAPM_ENUM("Route", diffmux);
  216. /* Mono ADC Mux */
  217. static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)",
  218. "Mono (Right)", "Digital Mono"};
  219. static const struct soc_enum monomux =
  220. SOC_ENUM_SINGLE(WM8988_ADCIN, 6, 4, wm8988_mono_mux);
  221. static const struct snd_kcontrol_new wm8988_monomux_controls =
  222. SOC_DAPM_ENUM("Route", monomux);
  223. static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = {
  224. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8988_PWR1, 1, 0),
  225. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  226. &wm8988_diffmux_controls),
  227. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  228. &wm8988_monomux_controls),
  229. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  230. &wm8988_monomux_controls),
  231. SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0,
  232. &wm8988_left_pga_controls),
  233. SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0,
  234. &wm8988_right_pga_controls),
  235. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  236. &wm8988_left_line_controls),
  237. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  238. &wm8988_right_line_controls),
  239. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0),
  240. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0),
  241. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0),
  242. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0),
  243. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  244. &wm8988_left_mixer_controls[0],
  245. ARRAY_SIZE(wm8988_left_mixer_controls)),
  246. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  247. &wm8988_right_mixer_controls[0],
  248. ARRAY_SIZE(wm8988_right_mixer_controls)),
  249. SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0),
  250. SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0),
  251. SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0),
  252. SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0),
  253. SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control),
  254. SND_SOC_DAPM_OUTPUT("LOUT1"),
  255. SND_SOC_DAPM_OUTPUT("ROUT1"),
  256. SND_SOC_DAPM_OUTPUT("LOUT2"),
  257. SND_SOC_DAPM_OUTPUT("ROUT2"),
  258. SND_SOC_DAPM_OUTPUT("VREF"),
  259. SND_SOC_DAPM_INPUT("LINPUT1"),
  260. SND_SOC_DAPM_INPUT("LINPUT2"),
  261. SND_SOC_DAPM_INPUT("RINPUT1"),
  262. SND_SOC_DAPM_INPUT("RINPUT2"),
  263. };
  264. static const struct snd_soc_dapm_route audio_map[] = {
  265. { "Left Line Mux", "Line 1", "LINPUT1" },
  266. { "Left Line Mux", "Line 2", "LINPUT2" },
  267. { "Left Line Mux", "PGA", "Left PGA Mux" },
  268. { "Left Line Mux", "Differential", "Differential Mux" },
  269. { "Right Line Mux", "Line 1", "RINPUT1" },
  270. { "Right Line Mux", "Line 2", "RINPUT2" },
  271. { "Right Line Mux", "PGA", "Right PGA Mux" },
  272. { "Right Line Mux", "Differential", "Differential Mux" },
  273. { "Left PGA Mux", "Line 1", "LINPUT1" },
  274. { "Left PGA Mux", "Line 2", "LINPUT2" },
  275. { "Left PGA Mux", "Differential", "Differential Mux" },
  276. { "Right PGA Mux", "Line 1", "RINPUT1" },
  277. { "Right PGA Mux", "Line 2", "RINPUT2" },
  278. { "Right PGA Mux", "Differential", "Differential Mux" },
  279. { "Differential Mux", "Line 1", "LINPUT1" },
  280. { "Differential Mux", "Line 1", "RINPUT1" },
  281. { "Differential Mux", "Line 2", "LINPUT2" },
  282. { "Differential Mux", "Line 2", "RINPUT2" },
  283. { "Left ADC Mux", "Stereo", "Left PGA Mux" },
  284. { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
  285. { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
  286. { "Right ADC Mux", "Stereo", "Right PGA Mux" },
  287. { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
  288. { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
  289. { "Left ADC", NULL, "Left ADC Mux" },
  290. { "Right ADC", NULL, "Right ADC Mux" },
  291. { "Left Line Mux", "Line 1", "LINPUT1" },
  292. { "Left Line Mux", "Line 2", "LINPUT2" },
  293. { "Left Line Mux", "PGA", "Left PGA Mux" },
  294. { "Left Line Mux", "Differential", "Differential Mux" },
  295. { "Right Line Mux", "Line 1", "RINPUT1" },
  296. { "Right Line Mux", "Line 2", "RINPUT2" },
  297. { "Right Line Mux", "PGA", "Right PGA Mux" },
  298. { "Right Line Mux", "Differential", "Differential Mux" },
  299. { "Left Mixer", "Playback Switch", "Left DAC" },
  300. { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
  301. { "Left Mixer", "Right Playback Switch", "Right DAC" },
  302. { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
  303. { "Right Mixer", "Left Playback Switch", "Left DAC" },
  304. { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
  305. { "Right Mixer", "Playback Switch", "Right DAC" },
  306. { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
  307. { "Left Out 1", NULL, "Left Mixer" },
  308. { "LOUT1", NULL, "Left Out 1" },
  309. { "Right Out 1", NULL, "Right Mixer" },
  310. { "ROUT1", NULL, "Right Out 1" },
  311. { "Left Out 2", NULL, "Left Mixer" },
  312. { "LOUT2", NULL, "Left Out 2" },
  313. { "Right Out 2", NULL, "Right Mixer" },
  314. { "ROUT2", NULL, "Right Out 2" },
  315. };
  316. struct _coeff_div {
  317. u32 mclk;
  318. u32 rate;
  319. u16 fs;
  320. u8 sr:5;
  321. u8 usb:1;
  322. };
  323. /* codec hifi mclk clock divider coefficients */
  324. static const struct _coeff_div coeff_div[] = {
  325. /* 8k */
  326. {12288000, 8000, 1536, 0x6, 0x0},
  327. {11289600, 8000, 1408, 0x16, 0x0},
  328. {18432000, 8000, 2304, 0x7, 0x0},
  329. {16934400, 8000, 2112, 0x17, 0x0},
  330. {12000000, 8000, 1500, 0x6, 0x1},
  331. /* 11.025k */
  332. {11289600, 11025, 1024, 0x18, 0x0},
  333. {16934400, 11025, 1536, 0x19, 0x0},
  334. {12000000, 11025, 1088, 0x19, 0x1},
  335. /* 16k */
  336. {12288000, 16000, 768, 0xa, 0x0},
  337. {18432000, 16000, 1152, 0xb, 0x0},
  338. {12000000, 16000, 750, 0xa, 0x1},
  339. /* 22.05k */
  340. {11289600, 22050, 512, 0x1a, 0x0},
  341. {16934400, 22050, 768, 0x1b, 0x0},
  342. {12000000, 22050, 544, 0x1b, 0x1},
  343. /* 32k */
  344. {12288000, 32000, 384, 0xc, 0x0},
  345. {18432000, 32000, 576, 0xd, 0x0},
  346. {12000000, 32000, 375, 0xa, 0x1},
  347. /* 44.1k */
  348. {11289600, 44100, 256, 0x10, 0x0},
  349. {16934400, 44100, 384, 0x11, 0x0},
  350. {12000000, 44100, 272, 0x11, 0x1},
  351. /* 48k */
  352. {12288000, 48000, 256, 0x0, 0x0},
  353. {18432000, 48000, 384, 0x1, 0x0},
  354. {12000000, 48000, 250, 0x0, 0x1},
  355. /* 88.2k */
  356. {11289600, 88200, 128, 0x1e, 0x0},
  357. {16934400, 88200, 192, 0x1f, 0x0},
  358. {12000000, 88200, 136, 0x1f, 0x1},
  359. /* 96k */
  360. {12288000, 96000, 128, 0xe, 0x0},
  361. {18432000, 96000, 192, 0xf, 0x0},
  362. {12000000, 96000, 125, 0xe, 0x1},
  363. };
  364. static inline int get_coeff(int mclk, int rate)
  365. {
  366. int i;
  367. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  368. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  369. return i;
  370. }
  371. return -EINVAL;
  372. }
  373. /* The set of rates we can generate from the above for each SYSCLK */
  374. static unsigned int rates_12288[] = {
  375. 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
  376. };
  377. static struct snd_pcm_hw_constraint_list constraints_12288 = {
  378. .count = ARRAY_SIZE(rates_12288),
  379. .list = rates_12288,
  380. };
  381. static unsigned int rates_112896[] = {
  382. 8000, 11025, 22050, 44100,
  383. };
  384. static struct snd_pcm_hw_constraint_list constraints_112896 = {
  385. .count = ARRAY_SIZE(rates_112896),
  386. .list = rates_112896,
  387. };
  388. static unsigned int rates_12[] = {
  389. 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
  390. 48000, 88235, 96000,
  391. };
  392. static struct snd_pcm_hw_constraint_list constraints_12 = {
  393. .count = ARRAY_SIZE(rates_12),
  394. .list = rates_12,
  395. };
  396. /*
  397. * Note that this should be called from init rather than from hw_params.
  398. */
  399. static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  400. int clk_id, unsigned int freq, int dir)
  401. {
  402. struct snd_soc_codec *codec = codec_dai->codec;
  403. struct wm8988_priv *wm8988 = codec->private_data;
  404. switch (freq) {
  405. case 11289600:
  406. case 18432000:
  407. case 22579200:
  408. case 36864000:
  409. wm8988->sysclk_constraints = &constraints_112896;
  410. wm8988->sysclk = freq;
  411. return 0;
  412. case 12288000:
  413. case 16934400:
  414. case 24576000:
  415. case 33868800:
  416. wm8988->sysclk_constraints = &constraints_12288;
  417. wm8988->sysclk = freq;
  418. return 0;
  419. case 12000000:
  420. case 24000000:
  421. wm8988->sysclk_constraints = &constraints_12;
  422. wm8988->sysclk = freq;
  423. return 0;
  424. }
  425. return -EINVAL;
  426. }
  427. static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai,
  428. unsigned int fmt)
  429. {
  430. struct snd_soc_codec *codec = codec_dai->codec;
  431. u16 iface = 0;
  432. /* set master/slave audio interface */
  433. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  434. case SND_SOC_DAIFMT_CBM_CFM:
  435. iface = 0x0040;
  436. break;
  437. case SND_SOC_DAIFMT_CBS_CFS:
  438. break;
  439. default:
  440. return -EINVAL;
  441. }
  442. /* interface format */
  443. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  444. case SND_SOC_DAIFMT_I2S:
  445. iface |= 0x0002;
  446. break;
  447. case SND_SOC_DAIFMT_RIGHT_J:
  448. break;
  449. case SND_SOC_DAIFMT_LEFT_J:
  450. iface |= 0x0001;
  451. break;
  452. case SND_SOC_DAIFMT_DSP_A:
  453. iface |= 0x0003;
  454. break;
  455. case SND_SOC_DAIFMT_DSP_B:
  456. iface |= 0x0013;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. /* clock inversion */
  462. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  463. case SND_SOC_DAIFMT_NB_NF:
  464. break;
  465. case SND_SOC_DAIFMT_IB_IF:
  466. iface |= 0x0090;
  467. break;
  468. case SND_SOC_DAIFMT_IB_NF:
  469. iface |= 0x0080;
  470. break;
  471. case SND_SOC_DAIFMT_NB_IF:
  472. iface |= 0x0010;
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. snd_soc_write(codec, WM8988_IFACE, iface);
  478. return 0;
  479. }
  480. static int wm8988_pcm_startup(struct snd_pcm_substream *substream,
  481. struct snd_soc_dai *dai)
  482. {
  483. struct snd_soc_codec *codec = dai->codec;
  484. struct wm8988_priv *wm8988 = codec->private_data;
  485. /* The set of sample rates that can be supported depends on the
  486. * MCLK supplied to the CODEC - enforce this.
  487. */
  488. if (!wm8988->sysclk) {
  489. dev_err(codec->dev,
  490. "No MCLK configured, call set_sysclk() on init\n");
  491. return -EINVAL;
  492. }
  493. snd_pcm_hw_constraint_list(substream->runtime, 0,
  494. SNDRV_PCM_HW_PARAM_RATE,
  495. wm8988->sysclk_constraints);
  496. return 0;
  497. }
  498. static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream,
  499. struct snd_pcm_hw_params *params,
  500. struct snd_soc_dai *dai)
  501. {
  502. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  503. struct snd_soc_device *socdev = rtd->socdev;
  504. struct snd_soc_codec *codec = socdev->card->codec;
  505. struct wm8988_priv *wm8988 = codec->private_data;
  506. u16 iface = snd_soc_read(codec, WM8988_IFACE) & 0x1f3;
  507. u16 srate = snd_soc_read(codec, WM8988_SRATE) & 0x180;
  508. int coeff;
  509. coeff = get_coeff(wm8988->sysclk, params_rate(params));
  510. if (coeff < 0) {
  511. coeff = get_coeff(wm8988->sysclk / 2, params_rate(params));
  512. srate |= 0x40;
  513. }
  514. if (coeff < 0) {
  515. dev_err(codec->dev,
  516. "Unable to configure sample rate %dHz with %dHz MCLK\n",
  517. params_rate(params), wm8988->sysclk);
  518. return coeff;
  519. }
  520. /* bit size */
  521. switch (params_format(params)) {
  522. case SNDRV_PCM_FORMAT_S16_LE:
  523. break;
  524. case SNDRV_PCM_FORMAT_S20_3LE:
  525. iface |= 0x0004;
  526. break;
  527. case SNDRV_PCM_FORMAT_S24_LE:
  528. iface |= 0x0008;
  529. break;
  530. case SNDRV_PCM_FORMAT_S32_LE:
  531. iface |= 0x000c;
  532. break;
  533. }
  534. /* set iface & srate */
  535. snd_soc_write(codec, WM8988_IFACE, iface);
  536. if (coeff >= 0)
  537. snd_soc_write(codec, WM8988_SRATE, srate |
  538. (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
  539. return 0;
  540. }
  541. static int wm8988_mute(struct snd_soc_dai *dai, int mute)
  542. {
  543. struct snd_soc_codec *codec = dai->codec;
  544. u16 mute_reg = snd_soc_read(codec, WM8988_ADCDAC) & 0xfff7;
  545. if (mute)
  546. snd_soc_write(codec, WM8988_ADCDAC, mute_reg | 0x8);
  547. else
  548. snd_soc_write(codec, WM8988_ADCDAC, mute_reg);
  549. return 0;
  550. }
  551. static int wm8988_set_bias_level(struct snd_soc_codec *codec,
  552. enum snd_soc_bias_level level)
  553. {
  554. u16 pwr_reg = snd_soc_read(codec, WM8988_PWR1) & ~0x1c1;
  555. switch (level) {
  556. case SND_SOC_BIAS_ON:
  557. break;
  558. case SND_SOC_BIAS_PREPARE:
  559. /* VREF, VMID=2x50k, digital enabled */
  560. snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x00c0);
  561. break;
  562. case SND_SOC_BIAS_STANDBY:
  563. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  564. /* VREF, VMID=2x5k */
  565. snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x1c1);
  566. /* Charge caps */
  567. msleep(100);
  568. }
  569. /* VREF, VMID=2*500k, digital stopped */
  570. snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x0141);
  571. break;
  572. case SND_SOC_BIAS_OFF:
  573. snd_soc_write(codec, WM8988_PWR1, 0x0000);
  574. break;
  575. }
  576. codec->bias_level = level;
  577. return 0;
  578. }
  579. #define WM8988_RATES SNDRV_PCM_RATE_8000_96000
  580. #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  581. SNDRV_PCM_FMTBIT_S24_LE)
  582. static struct snd_soc_dai_ops wm8988_ops = {
  583. .startup = wm8988_pcm_startup,
  584. .hw_params = wm8988_pcm_hw_params,
  585. .set_fmt = wm8988_set_dai_fmt,
  586. .set_sysclk = wm8988_set_dai_sysclk,
  587. .digital_mute = wm8988_mute,
  588. };
  589. struct snd_soc_dai wm8988_dai = {
  590. .name = "WM8988",
  591. .playback = {
  592. .stream_name = "Playback",
  593. .channels_min = 1,
  594. .channels_max = 2,
  595. .rates = WM8988_RATES,
  596. .formats = WM8988_FORMATS,
  597. },
  598. .capture = {
  599. .stream_name = "Capture",
  600. .channels_min = 1,
  601. .channels_max = 2,
  602. .rates = WM8988_RATES,
  603. .formats = WM8988_FORMATS,
  604. },
  605. .ops = &wm8988_ops,
  606. .symmetric_rates = 1,
  607. };
  608. EXPORT_SYMBOL_GPL(wm8988_dai);
  609. static int wm8988_suspend(struct platform_device *pdev, pm_message_t state)
  610. {
  611. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  612. struct snd_soc_codec *codec = socdev->card->codec;
  613. wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF);
  614. return 0;
  615. }
  616. static int wm8988_resume(struct platform_device *pdev)
  617. {
  618. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  619. struct snd_soc_codec *codec = socdev->card->codec;
  620. int i;
  621. u8 data[2];
  622. u16 *cache = codec->reg_cache;
  623. /* Sync reg_cache with the hardware */
  624. for (i = 0; i < WM8988_NUM_REG; i++) {
  625. if (i == WM8988_RESET)
  626. continue;
  627. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  628. data[1] = cache[i] & 0x00ff;
  629. codec->hw_write(codec->control_data, data, 2);
  630. }
  631. wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  632. return 0;
  633. }
  634. static struct snd_soc_codec *wm8988_codec;
  635. static int wm8988_probe(struct platform_device *pdev)
  636. {
  637. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  638. struct snd_soc_codec *codec;
  639. int ret = 0;
  640. if (wm8988_codec == NULL) {
  641. dev_err(&pdev->dev, "Codec device not registered\n");
  642. return -ENODEV;
  643. }
  644. socdev->card->codec = wm8988_codec;
  645. codec = wm8988_codec;
  646. /* register pcms */
  647. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  648. if (ret < 0) {
  649. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  650. goto pcm_err;
  651. }
  652. snd_soc_add_controls(codec, wm8988_snd_controls,
  653. ARRAY_SIZE(wm8988_snd_controls));
  654. snd_soc_dapm_new_controls(codec, wm8988_dapm_widgets,
  655. ARRAY_SIZE(wm8988_dapm_widgets));
  656. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  657. return ret;
  658. pcm_err:
  659. return ret;
  660. }
  661. static int wm8988_remove(struct platform_device *pdev)
  662. {
  663. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  664. snd_soc_free_pcms(socdev);
  665. snd_soc_dapm_free(socdev);
  666. return 0;
  667. }
  668. struct snd_soc_codec_device soc_codec_dev_wm8988 = {
  669. .probe = wm8988_probe,
  670. .remove = wm8988_remove,
  671. .suspend = wm8988_suspend,
  672. .resume = wm8988_resume,
  673. };
  674. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8988);
  675. static int wm8988_register(struct wm8988_priv *wm8988,
  676. enum snd_soc_control_type control)
  677. {
  678. struct snd_soc_codec *codec = &wm8988->codec;
  679. int ret;
  680. u16 reg;
  681. if (wm8988_codec) {
  682. dev_err(codec->dev, "Another WM8988 is registered\n");
  683. ret = -EINVAL;
  684. goto err;
  685. }
  686. mutex_init(&codec->mutex);
  687. INIT_LIST_HEAD(&codec->dapm_widgets);
  688. INIT_LIST_HEAD(&codec->dapm_paths);
  689. codec->private_data = wm8988;
  690. codec->name = "WM8988";
  691. codec->owner = THIS_MODULE;
  692. codec->dai = &wm8988_dai;
  693. codec->num_dai = 1;
  694. codec->reg_cache_size = ARRAY_SIZE(wm8988->reg_cache);
  695. codec->reg_cache = &wm8988->reg_cache;
  696. codec->bias_level = SND_SOC_BIAS_OFF;
  697. codec->set_bias_level = wm8988_set_bias_level;
  698. memcpy(codec->reg_cache, wm8988_reg,
  699. sizeof(wm8988_reg));
  700. ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
  701. if (ret < 0) {
  702. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  703. goto err;
  704. }
  705. ret = wm8988_reset(codec);
  706. if (ret < 0) {
  707. dev_err(codec->dev, "Failed to issue reset\n");
  708. goto err;
  709. }
  710. /* set the update bits (we always update left then right) */
  711. reg = snd_soc_read(codec, WM8988_RADC);
  712. snd_soc_write(codec, WM8988_RADC, reg | 0x100);
  713. reg = snd_soc_read(codec, WM8988_RDAC);
  714. snd_soc_write(codec, WM8988_RDAC, reg | 0x0100);
  715. reg = snd_soc_read(codec, WM8988_ROUT1V);
  716. snd_soc_write(codec, WM8988_ROUT1V, reg | 0x0100);
  717. reg = snd_soc_read(codec, WM8988_ROUT2V);
  718. snd_soc_write(codec, WM8988_ROUT2V, reg | 0x0100);
  719. reg = snd_soc_read(codec, WM8988_RINVOL);
  720. snd_soc_write(codec, WM8988_RINVOL, reg | 0x0100);
  721. wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_STANDBY);
  722. wm8988_dai.dev = codec->dev;
  723. wm8988_codec = codec;
  724. ret = snd_soc_register_codec(codec);
  725. if (ret != 0) {
  726. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  727. goto err;
  728. }
  729. ret = snd_soc_register_dai(&wm8988_dai);
  730. if (ret != 0) {
  731. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  732. snd_soc_unregister_codec(codec);
  733. goto err_codec;
  734. }
  735. return 0;
  736. err_codec:
  737. snd_soc_unregister_codec(codec);
  738. err:
  739. kfree(wm8988);
  740. return ret;
  741. }
  742. static void wm8988_unregister(struct wm8988_priv *wm8988)
  743. {
  744. wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_OFF);
  745. snd_soc_unregister_dai(&wm8988_dai);
  746. snd_soc_unregister_codec(&wm8988->codec);
  747. kfree(wm8988);
  748. wm8988_codec = NULL;
  749. }
  750. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  751. static int wm8988_i2c_probe(struct i2c_client *i2c,
  752. const struct i2c_device_id *id)
  753. {
  754. struct wm8988_priv *wm8988;
  755. struct snd_soc_codec *codec;
  756. wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
  757. if (wm8988 == NULL)
  758. return -ENOMEM;
  759. codec = &wm8988->codec;
  760. i2c_set_clientdata(i2c, wm8988);
  761. codec->control_data = i2c;
  762. codec->dev = &i2c->dev;
  763. return wm8988_register(wm8988, SND_SOC_I2C);
  764. }
  765. static int wm8988_i2c_remove(struct i2c_client *client)
  766. {
  767. struct wm8988_priv *wm8988 = i2c_get_clientdata(client);
  768. wm8988_unregister(wm8988);
  769. return 0;
  770. }
  771. static const struct i2c_device_id wm8988_i2c_id[] = {
  772. { "wm8988", 0 },
  773. { }
  774. };
  775. MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id);
  776. static struct i2c_driver wm8988_i2c_driver = {
  777. .driver = {
  778. .name = "WM8988",
  779. .owner = THIS_MODULE,
  780. },
  781. .probe = wm8988_i2c_probe,
  782. .remove = wm8988_i2c_remove,
  783. .id_table = wm8988_i2c_id,
  784. };
  785. #endif
  786. #if defined(CONFIG_SPI_MASTER)
  787. static int __devinit wm8988_spi_probe(struct spi_device *spi)
  788. {
  789. struct wm8988_priv *wm8988;
  790. struct snd_soc_codec *codec;
  791. wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
  792. if (wm8988 == NULL)
  793. return -ENOMEM;
  794. codec = &wm8988->codec;
  795. codec->control_data = spi;
  796. codec->dev = &spi->dev;
  797. dev_set_drvdata(&spi->dev, wm8988);
  798. return wm8988_register(wm8988, SND_SOC_SPI);
  799. }
  800. static int __devexit wm8988_spi_remove(struct spi_device *spi)
  801. {
  802. struct wm8988_priv *wm8988 = dev_get_drvdata(&spi->dev);
  803. wm8988_unregister(wm8988);
  804. return 0;
  805. }
  806. static struct spi_driver wm8988_spi_driver = {
  807. .driver = {
  808. .name = "wm8988",
  809. .bus = &spi_bus_type,
  810. .owner = THIS_MODULE,
  811. },
  812. .probe = wm8988_spi_probe,
  813. .remove = __devexit_p(wm8988_spi_remove),
  814. };
  815. #endif
  816. static int __init wm8988_modinit(void)
  817. {
  818. int ret;
  819. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  820. ret = i2c_add_driver(&wm8988_i2c_driver);
  821. if (ret != 0)
  822. pr_err("WM8988: Unable to register I2C driver: %d\n", ret);
  823. #endif
  824. #if defined(CONFIG_SPI_MASTER)
  825. ret = spi_register_driver(&wm8988_spi_driver);
  826. if (ret != 0)
  827. pr_err("WM8988: Unable to register SPI driver: %d\n", ret);
  828. #endif
  829. return ret;
  830. }
  831. module_init(wm8988_modinit);
  832. static void __exit wm8988_exit(void)
  833. {
  834. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  835. i2c_del_driver(&wm8988_i2c_driver);
  836. #endif
  837. #if defined(CONFIG_SPI_MASTER)
  838. spi_unregister_driver(&wm8988_spi_driver);
  839. #endif
  840. }
  841. module_exit(wm8988_exit);
  842. MODULE_DESCRIPTION("ASoC WM8988 driver");
  843. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  844. MODULE_LICENSE("GPL");