wm8978.c 31 KB

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