swr-mstr-ctrl.c 107 KB

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