wm9081.c 36 KB

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