wm8903.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /*
  2. * wm8903.c -- WM8903 ALSA SoC Audio driver
  3. *
  4. * Copyright 2008 Wolfson Microelectronics
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO:
  13. * - TDM mode configuration.
  14. * - Mic detect.
  15. * - Digital microphone support.
  16. * - Interrupt support (mic detect and sequencer).
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/pm.h>
  23. #include <linux/i2c.h>
  24. #include <linux/platform_device.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/tlv.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dapm.h>
  31. #include <sound/initval.h>
  32. #include "wm8903.h"
  33. /* Register defaults at reset */
  34. static u16 wm8903_reg_defaults[] = {
  35. 0x8903, /* R0 - SW Reset and ID */
  36. 0x0000, /* R1 - Revision Number */
  37. 0x0000, /* R2 */
  38. 0x0000, /* R3 */
  39. 0x0018, /* R4 - Bias Control 0 */
  40. 0x0000, /* R5 - VMID Control 0 */
  41. 0x0000, /* R6 - Mic Bias Control 0 */
  42. 0x0000, /* R7 */
  43. 0x0001, /* R8 - Analogue DAC 0 */
  44. 0x0000, /* R9 */
  45. 0x0001, /* R10 - Analogue ADC 0 */
  46. 0x0000, /* R11 */
  47. 0x0000, /* R12 - Power Management 0 */
  48. 0x0000, /* R13 - Power Management 1 */
  49. 0x0000, /* R14 - Power Management 2 */
  50. 0x0000, /* R15 - Power Management 3 */
  51. 0x0000, /* R16 - Power Management 4 */
  52. 0x0000, /* R17 - Power Management 5 */
  53. 0x0000, /* R18 - Power Management 6 */
  54. 0x0000, /* R19 */
  55. 0x0400, /* R20 - Clock Rates 0 */
  56. 0x0D07, /* R21 - Clock Rates 1 */
  57. 0x0000, /* R22 - Clock Rates 2 */
  58. 0x0000, /* R23 */
  59. 0x0050, /* R24 - Audio Interface 0 */
  60. 0x0242, /* R25 - Audio Interface 1 */
  61. 0x0008, /* R26 - Audio Interface 2 */
  62. 0x0022, /* R27 - Audio Interface 3 */
  63. 0x0000, /* R28 */
  64. 0x0000, /* R29 */
  65. 0x00C0, /* R30 - DAC Digital Volume Left */
  66. 0x00C0, /* R31 - DAC Digital Volume Right */
  67. 0x0000, /* R32 - DAC Digital 0 */
  68. 0x0000, /* R33 - DAC Digital 1 */
  69. 0x0000, /* R34 */
  70. 0x0000, /* R35 */
  71. 0x00C0, /* R36 - ADC Digital Volume Left */
  72. 0x00C0, /* R37 - ADC Digital Volume Right */
  73. 0x0000, /* R38 - ADC Digital 0 */
  74. 0x0073, /* R39 - Digital Microphone 0 */
  75. 0x09BF, /* R40 - DRC 0 */
  76. 0x3241, /* R41 - DRC 1 */
  77. 0x0020, /* R42 - DRC 2 */
  78. 0x0000, /* R43 - DRC 3 */
  79. 0x0085, /* R44 - Analogue Left Input 0 */
  80. 0x0085, /* R45 - Analogue Right Input 0 */
  81. 0x0044, /* R46 - Analogue Left Input 1 */
  82. 0x0044, /* R47 - Analogue Right Input 1 */
  83. 0x0000, /* R48 */
  84. 0x0000, /* R49 */
  85. 0x0008, /* R50 - Analogue Left Mix 0 */
  86. 0x0004, /* R51 - Analogue Right Mix 0 */
  87. 0x0000, /* R52 - Analogue Spk Mix Left 0 */
  88. 0x0000, /* R53 - Analogue Spk Mix Left 1 */
  89. 0x0000, /* R54 - Analogue Spk Mix Right 0 */
  90. 0x0000, /* R55 - Analogue Spk Mix Right 1 */
  91. 0x0000, /* R56 */
  92. 0x002D, /* R57 - Analogue OUT1 Left */
  93. 0x002D, /* R58 - Analogue OUT1 Right */
  94. 0x0039, /* R59 - Analogue OUT2 Left */
  95. 0x0039, /* R60 - Analogue OUT2 Right */
  96. 0x0100, /* R61 */
  97. 0x0139, /* R62 - Analogue OUT3 Left */
  98. 0x0139, /* R63 - Analogue OUT3 Right */
  99. 0x0000, /* R64 */
  100. 0x0000, /* R65 - Analogue SPK Output Control 0 */
  101. 0x0000, /* R66 */
  102. 0x0010, /* R67 - DC Servo 0 */
  103. 0x0100, /* R68 */
  104. 0x00A4, /* R69 - DC Servo 2 */
  105. 0x0807, /* R70 */
  106. 0x0000, /* R71 */
  107. 0x0000, /* R72 */
  108. 0x0000, /* R73 */
  109. 0x0000, /* R74 */
  110. 0x0000, /* R75 */
  111. 0x0000, /* R76 */
  112. 0x0000, /* R77 */
  113. 0x0000, /* R78 */
  114. 0x000E, /* R79 */
  115. 0x0000, /* R80 */
  116. 0x0000, /* R81 */
  117. 0x0000, /* R82 */
  118. 0x0000, /* R83 */
  119. 0x0000, /* R84 */
  120. 0x0000, /* R85 */
  121. 0x0000, /* R86 */
  122. 0x0006, /* R87 */
  123. 0x0000, /* R88 */
  124. 0x0000, /* R89 */
  125. 0x0000, /* R90 - Analogue HP 0 */
  126. 0x0060, /* R91 */
  127. 0x0000, /* R92 */
  128. 0x0000, /* R93 */
  129. 0x0000, /* R94 - Analogue Lineout 0 */
  130. 0x0060, /* R95 */
  131. 0x0000, /* R96 */
  132. 0x0000, /* R97 */
  133. 0x0000, /* R98 - Charge Pump 0 */
  134. 0x1F25, /* R99 */
  135. 0x2B19, /* R100 */
  136. 0x01C0, /* R101 */
  137. 0x01EF, /* R102 */
  138. 0x2B00, /* R103 */
  139. 0x0000, /* R104 - Class W 0 */
  140. 0x01C0, /* R105 */
  141. 0x1C10, /* R106 */
  142. 0x0000, /* R107 */
  143. 0x0000, /* R108 - Write Sequencer 0 */
  144. 0x0000, /* R109 - Write Sequencer 1 */
  145. 0x0000, /* R110 - Write Sequencer 2 */
  146. 0x0000, /* R111 - Write Sequencer 3 */
  147. 0x0000, /* R112 - Write Sequencer 4 */
  148. 0x0000, /* R113 */
  149. 0x0000, /* R114 - Control Interface */
  150. 0x0000, /* R115 */
  151. 0x00A8, /* R116 - GPIO Control 1 */
  152. 0x00A8, /* R117 - GPIO Control 2 */
  153. 0x00A8, /* R118 - GPIO Control 3 */
  154. 0x0220, /* R119 - GPIO Control 4 */
  155. 0x01A0, /* R120 - GPIO Control 5 */
  156. 0x0000, /* R121 - Interrupt Status 1 */
  157. 0xFFFF, /* R122 - Interrupt Status 1 Mask */
  158. 0x0000, /* R123 - Interrupt Polarity 1 */
  159. 0x0000, /* R124 */
  160. 0x0003, /* R125 */
  161. 0x0000, /* R126 - Interrupt Control */
  162. 0x0000, /* R127 */
  163. 0x0005, /* R128 */
  164. 0x0000, /* R129 - Control Interface Test 1 */
  165. 0x0000, /* R130 */
  166. 0x0000, /* R131 */
  167. 0x0000, /* R132 */
  168. 0x0000, /* R133 */
  169. 0x0000, /* R134 */
  170. 0x03FF, /* R135 */
  171. 0x0007, /* R136 */
  172. 0x0040, /* R137 */
  173. 0x0000, /* R138 */
  174. 0x0000, /* R139 */
  175. 0x0000, /* R140 */
  176. 0x0000, /* R141 */
  177. 0x0000, /* R142 */
  178. 0x0000, /* R143 */
  179. 0x0000, /* R144 */
  180. 0x0000, /* R145 */
  181. 0x0000, /* R146 */
  182. 0x0000, /* R147 */
  183. 0x4000, /* R148 */
  184. 0x6810, /* R149 - Charge Pump Test 1 */
  185. 0x0004, /* R150 */
  186. 0x0000, /* R151 */
  187. 0x0000, /* R152 */
  188. 0x0000, /* R153 */
  189. 0x0000, /* R154 */
  190. 0x0000, /* R155 */
  191. 0x0000, /* R156 */
  192. 0x0000, /* R157 */
  193. 0x0000, /* R158 */
  194. 0x0000, /* R159 */
  195. 0x0000, /* R160 */
  196. 0x0000, /* R161 */
  197. 0x0000, /* R162 */
  198. 0x0000, /* R163 */
  199. 0x0028, /* R164 - Clock Rate Test 4 */
  200. 0x0004, /* R165 */
  201. 0x0000, /* R166 */
  202. 0x0060, /* R167 */
  203. 0x0000, /* R168 */
  204. 0x0000, /* R169 */
  205. 0x0000, /* R170 */
  206. 0x0000, /* R171 */
  207. 0x0000, /* R172 - Analogue Output Bias 0 */
  208. };
  209. struct wm8903_priv {
  210. struct snd_soc_codec codec;
  211. u16 reg_cache[ARRAY_SIZE(wm8903_reg_defaults)];
  212. int sysclk;
  213. /* Reference counts */
  214. int charge_pump_users;
  215. int class_w_users;
  216. int playback_active;
  217. int capture_active;
  218. struct snd_pcm_substream *master_substream;
  219. struct snd_pcm_substream *slave_substream;
  220. };
  221. static unsigned int wm8903_read_reg_cache(struct snd_soc_codec *codec,
  222. unsigned int reg)
  223. {
  224. u16 *cache = codec->reg_cache;
  225. BUG_ON(reg >= ARRAY_SIZE(wm8903_reg_defaults));
  226. return cache[reg];
  227. }
  228. static unsigned int wm8903_hw_read(struct snd_soc_codec *codec, u8 reg)
  229. {
  230. struct i2c_msg xfer[2];
  231. u16 data;
  232. int ret;
  233. struct i2c_client *client = codec->control_data;
  234. /* Write register */
  235. xfer[0].addr = client->addr;
  236. xfer[0].flags = 0;
  237. xfer[0].len = 1;
  238. xfer[0].buf = &reg;
  239. /* Read data */
  240. xfer[1].addr = client->addr;
  241. xfer[1].flags = I2C_M_RD;
  242. xfer[1].len = 2;
  243. xfer[1].buf = (u8 *)&data;
  244. ret = i2c_transfer(client->adapter, xfer, 2);
  245. if (ret != 2) {
  246. pr_err("i2c_transfer returned %d\n", ret);
  247. return 0;
  248. }
  249. return (data >> 8) | ((data & 0xff) << 8);
  250. }
  251. static unsigned int wm8903_read(struct snd_soc_codec *codec,
  252. unsigned int reg)
  253. {
  254. switch (reg) {
  255. case WM8903_SW_RESET_AND_ID:
  256. case WM8903_REVISION_NUMBER:
  257. case WM8903_INTERRUPT_STATUS_1:
  258. case WM8903_WRITE_SEQUENCER_4:
  259. return wm8903_hw_read(codec, reg);
  260. default:
  261. return wm8903_read_reg_cache(codec, reg);
  262. }
  263. }
  264. static void wm8903_write_reg_cache(struct snd_soc_codec *codec,
  265. u16 reg, unsigned int value)
  266. {
  267. u16 *cache = codec->reg_cache;
  268. BUG_ON(reg >= ARRAY_SIZE(wm8903_reg_defaults));
  269. switch (reg) {
  270. case WM8903_SW_RESET_AND_ID:
  271. case WM8903_REVISION_NUMBER:
  272. break;
  273. default:
  274. cache[reg] = value;
  275. break;
  276. }
  277. }
  278. static int wm8903_write(struct snd_soc_codec *codec, unsigned int reg,
  279. unsigned int value)
  280. {
  281. u8 data[3];
  282. wm8903_write_reg_cache(codec, reg, value);
  283. /* Data format is 1 byte of address followed by 2 bytes of data */
  284. data[0] = reg;
  285. data[1] = (value >> 8) & 0xff;
  286. data[2] = value & 0xff;
  287. if (codec->hw_write(codec->control_data, data, 3) == 2)
  288. return 0;
  289. else
  290. return -EIO;
  291. }
  292. static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
  293. {
  294. u16 reg[5];
  295. struct i2c_client *i2c = codec->control_data;
  296. BUG_ON(start > 48);
  297. /* Enable the sequencer */
  298. reg[0] = wm8903_read(codec, WM8903_WRITE_SEQUENCER_0);
  299. reg[0] |= WM8903_WSEQ_ENA;
  300. wm8903_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
  301. dev_dbg(&i2c->dev, "Starting sequence at %d\n", start);
  302. wm8903_write(codec, WM8903_WRITE_SEQUENCER_3,
  303. start | WM8903_WSEQ_START);
  304. /* Wait for it to complete. If we have the interrupt wired up then
  305. * we could block waiting for an interrupt, though polling may still
  306. * be desirable for diagnostic purposes.
  307. */
  308. do {
  309. msleep(10);
  310. reg[4] = wm8903_read(codec, WM8903_WRITE_SEQUENCER_4);
  311. } while (reg[4] & WM8903_WSEQ_BUSY);
  312. dev_dbg(&i2c->dev, "Sequence complete\n");
  313. /* Disable the sequencer again */
  314. wm8903_write(codec, WM8903_WRITE_SEQUENCER_0,
  315. reg[0] & ~WM8903_WSEQ_ENA);
  316. return 0;
  317. }
  318. static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
  319. {
  320. int i;
  321. /* There really ought to be something better we can do here :/ */
  322. for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
  323. cache[i] = wm8903_hw_read(codec, i);
  324. }
  325. static void wm8903_reset(struct snd_soc_codec *codec)
  326. {
  327. wm8903_write(codec, WM8903_SW_RESET_AND_ID, 0);
  328. memcpy(codec->reg_cache, wm8903_reg_defaults,
  329. sizeof(wm8903_reg_defaults));
  330. }
  331. #define WM8903_OUTPUT_SHORT 0x8
  332. #define WM8903_OUTPUT_OUT 0x4
  333. #define WM8903_OUTPUT_INT 0x2
  334. #define WM8903_OUTPUT_IN 0x1
  335. /*
  336. * Event for headphone and line out amplifier power changes. Special
  337. * power up/down sequences are required in order to maximise pop/click
  338. * performance.
  339. */
  340. static int wm8903_output_event(struct snd_soc_dapm_widget *w,
  341. struct snd_kcontrol *kcontrol, int event)
  342. {
  343. struct snd_soc_codec *codec = w->codec;
  344. struct wm8903_priv *wm8903 = codec->private_data;
  345. struct i2c_client *i2c = codec->control_data;
  346. u16 val;
  347. u16 reg;
  348. int shift;
  349. u16 cp_reg = wm8903_read(codec, WM8903_CHARGE_PUMP_0);
  350. switch (w->reg) {
  351. case WM8903_POWER_MANAGEMENT_2:
  352. reg = WM8903_ANALOGUE_HP_0;
  353. break;
  354. case WM8903_POWER_MANAGEMENT_3:
  355. reg = WM8903_ANALOGUE_LINEOUT_0;
  356. break;
  357. default:
  358. BUG();
  359. return -EINVAL; /* Spurious warning from some compilers */
  360. }
  361. switch (w->shift) {
  362. case 0:
  363. shift = 0;
  364. break;
  365. case 1:
  366. shift = 4;
  367. break;
  368. default:
  369. BUG();
  370. return -EINVAL; /* Spurious warning from some compilers */
  371. }
  372. if (event & SND_SOC_DAPM_PRE_PMU) {
  373. val = wm8903_read(codec, reg);
  374. /* Short the output */
  375. val &= ~(WM8903_OUTPUT_SHORT << shift);
  376. wm8903_write(codec, reg, val);
  377. wm8903->charge_pump_users++;
  378. dev_dbg(&i2c->dev, "Charge pump use count now %d\n",
  379. wm8903->charge_pump_users);
  380. if (wm8903->charge_pump_users == 1) {
  381. dev_dbg(&i2c->dev, "Enabling charge pump\n");
  382. wm8903_write(codec, WM8903_CHARGE_PUMP_0,
  383. cp_reg | WM8903_CP_ENA);
  384. mdelay(4);
  385. }
  386. }
  387. if (event & SND_SOC_DAPM_POST_PMU) {
  388. val = wm8903_read(codec, reg);
  389. val |= (WM8903_OUTPUT_IN << shift);
  390. wm8903_write(codec, reg, val);
  391. val |= (WM8903_OUTPUT_INT << shift);
  392. wm8903_write(codec, reg, val);
  393. /* Turn on the output ENA_OUTP */
  394. val |= (WM8903_OUTPUT_OUT << shift);
  395. wm8903_write(codec, reg, val);
  396. /* Remove the short */
  397. val |= (WM8903_OUTPUT_SHORT << shift);
  398. wm8903_write(codec, reg, val);
  399. }
  400. if (event & SND_SOC_DAPM_PRE_PMD) {
  401. val = wm8903_read(codec, reg);
  402. /* Short the output */
  403. val &= ~(WM8903_OUTPUT_SHORT << shift);
  404. wm8903_write(codec, reg, val);
  405. /* Then disable the intermediate and output stages */
  406. val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
  407. WM8903_OUTPUT_IN) << shift);
  408. wm8903_write(codec, reg, val);
  409. }
  410. if (event & SND_SOC_DAPM_POST_PMD) {
  411. wm8903->charge_pump_users--;
  412. dev_dbg(&i2c->dev, "Charge pump use count now %d\n",
  413. wm8903->charge_pump_users);
  414. if (wm8903->charge_pump_users == 0) {
  415. dev_dbg(&i2c->dev, "Disabling charge pump\n");
  416. wm8903_write(codec, WM8903_CHARGE_PUMP_0,
  417. cp_reg & ~WM8903_CP_ENA);
  418. }
  419. }
  420. return 0;
  421. }
  422. /*
  423. * When used with DAC outputs only the WM8903 charge pump supports
  424. * operation in class W mode, providing very low power consumption
  425. * when used with digital sources. Enable and disable this mode
  426. * automatically depending on the mixer configuration.
  427. *
  428. * All the relevant controls are simple switches.
  429. */
  430. static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
  431. struct snd_ctl_elem_value *ucontrol)
  432. {
  433. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  434. struct snd_soc_codec *codec = widget->codec;
  435. struct wm8903_priv *wm8903 = codec->private_data;
  436. struct i2c_client *i2c = codec->control_data;
  437. u16 reg;
  438. int ret;
  439. reg = wm8903_read(codec, WM8903_CLASS_W_0);
  440. /* Turn it off if we're about to enable bypass */
  441. if (ucontrol->value.integer.value[0]) {
  442. if (wm8903->class_w_users == 0) {
  443. dev_dbg(&i2c->dev, "Disabling Class W\n");
  444. wm8903_write(codec, WM8903_CLASS_W_0, reg &
  445. ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
  446. }
  447. wm8903->class_w_users++;
  448. }
  449. /* Implement the change */
  450. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  451. /* If we've just disabled the last bypass path turn Class W on */
  452. if (!ucontrol->value.integer.value[0]) {
  453. if (wm8903->class_w_users == 1) {
  454. dev_dbg(&i2c->dev, "Enabling Class W\n");
  455. wm8903_write(codec, WM8903_CLASS_W_0, reg |
  456. WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
  457. }
  458. wm8903->class_w_users--;
  459. }
  460. dev_dbg(&i2c->dev, "Bypass use count now %d\n",
  461. wm8903->class_w_users);
  462. return ret;
  463. }
  464. #define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
  465. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  466. .info = snd_soc_info_volsw, \
  467. .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
  468. .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
  469. /* ALSA can only do steps of .01dB */
  470. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  471. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  472. static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
  473. static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
  474. static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
  475. static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
  476. static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
  477. static const char *drc_slope_text[] = {
  478. "1", "1/2", "1/4", "1/8", "1/16", "0"
  479. };
  480. static const struct soc_enum drc_slope_r0 =
  481. SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
  482. static const struct soc_enum drc_slope_r1 =
  483. SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
  484. static const char *drc_attack_text[] = {
  485. "instantaneous",
  486. "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
  487. "46.4ms", "92.8ms", "185.6ms"
  488. };
  489. static const struct soc_enum drc_attack =
  490. SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
  491. static const char *drc_decay_text[] = {
  492. "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
  493. "23.87s", "47.56s"
  494. };
  495. static const struct soc_enum drc_decay =
  496. SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
  497. static const char *drc_ff_delay_text[] = {
  498. "5 samples", "9 samples"
  499. };
  500. static const struct soc_enum drc_ff_delay =
  501. SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
  502. static const char *drc_qr_decay_text[] = {
  503. "0.725ms", "1.45ms", "5.8ms"
  504. };
  505. static const struct soc_enum drc_qr_decay =
  506. SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
  507. static const char *drc_smoothing_text[] = {
  508. "Low", "Medium", "High"
  509. };
  510. static const struct soc_enum drc_smoothing =
  511. SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
  512. static const char *soft_mute_text[] = {
  513. "Fast (fs/2)", "Slow (fs/32)"
  514. };
  515. static const struct soc_enum soft_mute =
  516. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
  517. static const char *mute_mode_text[] = {
  518. "Hard", "Soft"
  519. };
  520. static const struct soc_enum mute_mode =
  521. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
  522. static const char *dac_deemphasis_text[] = {
  523. "Disabled", "32kHz", "44.1kHz", "48kHz"
  524. };
  525. static const struct soc_enum dac_deemphasis =
  526. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
  527. static const char *companding_text[] = {
  528. "ulaw", "alaw"
  529. };
  530. static const struct soc_enum dac_companding =
  531. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
  532. static const struct soc_enum adc_companding =
  533. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
  534. static const char *input_mode_text[] = {
  535. "Single-Ended", "Differential Line", "Differential Mic"
  536. };
  537. static const struct soc_enum linput_mode_enum =
  538. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  539. static const struct soc_enum rinput_mode_enum =
  540. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  541. static const char *linput_mux_text[] = {
  542. "IN1L", "IN2L", "IN3L"
  543. };
  544. static const struct soc_enum linput_enum =
  545. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
  546. static const struct soc_enum linput_inv_enum =
  547. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
  548. static const char *rinput_mux_text[] = {
  549. "IN1R", "IN2R", "IN3R"
  550. };
  551. static const struct soc_enum rinput_enum =
  552. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
  553. static const struct soc_enum rinput_inv_enum =
  554. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
  555. static const struct snd_kcontrol_new wm8903_snd_controls[] = {
  556. /* Input PGAs - No TLV since the scale depends on PGA mode */
  557. SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
  558. 7, 1, 1),
  559. SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
  560. 0, 31, 0),
  561. SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
  562. 6, 1, 0),
  563. SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
  564. 7, 1, 1),
  565. SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
  566. 0, 31, 0),
  567. SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
  568. 6, 1, 0),
  569. /* ADCs */
  570. SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
  571. SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
  572. SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
  573. SOC_SINGLE_TLV("DRC Compressor Threashold Volume", WM8903_DRC_3, 5, 124, 1,
  574. drc_tlv_thresh),
  575. SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
  576. SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
  577. SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
  578. SOC_ENUM("DRC Attack Rate", drc_attack),
  579. SOC_ENUM("DRC Decay Rate", drc_decay),
  580. SOC_ENUM("DRC FF Delay", drc_ff_delay),
  581. SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
  582. SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
  583. SOC_SINGLE_TLV("DRC QR Threashold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
  584. SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
  585. SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
  586. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
  587. SOC_ENUM("DRC Smoothing Threashold", drc_smoothing),
  588. SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
  589. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
  590. WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  591. SOC_ENUM("ADC Companding Mode", adc_companding),
  592. SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
  593. /* DAC */
  594. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
  595. WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
  596. SOC_ENUM("DAC Soft Mute Rate", soft_mute),
  597. SOC_ENUM("DAC Mute Mode", mute_mode),
  598. SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
  599. SOC_ENUM("DAC De-emphasis", dac_deemphasis),
  600. SOC_SINGLE("DAC Sloping Stopband Filter Switch",
  601. WM8903_DAC_DIGITAL_1, 11, 1, 0),
  602. SOC_ENUM("DAC Companding Mode", dac_companding),
  603. SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
  604. /* Headphones */
  605. SOC_DOUBLE_R("Headphone Switch",
  606. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  607. 8, 1, 1),
  608. SOC_DOUBLE_R("Headphone ZC Switch",
  609. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  610. 6, 1, 0),
  611. SOC_DOUBLE_R_TLV("Headphone Volume",
  612. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  613. 0, 63, 0, out_tlv),
  614. /* Line out */
  615. SOC_DOUBLE_R("Line Out Switch",
  616. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  617. 8, 1, 1),
  618. SOC_DOUBLE_R("Line Out ZC Switch",
  619. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  620. 6, 1, 0),
  621. SOC_DOUBLE_R_TLV("Line Out Volume",
  622. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  623. 0, 63, 0, out_tlv),
  624. /* Speaker */
  625. SOC_DOUBLE_R("Speaker Switch",
  626. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
  627. SOC_DOUBLE_R("Speaker ZC Switch",
  628. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
  629. SOC_DOUBLE_R_TLV("Speaker Volume",
  630. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
  631. 0, 63, 0, out_tlv),
  632. };
  633. static int wm8903_add_controls(struct snd_soc_codec *codec)
  634. {
  635. int err, i;
  636. for (i = 0; i < ARRAY_SIZE(wm8903_snd_controls); i++) {
  637. err = snd_ctl_add(codec->card,
  638. snd_soc_cnew(&wm8903_snd_controls[i],
  639. codec, NULL));
  640. if (err < 0)
  641. return err;
  642. }
  643. return 0;
  644. }
  645. static const struct snd_kcontrol_new linput_mode_mux =
  646. SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
  647. static const struct snd_kcontrol_new rinput_mode_mux =
  648. SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
  649. static const struct snd_kcontrol_new linput_mux =
  650. SOC_DAPM_ENUM("Left Input Mux", linput_enum);
  651. static const struct snd_kcontrol_new linput_inv_mux =
  652. SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
  653. static const struct snd_kcontrol_new rinput_mux =
  654. SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
  655. static const struct snd_kcontrol_new rinput_inv_mux =
  656. SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
  657. static const struct snd_kcontrol_new left_output_mixer[] = {
  658. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
  659. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
  660. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
  661. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
  662. };
  663. static const struct snd_kcontrol_new right_output_mixer[] = {
  664. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
  665. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
  666. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
  667. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
  668. };
  669. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  670. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
  671. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
  672. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
  673. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
  674. 0, 1, 0),
  675. };
  676. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  677. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
  678. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
  679. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  680. 1, 1, 0),
  681. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  682. 0, 1, 0),
  683. };
  684. static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
  685. SND_SOC_DAPM_INPUT("IN1L"),
  686. SND_SOC_DAPM_INPUT("IN1R"),
  687. SND_SOC_DAPM_INPUT("IN2L"),
  688. SND_SOC_DAPM_INPUT("IN2R"),
  689. SND_SOC_DAPM_INPUT("IN3L"),
  690. SND_SOC_DAPM_INPUT("IN3R"),
  691. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  692. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  693. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  694. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  695. SND_SOC_DAPM_OUTPUT("LOP"),
  696. SND_SOC_DAPM_OUTPUT("LON"),
  697. SND_SOC_DAPM_OUTPUT("ROP"),
  698. SND_SOC_DAPM_OUTPUT("RON"),
  699. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
  700. SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
  701. SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  702. &linput_inv_mux),
  703. SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
  704. SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
  705. SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  706. &rinput_inv_mux),
  707. SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
  708. SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
  709. SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
  710. SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
  711. SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
  712. SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
  713. SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
  714. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
  715. left_output_mixer, ARRAY_SIZE(left_output_mixer)),
  716. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
  717. right_output_mixer, ARRAY_SIZE(right_output_mixer)),
  718. SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
  719. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  720. SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
  721. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  722. SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
  723. 1, 0, NULL, 0, wm8903_output_event,
  724. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  725. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  726. SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
  727. 0, 0, NULL, 0, wm8903_output_event,
  728. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  729. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  730. SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
  731. NULL, 0, wm8903_output_event,
  732. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  733. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  734. SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
  735. NULL, 0, wm8903_output_event,
  736. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  737. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  738. SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
  739. NULL, 0),
  740. SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
  741. NULL, 0),
  742. };
  743. static const struct snd_soc_dapm_route intercon[] = {
  744. { "Left Input Mux", "IN1L", "IN1L" },
  745. { "Left Input Mux", "IN2L", "IN2L" },
  746. { "Left Input Mux", "IN3L", "IN3L" },
  747. { "Left Input Inverting Mux", "IN1L", "IN1L" },
  748. { "Left Input Inverting Mux", "IN2L", "IN2L" },
  749. { "Left Input Inverting Mux", "IN3L", "IN3L" },
  750. { "Right Input Mux", "IN1R", "IN1R" },
  751. { "Right Input Mux", "IN2R", "IN2R" },
  752. { "Right Input Mux", "IN3R", "IN3R" },
  753. { "Right Input Inverting Mux", "IN1R", "IN1R" },
  754. { "Right Input Inverting Mux", "IN2R", "IN2R" },
  755. { "Right Input Inverting Mux", "IN3R", "IN3R" },
  756. { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
  757. { "Left Input Mode Mux", "Differential Line",
  758. "Left Input Mux" },
  759. { "Left Input Mode Mux", "Differential Line",
  760. "Left Input Inverting Mux" },
  761. { "Left Input Mode Mux", "Differential Mic",
  762. "Left Input Mux" },
  763. { "Left Input Mode Mux", "Differential Mic",
  764. "Left Input Inverting Mux" },
  765. { "Right Input Mode Mux", "Single-Ended",
  766. "Right Input Inverting Mux" },
  767. { "Right Input Mode Mux", "Differential Line",
  768. "Right Input Mux" },
  769. { "Right Input Mode Mux", "Differential Line",
  770. "Right Input Inverting Mux" },
  771. { "Right Input Mode Mux", "Differential Mic",
  772. "Right Input Mux" },
  773. { "Right Input Mode Mux", "Differential Mic",
  774. "Right Input Inverting Mux" },
  775. { "Left Input PGA", NULL, "Left Input Mode Mux" },
  776. { "Right Input PGA", NULL, "Right Input Mode Mux" },
  777. { "ADCL", NULL, "Left Input PGA" },
  778. { "ADCR", NULL, "Right Input PGA" },
  779. { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  780. { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  781. { "Left Output Mixer", "DACL Switch", "DACL" },
  782. { "Left Output Mixer", "DACR Switch", "DACR" },
  783. { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  784. { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  785. { "Right Output Mixer", "DACL Switch", "DACL" },
  786. { "Right Output Mixer", "DACR Switch", "DACR" },
  787. { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  788. { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  789. { "Left Speaker Mixer", "DACL Switch", "DACL" },
  790. { "Left Speaker Mixer", "DACR Switch", "DACR" },
  791. { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  792. { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  793. { "Right Speaker Mixer", "DACL Switch", "DACL" },
  794. { "Right Speaker Mixer", "DACR Switch", "DACR" },
  795. { "Left Line Output PGA", NULL, "Left Output Mixer" },
  796. { "Right Line Output PGA", NULL, "Right Output Mixer" },
  797. { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
  798. { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
  799. { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
  800. { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
  801. { "HPOUTL", NULL, "Left Headphone Output PGA" },
  802. { "HPOUTR", NULL, "Right Headphone Output PGA" },
  803. { "LINEOUTL", NULL, "Left Line Output PGA" },
  804. { "LINEOUTR", NULL, "Right Line Output PGA" },
  805. { "LOP", NULL, "Left Speaker PGA" },
  806. { "LON", NULL, "Left Speaker PGA" },
  807. { "ROP", NULL, "Right Speaker PGA" },
  808. { "RON", NULL, "Right Speaker PGA" },
  809. };
  810. static int wm8903_add_widgets(struct snd_soc_codec *codec)
  811. {
  812. snd_soc_dapm_new_controls(codec, wm8903_dapm_widgets,
  813. ARRAY_SIZE(wm8903_dapm_widgets));
  814. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  815. snd_soc_dapm_new_widgets(codec);
  816. return 0;
  817. }
  818. static int wm8903_set_bias_level(struct snd_soc_codec *codec,
  819. enum snd_soc_bias_level level)
  820. {
  821. struct i2c_client *i2c = codec->control_data;
  822. u16 reg, reg2;
  823. switch (level) {
  824. case SND_SOC_BIAS_ON:
  825. case SND_SOC_BIAS_PREPARE:
  826. reg = wm8903_read(codec, WM8903_VMID_CONTROL_0);
  827. reg &= ~(WM8903_VMID_RES_MASK);
  828. reg |= WM8903_VMID_RES_50K;
  829. wm8903_write(codec, WM8903_VMID_CONTROL_0, reg);
  830. break;
  831. case SND_SOC_BIAS_STANDBY:
  832. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  833. wm8903_write(codec, WM8903_CLOCK_RATES_2,
  834. WM8903_CLK_SYS_ENA);
  835. wm8903_run_sequence(codec, 0);
  836. wm8903_sync_reg_cache(codec, codec->reg_cache);
  837. /* Enable low impedence charge pump output */
  838. reg = wm8903_read(codec,
  839. WM8903_CONTROL_INTERFACE_TEST_1);
  840. wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
  841. reg | WM8903_TEST_KEY);
  842. reg2 = wm8903_read(codec, WM8903_CHARGE_PUMP_TEST_1);
  843. wm8903_write(codec, WM8903_CHARGE_PUMP_TEST_1,
  844. reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
  845. wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
  846. reg);
  847. /* By default no bypass paths are enabled so
  848. * enable Class W support.
  849. */
  850. dev_dbg(&i2c->dev, "Enabling Class W\n");
  851. wm8903_write(codec, WM8903_CLASS_W_0, reg |
  852. WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
  853. }
  854. reg = wm8903_read(codec, WM8903_VMID_CONTROL_0);
  855. reg &= ~(WM8903_VMID_RES_MASK);
  856. reg |= WM8903_VMID_RES_250K;
  857. wm8903_write(codec, WM8903_VMID_CONTROL_0, reg);
  858. break;
  859. case SND_SOC_BIAS_OFF:
  860. wm8903_run_sequence(codec, 32);
  861. reg = wm8903_read(codec, WM8903_CLOCK_RATES_2);
  862. reg &= ~WM8903_CLK_SYS_ENA;
  863. wm8903_write(codec, WM8903_CLOCK_RATES_2, reg);
  864. break;
  865. }
  866. codec->bias_level = level;
  867. return 0;
  868. }
  869. static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  870. int clk_id, unsigned int freq, int dir)
  871. {
  872. struct snd_soc_codec *codec = codec_dai->codec;
  873. struct wm8903_priv *wm8903 = codec->private_data;
  874. wm8903->sysclk = freq;
  875. return 0;
  876. }
  877. static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
  878. unsigned int fmt)
  879. {
  880. struct snd_soc_codec *codec = codec_dai->codec;
  881. u16 aif1 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_1);
  882. aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
  883. WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
  884. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  885. case SND_SOC_DAIFMT_CBS_CFS:
  886. break;
  887. case SND_SOC_DAIFMT_CBS_CFM:
  888. aif1 |= WM8903_LRCLK_DIR;
  889. break;
  890. case SND_SOC_DAIFMT_CBM_CFM:
  891. aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
  892. break;
  893. case SND_SOC_DAIFMT_CBM_CFS:
  894. aif1 |= WM8903_BCLK_DIR;
  895. break;
  896. default:
  897. return -EINVAL;
  898. }
  899. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  900. case SND_SOC_DAIFMT_DSP_A:
  901. aif1 |= 0x3;
  902. break;
  903. case SND_SOC_DAIFMT_DSP_B:
  904. aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
  905. break;
  906. case SND_SOC_DAIFMT_I2S:
  907. aif1 |= 0x2;
  908. break;
  909. case SND_SOC_DAIFMT_RIGHT_J:
  910. aif1 |= 0x1;
  911. break;
  912. case SND_SOC_DAIFMT_LEFT_J:
  913. break;
  914. default:
  915. return -EINVAL;
  916. }
  917. /* Clock inversion */
  918. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  919. case SND_SOC_DAIFMT_DSP_A:
  920. case SND_SOC_DAIFMT_DSP_B:
  921. /* frame inversion not valid for DSP modes */
  922. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  923. case SND_SOC_DAIFMT_NB_NF:
  924. break;
  925. case SND_SOC_DAIFMT_IB_NF:
  926. aif1 |= WM8903_AIF_BCLK_INV;
  927. break;
  928. default:
  929. return -EINVAL;
  930. }
  931. break;
  932. case SND_SOC_DAIFMT_I2S:
  933. case SND_SOC_DAIFMT_RIGHT_J:
  934. case SND_SOC_DAIFMT_LEFT_J:
  935. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  936. case SND_SOC_DAIFMT_NB_NF:
  937. break;
  938. case SND_SOC_DAIFMT_IB_IF:
  939. aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
  940. break;
  941. case SND_SOC_DAIFMT_IB_NF:
  942. aif1 |= WM8903_AIF_BCLK_INV;
  943. break;
  944. case SND_SOC_DAIFMT_NB_IF:
  945. aif1 |= WM8903_AIF_LRCLK_INV;
  946. break;
  947. default:
  948. return -EINVAL;
  949. }
  950. break;
  951. default:
  952. return -EINVAL;
  953. }
  954. wm8903_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  955. return 0;
  956. }
  957. static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  958. {
  959. struct snd_soc_codec *codec = codec_dai->codec;
  960. u16 reg;
  961. reg = wm8903_read(codec, WM8903_DAC_DIGITAL_1);
  962. if (mute)
  963. reg |= WM8903_DAC_MUTE;
  964. else
  965. reg &= ~WM8903_DAC_MUTE;
  966. wm8903_write(codec, WM8903_DAC_DIGITAL_1, reg);
  967. return 0;
  968. }
  969. /* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
  970. * for optimal performance so we list the lower rates first and match
  971. * on the last match we find. */
  972. static struct {
  973. int div;
  974. int rate;
  975. int mode;
  976. int mclk_div;
  977. } clk_sys_ratios[] = {
  978. { 64, 0x0, 0x0, 1 },
  979. { 68, 0x0, 0x1, 1 },
  980. { 125, 0x0, 0x2, 1 },
  981. { 128, 0x1, 0x0, 1 },
  982. { 136, 0x1, 0x1, 1 },
  983. { 192, 0x2, 0x0, 1 },
  984. { 204, 0x2, 0x1, 1 },
  985. { 64, 0x0, 0x0, 2 },
  986. { 68, 0x0, 0x1, 2 },
  987. { 125, 0x0, 0x2, 2 },
  988. { 128, 0x1, 0x0, 2 },
  989. { 136, 0x1, 0x1, 2 },
  990. { 192, 0x2, 0x0, 2 },
  991. { 204, 0x2, 0x1, 2 },
  992. { 250, 0x2, 0x2, 1 },
  993. { 256, 0x3, 0x0, 1 },
  994. { 272, 0x3, 0x1, 1 },
  995. { 384, 0x4, 0x0, 1 },
  996. { 408, 0x4, 0x1, 1 },
  997. { 375, 0x4, 0x2, 1 },
  998. { 512, 0x5, 0x0, 1 },
  999. { 544, 0x5, 0x1, 1 },
  1000. { 500, 0x5, 0x2, 1 },
  1001. { 768, 0x6, 0x0, 1 },
  1002. { 816, 0x6, 0x1, 1 },
  1003. { 750, 0x6, 0x2, 1 },
  1004. { 1024, 0x7, 0x0, 1 },
  1005. { 1088, 0x7, 0x1, 1 },
  1006. { 1000, 0x7, 0x2, 1 },
  1007. { 1408, 0x8, 0x0, 1 },
  1008. { 1496, 0x8, 0x1, 1 },
  1009. { 1536, 0x9, 0x0, 1 },
  1010. { 1632, 0x9, 0x1, 1 },
  1011. { 1500, 0x9, 0x2, 1 },
  1012. { 250, 0x2, 0x2, 2 },
  1013. { 256, 0x3, 0x0, 2 },
  1014. { 272, 0x3, 0x1, 2 },
  1015. { 384, 0x4, 0x0, 2 },
  1016. { 408, 0x4, 0x1, 2 },
  1017. { 375, 0x4, 0x2, 2 },
  1018. { 512, 0x5, 0x0, 2 },
  1019. { 544, 0x5, 0x1, 2 },
  1020. { 500, 0x5, 0x2, 2 },
  1021. { 768, 0x6, 0x0, 2 },
  1022. { 816, 0x6, 0x1, 2 },
  1023. { 750, 0x6, 0x2, 2 },
  1024. { 1024, 0x7, 0x0, 2 },
  1025. { 1088, 0x7, 0x1, 2 },
  1026. { 1000, 0x7, 0x2, 2 },
  1027. { 1408, 0x8, 0x0, 2 },
  1028. { 1496, 0x8, 0x1, 2 },
  1029. { 1536, 0x9, 0x0, 2 },
  1030. { 1632, 0x9, 0x1, 2 },
  1031. { 1500, 0x9, 0x2, 2 },
  1032. };
  1033. /* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
  1034. static struct {
  1035. int ratio;
  1036. int div;
  1037. } bclk_divs[] = {
  1038. { 10, 0 },
  1039. { 15, 1 },
  1040. { 20, 2 },
  1041. { 30, 3 },
  1042. { 40, 4 },
  1043. { 50, 5 },
  1044. { 55, 6 },
  1045. { 60, 7 },
  1046. { 80, 8 },
  1047. { 100, 9 },
  1048. { 110, 10 },
  1049. { 120, 11 },
  1050. { 160, 12 },
  1051. { 200, 13 },
  1052. { 220, 14 },
  1053. { 240, 15 },
  1054. { 250, 16 },
  1055. { 300, 17 },
  1056. { 320, 18 },
  1057. { 440, 19 },
  1058. { 480, 20 },
  1059. };
  1060. /* Sample rates for DSP */
  1061. static struct {
  1062. int rate;
  1063. int value;
  1064. } sample_rates[] = {
  1065. { 8000, 0 },
  1066. { 11025, 1 },
  1067. { 12000, 2 },
  1068. { 16000, 3 },
  1069. { 22050, 4 },
  1070. { 24000, 5 },
  1071. { 32000, 6 },
  1072. { 44100, 7 },
  1073. { 48000, 8 },
  1074. { 88200, 9 },
  1075. { 96000, 10 },
  1076. { 0, 0 },
  1077. };
  1078. static int wm8903_startup(struct snd_pcm_substream *substream,
  1079. struct snd_soc_dai *dai)
  1080. {
  1081. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1082. struct snd_soc_device *socdev = rtd->socdev;
  1083. struct snd_soc_codec *codec = socdev->codec;
  1084. struct wm8903_priv *wm8903 = codec->private_data;
  1085. struct i2c_client *i2c = codec->control_data;
  1086. struct snd_pcm_runtime *master_runtime;
  1087. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1088. wm8903->playback_active++;
  1089. else
  1090. wm8903->capture_active++;
  1091. /* The DAI has shared clocks so if we already have a playback or
  1092. * capture going then constrain this substream to match it.
  1093. */
  1094. if (wm8903->master_substream) {
  1095. master_runtime = wm8903->master_substream->runtime;
  1096. dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
  1097. master_runtime->sample_bits,
  1098. master_runtime->rate);
  1099. snd_pcm_hw_constraint_minmax(substream->runtime,
  1100. SNDRV_PCM_HW_PARAM_RATE,
  1101. master_runtime->rate,
  1102. master_runtime->rate);
  1103. snd_pcm_hw_constraint_minmax(substream->runtime,
  1104. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1105. master_runtime->sample_bits,
  1106. master_runtime->sample_bits);
  1107. wm8903->slave_substream = substream;
  1108. } else
  1109. wm8903->master_substream = substream;
  1110. return 0;
  1111. }
  1112. static void wm8903_shutdown(struct snd_pcm_substream *substream,
  1113. struct snd_soc_dai *dai)
  1114. {
  1115. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1116. struct snd_soc_device *socdev = rtd->socdev;
  1117. struct snd_soc_codec *codec = socdev->codec;
  1118. struct wm8903_priv *wm8903 = codec->private_data;
  1119. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1120. wm8903->playback_active--;
  1121. else
  1122. wm8903->capture_active--;
  1123. if (wm8903->master_substream == substream)
  1124. wm8903->master_substream = wm8903->slave_substream;
  1125. wm8903->slave_substream = NULL;
  1126. }
  1127. static int wm8903_hw_params(struct snd_pcm_substream *substream,
  1128. struct snd_pcm_hw_params *params,
  1129. struct snd_soc_dai *dai)
  1130. {
  1131. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1132. struct snd_soc_device *socdev = rtd->socdev;
  1133. struct snd_soc_codec *codec = socdev->codec;
  1134. struct wm8903_priv *wm8903 = codec->private_data;
  1135. struct i2c_client *i2c = codec->control_data;
  1136. int fs = params_rate(params);
  1137. int bclk;
  1138. int bclk_div;
  1139. int i;
  1140. int dsp_config;
  1141. int clk_config;
  1142. int best_val;
  1143. int cur_val;
  1144. int clk_sys;
  1145. u16 aif1 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_1);
  1146. u16 aif2 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_2);
  1147. u16 aif3 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_3);
  1148. u16 clock0 = wm8903_read(codec, WM8903_CLOCK_RATES_0);
  1149. u16 clock1 = wm8903_read(codec, WM8903_CLOCK_RATES_1);
  1150. if (substream == wm8903->slave_substream) {
  1151. dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
  1152. return 0;
  1153. }
  1154. /* Configure sample rate logic for DSP - choose nearest rate */
  1155. dsp_config = 0;
  1156. best_val = abs(sample_rates[dsp_config].rate - fs);
  1157. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1158. cur_val = abs(sample_rates[i].rate - fs);
  1159. if (cur_val <= best_val) {
  1160. dsp_config = i;
  1161. best_val = cur_val;
  1162. }
  1163. }
  1164. /* Constraints should stop us hitting this but let's make sure */
  1165. if (wm8903->capture_active)
  1166. switch (sample_rates[dsp_config].rate) {
  1167. case 88200:
  1168. case 96000:
  1169. dev_err(&i2c->dev, "%dHz unsupported by ADC\n",
  1170. fs);
  1171. return -EINVAL;
  1172. default:
  1173. break;
  1174. }
  1175. dev_dbg(&i2c->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
  1176. clock1 &= ~WM8903_SAMPLE_RATE_MASK;
  1177. clock1 |= sample_rates[dsp_config].value;
  1178. aif1 &= ~WM8903_AIF_WL_MASK;
  1179. bclk = 2 * fs;
  1180. switch (params_format(params)) {
  1181. case SNDRV_PCM_FORMAT_S16_LE:
  1182. bclk *= 16;
  1183. break;
  1184. case SNDRV_PCM_FORMAT_S20_3LE:
  1185. bclk *= 20;
  1186. aif1 |= 0x4;
  1187. break;
  1188. case SNDRV_PCM_FORMAT_S24_LE:
  1189. bclk *= 24;
  1190. aif1 |= 0x8;
  1191. break;
  1192. case SNDRV_PCM_FORMAT_S32_LE:
  1193. bclk *= 32;
  1194. aif1 |= 0xc;
  1195. break;
  1196. default:
  1197. return -EINVAL;
  1198. }
  1199. dev_dbg(&i2c->dev, "MCLK = %dHz, target sample rate = %dHz\n",
  1200. wm8903->sysclk, fs);
  1201. /* We may not have an MCLK which allows us to generate exactly
  1202. * the clock we want, particularly with USB derived inputs, so
  1203. * approximate.
  1204. */
  1205. clk_config = 0;
  1206. best_val = abs((wm8903->sysclk /
  1207. (clk_sys_ratios[0].mclk_div *
  1208. clk_sys_ratios[0].div)) - fs);
  1209. for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
  1210. cur_val = abs((wm8903->sysclk /
  1211. (clk_sys_ratios[i].mclk_div *
  1212. clk_sys_ratios[i].div)) - fs);
  1213. if (cur_val <= best_val) {
  1214. clk_config = i;
  1215. best_val = cur_val;
  1216. }
  1217. }
  1218. if (clk_sys_ratios[clk_config].mclk_div == 2) {
  1219. clock0 |= WM8903_MCLKDIV2;
  1220. clk_sys = wm8903->sysclk / 2;
  1221. } else {
  1222. clock0 &= ~WM8903_MCLKDIV2;
  1223. clk_sys = wm8903->sysclk;
  1224. }
  1225. clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
  1226. WM8903_CLK_SYS_MODE_MASK);
  1227. clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
  1228. clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
  1229. dev_dbg(&i2c->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
  1230. clk_sys_ratios[clk_config].rate,
  1231. clk_sys_ratios[clk_config].mode,
  1232. clk_sys_ratios[clk_config].div);
  1233. dev_dbg(&i2c->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
  1234. /* We may not get quite the right frequency if using
  1235. * approximate clocks so look for the closest match that is
  1236. * higher than the target (we need to ensure that there enough
  1237. * BCLKs to clock out the samples).
  1238. */
  1239. bclk_div = 0;
  1240. best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
  1241. i = 1;
  1242. while (i < ARRAY_SIZE(bclk_divs)) {
  1243. cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
  1244. if (cur_val < 0) /* BCLK table is sorted */
  1245. break;
  1246. bclk_div = i;
  1247. best_val = cur_val;
  1248. i++;
  1249. }
  1250. aif2 &= ~WM8903_BCLK_DIV_MASK;
  1251. aif3 &= ~WM8903_LRCLK_RATE_MASK;
  1252. dev_dbg(&i2c->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
  1253. bclk_divs[bclk_div].ratio / 10, bclk,
  1254. (clk_sys * 10) / bclk_divs[bclk_div].ratio);
  1255. aif2 |= bclk_divs[bclk_div].div;
  1256. aif3 |= bclk / fs;
  1257. wm8903_write(codec, WM8903_CLOCK_RATES_0, clock0);
  1258. wm8903_write(codec, WM8903_CLOCK_RATES_1, clock1);
  1259. wm8903_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  1260. wm8903_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
  1261. wm8903_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
  1262. return 0;
  1263. }
  1264. #define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
  1265. SNDRV_PCM_RATE_11025 | \
  1266. SNDRV_PCM_RATE_16000 | \
  1267. SNDRV_PCM_RATE_22050 | \
  1268. SNDRV_PCM_RATE_32000 | \
  1269. SNDRV_PCM_RATE_44100 | \
  1270. SNDRV_PCM_RATE_48000 | \
  1271. SNDRV_PCM_RATE_88200 | \
  1272. SNDRV_PCM_RATE_96000)
  1273. #define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
  1274. SNDRV_PCM_RATE_11025 | \
  1275. SNDRV_PCM_RATE_16000 | \
  1276. SNDRV_PCM_RATE_22050 | \
  1277. SNDRV_PCM_RATE_32000 | \
  1278. SNDRV_PCM_RATE_44100 | \
  1279. SNDRV_PCM_RATE_48000)
  1280. #define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1281. SNDRV_PCM_FMTBIT_S20_3LE |\
  1282. SNDRV_PCM_FMTBIT_S24_LE)
  1283. struct snd_soc_dai wm8903_dai = {
  1284. .name = "WM8903",
  1285. .playback = {
  1286. .stream_name = "Playback",
  1287. .channels_min = 2,
  1288. .channels_max = 2,
  1289. .rates = WM8903_PLAYBACK_RATES,
  1290. .formats = WM8903_FORMATS,
  1291. },
  1292. .capture = {
  1293. .stream_name = "Capture",
  1294. .channels_min = 2,
  1295. .channels_max = 2,
  1296. .rates = WM8903_CAPTURE_RATES,
  1297. .formats = WM8903_FORMATS,
  1298. },
  1299. .ops = {
  1300. .startup = wm8903_startup,
  1301. .shutdown = wm8903_shutdown,
  1302. .hw_params = wm8903_hw_params,
  1303. .digital_mute = wm8903_digital_mute,
  1304. .set_fmt = wm8903_set_dai_fmt,
  1305. .set_sysclk = wm8903_set_dai_sysclk
  1306. }
  1307. };
  1308. EXPORT_SYMBOL_GPL(wm8903_dai);
  1309. static int wm8903_suspend(struct platform_device *pdev, pm_message_t state)
  1310. {
  1311. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1312. struct snd_soc_codec *codec = socdev->codec;
  1313. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1314. return 0;
  1315. }
  1316. static int wm8903_resume(struct platform_device *pdev)
  1317. {
  1318. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1319. struct snd_soc_codec *codec = socdev->codec;
  1320. struct i2c_client *i2c = codec->control_data;
  1321. int i;
  1322. u16 *reg_cache = codec->reg_cache;
  1323. u16 *tmp_cache = kmemdup(codec->reg_cache, sizeof(wm8903_reg_defaults),
  1324. GFP_KERNEL);
  1325. /* Bring the codec back up to standby first to minimise pop/clicks */
  1326. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1327. wm8903_set_bias_level(codec, codec->suspend_bias_level);
  1328. /* Sync back everything else */
  1329. if (tmp_cache) {
  1330. for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
  1331. if (tmp_cache[i] != reg_cache[i])
  1332. wm8903_write(codec, i, tmp_cache[i]);
  1333. } else {
  1334. dev_err(&i2c->dev, "Failed to allocate temporary cache\n");
  1335. }
  1336. return 0;
  1337. }
  1338. static struct snd_soc_codec *wm8903_codec;
  1339. static int wm8903_i2c_probe(struct i2c_client *i2c,
  1340. const struct i2c_device_id *id)
  1341. {
  1342. struct wm8903_priv *wm8903;
  1343. struct snd_soc_codec *codec;
  1344. int ret;
  1345. u16 val;
  1346. wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
  1347. if (wm8903 == NULL)
  1348. return -ENOMEM;
  1349. codec = &wm8903->codec;
  1350. mutex_init(&codec->mutex);
  1351. INIT_LIST_HEAD(&codec->dapm_widgets);
  1352. INIT_LIST_HEAD(&codec->dapm_paths);
  1353. codec->dev = &i2c->dev;
  1354. codec->name = "WM8903";
  1355. codec->owner = THIS_MODULE;
  1356. codec->read = wm8903_read;
  1357. codec->write = wm8903_write;
  1358. codec->hw_write = (hw_write_t)i2c_master_send;
  1359. codec->bias_level = SND_SOC_BIAS_OFF;
  1360. codec->set_bias_level = wm8903_set_bias_level;
  1361. codec->dai = &wm8903_dai;
  1362. codec->num_dai = 1;
  1363. codec->reg_cache_size = ARRAY_SIZE(wm8903->reg_cache);
  1364. codec->reg_cache = &wm8903->reg_cache[0];
  1365. codec->private_data = wm8903;
  1366. i2c_set_clientdata(i2c, codec);
  1367. codec->control_data = i2c;
  1368. val = wm8903_hw_read(codec, WM8903_SW_RESET_AND_ID);
  1369. if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
  1370. dev_err(&i2c->dev,
  1371. "Device with ID register %x is not a WM8903\n", val);
  1372. return -ENODEV;
  1373. }
  1374. val = wm8903_read(codec, WM8903_REVISION_NUMBER);
  1375. dev_info(&i2c->dev, "WM8903 revision %d\n",
  1376. val & WM8903_CHIP_REV_MASK);
  1377. wm8903_reset(codec);
  1378. /* power on device */
  1379. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1380. /* Latch volume update bits */
  1381. val = wm8903_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
  1382. val |= WM8903_ADCVU;
  1383. wm8903_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
  1384. wm8903_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
  1385. val = wm8903_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
  1386. val |= WM8903_DACVU;
  1387. wm8903_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
  1388. wm8903_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
  1389. val = wm8903_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
  1390. val |= WM8903_HPOUTVU;
  1391. wm8903_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
  1392. wm8903_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
  1393. val = wm8903_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
  1394. val |= WM8903_LINEOUTVU;
  1395. wm8903_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
  1396. wm8903_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
  1397. val = wm8903_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
  1398. val |= WM8903_SPKVU;
  1399. wm8903_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
  1400. wm8903_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
  1401. /* Enable DAC soft mute by default */
  1402. val = wm8903_read(codec, WM8903_DAC_DIGITAL_1);
  1403. val |= WM8903_DAC_MUTEMODE;
  1404. wm8903_write(codec, WM8903_DAC_DIGITAL_1, val);
  1405. wm8903_dai.dev = &i2c->dev;
  1406. wm8903_codec = codec;
  1407. ret = snd_soc_register_codec(codec);
  1408. if (ret != 0) {
  1409. dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
  1410. goto err;
  1411. }
  1412. ret = snd_soc_register_dai(&wm8903_dai);
  1413. if (ret != 0) {
  1414. dev_err(&i2c->dev, "Failed to register DAI: %d\n", ret);
  1415. goto err_codec;
  1416. }
  1417. return ret;
  1418. err_codec:
  1419. snd_soc_unregister_codec(codec);
  1420. err:
  1421. wm8903_codec = NULL;
  1422. kfree(wm8903);
  1423. return ret;
  1424. }
  1425. static int wm8903_i2c_remove(struct i2c_client *client)
  1426. {
  1427. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  1428. snd_soc_unregister_dai(&wm8903_dai);
  1429. snd_soc_unregister_codec(codec);
  1430. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1431. kfree(codec->private_data);
  1432. wm8903_codec = NULL;
  1433. wm8903_dai.dev = NULL;
  1434. return 0;
  1435. }
  1436. /* i2c codec control layer */
  1437. static const struct i2c_device_id wm8903_i2c_id[] = {
  1438. { "wm8903", 0 },
  1439. { }
  1440. };
  1441. MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
  1442. static struct i2c_driver wm8903_i2c_driver = {
  1443. .driver = {
  1444. .name = "WM8903",
  1445. .owner = THIS_MODULE,
  1446. },
  1447. .probe = wm8903_i2c_probe,
  1448. .remove = wm8903_i2c_remove,
  1449. .id_table = wm8903_i2c_id,
  1450. };
  1451. static int wm8903_probe(struct platform_device *pdev)
  1452. {
  1453. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1454. int ret = 0;
  1455. if (!wm8903_codec) {
  1456. dev_err(&pdev->dev, "I2C device not yet probed\n");
  1457. goto err;
  1458. }
  1459. socdev->codec = wm8903_codec;
  1460. /* register pcms */
  1461. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1462. if (ret < 0) {
  1463. dev_err(&pdev->dev, "failed to create pcms\n");
  1464. goto err;
  1465. }
  1466. wm8903_add_controls(socdev->codec);
  1467. wm8903_add_widgets(socdev->codec);
  1468. ret = snd_soc_init_card(socdev);
  1469. if (ret < 0) {
  1470. dev_err(&pdev->dev, "wm8903: failed to register card\n");
  1471. goto card_err;
  1472. }
  1473. return ret;
  1474. card_err:
  1475. snd_soc_free_pcms(socdev);
  1476. snd_soc_dapm_free(socdev);
  1477. err:
  1478. return ret;
  1479. }
  1480. /* power down chip */
  1481. static int wm8903_remove(struct platform_device *pdev)
  1482. {
  1483. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1484. struct snd_soc_codec *codec = socdev->codec;
  1485. if (codec->control_data)
  1486. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1487. snd_soc_free_pcms(socdev);
  1488. snd_soc_dapm_free(socdev);
  1489. return 0;
  1490. }
  1491. struct snd_soc_codec_device soc_codec_dev_wm8903 = {
  1492. .probe = wm8903_probe,
  1493. .remove = wm8903_remove,
  1494. .suspend = wm8903_suspend,
  1495. .resume = wm8903_resume,
  1496. };
  1497. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8903);
  1498. static int __init wm8903_modinit(void)
  1499. {
  1500. return i2c_add_driver(&wm8903_i2c_driver);
  1501. }
  1502. module_init(wm8903_modinit);
  1503. static void __exit wm8903_exit(void)
  1504. {
  1505. i2c_del_driver(&wm8903_i2c_driver);
  1506. }
  1507. module_exit(wm8903_exit);
  1508. MODULE_DESCRIPTION("ASoC WM8903 driver");
  1509. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
  1510. MODULE_LICENSE("GPL");