Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Apple Onboard Audio driver for tas codec
0004  *
0005  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
0006  *
0007  * Open questions:
0008  *  - How to distinguish between 3004 and versions?
0009  *
0010  * FIXMEs:
0011  *  - This codec driver doesn't honour the 'connected'
0012  *    property of the aoa_codec struct, hence if
0013  *    it is used in machines where not everything is
0014  *    connected it will display wrong mixer elements.
0015  *  - Driver assumes that the microphone is always
0016  *    monaureal and connected to the right channel of
0017  *    the input. This should also be a codec-dependent
0018  *    flag, maybe the codec should have 3 different
0019  *    bits for the three different possibilities how
0020  *    it can be hooked up...
0021  *    But as long as I don't see any hardware hooked
0022  *    up that way...
0023  *  - As Apple notes in their code, the tas3004 seems
0024  *    to delay the right channel by one sample. You can
0025  *    see this when for example recording stereo in
0026  *    audacity, or recording the tas output via cable
0027  *    on another machine (use a sinus generator or so).
0028  *    I tried programming the BiQuads but couldn't
0029  *    make the delay work, maybe someone can read the
0030  *    datasheet and fix it. The relevant Apple comment
0031  *    is in AppleTAS3004Audio.cpp lines 1637 ff. Note
0032  *    that their comment describing how they program
0033  *    the filters sucks...
0034  *
0035  * Other things:
0036  *  - this should actually register *two* aoa_codec
0037  *    structs since it has two inputs. Then it must
0038  *    use the prepare callback to forbid running the
0039  *    secondary output on a different clock.
0040  *    Also, whatever bus knows how to do this must
0041  *    provide two soundbus_dev devices and the fabric
0042  *    must be able to link them correctly.
0043  *
0044  *    I don't even know if Apple ever uses the second
0045  *    port on the tas3004 though, I don't think their
0046  *    i2s controllers can even do it. OTOH, they all
0047  *    derive the clocks from common clocks, so it
0048  *    might just be possible. The framework allows the
0049  *    codec to refine the transfer_info items in the
0050  *    usable callback, so we can simply remove the
0051  *    rates the second instance is not using when it
0052  *    actually is in use.
0053  *    Maybe we'll need to make the sound busses have
0054  *    a 'clock group id' value so the codec can
0055  *    determine if the two outputs can be driven at
0056  *    the same time. But that is likely overkill, up
0057  *    to the fabric to not link them up incorrectly,
0058  *    and up to the hardware designer to not wire
0059  *    them up in some weird unusable way.
0060  */
0061 #include <linux/i2c.h>
0062 #include <asm/pmac_low_i2c.h>
0063 #include <asm/prom.h>
0064 #include <linux/delay.h>
0065 #include <linux/module.h>
0066 #include <linux/mutex.h>
0067 #include <linux/slab.h>
0068 
0069 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
0070 MODULE_LICENSE("GPL");
0071 MODULE_DESCRIPTION("tas codec driver for snd-aoa");
0072 
0073 #include "tas.h"
0074 #include "tas-gain-table.h"
0075 #include "tas-basstreble.h"
0076 #include "../aoa.h"
0077 #include "../soundbus/soundbus.h"
0078 
0079 #define PFX "snd-aoa-codec-tas: "
0080 
0081 
0082 struct tas {
0083     struct aoa_codec    codec;
0084     struct i2c_client   *i2c;
0085     u32         mute_l:1, mute_r:1 ,
0086                 controls_created:1 ,
0087                 drc_enabled:1,
0088                 hw_enabled:1;
0089     u8          cached_volume_l, cached_volume_r;
0090     u8          mixer_l[3], mixer_r[3];
0091     u8          bass, treble;
0092     u8          acr;
0093     int         drc_range;
0094     /* protects hardware access against concurrency from
0095      * userspace when hitting controls and during
0096      * codec init/suspend/resume */
0097     struct mutex        mtx;
0098 };
0099 
0100 static int tas_reset_init(struct tas *tas);
0101 
0102 static struct tas *codec_to_tas(struct aoa_codec *codec)
0103 {
0104     return container_of(codec, struct tas, codec);
0105 }
0106 
0107 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
0108 {
0109     if (len == 1)
0110         return i2c_smbus_write_byte_data(tas->i2c, reg, *data);
0111     else
0112         return i2c_smbus_write_i2c_block_data(tas->i2c, reg, len, data);
0113 }
0114 
0115 static void tas3004_set_drc(struct tas *tas)
0116 {
0117     unsigned char val[6];
0118 
0119     if (tas->drc_enabled)
0120         val[0] = 0x50; /* 3:1 above threshold */
0121     else
0122         val[0] = 0x51; /* disabled */
0123     val[1] = 0x02; /* 1:1 below threshold */
0124     if (tas->drc_range > 0xef)
0125         val[2] = 0xef;
0126     else if (tas->drc_range < 0)
0127         val[2] = 0x00;
0128     else
0129         val[2] = tas->drc_range;
0130     val[3] = 0xb0;
0131     val[4] = 0x60;
0132     val[5] = 0xa0;
0133 
0134     tas_write_reg(tas, TAS_REG_DRC, 6, val);
0135 }
0136 
0137 static void tas_set_treble(struct tas *tas)
0138 {
0139     u8 tmp;
0140 
0141     tmp = tas3004_treble(tas->treble);
0142     tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
0143 }
0144 
0145 static void tas_set_bass(struct tas *tas)
0146 {
0147     u8 tmp;
0148 
0149     tmp = tas3004_bass(tas->bass);
0150     tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
0151 }
0152 
0153 static void tas_set_volume(struct tas *tas)
0154 {
0155     u8 block[6];
0156     int tmp;
0157     u8 left, right;
0158 
0159     left = tas->cached_volume_l;
0160     right = tas->cached_volume_r;
0161 
0162     if (left > 177) left = 177;
0163     if (right > 177) right = 177;
0164 
0165     if (tas->mute_l) left = 0;
0166     if (tas->mute_r) right = 0;
0167 
0168     /* analysing the volume and mixer tables shows
0169      * that they are similar enough when we shift
0170      * the mixer table down by 4 bits. The error
0171      * is miniscule, in just one item the error
0172      * is 1, at a value of 0x07f17b (mixer table
0173      * value is 0x07f17a) */
0174     tmp = tas_gaintable[left];
0175     block[0] = tmp>>20;
0176     block[1] = tmp>>12;
0177     block[2] = tmp>>4;
0178     tmp = tas_gaintable[right];
0179     block[3] = tmp>>20;
0180     block[4] = tmp>>12;
0181     block[5] = tmp>>4;
0182     tas_write_reg(tas, TAS_REG_VOL, 6, block);
0183 }
0184 
0185 static void tas_set_mixer(struct tas *tas)
0186 {
0187     u8 block[9];
0188     int tmp, i;
0189     u8 val;
0190 
0191     for (i=0;i<3;i++) {
0192         val = tas->mixer_l[i];
0193         if (val > 177) val = 177;
0194         tmp = tas_gaintable[val];
0195         block[3*i+0] = tmp>>16;
0196         block[3*i+1] = tmp>>8;
0197         block[3*i+2] = tmp;
0198     }
0199     tas_write_reg(tas, TAS_REG_LMIX, 9, block);
0200 
0201     for (i=0;i<3;i++) {
0202         val = tas->mixer_r[i];
0203         if (val > 177) val = 177;
0204         tmp = tas_gaintable[val];
0205         block[3*i+0] = tmp>>16;
0206         block[3*i+1] = tmp>>8;
0207         block[3*i+2] = tmp;
0208     }
0209     tas_write_reg(tas, TAS_REG_RMIX, 9, block);
0210 }
0211 
0212 /* alsa stuff */
0213 
0214 static int tas_dev_register(struct snd_device *dev)
0215 {
0216     return 0;
0217 }
0218 
0219 static const struct snd_device_ops ops = {
0220     .dev_register = tas_dev_register,
0221 };
0222 
0223 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
0224     struct snd_ctl_elem_info *uinfo)
0225 {
0226     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0227     uinfo->count = 2;
0228     uinfo->value.integer.min = 0;
0229     uinfo->value.integer.max = 177;
0230     return 0;
0231 }
0232 
0233 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
0234     struct snd_ctl_elem_value *ucontrol)
0235 {
0236     struct tas *tas = snd_kcontrol_chip(kcontrol);
0237 
0238     mutex_lock(&tas->mtx);
0239     ucontrol->value.integer.value[0] = tas->cached_volume_l;
0240     ucontrol->value.integer.value[1] = tas->cached_volume_r;
0241     mutex_unlock(&tas->mtx);
0242     return 0;
0243 }
0244 
0245 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
0246     struct snd_ctl_elem_value *ucontrol)
0247 {
0248     struct tas *tas = snd_kcontrol_chip(kcontrol);
0249 
0250     if (ucontrol->value.integer.value[0] < 0 ||
0251         ucontrol->value.integer.value[0] > 177)
0252         return -EINVAL;
0253     if (ucontrol->value.integer.value[1] < 0 ||
0254         ucontrol->value.integer.value[1] > 177)
0255         return -EINVAL;
0256 
0257     mutex_lock(&tas->mtx);
0258     if (tas->cached_volume_l == ucontrol->value.integer.value[0]
0259      && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
0260         mutex_unlock(&tas->mtx);
0261         return 0;
0262     }
0263 
0264     tas->cached_volume_l = ucontrol->value.integer.value[0];
0265     tas->cached_volume_r = ucontrol->value.integer.value[1];
0266     if (tas->hw_enabled)
0267         tas_set_volume(tas);
0268     mutex_unlock(&tas->mtx);
0269     return 1;
0270 }
0271 
0272 static const struct snd_kcontrol_new volume_control = {
0273     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0274     .name = "Master Playback Volume",
0275     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0276     .info = tas_snd_vol_info,
0277     .get = tas_snd_vol_get,
0278     .put = tas_snd_vol_put,
0279 };
0280 
0281 #define tas_snd_mute_info   snd_ctl_boolean_stereo_info
0282 
0283 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
0284     struct snd_ctl_elem_value *ucontrol)
0285 {
0286     struct tas *tas = snd_kcontrol_chip(kcontrol);
0287 
0288     mutex_lock(&tas->mtx);
0289     ucontrol->value.integer.value[0] = !tas->mute_l;
0290     ucontrol->value.integer.value[1] = !tas->mute_r;
0291     mutex_unlock(&tas->mtx);
0292     return 0;
0293 }
0294 
0295 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
0296     struct snd_ctl_elem_value *ucontrol)
0297 {
0298     struct tas *tas = snd_kcontrol_chip(kcontrol);
0299 
0300     mutex_lock(&tas->mtx);
0301     if (tas->mute_l == !ucontrol->value.integer.value[0]
0302      && tas->mute_r == !ucontrol->value.integer.value[1]) {
0303         mutex_unlock(&tas->mtx);
0304         return 0;
0305     }
0306 
0307     tas->mute_l = !ucontrol->value.integer.value[0];
0308     tas->mute_r = !ucontrol->value.integer.value[1];
0309     if (tas->hw_enabled)
0310         tas_set_volume(tas);
0311     mutex_unlock(&tas->mtx);
0312     return 1;
0313 }
0314 
0315 static const struct snd_kcontrol_new mute_control = {
0316     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0317     .name = "Master Playback Switch",
0318     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0319     .info = tas_snd_mute_info,
0320     .get = tas_snd_mute_get,
0321     .put = tas_snd_mute_put,
0322 };
0323 
0324 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
0325     struct snd_ctl_elem_info *uinfo)
0326 {
0327     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0328     uinfo->count = 2;
0329     uinfo->value.integer.min = 0;
0330     uinfo->value.integer.max = 177;
0331     return 0;
0332 }
0333 
0334 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
0335     struct snd_ctl_elem_value *ucontrol)
0336 {
0337     struct tas *tas = snd_kcontrol_chip(kcontrol);
0338     int idx = kcontrol->private_value;
0339 
0340     mutex_lock(&tas->mtx);
0341     ucontrol->value.integer.value[0] = tas->mixer_l[idx];
0342     ucontrol->value.integer.value[1] = tas->mixer_r[idx];
0343     mutex_unlock(&tas->mtx);
0344 
0345     return 0;
0346 }
0347 
0348 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
0349     struct snd_ctl_elem_value *ucontrol)
0350 {
0351     struct tas *tas = snd_kcontrol_chip(kcontrol);
0352     int idx = kcontrol->private_value;
0353 
0354     mutex_lock(&tas->mtx);
0355     if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
0356      && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
0357         mutex_unlock(&tas->mtx);
0358         return 0;
0359     }
0360 
0361     tas->mixer_l[idx] = ucontrol->value.integer.value[0];
0362     tas->mixer_r[idx] = ucontrol->value.integer.value[1];
0363 
0364     if (tas->hw_enabled)
0365         tas_set_mixer(tas);
0366     mutex_unlock(&tas->mtx);
0367     return 1;
0368 }
0369 
0370 #define MIXER_CONTROL(n,descr,idx)          \
0371 static const struct snd_kcontrol_new n##_control = {    \
0372     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,        \
0373     .name = descr " Playback Volume",       \
0374     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,  \
0375     .info = tas_snd_mixer_info,         \
0376     .get = tas_snd_mixer_get,           \
0377     .put = tas_snd_mixer_put,           \
0378     .private_value = idx,               \
0379 }
0380 
0381 MIXER_CONTROL(pcm1, "PCM", 0);
0382 MIXER_CONTROL(monitor, "Monitor", 2);
0383 
0384 static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
0385     struct snd_ctl_elem_info *uinfo)
0386 {
0387     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0388     uinfo->count = 1;
0389     uinfo->value.integer.min = 0;
0390     uinfo->value.integer.max = TAS3004_DRC_MAX;
0391     return 0;
0392 }
0393 
0394 static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
0395     struct snd_ctl_elem_value *ucontrol)
0396 {
0397     struct tas *tas = snd_kcontrol_chip(kcontrol);
0398 
0399     mutex_lock(&tas->mtx);
0400     ucontrol->value.integer.value[0] = tas->drc_range;
0401     mutex_unlock(&tas->mtx);
0402     return 0;
0403 }
0404 
0405 static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
0406     struct snd_ctl_elem_value *ucontrol)
0407 {
0408     struct tas *tas = snd_kcontrol_chip(kcontrol);
0409 
0410     if (ucontrol->value.integer.value[0] < 0 ||
0411         ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
0412         return -EINVAL;
0413 
0414     mutex_lock(&tas->mtx);
0415     if (tas->drc_range == ucontrol->value.integer.value[0]) {
0416         mutex_unlock(&tas->mtx);
0417         return 0;
0418     }
0419 
0420     tas->drc_range = ucontrol->value.integer.value[0];
0421     if (tas->hw_enabled)
0422         tas3004_set_drc(tas);
0423     mutex_unlock(&tas->mtx);
0424     return 1;
0425 }
0426 
0427 static const struct snd_kcontrol_new drc_range_control = {
0428     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0429     .name = "DRC Range",
0430     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0431     .info = tas_snd_drc_range_info,
0432     .get = tas_snd_drc_range_get,
0433     .put = tas_snd_drc_range_put,
0434 };
0435 
0436 #define tas_snd_drc_switch_info     snd_ctl_boolean_mono_info
0437 
0438 static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
0439     struct snd_ctl_elem_value *ucontrol)
0440 {
0441     struct tas *tas = snd_kcontrol_chip(kcontrol);
0442 
0443     mutex_lock(&tas->mtx);
0444     ucontrol->value.integer.value[0] = tas->drc_enabled;
0445     mutex_unlock(&tas->mtx);
0446     return 0;
0447 }
0448 
0449 static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
0450     struct snd_ctl_elem_value *ucontrol)
0451 {
0452     struct tas *tas = snd_kcontrol_chip(kcontrol);
0453 
0454     mutex_lock(&tas->mtx);
0455     if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
0456         mutex_unlock(&tas->mtx);
0457         return 0;
0458     }
0459 
0460     tas->drc_enabled = !!ucontrol->value.integer.value[0];
0461     if (tas->hw_enabled)
0462         tas3004_set_drc(tas);
0463     mutex_unlock(&tas->mtx);
0464     return 1;
0465 }
0466 
0467 static const struct snd_kcontrol_new drc_switch_control = {
0468     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0469     .name = "DRC Range Switch",
0470     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0471     .info = tas_snd_drc_switch_info,
0472     .get = tas_snd_drc_switch_get,
0473     .put = tas_snd_drc_switch_put,
0474 };
0475 
0476 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
0477     struct snd_ctl_elem_info *uinfo)
0478 {
0479     static const char * const texts[] = { "Line-In", "Microphone" };
0480 
0481     return snd_ctl_enum_info(uinfo, 1, 2, texts);
0482 }
0483 
0484 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
0485     struct snd_ctl_elem_value *ucontrol)
0486 {
0487     struct tas *tas = snd_kcontrol_chip(kcontrol);
0488 
0489     mutex_lock(&tas->mtx);
0490     ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
0491     mutex_unlock(&tas->mtx);
0492     return 0;
0493 }
0494 
0495 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
0496     struct snd_ctl_elem_value *ucontrol)
0497 {
0498     struct tas *tas = snd_kcontrol_chip(kcontrol);
0499     int oldacr;
0500 
0501     if (ucontrol->value.enumerated.item[0] > 1)
0502         return -EINVAL;
0503     mutex_lock(&tas->mtx);
0504     oldacr = tas->acr;
0505 
0506     /*
0507      * Despite what the data sheet says in one place, the
0508      * TAS_ACR_B_MONAUREAL bit forces mono output even when
0509      * input A (line in) is selected.
0510      */
0511     tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
0512     if (ucontrol->value.enumerated.item[0])
0513         tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
0514               TAS_ACR_B_MON_SEL_RIGHT;
0515     if (oldacr == tas->acr) {
0516         mutex_unlock(&tas->mtx);
0517         return 0;
0518     }
0519     if (tas->hw_enabled)
0520         tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
0521     mutex_unlock(&tas->mtx);
0522     return 1;
0523 }
0524 
0525 static const struct snd_kcontrol_new capture_source_control = {
0526     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0527     /* If we name this 'Input Source', it properly shows up in
0528      * alsamixer as a selection, * but it's shown under the
0529      * 'Playback' category.
0530      * If I name it 'Capture Source', it shows up in strange
0531      * ways (two bools of which one can be selected at a
0532      * time) but at least it's shown in the 'Capture'
0533      * category.
0534      * I was told that this was due to backward compatibility,
0535      * but I don't understand then why the mangling is *not*
0536      * done when I name it "Input Source".....
0537      */
0538     .name = "Capture Source",
0539     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0540     .info = tas_snd_capture_source_info,
0541     .get = tas_snd_capture_source_get,
0542     .put = tas_snd_capture_source_put,
0543 };
0544 
0545 static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
0546     struct snd_ctl_elem_info *uinfo)
0547 {
0548     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0549     uinfo->count = 1;
0550     uinfo->value.integer.min = TAS3004_TREBLE_MIN;
0551     uinfo->value.integer.max = TAS3004_TREBLE_MAX;
0552     return 0;
0553 }
0554 
0555 static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
0556     struct snd_ctl_elem_value *ucontrol)
0557 {
0558     struct tas *tas = snd_kcontrol_chip(kcontrol);
0559 
0560     mutex_lock(&tas->mtx);
0561     ucontrol->value.integer.value[0] = tas->treble;
0562     mutex_unlock(&tas->mtx);
0563     return 0;
0564 }
0565 
0566 static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
0567     struct snd_ctl_elem_value *ucontrol)
0568 {
0569     struct tas *tas = snd_kcontrol_chip(kcontrol);
0570 
0571     if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
0572         ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
0573         return -EINVAL;
0574     mutex_lock(&tas->mtx);
0575     if (tas->treble == ucontrol->value.integer.value[0]) {
0576         mutex_unlock(&tas->mtx);
0577         return 0;
0578     }
0579 
0580     tas->treble = ucontrol->value.integer.value[0];
0581     if (tas->hw_enabled)
0582         tas_set_treble(tas);
0583     mutex_unlock(&tas->mtx);
0584     return 1;
0585 }
0586 
0587 static const struct snd_kcontrol_new treble_control = {
0588     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0589     .name = "Treble",
0590     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0591     .info = tas_snd_treble_info,
0592     .get = tas_snd_treble_get,
0593     .put = tas_snd_treble_put,
0594 };
0595 
0596 static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
0597     struct snd_ctl_elem_info *uinfo)
0598 {
0599     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0600     uinfo->count = 1;
0601     uinfo->value.integer.min = TAS3004_BASS_MIN;
0602     uinfo->value.integer.max = TAS3004_BASS_MAX;
0603     return 0;
0604 }
0605 
0606 static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
0607     struct snd_ctl_elem_value *ucontrol)
0608 {
0609     struct tas *tas = snd_kcontrol_chip(kcontrol);
0610 
0611     mutex_lock(&tas->mtx);
0612     ucontrol->value.integer.value[0] = tas->bass;
0613     mutex_unlock(&tas->mtx);
0614     return 0;
0615 }
0616 
0617 static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
0618     struct snd_ctl_elem_value *ucontrol)
0619 {
0620     struct tas *tas = snd_kcontrol_chip(kcontrol);
0621 
0622     if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
0623         ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
0624         return -EINVAL;
0625     mutex_lock(&tas->mtx);
0626     if (tas->bass == ucontrol->value.integer.value[0]) {
0627         mutex_unlock(&tas->mtx);
0628         return 0;
0629     }
0630 
0631     tas->bass = ucontrol->value.integer.value[0];
0632     if (tas->hw_enabled)
0633         tas_set_bass(tas);
0634     mutex_unlock(&tas->mtx);
0635     return 1;
0636 }
0637 
0638 static const struct snd_kcontrol_new bass_control = {
0639     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0640     .name = "Bass",
0641     .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
0642     .info = tas_snd_bass_info,
0643     .get = tas_snd_bass_get,
0644     .put = tas_snd_bass_put,
0645 };
0646 
0647 static struct transfer_info tas_transfers[] = {
0648     {
0649         /* input */
0650         .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
0651         .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
0652         .transfer_in = 1,
0653     },
0654     {
0655         /* output */
0656         .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
0657         .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
0658         .transfer_in = 0,
0659     },
0660     {}
0661 };
0662 
0663 static int tas_usable(struct codec_info_item *cii,
0664               struct transfer_info *ti,
0665               struct transfer_info *out)
0666 {
0667     return 1;
0668 }
0669 
0670 static int tas_reset_init(struct tas *tas)
0671 {
0672     u8 tmp;
0673 
0674     tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
0675     msleep(5);
0676     tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
0677     msleep(5);
0678     tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
0679     msleep(20);
0680     tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
0681     msleep(10);
0682     tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
0683 
0684     tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
0685     if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
0686         goto outerr;
0687 
0688     tas->acr |= TAS_ACR_ANALOG_PDOWN;
0689     if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
0690         goto outerr;
0691 
0692     tmp = 0;
0693     if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
0694         goto outerr;
0695 
0696     tas3004_set_drc(tas);
0697 
0698     /* Set treble & bass to 0dB */
0699     tas->treble = TAS3004_TREBLE_ZERO;
0700     tas->bass = TAS3004_BASS_ZERO;
0701     tas_set_treble(tas);
0702     tas_set_bass(tas);
0703 
0704     tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
0705     if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
0706         goto outerr;
0707 
0708     return 0;
0709  outerr:
0710     return -ENODEV;
0711 }
0712 
0713 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
0714 {
0715     struct tas *tas = cii->codec_data;
0716 
0717     switch(clock) {
0718     case CLOCK_SWITCH_PREPARE_SLAVE:
0719         /* Clocks are going away, mute mute mute */
0720         tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
0721         tas->hw_enabled = 0;
0722         break;
0723     case CLOCK_SWITCH_SLAVE:
0724         /* Clocks are back, re-init the codec */
0725         mutex_lock(&tas->mtx);
0726         tas_reset_init(tas);
0727         tas_set_volume(tas);
0728         tas_set_mixer(tas);
0729         tas->hw_enabled = 1;
0730         tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
0731         mutex_unlock(&tas->mtx);
0732         break;
0733     default:
0734         /* doesn't happen as of now */
0735         return -EINVAL;
0736     }
0737     return 0;
0738 }
0739 
0740 #ifdef CONFIG_PM
0741 /* we are controlled via i2c and assume that is always up
0742  * If that wasn't the case, we'd have to suspend once
0743  * our i2c device is suspended, and then take note of that! */
0744 static int tas_suspend(struct tas *tas)
0745 {
0746     mutex_lock(&tas->mtx);
0747     tas->hw_enabled = 0;
0748     tas->acr |= TAS_ACR_ANALOG_PDOWN;
0749     tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
0750     mutex_unlock(&tas->mtx);
0751     return 0;
0752 }
0753 
0754 static int tas_resume(struct tas *tas)
0755 {
0756     /* reset codec */
0757     mutex_lock(&tas->mtx);
0758     tas_reset_init(tas);
0759     tas_set_volume(tas);
0760     tas_set_mixer(tas);
0761     tas->hw_enabled = 1;
0762     mutex_unlock(&tas->mtx);
0763     return 0;
0764 }
0765 
0766 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
0767 {
0768     return tas_suspend(cii->codec_data);
0769 }
0770 
0771 static int _tas_resume(struct codec_info_item *cii)
0772 {
0773     return tas_resume(cii->codec_data);
0774 }
0775 #else /* CONFIG_PM */
0776 #define _tas_suspend    NULL
0777 #define _tas_resume NULL
0778 #endif /* CONFIG_PM */
0779 
0780 static struct codec_info tas_codec_info = {
0781     .transfers = tas_transfers,
0782     /* in theory, we can drive it at 512 too...
0783      * but so far the framework doesn't allow
0784      * for that and I don't see much point in it. */
0785     .sysclock_factor = 256,
0786     /* same here, could be 32 for just one 16 bit format */
0787     .bus_factor = 64,
0788     .owner = THIS_MODULE,
0789     .usable = tas_usable,
0790     .switch_clock = tas_switch_clock,
0791     .suspend = _tas_suspend,
0792     .resume = _tas_resume,
0793 };
0794 
0795 static int tas_init_codec(struct aoa_codec *codec)
0796 {
0797     struct tas *tas = codec_to_tas(codec);
0798     int err;
0799 
0800     if (!tas->codec.gpio || !tas->codec.gpio->methods) {
0801         printk(KERN_ERR PFX "gpios not assigned!!\n");
0802         return -EINVAL;
0803     }
0804 
0805     mutex_lock(&tas->mtx);
0806     if (tas_reset_init(tas)) {
0807         printk(KERN_ERR PFX "tas failed to initialise\n");
0808         mutex_unlock(&tas->mtx);
0809         return -ENXIO;
0810     }
0811     tas->hw_enabled = 1;
0812     mutex_unlock(&tas->mtx);
0813 
0814     if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
0815                            aoa_get_card(),
0816                            &tas_codec_info, tas)) {
0817         printk(KERN_ERR PFX "error attaching tas to soundbus\n");
0818         return -ENODEV;
0819     }
0820 
0821     if (aoa_snd_device_new(SNDRV_DEV_CODEC, tas, &ops)) {
0822         printk(KERN_ERR PFX "failed to create tas snd device!\n");
0823         return -ENODEV;
0824     }
0825     err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
0826     if (err)
0827         goto error;
0828 
0829     err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
0830     if (err)
0831         goto error;
0832 
0833     err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
0834     if (err)
0835         goto error;
0836 
0837     err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
0838     if (err)
0839         goto error;
0840 
0841     err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
0842     if (err)
0843         goto error;
0844 
0845     err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
0846     if (err)
0847         goto error;
0848 
0849     err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
0850     if (err)
0851         goto error;
0852 
0853     err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
0854     if (err)
0855         goto error;
0856 
0857     err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
0858     if (err)
0859         goto error;
0860 
0861     return 0;
0862  error:
0863     tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
0864     snd_device_free(aoa_get_card(), tas);
0865     return err;
0866 }
0867 
0868 static void tas_exit_codec(struct aoa_codec *codec)
0869 {
0870     struct tas *tas = codec_to_tas(codec);
0871 
0872     if (!tas->codec.soundbus_dev)
0873         return;
0874     tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
0875 }
0876 
0877 
0878 static int tas_i2c_probe(struct i2c_client *client,
0879              const struct i2c_device_id *id)
0880 {
0881     struct device_node *node = client->dev.of_node;
0882     struct tas *tas;
0883 
0884     tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
0885 
0886     if (!tas)
0887         return -ENOMEM;
0888 
0889     mutex_init(&tas->mtx);
0890     tas->i2c = client;
0891     i2c_set_clientdata(client, tas);
0892 
0893     /* seems that half is a saner default */
0894     tas->drc_range = TAS3004_DRC_MAX / 2;
0895 
0896     strscpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
0897     tas->codec.owner = THIS_MODULE;
0898     tas->codec.init = tas_init_codec;
0899     tas->codec.exit = tas_exit_codec;
0900     tas->codec.node = of_node_get(node);
0901 
0902     if (aoa_codec_register(&tas->codec)) {
0903         goto fail;
0904     }
0905     printk(KERN_DEBUG
0906            "snd-aoa-codec-tas: tas found, addr 0x%02x on %pOF\n",
0907            (unsigned int)client->addr, node);
0908     return 0;
0909  fail:
0910     mutex_destroy(&tas->mtx);
0911     kfree(tas);
0912     return -EINVAL;
0913 }
0914 
0915 static int tas_i2c_remove(struct i2c_client *client)
0916 {
0917     struct tas *tas = i2c_get_clientdata(client);
0918     u8 tmp = TAS_ACR_ANALOG_PDOWN;
0919 
0920     aoa_codec_unregister(&tas->codec);
0921     of_node_put(tas->codec.node);
0922 
0923     /* power down codec chip */
0924     tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
0925 
0926     mutex_destroy(&tas->mtx);
0927     kfree(tas);
0928     return 0;
0929 }
0930 
0931 static const struct i2c_device_id tas_i2c_id[] = {
0932     { "MAC,tas3004", 0 },
0933     { }
0934 };
0935 MODULE_DEVICE_TABLE(i2c,tas_i2c_id);
0936 
0937 static struct i2c_driver tas_driver = {
0938     .driver = {
0939         .name = "aoa_codec_tas",
0940     },
0941     .probe = tas_i2c_probe,
0942     .remove = tas_i2c_remove,
0943     .id_table = tas_i2c_id,
0944 };
0945 
0946 module_i2c_driver(tas_driver);