swr-mstr-ctrl.c 89 KB

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