stream.c 49 KB

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