wm9081.c 36 KB

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