wm9081.c 36 KB

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