swr-mstr-ctrl.c 93 KB

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