wm9081.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * wm9081.c -- WM9081 ALSA SoC Audio driver
  3. *
  4. * Author: Mark Brown
  5. *
  6. * Copyright 2009-12 Wolfson Microelectronics plc
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <sound/wm9081.h>
  29. #include "wm9081.h"
  30. static const struct reg_default wm9081_reg[] = {
  31. { 2, 0x00B9 }, /* R2 - Analogue Lineout */
  32. { 3, 0x00B9 }, /* R3 - Analogue Speaker PGA */
  33. { 4, 0x0001 }, /* R4 - VMID Control */
  34. { 5, 0x0068 }, /* R5 - Bias Control 1 */
  35. { 7, 0x0000 }, /* R7 - Analogue Mixer */
  36. { 8, 0x0000 }, /* R8 - Anti Pop Control */
  37. { 9, 0x01DB }, /* R9 - Analogue Speaker 1 */
  38. { 10, 0x0018 }, /* R10 - Analogue Speaker 2 */
  39. { 11, 0x0180 }, /* R11 - Power Management */
  40. { 12, 0x0000 }, /* R12 - Clock Control 1 */
  41. { 13, 0x0038 }, /* R13 - Clock Control 2 */
  42. { 14, 0x4000 }, /* R14 - Clock Control 3 */
  43. { 16, 0x0000 }, /* R16 - FLL Control 1 */
  44. { 17, 0x0200 }, /* R17 - FLL Control 2 */
  45. { 18, 0x0000 }, /* R18 - FLL Control 3 */
  46. { 19, 0x0204 }, /* R19 - FLL Control 4 */
  47. { 20, 0x0000 }, /* R20 - FLL Control 5 */
  48. { 22, 0x0000 }, /* R22 - Audio Interface 1 */
  49. { 23, 0x0002 }, /* R23 - Audio Interface 2 */
  50. { 24, 0x0008 }, /* R24 - Audio Interface 3 */
  51. { 25, 0x0022 }, /* R25 - Audio Interface 4 */
  52. { 27, 0x0006 }, /* R27 - Interrupt Status Mask */
  53. { 28, 0x0000 }, /* R28 - Interrupt Polarity */
  54. { 29, 0x0000 }, /* R29 - Interrupt Control */
  55. { 30, 0x00C0 }, /* R30 - DAC Digital 1 */
  56. { 31, 0x0008 }, /* R31 - DAC Digital 2 */
  57. { 32, 0x09AF }, /* R32 - DRC 1 */
  58. { 33, 0x4201 }, /* R33 - DRC 2 */
  59. { 34, 0x0000 }, /* R34 - DRC 3 */
  60. { 35, 0x0000 }, /* R35 - DRC 4 */
  61. { 38, 0x0000 }, /* R38 - Write Sequencer 1 */
  62. { 39, 0x0000 }, /* R39 - Write Sequencer 2 */
  63. { 40, 0x0002 }, /* R40 - MW Slave 1 */
  64. { 42, 0x0000 }, /* R42 - EQ 1 */
  65. { 43, 0x0000 }, /* R43 - EQ 2 */
  66. { 44, 0x0FCA }, /* R44 - EQ 3 */
  67. { 45, 0x0400 }, /* R45 - EQ 4 */
  68. { 46, 0x00B8 }, /* R46 - EQ 5 */
  69. { 47, 0x1EB5 }, /* R47 - EQ 6 */
  70. { 48, 0xF145 }, /* R48 - EQ 7 */
  71. { 49, 0x0B75 }, /* R49 - EQ 8 */
  72. { 50, 0x01C5 }, /* R50 - EQ 9 */
  73. { 51, 0x169E }, /* R51 - EQ 10 */
  74. { 52, 0xF829 }, /* R52 - EQ 11 */
  75. { 53, 0x07AD }, /* R53 - EQ 12 */
  76. { 54, 0x1103 }, /* R54 - EQ 13 */
  77. { 55, 0x1C58 }, /* R55 - EQ 14 */
  78. { 56, 0xF373 }, /* R56 - EQ 15 */
  79. { 57, 0x0A54 }, /* R57 - EQ 16 */
  80. { 58, 0x0558 }, /* R58 - EQ 17 */
  81. { 59, 0x0564 }, /* R59 - EQ 18 */
  82. { 60, 0x0559 }, /* R60 - EQ 19 */
  83. { 61, 0x4000 }, /* R61 - EQ 20 */
  84. };
  85. static struct {
  86. int ratio;
  87. int clk_sys_rate;
  88. } clk_sys_rates[] = {
  89. { 64, 0 },
  90. { 128, 1 },
  91. { 192, 2 },
  92. { 256, 3 },
  93. { 384, 4 },
  94. { 512, 5 },
  95. { 768, 6 },
  96. { 1024, 7 },
  97. { 1408, 8 },
  98. { 1536, 9 },
  99. };
  100. static struct {
  101. int rate;
  102. int sample_rate;
  103. } sample_rates[] = {
  104. { 8000, 0 },
  105. { 11025, 1 },
  106. { 12000, 2 },
  107. { 16000, 3 },
  108. { 22050, 4 },
  109. { 24000, 5 },
  110. { 32000, 6 },
  111. { 44100, 7 },
  112. { 48000, 8 },
  113. { 88200, 9 },
  114. { 96000, 10 },
  115. };
  116. static struct {
  117. int div; /* *10 due to .5s */
  118. int bclk_div;
  119. } bclk_divs[] = {
  120. { 10, 0 },
  121. { 15, 1 },
  122. { 20, 2 },
  123. { 30, 3 },
  124. { 40, 4 },
  125. { 50, 5 },
  126. { 55, 6 },
  127. { 60, 7 },
  128. { 80, 8 },
  129. { 100, 9 },
  130. { 110, 10 },
  131. { 120, 11 },
  132. { 160, 12 },
  133. { 200, 13 },
  134. { 220, 14 },
  135. { 240, 15 },
  136. { 250, 16 },
  137. { 300, 17 },
  138. { 320, 18 },
  139. { 440, 19 },
  140. { 480, 20 },
  141. };
  142. struct wm9081_priv {
  143. struct regmap *regmap;
  144. int sysclk_source;
  145. int mclk_rate;
  146. int sysclk_rate;
  147. int fs;
  148. int bclk;
  149. int master;
  150. int fll_fref;
  151. int fll_fout;
  152. int tdm_width;
  153. struct wm9081_pdata pdata;
  154. };
  155. static bool wm9081_volatile_register(struct device *dev, unsigned int reg)
  156. {
  157. switch (reg) {
  158. case WM9081_SOFTWARE_RESET:
  159. case WM9081_INTERRUPT_STATUS:
  160. return true;
  161. default:
  162. return false;
  163. }
  164. }
  165. static bool wm9081_readable_register(struct device *dev, unsigned int reg)
  166. {
  167. switch (reg) {
  168. case WM9081_SOFTWARE_RESET:
  169. case WM9081_ANALOGUE_LINEOUT:
  170. case WM9081_ANALOGUE_SPEAKER_PGA:
  171. case WM9081_VMID_CONTROL:
  172. case WM9081_BIAS_CONTROL_1:
  173. case WM9081_ANALOGUE_MIXER:
  174. case WM9081_ANTI_POP_CONTROL:
  175. case WM9081_ANALOGUE_SPEAKER_1:
  176. case WM9081_ANALOGUE_SPEAKER_2:
  177. case WM9081_POWER_MANAGEMENT:
  178. case WM9081_CLOCK_CONTROL_1:
  179. case WM9081_CLOCK_CONTROL_2:
  180. case WM9081_CLOCK_CONTROL_3:
  181. case WM9081_FLL_CONTROL_1:
  182. case WM9081_FLL_CONTROL_2:
  183. case WM9081_FLL_CONTROL_3:
  184. case WM9081_FLL_CONTROL_4:
  185. case WM9081_FLL_CONTROL_5:
  186. case WM9081_AUDIO_INTERFACE_1:
  187. case WM9081_AUDIO_INTERFACE_2:
  188. case WM9081_AUDIO_INTERFACE_3:
  189. case WM9081_AUDIO_INTERFACE_4:
  190. case WM9081_INTERRUPT_STATUS:
  191. case WM9081_INTERRUPT_STATUS_MASK:
  192. case WM9081_INTERRUPT_POLARITY:
  193. case WM9081_INTERRUPT_CONTROL:
  194. case WM9081_DAC_DIGITAL_1:
  195. case WM9081_DAC_DIGITAL_2:
  196. case WM9081_DRC_1:
  197. case WM9081_DRC_2:
  198. case WM9081_DRC_3:
  199. case WM9081_DRC_4:
  200. case WM9081_WRITE_SEQUENCER_1:
  201. case WM9081_WRITE_SEQUENCER_2:
  202. case WM9081_MW_SLAVE_1:
  203. case WM9081_EQ_1:
  204. case WM9081_EQ_2:
  205. case WM9081_EQ_3:
  206. case WM9081_EQ_4:
  207. case WM9081_EQ_5:
  208. case WM9081_EQ_6:
  209. case WM9081_EQ_7:
  210. case WM9081_EQ_8:
  211. case WM9081_EQ_9:
  212. case WM9081_EQ_10:
  213. case WM9081_EQ_11:
  214. case WM9081_EQ_12:
  215. case WM9081_EQ_13:
  216. case WM9081_EQ_14:
  217. case WM9081_EQ_15:
  218. case WM9081_EQ_16:
  219. case WM9081_EQ_17:
  220. case WM9081_EQ_18:
  221. case WM9081_EQ_19:
  222. case WM9081_EQ_20:
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. static int wm9081_reset(struct regmap *map)
  229. {
  230. return regmap_write(map, WM9081_SOFTWARE_RESET, 0x9081);
  231. }
  232. static const DECLARE_TLV_DB_SCALE(drc_in_tlv, -4500, 75, 0);
  233. static const DECLARE_TLV_DB_SCALE(drc_out_tlv, -2250, 75, 0);
  234. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  235. static const DECLARE_TLV_DB_RANGE(drc_max_tlv,
  236. 0, 0, TLV_DB_SCALE_ITEM(1200, 0, 0),
  237. 1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
  238. 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
  239. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0)
  240. );
  241. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  242. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -300, 50, 0);
  243. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  244. static const DECLARE_TLV_DB_SCALE(in_tlv, -600, 600, 0);
  245. static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
  246. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  247. static const char *drc_high_text[] = {
  248. "1",
  249. "1/2",
  250. "1/4",
  251. "1/8",
  252. "1/16",
  253. "0",
  254. };
  255. static SOC_ENUM_SINGLE_DECL(drc_high, WM9081_DRC_3, 3, drc_high_text);
  256. static const char *drc_low_text[] = {
  257. "1",
  258. "1/2",
  259. "1/4",
  260. "1/8",
  261. "0",
  262. };
  263. static SOC_ENUM_SINGLE_DECL(drc_low, WM9081_DRC_3, 0, drc_low_text);
  264. static const char *drc_atk_text[] = {
  265. "181us",
  266. "181us",
  267. "363us",
  268. "726us",
  269. "1.45ms",
  270. "2.9ms",
  271. "5.8ms",
  272. "11.6ms",
  273. "23.2ms",
  274. "46.4ms",
  275. "92.8ms",
  276. "185.6ms",
  277. };
  278. static SOC_ENUM_SINGLE_DECL(drc_atk, WM9081_DRC_2, 12, drc_atk_text);
  279. static const char *drc_dcy_text[] = {
  280. "186ms",
  281. "372ms",
  282. "743ms",
  283. "1.49s",
  284. "2.97s",
  285. "5.94s",
  286. "11.89s",
  287. "23.78s",
  288. "47.56s",
  289. };
  290. static SOC_ENUM_SINGLE_DECL(drc_dcy, WM9081_DRC_2, 8, drc_dcy_text);
  291. static const char *drc_qr_dcy_text[] = {
  292. "0.725ms",
  293. "1.45ms",
  294. "5.8ms",
  295. };
  296. static SOC_ENUM_SINGLE_DECL(drc_qr_dcy, WM9081_DRC_2, 4, drc_qr_dcy_text);
  297. static const char *dac_deemph_text[] = {
  298. "None",
  299. "32kHz",
  300. "44.1kHz",
  301. "48kHz",
  302. };
  303. static SOC_ENUM_SINGLE_DECL(dac_deemph, WM9081_DAC_DIGITAL_2, 1,
  304. dac_deemph_text);
  305. static const char *speaker_mode_text[] = {
  306. "Class D",
  307. "Class AB",
  308. };
  309. static SOC_ENUM_SINGLE_DECL(speaker_mode, WM9081_ANALOGUE_SPEAKER_2, 6,
  310. speaker_mode_text);
  311. static int speaker_mode_get(struct snd_kcontrol *kcontrol,
  312. struct snd_ctl_elem_value *ucontrol)
  313. {
  314. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  315. unsigned int reg;
  316. reg = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  317. if (reg & WM9081_SPK_MODE)
  318. ucontrol->value.enumerated.item[0] = 1;
  319. else
  320. ucontrol->value.enumerated.item[0] = 0;
  321. return 0;
  322. }
  323. /*
  324. * Stop any attempts to change speaker mode while the speaker is enabled.
  325. *
  326. * We also have some special anti-pop controls dependent on speaker
  327. * mode which must be changed along with the mode.
  328. */
  329. static int speaker_mode_put(struct snd_kcontrol *kcontrol,
  330. struct snd_ctl_elem_value *ucontrol)
  331. {
  332. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  333. unsigned int reg_pwr = snd_soc_read(codec, WM9081_POWER_MANAGEMENT);
  334. unsigned int reg2 = snd_soc_read(codec, WM9081_ANALOGUE_SPEAKER_2);
  335. /* Are we changing anything? */
  336. if (ucontrol->value.enumerated.item[0] ==
  337. ((reg2 & WM9081_SPK_MODE) != 0))
  338. return 0;
  339. /* Don't try to change modes while enabled */
  340. if (reg_pwr & WM9081_SPK_ENA)
  341. return -EINVAL;
  342. if (ucontrol->value.enumerated.item[0]) {
  343. /* Class AB */
  344. reg2 &= ~(WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL);
  345. reg2 |= WM9081_SPK_MODE;
  346. } else {
  347. /* Class D */
  348. reg2 |= WM9081_SPK_INV_MUTE | WM9081_OUT_SPK_CTRL;
  349. reg2 &= ~WM9081_SPK_MODE;
  350. }
  351. snd_soc_write(codec, WM9081_ANALOGUE_SPEAKER_2, reg2);
  352. return 0;
  353. }
  354. static const struct snd_kcontrol_new wm9081_snd_controls[] = {
  355. SOC_SINGLE_TLV("IN1 Volume", WM9081_ANALOGUE_MIXER, 1, 1, 1, in_tlv),
  356. SOC_SINGLE_TLV("IN2 Volume", WM9081_ANALOGUE_MIXER, 3, 1, 1, in_tlv),
  357. SOC_SINGLE_TLV("Playback Volume", WM9081_DAC_DIGITAL_1, 1, 96, 0, dac_tlv),
  358. SOC_SINGLE("LINEOUT Switch", WM9081_ANALOGUE_LINEOUT, 7, 1, 1),
  359. SOC_SINGLE("LINEOUT ZC Switch", WM9081_ANALOGUE_LINEOUT, 6, 1, 0),
  360. SOC_SINGLE_TLV("LINEOUT Volume", WM9081_ANALOGUE_LINEOUT, 0, 63, 0, out_tlv),
  361. SOC_SINGLE("DRC Switch", WM9081_DRC_1, 15, 1, 0),
  362. SOC_ENUM("DRC High Slope", drc_high),
  363. SOC_ENUM("DRC Low Slope", drc_low),
  364. SOC_SINGLE_TLV("DRC Input Volume", WM9081_DRC_4, 5, 60, 1, drc_in_tlv),
  365. SOC_SINGLE_TLV("DRC Output Volume", WM9081_DRC_4, 0, 30, 1, drc_out_tlv),
  366. SOC_SINGLE_TLV("DRC Minimum Volume", WM9081_DRC_2, 2, 3, 1, drc_min_tlv),
  367. SOC_SINGLE_TLV("DRC Maximum Volume", WM9081_DRC_2, 0, 3, 0, drc_max_tlv),
  368. SOC_ENUM("DRC Attack", drc_atk),
  369. SOC_ENUM("DRC Decay", drc_dcy),
  370. SOC_SINGLE("DRC Quick Release Switch", WM9081_DRC_1, 2, 1, 0),
  371. SOC_SINGLE_TLV("DRC Quick Release Volume", WM9081_DRC_2, 6, 3, 0, drc_qr_tlv),
  372. SOC_ENUM("DRC Quick Release Decay", drc_qr_dcy),
  373. SOC_SINGLE_TLV("DRC Startup Volume", WM9081_DRC_1, 6, 18, 0, drc_startup_tlv),
  374. SOC_SINGLE("EQ Switch", WM9081_EQ_1, 0, 1, 0),
  375. SOC_SINGLE("Speaker DC Volume", WM9081_ANALOGUE_SPEAKER_1, 3, 5, 0),
  376. SOC_SINGLE("Speaker AC Volume", WM9081_ANALOGUE_SPEAKER_1, 0, 5, 0),
  377. SOC_SINGLE("Speaker Switch", WM9081_ANALOGUE_SPEAKER_PGA, 7, 1, 1),
  378. SOC_SINGLE("Speaker ZC Switch", WM9081_ANALOGUE_SPEAKER_PGA, 6, 1, 0),
  379. SOC_SINGLE_TLV("Speaker Volume", WM9081_ANALOGUE_SPEAKER_PGA, 0, 63, 0,
  380. out_tlv),
  381. SOC_ENUM("DAC Deemphasis", dac_deemph),
  382. SOC_ENUM_EXT("Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
  383. };
  384. static const struct snd_kcontrol_new wm9081_eq_controls[] = {
  385. SOC_SINGLE_TLV("EQ1 Volume", WM9081_EQ_1, 11, 24, 0, eq_tlv),
  386. SOC_SINGLE_TLV("EQ2 Volume", WM9081_EQ_1, 6, 24, 0, eq_tlv),
  387. SOC_SINGLE_TLV("EQ3 Volume", WM9081_EQ_1, 1, 24, 0, eq_tlv),
  388. SOC_SINGLE_TLV("EQ4 Volume", WM9081_EQ_2, 11, 24, 0, eq_tlv),
  389. SOC_SINGLE_TLV("EQ5 Volume", WM9081_EQ_2, 6, 24, 0, eq_tlv),
  390. };
  391. static const struct snd_kcontrol_new mixer[] = {
  392. SOC_DAPM_SINGLE("IN1 Switch", WM9081_ANALOGUE_MIXER, 0, 1, 0),
  393. SOC_DAPM_SINGLE("IN2 Switch", WM9081_ANALOGUE_MIXER, 2, 1, 0),
  394. SOC_DAPM_SINGLE("Playback Switch", WM9081_ANALOGUE_MIXER, 4, 1, 0),
  395. };
  396. struct _fll_div {
  397. u16 fll_fratio;
  398. u16 fll_outdiv;
  399. u16 fll_clk_ref_div;
  400. u16 n;
  401. u16 k;
  402. };
  403. /* The size in bits of the FLL divide multiplied by 10
  404. * to allow rounding later */
  405. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  406. static struct {
  407. unsigned int min;
  408. unsigned int max;
  409. u16 fll_fratio;
  410. int ratio;
  411. } fll_fratios[] = {
  412. { 0, 64000, 4, 16 },
  413. { 64000, 128000, 3, 8 },
  414. { 128000, 256000, 2, 4 },
  415. { 256000, 1000000, 1, 2 },
  416. { 1000000, 13500000, 0, 1 },
  417. };
  418. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  419. unsigned int Fout)
  420. {
  421. u64 Kpart;
  422. unsigned int K, Ndiv, Nmod, target;
  423. unsigned int div;
  424. int i;
  425. /* Fref must be <=13.5MHz */
  426. div = 1;
  427. while ((Fref / div) > 13500000) {
  428. div *= 2;
  429. if (div > 8) {
  430. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  431. Fref);
  432. return -EINVAL;
  433. }
  434. }
  435. fll_div->fll_clk_ref_div = div / 2;
  436. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  437. /* Apply the division for our remaining calculations */
  438. Fref /= div;
  439. /* Fvco should be 90-100MHz; don't check the upper bound */
  440. div = 0;
  441. target = Fout * 2;
  442. while (target < 90000000) {
  443. div++;
  444. target *= 2;
  445. if (div > 7) {
  446. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  447. Fout);
  448. return -EINVAL;
  449. }
  450. }
  451. fll_div->fll_outdiv = div;
  452. pr_debug("Fvco=%dHz\n", target);
  453. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  454. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  455. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  456. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  457. target /= fll_fratios[i].ratio;
  458. break;
  459. }
  460. }
  461. if (i == ARRAY_SIZE(fll_fratios)) {
  462. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  463. return -EINVAL;
  464. }
  465. /* Now, calculate N.K */
  466. Ndiv = target / Fref;
  467. fll_div->n = Ndiv;
  468. Nmod = target % Fref;
  469. pr_debug("Nmod=%d\n", Nmod);
  470. /* Calculate fractional part - scale up so we can round. */
  471. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  472. do_div(Kpart, Fref);
  473. K = Kpart & 0xFFFFFFFF;
  474. if ((K % 10) >= 5)
  475. K += 5;
  476. /* Move down to proper range now rounding is done */
  477. fll_div->k = K / 10;
  478. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  479. fll_div->n, fll_div->k,
  480. fll_div->fll_fratio, fll_div->fll_outdiv,
  481. fll_div->fll_clk_ref_div);
  482. return 0;
  483. }
  484. static int wm9081_set_fll(struct snd_soc_codec *codec, int fll_id,
  485. unsigned int Fref, unsigned int Fout)
  486. {
  487. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  488. u16 reg1, reg4, reg5;
  489. struct _fll_div fll_div;
  490. int ret;
  491. int clk_sys_reg;
  492. /* Any change? */
  493. if (Fref == wm9081->fll_fref && Fout == wm9081->fll_fout)
  494. return 0;
  495. /* Disable the FLL */
  496. if (Fout == 0) {
  497. dev_dbg(codec->dev, "FLL disabled\n");
  498. wm9081->fll_fref = 0;
  499. wm9081->fll_fout = 0;
  500. return 0;
  501. }
  502. ret = fll_factors(&fll_div, Fref, Fout);
  503. if (ret != 0)
  504. return ret;
  505. reg5 = snd_soc_read(codec, WM9081_FLL_CONTROL_5);
  506. reg5 &= ~WM9081_FLL_CLK_SRC_MASK;
  507. switch (fll_id) {
  508. case WM9081_SYSCLK_FLL_MCLK:
  509. reg5 |= 0x1;
  510. break;
  511. default:
  512. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  513. return -EINVAL;
  514. }
  515. /* Disable CLK_SYS while we reconfigure */
  516. clk_sys_reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  517. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  518. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3,
  519. clk_sys_reg & ~WM9081_CLK_SYS_ENA);
  520. /* Any FLL configuration change requires that the FLL be
  521. * disabled first. */
  522. reg1 = snd_soc_read(codec, WM9081_FLL_CONTROL_1);
  523. reg1 &= ~WM9081_FLL_ENA;
  524. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  525. /* Apply the configuration */
  526. if (fll_div.k)
  527. reg1 |= WM9081_FLL_FRAC_MASK;
  528. else
  529. reg1 &= ~WM9081_FLL_FRAC_MASK;
  530. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1);
  531. snd_soc_write(codec, WM9081_FLL_CONTROL_2,
  532. (fll_div.fll_outdiv << WM9081_FLL_OUTDIV_SHIFT) |
  533. (fll_div.fll_fratio << WM9081_FLL_FRATIO_SHIFT));
  534. snd_soc_write(codec, WM9081_FLL_CONTROL_3, fll_div.k);
  535. reg4 = snd_soc_read(codec, WM9081_FLL_CONTROL_4);
  536. reg4 &= ~WM9081_FLL_N_MASK;
  537. reg4 |= fll_div.n << WM9081_FLL_N_SHIFT;
  538. snd_soc_write(codec, WM9081_FLL_CONTROL_4, reg4);
  539. reg5 &= ~WM9081_FLL_CLK_REF_DIV_MASK;
  540. reg5 |= fll_div.fll_clk_ref_div << WM9081_FLL_CLK_REF_DIV_SHIFT;
  541. snd_soc_write(codec, WM9081_FLL_CONTROL_5, reg5);
  542. /* Set gain to the recommended value */
  543. snd_soc_update_bits(codec, WM9081_FLL_CONTROL_4,
  544. WM9081_FLL_GAIN_MASK, 0);
  545. /* Enable the FLL */
  546. snd_soc_write(codec, WM9081_FLL_CONTROL_1, reg1 | WM9081_FLL_ENA);
  547. /* Then bring CLK_SYS up again if it was disabled */
  548. if (clk_sys_reg & WM9081_CLK_SYS_ENA)
  549. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, clk_sys_reg);
  550. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  551. wm9081->fll_fref = Fref;
  552. wm9081->fll_fout = Fout;
  553. return 0;
  554. }
  555. static int configure_clock(struct snd_soc_codec *codec)
  556. {
  557. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  558. int new_sysclk, i, target;
  559. unsigned int reg;
  560. int ret = 0;
  561. int mclkdiv = 0;
  562. int fll = 0;
  563. switch (wm9081->sysclk_source) {
  564. case WM9081_SYSCLK_MCLK:
  565. if (wm9081->mclk_rate > 12225000) {
  566. mclkdiv = 1;
  567. wm9081->sysclk_rate = wm9081->mclk_rate / 2;
  568. } else {
  569. wm9081->sysclk_rate = wm9081->mclk_rate;
  570. }
  571. wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK, 0, 0);
  572. break;
  573. case WM9081_SYSCLK_FLL_MCLK:
  574. /* If we have a sample rate calculate a CLK_SYS that
  575. * gives us a suitable DAC configuration, plus BCLK.
  576. * Ideally we would check to see if we can clock
  577. * directly from MCLK and only use the FLL if this is
  578. * not the case, though care must be taken with free
  579. * running mode.
  580. */
  581. if (wm9081->master && wm9081->bclk) {
  582. /* Make sure we can generate CLK_SYS and BCLK
  583. * and that we've got 3MHz for optimal
  584. * performance. */
  585. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  586. target = wm9081->fs * clk_sys_rates[i].ratio;
  587. new_sysclk = target;
  588. if (target >= wm9081->bclk &&
  589. target > 3000000)
  590. break;
  591. }
  592. if (i == ARRAY_SIZE(clk_sys_rates))
  593. return -EINVAL;
  594. } else if (wm9081->fs) {
  595. for (i = 0; i < ARRAY_SIZE(clk_sys_rates); i++) {
  596. new_sysclk = clk_sys_rates[i].ratio
  597. * wm9081->fs;
  598. if (new_sysclk > 3000000)
  599. break;
  600. }
  601. if (i == ARRAY_SIZE(clk_sys_rates))
  602. return -EINVAL;
  603. } else {
  604. new_sysclk = 12288000;
  605. }
  606. ret = wm9081_set_fll(codec, WM9081_SYSCLK_FLL_MCLK,
  607. wm9081->mclk_rate, new_sysclk);
  608. if (ret == 0) {
  609. wm9081->sysclk_rate = new_sysclk;
  610. /* Switch SYSCLK over to FLL */
  611. fll = 1;
  612. } else {
  613. wm9081->sysclk_rate = wm9081->mclk_rate;
  614. }
  615. break;
  616. default:
  617. return -EINVAL;
  618. }
  619. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_1);
  620. if (mclkdiv)
  621. reg |= WM9081_MCLKDIV2;
  622. else
  623. reg &= ~WM9081_MCLKDIV2;
  624. snd_soc_write(codec, WM9081_CLOCK_CONTROL_1, reg);
  625. reg = snd_soc_read(codec, WM9081_CLOCK_CONTROL_3);
  626. if (fll)
  627. reg |= WM9081_CLK_SRC_SEL;
  628. else
  629. reg &= ~WM9081_CLK_SRC_SEL;
  630. snd_soc_write(codec, WM9081_CLOCK_CONTROL_3, reg);
  631. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm9081->sysclk_rate);
  632. return ret;
  633. }
  634. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  635. struct snd_kcontrol *kcontrol, int event)
  636. {
  637. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  638. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  639. /* This should be done on init() for bypass paths */
  640. switch (wm9081->sysclk_source) {
  641. case WM9081_SYSCLK_MCLK:
  642. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm9081->mclk_rate);
  643. break;
  644. case WM9081_SYSCLK_FLL_MCLK:
  645. dev_dbg(codec->dev, "Using %dHz MCLK with FLL\n",
  646. wm9081->mclk_rate);
  647. break;
  648. default:
  649. dev_err(codec->dev, "System clock not configured\n");
  650. return -EINVAL;
  651. }
  652. switch (event) {
  653. case SND_SOC_DAPM_PRE_PMU:
  654. configure_clock(codec);
  655. break;
  656. case SND_SOC_DAPM_POST_PMD:
  657. /* Disable the FLL if it's running */
  658. wm9081_set_fll(codec, 0, 0, 0);
  659. break;
  660. }
  661. return 0;
  662. }
  663. static const struct snd_soc_dapm_widget wm9081_dapm_widgets[] = {
  664. SND_SOC_DAPM_INPUT("IN1"),
  665. SND_SOC_DAPM_INPUT("IN2"),
  666. SND_SOC_DAPM_DAC("DAC", NULL, WM9081_POWER_MANAGEMENT, 0, 0),
  667. SND_SOC_DAPM_MIXER_NAMED_CTL("Mixer", SND_SOC_NOPM, 0, 0,
  668. mixer, ARRAY_SIZE(mixer)),
  669. SND_SOC_DAPM_PGA("LINEOUT PGA", WM9081_POWER_MANAGEMENT, 4, 0, NULL, 0),
  670. SND_SOC_DAPM_PGA("Speaker PGA", WM9081_POWER_MANAGEMENT, 2, 0, NULL, 0),
  671. SND_SOC_DAPM_OUT_DRV("Speaker", WM9081_POWER_MANAGEMENT, 1, 0, NULL, 0),
  672. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  673. SND_SOC_DAPM_OUTPUT("SPKN"),
  674. SND_SOC_DAPM_OUTPUT("SPKP"),
  675. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM9081_CLOCK_CONTROL_3, 0, 0, clk_sys_event,
  676. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  677. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM9081_CLOCK_CONTROL_3, 1, 0, NULL, 0),
  678. SND_SOC_DAPM_SUPPLY("TOCLK", WM9081_CLOCK_CONTROL_3, 2, 0, NULL, 0),
  679. SND_SOC_DAPM_SUPPLY("TSENSE", WM9081_POWER_MANAGEMENT, 7, 0, NULL, 0),
  680. };
  681. static const struct snd_soc_dapm_route wm9081_audio_paths[] = {
  682. { "DAC", NULL, "CLK_SYS" },
  683. { "DAC", NULL, "CLK_DSP" },
  684. { "DAC", NULL, "AIF" },
  685. { "Mixer", "IN1 Switch", "IN1" },
  686. { "Mixer", "IN2 Switch", "IN2" },
  687. { "Mixer", "Playback Switch", "DAC" },
  688. { "LINEOUT PGA", NULL, "Mixer" },
  689. { "LINEOUT PGA", NULL, "TOCLK" },
  690. { "LINEOUT PGA", NULL, "CLK_SYS" },
  691. { "LINEOUT", NULL, "LINEOUT PGA" },
  692. { "Speaker PGA", NULL, "Mixer" },
  693. { "Speaker PGA", NULL, "TOCLK" },
  694. { "Speaker PGA", NULL, "CLK_SYS" },
  695. { "Speaker", NULL, "Speaker PGA" },
  696. { "Speaker", NULL, "TSENSE" },
  697. { "SPKN", NULL, "Speaker" },
  698. { "SPKP", NULL, "Speaker" },
  699. };
  700. static int wm9081_set_bias_level(struct snd_soc_codec *codec,
  701. enum snd_soc_bias_level level)
  702. {
  703. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  704. switch (level) {
  705. case SND_SOC_BIAS_ON:
  706. break;
  707. case SND_SOC_BIAS_PREPARE:
  708. /* VMID=2*40k */
  709. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  710. WM9081_VMID_SEL_MASK, 0x2);
  711. /* Normal bias current */
  712. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  713. WM9081_STBY_BIAS_ENA, 0);
  714. break;
  715. case SND_SOC_BIAS_STANDBY:
  716. /* Initial cold start */
  717. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  718. regcache_cache_only(wm9081->regmap, false);
  719. regcache_sync(wm9081->regmap);
  720. /* Disable LINEOUT discharge */
  721. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  722. WM9081_LINEOUT_DISCH, 0);
  723. /* Select startup bias source */
  724. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  725. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  726. WM9081_BIAS_SRC | WM9081_BIAS_ENA);
  727. /* VMID 2*4k; Soft VMID ramp enable */
  728. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  729. WM9081_VMID_RAMP |
  730. WM9081_VMID_SEL_MASK,
  731. WM9081_VMID_RAMP | 0x6);
  732. mdelay(100);
  733. /* Normal bias enable & soft start off */
  734. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  735. WM9081_VMID_RAMP, 0);
  736. /* Standard bias source */
  737. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  738. WM9081_BIAS_SRC, 0);
  739. }
  740. /* VMID 2*240k */
  741. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  742. WM9081_VMID_SEL_MASK, 0x04);
  743. /* Standby bias current on */
  744. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  745. WM9081_STBY_BIAS_ENA,
  746. WM9081_STBY_BIAS_ENA);
  747. break;
  748. case SND_SOC_BIAS_OFF:
  749. /* Startup bias source and disable bias */
  750. snd_soc_update_bits(codec, WM9081_BIAS_CONTROL_1,
  751. WM9081_BIAS_SRC | WM9081_BIAS_ENA,
  752. WM9081_BIAS_SRC);
  753. /* Disable VMID with soft ramping */
  754. snd_soc_update_bits(codec, WM9081_VMID_CONTROL,
  755. WM9081_VMID_RAMP | WM9081_VMID_SEL_MASK,
  756. WM9081_VMID_RAMP);
  757. /* Actively discharge LINEOUT */
  758. snd_soc_update_bits(codec, WM9081_ANTI_POP_CONTROL,
  759. WM9081_LINEOUT_DISCH,
  760. WM9081_LINEOUT_DISCH);
  761. regcache_cache_only(wm9081->regmap, true);
  762. break;
  763. }
  764. return 0;
  765. }
  766. static int wm9081_set_dai_fmt(struct snd_soc_dai *dai,
  767. unsigned int fmt)
  768. {
  769. struct snd_soc_codec *codec = dai->codec;
  770. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  771. unsigned int aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  772. aif2 &= ~(WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV |
  773. WM9081_BCLK_DIR | WM9081_LRCLK_DIR | WM9081_AIF_FMT_MASK);
  774. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  775. case SND_SOC_DAIFMT_CBS_CFS:
  776. wm9081->master = 0;
  777. break;
  778. case SND_SOC_DAIFMT_CBS_CFM:
  779. aif2 |= WM9081_LRCLK_DIR;
  780. wm9081->master = 1;
  781. break;
  782. case SND_SOC_DAIFMT_CBM_CFS:
  783. aif2 |= WM9081_BCLK_DIR;
  784. wm9081->master = 1;
  785. break;
  786. case SND_SOC_DAIFMT_CBM_CFM:
  787. aif2 |= WM9081_LRCLK_DIR | WM9081_BCLK_DIR;
  788. wm9081->master = 1;
  789. break;
  790. default:
  791. return -EINVAL;
  792. }
  793. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  794. case SND_SOC_DAIFMT_DSP_B:
  795. aif2 |= WM9081_AIF_LRCLK_INV;
  796. case SND_SOC_DAIFMT_DSP_A:
  797. aif2 |= 0x3;
  798. break;
  799. case SND_SOC_DAIFMT_I2S:
  800. aif2 |= 0x2;
  801. break;
  802. case SND_SOC_DAIFMT_RIGHT_J:
  803. break;
  804. case SND_SOC_DAIFMT_LEFT_J:
  805. aif2 |= 0x1;
  806. break;
  807. default:
  808. return -EINVAL;
  809. }
  810. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  811. case SND_SOC_DAIFMT_DSP_A:
  812. case SND_SOC_DAIFMT_DSP_B:
  813. /* frame inversion not valid for DSP modes */
  814. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  815. case SND_SOC_DAIFMT_NB_NF:
  816. break;
  817. case SND_SOC_DAIFMT_IB_NF:
  818. aif2 |= WM9081_AIF_BCLK_INV;
  819. break;
  820. default:
  821. return -EINVAL;
  822. }
  823. break;
  824. case SND_SOC_DAIFMT_I2S:
  825. case SND_SOC_DAIFMT_RIGHT_J:
  826. case SND_SOC_DAIFMT_LEFT_J:
  827. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  828. case SND_SOC_DAIFMT_NB_NF:
  829. break;
  830. case SND_SOC_DAIFMT_IB_IF:
  831. aif2 |= WM9081_AIF_BCLK_INV | WM9081_AIF_LRCLK_INV;
  832. break;
  833. case SND_SOC_DAIFMT_IB_NF:
  834. aif2 |= WM9081_AIF_BCLK_INV;
  835. break;
  836. case SND_SOC_DAIFMT_NB_IF:
  837. aif2 |= WM9081_AIF_LRCLK_INV;
  838. break;
  839. default:
  840. return -EINVAL;
  841. }
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  847. return 0;
  848. }
  849. static int wm9081_hw_params(struct snd_pcm_substream *substream,
  850. struct snd_pcm_hw_params *params,
  851. struct snd_soc_dai *dai)
  852. {
  853. struct snd_soc_codec *codec = dai->codec;
  854. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  855. int ret, i, best, best_val, cur_val;
  856. unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
  857. clk_ctrl2 = snd_soc_read(codec, WM9081_CLOCK_CONTROL_2);
  858. clk_ctrl2 &= ~(WM9081_CLK_SYS_RATE_MASK | WM9081_SAMPLE_RATE_MASK);
  859. aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  860. aif2 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_2);
  861. aif2 &= ~WM9081_AIF_WL_MASK;
  862. aif3 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_3);
  863. aif3 &= ~WM9081_BCLK_DIV_MASK;
  864. aif4 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_4);
  865. aif4 &= ~WM9081_LRCLK_RATE_MASK;
  866. wm9081->fs = params_rate(params);
  867. if (wm9081->tdm_width) {
  868. /* If TDM is set up then that fixes our BCLK. */
  869. int slots = ((aif1 & WM9081_AIFDAC_TDM_MODE_MASK) >>
  870. WM9081_AIFDAC_TDM_MODE_SHIFT) + 1;
  871. wm9081->bclk = wm9081->fs * wm9081->tdm_width * slots;
  872. } else {
  873. /* Otherwise work out a BCLK from the sample size */
  874. wm9081->bclk = 2 * wm9081->fs;
  875. switch (params_width(params)) {
  876. case 16:
  877. wm9081->bclk *= 16;
  878. break;
  879. case 20:
  880. wm9081->bclk *= 20;
  881. aif2 |= 0x4;
  882. break;
  883. case 24:
  884. wm9081->bclk *= 24;
  885. aif2 |= 0x8;
  886. break;
  887. case 32:
  888. wm9081->bclk *= 32;
  889. aif2 |= 0xc;
  890. break;
  891. default:
  892. return -EINVAL;
  893. }
  894. }
  895. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm9081->bclk);
  896. ret = configure_clock(codec);
  897. if (ret != 0)
  898. return ret;
  899. /* Select nearest CLK_SYS_RATE */
  900. best = 0;
  901. best_val = abs((wm9081->sysclk_rate / clk_sys_rates[0].ratio)
  902. - wm9081->fs);
  903. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  904. cur_val = abs((wm9081->sysclk_rate /
  905. clk_sys_rates[i].ratio) - wm9081->fs);
  906. if (cur_val < best_val) {
  907. best = i;
  908. best_val = cur_val;
  909. }
  910. }
  911. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  912. clk_sys_rates[best].ratio);
  913. clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
  914. << WM9081_CLK_SYS_RATE_SHIFT);
  915. /* SAMPLE_RATE */
  916. best = 0;
  917. best_val = abs(wm9081->fs - sample_rates[0].rate);
  918. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  919. /* Closest match */
  920. cur_val = abs(wm9081->fs - sample_rates[i].rate);
  921. if (cur_val < best_val) {
  922. best = i;
  923. best_val = cur_val;
  924. }
  925. }
  926. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  927. sample_rates[best].rate);
  928. clk_ctrl2 |= (sample_rates[best].sample_rate
  929. << WM9081_SAMPLE_RATE_SHIFT);
  930. /* BCLK_DIV */
  931. best = 0;
  932. best_val = INT_MAX;
  933. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  934. cur_val = ((wm9081->sysclk_rate * 10) / bclk_divs[i].div)
  935. - wm9081->bclk;
  936. if (cur_val < 0) /* Table is sorted */
  937. break;
  938. if (cur_val < best_val) {
  939. best = i;
  940. best_val = cur_val;
  941. }
  942. }
  943. wm9081->bclk = (wm9081->sysclk_rate * 10) / bclk_divs[best].div;
  944. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  945. bclk_divs[best].div, wm9081->bclk);
  946. aif3 |= bclk_divs[best].bclk_div;
  947. /* LRCLK is a simple fraction of BCLK */
  948. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm9081->bclk / wm9081->fs);
  949. aif4 |= wm9081->bclk / wm9081->fs;
  950. /* Apply a ReTune Mobile configuration if it's in use */
  951. if (wm9081->pdata.num_retune_configs) {
  952. struct wm9081_pdata *pdata = &wm9081->pdata;
  953. struct wm9081_retune_mobile_setting *s;
  954. int eq1;
  955. best = 0;
  956. best_val = abs(pdata->retune_configs[0].rate - wm9081->fs);
  957. for (i = 0; i < pdata->num_retune_configs; i++) {
  958. cur_val = abs(pdata->retune_configs[i].rate -
  959. wm9081->fs);
  960. if (cur_val < best_val) {
  961. best_val = cur_val;
  962. best = i;
  963. }
  964. }
  965. s = &pdata->retune_configs[best];
  966. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  967. s->name, s->rate);
  968. /* If the EQ is enabled then disable it while we write out */
  969. eq1 = snd_soc_read(codec, WM9081_EQ_1) & WM9081_EQ_ENA;
  970. if (eq1 & WM9081_EQ_ENA)
  971. snd_soc_write(codec, WM9081_EQ_1, 0);
  972. /* Write out the other values */
  973. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  974. snd_soc_write(codec, WM9081_EQ_1 + i, s->config[i]);
  975. eq1 |= (s->config[0] & ~WM9081_EQ_ENA);
  976. snd_soc_write(codec, WM9081_EQ_1, eq1);
  977. }
  978. snd_soc_write(codec, WM9081_CLOCK_CONTROL_2, clk_ctrl2);
  979. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_2, aif2);
  980. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_3, aif3);
  981. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_4, aif4);
  982. return 0;
  983. }
  984. static int wm9081_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  985. {
  986. struct snd_soc_codec *codec = codec_dai->codec;
  987. unsigned int reg;
  988. reg = snd_soc_read(codec, WM9081_DAC_DIGITAL_2);
  989. if (mute)
  990. reg |= WM9081_DAC_MUTE;
  991. else
  992. reg &= ~WM9081_DAC_MUTE;
  993. snd_soc_write(codec, WM9081_DAC_DIGITAL_2, reg);
  994. return 0;
  995. }
  996. static int wm9081_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  997. int source, unsigned int freq, int dir)
  998. {
  999. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1000. switch (clk_id) {
  1001. case WM9081_SYSCLK_MCLK:
  1002. case WM9081_SYSCLK_FLL_MCLK:
  1003. wm9081->sysclk_source = clk_id;
  1004. wm9081->mclk_rate = freq;
  1005. break;
  1006. default:
  1007. return -EINVAL;
  1008. }
  1009. return 0;
  1010. }
  1011. static int wm9081_set_tdm_slot(struct snd_soc_dai *dai,
  1012. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  1013. {
  1014. struct snd_soc_codec *codec = dai->codec;
  1015. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1016. unsigned int aif1 = snd_soc_read(codec, WM9081_AUDIO_INTERFACE_1);
  1017. aif1 &= ~(WM9081_AIFDAC_TDM_SLOT_MASK | WM9081_AIFDAC_TDM_MODE_MASK);
  1018. if (slots < 0 || slots > 4)
  1019. return -EINVAL;
  1020. wm9081->tdm_width = slot_width;
  1021. if (slots == 0)
  1022. slots = 1;
  1023. aif1 |= (slots - 1) << WM9081_AIFDAC_TDM_MODE_SHIFT;
  1024. switch (rx_mask) {
  1025. case 1:
  1026. break;
  1027. case 2:
  1028. aif1 |= 0x10;
  1029. break;
  1030. case 4:
  1031. aif1 |= 0x20;
  1032. break;
  1033. case 8:
  1034. aif1 |= 0x30;
  1035. break;
  1036. default:
  1037. return -EINVAL;
  1038. }
  1039. snd_soc_write(codec, WM9081_AUDIO_INTERFACE_1, aif1);
  1040. return 0;
  1041. }
  1042. #define WM9081_RATES SNDRV_PCM_RATE_8000_96000
  1043. #define WM9081_FORMATS \
  1044. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  1045. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1046. static const struct snd_soc_dai_ops wm9081_dai_ops = {
  1047. .hw_params = wm9081_hw_params,
  1048. .set_fmt = wm9081_set_dai_fmt,
  1049. .digital_mute = wm9081_digital_mute,
  1050. .set_tdm_slot = wm9081_set_tdm_slot,
  1051. };
  1052. /* We report two channels because the CODEC processes a stereo signal, even
  1053. * though it is only capable of handling a mono output.
  1054. */
  1055. static struct snd_soc_dai_driver wm9081_dai = {
  1056. .name = "wm9081-hifi",
  1057. .playback = {
  1058. .stream_name = "AIF",
  1059. .channels_min = 1,
  1060. .channels_max = 2,
  1061. .rates = WM9081_RATES,
  1062. .formats = WM9081_FORMATS,
  1063. },
  1064. .ops = &wm9081_dai_ops,
  1065. };
  1066. static int wm9081_probe(struct snd_soc_codec *codec)
  1067. {
  1068. struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
  1069. /* Enable zero cross by default */
  1070. snd_soc_update_bits(codec, WM9081_ANALOGUE_LINEOUT,
  1071. WM9081_LINEOUTZC, WM9081_LINEOUTZC);
  1072. snd_soc_update_bits(codec, WM9081_ANALOGUE_SPEAKER_PGA,
  1073. WM9081_SPKPGAZC, WM9081_SPKPGAZC);
  1074. if (!wm9081->pdata.num_retune_configs) {
  1075. dev_dbg(codec->dev,
  1076. "No ReTune Mobile data, using normal EQ\n");
  1077. snd_soc_add_codec_controls(codec, wm9081_eq_controls,
  1078. ARRAY_SIZE(wm9081_eq_controls));
  1079. }
  1080. return 0;
  1081. }
  1082. static const struct snd_soc_codec_driver soc_codec_dev_wm9081 = {
  1083. .probe = wm9081_probe,
  1084. .set_sysclk = wm9081_set_sysclk,
  1085. .set_bias_level = wm9081_set_bias_level,
  1086. .idle_bias_off = true,
  1087. .component_driver = {
  1088. .controls = wm9081_snd_controls,
  1089. .num_controls = ARRAY_SIZE(wm9081_snd_controls),
  1090. .dapm_widgets = wm9081_dapm_widgets,
  1091. .num_dapm_widgets = ARRAY_SIZE(wm9081_dapm_widgets),
  1092. .dapm_routes = wm9081_audio_paths,
  1093. .num_dapm_routes = ARRAY_SIZE(wm9081_audio_paths),
  1094. },
  1095. };
  1096. static const struct regmap_config wm9081_regmap = {
  1097. .reg_bits = 8,
  1098. .val_bits = 16,
  1099. .max_register = WM9081_MAX_REGISTER,
  1100. .reg_defaults = wm9081_reg,
  1101. .num_reg_defaults = ARRAY_SIZE(wm9081_reg),
  1102. .volatile_reg = wm9081_volatile_register,
  1103. .readable_reg = wm9081_readable_register,
  1104. .cache_type = REGCACHE_RBTREE,
  1105. };
  1106. #if IS_ENABLED(CONFIG_I2C)
  1107. static int wm9081_i2c_probe(struct i2c_client *i2c,
  1108. const struct i2c_device_id *id)
  1109. {
  1110. struct wm9081_priv *wm9081;
  1111. unsigned int reg;
  1112. int ret;
  1113. wm9081 = devm_kzalloc(&i2c->dev, sizeof(struct wm9081_priv),
  1114. GFP_KERNEL);
  1115. if (wm9081 == NULL)
  1116. return -ENOMEM;
  1117. i2c_set_clientdata(i2c, wm9081);
  1118. wm9081->regmap = devm_regmap_init_i2c(i2c, &wm9081_regmap);
  1119. if (IS_ERR(wm9081->regmap)) {
  1120. ret = PTR_ERR(wm9081->regmap);
  1121. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1122. return ret;
  1123. }
  1124. ret = regmap_read(wm9081->regmap, WM9081_SOFTWARE_RESET, &reg);
  1125. if (ret != 0) {
  1126. dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
  1127. return ret;
  1128. }
  1129. if (reg != 0x9081) {
  1130. dev_err(&i2c->dev, "Device is not a WM9081: ID=0x%x\n", reg);
  1131. return -EINVAL;
  1132. }
  1133. ret = wm9081_reset(wm9081->regmap);
  1134. if (ret < 0) {
  1135. dev_err(&i2c->dev, "Failed to issue reset\n");
  1136. return ret;
  1137. }
  1138. if (dev_get_platdata(&i2c->dev))
  1139. memcpy(&wm9081->pdata, dev_get_platdata(&i2c->dev),
  1140. sizeof(wm9081->pdata));
  1141. reg = 0;
  1142. if (wm9081->pdata.irq_high)
  1143. reg |= WM9081_IRQ_POL;
  1144. if (!wm9081->pdata.irq_cmos)
  1145. reg |= WM9081_IRQ_OP_CTRL;
  1146. regmap_update_bits(wm9081->regmap, WM9081_INTERRUPT_CONTROL,
  1147. WM9081_IRQ_POL | WM9081_IRQ_OP_CTRL, reg);
  1148. regcache_cache_only(wm9081->regmap, true);
  1149. ret = snd_soc_register_codec(&i2c->dev,
  1150. &soc_codec_dev_wm9081, &wm9081_dai, 1);
  1151. if (ret < 0)
  1152. return ret;
  1153. return 0;
  1154. }
  1155. static int wm9081_i2c_remove(struct i2c_client *client)
  1156. {
  1157. snd_soc_unregister_codec(&client->dev);
  1158. return 0;
  1159. }
  1160. static const struct i2c_device_id wm9081_i2c_id[] = {
  1161. { "wm9081", 0 },
  1162. { }
  1163. };
  1164. MODULE_DEVICE_TABLE(i2c, wm9081_i2c_id);
  1165. static struct i2c_driver wm9081_i2c_driver = {
  1166. .driver = {
  1167. .name = "wm9081",
  1168. },
  1169. .probe = wm9081_i2c_probe,
  1170. .remove = wm9081_i2c_remove,
  1171. .id_table = wm9081_i2c_id,
  1172. };
  1173. #endif
  1174. module_i2c_driver(wm9081_i2c_driver);
  1175. MODULE_DESCRIPTION("ASoC WM9081 driver");
  1176. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1177. MODULE_LICENSE("GPL");