wm9081.c 37 KB

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