wm9081.c 38 KB

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