swr-mstr-ctrl.c 74 KB

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