wm8993.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * wm8993.c -- WM8993 ALSA SoC audio driver
  3. *
  4. * Copyright 2009, 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  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. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/tlv.h>
  26. #include <sound/soc.h>
  27. #include <sound/initval.h>
  28. #include <sound/wm8993.h>
  29. #include "wm8993.h"
  30. #include "wm_hubs.h"
  31. #define WM8993_NUM_SUPPLIES 6
  32. static const char *wm8993_supply_names[WM8993_NUM_SUPPLIES] = {
  33. "DCVDD",
  34. "DBVDD",
  35. "AVDD1",
  36. "AVDD2",
  37. "CPVDD",
  38. "SPKVDD",
  39. };
  40. static struct reg_default wm8993_reg_defaults[] = {
  41. { 1, 0x0000 }, /* R1 - Power Management (1) */
  42. { 2, 0x6000 }, /* R2 - Power Management (2) */
  43. { 3, 0x0000 }, /* R3 - Power Management (3) */
  44. { 4, 0x4050 }, /* R4 - Audio Interface (1) */
  45. { 5, 0x4000 }, /* R5 - Audio Interface (2) */
  46. { 6, 0x01C8 }, /* R6 - Clocking 1 */
  47. { 7, 0x0000 }, /* R7 - Clocking 2 */
  48. { 8, 0x0000 }, /* R8 - Audio Interface (3) */
  49. { 9, 0x0040 }, /* R9 - Audio Interface (4) */
  50. { 10, 0x0004 }, /* R10 - DAC CTRL */
  51. { 11, 0x00C0 }, /* R11 - Left DAC Digital Volume */
  52. { 12, 0x00C0 }, /* R12 - Right DAC Digital Volume */
  53. { 13, 0x0000 }, /* R13 - Digital Side Tone */
  54. { 14, 0x0300 }, /* R14 - ADC CTRL */
  55. { 15, 0x00C0 }, /* R15 - Left ADC Digital Volume */
  56. { 16, 0x00C0 }, /* R16 - Right ADC Digital Volume */
  57. { 18, 0x0000 }, /* R18 - GPIO CTRL 1 */
  58. { 19, 0x0010 }, /* R19 - GPIO1 */
  59. { 20, 0x0000 }, /* R20 - IRQ_DEBOUNCE */
  60. { 21, 0x8000 }, /* R22 - GPIOCTRL 2 */
  61. { 22, 0x0800 }, /* R23 - GPIO_POL */
  62. { 24, 0x008B }, /* R24 - Left Line Input 1&2 Volume */
  63. { 25, 0x008B }, /* R25 - Left Line Input 3&4 Volume */
  64. { 26, 0x008B }, /* R26 - Right Line Input 1&2 Volume */
  65. { 27, 0x008B }, /* R27 - Right Line Input 3&4 Volume */
  66. { 28, 0x006D }, /* R28 - Left Output Volume */
  67. { 29, 0x006D }, /* R29 - Right Output Volume */
  68. { 30, 0x0066 }, /* R30 - Line Outputs Volume */
  69. { 31, 0x0020 }, /* R31 - HPOUT2 Volume */
  70. { 32, 0x0079 }, /* R32 - Left OPGA Volume */
  71. { 33, 0x0079 }, /* R33 - Right OPGA Volume */
  72. { 34, 0x0003 }, /* R34 - SPKMIXL Attenuation */
  73. { 35, 0x0003 }, /* R35 - SPKMIXR Attenuation */
  74. { 36, 0x0011 }, /* R36 - SPKOUT Mixers */
  75. { 37, 0x0100 }, /* R37 - SPKOUT Boost */
  76. { 38, 0x0079 }, /* R38 - Speaker Volume Left */
  77. { 39, 0x0079 }, /* R39 - Speaker Volume Right */
  78. { 40, 0x0000 }, /* R40 - Input Mixer2 */
  79. { 41, 0x0000 }, /* R41 - Input Mixer3 */
  80. { 42, 0x0000 }, /* R42 - Input Mixer4 */
  81. { 43, 0x0000 }, /* R43 - Input Mixer5 */
  82. { 44, 0x0000 }, /* R44 - Input Mixer6 */
  83. { 45, 0x0000 }, /* R45 - Output Mixer1 */
  84. { 46, 0x0000 }, /* R46 - Output Mixer2 */
  85. { 47, 0x0000 }, /* R47 - Output Mixer3 */
  86. { 48, 0x0000 }, /* R48 - Output Mixer4 */
  87. { 49, 0x0000 }, /* R49 - Output Mixer5 */
  88. { 50, 0x0000 }, /* R50 - Output Mixer6 */
  89. { 51, 0x0000 }, /* R51 - HPOUT2 Mixer */
  90. { 52, 0x0000 }, /* R52 - Line Mixer1 */
  91. { 53, 0x0000 }, /* R53 - Line Mixer2 */
  92. { 54, 0x0000 }, /* R54 - Speaker Mixer */
  93. { 55, 0x0000 }, /* R55 - Additional Control */
  94. { 56, 0x0000 }, /* R56 - AntiPOP1 */
  95. { 57, 0x0000 }, /* R57 - AntiPOP2 */
  96. { 58, 0x0000 }, /* R58 - MICBIAS */
  97. { 60, 0x0000 }, /* R60 - FLL Control 1 */
  98. { 61, 0x0000 }, /* R61 - FLL Control 2 */
  99. { 62, 0x0000 }, /* R62 - FLL Control 3 */
  100. { 63, 0x2EE0 }, /* R63 - FLL Control 4 */
  101. { 64, 0x0002 }, /* R64 - FLL Control 5 */
  102. { 65, 0x2287 }, /* R65 - Clocking 3 */
  103. { 66, 0x025F }, /* R66 - Clocking 4 */
  104. { 67, 0x0000 }, /* R67 - MW Slave Control */
  105. { 69, 0x0002 }, /* R69 - Bus Control 1 */
  106. { 70, 0x0000 }, /* R70 - Write Sequencer 0 */
  107. { 71, 0x0000 }, /* R71 - Write Sequencer 1 */
  108. { 72, 0x0000 }, /* R72 - Write Sequencer 2 */
  109. { 73, 0x0000 }, /* R73 - Write Sequencer 3 */
  110. { 74, 0x0000 }, /* R74 - Write Sequencer 4 */
  111. { 75, 0x0000 }, /* R75 - Write Sequencer 5 */
  112. { 76, 0x1F25 }, /* R76 - Charge Pump 1 */
  113. { 81, 0x0000 }, /* R81 - Class W 0 */
  114. { 85, 0x054A }, /* R85 - DC Servo 1 */
  115. { 87, 0x0000 }, /* R87 - DC Servo 3 */
  116. { 96, 0x0100 }, /* R96 - Analogue HP 0 */
  117. { 98, 0x0000 }, /* R98 - EQ1 */
  118. { 99, 0x000C }, /* R99 - EQ2 */
  119. { 100, 0x000C }, /* R100 - EQ3 */
  120. { 101, 0x000C }, /* R101 - EQ4 */
  121. { 102, 0x000C }, /* R102 - EQ5 */
  122. { 103, 0x000C }, /* R103 - EQ6 */
  123. { 104, 0x0FCA }, /* R104 - EQ7 */
  124. { 105, 0x0400 }, /* R105 - EQ8 */
  125. { 106, 0x00D8 }, /* R106 - EQ9 */
  126. { 107, 0x1EB5 }, /* R107 - EQ10 */
  127. { 108, 0xF145 }, /* R108 - EQ11 */
  128. { 109, 0x0B75 }, /* R109 - EQ12 */
  129. { 110, 0x01C5 }, /* R110 - EQ13 */
  130. { 111, 0x1C58 }, /* R111 - EQ14 */
  131. { 112, 0xF373 }, /* R112 - EQ15 */
  132. { 113, 0x0A54 }, /* R113 - EQ16 */
  133. { 114, 0x0558 }, /* R114 - EQ17 */
  134. { 115, 0x168E }, /* R115 - EQ18 */
  135. { 116, 0xF829 }, /* R116 - EQ19 */
  136. { 117, 0x07AD }, /* R117 - EQ20 */
  137. { 118, 0x1103 }, /* R118 - EQ21 */
  138. { 119, 0x0564 }, /* R119 - EQ22 */
  139. { 120, 0x0559 }, /* R120 - EQ23 */
  140. { 121, 0x4000 }, /* R121 - EQ24 */
  141. { 122, 0x0000 }, /* R122 - Digital Pulls */
  142. { 123, 0x0F08 }, /* R123 - DRC Control 1 */
  143. { 124, 0x0000 }, /* R124 - DRC Control 2 */
  144. { 125, 0x0080 }, /* R125 - DRC Control 3 */
  145. { 126, 0x0000 }, /* R126 - DRC Control 4 */
  146. };
  147. static struct {
  148. int ratio;
  149. int clk_sys_rate;
  150. } clk_sys_rates[] = {
  151. { 64, 0 },
  152. { 128, 1 },
  153. { 192, 2 },
  154. { 256, 3 },
  155. { 384, 4 },
  156. { 512, 5 },
  157. { 768, 6 },
  158. { 1024, 7 },
  159. { 1408, 8 },
  160. { 1536, 9 },
  161. };
  162. static struct {
  163. int rate;
  164. int sample_rate;
  165. } sample_rates[] = {
  166. { 8000, 0 },
  167. { 11025, 1 },
  168. { 12000, 1 },
  169. { 16000, 2 },
  170. { 22050, 3 },
  171. { 24000, 3 },
  172. { 32000, 4 },
  173. { 44100, 5 },
  174. { 48000, 5 },
  175. };
  176. static struct {
  177. int div; /* *10 due to .5s */
  178. int bclk_div;
  179. } bclk_divs[] = {
  180. { 10, 0 },
  181. { 15, 1 },
  182. { 20, 2 },
  183. { 30, 3 },
  184. { 40, 4 },
  185. { 55, 5 },
  186. { 60, 6 },
  187. { 80, 7 },
  188. { 110, 8 },
  189. { 120, 9 },
  190. { 160, 10 },
  191. { 220, 11 },
  192. { 240, 12 },
  193. { 320, 13 },
  194. { 440, 14 },
  195. { 480, 15 },
  196. };
  197. struct wm8993_priv {
  198. struct wm_hubs_data hubs_data;
  199. struct device *dev;
  200. struct regmap *regmap;
  201. struct regulator_bulk_data supplies[WM8993_NUM_SUPPLIES];
  202. struct wm8993_platform_data pdata;
  203. struct completion fll_lock;
  204. int master;
  205. int sysclk_source;
  206. int tdm_slots;
  207. int tdm_width;
  208. unsigned int mclk_rate;
  209. unsigned int sysclk_rate;
  210. unsigned int fs;
  211. unsigned int bclk;
  212. int class_w_users;
  213. unsigned int fll_fref;
  214. unsigned int fll_fout;
  215. int fll_src;
  216. };
  217. static bool wm8993_volatile(struct device *dev, unsigned int reg)
  218. {
  219. switch (reg) {
  220. case WM8993_SOFTWARE_RESET:
  221. case WM8993_GPIO_CTRL_1:
  222. case WM8993_DC_SERVO_0:
  223. case WM8993_DC_SERVO_READBACK_0:
  224. case WM8993_DC_SERVO_READBACK_1:
  225. case WM8993_DC_SERVO_READBACK_2:
  226. return true;
  227. default:
  228. return false;
  229. }
  230. }
  231. static bool wm8993_readable(struct device *dev, unsigned int reg)
  232. {
  233. switch (reg) {
  234. case WM8993_SOFTWARE_RESET:
  235. case WM8993_POWER_MANAGEMENT_1:
  236. case WM8993_POWER_MANAGEMENT_2:
  237. case WM8993_POWER_MANAGEMENT_3:
  238. case WM8993_AUDIO_INTERFACE_1:
  239. case WM8993_AUDIO_INTERFACE_2:
  240. case WM8993_CLOCKING_1:
  241. case WM8993_CLOCKING_2:
  242. case WM8993_AUDIO_INTERFACE_3:
  243. case WM8993_AUDIO_INTERFACE_4:
  244. case WM8993_DAC_CTRL:
  245. case WM8993_LEFT_DAC_DIGITAL_VOLUME:
  246. case WM8993_RIGHT_DAC_DIGITAL_VOLUME:
  247. case WM8993_DIGITAL_SIDE_TONE:
  248. case WM8993_ADC_CTRL:
  249. case WM8993_LEFT_ADC_DIGITAL_VOLUME:
  250. case WM8993_RIGHT_ADC_DIGITAL_VOLUME:
  251. case WM8993_GPIO_CTRL_1:
  252. case WM8993_GPIO1:
  253. case WM8993_IRQ_DEBOUNCE:
  254. case WM8993_GPIOCTRL_2:
  255. case WM8993_GPIO_POL:
  256. case WM8993_LEFT_LINE_INPUT_1_2_VOLUME:
  257. case WM8993_LEFT_LINE_INPUT_3_4_VOLUME:
  258. case WM8993_RIGHT_LINE_INPUT_1_2_VOLUME:
  259. case WM8993_RIGHT_LINE_INPUT_3_4_VOLUME:
  260. case WM8993_LEFT_OUTPUT_VOLUME:
  261. case WM8993_RIGHT_OUTPUT_VOLUME:
  262. case WM8993_LINE_OUTPUTS_VOLUME:
  263. case WM8993_HPOUT2_VOLUME:
  264. case WM8993_LEFT_OPGA_VOLUME:
  265. case WM8993_RIGHT_OPGA_VOLUME:
  266. case WM8993_SPKMIXL_ATTENUATION:
  267. case WM8993_SPKMIXR_ATTENUATION:
  268. case WM8993_SPKOUT_MIXERS:
  269. case WM8993_SPKOUT_BOOST:
  270. case WM8993_SPEAKER_VOLUME_LEFT:
  271. case WM8993_SPEAKER_VOLUME_RIGHT:
  272. case WM8993_INPUT_MIXER2:
  273. case WM8993_INPUT_MIXER3:
  274. case WM8993_INPUT_MIXER4:
  275. case WM8993_INPUT_MIXER5:
  276. case WM8993_INPUT_MIXER6:
  277. case WM8993_OUTPUT_MIXER1:
  278. case WM8993_OUTPUT_MIXER2:
  279. case WM8993_OUTPUT_MIXER3:
  280. case WM8993_OUTPUT_MIXER4:
  281. case WM8993_OUTPUT_MIXER5:
  282. case WM8993_OUTPUT_MIXER6:
  283. case WM8993_HPOUT2_MIXER:
  284. case WM8993_LINE_MIXER1:
  285. case WM8993_LINE_MIXER2:
  286. case WM8993_SPEAKER_MIXER:
  287. case WM8993_ADDITIONAL_CONTROL:
  288. case WM8993_ANTIPOP1:
  289. case WM8993_ANTIPOP2:
  290. case WM8993_MICBIAS:
  291. case WM8993_FLL_CONTROL_1:
  292. case WM8993_FLL_CONTROL_2:
  293. case WM8993_FLL_CONTROL_3:
  294. case WM8993_FLL_CONTROL_4:
  295. case WM8993_FLL_CONTROL_5:
  296. case WM8993_CLOCKING_3:
  297. case WM8993_CLOCKING_4:
  298. case WM8993_MW_SLAVE_CONTROL:
  299. case WM8993_BUS_CONTROL_1:
  300. case WM8993_WRITE_SEQUENCER_0:
  301. case WM8993_WRITE_SEQUENCER_1:
  302. case WM8993_WRITE_SEQUENCER_2:
  303. case WM8993_WRITE_SEQUENCER_3:
  304. case WM8993_WRITE_SEQUENCER_4:
  305. case WM8993_WRITE_SEQUENCER_5:
  306. case WM8993_CHARGE_PUMP_1:
  307. case WM8993_CLASS_W_0:
  308. case WM8993_DC_SERVO_0:
  309. case WM8993_DC_SERVO_1:
  310. case WM8993_DC_SERVO_3:
  311. case WM8993_DC_SERVO_READBACK_0:
  312. case WM8993_DC_SERVO_READBACK_1:
  313. case WM8993_DC_SERVO_READBACK_2:
  314. case WM8993_ANALOGUE_HP_0:
  315. case WM8993_EQ1:
  316. case WM8993_EQ2:
  317. case WM8993_EQ3:
  318. case WM8993_EQ4:
  319. case WM8993_EQ5:
  320. case WM8993_EQ6:
  321. case WM8993_EQ7:
  322. case WM8993_EQ8:
  323. case WM8993_EQ9:
  324. case WM8993_EQ10:
  325. case WM8993_EQ11:
  326. case WM8993_EQ12:
  327. case WM8993_EQ13:
  328. case WM8993_EQ14:
  329. case WM8993_EQ15:
  330. case WM8993_EQ16:
  331. case WM8993_EQ17:
  332. case WM8993_EQ18:
  333. case WM8993_EQ19:
  334. case WM8993_EQ20:
  335. case WM8993_EQ21:
  336. case WM8993_EQ22:
  337. case WM8993_EQ23:
  338. case WM8993_EQ24:
  339. case WM8993_DIGITAL_PULLS:
  340. case WM8993_DRC_CONTROL_1:
  341. case WM8993_DRC_CONTROL_2:
  342. case WM8993_DRC_CONTROL_3:
  343. case WM8993_DRC_CONTROL_4:
  344. return true;
  345. default:
  346. return false;
  347. }
  348. }
  349. struct _fll_div {
  350. u16 fll_fratio;
  351. u16 fll_outdiv;
  352. u16 fll_clk_ref_div;
  353. u16 n;
  354. u16 k;
  355. };
  356. /* The size in bits of the FLL divide multiplied by 10
  357. * to allow rounding later */
  358. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  359. static struct {
  360. unsigned int min;
  361. unsigned int max;
  362. u16 fll_fratio;
  363. int ratio;
  364. } fll_fratios[] = {
  365. { 0, 64000, 4, 16 },
  366. { 64000, 128000, 3, 8 },
  367. { 128000, 256000, 2, 4 },
  368. { 256000, 1000000, 1, 2 },
  369. { 1000000, 13500000, 0, 1 },
  370. };
  371. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  372. unsigned int Fout)
  373. {
  374. u64 Kpart;
  375. unsigned int K, Ndiv, Nmod, target;
  376. unsigned int div;
  377. int i;
  378. /* Fref must be <=13.5MHz */
  379. div = 1;
  380. fll_div->fll_clk_ref_div = 0;
  381. while ((Fref / div) > 13500000) {
  382. div *= 2;
  383. fll_div->fll_clk_ref_div++;
  384. if (div > 8) {
  385. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  386. Fref);
  387. return -EINVAL;
  388. }
  389. }
  390. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  391. /* Apply the division for our remaining calculations */
  392. Fref /= div;
  393. /* Fvco should be 90-100MHz; don't check the upper bound */
  394. div = 0;
  395. target = Fout * 2;
  396. while (target < 90000000) {
  397. div++;
  398. target *= 2;
  399. if (div > 7) {
  400. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  401. Fout);
  402. return -EINVAL;
  403. }
  404. }
  405. fll_div->fll_outdiv = div;
  406. pr_debug("Fvco=%dHz\n", target);
  407. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  408. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  409. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  410. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  411. target /= fll_fratios[i].ratio;
  412. break;
  413. }
  414. }
  415. if (i == ARRAY_SIZE(fll_fratios)) {
  416. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  417. return -EINVAL;
  418. }
  419. /* Now, calculate N.K */
  420. Ndiv = target / Fref;
  421. fll_div->n = Ndiv;
  422. Nmod = target % Fref;
  423. pr_debug("Nmod=%d\n", Nmod);
  424. /* Calculate fractional part - scale up so we can round. */
  425. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  426. do_div(Kpart, Fref);
  427. K = Kpart & 0xFFFFFFFF;
  428. if ((K % 10) >= 5)
  429. K += 5;
  430. /* Move down to proper range now rounding is done */
  431. fll_div->k = K / 10;
  432. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  433. fll_div->n, fll_div->k,
  434. fll_div->fll_fratio, fll_div->fll_outdiv,
  435. fll_div->fll_clk_ref_div);
  436. return 0;
  437. }
  438. static int _wm8993_set_fll(struct snd_soc_codec *codec, int fll_id, int source,
  439. unsigned int Fref, unsigned int Fout)
  440. {
  441. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  442. struct i2c_client *i2c = to_i2c_client(codec->dev);
  443. u16 reg1, reg4, reg5;
  444. struct _fll_div fll_div;
  445. unsigned int timeout;
  446. int ret;
  447. /* Any change? */
  448. if (Fref == wm8993->fll_fref && Fout == wm8993->fll_fout)
  449. return 0;
  450. /* Disable the FLL */
  451. if (Fout == 0) {
  452. dev_dbg(codec->dev, "FLL disabled\n");
  453. wm8993->fll_fref = 0;
  454. wm8993->fll_fout = 0;
  455. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  456. reg1 &= ~WM8993_FLL_ENA;
  457. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  458. return 0;
  459. }
  460. ret = fll_factors(&fll_div, Fref, Fout);
  461. if (ret != 0)
  462. return ret;
  463. reg5 = snd_soc_read(codec, WM8993_FLL_CONTROL_5);
  464. reg5 &= ~WM8993_FLL_CLK_SRC_MASK;
  465. switch (fll_id) {
  466. case WM8993_FLL_MCLK:
  467. break;
  468. case WM8993_FLL_LRCLK:
  469. reg5 |= 1;
  470. break;
  471. case WM8993_FLL_BCLK:
  472. reg5 |= 2;
  473. break;
  474. default:
  475. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  476. return -EINVAL;
  477. }
  478. /* Any FLL configuration change requires that the FLL be
  479. * disabled first. */
  480. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  481. reg1 &= ~WM8993_FLL_ENA;
  482. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  483. /* Apply the configuration */
  484. if (fll_div.k)
  485. reg1 |= WM8993_FLL_FRAC_MASK;
  486. else
  487. reg1 &= ~WM8993_FLL_FRAC_MASK;
  488. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  489. snd_soc_write(codec, WM8993_FLL_CONTROL_2,
  490. (fll_div.fll_outdiv << WM8993_FLL_OUTDIV_SHIFT) |
  491. (fll_div.fll_fratio << WM8993_FLL_FRATIO_SHIFT));
  492. snd_soc_write(codec, WM8993_FLL_CONTROL_3, fll_div.k);
  493. reg4 = snd_soc_read(codec, WM8993_FLL_CONTROL_4);
  494. reg4 &= ~WM8993_FLL_N_MASK;
  495. reg4 |= fll_div.n << WM8993_FLL_N_SHIFT;
  496. snd_soc_write(codec, WM8993_FLL_CONTROL_4, reg4);
  497. reg5 &= ~WM8993_FLL_CLK_REF_DIV_MASK;
  498. reg5 |= fll_div.fll_clk_ref_div << WM8993_FLL_CLK_REF_DIV_SHIFT;
  499. snd_soc_write(codec, WM8993_FLL_CONTROL_5, reg5);
  500. /* If we've got an interrupt wired up make sure we get it */
  501. if (i2c->irq)
  502. timeout = msecs_to_jiffies(20);
  503. else if (Fref < 1000000)
  504. timeout = msecs_to_jiffies(3);
  505. else
  506. timeout = msecs_to_jiffies(1);
  507. try_wait_for_completion(&wm8993->fll_lock);
  508. /* Enable the FLL */
  509. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1 | WM8993_FLL_ENA);
  510. timeout = wait_for_completion_timeout(&wm8993->fll_lock, timeout);
  511. if (i2c->irq && !timeout)
  512. dev_warn(codec->dev, "Timed out waiting for FLL\n");
  513. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  514. wm8993->fll_fref = Fref;
  515. wm8993->fll_fout = Fout;
  516. wm8993->fll_src = source;
  517. return 0;
  518. }
  519. static int wm8993_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  520. unsigned int Fref, unsigned int Fout)
  521. {
  522. return _wm8993_set_fll(dai->codec, fll_id, source, Fref, Fout);
  523. }
  524. static int configure_clock(struct snd_soc_codec *codec)
  525. {
  526. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  527. unsigned int reg;
  528. /* This should be done on init() for bypass paths */
  529. switch (wm8993->sysclk_source) {
  530. case WM8993_SYSCLK_MCLK:
  531. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8993->mclk_rate);
  532. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  533. reg &= ~(WM8993_MCLK_DIV | WM8993_SYSCLK_SRC);
  534. if (wm8993->mclk_rate > 13500000) {
  535. reg |= WM8993_MCLK_DIV;
  536. wm8993->sysclk_rate = wm8993->mclk_rate / 2;
  537. } else {
  538. reg &= ~WM8993_MCLK_DIV;
  539. wm8993->sysclk_rate = wm8993->mclk_rate;
  540. }
  541. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  542. break;
  543. case WM8993_SYSCLK_FLL:
  544. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  545. wm8993->fll_fout);
  546. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  547. reg |= WM8993_SYSCLK_SRC;
  548. if (wm8993->fll_fout > 13500000) {
  549. reg |= WM8993_MCLK_DIV;
  550. wm8993->sysclk_rate = wm8993->fll_fout / 2;
  551. } else {
  552. reg &= ~WM8993_MCLK_DIV;
  553. wm8993->sysclk_rate = wm8993->fll_fout;
  554. }
  555. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  556. break;
  557. default:
  558. dev_err(codec->dev, "System clock not configured\n");
  559. return -EINVAL;
  560. }
  561. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8993->sysclk_rate);
  562. return 0;
  563. }
  564. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  565. static const DECLARE_TLV_DB_SCALE(drc_comp_threash, -4500, 75, 0);
  566. static const DECLARE_TLV_DB_SCALE(drc_comp_amp, -2250, 75, 0);
  567. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  568. static const unsigned int drc_max_tlv[] = {
  569. TLV_DB_RANGE_HEAD(2),
  570. 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
  571. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
  572. };
  573. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  574. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -1800, 300, 0);
  575. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  576. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  577. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  578. static const char *dac_deemph_text[] = {
  579. "None",
  580. "32kHz",
  581. "44.1kHz",
  582. "48kHz",
  583. };
  584. static const struct soc_enum dac_deemph =
  585. SOC_ENUM_SINGLE(WM8993_DAC_CTRL, 4, 4, dac_deemph_text);
  586. static const char *adc_hpf_text[] = {
  587. "Hi-Fi",
  588. "Voice 1",
  589. "Voice 2",
  590. "Voice 3",
  591. };
  592. static const struct soc_enum adc_hpf =
  593. SOC_ENUM_SINGLE(WM8993_ADC_CTRL, 5, 4, adc_hpf_text);
  594. static const char *drc_path_text[] = {
  595. "ADC",
  596. "DAC"
  597. };
  598. static const struct soc_enum drc_path =
  599. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 14, 2, drc_path_text);
  600. static const char *drc_r0_text[] = {
  601. "1",
  602. "1/2",
  603. "1/4",
  604. "1/8",
  605. "1/16",
  606. "0",
  607. };
  608. static const struct soc_enum drc_r0 =
  609. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 8, 6, drc_r0_text);
  610. static const char *drc_r1_text[] = {
  611. "1",
  612. "1/2",
  613. "1/4",
  614. "1/8",
  615. "0",
  616. };
  617. static const struct soc_enum drc_r1 =
  618. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_4, 13, 5, drc_r1_text);
  619. static const char *drc_attack_text[] = {
  620. "Reserved",
  621. "181us",
  622. "363us",
  623. "726us",
  624. "1.45ms",
  625. "2.9ms",
  626. "5.8ms",
  627. "11.6ms",
  628. "23.2ms",
  629. "46.4ms",
  630. "92.8ms",
  631. "185.6ms",
  632. };
  633. static const struct soc_enum drc_attack =
  634. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 12, 12, drc_attack_text);
  635. static const char *drc_decay_text[] = {
  636. "186ms",
  637. "372ms",
  638. "743ms",
  639. "1.49s",
  640. "2.97ms",
  641. "5.94ms",
  642. "11.89ms",
  643. "23.78ms",
  644. "47.56ms",
  645. };
  646. static const struct soc_enum drc_decay =
  647. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 8, 9, drc_decay_text);
  648. static const char *drc_ff_text[] = {
  649. "5 samples",
  650. "9 samples",
  651. };
  652. static const struct soc_enum drc_ff =
  653. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 7, 2, drc_ff_text);
  654. static const char *drc_qr_rate_text[] = {
  655. "0.725ms",
  656. "1.45ms",
  657. "5.8ms",
  658. };
  659. static const struct soc_enum drc_qr_rate =
  660. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 0, 3, drc_qr_rate_text);
  661. static const char *drc_smooth_text[] = {
  662. "Low",
  663. "Medium",
  664. "High",
  665. };
  666. static const struct soc_enum drc_smooth =
  667. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 4, 3, drc_smooth_text);
  668. static const struct snd_kcontrol_new wm8993_snd_controls[] = {
  669. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8993_DIGITAL_SIDE_TONE,
  670. 5, 9, 12, 0, sidetone_tlv),
  671. SOC_SINGLE("DRC Switch", WM8993_DRC_CONTROL_1, 15, 1, 0),
  672. SOC_ENUM("DRC Path", drc_path),
  673. SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8993_DRC_CONTROL_2,
  674. 2, 60, 1, drc_comp_threash),
  675. SOC_SINGLE_TLV("DRC Compressor Amplitude Volume", WM8993_DRC_CONTROL_3,
  676. 11, 30, 1, drc_comp_amp),
  677. SOC_ENUM("DRC R0", drc_r0),
  678. SOC_ENUM("DRC R1", drc_r1),
  679. SOC_SINGLE_TLV("DRC Minimum Volume", WM8993_DRC_CONTROL_1, 2, 3, 1,
  680. drc_min_tlv),
  681. SOC_SINGLE_TLV("DRC Maximum Volume", WM8993_DRC_CONTROL_1, 0, 3, 0,
  682. drc_max_tlv),
  683. SOC_ENUM("DRC Attack Rate", drc_attack),
  684. SOC_ENUM("DRC Decay Rate", drc_decay),
  685. SOC_ENUM("DRC FF Delay", drc_ff),
  686. SOC_SINGLE("DRC Anti-clip Switch", WM8993_DRC_CONTROL_1, 9, 1, 0),
  687. SOC_SINGLE("DRC Quick Release Switch", WM8993_DRC_CONTROL_1, 10, 1, 0),
  688. SOC_SINGLE_TLV("DRC Quick Release Volume", WM8993_DRC_CONTROL_3, 2, 3, 0,
  689. drc_qr_tlv),
  690. SOC_ENUM("DRC Quick Release Rate", drc_qr_rate),
  691. SOC_SINGLE("DRC Smoothing Switch", WM8993_DRC_CONTROL_1, 11, 1, 0),
  692. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8993_DRC_CONTROL_1, 8, 1, 0),
  693. SOC_ENUM("DRC Smoothing Hysteresis Threshold", drc_smooth),
  694. SOC_SINGLE_TLV("DRC Startup Volume", WM8993_DRC_CONTROL_4, 8, 18, 0,
  695. drc_startup_tlv),
  696. SOC_SINGLE("EQ Switch", WM8993_EQ1, 0, 1, 0),
  697. SOC_DOUBLE_R_TLV("Capture Volume", WM8993_LEFT_ADC_DIGITAL_VOLUME,
  698. WM8993_RIGHT_ADC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  699. SOC_SINGLE("ADC High Pass Filter Switch", WM8993_ADC_CTRL, 8, 1, 0),
  700. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  701. SOC_DOUBLE_R_TLV("Playback Volume", WM8993_LEFT_DAC_DIGITAL_VOLUME,
  702. WM8993_RIGHT_DAC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  703. SOC_SINGLE_TLV("Playback Boost Volume", WM8993_AUDIO_INTERFACE_2, 10, 3, 0,
  704. dac_boost_tlv),
  705. SOC_ENUM("DAC Deemphasis", dac_deemph),
  706. SOC_SINGLE_TLV("SPKL DAC Volume", WM8993_SPKMIXL_ATTENUATION,
  707. 2, 1, 1, wm_hubs_spkmix_tlv),
  708. SOC_SINGLE_TLV("SPKR DAC Volume", WM8993_SPKMIXR_ATTENUATION,
  709. 2, 1, 1, wm_hubs_spkmix_tlv),
  710. };
  711. static const struct snd_kcontrol_new wm8993_eq_controls[] = {
  712. SOC_SINGLE_TLV("EQ1 Volume", WM8993_EQ2, 0, 24, 0, eq_tlv),
  713. SOC_SINGLE_TLV("EQ2 Volume", WM8993_EQ3, 0, 24, 0, eq_tlv),
  714. SOC_SINGLE_TLV("EQ3 Volume", WM8993_EQ4, 0, 24, 0, eq_tlv),
  715. SOC_SINGLE_TLV("EQ4 Volume", WM8993_EQ5, 0, 24, 0, eq_tlv),
  716. SOC_SINGLE_TLV("EQ5 Volume", WM8993_EQ6, 0, 24, 0, eq_tlv),
  717. };
  718. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  719. struct snd_kcontrol *kcontrol, int event)
  720. {
  721. struct snd_soc_codec *codec = w->codec;
  722. switch (event) {
  723. case SND_SOC_DAPM_PRE_PMU:
  724. return configure_clock(codec);
  725. case SND_SOC_DAPM_POST_PMD:
  726. break;
  727. }
  728. return 0;
  729. }
  730. /*
  731. * When used with DAC outputs only the WM8993 charge pump supports
  732. * operation in class W mode, providing very low power consumption
  733. * when used with digital sources. Enable and disable this mode
  734. * automatically depending on the mixer configuration.
  735. *
  736. * Currently the only supported paths are the direct DAC->headphone
  737. * paths (which provide minimum power consumption anyway).
  738. */
  739. static int class_w_put(struct snd_kcontrol *kcontrol,
  740. struct snd_ctl_elem_value *ucontrol)
  741. {
  742. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  743. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  744. struct snd_soc_codec *codec = widget->codec;
  745. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  746. int ret;
  747. /* Turn it off if we're using the main output mixer */
  748. if (ucontrol->value.integer.value[0] == 0) {
  749. if (wm8993->class_w_users == 0) {
  750. dev_dbg(codec->dev, "Disabling Class W\n");
  751. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  752. WM8993_CP_DYN_FREQ |
  753. WM8993_CP_DYN_V,
  754. 0);
  755. }
  756. wm8993->class_w_users++;
  757. wm8993->hubs_data.class_w = true;
  758. }
  759. /* Implement the change */
  760. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  761. /* Enable it if we're using the direct DAC path */
  762. if (ucontrol->value.integer.value[0] == 1) {
  763. if (wm8993->class_w_users == 1) {
  764. dev_dbg(codec->dev, "Enabling Class W\n");
  765. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  766. WM8993_CP_DYN_FREQ |
  767. WM8993_CP_DYN_V,
  768. WM8993_CP_DYN_FREQ |
  769. WM8993_CP_DYN_V);
  770. }
  771. wm8993->class_w_users--;
  772. wm8993->hubs_data.class_w = false;
  773. }
  774. dev_dbg(codec->dev, "Indirect DAC use count now %d\n",
  775. wm8993->class_w_users);
  776. return ret;
  777. }
  778. #define SOC_DAPM_ENUM_W(xname, xenum) \
  779. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  780. .info = snd_soc_info_enum_double, \
  781. .get = snd_soc_dapm_get_enum_double, \
  782. .put = class_w_put, \
  783. .private_value = (unsigned long)&xenum }
  784. static const char *hp_mux_text[] = {
  785. "Mixer",
  786. "DAC",
  787. };
  788. static const struct soc_enum hpl_enum =
  789. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER1, 8, 2, hp_mux_text);
  790. static const struct snd_kcontrol_new hpl_mux =
  791. SOC_DAPM_ENUM_W("Left Headphone Mux", hpl_enum);
  792. static const struct soc_enum hpr_enum =
  793. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER2, 8, 2, hp_mux_text);
  794. static const struct snd_kcontrol_new hpr_mux =
  795. SOC_DAPM_ENUM_W("Right Headphone Mux", hpr_enum);
  796. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  797. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 7, 1, 0),
  798. SOC_DAPM_SINGLE("IN1LP Switch", WM8993_SPEAKER_MIXER, 5, 1, 0),
  799. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 3, 1, 0),
  800. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  801. };
  802. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  803. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  804. SOC_DAPM_SINGLE("IN1RP Switch", WM8993_SPEAKER_MIXER, 4, 1, 0),
  805. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 2, 1, 0),
  806. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 0, 1, 0),
  807. };
  808. static const char *aif_text[] = {
  809. "Left", "Right"
  810. };
  811. static const struct soc_enum aifoutl_enum =
  812. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 15, 2, aif_text);
  813. static const struct snd_kcontrol_new aifoutl_mux =
  814. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  815. static const struct soc_enum aifoutr_enum =
  816. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 14, 2, aif_text);
  817. static const struct snd_kcontrol_new aifoutr_mux =
  818. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  819. static const struct soc_enum aifinl_enum =
  820. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 15, 2, aif_text);
  821. static const struct snd_kcontrol_new aifinl_mux =
  822. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  823. static const struct soc_enum aifinr_enum =
  824. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 14, 2, aif_text);
  825. static const struct snd_kcontrol_new aifinr_mux =
  826. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  827. static const char *sidetone_text[] = {
  828. "None", "Left", "Right"
  829. };
  830. static const struct soc_enum sidetonel_enum =
  831. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 2, 3, sidetone_text);
  832. static const struct snd_kcontrol_new sidetonel_mux =
  833. SOC_DAPM_ENUM("Left Sidetone", sidetonel_enum);
  834. static const struct soc_enum sidetoner_enum =
  835. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 0, 3, sidetone_text);
  836. static const struct snd_kcontrol_new sidetoner_mux =
  837. SOC_DAPM_ENUM("Right Sidetone", sidetoner_enum);
  838. static const struct snd_soc_dapm_widget wm8993_dapm_widgets[] = {
  839. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8993_BUS_CONTROL_1, 1, 0, clk_sys_event,
  840. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  841. SND_SOC_DAPM_SUPPLY("TOCLK", WM8993_CLOCKING_1, 14, 0, NULL, 0),
  842. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8993_CLOCKING_3, 0, 0, NULL, 0),
  843. SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, NULL, 0),
  844. SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
  845. SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
  846. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  847. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  848. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  849. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  850. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  851. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  852. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  853. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  854. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &sidetonel_mux),
  855. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &sidetoner_mux),
  856. SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
  857. SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
  858. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  859. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  860. SND_SOC_DAPM_MIXER("SPKL", WM8993_POWER_MANAGEMENT_3, 8, 0,
  861. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  862. SND_SOC_DAPM_MIXER("SPKR", WM8993_POWER_MANAGEMENT_3, 9, 0,
  863. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  864. SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
  865. };
  866. static const struct snd_soc_dapm_route routes[] = {
  867. { "MICBIAS1", NULL, "VMID" },
  868. { "MICBIAS2", NULL, "VMID" },
  869. { "ADCL", NULL, "CLK_SYS" },
  870. { "ADCL", NULL, "CLK_DSP" },
  871. { "ADCR", NULL, "CLK_SYS" },
  872. { "ADCR", NULL, "CLK_DSP" },
  873. { "AIFOUTL Mux", "Left", "ADCL" },
  874. { "AIFOUTL Mux", "Right", "ADCR" },
  875. { "AIFOUTR Mux", "Left", "ADCL" },
  876. { "AIFOUTR Mux", "Right", "ADCR" },
  877. { "AIFOUTL", NULL, "AIFOUTL Mux" },
  878. { "AIFOUTR", NULL, "AIFOUTR Mux" },
  879. { "DACL Mux", "Left", "AIFINL" },
  880. { "DACL Mux", "Right", "AIFINR" },
  881. { "DACR Mux", "Left", "AIFINL" },
  882. { "DACR Mux", "Right", "AIFINR" },
  883. { "DACL Sidetone", "Left", "ADCL" },
  884. { "DACL Sidetone", "Right", "ADCR" },
  885. { "DACR Sidetone", "Left", "ADCL" },
  886. { "DACR Sidetone", "Right", "ADCR" },
  887. { "DACL", NULL, "CLK_SYS" },
  888. { "DACL", NULL, "CLK_DSP" },
  889. { "DACL", NULL, "DACL Mux" },
  890. { "DACL", NULL, "DACL Sidetone" },
  891. { "DACR", NULL, "CLK_SYS" },
  892. { "DACR", NULL, "CLK_DSP" },
  893. { "DACR", NULL, "DACR Mux" },
  894. { "DACR", NULL, "DACR Sidetone" },
  895. { "Left Output Mixer", "DAC Switch", "DACL" },
  896. { "Right Output Mixer", "DAC Switch", "DACR" },
  897. { "Left Output PGA", NULL, "CLK_SYS" },
  898. { "Right Output PGA", NULL, "CLK_SYS" },
  899. { "SPKL", "DAC Switch", "DACL" },
  900. { "SPKL", NULL, "CLK_SYS" },
  901. { "SPKR", "DAC Switch", "DACR" },
  902. { "SPKR", NULL, "CLK_SYS" },
  903. { "Left Headphone Mux", "DAC", "DACL" },
  904. { "Right Headphone Mux", "DAC", "DACR" },
  905. };
  906. static int wm8993_set_bias_level(struct snd_soc_codec *codec,
  907. enum snd_soc_bias_level level)
  908. {
  909. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  910. int ret;
  911. switch (level) {
  912. case SND_SOC_BIAS_ON:
  913. case SND_SOC_BIAS_PREPARE:
  914. /* VMID=2*40k */
  915. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  916. WM8993_VMID_SEL_MASK, 0x2);
  917. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  918. WM8993_TSHUT_ENA, WM8993_TSHUT_ENA);
  919. break;
  920. case SND_SOC_BIAS_STANDBY:
  921. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  922. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  923. wm8993->supplies);
  924. if (ret != 0)
  925. return ret;
  926. regcache_cache_only(wm8993->regmap, false);
  927. regcache_sync(wm8993->regmap);
  928. /* Tune DC servo configuration */
  929. snd_soc_write(codec, 0x44, 3);
  930. snd_soc_write(codec, 0x56, 3);
  931. snd_soc_write(codec, 0x44, 0);
  932. /* Bring up VMID with fast soft start */
  933. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  934. WM8993_STARTUP_BIAS_ENA |
  935. WM8993_VMID_BUF_ENA |
  936. WM8993_VMID_RAMP_MASK |
  937. WM8993_BIAS_SRC,
  938. WM8993_STARTUP_BIAS_ENA |
  939. WM8993_VMID_BUF_ENA |
  940. WM8993_VMID_RAMP_MASK |
  941. WM8993_BIAS_SRC);
  942. /* If either line output is single ended we
  943. * need the VMID buffer */
  944. if (!wm8993->pdata.lineout1_diff ||
  945. !wm8993->pdata.lineout2_diff)
  946. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  947. WM8993_LINEOUT_VMID_BUF_ENA,
  948. WM8993_LINEOUT_VMID_BUF_ENA);
  949. /* VMID=2*40k */
  950. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  951. WM8993_VMID_SEL_MASK |
  952. WM8993_BIAS_ENA,
  953. WM8993_BIAS_ENA | 0x2);
  954. msleep(32);
  955. /* Switch to normal bias */
  956. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  957. WM8993_BIAS_SRC |
  958. WM8993_STARTUP_BIAS_ENA, 0);
  959. }
  960. /* VMID=2*240k */
  961. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  962. WM8993_VMID_SEL_MASK, 0x4);
  963. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  964. WM8993_TSHUT_ENA, 0);
  965. break;
  966. case SND_SOC_BIAS_OFF:
  967. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  968. WM8993_LINEOUT_VMID_BUF_ENA, 0);
  969. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  970. WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
  971. 0);
  972. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  973. WM8993_STARTUP_BIAS_ENA |
  974. WM8993_VMID_BUF_ENA |
  975. WM8993_VMID_RAMP_MASK |
  976. WM8993_BIAS_SRC, 0);
  977. regcache_cache_only(wm8993->regmap, true);
  978. regcache_mark_dirty(wm8993->regmap);
  979. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies),
  980. wm8993->supplies);
  981. break;
  982. }
  983. codec->dapm.bias_level = level;
  984. return 0;
  985. }
  986. static int wm8993_set_sysclk(struct snd_soc_dai *codec_dai,
  987. int clk_id, unsigned int freq, int dir)
  988. {
  989. struct snd_soc_codec *codec = codec_dai->codec;
  990. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  991. switch (clk_id) {
  992. case WM8993_SYSCLK_MCLK:
  993. wm8993->mclk_rate = freq;
  994. case WM8993_SYSCLK_FLL:
  995. wm8993->sysclk_source = clk_id;
  996. break;
  997. default:
  998. return -EINVAL;
  999. }
  1000. return 0;
  1001. }
  1002. static int wm8993_set_dai_fmt(struct snd_soc_dai *dai,
  1003. unsigned int fmt)
  1004. {
  1005. struct snd_soc_codec *codec = dai->codec;
  1006. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1007. unsigned int aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  1008. unsigned int aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  1009. aif1 &= ~(WM8993_BCLK_DIR | WM8993_AIF_BCLK_INV |
  1010. WM8993_AIF_LRCLK_INV | WM8993_AIF_FMT_MASK);
  1011. aif4 &= ~WM8993_LRCLK_DIR;
  1012. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1013. case SND_SOC_DAIFMT_CBS_CFS:
  1014. wm8993->master = 0;
  1015. break;
  1016. case SND_SOC_DAIFMT_CBS_CFM:
  1017. aif4 |= WM8993_LRCLK_DIR;
  1018. wm8993->master = 1;
  1019. break;
  1020. case SND_SOC_DAIFMT_CBM_CFS:
  1021. aif1 |= WM8993_BCLK_DIR;
  1022. wm8993->master = 1;
  1023. break;
  1024. case SND_SOC_DAIFMT_CBM_CFM:
  1025. aif1 |= WM8993_BCLK_DIR;
  1026. aif4 |= WM8993_LRCLK_DIR;
  1027. wm8993->master = 1;
  1028. break;
  1029. default:
  1030. return -EINVAL;
  1031. }
  1032. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1033. case SND_SOC_DAIFMT_DSP_B:
  1034. aif1 |= WM8993_AIF_LRCLK_INV;
  1035. case SND_SOC_DAIFMT_DSP_A:
  1036. aif1 |= 0x18;
  1037. break;
  1038. case SND_SOC_DAIFMT_I2S:
  1039. aif1 |= 0x10;
  1040. break;
  1041. case SND_SOC_DAIFMT_RIGHT_J:
  1042. break;
  1043. case SND_SOC_DAIFMT_LEFT_J:
  1044. aif1 |= 0x8;
  1045. break;
  1046. default:
  1047. return -EINVAL;
  1048. }
  1049. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1050. case SND_SOC_DAIFMT_DSP_A:
  1051. case SND_SOC_DAIFMT_DSP_B:
  1052. /* frame inversion not valid for DSP modes */
  1053. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1054. case SND_SOC_DAIFMT_NB_NF:
  1055. break;
  1056. case SND_SOC_DAIFMT_IB_NF:
  1057. aif1 |= WM8993_AIF_BCLK_INV;
  1058. break;
  1059. default:
  1060. return -EINVAL;
  1061. }
  1062. break;
  1063. case SND_SOC_DAIFMT_I2S:
  1064. case SND_SOC_DAIFMT_RIGHT_J:
  1065. case SND_SOC_DAIFMT_LEFT_J:
  1066. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1067. case SND_SOC_DAIFMT_NB_NF:
  1068. break;
  1069. case SND_SOC_DAIFMT_IB_IF:
  1070. aif1 |= WM8993_AIF_BCLK_INV | WM8993_AIF_LRCLK_INV;
  1071. break;
  1072. case SND_SOC_DAIFMT_IB_NF:
  1073. aif1 |= WM8993_AIF_BCLK_INV;
  1074. break;
  1075. case SND_SOC_DAIFMT_NB_IF:
  1076. aif1 |= WM8993_AIF_LRCLK_INV;
  1077. break;
  1078. default:
  1079. return -EINVAL;
  1080. }
  1081. break;
  1082. default:
  1083. return -EINVAL;
  1084. }
  1085. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  1086. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  1087. return 0;
  1088. }
  1089. static int wm8993_hw_params(struct snd_pcm_substream *substream,
  1090. struct snd_pcm_hw_params *params,
  1091. struct snd_soc_dai *dai)
  1092. {
  1093. struct snd_soc_codec *codec = dai->codec;
  1094. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1095. int ret, i, best, best_val, cur_val;
  1096. unsigned int clocking1, clocking3, aif1, aif4;
  1097. clocking1 = snd_soc_read(codec, WM8993_CLOCKING_1);
  1098. clocking1 &= ~WM8993_BCLK_DIV_MASK;
  1099. clocking3 = snd_soc_read(codec, WM8993_CLOCKING_3);
  1100. clocking3 &= ~(WM8993_CLK_SYS_RATE_MASK | WM8993_SAMPLE_RATE_MASK);
  1101. aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  1102. aif1 &= ~WM8993_AIF_WL_MASK;
  1103. aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  1104. aif4 &= ~WM8993_LRCLK_RATE_MASK;
  1105. /* What BCLK do we need? */
  1106. wm8993->fs = params_rate(params);
  1107. wm8993->bclk = 2 * wm8993->fs;
  1108. if (wm8993->tdm_slots) {
  1109. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1110. wm8993->tdm_slots, wm8993->tdm_width);
  1111. wm8993->bclk *= wm8993->tdm_width * wm8993->tdm_slots;
  1112. } else {
  1113. switch (params_format(params)) {
  1114. case SNDRV_PCM_FORMAT_S16_LE:
  1115. wm8993->bclk *= 16;
  1116. break;
  1117. case SNDRV_PCM_FORMAT_S20_3LE:
  1118. wm8993->bclk *= 20;
  1119. aif1 |= 0x8;
  1120. break;
  1121. case SNDRV_PCM_FORMAT_S24_LE:
  1122. wm8993->bclk *= 24;
  1123. aif1 |= 0x10;
  1124. break;
  1125. case SNDRV_PCM_FORMAT_S32_LE:
  1126. wm8993->bclk *= 32;
  1127. aif1 |= 0x18;
  1128. break;
  1129. default:
  1130. return -EINVAL;
  1131. }
  1132. }
  1133. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8993->bclk);
  1134. ret = configure_clock(codec);
  1135. if (ret != 0)
  1136. return ret;
  1137. /* Select nearest CLK_SYS_RATE */
  1138. best = 0;
  1139. best_val = abs((wm8993->sysclk_rate / clk_sys_rates[0].ratio)
  1140. - wm8993->fs);
  1141. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1142. cur_val = abs((wm8993->sysclk_rate /
  1143. clk_sys_rates[i].ratio) - wm8993->fs);
  1144. if (cur_val < best_val) {
  1145. best = i;
  1146. best_val = cur_val;
  1147. }
  1148. }
  1149. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1150. clk_sys_rates[best].ratio);
  1151. clocking3 |= (clk_sys_rates[best].clk_sys_rate
  1152. << WM8993_CLK_SYS_RATE_SHIFT);
  1153. /* SAMPLE_RATE */
  1154. best = 0;
  1155. best_val = abs(wm8993->fs - sample_rates[0].rate);
  1156. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1157. /* Closest match */
  1158. cur_val = abs(wm8993->fs - sample_rates[i].rate);
  1159. if (cur_val < best_val) {
  1160. best = i;
  1161. best_val = cur_val;
  1162. }
  1163. }
  1164. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1165. sample_rates[best].rate);
  1166. clocking3 |= (sample_rates[best].sample_rate
  1167. << WM8993_SAMPLE_RATE_SHIFT);
  1168. /* BCLK_DIV */
  1169. best = 0;
  1170. best_val = INT_MAX;
  1171. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1172. cur_val = ((wm8993->sysclk_rate * 10) / bclk_divs[i].div)
  1173. - wm8993->bclk;
  1174. if (cur_val < 0) /* Table is sorted */
  1175. break;
  1176. if (cur_val < best_val) {
  1177. best = i;
  1178. best_val = cur_val;
  1179. }
  1180. }
  1181. wm8993->bclk = (wm8993->sysclk_rate * 10) / bclk_divs[best].div;
  1182. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1183. bclk_divs[best].div, wm8993->bclk);
  1184. clocking1 |= bclk_divs[best].bclk_div << WM8993_BCLK_DIV_SHIFT;
  1185. /* LRCLK is a simple fraction of BCLK */
  1186. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8993->bclk / wm8993->fs);
  1187. aif4 |= wm8993->bclk / wm8993->fs;
  1188. snd_soc_write(codec, WM8993_CLOCKING_1, clocking1);
  1189. snd_soc_write(codec, WM8993_CLOCKING_3, clocking3);
  1190. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  1191. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  1192. /* ReTune Mobile? */
  1193. if (wm8993->pdata.num_retune_configs) {
  1194. u16 eq1 = snd_soc_read(codec, WM8993_EQ1);
  1195. struct wm8993_retune_mobile_setting *s;
  1196. best = 0;
  1197. best_val = abs(wm8993->pdata.retune_configs[0].rate
  1198. - wm8993->fs);
  1199. for (i = 0; i < wm8993->pdata.num_retune_configs; i++) {
  1200. cur_val = abs(wm8993->pdata.retune_configs[i].rate
  1201. - wm8993->fs);
  1202. if (cur_val < best_val) {
  1203. best_val = cur_val;
  1204. best = i;
  1205. }
  1206. }
  1207. s = &wm8993->pdata.retune_configs[best];
  1208. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  1209. s->name, s->rate);
  1210. /* Disable EQ while we reconfigure */
  1211. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, 0);
  1212. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  1213. snd_soc_write(codec, WM8993_EQ1 + i, s->config[i]);
  1214. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, eq1);
  1215. }
  1216. return 0;
  1217. }
  1218. static int wm8993_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1219. {
  1220. struct snd_soc_codec *codec = codec_dai->codec;
  1221. unsigned int reg;
  1222. reg = snd_soc_read(codec, WM8993_DAC_CTRL);
  1223. if (mute)
  1224. reg |= WM8993_DAC_MUTE;
  1225. else
  1226. reg &= ~WM8993_DAC_MUTE;
  1227. snd_soc_write(codec, WM8993_DAC_CTRL, reg);
  1228. return 0;
  1229. }
  1230. static int wm8993_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1231. unsigned int rx_mask, int slots, int slot_width)
  1232. {
  1233. struct snd_soc_codec *codec = dai->codec;
  1234. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1235. int aif1 = 0;
  1236. int aif2 = 0;
  1237. /* Don't need to validate anything if we're turning off TDM */
  1238. if (slots == 0) {
  1239. wm8993->tdm_slots = 0;
  1240. goto out;
  1241. }
  1242. /* Note that we allow configurations we can't handle ourselves -
  1243. * for example, we can generate clocks for slots 2 and up even if
  1244. * we can't use those slots ourselves.
  1245. */
  1246. aif1 |= WM8993_AIFADC_TDM;
  1247. aif2 |= WM8993_AIFDAC_TDM;
  1248. switch (rx_mask) {
  1249. case 3:
  1250. break;
  1251. case 0xc:
  1252. aif1 |= WM8993_AIFADC_TDM_CHAN;
  1253. break;
  1254. default:
  1255. return -EINVAL;
  1256. }
  1257. switch (tx_mask) {
  1258. case 3:
  1259. break;
  1260. case 0xc:
  1261. aif2 |= WM8993_AIFDAC_TDM_CHAN;
  1262. break;
  1263. default:
  1264. return -EINVAL;
  1265. }
  1266. out:
  1267. wm8993->tdm_width = slot_width;
  1268. wm8993->tdm_slots = slots / 2;
  1269. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_1,
  1270. WM8993_AIFADC_TDM | WM8993_AIFADC_TDM_CHAN, aif1);
  1271. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_2,
  1272. WM8993_AIFDAC_TDM | WM8993_AIFDAC_TDM_CHAN, aif2);
  1273. return 0;
  1274. }
  1275. static irqreturn_t wm8993_irq(int irq, void *data)
  1276. {
  1277. struct wm8993_priv *wm8993 = data;
  1278. int mask, val, ret;
  1279. ret = regmap_read(wm8993->regmap, WM8993_GPIO_CTRL_1, &val);
  1280. if (ret != 0) {
  1281. dev_err(wm8993->dev, "Failed to read interrupt status: %d\n",
  1282. ret);
  1283. return IRQ_NONE;
  1284. }
  1285. ret = regmap_read(wm8993->regmap, WM8993_GPIOCTRL_2, &mask);
  1286. if (ret != 0) {
  1287. dev_err(wm8993->dev, "Failed to read interrupt mask: %d\n",
  1288. ret);
  1289. return IRQ_NONE;
  1290. }
  1291. /* The IRQ pin status is visible in the register too */
  1292. val &= ~(mask | WM8993_IRQ);
  1293. if (!val)
  1294. return IRQ_NONE;
  1295. if (val & WM8993_TEMPOK_EINT)
  1296. dev_crit(wm8993->dev, "Thermal warning\n");
  1297. if (val & WM8993_FLL_LOCK_EINT) {
  1298. dev_dbg(wm8993->dev, "FLL locked\n");
  1299. complete(&wm8993->fll_lock);
  1300. }
  1301. ret = regmap_write(wm8993->regmap, WM8993_GPIO_CTRL_1, val);
  1302. if (ret != 0)
  1303. dev_err(wm8993->dev, "Failed to ack interrupt: %d\n", ret);
  1304. return IRQ_HANDLED;
  1305. }
  1306. static const struct snd_soc_dai_ops wm8993_ops = {
  1307. .set_sysclk = wm8993_set_sysclk,
  1308. .set_fmt = wm8993_set_dai_fmt,
  1309. .hw_params = wm8993_hw_params,
  1310. .digital_mute = wm8993_digital_mute,
  1311. .set_pll = wm8993_set_fll,
  1312. .set_tdm_slot = wm8993_set_tdm_slot,
  1313. };
  1314. #define WM8993_RATES SNDRV_PCM_RATE_8000_48000
  1315. #define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1316. SNDRV_PCM_FMTBIT_S20_3LE |\
  1317. SNDRV_PCM_FMTBIT_S24_LE |\
  1318. SNDRV_PCM_FMTBIT_S32_LE)
  1319. static struct snd_soc_dai_driver wm8993_dai = {
  1320. .name = "wm8993-hifi",
  1321. .playback = {
  1322. .stream_name = "Playback",
  1323. .channels_min = 1,
  1324. .channels_max = 2,
  1325. .rates = WM8993_RATES,
  1326. .formats = WM8993_FORMATS,
  1327. },
  1328. .capture = {
  1329. .stream_name = "Capture",
  1330. .channels_min = 1,
  1331. .channels_max = 2,
  1332. .rates = WM8993_RATES,
  1333. .formats = WM8993_FORMATS,
  1334. },
  1335. .ops = &wm8993_ops,
  1336. .symmetric_rates = 1,
  1337. };
  1338. static int wm8993_probe(struct snd_soc_codec *codec)
  1339. {
  1340. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1341. struct snd_soc_dapm_context *dapm = &codec->dapm;
  1342. int ret;
  1343. wm8993->hubs_data.hp_startup_mode = 1;
  1344. wm8993->hubs_data.dcs_codes_l = -2;
  1345. wm8993->hubs_data.dcs_codes_r = -2;
  1346. wm8993->hubs_data.series_startup = 1;
  1347. codec->control_data = wm8993->regmap;
  1348. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  1349. if (ret != 0) {
  1350. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1351. return ret;
  1352. }
  1353. /* By default we're using the output mixers */
  1354. wm8993->class_w_users = 2;
  1355. /* Latch volume update bits and default ZC on */
  1356. snd_soc_update_bits(codec, WM8993_RIGHT_DAC_DIGITAL_VOLUME,
  1357. WM8993_DAC_VU, WM8993_DAC_VU);
  1358. snd_soc_update_bits(codec, WM8993_RIGHT_ADC_DIGITAL_VOLUME,
  1359. WM8993_ADC_VU, WM8993_ADC_VU);
  1360. /* Manualy manage the HPOUT sequencing for independent stereo
  1361. * control. */
  1362. snd_soc_update_bits(codec, WM8993_ANALOGUE_HP_0,
  1363. WM8993_HPOUT1_AUTO_PU, 0);
  1364. /* Use automatic clock configuration */
  1365. snd_soc_update_bits(codec, WM8993_CLOCKING_4, WM8993_SR_MODE, 0);
  1366. wm_hubs_handle_analogue_pdata(codec, wm8993->pdata.lineout1_diff,
  1367. wm8993->pdata.lineout2_diff,
  1368. wm8993->pdata.lineout1fb,
  1369. wm8993->pdata.lineout2fb,
  1370. wm8993->pdata.jd_scthr,
  1371. wm8993->pdata.jd_thr,
  1372. wm8993->pdata.micbias1_lvl,
  1373. wm8993->pdata.micbias2_lvl);
  1374. ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1375. if (ret != 0)
  1376. return ret;
  1377. snd_soc_add_controls(codec, wm8993_snd_controls,
  1378. ARRAY_SIZE(wm8993_snd_controls));
  1379. if (wm8993->pdata.num_retune_configs != 0) {
  1380. dev_dbg(codec->dev, "Using ReTune Mobile\n");
  1381. } else {
  1382. dev_dbg(codec->dev, "No ReTune Mobile, using normal EQ\n");
  1383. snd_soc_add_controls(codec, wm8993_eq_controls,
  1384. ARRAY_SIZE(wm8993_eq_controls));
  1385. }
  1386. snd_soc_dapm_new_controls(dapm, wm8993_dapm_widgets,
  1387. ARRAY_SIZE(wm8993_dapm_widgets));
  1388. wm_hubs_add_analogue_controls(codec);
  1389. snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
  1390. wm_hubs_add_analogue_routes(codec, wm8993->pdata.lineout1_diff,
  1391. wm8993->pdata.lineout2_diff);
  1392. return 0;
  1393. }
  1394. static int wm8993_remove(struct snd_soc_codec *codec)
  1395. {
  1396. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1397. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1398. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1399. return 0;
  1400. }
  1401. #ifdef CONFIG_PM
  1402. static int wm8993_suspend(struct snd_soc_codec *codec)
  1403. {
  1404. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1405. int fll_fout = wm8993->fll_fout;
  1406. int fll_fref = wm8993->fll_fref;
  1407. int ret;
  1408. /* Stop the FLL in an orderly fashion */
  1409. ret = _wm8993_set_fll(codec, 0, 0, 0, 0);
  1410. if (ret != 0) {
  1411. dev_err(codec->dev, "Failed to stop FLL\n");
  1412. return ret;
  1413. }
  1414. wm8993->fll_fout = fll_fout;
  1415. wm8993->fll_fref = fll_fref;
  1416. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1417. return 0;
  1418. }
  1419. static int wm8993_resume(struct snd_soc_codec *codec)
  1420. {
  1421. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1422. int ret;
  1423. wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1424. /* Restart the FLL? */
  1425. if (wm8993->fll_fout) {
  1426. int fll_fout = wm8993->fll_fout;
  1427. int fll_fref = wm8993->fll_fref;
  1428. wm8993->fll_fref = 0;
  1429. wm8993->fll_fout = 0;
  1430. ret = _wm8993_set_fll(codec, 0, wm8993->fll_src,
  1431. fll_fref, fll_fout);
  1432. if (ret != 0)
  1433. dev_err(codec->dev, "Failed to restart FLL\n");
  1434. }
  1435. return 0;
  1436. }
  1437. #else
  1438. #define wm8993_suspend NULL
  1439. #define wm8993_resume NULL
  1440. #endif
  1441. static const struct regmap_config wm8993_regmap = {
  1442. .reg_bits = 8,
  1443. .val_bits = 16,
  1444. .max_register = WM8993_MAX_REGISTER,
  1445. .volatile_reg = wm8993_volatile,
  1446. .readable_reg = wm8993_readable,
  1447. .cache_type = REGCACHE_RBTREE,
  1448. .reg_defaults = wm8993_reg_defaults,
  1449. .num_reg_defaults = ARRAY_SIZE(wm8993_reg_defaults),
  1450. };
  1451. static struct snd_soc_codec_driver soc_codec_dev_wm8993 = {
  1452. .probe = wm8993_probe,
  1453. .remove = wm8993_remove,
  1454. .suspend = wm8993_suspend,
  1455. .resume = wm8993_resume,
  1456. .set_bias_level = wm8993_set_bias_level,
  1457. };
  1458. static __devinit int wm8993_i2c_probe(struct i2c_client *i2c,
  1459. const struct i2c_device_id *id)
  1460. {
  1461. struct wm8993_priv *wm8993;
  1462. unsigned int reg;
  1463. int ret, i;
  1464. wm8993 = devm_kzalloc(&i2c->dev, sizeof(struct wm8993_priv),
  1465. GFP_KERNEL);
  1466. if (wm8993 == NULL)
  1467. return -ENOMEM;
  1468. wm8993->dev = &i2c->dev;
  1469. init_completion(&wm8993->fll_lock);
  1470. wm8993->regmap = regmap_init_i2c(i2c, &wm8993_regmap);
  1471. if (IS_ERR(wm8993->regmap)) {
  1472. ret = PTR_ERR(wm8993->regmap);
  1473. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  1474. return ret;
  1475. }
  1476. i2c_set_clientdata(i2c, wm8993);
  1477. for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
  1478. wm8993->supplies[i].supply = wm8993_supply_names[i];
  1479. ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8993->supplies),
  1480. wm8993->supplies);
  1481. if (ret != 0) {
  1482. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  1483. goto err;
  1484. }
  1485. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  1486. wm8993->supplies);
  1487. if (ret != 0) {
  1488. dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
  1489. goto err_get;
  1490. }
  1491. ret = regmap_read(wm8993->regmap, WM8993_SOFTWARE_RESET, &reg);
  1492. if (ret != 0) {
  1493. dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
  1494. goto err_enable;
  1495. }
  1496. if (reg != 0x8993) {
  1497. dev_err(&i2c->dev, "Invalid ID register value %x\n", reg);
  1498. ret = -EINVAL;
  1499. goto err_enable;
  1500. }
  1501. ret = regmap_write(wm8993->regmap, WM8993_SOFTWARE_RESET, 0xffff);
  1502. if (ret != 0)
  1503. goto err_enable;
  1504. if (i2c->irq) {
  1505. /* Put GPIO1 into interrupt mode (only GPIO1 can output IRQ) */
  1506. ret = regmap_update_bits(wm8993->regmap, WM8993_GPIO1,
  1507. WM8993_GPIO1_PD |
  1508. WM8993_GPIO1_SEL_MASK, 7);
  1509. if (ret != 0)
  1510. goto err_enable;
  1511. ret = request_threaded_irq(i2c->irq, NULL, wm8993_irq,
  1512. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1513. "wm8993", wm8993);
  1514. if (ret != 0)
  1515. goto err_enable;
  1516. }
  1517. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1518. regcache_cache_only(wm8993->regmap, true);
  1519. ret = snd_soc_register_codec(&i2c->dev,
  1520. &soc_codec_dev_wm8993, &wm8993_dai, 1);
  1521. if (ret != 0) {
  1522. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  1523. goto err_irq;
  1524. }
  1525. return 0;
  1526. err_irq:
  1527. if (i2c->irq)
  1528. free_irq(i2c->irq, wm8993);
  1529. err_enable:
  1530. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1531. err_get:
  1532. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1533. err:
  1534. regmap_exit(wm8993->regmap);
  1535. return ret;
  1536. }
  1537. static __devexit int wm8993_i2c_remove(struct i2c_client *i2c)
  1538. {
  1539. struct wm8993_priv *wm8993 = i2c_get_clientdata(i2c);
  1540. snd_soc_unregister_codec(&i2c->dev);
  1541. if (i2c->irq)
  1542. free_irq(i2c->irq, wm8993);
  1543. regmap_exit(wm8993->regmap);
  1544. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1545. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1546. return 0;
  1547. }
  1548. static const struct i2c_device_id wm8993_i2c_id[] = {
  1549. { "wm8993", 0 },
  1550. { }
  1551. };
  1552. MODULE_DEVICE_TABLE(i2c, wm8993_i2c_id);
  1553. static struct i2c_driver wm8993_i2c_driver = {
  1554. .driver = {
  1555. .name = "wm8993",
  1556. .owner = THIS_MODULE,
  1557. },
  1558. .probe = wm8993_i2c_probe,
  1559. .remove = __devexit_p(wm8993_i2c_remove),
  1560. .id_table = wm8993_i2c_id,
  1561. };
  1562. static int __init wm8993_modinit(void)
  1563. {
  1564. int ret = 0;
  1565. ret = i2c_add_driver(&wm8993_i2c_driver);
  1566. if (ret != 0) {
  1567. pr_err("WM8993: Unable to register I2C driver: %d\n",
  1568. ret);
  1569. }
  1570. return ret;
  1571. }
  1572. module_init(wm8993_modinit);
  1573. static void __exit wm8993_exit(void)
  1574. {
  1575. i2c_del_driver(&wm8993_i2c_driver);
  1576. }
  1577. module_exit(wm8993_exit);
  1578. MODULE_DESCRIPTION("ASoC WM8993 driver");
  1579. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1580. MODULE_LICENSE("GPL");