wm9081.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. /*
  2. * wm9081.c -- WM9081 ALSA SoC Audio driver
  3. *
  4. * Author: Mark Brown
  5. *
  6. * Copyright 2009-12 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 SOC_ENUM_SINGLE_DECL(drc_high, WM9081_DRC_3, 3, drc_high_text);
  257. static const char *drc_low_text[] = {
  258. "1",
  259. "1/2",
  260. "1/4",
  261. "1/8",
  262. "0",
  263. };
  264. static SOC_ENUM_SINGLE_DECL(drc_low, WM9081_DRC_3, 0, drc_low_text);
  265. static const char *drc_atk_text[] = {
  266. "181us",
  267. "181us",
  268. "363us",
  269. "726us",
  270. "1.45ms",
  271. "2.9ms",
  272. "5.8ms",
  273. "11.6ms",
  274. "23.2ms",
  275. "46.4ms",
  276. "92.8ms",
  277. "185.6ms",
  278. };
  279. static SOC_ENUM_SINGLE_DECL(drc_atk, WM9081_DRC_2, 12, drc_atk_text);
  280. static const char *drc_dcy_text[] = {
  281. "186ms",
  282. "372ms",
  283. "743ms",
  284. "1.49s",
  285. "2.97s",
  286. "5.94s",
  287. "11.89s",
  288. "23.78s",
  289. "47.56s",
  290. };
  291. static SOC_ENUM_SINGLE_DECL(drc_dcy, WM9081_DRC_2, 8, drc_dcy_text);
  292. static const char *drc_qr_dcy_text[] = {
  293. "0.725ms",
  294. "1.45ms",
  295. "5.8ms",
  296. };
  297. static SOC_ENUM_SINGLE_DECL(drc_qr_dcy, WM9081_DRC_2, 4, drc_qr_dcy_text);
  298. static const char *dac_deemph_text[] = {
  299. "None",
  300. "32kHz",
  301. "44.1kHz",
  302. "48kHz",
  303. };
  304. static SOC_ENUM_SINGLE_DECL(dac_deemph, WM9081_DAC_DIGITAL_2, 1,
  305. dac_deemph_text);
  306. static const char *speaker_mode_text[] = {
  307. "Class D",
  308. "Class AB",
  309. };
  310. static SOC_ENUM_SINGLE_DECL(speaker_mode, WM9081_ANALOGUE_SPEAKER_2, 6,
  311. speaker_mode_text);
  312. static int speaker_mode_get(struct snd_kcontrol *kcontrol,
  313. struct snd_ctl_elem_value *ucontrol)
  314. {
  315. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  316. unsigned int reg;
  317. reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  318. if (reg & WM9081_SPK_MODE)
  319. ucontrol->value.integer.value[0] = 1;
  320. else
  321. ucontrol->value.integer.value[0] = 0;
  322. return 0;
  323. }
  324. /*
  325. * Stop any attempts to change speaker mode while the speaker is enabled.
  326. *
  327. * We also have some special anti-pop controls dependent on speaker
  328. * mode which must be changed along with the mode.
  329. */
  330. static int speaker_mode_put(struct snd_kcontrol *kcontrol,
  331. struct snd_ctl_elem_value *ucontrol)
  332. {
  333. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  334. unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
  335. unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  336. /* Are we changing anything? */
  337. if (ucontrol->value.integer.value[0] ==
  338. ((reg2 & WM9081_SPK_MODE) != 0))
  339. return 0;
  340. /* Don't try to change modes while enabled */
  341. if (reg_pwr & WM9081_SPK_ENA)
  342. return -EINVAL;
  343. if (ucontrol->value.integer.value[0]) {
  344. /* Class AB */
  345. reg2 &= ~(WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL);
  346. reg2 |= WM9081_SPK_MODE;
  347. } else {
  348. /* Class D */
  349. reg2 |= WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL;
  350. reg2 &= ~WM9081_SPK_MODE;
  351. }
  352. snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
  353. return 0;
  354. }
  355. static const struct snd_kcontrol_new wm9081_snd_controls[] = {
  356. SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
  357. SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
  358. SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
  359. SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
  360. SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
  361. SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
  362. SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
  363. SOC_ENUM("DRC High Slope", drc_high),
  364. SOC_ENUM("DRC Low Slope", drc_low),
  365. SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
  366. SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
  367. SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
  368. SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
  369. SOC_ENUM("DRC Attack", drc_atk),
  370. SOC_ENUM("DRC Decay", drc_dcy),
  371. SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
  372. SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
  373. SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
  374. SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
  375. SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
  376. SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
  377. SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
  378. SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
  379. SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
  380. SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
  381. out_tlv),
  382. SOC_ENUM("DAC Deemphasis", dac_deemph),
  383. SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
  384. };
  385. static const struct snd_kcontrol_new wm9081_eq_controls[] = {
  386. SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
  387. SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
  388. SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
  389. SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
  390. SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
  391. };
  392. static const struct snd_kcontrol_new mixer[] = {
  393. SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
  394. SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
  395. SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
  396. };
  397. struct _fll_div {
  398. u16 fll_fratio;
  399. u16 fll_outdiv;
  400. u16 fll_clk_ref_div;
  401. u16 n;
  402. u16 k;
  403. };
  404. /* The size in bits of the FLL divide multiplied by 10
  405. * to allow rounding later */
  406. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  407. static struct {
  408. unsigned int min;
  409. unsigned int max;
  410. u16 fll_fratio;
  411. int ratio;
  412. } fll_fratios[] = {
  413. { 0, 64000, 4, 16 },
  414. { 64000, 128000, 3, 8 },
  415. { 128000, 256000, 2, 4 },
  416. { 256000, 1000000, 1, 2 },
  417. { 1000000, 13500000, 0, 1 },
  418. };
  419. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  420. unsigned int Fout)
  421. {
  422. u64 Kpart;
  423. unsigned int K, Ndiv, Nmod, target;
  424. unsigned int div;
  425. int i;
  426. /* Fref must be <=13.5MHz */
  427. div = 1;
  428. while ((Fref / div) > 13500000) {
  429. div *= 2;
  430. if (div > 8) {
  431. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  432. Fref);
  433. return -EINVAL;
  434. }
  435. }
  436. fll_div->fll_clk_ref_div = div / 2;
  437. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  438. /* Apply the division for our remaining calculations */
  439. Fref /= div;
  440. /* Fvco should be 90-100MHz; don't check the upper bound */
  441. div = 0;
  442. target = Fout * 2;
  443. while (target < 90000000) {
  444. div++;
  445. target *= 2;
  446. if (div > 7) {
  447. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  448. Fout);
  449. return -EINVAL;
  450. }
  451. }
  452. fll_div->fll_outdiv = div;
  453. pr_debug("Fvco=%dHz\n", target);
  454. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  455. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  456. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  457. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  458. target /= fll_fratios[i].ratio;
  459. break;
  460. }
  461. }
  462. if (i == ARRAY_SIZE(fll_fratios)) {
  463. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  464. return -EINVAL;
  465. }
  466. /* Now, calculate N.K */
  467. Ndiv = target / Fref;
  468. fll_div->n = Ndiv;
  469. Nmod = target % Fref;
  470. pr_debug("Nmod=%d\n", Nmod);
  471. /* Calculate fractional part - scale up so we can round. */
  472. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  473. do_div(Kpart, Fref);
  474. K = Kpart & 0xFFFFFFFF;
  475. if ((K % 10) >= 5)
  476. K += 5;
  477. /* Move down to proper range now rounding is done */
  478. fll_div->k = K / 10;
  479. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  480. fll_div->n, fll_div->k,
  481. fll_div->fll_fratio, fll_div->fll_outdiv,
  482. fll_div->fll_clk_ref_div);
  483. return 0;
  484. }
  485. static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
  486. unsigned int Fref, unsigned int Fout)
  487. {
  488. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  489. u16 reg1, reg4, reg5;
  490. struct _fll_div fll_div;
  491. int ret;
  492. int clk_sys_reg;
  493. /* Any change? */
  494. if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
  495. return 0;
  496. /* Disable the FLL */
  497. if (Fout == 0) {
  498. dev_dbg(codec->dev, "FLL disabled\n");
  499. wm9081->fll_fref = 0;
  500. wm9081->fll_fout = 0;
  501. return 0;
  502. }
  503. ret = fll_factors(&fll_div, Fref, Fout);
  504. if (ret != 0)
  505. return ret;
  506. reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
  507. reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
  508. switch (fll_id) {
  509. case WM9081_SYSCLK_FLL_MCLK:
  510. reg5 |= 0x1;
  511. break;
  512. default:
  513. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  514. return -EINVAL;
  515. }
  516. /* Disable CLK_SYS while we reconfigure */
  517. clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  518. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  519. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
  520. clk_sys_reg & ~WM9081_CLK_SYS_ENA);
  521. /* Any FLL configuration change requires that the FLL be
  522. * disabled first. */
  523. reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
  524. reg1 &= ~WM9081_FLL_ENA;
  525. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  526. /* Apply the configuration */
  527. if (fll_div.k)
  528. reg1 |= WM9081_FLL_FRAC_MASK;
  529. else
  530. reg1 &= ~WM9081_FLL_FRAC_MASK;
  531. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  532. snd_soc_write(codec, WM9081_FLL_CONTROL_2,
  533. (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
  534. (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
  535. snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
  536. reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
  537. reg4 &= ~WM9081_FLL_N_MASK;
  538. reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
  539. snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
  540. reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
  541. reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
  542. snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
  543. /* Set gain to the recommended value */
  544. snd_soc_update_bits(codec, WM9081_FLL_CONTROL_4,
  545. WM9081_FLL_GAIN_MASK, 0);
  546. /* Enable the FLL */
  547. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
  548. /* Then bring CLK_SYS up again if it was disabled */
  549. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  550. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
  551. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  552. wm9081->fll_fref = Fref;
  553. wm9081->fll_fout = Fout;
  554. return 0;
  555. }
  556. static int configure_clock(struct snd_soc_codec *codec)
  557. {
  558. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  559. int new_sysclk, i, target;
  560. unsigned int reg;
  561. int ret = 0;
  562. int mclkdiv = 0;
  563. int fll = 0;
  564. switch (wm9081->sysclk_source) {
  565. case WM9081_SYSCLK_MCLK:
  566. if (wm9081->mclk_rate > 12225000) {
  567. mclkdiv = 1;
  568. wm9081->sysclk_rate = wm9081->mclk_rate / 2;
  569. } else {
  570. wm9081->sysclk_rate = wm9081->mclk_rate;
  571. }
  572. wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
  573. break;
  574. case WM9081_SYSCLK_FLL_MCLK:
  575. /* If we have a sample rate calculate a CLK_SYS that
  576. * gives us a suitable DAC configuration, plus BCLK.
  577. * Ideally we would check to see if we can clock
  578. * directly from MCLK and only use the FLL if this is
  579. * not the case, though care must be taken with free
  580. * running mode.
  581. */
  582. if (wm9081->master && wm9081->bclk) {
  583. /* Make sure we can generate CLK_SYS and BCLK
  584. * and that we've got 3MHz for optimal
  585. * performance. */
  586. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  587. target = wm9081->fs * clk_sys_rates[i].ratio;
  588. new_sysclk = target;
  589. if (target >= wm9081->bclk &&
  590. target > 3000000)
  591. break;
  592. }
  593. if (i == ARRAY_SIZE(clk_sys_rates))
  594. return -EINVAL;
  595. } else if (wm9081->fs) {
  596. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  597. new_sysclk = clk_sys_rates[i].ratio
  598. * wm9081->fs;
  599. if (new_sysclk > 3000000)
  600. break;
  601. }
  602. if (i == ARRAY_SIZE(clk_sys_rates))
  603. return -EINVAL;
  604. } else {
  605. new_sysclk = 12288000;
  606. }
  607. ret = wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK,
  608. wm9081->mclk_rate, new_sysclk);
  609. if (ret == 0) {
  610. wm9081->sysclk_rate = new_sysclk;
  611. /* Switch SYSCLK over to FLL */
  612. fll = 1;
  613. } else {
  614. wm9081->sysclk_rate = wm9081->mclk_rate;
  615. }
  616. break;
  617. default:
  618. return -EINVAL;
  619. }
  620. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
  621. if (mclkdiv)
  622. reg |= WM9081_MCLKDIV2;
  623. else
  624. reg &= ~WM9081_MCLKDIV2;
  625. snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
  626. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  627. if (fll)
  628. reg |= WM9081_CLK_SRC_SEL;
  629. else
  630. reg &= ~WM9081_CLK_SRC_SEL;
  631. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
  632. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
  633. return ret;
  634. }
  635. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  636. struct snd_kcontrol *kcontrol, int event)
  637. {
  638. struct snd_soc_codec *codec = w->codec;
  639. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  640. /* This should be done on init() for bypass paths */
  641. switch (wm9081->sysclk_source) {
  642. case WM9081_SYSCLK_MCLK:
  643. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm9081->mclk_rate);
  644. break;
  645. case WM9081_SYSCLK_FLL_MCLK:
  646. dev_dbg(codec->dev, "Using %dHz MCLK with FLL\n",
  647. wm9081->mclk_rate);
  648. break;
  649. default:
  650. dev_err(codec->dev, "System clock not configured\n");
  651. return -EINVAL;
  652. }
  653. switch (event) {
  654. case SND_SOC_DAPM_PRE_PMU:
  655. configure_clock(codec);
  656. break;
  657. case SND_SOC_DAPM_POST_PMD:
  658. /* Disable the FLL if it's running */
  659. wm9081_set_fll(codec, 0, 0, 0);
  660. break;
  661. }
  662. return 0;
  663. }
  664. static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
  665. SND_SOC_DAPM_INPUT("IN1"),
  666. SND_SOC_DAPM_INPUT("IN2"),
  667. SND_SOC_DAPM_DAC("DAC", NULL, WM9081_POWER_MANAGEMENT, 0, 0),
  668. SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
  669. mixer, ARRAY_SIZE(mixer)),
  670. SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
  671. SND_SOC_DAPM_PGA("Speaker PGA", WM9081_POWER_MANAGEMENT, 2, 0, NULL, 0),
  672. SND_SOC_DAPM_OUT_DRV("Speaker", WM9081_POWER_MANAGEMENT, 1, 0, NULL, 0),
  673. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  674. SND_SOC_DAPM_OUTPUT("SPKN"),
  675. SND_SOC_DAPM_OUTPUT("SPKP"),
  676. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM9081_CLOCK_CONTROL_3, 0, 0, clk_sys_event,
  677. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  678. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
  679. SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
  680. SND_SOC_DAPM_SUPPLY("TSENSE", WM9081_POWER_MANAGEMENT, 7, 0, NULL, 0),
  681. };
  682. static const struct snd_soc_dapm_route wm9081_audio_paths[] = {
  683. { "DAC", NULL, "CLK_SYS" },
  684. { "DAC", NULL, "CLK_DSP" },
  685. { "DAC", NULL, "AIF" },
  686. { "Mixer", "IN1 Switch", "IN1" },
  687. { "Mixer", "IN2 Switch", "IN2" },
  688. { "Mixer", "Playback Switch", "DAC" },
  689. { "LINEOUT PGA", NULL, "Mixer" },
  690. { "LINEOUT PGA", NULL, "TOCLK" },
  691. { "LINEOUT PGA", NULL, "CLK_SYS" },
  692. { "LINEOUT", NULL, "LINEOUT PGA" },
  693. { "Speaker PGA", NULL, "Mixer" },
  694. { "Speaker PGA", NULL, "TOCLK" },
  695. { "Speaker PGA", NULL, "CLK_SYS" },
  696. { "Speaker", NULL, "Speaker PGA" },
  697. { "Speaker", NULL, "TSENSE" },
  698. { "SPKN", NULL, "Speaker" },
  699. { "SPKP", NULL, "Speaker" },
  700. };
  701. static int wm9081_set_bias_level(struct snd_soc_codec *codec,
  702. enum snd_soc_bias_level level)
  703. {
  704. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  705. switch (level) {
  706. case SND_SOC_BIAS_ON:
  707. break;
  708. case SND_SOC_BIAS_PREPARE:
  709. /* VMID=2*40k */
  710. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  711. WM9081_VMID_SEL_MASK, 0x2);
  712. /* Normal bias current */
  713. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  714. WM9081_STBY_BIAS_ENA, 0);
  715. break;
  716. case SND_SOC_BIAS_STANDBY:
  717. /* Initial cold start */
  718. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  719. regcache_cache_only(wm9081->regmap, false);
  720. regcache_sync(wm9081->regmap);
  721. /* Disable LINEOUT discharge */
  722. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  723. WM9081_LINEOUT_DISCH, 0);
  724. /* Select startup bias source */
  725. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  726. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  727. WM9081_BIAS_SRC | WM9081_BIAS_ENA);
  728. /* VMID 2*4k; Soft VMID ramp enable */
  729. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  730. WM9081_VMID_RAMP |
  731. WM9081_VMID_SEL_MASK,
  732. WM9081_VMID_RAMP | 0x6);
  733. mdelay(100);
  734. /* Normal bias enable & soft start off */
  735. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  736. WM9081_VMID_RAMP, 0);
  737. /* Standard bias source */
  738. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  739. WM9081_BIAS_SRC, 0);
  740. }
  741. /* VMID 2*240k */
  742. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  743. WM9081_VMID_SEL_MASK, 0x04);
  744. /* Standby bias current on */
  745. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  746. WM9081_STBY_BIAS_ENA,
  747. WM9081_STBY_BIAS_ENA);
  748. break;
  749. case SND_SOC_BIAS_OFF:
  750. /* Startup bias source and disable bias */
  751. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  752. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  753. WM9081_BIAS_SRC);
  754. /* Disable VMID with soft ramping */
  755. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  756. WM9081_VMID_RAMP | WM9081_VMID_SEL_MASK,
  757. WM9081_VMID_RAMP);
  758. /* Actively discharge LINEOUT */
  759. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  760. WM9081_LINEOUT_DISCH,
  761. WM9081_LINEOUT_DISCH);
  762. regcache_cache_only(wm9081->regmap, true);
  763. break;
  764. }
  765. codec->dapm.bias_level = level;
  766. return 0;
  767. }
  768. static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
  769. unsigned int fmt)
  770. {
  771. struct snd_soc_codec *codec = dai->codec;
  772. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  773. unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  774. aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
  775. WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
  776. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  777. case SND_SOC_DAIFMT_CBS_CFS:
  778. wm9081->master = 0;
  779. break;
  780. case SND_SOC_DAIFMT_CBS_CFM:
  781. aif2 |= WM9081_LRCLK_DIR;
  782. wm9081->master = 1;
  783. break;
  784. case SND_SOC_DAIFMT_CBM_CFS:
  785. aif2 |= WM9081_BCLK_DIR;
  786. wm9081->master = 1;
  787. break;
  788. case SND_SOC_DAIFMT_CBM_CFM:
  789. aif2 |= WM9081_LRCLK_DIR | WM9081_BCLK_DIR;
  790. wm9081->master = 1;
  791. break;
  792. default:
  793. return -EINVAL;
  794. }
  795. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  796. case SND_SOC_DAIFMT_DSP_B:
  797. aif2 |= WM9081_AIF_LRCLK_INV;
  798. case SND_SOC_DAIFMT_DSP_A:
  799. aif2 |= 0x3;
  800. break;
  801. case SND_SOC_DAIFMT_I2S:
  802. aif2 |= 0x2;
  803. break;
  804. case SND_SOC_DAIFMT_RIGHT_J:
  805. break;
  806. case SND_SOC_DAIFMT_LEFT_J:
  807. aif2 |= 0x1;
  808. break;
  809. default:
  810. return -EINVAL;
  811. }
  812. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  813. case SND_SOC_DAIFMT_DSP_A:
  814. case SND_SOC_DAIFMT_DSP_B:
  815. /* frame inversion not valid for DSP modes */
  816. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  817. case SND_SOC_DAIFMT_NB_NF:
  818. break;
  819. case SND_SOC_DAIFMT_IB_NF:
  820. aif2 |= WM9081_AIF_BCLK_INV;
  821. break;
  822. default:
  823. return -EINVAL;
  824. }
  825. break;
  826. case SND_SOC_DAIFMT_I2S:
  827. case SND_SOC_DAIFMT_RIGHT_J:
  828. case SND_SOC_DAIFMT_LEFT_J:
  829. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  830. case SND_SOC_DAIFMT_NB_NF:
  831. break;
  832. case SND_SOC_DAIFMT_IB_IF:
  833. aif2 |= WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV;
  834. break;
  835. case SND_SOC_DAIFMT_IB_NF:
  836. aif2 |= WM9081_AIF_BCLK_INV;
  837. break;
  838. case SND_SOC_DAIFMT_NB_IF:
  839. aif2 |= WM9081_AIF_LRCLK_INV;
  840. break;
  841. default:
  842. return -EINVAL;
  843. }
  844. break;
  845. default:
  846. return -EINVAL;
  847. }
  848. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  849. return 0;
  850. }
  851. static int wm9081_hw_params(struct snd_pcm_substream *substream,
  852. struct snd_pcm_hw_params *params,
  853. struct snd_soc_dai *dai)
  854. {
  855. struct snd_soc_codec *codec = dai->codec;
  856. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  857. int ret, i, best, best_val, cur_val;
  858. unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
  859. clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
  860. clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
  861. aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  862. aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  863. aif2 &= ~WM9081_AIF_WL_MASK;
  864. aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
  865. aif3 &= ~WM9081_BCLK_DIV_MASK;
  866. aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
  867. aif4 &= ~WM9081_LRCLK_RATE_MASK;
  868. wm9081->fs = params_rate(params);
  869. if (wm9081->tdm_width) {
  870. /* If TDM is set up then that fixes our BCLK. */
  871. int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
  872. WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
  873. wm9081->bclk = wm9081->fs * wm9081->tdm_width * slots;
  874. } else {
  875. /* Otherwise work out a BCLK from the sample size */
  876. wm9081->bclk = 2 * wm9081->fs;
  877. switch (params_width(params)) {
  878. case 16:
  879. wm9081->bclk *= 16;
  880. break;
  881. case 20:
  882. wm9081->bclk *= 20;
  883. aif2 |= 0x4;
  884. break;
  885. case 24:
  886. wm9081->bclk *= 24;
  887. aif2 |= 0x8;
  888. break;
  889. case 32:
  890. wm9081->bclk *= 32;
  891. aif2 |= 0xc;
  892. break;
  893. default:
  894. return -EINVAL;
  895. }
  896. }
  897. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
  898. ret = configure_clock(codec);
  899. if (ret != 0)
  900. return ret;
  901. /* Select nearest CLK_SYS_RATE */
  902. best = 0;
  903. best_val = abs((wm9081->sysclk_rate / clk_sys_rates[0].ratio)
  904. - wm9081->fs);
  905. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  906. cur_val = abs((wm9081->sysclk_rate /
  907. clk_sys_rates[i].ratio) - wm9081->fs);
  908. if (cur_val < best_val) {
  909. best = i;
  910. best_val = cur_val;
  911. }
  912. }
  913. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  914. clk_sys_rates[best].ratio);
  915. clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
  916. << WM9081_CLK_SYS_RATE_SHIFT);
  917. /* SAMPLE_RATE */
  918. best = 0;
  919. best_val = abs(wm9081->fs - sample_rates[0].rate);
  920. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  921. /* Closest match */
  922. cur_val = abs(wm9081->fs - sample_rates[i].rate);
  923. if (cur_val < best_val) {
  924. best = i;
  925. best_val = cur_val;
  926. }
  927. }
  928. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  929. sample_rates[best].rate);
  930. clk_ctrl2 |= (sample_rates[best].sample_rate
  931. << WM9081_SAMPLE_RATE_SHIFT);
  932. /* BCLK_DIV */
  933. best = 0;
  934. best_val = INT_MAX;
  935. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  936. cur_val = ((wm9081->sysclk_rate * 10) / bclk_divs[i].div)
  937. - wm9081->bclk;
  938. if (cur_val < 0) /* Table is sorted */
  939. break;
  940. if (cur_val < best_val) {
  941. best = i;
  942. best_val = cur_val;
  943. }
  944. }
  945. wm9081->bclk = (wm9081->sysclk_rate * 10) / bclk_divs[best].div;
  946. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  947. bclk_divs[best].div, wm9081->bclk);
  948. aif3 |= bclk_divs[best].bclk_div;
  949. /* LRCLK is a simple fraction of BCLK */
  950. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm9081->bclk / wm9081->fs);
  951. aif4 |= wm9081->bclk / wm9081->fs;
  952. /* Apply a ReTune Mobile configuration if it's in use */
  953. if (wm9081->pdata.num_retune_configs) {
  954. struct wm9081_pdata *pdata = &wm9081->pdata;
  955. struct wm9081_retune_mobile_setting *s;
  956. int eq1;
  957. best = 0;
  958. best_val = abs(pdata->retune_configs[0].rate - wm9081->fs);
  959. for (i = 0; i < pdata->num_retune_configs; i++) {
  960. cur_val = abs(pdata->retune_configs[i].rate -
  961. wm9081->fs);
  962. if (cur_val < best_val) {
  963. best_val = cur_val;
  964. best = i;
  965. }
  966. }
  967. s = &pdata->retune_configs[best];
  968. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  969. s->name, s->rate);
  970. /* If the EQ is enabled then disable it while we write out */
  971. eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
  972. if (eq1 & WM9081_EQ_ENA)
  973. snd_soc_write(codec, WM9081_EQ_1, 0);
  974. /* Write out the other values */
  975. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  976. snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
  977. eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
  978. snd_soc_write(codec, WM9081_EQ_1, eq1);
  979. }
  980. snd_soc_write(codec, WM9081_CLOCK_CONTROL_2, clk_ctrl2);
  981. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  982. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_3, aif3);
  983. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_4, aif4);
  984. return 0;
  985. }
  986. static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  987. {
  988. struct snd_soc_codec *codec = codec_dai->codec;
  989. unsigned int reg;
  990. reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
  991. if (mute)
  992. reg |= WM9081_DAC_MUTE;
  993. else
  994. reg &= ~WM9081_DAC_MUTE;
  995. snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
  996. return 0;
  997. }
  998. static int wm9081_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  999. int source, unsigned int freq, int dir)
  1000. {
  1001. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1002. switch (clk_id) {
  1003. case WM9081_SYSCLK_MCLK:
  1004. case WM9081_SYSCLK_FLL_MCLK:
  1005. wm9081->sysclk_source = clk_id;
  1006. wm9081->mclk_rate = freq;
  1007. break;
  1008. default:
  1009. return -EINVAL;
  1010. }
  1011. return 0;
  1012. }
  1013. static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
  1014. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  1015. {
  1016. struct snd_soc_codec *codec = dai->codec;
  1017. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1018. unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  1019. aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
  1020. if (slots < 0 || slots > 4)
  1021. return -EINVAL;
  1022. wm9081->tdm_width = slot_width;
  1023. if (slots == 0)
  1024. slots = 1;
  1025. aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
  1026. switch (rx_mask) {
  1027. case 1:
  1028. break;
  1029. case 2:
  1030. aif1 |= 0x10;
  1031. break;
  1032. case 4:
  1033. aif1 |= 0x20;
  1034. break;
  1035. case 8:
  1036. aif1 |= 0x30;
  1037. break;
  1038. default:
  1039. return -EINVAL;
  1040. }
  1041. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
  1042. return 0;
  1043. }
  1044. #define WM9081_RATES SNDRV_PCM_RATE_8000_96000
  1045. #define WM9081_FORMATS \
  1046. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1047. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1048. static const struct snd_soc_dai_ops wm9081_dai_ops = {
  1049. .hw_params = wm9081_hw_params,
  1050. .set_fmt = wm9081_set_dai_fmt,
  1051. .digital_mute = wm9081_digital_mute,
  1052. .set_tdm_slot = wm9081_set_tdm_slot,
  1053. };
  1054. /* We report two channels because the CODEC processes a stereo signal, even
  1055. * though it is only capable of handling a mono output.
  1056. */
  1057. static struct snd_soc_dai_driver wm9081_dai = {
  1058. .name = "wm9081-hifi",
  1059. .playback = {
  1060. .stream_name = "AIF",
  1061. .channels_min = 1,
  1062. .channels_max = 2,
  1063. .rates = WM9081_RATES,
  1064. .formats = WM9081_FORMATS,
  1065. },
  1066. .ops = &wm9081_dai_ops,
  1067. };
  1068. static int wm9081_probe(struct snd_soc_codec *codec)
  1069. {
  1070. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1071. /* Enable zero cross by default */
  1072. snd_soc_update_bits(codec, WM9081_ANALOGUE_LINEOUT,
  1073. WM9081_LINEOUTZC, WM9081_LINEOUTZC);
  1074. snd_soc_update_bits(codec, WM9081_ANALOGUE_SPEAKER_PGA,
  1075. WM9081_SPKPGAZC, WM9081_SPKPGAZC);
  1076. if (!wm9081->pdata.num_retune_configs) {
  1077. dev_dbg(codec->dev,
  1078. "No ReTune Mobile data, using normal EQ\n");
  1079. snd_soc_add_codec_controls(codec, wm9081_eq_controls,
  1080. ARRAY_SIZE(wm9081_eq_controls));
  1081. }
  1082. return 0;
  1083. }
  1084. static int wm9081_remove(struct snd_soc_codec *codec)
  1085. {
  1086. wm9081_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1087. return 0;
  1088. }
  1089. static struct snd_soc_codec_driver soc_codec_dev_wm9081 = {
  1090. .probe = wm9081_probe,
  1091. .remove = wm9081_remove,
  1092. .set_sysclk = wm9081_set_sysclk,
  1093. .set_bias_level = wm9081_set_bias_level,
  1094. .idle_bias_off = true,
  1095. .controls = wm9081_snd_controls,
  1096. .num_controls = ARRAY_SIZE(wm9081_snd_controls),
  1097. .dapm_widgets = wm9081_dapm_widgets,
  1098. .num_dapm_widgets = ARRAY_SIZE(wm9081_dapm_widgets),
  1099. .dapm_routes = wm9081_audio_paths,
  1100. .num_dapm_routes = ARRAY_SIZE(wm9081_audio_paths),
  1101. };
  1102. static const struct regmap_config wm9081_regmap = {
  1103. .reg_bits = 8,
  1104. .val_bits = 16,
  1105. .max_register = WM9081_MAX_REGISTER,
  1106. .reg_defaults = wm9081_reg,
  1107. .num_reg_defaults = ARRAY_SIZE(wm9081_reg),
  1108. .volatile_reg = wm9081_volatile_register,
  1109. .readable_reg = wm9081_readable_register,
  1110. .cache_type = REGCACHE_RBTREE,
  1111. };
  1112. #if IS_ENABLED(CONFIG_I2C)
  1113. static int wm9081_i2c_probe(struct i2c_client *i2c,
  1114. const struct i2c_device_id *id)
  1115. {
  1116. struct wm9081_priv *wm9081;
  1117. unsigned int reg;
  1118. int ret;
  1119. wm9081 = devm_kzalloc(&i2c->dev, sizeof(struct wm9081_priv),
  1120. GFP_KERNEL);
  1121. if (wm9081 == NULL)
  1122. return -ENOMEM;
  1123. i2c_set_clientdata(i2c, wm9081);
  1124. wm9081->regmap = devm_regmap_init_i2c(i2c, &wm9081_regmap);
  1125. if (IS_ERR(wm9081->regmap)) {
  1126. ret = PTR_ERR(wm9081->regmap);
  1127. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1128. return ret;
  1129. }
  1130. ret = regmap_read(wm9081->regmap, WM9081_SOFTWARE_RESET, &reg);
  1131. if (ret != 0) {
  1132. dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
  1133. return ret;
  1134. }
  1135. if (reg != 0x9081) {
  1136. dev_err(&i2c->dev, "Device is not a WM9081: ID=0x%x\n", reg);
  1137. return -EINVAL;
  1138. }
  1139. ret = wm9081_reset(wm9081->regmap);
  1140. if (ret < 0) {
  1141. dev_err(&i2c->dev, "Failed to issue reset\n");
  1142. return ret;
  1143. }
  1144. if (dev_get_platdata(&i2c->dev))
  1145. memcpy(&wm9081->pdata, dev_get_platdata(&i2c->dev),
  1146. sizeof(wm9081->pdata));
  1147. reg = 0;
  1148. if (wm9081->pdata.irq_high)
  1149. reg |= WM9081_IRQ_POL;
  1150. if (!wm9081->pdata.irq_cmos)
  1151. reg |= WM9081_IRQ_OP_CTRL;
  1152. regmap_update_bits(wm9081->regmap, WM9081_INTERRUPT_CONTROL,
  1153. WM9081_IRQ_POL | WM9081_IRQ_OP_CTRL, reg);
  1154. regcache_cache_only(wm9081->regmap, true);
  1155. ret = snd_soc_register_codec(&i2c->dev,
  1156. &soc_codec_dev_wm9081, &wm9081_dai, 1);
  1157. if (ret < 0)
  1158. return ret;
  1159. return 0;
  1160. }
  1161. static int wm9081_i2c_remove(struct i2c_client *client)
  1162. {
  1163. snd_soc_unregister_codec(&client->dev);
  1164. return 0;
  1165. }
  1166. static const struct i2c_device_id wm9081_i2c_id[] = {
  1167. { "wm9081", 0 },
  1168. { }
  1169. };
  1170. MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
  1171. static struct i2c_driver wm9081_i2c_driver = {
  1172. .driver = {
  1173. .name = "wm9081",
  1174. .owner = THIS_MODULE,
  1175. },
  1176. .probe = wm9081_i2c_probe,
  1177. .remove = wm9081_i2c_remove,
  1178. .id_table = wm9081_i2c_id,
  1179. };
  1180. #endif
  1181. module_i2c_driver(wm9081_i2c_driver);
  1182. MODULE_DESCRIPTION("ASoC WM9081 driver");
  1183. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1184. MODULE_LICENSE("GPL");