swr-mstr-ctrl.c 106 KB

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