wm8978.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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_codec *codec = dai->codec;
  608. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  609. /* Word length mask = 0x60 */
  610. u16 iface_ctl = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x60;
  611. /* Sampling rate mask = 0xe (for filters) */
  612. u16 add_ctl = snd_soc_read(codec, WM8978_ADDITIONAL_CONTROL) & ~0xe;
  613. u16 clking = snd_soc_read(codec, WM8978_CLOCKING);
  614. enum wm8978_sysclk_src current_clk_id = clking & 0x100 ?
  615. WM8978_PLL : WM8978_MCLK;
  616. unsigned int f_sel, diff, diff_best = INT_MAX;
  617. int i, best = 0;
  618. if (!wm8978->f_mclk)
  619. return -EINVAL;
  620. /* bit size */
  621. switch (params_format(params)) {
  622. case SNDRV_PCM_FORMAT_S16_LE:
  623. break;
  624. case SNDRV_PCM_FORMAT_S20_3LE:
  625. iface_ctl |= 0x20;
  626. break;
  627. case SNDRV_PCM_FORMAT_S24_LE:
  628. iface_ctl |= 0x40;
  629. break;
  630. case SNDRV_PCM_FORMAT_S32_LE:
  631. iface_ctl |= 0x60;
  632. break;
  633. }
  634. /* filter coefficient */
  635. switch (params_rate(params)) {
  636. case 8000:
  637. add_ctl |= 0x5 << 1;
  638. break;
  639. case 11025:
  640. add_ctl |= 0x4 << 1;
  641. break;
  642. case 16000:
  643. add_ctl |= 0x3 << 1;
  644. break;
  645. case 22050:
  646. add_ctl |= 0x2 << 1;
  647. break;
  648. case 32000:
  649. add_ctl |= 0x1 << 1;
  650. break;
  651. case 44100:
  652. case 48000:
  653. break;
  654. }
  655. /* Sampling rate is known now, can configure the MCLK divider */
  656. wm8978->f_256fs = params_rate(params) * 256;
  657. if (wm8978->sysclk == WM8978_MCLK) {
  658. wm8978->mclk_idx = -1;
  659. f_sel = wm8978->f_mclk;
  660. } else {
  661. if (!wm8978->f_pllout) {
  662. /* We only enter here, if OPCLK is not used */
  663. int ret = wm8978_configure_pll(codec);
  664. if (ret < 0)
  665. return ret;
  666. }
  667. f_sel = wm8978->f_pllout;
  668. }
  669. if (wm8978->mclk_idx < 0) {
  670. /* Either MCLK is used directly, or OPCLK is used */
  671. if (f_sel < wm8978->f_256fs || f_sel > 12 * wm8978->f_256fs)
  672. return -EINVAL;
  673. for (i = 0; i < ARRAY_SIZE(mclk_numerator); i++) {
  674. diff = abs(wm8978->f_256fs * 3 -
  675. f_sel * 3 * mclk_denominator[i] / mclk_numerator[i]);
  676. if (diff < diff_best) {
  677. diff_best = diff;
  678. best = i;
  679. }
  680. if (!diff)
  681. break;
  682. }
  683. } else {
  684. /* OPCLK not used, codec driven by PLL */
  685. best = wm8978->mclk_idx;
  686. diff = 0;
  687. }
  688. if (diff)
  689. dev_warn(codec->dev, "Imprecise sampling rate: %uHz%s\n",
  690. f_sel * mclk_denominator[best] / mclk_numerator[best] / 256,
  691. wm8978->sysclk == WM8978_MCLK ?
  692. ", consider using PLL" : "");
  693. dev_dbg(codec->dev, "%s: fmt %d, rate %u, MCLK divisor #%d\n", __func__,
  694. params_format(params), params_rate(params), best);
  695. /* MCLK divisor mask = 0xe0 */
  696. snd_soc_update_bits(codec, WM8978_CLOCKING, 0xe0, best << 5);
  697. snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface_ctl);
  698. snd_soc_write(codec, WM8978_ADDITIONAL_CONTROL, add_ctl);
  699. if (wm8978->sysclk != current_clk_id) {
  700. if (wm8978->sysclk == WM8978_PLL)
  701. /* Run CODEC from PLL instead of MCLK */
  702. snd_soc_update_bits(codec, WM8978_CLOCKING,
  703. 0x100, 0x100);
  704. else
  705. /* Clock CODEC directly from MCLK */
  706. snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
  707. }
  708. return 0;
  709. }
  710. static int wm8978_mute(struct snd_soc_dai *dai, int mute)
  711. {
  712. struct snd_soc_codec *codec = dai->codec;
  713. dev_dbg(codec->dev, "%s: %d\n", __func__, mute);
  714. if (mute)
  715. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0x40);
  716. else
  717. snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0);
  718. return 0;
  719. }
  720. static int wm8978_set_bias_level(struct snd_soc_codec *codec,
  721. enum snd_soc_bias_level level)
  722. {
  723. u16 power1 = snd_soc_read(codec, WM8978_POWER_MANAGEMENT_1) & ~3;
  724. switch (level) {
  725. case SND_SOC_BIAS_ON:
  726. case SND_SOC_BIAS_PREPARE:
  727. power1 |= 1; /* VMID 75k */
  728. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  729. break;
  730. case SND_SOC_BIAS_STANDBY:
  731. /* bit 3: enable bias, bit 2: enable I/O tie off buffer */
  732. power1 |= 0xc;
  733. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  734. /* Initial cap charge at VMID 5k */
  735. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1,
  736. power1 | 0x3);
  737. mdelay(100);
  738. }
  739. power1 |= 0x2; /* VMID 500k */
  740. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
  741. break;
  742. case SND_SOC_BIAS_OFF:
  743. /* Preserve PLL - OPCLK may be used by someone */
  744. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, ~0x20, 0);
  745. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0);
  746. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_3, 0);
  747. break;
  748. }
  749. dev_dbg(codec->dev, "%s: %d, %x\n", __func__, level, power1);
  750. codec->dapm.bias_level = level;
  751. return 0;
  752. }
  753. #define WM8978_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  754. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  755. static const struct snd_soc_dai_ops wm8978_dai_ops = {
  756. .hw_params = wm8978_hw_params,
  757. .digital_mute = wm8978_mute,
  758. .set_fmt = wm8978_set_dai_fmt,
  759. .set_clkdiv = wm8978_set_dai_clkdiv,
  760. .set_sysclk = wm8978_set_dai_sysclk,
  761. };
  762. /* Also supports 12kHz */
  763. static struct snd_soc_dai_driver wm8978_dai = {
  764. .name = "wm8978-hifi",
  765. .playback = {
  766. .stream_name = "Playback",
  767. .channels_min = 1,
  768. .channels_max = 2,
  769. .rates = SNDRV_PCM_RATE_8000_48000,
  770. .formats = WM8978_FORMATS,
  771. },
  772. .capture = {
  773. .stream_name = "Capture",
  774. .channels_min = 1,
  775. .channels_max = 2,
  776. .rates = SNDRV_PCM_RATE_8000_48000,
  777. .formats = WM8978_FORMATS,
  778. },
  779. .ops = &wm8978_dai_ops,
  780. };
  781. static int wm8978_suspend(struct snd_soc_codec *codec)
  782. {
  783. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  784. wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
  785. /* Also switch PLL off */
  786. snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, 0);
  787. regcache_mark_dirty(wm8978->regmap);
  788. return 0;
  789. }
  790. static int wm8978_resume(struct snd_soc_codec *codec)
  791. {
  792. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  793. /* Sync reg_cache with the hardware */
  794. regcache_sync(wm8978->regmap);
  795. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  796. if (wm8978->f_pllout)
  797. /* Switch PLL on */
  798. snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
  799. return 0;
  800. }
  801. /*
  802. * These registers contain an "update" bit - bit 8. This means, for example,
  803. * that one can write new DAC digital volume for both channels, but only when
  804. * the update bit is set, will also the volume be updated - simultaneously for
  805. * both channels.
  806. */
  807. static const int update_reg[] = {
  808. WM8978_LEFT_DAC_DIGITAL_VOLUME,
  809. WM8978_RIGHT_DAC_DIGITAL_VOLUME,
  810. WM8978_LEFT_ADC_DIGITAL_VOLUME,
  811. WM8978_RIGHT_ADC_DIGITAL_VOLUME,
  812. WM8978_LEFT_INP_PGA_CONTROL,
  813. WM8978_RIGHT_INP_PGA_CONTROL,
  814. WM8978_LOUT1_HP_CONTROL,
  815. WM8978_ROUT1_HP_CONTROL,
  816. WM8978_LOUT2_SPK_CONTROL,
  817. WM8978_ROUT2_SPK_CONTROL,
  818. };
  819. static int wm8978_probe(struct snd_soc_codec *codec)
  820. {
  821. struct wm8978_priv *wm8978 = snd_soc_codec_get_drvdata(codec);
  822. int ret = 0, i;
  823. /*
  824. * Set default system clock to PLL, it is more precise, this is also the
  825. * default hardware setting
  826. */
  827. wm8978->sysclk = WM8978_PLL;
  828. codec->control_data = wm8978->regmap;
  829. ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
  830. if (ret < 0) {
  831. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  832. return ret;
  833. }
  834. /*
  835. * Set the update bit in all registers, that have one. This way all
  836. * writes to those registers will also cause the update bit to be
  837. * written.
  838. */
  839. for (i = 0; i < ARRAY_SIZE(update_reg); i++)
  840. snd_soc_update_bits(codec, update_reg[i], 0x100, 0x100);
  841. wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  842. return 0;
  843. }
  844. /* power down chip */
  845. static int wm8978_remove(struct snd_soc_codec *codec)
  846. {
  847. wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
  848. return 0;
  849. }
  850. static struct snd_soc_codec_driver soc_codec_dev_wm8978 = {
  851. .probe = wm8978_probe,
  852. .remove = wm8978_remove,
  853. .suspend = wm8978_suspend,
  854. .resume = wm8978_resume,
  855. .set_bias_level = wm8978_set_bias_level,
  856. .controls = wm8978_snd_controls,
  857. .num_controls = ARRAY_SIZE(wm8978_snd_controls),
  858. .dapm_widgets = wm8978_dapm_widgets,
  859. .num_dapm_widgets = ARRAY_SIZE(wm8978_dapm_widgets),
  860. .dapm_routes = wm8978_dapm_routes,
  861. .num_dapm_routes = ARRAY_SIZE(wm8978_dapm_routes),
  862. };
  863. static const struct regmap_config wm8978_regmap_config = {
  864. .reg_bits = 7,
  865. .val_bits = 9,
  866. .max_register = WM8978_MAX_REGISTER,
  867. .volatile_reg = wm8978_volatile,
  868. .cache_type = REGCACHE_RBTREE,
  869. .reg_defaults = wm8978_reg_defaults,
  870. .num_reg_defaults = ARRAY_SIZE(wm8978_reg_defaults),
  871. };
  872. static __devinit int wm8978_i2c_probe(struct i2c_client *i2c,
  873. const struct i2c_device_id *id)
  874. {
  875. struct wm8978_priv *wm8978;
  876. int ret;
  877. wm8978 = devm_kzalloc(&i2c->dev, sizeof(struct wm8978_priv),
  878. GFP_KERNEL);
  879. if (wm8978 == NULL)
  880. return -ENOMEM;
  881. wm8978->regmap = regmap_init_i2c(i2c, &wm8978_regmap_config);
  882. if (IS_ERR(wm8978->regmap)) {
  883. ret = PTR_ERR(wm8978->regmap);
  884. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  885. return ret;
  886. }
  887. i2c_set_clientdata(i2c, wm8978);
  888. /* Reset the codec */
  889. ret = regmap_write(wm8978->regmap, WM8978_RESET, 0);
  890. if (ret != 0) {
  891. dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
  892. goto err;
  893. }
  894. ret = snd_soc_register_codec(&i2c->dev,
  895. &soc_codec_dev_wm8978, &wm8978_dai, 1);
  896. if (ret != 0) {
  897. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  898. goto err;
  899. }
  900. return 0;
  901. err:
  902. regmap_exit(wm8978->regmap);
  903. return ret;
  904. }
  905. static __devexit int wm8978_i2c_remove(struct i2c_client *client)
  906. {
  907. struct wm8978_priv *wm8978 = i2c_get_clientdata(client);
  908. snd_soc_unregister_codec(&client->dev);
  909. regmap_exit(wm8978->regmap);
  910. return 0;
  911. }
  912. static const struct i2c_device_id wm8978_i2c_id[] = {
  913. { "wm8978", 0 },
  914. { }
  915. };
  916. MODULE_DEVICE_TABLE(i2c, wm8978_i2c_id);
  917. static struct i2c_driver wm8978_i2c_driver = {
  918. .driver = {
  919. .name = "wm8978",
  920. .owner = THIS_MODULE,
  921. },
  922. .probe = wm8978_i2c_probe,
  923. .remove = __devexit_p(wm8978_i2c_remove),
  924. .id_table = wm8978_i2c_id,
  925. };
  926. static int __init wm8978_modinit(void)
  927. {
  928. int ret = 0;
  929. ret = i2c_add_driver(&wm8978_i2c_driver);
  930. if (ret != 0) {
  931. printk(KERN_ERR "Failed to register WM8978 I2C driver: %d\n",
  932. ret);
  933. }
  934. return ret;
  935. }
  936. module_init(wm8978_modinit);
  937. static void __exit wm8978_exit(void)
  938. {
  939. i2c_del_driver(&wm8978_i2c_driver);
  940. }
  941. module_exit(wm8978_exit);
  942. MODULE_DESCRIPTION("ASoC WM8978 codec driver");
  943. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  944. MODULE_LICENSE("GPL");