wm9090.c 20 KB

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