wm8978.c 31 KB

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