swr-mstr-ctrl.c 90 KB

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