wm8904.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  521. .name = "ADC 128x OSR Switch",
  522. .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,
  523. .put = wm8904_adc_osr_put,
  524. .private_value = SOC_SINGLE_VALUE(WM8904_ANALOGUE_ADC_0, 0, 1, 0),
  525. },
  526. };
  527. static const char *drc_path_text[] = {
  528. "ADC", "DAC"
  529. };
  530. static const struct soc_enum drc_path =
  531. SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
  532. static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
  533. SOC_SINGLE_TLV("Digital Playback Boost Volume",
  534. WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
  535. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
  536. WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  537. SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
  538. WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
  539. SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
  540. WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
  541. SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
  542. WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
  543. SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
  544. WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
  545. SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
  546. WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
  547. SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
  548. WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
  549. SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
  550. SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
  551. SOC_ENUM("DRC Path", drc_path),
  552. SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
  553. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  554. wm8904_get_deemph, wm8904_put_deemph),
  555. };
  556. static const struct snd_kcontrol_new wm8904_snd_controls[] = {
  557. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
  558. sidetone_tlv),
  559. };
  560. static const struct snd_kcontrol_new wm8904_eq_controls[] = {
  561. SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
  562. SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
  563. SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
  564. SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
  565. SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
  566. };
  567. static int cp_event(struct snd_soc_dapm_widget *w,
  568. struct snd_kcontrol *kcontrol, int event)
  569. {
  570. BUG_ON(event != SND_SOC_DAPM_POST_PMU);
  571. /* Maximum startup time */
  572. udelay(500);
  573. return 0;
  574. }
  575. static int sysclk_event(struct snd_soc_dapm_widget *w,
  576. struct snd_kcontrol *kcontrol, int event)
  577. {
  578. struct snd_soc_codec *codec = w->codec;
  579. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  580. switch (event) {
  581. case SND_SOC_DAPM_PRE_PMU:
  582. /* If we're using the FLL then we only start it when
  583. * required; we assume that the configuration has been
  584. * done previously and all we need to do is kick it
  585. * off.
  586. */
  587. switch (wm8904->sysclk_src) {
  588. case WM8904_CLK_FLL:
  589. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  590. WM8904_FLL_OSC_ENA,
  591. WM8904_FLL_OSC_ENA);
  592. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  593. WM8904_FLL_ENA,
  594. WM8904_FLL_ENA);
  595. break;
  596. default:
  597. break;
  598. }
  599. break;
  600. case SND_SOC_DAPM_POST_PMD:
  601. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  602. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  603. break;
  604. }
  605. return 0;
  606. }
  607. static int out_pga_event(struct snd_soc_dapm_widget *w,
  608. struct snd_kcontrol *kcontrol, int event)
  609. {
  610. struct snd_soc_codec *codec = w->codec;
  611. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  612. int reg, val;
  613. int dcs_mask;
  614. int dcs_l, dcs_r;
  615. int dcs_l_reg, dcs_r_reg;
  616. int timeout;
  617. int pwr_reg;
  618. /* This code is shared between HP and LINEOUT; we do all our
  619. * power management in stereo pairs to avoid latency issues so
  620. * we reuse shift to identify which rather than strcmp() the
  621. * name. */
  622. reg = w->shift;
  623. switch (reg) {
  624. case WM8904_ANALOGUE_HP_0:
  625. pwr_reg = WM8904_POWER_MANAGEMENT_2;
  626. dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
  627. dcs_r_reg = WM8904_DC_SERVO_8;
  628. dcs_l_reg = WM8904_DC_SERVO_9;
  629. dcs_l = 0;
  630. dcs_r = 1;
  631. break;
  632. case WM8904_ANALOGUE_LINEOUT_0:
  633. pwr_reg = WM8904_POWER_MANAGEMENT_3;
  634. dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
  635. dcs_r_reg = WM8904_DC_SERVO_6;
  636. dcs_l_reg = WM8904_DC_SERVO_7;
  637. dcs_l = 2;
  638. dcs_r = 3;
  639. break;
  640. default:
  641. BUG();
  642. return -EINVAL;
  643. }
  644. switch (event) {
  645. case SND_SOC_DAPM_PRE_PMU:
  646. /* Power on the PGAs */
  647. snd_soc_update_bits(codec, pwr_reg,
  648. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  649. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
  650. /* Power on the amplifier */
  651. snd_soc_update_bits(codec, reg,
  652. WM8904_HPL_ENA | WM8904_HPR_ENA,
  653. WM8904_HPL_ENA | WM8904_HPR_ENA);
  654. /* Enable the first stage */
  655. snd_soc_update_bits(codec, reg,
  656. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
  657. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
  658. /* Power up the DC servo */
  659. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  660. dcs_mask, dcs_mask);
  661. /* Either calibrate the DC servo or restore cached state
  662. * if we have that.
  663. */
  664. if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
  665. dev_dbg(codec->dev, "Restoring DC servo state\n");
  666. snd_soc_write(codec, dcs_l_reg,
  667. wm8904->dcs_state[dcs_l]);
  668. snd_soc_write(codec, dcs_r_reg,
  669. wm8904->dcs_state[dcs_r]);
  670. snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
  671. timeout = 20;
  672. } else {
  673. dev_dbg(codec->dev, "Calibrating DC servo\n");
  674. snd_soc_write(codec, WM8904_DC_SERVO_1,
  675. dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
  676. timeout = 500;
  677. }
  678. /* Wait for DC servo to complete */
  679. dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
  680. do {
  681. val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
  682. if ((val & dcs_mask) == dcs_mask)
  683. break;
  684. msleep(1);
  685. } while (--timeout);
  686. if ((val & dcs_mask) != dcs_mask)
  687. dev_warn(codec->dev, "DC servo timed out\n");
  688. else
  689. dev_dbg(codec->dev, "DC servo ready\n");
  690. /* Enable the output stage */
  691. snd_soc_update_bits(codec, reg,
  692. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  693. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
  694. break;
  695. case SND_SOC_DAPM_POST_PMU:
  696. /* Unshort the output itself */
  697. snd_soc_update_bits(codec, reg,
  698. WM8904_HPL_RMV_SHORT |
  699. WM8904_HPR_RMV_SHORT,
  700. WM8904_HPL_RMV_SHORT |
  701. WM8904_HPR_RMV_SHORT);
  702. break;
  703. case SND_SOC_DAPM_PRE_PMD:
  704. /* Short the output */
  705. snd_soc_update_bits(codec, reg,
  706. WM8904_HPL_RMV_SHORT |
  707. WM8904_HPR_RMV_SHORT, 0);
  708. break;
  709. case SND_SOC_DAPM_POST_PMD:
  710. /* Cache the DC servo configuration; this will be
  711. * invalidated if we change the configuration. */
  712. wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
  713. wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
  714. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  715. dcs_mask, 0);
  716. /* Disable the amplifier input and output stages */
  717. snd_soc_update_bits(codec, reg,
  718. WM8904_HPL_ENA | WM8904_HPR_ENA |
  719. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
  720. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  721. 0);
  722. /* PGAs too */
  723. snd_soc_update_bits(codec, pwr_reg,
  724. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  725. 0);
  726. break;
  727. }
  728. return 0;
  729. }
  730. static const char *lin_text[] = {
  731. "IN1L", "IN2L", "IN3L"
  732. };
  733. static const struct soc_enum lin_enum =
  734. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
  735. static const struct snd_kcontrol_new lin_mux =
  736. SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
  737. static const struct soc_enum lin_inv_enum =
  738. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
  739. static const struct snd_kcontrol_new lin_inv_mux =
  740. SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
  741. static const char *rin_text[] = {
  742. "IN1R", "IN2R", "IN3R"
  743. };
  744. static const struct soc_enum rin_enum =
  745. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
  746. static const struct snd_kcontrol_new rin_mux =
  747. SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
  748. static const struct soc_enum rin_inv_enum =
  749. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
  750. static const struct snd_kcontrol_new rin_inv_mux =
  751. SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
  752. static const char *aif_text[] = {
  753. "Left", "Right"
  754. };
  755. static const struct soc_enum aifoutl_enum =
  756. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
  757. static const struct snd_kcontrol_new aifoutl_mux =
  758. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  759. static const struct soc_enum aifoutr_enum =
  760. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
  761. static const struct snd_kcontrol_new aifoutr_mux =
  762. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  763. static const struct soc_enum aifinl_enum =
  764. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
  765. static const struct snd_kcontrol_new aifinl_mux =
  766. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  767. static const struct soc_enum aifinr_enum =
  768. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
  769. static const struct snd_kcontrol_new aifinr_mux =
  770. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  771. static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
  772. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
  773. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  774. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
  775. SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
  776. };
  777. static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
  778. SND_SOC_DAPM_INPUT("IN1L"),
  779. SND_SOC_DAPM_INPUT("IN1R"),
  780. SND_SOC_DAPM_INPUT("IN2L"),
  781. SND_SOC_DAPM_INPUT("IN2R"),
  782. SND_SOC_DAPM_INPUT("IN3L"),
  783. SND_SOC_DAPM_INPUT("IN3R"),
  784. SND_SOC_DAPM_SUPPLY("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0, NULL, 0),
  785. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
  786. SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  787. &lin_inv_mux),
  788. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
  789. SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  790. &rin_inv_mux),
  791. SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
  792. NULL, 0),
  793. SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
  794. NULL, 0),
  795. SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
  796. SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
  797. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  798. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  799. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  800. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  801. };
  802. static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
  803. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  804. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  805. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  806. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  807. SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
  808. SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
  809. SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
  810. SND_SOC_DAPM_POST_PMU),
  811. SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  812. SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  813. SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  814. SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  815. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_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_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
  820. 0, NULL, 0, out_pga_event,
  821. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  822. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  823. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  824. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  825. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  826. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  827. };
  828. static const char *out_mux_text[] = {
  829. "DAC", "Bypass"
  830. };
  831. static const struct soc_enum hpl_enum =
  832. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
  833. static const struct snd_kcontrol_new hpl_mux =
  834. SOC_DAPM_ENUM("HPL Mux", hpl_enum);
  835. static const struct soc_enum hpr_enum =
  836. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
  837. static const struct snd_kcontrol_new hpr_mux =
  838. SOC_DAPM_ENUM("HPR Mux", hpr_enum);
  839. static const struct soc_enum linel_enum =
  840. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
  841. static const struct snd_kcontrol_new linel_mux =
  842. SOC_DAPM_ENUM("LINEL Mux", linel_enum);
  843. static const struct soc_enum liner_enum =
  844. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
  845. static const struct snd_kcontrol_new liner_mux =
  846. SOC_DAPM_ENUM("LINEL Mux", liner_enum);
  847. static const char *sidetone_text[] = {
  848. "None", "Left", "Right"
  849. };
  850. static const struct soc_enum dacl_sidetone_enum =
  851. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
  852. static const struct snd_kcontrol_new dacl_sidetone_mux =
  853. SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
  854. static const struct soc_enum dacr_sidetone_enum =
  855. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
  856. static const struct snd_kcontrol_new dacr_sidetone_mux =
  857. SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
  858. static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
  859. SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
  860. SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  861. SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  862. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
  863. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
  864. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  865. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  866. SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
  867. SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
  868. };
  869. static const struct snd_soc_dapm_route core_intercon[] = {
  870. { "CLK_DSP", NULL, "SYSCLK" },
  871. { "TOCLK", NULL, "SYSCLK" },
  872. };
  873. static const struct snd_soc_dapm_route adc_intercon[] = {
  874. { "Left Capture Mux", "IN1L", "IN1L" },
  875. { "Left Capture Mux", "IN2L", "IN2L" },
  876. { "Left Capture Mux", "IN3L", "IN3L" },
  877. { "Left Capture Inverting Mux", "IN1L", "IN1L" },
  878. { "Left Capture Inverting Mux", "IN2L", "IN2L" },
  879. { "Left Capture Inverting Mux", "IN3L", "IN3L" },
  880. { "Right Capture Mux", "IN1R", "IN1R" },
  881. { "Right Capture Mux", "IN2R", "IN2R" },
  882. { "Right Capture Mux", "IN3R", "IN3R" },
  883. { "Right Capture Inverting Mux", "IN1R", "IN1R" },
  884. { "Right Capture Inverting Mux", "IN2R", "IN2R" },
  885. { "Right Capture Inverting Mux", "IN3R", "IN3R" },
  886. { "Left Capture PGA", NULL, "Left Capture Mux" },
  887. { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
  888. { "Right Capture PGA", NULL, "Right Capture Mux" },
  889. { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
  890. { "AIFOUTL", "Left", "ADCL" },
  891. { "AIFOUTL", "Right", "ADCR" },
  892. { "AIFOUTR", "Left", "ADCL" },
  893. { "AIFOUTR", "Right", "ADCR" },
  894. { "ADCL", NULL, "CLK_DSP" },
  895. { "ADCL", NULL, "Left Capture PGA" },
  896. { "ADCR", NULL, "CLK_DSP" },
  897. { "ADCR", NULL, "Right Capture PGA" },
  898. };
  899. static const struct snd_soc_dapm_route dac_intercon[] = {
  900. { "DACL", "Right", "AIFINR" },
  901. { "DACL", "Left", "AIFINL" },
  902. { "DACL", NULL, "CLK_DSP" },
  903. { "DACR", "Right", "AIFINR" },
  904. { "DACR", "Left", "AIFINL" },
  905. { "DACR", NULL, "CLK_DSP" },
  906. { "Charge pump", NULL, "SYSCLK" },
  907. { "Headphone Output", NULL, "HPL PGA" },
  908. { "Headphone Output", NULL, "HPR PGA" },
  909. { "Headphone Output", NULL, "Charge pump" },
  910. { "Headphone Output", NULL, "TOCLK" },
  911. { "Line Output", NULL, "LINEL PGA" },
  912. { "Line Output", NULL, "LINER PGA" },
  913. { "Line Output", NULL, "Charge pump" },
  914. { "Line Output", NULL, "TOCLK" },
  915. { "HPOUTL", NULL, "Headphone Output" },
  916. { "HPOUTR", NULL, "Headphone Output" },
  917. { "LINEOUTL", NULL, "Line Output" },
  918. { "LINEOUTR", NULL, "Line Output" },
  919. };
  920. static const struct snd_soc_dapm_route wm8904_intercon[] = {
  921. { "Left Sidetone", "Left", "ADCL" },
  922. { "Left Sidetone", "Right", "ADCR" },
  923. { "DACL", NULL, "Left Sidetone" },
  924. { "Right Sidetone", "Left", "ADCL" },
  925. { "Right Sidetone", "Right", "ADCR" },
  926. { "DACR", NULL, "Right Sidetone" },
  927. { "Left Bypass", NULL, "Class G" },
  928. { "Left Bypass", NULL, "Left Capture PGA" },
  929. { "Right Bypass", NULL, "Class G" },
  930. { "Right Bypass", NULL, "Right Capture PGA" },
  931. { "HPL Mux", "DAC", "DACL" },
  932. { "HPL Mux", "Bypass", "Left Bypass" },
  933. { "HPR Mux", "DAC", "DACR" },
  934. { "HPR Mux", "Bypass", "Right Bypass" },
  935. { "LINEL Mux", "DAC", "DACL" },
  936. { "LINEL Mux", "Bypass", "Left Bypass" },
  937. { "LINER Mux", "DAC", "DACR" },
  938. { "LINER Mux", "Bypass", "Right Bypass" },
  939. { "HPL PGA", NULL, "HPL Mux" },
  940. { "HPR PGA", NULL, "HPR Mux" },
  941. { "LINEL PGA", NULL, "LINEL Mux" },
  942. { "LINER PGA", NULL, "LINER Mux" },
  943. };
  944. static const struct snd_soc_dapm_route wm8912_intercon[] = {
  945. { "HPL PGA", NULL, "DACL" },
  946. { "HPR PGA", NULL, "DACR" },
  947. { "LINEL PGA", NULL, "DACL" },
  948. { "LINER PGA", NULL, "DACR" },
  949. };
  950. static int wm8904_add_widgets(struct snd_soc_codec *codec)
  951. {
  952. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  953. struct snd_soc_dapm_context *dapm = &codec->dapm;
  954. snd_soc_dapm_new_controls(dapm, wm8904_core_dapm_widgets,
  955. ARRAY_SIZE(wm8904_core_dapm_widgets));
  956. snd_soc_dapm_add_routes(dapm, core_intercon,
  957. ARRAY_SIZE(core_intercon));
  958. switch (wm8904->devtype) {
  959. case WM8904:
  960. snd_soc_add_codec_controls(codec, wm8904_adc_snd_controls,
  961. ARRAY_SIZE(wm8904_adc_snd_controls));
  962. snd_soc_add_codec_controls(codec, wm8904_dac_snd_controls,
  963. ARRAY_SIZE(wm8904_dac_snd_controls));
  964. snd_soc_add_codec_controls(codec, wm8904_snd_controls,
  965. ARRAY_SIZE(wm8904_snd_controls));
  966. snd_soc_dapm_new_controls(dapm, wm8904_adc_dapm_widgets,
  967. ARRAY_SIZE(wm8904_adc_dapm_widgets));
  968. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  969. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  970. snd_soc_dapm_new_controls(dapm, wm8904_dapm_widgets,
  971. ARRAY_SIZE(wm8904_dapm_widgets));
  972. snd_soc_dapm_add_routes(dapm, adc_intercon,
  973. ARRAY_SIZE(adc_intercon));
  974. snd_soc_dapm_add_routes(dapm, dac_intercon,
  975. ARRAY_SIZE(dac_intercon));
  976. snd_soc_dapm_add_routes(dapm, wm8904_intercon,
  977. ARRAY_SIZE(wm8904_intercon));
  978. break;
  979. case WM8912:
  980. snd_soc_add_codec_controls(codec, wm8904_dac_snd_controls,
  981. ARRAY_SIZE(wm8904_dac_snd_controls));
  982. snd_soc_dapm_new_controls(dapm, wm8904_dac_dapm_widgets,
  983. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  984. snd_soc_dapm_add_routes(dapm, dac_intercon,
  985. ARRAY_SIZE(dac_intercon));
  986. snd_soc_dapm_add_routes(dapm, wm8912_intercon,
  987. ARRAY_SIZE(wm8912_intercon));
  988. break;
  989. }
  990. snd_soc_dapm_new_widgets(dapm);
  991. return 0;
  992. }
  993. static struct {
  994. int ratio;
  995. unsigned int clk_sys_rate;
  996. } clk_sys_rates[] = {
  997. { 64, 0 },
  998. { 128, 1 },
  999. { 192, 2 },
  1000. { 256, 3 },
  1001. { 384, 4 },
  1002. { 512, 5 },
  1003. { 786, 6 },
  1004. { 1024, 7 },
  1005. { 1408, 8 },
  1006. { 1536, 9 },
  1007. };
  1008. static struct {
  1009. int rate;
  1010. int sample_rate;
  1011. } sample_rates[] = {
  1012. { 8000, 0 },
  1013. { 11025, 1 },
  1014. { 12000, 1 },
  1015. { 16000, 2 },
  1016. { 22050, 3 },
  1017. { 24000, 3 },
  1018. { 32000, 4 },
  1019. { 44100, 5 },
  1020. { 48000, 5 },
  1021. };
  1022. static struct {
  1023. int div; /* *10 due to .5s */
  1024. int bclk_div;
  1025. } bclk_divs[] = {
  1026. { 10, 0 },
  1027. { 15, 1 },
  1028. { 20, 2 },
  1029. { 30, 3 },
  1030. { 40, 4 },
  1031. { 50, 5 },
  1032. { 55, 6 },
  1033. { 60, 7 },
  1034. { 80, 8 },
  1035. { 100, 9 },
  1036. { 110, 10 },
  1037. { 120, 11 },
  1038. { 160, 12 },
  1039. { 200, 13 },
  1040. { 220, 14 },
  1041. { 240, 16 },
  1042. { 200, 17 },
  1043. { 320, 18 },
  1044. { 440, 19 },
  1045. { 480, 20 },
  1046. };
  1047. static int wm8904_hw_params(struct snd_pcm_substream *substream,
  1048. struct snd_pcm_hw_params *params,
  1049. struct snd_soc_dai *dai)
  1050. {
  1051. struct snd_soc_codec *codec = dai->codec;
  1052. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1053. int ret, i, best, best_val, cur_val;
  1054. unsigned int aif1 = 0;
  1055. unsigned int aif2 = 0;
  1056. unsigned int aif3 = 0;
  1057. unsigned int clock1 = 0;
  1058. unsigned int dac_digital1 = 0;
  1059. /* What BCLK do we need? */
  1060. wm8904->fs = params_rate(params);
  1061. if (wm8904->tdm_slots) {
  1062. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1063. wm8904->tdm_slots, wm8904->tdm_width);
  1064. wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
  1065. wm8904->tdm_width, 2,
  1066. wm8904->tdm_slots);
  1067. } else {
  1068. wm8904->bclk = snd_soc_params_to_bclk(params);
  1069. }
  1070. switch (params_format(params)) {
  1071. case SNDRV_PCM_FORMAT_S16_LE:
  1072. break;
  1073. case SNDRV_PCM_FORMAT_S20_3LE:
  1074. aif1 |= 0x40;
  1075. break;
  1076. case SNDRV_PCM_FORMAT_S24_LE:
  1077. aif1 |= 0x80;
  1078. break;
  1079. case SNDRV_PCM_FORMAT_S32_LE:
  1080. aif1 |= 0xc0;
  1081. break;
  1082. default:
  1083. return -EINVAL;
  1084. }
  1085. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
  1086. ret = wm8904_configure_clocking(codec);
  1087. if (ret != 0)
  1088. return ret;
  1089. /* Select nearest CLK_SYS_RATE */
  1090. best = 0;
  1091. best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
  1092. - wm8904->fs);
  1093. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1094. cur_val = abs((wm8904->sysclk_rate /
  1095. clk_sys_rates[i].ratio) - wm8904->fs);
  1096. if (cur_val < best_val) {
  1097. best = i;
  1098. best_val = cur_val;
  1099. }
  1100. }
  1101. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1102. clk_sys_rates[best].ratio);
  1103. clock1 |= (clk_sys_rates[best].clk_sys_rate
  1104. << WM8904_CLK_SYS_RATE_SHIFT);
  1105. /* SAMPLE_RATE */
  1106. best = 0;
  1107. best_val = abs(wm8904->fs - sample_rates[0].rate);
  1108. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1109. /* Closest match */
  1110. cur_val = abs(wm8904->fs - sample_rates[i].rate);
  1111. if (cur_val < best_val) {
  1112. best = i;
  1113. best_val = cur_val;
  1114. }
  1115. }
  1116. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1117. sample_rates[best].rate);
  1118. clock1 |= (sample_rates[best].sample_rate
  1119. << WM8904_SAMPLE_RATE_SHIFT);
  1120. /* Enable sloping stopband filter for low sample rates */
  1121. if (wm8904->fs <= 24000)
  1122. dac_digital1 |= WM8904_DAC_SB_FILT;
  1123. /* BCLK_DIV */
  1124. best = 0;
  1125. best_val = INT_MAX;
  1126. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1127. cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
  1128. - wm8904->bclk;
  1129. if (cur_val < 0) /* Table is sorted */
  1130. break;
  1131. if (cur_val < best_val) {
  1132. best = i;
  1133. best_val = cur_val;
  1134. }
  1135. }
  1136. wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
  1137. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1138. bclk_divs[best].div, wm8904->bclk);
  1139. aif2 |= bclk_divs[best].bclk_div;
  1140. /* LRCLK is a simple fraction of BCLK */
  1141. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
  1142. aif3 |= wm8904->bclk / wm8904->fs;
  1143. /* Apply the settings */
  1144. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  1145. WM8904_DAC_SB_FILT, dac_digital1);
  1146. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1147. WM8904_AIF_WL_MASK, aif1);
  1148. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
  1149. WM8904_BCLK_DIV_MASK, aif2);
  1150. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1151. WM8904_LRCLK_RATE_MASK, aif3);
  1152. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
  1153. WM8904_SAMPLE_RATE_MASK |
  1154. WM8904_CLK_SYS_RATE_MASK, clock1);
  1155. /* Update filters for the new settings */
  1156. wm8904_set_retune_mobile(codec);
  1157. wm8904_set_deemph(codec);
  1158. return 0;
  1159. }
  1160. static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1161. unsigned int freq, int dir)
  1162. {
  1163. struct snd_soc_codec *codec = dai->codec;
  1164. struct wm8904_priv *priv = snd_soc_codec_get_drvdata(codec);
  1165. switch (clk_id) {
  1166. case WM8904_CLK_MCLK:
  1167. priv->sysclk_src = clk_id;
  1168. priv->mclk_rate = freq;
  1169. break;
  1170. case WM8904_CLK_FLL:
  1171. priv->sysclk_src = clk_id;
  1172. break;
  1173. default:
  1174. return -EINVAL;
  1175. }
  1176. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  1177. wm8904_configure_clocking(codec);
  1178. return 0;
  1179. }
  1180. static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1181. {
  1182. struct snd_soc_codec *codec = dai->codec;
  1183. unsigned int aif1 = 0;
  1184. unsigned int aif3 = 0;
  1185. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1186. case SND_SOC_DAIFMT_CBS_CFS:
  1187. break;
  1188. case SND_SOC_DAIFMT_CBS_CFM:
  1189. aif3 |= WM8904_LRCLK_DIR;
  1190. break;
  1191. case SND_SOC_DAIFMT_CBM_CFS:
  1192. aif1 |= WM8904_BCLK_DIR;
  1193. break;
  1194. case SND_SOC_DAIFMT_CBM_CFM:
  1195. aif1 |= WM8904_BCLK_DIR;
  1196. aif3 |= WM8904_LRCLK_DIR;
  1197. break;
  1198. default:
  1199. return -EINVAL;
  1200. }
  1201. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1202. case SND_SOC_DAIFMT_DSP_B:
  1203. aif1 |= WM8904_AIF_LRCLK_INV;
  1204. case SND_SOC_DAIFMT_DSP_A:
  1205. aif1 |= 0x3;
  1206. break;
  1207. case SND_SOC_DAIFMT_I2S:
  1208. aif1 |= 0x2;
  1209. break;
  1210. case SND_SOC_DAIFMT_RIGHT_J:
  1211. break;
  1212. case SND_SOC_DAIFMT_LEFT_J:
  1213. aif1 |= 0x1;
  1214. break;
  1215. default:
  1216. return -EINVAL;
  1217. }
  1218. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1219. case SND_SOC_DAIFMT_DSP_A:
  1220. case SND_SOC_DAIFMT_DSP_B:
  1221. /* frame inversion not valid for DSP modes */
  1222. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1223. case SND_SOC_DAIFMT_NB_NF:
  1224. break;
  1225. case SND_SOC_DAIFMT_IB_NF:
  1226. aif1 |= WM8904_AIF_BCLK_INV;
  1227. break;
  1228. default:
  1229. return -EINVAL;
  1230. }
  1231. break;
  1232. case SND_SOC_DAIFMT_I2S:
  1233. case SND_SOC_DAIFMT_RIGHT_J:
  1234. case SND_SOC_DAIFMT_LEFT_J:
  1235. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1236. case SND_SOC_DAIFMT_NB_NF:
  1237. break;
  1238. case SND_SOC_DAIFMT_IB_IF:
  1239. aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
  1240. break;
  1241. case SND_SOC_DAIFMT_IB_NF:
  1242. aif1 |= WM8904_AIF_BCLK_INV;
  1243. break;
  1244. case SND_SOC_DAIFMT_NB_IF:
  1245. aif1 |= WM8904_AIF_LRCLK_INV;
  1246. break;
  1247. default:
  1248. return -EINVAL;
  1249. }
  1250. break;
  1251. default:
  1252. return -EINVAL;
  1253. }
  1254. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1255. WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
  1256. WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
  1257. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1258. WM8904_LRCLK_DIR, aif3);
  1259. return 0;
  1260. }
  1261. static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1262. unsigned int rx_mask, int slots, int slot_width)
  1263. {
  1264. struct snd_soc_codec *codec = dai->codec;
  1265. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1266. int aif1 = 0;
  1267. /* Don't need to validate anything if we're turning off TDM */
  1268. if (slots == 0)
  1269. goto out;
  1270. /* Note that we allow configurations we can't handle ourselves -
  1271. * for example, we can generate clocks for slots 2 and up even if
  1272. * we can't use those slots ourselves.
  1273. */
  1274. aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
  1275. switch (rx_mask) {
  1276. case 3:
  1277. break;
  1278. case 0xc:
  1279. aif1 |= WM8904_AIFADC_TDM_CHAN;
  1280. break;
  1281. default:
  1282. return -EINVAL;
  1283. }
  1284. switch (tx_mask) {
  1285. case 3:
  1286. break;
  1287. case 0xc:
  1288. aif1 |= WM8904_AIFDAC_TDM_CHAN;
  1289. break;
  1290. default:
  1291. return -EINVAL;
  1292. }
  1293. out:
  1294. wm8904->tdm_width = slot_width;
  1295. wm8904->tdm_slots = slots / 2;
  1296. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1297. WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
  1298. WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
  1299. return 0;
  1300. }
  1301. struct _fll_div {
  1302. u16 fll_fratio;
  1303. u16 fll_outdiv;
  1304. u16 fll_clk_ref_div;
  1305. u16 n;
  1306. u16 k;
  1307. };
  1308. /* The size in bits of the FLL divide multiplied by 10
  1309. * to allow rounding later */
  1310. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1311. static struct {
  1312. unsigned int min;
  1313. unsigned int max;
  1314. u16 fll_fratio;
  1315. int ratio;
  1316. } fll_fratios[] = {
  1317. { 0, 64000, 4, 16 },
  1318. { 64000, 128000, 3, 8 },
  1319. { 128000, 256000, 2, 4 },
  1320. { 256000, 1000000, 1, 2 },
  1321. { 1000000, 13500000, 0, 1 },
  1322. };
  1323. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1324. unsigned int Fout)
  1325. {
  1326. u64 Kpart;
  1327. unsigned int K, Ndiv, Nmod, target;
  1328. unsigned int div;
  1329. int i;
  1330. /* Fref must be <=13.5MHz */
  1331. div = 1;
  1332. fll_div->fll_clk_ref_div = 0;
  1333. while ((Fref / div) > 13500000) {
  1334. div *= 2;
  1335. fll_div->fll_clk_ref_div++;
  1336. if (div > 8) {
  1337. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1338. Fref);
  1339. return -EINVAL;
  1340. }
  1341. }
  1342. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  1343. /* Apply the division for our remaining calculations */
  1344. Fref /= div;
  1345. /* Fvco should be 90-100MHz; don't check the upper bound */
  1346. div = 4;
  1347. while (Fout * div < 90000000) {
  1348. div++;
  1349. if (div > 64) {
  1350. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1351. Fout);
  1352. return -EINVAL;
  1353. }
  1354. }
  1355. target = Fout * div;
  1356. fll_div->fll_outdiv = div - 1;
  1357. pr_debug("Fvco=%dHz\n", target);
  1358. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  1359. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1360. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1361. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1362. target /= fll_fratios[i].ratio;
  1363. break;
  1364. }
  1365. }
  1366. if (i == ARRAY_SIZE(fll_fratios)) {
  1367. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1368. return -EINVAL;
  1369. }
  1370. /* Now, calculate N.K */
  1371. Ndiv = target / Fref;
  1372. fll_div->n = Ndiv;
  1373. Nmod = target % Fref;
  1374. pr_debug("Nmod=%d\n", Nmod);
  1375. /* Calculate fractional part - scale up so we can round. */
  1376. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1377. do_div(Kpart, Fref);
  1378. K = Kpart & 0xFFFFFFFF;
  1379. if ((K % 10) >= 5)
  1380. K += 5;
  1381. /* Move down to proper range now rounding is done */
  1382. fll_div->k = K / 10;
  1383. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  1384. fll_div->n, fll_div->k,
  1385. fll_div->fll_fratio, fll_div->fll_outdiv,
  1386. fll_div->fll_clk_ref_div);
  1387. return 0;
  1388. }
  1389. static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  1390. unsigned int Fref, unsigned int Fout)
  1391. {
  1392. struct snd_soc_codec *codec = dai->codec;
  1393. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1394. struct _fll_div fll_div;
  1395. int ret, val;
  1396. int clock2, fll1;
  1397. /* Any change? */
  1398. if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
  1399. Fout == wm8904->fll_fout)
  1400. return 0;
  1401. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  1402. if (Fout == 0) {
  1403. dev_dbg(codec->dev, "FLL disabled\n");
  1404. wm8904->fll_fref = 0;
  1405. wm8904->fll_fout = 0;
  1406. /* Gate SYSCLK to avoid glitches */
  1407. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1408. WM8904_CLK_SYS_ENA, 0);
  1409. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1410. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1411. goto out;
  1412. }
  1413. /* Validate the FLL ID */
  1414. switch (source) {
  1415. case WM8904_FLL_MCLK:
  1416. case WM8904_FLL_LRCLK:
  1417. case WM8904_FLL_BCLK:
  1418. ret = fll_factors(&fll_div, Fref, Fout);
  1419. if (ret != 0)
  1420. return ret;
  1421. break;
  1422. case WM8904_FLL_FREE_RUNNING:
  1423. dev_dbg(codec->dev, "Using free running FLL\n");
  1424. /* Force 12MHz and output/4 for now */
  1425. Fout = 12000000;
  1426. Fref = 12000000;
  1427. memset(&fll_div, 0, sizeof(fll_div));
  1428. fll_div.fll_outdiv = 3;
  1429. break;
  1430. default:
  1431. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  1432. return -EINVAL;
  1433. }
  1434. /* Save current state then disable the FLL and SYSCLK to avoid
  1435. * misclocking */
  1436. fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
  1437. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1438. WM8904_CLK_SYS_ENA, 0);
  1439. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1440. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1441. /* Unlock forced oscilator control to switch it on/off */
  1442. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1443. WM8904_USER_KEY, WM8904_USER_KEY);
  1444. if (fll_id == WM8904_FLL_FREE_RUNNING) {
  1445. val = WM8904_FLL_FRC_NCO;
  1446. } else {
  1447. val = 0;
  1448. }
  1449. snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
  1450. val);
  1451. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1452. WM8904_USER_KEY, 0);
  1453. switch (fll_id) {
  1454. case WM8904_FLL_MCLK:
  1455. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1456. WM8904_FLL_CLK_REF_SRC_MASK, 0);
  1457. break;
  1458. case WM8904_FLL_LRCLK:
  1459. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1460. WM8904_FLL_CLK_REF_SRC_MASK, 1);
  1461. break;
  1462. case WM8904_FLL_BCLK:
  1463. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1464. WM8904_FLL_CLK_REF_SRC_MASK, 2);
  1465. break;
  1466. }
  1467. if (fll_div.k)
  1468. val = WM8904_FLL_FRACN_ENA;
  1469. else
  1470. val = 0;
  1471. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1472. WM8904_FLL_FRACN_ENA, val);
  1473. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
  1474. WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
  1475. (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
  1476. (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
  1477. snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
  1478. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
  1479. fll_div.n << WM8904_FLL_N_SHIFT);
  1480. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1481. WM8904_FLL_CLK_REF_DIV_MASK,
  1482. fll_div.fll_clk_ref_div
  1483. << WM8904_FLL_CLK_REF_DIV_SHIFT);
  1484. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  1485. wm8904->fll_fref = Fref;
  1486. wm8904->fll_fout = Fout;
  1487. wm8904->fll_src = source;
  1488. /* Enable the FLL if it was previously active */
  1489. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1490. WM8904_FLL_OSC_ENA, fll1);
  1491. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1492. WM8904_FLL_ENA, fll1);
  1493. out:
  1494. /* Reenable SYSCLK if it was previously active */
  1495. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1496. WM8904_CLK_SYS_ENA, clock2);
  1497. return 0;
  1498. }
  1499. static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1500. {
  1501. struct snd_soc_codec *codec = codec_dai->codec;
  1502. int val;
  1503. if (mute)
  1504. val = WM8904_DAC_MUTE;
  1505. else
  1506. val = 0;
  1507. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
  1508. return 0;
  1509. }
  1510. static int wm8904_set_bias_level(struct snd_soc_codec *codec,
  1511. enum snd_soc_bias_level level)
  1512. {
  1513. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1514. int ret;
  1515. switch (level) {
  1516. case SND_SOC_BIAS_ON:
  1517. break;
  1518. case SND_SOC_BIAS_PREPARE:
  1519. /* VMID resistance 2*50k */
  1520. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1521. WM8904_VMID_RES_MASK,
  1522. 0x1 << WM8904_VMID_RES_SHIFT);
  1523. /* Normal bias current */
  1524. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1525. WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
  1526. break;
  1527. case SND_SOC_BIAS_STANDBY:
  1528. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1529. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1530. wm8904->supplies);
  1531. if (ret != 0) {
  1532. dev_err(codec->dev,
  1533. "Failed to enable supplies: %d\n",
  1534. ret);
  1535. return ret;
  1536. }
  1537. regcache_cache_only(wm8904->regmap, false);
  1538. regcache_sync(wm8904->regmap);
  1539. /* Enable bias */
  1540. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1541. WM8904_BIAS_ENA, WM8904_BIAS_ENA);
  1542. /* Enable VMID, VMID buffering, 2*5k resistance */
  1543. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1544. WM8904_VMID_ENA |
  1545. WM8904_VMID_RES_MASK,
  1546. WM8904_VMID_ENA |
  1547. 0x3 << WM8904_VMID_RES_SHIFT);
  1548. /* Let VMID ramp */
  1549. msleep(1);
  1550. }
  1551. /* Maintain VMID with 2*250k */
  1552. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1553. WM8904_VMID_RES_MASK,
  1554. 0x2 << WM8904_VMID_RES_SHIFT);
  1555. /* Bias current *0.5 */
  1556. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1557. WM8904_ISEL_MASK, 0);
  1558. break;
  1559. case SND_SOC_BIAS_OFF:
  1560. /* Turn off VMID */
  1561. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1562. WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
  1563. /* Stop bias generation */
  1564. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1565. WM8904_BIAS_ENA, 0);
  1566. regcache_cache_only(wm8904->regmap, true);
  1567. regcache_mark_dirty(wm8904->regmap);
  1568. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
  1569. wm8904->supplies);
  1570. break;
  1571. }
  1572. codec->dapm.bias_level = level;
  1573. return 0;
  1574. }
  1575. #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
  1576. #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1577. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1578. static const struct snd_soc_dai_ops wm8904_dai_ops = {
  1579. .set_sysclk = wm8904_set_sysclk,
  1580. .set_fmt = wm8904_set_fmt,
  1581. .set_tdm_slot = wm8904_set_tdm_slot,
  1582. .set_pll = wm8904_set_fll,
  1583. .hw_params = wm8904_hw_params,
  1584. .digital_mute = wm8904_digital_mute,
  1585. };
  1586. static struct snd_soc_dai_driver wm8904_dai = {
  1587. .name = "wm8904-hifi",
  1588. .playback = {
  1589. .stream_name = "Playback",
  1590. .channels_min = 2,
  1591. .channels_max = 2,
  1592. .rates = WM8904_RATES,
  1593. .formats = WM8904_FORMATS,
  1594. },
  1595. .capture = {
  1596. .stream_name = "Capture",
  1597. .channels_min = 2,
  1598. .channels_max = 2,
  1599. .rates = WM8904_RATES,
  1600. .formats = WM8904_FORMATS,
  1601. },
  1602. .ops = &wm8904_dai_ops,
  1603. .symmetric_rates = 1,
  1604. };
  1605. static void wm8904_handle_retune_mobile_pdata(struct snd_soc_codec *codec)
  1606. {
  1607. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1608. struct wm8904_pdata *pdata = wm8904->pdata;
  1609. struct snd_kcontrol_new control =
  1610. SOC_ENUM_EXT("EQ Mode",
  1611. wm8904->retune_mobile_enum,
  1612. wm8904_get_retune_mobile_enum,
  1613. wm8904_put_retune_mobile_enum);
  1614. int ret, i, j;
  1615. const char **t;
  1616. /* We need an array of texts for the enum API but the number
  1617. * of texts is likely to be less than the number of
  1618. * configurations due to the sample rate dependency of the
  1619. * configurations. */
  1620. wm8904->num_retune_mobile_texts = 0;
  1621. wm8904->retune_mobile_texts = NULL;
  1622. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  1623. for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
  1624. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  1625. wm8904->retune_mobile_texts[j]) == 0)
  1626. break;
  1627. }
  1628. if (j != wm8904->num_retune_mobile_texts)
  1629. continue;
  1630. /* Expand the array... */
  1631. t = krealloc(wm8904->retune_mobile_texts,
  1632. sizeof(char *) *
  1633. (wm8904->num_retune_mobile_texts + 1),
  1634. GFP_KERNEL);
  1635. if (t == NULL)
  1636. continue;
  1637. /* ...store the new entry... */
  1638. t[wm8904->num_retune_mobile_texts] =
  1639. pdata->retune_mobile_cfgs[i].name;
  1640. /* ...and remember the new version. */
  1641. wm8904->num_retune_mobile_texts++;
  1642. wm8904->retune_mobile_texts = t;
  1643. }
  1644. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  1645. wm8904->num_retune_mobile_texts);
  1646. wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
  1647. wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
  1648. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1649. if (ret != 0)
  1650. dev_err(codec->dev,
  1651. "Failed to add ReTune Mobile control: %d\n", ret);
  1652. }
  1653. static void wm8904_handle_pdata(struct snd_soc_codec *codec)
  1654. {
  1655. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1656. struct wm8904_pdata *pdata = wm8904->pdata;
  1657. int ret, i;
  1658. if (!pdata) {
  1659. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1660. ARRAY_SIZE(wm8904_eq_controls));
  1661. return;
  1662. }
  1663. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  1664. if (pdata->num_drc_cfgs) {
  1665. struct snd_kcontrol_new control =
  1666. SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
  1667. wm8904_get_drc_enum, wm8904_put_drc_enum);
  1668. /* We need an array of texts for the enum API */
  1669. wm8904->drc_texts = kmalloc(sizeof(char *)
  1670. * pdata->num_drc_cfgs, GFP_KERNEL);
  1671. if (!wm8904->drc_texts) {
  1672. dev_err(codec->dev,
  1673. "Failed to allocate %d DRC config texts\n",
  1674. pdata->num_drc_cfgs);
  1675. return;
  1676. }
  1677. for (i = 0; i < pdata->num_drc_cfgs; i++)
  1678. wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
  1679. wm8904->drc_enum.max = pdata->num_drc_cfgs;
  1680. wm8904->drc_enum.texts = wm8904->drc_texts;
  1681. ret = snd_soc_add_codec_controls(codec, &control, 1);
  1682. if (ret != 0)
  1683. dev_err(codec->dev,
  1684. "Failed to add DRC mode control: %d\n", ret);
  1685. wm8904_set_drc(codec);
  1686. }
  1687. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  1688. pdata->num_retune_mobile_cfgs);
  1689. if (pdata->num_retune_mobile_cfgs)
  1690. wm8904_handle_retune_mobile_pdata(codec);
  1691. else
  1692. snd_soc_add_codec_controls(codec, wm8904_eq_controls,
  1693. ARRAY_SIZE(wm8904_eq_controls));
  1694. }
  1695. static int wm8904_probe(struct snd_soc_codec *codec)
  1696. {
  1697. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1698. int ret;
  1699. codec->control_data = wm8904->regmap;
  1700. switch (wm8904->devtype) {
  1701. case WM8904:
  1702. break;
  1703. case WM8912:
  1704. memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
  1705. break;
  1706. default:
  1707. dev_err(codec->dev, "Unknown device type %d\n",
  1708. wm8904->devtype);
  1709. return -EINVAL;
  1710. }
  1711. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
  1712. if (ret != 0) {
  1713. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1714. return ret;
  1715. }
  1716. wm8904_handle_pdata(codec);
  1717. wm8904_add_widgets(codec);
  1718. return 0;
  1719. }
  1720. static int wm8904_remove(struct snd_soc_codec *codec)
  1721. {
  1722. struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
  1723. kfree(wm8904->retune_mobile_texts);
  1724. kfree(wm8904->drc_texts);
  1725. return 0;
  1726. }
  1727. static struct snd_soc_codec_driver soc_codec_dev_wm8904 = {
  1728. .probe = wm8904_probe,
  1729. .remove = wm8904_remove,
  1730. .set_bias_level = wm8904_set_bias_level,
  1731. .idle_bias_off = true,
  1732. };
  1733. static const struct regmap_config wm8904_regmap = {
  1734. .reg_bits = 8,
  1735. .val_bits = 16,
  1736. .max_register = WM8904_MAX_REGISTER,
  1737. .volatile_reg = wm8904_volatile_register,
  1738. .readable_reg = wm8904_readable_register,
  1739. .cache_type = REGCACHE_RBTREE,
  1740. .reg_defaults = wm8904_reg_defaults,
  1741. .num_reg_defaults = ARRAY_SIZE(wm8904_reg_defaults),
  1742. };
  1743. static int wm8904_i2c_probe(struct i2c_client *i2c,
  1744. const struct i2c_device_id *id)
  1745. {
  1746. struct wm8904_priv *wm8904;
  1747. unsigned int val;
  1748. int ret, i;
  1749. wm8904 = devm_kzalloc(&i2c->dev, sizeof(struct wm8904_priv),
  1750. GFP_KERNEL);
  1751. if (wm8904 == NULL)
  1752. return -ENOMEM;
  1753. wm8904->regmap = devm_regmap_init_i2c(i2c, &wm8904_regmap);
  1754. if (IS_ERR(wm8904->regmap)) {
  1755. ret = PTR_ERR(wm8904->regmap);
  1756. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1757. ret);
  1758. return ret;
  1759. }
  1760. wm8904->devtype = id->driver_data;
  1761. i2c_set_clientdata(i2c, wm8904);
  1762. wm8904->pdata = i2c->dev.platform_data;
  1763. for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
  1764. wm8904->supplies[i].supply = wm8904_supply_names[i];
  1765. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8904->supplies),
  1766. wm8904->supplies);
  1767. if (ret != 0) {
  1768. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  1769. return ret;
  1770. }
  1771. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1772. wm8904->supplies);
  1773. if (ret != 0) {
  1774. dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
  1775. return ret;
  1776. }
  1777. ret = regmap_read(wm8904->regmap, WM8904_SW_RESET_AND_ID, &val);
  1778. if (ret < 0) {
  1779. dev_err(&i2c->dev, "Failed to read ID register: %d\n", ret);
  1780. goto err_enable;
  1781. }
  1782. if (val != 0x8904) {
  1783. dev_err(&i2c->dev, "Device is not a WM8904, ID is %x\n", val);
  1784. ret = -EINVAL;
  1785. goto err_enable;
  1786. }
  1787. ret = regmap_read(wm8904->regmap, WM8904_REVISION, &val);
  1788. if (ret < 0) {
  1789. dev_err(&i2c->dev, "Failed to read device revision: %d\n",
  1790. ret);
  1791. goto err_enable;
  1792. }
  1793. dev_info(&i2c->dev, "revision %c\n", val + 'A');
  1794. ret = regmap_write(wm8904->regmap, WM8904_SW_RESET_AND_ID, 0);
  1795. if (ret < 0) {
  1796. dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
  1797. goto err_enable;
  1798. }
  1799. /* Change some default settings - latch VU and enable ZC */
  1800. regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_LEFT,
  1801. WM8904_ADC_VU, WM8904_ADC_VU);
  1802. regmap_update_bits(wm8904->regmap, WM8904_ADC_DIGITAL_VOLUME_RIGHT,
  1803. WM8904_ADC_VU, WM8904_ADC_VU);
  1804. regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_LEFT,
  1805. WM8904_DAC_VU, WM8904_DAC_VU);
  1806. regmap_update_bits(wm8904->regmap, WM8904_DAC_DIGITAL_VOLUME_RIGHT,
  1807. WM8904_DAC_VU, WM8904_DAC_VU);
  1808. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_LEFT,
  1809. WM8904_HPOUT_VU | WM8904_HPOUTLZC,
  1810. WM8904_HPOUT_VU | WM8904_HPOUTLZC);
  1811. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT1_RIGHT,
  1812. WM8904_HPOUT_VU | WM8904_HPOUTRZC,
  1813. WM8904_HPOUT_VU | WM8904_HPOUTRZC);
  1814. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_LEFT,
  1815. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC,
  1816. WM8904_LINEOUT_VU | WM8904_LINEOUTLZC);
  1817. regmap_update_bits(wm8904->regmap, WM8904_ANALOGUE_OUT2_RIGHT,
  1818. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC,
  1819. WM8904_LINEOUT_VU | WM8904_LINEOUTRZC);
  1820. regmap_update_bits(wm8904->regmap, WM8904_CLOCK_RATES_0,
  1821. WM8904_SR_MODE, 0);
  1822. /* Apply configuration from the platform data. */
  1823. if (wm8904->pdata) {
  1824. for (i = 0; i < WM8904_GPIO_REGS; i++) {
  1825. if (!wm8904->pdata->gpio_cfg[i])
  1826. continue;
  1827. regmap_update_bits(wm8904->regmap,
  1828. WM8904_GPIO_CONTROL_1 + i,
  1829. 0xffff,
  1830. wm8904->pdata->gpio_cfg[i]);
  1831. }
  1832. /* Zero is the default value for these anyway */
  1833. for (i = 0; i < WM8904_MIC_REGS; i++)
  1834. regmap_update_bits(wm8904->regmap,
  1835. WM8904_MIC_BIAS_CONTROL_0 + i,
  1836. 0xffff,
  1837. wm8904->pdata->mic_cfg[i]);
  1838. }
  1839. /* Set Class W by default - this will be managed by the Class
  1840. * G widget at runtime where bypass paths are available.
  1841. */
  1842. regmap_update_bits(wm8904->regmap, WM8904_CLASS_W_0,
  1843. WM8904_CP_DYN_PWR, WM8904_CP_DYN_PWR);
  1844. /* Use normal bias source */
  1845. regmap_update_bits(wm8904->regmap, WM8904_BIAS_CONTROL_0,
  1846. WM8904_POBCTRL, 0);
  1847. /* Can leave the device powered off until we need it */
  1848. regcache_cache_only(wm8904->regmap, true);
  1849. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1850. ret = snd_soc_register_codec(&i2c->dev,
  1851. &soc_codec_dev_wm8904, &wm8904_dai, 1);
  1852. if (ret != 0)
  1853. return ret;
  1854. return 0;
  1855. err_enable:
  1856. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  1857. return ret;
  1858. }
  1859. static int wm8904_i2c_remove(struct i2c_client *client)
  1860. {
  1861. snd_soc_unregister_codec(&client->dev);
  1862. return 0;
  1863. }
  1864. static const struct i2c_device_id wm8904_i2c_id[] = {
  1865. { "wm8904", WM8904 },
  1866. { "wm8912", WM8912 },
  1867. { "wm8918", WM8904 }, /* Actually a subset, updates to follow */
  1868. { }
  1869. };
  1870. MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
  1871. static struct i2c_driver wm8904_i2c_driver = {
  1872. .driver = {
  1873. .name = "wm8904",
  1874. .owner = THIS_MODULE,
  1875. },
  1876. .probe = wm8904_i2c_probe,
  1877. .remove = wm8904_i2c_remove,
  1878. .id_table = wm8904_i2c_id,
  1879. };
  1880. module_i2c_driver(wm8904_i2c_driver);
  1881. MODULE_DESCRIPTION("ASoC WM8904 driver");
  1882. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1883. MODULE_LICENSE("GPL");