wm9090.c 21 KB

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