wm9090.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * ALSA SoC WM9090 driver
  3. *
  4. * Copyright 2009, 2010 Wolfson Microelectronics
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <linux/i2c.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <sound/initval.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dapm.h>
  31. #include <sound/tlv.h>
  32. #include <sound/wm9090.h>
  33. #include "wm9090.h"
  34. static struct snd_soc_codec *wm9090_codec;
  35. static const u16 wm9090_reg_defaults[] = {
  36. 0x9093, /* R0 - Software Reset */
  37. 0x0006, /* R1 - Power Management (1) */
  38. 0x6000, /* R2 - Power Management (2) */
  39. 0x0000, /* R3 - Power Management (3) */
  40. 0x0000, /* R4 */
  41. 0x0000, /* R5 */
  42. 0x01C0, /* R6 - Clocking 1 */
  43. 0x0000, /* R7 */
  44. 0x0000, /* R8 */
  45. 0x0000, /* R9 */
  46. 0x0000, /* R10 */
  47. 0x0000, /* R11 */
  48. 0x0000, /* R12 */
  49. 0x0000, /* R13 */
  50. 0x0000, /* R14 */
  51. 0x0000, /* R15 */
  52. 0x0000, /* R16 */
  53. 0x0000, /* R17 */
  54. 0x0000, /* R18 */
  55. 0x0000, /* R19 */
  56. 0x0000, /* R20 */
  57. 0x0000, /* R21 */
  58. 0x0003, /* R22 - IN1 Line Control */
  59. 0x0003, /* R23 - IN2 Line Control */
  60. 0x0083, /* R24 - IN1 Line Input A Volume */
  61. 0x0083, /* R25 - IN1 Line Input B Volume */
  62. 0x0083, /* R26 - IN2 Line Input A Volume */
  63. 0x0083, /* R27 - IN2 Line Input B Volume */
  64. 0x002D, /* R28 - Left Output Volume */
  65. 0x002D, /* R29 - Right Output Volume */
  66. 0x0000, /* R30 */
  67. 0x0000, /* R31 */
  68. 0x0000, /* R32 */
  69. 0x0000, /* R33 */
  70. 0x0100, /* R34 - SPKMIXL Attenuation */
  71. 0x0000, /* R35 */
  72. 0x0010, /* R36 - SPKOUT Mixers */
  73. 0x0140, /* R37 - ClassD3 */
  74. 0x0039, /* R38 - Speaker Volume Left */
  75. 0x0000, /* R39 */
  76. 0x0000, /* R40 */
  77. 0x0000, /* R41 */
  78. 0x0000, /* R42 */
  79. 0x0000, /* R43 */
  80. 0x0000, /* R44 */
  81. 0x0000, /* R45 - Output Mixer1 */
  82. 0x0000, /* R46 - Output Mixer2 */
  83. 0x0100, /* R47 - Output Mixer3 */
  84. 0x0100, /* R48 - Output Mixer4 */
  85. 0x0000, /* R49 */
  86. 0x0000, /* R50 */
  87. 0x0000, /* R51 */
  88. 0x0000, /* R52 */
  89. 0x0000, /* R53 */
  90. 0x0000, /* R54 - Speaker Mixer */
  91. 0x0000, /* R55 */
  92. 0x0000, /* R56 */
  93. 0x000D, /* R57 - AntiPOP2 */
  94. 0x0000, /* R58 */
  95. 0x0000, /* R59 */
  96. 0x0000, /* R60 */
  97. 0x0000, /* R61 */
  98. 0x0000, /* R62 */
  99. 0x0000, /* R63 */
  100. 0x0000, /* R64 */
  101. 0x0000, /* R65 */
  102. 0x0000, /* R66 */
  103. 0x0000, /* R67 */
  104. 0x0000, /* R68 */
  105. 0x0000, /* R69 */
  106. 0x0000, /* R70 - Write Sequencer 0 */
  107. 0x0000, /* R71 - Write Sequencer 1 */
  108. 0x0000, /* R72 - Write Sequencer 2 */
  109. 0x0000, /* R73 - Write Sequencer 3 */
  110. 0x0000, /* R74 - Write Sequencer 4 */
  111. 0x0000, /* R75 - Write Sequencer 5 */
  112. 0x1F25, /* R76 - Charge Pump 1 */
  113. 0x0000, /* R77 */
  114. 0x0000, /* R78 */
  115. 0x0000, /* R79 */
  116. 0x0000, /* R80 */
  117. 0x0000, /* R81 */
  118. 0x0000, /* R82 */
  119. 0x0000, /* R83 */
  120. 0x0000, /* R84 - DC Servo 0 */
  121. 0x054A, /* R85 - DC Servo 1 */
  122. 0x0000, /* R86 */
  123. 0x0000, /* R87 - DC Servo 3 */
  124. 0x0000, /* R88 - DC Servo Readback 0 */
  125. 0x0000, /* R89 - DC Servo Readback 1 */
  126. 0x0000, /* R90 - DC Servo Readback 2 */
  127. 0x0000, /* R91 */
  128. 0x0000, /* R92 */
  129. 0x0000, /* R93 */
  130. 0x0000, /* R94 */
  131. 0x0000, /* R95 */
  132. 0x0100, /* R96 - Analogue HP 0 */
  133. 0x0000, /* R97 */
  134. 0x8640, /* R98 - AGC Control 0 */
  135. 0xC000, /* R99 - AGC Control 1 */
  136. 0x0200, /* R100 - AGC Control 2 */
  137. };
  138. /* This struct is used to save the context */
  139. struct wm9090_priv {
  140. /* We're not really registering as a CODEC since ASoC core
  141. * does not yet support multiple CODECs but having the CODEC
  142. * structure means we can reuse some of the ASoC core
  143. * features.
  144. */
  145. struct snd_soc_codec codec;
  146. struct mutex mutex;
  147. u16 reg_cache[WM9090_MAX_REGISTER + 1];
  148. struct wm9090_platform_data pdata;
  149. };
  150. static int wm9090_volatile(unsigned int reg)
  151. {
  152. switch (reg) {
  153. case WM9090_SOFTWARE_RESET:
  154. case WM9090_DC_SERVO_0:
  155. case WM9090_DC_SERVO_READBACK_0:
  156. case WM9090_DC_SERVO_READBACK_1:
  157. case WM9090_DC_SERVO_READBACK_2:
  158. return 1;
  159. default:
  160. return 0;
  161. }
  162. }
  163. static void wait_for_dc_servo(struct snd_soc_codec *codec)
  164. {
  165. unsigned int reg;
  166. int count = 0;
  167. dev_dbg(codec->dev, "Waiting for DC servo...\n");
  168. do {
  169. count++;
  170. msleep(1);
  171. reg = snd_soc_read(codec, WM9090_DC_SERVO_READBACK_0);
  172. dev_dbg(codec->dev, "DC servo status: %x\n", reg);
  173. } while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
  174. != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
  175. if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
  176. != WM9090_DCS_CAL_COMPLETE_MASK)
  177. dev_err(codec->dev, "Timed out waiting for DC Servo\n");
  178. }
  179. static const unsigned int in_tlv[] = {
  180. TLV_DB_RANGE_HEAD(6),
  181. 0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
  182. 1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
  183. 4, 6, TLV_DB_SCALE_ITEM(600, 600, 0),
  184. };
  185. static const unsigned int mix_tlv[] = {
  186. TLV_DB_RANGE_HEAD(4),
  187. 0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
  188. 3, 3, TLV_DB_SCALE_ITEM(0, 0, 0),
  189. };
  190. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  191. static const unsigned int spkboost_tlv[] = {
  192. TLV_DB_RANGE_HEAD(7),
  193. 0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
  194. 7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
  195. };
  196. static const struct snd_kcontrol_new wm9090_controls[] = {
  197. SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
  198. in_tlv),
  199. SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
  200. SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
  201. SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
  202. in_tlv),
  203. SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
  204. SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
  205. SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
  206. SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
  207. mix_tlv),
  208. SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
  209. mix_tlv),
  210. SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
  211. SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
  212. mix_tlv),
  213. SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
  214. mix_tlv),
  215. SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
  216. SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
  217. mix_tlv),
  218. SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
  219. mix_tlv),
  220. SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
  221. WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
  222. SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
  223. WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
  224. SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
  225. WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
  226. SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
  227. out_tlv),
  228. SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
  229. SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
  230. SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
  231. };
  232. static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
  233. SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
  234. in_tlv),
  235. SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
  236. SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
  237. SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
  238. mix_tlv),
  239. SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
  240. mix_tlv),
  241. SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
  242. mix_tlv),
  243. };
  244. static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
  245. SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
  246. in_tlv),
  247. SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
  248. SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
  249. SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
  250. mix_tlv),
  251. SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
  252. mix_tlv),
  253. SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
  254. mix_tlv),
  255. };
  256. static int hp_ev(struct snd_soc_dapm_widget *w,
  257. struct snd_kcontrol *kcontrol, int event)
  258. {
  259. struct snd_soc_codec *codec = w->codec;
  260. unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
  261. switch (event) {
  262. case SND_SOC_DAPM_POST_PMU:
  263. snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
  264. WM9090_CP_ENA, WM9090_CP_ENA);
  265. msleep(5);
  266. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  267. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
  268. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
  269. reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
  270. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  271. /* Start the DC servo. We don't currently use the
  272. * ability to save the state since we don't have full
  273. * control of the analogue paths and they can change
  274. * DC offsets; see the WM8904 driver for an example of
  275. * doing so.
  276. */
  277. snd_soc_write(codec, WM9090_DC_SERVO_0,
  278. WM9090_DCS_ENA_CHAN_0 |
  279. WM9090_DCS_ENA_CHAN_1 |
  280. WM9090_DCS_TRIG_STARTUP_1 |
  281. WM9090_DCS_TRIG_STARTUP_0);
  282. wait_for_dc_servo(codec);
  283. reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
  284. WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
  285. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  286. break;
  287. case SND_SOC_DAPM_PRE_PMD:
  288. reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
  289. WM9090_HPOUT1L_DLY |
  290. WM9090_HPOUT1L_OUTP |
  291. WM9090_HPOUT1R_RMV_SHORT |
  292. WM9090_HPOUT1R_DLY |
  293. WM9090_HPOUT1R_OUTP);
  294. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  295. snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
  296. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  297. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
  298. 0);
  299. snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
  300. WM9090_CP_ENA, 0);
  301. break;
  302. }
  303. return 0;
  304. }
  305. static const struct snd_kcontrol_new spkmix[] = {
  306. SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
  307. SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
  308. SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
  309. SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
  310. };
  311. static const struct snd_kcontrol_new spkout[] = {
  312. SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
  313. };
  314. static const struct snd_kcontrol_new mixoutl[] = {
  315. SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
  316. SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
  317. SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
  318. SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
  319. };
  320. static const struct snd_kcontrol_new mixoutr[] = {
  321. SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
  322. SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
  323. SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
  324. SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
  325. };
  326. static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
  327. SND_SOC_DAPM_INPUT("IN1+"),
  328. SND_SOC_DAPM_INPUT("IN1-"),
  329. SND_SOC_DAPM_INPUT("IN2+"),
  330. SND_SOC_DAPM_INPUT("IN2-"),
  331. SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
  332. SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
  333. SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
  334. SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  335. SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  336. SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
  337. spkmix, ARRAY_SIZE(spkmix)),
  338. SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
  339. mixoutl, ARRAY_SIZE(mixoutl)),
  340. SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
  341. mixoutr, ARRAY_SIZE(mixoutr)),
  342. SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  343. hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  344. SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
  345. SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
  346. spkout, ARRAY_SIZE(spkout)),
  347. SND_SOC_DAPM_OUTPUT("HPR"),
  348. SND_SOC_DAPM_OUTPUT("HPL"),
  349. SND_SOC_DAPM_OUTPUT("Speaker"),
  350. };
  351. static const struct snd_soc_dapm_route audio_map[] = {
  352. { "IN1A PGA", NULL, "IN1+" },
  353. { "IN2A PGA", NULL, "IN2+" },
  354. { "SPKMIX", "IN1A Switch", "IN1A PGA" },
  355. { "SPKMIX", "IN2A Switch", "IN2A PGA" },
  356. { "MIXOUTL", "IN1A Switch", "IN1A PGA" },
  357. { "MIXOUTL", "IN2A Switch", "IN2A PGA" },
  358. { "MIXOUTR", "IN1A Switch", "IN1A PGA" },
  359. { "MIXOUTR", "IN2A Switch", "IN2A PGA" },
  360. { "HP PGA", NULL, "OSC" },
  361. { "HP PGA", NULL, "MIXOUTL" },
  362. { "HP PGA", NULL, "MIXOUTR" },
  363. { "HPL", NULL, "HP PGA" },
  364. { "HPR", NULL, "HP PGA" },
  365. { "SPKPGA", NULL, "OSC" },
  366. { "SPKPGA", NULL, "SPKMIX" },
  367. { "SPKOUT", "Mixer Switch", "SPKPGA" },
  368. { "Speaker", NULL, "SPKOUT" },
  369. };
  370. static const struct snd_soc_dapm_route audio_map_in1_se[] = {
  371. { "IN1B PGA", NULL, "IN1-" },
  372. { "SPKMIX", "IN1B Switch", "IN1B PGA" },
  373. { "MIXOUTL", "IN1B Switch", "IN1B PGA" },
  374. { "MIXOUTR", "IN1B Switch", "IN1B PGA" },
  375. };
  376. static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
  377. { "IN1A PGA", NULL, "IN1-" },
  378. };
  379. static const struct snd_soc_dapm_route audio_map_in2_se[] = {
  380. { "IN2B PGA", NULL, "IN2-" },
  381. { "SPKMIX", "IN2B Switch", "IN2B PGA" },
  382. { "MIXOUTL", "IN2B Switch", "IN2B PGA" },
  383. { "MIXOUTR", "IN2B Switch", "IN2B PGA" },
  384. };
  385. static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
  386. { "IN2A PGA", NULL, "IN2-" },
  387. };
  388. static int wm9090_add_controls(struct snd_soc_codec *codec)
  389. {
  390. struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
  391. int i;
  392. snd_soc_dapm_new_controls(codec, wm9090_dapm_widgets,
  393. ARRAY_SIZE(wm9090_dapm_widgets));
  394. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  395. snd_soc_add_controls(codec, wm9090_controls,
  396. ARRAY_SIZE(wm9090_controls));
  397. if (wm9090->pdata.lin1_diff) {
  398. snd_soc_dapm_add_routes(codec, audio_map_in1_diff,
  399. ARRAY_SIZE(audio_map_in1_diff));
  400. } else {
  401. snd_soc_dapm_add_routes(codec, audio_map_in1_se,
  402. ARRAY_SIZE(audio_map_in1_se));
  403. snd_soc_add_controls(codec, wm9090_in1_se_controls,
  404. ARRAY_SIZE(wm9090_in1_se_controls));
  405. }
  406. if (wm9090->pdata.lin2_diff) {
  407. snd_soc_dapm_add_routes(codec, audio_map_in2_diff,
  408. ARRAY_SIZE(audio_map_in2_diff));
  409. } else {
  410. snd_soc_dapm_add_routes(codec, audio_map_in2_se,
  411. ARRAY_SIZE(audio_map_in2_se));
  412. snd_soc_add_controls(codec, wm9090_in2_se_controls,
  413. ARRAY_SIZE(wm9090_in2_se_controls));
  414. }
  415. if (wm9090->pdata.agc_ena) {
  416. for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
  417. snd_soc_write(codec, WM9090_AGC_CONTROL_0 + i,
  418. wm9090->pdata.agc[i]);
  419. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
  420. WM9090_AGC_ENA, WM9090_AGC_ENA);
  421. } else {
  422. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
  423. WM9090_AGC_ENA, 0);
  424. }
  425. return 0;
  426. }
  427. /*
  428. * The machine driver should call this from their set_bias_level; if there
  429. * isn't one then this can just be set as the set_bias_level function.
  430. */
  431. static int wm9090_set_bias_level(struct snd_soc_codec *codec,
  432. enum snd_soc_bias_level level)
  433. {
  434. u16 *reg_cache = codec->reg_cache;
  435. int i, ret;
  436. switch (level) {
  437. case SND_SOC_BIAS_ON:
  438. break;
  439. case SND_SOC_BIAS_PREPARE:
  440. snd_soc_update_bits(codec, WM9090_ANTIPOP2, WM9090_VMID_ENA,
  441. WM9090_VMID_ENA);
  442. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  443. WM9090_BIAS_ENA |
  444. WM9090_VMID_RES_MASK,
  445. WM9090_BIAS_ENA |
  446. 1 << WM9090_VMID_RES_SHIFT);
  447. msleep(1); /* Probably an overestimate */
  448. break;
  449. case SND_SOC_BIAS_STANDBY:
  450. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  451. /* Restore the register cache */
  452. for (i = 1; i < codec->reg_cache_size; i++) {
  453. if (reg_cache[i] == wm9090_reg_defaults[i])
  454. continue;
  455. if (wm9090_volatile(i))
  456. continue;
  457. ret = snd_soc_write(codec, i, reg_cache[i]);
  458. if (ret != 0)
  459. dev_warn(codec->dev,
  460. "Failed to restore register %d: %d\n",
  461. i, ret);
  462. }
  463. }
  464. /* We keep VMID off during standby since the combination of
  465. * ground referenced outputs and class D speaker mean that
  466. * latency is not an issue.
  467. */
  468. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  469. WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
  470. snd_soc_update_bits(codec, WM9090_ANTIPOP2,
  471. WM9090_VMID_ENA, 0);
  472. break;
  473. case SND_SOC_BIAS_OFF:
  474. break;
  475. }
  476. codec->bias_level = level;
  477. return 0;
  478. }
  479. static int wm9090_probe(struct platform_device *pdev)
  480. {
  481. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  482. struct snd_soc_codec *codec;
  483. int ret = 0;
  484. if (wm9090_codec == NULL) {
  485. dev_err(&pdev->dev, "Codec device not registered\n");
  486. return -ENODEV;
  487. }
  488. socdev->card->codec = wm9090_codec;
  489. codec = wm9090_codec;
  490. /* register pcms */
  491. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  492. if (ret < 0) {
  493. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  494. goto pcm_err;
  495. }
  496. wm9090_add_controls(codec);
  497. return 0;
  498. pcm_err:
  499. return ret;
  500. }
  501. #ifdef CONFIG_PM
  502. static int wm9090_suspend(struct platform_device *pdev, pm_message_t state)
  503. {
  504. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  505. struct snd_soc_codec *codec = socdev->card->codec;
  506. wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
  507. return 0;
  508. }
  509. static int wm9090_resume(struct platform_device *pdev)
  510. {
  511. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  512. struct snd_soc_codec *codec = socdev->card->codec;
  513. wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  514. return 0;
  515. }
  516. #else
  517. #define wm9090_suspend NULL
  518. #define wm9090_resume NULL
  519. #endif
  520. static int wm9090_remove(struct platform_device *pdev)
  521. {
  522. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  523. snd_soc_free_pcms(socdev);
  524. snd_soc_dapm_free(socdev);
  525. return 0;
  526. }
  527. struct snd_soc_codec_device soc_codec_dev_wm9090 = {
  528. .probe = wm9090_probe,
  529. .remove = wm9090_remove,
  530. .suspend = wm9090_suspend,
  531. .resume = wm9090_resume,
  532. };
  533. EXPORT_SYMBOL_GPL(soc_codec_dev_wm9090);
  534. static int wm9090_i2c_probe(struct i2c_client *i2c,
  535. const struct i2c_device_id *id)
  536. {
  537. struct wm9090_priv *wm9090;
  538. struct snd_soc_codec *codec;
  539. int ret;
  540. wm9090 = kzalloc(sizeof(*wm9090), GFP_KERNEL);
  541. if (wm9090 == NULL) {
  542. dev_err(&i2c->dev, "Can not allocate memory\n");
  543. return -ENOMEM;
  544. }
  545. codec = &wm9090->codec;
  546. if (i2c->dev.platform_data)
  547. memcpy(&wm9090->pdata, i2c->dev.platform_data,
  548. sizeof(wm9090->pdata));
  549. wm9090_codec = codec;
  550. i2c_set_clientdata(i2c, wm9090);
  551. mutex_init(&codec->mutex);
  552. INIT_LIST_HEAD(&codec->dapm_widgets);
  553. INIT_LIST_HEAD(&codec->dapm_paths);
  554. codec->control_data = i2c;
  555. snd_soc_codec_set_drvdata(codec, wm9090);
  556. codec->dev = &i2c->dev;
  557. codec->name = "WM9090";
  558. codec->owner = THIS_MODULE;
  559. codec->bias_level = SND_SOC_BIAS_OFF;
  560. codec->set_bias_level = wm9090_set_bias_level,
  561. codec->reg_cache_size = WM9090_MAX_REGISTER + 1;
  562. codec->reg_cache = &wm9090->reg_cache;
  563. codec->volatile_register = wm9090_volatile;
  564. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  565. if (ret != 0) {
  566. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  567. goto err;
  568. }
  569. memcpy(&wm9090->reg_cache, wm9090_reg_defaults,
  570. sizeof(wm9090->reg_cache));
  571. ret = snd_soc_read(codec, WM9090_SOFTWARE_RESET);
  572. if (ret < 0)
  573. goto err;
  574. if (ret != wm9090_reg_defaults[WM9090_SOFTWARE_RESET]) {
  575. dev_err(&i2c->dev, "Device is not a WM9090, ID=%x\n", ret);
  576. ret = -EINVAL;
  577. goto err;
  578. }
  579. ret = snd_soc_write(codec, WM9090_SOFTWARE_RESET, 0);
  580. if (ret < 0)
  581. goto err;
  582. /* Configure some defaults; they will be written out when we
  583. * bring the bias up.
  584. */
  585. wm9090->reg_cache[WM9090_IN1_LINE_INPUT_A_VOLUME] |= WM9090_IN1_VU
  586. | WM9090_IN1A_ZC;
  587. wm9090->reg_cache[WM9090_IN1_LINE_INPUT_B_VOLUME] |= WM9090_IN1_VU
  588. | WM9090_IN1B_ZC;
  589. wm9090->reg_cache[WM9090_IN2_LINE_INPUT_A_VOLUME] |= WM9090_IN2_VU
  590. | WM9090_IN2A_ZC;
  591. wm9090->reg_cache[WM9090_IN2_LINE_INPUT_B_VOLUME] |= WM9090_IN2_VU
  592. | WM9090_IN2B_ZC;
  593. wm9090->reg_cache[WM9090_SPEAKER_VOLUME_LEFT] |=
  594. WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC;
  595. wm9090->reg_cache[WM9090_LEFT_OUTPUT_VOLUME] |=
  596. WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC;
  597. wm9090->reg_cache[WM9090_RIGHT_OUTPUT_VOLUME] |=
  598. WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC;
  599. wm9090->reg_cache[WM9090_CLOCKING_1] |= WM9090_TOCLK_ENA;
  600. wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  601. ret = snd_soc_register_codec(codec);
  602. if (ret != 0) {
  603. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  604. goto err_bias;
  605. }
  606. return 0;
  607. err_bias:
  608. wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
  609. err:
  610. kfree(wm9090);
  611. i2c_set_clientdata(i2c, NULL);
  612. wm9090_codec = NULL;
  613. return ret;
  614. }
  615. static int wm9090_i2c_remove(struct i2c_client *i2c)
  616. {
  617. struct wm9090_priv *wm9090 = i2c_get_clientdata(i2c);
  618. struct snd_soc_codec *codec = &wm9090->codec;
  619. snd_soc_unregister_codec(codec);
  620. wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
  621. kfree(wm9090);
  622. wm9090_codec = NULL;
  623. return 0;
  624. }
  625. static const struct i2c_device_id wm9090_id[] = {
  626. { "wm9090", 0 },
  627. { }
  628. };
  629. MODULE_DEVICE_TABLE(i2c, wm9090_id);
  630. static struct i2c_driver wm9090_i2c_driver = {
  631. .driver = {
  632. .name = "wm9090",
  633. .owner = THIS_MODULE,
  634. },
  635. .probe = wm9090_i2c_probe,
  636. .remove = __devexit_p(wm9090_i2c_remove),
  637. .id_table = wm9090_id,
  638. };
  639. static int __init wm9090_init(void)
  640. {
  641. return i2c_add_driver(&wm9090_i2c_driver);
  642. }
  643. module_init(wm9090_init);
  644. static void __exit wm9090_exit(void)
  645. {
  646. i2c_del_driver(&wm9090_i2c_driver);
  647. }
  648. module_exit(wm9090_exit);
  649. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  650. MODULE_DESCRIPTION("WM9090 ASoC driver");
  651. MODULE_LICENSE("GPL");