1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
8 * This file contains the implementation of some generic helper functions.
14 #include "ctresource.h"
15 #include "cthardware.h"
16 #include <linux/err.h>
17 #include <linux/slab.h>
19 #define AUDIO_SLOT_BLOCK_NUM 256
21 /* Resource allocation based on bit-map management mechanism */
23 get_resource(u8
*rscs
, unsigned int amount
,
24 unsigned int multi
, unsigned int *ridx
)
28 /* Check whether there are sufficient resources to meet request. */
29 for (i
= 0, n
= multi
; i
< amount
; i
++) {
32 if (rscs
[j
] & ((u8
)1 << k
)) {
37 break; /* found sufficient contiguous resources */
41 /* Can not find sufficient contiguous resources */
45 /* Mark the contiguous bits in resource bit-map as used */
46 for (n
= multi
; n
> 0; n
--) {
49 rscs
[j
] |= ((u8
)1 << k
);
58 static int put_resource(u8
*rscs
, unsigned int multi
, unsigned int idx
)
60 unsigned int i
, j
, k
, n
;
62 /* Mark the contiguous bits in resource bit-map as used */
63 for (n
= multi
, i
= idx
; n
> 0; n
--) {
66 rscs
[j
] &= ~((u8
)1 << k
);
73 int mgr_get_resource(struct rsc_mgr
*mgr
, unsigned int n
, unsigned int *ridx
)
80 err
= get_resource(mgr
->rscs
, mgr
->amount
, n
, ridx
);
87 int mgr_put_resource(struct rsc_mgr
*mgr
, unsigned int n
, unsigned int idx
)
89 put_resource(mgr
->rscs
, n
, idx
);
95 static const unsigned char offset_in_audio_slot_block
[NUM_RSCTYP
] = {
96 /* SRC channel is at Audio Ring slot 1 every 16 slots. */
102 static int rsc_index(const struct rsc
*rsc
)
107 static int audio_ring_slot(const struct rsc
*rsc
)
109 return (rsc
->conj
<< 4) + offset_in_audio_slot_block
[rsc
->type
];
112 static int rsc_next_conj(struct rsc
*rsc
)
115 for (i
= 0; (i
< 8) && (!(rsc
->msr
& (0x1 << i
))); )
117 rsc
->conj
+= (AUDIO_SLOT_BLOCK_NUM
>> i
);
121 static int rsc_master(struct rsc
*rsc
)
123 return rsc
->conj
= rsc
->idx
;
126 static const struct rsc_ops rsc_generic_ops
= {
128 .output_slot
= audio_ring_slot
,
129 .master
= rsc_master
,
130 .next_conj
= rsc_next_conj
,
134 rsc_init(struct rsc
*rsc
, u32 idx
, enum RSCTYP type
, u32 msr
, struct hw
*hw
)
143 rsc
->ops
= &rsc_generic_ops
;
145 rsc
->ctrl_blk
= NULL
;
151 err
= hw
->src_rsc_get_ctrl_blk(&rsc
->ctrl_blk
);
154 err
= hw
->amixer_rsc_get_ctrl_blk(&rsc
->ctrl_blk
);
161 dev_err(((struct hw
*)hw
)->card
->dev
,
162 "Invalid resource type value %d!\n", type
);
167 dev_err(((struct hw
*)hw
)->card
->dev
,
168 "Failed to get resource control block!\n");
175 int rsc_uninit(struct rsc
*rsc
)
177 if ((NULL
!= rsc
->hw
) && (NULL
!= rsc
->ctrl_blk
)) {
180 rsc
->hw
->src_rsc_put_ctrl_blk(rsc
->ctrl_blk
);
183 rsc
->hw
->amixer_rsc_put_ctrl_blk(rsc
->ctrl_blk
);
189 dev_err(((struct hw
*)rsc
->hw
)->card
->dev
,
190 "Invalid resource type value %d!\n",
195 rsc
->hw
= rsc
->ctrl_blk
= NULL
;
198 rsc
->idx
= rsc
->conj
= 0;
199 rsc
->type
= NUM_RSCTYP
;
205 int rsc_mgr_init(struct rsc_mgr
*mgr
, enum RSCTYP type
,
206 unsigned int amount
, struct hw
*hw
)
210 mgr
->type
= NUM_RSCTYP
;
212 mgr
->rscs
= kzalloc(((amount
+ 8 - 1) / 8), GFP_KERNEL
);
218 err
= hw
->src_mgr_get_ctrl_blk(&mgr
->ctrl_blk
);
221 err
= hw
->srcimp_mgr_get_ctrl_blk(&mgr
->ctrl_blk
);
224 err
= hw
->amixer_mgr_get_ctrl_blk(&mgr
->ctrl_blk
);
227 err
= hw
->daio_mgr_get_ctrl_blk(hw
, &mgr
->ctrl_blk
);
232 dev_err(hw
->card
->dev
,
233 "Invalid resource type value %d!\n", type
);
239 dev_err(hw
->card
->dev
,
240 "Failed to get manager control block!\n");
245 mgr
->avail
= mgr
->amount
= amount
;
255 int rsc_mgr_uninit(struct rsc_mgr
*mgr
)
260 if ((NULL
!= mgr
->hw
) && (NULL
!= mgr
->ctrl_blk
)) {
263 mgr
->hw
->src_mgr_put_ctrl_blk(mgr
->ctrl_blk
);
266 mgr
->hw
->srcimp_mgr_put_ctrl_blk(mgr
->ctrl_blk
);
269 mgr
->hw
->amixer_mgr_put_ctrl_blk(mgr
->ctrl_blk
);
272 mgr
->hw
->daio_mgr_put_ctrl_blk(mgr
->ctrl_blk
);
277 dev_err(((struct hw
*)mgr
->hw
)->card
->dev
,
278 "Invalid resource type value %d!\n",
283 mgr
->hw
= mgr
->ctrl_blk
= NULL
;
286 mgr
->type
= NUM_RSCTYP
;
287 mgr
->avail
= mgr
->amount
= 0;