swr-mstr-ctrl.c 94 KB

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