wm8904.c 72 KB

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