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. struct wm9090_platform_data pdata;
  141. void *control_data;
  142. };
  143. static int wm9090_volatile(unsigned int reg)
  144. {
  145. switch (reg) {
  146. case WM9090_SOFTWARE_RESET:
  147. case WM9090_DC_SERVO_0:
  148. case WM9090_DC_SERVO_READBACK_0:
  149. case WM9090_DC_SERVO_READBACK_1:
  150. case WM9090_DC_SERVO_READBACK_2:
  151. return 1;
  152. default:
  153. return 0;
  154. }
  155. }
  156. static void wait_for_dc_servo(struct snd_soc_codec *codec)
  157. {
  158. unsigned int reg;
  159. int count = 0;
  160. dev_dbg(codec->dev, "Waiting for DC servo...\n");
  161. do {
  162. count++;
  163. msleep(1);
  164. reg = snd_soc_read(codec, WM9090_DC_SERVO_READBACK_0);
  165. dev_dbg(codec->dev, "DC servo status: %x\n", reg);
  166. } while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
  167. != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
  168. if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
  169. != WM9090_DCS_CAL_COMPLETE_MASK)
  170. dev_err(codec->dev, "Timed out waiting for DC Servo\n");
  171. }
  172. static const unsigned int in_tlv[] = {
  173. TLV_DB_RANGE_HEAD(6),
  174. 0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
  175. 1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
  176. 4, 6, TLV_DB_SCALE_ITEM(600, 600, 0),
  177. };
  178. static const unsigned int mix_tlv[] = {
  179. TLV_DB_RANGE_HEAD(4),
  180. 0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
  181. 3, 3, TLV_DB_SCALE_ITEM(0, 0, 0),
  182. };
  183. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  184. static const unsigned int spkboost_tlv[] = {
  185. TLV_DB_RANGE_HEAD(7),
  186. 0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
  187. 7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
  188. };
  189. static const struct snd_kcontrol_new wm9090_controls[] = {
  190. SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
  191. in_tlv),
  192. SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
  193. SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
  194. SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
  195. in_tlv),
  196. SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
  197. SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
  198. SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
  199. SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
  200. mix_tlv),
  201. SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
  202. mix_tlv),
  203. SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
  204. SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
  205. mix_tlv),
  206. SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
  207. mix_tlv),
  208. SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
  209. SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
  210. mix_tlv),
  211. SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
  212. mix_tlv),
  213. SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
  214. WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
  215. SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
  216. WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
  217. SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
  218. WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
  219. SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
  220. out_tlv),
  221. SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
  222. SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
  223. SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
  224. };
  225. static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
  226. SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
  227. in_tlv),
  228. SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
  229. SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
  230. SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
  231. mix_tlv),
  232. SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
  233. mix_tlv),
  234. SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
  235. mix_tlv),
  236. };
  237. static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
  238. SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
  239. in_tlv),
  240. SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
  241. SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
  242. SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
  243. mix_tlv),
  244. SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
  245. mix_tlv),
  246. SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
  247. mix_tlv),
  248. };
  249. static int hp_ev(struct snd_soc_dapm_widget *w,
  250. struct snd_kcontrol *kcontrol, int event)
  251. {
  252. struct snd_soc_codec *codec = w->codec;
  253. unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
  254. switch (event) {
  255. case SND_SOC_DAPM_POST_PMU:
  256. snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
  257. WM9090_CP_ENA, WM9090_CP_ENA);
  258. msleep(5);
  259. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  260. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
  261. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
  262. reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
  263. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  264. /* Start the DC servo. We don't currently use the
  265. * ability to save the state since we don't have full
  266. * control of the analogue paths and they can change
  267. * DC offsets; see the WM8904 driver for an example of
  268. * doing so.
  269. */
  270. snd_soc_write(codec, WM9090_DC_SERVO_0,
  271. WM9090_DCS_ENA_CHAN_0 |
  272. WM9090_DCS_ENA_CHAN_1 |
  273. WM9090_DCS_TRIG_STARTUP_1 |
  274. WM9090_DCS_TRIG_STARTUP_0);
  275. wait_for_dc_servo(codec);
  276. reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
  277. WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
  278. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  279. break;
  280. case SND_SOC_DAPM_PRE_PMD:
  281. reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
  282. WM9090_HPOUT1L_DLY |
  283. WM9090_HPOUT1L_OUTP |
  284. WM9090_HPOUT1R_RMV_SHORT |
  285. WM9090_HPOUT1R_DLY |
  286. WM9090_HPOUT1R_OUTP);
  287. snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
  288. snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
  289. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  290. WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
  291. 0);
  292. snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
  293. WM9090_CP_ENA, 0);
  294. break;
  295. }
  296. return 0;
  297. }
  298. static const struct snd_kcontrol_new spkmix[] = {
  299. SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
  300. SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
  301. SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
  302. SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
  303. };
  304. static const struct snd_kcontrol_new spkout[] = {
  305. SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
  306. };
  307. static const struct snd_kcontrol_new mixoutl[] = {
  308. SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
  309. SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
  310. SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
  311. SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
  312. };
  313. static const struct snd_kcontrol_new mixoutr[] = {
  314. SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
  315. SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
  316. SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
  317. SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
  318. };
  319. static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
  320. SND_SOC_DAPM_INPUT("IN1+"),
  321. SND_SOC_DAPM_INPUT("IN1-"),
  322. SND_SOC_DAPM_INPUT("IN2+"),
  323. SND_SOC_DAPM_INPUT("IN2-"),
  324. SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
  325. SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
  326. SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
  327. SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
  328. SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
  329. SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
  330. spkmix, ARRAY_SIZE(spkmix)),
  331. SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
  332. mixoutl, ARRAY_SIZE(mixoutl)),
  333. SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
  334. mixoutr, ARRAY_SIZE(mixoutr)),
  335. SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  336. hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  337. SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
  338. SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
  339. spkout, ARRAY_SIZE(spkout)),
  340. SND_SOC_DAPM_OUTPUT("HPR"),
  341. SND_SOC_DAPM_OUTPUT("HPL"),
  342. SND_SOC_DAPM_OUTPUT("Speaker"),
  343. };
  344. static const struct snd_soc_dapm_route audio_map[] = {
  345. { "IN1A PGA", NULL, "IN1+" },
  346. { "IN2A PGA", NULL, "IN2+" },
  347. { "SPKMIX", "IN1A Switch", "IN1A PGA" },
  348. { "SPKMIX", "IN2A Switch", "IN2A PGA" },
  349. { "MIXOUTL", "IN1A Switch", "IN1A PGA" },
  350. { "MIXOUTL", "IN2A Switch", "IN2A PGA" },
  351. { "MIXOUTR", "IN1A Switch", "IN1A PGA" },
  352. { "MIXOUTR", "IN2A Switch", "IN2A PGA" },
  353. { "HP PGA", NULL, "OSC" },
  354. { "HP PGA", NULL, "MIXOUTL" },
  355. { "HP PGA", NULL, "MIXOUTR" },
  356. { "HPL", NULL, "HP PGA" },
  357. { "HPR", NULL, "HP PGA" },
  358. { "SPKPGA", NULL, "OSC" },
  359. { "SPKPGA", NULL, "SPKMIX" },
  360. { "SPKOUT", "Mixer Switch", "SPKPGA" },
  361. { "Speaker", NULL, "SPKOUT" },
  362. };
  363. static const struct snd_soc_dapm_route audio_map_in1_se[] = {
  364. { "IN1B PGA", NULL, "IN1-" },
  365. { "SPKMIX", "IN1B Switch", "IN1B PGA" },
  366. { "MIXOUTL", "IN1B Switch", "IN1B PGA" },
  367. { "MIXOUTR", "IN1B Switch", "IN1B PGA" },
  368. };
  369. static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
  370. { "IN1A PGA", NULL, "IN1-" },
  371. };
  372. static const struct snd_soc_dapm_route audio_map_in2_se[] = {
  373. { "IN2B PGA", NULL, "IN2-" },
  374. { "SPKMIX", "IN2B Switch", "IN2B PGA" },
  375. { "MIXOUTL", "IN2B Switch", "IN2B PGA" },
  376. { "MIXOUTR", "IN2B Switch", "IN2B PGA" },
  377. };
  378. static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
  379. { "IN2A PGA", NULL, "IN2-" },
  380. };
  381. static int wm9090_add_controls(struct snd_soc_codec *codec)
  382. {
  383. struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
  384. int i;
  385. snd_soc_dapm_new_controls(codec, wm9090_dapm_widgets,
  386. ARRAY_SIZE(wm9090_dapm_widgets));
  387. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  388. snd_soc_add_controls(codec, wm9090_controls,
  389. ARRAY_SIZE(wm9090_controls));
  390. if (wm9090->pdata.lin1_diff) {
  391. snd_soc_dapm_add_routes(codec, audio_map_in1_diff,
  392. ARRAY_SIZE(audio_map_in1_diff));
  393. } else {
  394. snd_soc_dapm_add_routes(codec, audio_map_in1_se,
  395. ARRAY_SIZE(audio_map_in1_se));
  396. snd_soc_add_controls(codec, wm9090_in1_se_controls,
  397. ARRAY_SIZE(wm9090_in1_se_controls));
  398. }
  399. if (wm9090->pdata.lin2_diff) {
  400. snd_soc_dapm_add_routes(codec, audio_map_in2_diff,
  401. ARRAY_SIZE(audio_map_in2_diff));
  402. } else {
  403. snd_soc_dapm_add_routes(codec, audio_map_in2_se,
  404. ARRAY_SIZE(audio_map_in2_se));
  405. snd_soc_add_controls(codec, wm9090_in2_se_controls,
  406. ARRAY_SIZE(wm9090_in2_se_controls));
  407. }
  408. if (wm9090->pdata.agc_ena) {
  409. for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
  410. snd_soc_write(codec, WM9090_AGC_CONTROL_0 + i,
  411. wm9090->pdata.agc[i]);
  412. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
  413. WM9090_AGC_ENA, WM9090_AGC_ENA);
  414. } else {
  415. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
  416. WM9090_AGC_ENA, 0);
  417. }
  418. return 0;
  419. }
  420. /*
  421. * The machine driver should call this from their set_bias_level; if there
  422. * isn't one then this can just be set as the set_bias_level function.
  423. */
  424. static int wm9090_set_bias_level(struct snd_soc_codec *codec,
  425. enum snd_soc_bias_level level)
  426. {
  427. u16 *reg_cache = codec->reg_cache;
  428. int i, ret;
  429. switch (level) {
  430. case SND_SOC_BIAS_ON:
  431. break;
  432. case SND_SOC_BIAS_PREPARE:
  433. snd_soc_update_bits(codec, WM9090_ANTIPOP2, WM9090_VMID_ENA,
  434. WM9090_VMID_ENA);
  435. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  436. WM9090_BIAS_ENA |
  437. WM9090_VMID_RES_MASK,
  438. WM9090_BIAS_ENA |
  439. 1 << WM9090_VMID_RES_SHIFT);
  440. msleep(1); /* Probably an overestimate */
  441. break;
  442. case SND_SOC_BIAS_STANDBY:
  443. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  444. /* Restore the register cache */
  445. for (i = 1; i < codec->driver->reg_cache_size; i++) {
  446. if (reg_cache[i] == wm9090_reg_defaults[i])
  447. continue;
  448. if (wm9090_volatile(i))
  449. continue;
  450. ret = snd_soc_write(codec, i, reg_cache[i]);
  451. if (ret != 0)
  452. dev_warn(codec->dev,
  453. "Failed to restore register %d: %d\n",
  454. i, ret);
  455. }
  456. }
  457. /* We keep VMID off during standby since the combination of
  458. * ground referenced outputs and class D speaker mean that
  459. * latency is not an issue.
  460. */
  461. snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
  462. WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
  463. snd_soc_update_bits(codec, WM9090_ANTIPOP2,
  464. WM9090_VMID_ENA, 0);
  465. break;
  466. case SND_SOC_BIAS_OFF:
  467. break;
  468. }
  469. codec->bias_level = level;
  470. return 0;
  471. }
  472. static int wm9090_probe(struct snd_soc_codec *codec)
  473. {
  474. struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
  475. u16 *reg_cache = codec->reg_cache;
  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. reg_cache[WM9090_IN1_LINE_INPUT_A_VOLUME] |= WM9090_IN1_VU
  497. | WM9090_IN1A_ZC;
  498. reg_cache[WM9090_IN1_LINE_INPUT_B_VOLUME] |= WM9090_IN1_VU
  499. | WM9090_IN1B_ZC;
  500. reg_cache[WM9090_IN2_LINE_INPUT_A_VOLUME] |= WM9090_IN2_VU
  501. | WM9090_IN2A_ZC;
  502. reg_cache[WM9090_IN2_LINE_INPUT_B_VOLUME] |= WM9090_IN2_VU
  503. | WM9090_IN2B_ZC;
  504. reg_cache[WM9090_SPEAKER_VOLUME_LEFT] |=
  505. WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC;
  506. reg_cache[WM9090_LEFT_OUTPUT_VOLUME] |=
  507. WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC;
  508. reg_cache[WM9090_RIGHT_OUTPUT_VOLUME] |=
  509. WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC;
  510. 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");