wm8904.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. /*
  2. * wm8904.c -- WM8904 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009 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/platform_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/initval.h>
  27. #include <sound/tlv.h>
  28. #include <sound/wm8904.h>
  29. #include "wm8904.h"
  30. static struct snd_soc_codec *wm8904_codec;
  31. struct snd_soc_codec_device soc_codec_dev_wm8904;
  32. enum wm8904_type {
  33. WM8904,
  34. WM8912,
  35. };
  36. #define WM8904_NUM_DCS_CHANNELS 4
  37. #define WM8904_NUM_SUPPLIES 5
  38. static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
  39. "DCVDD",
  40. "DBVDD",
  41. "AVDD",
  42. "CPVDD",
  43. "MICVDD",
  44. };
  45. /* codec private data */
  46. struct wm8904_priv {
  47. struct snd_soc_codec codec;
  48. u16 reg_cache[WM8904_MAX_REGISTER + 1];
  49. enum wm8904_type devtype;
  50. struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
  51. struct wm8904_pdata *pdata;
  52. int deemph;
  53. /* Platform provided DRC configuration */
  54. const char **drc_texts;
  55. int drc_cfg;
  56. struct soc_enum drc_enum;
  57. /* Platform provided ReTune mobile configuration */
  58. int num_retune_mobile_texts;
  59. const char **retune_mobile_texts;
  60. int retune_mobile_cfg;
  61. struct soc_enum retune_mobile_enum;
  62. /* FLL setup */
  63. int fll_src;
  64. int fll_fref;
  65. int fll_fout;
  66. /* Clocking configuration */
  67. unsigned int mclk_rate;
  68. int sysclk_src;
  69. unsigned int sysclk_rate;
  70. int tdm_width;
  71. int tdm_slots;
  72. int bclk;
  73. int fs;
  74. /* DC servo configuration - cached offset values */
  75. int dcs_state[WM8904_NUM_DCS_CHANNELS];
  76. };
  77. static const u16 wm8904_reg[WM8904_MAX_REGISTER + 1] = {
  78. 0x8904, /* R0 - SW Reset and ID */
  79. 0x0000, /* R1 - Revision */
  80. 0x0000, /* R2 */
  81. 0x0000, /* R3 */
  82. 0x0018, /* R4 - Bias Control 0 */
  83. 0x0000, /* R5 - VMID Control 0 */
  84. 0x0000, /* R6 - Mic Bias Control 0 */
  85. 0x0000, /* R7 - Mic Bias Control 1 */
  86. 0x0001, /* R8 - Analogue DAC 0 */
  87. 0x9696, /* R9 - mic Filter Control */
  88. 0x0001, /* R10 - Analogue ADC 0 */
  89. 0x0000, /* R11 */
  90. 0x0000, /* R12 - Power Management 0 */
  91. 0x0000, /* R13 */
  92. 0x0000, /* R14 - Power Management 2 */
  93. 0x0000, /* R15 - Power Management 3 */
  94. 0x0000, /* R16 */
  95. 0x0000, /* R17 */
  96. 0x0000, /* R18 - Power Management 6 */
  97. 0x0000, /* R19 */
  98. 0x945E, /* R20 - Clock Rates 0 */
  99. 0x0C05, /* R21 - Clock Rates 1 */
  100. 0x0006, /* R22 - Clock Rates 2 */
  101. 0x0000, /* R23 */
  102. 0x0050, /* R24 - Audio Interface 0 */
  103. 0x000A, /* R25 - Audio Interface 1 */
  104. 0x00E4, /* R26 - Audio Interface 2 */
  105. 0x0040, /* R27 - Audio Interface 3 */
  106. 0x0000, /* R28 */
  107. 0x0000, /* R29 */
  108. 0x00C0, /* R30 - DAC Digital Volume Left */
  109. 0x00C0, /* R31 - DAC Digital Volume Right */
  110. 0x0000, /* R32 - DAC Digital 0 */
  111. 0x0008, /* R33 - DAC Digital 1 */
  112. 0x0000, /* R34 */
  113. 0x0000, /* R35 */
  114. 0x00C0, /* R36 - ADC Digital Volume Left */
  115. 0x00C0, /* R37 - ADC Digital Volume Right */
  116. 0x0010, /* R38 - ADC Digital 0 */
  117. 0x0000, /* R39 - Digital Microphone 0 */
  118. 0x01AF, /* R40 - DRC 0 */
  119. 0x3248, /* R41 - DRC 1 */
  120. 0x0000, /* R42 - DRC 2 */
  121. 0x0000, /* R43 - DRC 3 */
  122. 0x0085, /* R44 - Analogue Left Input 0 */
  123. 0x0085, /* R45 - Analogue Right Input 0 */
  124. 0x0044, /* R46 - Analogue Left Input 1 */
  125. 0x0044, /* R47 - Analogue Right Input 1 */
  126. 0x0000, /* R48 */
  127. 0x0000, /* R49 */
  128. 0x0000, /* R50 */
  129. 0x0000, /* R51 */
  130. 0x0000, /* R52 */
  131. 0x0000, /* R53 */
  132. 0x0000, /* R54 */
  133. 0x0000, /* R55 */
  134. 0x0000, /* R56 */
  135. 0x002D, /* R57 - Analogue OUT1 Left */
  136. 0x002D, /* R58 - Analogue OUT1 Right */
  137. 0x0039, /* R59 - Analogue OUT2 Left */
  138. 0x0039, /* R60 - Analogue OUT2 Right */
  139. 0x0000, /* R61 - Analogue OUT12 ZC */
  140. 0x0000, /* R62 */
  141. 0x0000, /* R63 */
  142. 0x0000, /* R64 */
  143. 0x0000, /* R65 */
  144. 0x0000, /* R66 */
  145. 0x0000, /* R67 - DC Servo 0 */
  146. 0x0000, /* R68 - DC Servo 1 */
  147. 0xAAAA, /* R69 - DC Servo 2 */
  148. 0x0000, /* R70 */
  149. 0xAAAA, /* R71 - DC Servo 4 */
  150. 0xAAAA, /* R72 - DC Servo 5 */
  151. 0x0000, /* R73 - DC Servo 6 */
  152. 0x0000, /* R74 - DC Servo 7 */
  153. 0x0000, /* R75 - DC Servo 8 */
  154. 0x0000, /* R76 - DC Servo 9 */
  155. 0x0000, /* R77 - DC Servo Readback 0 */
  156. 0x0000, /* R78 */
  157. 0x0000, /* R79 */
  158. 0x0000, /* R80 */
  159. 0x0000, /* R81 */
  160. 0x0000, /* R82 */
  161. 0x0000, /* R83 */
  162. 0x0000, /* R84 */
  163. 0x0000, /* R85 */
  164. 0x0000, /* R86 */
  165. 0x0000, /* R87 */
  166. 0x0000, /* R88 */
  167. 0x0000, /* R89 */
  168. 0x0000, /* R90 - Analogue HP 0 */
  169. 0x0000, /* R91 */
  170. 0x0000, /* R92 */
  171. 0x0000, /* R93 */
  172. 0x0000, /* R94 - Analogue Lineout 0 */
  173. 0x0000, /* R95 */
  174. 0x0000, /* R96 */
  175. 0x0000, /* R97 */
  176. 0x0000, /* R98 - Charge Pump 0 */
  177. 0x0000, /* R99 */
  178. 0x0000, /* R100 */
  179. 0x0000, /* R101 */
  180. 0x0000, /* R102 */
  181. 0x0000, /* R103 */
  182. 0x0004, /* R104 - Class W 0 */
  183. 0x0000, /* R105 */
  184. 0x0000, /* R106 */
  185. 0x0000, /* R107 */
  186. 0x0000, /* R108 - Write Sequencer 0 */
  187. 0x0000, /* R109 - Write Sequencer 1 */
  188. 0x0000, /* R110 - Write Sequencer 2 */
  189. 0x0000, /* R111 - Write Sequencer 3 */
  190. 0x0000, /* R112 - Write Sequencer 4 */
  191. 0x0000, /* R113 */
  192. 0x0000, /* R114 */
  193. 0x0000, /* R115 */
  194. 0x0000, /* R116 - FLL Control 1 */
  195. 0x0007, /* R117 - FLL Control 2 */
  196. 0x0000, /* R118 - FLL Control 3 */
  197. 0x2EE0, /* R119 - FLL Control 4 */
  198. 0x0004, /* R120 - FLL Control 5 */
  199. 0x0014, /* R121 - GPIO Control 1 */
  200. 0x0010, /* R122 - GPIO Control 2 */
  201. 0x0010, /* R123 - GPIO Control 3 */
  202. 0x0000, /* R124 - GPIO Control 4 */
  203. 0x0000, /* R125 */
  204. 0x0000, /* R126 - Digital Pulls */
  205. 0x0000, /* R127 - Interrupt Status */
  206. 0xFFFF, /* R128 - Interrupt Status Mask */
  207. 0x0000, /* R129 - Interrupt Polarity */
  208. 0x0000, /* R130 - Interrupt Debounce */
  209. 0x0000, /* R131 */
  210. 0x0000, /* R132 */
  211. 0x0000, /* R133 */
  212. 0x0000, /* R134 - EQ1 */
  213. 0x000C, /* R135 - EQ2 */
  214. 0x000C, /* R136 - EQ3 */
  215. 0x000C, /* R137 - EQ4 */
  216. 0x000C, /* R138 - EQ5 */
  217. 0x000C, /* R139 - EQ6 */
  218. 0x0FCA, /* R140 - EQ7 */
  219. 0x0400, /* R141 - EQ8 */
  220. 0x00D8, /* R142 - EQ9 */
  221. 0x1EB5, /* R143 - EQ10 */
  222. 0xF145, /* R144 - EQ11 */
  223. 0x0B75, /* R145 - EQ12 */
  224. 0x01C5, /* R146 - EQ13 */
  225. 0x1C58, /* R147 - EQ14 */
  226. 0xF373, /* R148 - EQ15 */
  227. 0x0A54, /* R149 - EQ16 */
  228. 0x0558, /* R150 - EQ17 */
  229. 0x168E, /* R151 - EQ18 */
  230. 0xF829, /* R152 - EQ19 */
  231. 0x07AD, /* R153 - EQ20 */
  232. 0x1103, /* R154 - EQ21 */
  233. 0x0564, /* R155 - EQ22 */
  234. 0x0559, /* R156 - EQ23 */
  235. 0x4000, /* R157 - EQ24 */
  236. 0x0000, /* R158 */
  237. 0x0000, /* R159 */
  238. 0x0000, /* R160 */
  239. 0x0000, /* R161 - Control Interface Test 1 */
  240. 0x0000, /* R162 */
  241. 0x0000, /* R163 */
  242. 0x0000, /* R164 */
  243. 0x0000, /* R165 */
  244. 0x0000, /* R166 */
  245. 0x0000, /* R167 */
  246. 0x0000, /* R168 */
  247. 0x0000, /* R169 */
  248. 0x0000, /* R170 */
  249. 0x0000, /* R171 */
  250. 0x0000, /* R172 */
  251. 0x0000, /* R173 */
  252. 0x0000, /* R174 */
  253. 0x0000, /* R175 */
  254. 0x0000, /* R176 */
  255. 0x0000, /* R177 */
  256. 0x0000, /* R178 */
  257. 0x0000, /* R179 */
  258. 0x0000, /* R180 */
  259. 0x0000, /* R181 */
  260. 0x0000, /* R182 */
  261. 0x0000, /* R183 */
  262. 0x0000, /* R184 */
  263. 0x0000, /* R185 */
  264. 0x0000, /* R186 */
  265. 0x0000, /* R187 */
  266. 0x0000, /* R188 */
  267. 0x0000, /* R189 */
  268. 0x0000, /* R190 */
  269. 0x0000, /* R191 */
  270. 0x0000, /* R192 */
  271. 0x0000, /* R193 */
  272. 0x0000, /* R194 */
  273. 0x0000, /* R195 */
  274. 0x0000, /* R196 */
  275. 0x0000, /* R197 */
  276. 0x0000, /* R198 */
  277. 0x0000, /* R199 */
  278. 0x0000, /* R200 */
  279. 0x0000, /* R201 */
  280. 0x0000, /* R202 */
  281. 0x0000, /* R203 */
  282. 0x0000, /* R204 - Analogue Output Bias 0 */
  283. 0x0000, /* R205 */
  284. 0x0000, /* R206 */
  285. 0x0000, /* R207 */
  286. 0x0000, /* R208 */
  287. 0x0000, /* R209 */
  288. 0x0000, /* R210 */
  289. 0x0000, /* R211 */
  290. 0x0000, /* R212 */
  291. 0x0000, /* R213 */
  292. 0x0000, /* R214 */
  293. 0x0000, /* R215 */
  294. 0x0000, /* R216 */
  295. 0x0000, /* R217 */
  296. 0x0000, /* R218 */
  297. 0x0000, /* R219 */
  298. 0x0000, /* R220 */
  299. 0x0000, /* R221 */
  300. 0x0000, /* R222 */
  301. 0x0000, /* R223 */
  302. 0x0000, /* R224 */
  303. 0x0000, /* R225 */
  304. 0x0000, /* R226 */
  305. 0x0000, /* R227 */
  306. 0x0000, /* R228 */
  307. 0x0000, /* R229 */
  308. 0x0000, /* R230 */
  309. 0x0000, /* R231 */
  310. 0x0000, /* R232 */
  311. 0x0000, /* R233 */
  312. 0x0000, /* R234 */
  313. 0x0000, /* R235 */
  314. 0x0000, /* R236 */
  315. 0x0000, /* R237 */
  316. 0x0000, /* R238 */
  317. 0x0000, /* R239 */
  318. 0x0000, /* R240 */
  319. 0x0000, /* R241 */
  320. 0x0000, /* R242 */
  321. 0x0000, /* R243 */
  322. 0x0000, /* R244 */
  323. 0x0000, /* R245 */
  324. 0x0000, /* R246 */
  325. 0x0000, /* R247 - FLL NCO Test 0 */
  326. 0x0019, /* R248 - FLL NCO Test 1 */
  327. };
  328. static struct {
  329. int readable;
  330. int writable;
  331. int vol;
  332. } wm8904_access[] = {
  333. { 0xFFFF, 0xFFFF, 1 }, /* R0 - SW Reset and ID */
  334. { 0x0000, 0x0000, 0 }, /* R1 - Revision */
  335. { 0x0000, 0x0000, 0 }, /* R2 */
  336. { 0x0000, 0x0000, 0 }, /* R3 */
  337. { 0x001F, 0x001F, 0 }, /* R4 - Bias Control 0 */
  338. { 0x0047, 0x0047, 0 }, /* R5 - VMID Control 0 */
  339. { 0x007F, 0x007F, 0 }, /* R6 - Mic Bias Control 0 */
  340. { 0xC007, 0xC007, 0 }, /* R7 - Mic Bias Control 1 */
  341. { 0x001E, 0x001E, 0 }, /* R8 - Analogue DAC 0 */
  342. { 0xFFFF, 0xFFFF, 0 }, /* R9 - mic Filter Control */
  343. { 0x0001, 0x0001, 0 }, /* R10 - Analogue ADC 0 */
  344. { 0x0000, 0x0000, 0 }, /* R11 */
  345. { 0x0003, 0x0003, 0 }, /* R12 - Power Management 0 */
  346. { 0x0000, 0x0000, 0 }, /* R13 */
  347. { 0x0003, 0x0003, 0 }, /* R14 - Power Management 2 */
  348. { 0x0003, 0x0003, 0 }, /* R15 - Power Management 3 */
  349. { 0x0000, 0x0000, 0 }, /* R16 */
  350. { 0x0000, 0x0000, 0 }, /* R17 */
  351. { 0x000F, 0x000F, 0 }, /* R18 - Power Management 6 */
  352. { 0x0000, 0x0000, 0 }, /* R19 */
  353. { 0x7001, 0x7001, 0 }, /* R20 - Clock Rates 0 */
  354. { 0x3C07, 0x3C07, 0 }, /* R21 - Clock Rates 1 */
  355. { 0xD00F, 0xD00F, 0 }, /* R22 - Clock Rates 2 */
  356. { 0x0000, 0x0000, 0 }, /* R23 */
  357. { 0x1FFF, 0x1FFF, 0 }, /* R24 - Audio Interface 0 */
  358. { 0x3DDF, 0x3DDF, 0 }, /* R25 - Audio Interface 1 */
  359. { 0x0F1F, 0x0F1F, 0 }, /* R26 - Audio Interface 2 */
  360. { 0x0FFF, 0x0FFF, 0 }, /* R27 - Audio Interface 3 */
  361. { 0x0000, 0x0000, 0 }, /* R28 */
  362. { 0x0000, 0x0000, 0 }, /* R29 */
  363. { 0x00FF, 0x01FF, 0 }, /* R30 - DAC Digital Volume Left */
  364. { 0x00FF, 0x01FF, 0 }, /* R31 - DAC Digital Volume Right */
  365. { 0x0FFF, 0x0FFF, 0 }, /* R32 - DAC Digital 0 */
  366. { 0x1E4E, 0x1E4E, 0 }, /* R33 - DAC Digital 1 */
  367. { 0x0000, 0x0000, 0 }, /* R34 */
  368. { 0x0000, 0x0000, 0 }, /* R35 */
  369. { 0x00FF, 0x01FF, 0 }, /* R36 - ADC Digital Volume Left */
  370. { 0x00FF, 0x01FF, 0 }, /* R37 - ADC Digital Volume Right */
  371. { 0x0073, 0x0073, 0 }, /* R38 - ADC Digital 0 */
  372. { 0x1800, 0x1800, 0 }, /* R39 - Digital Microphone 0 */
  373. { 0xDFEF, 0xDFEF, 0 }, /* R40 - DRC 0 */
  374. { 0xFFFF, 0xFFFF, 0 }, /* R41 - DRC 1 */
  375. { 0x003F, 0x003F, 0 }, /* R42 - DRC 2 */
  376. { 0x07FF, 0x07FF, 0 }, /* R43 - DRC 3 */
  377. { 0x009F, 0x009F, 0 }, /* R44 - Analogue Left Input 0 */
  378. { 0x009F, 0x009F, 0 }, /* R45 - Analogue Right Input 0 */
  379. { 0x007F, 0x007F, 0 }, /* R46 - Analogue Left Input 1 */
  380. { 0x007F, 0x007F, 0 }, /* R47 - Analogue Right Input 1 */
  381. { 0x0000, 0x0000, 0 }, /* R48 */
  382. { 0x0000, 0x0000, 0 }, /* R49 */
  383. { 0x0000, 0x0000, 0 }, /* R50 */
  384. { 0x0000, 0x0000, 0 }, /* R51 */
  385. { 0x0000, 0x0000, 0 }, /* R52 */
  386. { 0x0000, 0x0000, 0 }, /* R53 */
  387. { 0x0000, 0x0000, 0 }, /* R54 */
  388. { 0x0000, 0x0000, 0 }, /* R55 */
  389. { 0x0000, 0x0000, 0 }, /* R56 */
  390. { 0x017F, 0x01FF, 0 }, /* R57 - Analogue OUT1 Left */
  391. { 0x017F, 0x01FF, 0 }, /* R58 - Analogue OUT1 Right */
  392. { 0x017F, 0x01FF, 0 }, /* R59 - Analogue OUT2 Left */
  393. { 0x017F, 0x01FF, 0 }, /* R60 - Analogue OUT2 Right */
  394. { 0x000F, 0x000F, 0 }, /* R61 - Analogue OUT12 ZC */
  395. { 0x0000, 0x0000, 0 }, /* R62 */
  396. { 0x0000, 0x0000, 0 }, /* R63 */
  397. { 0x0000, 0x0000, 0 }, /* R64 */
  398. { 0x0000, 0x0000, 0 }, /* R65 */
  399. { 0x0000, 0x0000, 0 }, /* R66 */
  400. { 0x000F, 0x000F, 0 }, /* R67 - DC Servo 0 */
  401. { 0xFFFF, 0xFFFF, 1 }, /* R68 - DC Servo 1 */
  402. { 0x0F0F, 0x0F0F, 0 }, /* R69 - DC Servo 2 */
  403. { 0x0000, 0x0000, 0 }, /* R70 */
  404. { 0x007F, 0x007F, 0 }, /* R71 - DC Servo 4 */
  405. { 0x007F, 0x007F, 0 }, /* R72 - DC Servo 5 */
  406. { 0x00FF, 0x00FF, 1 }, /* R73 - DC Servo 6 */
  407. { 0x00FF, 0x00FF, 1 }, /* R74 - DC Servo 7 */
  408. { 0x00FF, 0x00FF, 1 }, /* R75 - DC Servo 8 */
  409. { 0x00FF, 0x00FF, 1 }, /* R76 - DC Servo 9 */
  410. { 0x0FFF, 0x0000, 1 }, /* R77 - DC Servo Readback 0 */
  411. { 0x0000, 0x0000, 0 }, /* R78 */
  412. { 0x0000, 0x0000, 0 }, /* R79 */
  413. { 0x0000, 0x0000, 0 }, /* R80 */
  414. { 0x0000, 0x0000, 0 }, /* R81 */
  415. { 0x0000, 0x0000, 0 }, /* R82 */
  416. { 0x0000, 0x0000, 0 }, /* R83 */
  417. { 0x0000, 0x0000, 0 }, /* R84 */
  418. { 0x0000, 0x0000, 0 }, /* R85 */
  419. { 0x0000, 0x0000, 0 }, /* R86 */
  420. { 0x0000, 0x0000, 0 }, /* R87 */
  421. { 0x0000, 0x0000, 0 }, /* R88 */
  422. { 0x0000, 0x0000, 0 }, /* R89 */
  423. { 0x00FF, 0x00FF, 0 }, /* R90 - Analogue HP 0 */
  424. { 0x0000, 0x0000, 0 }, /* R91 */
  425. { 0x0000, 0x0000, 0 }, /* R92 */
  426. { 0x0000, 0x0000, 0 }, /* R93 */
  427. { 0x00FF, 0x00FF, 0 }, /* R94 - Analogue Lineout 0 */
  428. { 0x0000, 0x0000, 0 }, /* R95 */
  429. { 0x0000, 0x0000, 0 }, /* R96 */
  430. { 0x0000, 0x0000, 0 }, /* R97 */
  431. { 0x0001, 0x0001, 0 }, /* R98 - Charge Pump 0 */
  432. { 0x0000, 0x0000, 0 }, /* R99 */
  433. { 0x0000, 0x0000, 0 }, /* R100 */
  434. { 0x0000, 0x0000, 0 }, /* R101 */
  435. { 0x0000, 0x0000, 0 }, /* R102 */
  436. { 0x0000, 0x0000, 0 }, /* R103 */
  437. { 0x0001, 0x0001, 0 }, /* R104 - Class W 0 */
  438. { 0x0000, 0x0000, 0 }, /* R105 */
  439. { 0x0000, 0x0000, 0 }, /* R106 */
  440. { 0x0000, 0x0000, 0 }, /* R107 */
  441. { 0x011F, 0x011F, 0 }, /* R108 - Write Sequencer 0 */
  442. { 0x7FFF, 0x7FFF, 0 }, /* R109 - Write Sequencer 1 */
  443. { 0x4FFF, 0x4FFF, 0 }, /* R110 - Write Sequencer 2 */
  444. { 0x003F, 0x033F, 0 }, /* R111 - Write Sequencer 3 */
  445. { 0x03F1, 0x0000, 0 }, /* R112 - Write Sequencer 4 */
  446. { 0x0000, 0x0000, 0 }, /* R113 */
  447. { 0x0000, 0x0000, 0 }, /* R114 */
  448. { 0x0000, 0x0000, 0 }, /* R115 */
  449. { 0x0007, 0x0007, 0 }, /* R116 - FLL Control 1 */
  450. { 0x3F77, 0x3F77, 0 }, /* R117 - FLL Control 2 */
  451. { 0xFFFF, 0xFFFF, 0 }, /* R118 - FLL Control 3 */
  452. { 0x7FEF, 0x7FEF, 0 }, /* R119 - FLL Control 4 */
  453. { 0x001B, 0x001B, 0 }, /* R120 - FLL Control 5 */
  454. { 0x003F, 0x003F, 0 }, /* R121 - GPIO Control 1 */
  455. { 0x003F, 0x003F, 0 }, /* R122 - GPIO Control 2 */
  456. { 0x003F, 0x003F, 0 }, /* R123 - GPIO Control 3 */
  457. { 0x038F, 0x038F, 0 }, /* R124 - GPIO Control 4 */
  458. { 0x0000, 0x0000, 0 }, /* R125 */
  459. { 0x00FF, 0x00FF, 0 }, /* R126 - Digital Pulls */
  460. { 0x07FF, 0x03FF, 1 }, /* R127 - Interrupt Status */
  461. { 0x03FF, 0x03FF, 0 }, /* R128 - Interrupt Status Mask */
  462. { 0x03FF, 0x03FF, 0 }, /* R129 - Interrupt Polarity */
  463. { 0x03FF, 0x03FF, 0 }, /* R130 - Interrupt Debounce */
  464. { 0x0000, 0x0000, 0 }, /* R131 */
  465. { 0x0000, 0x0000, 0 }, /* R132 */
  466. { 0x0000, 0x0000, 0 }, /* R133 */
  467. { 0x0001, 0x0001, 0 }, /* R134 - EQ1 */
  468. { 0x001F, 0x001F, 0 }, /* R135 - EQ2 */
  469. { 0x001F, 0x001F, 0 }, /* R136 - EQ3 */
  470. { 0x001F, 0x001F, 0 }, /* R137 - EQ4 */
  471. { 0x001F, 0x001F, 0 }, /* R138 - EQ5 */
  472. { 0x001F, 0x001F, 0 }, /* R139 - EQ6 */
  473. { 0xFFFF, 0xFFFF, 0 }, /* R140 - EQ7 */
  474. { 0xFFFF, 0xFFFF, 0 }, /* R141 - EQ8 */
  475. { 0xFFFF, 0xFFFF, 0 }, /* R142 - EQ9 */
  476. { 0xFFFF, 0xFFFF, 0 }, /* R143 - EQ10 */
  477. { 0xFFFF, 0xFFFF, 0 }, /* R144 - EQ11 */
  478. { 0xFFFF, 0xFFFF, 0 }, /* R145 - EQ12 */
  479. { 0xFFFF, 0xFFFF, 0 }, /* R146 - EQ13 */
  480. { 0xFFFF, 0xFFFF, 0 }, /* R147 - EQ14 */
  481. { 0xFFFF, 0xFFFF, 0 }, /* R148 - EQ15 */
  482. { 0xFFFF, 0xFFFF, 0 }, /* R149 - EQ16 */
  483. { 0xFFFF, 0xFFFF, 0 }, /* R150 - EQ17 */
  484. { 0xFFFF, 0xFFFF, 0 }, /* R151wm8523_dai - EQ18 */
  485. { 0xFFFF, 0xFFFF, 0 }, /* R152 - EQ19 */
  486. { 0xFFFF, 0xFFFF, 0 }, /* R153 - EQ20 */
  487. { 0xFFFF, 0xFFFF, 0 }, /* R154 - EQ21 */
  488. { 0xFFFF, 0xFFFF, 0 }, /* R155 - EQ22 */
  489. { 0xFFFF, 0xFFFF, 0 }, /* R156 - EQ23 */
  490. { 0xFFFF, 0xFFFF, 0 }, /* R157 - EQ24 */
  491. { 0x0000, 0x0000, 0 }, /* R158 */
  492. { 0x0000, 0x0000, 0 }, /* R159 */
  493. { 0x0000, 0x0000, 0 }, /* R160 */
  494. { 0x0002, 0x0002, 0 }, /* R161 - Control Interface Test 1 */
  495. { 0x0000, 0x0000, 0 }, /* R162 */
  496. { 0x0000, 0x0000, 0 }, /* R163 */
  497. { 0x0000, 0x0000, 0 }, /* R164 */
  498. { 0x0000, 0x0000, 0 }, /* R165 */
  499. { 0x0000, 0x0000, 0 }, /* R166 */
  500. { 0x0000, 0x0000, 0 }, /* R167 */
  501. { 0x0000, 0x0000, 0 }, /* R168 */
  502. { 0x0000, 0x0000, 0 }, /* R169 */
  503. { 0x0000, 0x0000, 0 }, /* R170 */
  504. { 0x0000, 0x0000, 0 }, /* R171 */
  505. { 0x0000, 0x0000, 0 }, /* R172 */
  506. { 0x0000, 0x0000, 0 }, /* R173 */
  507. { 0x0000, 0x0000, 0 }, /* R174 */
  508. { 0x0000, 0x0000, 0 }, /* R175 */
  509. { 0x0000, 0x0000, 0 }, /* R176 */
  510. { 0x0000, 0x0000, 0 }, /* R177 */
  511. { 0x0000, 0x0000, 0 }, /* R178 */
  512. { 0x0000, 0x0000, 0 }, /* R179 */
  513. { 0x0000, 0x0000, 0 }, /* R180 */
  514. { 0x0000, 0x0000, 0 }, /* R181 */
  515. { 0x0000, 0x0000, 0 }, /* R182 */
  516. { 0x0000, 0x0000, 0 }, /* R183 */
  517. { 0x0000, 0x0000, 0 }, /* R184 */
  518. { 0x0000, 0x0000, 0 }, /* R185 */
  519. { 0x0000, 0x0000, 0 }, /* R186 */
  520. { 0x0000, 0x0000, 0 }, /* R187 */
  521. { 0x0000, 0x0000, 0 }, /* R188 */
  522. { 0x0000, 0x0000, 0 }, /* R189 */
  523. { 0x0000, 0x0000, 0 }, /* R190 */
  524. { 0x0000, 0x0000, 0 }, /* R191 */
  525. { 0x0000, 0x0000, 0 }, /* R192 */
  526. { 0x0000, 0x0000, 0 }, /* R193 */
  527. { 0x0000, 0x0000, 0 }, /* R194 */
  528. { 0x0000, 0x0000, 0 }, /* R195 */
  529. { 0x0000, 0x0000, 0 }, /* R196 */
  530. { 0x0000, 0x0000, 0 }, /* R197 */
  531. { 0x0000, 0x0000, 0 }, /* R198 */
  532. { 0x0000, 0x0000, 0 }, /* R199 */
  533. { 0x0000, 0x0000, 0 }, /* R200 */
  534. { 0x0000, 0x0000, 0 }, /* R201 */
  535. { 0x0000, 0x0000, 0 }, /* R202 */
  536. { 0x0000, 0x0000, 0 }, /* R203 */
  537. { 0x0070, 0x0070, 0 }, /* R204 - Analogue Output Bias 0 */
  538. { 0x0000, 0x0000, 0 }, /* R205 */
  539. { 0x0000, 0x0000, 0 }, /* R206 */
  540. { 0x0000, 0x0000, 0 }, /* R207 */
  541. { 0x0000, 0x0000, 0 }, /* R208 */
  542. { 0x0000, 0x0000, 0 }, /* R209 */
  543. { 0x0000, 0x0000, 0 }, /* R210 */
  544. { 0x0000, 0x0000, 0 }, /* R211 */
  545. { 0x0000, 0x0000, 0 }, /* R212 */
  546. { 0x0000, 0x0000, 0 }, /* R213 */
  547. { 0x0000, 0x0000, 0 }, /* R214 */
  548. { 0x0000, 0x0000, 0 }, /* R215 */
  549. { 0x0000, 0x0000, 0 }, /* R216 */
  550. { 0x0000, 0x0000, 0 }, /* R217 */
  551. { 0x0000, 0x0000, 0 }, /* R218 */
  552. { 0x0000, 0x0000, 0 }, /* R219 */
  553. { 0x0000, 0x0000, 0 }, /* R220 */
  554. { 0x0000, 0x0000, 0 }, /* R221 */
  555. { 0x0000, 0x0000, 0 }, /* R222 */
  556. { 0x0000, 0x0000, 0 }, /* R223 */
  557. { 0x0000, 0x0000, 0 }, /* R224 */
  558. { 0x0000, 0x0000, 0 }, /* R225 */
  559. { 0x0000, 0x0000, 0 }, /* R226 */
  560. { 0x0000, 0x0000, 0 }, /* R227 */
  561. { 0x0000, 0x0000, 0 }, /* R228 */
  562. { 0x0000, 0x0000, 0 }, /* R229 */
  563. { 0x0000, 0x0000, 0 }, /* R230 */
  564. { 0x0000, 0x0000, 0 }, /* R231 */
  565. { 0x0000, 0x0000, 0 }, /* R232 */
  566. { 0x0000, 0x0000, 0 }, /* R233 */
  567. { 0x0000, 0x0000, 0 }, /* R234 */
  568. { 0x0000, 0x0000, 0 }, /* R235 */
  569. { 0x0000, 0x0000, 0 }, /* R236 */
  570. { 0x0000, 0x0000, 0 }, /* R237 */
  571. { 0x0000, 0x0000, 0 }, /* R238 */
  572. { 0x0000, 0x0000, 0 }, /* R239 */
  573. { 0x0000, 0x0000, 0 }, /* R240 */
  574. { 0x0000, 0x0000, 0 }, /* R241 */
  575. { 0x0000, 0x0000, 0 }, /* R242 */
  576. { 0x0000, 0x0000, 0 }, /* R243 */
  577. { 0x0000, 0x0000, 0 }, /* R244 */
  578. { 0x0000, 0x0000, 0 }, /* R245 */
  579. { 0x0000, 0x0000, 0 }, /* R246 */
  580. { 0x0001, 0x0001, 0 }, /* R247 - FLL NCO Test 0 */
  581. { 0x003F, 0x003F, 0 }, /* R248 - FLL NCO Test 1 */
  582. };
  583. static int wm8904_volatile_register(unsigned int reg)
  584. {
  585. return wm8904_access[reg].vol;
  586. }
  587. static int wm8904_reset(struct snd_soc_codec *codec)
  588. {
  589. return snd_soc_write(codec, WM8904_SW_RESET_AND_ID, 0);
  590. }
  591. static int wm8904_configure_clocking(struct snd_soc_codec *codec)
  592. {
  593. struct wm8904_priv *wm8904 = codec->private_data;
  594. unsigned int clock0, clock2, rate;
  595. /* Gate the clock while we're updating to avoid misclocking */
  596. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  597. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  598. WM8904_SYSCLK_SRC, 0);
  599. /* This should be done on init() for bypass paths */
  600. switch (wm8904->sysclk_src) {
  601. case WM8904_CLK_MCLK:
  602. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
  603. clock2 &= ~WM8904_SYSCLK_SRC;
  604. rate = wm8904->mclk_rate;
  605. /* Ensure the FLL is stopped */
  606. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  607. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  608. break;
  609. case WM8904_CLK_FLL:
  610. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  611. wm8904->fll_fout);
  612. clock2 |= WM8904_SYSCLK_SRC;
  613. rate = wm8904->fll_fout;
  614. break;
  615. default:
  616. dev_err(codec->dev, "System clock not configured\n");
  617. return -EINVAL;
  618. }
  619. /* SYSCLK shouldn't be over 13.5MHz */
  620. if (rate > 13500000) {
  621. clock0 = WM8904_MCLK_DIV;
  622. wm8904->sysclk_rate = rate / 2;
  623. } else {
  624. clock0 = 0;
  625. wm8904->sysclk_rate = rate;
  626. }
  627. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
  628. clock0);
  629. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  630. WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
  631. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
  632. return 0;
  633. }
  634. static void wm8904_set_drc(struct snd_soc_codec *codec)
  635. {
  636. struct wm8904_priv *wm8904 = codec->private_data;
  637. struct wm8904_pdata *pdata = wm8904->pdata;
  638. int save, i;
  639. /* Save any enables; the configuration should clear them. */
  640. save = snd_soc_read(codec, WM8904_DRC_0);
  641. for (i = 0; i < WM8904_DRC_REGS; i++)
  642. snd_soc_update_bits(codec, WM8904_DRC_0 + i, 0xffff,
  643. pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
  644. /* Reenable the DRC */
  645. snd_soc_update_bits(codec, WM8904_DRC_0,
  646. WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
  647. }
  648. static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
  649. struct snd_ctl_elem_value *ucontrol)
  650. {
  651. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  652. struct wm8904_priv *wm8904 = codec->private_data;
  653. struct wm8904_pdata *pdata = wm8904->pdata;
  654. int value = ucontrol->value.integer.value[0];
  655. if (value >= pdata->num_drc_cfgs)
  656. return -EINVAL;
  657. wm8904->drc_cfg = value;
  658. wm8904_set_drc(codec);
  659. return 0;
  660. }
  661. static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
  662. struct snd_ctl_elem_value *ucontrol)
  663. {
  664. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  665. struct wm8904_priv *wm8904 = codec->private_data;
  666. ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
  667. return 0;
  668. }
  669. static void wm8904_set_retune_mobile(struct snd_soc_codec *codec)
  670. {
  671. struct wm8904_priv *wm8904 = codec->private_data;
  672. struct wm8904_pdata *pdata = wm8904->pdata;
  673. int best, best_val, save, i, cfg;
  674. if (!pdata || !wm8904->num_retune_mobile_texts)
  675. return;
  676. /* Find the version of the currently selected configuration
  677. * with the nearest sample rate. */
  678. cfg = wm8904->retune_mobile_cfg;
  679. best = 0;
  680. best_val = INT_MAX;
  681. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  682. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  683. wm8904->retune_mobile_texts[cfg]) == 0 &&
  684. abs(pdata->retune_mobile_cfgs[i].rate
  685. - wm8904->fs) < best_val) {
  686. best = i;
  687. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  688. - wm8904->fs);
  689. }
  690. }
  691. dev_dbg(codec->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
  692. pdata->retune_mobile_cfgs[best].name,
  693. pdata->retune_mobile_cfgs[best].rate,
  694. wm8904->fs);
  695. /* The EQ will be disabled while reconfiguring it, remember the
  696. * current configuration.
  697. */
  698. save = snd_soc_read(codec, WM8904_EQ1);
  699. for (i = 0; i < WM8904_EQ_REGS; i++)
  700. snd_soc_update_bits(codec, WM8904_EQ1 + i, 0xffff,
  701. pdata->retune_mobile_cfgs[best].regs[i]);
  702. snd_soc_update_bits(codec, WM8904_EQ1, WM8904_EQ_ENA, save);
  703. }
  704. static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_value *ucontrol)
  706. {
  707. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  708. struct wm8904_priv *wm8904 = codec->private_data;
  709. struct wm8904_pdata *pdata = wm8904->pdata;
  710. int value = ucontrol->value.integer.value[0];
  711. if (value >= pdata->num_retune_mobile_cfgs)
  712. return -EINVAL;
  713. wm8904->retune_mobile_cfg = value;
  714. wm8904_set_retune_mobile(codec);
  715. return 0;
  716. }
  717. static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  718. struct snd_ctl_elem_value *ucontrol)
  719. {
  720. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  721. struct wm8904_priv *wm8904 = codec->private_data;
  722. ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
  723. return 0;
  724. }
  725. static int deemph_settings[] = { 0, 32000, 44100, 48000 };
  726. static int wm8904_set_deemph(struct snd_soc_codec *codec)
  727. {
  728. struct wm8904_priv *wm8904 = codec->private_data;
  729. int val, i, best;
  730. /* If we're using deemphasis select the nearest available sample
  731. * rate.
  732. */
  733. if (wm8904->deemph) {
  734. best = 1;
  735. for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
  736. if (abs(deemph_settings[i] - wm8904->fs) <
  737. abs(deemph_settings[best] - wm8904->fs))
  738. best = i;
  739. }
  740. val = best << WM8904_DEEMPH_SHIFT;
  741. } else {
  742. val = 0;
  743. }
  744. dev_dbg(codec->dev, "Set deemphasis %d\n", val);
  745. return snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  746. WM8904_DEEMPH_MASK, val);
  747. }
  748. static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
  749. struct snd_ctl_elem_value *ucontrol)
  750. {
  751. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  752. struct wm8904_priv *wm8904 = codec->private_data;
  753. return wm8904->deemph;
  754. }
  755. static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
  756. struct snd_ctl_elem_value *ucontrol)
  757. {
  758. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  759. struct wm8904_priv *wm8904 = codec->private_data;
  760. int deemph = ucontrol->value.enumerated.item[0];
  761. if (deemph > 1)
  762. return -EINVAL;
  763. wm8904->deemph = deemph;
  764. return wm8904_set_deemph(codec);
  765. }
  766. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  767. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  768. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  769. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  770. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  771. static const char *input_mode_text[] = {
  772. "Single-Ended", "Differential Line", "Differential Mic"
  773. };
  774. static const struct soc_enum lin_mode =
  775. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  776. static const struct soc_enum rin_mode =
  777. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  778. static const char *hpf_mode_text[] = {
  779. "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
  780. };
  781. static const struct soc_enum hpf_mode =
  782. SOC_ENUM_SINGLE(WM8904_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
  783. static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
  784. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
  785. WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
  786. SOC_ENUM("Left Caputure Mode", lin_mode),
  787. SOC_ENUM("Right Capture Mode", rin_mode),
  788. /* No TLV since it depends on mode */
  789. SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
  790. WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
  791. SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
  792. WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 0),
  793. SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
  794. SOC_ENUM("High Pass Filter Mode", hpf_mode),
  795. SOC_SINGLE("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0),
  796. };
  797. static const char *drc_path_text[] = {
  798. "ADC", "DAC"
  799. };
  800. static const struct soc_enum drc_path =
  801. SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
  802. static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
  803. SOC_SINGLE_TLV("Digital Playback Boost Volume",
  804. WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
  805. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
  806. WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  807. SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
  808. WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
  809. SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
  810. WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
  811. SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
  812. WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
  813. SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
  814. WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
  815. SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
  816. WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
  817. SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
  818. WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
  819. SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
  820. SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
  821. SOC_ENUM("DRC Path", drc_path),
  822. SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
  823. SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
  824. wm8904_get_deemph, wm8904_put_deemph),
  825. };
  826. static const struct snd_kcontrol_new wm8904_snd_controls[] = {
  827. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
  828. sidetone_tlv),
  829. };
  830. static const struct snd_kcontrol_new wm8904_eq_controls[] = {
  831. SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
  832. SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
  833. SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
  834. SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
  835. SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
  836. };
  837. static int cp_event(struct snd_soc_dapm_widget *w,
  838. struct snd_kcontrol *kcontrol, int event)
  839. {
  840. BUG_ON(event != SND_SOC_DAPM_POST_PMU);
  841. /* Maximum startup time */
  842. udelay(500);
  843. return 0;
  844. }
  845. static int sysclk_event(struct snd_soc_dapm_widget *w,
  846. struct snd_kcontrol *kcontrol, int event)
  847. {
  848. struct snd_soc_codec *codec = w->codec;
  849. struct wm8904_priv *wm8904 = codec->private_data;
  850. switch (event) {
  851. case SND_SOC_DAPM_PRE_PMU:
  852. /* If we're using the FLL then we only start it when
  853. * required; we assume that the configuration has been
  854. * done previously and all we need to do is kick it
  855. * off.
  856. */
  857. switch (wm8904->sysclk_src) {
  858. case WM8904_CLK_FLL:
  859. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  860. WM8904_FLL_OSC_ENA,
  861. WM8904_FLL_OSC_ENA);
  862. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  863. WM8904_FLL_ENA,
  864. WM8904_FLL_ENA);
  865. break;
  866. default:
  867. break;
  868. }
  869. break;
  870. case SND_SOC_DAPM_POST_PMD:
  871. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  872. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  873. break;
  874. }
  875. return 0;
  876. }
  877. static int out_pga_event(struct snd_soc_dapm_widget *w,
  878. struct snd_kcontrol *kcontrol, int event)
  879. {
  880. struct snd_soc_codec *codec = w->codec;
  881. struct wm8904_priv *wm8904 = codec->private_data;
  882. int reg, val;
  883. int dcs_mask;
  884. int dcs_l, dcs_r;
  885. int dcs_l_reg, dcs_r_reg;
  886. int timeout;
  887. int pwr_reg;
  888. /* This code is shared between HP and LINEOUT; we do all our
  889. * power management in stereo pairs to avoid latency issues so
  890. * we reuse shift to identify which rather than strcmp() the
  891. * name. */
  892. reg = w->shift;
  893. switch (reg) {
  894. case WM8904_ANALOGUE_HP_0:
  895. pwr_reg = WM8904_POWER_MANAGEMENT_2;
  896. dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
  897. dcs_r_reg = WM8904_DC_SERVO_8;
  898. dcs_l_reg = WM8904_DC_SERVO_9;
  899. dcs_l = 0;
  900. dcs_r = 1;
  901. break;
  902. case WM8904_ANALOGUE_LINEOUT_0:
  903. pwr_reg = WM8904_POWER_MANAGEMENT_3;
  904. dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
  905. dcs_r_reg = WM8904_DC_SERVO_6;
  906. dcs_l_reg = WM8904_DC_SERVO_7;
  907. dcs_l = 2;
  908. dcs_r = 3;
  909. break;
  910. default:
  911. BUG();
  912. return -EINVAL;
  913. }
  914. switch (event) {
  915. case SND_SOC_DAPM_PRE_PMU:
  916. /* Power on the PGAs */
  917. snd_soc_update_bits(codec, pwr_reg,
  918. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  919. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA);
  920. /* Power on the amplifier */
  921. snd_soc_update_bits(codec, reg,
  922. WM8904_HPL_ENA | WM8904_HPR_ENA,
  923. WM8904_HPL_ENA | WM8904_HPR_ENA);
  924. /* Enable the first stage */
  925. snd_soc_update_bits(codec, reg,
  926. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
  927. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
  928. /* Power up the DC servo */
  929. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  930. dcs_mask, dcs_mask);
  931. /* Either calibrate the DC servo or restore cached state
  932. * if we have that.
  933. */
  934. if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
  935. dev_dbg(codec->dev, "Restoring DC servo state\n");
  936. snd_soc_write(codec, dcs_l_reg,
  937. wm8904->dcs_state[dcs_l]);
  938. snd_soc_write(codec, dcs_r_reg,
  939. wm8904->dcs_state[dcs_r]);
  940. snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
  941. timeout = 20;
  942. } else {
  943. dev_dbg(codec->dev, "Calibrating DC servo\n");
  944. snd_soc_write(codec, WM8904_DC_SERVO_1,
  945. dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
  946. timeout = 500;
  947. }
  948. /* Wait for DC servo to complete */
  949. dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
  950. do {
  951. val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
  952. if ((val & dcs_mask) == dcs_mask)
  953. break;
  954. msleep(1);
  955. } while (--timeout);
  956. if ((val & dcs_mask) != dcs_mask)
  957. dev_warn(codec->dev, "DC servo timed out\n");
  958. else
  959. dev_dbg(codec->dev, "DC servo ready\n");
  960. /* Enable the output stage */
  961. snd_soc_update_bits(codec, reg,
  962. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  963. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
  964. break;
  965. case SND_SOC_DAPM_POST_PMU:
  966. /* Unshort the output itself */
  967. snd_soc_update_bits(codec, reg,
  968. WM8904_HPL_RMV_SHORT |
  969. WM8904_HPR_RMV_SHORT,
  970. WM8904_HPL_RMV_SHORT |
  971. WM8904_HPR_RMV_SHORT);
  972. break;
  973. case SND_SOC_DAPM_PRE_PMD:
  974. /* Short the output */
  975. snd_soc_update_bits(codec, reg,
  976. WM8904_HPL_RMV_SHORT |
  977. WM8904_HPR_RMV_SHORT, 0);
  978. break;
  979. case SND_SOC_DAPM_POST_PMD:
  980. /* Cache the DC servo configuration; this will be
  981. * invalidated if we change the configuration. */
  982. wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
  983. wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
  984. snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
  985. dcs_mask, 0);
  986. /* Disable the amplifier input and output stages */
  987. snd_soc_update_bits(codec, reg,
  988. WM8904_HPL_ENA | WM8904_HPR_ENA |
  989. WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
  990. WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
  991. 0);
  992. /* PGAs too */
  993. snd_soc_update_bits(codec, pwr_reg,
  994. WM8904_HPL_PGA_ENA | WM8904_HPR_PGA_ENA,
  995. 0);
  996. break;
  997. }
  998. return 0;
  999. }
  1000. static const char *lin_text[] = {
  1001. "IN1L", "IN2L", "IN3L"
  1002. };
  1003. static const struct soc_enum lin_enum =
  1004. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
  1005. static const struct snd_kcontrol_new lin_mux =
  1006. SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
  1007. static const struct soc_enum lin_inv_enum =
  1008. SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
  1009. static const struct snd_kcontrol_new lin_inv_mux =
  1010. SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
  1011. static const char *rin_text[] = {
  1012. "IN1R", "IN2R", "IN3R"
  1013. };
  1014. static const struct soc_enum rin_enum =
  1015. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
  1016. static const struct snd_kcontrol_new rin_mux =
  1017. SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
  1018. static const struct soc_enum rin_inv_enum =
  1019. SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
  1020. static const struct snd_kcontrol_new rin_inv_mux =
  1021. SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
  1022. static const char *aif_text[] = {
  1023. "Left", "Right"
  1024. };
  1025. static const struct soc_enum aifoutl_enum =
  1026. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
  1027. static const struct snd_kcontrol_new aifoutl_mux =
  1028. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  1029. static const struct soc_enum aifoutr_enum =
  1030. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
  1031. static const struct snd_kcontrol_new aifoutr_mux =
  1032. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  1033. static const struct soc_enum aifinl_enum =
  1034. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
  1035. static const struct snd_kcontrol_new aifinl_mux =
  1036. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  1037. static const struct soc_enum aifinr_enum =
  1038. SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
  1039. static const struct snd_kcontrol_new aifinr_mux =
  1040. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  1041. static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
  1042. SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
  1043. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1044. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
  1045. SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
  1046. };
  1047. static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
  1048. SND_SOC_DAPM_INPUT("IN1L"),
  1049. SND_SOC_DAPM_INPUT("IN1R"),
  1050. SND_SOC_DAPM_INPUT("IN2L"),
  1051. SND_SOC_DAPM_INPUT("IN2R"),
  1052. SND_SOC_DAPM_INPUT("IN3L"),
  1053. SND_SOC_DAPM_INPUT("IN3R"),
  1054. SND_SOC_DAPM_MICBIAS("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0),
  1055. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
  1056. SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  1057. &lin_inv_mux),
  1058. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
  1059. SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
  1060. &rin_inv_mux),
  1061. SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
  1062. NULL, 0),
  1063. SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
  1064. NULL, 0),
  1065. SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
  1066. SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
  1067. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  1068. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  1069. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  1070. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  1071. };
  1072. static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
  1073. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  1074. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  1075. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  1076. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  1077. SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
  1078. SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
  1079. SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
  1080. SND_SOC_DAPM_POST_PMU),
  1081. SND_SOC_DAPM_PGA("HPL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  1082. SND_SOC_DAPM_PGA("HPR PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  1083. SND_SOC_DAPM_PGA("LINEL PGA", SND_SOC_NOPM, 1, 0, NULL, 0),
  1084. SND_SOC_DAPM_PGA("LINER PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  1085. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
  1086. 0, NULL, 0, out_pga_event,
  1087. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1088. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1089. SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
  1090. 0, NULL, 0, out_pga_event,
  1091. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1092. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1093. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  1094. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  1095. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  1096. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  1097. };
  1098. static const char *out_mux_text[] = {
  1099. "DAC", "Bypass"
  1100. };
  1101. static const struct soc_enum hpl_enum =
  1102. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
  1103. static const struct snd_kcontrol_new hpl_mux =
  1104. SOC_DAPM_ENUM("HPL Mux", hpl_enum);
  1105. static const struct soc_enum hpr_enum =
  1106. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
  1107. static const struct snd_kcontrol_new hpr_mux =
  1108. SOC_DAPM_ENUM("HPR Mux", hpr_enum);
  1109. static const struct soc_enum linel_enum =
  1110. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
  1111. static const struct snd_kcontrol_new linel_mux =
  1112. SOC_DAPM_ENUM("LINEL Mux", linel_enum);
  1113. static const struct soc_enum liner_enum =
  1114. SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
  1115. static const struct snd_kcontrol_new liner_mux =
  1116. SOC_DAPM_ENUM("LINEL Mux", liner_enum);
  1117. static const char *sidetone_text[] = {
  1118. "None", "Left", "Right"
  1119. };
  1120. static const struct soc_enum dacl_sidetone_enum =
  1121. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
  1122. static const struct snd_kcontrol_new dacl_sidetone_mux =
  1123. SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
  1124. static const struct soc_enum dacr_sidetone_enum =
  1125. SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
  1126. static const struct snd_kcontrol_new dacr_sidetone_mux =
  1127. SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
  1128. static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
  1129. SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
  1130. SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  1131. SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
  1132. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
  1133. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
  1134. SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  1135. SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  1136. SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
  1137. SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
  1138. };
  1139. static const struct snd_soc_dapm_route core_intercon[] = {
  1140. { "CLK_DSP", NULL, "SYSCLK" },
  1141. { "TOCLK", NULL, "SYSCLK" },
  1142. };
  1143. static const struct snd_soc_dapm_route adc_intercon[] = {
  1144. { "Left Capture Mux", "IN1L", "IN1L" },
  1145. { "Left Capture Mux", "IN2L", "IN2L" },
  1146. { "Left Capture Mux", "IN3L", "IN3L" },
  1147. { "Left Capture Inverting Mux", "IN1L", "IN1L" },
  1148. { "Left Capture Inverting Mux", "IN2L", "IN2L" },
  1149. { "Left Capture Inverting Mux", "IN3L", "IN3L" },
  1150. { "Right Capture Mux", "IN1R", "IN1R" },
  1151. { "Right Capture Mux", "IN2R", "IN2R" },
  1152. { "Right Capture Mux", "IN3R", "IN3R" },
  1153. { "Right Capture Inverting Mux", "IN1R", "IN1R" },
  1154. { "Right Capture Inverting Mux", "IN2R", "IN2R" },
  1155. { "Right Capture Inverting Mux", "IN3R", "IN3R" },
  1156. { "Left Capture PGA", NULL, "Left Capture Mux" },
  1157. { "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
  1158. { "Right Capture PGA", NULL, "Right Capture Mux" },
  1159. { "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
  1160. { "AIFOUTL", "Left", "ADCL" },
  1161. { "AIFOUTL", "Right", "ADCR" },
  1162. { "AIFOUTR", "Left", "ADCL" },
  1163. { "AIFOUTR", "Right", "ADCR" },
  1164. { "ADCL", NULL, "CLK_DSP" },
  1165. { "ADCL", NULL, "Left Capture PGA" },
  1166. { "ADCR", NULL, "CLK_DSP" },
  1167. { "ADCR", NULL, "Right Capture PGA" },
  1168. };
  1169. static const struct snd_soc_dapm_route dac_intercon[] = {
  1170. { "DACL", "Right", "AIFINR" },
  1171. { "DACL", "Left", "AIFINL" },
  1172. { "DACL", NULL, "CLK_DSP" },
  1173. { "DACR", "Right", "AIFINR" },
  1174. { "DACR", "Left", "AIFINL" },
  1175. { "DACR", NULL, "CLK_DSP" },
  1176. { "Charge pump", NULL, "SYSCLK" },
  1177. { "Headphone Output", NULL, "HPL PGA" },
  1178. { "Headphone Output", NULL, "HPR PGA" },
  1179. { "Headphone Output", NULL, "Charge pump" },
  1180. { "Headphone Output", NULL, "TOCLK" },
  1181. { "Line Output", NULL, "LINEL PGA" },
  1182. { "Line Output", NULL, "LINER PGA" },
  1183. { "Line Output", NULL, "Charge pump" },
  1184. { "Line Output", NULL, "TOCLK" },
  1185. { "HPOUTL", NULL, "Headphone Output" },
  1186. { "HPOUTR", NULL, "Headphone Output" },
  1187. { "LINEOUTL", NULL, "Line Output" },
  1188. { "LINEOUTR", NULL, "Line Output" },
  1189. };
  1190. static const struct snd_soc_dapm_route wm8904_intercon[] = {
  1191. { "Left Sidetone", "Left", "ADCL" },
  1192. { "Left Sidetone", "Right", "ADCR" },
  1193. { "DACL", NULL, "Left Sidetone" },
  1194. { "Right Sidetone", "Left", "ADCL" },
  1195. { "Right Sidetone", "Right", "ADCR" },
  1196. { "DACR", NULL, "Right Sidetone" },
  1197. { "Left Bypass", NULL, "Class G" },
  1198. { "Left Bypass", NULL, "Left Capture PGA" },
  1199. { "Right Bypass", NULL, "Class G" },
  1200. { "Right Bypass", NULL, "Right Capture PGA" },
  1201. { "HPL Mux", "DAC", "DACL" },
  1202. { "HPL Mux", "Bypass", "Left Bypass" },
  1203. { "HPR Mux", "DAC", "DACR" },
  1204. { "HPR Mux", "Bypass", "Right Bypass" },
  1205. { "LINEL Mux", "DAC", "DACL" },
  1206. { "LINEL Mux", "Bypass", "Left Bypass" },
  1207. { "LINER Mux", "DAC", "DACR" },
  1208. { "LINER Mux", "Bypass", "Right Bypass" },
  1209. { "HPL PGA", NULL, "HPL Mux" },
  1210. { "HPR PGA", NULL, "HPR Mux" },
  1211. { "LINEL PGA", NULL, "LINEL Mux" },
  1212. { "LINER PGA", NULL, "LINER Mux" },
  1213. };
  1214. static const struct snd_soc_dapm_route wm8912_intercon[] = {
  1215. { "HPL PGA", NULL, "DACL" },
  1216. { "HPR PGA", NULL, "DACR" },
  1217. { "LINEL PGA", NULL, "DACL" },
  1218. { "LINER PGA", NULL, "DACR" },
  1219. };
  1220. static int wm8904_add_widgets(struct snd_soc_codec *codec)
  1221. {
  1222. struct wm8904_priv *wm8904 = codec->private_data;
  1223. snd_soc_dapm_new_controls(codec, wm8904_core_dapm_widgets,
  1224. ARRAY_SIZE(wm8904_core_dapm_widgets));
  1225. snd_soc_dapm_add_routes(codec, core_intercon,
  1226. ARRAY_SIZE(core_intercon));
  1227. switch (wm8904->devtype) {
  1228. case WM8904:
  1229. snd_soc_add_controls(codec, wm8904_adc_snd_controls,
  1230. ARRAY_SIZE(wm8904_adc_snd_controls));
  1231. snd_soc_add_controls(codec, wm8904_dac_snd_controls,
  1232. ARRAY_SIZE(wm8904_dac_snd_controls));
  1233. snd_soc_add_controls(codec, wm8904_snd_controls,
  1234. ARRAY_SIZE(wm8904_snd_controls));
  1235. snd_soc_dapm_new_controls(codec, wm8904_adc_dapm_widgets,
  1236. ARRAY_SIZE(wm8904_adc_dapm_widgets));
  1237. snd_soc_dapm_new_controls(codec, wm8904_dac_dapm_widgets,
  1238. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  1239. snd_soc_dapm_new_controls(codec, wm8904_dapm_widgets,
  1240. ARRAY_SIZE(wm8904_dapm_widgets));
  1241. snd_soc_dapm_add_routes(codec, core_intercon,
  1242. ARRAY_SIZE(core_intercon));
  1243. snd_soc_dapm_add_routes(codec, adc_intercon,
  1244. ARRAY_SIZE(adc_intercon));
  1245. snd_soc_dapm_add_routes(codec, dac_intercon,
  1246. ARRAY_SIZE(dac_intercon));
  1247. snd_soc_dapm_add_routes(codec, wm8904_intercon,
  1248. ARRAY_SIZE(wm8904_intercon));
  1249. break;
  1250. case WM8912:
  1251. snd_soc_add_controls(codec, wm8904_dac_snd_controls,
  1252. ARRAY_SIZE(wm8904_dac_snd_controls));
  1253. snd_soc_dapm_new_controls(codec, wm8904_dac_dapm_widgets,
  1254. ARRAY_SIZE(wm8904_dac_dapm_widgets));
  1255. snd_soc_dapm_add_routes(codec, dac_intercon,
  1256. ARRAY_SIZE(dac_intercon));
  1257. snd_soc_dapm_add_routes(codec, wm8912_intercon,
  1258. ARRAY_SIZE(wm8912_intercon));
  1259. break;
  1260. }
  1261. snd_soc_dapm_new_widgets(codec);
  1262. return 0;
  1263. }
  1264. static struct {
  1265. int ratio;
  1266. unsigned int clk_sys_rate;
  1267. } clk_sys_rates[] = {
  1268. { 64, 0 },
  1269. { 128, 1 },
  1270. { 192, 2 },
  1271. { 256, 3 },
  1272. { 384, 4 },
  1273. { 512, 5 },
  1274. { 786, 6 },
  1275. { 1024, 7 },
  1276. { 1408, 8 },
  1277. { 1536, 9 },
  1278. };
  1279. static struct {
  1280. int rate;
  1281. int sample_rate;
  1282. } sample_rates[] = {
  1283. { 8000, 0 },
  1284. { 11025, 1 },
  1285. { 12000, 1 },
  1286. { 16000, 2 },
  1287. { 22050, 3 },
  1288. { 24000, 3 },
  1289. { 32000, 4 },
  1290. { 44100, 5 },
  1291. { 48000, 5 },
  1292. };
  1293. static struct {
  1294. int div; /* *10 due to .5s */
  1295. int bclk_div;
  1296. } bclk_divs[] = {
  1297. { 10, 0 },
  1298. { 15, 1 },
  1299. { 20, 2 },
  1300. { 30, 3 },
  1301. { 40, 4 },
  1302. { 50, 5 },
  1303. { 55, 6 },
  1304. { 60, 7 },
  1305. { 80, 8 },
  1306. { 100, 9 },
  1307. { 110, 10 },
  1308. { 120, 11 },
  1309. { 160, 12 },
  1310. { 200, 13 },
  1311. { 220, 14 },
  1312. { 240, 16 },
  1313. { 200, 17 },
  1314. { 320, 18 },
  1315. { 440, 19 },
  1316. { 480, 20 },
  1317. };
  1318. static int wm8904_hw_params(struct snd_pcm_substream *substream,
  1319. struct snd_pcm_hw_params *params,
  1320. struct snd_soc_dai *dai)
  1321. {
  1322. struct snd_soc_codec *codec = dai->codec;
  1323. struct wm8904_priv *wm8904 = codec->private_data;
  1324. int ret, i, best, best_val, cur_val;
  1325. unsigned int aif1 = 0;
  1326. unsigned int aif2 = 0;
  1327. unsigned int aif3 = 0;
  1328. unsigned int clock1 = 0;
  1329. unsigned int dac_digital1 = 0;
  1330. /* What BCLK do we need? */
  1331. wm8904->fs = params_rate(params);
  1332. if (wm8904->tdm_slots) {
  1333. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1334. wm8904->tdm_slots, wm8904->tdm_width);
  1335. wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
  1336. wm8904->tdm_width, 2,
  1337. wm8904->tdm_slots);
  1338. } else {
  1339. wm8904->bclk = snd_soc_params_to_bclk(params);
  1340. }
  1341. switch (params_format(params)) {
  1342. case SNDRV_PCM_FORMAT_S16_LE:
  1343. break;
  1344. case SNDRV_PCM_FORMAT_S20_3LE:
  1345. aif1 |= 0x40;
  1346. break;
  1347. case SNDRV_PCM_FORMAT_S24_LE:
  1348. aif1 |= 0x80;
  1349. break;
  1350. case SNDRV_PCM_FORMAT_S32_LE:
  1351. aif1 |= 0xc0;
  1352. break;
  1353. default:
  1354. return -EINVAL;
  1355. }
  1356. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
  1357. ret = wm8904_configure_clocking(codec);
  1358. if (ret != 0)
  1359. return ret;
  1360. /* Select nearest CLK_SYS_RATE */
  1361. best = 0;
  1362. best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
  1363. - wm8904->fs);
  1364. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1365. cur_val = abs((wm8904->sysclk_rate /
  1366. clk_sys_rates[i].ratio) - wm8904->fs);;
  1367. if (cur_val < best_val) {
  1368. best = i;
  1369. best_val = cur_val;
  1370. }
  1371. }
  1372. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1373. clk_sys_rates[best].ratio);
  1374. clock1 |= (clk_sys_rates[best].clk_sys_rate
  1375. << WM8904_CLK_SYS_RATE_SHIFT);
  1376. /* SAMPLE_RATE */
  1377. best = 0;
  1378. best_val = abs(wm8904->fs - sample_rates[0].rate);
  1379. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1380. /* Closest match */
  1381. cur_val = abs(wm8904->fs - sample_rates[i].rate);
  1382. if (cur_val < best_val) {
  1383. best = i;
  1384. best_val = cur_val;
  1385. }
  1386. }
  1387. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1388. sample_rates[best].rate);
  1389. clock1 |= (sample_rates[best].sample_rate
  1390. << WM8904_SAMPLE_RATE_SHIFT);
  1391. /* Enable sloping stopband filter for low sample rates */
  1392. if (wm8904->fs <= 24000)
  1393. dac_digital1 |= WM8904_DAC_SB_FILT;
  1394. /* BCLK_DIV */
  1395. best = 0;
  1396. best_val = INT_MAX;
  1397. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1398. cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
  1399. - wm8904->bclk;
  1400. if (cur_val < 0) /* Table is sorted */
  1401. break;
  1402. if (cur_val < best_val) {
  1403. best = i;
  1404. best_val = cur_val;
  1405. }
  1406. }
  1407. wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
  1408. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1409. bclk_divs[best].div, wm8904->bclk);
  1410. aif2 |= bclk_divs[best].bclk_div;
  1411. /* LRCLK is a simple fraction of BCLK */
  1412. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
  1413. aif3 |= wm8904->bclk / wm8904->fs;
  1414. /* Apply the settings */
  1415. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
  1416. WM8904_DAC_SB_FILT, dac_digital1);
  1417. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1418. WM8904_AIF_WL_MASK, aif1);
  1419. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
  1420. WM8904_BCLK_DIV_MASK, aif2);
  1421. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1422. WM8904_LRCLK_RATE_MASK, aif3);
  1423. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
  1424. WM8904_SAMPLE_RATE_MASK |
  1425. WM8904_CLK_SYS_RATE_MASK, clock1);
  1426. /* Update filters for the new settings */
  1427. wm8904_set_retune_mobile(codec);
  1428. wm8904_set_deemph(codec);
  1429. return 0;
  1430. }
  1431. static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1432. unsigned int freq, int dir)
  1433. {
  1434. struct snd_soc_codec *codec = dai->codec;
  1435. struct wm8904_priv *priv = codec->private_data;
  1436. switch (clk_id) {
  1437. case WM8904_CLK_MCLK:
  1438. priv->sysclk_src = clk_id;
  1439. priv->mclk_rate = freq;
  1440. break;
  1441. case WM8904_CLK_FLL:
  1442. priv->sysclk_src = clk_id;
  1443. break;
  1444. default:
  1445. return -EINVAL;
  1446. }
  1447. dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
  1448. wm8904_configure_clocking(codec);
  1449. return 0;
  1450. }
  1451. static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1452. {
  1453. struct snd_soc_codec *codec = dai->codec;
  1454. unsigned int aif1 = 0;
  1455. unsigned int aif3 = 0;
  1456. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1457. case SND_SOC_DAIFMT_CBS_CFS:
  1458. break;
  1459. case SND_SOC_DAIFMT_CBS_CFM:
  1460. aif3 |= WM8904_LRCLK_DIR;
  1461. break;
  1462. case SND_SOC_DAIFMT_CBM_CFS:
  1463. aif1 |= WM8904_BCLK_DIR;
  1464. break;
  1465. case SND_SOC_DAIFMT_CBM_CFM:
  1466. aif1 |= WM8904_BCLK_DIR;
  1467. aif3 |= WM8904_LRCLK_DIR;
  1468. break;
  1469. default:
  1470. return -EINVAL;
  1471. }
  1472. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1473. case SND_SOC_DAIFMT_DSP_B:
  1474. aif1 |= WM8904_AIF_LRCLK_INV;
  1475. case SND_SOC_DAIFMT_DSP_A:
  1476. aif1 |= 0x3;
  1477. break;
  1478. case SND_SOC_DAIFMT_I2S:
  1479. aif1 |= 0x2;
  1480. break;
  1481. case SND_SOC_DAIFMT_RIGHT_J:
  1482. break;
  1483. case SND_SOC_DAIFMT_LEFT_J:
  1484. aif1 |= 0x1;
  1485. break;
  1486. default:
  1487. return -EINVAL;
  1488. }
  1489. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1490. case SND_SOC_DAIFMT_DSP_A:
  1491. case SND_SOC_DAIFMT_DSP_B:
  1492. /* frame inversion not valid for DSP modes */
  1493. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1494. case SND_SOC_DAIFMT_NB_NF:
  1495. break;
  1496. case SND_SOC_DAIFMT_IB_NF:
  1497. aif1 |= WM8904_AIF_BCLK_INV;
  1498. break;
  1499. default:
  1500. return -EINVAL;
  1501. }
  1502. break;
  1503. case SND_SOC_DAIFMT_I2S:
  1504. case SND_SOC_DAIFMT_RIGHT_J:
  1505. case SND_SOC_DAIFMT_LEFT_J:
  1506. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1507. case SND_SOC_DAIFMT_NB_NF:
  1508. break;
  1509. case SND_SOC_DAIFMT_IB_IF:
  1510. aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
  1511. break;
  1512. case SND_SOC_DAIFMT_IB_NF:
  1513. aif1 |= WM8904_AIF_BCLK_INV;
  1514. break;
  1515. case SND_SOC_DAIFMT_NB_IF:
  1516. aif1 |= WM8904_AIF_LRCLK_INV;
  1517. break;
  1518. default:
  1519. return -EINVAL;
  1520. }
  1521. break;
  1522. default:
  1523. return -EINVAL;
  1524. }
  1525. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1526. WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
  1527. WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
  1528. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
  1529. WM8904_LRCLK_DIR, aif3);
  1530. return 0;
  1531. }
  1532. static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1533. unsigned int rx_mask, int slots, int slot_width)
  1534. {
  1535. struct snd_soc_codec *codec = dai->codec;
  1536. struct wm8904_priv *wm8904 = codec->private_data;
  1537. int aif1 = 0;
  1538. /* Don't need to validate anything if we're turning off TDM */
  1539. if (slots == 0)
  1540. goto out;
  1541. /* Note that we allow configurations we can't handle ourselves -
  1542. * for example, we can generate clocks for slots 2 and up even if
  1543. * we can't use those slots ourselves.
  1544. */
  1545. aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
  1546. switch (rx_mask) {
  1547. case 3:
  1548. break;
  1549. case 0xc:
  1550. aif1 |= WM8904_AIFADC_TDM_CHAN;
  1551. break;
  1552. default:
  1553. return -EINVAL;
  1554. }
  1555. switch (tx_mask) {
  1556. case 3:
  1557. break;
  1558. case 0xc:
  1559. aif1 |= WM8904_AIFDAC_TDM_CHAN;
  1560. break;
  1561. default:
  1562. return -EINVAL;
  1563. }
  1564. out:
  1565. wm8904->tdm_width = slot_width;
  1566. wm8904->tdm_slots = slots / 2;
  1567. snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
  1568. WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
  1569. WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
  1570. return 0;
  1571. }
  1572. struct _fll_div {
  1573. u16 fll_fratio;
  1574. u16 fll_outdiv;
  1575. u16 fll_clk_ref_div;
  1576. u16 n;
  1577. u16 k;
  1578. };
  1579. /* The size in bits of the FLL divide multiplied by 10
  1580. * to allow rounding later */
  1581. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1582. static struct {
  1583. unsigned int min;
  1584. unsigned int max;
  1585. u16 fll_fratio;
  1586. int ratio;
  1587. } fll_fratios[] = {
  1588. { 0, 64000, 4, 16 },
  1589. { 64000, 128000, 3, 8 },
  1590. { 128000, 256000, 2, 4 },
  1591. { 256000, 1000000, 1, 2 },
  1592. { 1000000, 13500000, 0, 1 },
  1593. };
  1594. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  1595. unsigned int Fout)
  1596. {
  1597. u64 Kpart;
  1598. unsigned int K, Ndiv, Nmod, target;
  1599. unsigned int div;
  1600. int i;
  1601. /* Fref must be <=13.5MHz */
  1602. div = 1;
  1603. fll_div->fll_clk_ref_div = 0;
  1604. while ((Fref / div) > 13500000) {
  1605. div *= 2;
  1606. fll_div->fll_clk_ref_div++;
  1607. if (div > 8) {
  1608. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  1609. Fref);
  1610. return -EINVAL;
  1611. }
  1612. }
  1613. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  1614. /* Apply the division for our remaining calculations */
  1615. Fref /= div;
  1616. /* Fvco should be 90-100MHz; don't check the upper bound */
  1617. div = 4;
  1618. while (Fout * div < 90000000) {
  1619. div++;
  1620. if (div > 64) {
  1621. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  1622. Fout);
  1623. return -EINVAL;
  1624. }
  1625. }
  1626. target = Fout * div;
  1627. fll_div->fll_outdiv = div - 1;
  1628. pr_debug("Fvco=%dHz\n", target);
  1629. /* Find an appropraite FLL_FRATIO and factor it out of the target */
  1630. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  1631. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  1632. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  1633. target /= fll_fratios[i].ratio;
  1634. break;
  1635. }
  1636. }
  1637. if (i == ARRAY_SIZE(fll_fratios)) {
  1638. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  1639. return -EINVAL;
  1640. }
  1641. /* Now, calculate N.K */
  1642. Ndiv = target / Fref;
  1643. fll_div->n = Ndiv;
  1644. Nmod = target % Fref;
  1645. pr_debug("Nmod=%d\n", Nmod);
  1646. /* Calculate fractional part - scale up so we can round. */
  1647. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1648. do_div(Kpart, Fref);
  1649. K = Kpart & 0xFFFFFFFF;
  1650. if ((K % 10) >= 5)
  1651. K += 5;
  1652. /* Move down to proper range now rounding is done */
  1653. fll_div->k = K / 10;
  1654. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  1655. fll_div->n, fll_div->k,
  1656. fll_div->fll_fratio, fll_div->fll_outdiv,
  1657. fll_div->fll_clk_ref_div);
  1658. return 0;
  1659. }
  1660. static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  1661. unsigned int Fref, unsigned int Fout)
  1662. {
  1663. struct snd_soc_codec *codec = dai->codec;
  1664. struct wm8904_priv *wm8904 = codec->private_data;
  1665. struct _fll_div fll_div;
  1666. int ret, val;
  1667. int clock2, fll1;
  1668. /* Any change? */
  1669. if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
  1670. Fout == wm8904->fll_fout)
  1671. return 0;
  1672. clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
  1673. if (Fout == 0) {
  1674. dev_dbg(codec->dev, "FLL disabled\n");
  1675. wm8904->fll_fref = 0;
  1676. wm8904->fll_fout = 0;
  1677. /* Gate SYSCLK to avoid glitches */
  1678. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1679. WM8904_CLK_SYS_ENA, 0);
  1680. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1681. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1682. goto out;
  1683. }
  1684. /* Validate the FLL ID */
  1685. switch (source) {
  1686. case WM8904_FLL_MCLK:
  1687. case WM8904_FLL_LRCLK:
  1688. case WM8904_FLL_BCLK:
  1689. ret = fll_factors(&fll_div, Fref, Fout);
  1690. if (ret != 0)
  1691. return ret;
  1692. break;
  1693. case WM8904_FLL_FREE_RUNNING:
  1694. dev_dbg(codec->dev, "Using free running FLL\n");
  1695. /* Force 12MHz and output/4 for now */
  1696. Fout = 12000000;
  1697. Fref = 12000000;
  1698. memset(&fll_div, 0, sizeof(fll_div));
  1699. fll_div.fll_outdiv = 3;
  1700. break;
  1701. default:
  1702. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  1703. return -EINVAL;
  1704. }
  1705. /* Save current state then disable the FLL and SYSCLK to avoid
  1706. * misclocking */
  1707. fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
  1708. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1709. WM8904_CLK_SYS_ENA, 0);
  1710. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1711. WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
  1712. /* Unlock forced oscilator control to switch it on/off */
  1713. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1714. WM8904_USER_KEY, WM8904_USER_KEY);
  1715. if (fll_id == WM8904_FLL_FREE_RUNNING) {
  1716. val = WM8904_FLL_FRC_NCO;
  1717. } else {
  1718. val = 0;
  1719. }
  1720. snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
  1721. val);
  1722. snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
  1723. WM8904_USER_KEY, 0);
  1724. switch (fll_id) {
  1725. case WM8904_FLL_MCLK:
  1726. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1727. WM8904_FLL_CLK_REF_SRC_MASK, 0);
  1728. break;
  1729. case WM8904_FLL_LRCLK:
  1730. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1731. WM8904_FLL_CLK_REF_SRC_MASK, 1);
  1732. break;
  1733. case WM8904_FLL_BCLK:
  1734. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1735. WM8904_FLL_CLK_REF_SRC_MASK, 2);
  1736. break;
  1737. }
  1738. if (fll_div.k)
  1739. val = WM8904_FLL_FRACN_ENA;
  1740. else
  1741. val = 0;
  1742. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1743. WM8904_FLL_FRACN_ENA, val);
  1744. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
  1745. WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
  1746. (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
  1747. (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
  1748. snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
  1749. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
  1750. fll_div.n << WM8904_FLL_N_SHIFT);
  1751. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
  1752. WM8904_FLL_CLK_REF_DIV_MASK,
  1753. fll_div.fll_clk_ref_div
  1754. << WM8904_FLL_CLK_REF_DIV_SHIFT);
  1755. dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
  1756. wm8904->fll_fref = Fref;
  1757. wm8904->fll_fout = Fout;
  1758. wm8904->fll_src = source;
  1759. /* Enable the FLL if it was previously active */
  1760. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1761. WM8904_FLL_OSC_ENA, fll1);
  1762. snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
  1763. WM8904_FLL_ENA, fll1);
  1764. out:
  1765. /* Reenable SYSCLK if it was previously active */
  1766. snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
  1767. WM8904_CLK_SYS_ENA, clock2);
  1768. return 0;
  1769. }
  1770. static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1771. {
  1772. struct snd_soc_codec *codec = codec_dai->codec;
  1773. int val;
  1774. if (mute)
  1775. val = WM8904_DAC_MUTE;
  1776. else
  1777. val = 0;
  1778. snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
  1779. return 0;
  1780. }
  1781. static void wm8904_sync_cache(struct snd_soc_codec *codec)
  1782. {
  1783. struct wm8904_priv *wm8904 = codec->private_data;
  1784. int i;
  1785. if (!codec->cache_sync)
  1786. return;
  1787. codec->cache_only = 0;
  1788. /* Sync back cached values if they're different from the
  1789. * hardware default.
  1790. */
  1791. for (i = 1; i < ARRAY_SIZE(wm8904->reg_cache); i++) {
  1792. if (!wm8904_access[i].writable)
  1793. continue;
  1794. if (wm8904->reg_cache[i] == wm8904_reg[i])
  1795. continue;
  1796. snd_soc_write(codec, i, wm8904->reg_cache[i]);
  1797. }
  1798. codec->cache_sync = 0;
  1799. }
  1800. static int wm8904_set_bias_level(struct snd_soc_codec *codec,
  1801. enum snd_soc_bias_level level)
  1802. {
  1803. struct wm8904_priv *wm8904 = codec->private_data;
  1804. int ret;
  1805. switch (level) {
  1806. case SND_SOC_BIAS_ON:
  1807. break;
  1808. case SND_SOC_BIAS_PREPARE:
  1809. /* VMID resistance 2*50k */
  1810. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1811. WM8904_VMID_RES_MASK,
  1812. 0x1 << WM8904_VMID_RES_SHIFT);
  1813. /* Normal bias current */
  1814. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1815. WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
  1816. break;
  1817. case SND_SOC_BIAS_STANDBY:
  1818. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  1819. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  1820. wm8904->supplies);
  1821. if (ret != 0) {
  1822. dev_err(codec->dev,
  1823. "Failed to enable supplies: %d\n",
  1824. ret);
  1825. return ret;
  1826. }
  1827. wm8904_sync_cache(codec);
  1828. /* Enable bias */
  1829. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1830. WM8904_BIAS_ENA, WM8904_BIAS_ENA);
  1831. /* Enable VMID, VMID buffering, 2*5k resistance */
  1832. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1833. WM8904_VMID_ENA |
  1834. WM8904_VMID_RES_MASK,
  1835. WM8904_VMID_ENA |
  1836. 0x3 << WM8904_VMID_RES_SHIFT);
  1837. /* Let VMID ramp */
  1838. msleep(1);
  1839. }
  1840. /* Maintain VMID with 2*250k */
  1841. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1842. WM8904_VMID_RES_MASK,
  1843. 0x2 << WM8904_VMID_RES_SHIFT);
  1844. /* Bias current *0.5 */
  1845. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1846. WM8904_ISEL_MASK, 0);
  1847. break;
  1848. case SND_SOC_BIAS_OFF:
  1849. /* Turn off VMID */
  1850. snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
  1851. WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
  1852. /* Stop bias generation */
  1853. snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
  1854. WM8904_BIAS_ENA, 0);
  1855. #ifdef CONFIG_REGULATOR
  1856. /* Post 2.6.34 we will be able to get a callback when
  1857. * the regulators are disabled which we can use but
  1858. * for now just assume that the power will be cut if
  1859. * the regulator API is in use.
  1860. */
  1861. codec->cache_sync = 1;
  1862. #endif
  1863. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
  1864. wm8904->supplies);
  1865. break;
  1866. }
  1867. codec->bias_level = level;
  1868. return 0;
  1869. }
  1870. #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
  1871. #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1872. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1873. static struct snd_soc_dai_ops wm8904_dai_ops = {
  1874. .set_sysclk = wm8904_set_sysclk,
  1875. .set_fmt = wm8904_set_fmt,
  1876. .set_tdm_slot = wm8904_set_tdm_slot,
  1877. .set_pll = wm8904_set_fll,
  1878. .hw_params = wm8904_hw_params,
  1879. .digital_mute = wm8904_digital_mute,
  1880. };
  1881. struct snd_soc_dai wm8904_dai = {
  1882. .name = "WM8904",
  1883. .playback = {
  1884. .stream_name = "Playback",
  1885. .channels_min = 2,
  1886. .channels_max = 2,
  1887. .rates = WM8904_RATES,
  1888. .formats = WM8904_FORMATS,
  1889. },
  1890. .capture = {
  1891. .stream_name = "Capture",
  1892. .channels_min = 2,
  1893. .channels_max = 2,
  1894. .rates = WM8904_RATES,
  1895. .formats = WM8904_FORMATS,
  1896. },
  1897. .ops = &wm8904_dai_ops,
  1898. .symmetric_rates = 1,
  1899. };
  1900. EXPORT_SYMBOL_GPL(wm8904_dai);
  1901. #ifdef CONFIG_PM
  1902. static int wm8904_suspend(struct platform_device *pdev, pm_message_t state)
  1903. {
  1904. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1905. struct snd_soc_codec *codec = socdev->card->codec;
  1906. wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1907. return 0;
  1908. }
  1909. static int wm8904_resume(struct platform_device *pdev)
  1910. {
  1911. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1912. struct snd_soc_codec *codec = socdev->card->codec;
  1913. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1914. return 0;
  1915. }
  1916. #else
  1917. #define wm8904_suspend NULL
  1918. #define wm8904_resume NULL
  1919. #endif
  1920. static void wm8904_handle_retune_mobile_pdata(struct wm8904_priv *wm8904)
  1921. {
  1922. struct snd_soc_codec *codec = &wm8904->codec;
  1923. struct wm8904_pdata *pdata = wm8904->pdata;
  1924. struct snd_kcontrol_new control =
  1925. SOC_ENUM_EXT("EQ Mode",
  1926. wm8904->retune_mobile_enum,
  1927. wm8904_get_retune_mobile_enum,
  1928. wm8904_put_retune_mobile_enum);
  1929. int ret, i, j;
  1930. const char **t;
  1931. /* We need an array of texts for the enum API but the number
  1932. * of texts is likely to be less than the number of
  1933. * configurations due to the sample rate dependency of the
  1934. * configurations. */
  1935. wm8904->num_retune_mobile_texts = 0;
  1936. wm8904->retune_mobile_texts = NULL;
  1937. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  1938. for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
  1939. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  1940. wm8904->retune_mobile_texts[j]) == 0)
  1941. break;
  1942. }
  1943. if (j != wm8904->num_retune_mobile_texts)
  1944. continue;
  1945. /* Expand the array... */
  1946. t = krealloc(wm8904->retune_mobile_texts,
  1947. sizeof(char *) *
  1948. (wm8904->num_retune_mobile_texts + 1),
  1949. GFP_KERNEL);
  1950. if (t == NULL)
  1951. continue;
  1952. /* ...store the new entry... */
  1953. t[wm8904->num_retune_mobile_texts] =
  1954. pdata->retune_mobile_cfgs[i].name;
  1955. /* ...and remember the new version. */
  1956. wm8904->num_retune_mobile_texts++;
  1957. wm8904->retune_mobile_texts = t;
  1958. }
  1959. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  1960. wm8904->num_retune_mobile_texts);
  1961. wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
  1962. wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
  1963. ret = snd_soc_add_controls(&wm8904->codec, &control, 1);
  1964. if (ret != 0)
  1965. dev_err(wm8904->codec.dev,
  1966. "Failed to add ReTune Mobile control: %d\n", ret);
  1967. }
  1968. static void wm8904_handle_pdata(struct wm8904_priv *wm8904)
  1969. {
  1970. struct snd_soc_codec *codec = &wm8904->codec;
  1971. struct wm8904_pdata *pdata = wm8904->pdata;
  1972. int ret, i;
  1973. if (!pdata) {
  1974. snd_soc_add_controls(&wm8904->codec, wm8904_eq_controls,
  1975. ARRAY_SIZE(wm8904_eq_controls));
  1976. return;
  1977. }
  1978. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  1979. if (pdata->num_drc_cfgs) {
  1980. struct snd_kcontrol_new control =
  1981. SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
  1982. wm8904_get_drc_enum, wm8904_put_drc_enum);
  1983. /* We need an array of texts for the enum API */
  1984. wm8904->drc_texts = kmalloc(sizeof(char *)
  1985. * pdata->num_drc_cfgs, GFP_KERNEL);
  1986. if (!wm8904->drc_texts) {
  1987. dev_err(wm8904->codec.dev,
  1988. "Failed to allocate %d DRC config texts\n",
  1989. pdata->num_drc_cfgs);
  1990. return;
  1991. }
  1992. for (i = 0; i < pdata->num_drc_cfgs; i++)
  1993. wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
  1994. wm8904->drc_enum.max = pdata->num_drc_cfgs;
  1995. wm8904->drc_enum.texts = wm8904->drc_texts;
  1996. ret = snd_soc_add_controls(&wm8904->codec, &control, 1);
  1997. if (ret != 0)
  1998. dev_err(wm8904->codec.dev,
  1999. "Failed to add DRC mode control: %d\n", ret);
  2000. wm8904_set_drc(codec);
  2001. }
  2002. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  2003. pdata->num_retune_mobile_cfgs);
  2004. if (pdata->num_retune_mobile_cfgs)
  2005. wm8904_handle_retune_mobile_pdata(wm8904);
  2006. else
  2007. snd_soc_add_controls(&wm8904->codec, wm8904_eq_controls,
  2008. ARRAY_SIZE(wm8904_eq_controls));
  2009. }
  2010. static int wm8904_probe(struct platform_device *pdev)
  2011. {
  2012. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  2013. struct snd_soc_codec *codec;
  2014. int ret = 0;
  2015. if (wm8904_codec == NULL) {
  2016. dev_err(&pdev->dev, "Codec device not registered\n");
  2017. return -ENODEV;
  2018. }
  2019. socdev->card->codec = wm8904_codec;
  2020. codec = wm8904_codec;
  2021. /* register pcms */
  2022. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  2023. if (ret < 0) {
  2024. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  2025. goto pcm_err;
  2026. }
  2027. wm8904_handle_pdata(codec->private_data);
  2028. wm8904_add_widgets(codec);
  2029. return ret;
  2030. pcm_err:
  2031. return ret;
  2032. }
  2033. static int wm8904_remove(struct platform_device *pdev)
  2034. {
  2035. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  2036. snd_soc_free_pcms(socdev);
  2037. snd_soc_dapm_free(socdev);
  2038. return 0;
  2039. }
  2040. struct snd_soc_codec_device soc_codec_dev_wm8904 = {
  2041. .probe = wm8904_probe,
  2042. .remove = wm8904_remove,
  2043. .suspend = wm8904_suspend,
  2044. .resume = wm8904_resume,
  2045. };
  2046. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8904);
  2047. static int wm8904_register(struct wm8904_priv *wm8904,
  2048. enum snd_soc_control_type control)
  2049. {
  2050. int ret;
  2051. struct snd_soc_codec *codec = &wm8904->codec;
  2052. int i;
  2053. if (wm8904_codec) {
  2054. dev_err(codec->dev, "Another WM8904 is registered\n");
  2055. return -EINVAL;
  2056. }
  2057. mutex_init(&codec->mutex);
  2058. INIT_LIST_HEAD(&codec->dapm_widgets);
  2059. INIT_LIST_HEAD(&codec->dapm_paths);
  2060. codec->private_data = wm8904;
  2061. codec->name = "WM8904";
  2062. codec->owner = THIS_MODULE;
  2063. codec->bias_level = SND_SOC_BIAS_OFF;
  2064. codec->set_bias_level = wm8904_set_bias_level;
  2065. codec->dai = &wm8904_dai;
  2066. codec->num_dai = 1;
  2067. codec->reg_cache_size = WM8904_MAX_REGISTER;
  2068. codec->reg_cache = &wm8904->reg_cache;
  2069. codec->volatile_register = wm8904_volatile_register;
  2070. codec->cache_sync = 1;
  2071. codec->idle_bias_off = 1;
  2072. switch (wm8904->devtype) {
  2073. case WM8904:
  2074. break;
  2075. case WM8912:
  2076. memset(&wm8904_dai.capture, 0, sizeof(wm8904_dai.capture));
  2077. break;
  2078. default:
  2079. dev_err(codec->dev, "Unknown device type %d\n",
  2080. wm8904->devtype);
  2081. return -EINVAL;
  2082. }
  2083. memcpy(codec->reg_cache, wm8904_reg, sizeof(wm8904_reg));
  2084. ret = snd_soc_codec_set_cache_io(codec, 8, 16, control);
  2085. if (ret != 0) {
  2086. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  2087. goto err;
  2088. }
  2089. for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
  2090. wm8904->supplies[i].supply = wm8904_supply_names[i];
  2091. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8904->supplies),
  2092. wm8904->supplies);
  2093. if (ret != 0) {
  2094. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  2095. goto err;
  2096. }
  2097. ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
  2098. wm8904->supplies);
  2099. if (ret != 0) {
  2100. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  2101. goto err_get;
  2102. }
  2103. ret = snd_soc_read(codec, WM8904_SW_RESET_AND_ID);
  2104. if (ret < 0) {
  2105. dev_err(codec->dev, "Failed to read ID register\n");
  2106. goto err_enable;
  2107. }
  2108. if (ret != wm8904_reg[WM8904_SW_RESET_AND_ID]) {
  2109. dev_err(codec->dev, "Device is not a WM8904, ID is %x\n", ret);
  2110. ret = -EINVAL;
  2111. goto err_enable;
  2112. }
  2113. ret = snd_soc_read(codec, WM8904_REVISION);
  2114. if (ret < 0) {
  2115. dev_err(codec->dev, "Failed to read device revision: %d\n",
  2116. ret);
  2117. goto err_enable;
  2118. }
  2119. dev_info(codec->dev, "revision %c\n", ret + 'A');
  2120. ret = wm8904_reset(codec);
  2121. if (ret < 0) {
  2122. dev_err(codec->dev, "Failed to issue reset\n");
  2123. goto err_enable;
  2124. }
  2125. wm8904_dai.dev = codec->dev;
  2126. /* Change some default settings - latch VU and enable ZC */
  2127. wm8904->reg_cache[WM8904_ADC_DIGITAL_VOLUME_LEFT] |= WM8904_ADC_VU;
  2128. wm8904->reg_cache[WM8904_ADC_DIGITAL_VOLUME_RIGHT] |= WM8904_ADC_VU;
  2129. wm8904->reg_cache[WM8904_DAC_DIGITAL_VOLUME_LEFT] |= WM8904_DAC_VU;
  2130. wm8904->reg_cache[WM8904_DAC_DIGITAL_VOLUME_RIGHT] |= WM8904_DAC_VU;
  2131. wm8904->reg_cache[WM8904_ANALOGUE_OUT1_LEFT] |= WM8904_HPOUT_VU |
  2132. WM8904_HPOUTLZC;
  2133. wm8904->reg_cache[WM8904_ANALOGUE_OUT1_RIGHT] |= WM8904_HPOUT_VU |
  2134. WM8904_HPOUTRZC;
  2135. wm8904->reg_cache[WM8904_ANALOGUE_OUT2_LEFT] |= WM8904_LINEOUT_VU |
  2136. WM8904_LINEOUTLZC;
  2137. wm8904->reg_cache[WM8904_ANALOGUE_OUT2_RIGHT] |= WM8904_LINEOUT_VU |
  2138. WM8904_LINEOUTRZC;
  2139. wm8904->reg_cache[WM8904_CLOCK_RATES_0] &= ~WM8904_SR_MODE;
  2140. /* Set Class W by default - this will be managed by the Class
  2141. * G widget at runtime where bypass paths are available.
  2142. */
  2143. wm8904->reg_cache[WM8904_CLASS_W_0] |= WM8904_CP_DYN_PWR;
  2144. /* Use normal bias source */
  2145. wm8904->reg_cache[WM8904_BIAS_CONTROL_0] &= ~WM8904_POBCTRL;
  2146. wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  2147. /* Bias level configuration will have done an extra enable */
  2148. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2149. wm8904_codec = codec;
  2150. ret = snd_soc_register_codec(codec);
  2151. if (ret != 0) {
  2152. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  2153. return ret;
  2154. }
  2155. ret = snd_soc_register_dai(&wm8904_dai);
  2156. if (ret != 0) {
  2157. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  2158. snd_soc_unregister_codec(codec);
  2159. return ret;
  2160. }
  2161. return 0;
  2162. err_enable:
  2163. regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2164. err_get:
  2165. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2166. err:
  2167. kfree(wm8904);
  2168. return ret;
  2169. }
  2170. static void wm8904_unregister(struct wm8904_priv *wm8904)
  2171. {
  2172. wm8904_set_bias_level(&wm8904->codec, SND_SOC_BIAS_OFF);
  2173. regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
  2174. snd_soc_unregister_dai(&wm8904_dai);
  2175. snd_soc_unregister_codec(&wm8904->codec);
  2176. kfree(wm8904);
  2177. wm8904_codec = NULL;
  2178. }
  2179. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2180. static __devinit int wm8904_i2c_probe(struct i2c_client *i2c,
  2181. const struct i2c_device_id *id)
  2182. {
  2183. struct wm8904_priv *wm8904;
  2184. struct snd_soc_codec *codec;
  2185. wm8904 = kzalloc(sizeof(struct wm8904_priv), GFP_KERNEL);
  2186. if (wm8904 == NULL)
  2187. return -ENOMEM;
  2188. codec = &wm8904->codec;
  2189. codec->hw_write = (hw_write_t)i2c_master_send;
  2190. wm8904->devtype = id->driver_data;
  2191. i2c_set_clientdata(i2c, wm8904);
  2192. codec->control_data = i2c;
  2193. wm8904->pdata = i2c->dev.platform_data;
  2194. codec->dev = &i2c->dev;
  2195. return wm8904_register(wm8904, SND_SOC_I2C);
  2196. }
  2197. static __devexit int wm8904_i2c_remove(struct i2c_client *client)
  2198. {
  2199. struct wm8904_priv *wm8904 = i2c_get_clientdata(client);
  2200. wm8904_unregister(wm8904);
  2201. return 0;
  2202. }
  2203. static const struct i2c_device_id wm8904_i2c_id[] = {
  2204. { "wm8904", WM8904 },
  2205. { "wm8912", WM8912 },
  2206. { }
  2207. };
  2208. MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
  2209. static struct i2c_driver wm8904_i2c_driver = {
  2210. .driver = {
  2211. .name = "WM8904",
  2212. .owner = THIS_MODULE,
  2213. },
  2214. .probe = wm8904_i2c_probe,
  2215. .remove = __devexit_p(wm8904_i2c_remove),
  2216. .id_table = wm8904_i2c_id,
  2217. };
  2218. #endif
  2219. static int __init wm8904_modinit(void)
  2220. {
  2221. int ret;
  2222. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2223. ret = i2c_add_driver(&wm8904_i2c_driver);
  2224. if (ret != 0) {
  2225. printk(KERN_ERR "Failed to register WM8904 I2C driver: %d\n",
  2226. ret);
  2227. }
  2228. #endif
  2229. return 0;
  2230. }
  2231. module_init(wm8904_modinit);
  2232. static void __exit wm8904_exit(void)
  2233. {
  2234. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  2235. i2c_del_driver(&wm8904_i2c_driver);
  2236. #endif
  2237. }
  2238. module_exit(wm8904_exit);
  2239. MODULE_DESCRIPTION("ASoC WM8904 driver");
  2240. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  2241. MODULE_LICENSE("GPL");