wm8993.c 48 KB

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