2 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
11 * This file contains the implementation of Digital Audio Input Output
12 * resource management object.
20 #include "cthardware.h"
22 #include <linux/slab.h>
23 #include <linux/kernel.h>
25 #define DAIO_OUT_MAX SPDIFOO
36 struct daio_rsc_idx idx_20k1
[NUM_DAIOTYP
] = {
37 [LINEO1
] = {.left
= 0x00, .right
= 0x01},
38 [LINEO2
] = {.left
= 0x18, .right
= 0x19},
39 [LINEO3
] = {.left
= 0x08, .right
= 0x09},
40 [LINEO4
] = {.left
= 0x10, .right
= 0x11},
41 [LINEIM
] = {.left
= 0x1b5, .right
= 0x1bd},
42 [SPDIFOO
] = {.left
= 0x20, .right
= 0x21},
43 [SPDIFIO
] = {.left
= 0x15, .right
= 0x1d},
44 [SPDIFI1
] = {.left
= 0x95, .right
= 0x9d},
47 struct daio_rsc_idx idx_20k2
[NUM_DAIOTYP
] = {
48 [LINEO1
] = {.left
= 0x40, .right
= 0x41},
49 [LINEO2
] = {.left
= 0x60, .right
= 0x61},
50 [LINEO3
] = {.left
= 0x50, .right
= 0x51},
51 [LINEO4
] = {.left
= 0x70, .right
= 0x71},
52 [LINEIM
] = {.left
= 0x45, .right
= 0xc5},
53 [MIC
] = {.left
= 0x55, .right
= 0xd5},
54 [SPDIFOO
] = {.left
= 0x00, .right
= 0x01},
55 [SPDIFIO
] = {.left
= 0x05, .right
= 0x85},
58 static int daio_master(struct rsc
*rsc
)
60 /* Actually, this is not the resource index of DAIO.
61 * For DAO, it is the input mapper index. And, for DAI,
62 * it is the output time-slot index. */
63 return rsc
->conj
= rsc
->idx
;
66 static int daio_index(const struct rsc
*rsc
)
71 static int daio_out_next_conj(struct rsc
*rsc
)
73 return rsc
->conj
+= 2;
76 static int daio_in_next_conj_20k1(struct rsc
*rsc
)
78 return rsc
->conj
+= 0x200;
81 static int daio_in_next_conj_20k2(struct rsc
*rsc
)
83 return rsc
->conj
+= 0x100;
86 static struct rsc_ops daio_out_rsc_ops
= {
87 .master
= daio_master
,
88 .next_conj
= daio_out_next_conj
,
93 static struct rsc_ops daio_in_rsc_ops_20k1
= {
94 .master
= daio_master
,
95 .next_conj
= daio_in_next_conj_20k1
,
97 .output_slot
= daio_index
,
100 static struct rsc_ops daio_in_rsc_ops_20k2
= {
101 .master
= daio_master
,
102 .next_conj
= daio_in_next_conj_20k2
,
104 .output_slot
= daio_index
,
107 static unsigned int daio_device_index(enum DAIOTYP type
, struct hw
*hw
)
109 switch (hw
->chip_type
) {
112 case SPDIFOO
: return 0;
113 case SPDIFIO
: return 0;
114 case SPDIFI1
: return 1;
115 case LINEO1
: return 4;
116 case LINEO2
: return 7;
117 case LINEO3
: return 5;
118 case LINEO4
: return 6;
119 case LINEIM
: return 7;
120 default: return -EINVAL
;
124 case SPDIFOO
: return 0;
125 case SPDIFIO
: return 0;
126 case LINEO1
: return 4;
127 case LINEO2
: return 7;
128 case LINEO3
: return 5;
129 case LINEO4
: return 6;
130 case LINEIM
: return 4;
132 default: return -EINVAL
;
139 static int dao_rsc_reinit(struct dao
*dao
, const struct dao_desc
*desc
);
141 static int dao_spdif_get_spos(struct dao
*dao
, unsigned int *spos
)
143 ((struct hw
*)dao
->hw
)->dao_get_spos(dao
->ctrl_blk
, spos
);
147 static int dao_spdif_set_spos(struct dao
*dao
, unsigned int spos
)
149 ((struct hw
*)dao
->hw
)->dao_set_spos(dao
->ctrl_blk
, spos
);
153 static int dao_commit_write(struct dao
*dao
)
155 ((struct hw
*)dao
->hw
)->dao_commit_write(dao
->hw
,
156 daio_device_index(dao
->daio
.type
, dao
->hw
), dao
->ctrl_blk
);
160 static int dao_set_left_input(struct dao
*dao
, struct rsc
*input
)
162 struct imapper
*entry
;
163 struct daio
*daio
= &dao
->daio
;
166 entry
= kzalloc((sizeof(*entry
) * daio
->rscl
.msr
), GFP_KERNEL
);
170 dao
->ops
->clear_left_input(dao
);
171 /* Program master and conjugate resources */
172 input
->ops
->master(input
);
173 daio
->rscl
.ops
->master(&daio
->rscl
);
174 for (i
= 0; i
< daio
->rscl
.msr
; i
++, entry
++) {
175 entry
->slot
= input
->ops
->output_slot(input
);
176 entry
->user
= entry
->addr
= daio
->rscl
.ops
->index(&daio
->rscl
);
177 dao
->mgr
->imap_add(dao
->mgr
, entry
);
178 dao
->imappers
[i
] = entry
;
180 input
->ops
->next_conj(input
);
181 daio
->rscl
.ops
->next_conj(&daio
->rscl
);
183 input
->ops
->master(input
);
184 daio
->rscl
.ops
->master(&daio
->rscl
);
189 static int dao_set_right_input(struct dao
*dao
, struct rsc
*input
)
191 struct imapper
*entry
;
192 struct daio
*daio
= &dao
->daio
;
195 entry
= kzalloc((sizeof(*entry
) * daio
->rscr
.msr
), GFP_KERNEL
);
199 dao
->ops
->clear_right_input(dao
);
200 /* Program master and conjugate resources */
201 input
->ops
->master(input
);
202 daio
->rscr
.ops
->master(&daio
->rscr
);
203 for (i
= 0; i
< daio
->rscr
.msr
; i
++, entry
++) {
204 entry
->slot
= input
->ops
->output_slot(input
);
205 entry
->user
= entry
->addr
= daio
->rscr
.ops
->index(&daio
->rscr
);
206 dao
->mgr
->imap_add(dao
->mgr
, entry
);
207 dao
->imappers
[daio
->rscl
.msr
+ i
] = entry
;
209 input
->ops
->next_conj(input
);
210 daio
->rscr
.ops
->next_conj(&daio
->rscr
);
212 input
->ops
->master(input
);
213 daio
->rscr
.ops
->master(&daio
->rscr
);
218 static int dao_clear_left_input(struct dao
*dao
)
220 struct imapper
*entry
;
221 struct daio
*daio
= &dao
->daio
;
224 if (!dao
->imappers
[0])
227 entry
= dao
->imappers
[0];
228 dao
->mgr
->imap_delete(dao
->mgr
, entry
);
229 /* Program conjugate resources */
230 for (i
= 1; i
< daio
->rscl
.msr
; i
++) {
231 entry
= dao
->imappers
[i
];
232 dao
->mgr
->imap_delete(dao
->mgr
, entry
);
233 dao
->imappers
[i
] = NULL
;
236 kfree(dao
->imappers
[0]);
237 dao
->imappers
[0] = NULL
;
242 static int dao_clear_right_input(struct dao
*dao
)
244 struct imapper
*entry
;
245 struct daio
*daio
= &dao
->daio
;
248 if (!dao
->imappers
[daio
->rscl
.msr
])
251 entry
= dao
->imappers
[daio
->rscl
.msr
];
252 dao
->mgr
->imap_delete(dao
->mgr
, entry
);
253 /* Program conjugate resources */
254 for (i
= 1; i
< daio
->rscr
.msr
; i
++) {
255 entry
= dao
->imappers
[daio
->rscl
.msr
+ i
];
256 dao
->mgr
->imap_delete(dao
->mgr
, entry
);
257 dao
->imappers
[daio
->rscl
.msr
+ i
] = NULL
;
260 kfree(dao
->imappers
[daio
->rscl
.msr
]);
261 dao
->imappers
[daio
->rscl
.msr
] = NULL
;
266 static struct dao_rsc_ops dao_ops
= {
267 .set_spos
= dao_spdif_set_spos
,
268 .commit_write
= dao_commit_write
,
269 .get_spos
= dao_spdif_get_spos
,
270 .reinit
= dao_rsc_reinit
,
271 .set_left_input
= dao_set_left_input
,
272 .set_right_input
= dao_set_right_input
,
273 .clear_left_input
= dao_clear_left_input
,
274 .clear_right_input
= dao_clear_right_input
,
277 static int dai_set_srt_srcl(struct dai
*dai
, struct rsc
*src
)
279 src
->ops
->master(src
);
280 ((struct hw
*)dai
->hw
)->dai_srt_set_srcm(dai
->ctrl_blk
,
281 src
->ops
->index(src
));
285 static int dai_set_srt_srcr(struct dai
*dai
, struct rsc
*src
)
287 src
->ops
->master(src
);
288 ((struct hw
*)dai
->hw
)->dai_srt_set_srco(dai
->ctrl_blk
,
289 src
->ops
->index(src
));
293 static int dai_set_srt_msr(struct dai
*dai
, unsigned int msr
)
297 for (rsr
= 0; msr
> 1; msr
>>= 1)
300 ((struct hw
*)dai
->hw
)->dai_srt_set_rsr(dai
->ctrl_blk
, rsr
);
304 static int dai_set_enb_src(struct dai
*dai
, unsigned int enb
)
306 ((struct hw
*)dai
->hw
)->dai_srt_set_ec(dai
->ctrl_blk
, enb
);
310 static int dai_set_enb_srt(struct dai
*dai
, unsigned int enb
)
312 ((struct hw
*)dai
->hw
)->dai_srt_set_et(dai
->ctrl_blk
, enb
);
316 static int dai_commit_write(struct dai
*dai
)
318 ((struct hw
*)dai
->hw
)->dai_commit_write(dai
->hw
,
319 daio_device_index(dai
->daio
.type
, dai
->hw
), dai
->ctrl_blk
);
323 static struct dai_rsc_ops dai_ops
= {
324 .set_srt_srcl
= dai_set_srt_srcl
,
325 .set_srt_srcr
= dai_set_srt_srcr
,
326 .set_srt_msr
= dai_set_srt_msr
,
327 .set_enb_src
= dai_set_enb_src
,
328 .set_enb_srt
= dai_set_enb_srt
,
329 .commit_write
= dai_commit_write
,
332 static int daio_rsc_init(struct daio
*daio
,
333 const struct daio_desc
*desc
,
337 unsigned int idx_l
, idx_r
;
339 switch (((struct hw
*)hw
)->chip_type
) {
341 idx_l
= idx_20k1
[desc
->type
].left
;
342 idx_r
= idx_20k1
[desc
->type
].right
;
345 idx_l
= idx_20k2
[desc
->type
].left
;
346 idx_r
= idx_20k2
[desc
->type
].right
;
351 err
= rsc_init(&daio
->rscl
, idx_l
, DAIO
, desc
->msr
, hw
);
355 err
= rsc_init(&daio
->rscr
, idx_r
, DAIO
, desc
->msr
, hw
);
359 /* Set daio->rscl/r->ops to daio specific ones */
360 if (desc
->type
<= DAIO_OUT_MAX
) {
361 daio
->rscl
.ops
= daio
->rscr
.ops
= &daio_out_rsc_ops
;
363 switch (((struct hw
*)hw
)->chip_type
) {
365 daio
->rscl
.ops
= daio
->rscr
.ops
= &daio_in_rsc_ops_20k1
;
368 daio
->rscl
.ops
= daio
->rscr
.ops
= &daio_in_rsc_ops_20k2
;
374 daio
->type
= desc
->type
;
379 rsc_uninit(&daio
->rscl
);
383 static int daio_rsc_uninit(struct daio
*daio
)
385 rsc_uninit(&daio
->rscl
);
386 rsc_uninit(&daio
->rscr
);
391 static int dao_rsc_init(struct dao
*dao
,
392 const struct daio_desc
*desc
,
393 struct daio_mgr
*mgr
)
395 struct hw
*hw
= mgr
->mgr
.hw
;
399 err
= daio_rsc_init(&dao
->daio
, desc
, mgr
->mgr
.hw
);
403 dao
->imappers
= kzalloc(sizeof(void *)*desc
->msr
*2, GFP_KERNEL
);
404 if (!dao
->imappers
) {
411 err
= hw
->dao_get_ctrl_blk(&dao
->ctrl_blk
);
415 hw
->daio_mgr_dsb_dao(mgr
->mgr
.ctrl_blk
,
416 daio_device_index(dao
->daio
.type
, hw
));
417 hw
->daio_mgr_commit_write(hw
, mgr
->mgr
.ctrl_blk
);
419 conf
= (desc
->msr
& 0x7) | (desc
->passthru
<< 3);
420 hw
->daio_mgr_dao_init(mgr
->mgr
.ctrl_blk
,
421 daio_device_index(dao
->daio
.type
, hw
), conf
);
422 hw
->daio_mgr_enb_dao(mgr
->mgr
.ctrl_blk
,
423 daio_device_index(dao
->daio
.type
, hw
));
424 hw
->daio_mgr_commit_write(hw
, mgr
->mgr
.ctrl_blk
);
429 kfree(dao
->imappers
);
430 dao
->imappers
= NULL
;
432 daio_rsc_uninit(&dao
->daio
);
436 static int dao_rsc_uninit(struct dao
*dao
)
439 if (dao
->imappers
[0])
440 dao_clear_left_input(dao
);
442 if (dao
->imappers
[dao
->daio
.rscl
.msr
])
443 dao_clear_right_input(dao
);
445 kfree(dao
->imappers
);
446 dao
->imappers
= NULL
;
448 ((struct hw
*)dao
->hw
)->dao_put_ctrl_blk(dao
->ctrl_blk
);
449 dao
->hw
= dao
->ctrl_blk
= NULL
;
450 daio_rsc_uninit(&dao
->daio
);
455 static int dao_rsc_reinit(struct dao
*dao
, const struct dao_desc
*desc
)
457 struct daio_mgr
*mgr
= dao
->mgr
;
458 struct daio_desc dsc
= {0};
460 dsc
.type
= dao
->daio
.type
;
462 dsc
.passthru
= desc
->passthru
;
464 return dao_rsc_init(dao
, &dsc
, mgr
);
467 static int dai_rsc_init(struct dai
*dai
,
468 const struct daio_desc
*desc
,
469 struct daio_mgr
*mgr
)
472 struct hw
*hw
= mgr
->mgr
.hw
;
473 unsigned int rsr
, msr
;
475 err
= daio_rsc_init(&dai
->daio
, desc
, mgr
->mgr
.hw
);
480 dai
->hw
= mgr
->mgr
.hw
;
481 err
= hw
->dai_get_ctrl_blk(&dai
->ctrl_blk
);
485 for (rsr
= 0, msr
= desc
->msr
; msr
> 1; msr
>>= 1)
488 hw
->dai_srt_set_rsr(dai
->ctrl_blk
, rsr
);
489 hw
->dai_srt_set_drat(dai
->ctrl_blk
, 0);
490 /* default to disabling control of a SRC */
491 hw
->dai_srt_set_ec(dai
->ctrl_blk
, 0);
492 hw
->dai_srt_set_et(dai
->ctrl_blk
, 0); /* default to disabling SRT */
493 hw
->dai_commit_write(hw
,
494 daio_device_index(dai
->daio
.type
, dai
->hw
), dai
->ctrl_blk
);
499 daio_rsc_uninit(&dai
->daio
);
503 static int dai_rsc_uninit(struct dai
*dai
)
505 ((struct hw
*)dai
->hw
)->dai_put_ctrl_blk(dai
->ctrl_blk
);
506 dai
->hw
= dai
->ctrl_blk
= NULL
;
507 daio_rsc_uninit(&dai
->daio
);
511 static int daio_mgr_get_rsc(struct rsc_mgr
*mgr
, enum DAIOTYP type
)
513 if (((struct daio_usage
*)mgr
->rscs
)->data
& (0x1 << type
))
516 ((struct daio_usage
*)mgr
->rscs
)->data
|= (0x1 << type
);
521 static int daio_mgr_put_rsc(struct rsc_mgr
*mgr
, enum DAIOTYP type
)
523 ((struct daio_usage
*)mgr
->rscs
)->data
&= ~(0x1 << type
);
528 static int get_daio_rsc(struct daio_mgr
*mgr
,
529 const struct daio_desc
*desc
,
533 struct dai
*dai
= NULL
;
534 struct dao
*dao
= NULL
;
539 /* Check whether there are sufficient daio resources to meet request. */
540 spin_lock_irqsave(&mgr
->mgr_lock
, flags
);
541 err
= daio_mgr_get_rsc(&mgr
->mgr
, desc
->type
);
542 spin_unlock_irqrestore(&mgr
->mgr_lock
, flags
);
544 printk(KERN_ERR
"Can't meet DAIO resource request!\n");
548 /* Allocate mem for daio resource */
549 if (desc
->type
<= DAIO_OUT_MAX
) {
550 dao
= kzalloc(sizeof(*dao
), GFP_KERNEL
);
555 err
= dao_rsc_init(dao
, desc
, mgr
);
561 dai
= kzalloc(sizeof(*dai
), GFP_KERNEL
);
566 err
= dai_rsc_init(dai
, desc
, mgr
);
573 mgr
->daio_enable(mgr
, *rdaio
);
574 mgr
->commit_write(mgr
);
584 spin_lock_irqsave(&mgr
->mgr_lock
, flags
);
585 daio_mgr_put_rsc(&mgr
->mgr
, desc
->type
);
586 spin_unlock_irqrestore(&mgr
->mgr_lock
, flags
);
590 static int put_daio_rsc(struct daio_mgr
*mgr
, struct daio
*daio
)
594 mgr
->daio_disable(mgr
, daio
);
595 mgr
->commit_write(mgr
);
597 spin_lock_irqsave(&mgr
->mgr_lock
, flags
);
598 daio_mgr_put_rsc(&mgr
->mgr
, daio
->type
);
599 spin_unlock_irqrestore(&mgr
->mgr_lock
, flags
);
601 if (daio
->type
<= DAIO_OUT_MAX
) {
602 dao_rsc_uninit(container_of(daio
, struct dao
, daio
));
603 kfree(container_of(daio
, struct dao
, daio
));
605 dai_rsc_uninit(container_of(daio
, struct dai
, daio
));
606 kfree(container_of(daio
, struct dai
, daio
));
612 static int daio_mgr_enb_daio(struct daio_mgr
*mgr
, struct daio
*daio
)
614 struct hw
*hw
= mgr
->mgr
.hw
;
616 if (DAIO_OUT_MAX
>= daio
->type
) {
617 hw
->daio_mgr_enb_dao(mgr
->mgr
.ctrl_blk
,
618 daio_device_index(daio
->type
, hw
));
620 hw
->daio_mgr_enb_dai(mgr
->mgr
.ctrl_blk
,
621 daio_device_index(daio
->type
, hw
));
626 static int daio_mgr_dsb_daio(struct daio_mgr
*mgr
, struct daio
*daio
)
628 struct hw
*hw
= mgr
->mgr
.hw
;
630 if (DAIO_OUT_MAX
>= daio
->type
) {
631 hw
->daio_mgr_dsb_dao(mgr
->mgr
.ctrl_blk
,
632 daio_device_index(daio
->type
, hw
));
634 hw
->daio_mgr_dsb_dai(mgr
->mgr
.ctrl_blk
,
635 daio_device_index(daio
->type
, hw
));
640 static int daio_map_op(void *data
, struct imapper
*entry
)
642 struct rsc_mgr
*mgr
= &((struct daio_mgr
*)data
)->mgr
;
643 struct hw
*hw
= mgr
->hw
;
645 hw
->daio_mgr_set_imaparc(mgr
->ctrl_blk
, entry
->slot
);
646 hw
->daio_mgr_set_imapnxt(mgr
->ctrl_blk
, entry
->next
);
647 hw
->daio_mgr_set_imapaddr(mgr
->ctrl_blk
, entry
->addr
);
648 hw
->daio_mgr_commit_write(mgr
->hw
, mgr
->ctrl_blk
);
653 static int daio_imap_add(struct daio_mgr
*mgr
, struct imapper
*entry
)
658 spin_lock_irqsave(&mgr
->imap_lock
, flags
);
659 if (!entry
->addr
&& mgr
->init_imap_added
) {
660 input_mapper_delete(&mgr
->imappers
, mgr
->init_imap
,
662 mgr
->init_imap_added
= 0;
664 err
= input_mapper_add(&mgr
->imappers
, entry
, daio_map_op
, mgr
);
665 spin_unlock_irqrestore(&mgr
->imap_lock
, flags
);
670 static int daio_imap_delete(struct daio_mgr
*mgr
, struct imapper
*entry
)
675 spin_lock_irqsave(&mgr
->imap_lock
, flags
);
676 err
= input_mapper_delete(&mgr
->imappers
, entry
, daio_map_op
, mgr
);
677 if (list_empty(&mgr
->imappers
)) {
678 input_mapper_add(&mgr
->imappers
, mgr
->init_imap
,
680 mgr
->init_imap_added
= 1;
682 spin_unlock_irqrestore(&mgr
->imap_lock
, flags
);
687 static int daio_mgr_commit_write(struct daio_mgr
*mgr
)
689 struct hw
*hw
= mgr
->mgr
.hw
;
691 hw
->daio_mgr_commit_write(hw
, mgr
->mgr
.ctrl_blk
);
695 int daio_mgr_create(void *hw
, struct daio_mgr
**rdaio_mgr
)
698 struct daio_mgr
*daio_mgr
;
699 struct imapper
*entry
;
702 daio_mgr
= kzalloc(sizeof(*daio_mgr
), GFP_KERNEL
);
706 err
= rsc_mgr_init(&daio_mgr
->mgr
, DAIO
, NUM_DAIOTYP
, hw
);
710 spin_lock_init(&daio_mgr
->mgr_lock
);
711 spin_lock_init(&daio_mgr
->imap_lock
);
712 INIT_LIST_HEAD(&daio_mgr
->imappers
);
713 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
718 entry
->slot
= entry
->addr
= entry
->next
= entry
->user
= 0;
719 list_add(&entry
->list
, &daio_mgr
->imappers
);
720 daio_mgr
->init_imap
= entry
;
721 daio_mgr
->init_imap_added
= 1;
723 daio_mgr
->get_daio
= get_daio_rsc
;
724 daio_mgr
->put_daio
= put_daio_rsc
;
725 daio_mgr
->daio_enable
= daio_mgr_enb_daio
;
726 daio_mgr
->daio_disable
= daio_mgr_dsb_daio
;
727 daio_mgr
->imap_add
= daio_imap_add
;
728 daio_mgr
->imap_delete
= daio_imap_delete
;
729 daio_mgr
->commit_write
= daio_mgr_commit_write
;
731 for (i
= 0; i
< 8; i
++) {
732 ((struct hw
*)hw
)->daio_mgr_dsb_dao(daio_mgr
->mgr
.ctrl_blk
, i
);
733 ((struct hw
*)hw
)->daio_mgr_dsb_dai(daio_mgr
->mgr
.ctrl_blk
, i
);
735 ((struct hw
*)hw
)->daio_mgr_commit_write(hw
, daio_mgr
->mgr
.ctrl_blk
);
737 *rdaio_mgr
= daio_mgr
;
742 rsc_mgr_uninit(&daio_mgr
->mgr
);
748 int daio_mgr_destroy(struct daio_mgr
*daio_mgr
)
752 /* free daio input mapper list */
753 spin_lock_irqsave(&daio_mgr
->imap_lock
, flags
);
754 free_input_mapper_list(&daio_mgr
->imappers
);
755 spin_unlock_irqrestore(&daio_mgr
->imap_lock
, flags
);
757 rsc_mgr_uninit(&daio_mgr
->mgr
);