stream.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-18 Intel Corporation.
  3. /*
  4. * stream.c - SoundWire Bus stream operations.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/slab.h>
  12. #include <linux/soundwire/sdw_registers.h>
  13. #include <linux/soundwire/sdw.h>
  14. #include "bus.h"
  15. /*
  16. * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
  17. *
  18. * The rows are arranged as per the array index value programmed
  19. * in register. The index 15 has dummy value 0 in order to fill hole.
  20. */
  21. int rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
  22. 96, 100, 120, 128, 150, 160, 250, 0,
  23. 192, 200, 240, 256, 72, 144, 90, 180};
  24. int cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
  25. static int sdw_find_col_index(int col)
  26. {
  27. int i;
  28. for (i = 0; i < SDW_FRAME_COLS; i++) {
  29. if (cols[i] == col)
  30. return i;
  31. }
  32. pr_warn("Requested column not found, selecting lowest column no: 2\n");
  33. return 0;
  34. }
  35. static int sdw_find_row_index(int row)
  36. {
  37. int i;
  38. for (i = 0; i < SDW_FRAME_ROWS; i++) {
  39. if (rows[i] == row)
  40. return i;
  41. }
  42. pr_warn("Requested row not found, selecting lowest row no: 48\n");
  43. return 0;
  44. }
  45. static int _sdw_program_slave_port_params(struct sdw_bus *bus,
  46. struct sdw_slave *slave,
  47. struct sdw_transport_params *t_params,
  48. enum sdw_dpn_type type)
  49. {
  50. u32 addr1, addr2, addr3, addr4;
  51. int ret;
  52. u16 wbuf;
  53. if (bus->params.next_bank) {
  54. addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
  55. addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
  56. addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
  57. addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
  58. } else {
  59. addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
  60. addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
  61. addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
  62. addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
  63. }
  64. /* Program DPN_OffsetCtrl2 registers */
  65. ret = sdw_write(slave, addr1, t_params->offset2);
  66. if (ret < 0) {
  67. dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed");
  68. return ret;
  69. }
  70. /* Program DPN_BlockCtrl3 register */
  71. ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
  72. if (ret < 0) {
  73. dev_err(bus->dev, "DPN_BlockCtrl3 register write failed");
  74. return ret;
  75. }
  76. /*
  77. * Data ports are FULL, SIMPLE and REDUCED. This function handles
  78. * FULL and REDUCED only and and beyond this point only FULL is
  79. * handled, so bail out if we are not FULL data port type
  80. */
  81. if (type != SDW_DPN_FULL)
  82. return ret;
  83. /* Program DPN_SampleCtrl2 register */
  84. wbuf = (t_params->sample_interval - 1);
  85. wbuf &= SDW_DPN_SAMPLECTRL_HIGH;
  86. wbuf >>= SDW_REG_SHIFT(SDW_DPN_SAMPLECTRL_HIGH);
  87. ret = sdw_write(slave, addr3, wbuf);
  88. if (ret < 0) {
  89. dev_err(bus->dev, "DPN_SampleCtrl2 register write failed");
  90. return ret;
  91. }
  92. /* Program DPN_HCtrl register */
  93. wbuf = t_params->hstart;
  94. wbuf <<= SDW_REG_SHIFT(SDW_DPN_HCTRL_HSTART);
  95. wbuf |= t_params->hstop;
  96. ret = sdw_write(slave, addr4, wbuf);
  97. if (ret < 0)
  98. dev_err(bus->dev, "DPN_HCtrl register write failed");
  99. return ret;
  100. }
  101. static int sdw_program_slave_port_params(struct sdw_bus *bus,
  102. struct sdw_slave_runtime *s_rt,
  103. struct sdw_port_runtime *p_rt)
  104. {
  105. struct sdw_transport_params *t_params = &p_rt->transport_params;
  106. struct sdw_port_params *p_params = &p_rt->port_params;
  107. struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
  108. u32 addr1, addr2, addr3, addr4, addr5, addr6;
  109. struct sdw_dpn_prop *dpn_prop;
  110. int ret;
  111. u8 wbuf;
  112. dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
  113. s_rt->direction,
  114. t_params->port_num);
  115. if (!dpn_prop)
  116. return -EINVAL;
  117. addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
  118. addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
  119. if (bus->params.next_bank) {
  120. addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
  121. addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
  122. addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
  123. addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
  124. } else {
  125. addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
  126. addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
  127. addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
  128. addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
  129. }
  130. /* Program DPN_PortCtrl register */
  131. wbuf = p_params->data_mode << SDW_REG_SHIFT(SDW_DPN_PORTCTRL_DATAMODE);
  132. wbuf |= p_params->flow_mode;
  133. ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
  134. if (ret < 0) {
  135. dev_err(&s_rt->slave->dev,
  136. "DPN_PortCtrl register write failed for port %d",
  137. t_params->port_num);
  138. return ret;
  139. }
  140. /* Program DPN_BlockCtrl1 register */
  141. ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
  142. if (ret < 0) {
  143. dev_err(&s_rt->slave->dev,
  144. "DPN_BlockCtrl1 register write failed for port %d",
  145. t_params->port_num);
  146. return ret;
  147. }
  148. /* Program DPN_SampleCtrl1 register */
  149. wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
  150. ret = sdw_write(s_rt->slave, addr3, wbuf);
  151. if (ret < 0) {
  152. dev_err(&s_rt->slave->dev,
  153. "DPN_SampleCtrl1 register write failed for port %d",
  154. t_params->port_num);
  155. return ret;
  156. }
  157. /* Program DPN_OffsetCtrl1 registers */
  158. ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
  159. if (ret < 0) {
  160. dev_err(&s_rt->slave->dev,
  161. "DPN_OffsetCtrl1 register write failed for port %d",
  162. t_params->port_num);
  163. return ret;
  164. }
  165. /* Program DPN_BlockCtrl2 register*/
  166. if (t_params->blk_grp_ctrl_valid) {
  167. ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
  168. if (ret < 0) {
  169. dev_err(&s_rt->slave->dev,
  170. "DPN_BlockCtrl2 reg write failed for port %d",
  171. t_params->port_num);
  172. return ret;
  173. }
  174. }
  175. /* program DPN_LaneCtrl register */
  176. if (slave_prop->lane_control_support) {
  177. ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
  178. if (ret < 0) {
  179. dev_err(&s_rt->slave->dev,
  180. "DPN_LaneCtrl register write failed for port %d",
  181. t_params->port_num);
  182. return ret;
  183. }
  184. }
  185. if (dpn_prop->type != SDW_DPN_SIMPLE) {
  186. ret = _sdw_program_slave_port_params(bus, s_rt->slave,
  187. t_params, dpn_prop->type);
  188. if (ret < 0)
  189. dev_err(&s_rt->slave->dev,
  190. "Transport reg write failed for port: %d",
  191. t_params->port_num);
  192. }
  193. return ret;
  194. }
  195. static int sdw_program_master_port_params(struct sdw_bus *bus,
  196. struct sdw_port_runtime *p_rt)
  197. {
  198. int ret;
  199. /*
  200. * we need to set transport and port parameters for the port.
  201. * Transport parameters refers to the smaple interval, offsets and
  202. * hstart/stop etc of the data. Port parameters refers to word
  203. * length, flow mode etc of the port
  204. */
  205. ret = bus->port_ops->dpn_set_port_transport_params(bus,
  206. &p_rt->transport_params,
  207. bus->params.next_bank);
  208. if (ret < 0)
  209. return ret;
  210. return bus->port_ops->dpn_set_port_params(bus,
  211. &p_rt->port_params,
  212. bus->params.next_bank);
  213. }
  214. /**
  215. * sdw_program_port_params() - Programs transport parameters of Master(s)
  216. * and Slave(s)
  217. *
  218. * @m_rt: Master stream runtime
  219. */
  220. static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
  221. {
  222. struct sdw_slave_runtime *s_rt = NULL;
  223. struct sdw_bus *bus = m_rt->bus;
  224. struct sdw_port_runtime *p_rt;
  225. int ret = 0;
  226. /* Program transport & port parameters for Slave(s) */
  227. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  228. list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
  229. ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. }
  234. /* Program transport & port parameters for Master(s) */
  235. list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
  236. ret = sdw_program_master_port_params(bus, p_rt);
  237. if (ret < 0)
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. /**
  243. * sdw_enable_disable_slave_ports: Enable/disable slave data port
  244. *
  245. * @bus: bus instance
  246. * @s_rt: slave runtime
  247. * @p_rt: port runtime
  248. * @en: enable or disable operation
  249. *
  250. * This function only sets the enable/disable bits in the relevant bank, the
  251. * actual enable/disable is done with a bank switch
  252. */
  253. static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
  254. struct sdw_slave_runtime *s_rt,
  255. struct sdw_port_runtime *p_rt, bool en)
  256. {
  257. struct sdw_transport_params *t_params = &p_rt->transport_params;
  258. u32 addr;
  259. int ret;
  260. if (bus->params.next_bank)
  261. addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
  262. else
  263. addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
  264. /*
  265. * Since bus doesn't support sharing a port across two streams,
  266. * it is safe to reset this register
  267. */
  268. if (en)
  269. ret = sdw_update(s_rt->slave, addr, 0xFF, p_rt->ch_mask);
  270. else
  271. ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
  272. if (ret < 0)
  273. dev_err(&s_rt->slave->dev,
  274. "Slave chn_en reg write failed:%d port:%d",
  275. ret, t_params->port_num);
  276. return ret;
  277. }
  278. static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
  279. struct sdw_port_runtime *p_rt, bool en)
  280. {
  281. struct sdw_transport_params *t_params = &p_rt->transport_params;
  282. struct sdw_bus *bus = m_rt->bus;
  283. struct sdw_enable_ch enable_ch;
  284. int ret = 0;
  285. enable_ch.port_num = p_rt->num;
  286. enable_ch.ch_mask = p_rt->ch_mask;
  287. enable_ch.enable = en;
  288. /* Perform Master port channel(s) enable/disable */
  289. if (bus->port_ops->dpn_port_enable_ch) {
  290. ret = bus->port_ops->dpn_port_enable_ch(bus,
  291. &enable_ch, bus->params.next_bank);
  292. if (ret < 0) {
  293. dev_err(bus->dev,
  294. "Master chn_en write failed:%d port:%d",
  295. ret, t_params->port_num);
  296. return ret;
  297. }
  298. } else {
  299. dev_err(bus->dev,
  300. "dpn_port_enable_ch not supported, %s failed\n",
  301. en ? "enable" : "disable");
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. /**
  307. * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
  308. * Slave(s)
  309. *
  310. * @m_rt: Master stream runtime
  311. * @en: mode (enable/disable)
  312. */
  313. static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
  314. {
  315. struct sdw_port_runtime *s_port, *m_port;
  316. struct sdw_slave_runtime *s_rt = NULL;
  317. int ret = 0;
  318. /* Enable/Disable Slave port(s) */
  319. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  320. list_for_each_entry(s_port, &s_rt->port_list, port_node) {
  321. ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
  322. s_port, en);
  323. if (ret < 0)
  324. return ret;
  325. }
  326. }
  327. /* Enable/Disable Master port(s) */
  328. list_for_each_entry(m_port, &m_rt->port_list, port_node) {
  329. ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
  330. if (ret < 0)
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
  336. struct sdw_prepare_ch prep_ch, enum sdw_port_prep_ops cmd)
  337. {
  338. const struct sdw_slave_ops *ops = s_rt->slave->ops;
  339. int ret;
  340. if (ops->port_prep) {
  341. ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
  342. if (ret < 0) {
  343. dev_err(&s_rt->slave->dev,
  344. "Slave Port Prep cmd %d failed: %d", cmd, ret);
  345. return ret;
  346. }
  347. }
  348. return 0;
  349. }
  350. static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
  351. struct sdw_slave_runtime *s_rt,
  352. struct sdw_port_runtime *p_rt, bool prep)
  353. {
  354. struct completion *port_ready = NULL;
  355. struct sdw_dpn_prop *dpn_prop;
  356. struct sdw_prepare_ch prep_ch;
  357. unsigned int time_left;
  358. bool intr = false;
  359. int ret = 0, val;
  360. u32 addr;
  361. prep_ch.num = p_rt->num;
  362. prep_ch.ch_mask = p_rt->ch_mask;
  363. dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
  364. s_rt->direction,
  365. prep_ch.num);
  366. if (!dpn_prop) {
  367. dev_err(bus->dev,
  368. "Slave Port:%d properties not found", prep_ch.num);
  369. return -EINVAL;
  370. }
  371. prep_ch.prepare = prep;
  372. prep_ch.bank = bus->params.next_bank;
  373. if (dpn_prop->device_interrupts || !dpn_prop->simple_ch_prep_sm)
  374. intr = true;
  375. /*
  376. * Enable interrupt before Port prepare.
  377. * For Port de-prepare, it is assumed that port
  378. * was prepared earlier
  379. */
  380. if (prep && intr) {
  381. ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
  382. dpn_prop->device_interrupts);
  383. if (ret < 0)
  384. return ret;
  385. }
  386. /* Inform slave about the impending port prepare */
  387. sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
  388. /* Prepare Slave port implementing CP_SM */
  389. if (!dpn_prop->simple_ch_prep_sm) {
  390. addr = SDW_DPN_PREPARECTRL(p_rt->num);
  391. if (prep)
  392. ret = sdw_update(s_rt->slave, addr,
  393. 0xFF, p_rt->ch_mask);
  394. else
  395. ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
  396. if (ret < 0) {
  397. dev_err(&s_rt->slave->dev,
  398. "Slave prep_ctrl reg write failed");
  399. return ret;
  400. }
  401. /* Wait for completion on port ready */
  402. port_ready = &s_rt->slave->port_ready[prep_ch.num];
  403. time_left = wait_for_completion_timeout(port_ready,
  404. msecs_to_jiffies(dpn_prop->ch_prep_timeout));
  405. val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
  406. val &= p_rt->ch_mask;
  407. if (!time_left || val) {
  408. dev_err(&s_rt->slave->dev,
  409. "Chn prep failed for port:%d", prep_ch.num);
  410. return -ETIMEDOUT;
  411. }
  412. }
  413. /* Inform slaves about ports prepared */
  414. sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
  415. /* Disable interrupt after Port de-prepare */
  416. if (!prep && intr)
  417. ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
  418. dpn_prop->device_interrupts);
  419. return ret;
  420. }
  421. static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
  422. struct sdw_port_runtime *p_rt, bool prep)
  423. {
  424. struct sdw_transport_params *t_params = &p_rt->transport_params;
  425. struct sdw_bus *bus = m_rt->bus;
  426. const struct sdw_master_port_ops *ops = bus->port_ops;
  427. struct sdw_prepare_ch prep_ch;
  428. int ret = 0;
  429. prep_ch.num = p_rt->num;
  430. prep_ch.ch_mask = p_rt->ch_mask;
  431. prep_ch.prepare = prep; /* Prepare/De-prepare */
  432. prep_ch.bank = bus->params.next_bank;
  433. /* Pre-prepare/Pre-deprepare port(s) */
  434. if (ops->dpn_port_prep) {
  435. ret = ops->dpn_port_prep(bus, &prep_ch);
  436. if (ret < 0) {
  437. dev_err(bus->dev, "Port prepare failed for port:%d",
  438. t_params->port_num);
  439. return ret;
  440. }
  441. }
  442. return ret;
  443. }
  444. /**
  445. * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
  446. * Slave(s)
  447. *
  448. * @m_rt: Master runtime handle
  449. * @prep: Prepare or De-prepare
  450. */
  451. static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
  452. {
  453. struct sdw_slave_runtime *s_rt = NULL;
  454. struct sdw_port_runtime *p_rt;
  455. int ret = 0;
  456. /* Prepare/De-prepare Slave port(s) */
  457. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  458. list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
  459. ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
  460. p_rt, prep);
  461. if (ret < 0)
  462. return ret;
  463. }
  464. }
  465. /* Prepare/De-prepare Master port(s) */
  466. list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
  467. ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
  468. if (ret < 0)
  469. return ret;
  470. }
  471. return ret;
  472. }
  473. /**
  474. * sdw_notify_config() - Notify bus configuration
  475. *
  476. * @m_rt: Master runtime handle
  477. *
  478. * This function notifies the Master(s) and Slave(s) of the
  479. * new bus configuration.
  480. */
  481. static int sdw_notify_config(struct sdw_master_runtime *m_rt)
  482. {
  483. struct sdw_slave_runtime *s_rt;
  484. struct sdw_bus *bus = m_rt->bus;
  485. struct sdw_slave *slave;
  486. int ret = 0;
  487. if (bus->ops->set_bus_conf) {
  488. ret = bus->ops->set_bus_conf(bus, &bus->params);
  489. if (ret < 0)
  490. return ret;
  491. }
  492. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  493. slave = s_rt->slave;
  494. if (slave->ops->bus_config) {
  495. ret = slave->ops->bus_config(slave, &bus->params);
  496. if (ret < 0)
  497. dev_err(bus->dev, "Notify Slave: %d failed",
  498. slave->dev_num);
  499. return ret;
  500. }
  501. }
  502. return ret;
  503. }
  504. /**
  505. * sdw_program_params() - Program transport and port parameters for Master(s)
  506. * and Slave(s)
  507. *
  508. * @bus: SDW bus instance
  509. */
  510. static int sdw_program_params(struct sdw_bus *bus)
  511. {
  512. struct sdw_master_runtime *m_rt = NULL;
  513. int ret = 0;
  514. list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
  515. ret = sdw_program_port_params(m_rt);
  516. if (ret < 0) {
  517. dev_err(bus->dev,
  518. "Program transport params failed: %d", ret);
  519. return ret;
  520. }
  521. ret = sdw_notify_config(m_rt);
  522. if (ret < 0) {
  523. dev_err(bus->dev, "Notify bus config failed: %d", ret);
  524. return ret;
  525. }
  526. /* Enable port(s) on alternate bank for all active streams */
  527. if (m_rt->stream->state != SDW_STREAM_ENABLED)
  528. continue;
  529. ret = sdw_enable_disable_ports(m_rt, true);
  530. if (ret < 0) {
  531. dev_err(bus->dev, "Enable channel failed: %d", ret);
  532. return ret;
  533. }
  534. }
  535. return ret;
  536. }
  537. static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
  538. {
  539. int col_index, row_index;
  540. bool multi_link;
  541. struct sdw_msg *wr_msg;
  542. u8 *wbuf = NULL;
  543. int ret = 0;
  544. u16 addr;
  545. wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
  546. if (!wr_msg)
  547. return -ENOMEM;
  548. bus->defer_msg.msg = wr_msg;
  549. wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
  550. if (!wbuf) {
  551. ret = -ENOMEM;
  552. goto error_1;
  553. }
  554. /* Get row and column index to program register */
  555. col_index = sdw_find_col_index(bus->params.col);
  556. row_index = sdw_find_row_index(bus->params.row);
  557. wbuf[0] = col_index | (row_index << 3);
  558. if (bus->params.next_bank)
  559. addr = SDW_SCP_FRAMECTRL_B1;
  560. else
  561. addr = SDW_SCP_FRAMECTRL_B0;
  562. sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
  563. SDW_MSG_FLAG_WRITE, wbuf);
  564. wr_msg->ssp_sync = true;
  565. /*
  566. * Set the multi_link flag only when both the hardware supports
  567. * and there is a stream handled by multiple masters
  568. */
  569. multi_link = bus->multi_link && (m_rt_count > 1);
  570. if (multi_link)
  571. ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
  572. else
  573. ret = sdw_transfer(bus, wr_msg);
  574. if (ret < 0) {
  575. dev_err(bus->dev, "Slave frame_ctrl reg write failed");
  576. goto error;
  577. }
  578. if (!multi_link) {
  579. kfree(wr_msg);
  580. kfree(wbuf);
  581. bus->defer_msg.msg = NULL;
  582. bus->params.curr_bank = !bus->params.curr_bank;
  583. bus->params.next_bank = !bus->params.next_bank;
  584. }
  585. return 0;
  586. error:
  587. kfree(wbuf);
  588. error_1:
  589. kfree(wr_msg);
  590. return ret;
  591. }
  592. /**
  593. * sdw_ml_sync_bank_switch: Multilink register bank switch
  594. *
  595. * @bus: SDW bus instance
  596. *
  597. * Caller function should free the buffers on error
  598. */
  599. static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
  600. {
  601. unsigned long time_left;
  602. if (!bus->multi_link)
  603. return 0;
  604. /* Wait for completion of transfer */
  605. time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
  606. bus->bank_switch_timeout);
  607. if (!time_left) {
  608. dev_err(bus->dev, "Controller Timed out on bank switch");
  609. return -ETIMEDOUT;
  610. }
  611. bus->params.curr_bank = !bus->params.curr_bank;
  612. bus->params.next_bank = !bus->params.next_bank;
  613. if (bus->defer_msg.msg) {
  614. kfree(bus->defer_msg.msg->buf);
  615. kfree(bus->defer_msg.msg);
  616. }
  617. return 0;
  618. }
  619. static int do_bank_switch(struct sdw_stream_runtime *stream)
  620. {
  621. struct sdw_master_runtime *m_rt = NULL;
  622. const struct sdw_master_ops *ops;
  623. struct sdw_bus *bus = NULL;
  624. bool multi_link = false;
  625. int ret = 0;
  626. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  627. bus = m_rt->bus;
  628. ops = bus->ops;
  629. if (bus->multi_link) {
  630. multi_link = true;
  631. mutex_lock(&bus->msg_lock);
  632. }
  633. /* Pre-bank switch */
  634. if (ops->pre_bank_switch) {
  635. ret = ops->pre_bank_switch(bus);
  636. if (ret < 0) {
  637. dev_err(bus->dev,
  638. "Pre bank switch op failed: %d", ret);
  639. goto msg_unlock;
  640. }
  641. }
  642. /*
  643. * Perform Bank switch operation.
  644. * For multi link cases, the actual bank switch is
  645. * synchronized across all Masters and happens later as a
  646. * part of post_bank_switch ops.
  647. */
  648. ret = sdw_bank_switch(bus, stream->m_rt_count);
  649. if (ret < 0) {
  650. dev_err(bus->dev, "Bank switch failed: %d", ret);
  651. goto error;
  652. }
  653. }
  654. /*
  655. * For multi link cases, it is expected that the bank switch is
  656. * triggered by the post_bank_switch for the first Master in the list
  657. * and for the other Masters the post_bank_switch() should return doing
  658. * nothing.
  659. */
  660. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  661. bus = m_rt->bus;
  662. ops = bus->ops;
  663. /* Post-bank switch */
  664. if (ops->post_bank_switch) {
  665. ret = ops->post_bank_switch(bus);
  666. if (ret < 0) {
  667. dev_err(bus->dev,
  668. "Post bank switch op failed: %d", ret);
  669. goto error;
  670. }
  671. } else if (bus->multi_link && stream->m_rt_count > 1) {
  672. dev_err(bus->dev,
  673. "Post bank switch ops not implemented");
  674. goto error;
  675. }
  676. /* Set the bank switch timeout to default, if not set */
  677. if (!bus->bank_switch_timeout)
  678. bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
  679. /* Check if bank switch was successful */
  680. ret = sdw_ml_sync_bank_switch(bus);
  681. if (ret < 0) {
  682. dev_err(bus->dev,
  683. "multi link bank switch failed: %d", ret);
  684. goto error;
  685. }
  686. mutex_unlock(&bus->msg_lock);
  687. }
  688. return ret;
  689. error:
  690. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  691. bus = m_rt->bus;
  692. kfree(bus->defer_msg.msg->buf);
  693. kfree(bus->defer_msg.msg);
  694. }
  695. msg_unlock:
  696. if (multi_link) {
  697. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  698. bus = m_rt->bus;
  699. if (mutex_is_locked(&bus->msg_lock))
  700. mutex_unlock(&bus->msg_lock);
  701. }
  702. }
  703. return ret;
  704. }
  705. /**
  706. * sdw_release_stream() - Free the assigned stream runtime
  707. *
  708. * @stream: SoundWire stream runtime
  709. *
  710. * sdw_release_stream should be called only once per stream
  711. */
  712. void sdw_release_stream(struct sdw_stream_runtime *stream)
  713. {
  714. kfree(stream);
  715. }
  716. EXPORT_SYMBOL(sdw_release_stream);
  717. /**
  718. * sdw_alloc_stream() - Allocate and return stream runtime
  719. *
  720. * @stream_name: SoundWire stream name
  721. *
  722. * Allocates a SoundWire stream runtime instance.
  723. * sdw_alloc_stream should be called only once per stream. Typically
  724. * invoked from ALSA/ASoC machine/platform driver.
  725. */
  726. struct sdw_stream_runtime *sdw_alloc_stream(char *stream_name)
  727. {
  728. struct sdw_stream_runtime *stream;
  729. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  730. if (!stream)
  731. return NULL;
  732. stream->name = stream_name;
  733. INIT_LIST_HEAD(&stream->master_list);
  734. stream->state = SDW_STREAM_ALLOCATED;
  735. stream->m_rt_count = 0;
  736. return stream;
  737. }
  738. EXPORT_SYMBOL(sdw_alloc_stream);
  739. static struct sdw_master_runtime
  740. *sdw_find_master_rt(struct sdw_bus *bus,
  741. struct sdw_stream_runtime *stream)
  742. {
  743. struct sdw_master_runtime *m_rt = NULL;
  744. /* Retrieve Bus handle if already available */
  745. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  746. if (m_rt->bus == bus)
  747. return m_rt;
  748. }
  749. return NULL;
  750. }
  751. /**
  752. * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
  753. *
  754. * @bus: SDW bus instance
  755. * @stream_config: Stream configuration
  756. * @stream: Stream runtime handle.
  757. *
  758. * This function is to be called with bus_lock held.
  759. */
  760. static struct sdw_master_runtime
  761. *sdw_alloc_master_rt(struct sdw_bus *bus,
  762. struct sdw_stream_config *stream_config,
  763. struct sdw_stream_runtime *stream)
  764. {
  765. struct sdw_master_runtime *m_rt;
  766. /*
  767. * check if Master is already allocated (as a result of Slave adding
  768. * it first), if so skip allocation and go to configure
  769. */
  770. m_rt = sdw_find_master_rt(bus, stream);
  771. if (m_rt)
  772. goto stream_config;
  773. m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
  774. if (!m_rt)
  775. return NULL;
  776. /* Initialization of Master runtime handle */
  777. INIT_LIST_HEAD(&m_rt->port_list);
  778. INIT_LIST_HEAD(&m_rt->slave_rt_list);
  779. list_add_tail(&m_rt->stream_node, &stream->master_list);
  780. list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
  781. stream_config:
  782. m_rt->ch_count = stream_config->ch_count;
  783. m_rt->bus = bus;
  784. m_rt->stream = stream;
  785. m_rt->direction = stream_config->direction;
  786. return m_rt;
  787. }
  788. /**
  789. * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
  790. *
  791. * @slave: Slave handle
  792. * @stream_config: Stream configuration
  793. * @stream: Stream runtime handle
  794. *
  795. * This function is to be called with bus_lock held.
  796. */
  797. static struct sdw_slave_runtime
  798. *sdw_alloc_slave_rt(struct sdw_slave *slave,
  799. struct sdw_stream_config *stream_config,
  800. struct sdw_stream_runtime *stream)
  801. {
  802. struct sdw_slave_runtime *s_rt = NULL;
  803. s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
  804. if (!s_rt)
  805. return NULL;
  806. INIT_LIST_HEAD(&s_rt->port_list);
  807. s_rt->ch_count = stream_config->ch_count;
  808. s_rt->direction = stream_config->direction;
  809. s_rt->slave = slave;
  810. return s_rt;
  811. }
  812. static void sdw_master_port_release(struct sdw_bus *bus,
  813. struct sdw_master_runtime *m_rt)
  814. {
  815. struct sdw_port_runtime *p_rt, *_p_rt;
  816. list_for_each_entry_safe(p_rt, _p_rt,
  817. &m_rt->port_list, port_node) {
  818. list_del(&p_rt->port_node);
  819. kfree(p_rt);
  820. }
  821. }
  822. static void sdw_slave_port_release(struct sdw_bus *bus,
  823. struct sdw_slave *slave,
  824. struct sdw_stream_runtime *stream)
  825. {
  826. struct sdw_port_runtime *p_rt, *_p_rt;
  827. struct sdw_master_runtime *m_rt;
  828. struct sdw_slave_runtime *s_rt;
  829. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  830. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  831. if (s_rt->slave != slave)
  832. continue;
  833. list_for_each_entry_safe(p_rt, _p_rt,
  834. &s_rt->port_list, port_node) {
  835. list_del(&p_rt->port_node);
  836. kfree(p_rt);
  837. }
  838. }
  839. }
  840. }
  841. /**
  842. * sdw_release_slave_stream() - Free Slave(s) runtime handle
  843. *
  844. * @slave: Slave handle.
  845. * @stream: Stream runtime handle.
  846. *
  847. * This function is to be called with bus_lock held.
  848. */
  849. static void sdw_release_slave_stream(struct sdw_slave *slave,
  850. struct sdw_stream_runtime *stream)
  851. {
  852. struct sdw_slave_runtime *s_rt, *_s_rt;
  853. struct sdw_master_runtime *m_rt;
  854. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  855. /* Retrieve Slave runtime handle */
  856. list_for_each_entry_safe(s_rt, _s_rt,
  857. &m_rt->slave_rt_list, m_rt_node) {
  858. if (s_rt->slave == slave) {
  859. list_del(&s_rt->m_rt_node);
  860. kfree(s_rt);
  861. return;
  862. }
  863. }
  864. }
  865. }
  866. /**
  867. * sdw_release_master_stream() - Free Master runtime handle
  868. *
  869. * @m_rt: Master runtime node
  870. * @stream: Stream runtime handle.
  871. *
  872. * This function is to be called with bus_lock held
  873. * It frees the Master runtime handle and associated Slave(s) runtime
  874. * handle. If this is called first then sdw_release_slave_stream() will have
  875. * no effect as Slave(s) runtime handle would already be freed up.
  876. */
  877. static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
  878. struct sdw_stream_runtime *stream)
  879. {
  880. struct sdw_slave_runtime *s_rt, *_s_rt;
  881. list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
  882. sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
  883. sdw_release_slave_stream(s_rt->slave, stream);
  884. }
  885. list_del(&m_rt->stream_node);
  886. list_del(&m_rt->bus_node);
  887. kfree(m_rt);
  888. }
  889. /**
  890. * sdw_stream_remove_master() - Remove master from sdw_stream
  891. *
  892. * @bus: SDW Bus instance
  893. * @stream: SoundWire stream
  894. *
  895. * This removes and frees port_rt and master_rt from a stream
  896. */
  897. int sdw_stream_remove_master(struct sdw_bus *bus,
  898. struct sdw_stream_runtime *stream)
  899. {
  900. struct sdw_master_runtime *m_rt, *_m_rt;
  901. mutex_lock(&bus->bus_lock);
  902. list_for_each_entry_safe(m_rt, _m_rt,
  903. &stream->master_list, stream_node) {
  904. if (m_rt->bus != bus)
  905. continue;
  906. sdw_master_port_release(bus, m_rt);
  907. sdw_release_master_stream(m_rt, stream);
  908. stream->m_rt_count--;
  909. }
  910. if (list_empty(&stream->master_list))
  911. stream->state = SDW_STREAM_RELEASED;
  912. mutex_unlock(&bus->bus_lock);
  913. return 0;
  914. }
  915. EXPORT_SYMBOL(sdw_stream_remove_master);
  916. /**
  917. * sdw_stream_remove_slave() - Remove slave from sdw_stream
  918. *
  919. * @slave: SDW Slave instance
  920. * @stream: SoundWire stream
  921. *
  922. * This removes and frees port_rt and slave_rt from a stream
  923. */
  924. int sdw_stream_remove_slave(struct sdw_slave *slave,
  925. struct sdw_stream_runtime *stream)
  926. {
  927. mutex_lock(&slave->bus->bus_lock);
  928. sdw_slave_port_release(slave->bus, slave, stream);
  929. sdw_release_slave_stream(slave, stream);
  930. mutex_unlock(&slave->bus->bus_lock);
  931. return 0;
  932. }
  933. EXPORT_SYMBOL(sdw_stream_remove_slave);
  934. /**
  935. * sdw_config_stream() - Configure the allocated stream
  936. *
  937. * @dev: SDW device
  938. * @stream: SoundWire stream
  939. * @stream_config: Stream configuration for audio stream
  940. * @is_slave: is API called from Slave or Master
  941. *
  942. * This function is to be called with bus_lock held.
  943. */
  944. static int sdw_config_stream(struct device *dev,
  945. struct sdw_stream_runtime *stream,
  946. struct sdw_stream_config *stream_config, bool is_slave)
  947. {
  948. /*
  949. * Update the stream rate, channel and bps based on data
  950. * source. For more than one data source (multilink),
  951. * match the rate, bps, stream type and increment number of channels.
  952. *
  953. * If rate/bps is zero, it means the values are not set, so skip
  954. * comparison and allow the value to be set and stored in stream
  955. */
  956. if (stream->params.rate &&
  957. stream->params.rate != stream_config->frame_rate) {
  958. dev_err(dev, "rate not matching, stream:%s", stream->name);
  959. return -EINVAL;
  960. }
  961. if (stream->params.bps &&
  962. stream->params.bps != stream_config->bps) {
  963. dev_err(dev, "bps not matching, stream:%s", stream->name);
  964. return -EINVAL;
  965. }
  966. stream->type = stream_config->type;
  967. stream->params.rate = stream_config->frame_rate;
  968. stream->params.bps = stream_config->bps;
  969. /* TODO: Update this check during Device-device support */
  970. if (is_slave)
  971. stream->params.ch_count += stream_config->ch_count;
  972. return 0;
  973. }
  974. static int sdw_is_valid_port_range(struct device *dev,
  975. struct sdw_port_runtime *p_rt)
  976. {
  977. if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
  978. dev_err(dev,
  979. "SoundWire: Invalid port number :%d", p_rt->num);
  980. return -EINVAL;
  981. }
  982. return 0;
  983. }
  984. static struct sdw_port_runtime *sdw_port_alloc(struct device *dev,
  985. struct sdw_port_config *port_config,
  986. int port_index)
  987. {
  988. struct sdw_port_runtime *p_rt;
  989. p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
  990. if (!p_rt)
  991. return NULL;
  992. p_rt->ch_mask = port_config[port_index].ch_mask;
  993. p_rt->num = port_config[port_index].num;
  994. return p_rt;
  995. }
  996. static int sdw_master_port_config(struct sdw_bus *bus,
  997. struct sdw_master_runtime *m_rt,
  998. struct sdw_port_config *port_config,
  999. unsigned int num_ports)
  1000. {
  1001. struct sdw_port_runtime *p_rt;
  1002. int i;
  1003. /* Iterate for number of ports to perform initialization */
  1004. for (i = 0; i < num_ports; i++) {
  1005. p_rt = sdw_port_alloc(bus->dev, port_config, i);
  1006. if (!p_rt)
  1007. return -ENOMEM;
  1008. /*
  1009. * TODO: Check port capabilities for requested
  1010. * configuration (audio mode support)
  1011. */
  1012. list_add_tail(&p_rt->port_node, &m_rt->port_list);
  1013. }
  1014. return 0;
  1015. }
  1016. static int sdw_slave_port_config(struct sdw_slave *slave,
  1017. struct sdw_slave_runtime *s_rt,
  1018. struct sdw_port_config *port_config,
  1019. unsigned int num_config)
  1020. {
  1021. struct sdw_port_runtime *p_rt;
  1022. int i, ret;
  1023. /* Iterate for number of ports to perform initialization */
  1024. for (i = 0; i < num_config; i++) {
  1025. p_rt = sdw_port_alloc(&slave->dev, port_config, i);
  1026. if (!p_rt)
  1027. return -ENOMEM;
  1028. /*
  1029. * TODO: Check valid port range as defined by DisCo/
  1030. * slave
  1031. */
  1032. ret = sdw_is_valid_port_range(&slave->dev, p_rt);
  1033. if (ret < 0) {
  1034. kfree(p_rt);
  1035. return ret;
  1036. }
  1037. /*
  1038. * TODO: Check port capabilities for requested
  1039. * configuration (audio mode support)
  1040. */
  1041. list_add_tail(&p_rt->port_node, &s_rt->port_list);
  1042. }
  1043. return 0;
  1044. }
  1045. /**
  1046. * sdw_stream_add_master() - Allocate and add master runtime to a stream
  1047. *
  1048. * @bus: SDW Bus instance
  1049. * @stream_config: Stream configuration for audio stream
  1050. * @port_config: Port configuration for audio stream
  1051. * @num_ports: Number of ports
  1052. * @stream: SoundWire stream
  1053. */
  1054. int sdw_stream_add_master(struct sdw_bus *bus,
  1055. struct sdw_stream_config *stream_config,
  1056. struct sdw_port_config *port_config,
  1057. unsigned int num_ports,
  1058. struct sdw_stream_runtime *stream)
  1059. {
  1060. struct sdw_master_runtime *m_rt = NULL;
  1061. int ret;
  1062. mutex_lock(&bus->bus_lock);
  1063. /*
  1064. * For multi link streams, add the second master only if
  1065. * the bus supports it.
  1066. * Check if bus->multi_link is set
  1067. */
  1068. if (!bus->multi_link && stream->m_rt_count > 0) {
  1069. dev_err(bus->dev,
  1070. "Multilink not supported, link %d", bus->link_id);
  1071. ret = -EINVAL;
  1072. goto unlock;
  1073. }
  1074. m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
  1075. if (!m_rt) {
  1076. dev_err(bus->dev,
  1077. "Master runtime config failed for stream:%s",
  1078. stream->name);
  1079. ret = -ENOMEM;
  1080. goto unlock;
  1081. }
  1082. ret = sdw_config_stream(bus->dev, stream, stream_config, false);
  1083. if (ret)
  1084. goto stream_error;
  1085. ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
  1086. if (ret)
  1087. goto stream_error;
  1088. stream->m_rt_count++;
  1089. goto unlock;
  1090. stream_error:
  1091. sdw_release_master_stream(m_rt, stream);
  1092. unlock:
  1093. mutex_unlock(&bus->bus_lock);
  1094. return ret;
  1095. }
  1096. EXPORT_SYMBOL(sdw_stream_add_master);
  1097. /**
  1098. * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
  1099. *
  1100. * @slave: SDW Slave instance
  1101. * @stream_config: Stream configuration for audio stream
  1102. * @stream: SoundWire stream
  1103. * @port_config: Port configuration for audio stream
  1104. * @num_ports: Number of ports
  1105. *
  1106. * It is expected that Slave is added before adding Master
  1107. * to the Stream.
  1108. *
  1109. */
  1110. int sdw_stream_add_slave(struct sdw_slave *slave,
  1111. struct sdw_stream_config *stream_config,
  1112. struct sdw_port_config *port_config,
  1113. unsigned int num_ports,
  1114. struct sdw_stream_runtime *stream)
  1115. {
  1116. struct sdw_slave_runtime *s_rt;
  1117. struct sdw_master_runtime *m_rt;
  1118. int ret;
  1119. mutex_lock(&slave->bus->bus_lock);
  1120. /*
  1121. * If this API is invoked by Slave first then m_rt is not valid.
  1122. * So, allocate m_rt and add Slave to it.
  1123. */
  1124. m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
  1125. if (!m_rt) {
  1126. dev_err(&slave->dev,
  1127. "alloc master runtime failed for stream:%s",
  1128. stream->name);
  1129. ret = -ENOMEM;
  1130. goto error;
  1131. }
  1132. s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
  1133. if (!s_rt) {
  1134. dev_err(&slave->dev,
  1135. "Slave runtime config failed for stream:%s",
  1136. stream->name);
  1137. ret = -ENOMEM;
  1138. goto stream_error;
  1139. }
  1140. ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
  1141. if (ret)
  1142. goto stream_error;
  1143. list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
  1144. ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
  1145. if (ret)
  1146. goto stream_error;
  1147. /*
  1148. * Change stream state to CONFIGURED on first Slave add.
  1149. * Bus is not aware of number of Slave(s) in a stream at this
  1150. * point so cannot depend on all Slave(s) to be added in order to
  1151. * change stream state to CONFIGURED.
  1152. */
  1153. stream->state = SDW_STREAM_CONFIGURED;
  1154. goto error;
  1155. stream_error:
  1156. /*
  1157. * we hit error so cleanup the stream, release all Slave(s) and
  1158. * Master runtime
  1159. */
  1160. sdw_release_master_stream(m_rt, stream);
  1161. error:
  1162. mutex_unlock(&slave->bus->bus_lock);
  1163. return ret;
  1164. }
  1165. EXPORT_SYMBOL(sdw_stream_add_slave);
  1166. /**
  1167. * sdw_get_slave_dpn_prop() - Get Slave port capabilities
  1168. *
  1169. * @slave: Slave handle
  1170. * @direction: Data direction.
  1171. * @port_num: Port number
  1172. */
  1173. struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
  1174. enum sdw_data_direction direction,
  1175. unsigned int port_num)
  1176. {
  1177. struct sdw_dpn_prop *dpn_prop;
  1178. u8 num_ports;
  1179. int i;
  1180. if (direction == SDW_DATA_DIR_TX) {
  1181. num_ports = hweight32(slave->prop.source_ports);
  1182. dpn_prop = slave->prop.src_dpn_prop;
  1183. } else {
  1184. num_ports = hweight32(slave->prop.sink_ports);
  1185. dpn_prop = slave->prop.sink_dpn_prop;
  1186. }
  1187. for (i = 0; i < num_ports; i++) {
  1188. dpn_prop = &dpn_prop[i];
  1189. if (dpn_prop->num == port_num)
  1190. return &dpn_prop[i];
  1191. }
  1192. return NULL;
  1193. }
  1194. /**
  1195. * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
  1196. *
  1197. * @stream: SoundWire stream
  1198. *
  1199. * Acquire bus_lock for each of the master runtime(m_rt) part of this
  1200. * stream to reconfigure the bus.
  1201. * NOTE: This function is called from SoundWire stream ops and is
  1202. * expected that a global lock is held before acquiring bus_lock.
  1203. */
  1204. static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
  1205. {
  1206. struct sdw_master_runtime *m_rt = NULL;
  1207. struct sdw_bus *bus = NULL;
  1208. /* Iterate for all Master(s) in Master list */
  1209. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1210. bus = m_rt->bus;
  1211. mutex_lock(&bus->bus_lock);
  1212. }
  1213. }
  1214. /**
  1215. * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
  1216. *
  1217. * @stream: SoundWire stream
  1218. *
  1219. * Release the previously held bus_lock after reconfiguring the bus.
  1220. * NOTE: This function is called from SoundWire stream ops and is
  1221. * expected that a global lock is held before releasing bus_lock.
  1222. */
  1223. static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
  1224. {
  1225. struct sdw_master_runtime *m_rt = NULL;
  1226. struct sdw_bus *bus = NULL;
  1227. /* Iterate for all Master(s) in Master list */
  1228. list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
  1229. bus = m_rt->bus;
  1230. mutex_unlock(&bus->bus_lock);
  1231. }
  1232. }
  1233. static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1234. {
  1235. struct sdw_master_runtime *m_rt = NULL;
  1236. struct sdw_bus *bus = NULL;
  1237. struct sdw_master_prop *prop = NULL;
  1238. struct sdw_bus_params params;
  1239. int ret;
  1240. /* Prepare Master(s) and Slave(s) port(s) associated with stream */
  1241. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1242. bus = m_rt->bus;
  1243. prop = &bus->prop;
  1244. memcpy(&params, &bus->params, sizeof(params));
  1245. /* TODO: Support Asynchronous mode */
  1246. if ((prop->max_freq % stream->params.rate) != 0) {
  1247. dev_err(bus->dev, "Async mode not supported");
  1248. return -EINVAL;
  1249. }
  1250. /* Increment cumulative bus bandwidth */
  1251. /* TODO: Update this during Device-Device support */
  1252. bus->params.bandwidth += m_rt->stream->params.rate *
  1253. m_rt->ch_count * m_rt->stream->params.bps;
  1254. /* Program params */
  1255. ret = sdw_program_params(bus);
  1256. if (ret < 0) {
  1257. dev_err(bus->dev, "Program params failed: %d", ret);
  1258. goto restore_params;
  1259. }
  1260. }
  1261. ret = do_bank_switch(stream);
  1262. if (ret < 0) {
  1263. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1264. goto restore_params;
  1265. }
  1266. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1267. bus = m_rt->bus;
  1268. /* Prepare port(s) on the new clock configuration */
  1269. ret = sdw_prep_deprep_ports(m_rt, true);
  1270. if (ret < 0) {
  1271. dev_err(bus->dev, "Prepare port(s) failed ret = %d",
  1272. ret);
  1273. return ret;
  1274. }
  1275. }
  1276. stream->state = SDW_STREAM_PREPARED;
  1277. return ret;
  1278. restore_params:
  1279. memcpy(&bus->params, &params, sizeof(params));
  1280. return ret;
  1281. }
  1282. /**
  1283. * sdw_prepare_stream() - Prepare SoundWire stream
  1284. *
  1285. * @stream: Soundwire stream
  1286. *
  1287. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1288. */
  1289. int sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1290. {
  1291. int ret = 0;
  1292. if (!stream) {
  1293. pr_err("SoundWire: Handle not found for stream");
  1294. return -EINVAL;
  1295. }
  1296. sdw_acquire_bus_lock(stream);
  1297. ret = _sdw_prepare_stream(stream);
  1298. if (ret < 0)
  1299. pr_err("Prepare for stream:%s failed: %d", stream->name, ret);
  1300. sdw_release_bus_lock(stream);
  1301. return ret;
  1302. }
  1303. EXPORT_SYMBOL(sdw_prepare_stream);
  1304. static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
  1305. {
  1306. struct sdw_master_runtime *m_rt = NULL;
  1307. struct sdw_bus *bus = NULL;
  1308. int ret;
  1309. /* Enable Master(s) and Slave(s) port(s) associated with stream */
  1310. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1311. bus = m_rt->bus;
  1312. /* Program params */
  1313. ret = sdw_program_params(bus);
  1314. if (ret < 0) {
  1315. dev_err(bus->dev, "Program params failed: %d", ret);
  1316. return ret;
  1317. }
  1318. /* Enable port(s) */
  1319. ret = sdw_enable_disable_ports(m_rt, true);
  1320. if (ret < 0) {
  1321. dev_err(bus->dev, "Enable port(s) failed ret: %d", ret);
  1322. return ret;
  1323. }
  1324. }
  1325. ret = do_bank_switch(stream);
  1326. if (ret < 0) {
  1327. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1328. return ret;
  1329. }
  1330. stream->state = SDW_STREAM_ENABLED;
  1331. return 0;
  1332. }
  1333. /**
  1334. * sdw_enable_stream() - Enable SoundWire stream
  1335. *
  1336. * @stream: Soundwire stream
  1337. *
  1338. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1339. */
  1340. int sdw_enable_stream(struct sdw_stream_runtime *stream)
  1341. {
  1342. int ret = 0;
  1343. if (!stream) {
  1344. pr_err("SoundWire: Handle not found for stream");
  1345. return -EINVAL;
  1346. }
  1347. sdw_acquire_bus_lock(stream);
  1348. ret = _sdw_enable_stream(stream);
  1349. if (ret < 0)
  1350. pr_err("Enable for stream:%s failed: %d", stream->name, ret);
  1351. sdw_release_bus_lock(stream);
  1352. return ret;
  1353. }
  1354. EXPORT_SYMBOL(sdw_enable_stream);
  1355. static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
  1356. {
  1357. struct sdw_master_runtime *m_rt = NULL;
  1358. struct sdw_bus *bus = NULL;
  1359. int ret;
  1360. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1361. bus = m_rt->bus;
  1362. /* Disable port(s) */
  1363. ret = sdw_enable_disable_ports(m_rt, false);
  1364. if (ret < 0) {
  1365. dev_err(bus->dev, "Disable port(s) failed: %d", ret);
  1366. return ret;
  1367. }
  1368. }
  1369. stream->state = SDW_STREAM_DISABLED;
  1370. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1371. bus = m_rt->bus;
  1372. /* Program params */
  1373. ret = sdw_program_params(bus);
  1374. if (ret < 0) {
  1375. dev_err(bus->dev, "Program params failed: %d", ret);
  1376. return ret;
  1377. }
  1378. }
  1379. return do_bank_switch(stream);
  1380. }
  1381. /**
  1382. * sdw_disable_stream() - Disable SoundWire stream
  1383. *
  1384. * @stream: Soundwire stream
  1385. *
  1386. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1387. */
  1388. int sdw_disable_stream(struct sdw_stream_runtime *stream)
  1389. {
  1390. int ret = 0;
  1391. if (!stream) {
  1392. pr_err("SoundWire: Handle not found for stream");
  1393. return -EINVAL;
  1394. }
  1395. sdw_acquire_bus_lock(stream);
  1396. ret = _sdw_disable_stream(stream);
  1397. if (ret < 0)
  1398. pr_err("Disable for stream:%s failed: %d", stream->name, ret);
  1399. sdw_release_bus_lock(stream);
  1400. return ret;
  1401. }
  1402. EXPORT_SYMBOL(sdw_disable_stream);
  1403. static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1404. {
  1405. struct sdw_master_runtime *m_rt = NULL;
  1406. struct sdw_bus *bus = NULL;
  1407. int ret = 0;
  1408. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1409. bus = m_rt->bus;
  1410. /* De-prepare port(s) */
  1411. ret = sdw_prep_deprep_ports(m_rt, false);
  1412. if (ret < 0) {
  1413. dev_err(bus->dev, "De-prepare port(s) failed: %d", ret);
  1414. return ret;
  1415. }
  1416. /* TODO: Update this during Device-Device support */
  1417. bus->params.bandwidth -= m_rt->stream->params.rate *
  1418. m_rt->ch_count * m_rt->stream->params.bps;
  1419. /* Program params */
  1420. ret = sdw_program_params(bus);
  1421. if (ret < 0) {
  1422. dev_err(bus->dev, "Program params failed: %d", ret);
  1423. return ret;
  1424. }
  1425. }
  1426. stream->state = SDW_STREAM_DEPREPARED;
  1427. return do_bank_switch(stream);
  1428. }
  1429. /**
  1430. * sdw_deprepare_stream() - Deprepare SoundWire stream
  1431. *
  1432. * @stream: Soundwire stream
  1433. *
  1434. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1435. */
  1436. int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1437. {
  1438. int ret = 0;
  1439. if (!stream) {
  1440. pr_err("SoundWire: Handle not found for stream");
  1441. return -EINVAL;
  1442. }
  1443. sdw_acquire_bus_lock(stream);
  1444. ret = _sdw_deprepare_stream(stream);
  1445. if (ret < 0)
  1446. pr_err("De-prepare for stream:%d failed: %d", ret, ret);
  1447. sdw_release_bus_lock(stream);
  1448. return ret;
  1449. }
  1450. EXPORT_SYMBOL(sdw_deprepare_stream);