swr-mstr-ctrl.c 73 KB

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