1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018, Linaro Limited
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>
13 * struct segdist_code - Segment Distributions code from
14 * Table 20 of SLIMbus Specs Version 2.0
16 * @ratem: Channel Rate Multipler(Segments per Superframe)
17 * @seg_interval: Number of slots between the first Slot of Segment
18 * and the first slot of the next consecutive Segment.
19 * @segdist_code: Segment Distribution Code SD[11:0]
20 * @seg_offset_mask: Segment offset mask in SD[11:0]
21 * @segdist_codes: List of all possible Segmet Distribution codes.
23 static const struct segdist_code
{
30 {1, 1536, 0x200, 0xdff},
31 {2, 768, 0x100, 0xcff},
32 {4, 384, 0x080, 0xc7f},
33 {8, 192, 0x040, 0xc3f},
34 {16, 96, 0x020, 0xc1f},
35 {32, 48, 0x010, 0xc0f},
36 {64, 24, 0x008, 0xc07},
37 {128, 12, 0x004, 0xc03},
38 {256, 6, 0x002, 0xc01},
39 {512, 3, 0x001, 0xc00},
40 {3, 512, 0xe00, 0x1ff},
41 {6, 256, 0xd00, 0x0ff},
42 {12, 128, 0xc80, 0x07f},
43 {24, 64, 0xc40, 0x03f},
44 {48, 32, 0xc20, 0x01f},
45 {96, 16, 0xc10, 0x00f},
46 {192, 8, 0xc08, 0x007},
47 {364, 4, 0xc04, 0x003},
48 {768, 2, 0xc02, 0x001},
52 * Presence Rate table for all Natural Frequencies
53 * The Presence rate of a constant bitrate stream is mean flow rate of the
54 * stream expressed in occupied Segments of that Data Channel per second.
55 * Table 66 from SLIMbus 2.0 Specs
57 * Index of the table corresponds to Presence rate code for the respective rate
60 static const int slim_presence_rate_table
[] = {
61 0, /* Not Indicated */
88 * slim_stream_allocate() - Allocate a new SLIMbus Stream
89 * @dev:Slim device to be associated with
90 * @name: name of the stream
92 * This is very first call for SLIMbus streaming, this API will allocate
93 * a new SLIMbus stream and return a valid stream runtime pointer for client
94 * to use it in subsequent stream apis. state of stream is set to ALLOCATED
96 * Return: valid pointer on success and error code on failure.
97 * From ASoC DPCM framework, this state is linked to startup() operation.
99 struct slim_stream_runtime
*slim_stream_allocate(struct slim_device
*dev
,
102 struct slim_stream_runtime
*rt
;
104 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
106 return ERR_PTR(-ENOMEM
);
108 rt
->name
= kasprintf(GFP_KERNEL
, "slim-%s", name
);
111 return ERR_PTR(-ENOMEM
);
115 spin_lock(&dev
->stream_list_lock
);
116 list_add_tail(&rt
->node
, &dev
->stream_list
);
117 spin_unlock(&dev
->stream_list_lock
);
121 EXPORT_SYMBOL_GPL(slim_stream_allocate
);
123 static int slim_connect_port_channel(struct slim_stream_runtime
*stream
,
124 struct slim_port
*port
)
126 struct slim_device
*sdev
= stream
->dev
;
128 struct slim_val_inf msg
= {0, 2, NULL
, wbuf
, NULL
};
129 u8 mc
= SLIM_MSG_MC_CONNECT_SOURCE
;
130 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 6, stream
->dev
->laddr
, &msg
);
132 if (port
->direction
== SLIM_PORT_SINK
)
133 txn
.mc
= SLIM_MSG_MC_CONNECT_SINK
;
136 wbuf
[1] = port
->ch
.id
;
137 port
->ch
.state
= SLIM_CH_STATE_ASSOCIATED
;
138 port
->state
= SLIM_PORT_UNCONFIGURED
;
140 return slim_do_transfer(sdev
->ctrl
, &txn
);
143 static int slim_disconnect_port(struct slim_stream_runtime
*stream
,
144 struct slim_port
*port
)
146 struct slim_device
*sdev
= stream
->dev
;
148 struct slim_val_inf msg
= {0, 1, NULL
, wbuf
, NULL
};
149 u8 mc
= SLIM_MSG_MC_DISCONNECT_PORT
;
150 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 5, stream
->dev
->laddr
, &msg
);
153 port
->ch
.state
= SLIM_CH_STATE_DISCONNECTED
;
154 port
->state
= SLIM_PORT_DISCONNECTED
;
156 return slim_do_transfer(sdev
->ctrl
, &txn
);
159 static int slim_deactivate_remove_channel(struct slim_stream_runtime
*stream
,
160 struct slim_port
*port
)
162 struct slim_device
*sdev
= stream
->dev
;
164 struct slim_val_inf msg
= {0, 1, NULL
, wbuf
, NULL
};
165 u8 mc
= SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL
;
166 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 5, stream
->dev
->laddr
, &msg
);
169 wbuf
[0] = port
->ch
.id
;
170 ret
= slim_do_transfer(sdev
->ctrl
, &txn
);
174 txn
.mc
= SLIM_MSG_MC_NEXT_REMOVE_CHANNEL
;
175 port
->ch
.state
= SLIM_CH_STATE_REMOVED
;
177 return slim_do_transfer(sdev
->ctrl
, &txn
);
180 static int slim_get_prate_code(int rate
)
184 for (i
= 0; i
< ARRAY_SIZE(slim_presence_rate_table
); i
++) {
185 if (rate
== slim_presence_rate_table
[i
])
193 * slim_stream_prepare() - Prepare a SLIMbus Stream
195 * @rt: instance of slim stream runtime to configure
196 * @cfg: new configuration for the stream
198 * This API will configure SLIMbus stream with config parameters from cfg.
199 * return zero on success and error code on failure. From ASoC DPCM framework,
200 * this state is linked to hw_params() operation.
202 int slim_stream_prepare(struct slim_stream_runtime
*rt
,
203 struct slim_stream_config
*cfg
)
205 struct slim_controller
*ctrl
= rt
->dev
->ctrl
;
206 struct slim_port
*port
;
207 int num_ports
, i
, port_id
;
210 dev_err(&rt
->dev
->dev
, "Stream already Prepared\n");
214 num_ports
= hweight32(cfg
->port_mask
);
215 rt
->ports
= kcalloc(num_ports
, sizeof(*port
), GFP_KERNEL
);
219 rt
->num_ports
= num_ports
;
220 rt
->rate
= cfg
->rate
;
222 rt
->direction
= cfg
->direction
;
224 if (cfg
->rate
% ctrl
->a_framer
->superfreq
) {
226 * data rate not exactly multiple of super frame,
227 * use PUSH/PULL protocol
229 if (cfg
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
230 rt
->prot
= SLIM_PROTO_PUSH
;
232 rt
->prot
= SLIM_PROTO_PULL
;
234 rt
->prot
= SLIM_PROTO_ISO
;
237 rt
->ratem
= cfg
->rate
/ctrl
->a_framer
->superfreq
;
240 for_each_set_bit(port_id
, &cfg
->port_mask
, SLIM_DEVICE_MAX_PORTS
) {
241 port
= &rt
->ports
[i
];
242 port
->state
= SLIM_PORT_DISCONNECTED
;
244 port
->ch
.prrate
= slim_get_prate_code(cfg
->rate
);
245 port
->ch
.id
= cfg
->chs
[i
];
246 port
->ch
.data_fmt
= SLIM_CH_DATA_FMT_NOT_DEFINED
;
247 port
->ch
.aux_fmt
= SLIM_CH_AUX_FMT_NOT_APPLICABLE
;
248 port
->ch
.state
= SLIM_CH_STATE_ALLOCATED
;
250 if (cfg
->direction
== SNDRV_PCM_STREAM_PLAYBACK
)
251 port
->direction
= SLIM_PORT_SINK
;
253 port
->direction
= SLIM_PORT_SOURCE
;
255 slim_connect_port_channel(rt
, port
);
261 EXPORT_SYMBOL_GPL(slim_stream_prepare
);
263 static int slim_define_channel_content(struct slim_stream_runtime
*stream
,
264 struct slim_port
*port
)
266 struct slim_device
*sdev
= stream
->dev
;
268 struct slim_val_inf msg
= {0, 4, NULL
, wbuf
, NULL
};
269 u8 mc
= SLIM_MSG_MC_NEXT_DEFINE_CONTENT
;
270 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 8, stream
->dev
->laddr
, &msg
);
272 wbuf
[0] = port
->ch
.id
;
273 wbuf
[1] = port
->ch
.prrate
;
275 /* Frequency Locked for ISO Protocol */
276 if (stream
->prot
!= SLIM_PROTO_ISO
)
277 wbuf
[1] |= SLIM_CHANNEL_CONTENT_FL
;
279 wbuf
[2] = port
->ch
.data_fmt
| (port
->ch
.aux_fmt
<< 4);
280 wbuf
[3] = stream
->bps
/SLIM_SLOT_LEN_BITS
;
281 port
->ch
.state
= SLIM_CH_STATE_CONTENT_DEFINED
;
283 return slim_do_transfer(sdev
->ctrl
, &txn
);
286 static int slim_get_segdist_code(int ratem
)
290 for (i
= 0; i
< ARRAY_SIZE(segdist_codes
); i
++) {
291 if (segdist_codes
[i
].ratem
== ratem
)
292 return segdist_codes
[i
].segdist_code
;
298 static int slim_define_channel(struct slim_stream_runtime
*stream
,
299 struct slim_port
*port
)
301 struct slim_device
*sdev
= stream
->dev
;
303 struct slim_val_inf msg
= {0, 4, NULL
, wbuf
, NULL
};
304 u8 mc
= SLIM_MSG_MC_NEXT_DEFINE_CHANNEL
;
305 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 8, stream
->dev
->laddr
, &msg
);
307 port
->ch
.seg_dist
= slim_get_segdist_code(stream
->ratem
);
309 wbuf
[0] = port
->ch
.id
;
310 wbuf
[1] = port
->ch
.seg_dist
& 0xFF;
311 wbuf
[2] = (stream
->prot
<< 4) | ((port
->ch
.seg_dist
& 0xF00) >> 8);
312 if (stream
->prot
== SLIM_PROTO_ISO
)
313 wbuf
[3] = stream
->bps
/SLIM_SLOT_LEN_BITS
;
315 wbuf
[3] = stream
->bps
/SLIM_SLOT_LEN_BITS
+ 1;
317 port
->ch
.state
= SLIM_CH_STATE_DEFINED
;
319 return slim_do_transfer(sdev
->ctrl
, &txn
);
322 static int slim_activate_channel(struct slim_stream_runtime
*stream
,
323 struct slim_port
*port
)
325 struct slim_device
*sdev
= stream
->dev
;
327 struct slim_val_inf msg
= {0, 1, NULL
, wbuf
, NULL
};
328 u8 mc
= SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL
;
329 DEFINE_SLIM_LDEST_TXN(txn
, mc
, 5, stream
->dev
->laddr
, &msg
);
331 txn
.msg
->num_bytes
= 1;
332 txn
.msg
->wbuf
= wbuf
;
333 wbuf
[0] = port
->ch
.id
;
334 port
->ch
.state
= SLIM_CH_STATE_ACTIVE
;
336 return slim_do_transfer(sdev
->ctrl
, &txn
);
340 * slim_stream_enable() - Enable a prepared SLIMbus Stream
342 * @stream: instance of slim stream runtime to enable
344 * This API will enable all the ports and channels associated with
347 * Return: zero on success and error code on failure. From ASoC DPCM framework,
348 * this state is linked to trigger() start operation.
350 int slim_stream_enable(struct slim_stream_runtime
*stream
)
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
;
357 if (ctrl
->enable_stream
) {
358 ret
= ctrl
->enable_stream(stream
);
362 for (i
= 0; i
< stream
->num_ports
; i
++)
363 stream
->ports
[i
].ch
.state
= SLIM_CH_STATE_ACTIVE
;
368 ret
= slim_do_transfer(ctrl
, &txn
);
372 /* define channels first before activating them */
373 for (i
= 0; i
< stream
->num_ports
; i
++) {
374 struct slim_port
*port
= &stream
->ports
[i
];
376 slim_define_channel(stream
, port
);
377 slim_define_channel_content(stream
, port
);
380 for (i
= 0; i
< stream
->num_ports
; i
++) {
381 struct slim_port
*port
= &stream
->ports
[i
];
383 slim_activate_channel(stream
, port
);
384 port
->state
= SLIM_PORT_CONFIGURED
;
386 txn
.mc
= SLIM_MSG_MC_RECONFIGURE_NOW
;
388 return slim_do_transfer(ctrl
, &txn
);
390 EXPORT_SYMBOL_GPL(slim_stream_enable
);
393 * slim_stream_disable() - Disable a SLIMbus Stream
395 * @stream: instance of slim stream runtime to disable
397 * This API will disable all the ports and channels associated with
400 * Return: zero on success and error code on failure. From ASoC DPCM framework,
401 * this state is linked to trigger() pause operation.
403 int slim_stream_disable(struct slim_stream_runtime
*stream
)
405 DEFINE_SLIM_BCAST_TXN(txn
, SLIM_MSG_MC_BEGIN_RECONFIGURATION
,
406 3, SLIM_LA_MANAGER
, NULL
);
407 struct slim_controller
*ctrl
= stream
->dev
->ctrl
;
410 if (ctrl
->disable_stream
)
411 ctrl
->disable_stream(stream
);
413 ret
= slim_do_transfer(ctrl
, &txn
);
417 for (i
= 0; i
< stream
->num_ports
; i
++)
418 slim_deactivate_remove_channel(stream
, &stream
->ports
[i
]);
420 txn
.mc
= SLIM_MSG_MC_RECONFIGURE_NOW
;
422 return slim_do_transfer(ctrl
, &txn
);
424 EXPORT_SYMBOL_GPL(slim_stream_disable
);
427 * slim_stream_unprepare() - Un-prepare a SLIMbus Stream
429 * @stream: instance of slim stream runtime to unprepare
431 * This API will un allocate all the ports and channels associated with
434 * Return: zero on success and error code on failure. From ASoC DPCM framework,
435 * this state is linked to trigger() stop operation.
437 int slim_stream_unprepare(struct slim_stream_runtime
*stream
)
441 for (i
= 0; i
< stream
->num_ports
; i
++)
442 slim_disconnect_port(stream
, &stream
->ports
[i
]);
444 kfree(stream
->ports
);
445 stream
->ports
= NULL
;
446 stream
->num_ports
= 0;
450 EXPORT_SYMBOL_GPL(slim_stream_unprepare
);
453 * slim_stream_free() - Free a SLIMbus Stream
455 * @stream: instance of slim stream runtime to free
457 * This API will un allocate all the memory associated with
458 * slim stream runtime, user is not allowed to make an dereference
459 * to stream after this call.
461 * Return: zero on success and error code on failure. From ASoC DPCM framework,
462 * this state is linked to shutdown() operation.
464 int slim_stream_free(struct slim_stream_runtime
*stream
)
466 struct slim_device
*sdev
= stream
->dev
;
468 spin_lock(&sdev
->stream_list_lock
);
469 list_del(&stream
->node
);
470 spin_unlock(&sdev
->stream_list_lock
);
477 EXPORT_SYMBOL_GPL(slim_stream_free
);