stream.c 12 KB

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