swr-mstr-ctrl.c 90 KB

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