wm8993.c 46 KB

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