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