wm8978.c 31 KB

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