swr-mstr-ctrl.c 100 KB

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