wm8978.c 31 KB

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