wm9090.c 20 KB

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