swr-mstr-ctrl.c 68 KB

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