wm9081.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /*
  2. * wm9081.c -- WM9081 ALSA SoC Audio driver
  3. *
  4. * Author: Mark Brown
  5. *
  6. * Copyright 2009 Wolfson Microelectronics plc
  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. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <sound/wm9081.h>
  29. #include "wm9081.h"
  30. static struct reg_default wm9081_reg[] = {
  31. { 0, 0x9081 }, /* R0 - Software Reset */
  32. { 2, 0x00B9 }, /* R2 - Analogue Lineout */
  33. { 3, 0x00B9 }, /* R3 - Analogue Speaker PGA */
  34. { 4, 0x0001 }, /* R4 - VMID Control */
  35. { 5, 0x0068 }, /* R5 - Bias Control 1 */
  36. { 7, 0x0000 }, /* R7 - Analogue Mixer */
  37. { 8, 0x0000 }, /* R8 - Anti Pop Control */
  38. { 9, 0x01DB }, /* R9 - Analogue Speaker 1 */
  39. { 10, 0x0018 }, /* R10 - Analogue Speaker 2 */
  40. { 11, 0x0180 }, /* R11 - Power Management */
  41. { 12, 0x0000 }, /* R12 - Clock Control 1 */
  42. { 13, 0x0038 }, /* R13 - Clock Control 2 */
  43. { 14, 0x4000 }, /* R14 - Clock Control 3 */
  44. { 16, 0x0000 }, /* R16 - FLL Control 1 */
  45. { 17, 0x0200 }, /* R17 - FLL Control 2 */
  46. { 18, 0x0000 }, /* R18 - FLL Control 3 */
  47. { 19, 0x0204 }, /* R19 - FLL Control 4 */
  48. { 20, 0x0000 }, /* R20 - FLL Control 5 */
  49. { 22, 0x0000 }, /* R22 - Audio Interface 1 */
  50. { 23, 0x0002 }, /* R23 - Audio Interface 2 */
  51. { 24, 0x0008 }, /* R24 - Audio Interface 3 */
  52. { 25, 0x0022 }, /* R25 - Audio Interface 4 */
  53. { 27, 0x0006 }, /* R27 - Interrupt Status Mask */
  54. { 28, 0x0000 }, /* R28 - Interrupt Polarity */
  55. { 29, 0x0000 }, /* R29 - Interrupt Control */
  56. { 30, 0x00C0 }, /* R30 - DAC Digital 1 */
  57. { 31, 0x0008 }, /* R31 - DAC Digital 2 */
  58. { 32, 0x09AF }, /* R32 - DRC 1 */
  59. { 33, 0x4201 }, /* R33 - DRC 2 */
  60. { 34, 0x0000 }, /* R34 - DRC 3 */
  61. { 35, 0x0000 }, /* R35 - DRC 4 */
  62. { 38, 0x0000 }, /* R38 - Write Sequencer 1 */
  63. { 39, 0x0000 }, /* R39 - Write Sequencer 2 */
  64. { 40, 0x0002 }, /* R40 - MW Slave 1 */
  65. { 42, 0x0000 }, /* R42 - EQ 1 */
  66. { 43, 0x0000 }, /* R43 - EQ 2 */
  67. { 44, 0x0FCA }, /* R44 - EQ 3 */
  68. { 45, 0x0400 }, /* R45 - EQ 4 */
  69. { 46, 0x00B8 }, /* R46 - EQ 5 */
  70. { 47, 0x1EB5 }, /* R47 - EQ 6 */
  71. { 48, 0xF145 }, /* R48 - EQ 7 */
  72. { 49, 0x0B75 }, /* R49 - EQ 8 */
  73. { 50, 0x01C5 }, /* R50 - EQ 9 */
  74. { 51, 0x169E }, /* R51 - EQ 10 */
  75. { 52, 0xF829 }, /* R52 - EQ 11 */
  76. { 53, 0x07AD }, /* R53 - EQ 12 */
  77. { 54, 0x1103 }, /* R54 - EQ 13 */
  78. { 55, 0x1C58 }, /* R55 - EQ 14 */
  79. { 56, 0xF373 }, /* R56 - EQ 15 */
  80. { 57, 0x0A54 }, /* R57 - EQ 16 */
  81. { 58, 0x0558 }, /* R58 - EQ 17 */
  82. { 59, 0x0564 }, /* R59 - EQ 18 */
  83. { 60, 0x0559 }, /* R60 - EQ 19 */
  84. { 61, 0x4000 }, /* R61 - EQ 20 */
  85. };
  86. static struct {
  87. int ratio;
  88. int clk_sys_rate;
  89. } clk_sys_rates[] = {
  90. { 64, 0 },
  91. { 128, 1 },
  92. { 192, 2 },
  93. { 256, 3 },
  94. { 384, 4 },
  95. { 512, 5 },
  96. { 768, 6 },
  97. { 1024, 7 },
  98. { 1408, 8 },
  99. { 1536, 9 },
  100. };
  101. static struct {
  102. int rate;
  103. int sample_rate;
  104. } sample_rates[] = {
  105. { 8000, 0 },
  106. { 11025, 1 },
  107. { 12000, 2 },
  108. { 16000, 3 },
  109. { 22050, 4 },
  110. { 24000, 5 },
  111. { 32000, 6 },
  112. { 44100, 7 },
  113. { 48000, 8 },
  114. { 88200, 9 },
  115. { 96000, 10 },
  116. };
  117. static struct {
  118. int div; /* *10 due to .5s */
  119. int bclk_div;
  120. } bclk_divs[] = {
  121. { 10, 0 },
  122. { 15, 1 },
  123. { 20, 2 },
  124. { 30, 3 },
  125. { 40, 4 },
  126. { 50, 5 },
  127. { 55, 6 },
  128. { 60, 7 },
  129. { 80, 8 },
  130. { 100, 9 },
  131. { 110, 10 },
  132. { 120, 11 },
  133. { 160, 12 },
  134. { 200, 13 },
  135. { 220, 14 },
  136. { 240, 15 },
  137. { 250, 16 },
  138. { 300, 17 },
  139. { 320, 18 },
  140. { 440, 19 },
  141. { 480, 20 },
  142. };
  143. struct wm9081_priv {
  144. struct regmap *regmap;
  145. int sysclk_source;
  146. int mclk_rate;
  147. int sysclk_rate;
  148. int fs;
  149. int bclk;
  150. int master;
  151. int fll_fref;
  152. int fll_fout;
  153. int tdm_width;
  154. struct wm9081_pdata pdata;
  155. };
  156. static bool wm9081_volatile_register(struct device *dev, unsigned int reg)
  157. {
  158. switch (reg) {
  159. case WM9081_SOFTWARE_RESET:
  160. case WM9081_INTERRUPT_STATUS:
  161. return true;
  162. default:
  163. return false;
  164. }
  165. }
  166. static bool wm9081_readable_register(struct device *dev, unsigned int reg)
  167. {
  168. switch (reg) {
  169. case WM9081_SOFTWARE_RESET:
  170. case WM9081_ANALOGUE_LINEOUT:
  171. case WM9081_ANALOGUE_SPEAKER_PGA:
  172. case WM9081_VMID_CONTROL:
  173. case WM9081_BIAS_CONTROL_1:
  174. case WM9081_ANALOGUE_MIXER:
  175. case WM9081_ANTI_POP_CONTROL:
  176. case WM9081_ANALOGUE_SPEAKER_1:
  177. case WM9081_ANALOGUE_SPEAKER_2:
  178. case WM9081_POWER_MANAGEMENT:
  179. case WM9081_CLOCK_CONTROL_1:
  180. case WM9081_CLOCK_CONTROL_2:
  181. case WM9081_CLOCK_CONTROL_3:
  182. case WM9081_FLL_CONTROL_1:
  183. case WM9081_FLL_CONTROL_2:
  184. case WM9081_FLL_CONTROL_3:
  185. case WM9081_FLL_CONTROL_4:
  186. case WM9081_FLL_CONTROL_5:
  187. case WM9081_AUDIO_INTERFACE_1:
  188. case WM9081_AUDIO_INTERFACE_2:
  189. case WM9081_AUDIO_INTERFACE_3:
  190. case WM9081_AUDIO_INTERFACE_4:
  191. case WM9081_INTERRUPT_STATUS:
  192. case WM9081_INTERRUPT_STATUS_MASK:
  193. case WM9081_INTERRUPT_POLARITY:
  194. case WM9081_INTERRUPT_CONTROL:
  195. case WM9081_DAC_DIGITAL_1:
  196. case WM9081_DAC_DIGITAL_2:
  197. case WM9081_DRC_1:
  198. case WM9081_DRC_2:
  199. case WM9081_DRC_3:
  200. case WM9081_DRC_4:
  201. case WM9081_WRITE_SEQUENCER_1:
  202. case WM9081_WRITE_SEQUENCER_2:
  203. case WM9081_MW_SLAVE_1:
  204. case WM9081_EQ_1:
  205. case WM9081_EQ_2:
  206. case WM9081_EQ_3:
  207. case WM9081_EQ_4:
  208. case WM9081_EQ_5:
  209. case WM9081_EQ_6:
  210. case WM9081_EQ_7:
  211. case WM9081_EQ_8:
  212. case WM9081_EQ_9:
  213. case WM9081_EQ_10:
  214. case WM9081_EQ_11:
  215. case WM9081_EQ_12:
  216. case WM9081_EQ_13:
  217. case WM9081_EQ_14:
  218. case WM9081_EQ_15:
  219. case WM9081_EQ_16:
  220. case WM9081_EQ_17:
  221. case WM9081_EQ_18:
  222. case WM9081_EQ_19:
  223. case WM9081_EQ_20:
  224. return true;
  225. default:
  226. return false;
  227. }
  228. }
  229. static int wm9081_reset(struct regmap *map)
  230. {
  231. return regmap_write(map, WM9081_SOFTWARE_RESET, 0x9081);
  232. }
  233. static const DECLARE_TLV_DB_SCALE(drc_in_tlv, -4500, 75, 0);
  234. static const DECLARE_TLV_DB_SCALE(drc_out_tlv, -2250, 75, 0);
  235. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  236. static unsigned int drc_max_tlv[] = {
  237. TLV_DB_RANGE_HEAD(4),
  238. 0, 0, TLV_DB_SCALE_ITEM(1200, 0, 0),
  239. 1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
  240. 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
  241. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
  242. };
  243. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  244. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -300, 50, 0);
  245. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  246. static const DECLARE_TLV_DB_SCALE(in_tlv, -600, 600, 0);
  247. static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
  248. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  249. static const char *drc_high_text[] = {
  250. "1",
  251. "1/2",
  252. "1/4",
  253. "1/8",
  254. "1/16",
  255. "0",
  256. };
  257. static const struct soc_enum drc_high =
  258. SOC_ENUM_SINGLE(WM9081_DRC_3, 3, 6, drc_high_text);
  259. static const char *drc_low_text[] = {
  260. "1",
  261. "1/2",
  262. "1/4",
  263. "1/8",
  264. "0",
  265. };
  266. static const struct soc_enum drc_low =
  267. SOC_ENUM_SINGLE(WM9081_DRC_3, 0, 5, drc_low_text);
  268. static const char *drc_atk_text[] = {
  269. "181us",
  270. "181us",
  271. "363us",
  272. "726us",
  273. "1.45ms",
  274. "2.9ms",
  275. "5.8ms",
  276. "11.6ms",
  277. "23.2ms",
  278. "46.4ms",
  279. "92.8ms",
  280. "185.6ms",
  281. };
  282. static const struct soc_enum drc_atk =
  283. SOC_ENUM_SINGLE(WM9081_DRC_2, 12, 12, drc_atk_text);
  284. static const char *drc_dcy_text[] = {
  285. "186ms",
  286. "372ms",
  287. "743ms",
  288. "1.49s",
  289. "2.97s",
  290. "5.94s",
  291. "11.89s",
  292. "23.78s",
  293. "47.56s",
  294. };
  295. static const struct soc_enum drc_dcy =
  296. SOC_ENUM_SINGLE(WM9081_DRC_2, 8, 9, drc_dcy_text);
  297. static const char *drc_qr_dcy_text[] = {
  298. "0.725ms",
  299. "1.45ms",
  300. "5.8ms",
  301. };
  302. static const struct soc_enum drc_qr_dcy =
  303. SOC_ENUM_SINGLE(WM9081_DRC_2, 4, 3, drc_qr_dcy_text);
  304. static const char *dac_deemph_text[] = {
  305. "None",
  306. "32kHz",
  307. "44.1kHz",
  308. "48kHz",
  309. };
  310. static const struct soc_enum dac_deemph =
  311. SOC_ENUM_SINGLE(WM9081_DAC_DIGITAL_2, 1, 4, dac_deemph_text);
  312. static const char *speaker_mode_text[] = {
  313. "Class D",
  314. "Class AB",
  315. };
  316. static const struct soc_enum speaker_mode =
  317. SOC_ENUM_SINGLE(WM9081_ANALOGUE_SPEAKER_2, 6, 2, speaker_mode_text);
  318. static int speaker_mode_get(struct snd_kcontrol *kcontrol,
  319. struct snd_ctl_elem_value *ucontrol)
  320. {
  321. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  322. unsigned int reg;
  323. reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  324. if (reg & WM9081_SPK_MODE)
  325. ucontrol->value.integer.value[0] = 1;
  326. else
  327. ucontrol->value.integer.value[0] = 0;
  328. return 0;
  329. }
  330. /*
  331. * Stop any attempts to change speaker mode while the speaker is enabled.
  332. *
  333. * We also have some special anti-pop controls dependent on speaker
  334. * mode which must be changed along with the mode.
  335. */
  336. static int speaker_mode_put(struct snd_kcontrol *kcontrol,
  337. struct snd_ctl_elem_value *ucontrol)
  338. {
  339. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  340. unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
  341. unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  342. /* Are we changing anything? */
  343. if (ucontrol->value.integer.value[0] ==
  344. ((reg2 & WM9081_SPK_MODE) != 0))
  345. return 0;
  346. /* Don't try to change modes while enabled */
  347. if (reg_pwr & WM9081_SPK_ENA)
  348. return -EINVAL;
  349. if (ucontrol->value.integer.value[0]) {
  350. /* Class AB */
  351. reg2 &= ~(WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL);
  352. reg2 |= WM9081_SPK_MODE;
  353. } else {
  354. /* Class D */
  355. reg2 |= WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL;
  356. reg2 &= ~WM9081_SPK_MODE;
  357. }
  358. snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
  359. return 0;
  360. }
  361. static const struct snd_kcontrol_new wm9081_snd_controls[] = {
  362. SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
  363. SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
  364. SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
  365. SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
  366. SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
  367. SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
  368. SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
  369. SOC_ENUM("DRC High Slope", drc_high),
  370. SOC_ENUM("DRC Low Slope", drc_low),
  371. SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
  372. SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
  373. SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
  374. SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
  375. SOC_ENUM("DRC Attack", drc_atk),
  376. SOC_ENUM("DRC Decay", drc_dcy),
  377. SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
  378. SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
  379. SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
  380. SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
  381. SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
  382. SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
  383. SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
  384. SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
  385. SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
  386. SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
  387. out_tlv),
  388. SOC_ENUM("DAC Deemphasis", dac_deemph),
  389. SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
  390. };
  391. static const struct snd_kcontrol_new wm9081_eq_controls[] = {
  392. SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
  393. SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
  394. SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
  395. SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
  396. SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
  397. };
  398. static const struct snd_kcontrol_new mixer[] = {
  399. SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
  400. SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
  401. SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
  402. };
  403. struct _fll_div {
  404. u16 fll_fratio;
  405. u16 fll_outdiv;
  406. u16 fll_clk_ref_div;
  407. u16 n;
  408. u16 k;
  409. };
  410. /* The size in bits of the FLL divide multiplied by 10
  411. * to allow rounding later */
  412. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  413. static struct {
  414. unsigned int min;
  415. unsigned int max;
  416. u16 fll_fratio;
  417. int ratio;
  418. } fll_fratios[] = {
  419. { 0, 64000, 4, 16 },
  420. { 64000, 128000, 3, 8 },
  421. { 128000, 256000, 2, 4 },
  422. { 256000, 1000000, 1, 2 },
  423. { 1000000, 13500000, 0, 1 },
  424. };
  425. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  426. unsigned int Fout)
  427. {
  428. u64 Kpart;
  429. unsigned int K, Ndiv, Nmod, target;
  430. unsigned int div;
  431. int i;
  432. /* Fref must be <=13.5MHz */
  433. div = 1;
  434. while ((Fref / div) > 13500000) {
  435. div *= 2;
  436. if (div > 8) {
  437. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  438. Fref);
  439. return -EINVAL;
  440. }
  441. }
  442. fll_div->fll_clk_ref_div = div / 2;
  443. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  444. /* Apply the division for our remaining calculations */
  445. Fref /= div;
  446. /* Fvco should be 90-100MHz; don't check the upper bound */
  447. div = 0;
  448. target = Fout * 2;
  449. while (target < 90000000) {
  450. div++;
  451. target *= 2;
  452. if (div > 7) {
  453. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  454. Fout);
  455. return -EINVAL;
  456. }
  457. }
  458. fll_div->fll_outdiv = div;
  459. pr_debug("Fvco=%dHz\n", target);
  460. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  461. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  462. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  463. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  464. target /= fll_fratios[i].ratio;
  465. break;
  466. }
  467. }
  468. if (i == ARRAY_SIZE(fll_fratios)) {
  469. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  470. return -EINVAL;
  471. }
  472. /* Now, calculate N.K */
  473. Ndiv = target / Fref;
  474. fll_div->n = Ndiv;
  475. Nmod = target % Fref;
  476. pr_debug("Nmod=%d\n", Nmod);
  477. /* Calculate fractional part - scale up so we can round. */
  478. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  479. do_div(Kpart, Fref);
  480. K = Kpart & 0xFFFFFFFF;
  481. if ((K % 10) >= 5)
  482. K += 5;
  483. /* Move down to proper range now rounding is done */
  484. fll_div->k = K / 10;
  485. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  486. fll_div->n, fll_div->k,
  487. fll_div->fll_fratio, fll_div->fll_outdiv,
  488. fll_div->fll_clk_ref_div);
  489. return 0;
  490. }
  491. static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
  492. unsigned int Fref, unsigned int Fout)
  493. {
  494. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  495. u16 reg1, reg4, reg5;
  496. struct _fll_div fll_div;
  497. int ret;
  498. int clk_sys_reg;
  499. /* Any change? */
  500. if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
  501. return 0;
  502. /* Disable the FLL */
  503. if (Fout == 0) {
  504. dev_dbg(codec->dev, "FLL disabled\n");
  505. wm9081->fll_fref = 0;
  506. wm9081->fll_fout = 0;
  507. return 0;
  508. }
  509. ret = fll_factors(&fll_div, Fref, Fout);
  510. if (ret != 0)
  511. return ret;
  512. reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
  513. reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
  514. switch (fll_id) {
  515. case WM9081_SYSCLK_FLL_MCLK:
  516. reg5 |= 0x1;
  517. break;
  518. default:
  519. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  520. return -EINVAL;
  521. }
  522. /* Disable CLK_SYS while we reconfigure */
  523. clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  524. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  525. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
  526. clk_sys_reg & ~WM9081_CLK_SYS_ENA);
  527. /* Any FLL configuration change requires that the FLL be
  528. * disabled first. */
  529. reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
  530. reg1 &= ~WM9081_FLL_ENA;
  531. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  532. /* Apply the configuration */
  533. if (fll_div.k)
  534. reg1 |= WM9081_FLL_FRAC_MASK;
  535. else
  536. reg1 &= ~WM9081_FLL_FRAC_MASK;
  537. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  538. snd_soc_write(codec, WM9081_FLL_CONTROL_2,
  539. (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
  540. (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
  541. snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
  542. reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
  543. reg4 &= ~WM9081_FLL_N_MASK;
  544. reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
  545. snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
  546. reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
  547. reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
  548. snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
  549. /* Set gain to the recommended value */
  550. snd_soc_update_bits(codec, WM9081_FLL_CONTROL_4,
  551. WM9081_FLL_GAIN_MASK, 0);
  552. /* Enable the FLL */
  553. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
  554. /* Then bring CLK_SYS up again if it was disabled */
  555. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  556. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
  557. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  558. wm9081->fll_fref = Fref;
  559. wm9081->fll_fout = Fout;
  560. return 0;
  561. }
  562. static int configure_clock(struct snd_soc_codec *codec)
  563. {
  564. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  565. int new_sysclk, i, target;
  566. unsigned int reg;
  567. int ret = 0;
  568. int mclkdiv = 0;
  569. int fll = 0;
  570. switch (wm9081->sysclk_source) {
  571. case WM9081_SYSCLK_MCLK:
  572. if (wm9081->mclk_rate > 12225000) {
  573. mclkdiv = 1;
  574. wm9081->sysclk_rate = wm9081->mclk_rate / 2;
  575. } else {
  576. wm9081->sysclk_rate = wm9081->mclk_rate;
  577. }
  578. wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
  579. break;
  580. case WM9081_SYSCLK_FLL_MCLK:
  581. /* If we have a sample rate calculate a CLK_SYS that
  582. * gives us a suitable DAC configuration, plus BCLK.
  583. * Ideally we would check to see if we can clock
  584. * directly from MCLK and only use the FLL if this is
  585. * not the case, though care must be taken with free
  586. * running mode.
  587. */
  588. if (wm9081->master && wm9081->bclk) {
  589. /* Make sure we can generate CLK_SYS and BCLK
  590. * and that we've got 3MHz for optimal
  591. * performance. */
  592. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  593. target = wm9081->fs * clk_sys_rates[i].ratio;
  594. new_sysclk = target;
  595. if (target >= wm9081->bclk &&
  596. target > 3000000)
  597. break;
  598. }
  599. if (i == ARRAY_SIZE(clk_sys_rates))
  600. return -EINVAL;
  601. } else if (wm9081->fs) {
  602. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  603. new_sysclk = clk_sys_rates[i].ratio
  604. * wm9081->fs;
  605. if (new_sysclk > 3000000)
  606. break;
  607. }
  608. if (i == ARRAY_SIZE(clk_sys_rates))
  609. return -EINVAL;
  610. } else {
  611. new_sysclk = 12288000;
  612. }
  613. ret = wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK,
  614. wm9081->mclk_rate, new_sysclk);
  615. if (ret == 0) {
  616. wm9081->sysclk_rate = new_sysclk;
  617. /* Switch SYSCLK over to FLL */
  618. fll = 1;
  619. } else {
  620. wm9081->sysclk_rate = wm9081->mclk_rate;
  621. }
  622. break;
  623. default:
  624. return -EINVAL;
  625. }
  626. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
  627. if (mclkdiv)
  628. reg |= WM9081_MCLKDIV2;
  629. else
  630. reg &= ~WM9081_MCLKDIV2;
  631. snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
  632. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  633. if (fll)
  634. reg |= WM9081_CLK_SRC_SEL;
  635. else
  636. reg &= ~WM9081_CLK_SRC_SEL;
  637. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
  638. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
  639. return ret;
  640. }
  641. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  642. struct snd_kcontrol *kcontrol, int event)
  643. {
  644. struct snd_soc_codec *codec = w->codec;
  645. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  646. /* This should be done on init() for bypass paths */
  647. switch (wm9081->sysclk_source) {
  648. case WM9081_SYSCLK_MCLK:
  649. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm9081->mclk_rate);
  650. break;
  651. case WM9081_SYSCLK_FLL_MCLK:
  652. dev_dbg(codec->dev, "Using %dHz MCLK with FLL\n",
  653. wm9081->mclk_rate);
  654. break;
  655. default:
  656. dev_err(codec->dev, "System clock not configured\n");
  657. return -EINVAL;
  658. }
  659. switch (event) {
  660. case SND_SOC_DAPM_PRE_PMU:
  661. configure_clock(codec);
  662. break;
  663. case SND_SOC_DAPM_POST_PMD:
  664. /* Disable the FLL if it's running */
  665. wm9081_set_fll(codec, 0, 0, 0);
  666. break;
  667. }
  668. return 0;
  669. }
  670. static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
  671. SND_SOC_DAPM_INPUT("IN1"),
  672. SND_SOC_DAPM_INPUT("IN2"),
  673. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM9081_POWER_MANAGEMENT, 0, 0),
  674. SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
  675. mixer, ARRAY_SIZE(mixer)),
  676. SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
  677. SND_SOC_DAPM_PGA("Speaker PGA", WM9081_POWER_MANAGEMENT, 2, 0, NULL, 0),
  678. SND_SOC_DAPM_OUT_DRV("Speaker", WM9081_POWER_MANAGEMENT, 1, 0, NULL, 0),
  679. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  680. SND_SOC_DAPM_OUTPUT("SPKN"),
  681. SND_SOC_DAPM_OUTPUT("SPKP"),
  682. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM9081_CLOCK_CONTROL_3, 0, 0, clk_sys_event,
  683. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  684. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
  685. SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
  686. SND_SOC_DAPM_SUPPLY("TSENSE", WM9081_POWER_MANAGEMENT, 7, 0, NULL, 0),
  687. };
  688. static const struct snd_soc_dapm_route wm9081_audio_paths[] = {
  689. { "DAC", NULL, "CLK_SYS" },
  690. { "DAC", NULL, "CLK_DSP" },
  691. { "Mixer", "IN1 Switch", "IN1" },
  692. { "Mixer", "IN2 Switch", "IN2" },
  693. { "Mixer", "Playback Switch", "DAC" },
  694. { "LINEOUT PGA", NULL, "Mixer" },
  695. { "LINEOUT PGA", NULL, "TOCLK" },
  696. { "LINEOUT PGA", NULL, "CLK_SYS" },
  697. { "LINEOUT", NULL, "LINEOUT PGA" },
  698. { "Speaker PGA", NULL, "Mixer" },
  699. { "Speaker PGA", NULL, "TOCLK" },
  700. { "Speaker PGA", NULL, "CLK_SYS" },
  701. { "Speaker", NULL, "Speaker PGA" },
  702. { "Speaker", NULL, "TSENSE" },
  703. { "SPKN", NULL, "Speaker" },
  704. { "SPKP", NULL, "Speaker" },
  705. };
  706. static int wm9081_set_bias_level(struct snd_soc_codec *codec,
  707. enum snd_soc_bias_level level)
  708. {
  709. switch (level) {
  710. case SND_SOC_BIAS_ON:
  711. break;
  712. case SND_SOC_BIAS_PREPARE:
  713. /* VMID=2*40k */
  714. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  715. WM9081_VMID_SEL_MASK, 0x2);
  716. /* Normal bias current */
  717. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  718. WM9081_STBY_BIAS_ENA, 0);
  719. break;
  720. case SND_SOC_BIAS_STANDBY:
  721. /* Initial cold start */
  722. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  723. /* Disable LINEOUT discharge */
  724. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  725. WM9081_LINEOUT_DISCH, 0);
  726. /* Select startup bias source */
  727. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  728. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  729. WM9081_BIAS_SRC | WM9081_BIAS_ENA);
  730. /* VMID 2*4k; Soft VMID ramp enable */
  731. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  732. WM9081_VMID_RAMP |
  733. WM9081_VMID_SEL_MASK,
  734. WM9081_VMID_RAMP | 0x6);
  735. mdelay(100);
  736. /* Normal bias enable & soft start off */
  737. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  738. WM9081_VMID_RAMP, 0);
  739. /* Standard bias source */
  740. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  741. WM9081_BIAS_SRC, 0);
  742. }
  743. /* VMID 2*240k */
  744. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  745. WM9081_VMID_SEL_MASK, 0x04);
  746. /* Standby bias current on */
  747. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  748. WM9081_STBY_BIAS_ENA,
  749. WM9081_STBY_BIAS_ENA);
  750. break;
  751. case SND_SOC_BIAS_OFF:
  752. /* Startup bias source and disable bias */
  753. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  754. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  755. WM9081_BIAS_SRC);
  756. /* Disable VMID with soft ramping */
  757. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  758. WM9081_VMID_RAMP | WM9081_VMID_SEL_MASK,
  759. WM9081_VMID_RAMP);
  760. /* Actively discharge LINEOUT */
  761. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  762. WM9081_LINEOUT_DISCH,
  763. WM9081_LINEOUT_DISCH);
  764. break;
  765. }
  766. codec->dapm.bias_level = level;
  767. return 0;
  768. }
  769. static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
  770. unsigned int fmt)
  771. {
  772. struct snd_soc_codec *codec = dai->codec;
  773. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  774. unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  775. aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
  776. WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
  777. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  778. case SND_SOC_DAIFMT_CBS_CFS:
  779. wm9081->master = 0;
  780. break;
  781. case SND_SOC_DAIFMT_CBS_CFM:
  782. aif2 |= WM9081_LRCLK_DIR;
  783. wm9081->master = 1;
  784. break;
  785. case SND_SOC_DAIFMT_CBM_CFS:
  786. aif2 |= WM9081_BCLK_DIR;
  787. wm9081->master = 1;
  788. break;
  789. case SND_SOC_DAIFMT_CBM_CFM:
  790. aif2 |= WM9081_LRCLK_DIR | WM9081_BCLK_DIR;
  791. wm9081->master = 1;
  792. break;
  793. default:
  794. return -EINVAL;
  795. }
  796. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  797. case SND_SOC_DAIFMT_DSP_B:
  798. aif2 |= WM9081_AIF_LRCLK_INV;
  799. case SND_SOC_DAIFMT_DSP_A:
  800. aif2 |= 0x3;
  801. break;
  802. case SND_SOC_DAIFMT_I2S:
  803. aif2 |= 0x2;
  804. break;
  805. case SND_SOC_DAIFMT_RIGHT_J:
  806. break;
  807. case SND_SOC_DAIFMT_LEFT_J:
  808. aif2 |= 0x1;
  809. break;
  810. default:
  811. return -EINVAL;
  812. }
  813. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  814. case SND_SOC_DAIFMT_DSP_A:
  815. case SND_SOC_DAIFMT_DSP_B:
  816. /* frame inversion not valid for DSP modes */
  817. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  818. case SND_SOC_DAIFMT_NB_NF:
  819. break;
  820. case SND_SOC_DAIFMT_IB_NF:
  821. aif2 |= WM9081_AIF_BCLK_INV;
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. break;
  827. case SND_SOC_DAIFMT_I2S:
  828. case SND_SOC_DAIFMT_RIGHT_J:
  829. case SND_SOC_DAIFMT_LEFT_J:
  830. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  831. case SND_SOC_DAIFMT_NB_NF:
  832. break;
  833. case SND_SOC_DAIFMT_IB_IF:
  834. aif2 |= WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV;
  835. break;
  836. case SND_SOC_DAIFMT_IB_NF:
  837. aif2 |= WM9081_AIF_BCLK_INV;
  838. break;
  839. case SND_SOC_DAIFMT_NB_IF:
  840. aif2 |= WM9081_AIF_LRCLK_INV;
  841. break;
  842. default:
  843. return -EINVAL;
  844. }
  845. break;
  846. default:
  847. return -EINVAL;
  848. }
  849. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  850. return 0;
  851. }
  852. static int wm9081_hw_params(struct snd_pcm_substream *substream,
  853. struct snd_pcm_hw_params *params,
  854. struct snd_soc_dai *dai)
  855. {
  856. struct snd_soc_codec *codec = dai->codec;
  857. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  858. int ret, i, best, best_val, cur_val;
  859. unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
  860. clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
  861. clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
  862. aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  863. aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  864. aif2 &= ~WM9081_AIF_WL_MASK;
  865. aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
  866. aif3 &= ~WM9081_BCLK_DIV_MASK;
  867. aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
  868. aif4 &= ~WM9081_LRCLK_RATE_MASK;
  869. wm9081->fs = params_rate(params);
  870. if (wm9081->tdm_width) {
  871. /* If TDM is set up then that fixes our BCLK. */
  872. int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
  873. WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
  874. wm9081->bclk = wm9081->fs * wm9081->tdm_width * slots;
  875. } else {
  876. /* Otherwise work out a BCLK from the sample size */
  877. wm9081->bclk = 2 * wm9081->fs;
  878. switch (params_format(params)) {
  879. case SNDRV_PCM_FORMAT_S16_LE:
  880. wm9081->bclk *= 16;
  881. break;
  882. case SNDRV_PCM_FORMAT_S20_3LE:
  883. wm9081->bclk *= 20;
  884. aif2 |= 0x4;
  885. break;
  886. case SNDRV_PCM_FORMAT_S24_LE:
  887. wm9081->bclk *= 24;
  888. aif2 |= 0x8;
  889. break;
  890. case SNDRV_PCM_FORMAT_S32_LE:
  891. wm9081->bclk *= 32;
  892. aif2 |= 0xc;
  893. break;
  894. default:
  895. return -EINVAL;
  896. }
  897. }
  898. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
  899. ret = configure_clock(codec);
  900. if (ret != 0)
  901. return ret;
  902. /* Select nearest CLK_SYS_RATE */
  903. best = 0;
  904. best_val = abs((wm9081->sysclk_rate / clk_sys_rates[0].ratio)
  905. - wm9081->fs);
  906. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  907. cur_val = abs((wm9081->sysclk_rate /
  908. clk_sys_rates[i].ratio) - wm9081->fs);
  909. if (cur_val < best_val) {
  910. best = i;
  911. best_val = cur_val;
  912. }
  913. }
  914. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  915. clk_sys_rates[best].ratio);
  916. clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
  917. << WM9081_CLK_SYS_RATE_SHIFT);
  918. /* SAMPLE_RATE */
  919. best = 0;
  920. best_val = abs(wm9081->fs - sample_rates[0].rate);
  921. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  922. /* Closest match */
  923. cur_val = abs(wm9081->fs - sample_rates[i].rate);
  924. if (cur_val < best_val) {
  925. best = i;
  926. best_val = cur_val;
  927. }
  928. }
  929. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  930. sample_rates[best].rate);
  931. clk_ctrl2 |= (sample_rates[best].sample_rate
  932. << WM9081_SAMPLE_RATE_SHIFT);
  933. /* BCLK_DIV */
  934. best = 0;
  935. best_val = INT_MAX;
  936. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  937. cur_val = ((wm9081->sysclk_rate * 10) / bclk_divs[i].div)
  938. - wm9081->bclk;
  939. if (cur_val < 0) /* Table is sorted */
  940. break;
  941. if (cur_val < best_val) {
  942. best = i;
  943. best_val = cur_val;
  944. }
  945. }
  946. wm9081->bclk = (wm9081->sysclk_rate * 10) / bclk_divs[best].div;
  947. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  948. bclk_divs[best].div, wm9081->bclk);
  949. aif3 |= bclk_divs[best].bclk_div;
  950. /* LRCLK is a simple fraction of BCLK */
  951. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm9081->bclk / wm9081->fs);
  952. aif4 |= wm9081->bclk / wm9081->fs;
  953. /* Apply a ReTune Mobile configuration if it's in use */
  954. if (wm9081->pdata.num_retune_configs) {
  955. struct wm9081_pdata *pdata = &wm9081->pdata;
  956. struct wm9081_retune_mobile_setting *s;
  957. int eq1;
  958. best = 0;
  959. best_val = abs(pdata->retune_configs[0].rate - wm9081->fs);
  960. for (i = 0; i < pdata->num_retune_configs; i++) {
  961. cur_val = abs(pdata->retune_configs[i].rate -
  962. wm9081->fs);
  963. if (cur_val < best_val) {
  964. best_val = cur_val;
  965. best = i;
  966. }
  967. }
  968. s = &pdata->retune_configs[best];
  969. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  970. s->name, s->rate);
  971. /* If the EQ is enabled then disable it while we write out */
  972. eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
  973. if (eq1 & WM9081_EQ_ENA)
  974. snd_soc_write(codec, WM9081_EQ_1, 0);
  975. /* Write out the other values */
  976. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  977. snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
  978. eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
  979. snd_soc_write(codec, WM9081_EQ_1, eq1);
  980. }
  981. snd_soc_write(codec, WM9081_CLOCK_CONTROL_2, clk_ctrl2);
  982. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  983. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_3, aif3);
  984. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_4, aif4);
  985. return 0;
  986. }
  987. static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  988. {
  989. struct snd_soc_codec *codec = codec_dai->codec;
  990. unsigned int reg;
  991. reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
  992. if (mute)
  993. reg |= WM9081_DAC_MUTE;
  994. else
  995. reg &= ~WM9081_DAC_MUTE;
  996. snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
  997. return 0;
  998. }
  999. static int wm9081_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  1000. int source, unsigned int freq, int dir)
  1001. {
  1002. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1003. switch (clk_id) {
  1004. case WM9081_SYSCLK_MCLK:
  1005. case WM9081_SYSCLK_FLL_MCLK:
  1006. wm9081->sysclk_source = clk_id;
  1007. wm9081->mclk_rate = freq;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. return 0;
  1013. }
  1014. static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
  1015. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  1016. {
  1017. struct snd_soc_codec *codec = dai->codec;
  1018. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1019. unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  1020. aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
  1021. if (slots < 0 || slots > 4)
  1022. return -EINVAL;
  1023. wm9081->tdm_width = slot_width;
  1024. if (slots == 0)
  1025. slots = 1;
  1026. aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
  1027. switch (rx_mask) {
  1028. case 1:
  1029. break;
  1030. case 2:
  1031. aif1 |= 0x10;
  1032. break;
  1033. case 4:
  1034. aif1 |= 0x20;
  1035. break;
  1036. case 8:
  1037. aif1 |= 0x30;
  1038. break;
  1039. default:
  1040. return -EINVAL;
  1041. }
  1042. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
  1043. return 0;
  1044. }
  1045. #define WM9081_RATES SNDRV_PCM_RATE_8000_96000
  1046. #define WM9081_FORMATS \
  1047. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1048. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1049. static const struct snd_soc_dai_ops wm9081_dai_ops = {
  1050. .hw_params = wm9081_hw_params,
  1051. .set_fmt = wm9081_set_dai_fmt,
  1052. .digital_mute = wm9081_digital_mute,
  1053. .set_tdm_slot = wm9081_set_tdm_slot,
  1054. };
  1055. /* We report two channels because the CODEC processes a stereo signal, even
  1056. * though it is only capable of handling a mono output.
  1057. */
  1058. static struct snd_soc_dai_driver wm9081_dai = {
  1059. .name = "wm9081-hifi",
  1060. .playback = {
  1061. .stream_name = "HiFi Playback",
  1062. .channels_min = 1,
  1063. .channels_max = 2,
  1064. .rates = WM9081_RATES,
  1065. .formats = WM9081_FORMATS,
  1066. },
  1067. .ops = &wm9081_dai_ops,
  1068. };
  1069. static int wm9081_probe(struct snd_soc_codec *codec)
  1070. {
  1071. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1072. int ret;
  1073. u16 reg;
  1074. codec->control_data = wm9081->regmap;
  1075. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  1076. if (ret != 0) {
  1077. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1078. return ret;
  1079. }
  1080. reg = 0;
  1081. if (wm9081->pdata.irq_high)
  1082. reg |= WM9081_IRQ_POL;
  1083. if (!wm9081->pdata.irq_cmos)
  1084. reg |= WM9081_IRQ_OP_CTRL;
  1085. snd_soc_update_bits(codec, WM9081_INTERRUPT_CONTROL,
  1086. WM9081_IRQ_POL | WM9081_IRQ_OP_CTRL, reg);
  1087. wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1088. /* Enable zero cross by default */
  1089. snd_soc_update_bits(codec, WM9081_ANALOGUE_LINEOUT,
  1090. WM9081_LINEOUTZC, WM9081_LINEOUTZC);
  1091. snd_soc_update_bits(codec, WM9081_ANALOGUE_SPEAKER_PGA,
  1092. WM9081_SPKPGAZC, WM9081_SPKPGAZC);
  1093. if (!wm9081->pdata.num_retune_configs) {
  1094. dev_dbg(codec->dev,
  1095. "No ReTune Mobile data, using normal EQ\n");
  1096. snd_soc_add_controls(codec, wm9081_eq_controls,
  1097. ARRAY_SIZE(wm9081_eq_controls));
  1098. }
  1099. return ret;
  1100. }
  1101. static int wm9081_remove(struct snd_soc_codec *codec)
  1102. {
  1103. wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1104. return 0;
  1105. }
  1106. #ifdef CONFIG_PM
  1107. static int wm9081_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1108. {
  1109. wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1110. return 0;
  1111. }
  1112. static int wm9081_resume(struct snd_soc_codec *codec)
  1113. {
  1114. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1115. regcache_sync(wm9081->regmap);
  1116. wm9081_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1117. return 0;
  1118. }
  1119. #else
  1120. #define wm9081_suspend NULL
  1121. #define wm9081_resume NULL
  1122. #endif
  1123. static struct snd_soc_codec_driver soc_codec_dev_wm9081 = {
  1124. .probe = wm9081_probe,
  1125. .remove = wm9081_remove,
  1126. .suspend = wm9081_suspend,
  1127. .resume = wm9081_resume,
  1128. .set_sysclk = wm9081_set_sysclk,
  1129. .set_bias_level = wm9081_set_bias_level,
  1130. .controls = wm9081_snd_controls,
  1131. .num_controls = ARRAY_SIZE(wm9081_snd_controls),
  1132. .dapm_widgets = wm9081_dapm_widgets,
  1133. .num_dapm_widgets = ARRAY_SIZE(wm9081_dapm_widgets),
  1134. .dapm_routes = wm9081_audio_paths,
  1135. .num_dapm_routes = ARRAY_SIZE(wm9081_audio_paths),
  1136. };
  1137. static const struct regmap_config wm9081_regmap = {
  1138. .reg_bits = 8,
  1139. .val_bits = 16,
  1140. .max_register = WM9081_MAX_REGISTER,
  1141. .reg_defaults = wm9081_reg,
  1142. .num_reg_defaults = ARRAY_SIZE(wm9081_reg),
  1143. .volatile_reg = wm9081_volatile_register,
  1144. .readable_reg = wm9081_readable_register,
  1145. .cache_type = REGCACHE_RBTREE,
  1146. };
  1147. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1148. static __devinit int wm9081_i2c_probe(struct i2c_client *i2c,
  1149. const struct i2c_device_id *id)
  1150. {
  1151. struct wm9081_priv *wm9081;
  1152. unsigned int reg;
  1153. int ret;
  1154. wm9081 = kzalloc(sizeof(struct wm9081_priv), GFP_KERNEL);
  1155. if (wm9081 == NULL)
  1156. return -ENOMEM;
  1157. i2c_set_clientdata(i2c, wm9081);
  1158. wm9081->regmap = regmap_init_i2c(i2c, &wm9081_regmap);
  1159. if (IS_ERR(wm9081->regmap)) {
  1160. ret = PTR_ERR(wm9081->regmap);
  1161. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1162. goto err;
  1163. }
  1164. ret = regmap_read(wm9081->regmap, WM9081_SOFTWARE_RESET, &reg);
  1165. if (ret != 0) {
  1166. dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
  1167. goto err_regmap;
  1168. }
  1169. if (reg != 0x9081) {
  1170. dev_err(&i2c->dev, "Device is not a WM9081: ID=0x%x\n", reg);
  1171. ret = -EINVAL;
  1172. goto err_regmap;
  1173. }
  1174. ret = wm9081_reset(wm9081->regmap);
  1175. if (ret < 0) {
  1176. dev_err(&i2c->dev, "Failed to issue reset\n");
  1177. goto err_regmap;
  1178. }
  1179. if (dev_get_platdata(&i2c->dev))
  1180. memcpy(&wm9081->pdata, dev_get_platdata(&i2c->dev),
  1181. sizeof(wm9081->pdata));
  1182. ret = snd_soc_register_codec(&i2c->dev,
  1183. &soc_codec_dev_wm9081, &wm9081_dai, 1);
  1184. if (ret < 0)
  1185. goto err_regmap;
  1186. return 0;
  1187. err_regmap:
  1188. regmap_exit(wm9081->regmap);
  1189. err:
  1190. kfree(wm9081);
  1191. return ret;
  1192. }
  1193. static __devexit int wm9081_i2c_remove(struct i2c_client *client)
  1194. {
  1195. struct wm9081_priv *wm9081 = i2c_get_clientdata(client);
  1196. snd_soc_unregister_codec(&client->dev);
  1197. regmap_exit(wm9081->regmap);
  1198. kfree(i2c_get_clientdata(client));
  1199. return 0;
  1200. }
  1201. static const struct i2c_device_id wm9081_i2c_id[] = {
  1202. { "wm9081", 0 },
  1203. { }
  1204. };
  1205. MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
  1206. static struct i2c_driver wm9081_i2c_driver = {
  1207. .driver = {
  1208. .name = "wm9081",
  1209. .owner = THIS_MODULE,
  1210. },
  1211. .probe = wm9081_i2c_probe,
  1212. .remove = __devexit_p(wm9081_i2c_remove),
  1213. .id_table = wm9081_i2c_id,
  1214. };
  1215. #endif
  1216. static int __init wm9081_modinit(void)
  1217. {
  1218. int ret = 0;
  1219. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1220. ret = i2c_add_driver(&wm9081_i2c_driver);
  1221. if (ret != 0) {
  1222. printk(KERN_ERR "Failed to register WM9081 I2C driver: %d\n",
  1223. ret);
  1224. }
  1225. #endif
  1226. return ret;
  1227. }
  1228. module_init(wm9081_modinit);
  1229. static void __exit wm9081_exit(void)
  1230. {
  1231. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1232. i2c_del_driver(&wm9081_i2c_driver);
  1233. #endif
  1234. }
  1235. module_exit(wm9081_exit);
  1236. MODULE_DESCRIPTION("ASoC WM9081 driver");
  1237. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1238. MODULE_LICENSE("GPL");