wm9081.c 35 KB

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