wm8903.c 51 KB

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