swr-mstr-ctrl.c 93 KB

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