wm8993.c 50 KB

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