wm8978.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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 <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <asm/div64.h>
  29. #include "wm8978.h"
  30. static struct snd_soc_codec *wm8978_codec;
  31. /* wm8978 register cache. Note that register 0 is not included in the cache. */
  32. static const u16 wm8978_reg[WM8978_CACHEREGNUM] = {
  33. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x00...0x03 */
  34. 0x0050, 0x0000, 0x0140, 0x0000, /* 0x04...0x07 */
  35. 0x0000, 0x0000, 0x0000, 0x00ff, /* 0x08...0x0b */
  36. 0x00ff, 0x0000, 0x0100, 0x00ff, /* 0x0c...0x0f */
  37. 0x00ff, 0x0000, 0x012c, 0x002c, /* 0x10...0x13 */
  38. 0x002c, 0x002c, 0x002c, 0x0000, /* 0x14...0x17 */
  39. 0x0032, 0x0000, 0x0000, 0x0000, /* 0x18...0x1b */
  40. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x1c...0x1f */
  41. 0x0038, 0x000b, 0x0032, 0x0000, /* 0x20...0x23 */
  42. 0x0008, 0x000c, 0x0093, 0x00e9, /* 0x24...0x27 */
  43. 0x0000, 0x0000, 0x0000, 0x0000, /* 0x28...0x2b */
  44. 0x0033, 0x0010, 0x0010, 0x0100, /* 0x2c...0x2f */
  45. 0x0100, 0x0002, 0x0001, 0x0001, /* 0x30...0x33 */
  46. 0x0039, 0x0039, 0x0039, 0x0039, /* 0x34...0x37 */
  47. 0x0001, 0x0001, /* 0x38...0x3b */
  48. };
  49. /* codec private data */
  50. struct wm8978_priv {
  51. struct snd_soc_codec codec;
  52. unsigned int f_pllout;
  53. unsigned int f_mclk;
  54. unsigned int f_256fs;
  55. unsigned int f_opclk;
  56. enum wm8978_sysclk_src sysclk;
  57. u16 reg_cache[WM8978_CACHEREGNUM];
  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 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("DAC Playback Limiter Boost",
  127. WM8978_DAC_LIMITER_2, 0, 15, 0),
  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, 7, 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, 15, 0),
  135. SOC_SINGLE("ALC Capture Attack", WM8978_ALC_CONTROL_3, 0, 15, 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, 8, 1, 0),
  179. SOC_SINGLE("ADC 128x Oversampling Switch", WM8978_ADC_CONTROL, 8, 1, 0),
  180. };
  181. /* Mixer #1: Output (OUT1, OUT2) Mixer: mix AUX, Input mixer output and DAC */
  182. static const struct snd_kcontrol_new wm8978_left_out_mixer[] = {
  183. SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_LEFT_MIXER_CONTROL, 1, 1, 0),
  184. SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_LEFT_MIXER_CONTROL, 5, 1, 0),
  185. SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_LEFT_MIXER_CONTROL, 0, 1, 0),
  186. };
  187. static const struct snd_kcontrol_new wm8978_right_out_mixer[] = {
  188. SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_RIGHT_MIXER_CONTROL, 1, 1, 0),
  189. SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 5, 1, 0),
  190. SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 0, 1, 0),
  191. };
  192. /* OUT3/OUT4 Mixer not implemented */
  193. /* Mixer #2: Input PGA Mute */
  194. static const struct snd_kcontrol_new wm8978_left_input_mixer[] = {
  195. SOC_DAPM_SINGLE("L2 Switch", WM8978_INPUT_CONTROL, 2, 1, 0),
  196. SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 1, 1, 0),
  197. SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 0, 1, 0),
  198. };
  199. static const struct snd_kcontrol_new wm8978_right_input_mixer[] = {
  200. SOC_DAPM_SINGLE("R2 Switch", WM8978_INPUT_CONTROL, 6, 1, 0),
  201. SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 5, 1, 0),
  202. SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 4, 1, 0),
  203. };
  204. static const struct snd_soc_dapm_widget wm8978_dapm_widgets[] = {
  205. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
  206. WM8978_POWER_MANAGEMENT_3, 0, 0),
  207. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
  208. WM8978_POWER_MANAGEMENT_3, 1, 0),
  209. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
  210. WM8978_POWER_MANAGEMENT_2, 0, 0),
  211. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
  212. WM8978_POWER_MANAGEMENT_2, 1, 0),
  213. /* Mixer #1: OUT1,2 */
  214. SOC_MIXER_ARRAY("Left Output Mixer", WM8978_POWER_MANAGEMENT_3,
  215. 2, 0, wm8978_left_out_mixer),
  216. SOC_MIXER_ARRAY("Right Output Mixer", WM8978_POWER_MANAGEMENT_3,
  217. 3, 0, wm8978_right_out_mixer),
  218. SOC_MIXER_ARRAY("Left Input Mixer", WM8978_POWER_MANAGEMENT_2,
  219. 2, 0, wm8978_left_input_mixer),
  220. SOC_MIXER_ARRAY("Right Input Mixer", WM8978_POWER_MANAGEMENT_2,
  221. 3, 0, wm8978_right_input_mixer),
  222. SND_SOC_DAPM_PGA("Left Boost Mixer", WM8978_POWER_MANAGEMENT_2,
  223. 4, 0, NULL, 0),
  224. SND_SOC_DAPM_PGA("Right Boost Mixer", WM8978_POWER_MANAGEMENT_2,
  225. 5, 0, NULL, 0),
  226. SND_SOC_DAPM_PGA("Left Capture PGA", WM8978_LEFT_INP_PGA_CONTROL,
  227. 6, 1, NULL, 0),
  228. SND_SOC_DAPM_PGA("Right Capture PGA", WM8978_RIGHT_INP_PGA_CONTROL,
  229. 6, 1, NULL, 0),
  230. SND_SOC_DAPM_PGA("Left Headphone Out", WM8978_POWER_MANAGEMENT_2,
  231. 7, 0, NULL, 0),
  232. SND_SOC_DAPM_PGA("Right Headphone Out", WM8978_POWER_MANAGEMENT_2,
  233. 8, 0, NULL, 0),
  234. SND_SOC_DAPM_PGA("Left Speaker Out", WM8978_POWER_MANAGEMENT_3,
  235. 6, 0, NULL, 0),
  236. SND_SOC_DAPM_PGA("Right Speaker Out", WM8978_POWER_MANAGEMENT_3,
  237. 5, 0, NULL, 0),
  238. SND_SOC_DAPM_MIXER("OUT4 VMID", WM8978_POWER_MANAGEMENT_3,
  239. 8, 0, NULL, 0),
  240. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8978_POWER_MANAGEMENT_1, 4, 0),
  241. SND_SOC_DAPM_INPUT("LMICN"),
  242. SND_SOC_DAPM_INPUT("LMICP"),
  243. SND_SOC_DAPM_INPUT("RMICN"),
  244. SND_SOC_DAPM_INPUT("RMICP"),
  245. SND_SOC_DAPM_INPUT("LAUX"),
  246. SND_SOC_DAPM_INPUT("RAUX"),
  247. SND_SOC_DAPM_INPUT("L2"),
  248. SND_SOC_DAPM_INPUT("R2"),
  249. SND_SOC_DAPM_OUTPUT("LHP"),
  250. SND_SOC_DAPM_OUTPUT("RHP"),
  251. SND_SOC_DAPM_OUTPUT("LSPK"),
  252. SND_SOC_DAPM_OUTPUT("RSPK"),
  253. };
  254. static const struct snd_soc_dapm_route audio_map[] = {
  255. /* Output mixer */
  256. {"Right Output Mixer", "PCM Playback Switch", "Right DAC"},
  257. {"Right Output Mixer", "Aux Playback Switch", "RAUX"},
  258. {"Right Output Mixer", "Line Bypass Switch", "Right Boost Mixer"},
  259. {"Left Output Mixer", "PCM Playback Switch", "Left DAC"},
  260. {"Left Output Mixer", "Aux Playback Switch", "LAUX"},
  261. {"Left Output Mixer", "Line Bypass Switch", "Left Boost Mixer"},
  262. /* Outputs */
  263. {"Right Headphone Out", NULL, "Right Output Mixer"},
  264. {"RHP", NULL, "Right Headphone Out"},
  265. {"Left Headphone Out", NULL, "Left Output Mixer"},
  266. {"LHP", NULL, "Left Headphone Out"},
  267. {"Right Speaker Out", NULL, "Right Output Mixer"},
  268. {"RSPK", NULL, "Right Speaker Out"},
  269. {"Left Speaker Out", NULL, "Left Output Mixer"},
  270. {"LSPK", NULL, "Left Speaker Out"},
  271. /* Boost Mixer */
  272. {"Right ADC", NULL, "Right Boost Mixer"},
  273. {"Right Boost Mixer", NULL, "RAUX"},
  274. {"Right Boost Mixer", NULL, "Right Capture PGA"},
  275. {"Right Boost Mixer", NULL, "R2"},
  276. {"Left ADC", NULL, "Left Boost Mixer"},
  277. {"Left Boost Mixer", NULL, "LAUX"},
  278. {"Left Boost Mixer", NULL, "Left Capture PGA"},
  279. {"Left Boost Mixer", NULL, "L2"},
  280. /* Input PGA */
  281. {"Right Capture PGA", NULL, "Right Input Mixer"},
  282. {"Left Capture PGA", NULL, "Left Input Mixer"},
  283. {"Right Input Mixer", "R2 Switch", "R2"},
  284. {"Right Input Mixer", "MicN Switch", "RMICN"},
  285. {"Right Input Mixer", "MicP Switch", "RMICP"},
  286. {"Left Input Mixer", "L2 Switch", "L2"},
  287. {"Left Input Mixer", "MicN Switch", "LMICN"},
  288. {"Left Input Mixer", "MicP Switch", "LMICP"},
  289. };
  290. static int wm8978_add_widgets(struct snd_soc_codec *codec)
  291. {
  292. snd_soc_dapm_new_controls(codec, wm8978_dapm_widgets,
  293. ARRAY_SIZE(wm8978_dapm_widgets));
  294. /* set up the WM8978 audio map */
  295. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  296. return 0;
  297. }
  298. /* PLL divisors */
  299. struct wm8978_pll_div {
  300. u32 k;
  301. u8 n;
  302. u8 div2;
  303. };
  304. #define FIXED_PLL_SIZE (1 << 24)
  305. static void pll_factors(struct wm8978_pll_div *pll_div, unsigned int target,
  306. unsigned int source)
  307. {
  308. u64 k_part;
  309. unsigned int k, n_div, n_mod;
  310. n_div = target / source;
  311. if (n_div < 6) {
  312. source >>= 1;
  313. pll_div->div2 = 1;
  314. n_div = target / source;
  315. } else {
  316. pll_div->div2 = 0;
  317. }
  318. if (n_div < 6 || n_div > 12)
  319. dev_warn(wm8978_codec->dev,
  320. "WM8978 N value exceeds recommended range! N = %u\n",
  321. n_div);
  322. pll_div->n = n_div;
  323. n_mod = target - source * n_div;
  324. k_part = FIXED_PLL_SIZE * (long long)n_mod + source / 2;
  325. do_div(k_part, source);
  326. k = k_part & 0xFFFFFFFF;
  327. pll_div->k = k;
  328. }
  329. /*
  330. * Calculate internal frequencies and dividers, according to Figure 40
  331. * "PLL and Clock Select Circuit" in WM8978 datasheet Rev. 2.6
  332. */
  333. static int wm8978_configure_pll(struct snd_soc_codec *codec)
  334. {
  335. struct wm8978_priv *wm8978 = codec->private_data;
  336. struct wm8978_pll_div pll_div;
  337. unsigned int f_opclk = wm8978->f_opclk, f_mclk = wm8978->f_mclk,
  338. f_256fs = wm8978->f_256fs;
  339. unsigned int f2, opclk_div;
  340. if (!f_mclk)
  341. return -EINVAL;
  342. if (f_opclk) {
  343. /*
  344. * The user needs OPCLK. Choose OPCLKDIV to put
  345. * 6 <= R = f2 / f1 < 13, 1 <= OPCLKDIV <= 4.
  346. * f_opclk = f_mclk * prescale * R / 4 / OPCLKDIV, where
  347. * prescale = 1, or prescale = 2. Prescale is calculated inside
  348. * pll_factors(). We have to select f_PLLOUT, such that
  349. * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
  350. * f_mclk * 3 / 16 <= f_opclk < f_mclk * 13 / 4.
  351. */
  352. if (16 * f_opclk < 3 * f_mclk || 4 * f_opclk >= 13 * f_mclk)
  353. return -EINVAL;
  354. if (4 * f_opclk < 3 * f_mclk)
  355. /* Have to use OPCLKDIV */
  356. opclk_div = (3 * f_mclk / 4 + f_opclk - 1) / f_opclk;
  357. else
  358. opclk_div = 1;
  359. dev_dbg(codec->dev, "%s: OPCLKDIV=%d\n", __func__, opclk_div);
  360. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 0x30,
  361. (opclk_div - 1) << 4);
  362. wm8978->f_pllout = f_opclk * opclk_div;
  363. } else if (f_256fs) {
  364. /*
  365. * Not using OPCLK, choose R:
  366. * 6 <= R = f2 / f1 < 13, to put 1 <= MCLKDIV <= 12.
  367. * f_256fs = f_mclk * prescale * R / 4 / MCLKDIV, where
  368. * prescale = 1, or prescale = 2. Prescale is calculated inside
  369. * pll_factors(). We have to select f_PLLOUT, such that
  370. * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
  371. * f_mclk * 3 / 48 <= f_256fs < f_mclk * 13 / 4. This means MCLK
  372. * must be 3.781MHz <= f_MCLK <= 32.768MHz
  373. */
  374. if (48 * f_256fs < 3 * f_mclk || 4 * f_256fs >= 13 * f_mclk)
  375. return -EINVAL;
  376. /*
  377. * MCLKDIV will be selected in .hw_params(), just choose a
  378. * suitable f_PLLOUT
  379. */
  380. if (4 * f_256fs < 3 * f_mclk)
  381. /* Will have to use MCLKDIV */
  382. wm8978->f_pllout = wm8978->f_mclk * 3 / 4;
  383. else
  384. wm8978->f_pllout = f_256fs;
  385. /* GPIO1 into default mode as input - before configuring PLL */
  386. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
  387. } else {
  388. return -EINVAL;
  389. }
  390. f2 = wm8978->f_pllout * 4;
  391. dev_dbg(codec->dev, "%s: f_MCLK=%uHz, f_PLLOUT=%uHz\n", __func__,
  392. wm8978->f_mclk, wm8978->f_pllout);
  393. pll_factors(&pll_div, f2, wm8978->f_mclk);
  394. dev_dbg(codec->dev, "%s: calculated PLL N=0x%x, K=0x%x, div2=%d\n",
  395. __func__, pll_div.n, pll_div.k, pll_div.div2);
  396. /* Turn PLL off for configuration... */
  397. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
  398. snd_soc_write(codec, WM8978_PLL_N, (pll_div.div2 << 4) | pll_div.n);
  399. snd_soc_write(codec, WM8978_PLL_K1, pll_div.k >> 18);
  400. snd_soc_write(codec, WM8978_PLL_K2, (pll_div.k >> 9) & 0x1ff);
  401. snd_soc_write(codec, WM8978_PLL_K3, pll_div.k & 0x1ff);
  402. /* ...and on again */
  403. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
  404. if (f_opclk)
  405. /* Output PLL (OPCLK) to GPIO1 */
  406. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 4);
  407. return 0;
  408. }
  409. /*
  410. * Configure WM8978 clock dividers.
  411. */
  412. static int wm8978_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  413. int div_id, int div)
  414. {
  415. struct snd_soc_codec *codec = codec_dai->codec;
  416. struct wm8978_priv *wm8978 = codec->private_data;
  417. int ret = 0;
  418. switch (div_id) {
  419. case WM8978_OPCLKRATE:
  420. wm8978->f_opclk = div;
  421. if (wm8978->f_mclk)
  422. ret = wm8978_configure_pll(codec);
  423. break;
  424. case WM8978_BCLKDIV:
  425. if (div & ~0x1c)
  426. return -EINVAL;
  427. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x1c, div);
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. dev_dbg(codec->dev, "%s: ID %d, value %u\n", __func__, div_id, div);
  433. return ret;
  434. }
  435. /*
  436. * @freq: when .set_pll() us not used, freq is codec MCLK input frequency
  437. */
  438. static int wm8978_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
  439. unsigned int freq, int dir)
  440. {
  441. struct snd_soc_codec *codec = codec_dai->codec;
  442. struct wm8978_priv *wm8978 = codec->private_data;
  443. int ret = 0;
  444. dev_dbg(codec->dev, "%s: ID %d, freq %u\n", __func__, clk_id, freq);
  445. if (freq) {
  446. wm8978->f_mclk = freq;
  447. /* Even if MCLK is used for system clock, might have to drive OPCLK */
  448. if (wm8978->f_opclk)
  449. ret = wm8978_configure_pll(codec);
  450. /* Our sysclk is fixed to 256 * fs, will configure in .hw_params() */
  451. if (!ret)
  452. wm8978->sysclk = clk_id;
  453. }
  454. if (wm8978->sysclk == WM8978_PLL && (!freq || clk_id == WM8978_MCLK)) {
  455. /* Clock CODEC directly from MCLK */
  456. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
  457. /* GPIO1 into default mode as input - before configuring PLL */
  458. snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
  459. /* Turn off PLL */
  460. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
  461. wm8978->sysclk = WM8978_MCLK;
  462. wm8978->f_pllout = 0;
  463. wm8978->f_opclk = 0;
  464. }
  465. return ret;
  466. }
  467. /*
  468. * Set ADC and Voice DAC format.
  469. */
  470. static int wm8978_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  471. {
  472. struct snd_soc_codec *codec = codec_dai->codec;
  473. /*
  474. * BCLK polarity mask = 0x100, LRC clock polarity mask = 0x80,
  475. * Data Format mask = 0x18: all will be calculated anew
  476. */
  477. u16 iface = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x198;
  478. u16 clk = snd_soc_read(codec, WM8978_CLOCKING);
  479. dev_dbg(codec->dev, "%s\n", __func__);
  480. /* set master/slave audio interface */
  481. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  482. case SND_SOC_DAIFMT_CBM_CFM:
  483. clk |= 1;
  484. break;
  485. case SND_SOC_DAIFMT_CBS_CFS:
  486. clk &= ~1;
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. /* interface format */
  492. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  493. case SND_SOC_DAIFMT_I2S:
  494. iface |= 0x10;
  495. break;
  496. case SND_SOC_DAIFMT_RIGHT_J:
  497. break;
  498. case SND_SOC_DAIFMT_LEFT_J:
  499. iface |= 0x8;
  500. break;
  501. case SND_SOC_DAIFMT_DSP_A:
  502. iface |= 0x18;
  503. break;
  504. default:
  505. return -EINVAL;
  506. }
  507. /* clock inversion */
  508. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  509. case SND_SOC_DAIFMT_NB_NF:
  510. break;
  511. case SND_SOC_DAIFMT_IB_IF:
  512. iface |= 0x180;
  513. break;
  514. case SND_SOC_DAIFMT_IB_NF:
  515. iface |= 0x100;
  516. break;
  517. case SND_SOC_DAIFMT_NB_IF:
  518. iface |= 0x80;
  519. break;
  520. default:
  521. return -EINVAL;
  522. }
  523. snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface);
  524. snd_soc_write(codec, WM8978_CLOCKING, clk);
  525. return 0;
  526. }
  527. /* MCLK dividers */
  528. static const int mclk_numerator[] = {1, 3, 2, 3, 4, 6, 8, 12};
  529. static const int mclk_denominator[] = {1, 2, 1, 1, 1, 1, 1, 1};
  530. /*
  531. * Set PCM DAI bit size and sample rate.
  532. */
  533. static int wm8978_hw_params(struct snd_pcm_substream *substream,
  534. struct snd_pcm_hw_params *params,
  535. struct snd_soc_dai *dai)
  536. {
  537. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  538. struct snd_soc_device *socdev = rtd->socdev;
  539. struct snd_soc_codec *codec = socdev->card->codec;
  540. struct wm8978_priv *wm8978 = codec->private_data;
  541. /* Word length mask = 0x60 */
  542. u16 iface_ctl = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x60;
  543. /* Sampling rate mask = 0xe (for filters) */
  544. u16 add_ctl = snd_soc_read(codec, WM8978_ADDITIONAL_CONTROL) & ~0xe;
  545. u16 clking = snd_soc_read(codec, WM8978_CLOCKING);
  546. enum wm8978_sysclk_src current_clk_id = clking & 0x100 ?
  547. WM8978_PLL : WM8978_MCLK;
  548. unsigned int f_sel, diff, diff_best = INT_MAX;
  549. int i, best = 0;
  550. if (!wm8978->f_mclk)
  551. return -EINVAL;
  552. /* bit size */
  553. switch (params_format(params)) {
  554. case SNDRV_PCM_FORMAT_S16_LE:
  555. break;
  556. case SNDRV_PCM_FORMAT_S20_3LE:
  557. iface_ctl |= 0x20;
  558. break;
  559. case SNDRV_PCM_FORMAT_S24_LE:
  560. iface_ctl |= 0x40;
  561. break;
  562. case SNDRV_PCM_FORMAT_S32_LE:
  563. iface_ctl |= 0x60;
  564. break;
  565. }
  566. /* filter coefficient */
  567. switch (params_rate(params)) {
  568. case 8000:
  569. add_ctl |= 0x5 << 1;
  570. break;
  571. case 11025:
  572. add_ctl |= 0x4 << 1;
  573. break;
  574. case 16000:
  575. add_ctl |= 0x3 << 1;
  576. break;
  577. case 22050:
  578. add_ctl |= 0x2 << 1;
  579. break;
  580. case 32000:
  581. add_ctl |= 0x1 << 1;
  582. break;
  583. case 44100:
  584. case 48000:
  585. break;
  586. }
  587. /* Sampling rate is known now, can configure the MCLK divider */
  588. wm8978->f_256fs = params_rate(params) * 256;
  589. if (wm8978->sysclk == WM8978_MCLK) {
  590. f_sel = wm8978->f_mclk;
  591. } else {
  592. if (!wm8978->f_pllout) {
  593. int ret = wm8978_configure_pll(codec);
  594. if (ret < 0)
  595. return ret;
  596. }
  597. f_sel = wm8978->f_pllout;
  598. }
  599. /*
  600. * In some cases it is possible to reconfigure PLL to a higher frequency
  601. * by raising OPCLKDIV, but normally OPCLK is configured to 256 * fs or
  602. * 512 * fs, so, we should be fine.
  603. */
  604. if (f_sel < wm8978->f_256fs || f_sel > 12 * wm8978->f_256fs)
  605. return -EINVAL;
  606. for (i = 0; i < ARRAY_SIZE(mclk_numerator); i++) {
  607. diff = abs(wm8978->f_256fs * 3 -
  608. f_sel * 3 * mclk_denominator[i] / mclk_numerator[i]);
  609. if (diff < diff_best) {
  610. diff_best = diff;
  611. best = i;
  612. }
  613. if (!diff)
  614. break;
  615. }
  616. if (diff)
  617. dev_warn(codec->dev, "Imprecise clock: %u%s\n",
  618. f_sel * mclk_denominator[best] / mclk_numerator[best],
  619. wm8978->sysclk == WM8978_MCLK ?
  620. ", consider using PLL" : "");
  621. dev_dbg(codec->dev, "%s: fmt %d, rate %u, MCLK divisor #%d\n", __func__,
  622. params_format(params), params_rate(params), best);
  623. /* MCLK divisor mask = 0xe0 */
  624. snd_soc_update_bits(codec, WM8978_CLOCKING, 0xe0, best << 5);
  625. snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface_ctl);
  626. snd_soc_write(codec, WM8978_ADDITIONAL_CONTROL, add_ctl);
  627. if (wm8978->sysclk != current_clk_id) {
  628. if (wm8978->sysclk == WM8978_PLL)
  629. /* Run CODEC from PLL instead of MCLK */
  630. snd_soc_update_bits(codec, WM8978_CLOCKING,
  631. 0x100, 0x100);
  632. else
  633. /* Clock CODEC directly from MCLK */
  634. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
  635. }
  636. return 0;
  637. }
  638. static int wm8978_mute(struct snd_soc_dai *dai, int mute)
  639. {
  640. struct snd_soc_codec *codec = dai->codec;
  641. dev_dbg(codec->dev, "%s: %d\n", __func__, mute);
  642. if (mute)
  643. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0x40);
  644. else
  645. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0);
  646. return 0;
  647. }
  648. static int wm8978_set_bias_level(struct snd_soc_codec *codec,
  649. enum snd_soc_bias_level level)
  650. {
  651. u16 power1 = snd_soc_read(codec, WM8978_POWER_MANAGEMENT_1) & ~3;
  652. switch (level) {
  653. case SND_SOC_BIAS_ON:
  654. case SND_SOC_BIAS_PREPARE:
  655. power1 |= 1; /* VMID 75k */
  656. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  657. break;
  658. case SND_SOC_BIAS_STANDBY:
  659. /* bit 3: enable bias, bit 2: enable I/O tie off buffer */
  660. power1 |= 0xc;
  661. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  662. /* Initial cap charge at VMID 5k */
  663. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1,
  664. power1 | 0x3);
  665. mdelay(100);
  666. }
  667. power1 |= 0x2; /* VMID 500k */
  668. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  669. break;
  670. case SND_SOC_BIAS_OFF:
  671. /* Preserve PLL - OPCLK may be used by someone */
  672. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, ~0x20, 0);
  673. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0);
  674. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_3, 0);
  675. break;
  676. }
  677. dev_dbg(codec->dev, "%s: %d, %x\n", __func__, level, power1);
  678. codec->bias_level = level;
  679. return 0;
  680. }
  681. #define WM8978_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  682. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  683. static struct snd_soc_dai_ops wm8978_dai_ops = {
  684. .hw_params = wm8978_hw_params,
  685. .digital_mute = wm8978_mute,
  686. .set_fmt = wm8978_set_dai_fmt,
  687. .set_clkdiv = wm8978_set_dai_clkdiv,
  688. .set_sysclk = wm8978_set_dai_sysclk,
  689. };
  690. /* Also supports 12kHz */
  691. struct snd_soc_dai wm8978_dai = {
  692. .name = "WM8978 HiFi",
  693. .id = 1,
  694. .playback = {
  695. .stream_name = "Playback",
  696. .channels_min = 1,
  697. .channels_max = 2,
  698. .rates = SNDRV_PCM_RATE_8000_48000,
  699. .formats = WM8978_FORMATS,
  700. },
  701. .capture = {
  702. .stream_name = "Capture",
  703. .channels_min = 1,
  704. .channels_max = 2,
  705. .rates = SNDRV_PCM_RATE_8000_48000,
  706. .formats = WM8978_FORMATS,
  707. },
  708. .ops = &wm8978_dai_ops,
  709. };
  710. EXPORT_SYMBOL_GPL(wm8978_dai);
  711. static int wm8978_suspend(struct platform_device *pdev, pm_message_t state)
  712. {
  713. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  714. struct snd_soc_codec *codec = socdev->card->codec;
  715. wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
  716. /* Also switch PLL off */
  717. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, 0);
  718. return 0;
  719. }
  720. static int wm8978_resume(struct platform_device *pdev)
  721. {
  722. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  723. struct snd_soc_codec *codec = socdev->card->codec;
  724. struct wm8978_priv *wm8978 = codec->private_data;
  725. int i;
  726. u16 *cache = codec->reg_cache;
  727. /* Sync reg_cache with the hardware */
  728. for (i = 0; i < ARRAY_SIZE(wm8978_reg); i++) {
  729. if (i == WM8978_RESET)
  730. continue;
  731. if (cache[i] != wm8978_reg[i])
  732. snd_soc_write(codec, i, cache[i]);
  733. }
  734. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  735. if (wm8978->f_pllout)
  736. /* Switch PLL on */
  737. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
  738. return 0;
  739. }
  740. static int wm8978_probe(struct platform_device *pdev)
  741. {
  742. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  743. struct snd_soc_codec *codec;
  744. int ret = 0;
  745. if (wm8978_codec == NULL) {
  746. dev_err(&pdev->dev, "Codec device not registered\n");
  747. return -ENODEV;
  748. }
  749. socdev->card->codec = wm8978_codec;
  750. codec = wm8978_codec;
  751. /* register pcms */
  752. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  753. if (ret < 0) {
  754. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  755. goto pcm_err;
  756. }
  757. snd_soc_add_controls(codec, wm8978_snd_controls,
  758. ARRAY_SIZE(wm8978_snd_controls));
  759. wm8978_add_widgets(codec);
  760. pcm_err:
  761. return ret;
  762. }
  763. /* power down chip */
  764. static int wm8978_remove(struct platform_device *pdev)
  765. {
  766. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  767. snd_soc_free_pcms(socdev);
  768. snd_soc_dapm_free(socdev);
  769. return 0;
  770. }
  771. struct snd_soc_codec_device soc_codec_dev_wm8978 = {
  772. .probe = wm8978_probe,
  773. .remove = wm8978_remove,
  774. .suspend = wm8978_suspend,
  775. .resume = wm8978_resume,
  776. };
  777. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8978);
  778. /*
  779. * These registers contain an "update" bit - bit 8. This means, for example,
  780. * that one can write new DAC digital volume for both channels, but only when
  781. * the update bit is set, will also the volume be updated - simultaneously for
  782. * both channels.
  783. */
  784. static const int update_reg[] = {
  785. WM8978_LEFT_DAC_DIGITAL_VOLUME,
  786. WM8978_RIGHT_DAC_DIGITAL_VOLUME,
  787. WM8978_LEFT_ADC_DIGITAL_VOLUME,
  788. WM8978_RIGHT_ADC_DIGITAL_VOLUME,
  789. WM8978_LEFT_INP_PGA_CONTROL,
  790. WM8978_RIGHT_INP_PGA_CONTROL,
  791. WM8978_LOUT1_HP_CONTROL,
  792. WM8978_ROUT1_HP_CONTROL,
  793. WM8978_LOUT2_SPK_CONTROL,
  794. WM8978_ROUT2_SPK_CONTROL,
  795. };
  796. static __devinit int wm8978_register(struct wm8978_priv *wm8978)
  797. {
  798. int ret, i;
  799. struct snd_soc_codec *codec = &wm8978->codec;
  800. if (wm8978_codec) {
  801. dev_err(codec->dev, "Another WM8978 is registered\n");
  802. return -EINVAL;
  803. }
  804. /*
  805. * Set default system clock to PLL, it is more precise, this is also the
  806. * default hardware setting
  807. */
  808. wm8978->sysclk = WM8978_PLL;
  809. mutex_init(&codec->mutex);
  810. INIT_LIST_HEAD(&codec->dapm_widgets);
  811. INIT_LIST_HEAD(&codec->dapm_paths);
  812. codec->private_data = wm8978;
  813. codec->name = "WM8978";
  814. codec->owner = THIS_MODULE;
  815. codec->bias_level = SND_SOC_BIAS_OFF;
  816. codec->set_bias_level = wm8978_set_bias_level;
  817. codec->dai = &wm8978_dai;
  818. codec->num_dai = 1;
  819. codec->reg_cache_size = WM8978_CACHEREGNUM;
  820. codec->reg_cache = &wm8978->reg_cache;
  821. ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C);
  822. if (ret < 0) {
  823. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  824. goto err;
  825. }
  826. memcpy(codec->reg_cache, wm8978_reg, sizeof(wm8978_reg));
  827. /*
  828. * Set the update bit in all registers, that have one. This way all
  829. * writes to those registers will also cause the update bit to be
  830. * written.
  831. */
  832. for (i = 0; i < ARRAY_SIZE(update_reg); i++)
  833. ((u16 *)codec->reg_cache)[update_reg[i]] |= 0x100;
  834. /* Reset the codec */
  835. ret = snd_soc_write(codec, WM8978_RESET, 0);
  836. if (ret < 0) {
  837. dev_err(codec->dev, "Failed to issue reset\n");
  838. goto err;
  839. }
  840. wm8978_dai.dev = codec->dev;
  841. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  842. wm8978_codec = codec;
  843. ret = snd_soc_register_codec(codec);
  844. if (ret != 0) {
  845. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  846. goto err;
  847. }
  848. ret = snd_soc_register_dai(&wm8978_dai);
  849. if (ret != 0) {
  850. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  851. goto err_codec;
  852. }
  853. return 0;
  854. err_codec:
  855. snd_soc_unregister_codec(codec);
  856. err:
  857. kfree(wm8978);
  858. return ret;
  859. }
  860. static __devexit void wm8978_unregister(struct wm8978_priv *wm8978)
  861. {
  862. wm8978_set_bias_level(&wm8978->codec, SND_SOC_BIAS_OFF);
  863. snd_soc_unregister_dai(&wm8978_dai);
  864. snd_soc_unregister_codec(&wm8978->codec);
  865. kfree(wm8978);
  866. wm8978_codec = NULL;
  867. }
  868. static __devinit int wm8978_i2c_probe(struct i2c_client *i2c,
  869. const struct i2c_device_id *id)
  870. {
  871. struct wm8978_priv *wm8978;
  872. struct snd_soc_codec *codec;
  873. wm8978 = kzalloc(sizeof(struct wm8978_priv), GFP_KERNEL);
  874. if (wm8978 == NULL)
  875. return -ENOMEM;
  876. codec = &wm8978->codec;
  877. codec->hw_write = (hw_write_t)i2c_master_send;
  878. i2c_set_clientdata(i2c, wm8978);
  879. codec->control_data = i2c;
  880. codec->dev = &i2c->dev;
  881. return wm8978_register(wm8978);
  882. }
  883. static __devexit int wm8978_i2c_remove(struct i2c_client *client)
  884. {
  885. struct wm8978_priv *wm8978 = i2c_get_clientdata(client);
  886. wm8978_unregister(wm8978);
  887. return 0;
  888. }
  889. static const struct i2c_device_id wm8978_i2c_id[] = {
  890. { "wm8978", 0 },
  891. { }
  892. };
  893. MODULE_DEVICE_TABLE(i2c, wm8978_i2c_id);
  894. static struct i2c_driver wm8978_i2c_driver = {
  895. .driver = {
  896. .name = "WM8978",
  897. .owner = THIS_MODULE,
  898. },
  899. .probe = wm8978_i2c_probe,
  900. .remove = __devexit_p(wm8978_i2c_remove),
  901. .id_table = wm8978_i2c_id,
  902. };
  903. static int __init wm8978_modinit(void)
  904. {
  905. return i2c_add_driver(&wm8978_i2c_driver);
  906. }
  907. module_init(wm8978_modinit);
  908. static void __exit wm8978_exit(void)
  909. {
  910. i2c_del_driver(&wm8978_i2c_driver);
  911. }
  912. module_exit(wm8978_exit);
  913. MODULE_DESCRIPTION("ASoC WM8978 codec driver");
  914. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  915. MODULE_LICENSE("GPL");