swr-mstr-ctrl.c 102 KB

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