swr-mstr-ctrl.c 95 KB

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