cs35l43.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cs35l43.c -- CS35l43 ALSA SoC audio driver
  4. *
  5. * Copyright 2021 Cirrus Logic, Inc.
  6. *
  7. * Author: David Rhodes <[email protected]>
  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. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/version.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/regmap.h>
  28. #include <linux/pm_runtime.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <sound/soc-dapm.h>
  34. #include <linux/gpio.h>
  35. #include <sound/initval.h>
  36. #include <sound/tlv.h>
  37. #include <linux/of_irq.h>
  38. #include <linux/completion.h>
  39. #include <linux/spi/spi.h>
  40. #include <linux/err.h>
  41. #include <linux/firmware.h>
  42. #include <linux/timekeeping.h>
  43. #include <linux/firmware/cirrus/cs_dsp.h>
  44. #include "wm_adsp.h"
  45. #include "cs35l43.h"
  46. #include <sound/cs35l43.h>
  47. #include <sound/cirrus/big_data.h>
  48. #include <sound/cirrus/core.h>
  49. #include <sound/cirrus/calibration.h>
  50. #define DRV_NAME "cs35l43"
  51. static const char * const cs35l43_supplies[] = {
  52. "VA",
  53. "VP",
  54. };
  55. static int cs35l43_exit_hibernate(struct cs35l43_private *cs35l43);
  56. static void cs35l43_pm_runtime_setup(struct cs35l43_private *cs35l43);
  57. static void cs35l43_log_status(struct cs35l43_private *cs35l43);
  58. static int cs35l43_check_dsp_regs(struct cs35l43_private *cs35l43);
  59. static int cs35l43_log_dsp_err(struct cs35l43_private *cs35l43);
  60. static void cs35l43_log_regmap_fail(struct cs35l43_private *cs35l43,
  61. unsigned int reg)
  62. {
  63. struct cirrus_amp *amp = cirrus_get_amp_from_suffix(cs35l43->pdata.mfd_suffix);
  64. dev_crit(cs35l43->dev, "Failed to access regmap, reg = 0x%x\n", reg);
  65. if (amp && amp->i2c_callback)
  66. amp->i2c_callback(cs35l43->pdata.mfd_suffix);
  67. }
  68. static int cs35l43_regmap_read(struct cs35l43_private *cs35l43,
  69. unsigned int reg, unsigned int *val)
  70. {
  71. int ret = 0, retry = CS35L43_REGMAP_RETRY;
  72. do {
  73. ret = regmap_read(cs35l43->regmap, reg, val);
  74. if (ret) {
  75. dev_err(cs35l43->dev, "%s: reg = 0x%x, ret = %d\n",
  76. __func__, reg, ret);
  77. usleep_range(1000, 1100);
  78. }
  79. } while (ret && --retry > 0);
  80. if (retry == 0) {
  81. dev_crit(cs35l43->dev, "%s: Retry failed after %d attempts",
  82. __func__, CS35L43_REGMAP_RETRY);
  83. cs35l43_log_regmap_fail(cs35l43, reg);
  84. }
  85. return ret;
  86. }
  87. static int cs35l43_regmap_bulk_read(struct cs35l43_private *cs35l43,
  88. unsigned int reg, void *val, size_t val_count)
  89. {
  90. int ret = 0, retry = CS35L43_REGMAP_RETRY;
  91. do {
  92. ret = regmap_bulk_read(cs35l43->regmap, reg, val, val_count);
  93. if (ret) {
  94. dev_err(cs35l43->dev, "%s: reg = 0x%x, ret = %d\n",
  95. __func__, reg, ret);
  96. usleep_range(1000, 1100);
  97. }
  98. } while (ret && --retry > 0);
  99. if (retry == 0) {
  100. dev_crit(cs35l43->dev, "%s: Retry failed after %d attempts",
  101. __func__, CS35L43_REGMAP_RETRY);
  102. cs35l43_log_regmap_fail(cs35l43, reg);
  103. }
  104. return ret;
  105. }
  106. static int cs35l43_regmap_write(struct cs35l43_private *cs35l43,
  107. unsigned int reg, unsigned int val)
  108. {
  109. int ret = 0, retry = CS35L43_REGMAP_RETRY;
  110. do {
  111. ret = regmap_write(cs35l43->regmap, reg, val);
  112. if (ret) {
  113. dev_err(cs35l43->dev, "%s: reg = 0x%x, ret = %d\n",
  114. __func__, reg, ret);
  115. usleep_range(1000, 1100);
  116. }
  117. } while (ret && --retry > 0);
  118. if (retry == 0) {
  119. dev_crit(cs35l43->dev, "%s: Retry failed after %d attempts",
  120. __func__, CS35L43_REGMAP_RETRY);
  121. cs35l43_log_regmap_fail(cs35l43, reg);
  122. }
  123. return ret;
  124. }
  125. static int cs35l43_regmap_update_bits(struct cs35l43_private *cs35l43,
  126. unsigned int reg, unsigned int mask, unsigned int val)
  127. {
  128. int ret = 0, retry = CS35L43_REGMAP_RETRY;
  129. do {
  130. ret = regmap_update_bits(cs35l43->regmap, reg, mask, val);
  131. if (ret) {
  132. dev_err(cs35l43->dev, "%s: reg = 0x%x, ret = %d\n",
  133. __func__, reg, ret);
  134. usleep_range(1000, 1100);
  135. }
  136. } while (ret && --retry > 0);
  137. if (retry == 0) {
  138. dev_crit(cs35l43->dev, "%s: Retry failed after %d attempts",
  139. __func__, CS35L43_REGMAP_RETRY);
  140. cs35l43_log_regmap_fail(cs35l43, reg);
  141. }
  142. return ret;
  143. }
  144. static const DECLARE_TLV_DB_RANGE(dig_vol_tlv,
  145. 0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  146. 1, 913, TLV_DB_SCALE_ITEM(-10200, 25, 0));
  147. static DECLARE_TLV_DB_SCALE(amp_gain_tlv, 0, 1, 1);
  148. static const char * const cs35l43_pcm_sftramp_text[] = {
  149. "Off", ".5ms", "1ms", "2ms", "4ms", "8ms", "15ms", "30ms"};
  150. static SOC_ENUM_SINGLE_DECL(pcm_sft_ramp,
  151. CS35L43_AMP_CTRL, 0,
  152. cs35l43_pcm_sftramp_text);
  153. static const char * const cs35l43_tx_input_texts[] = {
  154. "Zero", "ASPRX1", "ASPRX2", "VMON", "IMON", "VMON FS2", "IMON FS2",
  155. "VPMON", "VBSTMON", "DSP", "DSP FS2"};
  156. static const unsigned int cs35l43_tx_input_values[] = {0x00,
  157. CS35L43_INPUT_SRC_ASPRX1,
  158. CS35L43_INPUT_SRC_ASPRX2,
  159. CS35L43_INPUT_SRC_VMON,
  160. CS35L43_INPUT_SRC_IMON,
  161. CS35L43_INPUT_SRC_VMON_FS2,
  162. CS35L43_INPUT_SRC_IMON_FS2,
  163. CS35L43_INPUT_SRC_VPMON,
  164. CS35L43_INPUT_SRC_VBSTMON,
  165. CS35L43_INPUT_DSP_TX5,
  166. CS35L43_INPUT_DSP_TX6};
  167. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_asptx1_enum,
  168. CS35L43_ASPTX1_INPUT,
  169. 0, CS35L43_INPUT_MASK,
  170. cs35l43_tx_input_texts,
  171. cs35l43_tx_input_values);
  172. static const struct snd_kcontrol_new asp_tx1_mux =
  173. SOC_DAPM_ENUM("ASPTX1 SRC", cs35l43_asptx1_enum);
  174. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_asptx2_enum,
  175. CS35L43_ASPTX2_INPUT,
  176. 0, CS35L43_INPUT_MASK,
  177. cs35l43_tx_input_texts,
  178. cs35l43_tx_input_values);
  179. static const struct snd_kcontrol_new asp_tx2_mux =
  180. SOC_DAPM_ENUM("ASPTX2 SRC", cs35l43_asptx2_enum);
  181. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_asptx3_enum,
  182. CS35L43_ASPTX3_INPUT,
  183. 0, CS35L43_INPUT_MASK,
  184. cs35l43_tx_input_texts,
  185. cs35l43_tx_input_values);
  186. static const struct snd_kcontrol_new asp_tx3_mux =
  187. SOC_DAPM_ENUM("ASPTX3 SRC", cs35l43_asptx3_enum);
  188. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_asptx4_enum,
  189. CS35L43_ASPTX4_INPUT,
  190. 0, CS35L43_INPUT_MASK,
  191. cs35l43_tx_input_texts,
  192. cs35l43_tx_input_values);
  193. static const struct snd_kcontrol_new asp_tx4_mux =
  194. SOC_DAPM_ENUM("ASPTX4 SRC", cs35l43_asptx4_enum);
  195. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dsprx1_enum,
  196. CS35L43_DSP1RX1_INPUT,
  197. 0, CS35L43_INPUT_MASK,
  198. cs35l43_tx_input_texts,
  199. cs35l43_tx_input_values);
  200. static const struct snd_kcontrol_new dsp_rx1_mux =
  201. SOC_DAPM_ENUM("DSPRX1 SRC", cs35l43_dsprx1_enum);
  202. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dsprx2_enum,
  203. CS35L43_DSP1RX2_INPUT,
  204. 0, CS35L43_INPUT_MASK,
  205. cs35l43_tx_input_texts,
  206. cs35l43_tx_input_values);
  207. static const struct snd_kcontrol_new dsp_rx2_mux =
  208. SOC_DAPM_ENUM("DSPRX2 SRC", cs35l43_dsprx2_enum);
  209. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dsprx3_enum,
  210. CS35L43_DSP1RX3_INPUT,
  211. 0, CS35L43_INPUT_MASK,
  212. cs35l43_tx_input_texts,
  213. cs35l43_tx_input_values);
  214. static const struct snd_kcontrol_new dsp_rx3_mux =
  215. SOC_DAPM_ENUM("DSPRX3 SRC", cs35l43_dsprx3_enum);
  216. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dacpcm_enum,
  217. CS35L43_DACPCM1_INPUT,
  218. 0, CS35L43_INPUT_MASK,
  219. cs35l43_tx_input_texts,
  220. cs35l43_tx_input_values);
  221. static const struct snd_kcontrol_new dacpcm_mux =
  222. SOC_DAPM_ENUM("PCM Source", cs35l43_dacpcm_enum);
  223. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dacpcm2_enum,
  224. CS35L43_DACPCM2_INPUT,
  225. 0, CS35L43_INPUT_MASK,
  226. cs35l43_tx_input_texts,
  227. cs35l43_tx_input_values);
  228. static const struct snd_kcontrol_new dacpcm2_mux =
  229. SOC_DAPM_ENUM("High Rate PCM Source", cs35l43_dacpcm2_enum);
  230. static const char * const cs35l43_ultrasonic_mode_texts[] = {
  231. "Disabled", "In Band", "Out of Band"
  232. };
  233. static SOC_ENUM_SINGLE_DECL(cs35l43_ultrasonic_mode_enum, SND_SOC_NOPM, 0,
  234. cs35l43_ultrasonic_mode_texts);
  235. static const char * const cs35l43_wd_mode_texts[] = {"Normal", "Mute"};
  236. static const unsigned int cs35l43_wd_mode_values[] = {0x0, 0x3};
  237. static SOC_VALUE_ENUM_SINGLE_DECL(cs35l43_dc_wd_mode_enum, CS35L43_ALIVE_DCIN_WD,
  238. CS35L43_WD_MODE_SHIFT, 0x3,
  239. cs35l43_wd_mode_texts, cs35l43_wd_mode_values);
  240. static const char * const cs35l43_bst_en_text[] = {"Disabled", "Enabled"};
  241. static const unsigned int cs35l43_bst_en_values[] = {
  242. 0, CS35L43_BST_EN_DEFAULT};
  243. static SOC_VALUE_ENUM_SINGLE_DECL(bst_en_ctl,
  244. CS35L43_BLOCK_ENABLES,
  245. CS35L43_BST_EN_SHIFT, 0x3,
  246. cs35l43_bst_en_text,
  247. cs35l43_bst_en_values);
  248. static const char * const cs35l43_amp_mute_text[] = {"Muted", "Unmuted"};
  249. static const unsigned int cs35l43_amp_mute_values[] = {1, 0};
  250. static SOC_VALUE_ENUM_SINGLE_DECL(amp_mute_ctl,
  251. SND_SOC_NOPM,
  252. 0, 0,
  253. cs35l43_amp_mute_text,
  254. cs35l43_amp_mute_values);
  255. static int cs35l43_amp_mute_get(struct snd_kcontrol *kcontrol,
  256. struct snd_ctl_elem_value *ucontrol)
  257. {
  258. struct snd_soc_component *component;
  259. struct cs35l43_private *cs35l43;
  260. component = snd_soc_kcontrol_component(kcontrol);
  261. cs35l43 = snd_soc_component_get_drvdata(component);
  262. ucontrol->value.integer.value[0] = cs35l43->amp_mute;
  263. return 0;
  264. }
  265. static int cs35l43_amp_mute_put(struct snd_kcontrol *kcontrol,
  266. struct snd_ctl_elem_value *ucontrol)
  267. {
  268. struct snd_soc_component *component;
  269. struct cs35l43_private *cs35l43;
  270. unsigned int vol;
  271. component = snd_soc_kcontrol_component(kcontrol);
  272. cs35l43 = snd_soc_component_get_drvdata(component);
  273. if (ucontrol->value.integer.value[0] > 1 ||
  274. ucontrol->value.integer.value[0] < 0) {
  275. dev_err(cs35l43->dev, "%s: Invalid value 0x%lx\n",
  276. __func__, ucontrol->value.integer.value[0]);
  277. return -EINVAL;
  278. }
  279. cs35l43->amp_mute = ucontrol->value.integer.value[0];
  280. if (cs35l43->pcm_muted == false) {
  281. if (cs35l43->amp_mute == 0) /* Mute */
  282. vol = 0;
  283. else /* Unmute */
  284. vol = cs35l43->pcm_vol;
  285. /* convert control val to register val */
  286. if (vol < CS35L43_AMP_VOL_CTRL_DEFAULT)
  287. vol += CS35L43_AMP_VOL_PCM_MUTE;
  288. else
  289. vol -= CS35L43_AMP_VOL_CTRL_DEFAULT;
  290. regmap_update_bits(cs35l43->regmap,
  291. CS35L43_AMP_CTRL,
  292. CS35L43_AMP_VOL_PCM_MASK <<
  293. CS35L43_AMP_VOL_PCM_SHIFT,
  294. vol << CS35L43_AMP_VOL_PCM_SHIFT);
  295. }
  296. dev_info(cs35l43->dev, "%s: %s\n", __func__,
  297. cs35l43_amp_mute_text[ucontrol->value.integer.value[0]]);
  298. return 0;
  299. }
  300. static int cs35l43_pcm_vol_get(struct snd_kcontrol *kcontrol,
  301. struct snd_ctl_elem_value *ucontrol)
  302. {
  303. struct snd_soc_component *component;
  304. struct cs35l43_private *cs35l43;
  305. component = snd_soc_kcontrol_component(kcontrol);
  306. cs35l43 = snd_soc_component_get_drvdata(component);
  307. ucontrol->value.integer.value[0] = cs35l43->pcm_vol;
  308. return 0;
  309. }
  310. static int cs35l43_pcm_vol_put(struct snd_kcontrol *kcontrol,
  311. struct snd_ctl_elem_value *ucontrol)
  312. {
  313. struct snd_soc_component *component;
  314. struct cs35l43_private *cs35l43;
  315. component = snd_soc_kcontrol_component(kcontrol);
  316. cs35l43 = snd_soc_component_get_drvdata(component);
  317. if (ucontrol->value.integer.value[0] > 0x391 ||
  318. ucontrol->value.integer.value[0] < 0) {
  319. dev_err(cs35l43->dev, "%s: Invalid value 0x%lx\n",
  320. __func__, ucontrol->value.integer.value[0]);
  321. return -EINVAL;
  322. }
  323. dev_info(cs35l43->dev, "%s: 0x%lx\n", __func__,
  324. ucontrol->value.integer.value[0]);
  325. cs35l43->pcm_vol = ucontrol->value.integer.value[0];
  326. if (cs35l43->amp_mute == 0 || cs35l43->pcm_muted)
  327. return 0;
  328. else
  329. return snd_soc_put_volsw_sx(kcontrol, ucontrol);
  330. }
  331. static const char * const cs35l43_low_pwr_mode_text[] = {"Hibernate", "Standby"};
  332. static SOC_ENUM_SINGLE_DECL(low_pwr_mode_ctl,
  333. SND_SOC_NOPM, 0, cs35l43_low_pwr_mode_text);
  334. static int cs35l43_delta_select_get(struct snd_kcontrol *kcontrol,
  335. struct snd_ctl_elem_value *ucontrol)
  336. {
  337. struct snd_soc_component *component;
  338. struct cs35l43_private *cs35l43;
  339. component = snd_soc_kcontrol_component(kcontrol);
  340. cs35l43 = snd_soc_component_get_drvdata(component);
  341. ucontrol->value.integer.value[0] = cs35l43->delta_applied;
  342. return 0;
  343. }
  344. static int cs35l43_apply_delta_tuning(struct cs35l43_private *cs35l43)
  345. {
  346. const struct firmware *firmware;
  347. struct wm_adsp *dsp = &cs35l43->dsp;
  348. int ret = 0;
  349. char filename[NAME_MAX];
  350. if (!cs35l43->delta_requested ||
  351. cs35l43->delta_applied == cs35l43->delta_requested)
  352. return 0;
  353. dev_dbg(cs35l43->dev, "Applying delta file %d\n", cs35l43->delta_requested);
  354. snprintf(filename, NAME_MAX, "%s-delta-%d-spk-prot.bin",
  355. cs35l43->pdata.dsp_part_name, cs35l43->delta_requested);
  356. ret = request_firmware(&firmware, filename, cs35l43->dev);
  357. if (ret != 0) {
  358. dev_err(cs35l43->dev, "Failed to request '%s'\n", filename);
  359. return -EINVAL;
  360. }
  361. ret = cs_dsp_load_coeff(&dsp->cs_dsp, firmware, filename);
  362. if (ret)
  363. dev_err(cs35l43->dev, "Error applying delta file %s: %d\n",
  364. filename, ret);
  365. else
  366. cs35l43->delta_applied = cs35l43->delta_requested;
  367. return ret;
  368. }
  369. static int cs35l43_delta_select_put(struct snd_kcontrol *kcontrol,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct snd_soc_component *component =
  373. snd_soc_kcontrol_component(kcontrol);
  374. struct cs35l43_private *cs35l43 =
  375. snd_soc_component_get_drvdata(component);
  376. cs35l43->delta_requested = ucontrol->value.integer.value[0];
  377. mutex_lock(&cs35l43->hb_lock);
  378. if (cs35l43->hibernate_state == CS35L43_HIBERNATE_AWAKE)
  379. cs35l43_apply_delta_tuning(cs35l43);
  380. mutex_unlock(&cs35l43->hb_lock);
  381. return 0;
  382. }
  383. static int cs35l43_ultrasonic_mode_get(struct snd_kcontrol *kcontrol,
  384. struct snd_ctl_elem_value *ucontrol)
  385. {
  386. struct snd_soc_component *component;
  387. struct cs35l43_private *cs35l43;
  388. component = snd_soc_dapm_kcontrol_component(kcontrol);
  389. cs35l43 = snd_soc_component_get_drvdata(component);
  390. ucontrol->value.integer.value[0] = cs35l43->ultrasonic_mode;
  391. return 0;
  392. }
  393. static int cs35l43_ultrasonic_mode_put(struct snd_kcontrol *kcontrol,
  394. struct snd_ctl_elem_value *ucontrol)
  395. {
  396. struct snd_soc_component *component;
  397. struct cs35l43_private *cs35l43;
  398. unsigned int mon_rates, rx_rates, tx_rates, high_rate_enable;
  399. component = snd_soc_dapm_kcontrol_component(kcontrol);
  400. cs35l43 = snd_soc_component_get_drvdata(component);
  401. cs35l43->ultrasonic_mode = ucontrol->value.integer.value[0];
  402. switch (cs35l43->ultrasonic_mode) {
  403. case CS35L43_ULTRASONIC_MODE_INBAND:
  404. mon_rates = CS35L43_BASE_RATE;
  405. rx_rates = CS35L43_HIGH_RATE;
  406. tx_rates = CS35L43_HIGH_RATE;
  407. high_rate_enable = 1;
  408. break;
  409. case CS35L43_ULTRASONIC_MODE_OUT_OF_BAND:
  410. mon_rates = CS35L43_BASE_RATE;
  411. rx_rates = CS35L43_HIGH_RATE;
  412. tx_rates = CS35L43_HIGH_RATE;
  413. high_rate_enable = 1;
  414. break;
  415. case CS35L43_ULTRASONIC_MODE_DISABLED:
  416. default:
  417. mon_rates = CS35L43_BASE_RATE;
  418. rx_rates = CS35L43_BASE_RATE;
  419. tx_rates = CS35L43_BASE_RATE;
  420. high_rate_enable = 0;
  421. break;
  422. }
  423. pm_runtime_get_sync(cs35l43->dev);
  424. cs35l43_regmap_update_bits(cs35l43,
  425. CS35L43_DSP1_SAMPLE_RATE_RX1,
  426. CS35L43_DSP_RX1_RATE_MASK,
  427. rx_rates << CS35L43_DSP_RX1_RATE_SHIFT);
  428. cs35l43_regmap_update_bits(cs35l43,
  429. CS35L43_DSP1_SAMPLE_RATE_RX1,
  430. CS35L43_DSP_RX2_RATE_MASK,
  431. rx_rates << CS35L43_DSP_RX2_RATE_SHIFT);
  432. cs35l43_regmap_update_bits(cs35l43,
  433. CS35L43_DSP1_SAMPLE_RATE_RX1,
  434. CS35L43_DSP_RX3_RATE_MASK,
  435. rx_rates << CS35L43_DSP_RX3_RATE_SHIFT);
  436. cs35l43_regmap_update_bits(cs35l43,
  437. CS35L43_DSP1_SAMPLE_RATE_RX1,
  438. CS35L43_DSP_RX4_RATE_MASK,
  439. mon_rates << CS35L43_DSP_RX4_RATE_SHIFT);
  440. cs35l43_regmap_update_bits(cs35l43,
  441. CS35L43_DSP1_SAMPLE_RATE_RX2,
  442. CS35L43_DSP_RX5_RATE_MASK,
  443. mon_rates << CS35L43_DSP_RX5_RATE_SHIFT);
  444. cs35l43_regmap_update_bits(cs35l43,
  445. CS35L43_DSP1_SAMPLE_RATE_RX2,
  446. CS35L43_DSP_RX6_RATE_MASK,
  447. mon_rates << CS35L43_DSP_RX6_RATE_SHIFT);
  448. cs35l43_regmap_update_bits(cs35l43,
  449. CS35L43_DSP1_SAMPLE_RATE_TX1,
  450. CS35L43_DSP_TX1_RATE_MASK,
  451. tx_rates << CS35L43_DSP_TX1_RATE_SHIFT);
  452. cs35l43_regmap_update_bits(cs35l43,
  453. CS35L43_DSP1_SAMPLE_RATE_TX1,
  454. CS35L43_DSP_TX2_RATE_MASK,
  455. tx_rates << CS35L43_DSP_TX2_RATE_SHIFT);
  456. cs35l43_regmap_update_bits(cs35l43,
  457. CS35L43_DSP1_SAMPLE_RATE_TX1,
  458. CS35L43_DSP_TX3_RATE_MASK,
  459. tx_rates << CS35L43_DSP_TX3_RATE_SHIFT);
  460. cs35l43_regmap_update_bits(cs35l43,
  461. CS35L43_DSP1_SAMPLE_RATE_TX1,
  462. CS35L43_DSP_TX4_RATE_MASK,
  463. tx_rates << CS35L43_DSP_TX4_RATE_SHIFT);
  464. cs35l43_regmap_update_bits(cs35l43,
  465. CS35L43_DSP1_SAMPLE_RATE_TX2,
  466. CS35L43_DSP_TX5_RATE_MASK,
  467. CS35L43_BASE_RATE << CS35L43_DSP_TX5_RATE_SHIFT);
  468. cs35l43_regmap_update_bits(cs35l43,
  469. CS35L43_DSP1_SAMPLE_RATE_TX2,
  470. CS35L43_DSP_TX6_RATE_MASK,
  471. tx_rates << CS35L43_DSP_TX6_RATE_SHIFT);
  472. if (high_rate_enable) {
  473. cs35l43_regmap_update_bits(cs35l43, CS35L43_DAC_MSM_CONFIG,
  474. CS35L43_AMP_PCM_FSX2_EN_MASK,
  475. CS35L43_AMP_PCM_FSX2_EN_MASK);
  476. cs35l43_regmap_update_bits(cs35l43, CS35L43_MONITOR_FILT,
  477. CS35L43_VIMON_DUAL_RATE_MASK,
  478. CS35L43_VIMON_DUAL_RATE_MASK);
  479. } else {
  480. cs35l43_regmap_update_bits(cs35l43, CS35L43_DAC_MSM_CONFIG,
  481. CS35L43_AMP_PCM_FSX2_EN_MASK, 0);
  482. cs35l43_regmap_update_bits(cs35l43, CS35L43_MONITOR_FILT,
  483. CS35L43_VIMON_DUAL_RATE_MASK, 0);
  484. }
  485. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  486. CS35L43_MBOX_CMD_AUDIO_REINIT);
  487. pm_runtime_mark_last_busy(cs35l43->dev);
  488. pm_runtime_put_autosuspend(cs35l43->dev);
  489. return 0;
  490. }
  491. static int cs35l43_amp_en_get(struct snd_kcontrol *kcontrol,
  492. struct snd_ctl_elem_value *ucontrol)
  493. {
  494. return snd_soc_dapm_get_volsw(kcontrol, ucontrol);
  495. }
  496. static int cs35l43_amp_en_put(struct snd_kcontrol *kcontrol,
  497. struct snd_ctl_elem_value *ucontrol)
  498. {
  499. struct snd_soc_dapm_context *dapm;
  500. struct snd_soc_component *component;
  501. struct cs35l43_private *cs35l43;
  502. int ret = 0;
  503. dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  504. component = snd_soc_dapm_to_component(dapm);
  505. cs35l43 = snd_soc_component_get_drvdata(component);
  506. if (ucontrol->value.integer.value[0] != cs35l43->amp_switch) {
  507. if (ucontrol->value.integer.value[0]) {
  508. /* Switch enable */
  509. dev_info(cs35l43->dev, "AMP Enable Switch: enable\n");
  510. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE) {
  511. if (!pm_runtime_enabled(cs35l43->dev))
  512. pm_runtime_force_resume(cs35l43->dev);
  513. }
  514. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  515. } else {
  516. /* Switch disable */
  517. dev_info(cs35l43->dev, "AMP Enable Switch: disable\n");
  518. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  519. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE)
  520. pm_runtime_force_suspend(cs35l43->dev);
  521. }
  522. }
  523. cs35l43->amp_switch = ucontrol->value.integer.value[0];
  524. return ret;
  525. }
  526. static const struct snd_kcontrol_new amp_enable_ctrl = {
  527. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  528. .name = "Switch",
  529. .info = snd_soc_info_volsw,
  530. .get = cs35l43_amp_en_get,
  531. .put = cs35l43_amp_en_put,
  532. .private_value = SOC_SINGLE_VALUE(SND_SOC_NOPM, 0, 1, 0, 0)
  533. };
  534. static int cs35l43_reinit_get(struct snd_kcontrol *kcontrol,
  535. struct snd_ctl_elem_value *ucontrol)
  536. {
  537. ucontrol->value.integer.value[0] = 0;
  538. return 0;
  539. }
  540. static int cs35l43_reinit_put(struct snd_kcontrol *kcontrol,
  541. struct snd_ctl_elem_value *ucontrol)
  542. {
  543. struct snd_soc_component *component;
  544. struct cs35l43_private *cs35l43;
  545. int ret = 0;
  546. component = snd_soc_kcontrol_component(kcontrol);
  547. cs35l43 = snd_soc_component_get_drvdata(component);
  548. if (ucontrol->value.integer.value[0]) {
  549. pm_runtime_get_sync(cs35l43->dev);
  550. ret = cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  551. CS35L43_MBOX_CMD_AUDIO_REINIT);
  552. pm_runtime_mark_last_busy(cs35l43->dev);
  553. pm_runtime_put_autosuspend(cs35l43->dev);
  554. }
  555. return ret;
  556. }
  557. static const struct snd_kcontrol_new ultra_mux =
  558. SOC_DAPM_ENUM_EXT("Ultrasonic Mode", cs35l43_ultrasonic_mode_enum,
  559. cs35l43_ultrasonic_mode_get, cs35l43_ultrasonic_mode_put);
  560. static const struct snd_kcontrol_new cs35l43_aud_controls[] = {
  561. {.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "Digital PCM Volume",
  562. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  563. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  564. .tlv.p = dig_vol_tlv,
  565. .info = snd_soc_info_volsw_sx,
  566. .get = cs35l43_pcm_vol_get,
  567. .put = cs35l43_pcm_vol_put,
  568. .private_value = (unsigned long)&(struct soc_mixer_control)
  569. {.reg = CS35L43_AMP_CTRL, .rreg = CS35L43_AMP_CTRL,
  570. .shift = CS35L43_AMP_VOL_PCM_SHIFT, .rshift = CS35L43_AMP_VOL_PCM_SHIFT,
  571. .max = 0x391, .min = 0x4CF} },
  572. SOC_SINGLE("DC Watchdog Enable", CS35L43_ALIVE_DCIN_WD,
  573. CS35L43_DCIN_WD_EN_SHIFT, 1, 0),
  574. SOC_SINGLE("DC Watchdog Threshold", CS35L43_ALIVE_DCIN_WD,
  575. CS35L43_DCIN_WD_THLD_SHIFT, 0x28, 0),
  576. SOC_SINGLE("DC Watchdog Duration", CS35L43_ALIVE_DCIN_WD,
  577. CS35L43_DCIN_WD_DUR_SHIFT, 0x7, 0),
  578. SOC_ENUM("DC Watchdog Mode", cs35l43_dc_wd_mode_enum),
  579. SOC_SINGLE_TLV("Amp Gain", CS35L43_AMP_GAIN,
  580. CS35L43_AMP_GAIN_PCM_SHIFT, 20, 0,
  581. amp_gain_tlv),
  582. SOC_SINGLE_EXT("Reinit", SND_SOC_NOPM, 0, 1, 0,
  583. cs35l43_reinit_get, cs35l43_reinit_put),
  584. SOC_ENUM("PCM Soft Ramp", pcm_sft_ramp),
  585. SOC_SINGLE_RANGE("ASPTX1 Slot Position", CS35L43_ASP_FRAME_CONTROL1, 0, 0, 7, 0),
  586. SOC_SINGLE_RANGE("ASPTX2 Slot Position", CS35L43_ASP_FRAME_CONTROL1, 8, 0, 7, 0),
  587. SOC_SINGLE_RANGE("ASPTX3 Slot Position", CS35L43_ASP_FRAME_CONTROL1, 16, 0, 7, 0),
  588. SOC_SINGLE_RANGE("ASPTX4 Slot Position", CS35L43_ASP_FRAME_CONTROL1, 24, 0, 7, 0),
  589. SOC_SINGLE_RANGE("ASPRX1 Slot Position", CS35L43_ASP_FRAME_CONTROL5, 0, 0, 7, 0),
  590. SOC_SINGLE_RANGE("ASPRX2 Slot Position", CS35L43_ASP_FRAME_CONTROL5, 8, 0, 7, 0),
  591. SOC_SINGLE_RANGE("ASPRX3 Slot Position", CS35L43_ASP_FRAME_CONTROL5, 16, 0, 7, 0),
  592. SOC_ENUM("Boost Enable", bst_en_ctl),
  593. SOC_ENUM_EXT("AMP Mute", amp_mute_ctl, cs35l43_amp_mute_get, cs35l43_amp_mute_put),
  594. SOC_SINGLE_EXT("Delta Select", SND_SOC_NOPM, 0, 10, 0,
  595. cs35l43_delta_select_get, cs35l43_delta_select_put),
  596. WM_ADSP2_PRELOAD_SWITCH("DSP1", 1),
  597. WM_ADSP_FW_CONTROL("DSP1", 0),
  598. };
  599. static int cs35l43_dsp_preload_ev(struct snd_soc_dapm_widget *w,
  600. struct snd_kcontrol *kcontrol, int event);
  601. static int cs35l43_dsp_audio_ev(struct snd_soc_dapm_widget *w,
  602. struct snd_kcontrol *kcontrol, int event);
  603. static int cs35l43_main_amp_event(struct snd_soc_dapm_widget *w,
  604. struct snd_kcontrol *kcontrol, int event);
  605. static int cs35l43_write_seq_elem_update(struct cs35l43_write_seq_elem *write_seq_elem,
  606. unsigned int addr, unsigned int value)
  607. {
  608. switch (write_seq_elem->operation) {
  609. case CS35L43_POWER_SEQ_OP_WRITE_REG_FULL:
  610. write_seq_elem->words[0] = (addr & 0xFFFF0000) >> 16;
  611. write_seq_elem->words[1] = ((addr & 0xFFFF) << 8) |
  612. ((value & 0xFF000000) >> 24);
  613. write_seq_elem->words[2] = (value & 0xFFFFFF);
  614. break;
  615. case CS35L43_POWER_SEQ_OP_WRITE_REG_ADDR8:
  616. write_seq_elem->words[0] = (CS35L43_POWER_SEQ_OP_WRITE_REG_ADDR8 << 16) |
  617. ((addr & 0xFF) << 8) |
  618. ((value & 0xFF000000) >> 24);
  619. write_seq_elem->words[1] = (value & 0xFFFFFF);
  620. break;
  621. case CS35L43_POWER_SEQ_OP_WRITE_REG_L16:
  622. write_seq_elem->words[0] = (CS35L43_POWER_SEQ_OP_WRITE_REG_L16 << 16) |
  623. ((addr & 0xFFFF00) >> 8);
  624. write_seq_elem->words[1] = ((addr & 0xFF) << 16) | (value & 0xFFFF);
  625. break;
  626. default:
  627. break;
  628. }
  629. return 0;
  630. }
  631. static int cs35l43_write_seq_add(struct cs35l43_private *cs35l43,
  632. struct cs35l43_write_seq *sequence,
  633. unsigned int update_reg, unsigned int update_value,
  634. bool read)
  635. {
  636. struct device *dev = cs35l43->dev;
  637. u32 *buf, *op_words, addr = 0, prev_addr = 0, value = 0;
  638. u8 operation = CS35L43_POWER_SEQ_OP_END;
  639. unsigned int i, j, num_words, ret = 0;
  640. struct cs35l43_write_seq_elem *write_seq_elem;
  641. buf = kzalloc(sizeof(u32) * sequence->length, GFP_KERNEL);
  642. if (!buf) {
  643. dev_err(cs35l43->dev, "%s: failed to alloc write seq\n",
  644. __func__);
  645. return -ENOMEM;
  646. }
  647. ret = wm_adsp_read_ctl(&cs35l43->dsp, sequence->name,
  648. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, (void *)buf,
  649. sequence->length * sizeof(u32));
  650. if (ret != 0) {
  651. dev_err(dev, "%s: Failed to read control\n", __func__);
  652. goto exit;
  653. }
  654. for (i = 0; i < sequence->length; i++) {
  655. buf[i] = be32_to_cpu(buf[i]);
  656. dev_dbg(dev, "%s[%d] = 0x%x\n", sequence->name, i, buf[i]);
  657. }
  658. list_for_each_entry(write_seq_elem, &sequence->list_head, list) {
  659. switch (write_seq_elem->operation) {
  660. case CS35L43_POWER_SEQ_OP_WRITE_REG_FULL:
  661. addr = ((write_seq_elem->words[0] & 0xFFFF) << 16) |
  662. ((write_seq_elem->words[1] & 0xFFFF00) >> 8);
  663. value = ((write_seq_elem->words[1] & 0xFF) << 24) |
  664. (write_seq_elem->words[2] & 0xFFFFFF);
  665. break;
  666. case CS35L43_POWER_SEQ_OP_WRITE_REG_ADDR8:
  667. addr = (prev_addr & 0xFFFFFF00) |
  668. ((write_seq_elem->words[0] & 0xFF00) >> 8);
  669. value = ((write_seq_elem->words[0] & 0xFF) << 24) |
  670. (write_seq_elem->words[1] & 0xFFFFFF);
  671. break;
  672. case CS35L43_POWER_SEQ_OP_WRITE_REG_L16:
  673. addr = ((write_seq_elem->words[0] & 0xFFFF) << 8) |
  674. ((write_seq_elem->words[1] & 0xFF0000) >> 16);
  675. value = (write_seq_elem->words[1] & 0xFFFF);
  676. break;
  677. default:
  678. break;
  679. }
  680. dev_dbg(dev, "write seq elem: addr=0x%x, prev_addr=0x%x, val=0x%x\n",
  681. addr, prev_addr, value);
  682. prev_addr = addr;
  683. if (addr == update_reg) {
  684. if (read)
  685. cs35l43_regmap_read(cs35l43, addr, &update_value);
  686. dev_dbg(dev, "%s: Updating register 0x%x with value 0x%x\n",
  687. __func__, addr, update_value);
  688. cs35l43_write_seq_elem_update(write_seq_elem, update_reg, update_value);
  689. memcpy(buf + write_seq_elem->offset, write_seq_elem->words,
  690. write_seq_elem->size * sizeof(u32));
  691. goto write_exit;
  692. }
  693. }
  694. i = 0;
  695. while (i < sequence->length) {
  696. operation = (buf[i] & CS35L43_POWER_SEQ_OP_MASK) >>
  697. CS35L43_POWER_SEQ_OP_SHIFT;
  698. if (operation == CS35L43_POWER_SEQ_OP_END)
  699. break;
  700. /* get num words for given operation */
  701. for (j = 0; j < CS35L43_POWER_SEQ_NUM_OPS; j++) {
  702. if (cs35l43_write_seq_op_sizes[j][0] == operation) {
  703. num_words = cs35l43_write_seq_op_sizes[j][1];
  704. break;
  705. }
  706. }
  707. i += num_words;
  708. }
  709. if (operation != CS35L43_POWER_SEQ_OP_END ||
  710. i + CS35L43_POWER_SEQ_OP_WRITE_REG_FULL_WORDS +
  711. CS35L43_POWER_SEQ_OP_END_WORDS > sequence->length) {
  712. dev_err(dev, "WRITE SEQ END_OF_SCRIPT not found or sequence full\n");
  713. ret = -E2BIG;
  714. goto exit;
  715. }
  716. write_seq_elem = devm_kzalloc(dev, sizeof(*write_seq_elem), GFP_KERNEL);
  717. if (!write_seq_elem) {
  718. ret = -ENOMEM;
  719. goto exit;
  720. }
  721. write_seq_elem->size = CS35L43_POWER_SEQ_OP_WRITE_REG_FULL_WORDS;
  722. write_seq_elem->offset = i;
  723. write_seq_elem->operation = CS35L43_POWER_SEQ_OP_WRITE_REG_FULL;
  724. op_words = kzalloc(write_seq_elem->size * sizeof(u32), GFP_KERNEL);
  725. if (!op_words) {
  726. ret = -ENOMEM;
  727. goto err_elem;
  728. }
  729. write_seq_elem->words = op_words;
  730. if (read)
  731. cs35l43_regmap_read(cs35l43, update_reg, &update_value);
  732. cs35l43_write_seq_elem_update(write_seq_elem, update_reg, update_value);
  733. list_add_tail(&write_seq_elem->list, &sequence->list_head);
  734. sequence->num_ops++;
  735. memcpy(&buf[i], op_words, write_seq_elem->size * sizeof(u32));
  736. dev_dbg(dev, "%s: Added register 0x%x with value 0x%x\n",
  737. __func__, update_reg, update_value);
  738. for (i = 0; i < write_seq_elem->size; i++)
  739. dev_dbg(dev, "elem[%d]: 0x%x\n", i, write_seq_elem->words[i]);
  740. buf[write_seq_elem->offset + write_seq_elem->size] = 0xFFFFFFFF;
  741. write_exit:
  742. for (i = 0; i < sequence->length; i++) {
  743. dev_dbg(dev, "%s[%d] = 0x%x\n", sequence->name, i, buf[i]);
  744. buf[i] = cpu_to_be32(buf[i]);
  745. }
  746. ret = wm_adsp_write_ctl(&cs35l43->dsp, sequence->name,
  747. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, (void *)buf,
  748. sequence->length * sizeof(u32));
  749. goto exit;
  750. err_elem:
  751. kfree(write_seq_elem);
  752. exit:
  753. kfree(buf);
  754. return ret;
  755. }
  756. static int cs35l43_write_seq_update(struct cs35l43_private *cs35l43,
  757. struct cs35l43_write_seq *sequence)
  758. {
  759. struct device *dev = cs35l43->dev;
  760. u32 *buf;
  761. u32 addr = 0, prev_addr = 0, value = 0, reg_value = 0;
  762. unsigned int ret = 0, i;
  763. struct cs35l43_write_seq_elem *write_seq_elem;
  764. buf = kzalloc(sizeof(u32) * sequence->length, GFP_KERNEL);
  765. if (!buf) {
  766. dev_err(cs35l43->dev, "%s: failed to alloc write seq\n",
  767. __func__);
  768. return -ENOMEM;
  769. }
  770. ret = wm_adsp_read_ctl(&cs35l43->dsp, sequence->name,
  771. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, (void *)buf,
  772. sequence->length * sizeof(u32));
  773. if (ret != 0) {
  774. dev_err(dev, "%s: Failed to read control\n", __func__);
  775. goto err_free;
  776. }
  777. for (i = 0; i < sequence->length; i++) {
  778. buf[i] = be32_to_cpu(buf[i]);
  779. dev_dbg(dev, "%s[%d] = 0x%x\n", sequence->name, i, buf[i]);
  780. }
  781. dev_dbg(dev, "%s num ops: %d\n", sequence->name, sequence->num_ops);
  782. dev_dbg(dev, "offset\tsize\twords\n");
  783. list_for_each_entry(write_seq_elem, &sequence->list_head, list) {
  784. switch (write_seq_elem->operation) {
  785. case CS35L43_POWER_SEQ_OP_WRITE_REG_FULL:
  786. addr = ((write_seq_elem->words[0] & 0xFFFF) << 16) |
  787. ((write_seq_elem->words[1] & 0xFFFF00) >> 8);
  788. value = ((write_seq_elem->words[1] & 0xFF) << 24) |
  789. (write_seq_elem->words[2] & 0xFFFFFF);
  790. break;
  791. case CS35L43_POWER_SEQ_OP_WRITE_REG_ADDR8:
  792. addr = (prev_addr & 0xFFFFFF00) |
  793. ((write_seq_elem->words[0] & 0xFF00) >> 8);
  794. value = ((write_seq_elem->words[0] & 0xFF) << 24) |
  795. (write_seq_elem->words[1] & 0xFFFFFF);
  796. break;
  797. case CS35L43_POWER_SEQ_OP_WRITE_REG_L16:
  798. addr = ((write_seq_elem->words[0] & 0xFFFF) << 8) |
  799. ((write_seq_elem->words[1] & 0xFF0000) >> 16);
  800. value = (write_seq_elem->words[1] & 0xFFFF);
  801. break;
  802. default:
  803. break;
  804. }
  805. dev_dbg(dev, "write seq elem: addr=0x%x, prev_addr=0x%x, val=0x%x\n",
  806. addr, prev_addr, value);
  807. prev_addr = addr;
  808. cs35l43_regmap_read(cs35l43, addr, &reg_value);
  809. if (reg_value != value && addr != CS35L43_TEST_KEY_CTRL &&
  810. cs35l43_readable_reg(dev, addr)) {
  811. dev_dbg(dev,
  812. "%s: Updating register 0x%x with value 0x%x\t(prev value: 0x%x)\n",
  813. __func__, addr, reg_value, value);
  814. cs35l43_write_seq_elem_update(write_seq_elem, addr, reg_value);
  815. memcpy(buf + write_seq_elem->offset, write_seq_elem->words,
  816. write_seq_elem->size * sizeof(u32));
  817. for (i = 0; i < write_seq_elem->size; i++)
  818. dev_dbg(dev, "elem[%d]: 0x%x\n", i, write_seq_elem->words[i]);
  819. }
  820. }
  821. for (i = 0; i < sequence->length; i++) {
  822. dev_dbg(dev, "%s[%d] = 0x%x\n", sequence->name, i, buf[i]);
  823. buf[i] = cpu_to_be32(buf[i]);
  824. }
  825. ret = wm_adsp_write_ctl(&cs35l43->dsp, sequence->name,
  826. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, (void *)buf,
  827. sequence->length * sizeof(u32));
  828. err_free:
  829. kfree(buf);
  830. return ret;
  831. }
  832. static int cs35l43_write_seq_init(struct cs35l43_private *cs35l43,
  833. struct cs35l43_write_seq *sequence)
  834. {
  835. struct device *dev = cs35l43->dev;
  836. u32 *buf, *op_words;
  837. u8 operation = CS35L43_POWER_SEQ_OP_END;
  838. unsigned int i, j, num_words, ret = 0;
  839. struct cs35l43_write_seq_elem *write_seq_elem;
  840. INIT_LIST_HEAD(&sequence->list_head);
  841. sequence->num_ops = 0;
  842. buf = kzalloc(sizeof(u32) * sequence->length, GFP_KERNEL);
  843. if (!buf) {
  844. dev_err(cs35l43->dev, "%s: failed to alloc write seq\n",
  845. __func__);
  846. return -ENOMEM;
  847. }
  848. ret = wm_adsp_read_ctl(&cs35l43->dsp, sequence->name,
  849. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, (void *)buf,
  850. sequence->length * sizeof(u32));
  851. if (ret != 0) {
  852. dev_err(dev, "%s: Failed to read control\n", __func__);
  853. goto err_free;
  854. }
  855. for (i = 0; i < sequence->length; i++) {
  856. buf[i] = be32_to_cpu(buf[i]);
  857. dev_dbg(dev, "%s[%d] = 0x%x\n", sequence->name, i, buf[i]);
  858. }
  859. i = 0;
  860. while (i < sequence->length) {
  861. operation = (buf[i] & CS35L43_POWER_SEQ_OP_MASK) >>
  862. CS35L43_POWER_SEQ_OP_SHIFT;
  863. if (operation == CS35L43_POWER_SEQ_OP_END)
  864. break;
  865. /* get num words for given operation */
  866. for (j = 0; j < CS35L43_POWER_SEQ_NUM_OPS; j++) {
  867. if (cs35l43_write_seq_op_sizes[j][0] == operation) {
  868. num_words = cs35l43_write_seq_op_sizes[j][1];
  869. break;
  870. }
  871. }
  872. if (j == CS35L43_POWER_SEQ_NUM_OPS) {
  873. dev_err(dev, "Failed to determine op size\n");
  874. ret = -EINVAL;
  875. goto err_free;
  876. }
  877. op_words = kzalloc(num_words * sizeof(u32), GFP_KERNEL);
  878. if (!op_words) {
  879. ret = -ENOMEM;
  880. goto err_free;
  881. }
  882. memcpy(op_words, &buf[i], num_words * sizeof(u32));
  883. write_seq_elem = devm_kzalloc(dev, sizeof(*write_seq_elem), GFP_KERNEL);
  884. if (!write_seq_elem) {
  885. ret = -ENOMEM;
  886. goto err_parse;
  887. }
  888. write_seq_elem->size = num_words;
  889. write_seq_elem->offset = i;
  890. write_seq_elem->operation = operation;
  891. write_seq_elem->words = op_words;
  892. list_add_tail(&write_seq_elem->list, &sequence->list_head);
  893. sequence->num_ops++;
  894. i += num_words;
  895. }
  896. dev_dbg(dev, "%s num ops: %d\n", sequence->name, sequence->num_ops);
  897. dev_dbg(dev, "offset\tsize\twords\n");
  898. list_for_each_entry(write_seq_elem, &sequence->list_head, list) {
  899. dev_dbg(dev, "0x%04X\t%d", write_seq_elem->offset,
  900. write_seq_elem->size);
  901. for (j = 0; j < write_seq_elem->size; j++)
  902. dev_dbg(dev, "0x%08X", *(write_seq_elem->words + j));
  903. }
  904. if (operation != CS35L43_POWER_SEQ_OP_END) {
  905. dev_err(dev, "WRITE SEQ END_OF_SCRIPT not found\n");
  906. ret = -E2BIG;
  907. }
  908. kfree(buf);
  909. return ret;
  910. err_parse:
  911. kfree(op_words);
  912. err_free:
  913. kfree(buf);
  914. return ret;
  915. }
  916. static int cs35l43_dsp_reset(struct cs35l43_private *cs35l43)
  917. {
  918. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cs35l43->component);
  919. struct snd_soc_dapm_widget fake_dapm_widget = {.dapm = dapm};
  920. unsigned int val = 0, dsprx2 = CS35L43_INPUT_SRC_ASPRX1;
  921. int ret, retry = 10;
  922. if (!dapm || !cs35l43->component || !cs35l43->dsp.cs_dsp.booted)
  923. return -EINVAL;
  924. dev_info(cs35l43->dev, "%s\n", __func__);
  925. if (pm_runtime_enabled(cs35l43->dev))
  926. pm_runtime_get_sync(cs35l43->dev);
  927. regmap_write(cs35l43->regmap, CS35L43_DSP_VIRTUAL1_MBOX_1,
  928. CS35L43_MBOX_CMD_PREVENT_HIBERNATE);
  929. regmap_write(cs35l43->regmap, CS35L43_DSP_VIRTUAL1_MBOX_1,
  930. CS35L43_MBOX_CMD_AUDIO_PAUSE);
  931. usleep_range(5000, 5100);
  932. /* Disable DSP and reset state variables */
  933. regmap_write(cs35l43->regmap, CS35L43_DSP1_CCM_CORE_CONTROL, 0x80);
  934. regmap_write(cs35l43->regmap, CS35L43_PWRMGT_CTL, 0);
  935. /* Disable WDT */
  936. regmap_write(cs35l43->regmap, CS35L43_DSP1_WDT_CONTROL, 0x111000);
  937. /* Clear WDT status */
  938. regmap_write(cs35l43->regmap, CS35L43_DSP1_WDT_STATUS, 0x03);
  939. ret = wm_adsp_write_ctl(&cs35l43->dsp, "CALL_RAM_INIT", WMFW_ADSP2_XM,
  940. cs35l43->dsp.cs_dsp.fw_id,
  941. &val, sizeof(u32));
  942. if (ret < 0)
  943. dev_err(cs35l43->dev, "Failed to clear CALL_RAM_INIT\n");
  944. ret = wm_adsp_write_ctl(&cs35l43->dsp, "HALO_STATE", WMFW_ADSP2_XM,
  945. cs35l43->dsp.cs_dsp.fw_id,
  946. &val, sizeof(u32));
  947. if (ret < 0)
  948. dev_err(cs35l43->dev, "Failed to clear HALO_STATE\n");
  949. ret = wm_adsp_write_ctl(&cs35l43->dsp, "AUDIO_STATE", WMFW_ADSP2_XM, 0x5f212,
  950. &val, sizeof(u32));
  951. if (ret < 0)
  952. dev_err(cs35l43->dev, "Failed to clear AUDIO_STATE\n");
  953. regmap_write(cs35l43->regmap, CS35L43_DSP1_MPU_LOCK_STATE, 0x5555);
  954. regmap_write(cs35l43->regmap, CS35L43_DSP1_MPU_LOCK_STATE, 0xAAAA);
  955. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_XM_VIO_STATUS, &val);
  956. regmap_write(cs35l43->regmap, CS35L43_DSP1_MPU_XM_VIO_STATUS, val);
  957. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_YM_VIO_STATUS, &val);
  958. regmap_write(cs35l43->regmap, CS35L43_DSP1_MPU_YM_VIO_STATUS, val);
  959. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_PM_VIO_STATUS, &val);
  960. regmap_write(cs35l43->regmap, CS35L43_DSP1_MPU_PM_VIO_STATUS, val);
  961. regmap_write(cs35l43->regmap, CS35L43_DSP1_CCM_CORE_CONTROL, 0x281);
  962. do {
  963. usleep_range(10000, 10100);
  964. wm_adsp_read_ctl(&cs35l43->dsp, "HALO_STATE",
  965. WMFW_ADSP2_XM, cs35l43->dsp.cs_dsp.fw_id, &val, sizeof(u32));
  966. val = be32_to_cpu(val);
  967. dev_info(cs35l43->dev, "halo_state: %x\n", val);
  968. } while (val != 2 && retry-- >= 0);
  969. if (retry < 0)
  970. dev_err(cs35l43->dev, "%s: Cold boot failed\n", __func__);
  971. regmap_write(cs35l43->regmap, CS35L43_DSP_VIRTUAL1_MBOX_1,
  972. CS35L43_MBOX_CMD_PREVENT_HIBERNATE);
  973. regmap_write(cs35l43->regmap, CS35L43_DSP_VIRTUAL1_MBOX_1,
  974. CS35L43_MBOX_CMD_AUDIO_PAUSE);
  975. usleep_range(5000, 5100);
  976. cs35l43_log_status(cs35l43);
  977. /* Disable WDT */
  978. regmap_write(cs35l43->regmap, CS35L43_DSP1_WDT_CONTROL, 0x111000);
  979. /* Clear WDT status */
  980. regmap_write(cs35l43->regmap, CS35L43_DSP1_WDT_STATUS, 0x03);
  981. val = 0;
  982. ret = wm_adsp_write_ctl(&cs35l43->dsp, "AUDIO_STATE", WMFW_ADSP2_XM, 0x5f212,
  983. &val, sizeof(u32));
  984. if (ret < 0)
  985. dev_err(cs35l43->dev, "Failed to clear AUDIO_STATE\n");
  986. regmap_write(cs35l43->regmap, CS35L43_DSP1_CCM_CORE_CONTROL, 0x80);
  987. val = cpu_to_be32(1);
  988. ret = wm_adsp_write_ctl(&cs35l43->dsp, "CALL_RAM_INIT", WMFW_ADSP2_XM,
  989. cs35l43->dsp.cs_dsp.fw_id,
  990. &val, sizeof(u32));
  991. if (ret < 0)
  992. dev_err(cs35l43->dev, "Failed to set CALL_RAM_INIT\n");
  993. cs35l43->dsp.preloaded = 0;
  994. cs35l43_dsp_audio_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_PRE_PMD);
  995. cs35l43_main_amp_event(&fake_dapm_widget, NULL, SND_SOC_DAPM_POST_PMD);
  996. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_PRE_PMD);
  997. regmap_write(cs35l43->regmap, CS35L43_GLOBAL_ENABLES, 0);
  998. usleep_range(5000, 5100);
  999. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_PRE_PMU);
  1000. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_POST_PMU);
  1001. cs35l43_dsp_audio_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_POST_PMU);
  1002. /* Force update from cache */
  1003. regmap_read(cs35l43->regmap, CS35L43_DSP1RX2_INPUT, &dsprx2);
  1004. regcache_drop_region(cs35l43->regmap, CS35L43_DSP1RX2_INPUT, CS35L43_DSP1RX2_INPUT);
  1005. regmap_write(cs35l43->regmap, CS35L43_DSP1RX2_INPUT, dsprx2);
  1006. cs35l43_main_amp_event(&fake_dapm_widget, NULL, SND_SOC_DAPM_POST_PMU);
  1007. cs35l43->dsp.preloaded = 1;
  1008. if (cs35l43_check_dsp_regs(cs35l43) != 0)
  1009. dev_err(cs35l43->dev, "Failed to reset DSP\n");
  1010. /* Sync values that may have been changed by write seq */
  1011. regcache_sync_region(cs35l43->regmap, CS35L43_DEVID,
  1012. CS35L43_MIXER_NGATE_CH2_CFG);
  1013. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE) {
  1014. if (cs35l43->write_seq_initialized)
  1015. cs35l43_write_seq_update(cs35l43, &cs35l43->power_on_seq);
  1016. }
  1017. if (pm_runtime_enabled(cs35l43->dev))
  1018. pm_runtime_put_autosuspend(cs35l43->dev);
  1019. return ret;
  1020. }
  1021. static void cs35l43_error_work(struct work_struct *wk)
  1022. {
  1023. struct cs35l43_private *cs35l43;
  1024. cs35l43 = container_of(wk, struct cs35l43_private, err_work);
  1025. mutex_lock(&cs35l43->err_lock);
  1026. cs35l43_dsp_reset(cs35l43);
  1027. mutex_unlock(&cs35l43->err_lock);
  1028. }
  1029. static int cs35l43_dsp_preload_ev(struct snd_soc_dapm_widget *w,
  1030. struct snd_kcontrol *kcontrol, int event)
  1031. {
  1032. struct snd_soc_component *component =
  1033. snd_soc_dapm_to_component(w->dapm);
  1034. struct cs35l43_private *cs35l43 =
  1035. snd_soc_component_get_drvdata(component);
  1036. switch (event) {
  1037. case SND_SOC_DAPM_PRE_PMU:
  1038. if (cs35l43->dsp.cs_dsp.booted)
  1039. return 0;
  1040. if (cs35l43->limit_spi_clock) {
  1041. cs35l43_regmap_update_bits(cs35l43,
  1042. CS35L43_REFCLK_INPUT,
  1043. CS35L43_PLL_FORCE_EN_MASK,
  1044. CS35L43_PLL_FORCE_EN_MASK);
  1045. cs35l43->limit_spi_clock(cs35l43, false);
  1046. }
  1047. wm_adsp_early_event(w, kcontrol, event);
  1048. break;
  1049. case SND_SOC_DAPM_POST_PMU:
  1050. if (cs35l43->dsp.cs_dsp.running)
  1051. return 0;
  1052. cs35l43_regmap_write(cs35l43, CS35L43_PWRMGT_CTL, CS35L43_MEM_RDY);
  1053. wm_adsp_event(w, kcontrol, event);
  1054. cirrus_cal_apply(cs35l43->pdata.mfd_suffix);
  1055. cs35l43->first_event = 0;
  1056. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_STANDBY)
  1057. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1058. CS35L43_MBOX_CMD_PREVENT_HIBERNATE);
  1059. cs35l43->delta_applied = 0;
  1060. if (cs35l43->limit_spi_clock) {
  1061. cs35l43->limit_spi_clock(cs35l43, true);
  1062. cs35l43_regmap_update_bits(cs35l43,
  1063. CS35L43_REFCLK_INPUT,
  1064. CS35L43_PLL_FORCE_EN_MASK,
  1065. 0);
  1066. }
  1067. break;
  1068. case SND_SOC_DAPM_PRE_PMD:
  1069. if (cs35l43->dsp.preloaded)
  1070. return 0;
  1071. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE &&
  1072. cs35l43->hibernate_state == CS35L43_HIBERNATE_STANDBY) {
  1073. mutex_lock(&cs35l43->hb_lock);
  1074. cs35l43_exit_hibernate(cs35l43);
  1075. mutex_unlock(&cs35l43->hb_lock);
  1076. }
  1077. wm_adsp_early_event(w, kcontrol, event);
  1078. wm_adsp_event(w, kcontrol, event);
  1079. cs35l43->hibernate_state = CS35L43_HIBERNATE_NOT_LOADED;
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. return 0;
  1085. }
  1086. static int cs35l43_dsp_audio_ev(struct snd_soc_dapm_widget *w,
  1087. struct snd_kcontrol *kcontrol, int event)
  1088. {
  1089. struct snd_soc_component *component =
  1090. snd_soc_dapm_to_component(w->dapm);
  1091. struct cs35l43_private *cs35l43 =
  1092. snd_soc_component_get_drvdata(component);
  1093. unsigned int audio_state;
  1094. dev_info(cs35l43->dev, "%s\n", __func__);
  1095. if (!cs35l43->dsp.cs_dsp.running)
  1096. return 0;
  1097. switch (event) {
  1098. case SND_SOC_DAPM_POST_PMU:
  1099. if (!cs35l43->first_event) {
  1100. regmap_update_bits(cs35l43->regmap, CS35L43_IRQ1_MASK_1,
  1101. CS35L43_DSP_VIRTUAL2_MBOX_WR_EINT1_MASK, 0);
  1102. cs35l43->first_event = true;
  1103. }
  1104. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1105. CS35L43_MBOX_CMD_AUDIO_PLAY);
  1106. usleep_range(2000, 2200);
  1107. wm_adsp_read_ctl(&cs35l43->dsp, "AUDIO_STATE",
  1108. WMFW_ADSP2_XM, 0x5f212, &audio_state, sizeof(u32));
  1109. audio_state = be32_to_cpu(audio_state);
  1110. dev_info(cs35l43->dev, "PMU audio state post: 0x%x\n", audio_state);
  1111. if (audio_state != CS35L43_AUDIO_STATE_WAITING &&
  1112. audio_state != CS35L43_AUDIO_STATE_RUNNING &&
  1113. audio_state != CS35L43_AUDIO_STATE_RAMPDOWN &&
  1114. audio_state != CS35L43_AUDIO_STATE_AUX_NG_MUTED) {
  1115. dev_err(cs35l43->dev, "Failed to set MBOX cmd PLAY\n");
  1116. cs35l43_log_dsp_err(cs35l43);
  1117. cs35l43->mbox_err_pmu++;
  1118. } else
  1119. cs35l43->mbox_err_pmu = 0;
  1120. if (cs35l43_check_dsp_regs(cs35l43) != 0) {
  1121. if (!mutex_is_locked(&cs35l43->err_lock))
  1122. queue_work(cs35l43->err_wq, &cs35l43->err_work);
  1123. }
  1124. break;
  1125. case SND_SOC_DAPM_PRE_PMD:
  1126. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1127. CS35L43_MBOX_CMD_AUDIO_PAUSE);
  1128. usleep_range(2000, 2200);
  1129. wm_adsp_read_ctl(&cs35l43->dsp, "AUDIO_STATE",
  1130. WMFW_ADSP2_XM, 0x5f212, &audio_state, sizeof(u32));
  1131. audio_state = be32_to_cpu(audio_state);
  1132. dev_info(cs35l43->dev, "PMD audio state post: 0x%x\n", audio_state);
  1133. if (audio_state != CS35L43_AUDIO_STATE_READY &&
  1134. audio_state != CS35L43_AUDIO_STATE_RUNNING &&
  1135. audio_state != CS35L43_AUDIO_STATE_RAMPDOWN) {
  1136. dev_err(cs35l43->dev, "Failed to set MBOX cmd PAUSE\n");
  1137. cs35l43_log_dsp_err(cs35l43);
  1138. cs35l43->mbox_err_pmd++;
  1139. } else
  1140. cs35l43->mbox_err_pmd = 0;
  1141. if (cs35l43_check_dsp_regs(cs35l43) != 0 ||
  1142. cs35l43->mbox_err_pmu + cs35l43->mbox_err_pmd >= 2) {
  1143. if (!mutex_is_locked(&cs35l43->err_lock))
  1144. queue_work(cs35l43->err_wq, &cs35l43->err_work);
  1145. }
  1146. break;
  1147. default:
  1148. break;
  1149. }
  1150. return 0;
  1151. }
  1152. static int cs35l43_check_dsp_regs(struct cs35l43_private *cs35l43)
  1153. {
  1154. int ret = 0;
  1155. unsigned int val;
  1156. unsigned int pm_state, audio_state;
  1157. ret = wm_adsp_read_ctl(&cs35l43->dsp, "HALO_STATE", WMFW_ADSP2_XM,
  1158. cs35l43->dsp.cs_dsp.fw_id,
  1159. &val, sizeof(u32));
  1160. if (ret < 0) {
  1161. dev_err(cs35l43->dev, "Failed to read HALO_STATE\n");
  1162. return ret;
  1163. }
  1164. val = be32_to_cpu(val);
  1165. if (val != 2) {
  1166. dev_err(cs35l43->dev, "%s: Error HALO_STATE = %u\n", __func__, val);
  1167. return -EINVAL;
  1168. }
  1169. ret = wm_adsp_read_ctl(&cs35l43->dsp, "ERROR", WMFW_ADSP2_XM, 0x5f212,
  1170. &val, sizeof(u32));
  1171. if (ret < 0) {
  1172. dev_err(cs35l43->dev, "Failed to read AUDIO_SYSTEM ERROR\n");
  1173. return ret;
  1174. }
  1175. val = be32_to_cpu(val);
  1176. if (val != 0) {
  1177. dev_err(cs35l43->dev, "%s: Error AUDIO_SYSTEM ERROR = %u\n", __func__, val);
  1178. return -EINVAL;
  1179. }
  1180. regmap_read(cs35l43->regmap, CS35L43_DSP1_SCRATCH1, &val);
  1181. if (val)
  1182. ret = -EINVAL;
  1183. regmap_read(cs35l43->regmap, CS35L43_DSP1_SCRATCH2, &val);
  1184. if (val)
  1185. ret = -EINVAL;
  1186. regmap_read(cs35l43->regmap, CS35L43_DSP1_SCRATCH3, &val);
  1187. if (val)
  1188. ret = -EINVAL;
  1189. regmap_read(cs35l43->regmap, CS35L43_DSP1_SCRATCH4, &val);
  1190. if (val)
  1191. ret = -EINVAL;
  1192. if (ret) {
  1193. dev_err(cs35l43->dev, "%s: Error DSP SCRATCH\n", __func__);
  1194. return ret;
  1195. }
  1196. wm_adsp_read_ctl(&cs35l43->dsp, "PM_CUR_STATE",
  1197. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, &pm_state, sizeof(u32));
  1198. pm_state = be32_to_cpu(pm_state);
  1199. wm_adsp_read_ctl(&cs35l43->dsp, "AUDIO_STATE",
  1200. WMFW_ADSP2_XM, 0x5f212, &audio_state, sizeof(u32));
  1201. audio_state = be32_to_cpu(audio_state);
  1202. dev_info(cs35l43->dev, "PM_STATE: 0x%x\tAUDIO_STATE: 0x%x\n",
  1203. pm_state, audio_state);
  1204. if (pm_state == 0x2 && audio_state == CS35L43_AUDIO_STATE_RAMPDOWN) {
  1205. dev_err(cs35l43->dev, "%s: Error PM_STATE and AUDIO_STATE\n", __func__);
  1206. return -EINVAL;
  1207. }
  1208. return 0;
  1209. }
  1210. static int cs35l43_log_dsp_err(struct cs35l43_private *cs35l43)
  1211. {
  1212. int i;
  1213. unsigned int reg;
  1214. struct cs35l43_dsp_reg regs[] = {
  1215. { "PM_CUR_STATE", CS35L43_ALG_ID_PM },
  1216. { "AUDIO_STATE", 0x5f212 },
  1217. { "ERROR", 0x5f212 },
  1218. { "HALO_STATE", cs35l43->dsp.cs_dsp.fw_id },
  1219. { "HALO_HEARTBEAT", cs35l43->dsp.cs_dsp.fw_id },
  1220. { "AUDIO_BLK_SIZE", cs35l43->dsp.cs_dsp.fw_id },
  1221. { "RAM_INIT_COUNT", 0x5f224 },
  1222. { "HIBER_COUNT", 0x5f224 },
  1223. { "WDT_WARN_COUNT", 0x5f224 },
  1224. { "MIPS_OVERRUN_FLG", 0x5f224 } };
  1225. dev_info(cs35l43->dev, "%s\n", __func__);
  1226. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  1227. wm_adsp_read_ctl(&cs35l43->dsp, regs[i].name,
  1228. WMFW_ADSP2_XM, regs[i].id, &reg, sizeof(u32));
  1229. dev_info(cs35l43->dev, "%s (0x%x): 0x%x\n",
  1230. regs[i].name, regs[i].id, reg);
  1231. }
  1232. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_XM_VIO_STATUS, &reg);
  1233. dev_info(cs35l43->dev, "%s: CS35L43_DSP1_MPU_XM_VIO_STATUS 0x%x\n", __func__, reg);
  1234. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_YM_VIO_STATUS, &reg);
  1235. dev_info(cs35l43->dev, "%s: CS35L43_DSP1_MPU_YM_VIO_STATUS 0x%x\n", __func__, reg);
  1236. regmap_read(cs35l43->regmap, CS35L43_DSP1_MPU_PM_VIO_STATUS, &reg);
  1237. dev_info(cs35l43->dev, "%s: CS35L43_DSP1_MPU_PM_VIO_STATUS 0x%x\n", __func__, reg);
  1238. return 0;
  1239. }
  1240. static void cs35l43_pll_config(struct cs35l43_private *cs35l43)
  1241. {
  1242. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1243. CS35L43_PLL_OPEN_LOOP_MASK,
  1244. CS35L43_PLL_OPEN_LOOP_MASK);
  1245. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1246. CS35L43_PLL_REFCLK_FREQ_MASK,
  1247. cs35l43->extclk_cfg << CS35L43_PLL_REFCLK_FREQ_SHIFT);
  1248. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1249. CS35L43_PLL_REFCLK_EN_MASK, 0);
  1250. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1251. CS35L43_PLL_REFCLK_SEL_MASK, cs35l43->clk_id);
  1252. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1253. CS35L43_PLL_OPEN_LOOP_MASK,
  1254. 0);
  1255. cs35l43_regmap_update_bits(cs35l43, CS35L43_REFCLK_INPUT,
  1256. CS35L43_PLL_REFCLK_EN_MASK,
  1257. CS35L43_PLL_REFCLK_EN_MASK);
  1258. }
  1259. static int cs35l43_check_mailbox(struct cs35l43_private *cs35l43)
  1260. {
  1261. unsigned int *mbox;
  1262. unsigned int write_ptr, read_ptr, read_idx, write_idx, type, msg;
  1263. int i, ret;
  1264. mbox = kmalloc_array(8, sizeof(*mbox), GFP_KERNEL);
  1265. if (!mbox)
  1266. return -ENOMEM;
  1267. cs35l43_regmap_bulk_read(cs35l43, CS35L43_DSP_MBOX_1, mbox, 8);
  1268. for (i = 0; i < 8; i++)
  1269. dev_dbg(cs35l43->dev, "mbox[%d]: 0x%x\n", i + 1, mbox[i]);
  1270. ret = wm_adsp_read_ctl(&cs35l43->dsp, "QUEUE_WT",
  1271. WMFW_ADSP2_XM, CS35L43_ALG_ID_MAILBOX, &write_ptr, sizeof(u32));
  1272. if (ret < 0)
  1273. return ret;
  1274. ret = wm_adsp_read_ctl(&cs35l43->dsp, "QUEUE_RD",
  1275. WMFW_ADSP2_XM, CS35L43_ALG_ID_MAILBOX, &read_ptr, sizeof(u32));
  1276. if (ret < 0)
  1277. return ret;
  1278. write_ptr = be32_to_cpu(write_ptr);
  1279. read_ptr = be32_to_cpu(read_ptr);
  1280. dev_dbg(cs35l43->dev, "QUEUE_WT: 0x%x\n", write_ptr);
  1281. dev_dbg(cs35l43->dev, "QUEUE_RD: 0x%x\n", read_ptr);
  1282. read_idx = (read_ptr & 0x1F) / 4;
  1283. write_idx = (write_ptr & 0x1F) / 4;
  1284. if (write_idx == 0 || write_idx == read_idx)
  1285. goto exit;
  1286. do {
  1287. dev_info(cs35l43->dev, "MESSAGE: 0x%x\n", mbox[read_idx]);
  1288. type = mbox[read_idx] >> 24;
  1289. msg = mbox[read_idx];
  1290. switch (type) {
  1291. case CS35L43_MBOX_TYPE_PWR:
  1292. if (msg == CS35L43_MBOX_MSG_AWAKE)
  1293. dev_info(cs35l43->dev, "AWAKE\n");
  1294. break;
  1295. case CS35L43_MBOX_TYPE_SYS:
  1296. if (msg == CS35L43_MBOX_MSG_ACK)
  1297. dev_info(cs35l43->dev, "ACK\n");
  1298. break;
  1299. case CS35L43_MBOX_TYPE_AUDIO:
  1300. break;
  1301. case CS35L43_MBOX_TYPE_ERROR:
  1302. dev_err(cs35l43->dev, "Mailbox error: 0x%x\n", msg);
  1303. cs35l43_log_dsp_err(cs35l43);
  1304. break;
  1305. case CS35L43_MBOX_TYPE_MEM_VAL:
  1306. dev_err(cs35l43->dev, "Memory Validation error: 0x%x\n", msg);
  1307. cs35l43_log_dsp_err(cs35l43);
  1308. break;
  1309. case CS35L43_MBOX_TYPE_EVENT:
  1310. dev_info(cs35l43->dev, "Mailbox Event: 0x%x\n", msg);
  1311. break;
  1312. case CS35L43_MBOX_TYPE_WDT:
  1313. dev_info(cs35l43->dev, "WDT Warn: 0x%x\n", msg);
  1314. break;
  1315. default:
  1316. dev_err(cs35l43->dev, "Unknown msg type: 0x%x\n", type);
  1317. }
  1318. read_idx++;
  1319. read_idx = read_idx % 8;
  1320. if (read_idx == 0)
  1321. read_idx++;
  1322. } while (read_idx != write_idx);
  1323. write_ptr = cpu_to_be32(write_ptr);
  1324. wm_adsp_write_ctl(&cs35l43->dsp, "QUEUE_RD",
  1325. WMFW_ADSP2_XM, CS35L43_ALG_ID_MAILBOX, &write_ptr, sizeof(u32));
  1326. exit:
  1327. kfree(mbox);
  1328. return 0;
  1329. }
  1330. static void cs35l43_mbox_work(struct work_struct *wk)
  1331. {
  1332. struct cs35l43_private *cs35l43;
  1333. cs35l43 = container_of(wk, struct cs35l43_private, mbox_work);
  1334. cs35l43_check_mailbox(cs35l43);
  1335. }
  1336. static void cs35l43_irq_work(struct work_struct *wk)
  1337. {
  1338. struct delayed_work *dwork = to_delayed_work(wk);
  1339. struct cs35l43_private *cs35l43 =
  1340. container_of(dwork, struct cs35l43_private, irq_work);
  1341. cs35l43->irq_unhandled_events = 0;
  1342. }
  1343. static const struct reg_sequence cs35l43_pup_patch[] = {
  1344. {0x00000040, 0x00000055},
  1345. {0x00000040, 0x000000AA},
  1346. {0x00002084, 0x000F1AA0},
  1347. {0x00000040, 0x000000CC},
  1348. {0x00000040, 0x00000033},
  1349. };
  1350. static const struct reg_sequence cs35l43_pdn_patch[] = {
  1351. {0x00000040, 0x00000055},
  1352. {0x00000040, 0x000000AA},
  1353. {0x00002084, 0x000F1AA3},
  1354. {0x00000040, 0x000000CC},
  1355. {0x00000040, 0x00000033},
  1356. };
  1357. static void cs35l43_log_status(struct cs35l43_private *cs35l43)
  1358. {
  1359. unsigned int pm_state, audio_state, reg;
  1360. dev_info(cs35l43->dev, "%s\n", __func__);
  1361. wm_adsp_read_ctl(&cs35l43->dsp, "PM_CUR_STATE",
  1362. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, &pm_state, sizeof(u32));
  1363. pm_state = be32_to_cpu(pm_state);
  1364. wm_adsp_read_ctl(&cs35l43->dsp, "AUDIO_STATE",
  1365. WMFW_ADSP2_XM, 0x5f212, &audio_state, sizeof(u32));
  1366. audio_state = be32_to_cpu(audio_state);
  1367. if (pm_state == 0x2 && audio_state == CS35L43_AUDIO_STATE_RAMPDOWN) {
  1368. dev_err(cs35l43->dev, "%s: Error PM_STATE and AUDIO_STATE\n", __func__);
  1369. if (!mutex_is_locked(&cs35l43->err_lock))
  1370. queue_work(cs35l43->err_wq, &cs35l43->err_work);
  1371. }
  1372. dev_info(cs35l43->dev, "PM_STATE: 0x%x\tAUDIO_STATE: 0x%x\n",
  1373. pm_state, audio_state);
  1374. cs35l43_regmap_read(cs35l43, CS35L43_DACPCM1_INPUT, &reg);
  1375. dev_info(cs35l43->dev, "DACPCM1_INPUT: 0x%x\n", reg);
  1376. cs35l43_regmap_read(cs35l43, CS35L43_AMP_GAIN, &reg);
  1377. dev_info(cs35l43->dev, "AMP_GAIN: 0x%x\n", reg);
  1378. cs35l43_regmap_read(cs35l43, CS35L43_AMP_CTRL, &reg);
  1379. dev_info(cs35l43->dev, "AMP_CTRL: 0x%x\n", reg);
  1380. cs35l43_regmap_read(cs35l43, CS35L43_IRQ1_EINT_1, &reg);
  1381. dev_info(cs35l43->dev, "IRQ1_EINT1: 0x%x\n", reg);
  1382. }
  1383. static int cs35l43_enter_hibernate(struct cs35l43_private *cs35l43)
  1384. {
  1385. unsigned int pm_state, audio_state;
  1386. if (cs35l43->hibernate_state != CS35L43_HIBERNATE_AWAKE)
  1387. return 0;
  1388. dev_info(cs35l43->dev, "%s\n", __func__);
  1389. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_1, 0xFFFFFFFF);
  1390. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_2, 0xFFFFFFFF);
  1391. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_3, 0xFFFFFFFF);
  1392. if (cs35l43->limit_spi_clock)
  1393. cs35l43_regmap_write(cs35l43, CS35L43_WAKESRC_CTL,
  1394. CS35L43_WKSRC_SPI);
  1395. else
  1396. cs35l43_regmap_write(cs35l43, CS35L43_WAKESRC_CTL,
  1397. CS35L43_WKSRC_I2C);
  1398. wm_adsp_read_ctl(&cs35l43->dsp, "PM_CUR_STATE",
  1399. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, &pm_state, sizeof(u32));
  1400. wm_adsp_read_ctl(&cs35l43->dsp, "AUDIO_STATE",
  1401. WMFW_ADSP2_XM, 0x5f212, &audio_state, sizeof(u32));
  1402. dev_info(cs35l43->dev, "PM_STATE: 0x%x\tAUDIO_STATE: 0x%x\n",
  1403. pm_state, audio_state);
  1404. if (cs35l43->write_seq_initialized)
  1405. cs35l43_write_seq_update(cs35l43, &cs35l43->power_on_seq);
  1406. mutex_lock(&cs35l43->dsp.cs_dsp.pwr_lock);
  1407. cs35l43_regmap_write(cs35l43, CS35L43_GLOBAL_ENABLES, 0);
  1408. usleep_range(2100, 2500);
  1409. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1410. CS35L43_MBOX_CMD_ALLOW_HIBERNATE);
  1411. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1412. CS35L43_MBOX_CMD_HIBERNATE);
  1413. cs35l43->hibernate_state = CS35L43_HIBERNATE_STANDBY;
  1414. cs35l43->dsp.hibernate = true;
  1415. /* Do changes in cache during hibernation */
  1416. regcache_cache_only(cs35l43->regmap, true);
  1417. regcache_mark_dirty(cs35l43->regmap);
  1418. mutex_unlock(&cs35l43->dsp.cs_dsp.pwr_lock);
  1419. return 0;
  1420. }
  1421. static int cs35l43_exit_hibernate(struct cs35l43_private *cs35l43)
  1422. {
  1423. int timeout = 10, ret = 0;
  1424. unsigned int status;
  1425. if (cs35l43->hibernate_state != CS35L43_HIBERNATE_STANDBY &&
  1426. cs35l43->hibernate_state != CS35L43_HIBERNATE_UPDATE)
  1427. return 0;
  1428. dev_info(cs35l43->dev, "%s\n", __func__);
  1429. mutex_lock(&cs35l43->dsp.cs_dsp.pwr_lock);
  1430. regcache_cache_only(cs35l43->regmap, false);
  1431. do {
  1432. ret = regmap_write(cs35l43->regmap, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1433. CS35L43_MBOX_CMD_WAKEUP);
  1434. usleep_range(1000, 1100);
  1435. } while (ret < 0 && timeout-- > 0);
  1436. if (timeout == 0)
  1437. dev_err(cs35l43->dev, "Timeout at MBOX_CMD_WAKEUP\n");
  1438. else if (ret == 0)
  1439. dev_info(cs35l43->dev, "%s wakeup command success: %d\n",
  1440. __func__, 10 - timeout);
  1441. cs35l43->dsp.hibernate = false;
  1442. cs35l43_regmap_write(cs35l43, CS35L43_DSP_VIRTUAL1_MBOX_1,
  1443. CS35L43_MBOX_CMD_PREVENT_HIBERNATE);
  1444. usleep_range(2000, 2100);
  1445. ret = cs35l43_regmap_read(cs35l43, CS35L43_PWRMGT_STS, &status);
  1446. if (ret < 0 || !(status & CS35L43_WKSRC_STS_MASK))
  1447. dev_err(cs35l43->dev, "Error during wakeup, PWRMGT_STS = 0x%x\n", status);
  1448. mutex_unlock(&cs35l43->dsp.cs_dsp.pwr_lock);
  1449. /* PM_CUR_STATE should be non-zero */
  1450. wm_adsp_read_ctl(&cs35l43->dsp, "PM_CUR_STATE",
  1451. WMFW_ADSP2_XM, CS35L43_ALG_ID_PM, &status, sizeof(u32));
  1452. if (!status)
  1453. dev_err(cs35l43->dev, "Error during wakeup, PM_CUR_STATE = 0x%x\n", status);
  1454. /* First MBOX outbound message should be AWAKE = CMD_WAKEUP */
  1455. ret = cs35l43_regmap_read(cs35l43, CS35L43_DSP_MBOX_2, &status);
  1456. if (ret < 0 || (status != CS35L43_MBOX_CMD_WAKEUP))
  1457. dev_err(cs35l43->dev, "Error during wakeup, MBOX2 = 0x%x\n", status);
  1458. /*
  1459. * At this point FW applies register values stored in the sequencer
  1460. * Do sync to apply register values changed in cache during hibernation
  1461. */
  1462. regcache_sync_region(cs35l43->regmap, CS35L43_DEVID,
  1463. CS35L43_MIXER_NGATE_CH2_CFG);
  1464. /* Update write seq with values that could have changed in the cache */
  1465. if (cs35l43->write_seq_initialized)
  1466. cs35l43_write_seq_update(cs35l43, &cs35l43->power_on_seq);
  1467. cs35l43->hibernate_state = CS35L43_HIBERNATE_AWAKE;
  1468. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_1, 0xFFFFFFFF);
  1469. cs35l43_regmap_update_bits(cs35l43, CS35L43_IRQ1_MASK_1,
  1470. CS35L43_AMP_ERR_EINT1_MASK |
  1471. CS35L43_BST_SHORT_ERR_EINT1_MASK |
  1472. CS35L43_BST_DCM_UVP_ERR_EINT1_MASK |
  1473. CS35L43_BST_OVP_ERR_EINT1_MASK |
  1474. CS35L43_DSP_VIRTUAL2_MBOX_WR_EINT1_MASK |
  1475. CS35L43_DC_WATCHDOG_IRQ_RISE_EINT1_MASK |
  1476. CS35L43_WKSRC_STATUS6_EINT1_MASK |
  1477. CS35L43_WKSRC_STATUS_ANY_EINT1_MASK, 0);
  1478. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_2, 0xFFFFFFFF);
  1479. cs35l43_regmap_update_bits(cs35l43, CS35L43_IRQ1_MASK_2,
  1480. CS35L43_PLL_UNLOCK_FLAG_RISE_EINT1_MASK |
  1481. CS35L43_PLL_LOCK_EINT1_MASK, 0);
  1482. regmap_write(cs35l43->regmap, CS35L43_IRQ1_MASK_3, 0xFFFFFFFF);
  1483. regmap_update_bits(cs35l43->regmap, CS35L43_IRQ1_MASK_3,
  1484. CS35L43_DSP1_NMI_ERR_EINT1_MASK |
  1485. CS35L43_DSP1_MPU_ERR_EINT1_MASK |
  1486. CS35L43_DSP1_STRM_ARB_ERR_EINT1_MASK, 0);
  1487. regmap_multi_reg_write_bypassed(cs35l43->regmap,
  1488. cs35l43_pdn_patch,
  1489. ARRAY_SIZE(cs35l43_pdn_patch));
  1490. return 0;
  1491. }
  1492. int cs35l43_suspend_runtime(struct device *dev)
  1493. {
  1494. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  1495. int i, ret = 0;
  1496. mutex_lock(&cs35l43->hb_lock);
  1497. if (!cs35l43->write_seq_initialized && cs35l43->first_event &&
  1498. cs35l43->dsp.cs_dsp.running) {
  1499. cs35l43->power_on_seq.name = "PM_PWR_ON_SEQ";
  1500. cs35l43->power_on_seq.length = CS35L43_POWER_SEQ_MAX_WORDS;
  1501. ret = cs35l43_write_seq_init(cs35l43, &cs35l43->power_on_seq);
  1502. if (ret == -EINVAL) {
  1503. /* Fall back to control used before 7.15.3 */
  1504. cs35l43->power_on_seq.name = "POWER_ON_SEQUENCE";
  1505. ret = cs35l43_write_seq_init(cs35l43,
  1506. &cs35l43->power_on_seq);
  1507. }
  1508. if (ret)
  1509. goto err;
  1510. cs35l43_write_seq_update(cs35l43, &cs35l43->power_on_seq);
  1511. for (i = 0; i < ARRAY_SIZE(cs35l43_hibernate_update_regs); i++) {
  1512. if (cs35l43_hibernate_update_regs[i] == 0)
  1513. break;
  1514. cs35l43_write_seq_add(cs35l43, &cs35l43->power_on_seq,
  1515. cs35l43_hibernate_update_regs[i],
  1516. 0, true);
  1517. }
  1518. cs35l43->write_seq_initialized = true;
  1519. }
  1520. if (cs35l43->hibernate_state == CS35L43_HIBERNATE_NOT_LOADED &&
  1521. cs35l43->dsp.cs_dsp.running)
  1522. cs35l43->hibernate_state = CS35L43_HIBERNATE_AWAKE;
  1523. if (cs35l43->dsp.cs_dsp.running)
  1524. ret = cs35l43_enter_hibernate(cs35l43);
  1525. err:
  1526. mutex_unlock(&cs35l43->hb_lock);
  1527. return ret;
  1528. }
  1529. EXPORT_SYMBOL_GPL(cs35l43_suspend_runtime);
  1530. int cs35l43_resume_runtime(struct device *dev)
  1531. {
  1532. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  1533. int ret = 0;
  1534. mutex_lock(&cs35l43->hb_lock);
  1535. if (cs35l43->dsp.cs_dsp.running)
  1536. ret = cs35l43_exit_hibernate(cs35l43);
  1537. mutex_unlock(&cs35l43->hb_lock);
  1538. return ret;
  1539. }
  1540. EXPORT_SYMBOL_GPL(cs35l43_resume_runtime);
  1541. static int cs35l43_hibernate_dapm(struct snd_soc_dapm_widget *w,
  1542. struct snd_kcontrol *kcontrol, int event)
  1543. {
  1544. struct snd_soc_component *component =
  1545. snd_soc_dapm_to_component(w->dapm);
  1546. struct cs35l43_private *cs35l43 =
  1547. snd_soc_component_get_drvdata(component);
  1548. int ret = 0;
  1549. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_STANDBY)
  1550. return 0;
  1551. switch (event) {
  1552. case SND_SOC_DAPM_PRE_PMU:
  1553. if (pm_runtime_suspended(cs35l43->dev)) {
  1554. dev_info(cs35l43->dev, "resume from hibernate dapm\n");
  1555. pm_runtime_resume(cs35l43->dev);
  1556. }
  1557. break;
  1558. default:
  1559. dev_err(cs35l43->dev, "Invalid event = 0x%x\n", event);
  1560. ret = -EINVAL;
  1561. }
  1562. return ret;
  1563. }
  1564. static int cs35l43_main_amp_event(struct snd_soc_dapm_widget *w,
  1565. struct snd_kcontrol *kcontrol, int event)
  1566. {
  1567. struct snd_soc_component *component =
  1568. snd_soc_dapm_to_component(w->dapm);
  1569. struct cs35l43_private *cs35l43 =
  1570. snd_soc_component_get_drvdata(component);
  1571. int ret = 0;
  1572. dev_dbg(cs35l43->dev, "%s\n", __func__);
  1573. switch (event) {
  1574. case SND_SOC_DAPM_POST_PMU:
  1575. dev_info(cs35l43->dev, "%s PMU\n", __func__);
  1576. if (cs35l43->dsp.cs_dsp.running)
  1577. cs35l43_apply_delta_tuning(cs35l43);
  1578. regmap_multi_reg_write_bypassed(cs35l43->regmap,
  1579. cs35l43_pup_patch,
  1580. ARRAY_SIZE(cs35l43_pup_patch));
  1581. cs35l43_regmap_write(cs35l43, CS35L43_GLOBAL_ENABLES, 1);
  1582. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1583. CS35L43_AMP_EN_MASK, CS35L43_AMP_EN_MASK);
  1584. if (cs35l43->limit_spi_clock)
  1585. cs35l43->limit_spi_clock(cs35l43, false);
  1586. cs35l43_log_status(cs35l43);
  1587. break;
  1588. case SND_SOC_DAPM_POST_PMD:
  1589. dev_info(cs35l43->dev, "%s PMD\n", __func__);
  1590. if (cs35l43->limit_spi_clock)
  1591. cs35l43->limit_spi_clock(cs35l43, true);
  1592. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1593. CS35L43_AMP_EN_MASK, 0);
  1594. regmap_multi_reg_write_bypassed(cs35l43->regmap,
  1595. cs35l43_pdn_patch,
  1596. ARRAY_SIZE(cs35l43_pdn_patch));
  1597. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_STANDBY) {
  1598. usleep_range(1000, 1100);
  1599. cs35l43_regmap_write(cs35l43, CS35L43_GLOBAL_ENABLES, 0);
  1600. }
  1601. break;
  1602. default:
  1603. dev_err(cs35l43->dev, "Invalid event = 0x%x\n", event);
  1604. ret = -EINVAL;
  1605. }
  1606. return ret;
  1607. }
  1608. static const struct snd_soc_dapm_widget cs35l43_dapm_widgets[] = {
  1609. SND_SOC_DAPM_OUT_DRV_E("Main AMP", SND_SOC_NOPM, 0, 0, NULL, 0,
  1610. cs35l43_main_amp_event,
  1611. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
  1612. SND_SOC_DAPM_SUPPLY("Hibernate", SND_SOC_NOPM, 0, 0,
  1613. cs35l43_hibernate_dapm, SND_SOC_DAPM_PRE_PMU),
  1614. SND_SOC_DAPM_OUTPUT("AMP SPK"),
  1615. SND_SOC_DAPM_SPK("DSP1 Preload", NULL),
  1616. SND_SOC_DAPM_SUPPLY_S("DSP1 Preloader", 100,
  1617. SND_SOC_NOPM, 0, 0, cs35l43_dsp_preload_ev,
  1618. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1619. SND_SOC_DAPM_PRE_PMD),
  1620. SND_SOC_DAPM_OUT_DRV_E("DSP1", SND_SOC_NOPM, 0, 0, NULL, 0,
  1621. cs35l43_dsp_audio_ev, SND_SOC_DAPM_POST_PMU |
  1622. SND_SOC_DAPM_PRE_PMD),
  1623. SND_SOC_DAPM_AIF_IN("ASPRX1", NULL, 0, CS35L43_ASP_ENABLES1,
  1624. CS35L43_ASP_RX1_EN_SHIFT, 0),
  1625. SND_SOC_DAPM_AIF_IN("ASPRX2", NULL, 0, CS35L43_ASP_ENABLES1,
  1626. CS35L43_ASP_RX2_EN_SHIFT, 0),
  1627. SND_SOC_DAPM_AIF_OUT("ASPTX1", NULL, 0, CS35L43_ASP_ENABLES1,
  1628. CS35L43_ASP_TX1_EN_SHIFT, 0),
  1629. SND_SOC_DAPM_AIF_OUT("ASPTX2", NULL, 0, CS35L43_ASP_ENABLES1,
  1630. CS35L43_ASP_TX2_EN_SHIFT, 0),
  1631. SND_SOC_DAPM_AIF_OUT("ASPTX3", NULL, 0, CS35L43_ASP_ENABLES1,
  1632. CS35L43_ASP_TX3_EN_SHIFT, 0),
  1633. SND_SOC_DAPM_AIF_OUT("ASPTX4", NULL, 0, CS35L43_ASP_ENABLES1,
  1634. CS35L43_ASP_TX4_EN_SHIFT, 0),
  1635. SND_SOC_DAPM_MUX("ASP TX1 Source", SND_SOC_NOPM, 0, 0, &asp_tx1_mux),
  1636. SND_SOC_DAPM_MUX("ASP TX2 Source", SND_SOC_NOPM, 0, 0, &asp_tx2_mux),
  1637. SND_SOC_DAPM_MUX("ASP TX3 Source", SND_SOC_NOPM, 0, 0, &asp_tx3_mux),
  1638. SND_SOC_DAPM_MUX("ASP TX4 Source", SND_SOC_NOPM, 0, 0, &asp_tx4_mux),
  1639. SND_SOC_DAPM_MUX("DSP RX1 Source", SND_SOC_NOPM, 0, 0, &dsp_rx1_mux),
  1640. SND_SOC_DAPM_MUX("DSP RX2 Source", SND_SOC_NOPM, 0, 0, &dsp_rx2_mux),
  1641. SND_SOC_DAPM_MUX("DSP RX3 Source", SND_SOC_NOPM, 0, 0, &dsp_rx3_mux),
  1642. SND_SOC_DAPM_MUX("PCM Source", SND_SOC_NOPM, 0, 0, &dacpcm_mux),
  1643. SND_SOC_DAPM_MUX("High Rate PCM Source", SND_SOC_NOPM, 0, 0, &dacpcm2_mux),
  1644. SND_SOC_DAPM_MUX("Ultrasonic Mode", SND_SOC_NOPM, 0, 0, &ultra_mux),
  1645. SND_SOC_DAPM_ADC("VMON ADC", NULL, CS35L43_BLOCK_ENABLES,
  1646. CS35L43_VMON_EN_SHIFT, 0),
  1647. SND_SOC_DAPM_ADC("IMON ADC", NULL, CS35L43_BLOCK_ENABLES,
  1648. CS35L43_IMON_EN_SHIFT, 0),
  1649. SND_SOC_DAPM_ADC("VPMON ADC", NULL, CS35L43_BLOCK_ENABLES,
  1650. CS35L43_VPMON_EN_SHIFT, 0),
  1651. SND_SOC_DAPM_ADC("VBSTMON ADC", NULL, CS35L43_BLOCK_ENABLES,
  1652. CS35L43_VBSTMON_EN_SHIFT, 0),
  1653. SND_SOC_DAPM_ADC("TEMPMON ADC", NULL, CS35L43_BLOCK_ENABLES,
  1654. CS35L43_TEMPMON_EN_SHIFT, 0),
  1655. SND_SOC_DAPM_SWITCH("AMP Enable", SND_SOC_NOPM, 0, 1, &amp_enable_ctrl),
  1656. };
  1657. static const struct snd_soc_dapm_route cs35l43_audio_map[] = {
  1658. { "DSP1", NULL, "DSP1 Preloader" },
  1659. { "DSP1 Preload", NULL, "DSP1 Preloader" },
  1660. {"DSP1", NULL, "DSP RX1 Source"},
  1661. {"DSP1", NULL, "DSP RX2 Source"},
  1662. {"DSP1", NULL, "DSP RX3 Source"},
  1663. {"PCM Source", "ASPRX1", "ASPRX1"},
  1664. {"PCM Source", "ASPRX2", "ASPRX2"},
  1665. {"PCM Source", "DSP", "DSP1"},
  1666. {"PCM Source", "DSP FS2", "DSP1"},
  1667. {"High Rate PCM Source", "ASPRX1", "ASPRX1"},
  1668. {"High Rate PCM Source", "ASPRX2", "ASPRX2"},
  1669. {"High Rate PCM Source", "DSP", "DSP1"},
  1670. {"High Rate PCM Source", "DSP FS2", "DSP1"},
  1671. {"Ultrasonic Mode", "In Band", "High Rate PCM Source"},
  1672. {"Ultrasonic Mode", "Out of Band", "High Rate PCM Source"},
  1673. {"Main AMP", NULL, "Ultrasonic Mode"},
  1674. {"Main AMP", NULL, "PCM Source"},
  1675. {"AMP SPK", NULL, "Main AMP"},
  1676. {"AMP SPK", NULL, "Hibernate"},
  1677. {"ASP TX1 Source", "ASPRX1", "ASPRX1"},
  1678. {"ASP TX2 Source", "ASPRX1", "ASPRX1"},
  1679. {"ASP TX3 Source", "ASPRX1", "ASPRX1"},
  1680. {"ASP TX4 Source", "ASPRX1", "ASPRX1"},
  1681. {"DSP RX1 Source", "ASPRX1", "ASPRX1"},
  1682. {"DSP RX2 Source", "ASPRX1", "ASPRX1"},
  1683. {"DSP RX3 Source", "ASPRX1", "ASPRX1"},
  1684. {"ASP TX1 Source", "ASPRX2", "ASPRX2"},
  1685. {"ASP TX2 Source", "ASPRX2", "ASPRX2"},
  1686. {"ASP TX3 Source", "ASPRX2", "ASPRX2"},
  1687. {"ASP TX4 Source", "ASPRX2", "ASPRX2"},
  1688. {"DSP RX1 Source", "ASPRX2", "ASPRX2"},
  1689. {"DSP RX2 Source", "ASPRX2", "ASPRX2"},
  1690. {"DSP RX3 Source", "ASPRX2", "ASPRX2"},
  1691. {"ASP TX1 Source", "VMON", "VMON ADC"},
  1692. {"ASP TX2 Source", "VMON", "VMON ADC"},
  1693. {"ASP TX3 Source", "VMON", "VMON ADC"},
  1694. {"ASP TX4 Source", "VMON", "VMON ADC"},
  1695. {"DSP RX1 Source", "VMON", "VMON ADC"},
  1696. {"DSP RX2 Source", "VMON", "VMON ADC"},
  1697. {"DSP RX3 Source", "VMON", "VMON ADC"},
  1698. {"ASP TX1 Source", "VMON FS2", "VMON ADC"},
  1699. {"ASP TX2 Source", "VMON FS2", "VMON ADC"},
  1700. {"ASP TX3 Source", "VMON FS2", "VMON ADC"},
  1701. {"ASP TX4 Source", "VMON FS2", "VMON ADC"},
  1702. {"DSP RX1 Source", "VMON FS2", "VMON ADC"},
  1703. {"DSP RX2 Source", "VMON FS2", "VMON ADC"},
  1704. {"DSP RX3 Source", "VMON FS2", "VMON ADC"},
  1705. {"ASP TX1 Source", "IMON", "IMON ADC"},
  1706. {"ASP TX2 Source", "IMON", "IMON ADC"},
  1707. {"ASP TX3 Source", "IMON", "IMON ADC"},
  1708. {"ASP TX4 Source", "IMON", "IMON ADC"},
  1709. {"DSP RX1 Source", "IMON", "IMON ADC"},
  1710. {"DSP RX2 Source", "IMON", "IMON ADC"},
  1711. {"DSP RX3 Source", "IMON", "IMON ADC"},
  1712. {"ASP TX1 Source", "IMON FS2", "IMON ADC"},
  1713. {"ASP TX2 Source", "IMON FS2", "IMON ADC"},
  1714. {"ASP TX3 Source", "IMON FS2", "IMON ADC"},
  1715. {"ASP TX4 Source", "IMON FS2", "IMON ADC"},
  1716. {"DSP RX1 Source", "IMON FS2", "IMON ADC"},
  1717. {"DSP RX2 Source", "IMON FS2", "IMON ADC"},
  1718. {"DSP RX3 Source", "IMON FS2", "IMON ADC"},
  1719. {"ASP TX1 Source", "VPMON", "VPMON ADC"},
  1720. {"ASP TX2 Source", "VPMON", "VPMON ADC"},
  1721. {"ASP TX3 Source", "VPMON", "VPMON ADC"},
  1722. {"ASP TX4 Source", "VPMON", "VPMON ADC"},
  1723. {"DSP RX1 Source", "VPMON", "VPMON ADC"},
  1724. {"DSP RX2 Source", "VPMON", "VPMON ADC"},
  1725. {"DSP RX3 Source", "VPMON", "VPMON ADC"},
  1726. {"ASP TX1 Source", "VBSTMON", "VBSTMON ADC"},
  1727. {"ASP TX2 Source", "VBSTMON", "VBSTMON ADC"},
  1728. {"ASP TX3 Source", "VBSTMON", "VBSTMON ADC"},
  1729. {"ASP TX4 Source", "VBSTMON", "VBSTMON ADC"},
  1730. {"DSP RX1 Source", "VBSTMON", "VBSTMON ADC"},
  1731. {"DSP RX2 Source", "VBSTMON", "VBSTMON ADC"},
  1732. {"DSP RX3 Source", "VBSTMON", "VBSTMON ADC"},
  1733. {"ASP TX1 Source", "DSP", "DSP1"},
  1734. {"ASP TX2 Source", "DSP", "DSP1"},
  1735. {"ASP TX3 Source", "DSP", "DSP1"},
  1736. {"ASP TX4 Source", "DSP", "DSP1"},
  1737. {"ASP TX1 Source", "DSP FS2", "DSP1"},
  1738. {"ASP TX2 Source", "DSP FS2", "DSP1"},
  1739. {"ASP TX3 Source", "DSP FS2", "DSP1"},
  1740. {"ASP TX4 Source", "DSP FS2", "DSP1"},
  1741. {"ASPTX1", NULL, "ASP TX1 Source"},
  1742. {"ASPTX2", NULL, "ASP TX2 Source"},
  1743. {"ASPTX3", NULL, "ASP TX3 Source"},
  1744. {"ASPTX4", NULL, "ASP TX4 Source"},
  1745. {"AMP Capture", NULL, "ASPTX1"},
  1746. {"AMP Capture", NULL, "ASPTX2"},
  1747. {"AMP Capture", NULL, "ASPTX3"},
  1748. {"AMP Capture", NULL, "ASPTX4"},
  1749. {"DSP1", NULL, "IMON ADC"},
  1750. {"DSP1", NULL, "VMON ADC"},
  1751. {"DSP1", NULL, "VBSTMON ADC"},
  1752. {"DSP1", NULL, "VPMON ADC"},
  1753. {"DSP1", NULL, "TEMPMON ADC"},
  1754. {"AMP Enable", "Switch", "AMP Playback"},
  1755. {"ASPRX1", NULL, "AMP Enable"},
  1756. {"ASPRX2", NULL, "AMP Enable"},
  1757. {"VMON ADC", NULL, "AMP Enable"},
  1758. {"IMON ADC", NULL, "AMP Enable"},
  1759. {"VPMON ADC", NULL, "AMP Enable"},
  1760. {"VBSTMON ADC", NULL, "AMP Enable"},
  1761. {"TEMPMON ADC", NULL, "AMP Enable"},
  1762. };
  1763. static irqreturn_t cs35l43_irq(int irq, void *data)
  1764. {
  1765. struct cs35l43_private *cs35l43 = data;
  1766. unsigned int status[3], masks[3], pin_status;
  1767. int ret = IRQ_NONE, i;
  1768. bool is_pm_runtime_enabled = pm_runtime_enabled(cs35l43->dev);
  1769. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE &&
  1770. !pm_runtime_enabled(cs35l43->dev) &&
  1771. cs35l43->hibernate_state != CS35L43_HIBERNATE_NOT_LOADED) {
  1772. if (cs35l43->hibernate_state == CS35L43_HIBERNATE_AWAKE) {
  1773. /* hibernation entry is pending, ack EINTs and clear masks */
  1774. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1, 0xFFFFFFFF);
  1775. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_2, 0xFFFFFFFF);
  1776. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_3, 0xFFFFFFFF);
  1777. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_1, 0xFFFFFFFF);
  1778. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_2, 0xFFFFFFFF);
  1779. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_3, 0xFFFFFFFF);
  1780. return IRQ_HANDLED;
  1781. }
  1782. cs35l43->irq_unhandled_events++;
  1783. cancel_delayed_work(&cs35l43->irq_work);
  1784. queue_delayed_work(cs35l43->irq_wq, &cs35l43->irq_work,
  1785. msecs_to_jiffies(CS35L43_IRQ_UNHANDLED_ALERT_INTERVAL_MS));
  1786. dev_info_ratelimited(cs35l43->dev,
  1787. "%s: ignoring: pwr_mode=%d, pm_runtime_enabled=%d, hibernate_state=%d, irq_unhandled_events=%d\n",
  1788. __func__, cs35l43->low_pwr_mode, is_pm_runtime_enabled,
  1789. cs35l43->hibernate_state, cs35l43->irq_unhandled_events);
  1790. if (cs35l43->irq_unhandled_events > CS35L43_IRQ_UNHANDLED_ALERT_THRESH) {
  1791. dev_info(cs35l43->dev, "Excess IRQ events detected\n");
  1792. pm_runtime_enable(cs35l43->dev);
  1793. } else
  1794. return ret;
  1795. }
  1796. pm_runtime_get_sync(cs35l43->dev);
  1797. for (i = 0; i < ARRAY_SIZE(status); i++) {
  1798. cs35l43_regmap_read(cs35l43,
  1799. CS35L43_IRQ1_EINT_1 + (i * 4),
  1800. &status[i]);
  1801. cs35l43_regmap_read(cs35l43,
  1802. CS35L43_IRQ1_MASK_1 + (i * 4),
  1803. &masks[i]);
  1804. }
  1805. /* Check to see if unmasked bits are active */
  1806. if (!(status[0] & ~masks[0]) &&
  1807. !(status[1] & ~masks[1]) &&
  1808. !(status[2] & ~masks[2])) {
  1809. ret = IRQ_NONE;
  1810. dev_info_ratelimited(cs35l43->dev,
  1811. "%s: ignoring due to mask/status bits\n", __func__);
  1812. for (i = 0; i < ARRAY_SIZE(status); i++)
  1813. dev_info_ratelimited(cs35l43->dev, "mask[%d]=0x%x status[%d]=0x%x\n",
  1814. i, masks[i], i, status[i]);
  1815. cs35l43_regmap_read(cs35l43, CS35L43_IRQ1_STATUS, &pin_status);
  1816. dev_info_ratelimited(cs35l43->dev, "pin_status: %d\n", pin_status);
  1817. goto done;
  1818. }
  1819. /*
  1820. * The following interrupts require a
  1821. * protection release cycle to get the
  1822. * speaker out of Safe-Mode.
  1823. */
  1824. if (status[0] & CS35L43_AMP_ERR_EINT1_MASK) {
  1825. dev_crit(cs35l43->dev, "Amp short error\n");
  1826. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1827. CS35L43_AMP_ERR_EINT1_MASK);
  1828. cs35l43_regmap_write(cs35l43, CS35L43_ERROR_RELEASE, 0);
  1829. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1830. CS35L43_AMP_SHORT_ERR_RLS_MASK,
  1831. CS35L43_AMP_SHORT_ERR_RLS_MASK);
  1832. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1833. CS35L43_AMP_SHORT_ERR_RLS_MASK, 0);
  1834. cirrus_bd_amp_err(cs35l43->pdata.mfd_suffix);
  1835. }
  1836. if (status[0] & CS35L43_BST_OVP_ERR_EINT1_MASK) {
  1837. dev_crit(cs35l43->dev, "VBST Over Voltage error\n");
  1838. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1839. CS35L43_BST_EN_MASK <<
  1840. CS35L43_BST_EN_SHIFT, 0);
  1841. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1842. CS35L43_BST_OVP_ERR_EINT1_MASK);
  1843. cs35l43_regmap_write(cs35l43, CS35L43_ERROR_RELEASE, 0);
  1844. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1845. CS35L43_BST_OVP_ERR_RLS_MASK,
  1846. CS35L43_BST_OVP_ERR_RLS_MASK);
  1847. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1848. CS35L43_BST_OVP_ERR_RLS_MASK, 0);
  1849. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1850. CS35L43_BST_EN_MASK <<
  1851. CS35L43_BST_EN_SHIFT,
  1852. CS35L43_BST_EN_DEFAULT <<
  1853. CS35L43_BST_EN_SHIFT);
  1854. }
  1855. if (status[0] & CS35L43_BST_DCM_UVP_ERR_EINT1_MASK) {
  1856. dev_crit(cs35l43->dev, "DCM VBST Under Voltage Error\n");
  1857. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1858. CS35L43_BST_EN_MASK <<
  1859. CS35L43_BST_EN_SHIFT, 0);
  1860. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1861. CS35L43_BST_DCM_UVP_ERR_EINT1_MASK);
  1862. cs35l43_regmap_write(cs35l43, CS35L43_ERROR_RELEASE, 0);
  1863. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1864. CS35L43_BST_UVP_ERR_RLS_MASK,
  1865. CS35L43_BST_UVP_ERR_RLS_MASK);
  1866. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1867. CS35L43_BST_UVP_ERR_RLS_MASK, 0);
  1868. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1869. CS35L43_BST_EN_MASK <<
  1870. CS35L43_BST_EN_SHIFT,
  1871. CS35L43_BST_EN_DEFAULT <<
  1872. CS35L43_BST_EN_SHIFT);
  1873. }
  1874. if (status[0] & CS35L43_BST_SHORT_ERR_EINT1_MASK) {
  1875. dev_crit(cs35l43->dev, "LBST error: powering off!\n");
  1876. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1877. CS35L43_BST_EN_MASK <<
  1878. CS35L43_BST_EN_SHIFT, 0);
  1879. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1880. CS35L43_BST_SHORT_ERR_EINT1_MASK);
  1881. cs35l43_regmap_write(cs35l43, CS35L43_ERROR_RELEASE, 0);
  1882. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1883. CS35L43_BST_SHORT_ERR_RLS_MASK,
  1884. CS35L43_BST_SHORT_ERR_RLS_MASK);
  1885. cs35l43_regmap_update_bits(cs35l43, CS35L43_ERROR_RELEASE,
  1886. CS35L43_BST_SHORT_ERR_RLS_MASK, 0);
  1887. cs35l43_regmap_update_bits(cs35l43, CS35L43_BLOCK_ENABLES,
  1888. CS35L43_BST_EN_MASK <<
  1889. CS35L43_BST_EN_SHIFT,
  1890. CS35L43_BST_EN_DEFAULT <<
  1891. CS35L43_BST_EN_SHIFT);
  1892. cirrus_bd_bst_short(cs35l43->pdata.mfd_suffix);
  1893. }
  1894. if (status[0] & CS35L43_DC_WATCHDOG_IRQ_RISE_EINT1_MASK) {
  1895. dev_err(cs35l43->dev, "DC Detect INT\n");
  1896. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1897. CS35L43_DC_WATCHDOG_IRQ_RISE_EINT1_MASK);
  1898. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1899. CS35L43_DC_WATCHDOG_IRQ_RISE_EINT1_MASK);
  1900. }
  1901. if (status[0] & CS35L43_WKSRC_STATUS_ANY_EINT1_MASK ||
  1902. status[0] & CS35L43_WKSRC_STATUS6_EINT1_MASK) {
  1903. dev_info(cs35l43->dev, "Wakeup INT\n");
  1904. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1905. CS35L43_WKSRC_STATUS_ANY_EINT1_MASK);
  1906. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1907. CS35L43_WKSRC_STATUS6_EINT1_MASK);
  1908. }
  1909. if (status[0] & CS35L43_DSP_VIRTUAL2_MBOX_WR_EINT1_MASK) {
  1910. dev_info(cs35l43->dev, "Received Mailbox INT\n");
  1911. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_1,
  1912. CS35L43_DSP_VIRTUAL2_MBOX_WR_EINT1_MASK);
  1913. queue_work(cs35l43->mbox_wq, &cs35l43->mbox_work);
  1914. }
  1915. if (status[1] & CS35L43_PLL_UNLOCK_FLAG_RISE_EINT1_MASK) {
  1916. dev_info(cs35l43->dev, "PLL Unlock INT\n");
  1917. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_2,
  1918. CS35L43_PLL_UNLOCK_FLAG_RISE_EINT1_MASK);
  1919. }
  1920. if (status[1] & CS35L43_PLL_LOCK_EINT1_MASK) {
  1921. dev_info(cs35l43->dev, "PLL Lock INT\n");
  1922. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_EINT_2,
  1923. CS35L43_PLL_LOCK_EINT1_MASK);
  1924. }
  1925. if (status[2] & CS35L43_DSP1_NMI_ERR_EINT1_MASK) {
  1926. dev_err(cs35l43->dev, "NMI Error INT\n");
  1927. regmap_write(cs35l43->regmap, CS35L43_IRQ1_EINT_3,
  1928. CS35L43_DSP1_NMI_ERR_EINT1_MASK);
  1929. cs35l43_log_dsp_err(cs35l43);
  1930. if (!mutex_is_locked(&cs35l43->err_lock))
  1931. queue_work(cs35l43->err_wq, &cs35l43->err_work);
  1932. }
  1933. if (status[2] & CS35L43_DSP1_MPU_ERR_EINT1_MASK) {
  1934. dev_err(cs35l43->dev, "MPU Error INT\n");
  1935. regmap_write(cs35l43->regmap, CS35L43_IRQ1_EINT_3,
  1936. CS35L43_DSP1_MPU_ERR_EINT1_MASK);
  1937. cs35l43_log_dsp_err(cs35l43);
  1938. if (!mutex_is_locked(&cs35l43->err_lock))
  1939. queue_work(cs35l43->err_wq, &cs35l43->err_work);
  1940. }
  1941. if (status[2] & CS35L43_DSP1_STRM_ARB_ERR_EINT1_MASK) {
  1942. dev_err(cs35l43->dev, "Stream Arb Error INT\n");
  1943. regmap_write(cs35l43->regmap, CS35L43_IRQ1_EINT_3,
  1944. CS35L43_DSP1_STRM_ARB_ERR_EINT1_MASK);
  1945. cs35l43_log_dsp_err(cs35l43);
  1946. }
  1947. ret = IRQ_HANDLED;
  1948. done:
  1949. pm_runtime_mark_last_busy(cs35l43->dev);
  1950. pm_runtime_put_autosuspend(cs35l43->dev);
  1951. return ret;
  1952. }
  1953. static int cs35l43_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  1954. {
  1955. struct cs35l43_private *cs35l43 =
  1956. snd_soc_component_get_drvdata(codec_dai->component);
  1957. dev_dbg(cs35l43->dev, "%s\n", __func__);
  1958. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1959. case SND_SOC_DAIFMT_CBS_CFS:
  1960. break;
  1961. default:
  1962. dev_warn(cs35l43->dev,
  1963. "%s: Master mode unsupported\n", __func__);
  1964. return -EINVAL;
  1965. }
  1966. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1967. case SND_SOC_DAIFMT_DSP_A:
  1968. cs35l43->asp_fmt = 0;
  1969. break;
  1970. case SND_SOC_DAIFMT_I2S:
  1971. cs35l43->asp_fmt = 2;
  1972. break;
  1973. default:
  1974. dev_warn(cs35l43->dev,
  1975. "%s: Invalid or unsupported DAI format\n", __func__);
  1976. return -EINVAL;
  1977. }
  1978. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1979. case SND_SOC_DAIFMT_NB_IF:
  1980. cs35l43->lrclk_fmt = 1;
  1981. cs35l43->sclk_fmt = 0;
  1982. break;
  1983. case SND_SOC_DAIFMT_IB_NF:
  1984. cs35l43->lrclk_fmt = 0;
  1985. cs35l43->sclk_fmt = 1;
  1986. break;
  1987. case SND_SOC_DAIFMT_IB_IF:
  1988. cs35l43->lrclk_fmt = 1;
  1989. cs35l43->sclk_fmt = 1;
  1990. break;
  1991. case SND_SOC_DAIFMT_NB_NF:
  1992. cs35l43->lrclk_fmt = 0;
  1993. cs35l43->sclk_fmt = 0;
  1994. break;
  1995. default:
  1996. dev_warn(cs35l43->dev,
  1997. "%s: Invalid DAI clock INV\n", __func__);
  1998. return -EINVAL;
  1999. }
  2000. cs35l43_regmap_update_bits(cs35l43, CS35L43_ASP_CONTROL2,
  2001. CS35L43_ASP_FMT_MASK | CS35L43_ASP_BCLK_INV_MASK |
  2002. CS35L43_ASP_FSYNC_INV_MASK,
  2003. (cs35l43->asp_fmt << CS35L43_ASP_FMT_SHIFT) |
  2004. (cs35l43->lrclk_fmt << CS35L43_ASP_FSYNC_INV_SHIFT) |
  2005. (cs35l43->sclk_fmt << CS35L43_ASP_BCLK_INV_SHIFT));
  2006. return 0;
  2007. }
  2008. struct cs35l43_global_fs_config {
  2009. int rate;
  2010. int fs_cfg;
  2011. };
  2012. static const struct cs35l43_global_fs_config cs35l43_fs_rates[] = {
  2013. { 12000, 0x01 },
  2014. { 24000, 0x02 },
  2015. { 48000, 0x03 },
  2016. { 96000, 0x04 },
  2017. { 192000, 0x05 },
  2018. { 11025, 0x09 },
  2019. { 22050, 0x0A },
  2020. { 44100, 0x0B },
  2021. { 88200, 0x0C },
  2022. { 176400, 0x0D },
  2023. { 8000, 0x11 },
  2024. { 16000, 0x12 },
  2025. { 32000, 0x13 },
  2026. };
  2027. static int cs35l43_pcm_hw_params(struct snd_pcm_substream *substream,
  2028. struct snd_pcm_hw_params *params,
  2029. struct snd_soc_dai *dai)
  2030. {
  2031. int i;
  2032. unsigned int rate = params_rate(params);
  2033. u8 asp_width, asp_wl;
  2034. struct cs35l43_private *cs35l43 =
  2035. snd_soc_component_get_drvdata(dai->component);
  2036. dev_dbg(cs35l43->dev, "%s\n", __func__);
  2037. for (i = 0; i < ARRAY_SIZE(cs35l43_fs_rates); i++) {
  2038. if (rate == cs35l43_fs_rates[i].rate)
  2039. break;
  2040. }
  2041. if (i < ARRAY_SIZE(cs35l43_fs_rates) &&
  2042. cs35l43->ultrasonic_mode == CS35L43_ULTRASONIC_MODE_DISABLED)
  2043. cs35l43_regmap_update_bits(cs35l43, CS35L43_GLOBAL_SAMPLE_RATE,
  2044. CS35L43_GLOBAL_FS_MASK, cs35l43_fs_rates[i].fs_cfg);
  2045. else if (cs35l43->ultrasonic_mode != CS35L43_ULTRASONIC_MODE_DISABLED)
  2046. /* Assume 48k base rate */
  2047. cs35l43_regmap_update_bits(cs35l43, CS35L43_GLOBAL_SAMPLE_RATE,
  2048. CS35L43_GLOBAL_FS_MASK, 0x03);
  2049. else {
  2050. dev_err(cs35l43->dev, "%s: Unsupported rate\n", __func__);
  2051. return -EINVAL;
  2052. }
  2053. asp_wl = params_width(params);
  2054. asp_width = cs35l43->slot_width ?
  2055. cs35l43->slot_width : params_physical_width(params);
  2056. dev_dbg(cs35l43->dev, "%s\n wl=%d, width=%d, rate=%d", __func__,
  2057. asp_wl, asp_width, rate);
  2058. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2059. cs35l43_regmap_update_bits(cs35l43, CS35L43_ASP_CONTROL2,
  2060. CS35L43_ASP_RX_WIDTH_MASK, asp_width <<
  2061. CS35L43_ASP_RX_WIDTH_SHIFT);
  2062. cs35l43_regmap_update_bits(cs35l43, CS35L43_ASP_DATA_CONTROL5,
  2063. CS35L43_ASP_RX_WL_MASK, asp_wl);
  2064. } else {
  2065. cs35l43_regmap_update_bits(cs35l43, CS35L43_ASP_CONTROL2,
  2066. CS35L43_ASP_TX_WIDTH_MASK, asp_width <<
  2067. CS35L43_ASP_TX_WIDTH_SHIFT);
  2068. cs35l43_regmap_update_bits(cs35l43, CS35L43_ASP_DATA_CONTROL1,
  2069. CS35L43_ASP_TX_WL_MASK, asp_wl);
  2070. }
  2071. return 0;
  2072. }
  2073. static int cs35l43_convert_ramp_rate(struct cs35l43_private *cs35l43,
  2074. unsigned int ramp_rate)
  2075. {
  2076. const unsigned int ramp_conv_table[] = {0, 1, 2, 4, 8, 16, 30, 60};
  2077. /* Convert the ramp_rate register setting into a time delay in ms.
  2078. * This assumes the starting volume is 0 dB
  2079. */
  2080. if (ramp_rate >= ARRAY_SIZE(ramp_conv_table)) {
  2081. dev_err(cs35l43->dev,
  2082. "Invalid rate (%d) to convert\n", ramp_rate);
  2083. return -EINVAL;
  2084. }
  2085. return ramp_conv_table[ramp_rate] * CS35L43_AMP_VOL_MIN / 12;
  2086. }
  2087. static int cs35l43_pcm_mute(struct snd_soc_dai *dai, int mute, int direction)
  2088. {
  2089. struct cs35l43_private *cs35l43 =
  2090. snd_soc_component_get_drvdata(dai->component);
  2091. unsigned int vol, vol_ramp;
  2092. int vol_ramp_ms;
  2093. dev_info(cs35l43->dev, "%s mute=%d\n", __func__, mute);
  2094. if (mute) {
  2095. regmap_update_bits(cs35l43->regmap,
  2096. CS35L43_AMP_CTRL,
  2097. CS35L43_AMP_VOL_PCM_MASK <<
  2098. CS35L43_AMP_VOL_PCM_SHIFT,
  2099. CS35L43_AMP_VOL_PCM_MUTE <<
  2100. CS35L43_AMP_VOL_PCM_SHIFT);
  2101. regmap_read(cs35l43->regmap,
  2102. CS35L43_AMP_CTRL, &vol_ramp);
  2103. vol_ramp &= CS35L43_AMP_RAMP_PCM_MASK;
  2104. vol_ramp_ms = cs35l43_convert_ramp_rate(cs35l43,
  2105. vol_ramp);
  2106. if (vol_ramp_ms < 0)
  2107. dev_err(cs35l43->dev,
  2108. "%s: Could not convert ramp rate\n",
  2109. __func__);
  2110. else if (vol_ramp_ms < 20)
  2111. usleep_range(vol_ramp_ms * 1000,
  2112. vol_ramp_ms * 1000 + 100);
  2113. else
  2114. msleep(vol_ramp_ms);
  2115. cs35l43->pcm_muted = true;
  2116. } else {
  2117. if (cs35l43->amp_mute) {
  2118. vol = cs35l43->pcm_vol;
  2119. /* convert control val to register val */
  2120. if (vol < CS35L43_AMP_VOL_CTRL_DEFAULT)
  2121. vol += CS35L43_AMP_VOL_PCM_MUTE;
  2122. else
  2123. vol -= CS35L43_AMP_VOL_CTRL_DEFAULT;
  2124. /* unmute */
  2125. regmap_update_bits(cs35l43->regmap,
  2126. CS35L43_AMP_CTRL,
  2127. CS35L43_AMP_VOL_PCM_MASK <<
  2128. CS35L43_AMP_VOL_PCM_SHIFT,
  2129. vol << CS35L43_AMP_VOL_PCM_SHIFT);
  2130. }
  2131. cs35l43->pcm_muted = false;
  2132. }
  2133. return 0;
  2134. }
  2135. static int cs35l43_get_clk_config(int freq)
  2136. {
  2137. int i;
  2138. for (i = 0; i < ARRAY_SIZE(cs35l43_pll_sysclk); i++) {
  2139. if (cs35l43_pll_sysclk[i].freq == freq)
  2140. return cs35l43_pll_sysclk[i].clk_cfg;
  2141. }
  2142. return -EINVAL;
  2143. }
  2144. static const unsigned int cs35l43_src_rates[] = {
  2145. 8000, 12000, 11025, 16000, 22050, 24000, 32000,
  2146. 44100, 48000, 88200, 96000, 176400, 192000
  2147. };
  2148. static const struct snd_pcm_hw_constraint_list cs35l43_constraints = {
  2149. .count = ARRAY_SIZE(cs35l43_src_rates),
  2150. .list = cs35l43_src_rates,
  2151. };
  2152. static int cs35l43_pcm_startup(struct snd_pcm_substream *substream,
  2153. struct snd_soc_dai *dai)
  2154. {
  2155. struct cs35l43_private *cs35l43 =
  2156. snd_soc_component_get_drvdata(dai->component);
  2157. int ret = 0;
  2158. dev_dbg(cs35l43->dev, "%s\n", __func__);
  2159. if (substream->runtime)
  2160. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  2161. SNDRV_PCM_HW_PARAM_RATE, &cs35l43_constraints);
  2162. return ret;
  2163. }
  2164. static int cs35l43_get_fs_mon_config_index(int freq)
  2165. {
  2166. int i;
  2167. for (i = 0; i < ARRAY_SIZE(cs35l43_fs_mon); i++) {
  2168. if (cs35l43_fs_mon[i].freq == freq)
  2169. return i;
  2170. }
  2171. return -EINVAL;
  2172. }
  2173. static int cs35l43_component_set_sysclk(struct snd_soc_component *component,
  2174. int clk_id, int source, unsigned int freq,
  2175. int dir)
  2176. {
  2177. struct cs35l43_private *cs35l43 =
  2178. snd_soc_component_get_drvdata(component);
  2179. unsigned int fs1_val;
  2180. unsigned int fs2_val;
  2181. unsigned int val;
  2182. int fsIndex;
  2183. dev_dbg(cs35l43->dev, "%s\n", __func__);
  2184. dev_dbg(cs35l43->dev, "%s id = %d, freq=%d\n", __func__, clk_id, freq);
  2185. cs35l43->extclk_cfg = cs35l43_get_clk_config(freq);
  2186. cs35l43->clk_id = clk_id;
  2187. if (freq <= 6000000) {
  2188. /* Use the lookup table */
  2189. fsIndex = cs35l43_get_fs_mon_config_index(freq);
  2190. if (fsIndex < 0) {
  2191. dev_err(cs35l43->dev, "Invalid CLK Config freq: %u\n", freq);
  2192. return -EINVAL;
  2193. }
  2194. fs1_val = cs35l43_fs_mon[fsIndex].fs1;
  2195. fs2_val = cs35l43_fs_mon[fsIndex].fs2;
  2196. } else {
  2197. /* Use hard-coded values */
  2198. fs1_val = 18;
  2199. fs2_val = 33;
  2200. }
  2201. val = fs1_val;
  2202. val |= (fs2_val << CS35L43_FS2_START_WINDOW_SHIFT) & CS35L43_FS2_START_WINDOW_MASK;
  2203. if (cs35l43->extclk_cfg < 0) {
  2204. dev_err(cs35l43->dev, "Invalid CLK Config: %d, freq: %u\n",
  2205. cs35l43->extclk_cfg, freq);
  2206. return -EINVAL;
  2207. }
  2208. if (cs35l43->hibernate_state != CS35L43_HIBERNATE_STANDBY) {
  2209. cs35l43_pll_config(cs35l43);
  2210. cs35l43_regmap_write(cs35l43, CS35L43_FS_MON_0, val);
  2211. if (cs35l43->limit_spi_clock)
  2212. cs35l43->limit_spi_clock(cs35l43, false);
  2213. }
  2214. return 0;
  2215. }
  2216. static int cs35l43_dai_set_sysclk(struct snd_soc_dai *dai,
  2217. int clk_id, unsigned int freq, int dir)
  2218. {
  2219. struct cs35l43_private *cs35l43 =
  2220. snd_soc_component_get_drvdata(dai->component);
  2221. dev_dbg(cs35l43->dev, "%s\n", __func__);
  2222. return 0;
  2223. }
  2224. int cs35l43_component_write(struct snd_soc_component *component,
  2225. unsigned int reg, unsigned int val)
  2226. {
  2227. struct cs35l43_private *cs35l43 =
  2228. snd_soc_component_get_drvdata(component);
  2229. int ret = 0;
  2230. mutex_lock(&cs35l43->hb_lock);
  2231. ret = cs35l43_regmap_write(cs35l43, reg, val);
  2232. mutex_unlock(&cs35l43->hb_lock);
  2233. return ret;
  2234. }
  2235. unsigned int cs35l43_component_read(struct snd_soc_component *component,
  2236. unsigned int reg)
  2237. {
  2238. struct cs35l43_private *cs35l43 =
  2239. snd_soc_component_get_drvdata(component);
  2240. unsigned int val;
  2241. cs35l43_regmap_read(cs35l43, reg, &val);
  2242. return val;
  2243. }
  2244. static int cs35l43_irq_gpio_config(struct cs35l43_private *cs35l43)
  2245. {
  2246. int irq_pol = IRQF_TRIGGER_NONE;
  2247. if (cs35l43->pdata.gpio1_out_enable)
  2248. cs35l43_regmap_update_bits(cs35l43,
  2249. CS35L43_GPIO1_CTRL1,
  2250. CS35L43_GP1_DIR_MASK,
  2251. 0);
  2252. if (cs35l43->pdata.gpio1_src_sel)
  2253. cs35l43_regmap_update_bits(cs35l43,
  2254. CS35L43_GPIO_PAD_CONTROL,
  2255. CS35L43_GP1_CTRL_MASK,
  2256. cs35l43->pdata.gpio1_src_sel <<
  2257. CS35L43_GP1_CTRL_SHIFT);
  2258. if (cs35l43->pdata.gpio2_out_enable)
  2259. cs35l43_regmap_update_bits(cs35l43,
  2260. CS35L43_GPIO2_CTRL1,
  2261. CS35L43_GP2_DIR_MASK,
  2262. 0);
  2263. if (cs35l43->pdata.gpio2_src_sel)
  2264. cs35l43_regmap_update_bits(cs35l43,
  2265. CS35L43_GPIO_PAD_CONTROL,
  2266. CS35L43_GP2_CTRL_MASK,
  2267. cs35l43->pdata.gpio2_src_sel <<
  2268. CS35L43_GP2_CTRL_SHIFT);
  2269. if (cs35l43->pdata.gpio2_src_sel ==
  2270. (CS35L43_GP2_CTRL_OPEN_DRAIN_ACTV_LO | CS35L43_VALID_PDATA) ||
  2271. cs35l43->pdata.gpio2_src_sel ==
  2272. (CS35L43_GP2_CTRL_PUSH_PULL_ACTV_LO | CS35L43_VALID_PDATA))
  2273. irq_pol = IRQF_TRIGGER_LOW;
  2274. else if (cs35l43->pdata.gpio2_src_sel ==
  2275. (CS35L43_GP2_CTRL_PUSH_PULL_ACTV_HI | CS35L43_VALID_PDATA))
  2276. irq_pol = IRQF_TRIGGER_HIGH;
  2277. return irq_pol;
  2278. }
  2279. static int cs35l43_set_pdata(struct cs35l43_private *cs35l43)
  2280. {
  2281. if (cs35l43->pdata.bst_vctrl)
  2282. cs35l43_regmap_update_bits(cs35l43, CS35L43_VBST_CTL_1,
  2283. CS35L43_BST_CTL_MASK, cs35l43->pdata.bst_vctrl);
  2284. if (cs35l43->pdata.classh_disable)
  2285. cs35l43_regmap_update_bits(cs35l43, CS35L43_VBST_CTL_2,
  2286. CS35L43_BST_CTL_SEL_MASK, 0);
  2287. else {
  2288. if (cs35l43->pdata.bst_vctrl)
  2289. cs35l43_regmap_update_bits(cs35l43, CS35L43_VBST_CTL_2,
  2290. CS35L43_BST_CTL_LIM_EN_MASK,
  2291. CS35L43_BST_CTL_LIM_EN_MASK);
  2292. }
  2293. if (cs35l43->pdata.bst_ipk) {
  2294. cs35l43_regmap_update_bits(cs35l43,
  2295. CS35L43_BST_IPK_CTL,
  2296. CS35L43_BST_IPK_MASK,
  2297. cs35l43->pdata.bst_ipk);
  2298. }
  2299. if (cs35l43->pdata.dsp_ng_enable) {
  2300. cs35l43_regmap_update_bits(cs35l43,
  2301. CS35L43_MIXER_NGATE_CH1_CFG,
  2302. CS35L43_AUX_NGATE_CH1_EN_MASK,
  2303. CS35L43_AUX_NGATE_CH1_EN_MASK);
  2304. cs35l43_regmap_update_bits(cs35l43,
  2305. CS35L43_MIXER_NGATE_CH2_CFG,
  2306. CS35L43_AUX_NGATE_CH2_EN_MASK,
  2307. CS35L43_AUX_NGATE_CH2_EN_MASK);
  2308. if (cs35l43->pdata.dsp_ng_pcm_thld) {
  2309. cs35l43_regmap_update_bits(cs35l43,
  2310. CS35L43_MIXER_NGATE_CH1_CFG,
  2311. CS35L43_AUX_NGATE_CH1_THR_MASK,
  2312. cs35l43->pdata.dsp_ng_pcm_thld);
  2313. cs35l43_regmap_update_bits(cs35l43,
  2314. CS35L43_MIXER_NGATE_CH2_CFG,
  2315. CS35L43_AUX_NGATE_CH2_THR_MASK,
  2316. cs35l43->pdata.dsp_ng_pcm_thld);
  2317. }
  2318. if (cs35l43->pdata.dsp_ng_delay) {
  2319. cs35l43_regmap_update_bits(cs35l43,
  2320. CS35L43_MIXER_NGATE_CH1_CFG,
  2321. CS35L43_AUX_NGATE_CH1_HOLD_MASK,
  2322. cs35l43->pdata.dsp_ng_delay <<
  2323. CS35L43_AUX_NGATE_CH1_HOLD_SHIFT);
  2324. cs35l43_regmap_update_bits(cs35l43,
  2325. CS35L43_MIXER_NGATE_CH2_CFG,
  2326. CS35L43_AUX_NGATE_CH2_HOLD_MASK,
  2327. cs35l43->pdata.dsp_ng_delay <<
  2328. CS35L43_AUX_NGATE_CH2_HOLD_SHIFT);
  2329. }
  2330. }
  2331. if (cs35l43->pdata.asp_sdout_hiz)
  2332. cs35l43_regmap_update_bits(cs35l43,
  2333. CS35L43_ASP_CONTROL3,
  2334. CS35L41_ASP_DOUT_HIZ_CTRL_MASK,
  2335. cs35l43->pdata.asp_sdout_hiz);
  2336. if (cs35l43->pdata.hw_ng_sel)
  2337. cs35l43_regmap_update_bits(cs35l43,
  2338. CS35L43_NG_CONFIG,
  2339. CS35L43_NG_EN_SEL_MASK,
  2340. cs35l43->pdata.hw_ng_sel <<
  2341. CS35L43_NG_EN_SEL_SHIFT);
  2342. if (cs35l43->pdata.hw_ng_thld)
  2343. cs35l43_regmap_update_bits(cs35l43,
  2344. CS35L43_NG_CONFIG,
  2345. CS35L43_NG_PCM_THLD_MASK,
  2346. cs35l43->pdata.hw_ng_thld <<
  2347. CS35L43_NG_PCM_THLD_SHIFT);
  2348. if (cs35l43->pdata.hw_ng_delay)
  2349. cs35l43_regmap_update_bits(cs35l43,
  2350. CS35L43_NG_CONFIG,
  2351. CS35L43_NG_DELAY_MASK,
  2352. cs35l43->pdata.hw_ng_delay <<
  2353. CS35L43_NG_DELAY_SHIFT);
  2354. if (cs35l43->pdata.vpbr_rel_rate)
  2355. cs35l43_regmap_update_bits(cs35l43,
  2356. CS35L43_VPBR_CONFIG,
  2357. CS35L43_VPBR_REL_RATE_MASK,
  2358. cs35l43->pdata.vpbr_rel_rate <<
  2359. CS35L43_VPBR_REL_RATE_SHIFT);
  2360. if (cs35l43->pdata.vpbr_wait)
  2361. cs35l43_regmap_update_bits(cs35l43,
  2362. CS35L43_VPBR_CONFIG,
  2363. CS35L43_VPBR_WAIT_MASK,
  2364. cs35l43->pdata.vpbr_wait <<
  2365. CS35L43_VPBR_WAIT_SHIFT);
  2366. if (cs35l43->pdata.vpbr_atk_rate)
  2367. cs35l43_regmap_update_bits(cs35l43,
  2368. CS35L43_VPBR_CONFIG,
  2369. CS35L43_VPBR_ATK_RATE_MASK,
  2370. cs35l43->pdata.vpbr_atk_rate <<
  2371. CS35L43_VPBR_ATK_RATE_SHIFT);
  2372. if (cs35l43->pdata.vpbr_atk_vol)
  2373. cs35l43_regmap_update_bits(cs35l43,
  2374. CS35L43_VPBR_CONFIG,
  2375. CS35L43_VPBR_ATK_VOL_MASK,
  2376. cs35l43->pdata.vpbr_atk_vol <<
  2377. CS35L43_VPBR_ATK_VOL_SHIFT);
  2378. if (cs35l43->pdata.vpbr_max_att)
  2379. cs35l43_regmap_update_bits(cs35l43,
  2380. CS35L43_VPBR_CONFIG,
  2381. CS35L43_VPBR_MAX_ATT_MASK,
  2382. cs35l43->pdata.vpbr_max_att <<
  2383. CS35L43_VPBR_MAX_ATT_SHIFT);
  2384. if (cs35l43->pdata.vpbr_thld)
  2385. cs35l43_regmap_update_bits(cs35l43,
  2386. CS35L43_VPBR_CONFIG,
  2387. CS35L43_VPBR_THLD1_MASK,
  2388. cs35l43->pdata.vpbr_thld <<
  2389. CS35L43_VPBR_THLD1_SHIFT);
  2390. if (cs35l43->pdata.vpbr_enable)
  2391. cs35l43_regmap_update_bits(cs35l43,
  2392. CS35L43_BLOCK_ENABLES2,
  2393. CS35L43_VPBR_EN_MASK,
  2394. CS35L43_VPBR_EN_MASK);
  2395. return 0;
  2396. }
  2397. static int cs35l43_handle_of_data(struct device *dev,
  2398. struct cs35l43_platform_data *pdata,
  2399. struct cs35l43_private *cs35l43)
  2400. {
  2401. struct device_node *np = dev->of_node;
  2402. int ret, val;
  2403. if (!np)
  2404. return 0;
  2405. pdata->dsp_ng_enable = of_property_read_bool(np,
  2406. "cirrus,dsp-noise-gate-enable");
  2407. if (of_property_read_u32(np,
  2408. "cirrus,dsp-noise-gate-threshold", &val) >= 0)
  2409. pdata->dsp_ng_pcm_thld = val | CS35L43_VALID_PDATA;
  2410. if (of_property_read_u32(np, "cirrus,dsp-noise-gate-delay", &val) >= 0)
  2411. pdata->dsp_ng_delay = val | CS35L43_VALID_PDATA;
  2412. if (of_property_read_u32(np, "cirrus,hw-noise-gate-select", &val) >= 0)
  2413. pdata->hw_ng_sel = val | CS35L43_VALID_PDATA;
  2414. if (of_property_read_u32(np,
  2415. "cirrus,hw-noise-gate-threshold", &val) >= 0)
  2416. pdata->hw_ng_thld = val | CS35L43_VALID_PDATA;
  2417. if (of_property_read_u32(np, "cirrus,hw-noise-gate-delay", &val) >= 0)
  2418. pdata->hw_ng_delay = val | CS35L43_VALID_PDATA;
  2419. if (of_property_read_u32(np, "cirrus,gpio1-src-sel", &val) >= 0)
  2420. pdata->gpio1_src_sel = val | CS35L43_VALID_PDATA;
  2421. if (of_property_read_u32(np, "cirrus,gpio2-src-sel", &val) >= 0)
  2422. pdata->gpio2_src_sel = val | CS35L43_VALID_PDATA;
  2423. pdata->gpio1_out_enable = of_property_read_bool(np,
  2424. "cirrus,gpio1-output-enable");
  2425. pdata->gpio2_out_enable = of_property_read_bool(np,
  2426. "cirrus,gpio2-output-enable");
  2427. pdata->vpbr_enable = of_property_read_bool(np,
  2428. "cirrus,vpbr-enable");
  2429. if (of_property_read_u32(np, "cirrus,vpbr-rel-rate", &val) >= 0)
  2430. pdata->vpbr_rel_rate = val | CS35L43_VALID_PDATA;
  2431. if (of_property_read_u32(np, "cirrus,vpbr-wait", &val) >= 0)
  2432. pdata->vpbr_wait = val | CS35L43_VALID_PDATA;
  2433. if (of_property_read_u32(np, "cirrus,vpbr-atk-rate", &val) >= 0)
  2434. pdata->vpbr_atk_rate = val | CS35L43_VALID_PDATA;
  2435. if (of_property_read_u32(np, "cirrus,vpbr-atk-vol", &val) >= 0)
  2436. pdata->vpbr_atk_vol = val | CS35L43_VALID_PDATA;
  2437. if (of_property_read_u32(np, "cirrus,vpbr-max-att", &val) >= 0)
  2438. pdata->vpbr_max_att = val | CS35L43_VALID_PDATA;
  2439. if (of_property_read_u32(np, "cirrus,vpbr-thld", &val) >= 0)
  2440. pdata->vpbr_thld = val | CS35L43_VALID_PDATA;
  2441. if (of_property_read_u32(np, "cirrus,asp-sdout-hiz", &val) >= 0)
  2442. pdata->asp_sdout_hiz = val | CS35L43_VALID_PDATA;
  2443. if (of_property_read_u32(np, "cirrus,bst-ipk-ma", &val) >= 0) {
  2444. if ((val < 1600) || (val > 4500)) {
  2445. dev_err(dev, "Invalid boost inductor peak current: %d mA\n",
  2446. val);
  2447. return -EINVAL;
  2448. }
  2449. pdata->bst_ipk = ((val - 1600) / 50) + 0x10;
  2450. }
  2451. pdata->classh_disable = of_property_read_bool(np,
  2452. "cirrus,classh-disable");
  2453. ret = of_property_read_u32(np, "cirrus,boost-ctl-millivolt", &val);
  2454. if (ret >= 0) {
  2455. if (val < 2550 || val > 11000) {
  2456. dev_err(dev,
  2457. "Invalid Boost Voltage %u mV\n", val);
  2458. return -EINVAL;
  2459. }
  2460. pdata->bst_vctrl = ((val - 2550) / 100) + 1;
  2461. }
  2462. ret = of_property_read_string(np, "cirrus,dsp-part-name",
  2463. &pdata->dsp_part_name);
  2464. if (ret < 0)
  2465. pdata->dsp_part_name = "cs35l43";
  2466. cs35l43->low_pwr_mode = of_property_read_bool(np, "cirrus,low-pwr-mode-standby");
  2467. return 0;
  2468. }
  2469. static struct reg_sequence cs35l43_cal_pre_config[] = {
  2470. { CS35L43_NG_CONFIG, 0 },
  2471. { CS35L43_MIXER_NGATE_CH1_CFG, 0 },
  2472. { CS35L43_MIXER_NGATE_CH2_CFG, 0 },
  2473. };
  2474. static struct reg_sequence cs35l43_cal_post_config[] = {
  2475. { CS35L43_NG_CONFIG, 0 },
  2476. { CS35L43_MIXER_NGATE_CH1_CFG, 0 },
  2477. { CS35L43_MIXER_NGATE_CH2_CFG, 0 },
  2478. };
  2479. #define CS35L43_CAL_N_CONFIGS 1
  2480. static int cs35l43_cirrus_amp_probe(struct cs35l43_private *cs35l43,
  2481. struct snd_soc_component *component)
  2482. {
  2483. unsigned int property, target_temp = 0, exit_temp = 0;
  2484. struct device_node *pwr_params;
  2485. bool pwr_enable = false;
  2486. const char *dsp_part_name;
  2487. const char *mfd_suffix;
  2488. int ret, bd_max_temp;
  2489. struct cirrus_amp_config amp_cfg = {0};
  2490. bool calibration_disable;
  2491. unsigned int default_redc;
  2492. ret = of_property_read_string(cs35l43->dev->of_node,
  2493. "cirrus,dsp-part-name",
  2494. &dsp_part_name);
  2495. if (ret < 0)
  2496. dsp_part_name = "cs35l43";
  2497. ret = of_property_read_string(cs35l43->dev->of_node,
  2498. "cirrus,mfd-suffix",
  2499. &mfd_suffix);
  2500. if (ret >= 0)
  2501. cs35l43->pdata.mfd_suffix = mfd_suffix;
  2502. ret = of_property_read_u32(cs35l43->dev->of_node,
  2503. "cirrus,bd-max-temp", &bd_max_temp);
  2504. if (ret < 0)
  2505. bd_max_temp = -1;
  2506. ret = of_property_read_u32(cs35l43->dev->of_node,
  2507. "cirrus,default-redc", &default_redc);
  2508. if (ret < 0)
  2509. default_redc = 0;
  2510. calibration_disable = of_property_read_bool(cs35l43->dev->of_node,
  2511. "cirrus,calibration-disable");
  2512. pwr_params = of_get_child_by_name(cs35l43->dev->of_node,
  2513. "cirrus,pwr-params");
  2514. if (pwr_params) {
  2515. pwr_enable = of_property_read_bool(pwr_params,
  2516. "cirrus,pwr-global-enable");
  2517. ret = of_property_read_u32(pwr_params, "cirrus,pwr-target-temp",
  2518. &property);
  2519. if (ret >= 0)
  2520. target_temp = property;
  2521. ret = of_property_read_u32(pwr_params, "cirrus,pwr-exit-temp",
  2522. &property);
  2523. if (ret >= 0)
  2524. exit_temp = property;
  2525. }
  2526. of_node_put(pwr_params);
  2527. cs35l43_regmap_read(cs35l43, CS35L43_NG_CONFIG,
  2528. &cs35l43_cal_post_config[0].def);
  2529. cs35l43_regmap_read(cs35l43, CS35L43_MIXER_NGATE_CH1_CFG,
  2530. &cs35l43_cal_post_config[1].def);
  2531. cs35l43_regmap_read(cs35l43, CS35L43_MIXER_NGATE_CH2_CFG,
  2532. &cs35l43_cal_post_config[2].def);
  2533. amp_cfg.component = component;
  2534. amp_cfg.regmap = cs35l43->regmap;
  2535. amp_cfg.pre_config = cs35l43_cal_pre_config;
  2536. amp_cfg.post_config = cs35l43_cal_post_config;
  2537. amp_cfg.dsp_part_name = dsp_part_name;
  2538. amp_cfg.num_pre_configs = ARRAY_SIZE(cs35l43_cal_pre_config);
  2539. amp_cfg.num_post_configs = ARRAY_SIZE(cs35l43_cal_post_config);
  2540. amp_cfg.mbox_cmd = CS35L43_DSP_VIRTUAL1_MBOX_1;
  2541. amp_cfg.mbox_sts = 0;
  2542. amp_cfg.global_en = CS35L43_GLOBAL_ENABLES;
  2543. amp_cfg.global_en_mask = 1;
  2544. amp_cfg.bd_max_temp = bd_max_temp;
  2545. amp_cfg.target_temp = target_temp;
  2546. amp_cfg.exit_temp = exit_temp;
  2547. amp_cfg.pwr_enable = pwr_enable;
  2548. amp_cfg.default_redc = default_redc;
  2549. amp_cfg.perform_vimon_cal = true;
  2550. amp_cfg.calibration_disable = calibration_disable;
  2551. amp_cfg.vimon_alg_id = 0x5f224;
  2552. amp_cfg.halo_alg_id = 0x1810d6;
  2553. amp_cfg.bd_alg_id = 0x5f21f;
  2554. amp_cfg.bd_prefix = "";
  2555. amp_cfg.cal_vpk_id = 0;
  2556. amp_cfg.cal_ipk_id = 0;
  2557. amp_cfg.amp_reinit = cs35l43_reinit;
  2558. amp_cfg.cal_ops_idx = CIRRUS_CAL_CS35L43_CAL_OPS_IDX;
  2559. amp_cfg.runtime_pm = true;
  2560. ret = cirrus_amp_add(mfd_suffix, amp_cfg);
  2561. if (ret < 0) {
  2562. dev_err(cs35l43->dev, "Failed to register cirrus amp (%d)\n",
  2563. ret);
  2564. return -EPROBE_DEFER;
  2565. }
  2566. return 0;
  2567. }
  2568. static int cs35l43_component_probe(struct snd_soc_component *component)
  2569. {
  2570. int ret = 0;
  2571. struct cs35l43_private *cs35l43 =
  2572. snd_soc_component_get_drvdata(component);
  2573. cs35l43_set_pdata(cs35l43);
  2574. cs35l43->component = component;
  2575. wm_adsp2_component_probe(&cs35l43->dsp, component);
  2576. cs35l43_cirrus_amp_probe(cs35l43, component);
  2577. return ret;
  2578. }
  2579. static void cs35l43_component_remove(struct snd_soc_component *component)
  2580. {
  2581. }
  2582. static const struct cs_dsp_region cs35l43_dsp1_regions[] = {
  2583. { .type = WMFW_HALO_PM_PACKED, .base = CS35L43_DSP1_PMEM_0 },
  2584. { .type = WMFW_HALO_XM_PACKED, .base = CS35L43_DSP1_XMEM_PACKED_0 },
  2585. { .type = WMFW_HALO_YM_PACKED, .base = CS35L43_DSP1_YMEM_PACKED_0 },
  2586. {. type = WMFW_ADSP2_XM, .base = CS35L43_DSP1_XMEM_UNPACKED24_0},
  2587. {. type = WMFW_ADSP2_YM, .base = CS35L43_DSP1_YMEM_UNPACKED24_0},
  2588. };
  2589. static int cs35l43_dsp_init(struct cs35l43_private *cs35l43)
  2590. {
  2591. struct wm_adsp *dsp;
  2592. int ret;
  2593. dsp = &cs35l43->dsp;
  2594. dsp->part = cs35l43->pdata.dsp_part_name;
  2595. dsp->cs_dsp.num = 1;
  2596. dsp->cs_dsp.type = WMFW_HALO;
  2597. dsp->cs_dsp.rev = 0;
  2598. dsp->fw = 9; /* 9 is WM_ADSP_FW_SPK_PROT in wm_adsp.c */
  2599. dsp->cs_dsp.dev = cs35l43->dev;
  2600. dsp->cs_dsp.regmap = cs35l43->regmap;
  2601. dsp->cs_dsp.base = CS35L43_DSP1_CLOCK_FREQ;
  2602. dsp->cs_dsp.base_sysinfo = CS35L43_DSP1_SYS_INFO_ID;
  2603. dsp->cs_dsp.mem = cs35l43_dsp1_regions;
  2604. dsp->cs_dsp.num_mems = ARRAY_SIZE(cs35l43_dsp1_regions);
  2605. dsp->cs_dsp.lock_regions = 0xFFFFFFFF;
  2606. dsp->toggle_preload = true;
  2607. ret = wm_halo_init(dsp);
  2608. if (ret != 0) {
  2609. dev_err(cs35l43->dev, "wm_halo_init failed\n");
  2610. goto err;
  2611. }
  2612. cs_dsp_stop(&dsp->cs_dsp);
  2613. cs35l43_regmap_write(cs35l43, CS35L43_DSP1RX3_INPUT, CS35L43_INPUT_SRC_VBSTMON);
  2614. cs35l43_regmap_write(cs35l43, CS35L43_DSP1RX4_INPUT, CS35L43_INPUT_SRC_IMON);
  2615. cs35l43_regmap_write(cs35l43, CS35L43_DSP1RX5_INPUT, CS35L43_INPUT_SRC_VMON);
  2616. cs35l43_regmap_write(cs35l43, CS35L43_DSP1RX6_INPUT, CS35L43_INPUT_SRC_VPMON);
  2617. return 0;
  2618. err:
  2619. return ret;
  2620. }
  2621. static int cs35l43_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2622. unsigned int tx_mask, unsigned int rx_mask,
  2623. int slots, int slot_width)
  2624. {
  2625. struct cs35l43_private *cs35l43 =
  2626. snd_soc_component_get_drvdata(dai->component);
  2627. cs35l43->slot_width = slot_width;
  2628. return 0;
  2629. }
  2630. static int cs35l43_compr_open(struct snd_soc_component *component,
  2631. struct snd_compr_stream *stream)
  2632. {
  2633. struct snd_soc_pcm_runtime *rtd = stream->private_data;
  2634. struct cs35l43_private *cs35l43 =
  2635. snd_soc_component_get_drvdata(component);
  2636. struct snd_soc_dai *codec_dai = NULL;
  2637. int i;
  2638. for_each_rtd_dais(rtd, i, codec_dai) {
  2639. if (!strcmp(codec_dai->name, "cs35l43-dsp-textlog"))
  2640. return wm_adsp_compr_open(&cs35l43->dsp, stream);
  2641. }
  2642. dev_err(cs35l43->dev, "No DSP log DAI found\n");
  2643. return -EINVAL;
  2644. }
  2645. static const struct snd_soc_dai_ops cs35l43_ops = {
  2646. .startup = cs35l43_pcm_startup,
  2647. .set_fmt = cs35l43_set_dai_fmt,
  2648. .hw_params = cs35l43_pcm_hw_params,
  2649. .set_sysclk = cs35l43_dai_set_sysclk,
  2650. .set_tdm_slot = cs35l43_dai_set_tdm_slot,
  2651. .mute_stream = cs35l43_pcm_mute,
  2652. .no_capture_mute = 1,
  2653. };
  2654. static struct snd_soc_dai_driver cs35l43_dai[] = {
  2655. {
  2656. .name = "cs35l43-pcm",
  2657. .id = 0,
  2658. .playback = {
  2659. .stream_name = "AMP Playback",
  2660. .channels_min = 1,
  2661. .channels_max = 2,
  2662. .rates = SNDRV_PCM_RATE_KNOT,
  2663. .formats = CS35L43_RX_FORMATS,
  2664. },
  2665. .capture = {
  2666. .stream_name = "AMP Capture",
  2667. .channels_min = 1,
  2668. .channels_max = 8,
  2669. .rates = SNDRV_PCM_RATE_KNOT,
  2670. .formats = CS35L43_TX_FORMATS,
  2671. },
  2672. .ops = &cs35l43_ops,
  2673. .symmetric_rate = 1,
  2674. },
  2675. {
  2676. .name = "cs35l43-cpu-textlog",
  2677. .capture = {
  2678. .stream_name = "Audio Log CPU",
  2679. .channels_min = 1,
  2680. .channels_max = 1,
  2681. .rates = SNDRV_PCM_RATE_KNOT,
  2682. .formats = CS35L43_RX_FORMATS,
  2683. },
  2684. .compress_new = &snd_soc_new_compress,
  2685. },
  2686. {
  2687. .name = "cs35l43-dsp-textlog",
  2688. .capture = {
  2689. .stream_name = "Audio Log DSP",
  2690. .channels_min = 1,
  2691. .channels_max = 1,
  2692. .rates = SNDRV_PCM_RATE_KNOT,
  2693. .formats = CS35L43_RX_FORMATS,
  2694. },
  2695. }
  2696. };
  2697. static const struct snd_compress_ops cs35l43_compr_ops = {
  2698. .open = &cs35l43_compr_open,
  2699. .free = &wm_adsp_compr_free,
  2700. .set_params = &wm_adsp_compr_set_params,
  2701. .get_caps = &wm_adsp_compr_get_caps,
  2702. .trigger = &wm_adsp_compr_trigger,
  2703. .pointer = &wm_adsp_compr_pointer,
  2704. .copy = &wm_adsp_compr_copy,
  2705. };
  2706. static const struct snd_soc_component_driver soc_component_dev_cs35l43 = {
  2707. .name = DRV_NAME,
  2708. .probe = cs35l43_component_probe,
  2709. .remove = cs35l43_component_remove,
  2710. .dapm_widgets = cs35l43_dapm_widgets,
  2711. .num_dapm_widgets = ARRAY_SIZE(cs35l43_dapm_widgets),
  2712. .dapm_routes = cs35l43_audio_map,
  2713. .num_dapm_routes = ARRAY_SIZE(cs35l43_audio_map),
  2714. .controls = cs35l43_aud_controls,
  2715. .num_controls = ARRAY_SIZE(cs35l43_aud_controls),
  2716. .set_sysclk = cs35l43_component_set_sysclk,
  2717. .write = cs35l43_component_write,
  2718. .read = cs35l43_component_read,
  2719. .compress_ops = &cs35l43_compr_ops,
  2720. };
  2721. static struct reg_sequence cs35l43_errata_patch[] = {
  2722. { CS35L43_TST_DAC_MSM_CONFIG, 0x11330000 },
  2723. { CS35L43_BST_RSVD_1, 0x50000802 },
  2724. };
  2725. int cs35l43_probe(struct cs35l43_private *cs35l43,
  2726. struct cs35l43_platform_data *pdata)
  2727. {
  2728. int ret, i;
  2729. unsigned int regid, revid;
  2730. int irq_pol = IRQF_TRIGGER_HIGH;
  2731. for (i = 0; i < ARRAY_SIZE(cs35l43_supplies); i++)
  2732. cs35l43->supplies[i].supply = cs35l43_supplies[i];
  2733. cs35l43->num_supplies = ARRAY_SIZE(cs35l43_supplies);
  2734. ret = devm_regulator_bulk_get(cs35l43->dev, cs35l43->num_supplies,
  2735. cs35l43->supplies);
  2736. if (ret != 0) {
  2737. dev_err(cs35l43->dev,
  2738. "Failed to request core supplies: %d\n",
  2739. ret);
  2740. return ret;
  2741. }
  2742. if (pdata) {
  2743. cs35l43->pdata = *pdata;
  2744. } else if (cs35l43->dev->of_node) {
  2745. ret = cs35l43_handle_of_data(cs35l43->dev, &cs35l43->pdata,
  2746. cs35l43);
  2747. if (ret != 0)
  2748. return ret;
  2749. } else {
  2750. ret = -EINVAL;
  2751. goto err;
  2752. }
  2753. ret = regulator_bulk_enable(cs35l43->num_supplies, cs35l43->supplies);
  2754. if (ret != 0) {
  2755. dev_err(cs35l43->dev,
  2756. "Failed to enable core supplies: %d\n", ret);
  2757. return ret;
  2758. }
  2759. /* returning NULL can be an option if in stereo mode */
  2760. cs35l43->reset_gpio = devm_gpiod_get_optional(cs35l43->dev, "reset",
  2761. GPIOD_OUT_LOW);
  2762. if (IS_ERR(cs35l43->reset_gpio)) {
  2763. ret = PTR_ERR(cs35l43->reset_gpio);
  2764. cs35l43->reset_gpio = NULL;
  2765. if (ret == -EBUSY) {
  2766. dev_info(cs35l43->dev,
  2767. "Reset line busy, assuming shared reset\n");
  2768. } else {
  2769. dev_err(cs35l43->dev,
  2770. "Failed to get reset GPIO: %d\n", ret);
  2771. goto err;
  2772. }
  2773. }
  2774. if (cs35l43->reset_gpio) {
  2775. /* satisfy minimum reset pulse width spec */
  2776. usleep_range(2000, 2100);
  2777. gpiod_set_value_cansleep(cs35l43->reset_gpio, 1);
  2778. }
  2779. usleep_range(2000, 2100);
  2780. ret = cs35l43_regmap_read(cs35l43, CS35L43_DEVID, &regid);
  2781. if (ret < 0) {
  2782. dev_err(cs35l43->dev, "Get Device ID failed\n");
  2783. goto err;
  2784. }
  2785. ret = cs35l43_regmap_read(cs35l43, CS35L43_REVID, &revid);
  2786. if (ret < 0) {
  2787. dev_err(cs35l43->dev, "Get Revision ID failed\n");
  2788. goto err;
  2789. }
  2790. ret = regmap_register_patch(cs35l43->regmap,
  2791. cs35l43_errata_patch,
  2792. ARRAY_SIZE(cs35l43_errata_patch));
  2793. if (ret < 0) {
  2794. dev_err(cs35l43->dev, "Failed to apply errata patch %d\n", ret);
  2795. goto err;
  2796. }
  2797. cs35l43->hibernate_state = CS35L43_HIBERNATE_NOT_LOADED;
  2798. cs35l43->amp_switch = -1;
  2799. cs35l43->amp_mute = 1; /* Unmuted */
  2800. cs35l43->pcm_vol = CS35L43_AMP_VOL_CTRL_DEFAULT;
  2801. mutex_init(&cs35l43->hb_lock);
  2802. mutex_init(&cs35l43->err_lock);
  2803. cs35l43->err_wq = create_singlethread_workqueue("cs35l43_err");
  2804. INIT_WORK(&cs35l43->err_work, cs35l43_error_work);
  2805. cs35l43->mbox_wq = create_singlethread_workqueue("cs35l43_mbox");
  2806. INIT_WORK(&cs35l43->mbox_work, cs35l43_mbox_work);
  2807. cs35l43->irq_wq = create_singlethread_workqueue("cs35l43_irq");
  2808. INIT_DELAYED_WORK(&cs35l43->irq_work, cs35l43_irq_work);
  2809. cs35l43_dsp_init(cs35l43);
  2810. irq_pol = cs35l43_irq_gpio_config(cs35l43);
  2811. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_1, 0xFFFFFFFF);
  2812. cs35l43_regmap_update_bits(cs35l43, CS35L43_IRQ1_MASK_1,
  2813. CS35L43_AMP_ERR_EINT1_MASK |
  2814. CS35L43_BST_SHORT_ERR_EINT1_MASK |
  2815. CS35L43_BST_DCM_UVP_ERR_EINT1_MASK |
  2816. CS35L43_BST_OVP_ERR_EINT1_MASK |
  2817. CS35L43_DC_WATCHDOG_IRQ_RISE_EINT1_MASK |
  2818. CS35L43_WKSRC_STATUS6_EINT1_MASK |
  2819. CS35L43_WKSRC_STATUS_ANY_EINT1_MASK, 0);
  2820. cs35l43_regmap_write(cs35l43, CS35L43_IRQ1_MASK_2, 0xFFFFFFFF);
  2821. cs35l43_regmap_update_bits(cs35l43, CS35L43_IRQ1_MASK_2,
  2822. CS35L43_PLL_UNLOCK_FLAG_RISE_EINT1_MASK |
  2823. CS35L43_PLL_LOCK_EINT1_MASK, 0);
  2824. regmap_write(cs35l43->regmap, CS35L43_IRQ1_MASK_3, 0xFFFFFFFF);
  2825. regmap_update_bits(cs35l43->regmap, CS35L43_IRQ1_MASK_3,
  2826. CS35L43_DSP1_NMI_ERR_EINT1_MASK |
  2827. CS35L43_DSP1_MPU_ERR_EINT1_MASK |
  2828. CS35L43_DSP1_STRM_ARB_ERR_EINT1_MASK, 0);
  2829. cs35l43_regmap_update_bits(cs35l43, CS35L43_ALIVE_DCIN_WD,
  2830. CS35L43_DCIN_WD_EN_MASK,
  2831. CS35L43_DCIN_WD_EN_MASK);
  2832. cs35l43_regmap_update_bits(cs35l43, CS35L43_ALIVE_DCIN_WD,
  2833. CS35L43_DCIN_WD_THLD_MASK,
  2834. 1 << CS35L43_DCIN_WD_THLD_SHIFT);
  2835. /* ACK core wakeup message before core disabled in dsp_init */
  2836. regmap_write(cs35l43->regmap, CS35L43_IRQ1_EINT_1,
  2837. CS35L43_DSP_VIRTUAL2_MBOX_WR_EINT1_MASK);
  2838. ret = devm_request_threaded_irq(cs35l43->dev, cs35l43->irq, NULL,
  2839. cs35l43_irq, IRQF_ONESHOT | IRQF_SHARED |
  2840. irq_pol, "cs35l43", cs35l43);
  2841. if (ret != 0) {
  2842. dev_err(cs35l43->dev, "Failed to request IRQ: %d\n", ret);
  2843. goto err;
  2844. }
  2845. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE)
  2846. cs35l43_pm_runtime_setup(cs35l43);
  2847. ret = snd_soc_register_component(cs35l43->dev,
  2848. &soc_component_dev_cs35l43,
  2849. cs35l43_dai, ARRAY_SIZE(cs35l43_dai));
  2850. if (ret < 0) {
  2851. dev_err(cs35l43->dev, "%s: Register codec failed\n", __func__);
  2852. goto err_pm;
  2853. }
  2854. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE)
  2855. pm_runtime_put_autosuspend(cs35l43->dev);
  2856. dev_info(cs35l43->dev, "Cirrus Logic cs35l43 (%x), Revision: %02X\n",
  2857. regid, revid);
  2858. return 0;
  2859. err_pm:
  2860. pm_runtime_disable(cs35l43->dev);
  2861. pm_runtime_put_noidle(cs35l43->dev);
  2862. wm_adsp2_remove(&cs35l43->dsp);
  2863. mutex_destroy(&cs35l43->hb_lock);
  2864. err:
  2865. regulator_bulk_disable(cs35l43->num_supplies, cs35l43->supplies);
  2866. return ret;
  2867. }
  2868. int cs35l43_remove(struct cs35l43_private *cs35l43)
  2869. {
  2870. pm_runtime_get_sync(cs35l43->dev);
  2871. pm_runtime_disable(cs35l43->dev);
  2872. regulator_bulk_disable(cs35l43->num_supplies, cs35l43->supplies);
  2873. snd_soc_unregister_component(cs35l43->dev);
  2874. wm_adsp2_remove(&cs35l43->dsp);
  2875. pm_runtime_put_noidle(cs35l43->dev);
  2876. mutex_destroy(&cs35l43->hb_lock);
  2877. return 0;
  2878. }
  2879. int cs35l43_reinit(struct snd_soc_component *component)
  2880. {
  2881. struct cs35l43_private *cs35l43 = snd_soc_component_get_drvdata(component);
  2882. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  2883. struct snd_soc_dapm_widget fake_dapm_widget = {.dapm = dapm};
  2884. if (!cs35l43)
  2885. return 0;
  2886. if (cs35l43->reset_gpio) {
  2887. gpiod_direction_output(cs35l43->reset_gpio, 1);
  2888. gpiod_set_value_cansleep(cs35l43->reset_gpio, 0);
  2889. usleep_range(1000, 1100);
  2890. gpiod_set_value_cansleep(cs35l43->reset_gpio, 1);
  2891. }
  2892. usleep_range(2000, 2100);
  2893. regcache_cache_only(cs35l43->regmap, false);
  2894. cs35l43->hibernate_state = CS35L43_HIBERNATE_NOT_LOADED;
  2895. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE)
  2896. pm_runtime_get_sync(cs35l43->dev);
  2897. cs_dsp_stop(&cs35l43->dsp.cs_dsp);
  2898. if (cs35l43->dsp.preloaded) {
  2899. cs35l43->dsp.preloaded = 0;
  2900. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_PRE_PMD);
  2901. usleep_range(5000, 5100);
  2902. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_PRE_PMU);
  2903. cs35l43_dsp_preload_ev(&fake_dapm_widget, NULL, SND_SOC_DAPM_POST_PMU);
  2904. cs35l43->dsp.preloaded = 1;
  2905. }
  2906. regcache_mark_dirty(cs35l43->regmap);
  2907. regcache_sync_region(cs35l43->regmap, CS35L43_DEVID,
  2908. CS35L43_MIXER_NGATE_CH2_CFG);
  2909. if (cs35l43->low_pwr_mode == CS35L43_LOW_PWR_MODE_HIBERNATE) {
  2910. usleep_range(5000, 5100);
  2911. pm_runtime_put_autosuspend(cs35l43->dev);
  2912. }
  2913. dev_info(cs35l43->dev, "%s complete\n", __func__);
  2914. return 0;
  2915. }
  2916. EXPORT_SYMBOL_GPL(cs35l43_reinit);
  2917. static void cs35l43_pm_runtime_setup(struct cs35l43_private *cs35l43)
  2918. {
  2919. struct device *dev = cs35l43->dev;
  2920. pm_runtime_set_autosuspend_delay(dev, CS35L43_PM_AUTOSUSPEND_DELAY_MS);
  2921. pm_runtime_use_autosuspend(dev);
  2922. pm_runtime_mark_last_busy(dev);
  2923. pm_runtime_set_active(dev);
  2924. pm_runtime_get_noresume(dev);
  2925. pm_runtime_enable(dev);
  2926. }
  2927. int cs35l43_sys_suspend(struct device *dev)
  2928. {
  2929. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  2930. struct i2c_client *i2c_client = to_i2c_client(dev);
  2931. dev_dbg(cs35l43->dev, "System suspend, disabling IRQ\n");
  2932. disable_irq(i2c_client->irq);
  2933. return 0;
  2934. }
  2935. EXPORT_SYMBOL(cs35l43_sys_suspend);
  2936. int cs35l43_sys_suspend_noirq(struct device *dev)
  2937. {
  2938. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  2939. struct i2c_client *i2c_client = to_i2c_client(dev);
  2940. dev_dbg(cs35l43->dev, "Late system suspend, re-enabling IRQ\n");
  2941. enable_irq(i2c_client->irq);
  2942. return 0;
  2943. }
  2944. EXPORT_SYMBOL(cs35l43_sys_suspend_noirq);
  2945. int cs35l43_sys_resume(struct device *dev)
  2946. {
  2947. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  2948. struct i2c_client *i2c_client = to_i2c_client(dev);
  2949. dev_dbg(cs35l43->dev, "System resume, re-enabling IRQ\n");
  2950. enable_irq(i2c_client->irq);
  2951. return 0;
  2952. }
  2953. EXPORT_SYMBOL(cs35l43_sys_resume);
  2954. int cs35l43_sys_resume_noirq(struct device *dev)
  2955. {
  2956. struct cs35l43_private *cs35l43 = dev_get_drvdata(dev);
  2957. struct i2c_client *i2c_client = to_i2c_client(dev);
  2958. dev_dbg(cs35l43->dev, "Early system resume, disabling IRQ\n");
  2959. disable_irq(i2c_client->irq);
  2960. return 0;
  2961. }
  2962. EXPORT_SYMBOL(cs35l43_sys_resume_noirq);
  2963. MODULE_DESCRIPTION("ASoC CS35L43 driver");
  2964. MODULE_AUTHOR("David Rhodes, Cirrus Logic Inc, <[email protected]>");
  2965. MODULE_LICENSE("GPL");