stream.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018, Linaro Limited
  3. // Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  4. #include <linux/kernel.h>
  5. #include <linux/errno.h>
  6. #include <linux/slab.h>
  7. #include <linux/list.h>
  8. #include <linux/slimbus.h>
  9. #include <uapi/sound/asound.h>
  10. #include "slimbus.h"
  11. /**
  12. * struct segdist_code - Segment Distributions code from
  13. * Table 20 of SLIMbus Specs Version 2.0
  14. *
  15. * @ratem: Channel Rate Multipler(Segments per Superframe)
  16. * @seg_interval: Number of slots between the first Slot of Segment
  17. * and the first slot of the next consecutive Segment.
  18. * @segdist_code: Segment Distribution Code SD[11:0]
  19. * @seg_offset_mask: Segment offset mask in SD[11:0]
  20. * @segdist_codes: List of all possible Segmet Distribution codes.
  21. */
  22. static const struct segdist_code {
  23. int ratem;
  24. int seg_interval;
  25. int segdist_code;
  26. u32 seg_offset_mask;
  27. } segdist_codes[] = {
  28. {1, 1536, 0x200, 0xdff},
  29. {2, 768, 0x100, 0xcff},
  30. {4, 384, 0x080, 0xc7f},
  31. {8, 192, 0x040, 0xc3f},
  32. {16, 96, 0x020, 0xc1f},
  33. {32, 48, 0x010, 0xc0f},
  34. {64, 24, 0x008, 0xc07},
  35. {128, 12, 0x004, 0xc03},
  36. {256, 6, 0x002, 0xc01},
  37. {512, 3, 0x001, 0xc00},
  38. {3, 512, 0xe00, 0x1ff},
  39. {6, 256, 0xd00, 0x0ff},
  40. {12, 128, 0xc80, 0x07f},
  41. {24, 64, 0xc40, 0x03f},
  42. {48, 32, 0xc20, 0x01f},
  43. {96, 16, 0xc10, 0x00f},
  44. {192, 8, 0xc08, 0x007},
  45. {364, 4, 0xc04, 0x003},
  46. {768, 2, 0xc02, 0x001},
  47. };
  48. /**
  49. * slim_stream_allocate() - Allocate a new SLIMbus Stream
  50. * @dev:Slim device to be associated with
  51. * @name: name of the stream
  52. *
  53. * This is very first call for SLIMbus streaming, this API will allocate
  54. * a new SLIMbus stream and return a valid stream runtime pointer for client
  55. * to use it in subsequent stream apis. state of stream is set to ALLOCATED
  56. *
  57. * Return: valid pointer on success and error code on failure.
  58. * From ASoC DPCM framework, this state is linked to startup() operation.
  59. */
  60. struct slim_stream_runtime *slim_stream_allocate(struct slim_device *dev,
  61. const char *name)
  62. {
  63. struct slim_stream_runtime *rt;
  64. rt = kzalloc(sizeof(*rt), GFP_KERNEL);
  65. if (!rt)
  66. return ERR_PTR(-ENOMEM);
  67. rt->name = kasprintf(GFP_KERNEL, "slim-%s", name);
  68. if (!rt->name) {
  69. kfree(rt);
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. rt->dev = dev;
  73. spin_lock(&dev->stream_list_lock);
  74. list_add_tail(&rt->node, &dev->stream_list);
  75. spin_unlock(&dev->stream_list_lock);
  76. return rt;
  77. }
  78. EXPORT_SYMBOL_GPL(slim_stream_allocate);
  79. static int slim_connect_port_channel(struct slim_stream_runtime *stream,
  80. struct slim_port *port)
  81. {
  82. struct slim_device *sdev = stream->dev;
  83. u8 wbuf[2];
  84. struct slim_val_inf msg = {0, 2, NULL, wbuf, NULL};
  85. u8 mc = SLIM_MSG_MC_CONNECT_SOURCE;
  86. DEFINE_SLIM_LDEST_TXN(txn, mc, 6, stream->dev->laddr, &msg);
  87. if (port->direction == SLIM_PORT_SINK)
  88. txn.mc = SLIM_MSG_MC_CONNECT_SINK;
  89. wbuf[0] = port->id;
  90. wbuf[1] = port->ch.id;
  91. port->ch.state = SLIM_CH_STATE_ASSOCIATED;
  92. port->state = SLIM_PORT_UNCONFIGURED;
  93. return slim_do_transfer(sdev->ctrl, &txn);
  94. }
  95. static int slim_disconnect_port(struct slim_stream_runtime *stream,
  96. struct slim_port *port)
  97. {
  98. struct slim_device *sdev = stream->dev;
  99. u8 wbuf[1];
  100. struct slim_val_inf msg = {0, 1, NULL, wbuf, NULL};
  101. u8 mc = SLIM_MSG_MC_DISCONNECT_PORT;
  102. DEFINE_SLIM_LDEST_TXN(txn, mc, 5, stream->dev->laddr, &msg);
  103. wbuf[0] = port->id;
  104. port->ch.state = SLIM_CH_STATE_DISCONNECTED;
  105. port->state = SLIM_PORT_DISCONNECTED;
  106. return slim_do_transfer(sdev->ctrl, &txn);
  107. }
  108. static int slim_deactivate_remove_channel(struct slim_stream_runtime *stream,
  109. struct slim_port *port)
  110. {
  111. struct slim_device *sdev = stream->dev;
  112. u8 wbuf[1];
  113. struct slim_val_inf msg = {0, 1, NULL, wbuf, NULL};
  114. u8 mc = SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL;
  115. DEFINE_SLIM_LDEST_TXN(txn, mc, 5, stream->dev->laddr, &msg);
  116. int ret;
  117. wbuf[0] = port->ch.id;
  118. ret = slim_do_transfer(sdev->ctrl, &txn);
  119. if (ret)
  120. return ret;
  121. txn.mc = SLIM_MSG_MC_NEXT_REMOVE_CHANNEL;
  122. port->ch.state = SLIM_CH_STATE_REMOVED;
  123. return slim_do_transfer(sdev->ctrl, &txn);
  124. }
  125. static int slim_get_prate_code(int rate)
  126. {
  127. int ratem, ratefam, pr, exp = 0;
  128. bool done = false, exact = true;
  129. ratem = ((rate == SLIM_FREQ_441) || (rate == SLIM_FREQ_882)) ?
  130. (rate/SLIM_BASE_FREQ_11) : (rate/SLIM_BASE_FREQ_4);
  131. ratefam = ((rate == SLIM_FREQ_441) || (rate == SLIM_FREQ_882)) ?
  132. 2 : 1;
  133. while (!done) {
  134. while ((ratem & 0x1) != 0x1) {
  135. ratem >>= 1;
  136. exp++;
  137. }
  138. if (ratem > 3) {
  139. ratem++;
  140. exact = false;
  141. } else {
  142. done = true;
  143. }
  144. }
  145. if (ratefam == 1) {
  146. if (ratem == 1) {
  147. pr = 0x10;
  148. } else {
  149. pr = 0;
  150. exp++;
  151. }
  152. } else {
  153. pr = 8;
  154. exp++;
  155. }
  156. if (exp <= 7) {
  157. pr |= exp;
  158. if (exact)
  159. pr |= 0x80;
  160. } else {
  161. pr = 0;
  162. }
  163. return pr;
  164. }
  165. /**
  166. * slim_stream_prepare() - Prepare a SLIMbus Stream
  167. *
  168. * @rt: instance of slim stream runtime to configure
  169. * @cfg: new configuration for the stream
  170. *
  171. * This API will configure SLIMbus stream with config parameters from cfg.
  172. * return zero on success and error code on failure. From ASoC DPCM framework,
  173. * this state is linked to hw_params() operation.
  174. */
  175. int slim_stream_prepare(struct slim_stream_runtime *rt,
  176. struct slim_stream_config *cfg)
  177. {
  178. struct slim_controller *ctrl;
  179. struct slim_port *port;
  180. int num_ports, i, port_id;
  181. if (!rt || !cfg) {
  182. pr_err("%s: Stream or cfg is NULL, Check from client side\n", __func__);
  183. return -EINVAL;
  184. }
  185. ctrl = rt->dev->ctrl;
  186. if (rt->ports) {
  187. dev_err(&rt->dev->dev, "Stream already Prepared\n");
  188. return -EINVAL;
  189. }
  190. num_ports = hweight32(cfg->port_mask);
  191. rt->ports = kcalloc(num_ports, sizeof(*port), GFP_KERNEL);
  192. if (!rt->ports)
  193. return -ENOMEM;
  194. rt->num_ports = num_ports;
  195. rt->rate = cfg->rate;
  196. rt->bps = cfg->bps;
  197. rt->direction = cfg->direction;
  198. if (cfg->rate % ctrl->a_framer->superfreq) {
  199. /*
  200. * data rate not exactly multiple of super frame,
  201. * use PUSH/PULL protocol
  202. */
  203. if (cfg->direction == SNDRV_PCM_STREAM_PLAYBACK)
  204. rt->prot = SLIM_PROTO_PUSH;
  205. else
  206. rt->prot = SLIM_PROTO_PULL;
  207. } else {
  208. rt->prot = SLIM_PROTO_ISO;
  209. }
  210. rt->ratem = cfg->rate/ctrl->a_framer->superfreq;
  211. i = 0;
  212. for_each_set_bit(port_id, &cfg->port_mask, SLIM_DEVICE_MAX_PORTS) {
  213. port = &rt->ports[i];
  214. port->state = SLIM_PORT_DISCONNECTED;
  215. port->id = port_id;
  216. port->ch.prrate = slim_get_prate_code(cfg->rate);
  217. port->ch.id = cfg->chs[i];
  218. port->ch.data_fmt = SLIM_CH_DATA_FMT_NOT_DEFINED;
  219. port->ch.aux_fmt = SLIM_CH_AUX_FMT_NOT_APPLICABLE;
  220. port->ch.state = SLIM_CH_STATE_ALLOCATED;
  221. if (cfg->direction == SNDRV_PCM_STREAM_PLAYBACK)
  222. port->direction = SLIM_PORT_SINK;
  223. else
  224. port->direction = SLIM_PORT_SOURCE;
  225. slim_connect_port_channel(rt, port);
  226. i++;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(slim_stream_prepare);
  231. static int slim_define_channel_content(struct slim_stream_runtime *stream,
  232. struct slim_port *port)
  233. {
  234. struct slim_device *sdev = stream->dev;
  235. u8 wbuf[4];
  236. struct slim_val_inf msg = {0, 4, NULL, wbuf, NULL};
  237. u8 mc = SLIM_MSG_MC_NEXT_DEFINE_CONTENT;
  238. DEFINE_SLIM_LDEST_TXN(txn, mc, 8, stream->dev->laddr, &msg);
  239. wbuf[0] = port->ch.id;
  240. wbuf[1] = port->ch.prrate;
  241. /* Frequency Locked for ISO Protocol */
  242. if (stream->prot != SLIM_PROTO_ISO)
  243. wbuf[1] |= SLIM_CHANNEL_CONTENT_FL;
  244. wbuf[2] = port->ch.data_fmt | (port->ch.aux_fmt << 4);
  245. wbuf[3] = stream->bps/SLIM_SLOT_LEN_BITS;
  246. port->ch.state = SLIM_CH_STATE_CONTENT_DEFINED;
  247. return slim_do_transfer(sdev->ctrl, &txn);
  248. }
  249. static int slim_get_segdist_code(int ratem)
  250. {
  251. int i;
  252. for (i = 0; i < ARRAY_SIZE(segdist_codes); i++) {
  253. if (segdist_codes[i].ratem == ratem)
  254. return segdist_codes[i].segdist_code;
  255. }
  256. return -EINVAL;
  257. }
  258. static int slim_define_channel(struct slim_stream_runtime *stream,
  259. struct slim_port *port)
  260. {
  261. struct slim_device *sdev = stream->dev;
  262. u8 wbuf[4];
  263. struct slim_val_inf msg = {0, 4, NULL, wbuf, NULL};
  264. u8 mc = SLIM_MSG_MC_NEXT_DEFINE_CHANNEL;
  265. DEFINE_SLIM_LDEST_TXN(txn, mc, 8, stream->dev->laddr, &msg);
  266. port->ch.seg_dist = slim_get_segdist_code(stream->ratem);
  267. wbuf[0] = port->ch.id;
  268. wbuf[1] = port->ch.seg_dist & 0xFF;
  269. wbuf[2] = (stream->prot << 4) | ((port->ch.seg_dist & 0xF00) >> 8);
  270. if (stream->prot == SLIM_PROTO_ISO)
  271. wbuf[3] = stream->bps/SLIM_SLOT_LEN_BITS;
  272. else
  273. wbuf[3] = stream->bps/SLIM_SLOT_LEN_BITS + 1;
  274. port->ch.state = SLIM_CH_STATE_DEFINED;
  275. return slim_do_transfer(sdev->ctrl, &txn);
  276. }
  277. static int slim_activate_channel(struct slim_stream_runtime *stream,
  278. struct slim_port *port)
  279. {
  280. struct slim_device *sdev = stream->dev;
  281. u8 wbuf[1];
  282. struct slim_val_inf msg = {0, 1, NULL, wbuf, NULL};
  283. u8 mc = SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL;
  284. DEFINE_SLIM_LDEST_TXN(txn, mc, 5, stream->dev->laddr, &msg);
  285. txn.msg->num_bytes = 1;
  286. txn.msg->wbuf = wbuf;
  287. wbuf[0] = port->ch.id;
  288. port->ch.state = SLIM_CH_STATE_ACTIVE;
  289. return slim_do_transfer(sdev->ctrl, &txn);
  290. }
  291. /**
  292. * slim_stream_enable() - Enable a prepared SLIMbus Stream
  293. *
  294. * @stream: instance of slim stream runtime to enable
  295. *
  296. * This API will enable all the ports and channels associated with
  297. * SLIMbus stream
  298. *
  299. * Return: zero on success and error code on failure. From ASoC DPCM framework,
  300. * this state is linked to trigger() start operation.
  301. */
  302. int slim_stream_enable(struct slim_stream_runtime *stream)
  303. {
  304. DEFINE_SLIM_BCAST_TXN(txn, SLIM_MSG_MC_BEGIN_RECONFIGURATION,
  305. 3, SLIM_LA_MANAGER, NULL);
  306. struct slim_controller *ctrl;
  307. int ret, i;
  308. if (!stream) {
  309. pr_err("%s: Stream is NULL, Check from client side\n", __func__);
  310. return -EINVAL;
  311. }
  312. ctrl = stream->dev->ctrl;
  313. if (ctrl->enable_stream) {
  314. mutex_lock(&ctrl->stream_lock);
  315. ret = ctrl->enable_stream(stream);
  316. if (ret) {
  317. mutex_unlock(&ctrl->stream_lock);
  318. dev_err(ctrl->dev, "%s: ctrl->enable_stream ret %d\n",
  319. __func__, ret);
  320. return ret;
  321. }
  322. for (i = 0; i < stream->num_ports; i++)
  323. stream->ports[i].ch.state = SLIM_CH_STATE_ACTIVE;
  324. mutex_unlock(&ctrl->stream_lock);
  325. return ret;
  326. }
  327. ret = slim_do_transfer(ctrl, &txn);
  328. if (ret) {
  329. dev_err(ctrl->dev, "%s: %d slim_do_transfer failed %d\n", __func__, __LINE__, ret);
  330. return ret;
  331. }
  332. /* define channels first before activating them */
  333. for (i = 0; i < stream->num_ports; i++) {
  334. struct slim_port *port = &stream->ports[i];
  335. slim_define_channel(stream, port);
  336. slim_define_channel_content(stream, port);
  337. }
  338. for (i = 0; i < stream->num_ports; i++) {
  339. struct slim_port *port = &stream->ports[i];
  340. slim_activate_channel(stream, port);
  341. port->state = SLIM_PORT_CONFIGURED;
  342. }
  343. txn.mc = SLIM_MSG_MC_RECONFIGURE_NOW;
  344. return slim_do_transfer(ctrl, &txn);
  345. }
  346. EXPORT_SYMBOL_GPL(slim_stream_enable);
  347. /**
  348. * slim_stream_disable() - Disable a SLIMbus Stream
  349. *
  350. * @stream: instance of slim stream runtime to disable
  351. *
  352. * This API will disable all the ports and channels associated with
  353. * SLIMbus stream
  354. *
  355. * Return: zero on success and error code on failure. From ASoC DPCM framework,
  356. * this state is linked to trigger() pause operation.
  357. */
  358. int slim_stream_disable(struct slim_stream_runtime *stream)
  359. {
  360. DEFINE_SLIM_BCAST_TXN(txn, SLIM_MSG_MC_BEGIN_RECONFIGURATION,
  361. 3, SLIM_LA_MANAGER, NULL);
  362. struct slim_controller *ctrl;
  363. int ret, i;
  364. if (!stream) {
  365. pr_err("%s: Stream is NULL, Check from client side\n", __func__);
  366. return -EINVAL;
  367. }
  368. if (!stream->ports || !stream->num_ports) {
  369. pr_err("%s: Stream port is NULL %d\n", __func__, stream->num_ports);
  370. return -EINVAL;
  371. }
  372. ctrl = stream->dev->ctrl;
  373. if (ctrl->disable_stream) {
  374. mutex_lock(&ctrl->stream_lock);
  375. ret = ctrl->disable_stream(stream);
  376. if (ret) {
  377. dev_err(ctrl->dev, "%s: disable_stream failed %d\n", __func__, ret);
  378. mutex_unlock(&ctrl->stream_lock);
  379. return ret;
  380. }
  381. mutex_unlock(&ctrl->stream_lock);
  382. }
  383. ret = slim_do_transfer(ctrl, &txn);
  384. if (ret) {
  385. dev_err(ctrl->dev, "%s: %d slim_do_transfer failed %d\n", __func__, __LINE__, ret);
  386. return ret;
  387. }
  388. for (i = 0; i < stream->num_ports; i++)
  389. slim_deactivate_remove_channel(stream, &stream->ports[i]);
  390. txn.mc = SLIM_MSG_MC_RECONFIGURE_NOW;
  391. return slim_do_transfer(ctrl, &txn);
  392. }
  393. EXPORT_SYMBOL_GPL(slim_stream_disable);
  394. /**
  395. * slim_stream_unprepare() - Un-prepare a SLIMbus Stream
  396. *
  397. * @stream: instance of slim stream runtime to unprepare
  398. *
  399. * This API will un allocate all the ports and channels associated with
  400. * SLIMbus stream
  401. *
  402. * Return: zero on success and error code on failure. From ASoC DPCM framework,
  403. * this state is linked to trigger() stop operation.
  404. */
  405. int slim_stream_unprepare(struct slim_stream_runtime *stream)
  406. {
  407. int i;
  408. if (!stream) {
  409. pr_err("%s: Stream is NULL, Check from client side\n", __func__);
  410. return -EINVAL;
  411. }
  412. if (!stream->ports || !stream->num_ports) {
  413. pr_err("%s: Stream port is NULL %d\n", __func__, stream->num_ports);
  414. return -EINVAL;
  415. }
  416. for (i = 0; i < stream->num_ports; i++)
  417. slim_disconnect_port(stream, &stream->ports[i]);
  418. kfree(stream->ports);
  419. stream->ports = NULL;
  420. stream->num_ports = 0;
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(slim_stream_unprepare);
  424. /**
  425. * slim_stream_unprepare_disconnect_port() - Un-prepare and disconnect
  426. * selected SLIMbus Stream ports
  427. *
  428. * @stream: instance of slim stream runtime to unprepare
  429. * @disconnect_ports: disconnect the ports of the stream
  430. * @is_session_completed: free the ports allocated to the stream
  431. *
  432. * This API will disconnect all the ports and un allocate all the ports and
  433. * channels associated with SLIMbus stream.
  434. *
  435. * Return: zero on success and error code on failure. From ASoC DPCM framework,
  436. * this state is linked to trigger() stop operation.
  437. */
  438. int slim_stream_unprepare_disconnect_port(struct slim_stream_runtime *stream,
  439. bool disconnect_ports, bool is_session_completed)
  440. {
  441. int i;
  442. if (!stream) {
  443. pr_err("%s: Stream is NULL, Check from client side\n", __func__);
  444. return -EINVAL;
  445. }
  446. if (!stream->ports || !stream->num_ports) {
  447. pr_err("%s: Stream port is NULL %d\n", __func__, stream->num_ports);
  448. return -EINVAL;
  449. }
  450. if (disconnect_ports) {
  451. for (i = 0; i < stream->num_ports; i++)
  452. slim_disconnect_port(stream, &stream->ports[i]);
  453. }
  454. if (is_session_completed) {
  455. kfree(stream->ports);
  456. stream->ports = NULL;
  457. stream->num_ports = 0;
  458. }
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(slim_stream_unprepare_disconnect_port);
  462. /**
  463. * slim_stream_free() - Free a SLIMbus Stream
  464. *
  465. * @stream: instance of slim stream runtime to free
  466. *
  467. * This API will un allocate all the memory associated with
  468. * slim stream runtime, user is not allowed to make an dereference
  469. * to stream after this call.
  470. *
  471. * Return: zero on success and error code on failure. From ASoC DPCM framework,
  472. * this state is linked to shutdown() operation.
  473. */
  474. int slim_stream_free(struct slim_stream_runtime *stream)
  475. {
  476. struct slim_device *sdev;
  477. if (!stream) {
  478. pr_err("%s: Stream is NULL, Check from client side\n", __func__);
  479. return -EINVAL;
  480. }
  481. sdev = stream->dev;
  482. spin_lock(&sdev->stream_list_lock);
  483. list_del(&stream->node);
  484. spin_unlock(&sdev->stream_list_lock);
  485. kfree(stream->name);
  486. kfree(stream);
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(slim_stream_free);