WIP FPC-III support
[linux/fpc-iii.git] / sound / pci / ctxfi / ctdaio.c
blob4cb47b5a792c54d5a1c1f3bcc149443dd6a8fad3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /**
3 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
5 * @File ctdaio.c
7 * @Brief
8 * This file contains the implementation of Digital Audio Input Output
9 * resource management object.
11 * @Author Liu Chun
12 * @Date May 23 2008
15 #include "ctdaio.h"
16 #include "cthardware.h"
17 #include "ctimap.h"
18 #include <linux/slab.h>
19 #include <linux/kernel.h>
21 #define DAIO_OUT_MAX SPDIFOO
23 struct daio_usage {
24 unsigned short data;
27 struct daio_rsc_idx {
28 unsigned short left;
29 unsigned short right;
32 static const struct daio_rsc_idx idx_20k1[NUM_DAIOTYP] = {
33 [LINEO1] = {.left = 0x00, .right = 0x01},
34 [LINEO2] = {.left = 0x18, .right = 0x19},
35 [LINEO3] = {.left = 0x08, .right = 0x09},
36 [LINEO4] = {.left = 0x10, .right = 0x11},
37 [LINEIM] = {.left = 0x1b5, .right = 0x1bd},
38 [SPDIFOO] = {.left = 0x20, .right = 0x21},
39 [SPDIFIO] = {.left = 0x15, .right = 0x1d},
40 [SPDIFI1] = {.left = 0x95, .right = 0x9d},
43 static const struct daio_rsc_idx idx_20k2[NUM_DAIOTYP] = {
44 [LINEO1] = {.left = 0x40, .right = 0x41},
45 [LINEO2] = {.left = 0x60, .right = 0x61},
46 [LINEO3] = {.left = 0x50, .right = 0x51},
47 [LINEO4] = {.left = 0x70, .right = 0x71},
48 [LINEIM] = {.left = 0x45, .right = 0xc5},
49 [MIC] = {.left = 0x55, .right = 0xd5},
50 [SPDIFOO] = {.left = 0x00, .right = 0x01},
51 [SPDIFIO] = {.left = 0x05, .right = 0x85},
54 static int daio_master(struct rsc *rsc)
56 /* Actually, this is not the resource index of DAIO.
57 * For DAO, it is the input mapper index. And, for DAI,
58 * it is the output time-slot index. */
59 return rsc->conj = rsc->idx;
62 static int daio_index(const struct rsc *rsc)
64 return rsc->conj;
67 static int daio_out_next_conj(struct rsc *rsc)
69 return rsc->conj += 2;
72 static int daio_in_next_conj_20k1(struct rsc *rsc)
74 return rsc->conj += 0x200;
77 static int daio_in_next_conj_20k2(struct rsc *rsc)
79 return rsc->conj += 0x100;
82 static const struct rsc_ops daio_out_rsc_ops = {
83 .master = daio_master,
84 .next_conj = daio_out_next_conj,
85 .index = daio_index,
86 .output_slot = NULL,
89 static const struct rsc_ops daio_in_rsc_ops_20k1 = {
90 .master = daio_master,
91 .next_conj = daio_in_next_conj_20k1,
92 .index = NULL,
93 .output_slot = daio_index,
96 static const struct rsc_ops daio_in_rsc_ops_20k2 = {
97 .master = daio_master,
98 .next_conj = daio_in_next_conj_20k2,
99 .index = NULL,
100 .output_slot = daio_index,
103 static unsigned int daio_device_index(enum DAIOTYP type, struct hw *hw)
105 switch (hw->chip_type) {
106 case ATC20K1:
107 switch (type) {
108 case SPDIFOO: return 0;
109 case SPDIFIO: return 0;
110 case SPDIFI1: return 1;
111 case LINEO1: return 4;
112 case LINEO2: return 7;
113 case LINEO3: return 5;
114 case LINEO4: return 6;
115 case LINEIM: return 7;
116 default: return -EINVAL;
118 case ATC20K2:
119 switch (type) {
120 case SPDIFOO: return 0;
121 case SPDIFIO: return 0;
122 case LINEO1: return 4;
123 case LINEO2: return 7;
124 case LINEO3: return 5;
125 case LINEO4: return 6;
126 case LINEIM: return 4;
127 case MIC: return 5;
128 default: return -EINVAL;
130 default:
131 return -EINVAL;
135 static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc);
137 static int dao_spdif_get_spos(struct dao *dao, unsigned int *spos)
139 dao->hw->dao_get_spos(dao->ctrl_blk, spos);
140 return 0;
143 static int dao_spdif_set_spos(struct dao *dao, unsigned int spos)
145 dao->hw->dao_set_spos(dao->ctrl_blk, spos);
146 return 0;
149 static int dao_commit_write(struct dao *dao)
151 dao->hw->dao_commit_write(dao->hw,
152 daio_device_index(dao->daio.type, dao->hw), dao->ctrl_blk);
153 return 0;
156 static int dao_set_left_input(struct dao *dao, struct rsc *input)
158 struct imapper *entry;
159 struct daio *daio = &dao->daio;
160 int i;
162 entry = kzalloc((sizeof(*entry) * daio->rscl.msr), GFP_KERNEL);
163 if (!entry)
164 return -ENOMEM;
166 dao->ops->clear_left_input(dao);
167 /* Program master and conjugate resources */
168 input->ops->master(input);
169 daio->rscl.ops->master(&daio->rscl);
170 for (i = 0; i < daio->rscl.msr; i++, entry++) {
171 entry->slot = input->ops->output_slot(input);
172 entry->user = entry->addr = daio->rscl.ops->index(&daio->rscl);
173 dao->mgr->imap_add(dao->mgr, entry);
174 dao->imappers[i] = entry;
176 input->ops->next_conj(input);
177 daio->rscl.ops->next_conj(&daio->rscl);
179 input->ops->master(input);
180 daio->rscl.ops->master(&daio->rscl);
182 return 0;
185 static int dao_set_right_input(struct dao *dao, struct rsc *input)
187 struct imapper *entry;
188 struct daio *daio = &dao->daio;
189 int i;
191 entry = kzalloc((sizeof(*entry) * daio->rscr.msr), GFP_KERNEL);
192 if (!entry)
193 return -ENOMEM;
195 dao->ops->clear_right_input(dao);
196 /* Program master and conjugate resources */
197 input->ops->master(input);
198 daio->rscr.ops->master(&daio->rscr);
199 for (i = 0; i < daio->rscr.msr; i++, entry++) {
200 entry->slot = input->ops->output_slot(input);
201 entry->user = entry->addr = daio->rscr.ops->index(&daio->rscr);
202 dao->mgr->imap_add(dao->mgr, entry);
203 dao->imappers[daio->rscl.msr + i] = entry;
205 input->ops->next_conj(input);
206 daio->rscr.ops->next_conj(&daio->rscr);
208 input->ops->master(input);
209 daio->rscr.ops->master(&daio->rscr);
211 return 0;
214 static int dao_clear_left_input(struct dao *dao)
216 struct imapper *entry;
217 struct daio *daio = &dao->daio;
218 int i;
220 if (!dao->imappers[0])
221 return 0;
223 entry = dao->imappers[0];
224 dao->mgr->imap_delete(dao->mgr, entry);
225 /* Program conjugate resources */
226 for (i = 1; i < daio->rscl.msr; i++) {
227 entry = dao->imappers[i];
228 dao->mgr->imap_delete(dao->mgr, entry);
229 dao->imappers[i] = NULL;
232 kfree(dao->imappers[0]);
233 dao->imappers[0] = NULL;
235 return 0;
238 static int dao_clear_right_input(struct dao *dao)
240 struct imapper *entry;
241 struct daio *daio = &dao->daio;
242 int i;
244 if (!dao->imappers[daio->rscl.msr])
245 return 0;
247 entry = dao->imappers[daio->rscl.msr];
248 dao->mgr->imap_delete(dao->mgr, entry);
249 /* Program conjugate resources */
250 for (i = 1; i < daio->rscr.msr; i++) {
251 entry = dao->imappers[daio->rscl.msr + i];
252 dao->mgr->imap_delete(dao->mgr, entry);
253 dao->imappers[daio->rscl.msr + i] = NULL;
256 kfree(dao->imappers[daio->rscl.msr]);
257 dao->imappers[daio->rscl.msr] = NULL;
259 return 0;
262 static const struct dao_rsc_ops dao_ops = {
263 .set_spos = dao_spdif_set_spos,
264 .commit_write = dao_commit_write,
265 .get_spos = dao_spdif_get_spos,
266 .reinit = dao_rsc_reinit,
267 .set_left_input = dao_set_left_input,
268 .set_right_input = dao_set_right_input,
269 .clear_left_input = dao_clear_left_input,
270 .clear_right_input = dao_clear_right_input,
273 static int dai_set_srt_srcl(struct dai *dai, struct rsc *src)
275 src->ops->master(src);
276 dai->hw->dai_srt_set_srcm(dai->ctrl_blk, src->ops->index(src));
277 return 0;
280 static int dai_set_srt_srcr(struct dai *dai, struct rsc *src)
282 src->ops->master(src);
283 dai->hw->dai_srt_set_srco(dai->ctrl_blk, src->ops->index(src));
284 return 0;
287 static int dai_set_srt_msr(struct dai *dai, unsigned int msr)
289 unsigned int rsr;
291 for (rsr = 0; msr > 1; msr >>= 1)
292 rsr++;
294 dai->hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
295 return 0;
298 static int dai_set_enb_src(struct dai *dai, unsigned int enb)
300 dai->hw->dai_srt_set_ec(dai->ctrl_blk, enb);
301 return 0;
304 static int dai_set_enb_srt(struct dai *dai, unsigned int enb)
306 dai->hw->dai_srt_set_et(dai->ctrl_blk, enb);
307 return 0;
310 static int dai_commit_write(struct dai *dai)
312 dai->hw->dai_commit_write(dai->hw,
313 daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);
314 return 0;
317 static const struct dai_rsc_ops dai_ops = {
318 .set_srt_srcl = dai_set_srt_srcl,
319 .set_srt_srcr = dai_set_srt_srcr,
320 .set_srt_msr = dai_set_srt_msr,
321 .set_enb_src = dai_set_enb_src,
322 .set_enb_srt = dai_set_enb_srt,
323 .commit_write = dai_commit_write,
326 static int daio_rsc_init(struct daio *daio,
327 const struct daio_desc *desc,
328 struct hw *hw)
330 int err;
331 unsigned int idx_l, idx_r;
333 switch (hw->chip_type) {
334 case ATC20K1:
335 idx_l = idx_20k1[desc->type].left;
336 idx_r = idx_20k1[desc->type].right;
337 break;
338 case ATC20K2:
339 idx_l = idx_20k2[desc->type].left;
340 idx_r = idx_20k2[desc->type].right;
341 break;
342 default:
343 return -EINVAL;
345 err = rsc_init(&daio->rscl, idx_l, DAIO, desc->msr, hw);
346 if (err)
347 return err;
349 err = rsc_init(&daio->rscr, idx_r, DAIO, desc->msr, hw);
350 if (err)
351 goto error1;
353 /* Set daio->rscl/r->ops to daio specific ones */
354 if (desc->type <= DAIO_OUT_MAX) {
355 daio->rscl.ops = daio->rscr.ops = &daio_out_rsc_ops;
356 } else {
357 switch (hw->chip_type) {
358 case ATC20K1:
359 daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k1;
360 break;
361 case ATC20K2:
362 daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k2;
363 break;
364 default:
365 break;
368 daio->type = desc->type;
370 return 0;
372 error1:
373 rsc_uninit(&daio->rscl);
374 return err;
377 static int daio_rsc_uninit(struct daio *daio)
379 rsc_uninit(&daio->rscl);
380 rsc_uninit(&daio->rscr);
382 return 0;
385 static int dao_rsc_init(struct dao *dao,
386 const struct daio_desc *desc,
387 struct daio_mgr *mgr)
389 struct hw *hw = mgr->mgr.hw;
390 unsigned int conf;
391 int err;
393 err = daio_rsc_init(&dao->daio, desc, mgr->mgr.hw);
394 if (err)
395 return err;
397 dao->imappers = kzalloc(array3_size(sizeof(void *), desc->msr, 2),
398 GFP_KERNEL);
399 if (!dao->imappers) {
400 err = -ENOMEM;
401 goto error1;
403 dao->ops = &dao_ops;
404 dao->mgr = mgr;
405 dao->hw = hw;
406 err = hw->dao_get_ctrl_blk(&dao->ctrl_blk);
407 if (err)
408 goto error2;
410 hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
411 daio_device_index(dao->daio.type, hw));
412 hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
414 conf = (desc->msr & 0x7) | (desc->passthru << 3);
415 hw->daio_mgr_dao_init(mgr->mgr.ctrl_blk,
416 daio_device_index(dao->daio.type, hw), conf);
417 hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
418 daio_device_index(dao->daio.type, hw));
419 hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
421 return 0;
423 error2:
424 kfree(dao->imappers);
425 dao->imappers = NULL;
426 error1:
427 daio_rsc_uninit(&dao->daio);
428 return err;
431 static int dao_rsc_uninit(struct dao *dao)
433 if (dao->imappers) {
434 if (dao->imappers[0])
435 dao_clear_left_input(dao);
437 if (dao->imappers[dao->daio.rscl.msr])
438 dao_clear_right_input(dao);
440 kfree(dao->imappers);
441 dao->imappers = NULL;
443 dao->hw->dao_put_ctrl_blk(dao->ctrl_blk);
444 dao->hw = dao->ctrl_blk = NULL;
445 daio_rsc_uninit(&dao->daio);
447 return 0;
450 static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc)
452 struct daio_mgr *mgr = dao->mgr;
453 struct daio_desc dsc = {0};
455 dsc.type = dao->daio.type;
456 dsc.msr = desc->msr;
457 dsc.passthru = desc->passthru;
458 dao_rsc_uninit(dao);
459 return dao_rsc_init(dao, &dsc, mgr);
462 static int dai_rsc_init(struct dai *dai,
463 const struct daio_desc *desc,
464 struct daio_mgr *mgr)
466 int err;
467 struct hw *hw = mgr->mgr.hw;
468 unsigned int rsr, msr;
470 err = daio_rsc_init(&dai->daio, desc, mgr->mgr.hw);
471 if (err)
472 return err;
474 dai->ops = &dai_ops;
475 dai->hw = mgr->mgr.hw;
476 err = hw->dai_get_ctrl_blk(&dai->ctrl_blk);
477 if (err)
478 goto error1;
480 for (rsr = 0, msr = desc->msr; msr > 1; msr >>= 1)
481 rsr++;
483 hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
484 hw->dai_srt_set_drat(dai->ctrl_blk, 0);
485 /* default to disabling control of a SRC */
486 hw->dai_srt_set_ec(dai->ctrl_blk, 0);
487 hw->dai_srt_set_et(dai->ctrl_blk, 0); /* default to disabling SRT */
488 hw->dai_commit_write(hw,
489 daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);
491 return 0;
493 error1:
494 daio_rsc_uninit(&dai->daio);
495 return err;
498 static int dai_rsc_uninit(struct dai *dai)
500 dai->hw->dai_put_ctrl_blk(dai->ctrl_blk);
501 dai->hw = dai->ctrl_blk = NULL;
502 daio_rsc_uninit(&dai->daio);
503 return 0;
506 static int daio_mgr_get_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
508 if (((struct daio_usage *)mgr->rscs)->data & (0x1 << type))
509 return -ENOENT;
511 ((struct daio_usage *)mgr->rscs)->data |= (0x1 << type);
513 return 0;
516 static int daio_mgr_put_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
518 ((struct daio_usage *)mgr->rscs)->data &= ~(0x1 << type);
520 return 0;
523 static int get_daio_rsc(struct daio_mgr *mgr,
524 const struct daio_desc *desc,
525 struct daio **rdaio)
527 int err;
528 unsigned long flags;
530 *rdaio = NULL;
532 /* Check whether there are sufficient daio resources to meet request. */
533 spin_lock_irqsave(&mgr->mgr_lock, flags);
534 err = daio_mgr_get_rsc(&mgr->mgr, desc->type);
535 spin_unlock_irqrestore(&mgr->mgr_lock, flags);
536 if (err) {
537 dev_err(mgr->card->dev,
538 "Can't meet DAIO resource request!\n");
539 return err;
542 err = -ENOMEM;
543 /* Allocate mem for daio resource */
544 if (desc->type <= DAIO_OUT_MAX) {
545 struct dao *dao = kzalloc(sizeof(*dao), GFP_KERNEL);
546 if (!dao)
547 goto error;
549 err = dao_rsc_init(dao, desc, mgr);
550 if (err) {
551 kfree(dao);
552 goto error;
555 *rdaio = &dao->daio;
556 } else {
557 struct dai *dai = kzalloc(sizeof(*dai), GFP_KERNEL);
558 if (!dai)
559 goto error;
561 err = dai_rsc_init(dai, desc, mgr);
562 if (err) {
563 kfree(dai);
564 goto error;
567 *rdaio = &dai->daio;
570 mgr->daio_enable(mgr, *rdaio);
571 mgr->commit_write(mgr);
573 return 0;
575 error:
576 spin_lock_irqsave(&mgr->mgr_lock, flags);
577 daio_mgr_put_rsc(&mgr->mgr, desc->type);
578 spin_unlock_irqrestore(&mgr->mgr_lock, flags);
579 return err;
582 static int put_daio_rsc(struct daio_mgr *mgr, struct daio *daio)
584 unsigned long flags;
586 mgr->daio_disable(mgr, daio);
587 mgr->commit_write(mgr);
589 spin_lock_irqsave(&mgr->mgr_lock, flags);
590 daio_mgr_put_rsc(&mgr->mgr, daio->type);
591 spin_unlock_irqrestore(&mgr->mgr_lock, flags);
593 if (daio->type <= DAIO_OUT_MAX) {
594 dao_rsc_uninit(container_of(daio, struct dao, daio));
595 kfree(container_of(daio, struct dao, daio));
596 } else {
597 dai_rsc_uninit(container_of(daio, struct dai, daio));
598 kfree(container_of(daio, struct dai, daio));
601 return 0;
604 static int daio_mgr_enb_daio(struct daio_mgr *mgr, struct daio *daio)
606 struct hw *hw = mgr->mgr.hw;
608 if (DAIO_OUT_MAX >= daio->type) {
609 hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
610 daio_device_index(daio->type, hw));
611 } else {
612 hw->daio_mgr_enb_dai(mgr->mgr.ctrl_blk,
613 daio_device_index(daio->type, hw));
615 return 0;
618 static int daio_mgr_dsb_daio(struct daio_mgr *mgr, struct daio *daio)
620 struct hw *hw = mgr->mgr.hw;
622 if (DAIO_OUT_MAX >= daio->type) {
623 hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
624 daio_device_index(daio->type, hw));
625 } else {
626 hw->daio_mgr_dsb_dai(mgr->mgr.ctrl_blk,
627 daio_device_index(daio->type, hw));
629 return 0;
632 static int daio_map_op(void *data, struct imapper *entry)
634 struct rsc_mgr *mgr = &((struct daio_mgr *)data)->mgr;
635 struct hw *hw = mgr->hw;
637 hw->daio_mgr_set_imaparc(mgr->ctrl_blk, entry->slot);
638 hw->daio_mgr_set_imapnxt(mgr->ctrl_blk, entry->next);
639 hw->daio_mgr_set_imapaddr(mgr->ctrl_blk, entry->addr);
640 hw->daio_mgr_commit_write(mgr->hw, mgr->ctrl_blk);
642 return 0;
645 static int daio_imap_add(struct daio_mgr *mgr, struct imapper *entry)
647 unsigned long flags;
648 int err;
650 spin_lock_irqsave(&mgr->imap_lock, flags);
651 if (!entry->addr && mgr->init_imap_added) {
652 input_mapper_delete(&mgr->imappers, mgr->init_imap,
653 daio_map_op, mgr);
654 mgr->init_imap_added = 0;
656 err = input_mapper_add(&mgr->imappers, entry, daio_map_op, mgr);
657 spin_unlock_irqrestore(&mgr->imap_lock, flags);
659 return err;
662 static int daio_imap_delete(struct daio_mgr *mgr, struct imapper *entry)
664 unsigned long flags;
665 int err;
667 spin_lock_irqsave(&mgr->imap_lock, flags);
668 err = input_mapper_delete(&mgr->imappers, entry, daio_map_op, mgr);
669 if (list_empty(&mgr->imappers)) {
670 input_mapper_add(&mgr->imappers, mgr->init_imap,
671 daio_map_op, mgr);
672 mgr->init_imap_added = 1;
674 spin_unlock_irqrestore(&mgr->imap_lock, flags);
676 return err;
679 static int daio_mgr_commit_write(struct daio_mgr *mgr)
681 struct hw *hw = mgr->mgr.hw;
683 hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
684 return 0;
687 int daio_mgr_create(struct hw *hw, struct daio_mgr **rdaio_mgr)
689 int err, i;
690 struct daio_mgr *daio_mgr;
691 struct imapper *entry;
693 *rdaio_mgr = NULL;
694 daio_mgr = kzalloc(sizeof(*daio_mgr), GFP_KERNEL);
695 if (!daio_mgr)
696 return -ENOMEM;
698 err = rsc_mgr_init(&daio_mgr->mgr, DAIO, NUM_DAIOTYP, hw);
699 if (err)
700 goto error1;
702 spin_lock_init(&daio_mgr->mgr_lock);
703 spin_lock_init(&daio_mgr->imap_lock);
704 INIT_LIST_HEAD(&daio_mgr->imappers);
705 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
706 if (!entry) {
707 err = -ENOMEM;
708 goto error2;
710 entry->slot = entry->addr = entry->next = entry->user = 0;
711 list_add(&entry->list, &daio_mgr->imappers);
712 daio_mgr->init_imap = entry;
713 daio_mgr->init_imap_added = 1;
715 daio_mgr->get_daio = get_daio_rsc;
716 daio_mgr->put_daio = put_daio_rsc;
717 daio_mgr->daio_enable = daio_mgr_enb_daio;
718 daio_mgr->daio_disable = daio_mgr_dsb_daio;
719 daio_mgr->imap_add = daio_imap_add;
720 daio_mgr->imap_delete = daio_imap_delete;
721 daio_mgr->commit_write = daio_mgr_commit_write;
722 daio_mgr->card = hw->card;
724 for (i = 0; i < 8; i++) {
725 hw->daio_mgr_dsb_dao(daio_mgr->mgr.ctrl_blk, i);
726 hw->daio_mgr_dsb_dai(daio_mgr->mgr.ctrl_blk, i);
728 hw->daio_mgr_commit_write(hw, daio_mgr->mgr.ctrl_blk);
730 *rdaio_mgr = daio_mgr;
732 return 0;
734 error2:
735 rsc_mgr_uninit(&daio_mgr->mgr);
736 error1:
737 kfree(daio_mgr);
738 return err;
741 int daio_mgr_destroy(struct daio_mgr *daio_mgr)
743 unsigned long flags;
745 /* free daio input mapper list */
746 spin_lock_irqsave(&daio_mgr->imap_lock, flags);
747 free_input_mapper_list(&daio_mgr->imappers);
748 spin_unlock_irqrestore(&daio_mgr->imap_lock, flags);
750 rsc_mgr_uninit(&daio_mgr->mgr);
751 kfree(daio_mgr);
753 return 0;