swr-mstr-ctrl.c 110 KB

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