wm9081.c 35 KB

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