wm9081.c 37 KB

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