stream.c 12 KB

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