stream.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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. return stream;
  736. }
  737. EXPORT_SYMBOL(sdw_alloc_stream);
  738. static struct sdw_master_runtime
  739. *sdw_find_master_rt(struct sdw_bus *bus,
  740. struct sdw_stream_runtime *stream)
  741. {
  742. struct sdw_master_runtime *m_rt = NULL;
  743. /* Retrieve Bus handle if already available */
  744. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  745. if (m_rt->bus == bus)
  746. return m_rt;
  747. }
  748. return NULL;
  749. }
  750. /**
  751. * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
  752. *
  753. * @bus: SDW bus instance
  754. * @stream_config: Stream configuration
  755. * @stream: Stream runtime handle.
  756. *
  757. * This function is to be called with bus_lock held.
  758. */
  759. static struct sdw_master_runtime
  760. *sdw_alloc_master_rt(struct sdw_bus *bus,
  761. struct sdw_stream_config *stream_config,
  762. struct sdw_stream_runtime *stream)
  763. {
  764. struct sdw_master_runtime *m_rt;
  765. /*
  766. * check if Master is already allocated (as a result of Slave adding
  767. * it first), if so skip allocation and go to configure
  768. */
  769. m_rt = sdw_find_master_rt(bus, stream);
  770. if (m_rt)
  771. goto stream_config;
  772. m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
  773. if (!m_rt)
  774. return NULL;
  775. /* Initialization of Master runtime handle */
  776. INIT_LIST_HEAD(&m_rt->port_list);
  777. INIT_LIST_HEAD(&m_rt->slave_rt_list);
  778. list_add_tail(&m_rt->stream_node, &stream->master_list);
  779. list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
  780. stream_config:
  781. m_rt->ch_count = stream_config->ch_count;
  782. m_rt->bus = bus;
  783. m_rt->stream = stream;
  784. m_rt->direction = stream_config->direction;
  785. return m_rt;
  786. }
  787. /**
  788. * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
  789. *
  790. * @slave: Slave handle
  791. * @stream_config: Stream configuration
  792. * @stream: Stream runtime handle
  793. *
  794. * This function is to be called with bus_lock held.
  795. */
  796. static struct sdw_slave_runtime
  797. *sdw_alloc_slave_rt(struct sdw_slave *slave,
  798. struct sdw_stream_config *stream_config,
  799. struct sdw_stream_runtime *stream)
  800. {
  801. struct sdw_slave_runtime *s_rt = NULL;
  802. s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
  803. if (!s_rt)
  804. return NULL;
  805. INIT_LIST_HEAD(&s_rt->port_list);
  806. s_rt->ch_count = stream_config->ch_count;
  807. s_rt->direction = stream_config->direction;
  808. s_rt->slave = slave;
  809. return s_rt;
  810. }
  811. static void sdw_master_port_release(struct sdw_bus *bus,
  812. struct sdw_master_runtime *m_rt)
  813. {
  814. struct sdw_port_runtime *p_rt, *_p_rt;
  815. list_for_each_entry_safe(p_rt, _p_rt,
  816. &m_rt->port_list, port_node) {
  817. list_del(&p_rt->port_node);
  818. kfree(p_rt);
  819. }
  820. }
  821. static void sdw_slave_port_release(struct sdw_bus *bus,
  822. struct sdw_slave *slave,
  823. struct sdw_stream_runtime *stream)
  824. {
  825. struct sdw_port_runtime *p_rt, *_p_rt;
  826. struct sdw_master_runtime *m_rt;
  827. struct sdw_slave_runtime *s_rt;
  828. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  829. list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
  830. if (s_rt->slave != slave)
  831. continue;
  832. list_for_each_entry_safe(p_rt, _p_rt,
  833. &s_rt->port_list, port_node) {
  834. list_del(&p_rt->port_node);
  835. kfree(p_rt);
  836. }
  837. }
  838. }
  839. }
  840. /**
  841. * sdw_release_slave_stream() - Free Slave(s) runtime handle
  842. *
  843. * @slave: Slave handle.
  844. * @stream: Stream runtime handle.
  845. *
  846. * This function is to be called with bus_lock held.
  847. */
  848. static void sdw_release_slave_stream(struct sdw_slave *slave,
  849. struct sdw_stream_runtime *stream)
  850. {
  851. struct sdw_slave_runtime *s_rt, *_s_rt;
  852. struct sdw_master_runtime *m_rt;
  853. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  854. /* Retrieve Slave runtime handle */
  855. list_for_each_entry_safe(s_rt, _s_rt,
  856. &m_rt->slave_rt_list, m_rt_node) {
  857. if (s_rt->slave == slave) {
  858. list_del(&s_rt->m_rt_node);
  859. kfree(s_rt);
  860. return;
  861. }
  862. }
  863. }
  864. }
  865. /**
  866. * sdw_release_master_stream() - Free Master runtime handle
  867. *
  868. * @m_rt: Master runtime node
  869. * @stream: Stream runtime handle.
  870. *
  871. * This function is to be called with bus_lock held
  872. * It frees the Master runtime handle and associated Slave(s) runtime
  873. * handle. If this is called first then sdw_release_slave_stream() will have
  874. * no effect as Slave(s) runtime handle would already be freed up.
  875. */
  876. static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
  877. struct sdw_stream_runtime *stream)
  878. {
  879. struct sdw_slave_runtime *s_rt, *_s_rt;
  880. list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
  881. sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
  882. sdw_release_slave_stream(s_rt->slave, stream);
  883. }
  884. list_del(&m_rt->stream_node);
  885. list_del(&m_rt->bus_node);
  886. kfree(m_rt);
  887. }
  888. /**
  889. * sdw_stream_remove_master() - Remove master from sdw_stream
  890. *
  891. * @bus: SDW Bus instance
  892. * @stream: SoundWire stream
  893. *
  894. * This removes and frees port_rt and master_rt from a stream
  895. */
  896. int sdw_stream_remove_master(struct sdw_bus *bus,
  897. struct sdw_stream_runtime *stream)
  898. {
  899. struct sdw_master_runtime *m_rt, *_m_rt;
  900. mutex_lock(&bus->bus_lock);
  901. list_for_each_entry_safe(m_rt, _m_rt,
  902. &stream->master_list, stream_node) {
  903. if (m_rt->bus != bus)
  904. continue;
  905. sdw_master_port_release(bus, m_rt);
  906. sdw_release_master_stream(m_rt, stream);
  907. stream->m_rt_count--;
  908. }
  909. if (list_empty(&stream->master_list))
  910. stream->state = SDW_STREAM_RELEASED;
  911. mutex_unlock(&bus->bus_lock);
  912. return 0;
  913. }
  914. EXPORT_SYMBOL(sdw_stream_remove_master);
  915. /**
  916. * sdw_stream_remove_slave() - Remove slave from sdw_stream
  917. *
  918. * @slave: SDW Slave instance
  919. * @stream: SoundWire stream
  920. *
  921. * This removes and frees port_rt and slave_rt from a stream
  922. */
  923. int sdw_stream_remove_slave(struct sdw_slave *slave,
  924. struct sdw_stream_runtime *stream)
  925. {
  926. mutex_lock(&slave->bus->bus_lock);
  927. sdw_slave_port_release(slave->bus, slave, stream);
  928. sdw_release_slave_stream(slave, stream);
  929. mutex_unlock(&slave->bus->bus_lock);
  930. return 0;
  931. }
  932. EXPORT_SYMBOL(sdw_stream_remove_slave);
  933. /**
  934. * sdw_config_stream() - Configure the allocated stream
  935. *
  936. * @dev: SDW device
  937. * @stream: SoundWire stream
  938. * @stream_config: Stream configuration for audio stream
  939. * @is_slave: is API called from Slave or Master
  940. *
  941. * This function is to be called with bus_lock held.
  942. */
  943. static int sdw_config_stream(struct device *dev,
  944. struct sdw_stream_runtime *stream,
  945. struct sdw_stream_config *stream_config, bool is_slave)
  946. {
  947. /*
  948. * Update the stream rate, channel and bps based on data
  949. * source. For more than one data source (multilink),
  950. * match the rate, bps, stream type and increment number of channels.
  951. *
  952. * If rate/bps is zero, it means the values are not set, so skip
  953. * comparison and allow the value to be set and stored in stream
  954. */
  955. if (stream->params.rate &&
  956. stream->params.rate != stream_config->frame_rate) {
  957. dev_err(dev, "rate not matching, stream:%s", stream->name);
  958. return -EINVAL;
  959. }
  960. if (stream->params.bps &&
  961. stream->params.bps != stream_config->bps) {
  962. dev_err(dev, "bps not matching, stream:%s", stream->name);
  963. return -EINVAL;
  964. }
  965. stream->type = stream_config->type;
  966. stream->params.rate = stream_config->frame_rate;
  967. stream->params.bps = stream_config->bps;
  968. /* TODO: Update this check during Device-device support */
  969. if (is_slave)
  970. stream->params.ch_count += stream_config->ch_count;
  971. return 0;
  972. }
  973. static int sdw_is_valid_port_range(struct device *dev,
  974. struct sdw_port_runtime *p_rt)
  975. {
  976. if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
  977. dev_err(dev,
  978. "SoundWire: Invalid port number :%d", p_rt->num);
  979. return -EINVAL;
  980. }
  981. return 0;
  982. }
  983. static struct sdw_port_runtime *sdw_port_alloc(struct device *dev,
  984. struct sdw_port_config *port_config,
  985. int port_index)
  986. {
  987. struct sdw_port_runtime *p_rt;
  988. p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
  989. if (!p_rt)
  990. return NULL;
  991. p_rt->ch_mask = port_config[port_index].ch_mask;
  992. p_rt->num = port_config[port_index].num;
  993. return p_rt;
  994. }
  995. static int sdw_master_port_config(struct sdw_bus *bus,
  996. struct sdw_master_runtime *m_rt,
  997. struct sdw_port_config *port_config,
  998. unsigned int num_ports)
  999. {
  1000. struct sdw_port_runtime *p_rt;
  1001. int i;
  1002. /* Iterate for number of ports to perform initialization */
  1003. for (i = 0; i < num_ports; i++) {
  1004. p_rt = sdw_port_alloc(bus->dev, port_config, i);
  1005. if (!p_rt)
  1006. return -ENOMEM;
  1007. /*
  1008. * TODO: Check port capabilities for requested
  1009. * configuration (audio mode support)
  1010. */
  1011. list_add_tail(&p_rt->port_node, &m_rt->port_list);
  1012. }
  1013. return 0;
  1014. }
  1015. static int sdw_slave_port_config(struct sdw_slave *slave,
  1016. struct sdw_slave_runtime *s_rt,
  1017. struct sdw_port_config *port_config,
  1018. unsigned int num_config)
  1019. {
  1020. struct sdw_port_runtime *p_rt;
  1021. int i, ret;
  1022. /* Iterate for number of ports to perform initialization */
  1023. for (i = 0; i < num_config; i++) {
  1024. p_rt = sdw_port_alloc(&slave->dev, port_config, i);
  1025. if (!p_rt)
  1026. return -ENOMEM;
  1027. /*
  1028. * TODO: Check valid port range as defined by DisCo/
  1029. * slave
  1030. */
  1031. ret = sdw_is_valid_port_range(&slave->dev, p_rt);
  1032. if (ret < 0) {
  1033. kfree(p_rt);
  1034. return ret;
  1035. }
  1036. /*
  1037. * TODO: Check port capabilities for requested
  1038. * configuration (audio mode support)
  1039. */
  1040. list_add_tail(&p_rt->port_node, &s_rt->port_list);
  1041. }
  1042. return 0;
  1043. }
  1044. /**
  1045. * sdw_stream_add_master() - Allocate and add master runtime to a stream
  1046. *
  1047. * @bus: SDW Bus instance
  1048. * @stream_config: Stream configuration for audio stream
  1049. * @port_config: Port configuration for audio stream
  1050. * @num_ports: Number of ports
  1051. * @stream: SoundWire stream
  1052. */
  1053. int sdw_stream_add_master(struct sdw_bus *bus,
  1054. struct sdw_stream_config *stream_config,
  1055. struct sdw_port_config *port_config,
  1056. unsigned int num_ports,
  1057. struct sdw_stream_runtime *stream)
  1058. {
  1059. struct sdw_master_runtime *m_rt = NULL;
  1060. int ret;
  1061. mutex_lock(&bus->bus_lock);
  1062. /*
  1063. * For multi link streams, add the second master only if
  1064. * the bus supports it.
  1065. * Check if bus->multi_link is set
  1066. */
  1067. if (!bus->multi_link && stream->m_rt_count > 0) {
  1068. dev_err(bus->dev,
  1069. "Multilink not supported, link %d", bus->link_id);
  1070. ret = -EINVAL;
  1071. goto unlock;
  1072. }
  1073. m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
  1074. if (!m_rt) {
  1075. dev_err(bus->dev,
  1076. "Master runtime config failed for stream:%s",
  1077. stream->name);
  1078. ret = -ENOMEM;
  1079. goto unlock;
  1080. }
  1081. ret = sdw_config_stream(bus->dev, stream, stream_config, false);
  1082. if (ret)
  1083. goto stream_error;
  1084. ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
  1085. if (ret)
  1086. goto stream_error;
  1087. stream->m_rt_count++;
  1088. goto unlock;
  1089. stream_error:
  1090. sdw_release_master_stream(m_rt, stream);
  1091. unlock:
  1092. mutex_unlock(&bus->bus_lock);
  1093. return ret;
  1094. }
  1095. EXPORT_SYMBOL(sdw_stream_add_master);
  1096. /**
  1097. * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
  1098. *
  1099. * @slave: SDW Slave instance
  1100. * @stream_config: Stream configuration for audio stream
  1101. * @stream: SoundWire stream
  1102. * @port_config: Port configuration for audio stream
  1103. * @num_ports: Number of ports
  1104. *
  1105. * It is expected that Slave is added before adding Master
  1106. * to the Stream.
  1107. *
  1108. */
  1109. int sdw_stream_add_slave(struct sdw_slave *slave,
  1110. struct sdw_stream_config *stream_config,
  1111. struct sdw_port_config *port_config,
  1112. unsigned int num_ports,
  1113. struct sdw_stream_runtime *stream)
  1114. {
  1115. struct sdw_slave_runtime *s_rt;
  1116. struct sdw_master_runtime *m_rt;
  1117. int ret;
  1118. mutex_lock(&slave->bus->bus_lock);
  1119. /*
  1120. * If this API is invoked by Slave first then m_rt is not valid.
  1121. * So, allocate m_rt and add Slave to it.
  1122. */
  1123. m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
  1124. if (!m_rt) {
  1125. dev_err(&slave->dev,
  1126. "alloc master runtime failed for stream:%s",
  1127. stream->name);
  1128. ret = -ENOMEM;
  1129. goto error;
  1130. }
  1131. s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
  1132. if (!s_rt) {
  1133. dev_err(&slave->dev,
  1134. "Slave runtime config failed for stream:%s",
  1135. stream->name);
  1136. ret = -ENOMEM;
  1137. goto stream_error;
  1138. }
  1139. ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
  1140. if (ret)
  1141. goto stream_error;
  1142. list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
  1143. ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
  1144. if (ret)
  1145. goto stream_error;
  1146. /*
  1147. * Change stream state to CONFIGURED on first Slave add.
  1148. * Bus is not aware of number of Slave(s) in a stream at this
  1149. * point so cannot depend on all Slave(s) to be added in order to
  1150. * change stream state to CONFIGURED.
  1151. */
  1152. stream->state = SDW_STREAM_CONFIGURED;
  1153. goto error;
  1154. stream_error:
  1155. /*
  1156. * we hit error so cleanup the stream, release all Slave(s) and
  1157. * Master runtime
  1158. */
  1159. sdw_release_master_stream(m_rt, stream);
  1160. error:
  1161. mutex_unlock(&slave->bus->bus_lock);
  1162. return ret;
  1163. }
  1164. EXPORT_SYMBOL(sdw_stream_add_slave);
  1165. /**
  1166. * sdw_get_slave_dpn_prop() - Get Slave port capabilities
  1167. *
  1168. * @slave: Slave handle
  1169. * @direction: Data direction.
  1170. * @port_num: Port number
  1171. */
  1172. struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
  1173. enum sdw_data_direction direction,
  1174. unsigned int port_num)
  1175. {
  1176. struct sdw_dpn_prop *dpn_prop;
  1177. u8 num_ports;
  1178. int i;
  1179. if (direction == SDW_DATA_DIR_TX) {
  1180. num_ports = hweight32(slave->prop.source_ports);
  1181. dpn_prop = slave->prop.src_dpn_prop;
  1182. } else {
  1183. num_ports = hweight32(slave->prop.sink_ports);
  1184. dpn_prop = slave->prop.sink_dpn_prop;
  1185. }
  1186. for (i = 0; i < num_ports; i++) {
  1187. dpn_prop = &dpn_prop[i];
  1188. if (dpn_prop->num == port_num)
  1189. return &dpn_prop[i];
  1190. }
  1191. return NULL;
  1192. }
  1193. /**
  1194. * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
  1195. *
  1196. * @stream: SoundWire stream
  1197. *
  1198. * Acquire bus_lock for each of the master runtime(m_rt) part of this
  1199. * stream to reconfigure the bus.
  1200. * NOTE: This function is called from SoundWire stream ops and is
  1201. * expected that a global lock is held before acquiring bus_lock.
  1202. */
  1203. static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
  1204. {
  1205. struct sdw_master_runtime *m_rt = NULL;
  1206. struct sdw_bus *bus = NULL;
  1207. /* Iterate for all Master(s) in Master list */
  1208. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1209. bus = m_rt->bus;
  1210. mutex_lock(&bus->bus_lock);
  1211. }
  1212. }
  1213. /**
  1214. * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
  1215. *
  1216. * @stream: SoundWire stream
  1217. *
  1218. * Release the previously held bus_lock after reconfiguring the bus.
  1219. * NOTE: This function is called from SoundWire stream ops and is
  1220. * expected that a global lock is held before releasing bus_lock.
  1221. */
  1222. static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
  1223. {
  1224. struct sdw_master_runtime *m_rt = NULL;
  1225. struct sdw_bus *bus = NULL;
  1226. /* Iterate for all Master(s) in Master list */
  1227. list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
  1228. bus = m_rt->bus;
  1229. mutex_unlock(&bus->bus_lock);
  1230. }
  1231. }
  1232. static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1233. {
  1234. struct sdw_master_runtime *m_rt = NULL;
  1235. struct sdw_bus *bus = NULL;
  1236. struct sdw_master_prop *prop = NULL;
  1237. struct sdw_bus_params params;
  1238. int ret;
  1239. /* Prepare Master(s) and Slave(s) port(s) associated with stream */
  1240. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1241. bus = m_rt->bus;
  1242. prop = &bus->prop;
  1243. memcpy(&params, &bus->params, sizeof(params));
  1244. /* TODO: Support Asynchronous mode */
  1245. if ((prop->max_freq % stream->params.rate) != 0) {
  1246. dev_err(bus->dev, "Async mode not supported");
  1247. return -EINVAL;
  1248. }
  1249. /* Increment cumulative bus bandwidth */
  1250. /* TODO: Update this during Device-Device support */
  1251. bus->params.bandwidth += m_rt->stream->params.rate *
  1252. m_rt->ch_count * m_rt->stream->params.bps;
  1253. /* Program params */
  1254. ret = sdw_program_params(bus);
  1255. if (ret < 0) {
  1256. dev_err(bus->dev, "Program params failed: %d", ret);
  1257. goto restore_params;
  1258. }
  1259. }
  1260. ret = do_bank_switch(stream);
  1261. if (ret < 0) {
  1262. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1263. goto restore_params;
  1264. }
  1265. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1266. bus = m_rt->bus;
  1267. /* Prepare port(s) on the new clock configuration */
  1268. ret = sdw_prep_deprep_ports(m_rt, true);
  1269. if (ret < 0) {
  1270. dev_err(bus->dev, "Prepare port(s) failed ret = %d",
  1271. ret);
  1272. return ret;
  1273. }
  1274. }
  1275. stream->state = SDW_STREAM_PREPARED;
  1276. return ret;
  1277. restore_params:
  1278. memcpy(&bus->params, &params, sizeof(params));
  1279. return ret;
  1280. }
  1281. /**
  1282. * sdw_prepare_stream() - Prepare SoundWire stream
  1283. *
  1284. * @stream: Soundwire stream
  1285. *
  1286. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1287. */
  1288. int sdw_prepare_stream(struct sdw_stream_runtime *stream)
  1289. {
  1290. int ret = 0;
  1291. if (!stream) {
  1292. pr_err("SoundWire: Handle not found for stream");
  1293. return -EINVAL;
  1294. }
  1295. sdw_acquire_bus_lock(stream);
  1296. ret = _sdw_prepare_stream(stream);
  1297. if (ret < 0)
  1298. pr_err("Prepare for stream:%s failed: %d", stream->name, ret);
  1299. sdw_release_bus_lock(stream);
  1300. return ret;
  1301. }
  1302. EXPORT_SYMBOL(sdw_prepare_stream);
  1303. static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
  1304. {
  1305. struct sdw_master_runtime *m_rt = NULL;
  1306. struct sdw_bus *bus = NULL;
  1307. int ret;
  1308. /* Enable Master(s) and Slave(s) port(s) associated with stream */
  1309. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1310. bus = m_rt->bus;
  1311. /* Program params */
  1312. ret = sdw_program_params(bus);
  1313. if (ret < 0) {
  1314. dev_err(bus->dev, "Program params failed: %d", ret);
  1315. return ret;
  1316. }
  1317. /* Enable port(s) */
  1318. ret = sdw_enable_disable_ports(m_rt, true);
  1319. if (ret < 0) {
  1320. dev_err(bus->dev, "Enable port(s) failed ret: %d", ret);
  1321. return ret;
  1322. }
  1323. }
  1324. ret = do_bank_switch(stream);
  1325. if (ret < 0) {
  1326. dev_err(bus->dev, "Bank switch failed: %d", ret);
  1327. return ret;
  1328. }
  1329. stream->state = SDW_STREAM_ENABLED;
  1330. return 0;
  1331. }
  1332. /**
  1333. * sdw_enable_stream() - Enable SoundWire stream
  1334. *
  1335. * @stream: Soundwire stream
  1336. *
  1337. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1338. */
  1339. int sdw_enable_stream(struct sdw_stream_runtime *stream)
  1340. {
  1341. int ret = 0;
  1342. if (!stream) {
  1343. pr_err("SoundWire: Handle not found for stream");
  1344. return -EINVAL;
  1345. }
  1346. sdw_acquire_bus_lock(stream);
  1347. ret = _sdw_enable_stream(stream);
  1348. if (ret < 0)
  1349. pr_err("Enable for stream:%s failed: %d", stream->name, ret);
  1350. sdw_release_bus_lock(stream);
  1351. return ret;
  1352. }
  1353. EXPORT_SYMBOL(sdw_enable_stream);
  1354. static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
  1355. {
  1356. struct sdw_master_runtime *m_rt = NULL;
  1357. struct sdw_bus *bus = NULL;
  1358. int ret;
  1359. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1360. bus = m_rt->bus;
  1361. /* Disable port(s) */
  1362. ret = sdw_enable_disable_ports(m_rt, false);
  1363. if (ret < 0) {
  1364. dev_err(bus->dev, "Disable port(s) failed: %d", ret);
  1365. return ret;
  1366. }
  1367. }
  1368. stream->state = SDW_STREAM_DISABLED;
  1369. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1370. bus = m_rt->bus;
  1371. /* Program params */
  1372. ret = sdw_program_params(bus);
  1373. if (ret < 0) {
  1374. dev_err(bus->dev, "Program params failed: %d", ret);
  1375. return ret;
  1376. }
  1377. }
  1378. return do_bank_switch(stream);
  1379. }
  1380. /**
  1381. * sdw_disable_stream() - Disable SoundWire stream
  1382. *
  1383. * @stream: Soundwire stream
  1384. *
  1385. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1386. */
  1387. int sdw_disable_stream(struct sdw_stream_runtime *stream)
  1388. {
  1389. int ret = 0;
  1390. if (!stream) {
  1391. pr_err("SoundWire: Handle not found for stream");
  1392. return -EINVAL;
  1393. }
  1394. sdw_acquire_bus_lock(stream);
  1395. ret = _sdw_disable_stream(stream);
  1396. if (ret < 0)
  1397. pr_err("Disable for stream:%s failed: %d", stream->name, ret);
  1398. sdw_release_bus_lock(stream);
  1399. return ret;
  1400. }
  1401. EXPORT_SYMBOL(sdw_disable_stream);
  1402. static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1403. {
  1404. struct sdw_master_runtime *m_rt = NULL;
  1405. struct sdw_bus *bus = NULL;
  1406. int ret = 0;
  1407. list_for_each_entry(m_rt, &stream->master_list, stream_node) {
  1408. bus = m_rt->bus;
  1409. /* De-prepare port(s) */
  1410. ret = sdw_prep_deprep_ports(m_rt, false);
  1411. if (ret < 0) {
  1412. dev_err(bus->dev, "De-prepare port(s) failed: %d", ret);
  1413. return ret;
  1414. }
  1415. /* TODO: Update this during Device-Device support */
  1416. bus->params.bandwidth -= m_rt->stream->params.rate *
  1417. m_rt->ch_count * m_rt->stream->params.bps;
  1418. /* Program params */
  1419. ret = sdw_program_params(bus);
  1420. if (ret < 0) {
  1421. dev_err(bus->dev, "Program params failed: %d", ret);
  1422. return ret;
  1423. }
  1424. }
  1425. stream->state = SDW_STREAM_DEPREPARED;
  1426. return do_bank_switch(stream);
  1427. }
  1428. /**
  1429. * sdw_deprepare_stream() - Deprepare SoundWire stream
  1430. *
  1431. * @stream: Soundwire stream
  1432. *
  1433. * Documentation/driver-api/soundwire/stream.rst explains this API in detail
  1434. */
  1435. int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
  1436. {
  1437. int ret = 0;
  1438. if (!stream) {
  1439. pr_err("SoundWire: Handle not found for stream");
  1440. return -EINVAL;
  1441. }
  1442. sdw_acquire_bus_lock(stream);
  1443. ret = _sdw_deprepare_stream(stream);
  1444. if (ret < 0)
  1445. pr_err("De-prepare for stream:%d failed: %d", ret, ret);
  1446. sdw_release_bus_lock(stream);
  1447. return ret;
  1448. }
  1449. EXPORT_SYMBOL(sdw_deprepare_stream);