stream.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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)
  538. {
  539. int col_index, row_index;
  540. struct sdw_msg *wr_msg;
  541. u8 *wbuf = NULL;
  542. int ret = 0;
  543. u16 addr;
  544. wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
  545. if (!wr_msg)
  546. return -ENOMEM;
  547. wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
  548. if (!wbuf) {
  549. ret = -ENOMEM;
  550. goto error_1;
  551. }
  552. /* Get row and column index to program register */
  553. col_index = sdw_find_col_index(bus->params.col);
  554. row_index = sdw_find_row_index(bus->params.row);
  555. wbuf[0] = col_index | (row_index << 3);
  556. if (bus->params.next_bank)
  557. addr = SDW_SCP_FRAMECTRL_B1;
  558. else
  559. addr = SDW_SCP_FRAMECTRL_B0;
  560. sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
  561. SDW_MSG_FLAG_WRITE, wbuf);
  562. wr_msg->ssp_sync = true;
  563. ret = sdw_transfer(bus, wr_msg);
  564. if (ret < 0) {
  565. dev_err(bus->dev, "Slave frame_ctrl reg write failed");
  566. goto error;
  567. }
  568. kfree(wr_msg);
  569. kfree(wbuf);
  570. bus->defer_msg.msg = NULL;
  571. bus->params.curr_bank = !bus->params.curr_bank;
  572. bus->params.next_bank = !bus->params.next_bank;
  573. return 0;
  574. error:
  575. kfree(wbuf);
  576. error_1:
  577. kfree(wr_msg);
  578. return ret;
  579. }
  580. static int do_bank_switch(struct sdw_stream_runtime *stream)
  581. {
  582. struct sdw_master_runtime *m_rt = stream->m_rt;
  583. const struct sdw_master_ops *ops;
  584. struct sdw_bus *bus = m_rt->bus;
  585. int ret = 0;
  586. ops = bus->ops;
  587. /* Pre-bank switch */
  588. if (ops->pre_bank_switch) {
  589. ret = ops->pre_bank_switch(bus);
  590. if (ret < 0) {
  591. dev_err(bus->dev, "Pre bank switch op failed: %d", ret);
  592. return ret;
  593. }
  594. }
  595. /* Bank switch */
  596. ret = sdw_bank_switch(bus);
  597. if (ret < 0) {
  598. dev_err(bus->dev, "Bank switch failed: %d", ret);
  599. return ret;
  600. }
  601. /* Post-bank switch */
  602. if (ops->post_bank_switch) {
  603. ret = ops->post_bank_switch(bus);
  604. if (ret < 0) {
  605. dev_err(bus->dev,
  606. "Post bank switch op failed: %d", ret);
  607. }
  608. }
  609. return ret;
  610. }
  611. /**
  612. * sdw_release_stream() - Free the assigned stream runtime
  613. *
  614. * @stream: SoundWire stream runtime
  615. *
  616. * sdw_release_stream should be called only once per stream
  617. */
  618. void sdw_release_stream(struct sdw_stream_runtime *stream)
  619. {
  620. kfree(stream);
  621. }
  622. EXPORT_SYMBOL(sdw_release_stream);
  623. /**
  624. * sdw_alloc_stream() - Allocate and return stream runtime
  625. *
  626. * @stream_name: SoundWire stream name
  627. *
  628. * Allocates a SoundWire stream runtime instance.
  629. * sdw_alloc_stream should be called only once per stream. Typically
  630. * invoked from ALSA/ASoC machine/platform driver.
  631. */
  632. struct sdw_stream_runtime *sdw_alloc_stream(char *stream_name)
  633. {
  634. struct sdw_stream_runtime *stream;
  635. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  636. if (!stream)
  637. return NULL;
  638. stream->name = stream_name;
  639. stream->state = SDW_STREAM_ALLOCATED;
  640. return stream;
  641. }
  642. EXPORT_SYMBOL(sdw_alloc_stream);
  643. /**
  644. * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
  645. *
  646. * @bus: SDW bus instance
  647. * @stream_config: Stream configuration
  648. * @stream: Stream runtime handle.
  649. *
  650. * This function is to be called with bus_lock held.
  651. */
  652. static struct sdw_master_runtime
  653. *sdw_alloc_master_rt(struct sdw_bus *bus,
  654. struct sdw_stream_config *stream_config,
  655. struct sdw_stream_runtime *stream)
  656. {
  657. struct sdw_master_runtime *m_rt;
  658. m_rt = stream->m_rt;
  659. /*
  660. * check if Master is already allocated (as a result of Slave adding
  661. * it first), if so skip allocation and go to configure
  662. */
  663. if (m_rt)
  664. goto stream_config;
  665. m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
  666. if (!m_rt)
  667. return NULL;
  668. /* Initialization of Master runtime handle */
  669. INIT_LIST_HEAD(&m_rt->port_list);
  670. INIT_LIST_HEAD(&m_rt->slave_rt_list);
  671. stream->m_rt = m_rt;
  672. list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
  673. stream_config:
  674. m_rt->ch_count = stream_config->ch_count;
  675. m_rt->bus = bus;
  676. m_rt->stream = stream;
  677. m_rt->direction = stream_config->direction;
  678. return m_rt;
  679. }
  680. /**
  681. * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
  682. *
  683. * @slave: Slave handle
  684. * @stream_config: Stream configuration
  685. * @stream: Stream runtime handle
  686. *
  687. * This function is to be called with bus_lock held.
  688. */
  689. static struct sdw_slave_runtime
  690. *sdw_alloc_slave_rt(struct sdw_slave *slave,
  691. struct sdw_stream_config *stream_config,
  692. struct sdw_stream_runtime *stream)
  693. {
  694. struct sdw_slave_runtime *s_rt = NULL;
  695. s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
  696. if (!s_rt)
  697. return NULL;
  698. INIT_LIST_HEAD(&s_rt->port_list);
  699. s_rt->ch_count = stream_config->ch_count;
  700. s_rt->direction = stream_config->direction;
  701. s_rt->slave = slave;
  702. return s_rt;
  703. }
  704. static void sdw_master_port_release(struct sdw_bus *bus,
  705. struct sdw_master_runtime *m_rt)
  706. {
  707. struct sdw_port_runtime *p_rt, *_p_rt;
  708. list_for_each_entry_safe(p_rt, _p_rt,
  709. &m_rt->port_list, port_node) {
  710. list_del(&p_rt->port_node);
  711. kfree(p_rt);
  712. }
  713. }
  714. static void sdw_slave_port_release(struct sdw_bus *bus,
  715. struct sdw_slave *slave,
  716. struct sdw_stream_runtime *stream)
  717. {
  718. struct sdw_port_runtime *p_rt, *_p_rt;
  719. struct sdw_master_runtime *m_rt = stream->m_rt;
  720. struct sdw_slave_runtime *s_rt;
  721. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  722. if (s_rt->slave != slave)
  723. continue;
  724. list_for_each_entry_safe(p_rt, _p_rt,
  725. &s_rt->port_list, port_node) {
  726. list_del(&p_rt->port_node);
  727. kfree(p_rt);
  728. }
  729. }
  730. }
  731. /**
  732. * sdw_release_slave_stream() - Free Slave(s) runtime handle
  733. *
  734. * @slave: Slave handle.
  735. * @stream: Stream runtime handle.
  736. *
  737. * This function is to be called with bus_lock held.
  738. */
  739. static void sdw_release_slave_stream(struct sdw_slave *slave,
  740. struct sdw_stream_runtime *stream)
  741. {
  742. struct sdw_slave_runtime *s_rt, *_s_rt;
  743. struct sdw_master_runtime *m_rt = stream->m_rt;
  744. /* Retrieve Slave runtime handle */
  745. list_for_each_entry_safe(s_rt, _s_rt,
  746. &m_rt->slave_rt_list, m_rt_node) {
  747. if (s_rt->slave == slave) {
  748. list_del(&s_rt->m_rt_node);
  749. kfree(s_rt);
  750. return;
  751. }
  752. }
  753. }
  754. /**
  755. * sdw_release_master_stream() - Free Master runtime handle
  756. *
  757. * @stream: Stream runtime handle.
  758. *
  759. * This function is to be called with bus_lock held
  760. * It frees the Master runtime handle and associated Slave(s) runtime
  761. * handle. If this is called first then sdw_release_slave_stream() will have
  762. * no effect as Slave(s) runtime handle would already be freed up.
  763. */
  764. static void sdw_release_master_stream(struct sdw_stream_runtime *stream)
  765. {
  766. struct sdw_master_runtime *m_rt = stream->m_rt;
  767. struct sdw_slave_runtime *s_rt, *_s_rt;
  768. list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
  769. sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
  770. sdw_release_slave_stream(s_rt->slave, stream);
  771. }
  772. list_del(&m_rt->bus_node);
  773. }
  774. /**
  775. * sdw_stream_remove_master() - Remove master from sdw_stream
  776. *
  777. * @bus: SDW Bus instance
  778. * @stream: SoundWire stream
  779. *
  780. * This removes and frees port_rt and master_rt from a stream
  781. */
  782. int sdw_stream_remove_master(struct sdw_bus *bus,
  783. struct sdw_stream_runtime *stream)
  784. {
  785. mutex_lock(&bus->bus_lock);
  786. sdw_release_master_stream(stream);
  787. sdw_master_port_release(bus, stream->m_rt);
  788. stream->state = SDW_STREAM_RELEASED;
  789. kfree(stream->m_rt);
  790. stream->m_rt = NULL;
  791. mutex_unlock(&bus->bus_lock);
  792. return 0;
  793. }
  794. EXPORT_SYMBOL(sdw_stream_remove_master);
  795. /**
  796. * sdw_stream_remove_slave() - Remove slave from sdw_stream
  797. *
  798. * @slave: SDW Slave instance
  799. * @stream: SoundWire stream
  800. *
  801. * This removes and frees port_rt and slave_rt from a stream
  802. */
  803. int sdw_stream_remove_slave(struct sdw_slave *slave,
  804. struct sdw_stream_runtime *stream)
  805. {
  806. mutex_lock(&slave->bus->bus_lock);
  807. sdw_slave_port_release(slave->bus, slave, stream);
  808. sdw_release_slave_stream(slave, stream);
  809. mutex_unlock(&slave->bus->bus_lock);
  810. return 0;
  811. }
  812. EXPORT_SYMBOL(sdw_stream_remove_slave);
  813. /**
  814. * sdw_config_stream() - Configure the allocated stream
  815. *
  816. * @dev: SDW device
  817. * @stream: SoundWire stream
  818. * @stream_config: Stream configuration for audio stream
  819. * @is_slave: is API called from Slave or Master
  820. *
  821. * This function is to be called with bus_lock held.
  822. */
  823. static int sdw_config_stream(struct device *dev,
  824. struct sdw_stream_runtime *stream,
  825. struct sdw_stream_config *stream_config, bool is_slave)
  826. {
  827. /*
  828. * Update the stream rate, channel and bps based on data
  829. * source. For more than one data source (multilink),
  830. * match the rate, bps, stream type and increment number of channels.
  831. *
  832. * If rate/bps is zero, it means the values are not set, so skip
  833. * comparison and allow the value to be set and stored in stream
  834. */
  835. if (stream->params.rate &&
  836. stream->params.rate != stream_config->frame_rate) {
  837. dev_err(dev, "rate not matching, stream:%s", stream->name);
  838. return -EINVAL;
  839. }
  840. if (stream->params.bps &&
  841. stream->params.bps != stream_config->bps) {
  842. dev_err(dev, "bps not matching, stream:%s", stream->name);
  843. return -EINVAL;
  844. }
  845. stream->type = stream_config->type;
  846. stream->params.rate = stream_config->frame_rate;
  847. stream->params.bps = stream_config->bps;
  848. /* TODO: Update this check during Device-device support */
  849. if (is_slave)
  850. stream->params.ch_count += stream_config->ch_count;
  851. return 0;
  852. }
  853. static int sdw_is_valid_port_range(struct device *dev,
  854. struct sdw_port_runtime *p_rt)
  855. {
  856. if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
  857. dev_err(dev,
  858. "SoundWire: Invalid port number :%d", p_rt->num);
  859. return -EINVAL;
  860. }
  861. return 0;
  862. }
  863. static struct sdw_port_runtime *sdw_port_alloc(struct device *dev,
  864. struct sdw_port_config *port_config,
  865. int port_index)
  866. {
  867. struct sdw_port_runtime *p_rt;
  868. p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
  869. if (!p_rt)
  870. return NULL;
  871. p_rt->ch_mask = port_config[port_index].ch_mask;
  872. p_rt->num = port_config[port_index].num;
  873. return p_rt;
  874. }
  875. static int sdw_master_port_config(struct sdw_bus *bus,
  876. struct sdw_master_runtime *m_rt,
  877. struct sdw_port_config *port_config,
  878. unsigned int num_ports)
  879. {
  880. struct sdw_port_runtime *p_rt;
  881. int i;
  882. /* Iterate for number of ports to perform initialization */
  883. for (i = 0; i < num_ports; i++) {
  884. p_rt = sdw_port_alloc(bus->dev, port_config, i);
  885. if (!p_rt)
  886. return -ENOMEM;
  887. /*
  888. * TODO: Check port capabilities for requested
  889. * configuration (audio mode support)
  890. */
  891. list_add_tail(&p_rt->port_node, &m_rt->port_list);
  892. }
  893. return 0;
  894. }
  895. static int sdw_slave_port_config(struct sdw_slave *slave,
  896. struct sdw_slave_runtime *s_rt,
  897. struct sdw_port_config *port_config,
  898. unsigned int num_config)
  899. {
  900. struct sdw_port_runtime *p_rt;
  901. int i, ret;
  902. /* Iterate for number of ports to perform initialization */
  903. for (i = 0; i < num_config; i++) {
  904. p_rt = sdw_port_alloc(&slave->dev, port_config, i);
  905. if (!p_rt)
  906. return -ENOMEM;
  907. /*
  908. * TODO: Check valid port range as defined by DisCo/
  909. * slave
  910. */
  911. ret = sdw_is_valid_port_range(&slave->dev, p_rt);
  912. if (ret < 0) {
  913. kfree(p_rt);
  914. return ret;
  915. }
  916. /*
  917. * TODO: Check port capabilities for requested
  918. * configuration (audio mode support)
  919. */
  920. list_add_tail(&p_rt->port_node, &s_rt->port_list);
  921. }
  922. return 0;
  923. }
  924. /**
  925. * sdw_stream_add_master() - Allocate and add master runtime to a stream
  926. *
  927. * @bus: SDW Bus instance
  928. * @stream_config: Stream configuration for audio stream
  929. * @port_config: Port configuration for audio stream
  930. * @num_ports: Number of ports
  931. * @stream: SoundWire stream
  932. */
  933. int sdw_stream_add_master(struct sdw_bus *bus,
  934. struct sdw_stream_config *stream_config,
  935. struct sdw_port_config *port_config,
  936. unsigned int num_ports,
  937. struct sdw_stream_runtime *stream)
  938. {
  939. struct sdw_master_runtime *m_rt = NULL;
  940. int ret;
  941. mutex_lock(&bus->bus_lock);
  942. m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
  943. if (!m_rt) {
  944. dev_err(bus->dev,
  945. "Master runtime config failed for stream:%s",
  946. stream->name);
  947. ret = -ENOMEM;
  948. goto unlock;
  949. }
  950. ret = sdw_config_stream(bus->dev, stream, stream_config, false);
  951. if (ret)
  952. goto stream_error;
  953. ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
  954. if (ret)
  955. goto stream_error;
  956. goto unlock;
  957. stream_error:
  958. sdw_release_master_stream(stream);
  959. unlock:
  960. mutex_unlock(&bus->bus_lock);
  961. return ret;
  962. }
  963. EXPORT_SYMBOL(sdw_stream_add_master);
  964. /**
  965. * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
  966. *
  967. * @slave: SDW Slave instance
  968. * @stream_config: Stream configuration for audio stream
  969. * @stream: SoundWire stream
  970. * @port_config: Port configuration for audio stream
  971. * @num_ports: Number of ports
  972. *
  973. * It is expected that Slave is added before adding Master
  974. * to the Stream.
  975. *
  976. */
  977. int sdw_stream_add_slave(struct sdw_slave *slave,
  978. struct sdw_stream_config *stream_config,
  979. struct sdw_port_config *port_config,
  980. unsigned int num_ports,
  981. struct sdw_stream_runtime *stream)
  982. {
  983. struct sdw_slave_runtime *s_rt;
  984. struct sdw_master_runtime *m_rt;
  985. int ret;
  986. mutex_lock(&slave->bus->bus_lock);
  987. /*
  988. * If this API is invoked by Slave first then m_rt is not valid.
  989. * So, allocate m_rt and add Slave to it.
  990. */
  991. m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
  992. if (!m_rt) {
  993. dev_err(&slave->dev,
  994. "alloc master runtime failed for stream:%s",
  995. stream->name);
  996. ret = -ENOMEM;
  997. goto error;
  998. }
  999. s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
  1000. if (!s_rt) {
  1001. dev_err(&slave->dev,
  1002. "Slave runtime config failed for stream:%s",
  1003. stream->name);
  1004. ret = -ENOMEM;
  1005. goto stream_error;
  1006. }
  1007. ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
  1008. if (ret)
  1009. goto stream_error;
  1010. list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
  1011. ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
  1012. if (ret)
  1013. goto stream_error;
  1014. /*
  1015. * Change stream state to CONFIGURED on first Slave add.
  1016. * Bus is not aware of number of Slave(s) in a stream at this
  1017. * point so cannot depend on all Slave(s) to be added in order to
  1018. * change stream state to CONFIGURED.
  1019. */
  1020. stream->state = SDW_STREAM_CONFIGURED;
  1021. goto error;
  1022. stream_error:
  1023. /*
  1024. * we hit error so cleanup the stream, release all Slave(s) and
  1025. * Master runtime
  1026. */
  1027. sdw_release_master_stream(stream);
  1028. error:
  1029. mutex_unlock(&slave->bus->bus_lock);
  1030. return ret;
  1031. }
  1032. EXPORT_SYMBOL(sdw_stream_add_slave);
  1033. /**
  1034. * sdw_get_slave_dpn_prop() - Get Slave port capabilities
  1035. *
  1036. * @slave: Slave handle
  1037. * @direction: Data direction.
  1038. * @port_num: Port number
  1039. */
  1040. struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
  1041. enum sdw_data_direction direction,
  1042. unsigned int port_num)
  1043. {
  1044. struct sdw_dpn_prop *dpn_prop;
  1045. u8 num_ports;
  1046. int i;
  1047. if (direction == SDW_DATA_DIR_TX) {
  1048. num_ports = hweight32(slave->prop.source_ports);
  1049. dpn_prop = slave->prop.src_dpn_prop;
  1050. } else {
  1051. num_ports = hweight32(slave->prop.sink_ports);
  1052. dpn_prop = slave->prop.sink_dpn_prop;
  1053. }
  1054. for (i = 0; i < num_ports; i++) {
  1055. if (dpn_prop[i].num == port_num)
  1056. return &dpn_prop[i];
  1057. }
  1058. return NULL;
  1059. }
  1060. static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1061. {
  1062. struct sdw_master_runtime *m_rt = stream->m_rt;
  1063. struct sdw_bus *bus = m_rt->bus;
  1064. struct sdw_master_prop *prop = NULL;
  1065. struct sdw_bus_params params;
  1066. int ret;
  1067. prop = &bus->prop;
  1068. memcpy(&params, &bus->params, sizeof(params));
  1069. /* TODO: Support Asynchronous mode */
  1070. if ((prop->max_freq % stream->params.rate) != 0) {
  1071. dev_err(bus->dev, "Async mode not supported");
  1072. return -EINVAL;
  1073. }
  1074. /* Increment cumulative bus bandwidth */
  1075. /* TODO: Update this during Device-Device support */
  1076. bus->params.bandwidth += m_rt->stream->params.rate *
  1077. m_rt->ch_count * m_rt->stream->params.bps;
  1078. /* Program params */
  1079. ret = sdw_program_params(bus);
  1080. if (ret < 0) {
  1081. dev_err(bus->dev, "Program params failed: %d", ret);
  1082. goto restore_params;
  1083. }
  1084. ret = do_bank_switch(stream);
  1085. if (ret < 0) {
  1086. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1087. goto restore_params;
  1088. }
  1089. /* Prepare port(s) on the new clock configuration */
  1090. ret = sdw_prep_deprep_ports(m_rt, true);
  1091. if (ret < 0) {
  1092. dev_err(bus->dev, "Prepare port(s) failed ret = %d",
  1093. ret);
  1094. return ret;
  1095. }
  1096. stream->state = SDW_STREAM_PREPARED;
  1097. return ret;
  1098. restore_params:
  1099. memcpy(&bus->params, &params, sizeof(params));
  1100. return ret;
  1101. }
  1102. /**
  1103. * sdw_prepare_stream() - Prepare SoundWire stream
  1104. *
  1105. * @stream: Soundwire stream
  1106. *
  1107. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1108. */
  1109. int sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1110. {
  1111. int ret = 0;
  1112. if (!stream) {
  1113. pr_err("SoundWire: Handle not found for stream");
  1114. return -EINVAL;
  1115. }
  1116. mutex_lock(&stream->m_rt->bus->bus_lock);
  1117. ret = _sdw_prepare_stream(stream);
  1118. if (ret < 0)
  1119. pr_err("Prepare for stream:%s failed: %d", stream->name, ret);
  1120. mutex_unlock(&stream->m_rt->bus->bus_lock);
  1121. return ret;
  1122. }
  1123. EXPORT_SYMBOL(sdw_prepare_stream);
  1124. static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
  1125. {
  1126. struct sdw_master_runtime *m_rt = stream->m_rt;
  1127. struct sdw_bus *bus = m_rt->bus;
  1128. int ret;
  1129. /* Program params */
  1130. ret = sdw_program_params(bus);
  1131. if (ret < 0) {
  1132. dev_err(bus->dev, "Program params failed: %d", ret);
  1133. return ret;
  1134. }
  1135. /* Enable port(s) */
  1136. ret = sdw_enable_disable_ports(m_rt, true);
  1137. if (ret < 0) {
  1138. dev_err(bus->dev, "Enable port(s) failed ret: %d", ret);
  1139. return ret;
  1140. }
  1141. ret = do_bank_switch(stream);
  1142. if (ret < 0) {
  1143. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1144. return ret;
  1145. }
  1146. stream->state = SDW_STREAM_ENABLED;
  1147. return 0;
  1148. }
  1149. /**
  1150. * sdw_enable_stream() - Enable SoundWire stream
  1151. *
  1152. * @stream: Soundwire stream
  1153. *
  1154. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1155. */
  1156. int sdw_enable_stream(struct sdw_stream_runtime *stream)
  1157. {
  1158. int ret = 0;
  1159. if (!stream) {
  1160. pr_err("SoundWire: Handle not found for stream");
  1161. return -EINVAL;
  1162. }
  1163. mutex_lock(&stream->m_rt->bus->bus_lock);
  1164. ret = _sdw_enable_stream(stream);
  1165. if (ret < 0)
  1166. pr_err("Enable for stream:%s failed: %d", stream->name, ret);
  1167. mutex_unlock(&stream->m_rt->bus->bus_lock);
  1168. return ret;
  1169. }
  1170. EXPORT_SYMBOL(sdw_enable_stream);
  1171. static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
  1172. {
  1173. struct sdw_master_runtime *m_rt = stream->m_rt;
  1174. struct sdw_bus *bus = m_rt->bus;
  1175. int ret;
  1176. /* Disable port(s) */
  1177. ret = sdw_enable_disable_ports(m_rt, false);
  1178. if (ret < 0) {
  1179. dev_err(bus->dev, "Disable port(s) failed: %d", ret);
  1180. return ret;
  1181. }
  1182. stream->state = SDW_STREAM_DISABLED;
  1183. /* Program params */
  1184. ret = sdw_program_params(bus);
  1185. if (ret < 0) {
  1186. dev_err(bus->dev, "Program params failed: %d", ret);
  1187. return ret;
  1188. }
  1189. return do_bank_switch(stream);
  1190. }
  1191. /**
  1192. * sdw_disable_stream() - Disable SoundWire stream
  1193. *
  1194. * @stream: Soundwire stream
  1195. *
  1196. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1197. */
  1198. int sdw_disable_stream(struct sdw_stream_runtime *stream)
  1199. {
  1200. int ret = 0;
  1201. if (!stream) {
  1202. pr_err("SoundWire: Handle not found for stream");
  1203. return -EINVAL;
  1204. }
  1205. mutex_lock(&stream->m_rt->bus->bus_lock);
  1206. ret = _sdw_disable_stream(stream);
  1207. if (ret < 0)
  1208. pr_err("Disable for stream:%s failed: %d", stream->name, ret);
  1209. mutex_unlock(&stream->m_rt->bus->bus_lock);
  1210. return ret;
  1211. }
  1212. EXPORT_SYMBOL(sdw_disable_stream);
  1213. static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1214. {
  1215. struct sdw_master_runtime *m_rt = stream->m_rt;
  1216. struct sdw_bus *bus = m_rt->bus;
  1217. int ret = 0;
  1218. /* De-prepare port(s) */
  1219. ret = sdw_prep_deprep_ports(m_rt, false);
  1220. if (ret < 0) {
  1221. dev_err(bus->dev, "De-prepare port(s) failed: %d", ret);
  1222. return ret;
  1223. }
  1224. stream->state = SDW_STREAM_DEPREPARED;
  1225. /* TODO: Update this during Device-Device support */
  1226. bus->params.bandwidth -= m_rt->stream->params.rate *
  1227. m_rt->ch_count * m_rt->stream->params.bps;
  1228. /* Program params */
  1229. ret = sdw_program_params(bus);
  1230. if (ret < 0) {
  1231. dev_err(bus->dev, "Program params failed: %d", ret);
  1232. return ret;
  1233. }
  1234. return do_bank_switch(stream);
  1235. }
  1236. /**
  1237. * sdw_deprepare_stream() - Deprepare SoundWire stream
  1238. *
  1239. * @stream: Soundwire stream
  1240. *
  1241. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1242. */
  1243. int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1244. {
  1245. int ret = 0;
  1246. if (!stream) {
  1247. pr_err("SoundWire: Handle not found for stream");
  1248. return -EINVAL;
  1249. }
  1250. mutex_lock(&stream->m_rt->bus->bus_lock);
  1251. ret = _sdw_deprepare_stream(stream);
  1252. if (ret < 0)
  1253. pr_err("De-prepare for stream:%d failed: %d", ret, ret);
  1254. mutex_unlock(&stream->m_rt->bus->bus_lock);
  1255. return ret;
  1256. }
  1257. EXPORT_SYMBOL(sdw_deprepare_stream);