wm9081.c 37 KB

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