• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
3  *
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.
7  *
8  * @File	ctamixer.c
9  *
10  * @Brief
11  * This file contains the implementation of the Audio Mixer
12  * resource management object.
13  *
14  * @Author	Liu Chun
15  * @Date 	May 21 2008
16  *
17  */
18 
19 #include "ctamixer.h"
20 #include "cthardware.h"
21 #include <linux/slab.h>
22 
23 #define AMIXER_RESOURCE_NUM	256
24 #define SUM_RESOURCE_NUM	256
25 
26 #define AMIXER_Y_IMMEDIATE	1
27 
28 #define BLANK_SLOT		4094
29 
amixer_master(struct rsc * rsc)30 static void amixer_master(struct rsc *rsc)
31 {
32 	rsc->conj = 0;
33 	rsc->idx = container_of(rsc, struct amixer, rsc)->idx[0];
34 }
35 
amixer_next_conj(struct rsc * rsc)36 static void amixer_next_conj(struct rsc *rsc)
37 {
38 	rsc->conj++;
39 }
40 
amixer_index(const struct rsc * rsc)41 static int amixer_index(const struct rsc *rsc)
42 {
43 	return container_of(rsc, struct amixer, rsc)->idx[rsc->conj];
44 }
45 
amixer_output_slot(const struct rsc * rsc)46 static int amixer_output_slot(const struct rsc *rsc)
47 {
48 	return (amixer_index(rsc) << 4) + 0x4;
49 }
50 
51 static const struct rsc_ops amixer_basic_rsc_ops = {
52 	.master		= amixer_master,
53 	.next_conj	= amixer_next_conj,
54 	.index		= amixer_index,
55 	.output_slot	= amixer_output_slot,
56 };
57 
amixer_set_input(struct amixer * amixer,struct rsc * rsc)58 static int amixer_set_input(struct amixer *amixer, struct rsc *rsc)
59 {
60 	struct hw *hw;
61 
62 	hw = amixer->rsc.hw;
63 	hw->amixer_set_mode(amixer->rsc.ctrl_blk, AMIXER_Y_IMMEDIATE);
64 	amixer->input = rsc;
65 	if (!rsc)
66 		hw->amixer_set_x(amixer->rsc.ctrl_blk, BLANK_SLOT);
67 	else
68 		hw->amixer_set_x(amixer->rsc.ctrl_blk,
69 					rsc->ops->output_slot(rsc));
70 
71 	return 0;
72 }
73 
74 /* y is a 14-bit immediate constant */
amixer_set_y(struct amixer * amixer,unsigned int y)75 static int amixer_set_y(struct amixer *amixer, unsigned int y)
76 {
77 	struct hw *hw;
78 
79 	hw = amixer->rsc.hw;
80 	hw->amixer_set_y(amixer->rsc.ctrl_blk, y);
81 
82 	return 0;
83 }
84 
amixer_set_invalid_squash(struct amixer * amixer,unsigned int iv)85 static int amixer_set_invalid_squash(struct amixer *amixer, unsigned int iv)
86 {
87 	struct hw *hw;
88 
89 	hw = amixer->rsc.hw;
90 	hw->amixer_set_iv(amixer->rsc.ctrl_blk, iv);
91 
92 	return 0;
93 }
94 
amixer_set_sum(struct amixer * amixer,struct sum * sum)95 static int amixer_set_sum(struct amixer *amixer, struct sum *sum)
96 {
97 	struct hw *hw;
98 
99 	hw = amixer->rsc.hw;
100 	amixer->sum = sum;
101 	if (!sum) {
102 		hw->amixer_set_se(amixer->rsc.ctrl_blk, 0);
103 	} else {
104 		hw->amixer_set_se(amixer->rsc.ctrl_blk, 1);
105 		hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
106 					sum->rsc.ops->index(&sum->rsc));
107 	}
108 
109 	return 0;
110 }
111 
amixer_commit_write(struct amixer * amixer)112 static int amixer_commit_write(struct amixer *amixer)
113 {
114 	struct hw *hw;
115 	unsigned int index;
116 	int i;
117 	struct rsc *input;
118 	struct sum *sum;
119 
120 	hw = amixer->rsc.hw;
121 	input = amixer->input;
122 	sum = amixer->sum;
123 
124 	/* Program master and conjugate resources */
125 	amixer->rsc.ops->master(&amixer->rsc);
126 	if (input)
127 		input->ops->master(input);
128 
129 	if (sum)
130 		sum->rsc.ops->master(&sum->rsc);
131 
132 	for (i = 0; i < amixer->rsc.msr; i++) {
133 		hw->amixer_set_dirty_all(amixer->rsc.ctrl_blk);
134 		if (input) {
135 			hw->amixer_set_x(amixer->rsc.ctrl_blk,
136 						input->ops->output_slot(input));
137 			input->ops->next_conj(input);
138 		}
139 		if (sum) {
140 			hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
141 						sum->rsc.ops->index(&sum->rsc));
142 			sum->rsc.ops->next_conj(&sum->rsc);
143 		}
144 		index = amixer->rsc.ops->output_slot(&amixer->rsc);
145 		hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
146 		amixer->rsc.ops->next_conj(&amixer->rsc);
147 	}
148 	amixer->rsc.ops->master(&amixer->rsc);
149 	if (input)
150 		input->ops->master(input);
151 
152 	if (sum)
153 		sum->rsc.ops->master(&sum->rsc);
154 
155 	return 0;
156 }
157 
amixer_commit_raw_write(struct amixer * amixer)158 static int amixer_commit_raw_write(struct amixer *amixer)
159 {
160 	struct hw *hw;
161 	unsigned int index;
162 
163 	hw = amixer->rsc.hw;
164 	index = amixer->rsc.ops->output_slot(&amixer->rsc);
165 	hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
166 
167 	return 0;
168 }
169 
amixer_get_y(struct amixer * amixer)170 static int amixer_get_y(struct amixer *amixer)
171 {
172 	struct hw *hw;
173 
174 	hw = amixer->rsc.hw;
175 	return hw->amixer_get_y(amixer->rsc.ctrl_blk);
176 }
177 
amixer_setup(struct amixer * amixer,struct rsc * input,unsigned int scale,struct sum * sum)178 static int amixer_setup(struct amixer *amixer, struct rsc *input,
179 			unsigned int scale, struct sum *sum)
180 {
181 	amixer_set_input(amixer, input);
182 	amixer_set_y(amixer, scale);
183 	amixer_set_sum(amixer, sum);
184 	amixer_commit_write(amixer);
185 	return 0;
186 }
187 
188 static const struct amixer_rsc_ops amixer_ops = {
189 	.set_input		= amixer_set_input,
190 	.set_invalid_squash	= amixer_set_invalid_squash,
191 	.set_scale		= amixer_set_y,
192 	.set_sum		= amixer_set_sum,
193 	.commit_write		= amixer_commit_write,
194 	.commit_raw_write	= amixer_commit_raw_write,
195 	.setup			= amixer_setup,
196 	.get_scale		= amixer_get_y,
197 };
198 
amixer_rsc_init(struct amixer * amixer,const struct amixer_desc * desc,struct amixer_mgr * mgr)199 static int amixer_rsc_init(struct amixer *amixer,
200 			   const struct amixer_desc *desc,
201 			   struct amixer_mgr *mgr)
202 {
203 	int err;
204 
205 	err = rsc_init(&amixer->rsc, amixer->idx[0],
206 			AMIXER, desc->msr, mgr->mgr.hw);
207 	if (err)
208 		return err;
209 
210 	/* Set amixer specific operations */
211 	amixer->rsc.ops = &amixer_basic_rsc_ops;
212 	amixer->ops = &amixer_ops;
213 	amixer->input = NULL;
214 	amixer->sum = NULL;
215 
216 	amixer_setup(amixer, NULL, 0, NULL);
217 
218 	return 0;
219 }
220 
amixer_rsc_uninit(struct amixer * amixer)221 static int amixer_rsc_uninit(struct amixer *amixer)
222 {
223 	amixer_setup(amixer, NULL, 0, NULL);
224 	rsc_uninit(&amixer->rsc);
225 	amixer->ops = NULL;
226 	amixer->input = NULL;
227 	amixer->sum = NULL;
228 	return 0;
229 }
230 
get_amixer_rsc(struct amixer_mgr * mgr,const struct amixer_desc * desc,struct amixer ** ramixer)231 static int get_amixer_rsc(struct amixer_mgr *mgr,
232 			  const struct amixer_desc *desc,
233 			  struct amixer **ramixer)
234 {
235 	int err, i;
236 	unsigned int idx;
237 	struct amixer *amixer;
238 	unsigned long flags;
239 
240 	*ramixer = NULL;
241 
242 	/* Allocate mem for amixer resource */
243 	amixer = kzalloc(sizeof(*amixer), GFP_KERNEL);
244 	if (!amixer)
245 		return -ENOMEM;
246 
247 	/* Check whether there are sufficient
248 	 * amixer resources to meet request. */
249 	err = 0;
250 	spin_lock_irqsave(&mgr->mgr_lock, flags);
251 	for (i = 0; i < desc->msr; i++) {
252 		err = mgr_get_resource(&mgr->mgr, 1, &idx);
253 		if (err)
254 			break;
255 
256 		amixer->idx[i] = idx;
257 	}
258 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
259 	if (err) {
260 		dev_err(mgr->card->dev,
261 			"Can't meet AMIXER resource request!\n");
262 		goto error;
263 	}
264 
265 	err = amixer_rsc_init(amixer, desc, mgr);
266 	if (err)
267 		goto error;
268 
269 	*ramixer = amixer;
270 
271 	return 0;
272 
273 error:
274 	spin_lock_irqsave(&mgr->mgr_lock, flags);
275 	for (i--; i >= 0; i--)
276 		mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
277 
278 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
279 	kfree(amixer);
280 	return err;
281 }
282 
put_amixer_rsc(struct amixer_mgr * mgr,struct amixer * amixer)283 static int put_amixer_rsc(struct amixer_mgr *mgr, struct amixer *amixer)
284 {
285 	unsigned long flags;
286 	int i;
287 
288 	spin_lock_irqsave(&mgr->mgr_lock, flags);
289 	for (i = 0; i < amixer->rsc.msr; i++)
290 		mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
291 
292 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
293 	amixer_rsc_uninit(amixer);
294 	kfree(amixer);
295 
296 	return 0;
297 }
298 
amixer_mgr_create(struct hw * hw,struct amixer_mgr ** ramixer_mgr)299 int amixer_mgr_create(struct hw *hw, struct amixer_mgr **ramixer_mgr)
300 {
301 	int err;
302 	struct amixer_mgr *amixer_mgr;
303 
304 	*ramixer_mgr = NULL;
305 	amixer_mgr = kzalloc(sizeof(*amixer_mgr), GFP_KERNEL);
306 	if (!amixer_mgr)
307 		return -ENOMEM;
308 
309 	err = rsc_mgr_init(&amixer_mgr->mgr, AMIXER, AMIXER_RESOURCE_NUM, hw);
310 	if (err)
311 		goto error;
312 
313 	spin_lock_init(&amixer_mgr->mgr_lock);
314 
315 	amixer_mgr->get_amixer = get_amixer_rsc;
316 	amixer_mgr->put_amixer = put_amixer_rsc;
317 	amixer_mgr->card = hw->card;
318 
319 	*ramixer_mgr = amixer_mgr;
320 
321 	return 0;
322 
323 error:
324 	kfree(amixer_mgr);
325 	return err;
326 }
327 
amixer_mgr_destroy(struct amixer_mgr * amixer_mgr)328 int amixer_mgr_destroy(struct amixer_mgr *amixer_mgr)
329 {
330 	rsc_mgr_uninit(&amixer_mgr->mgr);
331 	kfree(amixer_mgr);
332 	return 0;
333 }
334 
335 /* SUM resource management */
336 
sum_master(struct rsc * rsc)337 static void sum_master(struct rsc *rsc)
338 {
339 	rsc->conj = 0;
340 	rsc->idx = container_of(rsc, struct sum, rsc)->idx[0];
341 }
342 
sum_next_conj(struct rsc * rsc)343 static void sum_next_conj(struct rsc *rsc)
344 {
345 	rsc->conj++;
346 }
347 
sum_index(const struct rsc * rsc)348 static int sum_index(const struct rsc *rsc)
349 {
350 	return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
351 }
352 
sum_output_slot(const struct rsc * rsc)353 static int sum_output_slot(const struct rsc *rsc)
354 {
355 	return (sum_index(rsc) << 4) + 0xc;
356 }
357 
358 static const struct rsc_ops sum_basic_rsc_ops = {
359 	.master		= sum_master,
360 	.next_conj	= sum_next_conj,
361 	.index		= sum_index,
362 	.output_slot	= sum_output_slot,
363 };
364 
sum_rsc_init(struct sum * sum,const struct sum_desc * desc,struct sum_mgr * mgr)365 static int sum_rsc_init(struct sum *sum,
366 			const struct sum_desc *desc,
367 			struct sum_mgr *mgr)
368 {
369 	int err;
370 
371 	err = rsc_init(&sum->rsc, sum->idx[0], SUM, desc->msr, mgr->mgr.hw);
372 	if (err)
373 		return err;
374 
375 	sum->rsc.ops = &sum_basic_rsc_ops;
376 
377 	return 0;
378 }
379 
sum_rsc_uninit(struct sum * sum)380 static int sum_rsc_uninit(struct sum *sum)
381 {
382 	rsc_uninit(&sum->rsc);
383 	return 0;
384 }
385 
get_sum_rsc(struct sum_mgr * mgr,const struct sum_desc * desc,struct sum ** rsum)386 static int get_sum_rsc(struct sum_mgr *mgr,
387 		       const struct sum_desc *desc,
388 		       struct sum **rsum)
389 {
390 	int err, i;
391 	unsigned int idx;
392 	struct sum *sum;
393 	unsigned long flags;
394 
395 	*rsum = NULL;
396 
397 	/* Allocate mem for sum resource */
398 	sum = kzalloc(sizeof(*sum), GFP_KERNEL);
399 	if (!sum)
400 		return -ENOMEM;
401 
402 	/* Check whether there are sufficient sum resources to meet request. */
403 	err = 0;
404 	spin_lock_irqsave(&mgr->mgr_lock, flags);
405 	for (i = 0; i < desc->msr; i++) {
406 		err = mgr_get_resource(&mgr->mgr, 1, &idx);
407 		if (err)
408 			break;
409 
410 		sum->idx[i] = idx;
411 	}
412 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
413 	if (err) {
414 		dev_err(mgr->card->dev,
415 			"Can't meet SUM resource request!\n");
416 		goto error;
417 	}
418 
419 	err = sum_rsc_init(sum, desc, mgr);
420 	if (err)
421 		goto error;
422 
423 	*rsum = sum;
424 
425 	return 0;
426 
427 error:
428 	spin_lock_irqsave(&mgr->mgr_lock, flags);
429 	for (i--; i >= 0; i--)
430 		mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
431 
432 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
433 	kfree(sum);
434 	return err;
435 }
436 
put_sum_rsc(struct sum_mgr * mgr,struct sum * sum)437 static int put_sum_rsc(struct sum_mgr *mgr, struct sum *sum)
438 {
439 	unsigned long flags;
440 	int i;
441 
442 	spin_lock_irqsave(&mgr->mgr_lock, flags);
443 	for (i = 0; i < sum->rsc.msr; i++)
444 		mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
445 
446 	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
447 	sum_rsc_uninit(sum);
448 	kfree(sum);
449 
450 	return 0;
451 }
452 
sum_mgr_create(struct hw * hw,struct sum_mgr ** rsum_mgr)453 int sum_mgr_create(struct hw *hw, struct sum_mgr **rsum_mgr)
454 {
455 	int err;
456 	struct sum_mgr *sum_mgr;
457 
458 	*rsum_mgr = NULL;
459 	sum_mgr = kzalloc(sizeof(*sum_mgr), GFP_KERNEL);
460 	if (!sum_mgr)
461 		return -ENOMEM;
462 
463 	err = rsc_mgr_init(&sum_mgr->mgr, SUM, SUM_RESOURCE_NUM, hw);
464 	if (err)
465 		goto error;
466 
467 	spin_lock_init(&sum_mgr->mgr_lock);
468 
469 	sum_mgr->get_sum = get_sum_rsc;
470 	sum_mgr->put_sum = put_sum_rsc;
471 	sum_mgr->card = hw->card;
472 
473 	*rsum_mgr = sum_mgr;
474 
475 	return 0;
476 
477 error:
478 	kfree(sum_mgr);
479 	return err;
480 }
481 
sum_mgr_destroy(struct sum_mgr * sum_mgr)482 int sum_mgr_destroy(struct sum_mgr *sum_mgr)
483 {
484 	rsc_mgr_uninit(&sum_mgr->mgr);
485 	kfree(sum_mgr);
486 	return 0;
487 }
488 
489