swr-mstr-ctrl.c 97 KB

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