wm8978.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * wm8978.c -- WM8978 ALSA SoC Audio Codec driver
  3. *
  4. * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. * Copyright (C) 2007 Carlos Munoz <carlos@kenati.com>
  6. * Copyright 2006-2009 Wolfson Microelectronics PLC.
  7. * Based on wm8974 and wm8990 by Liam Girdwood <lrg@slimlogic.co.uk>
  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/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include <asm/div64.h>
  28. #include "wm8978.h"
  29. /* wm8978 register cache. Note that register 0 is not included in the cache. */
  30. static const u16 wm8978_reg[WM8978_CACHEREGNUM] = {
  31. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x00...0x03 */
  32. 0x0050, 0x0000, 0x0140, 0x0000, /* 0x04...0x07 */
  33. 0x0000, 0x0000, 0x0000, 0x00ff, /* 0x08...0x0b */
  34. 0x00ff, 0x0000, 0x0100, 0x00ff, /* 0x0c...0x0f */
  35. 0x00ff, 0x0000, 0x012c, 0x002c, /* 0x10...0x13 */
  36. 0x002c, 0x002c, 0x002c, 0x0000, /* 0x14...0x17 */
  37. 0x0032, 0x0000, 0x0000, 0x0000, /* 0x18...0x1b */
  38. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x1c...0x1f */
  39. 0x0038, 0x000b, 0x0032, 0x0000, /* 0x20...0x23 */
  40. 0x0008, 0x000c, 0x0093, 0x00e9, /* 0x24...0x27 */
  41. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x28...0x2b */
  42. 0x0033, 0x0010, 0x0010, 0x0100, /* 0x2c...0x2f */
  43. 0x0100, 0x0002, 0x0001, 0x0001, /* 0x30...0x33 */
  44. 0x0039, 0x0039, 0x0039, 0x0039, /* 0x34...0x37 */
  45. 0x0001, 0x0001, /* 0x38...0x3b */
  46. };
  47. /* codec private data */
  48. struct wm8978_priv {
  49. unsigned int f_pllout;
  50. unsigned int f_mclk;
  51. unsigned int f_256fs;
  52. unsigned int f_opclk;
  53. int mclk_idx;
  54. enum wm8978_sysclk_src sysclk;
  55. };
  56. static const char *wm8978_companding[] = {"Off", "NC", "u-law", "A-law"};
  57. static const char *wm8978_eqmode[] = {"Capture", "Playback"};
  58. static const char *wm8978_bw[] = {"Narrow", "Wide"};
  59. static const char *wm8978_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz"};
  60. static const char *wm8978_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz"};
  61. static const char *wm8978_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz"};
  62. static const char *wm8978_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"};
  63. static const char *wm8978_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz"};
  64. static const char *wm8978_alc3[] = {"ALC", "Limiter"};
  65. static const char *wm8978_alc1[] = {"Off", "Right", "Left", "Both"};
  66. static const SOC_ENUM_SINGLE_DECL(adc_compand, WM8978_COMPANDING_CONTROL, 1,
  67. wm8978_companding);
  68. static const SOC_ENUM_SINGLE_DECL(dac_compand, WM8978_COMPANDING_CONTROL, 3,
  69. wm8978_companding);
  70. static const SOC_ENUM_SINGLE_DECL(eqmode, WM8978_EQ1, 8, wm8978_eqmode);
  71. static const SOC_ENUM_SINGLE_DECL(eq1, WM8978_EQ1, 5, wm8978_eq1);
  72. static const SOC_ENUM_SINGLE_DECL(eq2bw, WM8978_EQ2, 8, wm8978_bw);
  73. static const SOC_ENUM_SINGLE_DECL(eq2, WM8978_EQ2, 5, wm8978_eq2);
  74. static const SOC_ENUM_SINGLE_DECL(eq3bw, WM8978_EQ3, 8, wm8978_bw);
  75. static const SOC_ENUM_SINGLE_DECL(eq3, WM8978_EQ3, 5, wm8978_eq3);
  76. static const SOC_ENUM_SINGLE_DECL(eq4bw, WM8978_EQ4, 8, wm8978_bw);
  77. static const SOC_ENUM_SINGLE_DECL(eq4, WM8978_EQ4, 5, wm8978_eq4);
  78. static const SOC_ENUM_SINGLE_DECL(eq5, WM8978_EQ5, 5, wm8978_eq5);
  79. static const SOC_ENUM_SINGLE_DECL(alc3, WM8978_ALC_CONTROL_3, 8, wm8978_alc3);
  80. static const SOC_ENUM_SINGLE_DECL(alc1, WM8978_ALC_CONTROL_1, 7, wm8978_alc1);
  81. static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
  82. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  83. static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0);
  84. static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0);
  85. static const DECLARE_TLV_DB_SCALE(boost_tlv, -1500, 300, 1);
  86. static const DECLARE_TLV_DB_SCALE(limiter_tlv, 0, 100, 0);
  87. static const struct snd_kcontrol_new wm8978_snd_controls[] = {
  88. SOC_SINGLE("Digital Loopback Switch",
  89. WM8978_COMPANDING_CONTROL, 0, 1, 0),
  90. SOC_ENUM("ADC Companding", adc_compand),
  91. SOC_ENUM("DAC Companding", dac_compand),
  92. SOC_DOUBLE("DAC Inversion Switch", WM8978_DAC_CONTROL, 0, 1, 1, 0),
  93. SOC_DOUBLE_R_TLV("PCM Volume",
  94. WM8978_LEFT_DAC_DIGITAL_VOLUME, WM8978_RIGHT_DAC_DIGITAL_VOLUME,
  95. 0, 255, 0, digital_tlv),
  96. SOC_SINGLE("High Pass Filter Switch", WM8978_ADC_CONTROL, 8, 1, 0),
  97. SOC_SINGLE("High Pass Cut Off", WM8978_ADC_CONTROL, 4, 7, 0),
  98. SOC_DOUBLE("ADC Inversion Switch", WM8978_ADC_CONTROL, 0, 1, 1, 0),
  99. SOC_DOUBLE_R_TLV("ADC Volume",
  100. WM8978_LEFT_ADC_DIGITAL_VOLUME, WM8978_RIGHT_ADC_DIGITAL_VOLUME,
  101. 0, 255, 0, digital_tlv),
  102. SOC_ENUM("Equaliser Function", eqmode),
  103. SOC_ENUM("EQ1 Cut Off", eq1),
  104. SOC_SINGLE_TLV("EQ1 Volume", WM8978_EQ1, 0, 24, 1, eq_tlv),
  105. SOC_ENUM("Equaliser EQ2 Bandwith", eq2bw),
  106. SOC_ENUM("EQ2 Cut Off", eq2),
  107. SOC_SINGLE_TLV("EQ2 Volume", WM8978_EQ2, 0, 24, 1, eq_tlv),
  108. SOC_ENUM("Equaliser EQ3 Bandwith", eq3bw),
  109. SOC_ENUM("EQ3 Cut Off", eq3),
  110. SOC_SINGLE_TLV("EQ3 Volume", WM8978_EQ3, 0, 24, 1, eq_tlv),
  111. SOC_ENUM("Equaliser EQ4 Bandwith", eq4bw),
  112. SOC_ENUM("EQ4 Cut Off", eq4),
  113. SOC_SINGLE_TLV("EQ4 Volume", WM8978_EQ4, 0, 24, 1, eq_tlv),
  114. SOC_ENUM("EQ5 Cut Off", eq5),
  115. SOC_SINGLE_TLV("EQ5 Volume", WM8978_EQ5, 0, 24, 1, eq_tlv),
  116. SOC_SINGLE("DAC Playback Limiter Switch",
  117. WM8978_DAC_LIMITER_1, 8, 1, 0),
  118. SOC_SINGLE("DAC Playback Limiter Decay",
  119. WM8978_DAC_LIMITER_1, 4, 15, 0),
  120. SOC_SINGLE("DAC Playback Limiter Attack",
  121. WM8978_DAC_LIMITER_1, 0, 15, 0),
  122. SOC_SINGLE("DAC Playback Limiter Threshold",
  123. WM8978_DAC_LIMITER_2, 4, 7, 0),
  124. SOC_SINGLE_TLV("DAC Playback Limiter Volume",
  125. WM8978_DAC_LIMITER_2, 0, 12, 0, limiter_tlv),
  126. SOC_ENUM("ALC Enable Switch", alc1),
  127. SOC_SINGLE("ALC Capture Min Gain", WM8978_ALC_CONTROL_1, 0, 7, 0),
  128. SOC_SINGLE("ALC Capture Max Gain", WM8978_ALC_CONTROL_1, 3, 7, 0),
  129. SOC_SINGLE("ALC Capture Hold", WM8978_ALC_CONTROL_2, 4, 10, 0),
  130. SOC_SINGLE("ALC Capture Target", WM8978_ALC_CONTROL_2, 0, 15, 0),
  131. SOC_ENUM("ALC Capture Mode", alc3),
  132. SOC_SINGLE("ALC Capture Decay", WM8978_ALC_CONTROL_3, 4, 10, 0),
  133. SOC_SINGLE("ALC Capture Attack", WM8978_ALC_CONTROL_3, 0, 10, 0),
  134. SOC_SINGLE("ALC Capture Noise Gate Switch", WM8978_NOISE_GATE, 3, 1, 0),
  135. SOC_SINGLE("ALC Capture Noise Gate Threshold",
  136. WM8978_NOISE_GATE, 0, 7, 0),
  137. SOC_DOUBLE_R("Capture PGA ZC Switch",
  138. WM8978_LEFT_INP_PGA_CONTROL, WM8978_RIGHT_INP_PGA_CONTROL,
  139. 7, 1, 0),
  140. /* OUT1 - Headphones */
  141. SOC_DOUBLE_R("Headphone Playback ZC Switch",
  142. WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL, 7, 1, 0),
  143. SOC_DOUBLE_R_TLV("Headphone Playback Volume",
  144. WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL,
  145. 0, 63, 0, spk_tlv),
  146. /* OUT2 - Speakers */
  147. SOC_DOUBLE_R("Speaker Playback ZC Switch",
  148. WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL, 7, 1, 0),
  149. SOC_DOUBLE_R_TLV("Speaker Playback Volume",
  150. WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL,
  151. 0, 63, 0, spk_tlv),
  152. /* OUT3/4 - Line Output */
  153. SOC_DOUBLE_R("Line Playback Switch",
  154. WM8978_OUT3_MIXER_CONTROL, WM8978_OUT4_MIXER_CONTROL, 6, 1, 1),
  155. /* Mixer #3: Boost (Input) mixer */
  156. SOC_DOUBLE_R("PGA Boost (+20dB)",
  157. WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
  158. 8, 1, 0),
  159. SOC_DOUBLE_R_TLV("L2/R2 Boost Volume",
  160. WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
  161. 4, 7, 0, boost_tlv),
  162. SOC_DOUBLE_R_TLV("Aux Boost Volume",
  163. WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
  164. 0, 7, 0, boost_tlv),
  165. /* Input PGA volume */
  166. SOC_DOUBLE_R_TLV("Input PGA Volume",
  167. WM8978_LEFT_INP_PGA_CONTROL, WM8978_RIGHT_INP_PGA_CONTROL,
  168. 0, 63, 0, inpga_tlv),
  169. /* Headphone */
  170. SOC_DOUBLE_R("Headphone Switch",
  171. WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL, 6, 1, 1),
  172. /* Speaker */
  173. SOC_DOUBLE_R("Speaker Switch",
  174. WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL, 6, 1, 1),
  175. /* DAC / ADC oversampling */
  176. SOC_SINGLE("DAC 128x Oversampling Switch", WM8978_DAC_CONTROL,
  177. 5, 1, 0),
  178. SOC_SINGLE("ADC 128x Oversampling Switch", WM8978_ADC_CONTROL,
  179. 5, 1, 0),
  180. };
  181. /* Mixer #1: Output (OUT1, OUT2) Mixer: mix AUX, Input mixer output and DAC */
  182. static const struct snd_kcontrol_new wm8978_left_out_mixer[] = {
  183. SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_LEFT_MIXER_CONTROL, 1, 1, 0),
  184. SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_LEFT_MIXER_CONTROL, 5, 1, 0),
  185. SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_LEFT_MIXER_CONTROL, 0, 1, 0),
  186. };
  187. static const struct snd_kcontrol_new wm8978_right_out_mixer[] = {
  188. SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_RIGHT_MIXER_CONTROL, 1, 1, 0),
  189. SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 5, 1, 0),
  190. SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 0, 1, 0),
  191. };
  192. /* OUT3/OUT4 Mixer not implemented */
  193. /* Mixer #2: Input PGA Mute */
  194. static const struct snd_kcontrol_new wm8978_left_input_mixer[] = {
  195. SOC_DAPM_SINGLE("L2 Switch", WM8978_INPUT_CONTROL, 2, 1, 0),
  196. SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 1, 1, 0),
  197. SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 0, 1, 0),
  198. };
  199. static const struct snd_kcontrol_new wm8978_right_input_mixer[] = {
  200. SOC_DAPM_SINGLE("R2 Switch", WM8978_INPUT_CONTROL, 6, 1, 0),
  201. SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 5, 1, 0),
  202. SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 4, 1, 0),
  203. };
  204. static const struct snd_soc_dapm_widget wm8978_dapm_widgets[] = {
  205. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
  206. WM8978_POWER_MANAGEMENT_3, 0, 0),
  207. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
  208. WM8978_POWER_MANAGEMENT_3, 1, 0),
  209. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
  210. WM8978_POWER_MANAGEMENT_2, 0, 0),
  211. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
  212. WM8978_POWER_MANAGEMENT_2, 1, 0),
  213. /* Mixer #1: OUT1,2 */
  214. SOC_MIXER_ARRAY("Left Output Mixer", WM8978_POWER_MANAGEMENT_3,
  215. 2, 0, wm8978_left_out_mixer),
  216. SOC_MIXER_ARRAY("Right Output Mixer", WM8978_POWER_MANAGEMENT_3,
  217. 3, 0, wm8978_right_out_mixer),
  218. SOC_MIXER_ARRAY("Left Input Mixer", WM8978_POWER_MANAGEMENT_2,
  219. 2, 0, wm8978_left_input_mixer),
  220. SOC_MIXER_ARRAY("Right Input Mixer", WM8978_POWER_MANAGEMENT_2,
  221. 3, 0, wm8978_right_input_mixer),
  222. SND_SOC_DAPM_PGA("Left Boost Mixer", WM8978_POWER_MANAGEMENT_2,
  223. 4, 0, NULL, 0),
  224. SND_SOC_DAPM_PGA("Right Boost Mixer", WM8978_POWER_MANAGEMENT_2,
  225. 5, 0, NULL, 0),
  226. SND_SOC_DAPM_PGA("Left Capture PGA", WM8978_LEFT_INP_PGA_CONTROL,
  227. 6, 1, NULL, 0),
  228. SND_SOC_DAPM_PGA("Right Capture PGA", WM8978_RIGHT_INP_PGA_CONTROL,
  229. 6, 1, NULL, 0),
  230. SND_SOC_DAPM_PGA("Left Headphone Out", WM8978_POWER_MANAGEMENT_2,
  231. 7, 0, NULL, 0),
  232. SND_SOC_DAPM_PGA("Right Headphone Out", WM8978_POWER_MANAGEMENT_2,
  233. 8, 0, NULL, 0),
  234. SND_SOC_DAPM_PGA("Left Speaker Out", WM8978_POWER_MANAGEMENT_3,
  235. 6, 0, NULL, 0),
  236. SND_SOC_DAPM_PGA("Right Speaker Out", WM8978_POWER_MANAGEMENT_3,
  237. 5, 0, NULL, 0),
  238. SND_SOC_DAPM_MIXER("OUT4 VMID", WM8978_POWER_MANAGEMENT_3,
  239. 8, 0, NULL, 0),
  240. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8978_POWER_MANAGEMENT_1, 4, 0),
  241. SND_SOC_DAPM_INPUT("LMICN"),
  242. SND_SOC_DAPM_INPUT("LMICP"),
  243. SND_SOC_DAPM_INPUT("RMICN"),
  244. SND_SOC_DAPM_INPUT("RMICP"),
  245. SND_SOC_DAPM_INPUT("LAUX"),
  246. SND_SOC_DAPM_INPUT("RAUX"),
  247. SND_SOC_DAPM_INPUT("L2"),
  248. SND_SOC_DAPM_INPUT("R2"),
  249. SND_SOC_DAPM_OUTPUT("LHP"),
  250. SND_SOC_DAPM_OUTPUT("RHP"),
  251. SND_SOC_DAPM_OUTPUT("LSPK"),
  252. SND_SOC_DAPM_OUTPUT("RSPK"),
  253. };
  254. static const struct snd_soc_dapm_route wm8978_dapm_routes[] = {
  255. /* Output mixer */
  256. {"Right Output Mixer", "PCM Playback Switch", "Right DAC"},
  257. {"Right Output Mixer", "Aux Playback Switch", "RAUX"},
  258. {"Right Output Mixer", "Line Bypass Switch", "Right Boost Mixer"},
  259. {"Left Output Mixer", "PCM Playback Switch", "Left DAC"},
  260. {"Left Output Mixer", "Aux Playback Switch", "LAUX"},
  261. {"Left Output Mixer", "Line Bypass Switch", "Left Boost Mixer"},
  262. /* Outputs */
  263. {"Right Headphone Out", NULL, "Right Output Mixer"},
  264. {"RHP", NULL, "Right Headphone Out"},
  265. {"Left Headphone Out", NULL, "Left Output Mixer"},
  266. {"LHP", NULL, "Left Headphone Out"},
  267. {"Right Speaker Out", NULL, "Right Output Mixer"},
  268. {"RSPK", NULL, "Right Speaker Out"},
  269. {"Left Speaker Out", NULL, "Left Output Mixer"},
  270. {"LSPK", NULL, "Left Speaker Out"},
  271. /* Boost Mixer */
  272. {"Right ADC", NULL, "Right Boost Mixer"},
  273. {"Right Boost Mixer", NULL, "RAUX"},
  274. {"Right Boost Mixer", NULL, "Right Capture PGA"},
  275. {"Right Boost Mixer", NULL, "R2"},
  276. {"Left ADC", NULL, "Left Boost Mixer"},
  277. {"Left Boost Mixer", NULL, "LAUX"},
  278. {"Left Boost Mixer", NULL, "Left Capture PGA"},
  279. {"Left Boost Mixer", NULL, "L2"},
  280. /* Input PGA */
  281. {"Right Capture PGA", NULL, "Right Input Mixer"},
  282. {"Left Capture PGA", NULL, "Left Input Mixer"},
  283. {"Right Input Mixer", "R2 Switch", "R2"},
  284. {"Right Input Mixer", "MicN Switch", "RMICN"},
  285. {"Right Input Mixer", "MicP Switch", "RMICP"},
  286. {"Left Input Mixer", "L2 Switch", "L2"},
  287. {"Left Input Mixer", "MicN Switch", "LMICN"},
  288. {"Left Input Mixer", "MicP Switch", "LMICP"},
  289. };
  290. /* PLL divisors */
  291. struct wm8978_pll_div {
  292. u32 k;
  293. u8 n;
  294. u8 div2;
  295. };
  296. #define FIXED_PLL_SIZE (1 << 24)
  297. static void pll_factors(struct snd_soc_codec *codec,
  298. struct wm8978_pll_div *pll_div, unsigned int target, unsigned int source)
  299. {
  300. u64 k_part;
  301. unsigned int k, n_div, n_mod;
  302. n_div = target / source;
  303. if (n_div < 6) {
  304. source >>= 1;
  305. pll_div->div2 = 1;
  306. n_div = target / source;
  307. } else {
  308. pll_div->div2 = 0;
  309. }
  310. if (n_div < 6 || n_div > 12)
  311. dev_warn(codec->dev,
  312. "WM8978 N value exceeds recommended range! N = %u\n",
  313. n_div);
  314. pll_div->n = n_div;
  315. n_mod = target - source * n_div;
  316. k_part = FIXED_PLL_SIZE * (long long)n_mod + source / 2;
  317. do_div(k_part, source);
  318. k = k_part & 0xFFFFFFFF;
  319. pll_div->k = k;
  320. }
  321. /* MCLK dividers */
  322. static const int mclk_numerator[] = {1, 3, 2, 3, 4, 6, 8, 12};
  323. static const int mclk_denominator[] = {1, 2, 1, 1, 1, 1, 1, 1};
  324. /*
  325. * find index >= idx, such that, for a given f_out,
  326. * 3 * f_mclk / 4 <= f_PLLOUT < 13 * f_mclk / 4
  327. * f_out can be f_256fs or f_opclk, currently only used for f_256fs. Can be
  328. * generalised for f_opclk with suitable coefficient arrays, but currently
  329. * the OPCLK divisor is calculated directly, not iteratively.
  330. */
  331. static int wm8978_enum_mclk(unsigned int f_out, unsigned int f_mclk,
  332. unsigned int *f_pllout)
  333. {
  334. int i;
  335. for (i = 0; i < ARRAY_SIZE(mclk_numerator); i++) {
  336. unsigned int f_pllout_x4 = 4 * f_out * mclk_numerator[i] /
  337. mclk_denominator[i];
  338. if (3 * f_mclk <= f_pllout_x4 && f_pllout_x4 < 13 * f_mclk) {
  339. *f_pllout = f_pllout_x4 / 4;
  340. return i;
  341. }
  342. }
  343. return -EINVAL;
  344. }
  345. /*
  346. * Calculate internal frequencies and dividers, according to Figure 40
  347. * "PLL and Clock Select Circuit" in WM8978 datasheet Rev. 2.6
  348. */
  349. static int wm8978_configure_pll(struct snd_soc_codec *codec)
  350. {
  351. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  352. struct wm8978_pll_div pll_div;
  353. unsigned int f_opclk = wm8978->f_opclk, f_mclk = wm8978->f_mclk,
  354. f_256fs = wm8978->f_256fs;
  355. unsigned int f2;
  356. if (!f_mclk)
  357. return -EINVAL;
  358. if (f_opclk) {
  359. unsigned int opclk_div;
  360. /* Cannot set up MCLK divider now, do later */
  361. wm8978->mclk_idx = -1;
  362. /*
  363. * The user needs OPCLK. Choose OPCLKDIV to put
  364. * 6 <= R = f2 / f1 < 13, 1 <= OPCLKDIV <= 4.
  365. * f_opclk = f_mclk * prescale * R / 4 / OPCLKDIV, where
  366. * prescale = 1, or prescale = 2. Prescale is calculated inside
  367. * pll_factors(). We have to select f_PLLOUT, such that
  368. * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
  369. * f_mclk * 3 / 16 <= f_opclk < f_mclk * 13 / 4.
  370. */
  371. if (16 * f_opclk < 3 * f_mclk || 4 * f_opclk >= 13 * f_mclk)
  372. return -EINVAL;
  373. if (4 * f_opclk < 3 * f_mclk)
  374. /* Have to use OPCLKDIV */
  375. opclk_div = (3 * f_mclk / 4 + f_opclk - 1) / f_opclk;
  376. else
  377. opclk_div = 1;
  378. dev_dbg(codec->dev, "%s: OPCLKDIV=%d\n", __func__, opclk_div);
  379. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 0x30,
  380. (opclk_div - 1) << 4);
  381. wm8978->f_pllout = f_opclk * opclk_div;
  382. } else if (f_256fs) {
  383. /*
  384. * Not using OPCLK, but PLL is used for the codec, choose R:
  385. * 6 <= R = f2 / f1 < 13, to put 1 <= MCLKDIV <= 12.
  386. * f_256fs = f_mclk * prescale * R / 4 / MCLKDIV, where
  387. * prescale = 1, or prescale = 2. Prescale is calculated inside
  388. * pll_factors(). We have to select f_PLLOUT, such that
  389. * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
  390. * f_mclk * 3 / 48 <= f_256fs < f_mclk * 13 / 4. This means MCLK
  391. * must be 3.781MHz <= f_MCLK <= 32.768MHz
  392. */
  393. int idx = wm8978_enum_mclk(f_256fs, f_mclk, &wm8978->f_pllout);
  394. if (idx < 0)
  395. return idx;
  396. wm8978->mclk_idx = idx;
  397. /* GPIO1 into default mode as input - before configuring PLL */
  398. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
  399. } else {
  400. return -EINVAL;
  401. }
  402. f2 = wm8978->f_pllout * 4;
  403. dev_dbg(codec->dev, "%s: f_MCLK=%uHz, f_PLLOUT=%uHz\n", __func__,
  404. wm8978->f_mclk, wm8978->f_pllout);
  405. pll_factors(codec, &pll_div, f2, wm8978->f_mclk);
  406. dev_dbg(codec->dev, "%s: calculated PLL N=0x%x, K=0x%x, div2=%d\n",
  407. __func__, pll_div.n, pll_div.k, pll_div.div2);
  408. /* Turn PLL off for configuration... */
  409. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
  410. snd_soc_write(codec, WM8978_PLL_N, (pll_div.div2 << 4) | pll_div.n);
  411. snd_soc_write(codec, WM8978_PLL_K1, pll_div.k >> 18);
  412. snd_soc_write(codec, WM8978_PLL_K2, (pll_div.k >> 9) & 0x1ff);
  413. snd_soc_write(codec, WM8978_PLL_K3, pll_div.k & 0x1ff);
  414. /* ...and on again */
  415. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
  416. if (f_opclk)
  417. /* Output PLL (OPCLK) to GPIO1 */
  418. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 4);
  419. return 0;
  420. }
  421. /*
  422. * Configure WM8978 clock dividers.
  423. */
  424. static int wm8978_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  425. int div_id, int div)
  426. {
  427. struct snd_soc_codec *codec = codec_dai->codec;
  428. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  429. int ret = 0;
  430. switch (div_id) {
  431. case WM8978_OPCLKRATE:
  432. wm8978->f_opclk = div;
  433. if (wm8978->f_mclk)
  434. /*
  435. * We know the MCLK frequency, the user has requested
  436. * OPCLK, configure the PLL based on that and start it
  437. * and OPCLK immediately. We will configure PLL to match
  438. * user-requested OPCLK frquency as good as possible.
  439. * In fact, it is likely, that matching the sampling
  440. * rate, when it becomes known, is more important, and
  441. * we will not be reconfiguring PLL then, because we
  442. * must not interrupt OPCLK. But it should be fine,
  443. * because typically the user will request OPCLK to run
  444. * at 256fs or 512fs, and for these cases we will also
  445. * find an exact MCLK divider configuration - it will
  446. * be equal to or double the OPCLK divisor.
  447. */
  448. ret = wm8978_configure_pll(codec);
  449. break;
  450. case WM8978_BCLKDIV:
  451. if (div & ~0x1c)
  452. return -EINVAL;
  453. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x1c, div);
  454. break;
  455. default:
  456. return -EINVAL;
  457. }
  458. dev_dbg(codec->dev, "%s: ID %d, value %u\n", __func__, div_id, div);
  459. return ret;
  460. }
  461. /*
  462. * @freq: when .set_pll() us not used, freq is codec MCLK input frequency
  463. */
  464. static int wm8978_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
  465. unsigned int freq, int dir)
  466. {
  467. struct snd_soc_codec *codec = codec_dai->codec;
  468. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  469. int ret = 0;
  470. dev_dbg(codec->dev, "%s: ID %d, freq %u\n", __func__, clk_id, freq);
  471. if (freq) {
  472. wm8978->f_mclk = freq;
  473. /* Even if MCLK is used for system clock, might have to drive OPCLK */
  474. if (wm8978->f_opclk)
  475. ret = wm8978_configure_pll(codec);
  476. /* Our sysclk is fixed to 256 * fs, will configure in .hw_params() */
  477. if (!ret)
  478. wm8978->sysclk = clk_id;
  479. }
  480. if (wm8978->sysclk == WM8978_PLL && (!freq || clk_id == WM8978_MCLK)) {
  481. /* Clock CODEC directly from MCLK */
  482. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
  483. /* GPIO1 into default mode as input - before configuring PLL */
  484. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
  485. /* Turn off PLL */
  486. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
  487. wm8978->sysclk = WM8978_MCLK;
  488. wm8978->f_pllout = 0;
  489. wm8978->f_opclk = 0;
  490. }
  491. return ret;
  492. }
  493. /*
  494. * Set ADC and Voice DAC format.
  495. */
  496. static int wm8978_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  497. {
  498. struct snd_soc_codec *codec = codec_dai->codec;
  499. /*
  500. * BCLK polarity mask = 0x100, LRC clock polarity mask = 0x80,
  501. * Data Format mask = 0x18: all will be calculated anew
  502. */
  503. u16 iface = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x198;
  504. u16 clk = snd_soc_read(codec, WM8978_CLOCKING);
  505. dev_dbg(codec->dev, "%s\n", __func__);
  506. /* set master/slave audio interface */
  507. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  508. case SND_SOC_DAIFMT_CBM_CFM:
  509. clk |= 1;
  510. break;
  511. case SND_SOC_DAIFMT_CBS_CFS:
  512. clk &= ~1;
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. /* interface format */
  518. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  519. case SND_SOC_DAIFMT_I2S:
  520. iface |= 0x10;
  521. break;
  522. case SND_SOC_DAIFMT_RIGHT_J:
  523. break;
  524. case SND_SOC_DAIFMT_LEFT_J:
  525. iface |= 0x8;
  526. break;
  527. case SND_SOC_DAIFMT_DSP_A:
  528. iface |= 0x18;
  529. break;
  530. default:
  531. return -EINVAL;
  532. }
  533. /* clock inversion */
  534. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  535. case SND_SOC_DAIFMT_NB_NF:
  536. break;
  537. case SND_SOC_DAIFMT_IB_IF:
  538. iface |= 0x180;
  539. break;
  540. case SND_SOC_DAIFMT_IB_NF:
  541. iface |= 0x100;
  542. break;
  543. case SND_SOC_DAIFMT_NB_IF:
  544. iface |= 0x80;
  545. break;
  546. default:
  547. return -EINVAL;
  548. }
  549. snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface);
  550. snd_soc_write(codec, WM8978_CLOCKING, clk);
  551. return 0;
  552. }
  553. /*
  554. * Set PCM DAI bit size and sample rate.
  555. */
  556. static int wm8978_hw_params(struct snd_pcm_substream *substream,
  557. struct snd_pcm_hw_params *params,
  558. struct snd_soc_dai *dai)
  559. {
  560. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  561. struct snd_soc_codec *codec = rtd->codec;
  562. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  563. /* Word length mask = 0x60 */
  564. u16 iface_ctl = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x60;
  565. /* Sampling rate mask = 0xe (for filters) */
  566. u16 add_ctl = snd_soc_read(codec, WM8978_ADDITIONAL_CONTROL) & ~0xe;
  567. u16 clking = snd_soc_read(codec, WM8978_CLOCKING);
  568. enum wm8978_sysclk_src current_clk_id = clking & 0x100 ?
  569. WM8978_PLL : WM8978_MCLK;
  570. unsigned int f_sel, diff, diff_best = INT_MAX;
  571. int i, best = 0;
  572. if (!wm8978->f_mclk)
  573. return -EINVAL;
  574. /* bit size */
  575. switch (params_format(params)) {
  576. case SNDRV_PCM_FORMAT_S16_LE:
  577. break;
  578. case SNDRV_PCM_FORMAT_S20_3LE:
  579. iface_ctl |= 0x20;
  580. break;
  581. case SNDRV_PCM_FORMAT_S24_LE:
  582. iface_ctl |= 0x40;
  583. break;
  584. case SNDRV_PCM_FORMAT_S32_LE:
  585. iface_ctl |= 0x60;
  586. break;
  587. }
  588. /* filter coefficient */
  589. switch (params_rate(params)) {
  590. case 8000:
  591. add_ctl |= 0x5 << 1;
  592. break;
  593. case 11025:
  594. add_ctl |= 0x4 << 1;
  595. break;
  596. case 16000:
  597. add_ctl |= 0x3 << 1;
  598. break;
  599. case 22050:
  600. add_ctl |= 0x2 << 1;
  601. break;
  602. case 32000:
  603. add_ctl |= 0x1 << 1;
  604. break;
  605. case 44100:
  606. case 48000:
  607. break;
  608. }
  609. /* Sampling rate is known now, can configure the MCLK divider */
  610. wm8978->f_256fs = params_rate(params) * 256;
  611. if (wm8978->sysclk == WM8978_MCLK) {
  612. wm8978->mclk_idx = -1;
  613. f_sel = wm8978->f_mclk;
  614. } else {
  615. if (!wm8978->f_pllout) {
  616. /* We only enter here, if OPCLK is not used */
  617. int ret = wm8978_configure_pll(codec);
  618. if (ret < 0)
  619. return ret;
  620. }
  621. f_sel = wm8978->f_pllout;
  622. }
  623. if (wm8978->mclk_idx < 0) {
  624. /* Either MCLK is used directly, or OPCLK is used */
  625. if (f_sel < wm8978->f_256fs || f_sel > 12 * wm8978->f_256fs)
  626. return -EINVAL;
  627. for (i = 0; i < ARRAY_SIZE(mclk_numerator); i++) {
  628. diff = abs(wm8978->f_256fs * 3 -
  629. f_sel * 3 * mclk_denominator[i] / mclk_numerator[i]);
  630. if (diff < diff_best) {
  631. diff_best = diff;
  632. best = i;
  633. }
  634. if (!diff)
  635. break;
  636. }
  637. } else {
  638. /* OPCLK not used, codec driven by PLL */
  639. best = wm8978->mclk_idx;
  640. diff = 0;
  641. }
  642. if (diff)
  643. dev_warn(codec->dev, "Imprecise sampling rate: %uHz%s\n",
  644. f_sel * mclk_denominator[best] / mclk_numerator[best] / 256,
  645. wm8978->sysclk == WM8978_MCLK ?
  646. ", consider using PLL" : "");
  647. dev_dbg(codec->dev, "%s: fmt %d, rate %u, MCLK divisor #%d\n", __func__,
  648. params_format(params), params_rate(params), best);
  649. /* MCLK divisor mask = 0xe0 */
  650. snd_soc_update_bits(codec, WM8978_CLOCKING, 0xe0, best << 5);
  651. snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface_ctl);
  652. snd_soc_write(codec, WM8978_ADDITIONAL_CONTROL, add_ctl);
  653. if (wm8978->sysclk != current_clk_id) {
  654. if (wm8978->sysclk == WM8978_PLL)
  655. /* Run CODEC from PLL instead of MCLK */
  656. snd_soc_update_bits(codec, WM8978_CLOCKING,
  657. 0x100, 0x100);
  658. else
  659. /* Clock CODEC directly from MCLK */
  660. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
  661. }
  662. return 0;
  663. }
  664. static int wm8978_mute(struct snd_soc_dai *dai, int mute)
  665. {
  666. struct snd_soc_codec *codec = dai->codec;
  667. dev_dbg(codec->dev, "%s: %d\n", __func__, mute);
  668. if (mute)
  669. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0x40);
  670. else
  671. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0);
  672. return 0;
  673. }
  674. static int wm8978_set_bias_level(struct snd_soc_codec *codec,
  675. enum snd_soc_bias_level level)
  676. {
  677. u16 power1 = snd_soc_read(codec, WM8978_POWER_MANAGEMENT_1) & ~3;
  678. switch (level) {
  679. case SND_SOC_BIAS_ON:
  680. case SND_SOC_BIAS_PREPARE:
  681. power1 |= 1; /* VMID 75k */
  682. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  683. break;
  684. case SND_SOC_BIAS_STANDBY:
  685. /* bit 3: enable bias, bit 2: enable I/O tie off buffer */
  686. power1 |= 0xc;
  687. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  688. /* Initial cap charge at VMID 5k */
  689. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1,
  690. power1 | 0x3);
  691. mdelay(100);
  692. }
  693. power1 |= 0x2; /* VMID 500k */
  694. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  695. break;
  696. case SND_SOC_BIAS_OFF:
  697. /* Preserve PLL - OPCLK may be used by someone */
  698. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, ~0x20, 0);
  699. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0);
  700. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_3, 0);
  701. break;
  702. }
  703. dev_dbg(codec->dev, "%s: %d, %x\n", __func__, level, power1);
  704. codec->dapm.bias_level = level;
  705. return 0;
  706. }
  707. #define WM8978_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  708. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  709. static const struct snd_soc_dai_ops wm8978_dai_ops = {
  710. .hw_params = wm8978_hw_params,
  711. .digital_mute = wm8978_mute,
  712. .set_fmt = wm8978_set_dai_fmt,
  713. .set_clkdiv = wm8978_set_dai_clkdiv,
  714. .set_sysclk = wm8978_set_dai_sysclk,
  715. };
  716. /* Also supports 12kHz */
  717. static struct snd_soc_dai_driver wm8978_dai = {
  718. .name = "wm8978-hifi",
  719. .playback = {
  720. .stream_name = "Playback",
  721. .channels_min = 1,
  722. .channels_max = 2,
  723. .rates = SNDRV_PCM_RATE_8000_48000,
  724. .formats = WM8978_FORMATS,
  725. },
  726. .capture = {
  727. .stream_name = "Capture",
  728. .channels_min = 1,
  729. .channels_max = 2,
  730. .rates = SNDRV_PCM_RATE_8000_48000,
  731. .formats = WM8978_FORMATS,
  732. },
  733. .ops = &wm8978_dai_ops,
  734. };
  735. static int wm8978_suspend(struct snd_soc_codec *codec)
  736. {
  737. wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
  738. /* Also switch PLL off */
  739. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, 0);
  740. return 0;
  741. }
  742. static int wm8978_resume(struct snd_soc_codec *codec)
  743. {
  744. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  745. /* Sync reg_cache with the hardware */
  746. snd_soc_cache_sync(codec);
  747. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  748. if (wm8978->f_pllout)
  749. /* Switch PLL on */
  750. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
  751. return 0;
  752. }
  753. /*
  754. * These registers contain an "update" bit - bit 8. This means, for example,
  755. * that one can write new DAC digital volume for both channels, but only when
  756. * the update bit is set, will also the volume be updated - simultaneously for
  757. * both channels.
  758. */
  759. static const int update_reg[] = {
  760. WM8978_LEFT_DAC_DIGITAL_VOLUME,
  761. WM8978_RIGHT_DAC_DIGITAL_VOLUME,
  762. WM8978_LEFT_ADC_DIGITAL_VOLUME,
  763. WM8978_RIGHT_ADC_DIGITAL_VOLUME,
  764. WM8978_LEFT_INP_PGA_CONTROL,
  765. WM8978_RIGHT_INP_PGA_CONTROL,
  766. WM8978_LOUT1_HP_CONTROL,
  767. WM8978_ROUT1_HP_CONTROL,
  768. WM8978_LOUT2_SPK_CONTROL,
  769. WM8978_ROUT2_SPK_CONTROL,
  770. };
  771. static int wm8978_probe(struct snd_soc_codec *codec)
  772. {
  773. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  774. int ret = 0, i;
  775. /*
  776. * Set default system clock to PLL, it is more precise, this is also the
  777. * default hardware setting
  778. */
  779. wm8978->sysclk = WM8978_PLL;
  780. ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C);
  781. if (ret < 0) {
  782. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  783. return ret;
  784. }
  785. /*
  786. * Set the update bit in all registers, that have one. This way all
  787. * writes to those registers will also cause the update bit to be
  788. * written.
  789. */
  790. for (i = 0; i < ARRAY_SIZE(update_reg); i++)
  791. snd_soc_update_bits(codec, update_reg[i], 0x100, 0x100);
  792. /* Reset the codec */
  793. ret = snd_soc_write(codec, WM8978_RESET, 0);
  794. if (ret < 0) {
  795. dev_err(codec->dev, "Failed to issue reset\n");
  796. return ret;
  797. }
  798. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  799. return 0;
  800. }
  801. /* power down chip */
  802. static int wm8978_remove(struct snd_soc_codec *codec)
  803. {
  804. wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
  805. return 0;
  806. }
  807. static struct snd_soc_codec_driver soc_codec_dev_wm8978 = {
  808. .probe = wm8978_probe,
  809. .remove = wm8978_remove,
  810. .suspend = wm8978_suspend,
  811. .resume = wm8978_resume,
  812. .set_bias_level = wm8978_set_bias_level,
  813. .reg_cache_size = ARRAY_SIZE(wm8978_reg),
  814. .reg_word_size = sizeof(u16),
  815. .reg_cache_default = wm8978_reg,
  816. .controls = wm8978_snd_controls,
  817. .num_controls = ARRAY_SIZE(wm8978_snd_controls),
  818. .dapm_widgets = wm8978_dapm_widgets,
  819. .num_dapm_widgets = ARRAY_SIZE(wm8978_dapm_widgets),
  820. .dapm_routes = wm8978_dapm_routes,
  821. .num_dapm_routes = ARRAY_SIZE(wm8978_dapm_routes),
  822. };
  823. static __devinit int wm8978_i2c_probe(struct i2c_client *i2c,
  824. const struct i2c_device_id *id)
  825. {
  826. struct wm8978_priv *wm8978;
  827. int ret;
  828. wm8978 = devm_kzalloc(&i2c->dev, sizeof(struct wm8978_priv),
  829. GFP_KERNEL);
  830. if (wm8978 == NULL)
  831. return -ENOMEM;
  832. i2c_set_clientdata(i2c, wm8978);
  833. ret = snd_soc_register_codec(&i2c->dev,
  834. &soc_codec_dev_wm8978, &wm8978_dai, 1);
  835. return ret;
  836. }
  837. static __devexit int wm8978_i2c_remove(struct i2c_client *client)
  838. {
  839. snd_soc_unregister_codec(&client->dev);
  840. return 0;
  841. }
  842. static const struct i2c_device_id wm8978_i2c_id[] = {
  843. { "wm8978", 0 },
  844. { }
  845. };
  846. MODULE_DEVICE_TABLE(i2c, wm8978_i2c_id);
  847. static struct i2c_driver wm8978_i2c_driver = {
  848. .driver = {
  849. .name = "wm8978",
  850. .owner = THIS_MODULE,
  851. },
  852. .probe = wm8978_i2c_probe,
  853. .remove = __devexit_p(wm8978_i2c_remove),
  854. .id_table = wm8978_i2c_id,
  855. };
  856. static int __init wm8978_modinit(void)
  857. {
  858. int ret = 0;
  859. ret = i2c_add_driver(&wm8978_i2c_driver);
  860. if (ret != 0) {
  861. printk(KERN_ERR "Failed to register WM8978 I2C driver: %d\n",
  862. ret);
  863. }
  864. return ret;
  865. }
  866. module_init(wm8978_modinit);
  867. static void __exit wm8978_exit(void)
  868. {
  869. i2c_del_driver(&wm8978_i2c_driver);
  870. }
  871. module_exit(wm8978_exit);
  872. MODULE_DESCRIPTION("ASoC WM8978 codec driver");
  873. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  874. MODULE_LICENSE("GPL");