wm9081.c 35 KB

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