wm9081.c 37 KB

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