swr-mstr-ctrl.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/irq.h>
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/io.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/kthread.h>
  15. #include <linux/bitops.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/of.h>
  21. #include <soc/soundwire.h>
  22. #include <soc/swr-common.h>
  23. #include <linux/regmap.h>
  24. #include <dsp/msm-audio-event-notify.h>
  25. #include "swr-mstr-registers.h"
  26. #include "swr-slave-registers.h"
  27. #include <dsp/digital-cdc-rsc-mgr.h>
  28. #include "swr-mstr-ctrl.h"
  29. #define SWR_NUM_PORTS 4 /* TODO - Get this info from DT */
  30. #define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
  31. #define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */
  32. #define SWRM_PCM_OUT 0
  33. #define SWRM_PCM_IN 1
  34. #define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
  35. #define SWRM_SYS_SUSPEND_WAIT 1
  36. #define SWRM_DSD_PARAMS_PORT 4
  37. #define SWRM_SPK_DAC_PORT_RECEIVER 0
  38. #define SWR_BROADCAST_CMD_ID 0x0F
  39. #define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
  40. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  41. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  42. #define SWR_INVALID_PARAM 0xFF
  43. #define SWR_HSTOP_MAX_VAL 0xF
  44. #define SWR_HSTART_MIN_VAL 0x0
  45. #define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
  46. #define SWRM_LINK_STATUS_RETRY_CNT 100
  47. #define SWRM_ROW_48 48
  48. #define SWRM_ROW_50 50
  49. #define SWRM_ROW_64 64
  50. #define SWRM_COL_02 02
  51. #define SWRM_COL_16 16
  52. #define SWRS_SCP_INT_STATUS_CLEAR_1 0x40
  53. #define SWRS_SCP_INT_STATUS_MASK_1 0x41
  54. #define SWRM_MCP_SLV_STATUS_MASK 0x03
  55. #define SWRM_ROW_CTRL_MASK 0xF8
  56. #define SWRM_COL_CTRL_MASK 0x07
  57. #define SWRM_CLK_DIV_MASK 0x700
  58. #define SWRM_SSP_PERIOD_MASK 0xff0000
  59. #define SWRM_NUM_PINGS_MASK 0x3E0000
  60. #define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT 3
  61. #define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT 0
  62. #define SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT 8
  63. #define SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT 16
  64. #define SWRM_NUM_PINGS_POS 0x11
  65. #define SWRM_DP_PORT_CTRL_EN_CHAN_SHFT 0x18
  66. #define SWRM_DP_PORT_CTRL_OFFSET2_SHFT 0x10
  67. #define SWRM_DP_PORT_CTRL_OFFSET1_SHFT 0x08
  68. #define SWR_OVERFLOW_RETRY_COUNT 30
  69. #define CPU_IDLE_LATENCY 10
  70. #define SWRM_REG_GAP_START 0x2C54
  71. #define SWRM_REG_GAP_END 0x4000
  72. /* pm runtime auto suspend timer in msecs */
  73. static int auto_suspend_timer = 500;
  74. module_param(auto_suspend_timer, int, 0664);
  75. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  76. enum {
  77. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  78. SWR_ATTACHED_OK, /* Device is attached */
  79. SWR_ALERT, /* Device alters master for any interrupts */
  80. SWR_RESERVED, /* Reserved */
  81. };
  82. enum {
  83. MASTER_ID_WSA = 1,
  84. MASTER_ID_RX,
  85. MASTER_ID_TX
  86. };
  87. enum {
  88. ENABLE_PENDING,
  89. DISABLE_PENDING
  90. };
  91. enum {
  92. LPASS_HW_CORE,
  93. LPASS_AUDIO_CORE,
  94. };
  95. enum {
  96. SWRM_WR_CHECK_AVAIL,
  97. SWRM_RD_CHECK_AVAIL,
  98. };
  99. #define TRUE 1
  100. #define FALSE 0
  101. #define SWRM_MAX_PORT_REG 120
  102. #define SWRM_MAX_INIT_REG 12
  103. #define MAX_FIFO_RD_FAIL_RETRY 3
  104. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
  105. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
  106. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
  107. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
  108. static int swrm_runtime_resume(struct device *dev);
  109. static void swrm_wait_for_fifo_avail(struct swr_mstr_ctrl *swrm, int swrm_rd_wr);
  110. static u8 swrm_get_clk_div(int mclk_freq, int bus_clk_freq)
  111. {
  112. int clk_div = 0;
  113. u8 div_val = 0;
  114. if (!mclk_freq || !bus_clk_freq)
  115. return 0;
  116. clk_div = (mclk_freq / bus_clk_freq);
  117. switch (clk_div) {
  118. case 32:
  119. div_val = 5;
  120. break;
  121. case 16:
  122. div_val = 4;
  123. break;
  124. case 8:
  125. div_val = 3;
  126. break;
  127. case 4:
  128. div_val = 2;
  129. break;
  130. case 2:
  131. div_val = 1;
  132. break;
  133. case 1:
  134. default:
  135. div_val = 0;
  136. break;
  137. }
  138. return div_val;
  139. }
  140. static bool swrm_is_msm_variant(int val)
  141. {
  142. return (val == SWRM_VERSION_1_3);
  143. }
  144. static u8 get_cmd_id(struct swr_mstr_ctrl *swrm)
  145. {
  146. u8 id;
  147. id = swrm->cmd_id;
  148. swrm->cmd_id = (swrm->cmd_id == 0xE) ? 0 : ((swrm->cmd_id + 1) % 16);
  149. return id;
  150. }
  151. #ifdef CONFIG_DEBUG_FS
  152. static int swrm_debug_open(struct inode *inode, struct file *file)
  153. {
  154. file->private_data = inode->i_private;
  155. return 0;
  156. }
  157. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  158. {
  159. char *token;
  160. int base, cnt;
  161. token = strsep(&buf, " ");
  162. for (cnt = 0; cnt < num_of_par; cnt++) {
  163. if (token) {
  164. if ((token[1] == 'x') || (token[1] == 'X'))
  165. base = 16;
  166. else
  167. base = 10;
  168. if (kstrtou32(token, base, &param1[cnt]) != 0)
  169. return -EINVAL;
  170. token = strsep(&buf, " ");
  171. } else
  172. return -EINVAL;
  173. }
  174. return 0;
  175. }
  176. static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
  177. size_t count, loff_t *ppos)
  178. {
  179. int i, reg_val, len;
  180. ssize_t total = 0;
  181. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  182. if (!ubuf || !ppos)
  183. return 0;
  184. i = ((int) *ppos + SWRM_BASE);
  185. for (; i <= SWRM_MAX_REGISTER; i += 4) {
  186. /* No registers between SWRM_REG_GAP_START to SWRM_REG_GAP_END */
  187. if (i > SWRM_REG_GAP_START && i < SWRM_REG_GAP_END)
  188. continue;
  189. usleep_range(100, 150);
  190. reg_val = swr_master_read(swrm, i);
  191. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  192. if (len < 0) {
  193. pr_err_ratelimited("%s: fail to fill the buffer\n", __func__);
  194. total = -EFAULT;
  195. goto copy_err;
  196. }
  197. if ((total + len) >= count - 1)
  198. break;
  199. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  200. pr_err_ratelimited("%s: fail to copy reg dump\n", __func__);
  201. total = -EFAULT;
  202. goto copy_err;
  203. }
  204. *ppos += 4;
  205. total += len;
  206. }
  207. copy_err:
  208. return total;
  209. }
  210. static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
  211. size_t count, loff_t *ppos)
  212. {
  213. struct swr_mstr_ctrl *swrm;
  214. if (!count || !file || !ppos || !ubuf)
  215. return -EINVAL;
  216. swrm = file->private_data;
  217. if (!swrm)
  218. return -EINVAL;
  219. if (*ppos < 0)
  220. return -EINVAL;
  221. return swrm_reg_show(swrm, ubuf, count, ppos);
  222. }
  223. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  224. size_t count, loff_t *ppos)
  225. {
  226. char lbuf[SWR_MSTR_RD_BUF_LEN];
  227. struct swr_mstr_ctrl *swrm = NULL;
  228. if (!count || !file || !ppos || !ubuf)
  229. return -EINVAL;
  230. swrm = file->private_data;
  231. if (!swrm)
  232. return -EINVAL;
  233. if (*ppos < 0)
  234. return -EINVAL;
  235. snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
  236. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  237. strnlen(lbuf, 7));
  238. }
  239. static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
  240. size_t count, loff_t *ppos)
  241. {
  242. char lbuf[SWR_MSTR_RD_BUF_LEN];
  243. int rc;
  244. u32 param[5];
  245. struct swr_mstr_ctrl *swrm = NULL;
  246. if (!count || !file || !ppos || !ubuf)
  247. return -EINVAL;
  248. swrm = file->private_data;
  249. if (!swrm)
  250. return -EINVAL;
  251. if (*ppos < 0)
  252. return -EINVAL;
  253. if (count > sizeof(lbuf) - 1)
  254. return -EINVAL;
  255. rc = copy_from_user(lbuf, ubuf, count);
  256. if (rc)
  257. return -EFAULT;
  258. lbuf[count] = '\0';
  259. rc = get_parameters(lbuf, param, 1);
  260. if ((param[0] <= SWRM_MAX_REGISTER) && (rc == 0) && (param[0] % 4 == 0))
  261. swrm->read_data = swr_master_read(swrm, param[0]);
  262. else
  263. rc = -EINVAL;
  264. if (rc == 0)
  265. rc = count;
  266. else
  267. dev_err_ratelimited(swrm->dev, "%s: rc = %d\n", __func__, rc);
  268. return rc;
  269. }
  270. static ssize_t swrm_debug_write(struct file *file,
  271. const char __user *ubuf, size_t count, loff_t *ppos)
  272. {
  273. char lbuf[SWR_MSTR_WR_BUF_LEN];
  274. int rc;
  275. u32 param[5];
  276. struct swr_mstr_ctrl *swrm;
  277. if (!file || !ppos || !ubuf)
  278. return -EINVAL;
  279. swrm = file->private_data;
  280. if (!swrm)
  281. return -EINVAL;
  282. if (count > sizeof(lbuf) - 1)
  283. return -EINVAL;
  284. rc = copy_from_user(lbuf, ubuf, count);
  285. if (rc)
  286. return -EFAULT;
  287. lbuf[count] = '\0';
  288. rc = get_parameters(lbuf, param, 2);
  289. if ((param[0] <= SWRM_MAX_REGISTER) &&
  290. (param[1] <= 0xFFFFFFFF) &&
  291. (rc == 0) && (param[0] % 4 == 0))
  292. swr_master_write(swrm, param[0], param[1]);
  293. else
  294. rc = -EINVAL;
  295. if (rc == 0)
  296. rc = count;
  297. else
  298. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  299. return rc;
  300. }
  301. static const struct file_operations swrm_debug_read_ops = {
  302. .open = swrm_debug_open,
  303. .write = swrm_debug_peek_write,
  304. .read = swrm_debug_read,
  305. };
  306. static const struct file_operations swrm_debug_write_ops = {
  307. .open = swrm_debug_open,
  308. .write = swrm_debug_write,
  309. };
  310. static const struct file_operations swrm_debug_dump_ops = {
  311. .open = swrm_debug_open,
  312. .read = swrm_debug_reg_dump,
  313. };
  314. #endif
  315. static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
  316. u32 *reg, u32 *val, int len, const char* func)
  317. {
  318. int i = 0;
  319. for (i = 0; i < len; i++)
  320. dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
  321. func, reg[i], val[i]);
  322. }
  323. static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
  324. {
  325. return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
  326. }
  327. static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
  328. int core_type, bool enable)
  329. {
  330. int ret = 0;
  331. mutex_lock(&swrm->devlock);
  332. if (core_type == LPASS_HW_CORE) {
  333. if (swrm->lpass_core_hw_vote) {
  334. if (enable) {
  335. if (!swrm->dev_up) {
  336. dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
  337. __func__);
  338. mutex_unlock(&swrm->devlock);
  339. return -ENODEV;
  340. }
  341. if (++swrm->hw_core_clk_en == 1) {
  342. ret =
  343. digital_cdc_rsc_mgr_hw_vote_enable(
  344. swrm->lpass_core_hw_vote, swrm->dev);
  345. if (ret < 0) {
  346. dev_err_ratelimited(swrm->dev,
  347. "%s:lpass core hw enable failed\n",
  348. __func__);
  349. --swrm->hw_core_clk_en;
  350. }
  351. }
  352. } else {
  353. --swrm->hw_core_clk_en;
  354. if (swrm->hw_core_clk_en < 0)
  355. swrm->hw_core_clk_en = 0;
  356. else if (swrm->hw_core_clk_en == 0)
  357. digital_cdc_rsc_mgr_hw_vote_disable(
  358. swrm->lpass_core_hw_vote, swrm->dev);
  359. }
  360. }
  361. }
  362. if (core_type == LPASS_AUDIO_CORE) {
  363. if (swrm->lpass_core_audio) {
  364. if (enable) {
  365. if (!swrm->dev_up) {
  366. dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
  367. __func__);
  368. mutex_unlock(&swrm->devlock);
  369. return -ENODEV;
  370. }
  371. if (++swrm->aud_core_clk_en == 1) {
  372. ret =
  373. digital_cdc_rsc_mgr_hw_vote_enable(
  374. swrm->lpass_core_audio, swrm->dev);
  375. if (ret < 0) {
  376. dev_err_ratelimited(swrm->dev,
  377. "%s:lpass audio hw enable failed\n",
  378. __func__);
  379. --swrm->aud_core_clk_en;
  380. }
  381. }
  382. } else {
  383. --swrm->aud_core_clk_en;
  384. if (swrm->aud_core_clk_en < 0)
  385. swrm->aud_core_clk_en = 0;
  386. else if (swrm->aud_core_clk_en == 0)
  387. digital_cdc_rsc_mgr_hw_vote_disable(
  388. swrm->lpass_core_audio, swrm->dev);
  389. }
  390. }
  391. }
  392. mutex_unlock(&swrm->devlock);
  393. dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
  394. __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
  395. return ret;
  396. }
  397. static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
  398. int row, int col,
  399. int frame_sync)
  400. {
  401. if (!swrm || !row || !col || !frame_sync)
  402. return 1;
  403. return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
  404. }
  405. static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm, bool enable)
  406. {
  407. int ret = 0;
  408. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  409. if (!swrm->handle)
  410. return -EINVAL;
  411. mutex_lock(&swrm->clklock);
  412. if (!swrm->dev_up) {
  413. ret = -ENODEV;
  414. goto exit;
  415. }
  416. if (swrm->core_vote) {
  417. ret = swrm->core_vote(swrm->handle, enable);
  418. if (ret)
  419. if (__ratelimit(&rtl))
  420. dev_err_ratelimited(swrm->dev,
  421. "%s: core vote request failed\n", __func__);
  422. }
  423. exit:
  424. mutex_unlock(&swrm->clklock);
  425. return ret;
  426. }
  427. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  428. {
  429. int ret = 0;
  430. if (!swrm->clk || !swrm->handle)
  431. return -EINVAL;
  432. mutex_lock(&swrm->clklock);
  433. if (enable) {
  434. if (!swrm->dev_up) {
  435. ret = -ENODEV;
  436. goto exit;
  437. }
  438. if (is_swr_clk_needed(swrm)) {
  439. if (swrm->core_vote) {
  440. ret = swrm->core_vote(swrm->handle, true);
  441. if (ret) {
  442. dev_err_ratelimited(swrm->dev,
  443. "%s: core vote request failed\n",
  444. __func__);
  445. swrm->core_vote(swrm->handle, false);
  446. goto exit;
  447. }
  448. ret = swrm->core_vote(swrm->handle, false);
  449. }
  450. }
  451. swrm->clk_ref_count++;
  452. if (swrm->clk_ref_count == 1) {
  453. ret = swrm->clk(swrm->handle, true);
  454. if (ret) {
  455. dev_err_ratelimited(swrm->dev,
  456. "%s: clock enable req failed",
  457. __func__);
  458. --swrm->clk_ref_count;
  459. }
  460. }
  461. } else if (--swrm->clk_ref_count == 0) {
  462. swrm->clk(swrm->handle, false);
  463. complete(&swrm->clk_off_complete);
  464. }
  465. if (swrm->clk_ref_count < 0) {
  466. dev_err_ratelimited(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
  467. swrm->clk_ref_count = 0;
  468. }
  469. exit:
  470. mutex_unlock(&swrm->clklock);
  471. return ret;
  472. }
  473. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  474. u16 reg, u32 *value)
  475. {
  476. u32 temp = (u32)(*value);
  477. int ret = 0;
  478. int vote_ret = 0;
  479. mutex_lock(&swrm->devlock);
  480. if (!swrm->dev_up)
  481. goto err;
  482. if (is_swr_clk_needed(swrm)) {
  483. ret = swrm_clk_request(swrm, TRUE);
  484. if (ret) {
  485. dev_err_ratelimited(swrm->dev,
  486. "%s: clock request failed\n",
  487. __func__);
  488. goto err;
  489. }
  490. } else {
  491. vote_ret = swrm_core_vote_request(swrm, true);
  492. if (vote_ret == -ENOTSYNC)
  493. goto err_vote;
  494. else if (vote_ret)
  495. goto err;
  496. }
  497. iowrite32(temp, swrm->swrm_dig_base + reg);
  498. if (is_swr_clk_needed(swrm))
  499. swrm_clk_request(swrm, FALSE);
  500. err_vote:
  501. if (!is_swr_clk_needed(swrm))
  502. swrm_core_vote_request(swrm, false);
  503. err:
  504. mutex_unlock(&swrm->devlock);
  505. return ret;
  506. }
  507. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  508. u16 reg, u32 *value)
  509. {
  510. u32 temp = 0;
  511. int ret = 0;
  512. int vote_ret = 0;
  513. mutex_lock(&swrm->devlock);
  514. if (!swrm->dev_up)
  515. goto err;
  516. if (is_swr_clk_needed(swrm)) {
  517. ret = swrm_clk_request(swrm, TRUE);
  518. if (ret) {
  519. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  520. __func__);
  521. goto err;
  522. }
  523. } else {
  524. vote_ret = swrm_core_vote_request(swrm, true);
  525. if (vote_ret == -ENOTSYNC)
  526. goto err_vote;
  527. else if (vote_ret)
  528. goto err;
  529. }
  530. temp = ioread32(swrm->swrm_dig_base + reg);
  531. *value = temp;
  532. if (is_swr_clk_needed(swrm))
  533. swrm_clk_request(swrm, FALSE);
  534. err_vote:
  535. if (!is_swr_clk_needed(swrm))
  536. swrm_core_vote_request(swrm, false);
  537. err:
  538. mutex_unlock(&swrm->devlock);
  539. return ret;
  540. }
  541. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  542. {
  543. u32 val = 0;
  544. if (swrm->read)
  545. val = swrm->read(swrm->handle, reg_addr);
  546. else
  547. swrm_ahb_read(swrm, reg_addr, &val);
  548. return val;
  549. }
  550. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  551. {
  552. if (swrm->write)
  553. swrm->write(swrm->handle, reg_addr, val);
  554. else
  555. swrm_ahb_write(swrm, reg_addr, &val);
  556. }
  557. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  558. u32 *val, unsigned int length)
  559. {
  560. int i = 0;
  561. if (swrm->bulk_write)
  562. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  563. else {
  564. mutex_lock(&swrm->iolock);
  565. for (i = 0; i < length; i++) {
  566. /* wait for FIFO WR command to complete to avoid overflow */
  567. /*
  568. * Reduce sleep from 100us to 50us to meet KPIs
  569. * This still meets the hardware spec
  570. */
  571. usleep_range(50, 55);
  572. if (reg_addr[i] == SWRM_CMD_FIFO_WR_CMD(swrm->ee_val))
  573. swrm_wait_for_fifo_avail(swrm,
  574. SWRM_WR_CHECK_AVAIL);
  575. swr_master_write(swrm, reg_addr[i], val[i]);
  576. }
  577. usleep_range(100, 110);
  578. mutex_unlock(&swrm->iolock);
  579. }
  580. return 0;
  581. }
  582. static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
  583. {
  584. int retry = SWRM_LINK_STATUS_RETRY_CNT;
  585. int ret = false;
  586. int status = active ? 0x1 : 0x0;
  587. int comp_sts = 0x0;
  588. if ((swrm->version <= SWRM_VERSION_1_5_1))
  589. return true;
  590. do {
  591. #ifdef CONFIG_SWRM_VER_2P0
  592. comp_sts = swr_master_read(swrm, SWRM_LINK_STATUS(swrm->ee_val)) & 0x01;
  593. #else
  594. comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
  595. #endif
  596. /* check comp status and status requested met */
  597. if ((comp_sts && status) || (!comp_sts && !status)) {
  598. ret = true;
  599. break;
  600. }
  601. retry--;
  602. usleep_range(500, 510);
  603. } while (retry);
  604. if (retry == 0)
  605. dev_err_ratelimited(swrm->dev, "%s: link status not %s\n", __func__,
  606. active ? "connected" : "disconnected");
  607. return ret;
  608. }
  609. static bool swrm_is_port_en(struct swr_master *mstr)
  610. {
  611. return !!(mstr->num_port);
  612. }
  613. static void copy_port_tables(struct swr_mstr_ctrl *swrm,
  614. struct port_params *params)
  615. {
  616. u8 i;
  617. struct port_params *config = params;
  618. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  619. /* wsa uses single frame structure for all configurations */
  620. if (!swrm->mport_cfg[i].port_en)
  621. continue;
  622. swrm->mport_cfg[i].sinterval = config[i].si;
  623. swrm->mport_cfg[i].offset1 = config[i].off1;
  624. swrm->mport_cfg[i].offset2 = config[i].off2;
  625. swrm->mport_cfg[i].hstart = config[i].hstart;
  626. swrm->mport_cfg[i].hstop = config[i].hstop;
  627. swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
  628. swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
  629. swrm->mport_cfg[i].word_length = config[i].wd_len;
  630. swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
  631. swrm->mport_cfg[i].dir = config[i].dir;
  632. swrm->mport_cfg[i].stream_type = config[i].stream_type;
  633. }
  634. }
  635. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  636. {
  637. struct port_params *params;
  638. u32 usecase = 0;
  639. if (swrm->master_id == MASTER_ID_TX)
  640. return 0;
  641. /* TODO - Send usecase information to avoid checking for master_id */
  642. if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
  643. (swrm->master_id == MASTER_ID_RX))
  644. usecase = 1;
  645. else if ((swrm->master_id == MASTER_ID_RX) &&
  646. (swrm->bus_clk == SWR_CLK_RATE_11P2896MHZ))
  647. usecase = 2;
  648. if ((swrm->master_id == MASTER_ID_WSA) &&
  649. swrm->mport_cfg[SWRM_SPK_DAC_PORT_RECEIVER].port_en &&
  650. swrm->mport_cfg[SWRM_SPK_DAC_PORT_RECEIVER].ch_rate ==
  651. SWR_CLK_RATE_4P8MHZ)
  652. usecase = 1;
  653. params = swrm->port_param[usecase];
  654. copy_port_tables(swrm, params);
  655. return 0;
  656. }
  657. static int swrm_pcm_port_config(struct swr_mstr_ctrl *swrm, u8 port_num,
  658. u8 stream_type, bool dir, bool enable)
  659. {
  660. u16 reg_addr = 0;
  661. u32 reg_val = 0;
  662. if (!port_num || port_num > SWR_MSTR_PORT_LEN) {
  663. dev_err_ratelimited(swrm->dev, "%s: invalid port: %d\n",
  664. __func__, port_num);
  665. return -EINVAL;
  666. }
  667. switch (stream_type) {
  668. case SWR_PCM:
  669. case SWR_PDM_32:
  670. if (swrm->version != SWRM_VERSION_1_7) {
  671. if (dir)
  672. reg_addr = SWRM_DIN_DP_PCM_PORT_CTRL(port_num);
  673. else
  674. reg_addr = SWRM_DOUT_DP_PCM_PORT_CTRL(port_num);
  675. reg_val = enable ? 0x3 : 0x0;
  676. swr_master_write(swrm, reg_addr, reg_val);
  677. } else if (stream_type == SWR_PCM) {
  678. if (dir)
  679. reg_addr = SWRM_DIN_DP_PCM_PORT_CTRL(port_num);
  680. else
  681. reg_addr = SWRM_DOUT_DP_PCM_PORT_CTRL(port_num);
  682. swr_master_write(swrm, reg_addr, enable);
  683. }
  684. break;
  685. case SWR_PDM:
  686. default:
  687. return 0;
  688. }
  689. if (swrm->version == SWRM_VERSION_1_7) {
  690. reg_val = SWRM_COMP_FEATURE_CFG_DEFAULT_VAL_V1P7;
  691. if (enable) {
  692. if (swrm->pcm_enable_count == 0) {
  693. reg_val |= SWRM_COMP_FEATURE_CFG_PCM_EN_MASK;
  694. swr_master_write(swrm, SWRM_COMP_FEATURE_CFG, reg_val);
  695. }
  696. swrm->pcm_enable_count++;
  697. } else {
  698. if (swrm->pcm_enable_count > 0)
  699. swrm->pcm_enable_count--;
  700. if (swrm->pcm_enable_count == 0)
  701. swr_master_write(swrm, SWRM_COMP_FEATURE_CFG, reg_val);
  702. }
  703. }
  704. return 0;
  705. }
  706. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  707. u8 *mstr_ch_mask, u8 mstr_prt_type,
  708. u8 slv_port_id)
  709. {
  710. int i, j;
  711. *mstr_port_id = 0;
  712. for (i = 1; i <= swrm->num_ports; i++) {
  713. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  714. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  715. goto found;
  716. }
  717. }
  718. found:
  719. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  720. dev_err_ratelimited(swrm->dev, "%s: port type not supported by master\n",
  721. __func__);
  722. return -EINVAL;
  723. }
  724. /* id 0 corresponds to master port 1 */
  725. *mstr_port_id = i - 1;
  726. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  727. return 0;
  728. }
  729. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  730. u8 dev_addr, u16 reg_addr)
  731. {
  732. u32 val;
  733. u8 id = *cmd_id;
  734. if (id != SWR_BROADCAST_CMD_ID) {
  735. if (id < 14)
  736. id += 1;
  737. else
  738. id = 0;
  739. *cmd_id = id;
  740. }
  741. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  742. return val;
  743. }
  744. static void swrm_wait_for_fifo_avail(struct swr_mstr_ctrl *swrm, int swrm_rd_wr)
  745. {
  746. u32 fifo_outstanding_cmd;
  747. u32 fifo_retry_count = SWR_OVERFLOW_RETRY_COUNT;
  748. if (swrm_rd_wr) {
  749. /* Check for fifo underflow during read */
  750. /* Check no of outstanding commands in fifo before read */
  751. fifo_outstanding_cmd = ((swr_master_read(swrm,
  752. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x001F0000) >> 16);
  753. if (fifo_outstanding_cmd == 0) {
  754. while (fifo_retry_count) {
  755. usleep_range(500, 510);
  756. fifo_outstanding_cmd =
  757. ((swr_master_read (swrm,
  758. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x001F0000)
  759. >> 16);
  760. fifo_retry_count--;
  761. if (fifo_outstanding_cmd > 0)
  762. break;
  763. }
  764. }
  765. if (fifo_outstanding_cmd == 0)
  766. dev_err_ratelimited(swrm->dev,
  767. "%s err read underflow\n", __func__);
  768. } else {
  769. /* Check for fifo overflow during write */
  770. /* Check no of outstanding commands in fifo before write */
  771. fifo_outstanding_cmd = ((swr_master_read(swrm,
  772. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x00001F00)
  773. >> 8);
  774. if (fifo_outstanding_cmd == swrm->wr_fifo_depth) {
  775. while (fifo_retry_count) {
  776. usleep_range(500, 510);
  777. fifo_outstanding_cmd =
  778. ((swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val))
  779. & 0x00001F00) >> 8);
  780. fifo_retry_count--;
  781. if (fifo_outstanding_cmd < swrm->wr_fifo_depth)
  782. break;
  783. }
  784. }
  785. if (fifo_outstanding_cmd == swrm->wr_fifo_depth)
  786. dev_err_ratelimited(swrm->dev,
  787. "%s err write overflow\n", __func__);
  788. }
  789. }
  790. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  791. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  792. u32 len)
  793. {
  794. u32 val;
  795. u32 retry_attempt = 0;
  796. mutex_lock(&swrm->iolock);
  797. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  798. if (swrm->read) {
  799. /* skip delay if read is handled in platform driver */
  800. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD(swrm->ee_val), val);
  801. } else {
  802. /*
  803. * Check for outstanding cmd wrt. write fifo depth to avoid
  804. * overflow as read will also increase write fifo cnt.
  805. */
  806. swrm_wait_for_fifo_avail(swrm, SWRM_WR_CHECK_AVAIL);
  807. /* wait for FIFO RD to complete to avoid overflow */
  808. usleep_range(100, 105);
  809. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD(swrm->ee_val), val);
  810. /* wait for FIFO RD CMD complete to avoid overflow */
  811. usleep_range(250, 255);
  812. }
  813. /* Check if slave responds properly after FIFO RD is complete */
  814. swrm_wait_for_fifo_avail(swrm, SWRM_RD_CHECK_AVAIL);
  815. retry_read:
  816. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO(swrm->ee_val));
  817. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
  818. dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
  819. cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
  820. if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
  821. if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
  822. /* wait 500 us before retry on fifo read failure */
  823. usleep_range(500, 505);
  824. if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
  825. swr_master_write(swrm,
  826. SWRM_CMD_FIFO_RD_CMD(swrm->ee_val),
  827. val);
  828. }
  829. retry_attempt++;
  830. goto retry_read;
  831. } else {
  832. dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
  833. rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  834. __func__, reg_addr, cmd_id, swrm->rcmd_id,
  835. dev_addr, *cmd_data);
  836. dev_err_ratelimited(swrm->dev,
  837. "%s: failed to read fifo\n", __func__);
  838. }
  839. }
  840. mutex_unlock(&swrm->iolock);
  841. return 0;
  842. }
  843. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  844. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  845. {
  846. u32 val;
  847. int ret = 0;
  848. mutex_lock(&swrm->iolock);
  849. if (!cmd_id)
  850. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  851. dev_addr, reg_addr);
  852. else
  853. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  854. dev_addr, reg_addr);
  855. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
  856. dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
  857. reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
  858. /*
  859. * Check for outstanding cmd wrt. write fifo depth to avoid
  860. * overflow.
  861. */
  862. swrm_wait_for_fifo_avail(swrm, SWRM_WR_CHECK_AVAIL);
  863. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD(swrm->ee_val), val);
  864. /*
  865. * wait for FIFO WR command to complete to avoid overflow
  866. * skip delay if write is handled in platform driver.
  867. */
  868. if(!swrm->write)
  869. usleep_range(150, 155);
  870. if (cmd_id == 0xF) {
  871. /*
  872. * sleep for 10ms for MSM soundwire variant to allow broadcast
  873. * command to complete.
  874. */
  875. if (swrm_is_msm_variant(swrm->version))
  876. usleep_range(10000, 10100);
  877. else
  878. wait_for_completion_timeout(&swrm->broadcast,
  879. (2 * HZ/10));
  880. }
  881. mutex_unlock(&swrm->iolock);
  882. return ret;
  883. }
  884. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  885. void *buf, u32 len)
  886. {
  887. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  888. int ret = 0;
  889. int val;
  890. u8 *reg_val = (u8 *)buf;
  891. if (!swrm) {
  892. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  893. return -EINVAL;
  894. }
  895. if (!dev_num) {
  896. dev_err_ratelimited(&master->dev, "%s: invalid slave dev num\n", __func__);
  897. return -EINVAL;
  898. }
  899. mutex_lock(&swrm->devlock);
  900. if (!swrm->dev_up) {
  901. mutex_unlock(&swrm->devlock);
  902. return 0;
  903. }
  904. mutex_unlock(&swrm->devlock);
  905. pm_runtime_get_sync(swrm->dev);
  906. if (swrm->req_clk_switch)
  907. swrm_runtime_resume(swrm->dev);
  908. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num,
  909. get_cmd_id(swrm), reg_addr, len);
  910. if (!ret)
  911. *reg_val = (u8)val;
  912. pm_runtime_put_autosuspend(swrm->dev);
  913. pm_runtime_mark_last_busy(swrm->dev);
  914. return ret;
  915. }
  916. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  917. const void *buf)
  918. {
  919. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  920. int ret = 0;
  921. u8 reg_val = *(u8 *)buf;
  922. if (!swrm) {
  923. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  924. return -EINVAL;
  925. }
  926. if (!dev_num) {
  927. dev_err_ratelimited(&master->dev, "%s: invalid slave dev num\n", __func__);
  928. return -EINVAL;
  929. }
  930. mutex_lock(&swrm->devlock);
  931. if (!swrm->dev_up) {
  932. mutex_unlock(&swrm->devlock);
  933. return 0;
  934. }
  935. mutex_unlock(&swrm->devlock);
  936. pm_runtime_get_sync(swrm->dev);
  937. if (swrm->req_clk_switch)
  938. swrm_runtime_resume(swrm->dev);
  939. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num,
  940. get_cmd_id(swrm), reg_addr);
  941. pm_runtime_put_autosuspend(swrm->dev);
  942. pm_runtime_mark_last_busy(swrm->dev);
  943. return ret;
  944. }
  945. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  946. const void *buf, size_t len)
  947. {
  948. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  949. int ret = 0;
  950. int i;
  951. u32 *val;
  952. u32 *swr_fifo_reg;
  953. if (!swrm || !swrm->handle) {
  954. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  955. return -EINVAL;
  956. }
  957. if (len <= 0)
  958. return -EINVAL;
  959. mutex_lock(&swrm->devlock);
  960. if (!swrm->dev_up) {
  961. mutex_unlock(&swrm->devlock);
  962. return 0;
  963. }
  964. mutex_unlock(&swrm->devlock);
  965. pm_runtime_get_sync(swrm->dev);
  966. if (dev_num) {
  967. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  968. if (!swr_fifo_reg) {
  969. ret = -ENOMEM;
  970. goto err;
  971. }
  972. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  973. if (!val) {
  974. ret = -ENOMEM;
  975. goto mem_fail;
  976. }
  977. for (i = 0; i < len; i++) {
  978. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  979. ((u8 *)buf)[i],
  980. dev_num,
  981. ((u16 *)reg)[i]);
  982. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  983. }
  984. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  985. if (ret) {
  986. dev_err_ratelimited(&master->dev, "%s: bulk write failed\n",
  987. __func__);
  988. ret = -EINVAL;
  989. }
  990. } else {
  991. dev_err_ratelimited(&master->dev,
  992. "%s: No support of Bulk write for master regs\n",
  993. __func__);
  994. ret = -EINVAL;
  995. goto err;
  996. }
  997. kfree(val);
  998. mem_fail:
  999. kfree(swr_fifo_reg);
  1000. err:
  1001. pm_runtime_put_autosuspend(swrm->dev);
  1002. pm_runtime_mark_last_busy(swrm->dev);
  1003. return ret;
  1004. }
  1005. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  1006. {
  1007. return (swr_master_read(swrm, SWRM_MCP_STATUS) & 0x01) ? 0 : 1;
  1008. }
  1009. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  1010. u8 row, u8 col)
  1011. {
  1012. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  1013. SWRS_SCP_FRAME_CTRL_BANK(bank));
  1014. }
  1015. static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
  1016. {
  1017. u8 bank;
  1018. u32 n_row, n_col;
  1019. u32 value = 0;
  1020. u32 row = 0, col = 0;
  1021. u8 ssp_period = 0;
  1022. int frame_sync = SWRM_FRAME_SYNC_SEL;
  1023. if (mclk_freq == MCLK_FREQ_NATIVE) {
  1024. n_col = SWR_MAX_COL;
  1025. col = SWRM_COL_16;
  1026. n_row = SWR_ROW_64;
  1027. row = SWRM_ROW_64;
  1028. frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
  1029. } else {
  1030. n_col = SWR_MIN_COL;
  1031. col = SWRM_COL_02;
  1032. n_row = SWR_ROW_50;
  1033. row = SWRM_ROW_50;
  1034. frame_sync = SWRM_FRAME_SYNC_SEL;
  1035. }
  1036. bank = get_inactive_bank_num(swrm);
  1037. ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
  1038. dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
  1039. value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1040. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1041. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1042. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1043. enable_bank_switch(swrm, bank, n_row, n_col);
  1044. }
  1045. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  1046. u8 slv_port, u8 dev_num)
  1047. {
  1048. struct swr_port_info *port_req = NULL;
  1049. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1050. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  1051. if ((port_req->slave_port_id == slv_port)
  1052. && (port_req->dev_num == dev_num))
  1053. return port_req;
  1054. }
  1055. return NULL;
  1056. }
  1057. static bool swrm_remove_from_group(struct swr_master *master)
  1058. {
  1059. struct swr_device *swr_dev;
  1060. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1061. bool is_removed = false;
  1062. if (!swrm)
  1063. goto end;
  1064. mutex_lock(&swrm->mlock);
  1065. if (swrm->num_rx_chs > 1) {
  1066. list_for_each_entry(swr_dev, &master->devices,
  1067. dev_list) {
  1068. swr_dev->group_id = SWR_GROUP_NONE;
  1069. master->gr_sid = 0;
  1070. }
  1071. is_removed = true;
  1072. }
  1073. mutex_unlock(&swrm->mlock);
  1074. end:
  1075. return is_removed;
  1076. }
  1077. int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq)
  1078. {
  1079. if (!bus_clk_freq)
  1080. return mclk_freq;
  1081. if (mclk_freq == SWR_CLK_RATE_9P6MHZ) {
  1082. if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ)
  1083. bus_clk_freq = SWR_CLK_RATE_0P6MHZ;
  1084. else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ)
  1085. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1086. else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ)
  1087. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1088. else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ)
  1089. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1090. else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ)
  1091. bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
  1092. else
  1093. bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
  1094. } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ)
  1095. bus_clk_freq = SWR_CLK_RATE_11P2896MHZ;
  1096. return bus_clk_freq;
  1097. }
  1098. static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm)
  1099. {
  1100. int ret = 0;
  1101. int agg_clk = 0;
  1102. int i;
  1103. for (i = 0; i < SWR_MSTR_PORT_LEN; i++)
  1104. agg_clk += swrm->mport_cfg[i].ch_rate;
  1105. if (agg_clk)
  1106. swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq,
  1107. agg_clk);
  1108. else
  1109. swrm->bus_clk = swrm->mclk_freq;
  1110. dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n",
  1111. __func__, agg_clk, swrm->bus_clk);
  1112. return ret;
  1113. }
  1114. static void swrm_disable_ports(struct swr_master *master,
  1115. u8 bank)
  1116. {
  1117. u32 value;
  1118. struct swr_port_info *port_req;
  1119. int i;
  1120. struct swrm_mports *mport;
  1121. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1122. if (!swrm) {
  1123. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1124. return;
  1125. }
  1126. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1127. master->num_port);
  1128. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  1129. mport = &(swrm->mport_cfg[i]);
  1130. if (!mport->port_en)
  1131. continue;
  1132. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1133. /* skip ports with no change req's*/
  1134. if (port_req->req_ch == port_req->ch_en)
  1135. continue;
  1136. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  1137. port_req->dev_num, get_cmd_id(swrm),
  1138. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  1139. bank));
  1140. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  1141. __func__, i,
  1142. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)));
  1143. }
  1144. value = ((mport->req_ch)
  1145. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  1146. value |= ((mport->offset2)
  1147. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  1148. value |= ((mport->offset1)
  1149. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  1150. value |= (mport->sinterval & 0xFF);
  1151. swr_master_write(swrm,
  1152. SWRM_DP_PORT_CTRL_BANK((i + 1), bank),
  1153. value);
  1154. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  1155. __func__, i,
  1156. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)), value);
  1157. if (!mport->req_ch)
  1158. swrm_pcm_port_config(swrm, (i + 1),
  1159. mport->stream_type, mport->dir, false);
  1160. }
  1161. }
  1162. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  1163. {
  1164. struct swr_port_info *port_req, *next;
  1165. int i;
  1166. struct swrm_mports *mport;
  1167. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1168. if (!swrm) {
  1169. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1170. return;
  1171. }
  1172. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1173. master->num_port);
  1174. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  1175. mport = &(swrm->mport_cfg[i]);
  1176. list_for_each_entry_safe(port_req, next,
  1177. &mport->port_req_list, list) {
  1178. /* skip ports without new ch req */
  1179. if (port_req->ch_en == port_req->req_ch)
  1180. continue;
  1181. /* remove new ch req's*/
  1182. port_req->ch_en = port_req->req_ch;
  1183. /* If no streams enabled on port, remove the port req */
  1184. if (port_req->ch_en == 0) {
  1185. list_del(&port_req->list);
  1186. kfree(port_req);
  1187. }
  1188. }
  1189. /* remove new ch req's on mport*/
  1190. mport->ch_en = mport->req_ch;
  1191. if (!(mport->ch_en)) {
  1192. mport->port_en = false;
  1193. master->port_en_mask &= ~i;
  1194. }
  1195. }
  1196. }
  1197. static u8 swrm_get_controller_offset1(struct swr_mstr_ctrl *swrm,
  1198. u8* dev_offset, u8 off1)
  1199. {
  1200. u8 offset1 = 0x0F;
  1201. int i = 0;
  1202. if (swrm->master_id == MASTER_ID_TX) {
  1203. for (i = 1; i < SWRM_NUM_AUTO_ENUM_SLAVES; i++) {
  1204. pr_debug("%s: dev offset: %d\n",
  1205. __func__, dev_offset[i]);
  1206. if (offset1 > dev_offset[i])
  1207. offset1 = dev_offset[i];
  1208. }
  1209. } else {
  1210. offset1 = off1;
  1211. }
  1212. pr_debug("%s: offset: %d\n", __func__, offset1);
  1213. return offset1;
  1214. }
  1215. static int swrm_get_uc(int bus_clk)
  1216. {
  1217. switch (bus_clk) {
  1218. case SWR_CLK_RATE_4P8MHZ:
  1219. return SWR_UC1;
  1220. case SWR_CLK_RATE_1P2MHZ:
  1221. return SWR_UC2;
  1222. case SWR_CLK_RATE_0P6MHZ:
  1223. return SWR_UC3;
  1224. case SWR_CLK_RATE_9P6MHZ:
  1225. default:
  1226. return SWR_UC0;
  1227. }
  1228. return SWR_UC0;
  1229. }
  1230. static void swrm_get_device_frame_shape(struct swr_mstr_ctrl *swrm,
  1231. struct swrm_mports *mport,
  1232. struct swr_port_info *port_req)
  1233. {
  1234. u32 uc = SWR_UC0;
  1235. u32 port_id_offset = 0;
  1236. if (swrm->master_id == MASTER_ID_TX) {
  1237. uc = swrm_get_uc(swrm->bus_clk);
  1238. port_id_offset = (port_req->dev_num - 1) *
  1239. SWR_MAX_DEV_PORT_NUM +
  1240. port_req->slave_port_id;
  1241. if (port_id_offset >= SWR_MAX_MSTR_PORT_NUM)
  1242. return;
  1243. port_req->sinterval =
  1244. ((swrm->bus_clk * 2) / port_req->ch_rate) - 1;
  1245. port_req->offset1 = swrm->pp[uc][port_id_offset].offset1;
  1246. port_req->offset2 = 0x00;
  1247. port_req->hstart = 0xFF;
  1248. port_req->hstop = 0xFF;
  1249. port_req->word_length = 0xFF;
  1250. port_req->blk_pack_mode = 0xFF;
  1251. port_req->blk_grp_count = 0xFF;
  1252. port_req->lane_ctrl = swrm->pp[uc][port_id_offset].lane_ctrl;
  1253. } else {
  1254. /* copy master port config to slave */
  1255. port_req->sinterval = mport->sinterval;
  1256. port_req->offset1 = mport->offset1;
  1257. port_req->offset2 = mport->offset2;
  1258. port_req->hstart = mport->hstart;
  1259. port_req->hstop = mport->hstop;
  1260. port_req->word_length = mport->word_length;
  1261. port_req->blk_pack_mode = mport->blk_pack_mode;
  1262. port_req->blk_grp_count = mport->blk_grp_count;
  1263. port_req->lane_ctrl = mport->lane_ctrl;
  1264. }
  1265. if (swrm->master_id == MASTER_ID_WSA) {
  1266. uc = swrm_get_uc(swrm->bus_clk);
  1267. port_id_offset = (port_req->dev_num - 1) *
  1268. SWR_MAX_DEV_PORT_NUM +
  1269. port_req->slave_port_id;
  1270. if (port_id_offset >= SWR_MAX_MSTR_PORT_NUM ||
  1271. !swrm->pp[uc][port_id_offset].offset1)
  1272. return;
  1273. port_req->offset1 = swrm->pp[uc][port_id_offset].offset1;
  1274. }
  1275. }
  1276. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  1277. {
  1278. u32 value = 0, slv_id = 0;
  1279. struct swr_port_info *port_req;
  1280. int i, j;
  1281. u16 sinterval = 0xFFFF;
  1282. u8 lane_ctrl = 0;
  1283. struct swrm_mports *mport;
  1284. u32 reg[SWRM_MAX_PORT_REG];
  1285. u32 val[SWRM_MAX_PORT_REG];
  1286. int len = 0;
  1287. u8 hparams = 0;
  1288. u32 controller_offset = 0;
  1289. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1290. u8 dev_offset[SWRM_NUM_AUTO_ENUM_SLAVES];
  1291. if (!swrm) {
  1292. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1293. return;
  1294. }
  1295. memset(dev_offset, 0xff, SWRM_NUM_AUTO_ENUM_SLAVES);
  1296. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1297. master->num_port);
  1298. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  1299. mport = &(swrm->mport_cfg[i]);
  1300. if (!mport->port_en)
  1301. continue;
  1302. swrm_pcm_port_config(swrm, (i + 1),
  1303. mport->stream_type, mport->dir, true);
  1304. j = 0;
  1305. lane_ctrl = 0;
  1306. sinterval = 0xFFFF;
  1307. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1308. if (!port_req->dev_num)
  1309. continue;
  1310. j++;
  1311. slv_id = port_req->slave_port_id;
  1312. /* Assumption: If different channels in the same port
  1313. * on master is enabled for different slaves, then each
  1314. * slave offset should be configured differently.
  1315. */
  1316. swrm_get_device_frame_shape(swrm, mport, port_req);
  1317. if (j == 1) {
  1318. sinterval = port_req->sinterval;
  1319. lane_ctrl = port_req->lane_ctrl;
  1320. } else if (sinterval != port_req->sinterval ||
  1321. lane_ctrl != port_req->lane_ctrl) {
  1322. dev_err_ratelimited(swrm->dev,
  1323. "%s:slaves/slave ports attaching to mport%d"\
  1324. " are not using same SI or data lane, update slave tables,"\
  1325. "bailing out without setting port config\n",
  1326. __func__, i);
  1327. return;
  1328. }
  1329. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1330. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  1331. port_req->dev_num, get_cmd_id(swrm),
  1332. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  1333. bank));
  1334. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1335. val[len++] = SWR_REG_VAL_PACK(
  1336. port_req->sinterval & 0xFF,
  1337. port_req->dev_num, get_cmd_id(swrm),
  1338. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  1339. bank));
  1340. /* Only wite MSB if SI > 0xFF */
  1341. if (port_req->sinterval > 0xFF) {
  1342. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1343. val[len++] = SWR_REG_VAL_PACK(
  1344. (port_req->sinterval >> 8) & 0xFF,
  1345. port_req->dev_num, get_cmd_id(swrm),
  1346. SWRS_DP_SAMPLE_CONTROL_2_BANK(slv_id,
  1347. bank));
  1348. }
  1349. if (port_req->offset1 != SWR_INVALID_PARAM) {
  1350. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1351. val[len++] = SWR_REG_VAL_PACK(port_req->offset1,
  1352. port_req->dev_num, get_cmd_id(swrm),
  1353. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  1354. bank));
  1355. }
  1356. if (port_req->offset2 != SWR_INVALID_PARAM) {
  1357. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1358. val[len++] = SWR_REG_VAL_PACK(port_req->offset2,
  1359. port_req->dev_num, get_cmd_id(swrm),
  1360. SWRS_DP_OFFSET_CONTROL_2_BANK(
  1361. slv_id, bank));
  1362. }
  1363. if (port_req->hstart != SWR_INVALID_PARAM
  1364. && port_req->hstop != SWR_INVALID_PARAM) {
  1365. hparams = (port_req->hstart << 4) |
  1366. port_req->hstop;
  1367. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1368. val[len++] = SWR_REG_VAL_PACK(hparams,
  1369. port_req->dev_num, get_cmd_id(swrm),
  1370. SWRS_DP_HCONTROL_BANK(slv_id,
  1371. bank));
  1372. }
  1373. if (port_req->word_length != SWR_INVALID_PARAM) {
  1374. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1375. val[len++] =
  1376. SWR_REG_VAL_PACK(port_req->word_length,
  1377. port_req->dev_num, get_cmd_id(swrm),
  1378. SWRS_DP_BLOCK_CONTROL_1(slv_id));
  1379. }
  1380. if (port_req->blk_pack_mode != SWR_INVALID_PARAM) {
  1381. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1382. val[len++] =
  1383. SWR_REG_VAL_PACK(
  1384. port_req->blk_pack_mode,
  1385. port_req->dev_num, get_cmd_id(swrm),
  1386. SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
  1387. bank));
  1388. }
  1389. if (port_req->blk_grp_count != SWR_INVALID_PARAM) {
  1390. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1391. val[len++] =
  1392. SWR_REG_VAL_PACK(
  1393. port_req->blk_grp_count,
  1394. port_req->dev_num, get_cmd_id(swrm),
  1395. SWRS_DP_BLOCK_CONTROL_2_BANK(
  1396. slv_id, bank));
  1397. }
  1398. if (port_req->lane_ctrl != SWR_INVALID_PARAM) {
  1399. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1400. val[len++] =
  1401. SWR_REG_VAL_PACK(port_req->lane_ctrl,
  1402. port_req->dev_num, get_cmd_id(swrm),
  1403. SWRS_DP_LANE_CONTROL_BANK(
  1404. slv_id, bank));
  1405. }
  1406. port_req->ch_en = port_req->req_ch;
  1407. dev_offset[port_req->dev_num] = port_req->offset1;
  1408. }
  1409. if (swrm->master_id == MASTER_ID_TX) {
  1410. mport->sinterval = sinterval;
  1411. mport->lane_ctrl = lane_ctrl;
  1412. }
  1413. value = ((mport->req_ch)
  1414. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  1415. if (mport->offset2 != SWR_INVALID_PARAM)
  1416. value |= ((mport->offset2)
  1417. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  1418. controller_offset = (swrm_get_controller_offset1(swrm,
  1419. dev_offset, mport->offset1));
  1420. value |= (controller_offset << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  1421. mport->offset1 = controller_offset;
  1422. value |= (mport->sinterval & 0xFF);
  1423. reg[len] = SWRM_DP_PORT_CTRL_BANK((i + 1), bank);
  1424. val[len++] = value;
  1425. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  1426. __func__, (i + 1),
  1427. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)), value);
  1428. reg[len] = SWRM_DP_SAMPLECTRL2_BANK((i + 1), bank);
  1429. val[len++] = ((mport->sinterval >> 8) & 0xFF);
  1430. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  1431. reg[len] = SWRM_DP_PORT_CTRL_2_BANK((i + 1), bank);
  1432. val[len++] = mport->lane_ctrl;
  1433. }
  1434. if (mport->word_length != SWR_INVALID_PARAM) {
  1435. reg[len] = SWRM_DP_BLOCK_CTRL_1((i + 1));
  1436. val[len++] = mport->word_length;
  1437. }
  1438. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  1439. reg[len] = SWRM_DP_BLOCK_CTRL2_BANK((i + 1), bank);
  1440. val[len++] = mport->blk_grp_count;
  1441. }
  1442. if (mport->hstart != SWR_INVALID_PARAM
  1443. && mport->hstop != SWR_INVALID_PARAM) {
  1444. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1445. hparams = (mport->hstop << 4) | mport->hstart;
  1446. val[len++] = hparams;
  1447. } else {
  1448. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1449. hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
  1450. val[len++] = hparams;
  1451. }
  1452. if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
  1453. reg[len] = SWRM_DP_BLOCK_CTRL3_BANK((i + 1), bank);
  1454. val[len++] = mport->blk_pack_mode;
  1455. }
  1456. mport->ch_en = mport->req_ch;
  1457. }
  1458. swrm_reg_dump(swrm, reg, val, len, __func__);
  1459. swr_master_bulk_write(swrm, reg, val, len);
  1460. }
  1461. static void swrm_apply_port_config(struct swr_master *master)
  1462. {
  1463. u8 bank;
  1464. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1465. if (!swrm) {
  1466. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  1467. __func__);
  1468. return;
  1469. }
  1470. bank = get_inactive_bank_num(swrm);
  1471. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  1472. __func__, bank, master->num_port);
  1473. if (!swrm->disable_div2_clk_switch)
  1474. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, get_cmd_id(swrm),
  1475. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  1476. swrm_copy_data_port_config(master, bank);
  1477. }
  1478. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  1479. {
  1480. u8 bank;
  1481. u32 value = 0, n_row = 0, n_col = 0;
  1482. u32 row = 0, col = 0;
  1483. int bus_clk_div_factor;
  1484. int ret;
  1485. u8 ssp_period = 0;
  1486. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1487. int mask = (SWRM_ROW_CTRL_MASK | SWRM_COL_CTRL_MASK |
  1488. SWRM_CLK_DIV_MASK | SWRM_SSP_PERIOD_MASK);
  1489. u8 inactive_bank;
  1490. int frame_sync = SWRM_FRAME_SYNC_SEL;
  1491. if (!swrm) {
  1492. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1493. return -EFAULT;
  1494. }
  1495. mutex_lock(&swrm->mlock);
  1496. /*
  1497. * During disable if master is already down, which implies an ssr/pdr
  1498. * scenario, just mark ports as disabled and exit
  1499. */
  1500. if (swrm->state == SWR_MSTR_SSR && !enable) {
  1501. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1502. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1503. __func__);
  1504. goto exit;
  1505. }
  1506. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1507. swrm_cleanup_disabled_port_reqs(master);
  1508. /* reset enable_count to 0 in SSR if master is already down */
  1509. swrm->pcm_enable_count = 0;
  1510. if (!swrm_is_port_en(master)) {
  1511. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1512. __func__);
  1513. pm_runtime_mark_last_busy(swrm->dev);
  1514. pm_runtime_put_autosuspend(swrm->dev);
  1515. }
  1516. goto exit;
  1517. }
  1518. bank = get_inactive_bank_num(swrm);
  1519. if (enable) {
  1520. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  1521. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  1522. __func__);
  1523. goto exit;
  1524. }
  1525. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1526. ret = swrm_get_port_config(swrm);
  1527. if (ret) {
  1528. /* cannot accommodate ports */
  1529. swrm_cleanup_disabled_port_reqs(master);
  1530. mutex_unlock(&swrm->mlock);
  1531. return -EINVAL;
  1532. }
  1533. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  1534. SWRM_INTERRUPT_STATUS_MASK);
  1535. /* apply the new port config*/
  1536. swrm_apply_port_config(master);
  1537. } else {
  1538. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1539. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1540. __func__);
  1541. goto exit;
  1542. }
  1543. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1544. swrm_disable_ports(master, bank);
  1545. }
  1546. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
  1547. __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
  1548. if (enable) {
  1549. /* set col = 16 */
  1550. n_col = SWR_MAX_COL;
  1551. col = SWRM_COL_16;
  1552. if (swrm->bus_clk == MCLK_FREQ_LP) {
  1553. n_col = SWR_MIN_COL;
  1554. col = SWRM_COL_02;
  1555. }
  1556. } else {
  1557. /*
  1558. * Do not change to col = 2 if there are still active ports
  1559. */
  1560. if (!master->num_port) {
  1561. n_col = SWR_MIN_COL;
  1562. col = SWRM_COL_02;
  1563. } else {
  1564. n_col = SWR_MAX_COL;
  1565. col = SWRM_COL_16;
  1566. }
  1567. }
  1568. /* Use default 50 * x, frame shape. Change based on mclk */
  1569. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  1570. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
  1571. n_row = SWR_ROW_64;
  1572. row = SWRM_ROW_64;
  1573. frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
  1574. } else {
  1575. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
  1576. n_row = SWR_ROW_50;
  1577. row = SWRM_ROW_50;
  1578. frame_sync = SWRM_FRAME_SYNC_SEL;
  1579. }
  1580. ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
  1581. bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
  1582. dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
  1583. ssp_period, bus_clk_div_factor);
  1584. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank));
  1585. value &= (~mask);
  1586. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1587. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1588. (bus_clk_div_factor <<
  1589. SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
  1590. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1591. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1592. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  1593. SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1594. enable_bank_switch(swrm, bank, n_row, n_col);
  1595. inactive_bank = bank ? 0 : 1;
  1596. if (enable)
  1597. swrm_copy_data_port_config(master, inactive_bank);
  1598. else {
  1599. swrm_disable_ports(master, inactive_bank);
  1600. swrm_cleanup_disabled_port_reqs(master);
  1601. }
  1602. if (!swrm_is_port_en(master)) {
  1603. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1604. __func__);
  1605. pm_runtime_mark_last_busy(swrm->dev);
  1606. if (!enable)
  1607. pm_runtime_set_autosuspend_delay(swrm->dev, 80);
  1608. pm_runtime_put_autosuspend(swrm->dev);
  1609. }
  1610. exit:
  1611. mutex_unlock(&swrm->mlock);
  1612. return 0;
  1613. }
  1614. static int swrm_connect_port(struct swr_master *master,
  1615. struct swr_params *portinfo)
  1616. {
  1617. int i;
  1618. struct swr_port_info *port_req;
  1619. int ret = 0;
  1620. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1621. struct swrm_mports *mport;
  1622. u8 mstr_port_id, mstr_ch_msk;
  1623. dev_dbg(&master->dev, "%s: enter\n", __func__);
  1624. if (!portinfo)
  1625. return -EINVAL;
  1626. if (!swrm) {
  1627. dev_err_ratelimited(&master->dev,
  1628. "%s: Invalid handle to swr controller\n",
  1629. __func__);
  1630. return -EINVAL;
  1631. }
  1632. mutex_lock(&swrm->mlock);
  1633. mutex_lock(&swrm->devlock);
  1634. if (!swrm->dev_up) {
  1635. swr_port_response(master, portinfo->tid);
  1636. mutex_unlock(&swrm->devlock);
  1637. mutex_unlock(&swrm->mlock);
  1638. return -EINVAL;
  1639. }
  1640. mutex_unlock(&swrm->devlock);
  1641. if (!swrm_is_port_en(master))
  1642. pm_runtime_get_sync(swrm->dev);
  1643. for (i = 0; i < portinfo->num_port; i++) {
  1644. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  1645. portinfo->port_type[i],
  1646. portinfo->port_id[i]);
  1647. if (ret) {
  1648. dev_err_ratelimited(&master->dev,
  1649. "%s: mstr portid for slv port %d not found\n",
  1650. __func__, portinfo->port_id[i]);
  1651. goto port_fail;
  1652. }
  1653. mport = &(swrm->mport_cfg[mstr_port_id]);
  1654. /* get port req */
  1655. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1656. portinfo->dev_num);
  1657. if (!port_req) {
  1658. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  1659. __func__, portinfo->port_id[i],
  1660. portinfo->dev_num);
  1661. port_req = kzalloc(sizeof(struct swr_port_info),
  1662. GFP_KERNEL);
  1663. if (!port_req) {
  1664. ret = -ENOMEM;
  1665. goto mem_fail;
  1666. }
  1667. port_req->dev_num = portinfo->dev_num;
  1668. port_req->slave_port_id = portinfo->port_id[i];
  1669. port_req->num_ch = portinfo->num_ch[i];
  1670. port_req->ch_rate = portinfo->ch_rate[i];
  1671. port_req->ch_en = 0;
  1672. port_req->master_port_id = mstr_port_id;
  1673. list_add(&port_req->list, &mport->port_req_list);
  1674. }
  1675. port_req->req_ch |= portinfo->ch_en[i];
  1676. dev_dbg(&master->dev,
  1677. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  1678. __func__, port_req->master_port_id,
  1679. port_req->slave_port_id, port_req->ch_rate,
  1680. port_req->num_ch);
  1681. /* Put the port req on master port */
  1682. mport = &(swrm->mport_cfg[mstr_port_id]);
  1683. mport->port_en = true;
  1684. mport->req_ch |= mstr_ch_msk;
  1685. master->port_en_mask |= (1 << mstr_port_id);
  1686. if (swrm->clk_stop_mode0_supp &&
  1687. swrm->dynamic_port_map_supported) {
  1688. mport->ch_rate += portinfo->ch_rate[i];
  1689. swrm_update_bus_clk(swrm);
  1690. } else {
  1691. /*
  1692. * Fallback to assign slave port ch_rate
  1693. * as master port uses same ch_rate as slave
  1694. * unlike soundwire TX master ports where
  1695. * unified ports and multiple slave port
  1696. * channels can attach to same master port
  1697. */
  1698. mport->ch_rate = portinfo->ch_rate[i];
  1699. }
  1700. }
  1701. master->num_port += portinfo->num_port;
  1702. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1703. swr_port_response(master, portinfo->tid);
  1704. mutex_unlock(&swrm->mlock);
  1705. return 0;
  1706. port_fail:
  1707. mem_fail:
  1708. swr_port_response(master, portinfo->tid);
  1709. /* cleanup port reqs in error condition */
  1710. swrm_cleanup_disabled_port_reqs(master);
  1711. mutex_unlock(&swrm->mlock);
  1712. return ret;
  1713. }
  1714. static int swrm_disconnect_port(struct swr_master *master,
  1715. struct swr_params *portinfo)
  1716. {
  1717. int i, ret = 0;
  1718. struct swr_port_info *port_req;
  1719. struct swrm_mports *mport;
  1720. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1721. u8 mstr_port_id, mstr_ch_mask;
  1722. u8 num_port = 0;
  1723. if (!swrm) {
  1724. dev_err_ratelimited(&master->dev,
  1725. "%s: Invalid handle to swr controller\n",
  1726. __func__);
  1727. return -EINVAL;
  1728. }
  1729. if (!portinfo) {
  1730. dev_err_ratelimited(&master->dev, "%s: portinfo is NULL\n", __func__);
  1731. return -EINVAL;
  1732. }
  1733. mutex_lock(&swrm->mlock);
  1734. for (i = 0; i < portinfo->num_port; i++) {
  1735. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1736. portinfo->port_type[i], portinfo->port_id[i]);
  1737. if (ret) {
  1738. dev_err_ratelimited(&master->dev,
  1739. "%s: mstr portid for slv port %d not found\n",
  1740. __func__, portinfo->port_id[i]);
  1741. goto err;
  1742. }
  1743. mport = &(swrm->mport_cfg[mstr_port_id]);
  1744. /* get port req */
  1745. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1746. portinfo->dev_num);
  1747. if (!port_req) {
  1748. dev_err_ratelimited(&master->dev, "%s:port not enabled : port %d\n",
  1749. __func__, portinfo->port_id[i]);
  1750. continue;
  1751. }
  1752. port_req->req_ch &= ~portinfo->ch_en[i];
  1753. mport->req_ch &= ~mstr_ch_mask;
  1754. if (swrm->clk_stop_mode0_supp &&
  1755. swrm->dynamic_port_map_supported &&
  1756. !mport->req_ch) {
  1757. mport->ch_rate = 0;
  1758. swrm_update_bus_clk(swrm);
  1759. }
  1760. num_port++;
  1761. }
  1762. if (master->num_port > num_port)
  1763. master->num_port -= num_port;
  1764. else
  1765. master->num_port = 0;
  1766. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1767. swr_port_response(master, portinfo->tid);
  1768. mutex_unlock(&swrm->mlock);
  1769. return 0;
  1770. err:
  1771. swr_port_response(master, portinfo->tid);
  1772. mutex_unlock(&swrm->mlock);
  1773. return -EINVAL;
  1774. }
  1775. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1776. int status, u8 *devnum)
  1777. {
  1778. int i;
  1779. bool found = false;
  1780. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1781. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1782. *devnum = i;
  1783. found = true;
  1784. break;
  1785. }
  1786. status >>= 2;
  1787. }
  1788. if (found)
  1789. return 0;
  1790. else
  1791. return -EINVAL;
  1792. }
  1793. static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
  1794. {
  1795. int i;
  1796. int status = 0;
  1797. u32 temp;
  1798. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1799. if (!status) {
  1800. dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
  1801. __func__, status);
  1802. return;
  1803. }
  1804. dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
  1805. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1806. if (status & SWRM_MCP_SLV_STATUS_MASK) {
  1807. if (!swrm->clk_stop_wakeup) {
  1808. swrm_cmd_fifo_rd_cmd(swrm, &temp, i,
  1809. get_cmd_id(swrm), SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1810. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i,
  1811. get_cmd_id(swrm), SWRS_SCP_INT_STATUS_CLEAR_1);
  1812. }
  1813. swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, get_cmd_id(swrm),
  1814. SWRS_SCP_INT_STATUS_MASK_1);
  1815. }
  1816. status >>= 2;
  1817. }
  1818. }
  1819. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1820. int status, u8 *devnum)
  1821. {
  1822. int i;
  1823. int new_sts = status;
  1824. int ret = SWR_NOT_PRESENT;
  1825. if (status != swrm->slave_status) {
  1826. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1827. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1828. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1829. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1830. *devnum = i;
  1831. break;
  1832. }
  1833. status >>= 2;
  1834. swrm->slave_status >>= 2;
  1835. }
  1836. swrm->slave_status = new_sts;
  1837. }
  1838. return ret;
  1839. }
  1840. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1841. {
  1842. struct swr_mstr_ctrl *swrm = dev;
  1843. u32 value, intr_sts, intr_sts_masked;
  1844. u32 temp = 0;
  1845. u32 status, chg_sts, i;
  1846. u8 devnum = 0;
  1847. int ret = IRQ_HANDLED;
  1848. struct swr_device *swr_dev;
  1849. struct swr_master *mstr = &swrm->master;
  1850. int retry = 5;
  1851. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1852. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1853. return IRQ_NONE;
  1854. }
  1855. mutex_lock(&swrm->reslock);
  1856. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  1857. ret = IRQ_NONE;
  1858. goto exit;
  1859. }
  1860. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
  1861. ret = IRQ_NONE;
  1862. goto err_audio_hw_vote;
  1863. }
  1864. ret = swrm_clk_request(swrm, true);
  1865. if (ret) {
  1866. dev_err_ratelimited(dev, "%s: swrm clk failed\n", __func__);
  1867. ret = IRQ_NONE;
  1868. goto err_audio_core_vote;
  1869. }
  1870. mutex_unlock(&swrm->reslock);
  1871. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS(swrm->ee_val));
  1872. intr_sts_masked = intr_sts & swrm->intr_mask;
  1873. dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
  1874. handle_irq:
  1875. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1876. value = intr_sts_masked & (1 << i);
  1877. if (!value)
  1878. continue;
  1879. switch (value) {
  1880. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1881. dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
  1882. __func__);
  1883. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1884. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1885. if (ret) {
  1886. dev_err_ratelimited(swrm->dev,
  1887. "%s: no slave alert found.spurious interrupt\n",
  1888. __func__);
  1889. break;
  1890. }
  1891. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum,
  1892. get_cmd_id(swrm),
  1893. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1894. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum,
  1895. get_cmd_id(swrm),
  1896. SWRS_SCP_INT_STATUS_CLEAR_1);
  1897. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum,
  1898. get_cmd_id(swrm),
  1899. SWRS_SCP_INT_STATUS_CLEAR_1);
  1900. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1901. if (swr_dev->dev_num != devnum)
  1902. continue;
  1903. if (swr_dev->slave_irq) {
  1904. do {
  1905. swr_dev->slave_irq_pending = 0;
  1906. handle_nested_irq(
  1907. irq_find_mapping(
  1908. swr_dev->slave_irq, 0));
  1909. } while (swr_dev->slave_irq_pending && swrm->dev_up);
  1910. }
  1911. }
  1912. break;
  1913. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1914. dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
  1915. __func__);
  1916. break;
  1917. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1918. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1919. swrm_enable_slave_irq(swrm);
  1920. if (status == swrm->slave_status) {
  1921. dev_dbg(swrm->dev,
  1922. "%s: No change in slave status: 0x%x\n",
  1923. __func__, status);
  1924. break;
  1925. }
  1926. chg_sts = swrm_check_slave_change_status(swrm, status,
  1927. &devnum);
  1928. switch (chg_sts) {
  1929. case SWR_NOT_PRESENT:
  1930. dev_dbg(swrm->dev,
  1931. "%s: device %d got detached\n",
  1932. __func__, devnum);
  1933. if (devnum == 0) {
  1934. /*
  1935. * enable host irq if device 0 detached
  1936. * as hw will mask host_irq at slave
  1937. * but will not unmask it afterwards.
  1938. */
  1939. swrm->enable_slave_irq = true;
  1940. }
  1941. break;
  1942. case SWR_ATTACHED_OK:
  1943. dev_dbg(swrm->dev,
  1944. "%s: device %d got attached\n",
  1945. __func__, devnum);
  1946. /* enable host irq from slave device*/
  1947. swrm->enable_slave_irq = true;
  1948. break;
  1949. case SWR_ALERT:
  1950. dev_dbg(swrm->dev,
  1951. "%s: device %d has pending interrupt\n",
  1952. __func__, devnum);
  1953. break;
  1954. }
  1955. break;
  1956. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1957. dev_err_ratelimited(swrm->dev,
  1958. "%s: SWR bus clsh detected\n",
  1959. __func__);
  1960. swrm->intr_mask &=
  1961. ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
  1962. swr_master_write(swrm,
  1963. SWRM_INTERRUPT_EN(swrm->ee_val),
  1964. swrm->intr_mask);
  1965. break;
  1966. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1967. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  1968. dev_err_ratelimited(swrm->dev,
  1969. "%s: SWR read FIFO overflow fifo status %x\n",
  1970. __func__, value);
  1971. break;
  1972. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1973. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  1974. dev_err_ratelimited(swrm->dev,
  1975. "%s: SWR read FIFO underflow fifo status %x\n",
  1976. __func__, value);
  1977. break;
  1978. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1979. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  1980. dev_err_ratelimited(swrm->dev,
  1981. "%s: SWR write FIFO overflow fifo status %x\n",
  1982. __func__, value);
  1983. break;
  1984. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1985. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  1986. dev_err_ratelimited(swrm->dev,
  1987. "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1988. __func__, value);
  1989. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1990. break;
  1991. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1992. dev_err_ratelimited(swrm->dev,
  1993. "%s: SWR Port collision detected\n",
  1994. __func__);
  1995. swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1996. swr_master_write(swrm,
  1997. SWRM_INTERRUPT_EN(swrm->ee_val),
  1998. swrm->intr_mask);
  1999. break;
  2000. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  2001. dev_dbg(swrm->dev,
  2002. "%s: SWR read enable valid mismatch\n",
  2003. __func__);
  2004. swrm->intr_mask &=
  2005. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  2006. swr_master_write(swrm,
  2007. SWRM_INTERRUPT_EN(swrm->ee_val),
  2008. swrm->intr_mask);
  2009. break;
  2010. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  2011. complete(&swrm->broadcast);
  2012. dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
  2013. __func__);
  2014. break;
  2015. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  2016. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 0);
  2017. while (swr_master_read(swrm, SWRM_ENUMERATOR_STATUS)) {
  2018. if (!retry) {
  2019. dev_dbg(swrm->dev,
  2020. "%s: ENUM status is not idle\n",
  2021. __func__);
  2022. break;
  2023. }
  2024. retry--;
  2025. }
  2026. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 1);
  2027. break;
  2028. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  2029. break;
  2030. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  2031. swrm_check_link_status(swrm, 0x1);
  2032. break;
  2033. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  2034. break;
  2035. case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
  2036. if (swrm->state == SWR_MSTR_UP) {
  2037. dev_dbg(swrm->dev,
  2038. "%s:SWR Master is already up\n",
  2039. __func__);
  2040. } else {
  2041. dev_err_ratelimited(swrm->dev,
  2042. "%s: SWR wokeup during clock stop\n",
  2043. __func__);
  2044. /* It might be possible the slave device gets
  2045. * reset and slave interrupt gets missed. So
  2046. * re-enable Host IRQ and process slave pending
  2047. * interrupts, if any.
  2048. */
  2049. swrm->clk_stop_wakeup = true;
  2050. swrm_enable_slave_irq(swrm);
  2051. swrm->clk_stop_wakeup = false;
  2052. }
  2053. break;
  2054. #ifdef CONFIG_SWRM_VER_2P0
  2055. case SWRM_INTERRUPT_STATUS_CMD_IGNORED_AND_EXEC_CONTINUED:
  2056. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2057. dev_err_ratelimited(swrm->dev,
  2058. "%s: SWR CMD Ignored, fifo status 0x%x\n",
  2059. __func__, value);
  2060. /* Wait 3.5ms to clear */
  2061. usleep_range(3500, 3505);
  2062. break;
  2063. #endif
  2064. case SWRM_INTERRUPT_STATUS_DOUT_RATE_MISMATCH:
  2065. dev_err(swrm->dev,
  2066. "%s: SWR Port Channel rate mismatch\n", __func__);
  2067. swrm->intr_mask &=
  2068. ~SWRM_INTERRUPT_STATUS_DOUT_RATE_MISMATCH;
  2069. swr_master_write(swrm,
  2070. SWRM_INTERRUPT_EN(swrm->ee_val), swrm->intr_mask);
  2071. break;
  2072. default:
  2073. dev_err_ratelimited(swrm->dev,
  2074. "%s: SWR unknown interrupt value: %d\n",
  2075. __func__, value);
  2076. ret = IRQ_NONE;
  2077. break;
  2078. }
  2079. }
  2080. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR(swrm->ee_val), intr_sts);
  2081. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR(swrm->ee_val), 0x0);
  2082. if (swrm->enable_slave_irq) {
  2083. /* Enable slave irq here */
  2084. swrm_enable_slave_irq(swrm);
  2085. swrm->enable_slave_irq = false;
  2086. }
  2087. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS(swrm->ee_val));
  2088. intr_sts_masked = intr_sts & swrm->intr_mask;
  2089. if (intr_sts_masked && !pm_runtime_suspended(swrm->dev)) {
  2090. dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
  2091. __func__, intr_sts_masked);
  2092. goto handle_irq;
  2093. }
  2094. mutex_lock(&swrm->reslock);
  2095. swrm_clk_request(swrm, false);
  2096. err_audio_core_vote:
  2097. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  2098. err_audio_hw_vote:
  2099. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2100. exit:
  2101. mutex_unlock(&swrm->reslock);
  2102. swrm_unlock_sleep(swrm);
  2103. return ret;
  2104. }
  2105. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  2106. {
  2107. struct swr_mstr_ctrl *swrm = dev;
  2108. int ret = IRQ_HANDLED;
  2109. if (!swrm || !(swrm->dev)) {
  2110. pr_err_ratelimited("%s: swrm or dev is null\n", __func__);
  2111. return IRQ_NONE;
  2112. }
  2113. mutex_lock(&swrm->devlock);
  2114. if (swrm->state == SWR_MSTR_SSR || !swrm->dev_up) {
  2115. if (swrm->wake_irq > 0) {
  2116. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  2117. pr_err_ratelimited("%s: irq data is NULL\n", __func__);
  2118. mutex_unlock(&swrm->devlock);
  2119. return IRQ_NONE;
  2120. }
  2121. mutex_lock(&swrm->irq_lock);
  2122. if (!irqd_irq_disabled(
  2123. irq_get_irq_data(swrm->wake_irq))) {
  2124. irq_set_irq_wake(swrm->wake_irq, 0);
  2125. disable_irq_nosync(swrm->wake_irq);
  2126. }
  2127. mutex_unlock(&swrm->irq_lock);
  2128. }
  2129. mutex_unlock(&swrm->devlock);
  2130. return ret;
  2131. }
  2132. mutex_unlock(&swrm->devlock);
  2133. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2134. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2135. goto exit;
  2136. }
  2137. if (swrm->wake_irq > 0) {
  2138. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  2139. pr_err_ratelimited("%s: irq data is NULL\n", __func__);
  2140. return IRQ_NONE;
  2141. }
  2142. mutex_lock(&swrm->irq_lock);
  2143. if (!irqd_irq_disabled(irq_get_irq_data(swrm->wake_irq))) {
  2144. irq_set_irq_wake(swrm->wake_irq, 0);
  2145. disable_irq_nosync(swrm->wake_irq);
  2146. }
  2147. mutex_unlock(&swrm->irq_lock);
  2148. }
  2149. pm_runtime_get_sync(swrm->dev);
  2150. pm_runtime_mark_last_busy(swrm->dev);
  2151. pm_runtime_put_autosuspend(swrm->dev);
  2152. swrm_unlock_sleep(swrm);
  2153. exit:
  2154. return ret;
  2155. }
  2156. static void swrm_wakeup_work(struct work_struct *work)
  2157. {
  2158. struct swr_mstr_ctrl *swrm;
  2159. swrm = container_of(work, struct swr_mstr_ctrl,
  2160. wakeup_work);
  2161. if (!swrm || !(swrm->dev)) {
  2162. pr_err("%s: swrm or dev is null\n", __func__);
  2163. return;
  2164. }
  2165. mutex_lock(&swrm->devlock);
  2166. if (!swrm->dev_up) {
  2167. mutex_unlock(&swrm->devlock);
  2168. goto exit;
  2169. }
  2170. mutex_unlock(&swrm->devlock);
  2171. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2172. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2173. goto exit;
  2174. }
  2175. pm_runtime_get_sync(swrm->dev);
  2176. pm_runtime_mark_last_busy(swrm->dev);
  2177. pm_runtime_put_autosuspend(swrm->dev);
  2178. swrm_unlock_sleep(swrm);
  2179. exit:
  2180. pm_relax(swrm->dev);
  2181. }
  2182. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  2183. {
  2184. u32 val;
  2185. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  2186. val = (swrm->slave_status >> (devnum * 2));
  2187. val &= SWRM_MCP_SLV_STATUS_MASK;
  2188. return val;
  2189. }
  2190. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  2191. u8 *dev_num)
  2192. {
  2193. int i;
  2194. u64 id = 0;
  2195. int ret = -EINVAL;
  2196. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2197. struct swr_device *swr_dev;
  2198. u32 num_dev = 0;
  2199. if (!swrm) {
  2200. pr_err("%s: Invalid handle to swr controller\n",
  2201. __func__);
  2202. return ret;
  2203. }
  2204. num_dev = swrm->num_dev;
  2205. mutex_lock(&swrm->devlock);
  2206. if (!swrm->dev_up) {
  2207. mutex_unlock(&swrm->devlock);
  2208. return ret;
  2209. }
  2210. mutex_unlock(&swrm->devlock);
  2211. pm_runtime_get_sync(swrm->dev);
  2212. for (i = 1; i < (num_dev + 1); i++) {
  2213. id = ((u64)(swr_master_read(swrm,
  2214. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  2215. id |= swr_master_read(swrm,
  2216. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  2217. /*
  2218. * As pm_runtime_get_sync() brings all slaves out of reset
  2219. * update logical device number for all slaves.
  2220. */
  2221. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2222. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  2223. u32 status = swrm_get_device_status(swrm, i);
  2224. if ((status == 0x01) || (status == 0x02)) {
  2225. swr_dev->dev_num = i;
  2226. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  2227. *dev_num = i;
  2228. ret = 0;
  2229. dev_info(swrm->dev,
  2230. "%s: devnum %d assigned for dev %llx\n",
  2231. __func__, i,
  2232. swr_dev->addr);
  2233. }
  2234. }
  2235. }
  2236. }
  2237. }
  2238. if (ret)
  2239. dev_err(swrm->dev,
  2240. "%s: device 0x%llx is not ready\n",
  2241. __func__, dev_id);
  2242. pm_runtime_mark_last_busy(swrm->dev);
  2243. pm_runtime_put_autosuspend(swrm->dev);
  2244. return ret;
  2245. }
  2246. static int swrm_init_port_params(struct swr_master *mstr, u32 dev_num,
  2247. u32 num_ports,
  2248. struct swr_dev_frame_config *uc_arr)
  2249. {
  2250. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2251. int i, j, port_id_offset;
  2252. if (!swrm) {
  2253. pr_err("%s: Invalid handle to swr controller\n", __func__);
  2254. return 0;
  2255. }
  2256. if (dev_num == 0) {
  2257. pr_err("%s: Invalid device number 0\n", __func__);
  2258. return -EINVAL;
  2259. }
  2260. for (i = 0; i < SWR_UC_MAX; i++) {
  2261. for (j = 0; j < num_ports; j++) {
  2262. port_id_offset = (dev_num - 1) * SWR_MAX_DEV_PORT_NUM + j;
  2263. swrm->pp[i][port_id_offset].offset1 = uc_arr[i].pp[j].offset1;
  2264. swrm->pp[i][port_id_offset].lane_ctrl = uc_arr[i].pp[j].lane_ctrl;
  2265. }
  2266. }
  2267. return 0;
  2268. }
  2269. static void swrm_device_wakeup_vote(struct swr_master *mstr)
  2270. {
  2271. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2272. if (!swrm) {
  2273. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  2274. __func__);
  2275. return;
  2276. }
  2277. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2278. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2279. return;
  2280. }
  2281. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true))
  2282. dev_err_ratelimited(swrm->dev, "%s:lpass core hw enable failed\n",
  2283. __func__);
  2284. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2285. dev_err_ratelimited(swrm->dev, "%s:lpass audio hw enable failed\n",
  2286. __func__);
  2287. pm_runtime_get_sync(swrm->dev);
  2288. }
  2289. static void swrm_device_wakeup_unvote(struct swr_master *mstr)
  2290. {
  2291. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2292. if (!swrm) {
  2293. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  2294. __func__);
  2295. return;
  2296. }
  2297. pm_runtime_mark_last_busy(swrm->dev);
  2298. pm_runtime_put_autosuspend(swrm->dev);
  2299. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  2300. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2301. swrm_unlock_sleep(swrm);
  2302. }
  2303. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  2304. {
  2305. int ret = 0, i = 0;
  2306. u32 val;
  2307. u8 row_ctrl = SWR_ROW_50;
  2308. u8 col_ctrl = SWR_MIN_COL;
  2309. u8 ssp_period = 1;
  2310. u8 retry_cmd_num = 3;
  2311. u32 reg[SWRM_MAX_INIT_REG];
  2312. u32 value[SWRM_MAX_INIT_REG];
  2313. u32 temp = 0;
  2314. int len = 0;
  2315. /* Change no of retry counts to 1 for wsa to avoid underflow */
  2316. if (swrm->master_id == MASTER_ID_WSA)
  2317. retry_cmd_num = 1;
  2318. /* SW workaround to gate hw_ctl for SWR version >=1.6 */
  2319. if (swrm->version >= SWRM_VERSION_1_6) {
  2320. if (swrm->swrm_hctl_reg) {
  2321. temp = ioread32(swrm->swrm_hctl_reg);
  2322. temp &= 0xFFFFFFFD;
  2323. iowrite32(temp, swrm->swrm_hctl_reg);
  2324. usleep_range(500, 505);
  2325. temp = ioread32(swrm->swrm_hctl_reg);
  2326. dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n",
  2327. __func__, temp);
  2328. }
  2329. }
  2330. ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
  2331. SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
  2332. dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
  2333. /* Clear Rows and Cols */
  2334. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  2335. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  2336. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  2337. reg[len] = SWRM_MCP_FRAME_CTRL_BANK(0);
  2338. value[len++] = val;
  2339. /* Set Auto enumeration flag */
  2340. reg[len] = SWRM_ENUMERATOR_CFG;
  2341. value[len++] = 1;
  2342. /* Configure No pings */
  2343. val = swr_master_read(swrm, SWRM_MCP_CFG);
  2344. val &= ~SWRM_NUM_PINGS_MASK;
  2345. val |= (0x1f << SWRM_NUM_PINGS_POS);
  2346. reg[len] = SWRM_MCP_CFG;
  2347. value[len++] = val;
  2348. /* Configure number of retries of a read/write cmd */
  2349. val = (retry_cmd_num);
  2350. reg[len] = SWRM_CMD_FIFO_CFG;
  2351. value[len++] = val;
  2352. if (swrm->version >= SWRM_VERSION_1_7) {
  2353. reg[len] = SWRM_LINK_MANAGER_EE;
  2354. value[len++] = swrm->ee_val;
  2355. }
  2356. #ifdef CONFIG_SWRM_VER_2P0
  2357. reg[len] = SWRM_CLK_CTRL(swrm->ee_val);
  2358. value[len++] = 0x01;
  2359. #endif
  2360. #ifdef CONFIG_SWRM_VER_1P7
  2361. reg[len] = SWRM_MCP_BUS_CTRL;
  2362. if (swrm->version < SWRM_VERSION_1_7)
  2363. value[len++] = 0x2;
  2364. else
  2365. value[len++] = 0x2 << swrm->ee_val;
  2366. #endif
  2367. /* Set IRQ to PULSE */
  2368. reg[len] = SWRM_COMP_CFG;
  2369. value[len++] = 0x02;
  2370. reg[len] = SWRM_INTERRUPT_CLEAR(swrm->ee_val);
  2371. value[len++] = 0xFFFFFFFF;
  2372. swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
  2373. /* Mask soundwire interrupts */
  2374. reg[len] = SWRM_INTERRUPT_EN(swrm->ee_val);
  2375. value[len++] = swrm->intr_mask;
  2376. reg[len] = SWRM_COMP_CFG;
  2377. value[len++] = 0x03;
  2378. swr_master_bulk_write(swrm, reg, value, len);
  2379. if (!swrm_check_link_status(swrm, 0x1)) {
  2380. dev_err(swrm->dev,
  2381. "%s: swr link failed to connect\n",
  2382. __func__);
  2383. for (i = 0; i < len; i++) {
  2384. usleep_range(50, 55);
  2385. dev_err(swrm->dev,
  2386. "%s:reg:0x%x val:0x%x\n",
  2387. __func__,
  2388. reg[i], swr_master_read(swrm, reg[i]));
  2389. }
  2390. return -EINVAL;
  2391. }
  2392. /* Execute it for versions >= 1.5.1 */
  2393. if (swrm->version >= SWRM_VERSION_1_5_1)
  2394. swr_master_write(swrm, SWRM_CMD_FIFO_CFG,
  2395. (swr_master_read(swrm,
  2396. SWRM_CMD_FIFO_CFG) | 0x80000000));
  2397. return ret;
  2398. }
  2399. static int swrm_event_notify(struct notifier_block *self,
  2400. unsigned long action, void *data)
  2401. {
  2402. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  2403. event_notifier);
  2404. if (!swrm || !(swrm->dev)) {
  2405. pr_err_ratelimited("%s: swrm or dev is NULL\n", __func__);
  2406. return -EINVAL;
  2407. }
  2408. switch (action) {
  2409. case MSM_AUD_DC_EVENT:
  2410. schedule_work(&(swrm->dc_presence_work));
  2411. break;
  2412. case SWR_WAKE_IRQ_EVENT:
  2413. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  2414. swrm->ipc_wakeup_triggered = true;
  2415. pm_stay_awake(swrm->dev);
  2416. schedule_work(&swrm->wakeup_work);
  2417. }
  2418. break;
  2419. default:
  2420. dev_err_ratelimited(swrm->dev, "%s: invalid event type: %lu\n",
  2421. __func__, action);
  2422. return -EINVAL;
  2423. }
  2424. return 0;
  2425. }
  2426. static void swrm_notify_work_fn(struct work_struct *work)
  2427. {
  2428. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  2429. dc_presence_work);
  2430. if (!swrm || !swrm->pdev) {
  2431. pr_err_ratelimited("%s: swrm or pdev is NULL\n", __func__);
  2432. return;
  2433. }
  2434. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  2435. }
  2436. static int swrm_probe(struct platform_device *pdev)
  2437. {
  2438. struct swr_mstr_ctrl *swrm;
  2439. struct swr_ctrl_platform_data *pdata;
  2440. u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
  2441. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  2442. int ret = 0;
  2443. struct clk *lpass_core_hw_vote = NULL;
  2444. struct clk *lpass_core_audio = NULL;
  2445. u32 swrm_hw_ver = 0;
  2446. /* Allocate soundwire master driver structure */
  2447. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  2448. GFP_KERNEL);
  2449. if (!swrm) {
  2450. ret = -ENOMEM;
  2451. goto err_memory_fail;
  2452. }
  2453. swrm->pdev = pdev;
  2454. swrm->dev = &pdev->dev;
  2455. platform_set_drvdata(pdev, swrm);
  2456. swr_set_ctrl_data(&swrm->master, swrm);
  2457. pdata = dev_get_platdata(&pdev->dev);
  2458. if (!pdata) {
  2459. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  2460. __func__);
  2461. ret = -EINVAL;
  2462. goto err_pdata_fail;
  2463. }
  2464. swrm->handle = (void *)pdata->handle;
  2465. if (!swrm->handle) {
  2466. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  2467. __func__);
  2468. ret = -EINVAL;
  2469. goto err_pdata_fail;
  2470. }
  2471. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-master-ee-val",
  2472. &swrm->ee_val);
  2473. if (ret) {
  2474. dev_dbg(&pdev->dev,
  2475. "%s: ee_val not specified, initialize with default val\n",
  2476. __func__);
  2477. swrm->ee_val = 0x1;
  2478. }
  2479. ret = of_property_read_u32(pdev->dev.of_node,
  2480. "qcom,swr-master-version",
  2481. &swrm->version);
  2482. if (ret) {
  2483. dev_dbg(&pdev->dev, "%s: swrm version not defined, use default\n",
  2484. __func__);
  2485. swrm->version = SWRM_VERSION_2_0;
  2486. }
  2487. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  2488. &swrm->master_id);
  2489. if (ret) {
  2490. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  2491. goto err_pdata_fail;
  2492. }
  2493. ret = of_property_read_u32(pdev->dev.of_node, "qcom,dynamic-port-map-supported",
  2494. &swrm->dynamic_port_map_supported);
  2495. if (ret) {
  2496. dev_dbg(&pdev->dev,
  2497. "%s: failed to get dynamic port map support, use default\n",
  2498. __func__);
  2499. swrm->dynamic_port_map_supported = 1;
  2500. }
  2501. if (!(of_property_read_u32(pdev->dev.of_node,
  2502. "swrm-io-base", &swrm->swrm_base_reg)))
  2503. ret = of_property_read_u32(pdev->dev.of_node,
  2504. "swrm-io-base", &swrm->swrm_base_reg);
  2505. if (!swrm->swrm_base_reg) {
  2506. swrm->read = pdata->read;
  2507. if (!swrm->read) {
  2508. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  2509. __func__);
  2510. ret = -EINVAL;
  2511. goto err_pdata_fail;
  2512. }
  2513. swrm->write = pdata->write;
  2514. if (!swrm->write) {
  2515. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  2516. __func__);
  2517. ret = -EINVAL;
  2518. goto err_pdata_fail;
  2519. }
  2520. swrm->bulk_write = pdata->bulk_write;
  2521. if (!swrm->bulk_write) {
  2522. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  2523. __func__);
  2524. ret = -EINVAL;
  2525. goto err_pdata_fail;
  2526. }
  2527. } else {
  2528. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  2529. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  2530. }
  2531. swrm->core_vote = pdata->core_vote;
  2532. if (!(of_property_read_u32(pdev->dev.of_node,
  2533. "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
  2534. swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
  2535. swrm_hctl_reg, 0x4);
  2536. swrm->clk = pdata->clk;
  2537. if (!swrm->clk) {
  2538. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  2539. __func__);
  2540. ret = -EINVAL;
  2541. goto err_pdata_fail;
  2542. }
  2543. if (of_property_read_u32(pdev->dev.of_node,
  2544. "qcom,swr-clock-stop-mode0",
  2545. &swrm->clk_stop_mode0_supp)) {
  2546. swrm->clk_stop_mode0_supp = FALSE;
  2547. }
  2548. /* Parse soundwire port mapping */
  2549. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  2550. &num_ports);
  2551. if (ret) {
  2552. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  2553. goto err_pdata_fail;
  2554. }
  2555. swrm->num_ports = num_ports;
  2556. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  2557. &map_size)) {
  2558. dev_err(swrm->dev, "missing port mapping\n");
  2559. goto err_pdata_fail;
  2560. }
  2561. swrm->pcm_enable_count = 0;
  2562. map_length = map_size / (3 * sizeof(u32));
  2563. if (num_ports > SWR_MSTR_PORT_LEN) {
  2564. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  2565. __func__);
  2566. ret = -EINVAL;
  2567. goto err_pdata_fail;
  2568. }
  2569. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  2570. if (!temp) {
  2571. ret = -ENOMEM;
  2572. goto err_pdata_fail;
  2573. }
  2574. ret = of_property_read_u32_array(pdev->dev.of_node,
  2575. "qcom,swr-port-mapping", temp, 3 * map_length);
  2576. if (ret) {
  2577. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  2578. __func__);
  2579. goto err_pdata_fail;
  2580. }
  2581. for (i = 0; i < map_length; i++) {
  2582. port_num = temp[3 * i];
  2583. port_type = temp[3 * i + 1];
  2584. ch_mask = temp[3 * i + 2];
  2585. if (port_num != old_port_num)
  2586. ch_iter = 0;
  2587. if (port_num > SWR_MSTR_PORT_LEN ||
  2588. ch_iter >= SWR_MAX_CH_PER_PORT) {
  2589. dev_err(&pdev->dev,
  2590. "%s:invalid port_num %d or ch_iter %d\n",
  2591. __func__, port_num, ch_iter);
  2592. goto err_pdata_fail;
  2593. }
  2594. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  2595. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  2596. old_port_num = port_num;
  2597. }
  2598. devm_kfree(&pdev->dev, temp);
  2599. ret = of_property_read_u32(pdev->dev.of_node, "qcom,is-always-on",
  2600. &swrm->is_always_on);
  2601. if (ret)
  2602. dev_dbg(&pdev->dev, "%s: failed to get is_always_on flag\n", __func__);
  2603. swrm->reg_irq = pdata->reg_irq;
  2604. swrm->master.read = swrm_read;
  2605. swrm->master.write = swrm_write;
  2606. swrm->master.bulk_write = swrm_bulk_write;
  2607. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  2608. swrm->master.init_port_params = swrm_init_port_params;
  2609. swrm->master.connect_port = swrm_connect_port;
  2610. swrm->master.disconnect_port = swrm_disconnect_port;
  2611. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  2612. swrm->master.remove_from_group = swrm_remove_from_group;
  2613. swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
  2614. swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
  2615. swrm->master.dev.parent = &pdev->dev;
  2616. swrm->master.dev.of_node = pdev->dev.of_node;
  2617. swrm->master.num_port = 0;
  2618. swrm->rcmd_id = 0;
  2619. swrm->wcmd_id = 0;
  2620. swrm->cmd_id = 0;
  2621. swrm->slave_status = 0;
  2622. swrm->num_rx_chs = 0;
  2623. swrm->clk_ref_count = 0;
  2624. swrm->swr_irq_wakeup_capable = 0;
  2625. swrm->mclk_freq = MCLK_FREQ;
  2626. swrm->bus_clk = MCLK_FREQ;
  2627. swrm->dev_up = true;
  2628. swrm->state = SWR_MSTR_UP;
  2629. swrm->ipc_wakeup = false;
  2630. swrm->enable_slave_irq = false;
  2631. swrm->clk_stop_wakeup = false;
  2632. swrm->ipc_wakeup_triggered = false;
  2633. swrm->disable_div2_clk_switch = FALSE;
  2634. init_completion(&swrm->reset);
  2635. init_completion(&swrm->broadcast);
  2636. init_completion(&swrm->clk_off_complete);
  2637. mutex_init(&swrm->irq_lock);
  2638. mutex_init(&swrm->mlock);
  2639. mutex_init(&swrm->reslock);
  2640. mutex_init(&swrm->force_down_lock);
  2641. mutex_init(&swrm->iolock);
  2642. mutex_init(&swrm->clklock);
  2643. mutex_init(&swrm->devlock);
  2644. mutex_init(&swrm->pm_lock);
  2645. mutex_init(&swrm->runtime_lock);
  2646. swrm->wlock_holders = 0;
  2647. swrm->pm_state = SWRM_PM_SLEEPABLE;
  2648. init_waitqueue_head(&swrm->pm_wq);
  2649. cpu_latency_qos_add_request(&swrm->pm_qos_req,
  2650. PM_QOS_DEFAULT_VALUE);
  2651. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++) {
  2652. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  2653. if (swrm->master_id == MASTER_ID_TX) {
  2654. swrm->mport_cfg[i].sinterval = 0xFFFF;
  2655. swrm->mport_cfg[i].offset1 = 0x00;
  2656. swrm->mport_cfg[i].offset2 = 0x00;
  2657. swrm->mport_cfg[i].hstart = 0xFF;
  2658. swrm->mport_cfg[i].hstop = 0xFF;
  2659. swrm->mport_cfg[i].blk_pack_mode = 0xFF;
  2660. swrm->mport_cfg[i].blk_grp_count = 0xFF;
  2661. swrm->mport_cfg[i].word_length = 0xFF;
  2662. swrm->mport_cfg[i].lane_ctrl = 0x00;
  2663. swrm->mport_cfg[i].dir = 0x00;
  2664. swrm->mport_cfg[i].stream_type = 0x00;
  2665. }
  2666. }
  2667. if (of_property_read_u32(pdev->dev.of_node,
  2668. "qcom,disable-div2-clk-switch",
  2669. &swrm->disable_div2_clk_switch)) {
  2670. swrm->disable_div2_clk_switch = FALSE;
  2671. }
  2672. /* Register LPASS core hw vote */
  2673. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  2674. if (IS_ERR(lpass_core_hw_vote)) {
  2675. ret = PTR_ERR(lpass_core_hw_vote);
  2676. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2677. __func__, "lpass_core_hw_vote", ret);
  2678. lpass_core_hw_vote = NULL;
  2679. ret = 0;
  2680. }
  2681. swrm->lpass_core_hw_vote = lpass_core_hw_vote;
  2682. /* Register LPASS audio core vote */
  2683. lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  2684. if (IS_ERR(lpass_core_audio)) {
  2685. ret = PTR_ERR(lpass_core_audio);
  2686. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2687. __func__, "lpass_core_audio", ret);
  2688. lpass_core_audio = NULL;
  2689. ret = 0;
  2690. }
  2691. swrm->lpass_core_audio = lpass_core_audio;
  2692. if (swrm->reg_irq) {
  2693. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  2694. SWR_IRQ_REGISTER);
  2695. if (ret) {
  2696. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  2697. __func__, ret);
  2698. goto err_irq_fail;
  2699. }
  2700. } else {
  2701. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  2702. if (swrm->irq < 0) {
  2703. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  2704. __func__, swrm->irq);
  2705. goto err_irq_fail;
  2706. }
  2707. ret = request_threaded_irq(swrm->irq, NULL,
  2708. swr_mstr_interrupt,
  2709. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  2710. "swr_master_irq", swrm);
  2711. if (ret) {
  2712. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  2713. __func__, ret);
  2714. goto err_irq_fail;
  2715. }
  2716. }
  2717. /* Make inband tx interrupts as wakeup capable for slave irq */
  2718. ret = of_property_read_u32(pdev->dev.of_node,
  2719. "qcom,swr-mstr-irq-wakeup-capable",
  2720. &swrm->swr_irq_wakeup_capable);
  2721. if (ret)
  2722. dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
  2723. __func__);
  2724. if (swrm->swr_irq_wakeup_capable) {
  2725. irq_set_irq_wake(swrm->irq, 1);
  2726. ret = device_init_wakeup(swrm->dev, true);
  2727. if (ret)
  2728. dev_info(swrm->dev,
  2729. "%s: Device wakeup init failed: %d\n",
  2730. __func__, ret);
  2731. }
  2732. ret = swr_register_master(&swrm->master);
  2733. if (ret) {
  2734. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  2735. goto err_mstr_fail;
  2736. }
  2737. /* Add devices registered with board-info as the
  2738. * controller will be up now
  2739. */
  2740. swr_master_add_boarddevices(&swrm->master);
  2741. if (!swrm->is_always_on && swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2742. dev_dbg(&pdev->dev, "%s: Audio HW Vote is failed\n", __func__);
  2743. mutex_lock(&swrm->mlock);
  2744. swrm_clk_request(swrm, true);
  2745. swrm->rd_fifo_depth = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2746. & SWRM_COMP_PARAMS_RD_FIFO_DEPTH) >> 15);
  2747. swrm->wr_fifo_depth = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2748. & SWRM_COMP_PARAMS_WR_FIFO_DEPTH) >> 10);
  2749. swrm_hw_ver = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  2750. if (swrm->version != swrm_hw_ver) {
  2751. dev_info(&pdev->dev,
  2752. "%s: version specified in dtsi: 0x%x not match with HW read version 0x%x\n",
  2753. __func__, swrm->version, swrm_hw_ver);
  2754. swrm->version = swrm_hw_ver;
  2755. }
  2756. swrm->num_auto_enum = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2757. & SWRM_COMP_PARAMS_AUTO_ENUM_SLAVES) >> 20);
  2758. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  2759. &swrm->num_dev);
  2760. if (ret) {
  2761. dev_err(&pdev->dev, "%s: Looking up %s property failed\n",
  2762. __func__, "qcom,swr-num-dev");
  2763. mutex_unlock(&swrm->mlock);
  2764. goto err_parse_num_dev;
  2765. } else {
  2766. if (swrm->num_dev > swrm->num_auto_enum) {
  2767. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  2768. __func__, swrm->num_dev,
  2769. swrm->num_auto_enum);
  2770. ret = -EINVAL;
  2771. mutex_unlock(&swrm->mlock);
  2772. goto err_parse_num_dev;
  2773. } else {
  2774. dev_dbg(&pdev->dev,
  2775. "max swr devices expected to attach - %d, supported auto_enum - %d\n",
  2776. swrm->num_dev, swrm->num_auto_enum);
  2777. }
  2778. }
  2779. ret = swrm_master_init(swrm);
  2780. if (ret < 0) {
  2781. dev_err(&pdev->dev,
  2782. "%s: Error in master Initialization , err %d\n",
  2783. __func__, ret);
  2784. mutex_unlock(&swrm->mlock);
  2785. ret = -EPROBE_DEFER;
  2786. goto err_mstr_init_fail;
  2787. }
  2788. mutex_unlock(&swrm->mlock);
  2789. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  2790. if (pdev->dev.of_node)
  2791. of_register_swr_devices(&swrm->master);
  2792. #ifdef CONFIG_DEBUG_FS
  2793. swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  2794. if (!IS_ERR(swrm->debugfs_swrm_dent)) {
  2795. swrm->debugfs_peek = debugfs_create_file("swrm_peek",
  2796. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2797. (void *) swrm, &swrm_debug_read_ops);
  2798. swrm->debugfs_poke = debugfs_create_file("swrm_poke",
  2799. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2800. (void *) swrm, &swrm_debug_write_ops);
  2801. swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  2802. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2803. (void *) swrm,
  2804. &swrm_debug_dump_ops);
  2805. }
  2806. #endif
  2807. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  2808. pm_runtime_use_autosuspend(&pdev->dev);
  2809. pm_runtime_set_active(&pdev->dev);
  2810. pm_runtime_enable(&pdev->dev);
  2811. pm_runtime_mark_last_busy(&pdev->dev);
  2812. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  2813. swrm->event_notifier.notifier_call = swrm_event_notify;
  2814. //msm_aud_evt_register_client(&swrm->event_notifier);
  2815. return 0;
  2816. err_parse_num_dev:
  2817. err_mstr_init_fail:
  2818. swr_unregister_master(&swrm->master);
  2819. device_init_wakeup(swrm->dev, false);
  2820. err_mstr_fail:
  2821. if (swrm->reg_irq) {
  2822. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  2823. swrm, SWR_IRQ_FREE);
  2824. } else if (swrm->irq) {
  2825. if (irq_get_irq_data(swrm->irq) != NULL)
  2826. irqd_set_trigger_type(
  2827. irq_get_irq_data(swrm->irq),
  2828. IRQ_TYPE_NONE);
  2829. if (swrm->swr_irq_wakeup_capable)
  2830. irq_set_irq_wake(swrm->irq, 0);
  2831. free_irq(swrm->irq, swrm);
  2832. }
  2833. err_irq_fail:
  2834. mutex_destroy(&swrm->irq_lock);
  2835. mutex_destroy(&swrm->mlock);
  2836. mutex_destroy(&swrm->reslock);
  2837. mutex_destroy(&swrm->force_down_lock);
  2838. mutex_destroy(&swrm->iolock);
  2839. mutex_destroy(&swrm->clklock);
  2840. mutex_destroy(&swrm->pm_lock);
  2841. mutex_destroy(&swrm->runtime_lock);
  2842. cpu_latency_qos_remove_request(&swrm->pm_qos_req);
  2843. err_pdata_fail:
  2844. err_memory_fail:
  2845. return ret;
  2846. }
  2847. static int swrm_remove(struct platform_device *pdev)
  2848. {
  2849. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2850. if (swrm->reg_irq) {
  2851. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  2852. swrm, SWR_IRQ_FREE);
  2853. } else if (swrm->irq) {
  2854. if (irq_get_irq_data(swrm->irq) != NULL)
  2855. irqd_set_trigger_type(
  2856. irq_get_irq_data(swrm->irq),
  2857. IRQ_TYPE_NONE);
  2858. if (swrm->swr_irq_wakeup_capable) {
  2859. irq_set_irq_wake(swrm->irq, 0);
  2860. device_init_wakeup(swrm->dev, false);
  2861. }
  2862. free_irq(swrm->irq, swrm);
  2863. } else if (swrm->wake_irq > 0) {
  2864. free_irq(swrm->wake_irq, swrm);
  2865. }
  2866. cancel_work_sync(&swrm->wakeup_work);
  2867. pm_runtime_disable(&pdev->dev);
  2868. pm_runtime_set_suspended(&pdev->dev);
  2869. swr_unregister_master(&swrm->master);
  2870. //msm_aud_evt_unregister_client(&swrm->event_notifier);
  2871. mutex_destroy(&swrm->irq_lock);
  2872. mutex_destroy(&swrm->mlock);
  2873. mutex_destroy(&swrm->reslock);
  2874. mutex_destroy(&swrm->iolock);
  2875. mutex_destroy(&swrm->clklock);
  2876. mutex_destroy(&swrm->force_down_lock);
  2877. mutex_destroy(&swrm->pm_lock);
  2878. mutex_destroy(&swrm->runtime_lock);
  2879. cpu_latency_qos_remove_request(&swrm->pm_qos_req);
  2880. devm_kfree(&pdev->dev, swrm);
  2881. return 0;
  2882. }
  2883. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  2884. {
  2885. u32 val;
  2886. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  2887. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  2888. SWRM_INTERRUPT_STATUS_MASK);
  2889. val = swr_master_read(swrm, SWRM_MCP_CFG);
  2890. val |= 0x02;
  2891. swr_master_write(swrm, SWRM_MCP_CFG, val);
  2892. return 0;
  2893. }
  2894. #ifdef CONFIG_PM
  2895. static int swrm_runtime_resume(struct device *dev)
  2896. {
  2897. struct platform_device *pdev = to_platform_device(dev);
  2898. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2899. int ret = 0, val = 0;
  2900. bool swrm_clk_req_err = false;
  2901. bool hw_core_err = false, aud_core_err = false;
  2902. struct swr_master *mstr = &swrm->master;
  2903. struct swr_device *swr_dev;
  2904. u32 temp = 0;
  2905. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  2906. __func__, swrm->state);
  2907. mutex_lock(&swrm->runtime_lock);
  2908. mutex_lock(&swrm->reslock);
  2909. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  2910. dev_err_ratelimited(dev, "%s:lpass core hw enable failed\n",
  2911. __func__);
  2912. hw_core_err = true;
  2913. pm_runtime_set_autosuspend_delay(&pdev->dev,
  2914. ERR_AUTO_SUSPEND_TIMER_VAL);
  2915. if (swrm->req_clk_switch)
  2916. swrm->req_clk_switch = false;
  2917. mutex_unlock(&swrm->reslock);
  2918. mutex_unlock(&swrm->runtime_lock);
  2919. return 0;
  2920. }
  2921. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
  2922. dev_err_ratelimited(dev, "%s:lpass audio hw enable failed\n",
  2923. __func__);
  2924. aud_core_err = true;
  2925. }
  2926. if ((swrm->state == SWR_MSTR_DOWN) ||
  2927. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  2928. if (swrm->clk_stop_mode0_supp) {
  2929. if (swrm->wake_irq > 0) {
  2930. if (unlikely(!irq_get_irq_data
  2931. (swrm->wake_irq))) {
  2932. pr_err_ratelimited("%s: irq data is NULL\n",
  2933. __func__);
  2934. mutex_unlock(&swrm->reslock);
  2935. mutex_unlock(&swrm->runtime_lock);
  2936. return IRQ_NONE;
  2937. }
  2938. mutex_lock(&swrm->irq_lock);
  2939. if (!irqd_irq_disabled(irq_get_irq_data(swrm->wake_irq))) {
  2940. irq_set_irq_wake(swrm->wake_irq, 0);
  2941. disable_irq_nosync(swrm->wake_irq);
  2942. }
  2943. mutex_unlock(&swrm->irq_lock);
  2944. }
  2945. if (swrm->ipc_wakeup)
  2946. dev_err_ratelimited(dev, "%s:notifications disabled\n", __func__);
  2947. // msm_aud_evt_blocking_notifier_call_chain(
  2948. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  2949. }
  2950. if (swrm_clk_request(swrm, true)) {
  2951. /*
  2952. * Set autosuspend timer to 1 for
  2953. * master to enter into suspend.
  2954. */
  2955. swrm_clk_req_err = true;
  2956. goto exit;
  2957. }
  2958. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  2959. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2960. ret = swr_device_up(swr_dev);
  2961. if (ret == -ENODEV) {
  2962. dev_dbg(dev,
  2963. "%s slave device up not implemented\n",
  2964. __func__);
  2965. ret = 0;
  2966. } else if (ret) {
  2967. dev_err_ratelimited(dev,
  2968. "%s: failed to wakeup swr dev %d\n",
  2969. __func__, swr_dev->dev_num);
  2970. swrm_clk_request(swrm, false);
  2971. goto exit;
  2972. }
  2973. }
  2974. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2975. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2976. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, 0x01);
  2977. swrm_master_init(swrm);
  2978. /* wait for hw enumeration to complete */
  2979. usleep_range(100, 105);
  2980. if (!swrm_check_link_status(swrm, 0x1))
  2981. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  2982. __func__);
  2983. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, get_cmd_id(swrm),
  2984. SWRS_SCP_INT_STATUS_MASK_1);
  2985. if (swrm->state == SWR_MSTR_SSR) {
  2986. mutex_unlock(&swrm->reslock);
  2987. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  2988. mutex_lock(&swrm->reslock);
  2989. }
  2990. } else {
  2991. if (swrm->swrm_hctl_reg) {
  2992. temp = ioread32(swrm->swrm_hctl_reg);
  2993. temp &= 0xFFFFFFFD;
  2994. iowrite32(temp, swrm->swrm_hctl_reg);
  2995. }
  2996. /*wake up from clock stop*/
  2997. #ifdef CONFIG_SWRM_VER_2P0
  2998. val = 0x01;
  2999. swr_master_write(swrm,
  3000. SWRM_CLK_CTRL(swrm->ee_val), val);
  3001. #else
  3002. if (swrm->version < SWRM_VERSION_1_7)
  3003. val = 0x2;
  3004. else
  3005. val = 0x2 << swrm->ee_val;
  3006. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, val);
  3007. #endif
  3008. /* clear and enable bus clash interrupt */
  3009. swr_master_write(swrm,
  3010. SWRM_INTERRUPT_CLEAR(swrm->ee_val), 0x08);
  3011. swrm->intr_mask |= 0x08;
  3012. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  3013. swrm->intr_mask);
  3014. usleep_range(100, 105);
  3015. if (!swrm_check_link_status(swrm, 0x1))
  3016. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  3017. __func__);
  3018. }
  3019. swrm->state = SWR_MSTR_UP;
  3020. }
  3021. exit:
  3022. if (swrm->is_always_on && !aud_core_err)
  3023. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  3024. if (!hw_core_err)
  3025. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  3026. if (swrm_clk_req_err || aud_core_err || hw_core_err)
  3027. pm_runtime_set_autosuspend_delay(&pdev->dev,
  3028. ERR_AUTO_SUSPEND_TIMER_VAL);
  3029. else
  3030. pm_runtime_set_autosuspend_delay(&pdev->dev,
  3031. auto_suspend_timer);
  3032. if (swrm->req_clk_switch)
  3033. swrm->req_clk_switch = false;
  3034. mutex_unlock(&swrm->reslock);
  3035. mutex_unlock(&swrm->runtime_lock);
  3036. return ret;
  3037. }
  3038. static int swrm_runtime_suspend(struct device *dev)
  3039. {
  3040. struct platform_device *pdev = to_platform_device(dev);
  3041. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3042. int ret = 0;
  3043. bool hw_core_err = false, aud_core_err = false;
  3044. struct swr_master *mstr = &swrm->master;
  3045. struct swr_device *swr_dev;
  3046. int current_state = 0;
  3047. struct irq_data *irq_data = NULL;
  3048. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  3049. __func__, swrm->state);
  3050. if (swrm->state == SWR_MSTR_SSR_RESET) {
  3051. swrm->state = SWR_MSTR_SSR;
  3052. return 0;
  3053. }
  3054. mutex_lock(&swrm->runtime_lock);
  3055. mutex_lock(&swrm->reslock);
  3056. mutex_lock(&swrm->force_down_lock);
  3057. current_state = swrm->state;
  3058. mutex_unlock(&swrm->force_down_lock);
  3059. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  3060. dev_err_ratelimited(dev, "%s:lpass core hw enable failed\n",
  3061. __func__);
  3062. hw_core_err = true;
  3063. }
  3064. if (swrm->is_always_on && swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  3065. aud_core_err = true;
  3066. if ((current_state == SWR_MSTR_UP) ||
  3067. (current_state == SWR_MSTR_SSR)) {
  3068. if ((current_state != SWR_MSTR_SSR) &&
  3069. swrm_is_port_en(&swrm->master)) {
  3070. dev_dbg(dev, "%s ports are enabled\n", __func__);
  3071. ret = -EBUSY;
  3072. goto exit;
  3073. }
  3074. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  3075. dev_err_ratelimited(dev, "%s: clk stop mode not supported or SSR entry\n",
  3076. __func__);
  3077. if (swrm->state == SWR_MSTR_SSR)
  3078. goto chk_lnk_status;
  3079. mutex_unlock(&swrm->reslock);
  3080. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  3081. mutex_lock(&swrm->reslock);
  3082. swrm_clk_pause(swrm);
  3083. swr_master_write(swrm, SWRM_COMP_CFG, 0x00);
  3084. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  3085. ret = swr_device_down(swr_dev);
  3086. if (ret == -ENODEV) {
  3087. dev_dbg_ratelimited(dev,
  3088. "%s slave device down not implemented\n",
  3089. __func__);
  3090. ret = 0;
  3091. } else if (ret) {
  3092. dev_err_ratelimited(dev,
  3093. "%s: failed to shutdown swr dev %d\n",
  3094. __func__, swr_dev->dev_num);
  3095. goto exit;
  3096. }
  3097. }
  3098. } else {
  3099. /* Mask bus clash interrupt */
  3100. swrm->intr_mask &= ~((u32)0x08);
  3101. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  3102. swrm->intr_mask);
  3103. mutex_unlock(&swrm->reslock);
  3104. /* clock stop sequence */
  3105. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  3106. SWRS_SCP_CONTROL);
  3107. mutex_lock(&swrm->reslock);
  3108. usleep_range(100, 105);
  3109. }
  3110. chk_lnk_status:
  3111. if (!swrm_check_link_status(swrm, 0x0))
  3112. dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
  3113. __func__);
  3114. ret = swrm_clk_request(swrm, false);
  3115. if (ret) {
  3116. dev_err_ratelimited(dev, "%s: swrmn clk failed\n", __func__);
  3117. ret = 0;
  3118. goto exit;
  3119. }
  3120. if (swrm->clk_stop_mode0_supp) {
  3121. if (swrm->wake_irq > 0) {
  3122. irq_data = irq_get_irq_data(swrm->wake_irq);
  3123. mutex_lock(&swrm->irq_lock);
  3124. if (irq_data && irqd_irq_disabled(irq_data)) {
  3125. irq_set_irq_wake(swrm->wake_irq, 1);
  3126. enable_irq(swrm->wake_irq);
  3127. }
  3128. mutex_unlock(&swrm->irq_lock);
  3129. } else if (swrm->ipc_wakeup) {
  3130. //msm_aud_evt_blocking_notifier_call_chain(
  3131. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  3132. dev_err_ratelimited(dev, "%s:notifications disabled\n", __func__);
  3133. swrm->ipc_wakeup_triggered = false;
  3134. }
  3135. }
  3136. }
  3137. /* Retain SSR state until resume */
  3138. if (current_state != SWR_MSTR_SSR)
  3139. swrm->state = SWR_MSTR_DOWN;
  3140. exit:
  3141. if (!swrm->is_always_on && swrm->state != SWR_MSTR_UP) {
  3142. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false))
  3143. dev_dbg(dev, "%s:lpass audio hw enable failed\n",
  3144. __func__);
  3145. } else if (swrm->is_always_on && !aud_core_err)
  3146. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  3147. if (!hw_core_err)
  3148. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  3149. mutex_unlock(&swrm->reslock);
  3150. mutex_unlock(&swrm->runtime_lock);
  3151. dev_dbg(dev, "%s: pm_runtime: suspend done state: %d\n",
  3152. __func__, swrm->state);
  3153. pm_runtime_set_autosuspend_delay(dev, auto_suspend_timer);
  3154. return ret;
  3155. }
  3156. #endif /* CONFIG_PM */
  3157. static int swrm_device_suspend(struct device *dev)
  3158. {
  3159. struct platform_device *pdev = to_platform_device(dev);
  3160. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3161. int ret = 0;
  3162. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  3163. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  3164. ret = swrm_runtime_suspend(dev);
  3165. if (!ret) {
  3166. pm_runtime_disable(dev);
  3167. pm_runtime_set_suspended(dev);
  3168. pm_runtime_enable(dev);
  3169. }
  3170. }
  3171. return 0;
  3172. }
  3173. static int swrm_device_down(struct device *dev)
  3174. {
  3175. struct platform_device *pdev = to_platform_device(dev);
  3176. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3177. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  3178. mutex_lock(&swrm->force_down_lock);
  3179. swrm->state = SWR_MSTR_SSR;
  3180. mutex_unlock(&swrm->force_down_lock);
  3181. swrm_device_suspend(dev);
  3182. return 0;
  3183. }
  3184. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  3185. {
  3186. int ret = 0;
  3187. int irq, dir_apps_irq;
  3188. if (!swrm->ipc_wakeup) {
  3189. irq = of_get_named_gpio(swrm->dev->of_node,
  3190. "qcom,swr-wakeup-irq", 0);
  3191. if (gpio_is_valid(irq)) {
  3192. swrm->wake_irq = gpio_to_irq(irq);
  3193. if (swrm->wake_irq < 0) {
  3194. dev_err_ratelimited(swrm->dev,
  3195. "Unable to configure irq\n");
  3196. return swrm->wake_irq;
  3197. }
  3198. } else {
  3199. dir_apps_irq = platform_get_irq_byname(swrm->pdev,
  3200. "swr_wake_irq");
  3201. if (dir_apps_irq < 0) {
  3202. dev_err_ratelimited(swrm->dev,
  3203. "TLMM connect gpio not found\n");
  3204. return -EINVAL;
  3205. }
  3206. swrm->wake_irq = dir_apps_irq;
  3207. }
  3208. mutex_lock(&swrm->irq_lock);
  3209. ret = request_threaded_irq(swrm->wake_irq, NULL,
  3210. swrm_wakeup_interrupt,
  3211. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  3212. "swr_wake_irq", swrm);
  3213. if (ret) {
  3214. dev_err_ratelimited(swrm->dev, "%s: Failed to request irq %d\n",
  3215. __func__, ret);
  3216. mutex_unlock(&swrm->irq_lock);
  3217. return -EINVAL;
  3218. }
  3219. irq_set_irq_wake(swrm->wake_irq, 1);
  3220. mutex_unlock(&swrm->irq_lock);
  3221. }
  3222. return ret;
  3223. }
  3224. static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
  3225. u32 uc, u32 size)
  3226. {
  3227. if (!swrm->port_param) {
  3228. swrm->port_param = devm_kzalloc(dev,
  3229. sizeof(swrm->port_param) * SWR_UC_MAX,
  3230. GFP_KERNEL);
  3231. if (!swrm->port_param)
  3232. return -ENOMEM;
  3233. }
  3234. if (!swrm->port_param[uc]) {
  3235. swrm->port_param[uc] = devm_kcalloc(dev, size,
  3236. sizeof(struct port_params),
  3237. GFP_KERNEL);
  3238. if (!swrm->port_param[uc])
  3239. return -ENOMEM;
  3240. } else {
  3241. dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
  3242. __func__);
  3243. }
  3244. return 0;
  3245. }
  3246. static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
  3247. struct swrm_port_config *port_cfg,
  3248. u32 size)
  3249. {
  3250. int idx;
  3251. struct port_params *params;
  3252. int uc = port_cfg->uc;
  3253. int ret = 0;
  3254. for (idx = 0; idx < size; idx++) {
  3255. params = &((struct port_params *)port_cfg->params)[idx];
  3256. if (!params) {
  3257. dev_err_ratelimited(swrm->dev, "%s: Invalid params\n", __func__);
  3258. ret = -EINVAL;
  3259. break;
  3260. }
  3261. memcpy(&swrm->port_param[uc][idx], params,
  3262. sizeof(struct port_params));
  3263. }
  3264. return ret;
  3265. }
  3266. /**
  3267. * swrm_wcd_notify - parent device can notify to soundwire master through
  3268. * this function
  3269. * @pdev: pointer to platform device structure
  3270. * @id: command id from parent to the soundwire master
  3271. * @data: data from parent device to soundwire master
  3272. */
  3273. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  3274. {
  3275. struct swr_mstr_ctrl *swrm;
  3276. int ret = 0;
  3277. struct swr_master *mstr;
  3278. struct swr_device *swr_dev;
  3279. struct swrm_port_config *port_cfg;
  3280. if (!pdev) {
  3281. pr_err_ratelimited("%s: pdev is NULL\n", __func__);
  3282. return -EINVAL;
  3283. }
  3284. swrm = platform_get_drvdata(pdev);
  3285. if (!swrm) {
  3286. dev_err_ratelimited(&pdev->dev, "%s: swrm is NULL\n", __func__);
  3287. return -EINVAL;
  3288. }
  3289. mstr = &swrm->master;
  3290. switch (id) {
  3291. case SWR_REQ_CLK_SWITCH:
  3292. /* This will put soundwire in clock stop mode and disable the
  3293. * clocks, if there is no active usecase running, so that the
  3294. * next activity on soundwire will request clock from new clock
  3295. * source.
  3296. */
  3297. if (!data) {
  3298. dev_err_ratelimited(swrm->dev, "%s: data is NULL for id:%d\n",
  3299. __func__, id);
  3300. ret = -EINVAL;
  3301. break;
  3302. }
  3303. mutex_lock(&swrm->mlock);
  3304. if (swrm->clk_src != *(int *)data) {
  3305. if (swrm->state == SWR_MSTR_UP) {
  3306. swrm->req_clk_switch = true;
  3307. swrm_device_suspend(&pdev->dev);
  3308. if (swrm->state == SWR_MSTR_UP)
  3309. swrm->req_clk_switch = false;
  3310. }
  3311. swrm->clk_src = *(int *)data;
  3312. }
  3313. mutex_unlock(&swrm->mlock);
  3314. break;
  3315. case SWR_CLK_FREQ:
  3316. if (!data) {
  3317. dev_err_ratelimited(swrm->dev, "%s: data is NULL\n", __func__);
  3318. ret = -EINVAL;
  3319. } else {
  3320. mutex_lock(&swrm->mlock);
  3321. if (swrm->mclk_freq != *(int *)data) {
  3322. dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
  3323. if (swrm->state == SWR_MSTR_DOWN)
  3324. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3325. __func__, swrm->state);
  3326. else {
  3327. swrm->mclk_freq = *(int *)data;
  3328. swrm->bus_clk = swrm->mclk_freq;
  3329. swrm_switch_frame_shape(swrm,
  3330. swrm->bus_clk);
  3331. swrm_device_suspend(&pdev->dev);
  3332. }
  3333. /*
  3334. * add delay to ensure clk release happen
  3335. * if interrupt triggered for clk stop,
  3336. * wait for it to exit
  3337. */
  3338. usleep_range(10000, 10500);
  3339. }
  3340. swrm->mclk_freq = *(int *)data;
  3341. swrm->bus_clk = swrm->mclk_freq;
  3342. mutex_unlock(&swrm->mlock);
  3343. }
  3344. break;
  3345. case SWR_DEVICE_SSR_DOWN:
  3346. mutex_lock(&swrm->mlock);
  3347. mutex_lock(&swrm->devlock);
  3348. swrm->dev_up = false;
  3349. mutex_unlock(&swrm->devlock);
  3350. if (swrm->state == SWR_MSTR_DOWN)
  3351. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3352. __func__, swrm->state);
  3353. else
  3354. swrm_device_down(&pdev->dev);
  3355. mutex_lock(&swrm->devlock);
  3356. if (swrm->hw_core_clk_en)
  3357. digital_cdc_rsc_mgr_hw_vote_disable(
  3358. swrm->lpass_core_hw_vote, swrm->dev);
  3359. swrm->hw_core_clk_en = 0;
  3360. if (swrm->aud_core_clk_en)
  3361. digital_cdc_rsc_mgr_hw_vote_disable(
  3362. swrm->lpass_core_audio, swrm->dev);
  3363. swrm->aud_core_clk_en = 0;
  3364. mutex_unlock(&swrm->devlock);
  3365. mutex_lock(&swrm->reslock);
  3366. swrm->state = SWR_MSTR_SSR;
  3367. mutex_unlock(&swrm->reslock);
  3368. mutex_unlock(&swrm->mlock);
  3369. break;
  3370. case SWR_DEVICE_SSR_UP:
  3371. /* wait for clk voting to be zero */
  3372. reinit_completion(&swrm->clk_off_complete);
  3373. if (swrm->clk_ref_count &&
  3374. !wait_for_completion_timeout(&swrm->clk_off_complete,
  3375. msecs_to_jiffies(500)))
  3376. dev_err_ratelimited(swrm->dev, "%s: clock voting not zero\n",
  3377. __func__);
  3378. if (swrm->state == SWR_MSTR_UP ||
  3379. pm_runtime_autosuspend_expiration(swrm->dev)) {
  3380. swrm->state = SWR_MSTR_SSR_RESET;
  3381. dev_dbg(swrm->dev,
  3382. "%s:suspend swr if active at SSR up\n",
  3383. __func__);
  3384. pm_runtime_set_autosuspend_delay(swrm->dev,
  3385. ERR_AUTO_SUSPEND_TIMER_VAL);
  3386. usleep_range(50000, 50100);
  3387. swrm->state = SWR_MSTR_SSR;
  3388. }
  3389. mutex_lock(&swrm->devlock);
  3390. swrm->dev_up = true;
  3391. mutex_unlock(&swrm->devlock);
  3392. break;
  3393. case SWR_DEVICE_DOWN:
  3394. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  3395. mutex_lock(&swrm->mlock);
  3396. if (swrm->state == SWR_MSTR_DOWN)
  3397. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3398. __func__, swrm->state);
  3399. else
  3400. swrm_device_down(&pdev->dev);
  3401. mutex_unlock(&swrm->mlock);
  3402. break;
  3403. case SWR_DEVICE_UP:
  3404. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  3405. mutex_lock(&swrm->devlock);
  3406. if (!swrm->dev_up) {
  3407. dev_dbg(swrm->dev, "SSR not complete yet\n");
  3408. mutex_unlock(&swrm->devlock);
  3409. return -EBUSY;
  3410. }
  3411. mutex_unlock(&swrm->devlock);
  3412. mutex_lock(&swrm->mlock);
  3413. pm_runtime_mark_last_busy(&pdev->dev);
  3414. pm_runtime_get_sync(&pdev->dev);
  3415. mutex_lock(&swrm->reslock);
  3416. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  3417. ret = swr_reset_device(swr_dev);
  3418. if (ret == -ENODEV) {
  3419. dev_dbg_ratelimited(swrm->dev,
  3420. "%s slave reset not implemented\n",
  3421. __func__);
  3422. ret = 0;
  3423. } else if (ret) {
  3424. dev_err_ratelimited(swrm->dev,
  3425. "%s: failed to reset swr device %d\n",
  3426. __func__, swr_dev->dev_num);
  3427. swrm_clk_request(swrm, false);
  3428. }
  3429. }
  3430. pm_runtime_mark_last_busy(&pdev->dev);
  3431. pm_runtime_put_autosuspend(&pdev->dev);
  3432. mutex_unlock(&swrm->reslock);
  3433. mutex_unlock(&swrm->mlock);
  3434. break;
  3435. case SWR_SET_NUM_RX_CH:
  3436. if (!data) {
  3437. dev_err_ratelimited(swrm->dev, "%s: data is NULL\n", __func__);
  3438. ret = -EINVAL;
  3439. } else {
  3440. mutex_lock(&swrm->mlock);
  3441. swrm->num_rx_chs = *(int *)data;
  3442. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  3443. list_for_each_entry(swr_dev, &mstr->devices,
  3444. dev_list) {
  3445. ret = swr_set_device_group(swr_dev,
  3446. SWR_BROADCAST);
  3447. if (ret)
  3448. dev_err_ratelimited(swrm->dev,
  3449. "%s: set num ch failed\n",
  3450. __func__);
  3451. }
  3452. } else {
  3453. list_for_each_entry(swr_dev, &mstr->devices,
  3454. dev_list) {
  3455. ret = swr_set_device_group(swr_dev,
  3456. SWR_GROUP_NONE);
  3457. if (ret)
  3458. dev_err_ratelimited(swrm->dev,
  3459. "%s: set num ch failed\n",
  3460. __func__);
  3461. }
  3462. }
  3463. mutex_unlock(&swrm->mlock);
  3464. }
  3465. break;
  3466. case SWR_REGISTER_WAKE_IRQ:
  3467. if (!data) {
  3468. dev_err_ratelimited(swrm->dev, "%s: reg wake irq data is NULL\n",
  3469. __func__);
  3470. ret = -EINVAL;
  3471. } else {
  3472. mutex_lock(&swrm->mlock);
  3473. swrm->ipc_wakeup = *(u32 *)data;
  3474. ret = swrm_register_wake_irq(swrm);
  3475. if (ret)
  3476. dev_err_ratelimited(swrm->dev, "%s: register wake_irq failed\n",
  3477. __func__);
  3478. mutex_unlock(&swrm->mlock);
  3479. }
  3480. break;
  3481. case SWR_REGISTER_WAKEUP:
  3482. //msm_aud_evt_blocking_notifier_call_chain(
  3483. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  3484. break;
  3485. case SWR_DEREGISTER_WAKEUP:
  3486. //msm_aud_evt_blocking_notifier_call_chain(
  3487. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  3488. break;
  3489. case SWR_SET_PORT_MAP:
  3490. if (!data) {
  3491. dev_err_ratelimited(swrm->dev, "%s: data is NULL for id=%d\n",
  3492. __func__, id);
  3493. ret = -EINVAL;
  3494. } else {
  3495. mutex_lock(&swrm->mlock);
  3496. port_cfg = (struct swrm_port_config *)data;
  3497. if (!port_cfg->size) {
  3498. ret = -EINVAL;
  3499. goto done;
  3500. }
  3501. ret = swrm_alloc_port_mem(&pdev->dev, swrm,
  3502. port_cfg->uc, port_cfg->size);
  3503. if (!ret)
  3504. swrm_copy_port_config(swrm, port_cfg,
  3505. port_cfg->size);
  3506. done:
  3507. mutex_unlock(&swrm->mlock);
  3508. }
  3509. break;
  3510. default:
  3511. dev_err_ratelimited(swrm->dev, "%s: swr master unknown id %d\n",
  3512. __func__, id);
  3513. break;
  3514. }
  3515. return ret;
  3516. }
  3517. EXPORT_SYMBOL(swrm_wcd_notify);
  3518. /*
  3519. * swrm_pm_cmpxchg:
  3520. * Check old state and exchange with pm new state
  3521. * if old state matches with current state
  3522. *
  3523. * @swrm: pointer to wcd core resource
  3524. * @o: pm old state
  3525. * @n: pm new state
  3526. *
  3527. * Returns old state
  3528. */
  3529. static enum swrm_pm_state swrm_pm_cmpxchg(
  3530. struct swr_mstr_ctrl *swrm,
  3531. enum swrm_pm_state o,
  3532. enum swrm_pm_state n)
  3533. {
  3534. enum swrm_pm_state old;
  3535. if (!swrm)
  3536. return o;
  3537. mutex_lock(&swrm->pm_lock);
  3538. old = swrm->pm_state;
  3539. if (old == o)
  3540. swrm->pm_state = n;
  3541. mutex_unlock(&swrm->pm_lock);
  3542. return old;
  3543. }
  3544. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
  3545. {
  3546. enum swrm_pm_state os;
  3547. /*
  3548. * swrm_{lock/unlock}_sleep will be called by swr irq handler
  3549. * and slave wake up requests..
  3550. *
  3551. * If system didn't resume, we can simply return false so
  3552. * IRQ handler can return without handling IRQ.
  3553. */
  3554. mutex_lock(&swrm->pm_lock);
  3555. if (swrm->wlock_holders++ == 0) {
  3556. dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
  3557. cpu_latency_qos_update_request(&swrm->pm_qos_req,
  3558. CPU_IDLE_LATENCY);
  3559. pm_stay_awake(swrm->dev);
  3560. }
  3561. mutex_unlock(&swrm->pm_lock);
  3562. if (!wait_event_timeout(swrm->pm_wq,
  3563. ((os = swrm_pm_cmpxchg(swrm,
  3564. SWRM_PM_SLEEPABLE,
  3565. SWRM_PM_AWAKE)) ==
  3566. SWRM_PM_SLEEPABLE ||
  3567. (os == SWRM_PM_AWAKE)),
  3568. msecs_to_jiffies(
  3569. SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
  3570. dev_err_ratelimited(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
  3571. __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
  3572. swrm->wlock_holders);
  3573. swrm_unlock_sleep(swrm);
  3574. return false;
  3575. }
  3576. wake_up_all(&swrm->pm_wq);
  3577. return true;
  3578. }
  3579. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
  3580. {
  3581. mutex_lock(&swrm->pm_lock);
  3582. if (--swrm->wlock_holders == 0) {
  3583. dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
  3584. __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
  3585. /*
  3586. * if swrm_lock_sleep failed, pm_state would be still
  3587. * swrm_PM_ASLEEP, don't overwrite
  3588. */
  3589. if (likely(swrm->pm_state == SWRM_PM_AWAKE))
  3590. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3591. cpu_latency_qos_update_request(&swrm->pm_qos_req,
  3592. PM_QOS_DEFAULT_VALUE);
  3593. pm_relax(swrm->dev);
  3594. }
  3595. mutex_unlock(&swrm->pm_lock);
  3596. wake_up_all(&swrm->pm_wq);
  3597. }
  3598. #ifdef CONFIG_PM_SLEEP
  3599. static int swrm_suspend(struct device *dev)
  3600. {
  3601. int ret = -EBUSY;
  3602. struct platform_device *pdev = to_platform_device(dev);
  3603. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3604. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  3605. mutex_lock(&swrm->pm_lock);
  3606. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  3607. dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
  3608. __func__, swrm->pm_state,
  3609. swrm->wlock_holders);
  3610. /*
  3611. * before updating the pm_state to ASLEEP, check if device is
  3612. * runtime suspended or not. If it is not, then first make it
  3613. * runtime suspend, and then update the pm_state to ASLEEP.
  3614. */
  3615. mutex_unlock(&swrm->pm_lock); /* release pm_lock before dev suspend */
  3616. swrm_device_suspend(swrm->dev); /* runtime suspend the device */
  3617. mutex_lock(&swrm->pm_lock); /* acquire pm_lock and update state */
  3618. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  3619. swrm->pm_state = SWRM_PM_ASLEEP;
  3620. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  3621. ret = -EBUSY;
  3622. mutex_unlock(&swrm->pm_lock);
  3623. goto check_ebusy;
  3624. }
  3625. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  3626. /*
  3627. * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
  3628. * then set to SWRM_PM_ASLEEP
  3629. */
  3630. dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
  3631. __func__, swrm->pm_state,
  3632. swrm->wlock_holders);
  3633. mutex_unlock(&swrm->pm_lock);
  3634. if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
  3635. swrm, SWRM_PM_SLEEPABLE,
  3636. SWRM_PM_ASLEEP) ==
  3637. SWRM_PM_SLEEPABLE,
  3638. msecs_to_jiffies(
  3639. SWRM_SYS_SUSPEND_WAIT)))) {
  3640. dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
  3641. __func__, swrm->pm_state,
  3642. swrm->wlock_holders);
  3643. return 0;
  3644. } else {
  3645. dev_dbg(swrm->dev,
  3646. "%s: done, state %d, wlock %d\n",
  3647. __func__, swrm->pm_state,
  3648. swrm->wlock_holders);
  3649. }
  3650. mutex_lock(&swrm->pm_lock);
  3651. } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3652. dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
  3653. __func__, swrm->pm_state,
  3654. swrm->wlock_holders);
  3655. }
  3656. mutex_unlock(&swrm->pm_lock);
  3657. if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
  3658. ret = swrm_runtime_suspend(dev);
  3659. if (!ret) {
  3660. /*
  3661. * Synchronize runtime-pm and system-pm states:
  3662. * At this point, we are already suspended. If
  3663. * runtime-pm still thinks its active, then
  3664. * make sure its status is in sync with HW
  3665. * status. The three below calls let the
  3666. * runtime-pm know that we are suspended
  3667. * already without re-invoking the suspend
  3668. * callback
  3669. */
  3670. pm_runtime_disable(dev);
  3671. pm_runtime_set_suspended(dev);
  3672. pm_runtime_enable(dev);
  3673. }
  3674. }
  3675. check_ebusy:
  3676. if (ret == -EBUSY) {
  3677. /*
  3678. * There is a possibility that some audio stream is active
  3679. * during suspend. We dont want to return suspend failure in
  3680. * that case so that display and relevant components can still
  3681. * go to suspend.
  3682. * If there is some other error, then it should be passed-on
  3683. * to system level suspend
  3684. */
  3685. ret = 0;
  3686. }
  3687. return ret;
  3688. }
  3689. static int swrm_resume(struct device *dev)
  3690. {
  3691. int ret = 0;
  3692. struct platform_device *pdev = to_platform_device(dev);
  3693. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3694. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  3695. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  3696. ret = swrm_runtime_resume(dev);
  3697. if (!ret) {
  3698. pm_runtime_mark_last_busy(dev);
  3699. pm_request_autosuspend(dev);
  3700. }
  3701. }
  3702. mutex_lock(&swrm->pm_lock);
  3703. if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3704. dev_dbg(swrm->dev,
  3705. "%s: resuming system, state %d, wlock %d\n",
  3706. __func__, swrm->pm_state,
  3707. swrm->wlock_holders);
  3708. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3709. } else {
  3710. dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
  3711. __func__, swrm->pm_state,
  3712. swrm->wlock_holders);
  3713. }
  3714. mutex_unlock(&swrm->pm_lock);
  3715. wake_up_all(&swrm->pm_wq);
  3716. return ret;
  3717. }
  3718. #endif /* CONFIG_PM_SLEEP */
  3719. static const struct dev_pm_ops swrm_dev_pm_ops = {
  3720. SET_SYSTEM_SLEEP_PM_OPS(
  3721. swrm_suspend,
  3722. swrm_resume
  3723. )
  3724. SET_RUNTIME_PM_OPS(
  3725. swrm_runtime_suspend,
  3726. swrm_runtime_resume,
  3727. NULL
  3728. )
  3729. };
  3730. static const struct of_device_id swrm_dt_match[] = {
  3731. {
  3732. .compatible = "qcom,swr-mstr",
  3733. },
  3734. {}
  3735. };
  3736. static struct platform_driver swr_mstr_driver = {
  3737. .probe = swrm_probe,
  3738. .remove = swrm_remove,
  3739. .driver = {
  3740. .name = SWR_WCD_NAME,
  3741. .owner = THIS_MODULE,
  3742. .pm = &swrm_dev_pm_ops,
  3743. .of_match_table = swrm_dt_match,
  3744. .suppress_bind_attrs = true,
  3745. },
  3746. };
  3747. static int __init swrm_init(void)
  3748. {
  3749. return platform_driver_register(&swr_mstr_driver);
  3750. }
  3751. module_init(swrm_init);
  3752. static void __exit swrm_exit(void)
  3753. {
  3754. platform_driver_unregister(&swr_mstr_driver);
  3755. }
  3756. module_exit(swrm_exit);
  3757. MODULE_LICENSE("GPL v2");
  3758. MODULE_DESCRIPTION("SoundWire Master Controller");
  3759. MODULE_ALIAS("platform:swr-mstr");