]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - sound/hda/ext/hdac_ext_stream.c
ALSA: hdac: Add support to enable SPIB for hdac ext stream
[mirror_ubuntu-artful-kernel.git] / sound / hda / ext / hdac_ext_stream.c
CommitLineData
df203a4e
JK
1/*
2 * hdac-ext-stream.c - HD-audio extended stream operations.
3 *
4 * Copyright (C) 2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 */
19
20#include <linux/delay.h>
e7a3484d 21#include <linux/slab.h>
df203a4e
JK
22#include <sound/pcm.h>
23#include <sound/hda_register.h>
24#include <sound/hdaudio_ext.h>
25
26/**
27 * snd_hdac_ext_stream_init - initialize each stream (aka device)
28 * @ebus: HD-audio ext core bus
29 * @stream: HD-audio ext core stream object to initialize
30 * @idx: stream index number
31 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
32 * @tag: the tag id to assign
33 *
34 * initialize the stream, if ppcap is enabled then init those and then
35 * invoke hdac stream initialization routine
36 */
37void snd_hdac_ext_stream_init(struct hdac_ext_bus *ebus,
38 struct hdac_ext_stream *stream,
39 int idx, int direction, int tag)
40{
41 struct hdac_bus *bus = &ebus->bus;
42
43 if (ebus->ppcap) {
44 stream->pphc_addr = ebus->ppcap + AZX_PPHC_BASE +
45 AZX_PPHC_INTERVAL * idx;
46
47 stream->pplc_addr = ebus->ppcap + AZX_PPLC_BASE +
48 AZX_PPLC_MULTI * ebus->num_streams +
49 AZX_PPLC_INTERVAL * idx;
50 }
51
ee8bc4df
JK
52 if (ebus->spbcap) {
53 stream->spib_addr = ebus->spbcap + AZX_SPB_BASE +
54 AZX_SPB_INTERVAL * idx +
55 AZX_SPB_SPIB;
56
57 stream->fifo_addr = ebus->spbcap + AZX_SPB_BASE +
58 AZX_SPB_INTERVAL * idx +
59 AZX_SPB_MAXFIFO;
60 }
61
df203a4e
JK
62 stream->decoupled = false;
63 snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
64}
65EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
66
e7a3484d
VK
67/**
68 * snd_hdac_ext_stream_init_all - create and initialize the stream objects
69 * for an extended hda bus
70 * @ebus: HD-audio ext core bus
71 * @start_idx: start index for streams
72 * @num_stream: number of streams to initialize
73 * @dir: direction of streams
74 */
75int snd_hdac_ext_stream_init_all(struct hdac_ext_bus *ebus, int start_idx,
76 int num_stream, int dir)
77{
78 int stream_tag = 0;
79 int i, tag, idx = start_idx;
80
81 for (i = 0; i < num_stream; i++) {
82 struct hdac_ext_stream *stream =
83 kzalloc(sizeof(*stream), GFP_KERNEL);
84 if (!stream)
85 return -ENOMEM;
86 tag = ++stream_tag;
87 snd_hdac_ext_stream_init(ebus, stream, idx, dir, tag);
88 idx++;
89 }
90
91 return 0;
92
93}
94EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
95
96/**
97 * snd_hdac_stream_free_all - free hdac extended stream objects
98 *
99 * @ebus: HD-audio ext core bus
100 */
101void snd_hdac_stream_free_all(struct hdac_ext_bus *ebus)
102{
103 struct hdac_stream *s;
104 struct hdac_ext_stream *stream;
105 struct hdac_bus *bus = ebus_to_hbus(ebus);
106
107 while (!list_empty(&bus->stream_list)) {
108 s = list_first_entry(&bus->stream_list, struct hdac_stream, list);
109 stream = stream_to_hdac_ext_stream(s);
110 list_del(&s->list);
111 kfree(stream);
112 }
113}
114EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
115
df203a4e
JK
116/**
117 * snd_hdac_ext_stream_decouple - decouple the hdac stream
118 * @ebus: HD-audio ext core bus
119 * @stream: HD-audio ext core stream object to initialize
120 * @decouple: flag to decouple
121 */
122void snd_hdac_ext_stream_decouple(struct hdac_ext_bus *ebus,
123 struct hdac_ext_stream *stream, bool decouple)
124{
125 struct hdac_stream *hstream = &stream->hstream;
126 struct hdac_bus *bus = &ebus->bus;
127
128 spin_lock_irq(&bus->reg_lock);
129 if (decouple)
130 snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, 0,
131 AZX_PPCTL_PROCEN(hstream->index));
132 else
133 snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL,
134 AZX_PPCTL_PROCEN(hstream->index), 0);
135 stream->decoupled = decouple;
136 spin_unlock_irq(&bus->reg_lock);
137}
138EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
139
140/**
141 * snd_hdac_ext_linkstream_start - start a stream
142 * @stream: HD-audio ext core stream to start
143 */
144void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
145{
146 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_RUN);
147}
148EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
149
150/**
151 * snd_hdac_ext_link_stream_clear - stop a stream DMA
152 * @stream: HD-audio ext core stream to stop
153 */
154void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
155{
156 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
157}
158EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
159
160/**
161 * snd_hdac_ext_link_stream_reset - reset a stream
162 * @stream: HD-audio ext core stream to reset
163 */
164void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
165{
166 unsigned char val;
167 int timeout;
168
169 snd_hdac_ext_link_stream_clear(stream);
170
171 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_STRST);
172 udelay(3);
173 timeout = 50;
174 do {
175 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
176 AZX_PPLCCTL_STRST;
177 if (val)
178 break;
179 udelay(3);
180 } while (--timeout);
181 val &= ~AZX_PPLCCTL_STRST;
182 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
183 udelay(3);
184
185 timeout = 50;
186 /* waiting for hardware to report that the stream is out of reset */
187 do {
188 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
189 if (!val)
190 break;
191 udelay(3);
192 } while (--timeout);
193
194}
195EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
196
197/**
198 * snd_hdac_ext_link_stream_setup - set up the SD for streaming
199 * @stream: HD-audio ext core stream to set up
200 * @fmt: stream format
201 */
202int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
203{
204 struct hdac_stream *hstream = &stream->hstream;
205 unsigned int val;
206
207 /* make sure the run bit is zero for SD */
208 snd_hdac_ext_link_stream_clear(stream);
209 /* program the stream_tag */
210 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
211 val = (val & ~AZX_PPLCCTL_STRM_MASK) |
212 (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
213 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
214
215 /* program the stream format */
216 writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
217
218 return 0;
219}
220EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
221
222/**
223 * snd_hdac_ext_link_set_stream_id - maps stream id to link output
224 * @link: HD-audio ext link to set up
225 * @stream: stream id
226 */
227void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
228 int stream)
229{
230 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
231}
232EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
233
234/**
235 * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
236 * @link: HD-audio ext link to set up
237 * @stream: stream id
238 */
239void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
240 int stream)
241{
242 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, 0, (1 << stream));
243}
244EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
245
246static struct hdac_ext_stream *
247hdac_ext_link_stream_assign(struct hdac_ext_bus *ebus,
248 struct snd_pcm_substream *substream)
249{
250 struct hdac_ext_stream *res = NULL;
251 struct hdac_stream *stream = NULL;
252 struct hdac_bus *hbus = &ebus->bus;
253
254 if (!ebus->ppcap) {
255 dev_err(hbus->dev, "stream type not supported\n");
256 return NULL;
257 }
258
259 list_for_each_entry(stream, &hbus->stream_list, list) {
260 struct hdac_ext_stream *hstream = container_of(stream,
261 struct hdac_ext_stream,
262 hstream);
263 if (stream->direction != substream->stream)
264 continue;
265
266 /* check if decoupled stream and not in use is available */
267 if (hstream->decoupled && !hstream->link_locked) {
268 res = hstream;
269 break;
270 }
271
272 if (!hstream->link_locked) {
273 snd_hdac_ext_stream_decouple(ebus, hstream, true);
274 res = hstream;
275 break;
276 }
277 }
278 if (res) {
279 spin_lock_irq(&hbus->reg_lock);
280 res->link_locked = 1;
281 res->link_substream = substream;
282 spin_unlock_irq(&hbus->reg_lock);
283 }
284 return res;
285}
286
287static struct hdac_ext_stream *
288hdac_ext_host_stream_assign(struct hdac_ext_bus *ebus,
289 struct snd_pcm_substream *substream)
290{
291 struct hdac_ext_stream *res = NULL;
292 struct hdac_stream *stream = NULL;
293 struct hdac_bus *hbus = &ebus->bus;
294 int key;
295
296 if (!ebus->ppcap) {
297 dev_err(hbus->dev, "stream type not supported\n");
298 return NULL;
299 }
300
301 /* make a non-zero unique key for the substream */
302 key = (substream->pcm->device << 16) | (substream->number << 2) |
303 (substream->stream + 1);
304
305 list_for_each_entry(stream, &hbus->stream_list, list) {
306 struct hdac_ext_stream *hstream = container_of(stream,
307 struct hdac_ext_stream,
308 hstream);
309 if (stream->direction != substream->stream)
310 continue;
311
9b06dc93 312 if (!stream->opened) {
df203a4e
JK
313 if (!hstream->decoupled)
314 snd_hdac_ext_stream_decouple(ebus, hstream, true);
315 res = hstream;
316 break;
317 }
318 }
319 if (res) {
320 spin_lock_irq(&hbus->reg_lock);
321 res->hstream.opened = 1;
322 res->hstream.running = 0;
323 res->hstream.assigned_key = key;
324 res->hstream.substream = substream;
325 spin_unlock_irq(&hbus->reg_lock);
326 }
327
328 return res;
329}
330
331/**
332 * snd_hdac_ext_stream_assign - assign a stream for the PCM
333 * @ebus: HD-audio ext core bus
334 * @substream: PCM substream to assign
335 * @type: type of stream (coupled, host or link stream)
336 *
337 * This assigns the stream based on the type (coupled/host/link), for the
338 * given PCM substream, assigns it and returns the stream object
339 *
340 * coupled: Looks for an unused stream
341 * host: Looks for an unused decoupled host stream
342 * link: Looks for an unused decoupled link stream
343 *
344 * If no stream is free, returns NULL. The function tries to keep using
345 * the same stream object when it's used beforehand. when a stream is
346 * decoupled, it becomes a host stream and link stream.
347 */
348struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_ext_bus *ebus,
349 struct snd_pcm_substream *substream,
350 int type)
351{
352 struct hdac_ext_stream *hstream = NULL;
353 struct hdac_stream *stream = NULL;
354 struct hdac_bus *hbus = &ebus->bus;
355
356 switch (type) {
357 case HDAC_EXT_STREAM_TYPE_COUPLED:
358 stream = snd_hdac_stream_assign(hbus, substream);
359 if (stream)
360 hstream = container_of(stream,
361 struct hdac_ext_stream, hstream);
362 return hstream;
363
364 case HDAC_EXT_STREAM_TYPE_HOST:
365 return hdac_ext_host_stream_assign(ebus, substream);
366
367 case HDAC_EXT_STREAM_TYPE_LINK:
368 return hdac_ext_link_stream_assign(ebus, substream);
369
370 default:
371 return NULL;
372 }
373}
374EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
375
376/**
377 * snd_hdac_ext_stream_release - release the assigned stream
378 * @stream: HD-audio ext core stream to release
379 * @type: type of stream (coupled, host or link stream)
380 *
381 * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
382 */
383void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
384{
385 struct hdac_bus *bus = stream->hstream.bus;
386 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
387
388 switch (type) {
389 case HDAC_EXT_STREAM_TYPE_COUPLED:
390 snd_hdac_stream_release(&stream->hstream);
391 break;
392
393 case HDAC_EXT_STREAM_TYPE_HOST:
394 if (stream->decoupled) {
395 snd_hdac_ext_stream_decouple(ebus, stream, false);
396 snd_hdac_stream_release(&stream->hstream);
397 }
398 break;
399
400 case HDAC_EXT_STREAM_TYPE_LINK:
401 if (stream->decoupled)
402 snd_hdac_ext_stream_decouple(ebus, stream, false);
403 spin_lock_irq(&bus->reg_lock);
404 stream->link_locked = 0;
405 stream->link_substream = NULL;
406 spin_unlock_irq(&bus->reg_lock);
407 break;
408
409 default:
410 dev_dbg(bus->dev, "Invalid type %d\n", type);
411 }
412
413}
414EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
415
416/**
417 * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
418 * @ebus: HD-audio ext core bus
419 * @enable: flag to enable/disable SPIB
420 * @index: stream index for which SPIB need to be enabled
421 */
422void snd_hdac_ext_stream_spbcap_enable(struct hdac_ext_bus *ebus,
423 bool enable, int index)
424{
425 u32 mask = 0;
426 u32 register_mask = 0;
427 struct hdac_bus *bus = &ebus->bus;
428
429 if (!ebus->spbcap) {
430 dev_err(bus->dev, "Address of SPB capability is NULL");
431 return;
432 }
433
434 mask |= (1 << index);
435
a7e3dd85 436 register_mask = readl(ebus->spbcap + AZX_REG_SPB_SPBFCCTL);
df203a4e
JK
437
438 mask |= register_mask;
439
440 if (enable)
441 snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
442 else
443 snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
444}
445EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
446
ee8bc4df
JK
447/**
448 * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
449 * @ebus: HD-audio ext core bus
450 * @stream: hdac_ext_stream
451 * @value: spib value to set
452 */
453int snd_hdac_ext_stream_set_spib(struct hdac_ext_bus *ebus,
454 struct hdac_ext_stream *stream, u32 value)
455{
456 struct hdac_bus *bus = &ebus->bus;
457
458 if (!ebus->spbcap) {
459 dev_err(bus->dev, "Address of SPB capability is NULL");
460 return -EINVAL;
461 }
462
463 writel(value, stream->spib_addr);
464
465 return 0;
466}
467EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
468
469/**
470 * snd_hdac_ext_stream_set_spbmaxfifo - sets the spib value of a stream
471 * @ebus: HD-audio ext core bus
472 * @stream: hdac_ext_stream
473 *
474 * Return maxfifo for the stream
475 */
476int snd_hdac_ext_stream_set_spbmaxfifo(struct hdac_ext_bus *ebus,
477 struct hdac_ext_stream *stream)
478{
479 struct hdac_bus *bus = &ebus->bus;
480
481 if (!ebus->spbcap) {
482 dev_err(bus->dev, "Address of SPB capability is NULL");
483 return -EINVAL;
484 }
485
486 return readl(stream->fifo_addr);
487}
488EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spbmaxfifo);
489
490
df203a4e
JK
491/**
492 * snd_hdac_ext_stop_streams - stop all stream if running
493 * @ebus: HD-audio ext core bus
494 */
495void snd_hdac_ext_stop_streams(struct hdac_ext_bus *ebus)
496{
497 struct hdac_bus *bus = ebus_to_hbus(ebus);
498 struct hdac_stream *stream;
499
500 if (bus->chip_init) {
501 list_for_each_entry(stream, &bus->stream_list, list)
502 snd_hdac_stream_stop(stream);
503 snd_hdac_bus_stop_chip(bus);
504 }
505}
506EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);