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 some generic helper functions.
18 #include "ctresource.h"
19 #include "cthardware.h"
20 #include <linux/err.h>
21 #include <linux/slab.h>
23 #define AUDIO_SLOT_BLOCK_NUM 256
25 /* Resource allocation based on bit-map management mechanism */
27 get_resource(u8 *rscs, unsigned int amount,
28 unsigned int multi, unsigned int *ridx)
32 /* Check whether there are sufficient resources to meet request. */
33 for (i = 0, n = multi; i < amount; i++) {
36 if (rscs[j] & ((u8)1 << k)) {
41 break; /* found sufficient contiguous resources */
45 /* Can not find sufficient contiguous resources */
49 /* Mark the contiguous bits in resource bit-map as used */
50 for (n = multi; n > 0; n--) {
53 rscs[j] |= ((u8)1 << k);
62 static int put_resource(u8 *rscs, unsigned int multi, unsigned int idx)
64 unsigned int i, j, k, n;
66 /* Mark the contiguous bits in resource bit-map as used */
67 for (n = multi, i = idx; n > 0; n--) {
70 rscs[j] &= ~((u8)1 << k);
77 int mgr_get_resource(struct rsc_mgr *mgr, unsigned int n, unsigned int *ridx)
84 err = get_resource(mgr->rscs, mgr->amount, n, ridx);
91 int mgr_put_resource(struct rsc_mgr *mgr, unsigned int n, unsigned int idx)
93 put_resource(mgr->rscs, n, idx);
99 static unsigned char offset_in_audio_slot_block[NUM_RSCTYP] = {
100 /* SRC channel is at Audio Ring slot 1 every 16 slots. */
106 static int rsc_index(const struct rsc *rsc)
111 static int audio_ring_slot(const struct rsc *rsc)
113 return (rsc->conj << 4) + offset_in_audio_slot_block[rsc->type];
116 static void rsc_next_conj(struct rsc *rsc)
119 for (i = 0; (i < 8) && (!(rsc->msr & (0x1 << i))); )
121 rsc->conj += (AUDIO_SLOT_BLOCK_NUM >> i);
124 static void rsc_master(struct rsc *rsc)
126 rsc->conj = rsc->idx;
129 static const struct rsc_ops rsc_generic_ops = {
131 .output_slot = audio_ring_slot,
132 .master = rsc_master,
133 .next_conj = rsc_next_conj,
137 rsc_init(struct rsc *rsc, u32 idx, enum RSCTYP type, u32 msr, struct hw *hw)
146 rsc->ops = &rsc_generic_ops;
148 rsc->ctrl_blk = NULL;
154 err = hw->src_rsc_get_ctrl_blk(&rsc->ctrl_blk);
157 err = hw->amixer_rsc_get_ctrl_blk(&rsc->ctrl_blk);
164 dev_err(((struct hw *)hw)->card->dev,
165 "Invalid resource type value %d!\n", type);
170 dev_err(((struct hw *)hw)->card->dev,
171 "Failed to get resource control block!\n");
178 int rsc_uninit(struct rsc *rsc)
180 if ((NULL != rsc->hw) && (NULL != rsc->ctrl_blk)) {
183 rsc->hw->src_rsc_put_ctrl_blk(rsc->ctrl_blk);
186 rsc->hw->amixer_rsc_put_ctrl_blk(rsc->ctrl_blk);
192 dev_err(((struct hw *)rsc->hw)->card->dev,
193 "Invalid resource type value %d!\n",
198 rsc->hw = rsc->ctrl_blk = NULL;
201 rsc->idx = rsc->conj = 0;
202 rsc->type = NUM_RSCTYP;
208 int rsc_mgr_init(struct rsc_mgr *mgr, enum RSCTYP type,
209 unsigned int amount, struct hw *hw)
213 mgr->type = NUM_RSCTYP;
215 mgr->rscs = kzalloc(((amount + 8 - 1) / 8), GFP_KERNEL);
221 err = hw->src_mgr_get_ctrl_blk(&mgr->ctrl_blk);
224 err = hw->srcimp_mgr_get_ctrl_blk(&mgr->ctrl_blk);
227 err = hw->amixer_mgr_get_ctrl_blk(&mgr->ctrl_blk);
230 err = hw->daio_mgr_get_ctrl_blk(hw, &mgr->ctrl_blk);
235 dev_err(hw->card->dev,
236 "Invalid resource type value %d!\n", type);
242 dev_err(hw->card->dev,
243 "Failed to get manager control block!\n");
248 mgr->avail = mgr->amount = amount;
258 int rsc_mgr_uninit(struct rsc_mgr *mgr)
263 if ((NULL != mgr->hw) && (NULL != mgr->ctrl_blk)) {
266 mgr->hw->src_mgr_put_ctrl_blk(mgr->ctrl_blk);
269 mgr->hw->srcimp_mgr_put_ctrl_blk(mgr->ctrl_blk);
272 mgr->hw->amixer_mgr_put_ctrl_blk(mgr->ctrl_blk);
275 mgr->hw->daio_mgr_put_ctrl_blk(mgr->ctrl_blk);
280 dev_err(((struct hw *)mgr->hw)->card->dev,
281 "Invalid resource type value %d!\n",
286 mgr->hw = mgr->ctrl_blk = NULL;
289 mgr->type = NUM_RSCTYP;
290 mgr->avail = mgr->amount = 0;