wm9081.c 35 KB

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