swr-mstr-ctrl.c 110 KB

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