wm8961.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * wm8961.c -- WM8961 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009-10 Wolfson Microelectronics, plc
  5. *
  6. * Author: Mark Brown
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Currently unimplemented features:
  13. * - ALC
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/pm.h>
  20. #include <linux/i2c.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <sound/tlv.h>
  29. #include "wm8961.h"
  30. #define WM8961_MAX_REGISTER 0xFC
  31. static const struct reg_default wm8961_reg_defaults[] = {
  32. { 0, 0x009F }, /* R0 - Left Input volume */
  33. { 1, 0x009F }, /* R1 - Right Input volume */
  34. { 2, 0x0000 }, /* R2 - LOUT1 volume */
  35. { 3, 0x0000 }, /* R3 - ROUT1 volume */
  36. { 4, 0x0020 }, /* R4 - Clocking1 */
  37. { 5, 0x0008 }, /* R5 - ADC & DAC Control 1 */
  38. { 6, 0x0000 }, /* R6 - ADC & DAC Control 2 */
  39. { 7, 0x000A }, /* R7 - Audio Interface 0 */
  40. { 8, 0x01F4 }, /* R8 - Clocking2 */
  41. { 9, 0x0000 }, /* R9 - Audio Interface 1 */
  42. { 10, 0x00FF }, /* R10 - Left DAC volume */
  43. { 11, 0x00FF }, /* R11 - Right DAC volume */
  44. { 14, 0x0040 }, /* R14 - Audio Interface 2 */
  45. { 17, 0x007B }, /* R17 - ALC1 */
  46. { 18, 0x0000 }, /* R18 - ALC2 */
  47. { 19, 0x0032 }, /* R19 - ALC3 */
  48. { 20, 0x0000 }, /* R20 - Noise Gate */
  49. { 21, 0x00C0 }, /* R21 - Left ADC volume */
  50. { 22, 0x00C0 }, /* R22 - Right ADC volume */
  51. { 23, 0x0120 }, /* R23 - Additional control(1) */
  52. { 24, 0x0000 }, /* R24 - Additional control(2) */
  53. { 25, 0x0000 }, /* R25 - Pwr Mgmt (1) */
  54. { 26, 0x0000 }, /* R26 - Pwr Mgmt (2) */
  55. { 27, 0x0000 }, /* R27 - Additional Control (3) */
  56. { 28, 0x0000 }, /* R28 - Anti-pop */
  57. { 30, 0x005F }, /* R30 - Clocking 3 */
  58. { 32, 0x0000 }, /* R32 - ADCL signal path */
  59. { 33, 0x0000 }, /* R33 - ADCR signal path */
  60. { 40, 0x0000 }, /* R40 - LOUT2 volume */
  61. { 41, 0x0000 }, /* R41 - ROUT2 volume */
  62. { 47, 0x0000 }, /* R47 - Pwr Mgmt (3) */
  63. { 48, 0x0023 }, /* R48 - Additional Control (4) */
  64. { 49, 0x0000 }, /* R49 - Class D Control 1 */
  65. { 51, 0x0003 }, /* R51 - Class D Control 2 */
  66. { 56, 0x0106 }, /* R56 - Clocking 4 */
  67. { 57, 0x0000 }, /* R57 - DSP Sidetone 0 */
  68. { 58, 0x0000 }, /* R58 - DSP Sidetone 1 */
  69. { 60, 0x0000 }, /* R60 - DC Servo 0 */
  70. { 61, 0x0000 }, /* R61 - DC Servo 1 */
  71. { 63, 0x015E }, /* R63 - DC Servo 3 */
  72. { 65, 0x0010 }, /* R65 - DC Servo 5 */
  73. { 68, 0x0003 }, /* R68 - Analogue PGA Bias */
  74. { 69, 0x0000 }, /* R69 - Analogue HP 0 */
  75. { 71, 0x01FB }, /* R71 - Analogue HP 2 */
  76. { 72, 0x0000 }, /* R72 - Charge Pump 1 */
  77. { 82, 0x0000 }, /* R82 - Charge Pump B */
  78. { 87, 0x0000 }, /* R87 - Write Sequencer 1 */
  79. { 88, 0x0000 }, /* R88 - Write Sequencer 2 */
  80. { 89, 0x0000 }, /* R89 - Write Sequencer 3 */
  81. { 90, 0x0000 }, /* R90 - Write Sequencer 4 */
  82. { 91, 0x0000 }, /* R91 - Write Sequencer 5 */
  83. { 92, 0x0000 }, /* R92 - Write Sequencer 6 */
  84. { 93, 0x0000 }, /* R93 - Write Sequencer 7 */
  85. { 252, 0x0001 }, /* R252 - General test 1 */
  86. };
  87. struct wm8961_priv {
  88. struct regmap *regmap;
  89. int sysclk;
  90. };
  91. static bool wm8961_volatile(struct device *dev, unsigned int reg)
  92. {
  93. switch (reg) {
  94. case WM8961_SOFTWARE_RESET:
  95. case WM8961_WRITE_SEQUENCER_7:
  96. case WM8961_DC_SERVO_1:
  97. return true;
  98. default:
  99. return false;
  100. }
  101. }
  102. static bool wm8961_readable(struct device *dev, unsigned int reg)
  103. {
  104. switch (reg) {
  105. case WM8961_LEFT_INPUT_VOLUME:
  106. case WM8961_RIGHT_INPUT_VOLUME:
  107. case WM8961_LOUT1_VOLUME:
  108. case WM8961_ROUT1_VOLUME:
  109. case WM8961_CLOCKING1:
  110. case WM8961_ADC_DAC_CONTROL_1:
  111. case WM8961_ADC_DAC_CONTROL_2:
  112. case WM8961_AUDIO_INTERFACE_0:
  113. case WM8961_CLOCKING2:
  114. case WM8961_AUDIO_INTERFACE_1:
  115. case WM8961_LEFT_DAC_VOLUME:
  116. case WM8961_RIGHT_DAC_VOLUME:
  117. case WM8961_AUDIO_INTERFACE_2:
  118. case WM8961_SOFTWARE_RESET:
  119. case WM8961_ALC1:
  120. case WM8961_ALC2:
  121. case WM8961_ALC3:
  122. case WM8961_NOISE_GATE:
  123. case WM8961_LEFT_ADC_VOLUME:
  124. case WM8961_RIGHT_ADC_VOLUME:
  125. case WM8961_ADDITIONAL_CONTROL_1:
  126. case WM8961_ADDITIONAL_CONTROL_2:
  127. case WM8961_PWR_MGMT_1:
  128. case WM8961_PWR_MGMT_2:
  129. case WM8961_ADDITIONAL_CONTROL_3:
  130. case WM8961_ANTI_POP:
  131. case WM8961_CLOCKING_3:
  132. case WM8961_ADCL_SIGNAL_PATH:
  133. case WM8961_ADCR_SIGNAL_PATH:
  134. case WM8961_LOUT2_VOLUME:
  135. case WM8961_ROUT2_VOLUME:
  136. case WM8961_PWR_MGMT_3:
  137. case WM8961_ADDITIONAL_CONTROL_4:
  138. case WM8961_CLASS_D_CONTROL_1:
  139. case WM8961_CLASS_D_CONTROL_2:
  140. case WM8961_CLOCKING_4:
  141. case WM8961_DSP_SIDETONE_0:
  142. case WM8961_DSP_SIDETONE_1:
  143. case WM8961_DC_SERVO_0:
  144. case WM8961_DC_SERVO_1:
  145. case WM8961_DC_SERVO_3:
  146. case WM8961_DC_SERVO_5:
  147. case WM8961_ANALOGUE_PGA_BIAS:
  148. case WM8961_ANALOGUE_HP_0:
  149. case WM8961_ANALOGUE_HP_2:
  150. case WM8961_CHARGE_PUMP_1:
  151. case WM8961_CHARGE_PUMP_B:
  152. case WM8961_WRITE_SEQUENCER_1:
  153. case WM8961_WRITE_SEQUENCER_2:
  154. case WM8961_WRITE_SEQUENCER_3:
  155. case WM8961_WRITE_SEQUENCER_4:
  156. case WM8961_WRITE_SEQUENCER_5:
  157. case WM8961_WRITE_SEQUENCER_6:
  158. case WM8961_WRITE_SEQUENCER_7:
  159. case WM8961_GENERAL_TEST_1:
  160. return true;
  161. default:
  162. return false;
  163. }
  164. }
  165. /*
  166. * The headphone output supports special anti-pop sequences giving
  167. * silent power up and power down.
  168. */
  169. static int wm8961_hp_event(struct snd_soc_dapm_widget *w,
  170. struct snd_kcontrol *kcontrol, int event)
  171. {
  172. struct snd_soc_codec *codec = w->codec;
  173. u16 hp_reg = snd_soc_read(codec, WM8961_ANALOGUE_HP_0);
  174. u16 cp_reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_1);
  175. u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
  176. u16 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
  177. int timeout = 500;
  178. if (event & SND_SOC_DAPM_POST_PMU) {
  179. /* Make sure the output is shorted */
  180. hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
  181. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  182. /* Enable the charge pump */
  183. cp_reg |= WM8961_CP_ENA;
  184. snd_soc_write(codec, WM8961_CHARGE_PUMP_1, cp_reg);
  185. mdelay(5);
  186. /* Enable the PGA */
  187. pwr_reg |= WM8961_LOUT1_PGA | WM8961_ROUT1_PGA;
  188. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  189. /* Enable the amplifier */
  190. hp_reg |= WM8961_HPR_ENA | WM8961_HPL_ENA;
  191. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  192. /* Second stage enable */
  193. hp_reg |= WM8961_HPR_ENA_DLY | WM8961_HPL_ENA_DLY;
  194. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  195. /* Enable the DC servo & trigger startup */
  196. dcs_reg |=
  197. WM8961_DCS_ENA_CHAN_HPR | WM8961_DCS_TRIG_STARTUP_HPR |
  198. WM8961_DCS_ENA_CHAN_HPL | WM8961_DCS_TRIG_STARTUP_HPL;
  199. dev_dbg(codec->dev, "Enabling DC servo\n");
  200. snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
  201. do {
  202. msleep(1);
  203. dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
  204. } while (--timeout &&
  205. dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
  206. WM8961_DCS_TRIG_STARTUP_HPL));
  207. if (dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
  208. WM8961_DCS_TRIG_STARTUP_HPL))
  209. dev_err(codec->dev, "DC servo timed out\n");
  210. else
  211. dev_dbg(codec->dev, "DC servo startup complete\n");
  212. /* Enable the output stage */
  213. hp_reg |= WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP;
  214. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  215. /* Remove the short on the output stage */
  216. hp_reg |= WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT;
  217. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  218. }
  219. if (event & SND_SOC_DAPM_PRE_PMD) {
  220. /* Short the output */
  221. hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
  222. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  223. /* Disable the output stage */
  224. hp_reg &= ~(WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP);
  225. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  226. /* Disable DC offset cancellation */
  227. dcs_reg &= ~(WM8961_DCS_ENA_CHAN_HPR |
  228. WM8961_DCS_ENA_CHAN_HPL);
  229. snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
  230. /* Finish up */
  231. hp_reg &= ~(WM8961_HPR_ENA_DLY | WM8961_HPR_ENA |
  232. WM8961_HPL_ENA_DLY | WM8961_HPL_ENA);
  233. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  234. /* Disable the PGA */
  235. pwr_reg &= ~(WM8961_LOUT1_PGA | WM8961_ROUT1_PGA);
  236. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  237. /* Disable the charge pump */
  238. dev_dbg(codec->dev, "Disabling charge pump\n");
  239. snd_soc_write(codec, WM8961_CHARGE_PUMP_1,
  240. cp_reg & ~WM8961_CP_ENA);
  241. }
  242. return 0;
  243. }
  244. static int wm8961_spk_event(struct snd_soc_dapm_widget *w,
  245. struct snd_kcontrol *kcontrol, int event)
  246. {
  247. struct snd_soc_codec *codec = w->codec;
  248. u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
  249. u16 spk_reg = snd_soc_read(codec, WM8961_CLASS_D_CONTROL_1);
  250. if (event & SND_SOC_DAPM_POST_PMU) {
  251. /* Enable the PGA */
  252. pwr_reg |= WM8961_SPKL_PGA | WM8961_SPKR_PGA;
  253. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  254. /* Enable the amplifier */
  255. spk_reg |= WM8961_SPKL_ENA | WM8961_SPKR_ENA;
  256. snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
  257. }
  258. if (event & SND_SOC_DAPM_PRE_PMD) {
  259. /* Disable the amplifier */
  260. spk_reg &= ~(WM8961_SPKL_ENA | WM8961_SPKR_ENA);
  261. snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
  262. /* Disable the PGA */
  263. pwr_reg &= ~(WM8961_SPKL_PGA | WM8961_SPKR_PGA);
  264. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  265. }
  266. return 0;
  267. }
  268. static const char *adc_hpf_text[] = {
  269. "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
  270. };
  271. static SOC_ENUM_SINGLE_DECL(adc_hpf,
  272. WM8961_ADC_DAC_CONTROL_2, 7, adc_hpf_text);
  273. static const char *dac_deemph_text[] = {
  274. "None", "32kHz", "44.1kHz", "48kHz",
  275. };
  276. static SOC_ENUM_SINGLE_DECL(dac_deemph,
  277. WM8961_ADC_DAC_CONTROL_1, 1, dac_deemph_text);
  278. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  279. static const DECLARE_TLV_DB_SCALE(hp_sec_tlv, -700, 100, 0);
  280. static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
  281. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  282. static unsigned int boost_tlv[] = {
  283. TLV_DB_RANGE_HEAD(4),
  284. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  285. 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
  286. 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
  287. 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0),
  288. };
  289. static const DECLARE_TLV_DB_SCALE(pga_tlv, -2325, 75, 0);
  290. static const struct snd_kcontrol_new wm8961_snd_controls[] = {
  291. SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
  292. 0, 127, 0, out_tlv),
  293. SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2,
  294. 6, 3, 7, 0, hp_sec_tlv),
  295. SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
  296. 7, 1, 0),
  297. SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
  298. 0, 127, 0, out_tlv),
  299. SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
  300. 7, 1, 0),
  301. SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2, 0, 7, 0),
  302. SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2, 0, 1, 0),
  303. SOC_ENUM("DAC Deemphasis", dac_deemph),
  304. SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2, 3, 1, 0),
  305. SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0,
  306. WM8961_DSP_SIDETONE_1, 4, 12, 0, sidetone_tlv),
  307. SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1, 0, 1, 0),
  308. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  309. SOC_DOUBLE_R_TLV("Capture Volume",
  310. WM8961_LEFT_ADC_VOLUME, WM8961_RIGHT_ADC_VOLUME,
  311. 1, 119, 0, adc_tlv),
  312. SOC_DOUBLE_R_TLV("Capture Boost Volume",
  313. WM8961_ADCL_SIGNAL_PATH, WM8961_ADCR_SIGNAL_PATH,
  314. 4, 3, 0, boost_tlv),
  315. SOC_DOUBLE_R_TLV("Capture PGA Volume",
  316. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  317. 0, 62, 0, pga_tlv),
  318. SOC_DOUBLE_R("Capture PGA ZC Switch",
  319. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  320. 6, 1, 1),
  321. SOC_DOUBLE_R("Capture PGA Switch",
  322. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  323. 7, 1, 1),
  324. };
  325. static const char *sidetone_text[] = {
  326. "None", "Left", "Right"
  327. };
  328. static SOC_ENUM_SINGLE_DECL(dacl_sidetone,
  329. WM8961_DSP_SIDETONE_0, 2, sidetone_text);
  330. static SOC_ENUM_SINGLE_DECL(dacr_sidetone,
  331. WM8961_DSP_SIDETONE_1, 2, sidetone_text);
  332. static const struct snd_kcontrol_new dacl_mux =
  333. SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone);
  334. static const struct snd_kcontrol_new dacr_mux =
  335. SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone);
  336. static const struct snd_soc_dapm_widget wm8961_dapm_widgets[] = {
  337. SND_SOC_DAPM_INPUT("LINPUT"),
  338. SND_SOC_DAPM_INPUT("RINPUT"),
  339. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2, 4, 0, NULL, 0),
  340. SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1, 5, 0, NULL, 0),
  341. SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1, 4, 0, NULL, 0),
  342. SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1, 3, 0),
  343. SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1, 2, 0),
  344. SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1, 1, 0, NULL, 0),
  345. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &dacl_mux),
  346. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &dacr_mux),
  347. SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2, 8, 0),
  348. SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2, 7, 0),
  349. /* Handle as a mono path for DCS */
  350. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM,
  351. 4, 0, NULL, 0, wm8961_hp_event,
  352. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  353. SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM,
  354. 4, 0, NULL, 0, wm8961_spk_event,
  355. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  356. SND_SOC_DAPM_OUTPUT("HP_L"),
  357. SND_SOC_DAPM_OUTPUT("HP_R"),
  358. SND_SOC_DAPM_OUTPUT("SPK_LN"),
  359. SND_SOC_DAPM_OUTPUT("SPK_LP"),
  360. SND_SOC_DAPM_OUTPUT("SPK_RN"),
  361. SND_SOC_DAPM_OUTPUT("SPK_RP"),
  362. };
  363. static const struct snd_soc_dapm_route audio_paths[] = {
  364. { "DACL", NULL, "CLK_DSP" },
  365. { "DACL", NULL, "DACL Sidetone" },
  366. { "DACR", NULL, "CLK_DSP" },
  367. { "DACR", NULL, "DACR Sidetone" },
  368. { "DACL Sidetone", "Left", "ADCL" },
  369. { "DACL Sidetone", "Right", "ADCR" },
  370. { "DACR Sidetone", "Left", "ADCL" },
  371. { "DACR Sidetone", "Right", "ADCR" },
  372. { "HP_L", NULL, "Headphone Output" },
  373. { "HP_R", NULL, "Headphone Output" },
  374. { "Headphone Output", NULL, "DACL" },
  375. { "Headphone Output", NULL, "DACR" },
  376. { "SPK_LN", NULL, "Speaker Output" },
  377. { "SPK_LP", NULL, "Speaker Output" },
  378. { "SPK_RN", NULL, "Speaker Output" },
  379. { "SPK_RP", NULL, "Speaker Output" },
  380. { "Speaker Output", NULL, "DACL" },
  381. { "Speaker Output", NULL, "DACR" },
  382. { "ADCL", NULL, "Left Input" },
  383. { "ADCL", NULL, "CLK_DSP" },
  384. { "ADCR", NULL, "Right Input" },
  385. { "ADCR", NULL, "CLK_DSP" },
  386. { "Left Input", NULL, "LINPUT" },
  387. { "Right Input", NULL, "RINPUT" },
  388. };
  389. /* Values for CLK_SYS_RATE */
  390. static struct {
  391. int ratio;
  392. u16 val;
  393. } wm8961_clk_sys_ratio[] = {
  394. { 64, 0 },
  395. { 128, 1 },
  396. { 192, 2 },
  397. { 256, 3 },
  398. { 384, 4 },
  399. { 512, 5 },
  400. { 768, 6 },
  401. { 1024, 7 },
  402. { 1408, 8 },
  403. { 1536, 9 },
  404. };
  405. /* Values for SAMPLE_RATE */
  406. static struct {
  407. int rate;
  408. u16 val;
  409. } wm8961_srate[] = {
  410. { 48000, 0 },
  411. { 44100, 0 },
  412. { 32000, 1 },
  413. { 22050, 2 },
  414. { 24000, 2 },
  415. { 16000, 3 },
  416. { 11250, 4 },
  417. { 12000, 4 },
  418. { 8000, 5 },
  419. };
  420. static int wm8961_hw_params(struct snd_pcm_substream *substream,
  421. struct snd_pcm_hw_params *params,
  422. struct snd_soc_dai *dai)
  423. {
  424. struct snd_soc_codec *codec = dai->codec;
  425. struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
  426. int i, best, target, fs;
  427. u16 reg;
  428. fs = params_rate(params);
  429. if (!wm8961->sysclk) {
  430. dev_err(codec->dev, "MCLK has not been specified\n");
  431. return -EINVAL;
  432. }
  433. /* Find the closest sample rate for the filters */
  434. best = 0;
  435. for (i = 0; i < ARRAY_SIZE(wm8961_srate); i++) {
  436. if (abs(wm8961_srate[i].rate - fs) <
  437. abs(wm8961_srate[best].rate - fs))
  438. best = i;
  439. }
  440. reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_3);
  441. reg &= ~WM8961_SAMPLE_RATE_MASK;
  442. reg |= wm8961_srate[best].val;
  443. snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_3, reg);
  444. dev_dbg(codec->dev, "Selected SRATE %dHz for %dHz\n",
  445. wm8961_srate[best].rate, fs);
  446. /* Select a CLK_SYS/fs ratio equal to or higher than required */
  447. target = wm8961->sysclk / fs;
  448. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && target < 64) {
  449. dev_err(codec->dev,
  450. "SYSCLK must be at least 64*fs for DAC\n");
  451. return -EINVAL;
  452. }
  453. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && target < 256) {
  454. dev_err(codec->dev,
  455. "SYSCLK must be at least 256*fs for ADC\n");
  456. return -EINVAL;
  457. }
  458. for (i = 0; i < ARRAY_SIZE(wm8961_clk_sys_ratio); i++) {
  459. if (wm8961_clk_sys_ratio[i].ratio >= target)
  460. break;
  461. }
  462. if (i == ARRAY_SIZE(wm8961_clk_sys_ratio)) {
  463. dev_err(codec->dev, "Unable to generate CLK_SYS_RATE\n");
  464. return -EINVAL;
  465. }
  466. dev_dbg(codec->dev, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
  467. wm8961_clk_sys_ratio[i].ratio, wm8961->sysclk, fs,
  468. wm8961->sysclk / fs);
  469. reg = snd_soc_read(codec, WM8961_CLOCKING_4);
  470. reg &= ~WM8961_CLK_SYS_RATE_MASK;
  471. reg |= wm8961_clk_sys_ratio[i].val << WM8961_CLK_SYS_RATE_SHIFT;
  472. snd_soc_write(codec, WM8961_CLOCKING_4, reg);
  473. reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
  474. reg &= ~WM8961_WL_MASK;
  475. switch (params_width(params)) {
  476. case 16:
  477. break;
  478. case 20:
  479. reg |= 1 << WM8961_WL_SHIFT;
  480. break;
  481. case 24:
  482. reg |= 2 << WM8961_WL_SHIFT;
  483. break;
  484. case 32:
  485. reg |= 3 << WM8961_WL_SHIFT;
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, reg);
  491. /* Sloping stop-band filter is recommended for <= 24kHz */
  492. reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
  493. if (fs <= 24000)
  494. reg |= WM8961_DACSLOPE;
  495. else
  496. reg &= ~WM8961_DACSLOPE;
  497. snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
  498. return 0;
  499. }
  500. static int wm8961_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  501. unsigned int freq,
  502. int dir)
  503. {
  504. struct snd_soc_codec *codec = dai->codec;
  505. struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
  506. u16 reg = snd_soc_read(codec, WM8961_CLOCKING1);
  507. if (freq > 33000000) {
  508. dev_err(codec->dev, "MCLK must be <33MHz\n");
  509. return -EINVAL;
  510. }
  511. if (freq > 16500000) {
  512. dev_dbg(codec->dev, "Using MCLK/2 for %dHz MCLK\n", freq);
  513. reg |= WM8961_MCLKDIV;
  514. freq /= 2;
  515. } else {
  516. dev_dbg(codec->dev, "Using MCLK/1 for %dHz MCLK\n", freq);
  517. reg &= ~WM8961_MCLKDIV;
  518. }
  519. snd_soc_write(codec, WM8961_CLOCKING1, reg);
  520. wm8961->sysclk = freq;
  521. return 0;
  522. }
  523. static int wm8961_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  524. {
  525. struct snd_soc_codec *codec = dai->codec;
  526. u16 aif = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
  527. aif &= ~(WM8961_BCLKINV | WM8961_LRP |
  528. WM8961_MS | WM8961_FORMAT_MASK);
  529. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  530. case SND_SOC_DAIFMT_CBM_CFM:
  531. aif |= WM8961_MS;
  532. break;
  533. case SND_SOC_DAIFMT_CBS_CFS:
  534. break;
  535. default:
  536. return -EINVAL;
  537. }
  538. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  539. case SND_SOC_DAIFMT_RIGHT_J:
  540. break;
  541. case SND_SOC_DAIFMT_LEFT_J:
  542. aif |= 1;
  543. break;
  544. case SND_SOC_DAIFMT_I2S:
  545. aif |= 2;
  546. break;
  547. case SND_SOC_DAIFMT_DSP_B:
  548. aif |= WM8961_LRP;
  549. case SND_SOC_DAIFMT_DSP_A:
  550. aif |= 3;
  551. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  552. case SND_SOC_DAIFMT_NB_NF:
  553. case SND_SOC_DAIFMT_IB_NF:
  554. break;
  555. default:
  556. return -EINVAL;
  557. }
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  563. case SND_SOC_DAIFMT_NB_NF:
  564. break;
  565. case SND_SOC_DAIFMT_NB_IF:
  566. aif |= WM8961_LRP;
  567. break;
  568. case SND_SOC_DAIFMT_IB_NF:
  569. aif |= WM8961_BCLKINV;
  570. break;
  571. case SND_SOC_DAIFMT_IB_IF:
  572. aif |= WM8961_BCLKINV | WM8961_LRP;
  573. break;
  574. default:
  575. return -EINVAL;
  576. }
  577. return snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, aif);
  578. }
  579. static int wm8961_set_tristate(struct snd_soc_dai *dai, int tristate)
  580. {
  581. struct snd_soc_codec *codec = dai->codec;
  582. u16 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_2);
  583. if (tristate)
  584. reg |= WM8961_TRIS;
  585. else
  586. reg &= ~WM8961_TRIS;
  587. return snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_2, reg);
  588. }
  589. static int wm8961_digital_mute(struct snd_soc_dai *dai, int mute)
  590. {
  591. struct snd_soc_codec *codec = dai->codec;
  592. u16 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_1);
  593. if (mute)
  594. reg |= WM8961_DACMU;
  595. else
  596. reg &= ~WM8961_DACMU;
  597. msleep(17);
  598. return snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_1, reg);
  599. }
  600. static int wm8961_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
  601. {
  602. struct snd_soc_codec *codec = dai->codec;
  603. u16 reg;
  604. switch (div_id) {
  605. case WM8961_BCLK:
  606. reg = snd_soc_read(codec, WM8961_CLOCKING2);
  607. reg &= ~WM8961_BCLKDIV_MASK;
  608. reg |= div;
  609. snd_soc_write(codec, WM8961_CLOCKING2, reg);
  610. break;
  611. case WM8961_LRCLK:
  612. reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_2);
  613. reg &= ~WM8961_LRCLK_RATE_MASK;
  614. reg |= div;
  615. snd_soc_write(codec, WM8961_AUDIO_INTERFACE_2, reg);
  616. break;
  617. default:
  618. return -EINVAL;
  619. }
  620. return 0;
  621. }
  622. static int wm8961_set_bias_level(struct snd_soc_codec *codec,
  623. enum snd_soc_bias_level level)
  624. {
  625. u16 reg;
  626. /* This is all slightly unusual since we have no bypass paths
  627. * and the output amplifier structure means we can just slam
  628. * the biases straight up rather than having to ramp them
  629. * slowly.
  630. */
  631. switch (level) {
  632. case SND_SOC_BIAS_ON:
  633. break;
  634. case SND_SOC_BIAS_PREPARE:
  635. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  636. /* Enable bias generation */
  637. reg = snd_soc_read(codec, WM8961_ANTI_POP);
  638. reg |= WM8961_BUFIOEN | WM8961_BUFDCOPEN;
  639. snd_soc_write(codec, WM8961_ANTI_POP, reg);
  640. /* VMID=2*50k, VREF */
  641. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  642. reg &= ~WM8961_VMIDSEL_MASK;
  643. reg |= (1 << WM8961_VMIDSEL_SHIFT) | WM8961_VREF;
  644. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  645. }
  646. break;
  647. case SND_SOC_BIAS_STANDBY:
  648. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  649. /* VREF off */
  650. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  651. reg &= ~WM8961_VREF;
  652. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  653. /* Bias generation off */
  654. reg = snd_soc_read(codec, WM8961_ANTI_POP);
  655. reg &= ~(WM8961_BUFIOEN | WM8961_BUFDCOPEN);
  656. snd_soc_write(codec, WM8961_ANTI_POP, reg);
  657. /* VMID off */
  658. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  659. reg &= ~WM8961_VMIDSEL_MASK;
  660. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  661. }
  662. break;
  663. case SND_SOC_BIAS_OFF:
  664. break;
  665. }
  666. codec->dapm.bias_level = level;
  667. return 0;
  668. }
  669. #define WM8961_RATES SNDRV_PCM_RATE_8000_48000
  670. #define WM8961_FORMATS \
  671. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  672. SNDRV_PCM_FMTBIT_S24_LE)
  673. static const struct snd_soc_dai_ops wm8961_dai_ops = {
  674. .hw_params = wm8961_hw_params,
  675. .set_sysclk = wm8961_set_sysclk,
  676. .set_fmt = wm8961_set_fmt,
  677. .digital_mute = wm8961_digital_mute,
  678. .set_tristate = wm8961_set_tristate,
  679. .set_clkdiv = wm8961_set_clkdiv,
  680. };
  681. static struct snd_soc_dai_driver wm8961_dai = {
  682. .name = "wm8961-hifi",
  683. .playback = {
  684. .stream_name = "HiFi Playback",
  685. .channels_min = 1,
  686. .channels_max = 2,
  687. .rates = WM8961_RATES,
  688. .formats = WM8961_FORMATS,},
  689. .capture = {
  690. .stream_name = "HiFi Capture",
  691. .channels_min = 1,
  692. .channels_max = 2,
  693. .rates = WM8961_RATES,
  694. .formats = WM8961_FORMATS,},
  695. .ops = &wm8961_dai_ops,
  696. };
  697. static int wm8961_probe(struct snd_soc_codec *codec)
  698. {
  699. struct snd_soc_dapm_context *dapm = &codec->dapm;
  700. u16 reg;
  701. /* Enable class W */
  702. reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_B);
  703. reg |= WM8961_CP_DYN_PWR_MASK;
  704. snd_soc_write(codec, WM8961_CHARGE_PUMP_B, reg);
  705. /* Latch volume update bits (right channel only, we always
  706. * write both out) and default ZC on. */
  707. reg = snd_soc_read(codec, WM8961_ROUT1_VOLUME);
  708. snd_soc_write(codec, WM8961_ROUT1_VOLUME,
  709. reg | WM8961_LO1ZC | WM8961_OUT1VU);
  710. snd_soc_write(codec, WM8961_LOUT1_VOLUME, reg | WM8961_LO1ZC);
  711. reg = snd_soc_read(codec, WM8961_ROUT2_VOLUME);
  712. snd_soc_write(codec, WM8961_ROUT2_VOLUME,
  713. reg | WM8961_SPKRZC | WM8961_SPKVU);
  714. snd_soc_write(codec, WM8961_LOUT2_VOLUME, reg | WM8961_SPKLZC);
  715. reg = snd_soc_read(codec, WM8961_RIGHT_ADC_VOLUME);
  716. snd_soc_write(codec, WM8961_RIGHT_ADC_VOLUME, reg | WM8961_ADCVU);
  717. reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
  718. snd_soc_write(codec, WM8961_RIGHT_INPUT_VOLUME, reg | WM8961_IPVU);
  719. /* Use soft mute by default */
  720. reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
  721. reg |= WM8961_DACSMM;
  722. snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
  723. /* Use automatic clocking mode by default; for now this is all
  724. * we support.
  725. */
  726. reg = snd_soc_read(codec, WM8961_CLOCKING_3);
  727. reg &= ~WM8961_MANUAL_MODE;
  728. snd_soc_write(codec, WM8961_CLOCKING_3, reg);
  729. wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  730. snd_soc_add_codec_controls(codec, wm8961_snd_controls,
  731. ARRAY_SIZE(wm8961_snd_controls));
  732. snd_soc_dapm_new_controls(dapm, wm8961_dapm_widgets,
  733. ARRAY_SIZE(wm8961_dapm_widgets));
  734. snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
  735. return 0;
  736. }
  737. static int wm8961_remove(struct snd_soc_codec *codec)
  738. {
  739. wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
  740. return 0;
  741. }
  742. #ifdef CONFIG_PM
  743. static int wm8961_suspend(struct snd_soc_codec *codec)
  744. {
  745. wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
  746. return 0;
  747. }
  748. static int wm8961_resume(struct snd_soc_codec *codec)
  749. {
  750. snd_soc_cache_sync(codec);
  751. wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  752. return 0;
  753. }
  754. #else
  755. #define wm8961_suspend NULL
  756. #define wm8961_resume NULL
  757. #endif
  758. static struct snd_soc_codec_driver soc_codec_dev_wm8961 = {
  759. .probe = wm8961_probe,
  760. .remove = wm8961_remove,
  761. .suspend = wm8961_suspend,
  762. .resume = wm8961_resume,
  763. .set_bias_level = wm8961_set_bias_level,
  764. };
  765. static const struct regmap_config wm8961_regmap = {
  766. .reg_bits = 8,
  767. .val_bits = 16,
  768. .max_register = WM8961_MAX_REGISTER,
  769. .reg_defaults = wm8961_reg_defaults,
  770. .num_reg_defaults = ARRAY_SIZE(wm8961_reg_defaults),
  771. .cache_type = REGCACHE_RBTREE,
  772. .volatile_reg = wm8961_volatile,
  773. .readable_reg = wm8961_readable,
  774. };
  775. static int wm8961_i2c_probe(struct i2c_client *i2c,
  776. const struct i2c_device_id *id)
  777. {
  778. struct wm8961_priv *wm8961;
  779. unsigned int val;
  780. int ret;
  781. wm8961 = devm_kzalloc(&i2c->dev, sizeof(struct wm8961_priv),
  782. GFP_KERNEL);
  783. if (wm8961 == NULL)
  784. return -ENOMEM;
  785. wm8961->regmap = devm_regmap_init_i2c(i2c, &wm8961_regmap);
  786. if (IS_ERR(wm8961->regmap))
  787. return PTR_ERR(wm8961->regmap);
  788. ret = regmap_read(wm8961->regmap, WM8961_SOFTWARE_RESET, &val);
  789. if (ret != 0) {
  790. dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
  791. return ret;
  792. }
  793. if (val != 0x1801) {
  794. dev_err(&i2c->dev, "Device is not a WM8961: ID=0x%x\n", val);
  795. return -EINVAL;
  796. }
  797. /* This isn't volatile - readback doesn't correspond to write */
  798. regcache_cache_bypass(wm8961->regmap, true);
  799. ret = regmap_read(wm8961->regmap, WM8961_RIGHT_INPUT_VOLUME, &val);
  800. regcache_cache_bypass(wm8961->regmap, false);
  801. if (ret != 0) {
  802. dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
  803. return ret;
  804. }
  805. dev_info(&i2c->dev, "WM8961 family %d revision %c\n",
  806. (val & WM8961_DEVICE_ID_MASK) >> WM8961_DEVICE_ID_SHIFT,
  807. ((val & WM8961_CHIP_REV_MASK) >> WM8961_CHIP_REV_SHIFT)
  808. + 'A');
  809. ret = regmap_write(wm8961->regmap, WM8961_SOFTWARE_RESET, 0x1801);
  810. if (ret != 0) {
  811. dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
  812. return ret;
  813. }
  814. i2c_set_clientdata(i2c, wm8961);
  815. ret = snd_soc_register_codec(&i2c->dev,
  816. &soc_codec_dev_wm8961, &wm8961_dai, 1);
  817. return ret;
  818. }
  819. static int wm8961_i2c_remove(struct i2c_client *client)
  820. {
  821. snd_soc_unregister_codec(&client->dev);
  822. return 0;
  823. }
  824. static const struct i2c_device_id wm8961_i2c_id[] = {
  825. { "wm8961", 0 },
  826. { }
  827. };
  828. MODULE_DEVICE_TABLE(i2c, wm8961_i2c_id);
  829. static struct i2c_driver wm8961_i2c_driver = {
  830. .driver = {
  831. .name = "wm8961",
  832. .owner = THIS_MODULE,
  833. },
  834. .probe = wm8961_i2c_probe,
  835. .remove = wm8961_i2c_remove,
  836. .id_table = wm8961_i2c_id,
  837. };
  838. module_i2c_driver(wm8961_i2c_driver);
  839. MODULE_DESCRIPTION("ASoC WM8961 driver");
  840. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  841. MODULE_LICENSE("GPL");