Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
0004  *
0005  * @File    ctamixer.c
0006  *
0007  * @Brief
0008  * This file contains the implementation of the Audio Mixer
0009  * resource management object.
0010  *
0011  * @Author  Liu Chun
0012  * @Date    May 21 2008
0013  */
0014 
0015 #include "ctamixer.h"
0016 #include "cthardware.h"
0017 #include <linux/slab.h>
0018 
0019 #define AMIXER_RESOURCE_NUM 256
0020 #define SUM_RESOURCE_NUM    256
0021 
0022 #define AMIXER_Y_IMMEDIATE  1
0023 
0024 #define BLANK_SLOT      4094
0025 
0026 static void amixer_master(struct rsc *rsc)
0027 {
0028     rsc->conj = 0;
0029     rsc->idx = container_of(rsc, struct amixer, rsc)->idx[0];
0030 }
0031 
0032 static void amixer_next_conj(struct rsc *rsc)
0033 {
0034     rsc->conj++;
0035 }
0036 
0037 static int amixer_index(const struct rsc *rsc)
0038 {
0039     return container_of(rsc, struct amixer, rsc)->idx[rsc->conj];
0040 }
0041 
0042 static int amixer_output_slot(const struct rsc *rsc)
0043 {
0044     return (amixer_index(rsc) << 4) + 0x4;
0045 }
0046 
0047 static const struct rsc_ops amixer_basic_rsc_ops = {
0048     .master     = amixer_master,
0049     .next_conj  = amixer_next_conj,
0050     .index      = amixer_index,
0051     .output_slot    = amixer_output_slot,
0052 };
0053 
0054 static int amixer_set_input(struct amixer *amixer, struct rsc *rsc)
0055 {
0056     struct hw *hw;
0057 
0058     hw = amixer->rsc.hw;
0059     hw->amixer_set_mode(amixer->rsc.ctrl_blk, AMIXER_Y_IMMEDIATE);
0060     amixer->input = rsc;
0061     if (!rsc)
0062         hw->amixer_set_x(amixer->rsc.ctrl_blk, BLANK_SLOT);
0063     else
0064         hw->amixer_set_x(amixer->rsc.ctrl_blk,
0065                     rsc->ops->output_slot(rsc));
0066 
0067     return 0;
0068 }
0069 
0070 /* y is a 14-bit immediate constant */
0071 static int amixer_set_y(struct amixer *amixer, unsigned int y)
0072 {
0073     struct hw *hw;
0074 
0075     hw = amixer->rsc.hw;
0076     hw->amixer_set_y(amixer->rsc.ctrl_blk, y);
0077 
0078     return 0;
0079 }
0080 
0081 static int amixer_set_invalid_squash(struct amixer *amixer, unsigned int iv)
0082 {
0083     struct hw *hw;
0084 
0085     hw = amixer->rsc.hw;
0086     hw->amixer_set_iv(amixer->rsc.ctrl_blk, iv);
0087 
0088     return 0;
0089 }
0090 
0091 static int amixer_set_sum(struct amixer *amixer, struct sum *sum)
0092 {
0093     struct hw *hw;
0094 
0095     hw = amixer->rsc.hw;
0096     amixer->sum = sum;
0097     if (!sum) {
0098         hw->amixer_set_se(amixer->rsc.ctrl_blk, 0);
0099     } else {
0100         hw->amixer_set_se(amixer->rsc.ctrl_blk, 1);
0101         hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
0102                     sum->rsc.ops->index(&sum->rsc));
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 static int amixer_commit_write(struct amixer *amixer)
0109 {
0110     struct hw *hw;
0111     unsigned int index;
0112     int i;
0113     struct rsc *input;
0114     struct sum *sum;
0115 
0116     hw = amixer->rsc.hw;
0117     input = amixer->input;
0118     sum = amixer->sum;
0119 
0120     /* Program master and conjugate resources */
0121     amixer->rsc.ops->master(&amixer->rsc);
0122     if (input)
0123         input->ops->master(input);
0124 
0125     if (sum)
0126         sum->rsc.ops->master(&sum->rsc);
0127 
0128     for (i = 0; i < amixer->rsc.msr; i++) {
0129         hw->amixer_set_dirty_all(amixer->rsc.ctrl_blk);
0130         if (input) {
0131             hw->amixer_set_x(amixer->rsc.ctrl_blk,
0132                         input->ops->output_slot(input));
0133             input->ops->next_conj(input);
0134         }
0135         if (sum) {
0136             hw->amixer_set_sadr(amixer->rsc.ctrl_blk,
0137                         sum->rsc.ops->index(&sum->rsc));
0138             sum->rsc.ops->next_conj(&sum->rsc);
0139         }
0140         index = amixer->rsc.ops->output_slot(&amixer->rsc);
0141         hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
0142         amixer->rsc.ops->next_conj(&amixer->rsc);
0143     }
0144     amixer->rsc.ops->master(&amixer->rsc);
0145     if (input)
0146         input->ops->master(input);
0147 
0148     if (sum)
0149         sum->rsc.ops->master(&sum->rsc);
0150 
0151     return 0;
0152 }
0153 
0154 static int amixer_commit_raw_write(struct amixer *amixer)
0155 {
0156     struct hw *hw;
0157     unsigned int index;
0158 
0159     hw = amixer->rsc.hw;
0160     index = amixer->rsc.ops->output_slot(&amixer->rsc);
0161     hw->amixer_commit_write(hw, index, amixer->rsc.ctrl_blk);
0162 
0163     return 0;
0164 }
0165 
0166 static int amixer_get_y(struct amixer *amixer)
0167 {
0168     struct hw *hw;
0169 
0170     hw = amixer->rsc.hw;
0171     return hw->amixer_get_y(amixer->rsc.ctrl_blk);
0172 }
0173 
0174 static int amixer_setup(struct amixer *amixer, struct rsc *input,
0175             unsigned int scale, struct sum *sum)
0176 {
0177     amixer_set_input(amixer, input);
0178     amixer_set_y(amixer, scale);
0179     amixer_set_sum(amixer, sum);
0180     amixer_commit_write(amixer);
0181     return 0;
0182 }
0183 
0184 static const struct amixer_rsc_ops amixer_ops = {
0185     .set_input      = amixer_set_input,
0186     .set_invalid_squash = amixer_set_invalid_squash,
0187     .set_scale      = amixer_set_y,
0188     .set_sum        = amixer_set_sum,
0189     .commit_write       = amixer_commit_write,
0190     .commit_raw_write   = amixer_commit_raw_write,
0191     .setup          = amixer_setup,
0192     .get_scale      = amixer_get_y,
0193 };
0194 
0195 static int amixer_rsc_init(struct amixer *amixer,
0196                const struct amixer_desc *desc,
0197                struct amixer_mgr *mgr)
0198 {
0199     int err;
0200 
0201     err = rsc_init(&amixer->rsc, amixer->idx[0],
0202             AMIXER, desc->msr, mgr->mgr.hw);
0203     if (err)
0204         return err;
0205 
0206     /* Set amixer specific operations */
0207     amixer->rsc.ops = &amixer_basic_rsc_ops;
0208     amixer->ops = &amixer_ops;
0209     amixer->input = NULL;
0210     amixer->sum = NULL;
0211 
0212     amixer_setup(amixer, NULL, 0, NULL);
0213 
0214     return 0;
0215 }
0216 
0217 static int amixer_rsc_uninit(struct amixer *amixer)
0218 {
0219     amixer_setup(amixer, NULL, 0, NULL);
0220     rsc_uninit(&amixer->rsc);
0221     amixer->ops = NULL;
0222     amixer->input = NULL;
0223     amixer->sum = NULL;
0224     return 0;
0225 }
0226 
0227 static int get_amixer_rsc(struct amixer_mgr *mgr,
0228               const struct amixer_desc *desc,
0229               struct amixer **ramixer)
0230 {
0231     int err, i;
0232     unsigned int idx;
0233     struct amixer *amixer;
0234     unsigned long flags;
0235 
0236     *ramixer = NULL;
0237 
0238     /* Allocate mem for amixer resource */
0239     amixer = kzalloc(sizeof(*amixer), GFP_KERNEL);
0240     if (!amixer)
0241         return -ENOMEM;
0242 
0243     /* Check whether there are sufficient
0244      * amixer resources to meet request. */
0245     err = 0;
0246     spin_lock_irqsave(&mgr->mgr_lock, flags);
0247     for (i = 0; i < desc->msr; i++) {
0248         err = mgr_get_resource(&mgr->mgr, 1, &idx);
0249         if (err)
0250             break;
0251 
0252         amixer->idx[i] = idx;
0253     }
0254     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0255     if (err) {
0256         dev_err(mgr->card->dev,
0257             "Can't meet AMIXER resource request!\n");
0258         goto error;
0259     }
0260 
0261     err = amixer_rsc_init(amixer, desc, mgr);
0262     if (err)
0263         goto error;
0264 
0265     *ramixer = amixer;
0266 
0267     return 0;
0268 
0269 error:
0270     spin_lock_irqsave(&mgr->mgr_lock, flags);
0271     for (i--; i >= 0; i--)
0272         mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
0273 
0274     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0275     kfree(amixer);
0276     return err;
0277 }
0278 
0279 static int put_amixer_rsc(struct amixer_mgr *mgr, struct amixer *amixer)
0280 {
0281     unsigned long flags;
0282     int i;
0283 
0284     spin_lock_irqsave(&mgr->mgr_lock, flags);
0285     for (i = 0; i < amixer->rsc.msr; i++)
0286         mgr_put_resource(&mgr->mgr, 1, amixer->idx[i]);
0287 
0288     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0289     amixer_rsc_uninit(amixer);
0290     kfree(amixer);
0291 
0292     return 0;
0293 }
0294 
0295 int amixer_mgr_create(struct hw *hw, struct amixer_mgr **ramixer_mgr)
0296 {
0297     int err;
0298     struct amixer_mgr *amixer_mgr;
0299 
0300     *ramixer_mgr = NULL;
0301     amixer_mgr = kzalloc(sizeof(*amixer_mgr), GFP_KERNEL);
0302     if (!amixer_mgr)
0303         return -ENOMEM;
0304 
0305     err = rsc_mgr_init(&amixer_mgr->mgr, AMIXER, AMIXER_RESOURCE_NUM, hw);
0306     if (err)
0307         goto error;
0308 
0309     spin_lock_init(&amixer_mgr->mgr_lock);
0310 
0311     amixer_mgr->get_amixer = get_amixer_rsc;
0312     amixer_mgr->put_amixer = put_amixer_rsc;
0313     amixer_mgr->card = hw->card;
0314 
0315     *ramixer_mgr = amixer_mgr;
0316 
0317     return 0;
0318 
0319 error:
0320     kfree(amixer_mgr);
0321     return err;
0322 }
0323 
0324 int amixer_mgr_destroy(struct amixer_mgr *amixer_mgr)
0325 {
0326     rsc_mgr_uninit(&amixer_mgr->mgr);
0327     kfree(amixer_mgr);
0328     return 0;
0329 }
0330 
0331 /* SUM resource management */
0332 
0333 static void sum_master(struct rsc *rsc)
0334 {
0335     rsc->conj = 0;
0336     rsc->idx = container_of(rsc, struct sum, rsc)->idx[0];
0337 }
0338 
0339 static void sum_next_conj(struct rsc *rsc)
0340 {
0341     rsc->conj++;
0342 }
0343 
0344 static int sum_index(const struct rsc *rsc)
0345 {
0346     return container_of(rsc, struct sum, rsc)->idx[rsc->conj];
0347 }
0348 
0349 static int sum_output_slot(const struct rsc *rsc)
0350 {
0351     return (sum_index(rsc) << 4) + 0xc;
0352 }
0353 
0354 static const struct rsc_ops sum_basic_rsc_ops = {
0355     .master     = sum_master,
0356     .next_conj  = sum_next_conj,
0357     .index      = sum_index,
0358     .output_slot    = sum_output_slot,
0359 };
0360 
0361 static int sum_rsc_init(struct sum *sum,
0362             const struct sum_desc *desc,
0363             struct sum_mgr *mgr)
0364 {
0365     int err;
0366 
0367     err = rsc_init(&sum->rsc, sum->idx[0], SUM, desc->msr, mgr->mgr.hw);
0368     if (err)
0369         return err;
0370 
0371     sum->rsc.ops = &sum_basic_rsc_ops;
0372 
0373     return 0;
0374 }
0375 
0376 static int sum_rsc_uninit(struct sum *sum)
0377 {
0378     rsc_uninit(&sum->rsc);
0379     return 0;
0380 }
0381 
0382 static int get_sum_rsc(struct sum_mgr *mgr,
0383                const struct sum_desc *desc,
0384                struct sum **rsum)
0385 {
0386     int err, i;
0387     unsigned int idx;
0388     struct sum *sum;
0389     unsigned long flags;
0390 
0391     *rsum = NULL;
0392 
0393     /* Allocate mem for sum resource */
0394     sum = kzalloc(sizeof(*sum), GFP_KERNEL);
0395     if (!sum)
0396         return -ENOMEM;
0397 
0398     /* Check whether there are sufficient sum resources to meet request. */
0399     err = 0;
0400     spin_lock_irqsave(&mgr->mgr_lock, flags);
0401     for (i = 0; i < desc->msr; i++) {
0402         err = mgr_get_resource(&mgr->mgr, 1, &idx);
0403         if (err)
0404             break;
0405 
0406         sum->idx[i] = idx;
0407     }
0408     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0409     if (err) {
0410         dev_err(mgr->card->dev,
0411             "Can't meet SUM resource request!\n");
0412         goto error;
0413     }
0414 
0415     err = sum_rsc_init(sum, desc, mgr);
0416     if (err)
0417         goto error;
0418 
0419     *rsum = sum;
0420 
0421     return 0;
0422 
0423 error:
0424     spin_lock_irqsave(&mgr->mgr_lock, flags);
0425     for (i--; i >= 0; i--)
0426         mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
0427 
0428     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0429     kfree(sum);
0430     return err;
0431 }
0432 
0433 static int put_sum_rsc(struct sum_mgr *mgr, struct sum *sum)
0434 {
0435     unsigned long flags;
0436     int i;
0437 
0438     spin_lock_irqsave(&mgr->mgr_lock, flags);
0439     for (i = 0; i < sum->rsc.msr; i++)
0440         mgr_put_resource(&mgr->mgr, 1, sum->idx[i]);
0441 
0442     spin_unlock_irqrestore(&mgr->mgr_lock, flags);
0443     sum_rsc_uninit(sum);
0444     kfree(sum);
0445 
0446     return 0;
0447 }
0448 
0449 int sum_mgr_create(struct hw *hw, struct sum_mgr **rsum_mgr)
0450 {
0451     int err;
0452     struct sum_mgr *sum_mgr;
0453 
0454     *rsum_mgr = NULL;
0455     sum_mgr = kzalloc(sizeof(*sum_mgr), GFP_KERNEL);
0456     if (!sum_mgr)
0457         return -ENOMEM;
0458 
0459     err = rsc_mgr_init(&sum_mgr->mgr, SUM, SUM_RESOURCE_NUM, hw);
0460     if (err)
0461         goto error;
0462 
0463     spin_lock_init(&sum_mgr->mgr_lock);
0464 
0465     sum_mgr->get_sum = get_sum_rsc;
0466     sum_mgr->put_sum = put_sum_rsc;
0467     sum_mgr->card = hw->card;
0468 
0469     *rsum_mgr = sum_mgr;
0470 
0471     return 0;
0472 
0473 error:
0474     kfree(sum_mgr);
0475     return err;
0476 }
0477 
0478 int sum_mgr_destroy(struct sum_mgr *sum_mgr)
0479 {
0480     rsc_mgr_uninit(&sum_mgr->mgr);
0481     kfree(sum_mgr);
0482     return 0;
0483 }
0484