wm8904.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. /*
  2. * wm8904.c -- WM8904 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009-12 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/consumer.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/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <sound/wm8904.h>
  29. #include "wm8904.h"
  30. enum wm8904_type {
  31. WM8904,
  32. WM8912,
  33. };
  34. #define WM8904_NUM_DCS_CHANNELS 4
  35. #define WM8904_NUM_SUPPLIES 5
  36. static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
  37. "DCVDD",
  38. "DBVDD",
  39. "AVDD",
  40. "CPVDD",
  41. "MICVDD",
  42. };
  43. /* codec private data */
  44. struct wm8904_priv {
  45. struct regmap *regmap;
  46. enum wm8904_type devtype;
  47. struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
  48. struct wm8904_pdata *pdata;
  49. int deemph;
  50. /* Platform provided DRC configuration */
  51. const char **drc_texts;
  52. int drc_cfg;
  53. struct soc_enum drc_enum;
  54. /* Platform provided ReTune mobile configuration */
  55. int num_retune_mobile_texts;
  56. const char **retune_mobile_texts;
  57. int retune_mobile_cfg;
  58. struct soc_enum retune_mobile_enum;
  59. /* FLL setup */
  60. int fll_src;
  61. int fll_fref;
  62. int fll_fout;
  63. /* Clocking configuration */
  64. unsigned int mclk_rate;
  65. int sysclk_src;
  66. unsigned int sysclk_rate;
  67. int tdm_width;
  68. int tdm_slots;
  69. int bclk;
  70. int fs;
  71. /* DC servo configuration - cached offset values */
  72. int dcs_state[WM8904_NUM_DCS_CHANNELS];
  73. };
  74. static const struct reg_default wm8904_reg_defaults[] = {
  75. { 4, 0x0018 }, /* R4 - Bias Control 0 */
  76. { 5, 0x0000 }, /* R5 - VMID Control 0 */
  77. { 6, 0x0000 }, /* R6 - Mic Bias Control 0 */
  78. { 7, 0x0000 }, /* R7 - Mic Bias Control 1 */
  79. { 8, 0x0001 }, /* R8 - Analogue DAC 0 */
  80. { 9, 0x9696 }, /* R9 - mic Filter Control */
  81. { 10, 0x0001 }, /* R10 - Analogue ADC 0 */
  82. { 12, 0x0000 }, /* R12 - Power Management 0 */
  83. { 14, 0x0000 }, /* R14 - Power Management 2 */
  84. { 15, 0x0000 }, /* R15 - Power Management 3 */
  85. { 18, 0x0000 }, /* R18 - Power Management 6 */
  86. { 20, 0x945E }, /* R20 - Clock Rates 0 */
  87. { 21, 0x0C05 }, /* R21 - Clock Rates 1 */
  88. { 22, 0x0006 }, /* R22 - Clock Rates 2 */
  89. { 24, 0x0050 }, /* R24 - Audio Interface 0 */
  90. { 25, 0x000A }, /* R25 - Audio Interface 1 */
  91. { 26, 0x00E4 }, /* R26 - Audio Interface 2 */
  92. { 27, 0x0040 }, /* R27 - Audio Interface 3 */
  93. { 30, 0x00C0 }, /* R30 - DAC Digital Volume Left */
  94. { 31, 0x00C0 }, /* R31 - DAC Digital Volume Right */
  95. { 32, 0x0000 }, /* R32 - DAC Digital 0 */
  96. { 33, 0x0008 }, /* R33 - DAC Digital 1 */
  97. { 36, 0x00C0 }, /* R36 - ADC Digital Volume Left */
  98. { 37, 0x00C0 }, /* R37 - ADC Digital Volume Right */
  99. { 38, 0x0010 }, /* R38 - ADC Digital 0 */
  100. { 39, 0x0000 }, /* R39 - Digital Microphone 0 */
  101. { 40, 0x01AF }, /* R40 - DRC 0 */
  102. { 41, 0x3248 }, /* R41 - DRC 1 */
  103. { 42, 0x0000 }, /* R42 - DRC 2 */
  104. { 43, 0x0000 }, /* R43 - DRC 3 */
  105. { 44, 0x0085 }, /* R44 - Analogue Left Input 0 */
  106. { 45, 0x0085 }, /* R45 - Analogue Right Input 0 */
  107. { 46, 0x0044 }, /* R46 - Analogue Left Input 1 */
  108. { 47, 0x0044 }, /* R47 - Analogue Right Input 1 */
  109. { 57, 0x002D }, /* R57 - Analogue OUT1 Left */
  110. { 58, 0x002D }, /* R58 - Analogue OUT1 Right */
  111. { 59, 0x0039 }, /* R59 - Analogue OUT2 Left */
  112. { 60, 0x0039 }, /* R60 - Analogue OUT2 Right */
  113. { 61, 0x0000 }, /* R61 - Analogue OUT12 ZC */
  114. { 67, 0x0000 }, /* R67 - DC Servo 0 */
  115. { 69, 0xAAAA }, /* R69 - DC Servo 2 */
  116. { 71, 0xAAAA }, /* R71 - DC Servo 4 */
  117. { 72, 0xAAAA }, /* R72 - DC Servo 5 */
  118. { 90, 0x0000 }, /* R90 - Analogue HP 0 */
  119. { 94, 0x0000 }, /* R94 - Analogue Lineout 0 */
  120. { 98, 0x0000 }, /* R98 - Charge Pump 0 */
  121. { 104, 0x0004 }, /* R104 - Class W 0 */
  122. { 108, 0x0000 }, /* R108 - Write Sequencer 0 */
  123. { 109, 0x0000 }, /* R109 - Write Sequencer 1 */
  124. { 110, 0x0000 }, /* R110 - Write Sequencer 2 */
  125. { 111, 0x0000 }, /* R111 - Write Sequencer 3 */
  126. { 112, 0x0000 }, /* R112 - Write Sequencer 4 */
  127. { 116, 0x0000 }, /* R116 - FLL Control 1 */
  128. { 117, 0x0007 }, /* R117 - FLL Control 2 */
  129. { 118, 0x0000 }, /* R118 - FLL Control 3 */
  130. { 119, 0x2EE0 }, /* R119 - FLL Control 4 */
  131. { 120, 0x0004 }, /* R120 - FLL Control 5 */
  132. { 121, 0x0014 }, /* R121 - GPIO Control 1 */
  133. { 122, 0x0010 }, /* R122 - GPIO Control 2 */
  134. { 123, 0x0010 }, /* R123 - GPIO Control 3 */
  135. { 124, 0x0000 }, /* R124 - GPIO Control 4 */
  136. { 126, 0x0000 }, /* R126 - Digital Pulls */
  137. { 128, 0xFFFF }, /* R128 - Interrupt Status Mask */
  138. { 129, 0x0000 }, /* R129 - Interrupt Polarity */
  139. { 130, 0x0000 }, /* R130 - Interrupt Debounce */
  140. { 134, 0x0000 }, /* R134 - EQ1 */
  141. { 135, 0x000C }, /* R135 - EQ2 */
  142. { 136, 0x000C }, /* R136 - EQ3 */
  143. { 137, 0x000C }, /* R137 - EQ4 */
  144. { 138, 0x000C }, /* R138 - EQ5 */
  145. { 139, 0x000C }, /* R139 - EQ6 */
  146. { 140, 0x0FCA }, /* R140 - EQ7 */
  147. { 141, 0x0400 }, /* R141 - EQ8 */
  148. { 142, 0x00D8 }, /* R142 - EQ9 */
  149. { 143, 0x1EB5 }, /* R143 - EQ10 */
  150. { 144, 0xF145 }, /* R144 - EQ11 */
  151. { 145, 0x0B75 }, /* R145 - EQ12 */
  152. { 146, 0x01C5 }, /* R146 - EQ13 */
  153. { 147, 0x1C58 }, /* R147 - EQ14 */
  154. { 148, 0xF373 }, /* R148 - EQ15 */
  155. { 149, 0x0A54 }, /* R149 - EQ16 */
  156. { 150, 0x0558 }, /* R150 - EQ17 */
  157. { 151, 0x168E }, /* R151 - EQ18 */
  158. { 152, 0xF829 }, /* R152 - EQ19 */
  159. { 153, 0x07AD }, /* R153 - EQ20 */
  160. { 154, 0x1103 }, /* R154 - EQ21 */
  161. { 155, 0x0564 }, /* R155 - EQ22 */
  162. { 156, 0x0559 }, /* R156 - EQ23 */
  163. { 157, 0x4000 }, /* R157 - EQ24 */
  164. { 161, 0x0000 }, /* R161 - Control Interface Test 1 */
  165. { 204, 0x0000 }, /* R204 - Analogue Output Bias 0 */
  166. { 247, 0x0000 }, /* R247 - FLL NCO Test 0 */
  167. { 248, 0x0019 }, /* R248 - FLL NCO Test 1 */
  168. };
  169. static bool wm8904_volatile_register(struct device *dev, unsigned int reg)
  170. {
  171. switch (reg) {
  172. case WM8904_SW_RESET_AND_ID:
  173. case WM8904_REVISION:
  174. case WM8904_DC_SERVO_1:
  175. case WM8904_DC_SERVO_6:
  176. case WM8904_DC_SERVO_7:
  177. case WM8904_DC_SERVO_8:
  178. case WM8904_DC_SERVO_9:
  179. case WM8904_DC_SERVO_READBACK_0:
  180. case WM8904_INTERRUPT_STATUS:
  181. return true;
  182. default:
  183. return false;
  184. }
  185. }
  186. static bool wm8904_readable_register(struct device *dev, unsigned int reg)
  187. {
  188. switch (reg) {
  189. case WM8904_SW_RESET_AND_ID:
  190. case WM8904_REVISION:
  191. case WM8904_BIAS_CONTROL_0:
  192. case WM8904_VMID_CONTROL_0:
  193. case WM8904_MIC_BIAS_CONTROL_0:
  194. case WM8904_MIC_BIAS_CONTROL_1:
  195. case WM8904_ANALOGUE_DAC_0:
  196. case WM8904_MIC_FILTER_CONTROL:
  197. case WM8904_ANALOGUE_ADC_0:
  198. case WM8904_POWER_MANAGEMENT_0:
  199. case WM8904_POWER_MANAGEMENT_2:
  200. case WM8904_POWER_MANAGEMENT_3:
  201. case WM8904_POWER_MANAGEMENT_6:
  202. case WM8904_CLOCK_RATES_0:
  203. case WM8904_CLOCK_RATES_1:
  204. case WM8904_CLOCK_RATES_2:
  205. case WM8904_AUDIO_INTERFACE_0:
  206. case WM8904_AUDIO_INTERFACE_1:
  207. case WM8904_AUDIO_INTERFACE_2:
  208. case WM8904_AUDIO_INTERFACE_3:
  209. case WM8904_DAC_DIGITAL_VOLUME_LEFT:
  210. case WM8904_DAC_DIGITAL_VOLUME_RIGHT:
  211. case WM8904_DAC_DIGITAL_0:
  212. case WM8904_DAC_DIGITAL_1:
  213. case WM8904_ADC_DIGITAL_VOLUME_LEFT:
  214. case WM8904_ADC_DIGITAL_VOLUME_RIGHT:
  215. case WM8904_ADC_DIGITAL_0:
  216. case WM8904_DIGITAL_MICROPHONE_0:
  217. case WM8904_DRC_0:
  218. case WM8904_DRC_1:
  219. case WM8904_DRC_2:
  220. case WM8904_DRC_3:
  221. case WM8904_ANALOGUE_LEFT_INPUT_0:
  222. case WM8904_ANALOGUE_RIGHT_INPUT_0:
  223. case WM8904_ANALOGUE_LEFT_INPUT_1:
  224. case WM8904_ANALOGUE_RIGHT_INPUT_1:
  225. case WM8904_ANALOGUE_OUT1_LEFT:
  226. case WM8904_ANALOGUE_OUT1_RIGHT:
  227. case WM8904_ANALOGUE_OUT2_LEFT:
  228. case WM8904_ANALOGUE_OUT2_RIGHT:
  229. case WM8904_ANALOGUE_OUT12_ZC:
  230. case WM8904_DC_SERVO_0:
  231. case WM8904_DC_SERVO_1:
  232. case WM8904_DC_SERVO_2:
  233. case WM8904_DC_SERVO_4:
  234. case WM8904_DC_SERVO_5:
  235. case WM8904_DC_SERVO_6:
  236. case WM8904_DC_SERVO_7:
  237. case WM8904_DC_SERVO_8:
  238. case WM8904_DC_SERVO_9:
  239. case WM8904_DC_SERVO_READBACK_0:
  240. case WM8904_ANALOGUE_HP_0:
  241. case WM8904_ANALOGUE_LINEOUT_0:
  242. case WM8904_CHARGE_PUMP_0:
  243. case WM8904_CLASS_W_0:
  244. case WM8904_WRITE_SEQUENCER_0:
  245. case WM8904_WRITE_SEQUENCER_1:
  246. case WM8904_WRITE_SEQUENCER_2:
  247. case WM8904_WRITE_SEQUENCER_3:
  248. case WM8904_WRITE_SEQUENCER_4:
  249. case WM8904_FLL_CONTROL_1:
  250. case WM8904_FLL_CONTROL_2:
  251. case WM8904_FLL_CONTROL_3:
  252. case WM8904_FLL_CONTROL_4:
  253. case WM8904_FLL_CONTROL_5:
  254. case WM8904_GPIO_CONTROL_1:
  255. case WM8904_GPIO_CONTROL_2:
  256. case WM8904_GPIO_CONTROL_3:
  257. case WM8904_GPIO_CONTROL_4:
  258. case WM8904_DIGITAL_PULLS:
  259. case WM8904_INTERRUPT_STATUS:
  260. case WM8904_INTERRUPT_STATUS_MASK:
  261. case WM8904_INTERRUPT_POLARITY:
  262. case WM8904_INTERRUPT_DEBOUNCE:
  263. case WM8904_EQ1:
  264. case WM8904_EQ2:
  265. case WM8904_EQ3:
  266. case WM8904_EQ4:
  267. case WM8904_EQ5:
  268. case WM8904_EQ6:
  269. case WM8904_EQ7:
  270. case WM8904_EQ8:
  271. case WM8904_EQ9:
  272. case WM8904_EQ10:
  273. case WM8904_EQ11:
  274. case WM8904_EQ12:
  275. case WM8904_EQ13:
  276. case WM8904_EQ14:
  277. case WM8904_EQ15:
  278. case WM8904_EQ16:
  279. case WM8904_EQ17:
  280. case WM8904_EQ18:
  281. case WM8904_EQ19:
  282. case WM8904_EQ20:
  283. case WM8904_EQ21:
  284. case WM8904_EQ22:
  285. case WM8904_EQ23:
  286. case WM8904_EQ24:
  287. case WM8904_CONTROL_INTERFACE_TEST_1:
  288. case WM8904_ADC_TEST_0:
  289. case WM8904_ANALOGUE_OUTPUT_BIAS_0:
  290. case WM8904_FLL_NCO_TEST_0:
  291. case WM8904_FLL_NCO_TEST_1:
  292. return true;
  293. default:
  294. return true;
  295. }
  296. }
  297. static int wm8904_configure_clocking(struct snd_soc_codec *codec)
  298. {
  299. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  300. unsigned int clock0, clock2, rate;
  301. /* Gate the clock while we're updating to avoid misclocking */
  302. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  303. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  304. WM8904_SYSCLK_SRC, 0);
  305. /* This should be done on init() for bypass paths */
  306. switch (wm8904->sysclk_src) {
  307. case WM8904_CLK_MCLK:
  308. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
  309. clock2 &= ~WM8904_SYSCLK_SRC;
  310. rate = wm8904->mclk_rate;
  311. /* Ensure the FLL is stopped */
  312. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  313. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  314. break;
  315. case WM8904_CLK_FLL:
  316. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  317. wm8904->fll_fout);
  318. clock2 |= WM8904_SYSCLK_SRC;
  319. rate = wm8904->fll_fout;
  320. break;
  321. default:
  322. dev_err(codec->dev, "System clock not configured\n");
  323. return -EINVAL;
  324. }
  325. /* SYSCLK shouldn't be over 13.5MHz */
  326. if (rate > 13500000) {
  327. clock0 = WM8904_MCLK_DIV;
  328. wm8904->sysclk_rate = rate / 2;
  329. } else {
  330. clock0 = 0;
  331. wm8904->sysclk_rate = rate;
  332. }
  333. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
  334. clock0);
  335. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  336. WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
  337. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
  338. return 0;
  339. }
  340. static void wm8904_set_drc(struct snd_soc_codec *codec)
  341. {
  342. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  343. struct wm8904_pdata *pdata = wm8904->pdata;
  344. int save, i;
  345. /* Save any enables; the configuration should clear them. */
  346. save = snd_soc_read(codec, WM8904_DRC_0);
  347. for (i = 0; i < WM8904_DRC_REGS; i++)
  348. snd_soc_update_bits(codec, WM8904_DRC_0 + i, 0xffff,
  349. pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
  350. /* Reenable the DRC */
  351. snd_soc_update_bits(codec, WM8904_DRC_0,
  352. WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
  353. }
  354. static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
  355. struct snd_ctl_elem_value *ucontrol)
  356. {
  357. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  358. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  359. struct wm8904_pdata *pdata = wm8904->pdata;
  360. int value = ucontrol->value.integer.value[0];
  361. if (value >= pdata->num_drc_cfgs)
  362. return -EINVAL;
  363. wm8904->drc_cfg = value;
  364. wm8904_set_drc(codec);
  365. return 0;
  366. }
  367. static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
  368. struct snd_ctl_elem_value *ucontrol)
  369. {
  370. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  371. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  372. ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
  373. return 0;
  374. }
  375. static void wm8904_set_retune_mobile(struct snd_soc_codec *codec)
  376. {
  377. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  378. struct wm8904_pdata *pdata = wm8904->pdata;
  379. int best, best_val, save, i, cfg;
  380. if (!pdata || !wm8904->num_retune_mobile_texts)
  381. return;
  382. /* Find the version of the currently selected configuration
  383. * with the nearest sample rate. */
  384. cfg = wm8904->retune_mobile_cfg;
  385. best = 0;
  386. best_val = INT_MAX;
  387. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  388. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  389. wm8904->retune_mobile_texts[cfg]) == 0 &&
  390. abs(pdata->retune_mobile_cfgs[i].rate
  391. - wm8904->fs) < best_val) {
  392. best = i;
  393. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  394. - wm8904->fs);
  395. }
  396. }
  397. dev_dbg(codec->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
  398. pdata->retune_mobile_cfgs[best].name,
  399. pdata->retune_mobile_cfgs[best].rate,
  400. wm8904->fs);
  401. /* The EQ will be disabled while reconfiguring it, remember the
  402. * current configuration.
  403. */
  404. save = snd_soc_read(codec, WM8904_EQ1);
  405. for (i = 0; i < WM8904_EQ_REGS; i++)
  406. snd_soc_update_bits(codec, WM8904_EQ1 + i, 0xffff,
  407. pdata->retune_mobile_cfgs[best].regs[i]);
  408. snd_soc_update_bits(codec, WM8904_EQ1, WM8904_EQ_ENA, save);
  409. }
  410. static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  414. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  415. struct wm8904_pdata *pdata = wm8904->pdata;
  416. int value = ucontrol->value.integer.value[0];
  417. if (value >= pdata->num_retune_mobile_cfgs)
  418. return -EINVAL;
  419. wm8904->retune_mobile_cfg = value;
  420. wm8904_set_retune_mobile(codec);
  421. return 0;
  422. }
  423. static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  424. struct snd_ctl_elem_value *ucontrol)
  425. {
  426. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  427. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  428. ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
  429. return 0;
  430. }
  431. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  432. static int wm8904_set_deemph(struct snd_soc_codec *codec)
  433. {
  434. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  435. int val, i, best;
  436. /* If we're using deemphasis select the nearest available sample
  437. * rate.
  438. */
  439. if (wm8904->deemph) {
  440. best = 1;
  441. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  442. if (abs(deemph_settings[i] - wm8904->fs) <
  443. abs(deemph_settings[best] - wm8904->fs))
  444. best = i;
  445. }
  446. val = best << WM8904_DEEMPH_SHIFT;
  447. } else {
  448. val = 0;
  449. }
  450. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  451. return snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  452. WM8904_DEEMPH_MASK, val);
  453. }
  454. static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_value *ucontrol)
  456. {
  457. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  458. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  459. ucontrol->value.enumerated.item[0] = wm8904->deemph;
  460. return 0;
  461. }
  462. static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
  463. struct snd_ctl_elem_value *ucontrol)
  464. {
  465. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  466. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  467. int deemph = ucontrol->value.enumerated.item[0];
  468. if (deemph > 1)
  469. return -EINVAL;
  470. wm8904->deemph = deemph;
  471. return wm8904_set_deemph(codec);
  472. }
  473. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  474. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  475. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  476. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  477. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  478. static const char *input_mode_text[] = {
  479. "Single-Ended", "Differential Line", "Differential Mic"
  480. };
  481. static const struct soc_enum lin_mode =
  482. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  483. static const struct soc_enum rin_mode =
  484. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  485. static const char *hpf_mode_text[] = {
  486. "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
  487. };
  488. static const struct soc_enum hpf_mode =
  489. SOC_ENUM_SINGLE(WM8904_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
  490. static int wm8904_adc_osr_put(struct snd_kcontrol *kcontrol,
  491. struct snd_ctl_elem_value *ucontrol)
  492. {
  493. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  494. unsigned int val;
  495. int ret;
  496. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  497. if (ret < 0)
  498. return ret;
  499. if (ucontrol->value.integer.value[0])
  500. val = 0;
  501. else
  502. val = WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5;
  503. snd_soc_update_bits(codec, WM8904_ADC_TEST_0,
  504. WM8904_ADC_128_OSR_TST_MODE | WM8904_ADC_BIASX1P5,
  505. val);
  506. return ret;
  507. }
  508. static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
  509. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
  510. WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
  511. SOC_ENUM("Left Caputure Mode", lin_mode),
  512. SOC_ENUM("Right Capture Mode", rin_mode),
  513. /* No TLV since it depends on mode */
  514. SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
  515. WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
  516. SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
  517. WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 1),
  518. SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
  519. SOC_ENUM("High Pass Filter Mode", hpf_mode),
  520. SOC_SINGLE_EXT("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0,
  521. snd_soc_get_volsw, wm8904_adc_osr_put),
  522. };
  523. static const char *drc_path_text[] = {
  524. "ADC", "DAC"
  525. };
  526. static const struct soc_enum drc_path =
  527. SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
  528. static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
  529. SOC_SINGLE_TLV("Digital Playback Boost Volume",
  530. WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
  531. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
  532. WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  533. SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
  534. WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
  535. SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
  536. WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
  537. SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
  538. WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
  539. SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
  540. WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
  541. SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
  542. WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
  543. SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
  544. WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
  545. SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
  546. SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
  547. SOC_ENUM("DRC Path", drc_path),
  548. SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
  549. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  550. wm8904_get_deemph, wm8904_put_deemph),
  551. };
  552. static const struct snd_kcontrol_new wm8904_snd_controls[] = {
  553. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
  554. sidetone_tlv),
  555. };
  556. static const struct snd_kcontrol_new wm8904_eq_controls[] = {
  557. SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
  558. SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
  559. SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
  560. SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
  561. SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
  562. };
  563. static int cp_event(struct snd_soc_dapm_widget *w,
  564. struct snd_kcontrol *kcontrol, int event)
  565. {
  566. BUG_ON(event != SND_SOC_DAPM_POST_PMU);
  567. /* Maximum startup time */
  568. udelay(500);
  569. return 0;
  570. }
  571. static int sysclk_event(struct snd_soc_dapm_widget *w,
  572. struct snd_kcontrol *kcontrol, int event)
  573. {
  574. struct snd_soc_codec *codec = w->codec;
  575. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  576. switch (event) {
  577. case SND_SOC_DAPM_PRE_PMU:
  578. /* If we're using the FLL then we only start it when
  579. * required; we assume that the configuration has been
  580. * done previously and all we need to do is kick it
  581. * off.
  582. */
  583. switch (wm8904->sysclk_src) {
  584. case WM8904_CLK_FLL:
  585. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  586. WM8904_FLL_OSC_ENA,
  587. WM8904_FLL_OSC_ENA);
  588. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  589. WM8904_FLL_ENA,
  590. WM8904_FLL_ENA);
  591. break;
  592. default:
  593. break;
  594. }
  595. break;
  596. case SND_SOC_DAPM_POST_PMD:
  597. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  598. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  599. break;
  600. }
  601. return 0;
  602. }
  603. static int out_pga_event(struct snd_soc_dapm_widget *w,
  604. struct snd_kcontrol *kcontrol, int event)
  605. {
  606. struct snd_soc_codec *codec = w->codec;
  607. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  608. int reg, val;
  609. int dcs_mask;
  610. int dcs_l, dcs_r;
  611. int dcs_l_reg, dcs_r_reg;
  612. int timeout;
  613. int pwr_reg;
  614. /* This code is shared between HP and LINEOUT; we do all our
  615. * power management in stereo pairs to avoid latency issues so
  616. * we reuse shift to identify which rather than strcmp() the
  617. * name. */
  618. reg = w->shift;
  619. switch (reg) {
  620. case WM8904_ANALOGUE_HP_0:
  621. pwr_reg = WM8904_POWER_MANAGEMENT_2;
  622. dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
  623. dcs_r_reg = WM8904_DC_SERVO_8;
  624. dcs_l_reg = WM8904_DC_SERVO_9;
  625. dcs_l = 0;
  626. dcs_r = 1;
  627. break;
  628. case WM8904_ANALOGUE_LINEOUT_0:
  629. pwr_reg = WM8904_POWER_MANAGEMENT_3;
  630. dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
  631. dcs_r_reg = WM8904_DC_SERVO_6;
  632. dcs_l_reg = WM8904_DC_SERVO_7;
  633. dcs_l = 2;
  634. dcs_r = 3;
  635. break;
  636. default:
  637. BUG();
  638. return -EINVAL;
  639. }
  640. switch (event) {
  641. case SND_SOC_DAPM_PRE_PMU:
  642. /* Power on the PGAs */
  643. snd_soc_update_bits(codec, pwr_reg,
  644. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  645. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
  646. /* Power on the amplifier */
  647. snd_soc_update_bits(codec, reg,
  648. WM8904_HPL_ENA | WM8904_HPR_ENA,
  649. WM8904_HPL_ENA | WM8904_HPR_ENA);
  650. /* Enable the first stage */
  651. snd_soc_update_bits(codec, reg,
  652. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
  653. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
  654. /* Power up the DC servo */
  655. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  656. dcs_mask, dcs_mask);
  657. /* Either calibrate the DC servo or restore cached state
  658. * if we have that.
  659. */
  660. if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
  661. dev_dbg(codec->dev, "Restoring DC servo state\n");
  662. snd_soc_write(codec, dcs_l_reg,
  663. wm8904->dcs_state[dcs_l]);
  664. snd_soc_write(codec, dcs_r_reg,
  665. wm8904->dcs_state[dcs_r]);
  666. snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
  667. timeout = 20;
  668. } else {
  669. dev_dbg(codec->dev, "Calibrating DC servo\n");
  670. snd_soc_write(codec, WM8904_DC_SERVO_1,
  671. dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
  672. timeout = 500;
  673. }
  674. /* Wait for DC servo to complete */
  675. dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
  676. do {
  677. val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
  678. if ((val & dcs_mask) == dcs_mask)
  679. break;
  680. msleep(1);
  681. } while (--timeout);
  682. if ((val & dcs_mask) != dcs_mask)
  683. dev_warn(codec->dev, "DC servo timed out\n");
  684. else
  685. dev_dbg(codec->dev, "DC servo ready\n");
  686. /* Enable the output stage */
  687. snd_soc_update_bits(codec, reg,
  688. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  689. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
  690. break;
  691. case SND_SOC_DAPM_POST_PMU:
  692. /* Unshort the output itself */
  693. snd_soc_update_bits(codec, reg,
  694. WM8904_HPL_RMV_SHORT |
  695. WM8904_HPR_RMV_SHORT,
  696. WM8904_HPL_RMV_SHORT |
  697. WM8904_HPR_RMV_SHORT);
  698. break;
  699. case SND_SOC_DAPM_PRE_PMD:
  700. /* Short the output */
  701. snd_soc_update_bits(codec, reg,
  702. WM8904_HPL_RMV_SHORT |
  703. WM8904_HPR_RMV_SHORT, 0);
  704. break;
  705. case SND_SOC_DAPM_POST_PMD:
  706. /* Cache the DC servo configuration; this will be
  707. * invalidated if we change the configuration. */
  708. wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
  709. wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
  710. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  711. dcs_mask, 0);
  712. /* Disable the amplifier input and output stages */
  713. snd_soc_update_bits(codec, reg,
  714. WM8904_HPL_ENA | WM8904_HPR_ENA |
  715. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
  716. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  717. 0);
  718. /* PGAs too */
  719. snd_soc_update_bits(codec, pwr_reg,
  720. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  721. 0);
  722. break;
  723. }
  724. return 0;
  725. }
  726. static const char *lin_text[] = {
  727. "IN1L", "IN2L", "IN3L"
  728. };
  729. static const struct soc_enum lin_enum =
  730. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
  731. static const struct snd_kcontrol_new lin_mux =
  732. SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
  733. static const struct soc_enum lin_inv_enum =
  734. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
  735. static const struct snd_kcontrol_new lin_inv_mux =
  736. SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
  737. static const char *rin_text[] = {
  738. "IN1R", "IN2R", "IN3R"
  739. };
  740. static const struct soc_enum rin_enum =
  741. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
  742. static const struct snd_kcontrol_new rin_mux =
  743. SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
  744. static const struct soc_enum rin_inv_enum =
  745. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
  746. static const struct snd_kcontrol_new rin_inv_mux =
  747. SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
  748. static const char *aif_text[] = {
  749. "Left", "Right"
  750. };
  751. static const struct soc_enum aifoutl_enum =
  752. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
  753. static const struct snd_kcontrol_new aifoutl_mux =
  754. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  755. static const struct soc_enum aifoutr_enum =
  756. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
  757. static const struct snd_kcontrol_new aifoutr_mux =
  758. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  759. static const struct soc_enum aifinl_enum =
  760. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
  761. static const struct snd_kcontrol_new aifinl_mux =
  762. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  763. static const struct soc_enum aifinr_enum =
  764. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
  765. static const struct snd_kcontrol_new aifinr_mux =
  766. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  767. static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
  768. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
  769. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  770. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
  771. SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
  772. };
  773. static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
  774. SND_SOC_DAPM_INPUT("IN1L"),
  775. SND_SOC_DAPM_INPUT("IN1R"),
  776. SND_SOC_DAPM_INPUT("IN2L"),
  777. SND_SOC_DAPM_INPUT("IN2R"),
  778. SND_SOC_DAPM_INPUT("IN3L"),
  779. SND_SOC_DAPM_INPUT("IN3R"),
  780. SND_SOC_DAPM_SUPPLY("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
  781. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
  782. SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  783. &lin_inv_mux),
  784. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
  785. SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  786. &rin_inv_mux),
  787. SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
  788. NULL, 0),
  789. SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
  790. NULL, 0),
  791. SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
  792. SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
  793. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  794. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  795. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  796. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  797. };
  798. static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
  799. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  800. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  801. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  802. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  803. SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
  804. SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
  805. SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
  806. SND_SOC_DAPM_POST_PMU),
  807. SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  808. SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  809. SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  810. SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  811. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
  812. 0, NULL, 0, out_pga_event,
  813. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  814. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  815. SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
  816. 0, NULL, 0, out_pga_event,
  817. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  818. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  819. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  820. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  821. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  822. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  823. };
  824. static const char *out_mux_text[] = {
  825. "DAC", "Bypass"
  826. };
  827. static const struct soc_enum hpl_enum =
  828. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
  829. static const struct snd_kcontrol_new hpl_mux =
  830. SOC_DAPM_ENUM("HPL Mux", hpl_enum);
  831. static const struct soc_enum hpr_enum =
  832. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
  833. static const struct snd_kcontrol_new hpr_mux =
  834. SOC_DAPM_ENUM("HPR Mux", hpr_enum);
  835. static const struct soc_enum linel_enum =
  836. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
  837. static const struct snd_kcontrol_new linel_mux =
  838. SOC_DAPM_ENUM("LINEL Mux", linel_enum);
  839. static const struct soc_enum liner_enum =
  840. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
  841. static const struct snd_kcontrol_new liner_mux =
  842. SOC_DAPM_ENUM("LINER Mux", liner_enum);
  843. static const char *sidetone_text[] = {
  844. "None", "Left", "Right"
  845. };
  846. static const struct soc_enum dacl_sidetone_enum =
  847. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
  848. static const struct snd_kcontrol_new dacl_sidetone_mux =
  849. SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
  850. static const struct soc_enum dacr_sidetone_enum =
  851. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
  852. static const struct snd_kcontrol_new dacr_sidetone_mux =
  853. SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
  854. static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
  855. SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
  856. SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  857. SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  858. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
  859. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
  860. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  861. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  862. SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
  863. SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
  864. };
  865. static const struct snd_soc_dapm_route core_intercon[] = {
  866. { "CLK_DSP", NULL, "SYSCLK" },
  867. { "TOCLK", NULL, "SYSCLK" },
  868. };
  869. static const struct snd_soc_dapm_route adc_intercon[] = {
  870. { "Left Capture Mux", "IN1L", "IN1L" },
  871. { "Left Capture Mux", "IN2L", "IN2L" },
  872. { "Left Capture Mux", "IN3L", "IN3L" },
  873. { "Left Capture Inverting Mux", "IN1L", "IN1L" },
  874. { "Left Capture Inverting Mux", "IN2L", "IN2L" },
  875. { "Left Capture Inverting Mux", "IN3L", "IN3L" },
  876. { "Right Capture Mux", "IN1R", "IN1R" },
  877. { "Right Capture Mux", "IN2R", "IN2R" },
  878. { "Right Capture Mux", "IN3R", "IN3R" },
  879. { "Right Capture Inverting Mux", "IN1R", "IN1R" },
  880. { "Right Capture Inverting Mux", "IN2R", "IN2R" },
  881. { "Right Capture Inverting Mux", "IN3R", "IN3R" },
  882. { "Left Capture PGA", NULL, "Left Capture Mux" },
  883. { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
  884. { "Right Capture PGA", NULL, "Right Capture Mux" },
  885. { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
  886. { "AIFOUTL", "Left", "ADCL" },
  887. { "AIFOUTL", "Right", "ADCR" },
  888. { "AIFOUTR", "Left", "ADCL" },
  889. { "AIFOUTR", "Right", "ADCR" },
  890. { "ADCL", NULL, "CLK_DSP" },
  891. { "ADCL", NULL, "Left Capture PGA" },
  892. { "ADCR", NULL, "CLK_DSP" },
  893. { "ADCR", NULL, "Right Capture PGA" },
  894. };
  895. static const struct snd_soc_dapm_route dac_intercon[] = {
  896. { "DACL", "Right", "AIFINR" },
  897. { "DACL", "Left", "AIFINL" },
  898. { "DACL", NULL, "CLK_DSP" },
  899. { "DACR", "Right", "AIFINR" },
  900. { "DACR", "Left", "AIFINL" },
  901. { "DACR", NULL, "CLK_DSP" },
  902. { "Charge pump", NULL, "SYSCLK" },
  903. { "Headphone Output", NULL, "HPL PGA" },
  904. { "Headphone Output", NULL, "HPR PGA" },
  905. { "Headphone Output", NULL, "Charge pump" },
  906. { "Headphone Output", NULL, "TOCLK" },
  907. { "Line Output", NULL, "LINEL PGA" },
  908. { "Line Output", NULL, "LINER PGA" },
  909. { "Line Output", NULL, "Charge pump" },
  910. { "Line Output", NULL, "TOCLK" },
  911. { "HPOUTL", NULL, "Headphone Output" },
  912. { "HPOUTR", NULL, "Headphone Output" },
  913. { "LINEOUTL", NULL, "Line Output" },
  914. { "LINEOUTR", NULL, "Line Output" },
  915. };
  916. static const struct snd_soc_dapm_route wm8904_intercon[] = {
  917. { "Left Sidetone", "Left", "ADCL" },
  918. { "Left Sidetone", "Right", "ADCR" },
  919. { "DACL", NULL, "Left Sidetone" },
  920. { "Right Sidetone", "Left", "ADCL" },
  921. { "Right Sidetone", "Right", "ADCR" },
  922. { "DACR", NULL, "Right Sidetone" },
  923. { "Left Bypass", NULL, "Class G" },
  924. { "Left Bypass", NULL, "Left Capture PGA" },
  925. { "Right Bypass", NULL, "Class G" },
  926. { "Right Bypass", NULL, "Right Capture PGA" },
  927. { "HPL Mux", "DAC", "DACL" },
  928. { "HPL Mux", "Bypass", "Left Bypass" },
  929. { "HPR Mux", "DAC", "DACR" },
  930. { "HPR Mux", "Bypass", "Right Bypass" },
  931. { "LINEL Mux", "DAC", "DACL" },
  932. { "LINEL Mux", "Bypass", "Left Bypass" },
  933. { "LINER Mux", "DAC", "DACR" },
  934. { "LINER Mux", "Bypass", "Right Bypass" },
  935. { "HPL PGA", NULL, "HPL Mux" },
  936. { "HPR PGA", NULL, "HPR Mux" },
  937. { "LINEL PGA", NULL, "LINEL Mux" },
  938. { "LINER PGA", NULL, "LINER Mux" },
  939. };
  940. static const struct snd_soc_dapm_route wm8912_intercon[] = {
  941. { "HPL PGA", NULL, "DACL" },
  942. { "HPR PGA", NULL, "DACR" },
  943. { "LINEL PGA", NULL, "DACL" },
  944. { "LINER PGA", NULL, "DACR" },
  945. };
  946. static int wm8904_add_widgets(struct snd_soc_codec *codec)
  947. {
  948. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  949. struct snd_soc_dapm_context *dapm = &codec->dapm;
  950. snd_soc_dapm_new_controls(dapm, wm8904_core_dapm_widgets,
  951. ARRAY_SIZE(wm8904_core_dapm_widgets));
  952. snd_soc_dapm_add_routes(dapm, core_intercon,
  953. ARRAY_SIZE(core_intercon));
  954. switch (wm8904->devtype) {
  955. case WM8904:
  956. snd_soc_add_codec_controls(codec, wm8904_adc_snd_controls,
  957. ARRAY_SIZE(wm8904_adc_snd_controls));
  958. snd_soc_add_codec_controls(codec, wm8904_dac_snd_controls,
  959. ARRAY_SIZE(wm8904_dac_snd_controls));
  960. snd_soc_add_codec_controls(codec, wm8904_snd_controls,
  961. ARRAY_SIZE(wm8904_snd_controls));
  962. snd_soc_dapm_new_controls(dapm, wm8904_adc_dapm_widgets,
  963. ARRAY_SIZE(wm8904_adc_dapm_widgets));
  964. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  965. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  966. snd_soc_dapm_new_controls(dapm, wm8904_dapm_widgets,
  967. ARRAY_SIZE(wm8904_dapm_widgets));
  968. snd_soc_dapm_add_routes(dapm, adc_intercon,
  969. ARRAY_SIZE(adc_intercon));
  970. snd_soc_dapm_add_routes(dapm, dac_intercon,
  971. ARRAY_SIZE(dac_intercon));
  972. snd_soc_dapm_add_routes(dapm, wm8904_intercon,
  973. ARRAY_SIZE(wm8904_intercon));
  974. break;
  975. case WM8912:
  976. snd_soc_add_codec_controls(codec, wm8904_dac_snd_controls,
  977. ARRAY_SIZE(wm8904_dac_snd_controls));
  978. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  979. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  980. snd_soc_dapm_add_routes(dapm, dac_intercon,
  981. ARRAY_SIZE(dac_intercon));
  982. snd_soc_dapm_add_routes(dapm, wm8912_intercon,
  983. ARRAY_SIZE(wm8912_intercon));
  984. break;
  985. }
  986. return 0;
  987. }
  988. static struct {
  989. int ratio;
  990. unsigned int clk_sys_rate;
  991. } clk_sys_rates[] = {
  992. { 64, 0 },
  993. { 128, 1 },
  994. { 192, 2 },
  995. { 256, 3 },
  996. { 384, 4 },
  997. { 512, 5 },
  998. { 786, 6 },
  999. { 1024, 7 },
  1000. { 1408, 8 },
  1001. { 1536, 9 },
  1002. };
  1003. static struct {
  1004. int rate;
  1005. int sample_rate;
  1006. } sample_rates[] = {
  1007. { 8000, 0 },
  1008. { 11025, 1 },
  1009. { 12000, 1 },
  1010. { 16000, 2 },
  1011. { 22050, 3 },
  1012. { 24000, 3 },
  1013. { 32000, 4 },
  1014. { 44100, 5 },
  1015. { 48000, 5 },
  1016. };
  1017. static struct {
  1018. int div; /* *10 due to .5s */
  1019. int bclk_div;
  1020. } bclk_divs[] = {
  1021. { 10, 0 },
  1022. { 15, 1 },
  1023. { 20, 2 },
  1024. { 30, 3 },
  1025. { 40, 4 },
  1026. { 50, 5 },
  1027. { 55, 6 },
  1028. { 60, 7 },
  1029. { 80, 8 },
  1030. { 100, 9 },
  1031. { 110, 10 },
  1032. { 120, 11 },
  1033. { 160, 12 },
  1034. { 200, 13 },
  1035. { 220, 14 },
  1036. { 240, 16 },
  1037. { 200, 17 },
  1038. { 320, 18 },
  1039. { 440, 19 },
  1040. { 480, 20 },
  1041. };
  1042. static int wm8904_hw_params(struct snd_pcm_substream *substream,
  1043. struct snd_pcm_hw_params *params,
  1044. struct snd_soc_dai *dai)
  1045. {
  1046. struct snd_soc_codec *codec = dai->codec;
  1047. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1048. int ret, i, best, best_val, cur_val;
  1049. unsigned int aif1 = 0;
  1050. unsigned int aif2 = 0;
  1051. unsigned int aif3 = 0;
  1052. unsigned int clock1 = 0;
  1053. unsigned int dac_digital1 = 0;
  1054. /* What BCLK do we need? */
  1055. wm8904->fs = params_rate(params);
  1056. if (wm8904->tdm_slots) {
  1057. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1058. wm8904->tdm_slots, wm8904->tdm_width);
  1059. wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
  1060. wm8904->tdm_width, 2,
  1061. wm8904->tdm_slots);
  1062. } else {
  1063. wm8904->bclk = snd_soc_params_to_bclk(params);
  1064. }
  1065. switch (params_format(params)) {
  1066. case SNDRV_PCM_FORMAT_S16_LE:
  1067. break;
  1068. case SNDRV_PCM_FORMAT_S20_3LE:
  1069. aif1 |= 0x40;
  1070. break;
  1071. case SNDRV_PCM_FORMAT_S24_LE:
  1072. aif1 |= 0x80;
  1073. break;
  1074. case SNDRV_PCM_FORMAT_S32_LE:
  1075. aif1 |= 0xc0;
  1076. break;
  1077. default:
  1078. return -EINVAL;
  1079. }
  1080. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
  1081. ret = wm8904_configure_clocking(codec);
  1082. if (ret != 0)
  1083. return ret;
  1084. /* Select nearest CLK_SYS_RATE */
  1085. best = 0;
  1086. best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
  1087. - wm8904->fs);
  1088. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1089. cur_val = abs((wm8904->sysclk_rate /
  1090. clk_sys_rates[i].ratio) - wm8904->fs);
  1091. if (cur_val < best_val) {
  1092. best = i;
  1093. best_val = cur_val;
  1094. }
  1095. }
  1096. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1097. clk_sys_rates[best].ratio);
  1098. clock1 |= (clk_sys_rates[best].clk_sys_rate
  1099. << WM8904_CLK_SYS_RATE_SHIFT);
  1100. /* SAMPLE_RATE */
  1101. best = 0;
  1102. best_val = abs(wm8904->fs - sample_rates[0].rate);
  1103. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1104. /* Closest match */
  1105. cur_val = abs(wm8904->fs - sample_rates[i].rate);
  1106. if (cur_val < best_val) {
  1107. best = i;
  1108. best_val = cur_val;
  1109. }
  1110. }
  1111. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1112. sample_rates[best].rate);
  1113. clock1 |= (sample_rates[best].sample_rate
  1114. << WM8904_SAMPLE_RATE_SHIFT);
  1115. /* Enable sloping stopband filter for low sample rates */
  1116. if (wm8904->fs <= 24000)
  1117. dac_digital1 |= WM8904_DAC_SB_FILT;
  1118. /* BCLK_DIV */
  1119. best = 0;
  1120. best_val = INT_MAX;
  1121. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1122. cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
  1123. - wm8904->bclk;
  1124. if (cur_val < 0) /* Table is sorted */
  1125. break;
  1126. if (cur_val < best_val) {
  1127. best = i;
  1128. best_val = cur_val;
  1129. }
  1130. }
  1131. wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
  1132. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1133. bclk_divs[best].div, wm8904->bclk);
  1134. aif2 |= bclk_divs[best].bclk_div;
  1135. /* LRCLK is a simple fraction of BCLK */
  1136. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
  1137. aif3 |= wm8904->bclk / wm8904->fs;
  1138. /* Apply the settings */
  1139. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  1140. WM8904_DAC_SB_FILT, dac_digital1);
  1141. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1142. WM8904_AIF_WL_MASK, aif1);
  1143. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
  1144. WM8904_BCLK_DIV_MASK, aif2);
  1145. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1146. WM8904_LRCLK_RATE_MASK, aif3);
  1147. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
  1148. WM8904_SAMPLE_RATE_MASK |
  1149. WM8904_CLK_SYS_RATE_MASK, clock1);
  1150. /* Update filters for the new settings */
  1151. wm8904_set_retune_mobile(codec);
  1152. wm8904_set_deemph(codec);
  1153. return 0;
  1154. }
  1155. static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1156. unsigned int freq, int dir)
  1157. {
  1158. struct snd_soc_codec *codec = dai->codec;
  1159. struct wm8904_priv *priv = snd_soc_codec_get_drvdata(codec);
  1160. switch (clk_id) {
  1161. case WM8904_CLK_MCLK:
  1162. priv->sysclk_src = clk_id;
  1163. priv->mclk_rate = freq;
  1164. break;
  1165. case WM8904_CLK_FLL:
  1166. priv->sysclk_src = clk_id;
  1167. break;
  1168. default:
  1169. return -EINVAL;
  1170. }
  1171. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  1172. wm8904_configure_clocking(codec);
  1173. return 0;
  1174. }
  1175. static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1176. {
  1177. struct snd_soc_codec *codec = dai->codec;
  1178. unsigned int aif1 = 0;
  1179. unsigned int aif3 = 0;
  1180. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1181. case SND_SOC_DAIFMT_CBS_CFS:
  1182. break;
  1183. case SND_SOC_DAIFMT_CBS_CFM:
  1184. aif3 |= WM8904_LRCLK_DIR;
  1185. break;
  1186. case SND_SOC_DAIFMT_CBM_CFS:
  1187. aif1 |= WM8904_BCLK_DIR;
  1188. break;
  1189. case SND_SOC_DAIFMT_CBM_CFM:
  1190. aif1 |= WM8904_BCLK_DIR;
  1191. aif3 |= WM8904_LRCLK_DIR;
  1192. break;
  1193. default:
  1194. return -EINVAL;
  1195. }
  1196. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1197. case SND_SOC_DAIFMT_DSP_B:
  1198. aif1 |= WM8904_AIF_LRCLK_INV;
  1199. case SND_SOC_DAIFMT_DSP_A:
  1200. aif1 |= 0x3;
  1201. break;
  1202. case SND_SOC_DAIFMT_I2S:
  1203. aif1 |= 0x2;
  1204. break;
  1205. case SND_SOC_DAIFMT_RIGHT_J:
  1206. break;
  1207. case SND_SOC_DAIFMT_LEFT_J:
  1208. aif1 |= 0x1;
  1209. break;
  1210. default:
  1211. return -EINVAL;
  1212. }
  1213. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1214. case SND_SOC_DAIFMT_DSP_A:
  1215. case SND_SOC_DAIFMT_DSP_B:
  1216. /* frame inversion not valid for DSP modes */
  1217. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1218. case SND_SOC_DAIFMT_NB_NF:
  1219. break;
  1220. case SND_SOC_DAIFMT_IB_NF:
  1221. aif1 |= WM8904_AIF_BCLK_INV;
  1222. break;
  1223. default:
  1224. return -EINVAL;
  1225. }
  1226. break;
  1227. case SND_SOC_DAIFMT_I2S:
  1228. case SND_SOC_DAIFMT_RIGHT_J:
  1229. case SND_SOC_DAIFMT_LEFT_J:
  1230. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1231. case SND_SOC_DAIFMT_NB_NF:
  1232. break;
  1233. case SND_SOC_DAIFMT_IB_IF:
  1234. aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
  1235. break;
  1236. case SND_SOC_DAIFMT_IB_NF:
  1237. aif1 |= WM8904_AIF_BCLK_INV;
  1238. break;
  1239. case SND_SOC_DAIFMT_NB_IF:
  1240. aif1 |= WM8904_AIF_LRCLK_INV;
  1241. break;
  1242. default:
  1243. return -EINVAL;
  1244. }
  1245. break;
  1246. default:
  1247. return -EINVAL;
  1248. }
  1249. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1250. WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
  1251. WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
  1252. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1253. WM8904_LRCLK_DIR, aif3);
  1254. return 0;
  1255. }
  1256. static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1257. unsigned int rx_mask, int slots, int slot_width)
  1258. {
  1259. struct snd_soc_codec *codec = dai->codec;
  1260. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1261. int aif1 = 0;
  1262. /* Don't need to validate anything if we're turning off TDM */
  1263. if (slots == 0)
  1264. goto out;
  1265. /* Note that we allow configurations we can't handle ourselves -
  1266. * for example, we can generate clocks for slots 2 and up even if
  1267. * we can't use those slots ourselves.
  1268. */
  1269. aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
  1270. switch (rx_mask) {
  1271. case 3:
  1272. break;
  1273. case 0xc:
  1274. aif1 |= WM8904_AIFADC_TDM_CHAN;
  1275. break;
  1276. default:
  1277. return -EINVAL;
  1278. }
  1279. switch (tx_mask) {
  1280. case 3:
  1281. break;
  1282. case 0xc:
  1283. aif1 |= WM8904_AIFDAC_TDM_CHAN;
  1284. break;
  1285. default:
  1286. return -EINVAL;
  1287. }
  1288. out:
  1289. wm8904->tdm_width = slot_width;
  1290. wm8904->tdm_slots = slots / 2;
  1291. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1292. WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
  1293. WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
  1294. return 0;
  1295. }
  1296. struct _fll_div {
  1297. u16 fll_fratio;
  1298. u16 fll_outdiv;
  1299. u16 fll_clk_ref_div;
  1300. u16 n;
  1301. u16 k;
  1302. };
  1303. /* The size in bits of the FLL divide multiplied by 10
  1304. * to allow rounding later */
  1305. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1306. static struct {
  1307. unsigned int min;
  1308. unsigned int max;
  1309. u16 fll_fratio;
  1310. int ratio;
  1311. } fll_fratios[] = {
  1312. { 0, 64000, 4, 16 },
  1313. { 64000, 128000, 3, 8 },
  1314. { 128000, 256000, 2, 4 },
  1315. { 256000, 1000000, 1, 2 },
  1316. { 1000000, 13500000, 0, 1 },
  1317. };
  1318. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1319. unsigned int Fout)
  1320. {
  1321. u64 Kpart;
  1322. unsigned int K, Ndiv, Nmod, target;
  1323. unsigned int div;
  1324. int i;
  1325. /* Fref must be <=13.5MHz */
  1326. div = 1;
  1327. fll_div->fll_clk_ref_div = 0;
  1328. while ((Fref / div) > 13500000) {
  1329. div *= 2;
  1330. fll_div->fll_clk_ref_div++;
  1331. if (div > 8) {
  1332. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1333. Fref);
  1334. return -EINVAL;
  1335. }
  1336. }
  1337. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  1338. /* Apply the division for our remaining calculations */
  1339. Fref /= div;
  1340. /* Fvco should be 90-100MHz; don't check the upper bound */
  1341. div = 4;
  1342. while (Fout * div < 90000000) {
  1343. div++;
  1344. if (div > 64) {
  1345. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1346. Fout);
  1347. return -EINVAL;
  1348. }
  1349. }
  1350. target = Fout * div;
  1351. fll_div->fll_outdiv = div - 1;
  1352. pr_debug("Fvco=%dHz\n", target);
  1353. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  1354. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1355. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1356. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1357. target /= fll_fratios[i].ratio;
  1358. break;
  1359. }
  1360. }
  1361. if (i == ARRAY_SIZE(fll_fratios)) {
  1362. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1363. return -EINVAL;
  1364. }
  1365. /* Now, calculate N.K */
  1366. Ndiv = target / Fref;
  1367. fll_div->n = Ndiv;
  1368. Nmod = target % Fref;
  1369. pr_debug("Nmod=%d\n", Nmod);
  1370. /* Calculate fractional part - scale up so we can round. */
  1371. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1372. do_div(Kpart, Fref);
  1373. K = Kpart & 0xFFFFFFFF;
  1374. if ((K % 10) >= 5)
  1375. K += 5;
  1376. /* Move down to proper range now rounding is done */
  1377. fll_div->k = K / 10;
  1378. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  1379. fll_div->n, fll_div->k,
  1380. fll_div->fll_fratio, fll_div->fll_outdiv,
  1381. fll_div->fll_clk_ref_div);
  1382. return 0;
  1383. }
  1384. static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  1385. unsigned int Fref, unsigned int Fout)
  1386. {
  1387. struct snd_soc_codec *codec = dai->codec;
  1388. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1389. struct _fll_div fll_div;
  1390. int ret, val;
  1391. int clock2, fll1;
  1392. /* Any change? */
  1393. if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
  1394. Fout == wm8904->fll_fout)
  1395. return 0;
  1396. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  1397. if (Fout == 0) {
  1398. dev_dbg(codec->dev, "FLL disabled\n");
  1399. wm8904->fll_fref = 0;
  1400. wm8904->fll_fout = 0;
  1401. /* Gate SYSCLK to avoid glitches */
  1402. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1403. WM8904_CLK_SYS_ENA, 0);
  1404. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1405. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1406. goto out;
  1407. }
  1408. /* Validate the FLL ID */
  1409. switch (source) {
  1410. case WM8904_FLL_MCLK:
  1411. case WM8904_FLL_LRCLK:
  1412. case WM8904_FLL_BCLK:
  1413. ret = fll_factors(&fll_div, Fref, Fout);
  1414. if (ret != 0)
  1415. return ret;
  1416. break;
  1417. case WM8904_FLL_FREE_RUNNING:
  1418. dev_dbg(codec->dev, "Using free running FLL\n");
  1419. /* Force 12MHz and output/4 for now */
  1420. Fout = 12000000;
  1421. Fref = 12000000;
  1422. memset(&fll_div, 0, sizeof(fll_div));
  1423. fll_div.fll_outdiv = 3;
  1424. break;
  1425. default:
  1426. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  1427. return -EINVAL;
  1428. }
  1429. /* Save current state then disable the FLL and SYSCLK to avoid
  1430. * misclocking */
  1431. fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
  1432. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1433. WM8904_CLK_SYS_ENA, 0);
  1434. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1435. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1436. /* Unlock forced oscilator control to switch it on/off */
  1437. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1438. WM8904_USER_KEY, WM8904_USER_KEY);
  1439. if (fll_id == WM8904_FLL_FREE_RUNNING) {
  1440. val = WM8904_FLL_FRC_NCO;
  1441. } else {
  1442. val = 0;
  1443. }
  1444. snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
  1445. val);
  1446. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1447. WM8904_USER_KEY, 0);
  1448. switch (fll_id) {
  1449. case WM8904_FLL_MCLK:
  1450. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1451. WM8904_FLL_CLK_REF_SRC_MASK, 0);
  1452. break;
  1453. case WM8904_FLL_LRCLK:
  1454. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1455. WM8904_FLL_CLK_REF_SRC_MASK, 1);
  1456. break;
  1457. case WM8904_FLL_BCLK:
  1458. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1459. WM8904_FLL_CLK_REF_SRC_MASK, 2);
  1460. break;
  1461. }
  1462. if (fll_div.k)
  1463. val = WM8904_FLL_FRACN_ENA;
  1464. else
  1465. val = 0;
  1466. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1467. WM8904_FLL_FRACN_ENA, val);
  1468. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
  1469. WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
  1470. (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
  1471. (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
  1472. snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
  1473. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
  1474. fll_div.n << WM8904_FLL_N_SHIFT);
  1475. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1476. WM8904_FLL_CLK_REF_DIV_MASK,
  1477. fll_div.fll_clk_ref_div
  1478. << WM8904_FLL_CLK_REF_DIV_SHIFT);
  1479. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  1480. wm8904->fll_fref = Fref;
  1481. wm8904->fll_fout = Fout;
  1482. wm8904->fll_src = source;
  1483. /* Enable the FLL if it was previously active */
  1484. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1485. WM8904_FLL_OSC_ENA, fll1);
  1486. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1487. WM8904_FLL_ENA, fll1);
  1488. out:
  1489. /* Reenable SYSCLK if it was previously active */
  1490. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1491. WM8904_CLK_SYS_ENA, clock2);
  1492. return 0;
  1493. }
  1494. static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1495. {
  1496. struct snd_soc_codec *codec = codec_dai->codec;
  1497. int val;
  1498. if (mute)
  1499. val = WM8904_DAC_MUTE;
  1500. else
  1501. val = 0;
  1502. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
  1503. return 0;
  1504. }
  1505. static int wm8904_set_bias_level(struct snd_soc_codec *codec,
  1506. enum snd_soc_bias_level level)
  1507. {
  1508. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1509. int ret;
  1510. switch (level) {
  1511. case SND_SOC_BIAS_ON:
  1512. break;
  1513. case SND_SOC_BIAS_PREPARE:
  1514. /* VMID resistance 2*50k */
  1515. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1516. WM8904_VMID_RES_MASK,
  1517. 0x1 << WM8904_VMID_RES_SHIFT);
  1518. /* Normal bias current */
  1519. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1520. WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
  1521. break;
  1522. case SND_SOC_BIAS_STANDBY:
  1523. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1524. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1525. wm8904->supplies);
  1526. if (ret != 0) {
  1527. dev_err(codec->dev,
  1528. "Failed to enable supplies: %d\n",
  1529. ret);
  1530. return ret;
  1531. }
  1532. regcache_cache_only(wm8904->regmap, false);
  1533. regcache_sync(wm8904->regmap);
  1534. /* Enable bias */
  1535. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1536. WM8904_BIAS_ENA, WM8904_BIAS_ENA);
  1537. /* Enable VMID, VMID buffering, 2*5k resistance */
  1538. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1539. WM8904_VMID_ENA |
  1540. WM8904_VMID_RES_MASK,
  1541. WM8904_VMID_ENA |
  1542. 0x3 << WM8904_VMID_RES_SHIFT);
  1543. /* Let VMID ramp */
  1544. msleep(1);
  1545. }
  1546. /* Maintain VMID with 2*250k */
  1547. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1548. WM8904_VMID_RES_MASK,
  1549. 0x2 << WM8904_VMID_RES_SHIFT);
  1550. /* Bias current *0.5 */
  1551. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1552. WM8904_ISEL_MASK, 0);
  1553. break;
  1554. case SND_SOC_BIAS_OFF:
  1555. /* Turn off VMID */
  1556. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1557. WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
  1558. /* Stop bias generation */
  1559. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1560. WM8904_BIAS_ENA, 0);
  1561. regcache_cache_only(wm8904->regmap, true);
  1562. regcache_mark_dirty(wm8904->regmap);
  1563. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
  1564. wm8904->supplies);
  1565. break;
  1566. }
  1567. codec->dapm.bias_level = level;
  1568. return 0;
  1569. }
  1570. #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
  1571. #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1572. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1573. static const struct snd_soc_dai_ops wm8904_dai_ops = {
  1574. .set_sysclk = wm8904_set_sysclk,
  1575. .set_fmt = wm8904_set_fmt,
  1576. .set_tdm_slot = wm8904_set_tdm_slot,
  1577. .set_pll = wm8904_set_fll,
  1578. .hw_params = wm8904_hw_params,
  1579. .digital_mute = wm8904_digital_mute,
  1580. };
  1581. static struct snd_soc_dai_driver wm8904_dai = {
  1582. .name = "wm8904-hifi",
  1583. .playback = {
  1584. .stream_name = "Playback",
  1585. .channels_min = 2,
  1586. .channels_max = 2,
  1587. .rates = WM8904_RATES,
  1588. .formats = WM8904_FORMATS,
  1589. },
  1590. .capture = {
  1591. .stream_name = "Capture",
  1592. .channels_min = 2,
  1593. .channels_max = 2,
  1594. .rates = WM8904_RATES,
  1595. .formats = WM8904_FORMATS,
  1596. },
  1597. .ops = &wm8904_dai_ops,
  1598. .symmetric_rates = 1,
  1599. };
  1600. static void wm8904_handle_retune_mobile_pdata(struct snd_soc_codec *codec)
  1601. {
  1602. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1603. struct wm8904_pdata *pdata = wm8904->pdata;
  1604. struct snd_kcontrol_new control =
  1605. SOC_ENUM_EXT("EQ Mode",
  1606. wm8904->retune_mobile_enum,
  1607. wm8904_get_retune_mobile_enum,
  1608. wm8904_put_retune_mobile_enum);
  1609. int ret, i, j;
  1610. const char **t;
  1611. /* We need an array of texts for the enum API but the number
  1612. * of texts is likely to be less than the number of
  1613. * configurations due to the sample rate dependency of the
  1614. * configurations. */
  1615. wm8904->num_retune_mobile_texts = 0;
  1616. wm8904->retune_mobile_texts = NULL;
  1617. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  1618. for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
  1619. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  1620. wm8904->retune_mobile_texts[j]) == 0)
  1621. break;
  1622. }
  1623. if (j != wm8904->num_retune_mobile_texts)
  1624. continue;
  1625. /* Expand the array... */
  1626. t = krealloc(wm8904->retune_mobile_texts,
  1627. sizeof(char *) *
  1628. (wm8904->num_retune_mobile_texts + 1),
  1629. GFP_KERNEL);
  1630. if (t == NULL)
  1631. continue;
  1632. /* ...store the new entry... */
  1633. t[wm8904->num_retune_mobile_texts] =
  1634. pdata->retune_mobile_cfgs[i].name;
  1635. /* ...and remember the new version. */
  1636. wm8904->num_retune_mobile_texts++;
  1637. wm8904->retune_mobile_texts = t;
  1638. }
  1639. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  1640. wm8904->num_retune_mobile_texts);
  1641. wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
  1642. wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
  1643. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1644. if (ret != 0)
  1645. dev_err(codec->dev,
  1646. "Failed to add ReTune Mobile control: %d\n", ret);
  1647. }
  1648. static void wm8904_handle_pdata(struct snd_soc_codec *codec)
  1649. {
  1650. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1651. struct wm8904_pdata *pdata = wm8904->pdata;
  1652. int ret, i;
  1653. if (!pdata) {
  1654. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1655. ARRAY_SIZE(wm8904_eq_controls));
  1656. return;
  1657. }
  1658. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  1659. if (pdata->num_drc_cfgs) {
  1660. struct snd_kcontrol_new control =
  1661. SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
  1662. wm8904_get_drc_enum, wm8904_put_drc_enum);
  1663. /* We need an array of texts for the enum API */
  1664. wm8904->drc_texts = kmalloc(sizeof(char *)
  1665. * pdata->num_drc_cfgs, GFP_KERNEL);
  1666. if (!wm8904->drc_texts) {
  1667. dev_err(codec->dev,
  1668. "Failed to allocate %d DRC config texts\n",
  1669. pdata->num_drc_cfgs);
  1670. return;
  1671. }
  1672. for (i = 0; i < pdata->num_drc_cfgs; i++)
  1673. wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
  1674. wm8904->drc_enum.max = pdata->num_drc_cfgs;
  1675. wm8904->drc_enum.texts = wm8904->drc_texts;
  1676. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1677. if (ret != 0)
  1678. dev_err(codec->dev,
  1679. "Failed to add DRC mode control: %d\n", ret);
  1680. wm8904_set_drc(codec);
  1681. }
  1682. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  1683. pdata->num_retune_mobile_cfgs);
  1684. if (pdata->num_retune_mobile_cfgs)
  1685. wm8904_handle_retune_mobile_pdata(codec);
  1686. else
  1687. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1688. ARRAY_SIZE(wm8904_eq_controls));
  1689. }
  1690. static int wm8904_probe(struct snd_soc_codec *codec)
  1691. {
  1692. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1693. int ret;
  1694. codec->control_data = wm8904->regmap;
  1695. switch (wm8904->devtype) {
  1696. case WM8904:
  1697. break;
  1698. case WM8912:
  1699. memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
  1700. break;
  1701. default:
  1702. dev_err(codec->dev, "Unknown device type %d\n",
  1703. wm8904->devtype);
  1704. return -EINVAL;
  1705. }
  1706. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  1707. if (ret != 0) {
  1708. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1709. return ret;
  1710. }
  1711. wm8904_handle_pdata(codec);
  1712. wm8904_add_widgets(codec);
  1713. return 0;
  1714. }
  1715. static int wm8904_remove(struct snd_soc_codec *codec)
  1716. {
  1717. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1718. kfree(wm8904->retune_mobile_texts);
  1719. kfree(wm8904->drc_texts);
  1720. return 0;
  1721. }
  1722. static struct snd_soc_codec_driver soc_codec_dev_wm8904 = {
  1723. .probe = wm8904_probe,
  1724. .remove = wm8904_remove,
  1725. .set_bias_level = wm8904_set_bias_level,
  1726. .idle_bias_off = true,
  1727. };
  1728. static const struct regmap_config wm8904_regmap = {
  1729. .reg_bits = 8,
  1730. .val_bits = 16,
  1731. .max_register = WM8904_MAX_REGISTER,
  1732. .volatile_reg = wm8904_volatile_register,
  1733. .readable_reg = wm8904_readable_register,
  1734. .cache_type = REGCACHE_RBTREE,
  1735. .reg_defaults = wm8904_reg_defaults,
  1736. .num_reg_defaults = ARRAY_SIZE(wm8904_reg_defaults),
  1737. };
  1738. static int wm8904_i2c_probe(struct i2c_client *i2c,
  1739. const struct i2c_device_id *id)
  1740. {
  1741. struct wm8904_priv *wm8904;
  1742. unsigned int val;
  1743. int ret, i;
  1744. wm8904 = devm_kzalloc(&i2c->dev, sizeof(struct wm8904_priv),
  1745. GFP_KERNEL);
  1746. if (wm8904 == NULL)
  1747. return -ENOMEM;
  1748. wm8904->regmap = devm_regmap_init_i2c(i2c, &wm8904_regmap);
  1749. if (IS_ERR(wm8904->regmap)) {
  1750. ret = PTR_ERR(wm8904->regmap);
  1751. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1752. ret);
  1753. return ret;
  1754. }
  1755. wm8904->devtype = id->driver_data;
  1756. i2c_set_clientdata(i2c, wm8904);
  1757. wm8904->pdata = i2c->dev.platform_data;
  1758. for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
  1759. wm8904->supplies[i].supply = wm8904_supply_names[i];
  1760. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8904->supplies),
  1761. wm8904->supplies);
  1762. if (ret != 0) {
  1763. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  1764. return ret;
  1765. }
  1766. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1767. wm8904->supplies);
  1768. if (ret != 0) {
  1769. dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
  1770. return ret;
  1771. }
  1772. ret = regmap_read(wm8904->regmap, WM8904_SW_RESET_AND_ID, &val);
  1773. if (ret < 0) {
  1774. dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
  1775. goto err_enable;
  1776. }
  1777. if (val != 0x8904) {
  1778. dev_err(&i2c->dev, "Device is not a WM8904, ID is %x\n", val);
  1779. ret = -EINVAL;
  1780. goto err_enable;
  1781. }
  1782. ret = regmap_read(wm8904->regmap, WM8904_REVISION, &val);
  1783. if (ret < 0) {
  1784. dev_err(&i2c->dev, "Failed to read device revision: %d\n",
  1785. ret);
  1786. goto err_enable;
  1787. }
  1788. dev_info(&i2c->dev, "revision %c\n", val + 'A');
  1789. ret = regmap_write(wm8904->regmap, WM8904_SW_RESET_AND_ID, 0);
  1790. if (ret < 0) {
  1791. dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
  1792. goto err_enable;
  1793. }
  1794. /* Change some default settings - latch VU and enable ZC */
  1795. regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_LEFT,
  1796. WM8904_ADC_VU, WM8904_ADC_VU);
  1797. regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
  1798. WM8904_ADC_VU, WM8904_ADC_VU);
  1799. regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_LEFT,
  1800. WM8904_DAC_VU, WM8904_DAC_VU);
  1801. regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
  1802. WM8904_DAC_VU, WM8904_DAC_VU);
  1803. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_LEFT,
  1804. WM8904_HPOUT_VU | WM8904_HPOUTLZC,
  1805. WM8904_HPOUT_VU | WM8904_HPOUTLZC);
  1806. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_RIGHT,
  1807. WM8904_HPOUT_VU | WM8904_HPOUTRZC,
  1808. WM8904_HPOUT_VU | WM8904_HPOUTRZC);
  1809. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_LEFT,
  1810. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
  1811. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
  1812. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_RIGHT,
  1813. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
  1814. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
  1815. regmap_update_bits(wm8904->regmap, WM8904_CLOCK_RATES_0,
  1816. WM8904_SR_MODE, 0);
  1817. /* Apply configuration from the platform data. */
  1818. if (wm8904->pdata) {
  1819. for (i = 0; i < WM8904_GPIO_REGS; i++) {
  1820. if (!wm8904->pdata->gpio_cfg[i])
  1821. continue;
  1822. regmap_update_bits(wm8904->regmap,
  1823. WM8904_GPIO_CONTROL_1 + i,
  1824. 0xffff,
  1825. wm8904->pdata->gpio_cfg[i]);
  1826. }
  1827. /* Zero is the default value for these anyway */
  1828. for (i = 0; i < WM8904_MIC_REGS; i++)
  1829. regmap_update_bits(wm8904->regmap,
  1830. WM8904_MIC_BIAS_CONTROL_0 + i,
  1831. 0xffff,
  1832. wm8904->pdata->mic_cfg[i]);
  1833. }
  1834. /* Set Class W by default - this will be managed by the Class
  1835. * G widget at runtime where bypass paths are available.
  1836. */
  1837. regmap_update_bits(wm8904->regmap, WM8904_CLASS_W_0,
  1838. WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
  1839. /* Use normal bias source */
  1840. regmap_update_bits(wm8904->regmap, WM8904_BIAS_CONTROL_0,
  1841. WM8904_POBCTRL, 0);
  1842. /* Can leave the device powered off until we need it */
  1843. regcache_cache_only(wm8904->regmap, true);
  1844. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1845. ret = snd_soc_register_codec(&i2c->dev,
  1846. &soc_codec_dev_wm8904, &wm8904_dai, 1);
  1847. if (ret != 0)
  1848. return ret;
  1849. return 0;
  1850. err_enable:
  1851. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1852. return ret;
  1853. }
  1854. static int wm8904_i2c_remove(struct i2c_client *client)
  1855. {
  1856. snd_soc_unregister_codec(&client->dev);
  1857. return 0;
  1858. }
  1859. static const struct i2c_device_id wm8904_i2c_id[] = {
  1860. { "wm8904", WM8904 },
  1861. { "wm8912", WM8912 },
  1862. { "wm8918", WM8904 }, /* Actually a subset, updates to follow */
  1863. { }
  1864. };
  1865. MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
  1866. static struct i2c_driver wm8904_i2c_driver = {
  1867. .driver = {
  1868. .name = "wm8904",
  1869. .owner = THIS_MODULE,
  1870. },
  1871. .probe = wm8904_i2c_probe,
  1872. .remove = wm8904_i2c_remove,
  1873. .id_table = wm8904_i2c_id,
  1874. };
  1875. module_i2c_driver(wm8904_i2c_driver);
  1876. MODULE_DESCRIPTION("ASoC WM8904 driver");
  1877. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1878. MODULE_LICENSE("GPL");