swr-mstr-ctrl.c 92 KB

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