Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Routines for control of the TEA6330T circuit via i2c bus
0004  *  Sound fader control circuit for car radios by Philips Semiconductors
0005  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <sound/core.h>
0012 #include <sound/control.h>
0013 #include <sound/tea6330t.h>
0014 
0015 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0016 MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
0017 MODULE_LICENSE("GPL");
0018 
0019 #define TEA6330T_ADDR           (0x80>>1) /* fixed address */
0020 
0021 #define TEA6330T_SADDR_VOLUME_LEFT  0x00    /* volume left */
0022 #define TEA6330T_SADDR_VOLUME_RIGHT 0x01    /* volume right */
0023 #define TEA6330T_SADDR_BASS     0x02    /* bass control */
0024 #define TEA6330T_SADDR_TREBLE       0x03    /* treble control */
0025 #define TEA6330T_SADDR_FADER        0x04    /* fader control */
0026 #define   TEA6330T_MFN          0x20    /* mute control for selected channels */
0027 #define   TEA6330T_FCH          0x10    /* select fader channels - front or rear */
0028 #define TEA6330T_SADDR_AUDIO_SWITCH 0x05    /* audio switch */
0029 #define   TEA6330T_GMU          0x80    /* mute control, general mute */
0030 #define   TEA6330T_EQN          0x40    /* equalizer switchover (0=equalizer-on) */
0031 
0032 
0033 struct tea6330t {
0034     struct snd_i2c_device *device;
0035     struct snd_i2c_bus *bus;
0036     int equalizer;
0037     int fader;
0038     unsigned char regs[8];
0039     unsigned char mleft, mright;
0040     unsigned char bass, treble;
0041     unsigned char max_bass, max_treble;
0042 };
0043 
0044 
0045 int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
0046 {
0047     int res;
0048 
0049     snd_i2c_lock(bus);
0050     res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
0051     snd_i2c_unlock(bus);
0052     return res;
0053 }
0054 
0055 #if 0
0056 static void snd_tea6330t_set(struct tea6330t *tea,
0057                  unsigned char addr, unsigned char value)
0058 {
0059 #if 0
0060     printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
0061 #endif
0062     snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
0063 }
0064 #endif
0065 
0066 #define TEA6330T_MASTER_VOLUME(xname, xindex) \
0067 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0068   .info = snd_tea6330t_info_master_volume, \
0069   .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
0070 
0071 static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
0072                        struct snd_ctl_elem_info *uinfo)
0073 {
0074     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0075     uinfo->count = 2;
0076     uinfo->value.integer.min = 0;
0077     uinfo->value.integer.max = 43;
0078     return 0;
0079 }
0080 
0081 static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
0082                       struct snd_ctl_elem_value *ucontrol)
0083 {
0084     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0085     
0086     snd_i2c_lock(tea->bus);
0087     ucontrol->value.integer.value[0] = tea->mleft - 0x14;
0088     ucontrol->value.integer.value[1] = tea->mright - 0x14;
0089     snd_i2c_unlock(tea->bus);
0090     return 0;
0091 }
0092 
0093 static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
0094                       struct snd_ctl_elem_value *ucontrol)
0095 {
0096     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0097     int change, count, err;
0098     unsigned char bytes[3];
0099     unsigned char val1, val2;
0100     
0101     val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
0102     val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
0103     snd_i2c_lock(tea->bus);
0104     change = val1 != tea->mleft || val2 != tea->mright;
0105     tea->mleft = val1;
0106     tea->mright = val2;
0107     count = 0;
0108     if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
0109         bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
0110         bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
0111     }
0112     if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
0113         if (count == 0)
0114             bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
0115         bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
0116     }
0117     if (count > 0) {
0118         err = snd_i2c_sendbytes(tea->device, bytes, count);
0119         if (err < 0)
0120             change = err;
0121     }
0122     snd_i2c_unlock(tea->bus);
0123     return change;
0124 }
0125 
0126 #define TEA6330T_MASTER_SWITCH(xname, xindex) \
0127 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0128   .info = snd_tea6330t_info_master_switch, \
0129   .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
0130 
0131 #define snd_tea6330t_info_master_switch     snd_ctl_boolean_stereo_info
0132 
0133 static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
0134                       struct snd_ctl_elem_value *ucontrol)
0135 {
0136     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0137     
0138     snd_i2c_lock(tea->bus);
0139     ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
0140     ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
0141     snd_i2c_unlock(tea->bus);
0142     return 0;
0143 }
0144 
0145 static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
0146                       struct snd_ctl_elem_value *ucontrol)
0147 {
0148     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0149     int change, err;
0150     unsigned char bytes[3];
0151     unsigned char oval1, oval2, val1, val2;
0152     
0153     val1 = ucontrol->value.integer.value[0] & 1;
0154     val2 = ucontrol->value.integer.value[1] & 1;
0155     snd_i2c_lock(tea->bus);
0156     oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
0157     oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
0158     change = val1 != oval1 || val2 != oval2;
0159     tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
0160     tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
0161     bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
0162     bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
0163     bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
0164     err = snd_i2c_sendbytes(tea->device, bytes, 3);
0165     if (err < 0)
0166         change = err;
0167     snd_i2c_unlock(tea->bus);
0168     return change;
0169 }
0170 
0171 #define TEA6330T_BASS(xname, xindex) \
0172 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0173   .info = snd_tea6330t_info_bass, \
0174   .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
0175 
0176 static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
0177                   struct snd_ctl_elem_info *uinfo)
0178 {
0179     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0180 
0181     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0182     uinfo->count = 1;
0183     uinfo->value.integer.min = 0;
0184     uinfo->value.integer.max = tea->max_bass;
0185     return 0;
0186 }
0187 
0188 static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
0189                  struct snd_ctl_elem_value *ucontrol)
0190 {
0191     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0192     
0193     ucontrol->value.integer.value[0] = tea->bass;
0194     return 0;
0195 }
0196 
0197 static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
0198                  struct snd_ctl_elem_value *ucontrol)
0199 {
0200     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0201     int change, err;
0202     unsigned char bytes[2];
0203     unsigned char val1;
0204     
0205     val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
0206     snd_i2c_lock(tea->bus);
0207     tea->bass = val1;
0208     val1 += tea->equalizer ? 7 : 3;
0209     change = tea->regs[TEA6330T_SADDR_BASS] != val1;
0210     bytes[0] = TEA6330T_SADDR_BASS;
0211     bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
0212     err = snd_i2c_sendbytes(tea->device, bytes, 2);
0213     if (err < 0)
0214         change = err;
0215     snd_i2c_unlock(tea->bus);
0216     return change;
0217 }
0218 
0219 #define TEA6330T_TREBLE(xname, xindex) \
0220 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
0221   .info = snd_tea6330t_info_treble, \
0222   .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
0223 
0224 static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
0225                     struct snd_ctl_elem_info *uinfo)
0226 {
0227     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0228 
0229     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0230     uinfo->count = 1;
0231     uinfo->value.integer.min = 0;
0232     uinfo->value.integer.max = tea->max_treble;
0233     return 0;
0234 }
0235 
0236 static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
0237                    struct snd_ctl_elem_value *ucontrol)
0238 {
0239     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0240     
0241     ucontrol->value.integer.value[0] = tea->treble;
0242     return 0;
0243 }
0244 
0245 static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
0246                    struct snd_ctl_elem_value *ucontrol)
0247 {
0248     struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
0249     int change, err;
0250     unsigned char bytes[2];
0251     unsigned char val1;
0252     
0253     val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
0254     snd_i2c_lock(tea->bus);
0255     tea->treble = val1;
0256     val1 += 3;
0257     change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
0258     bytes[0] = TEA6330T_SADDR_TREBLE;
0259     bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
0260     err = snd_i2c_sendbytes(tea->device, bytes, 2);
0261     if (err < 0)
0262         change = err;
0263     snd_i2c_unlock(tea->bus);
0264     return change;
0265 }
0266 
0267 static const struct snd_kcontrol_new snd_tea6330t_controls[] = {
0268 TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
0269 TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
0270 TEA6330T_BASS("Tone Control - Bass", 0),
0271 TEA6330T_TREBLE("Tone Control - Treble", 0)
0272 };
0273 
0274 static void snd_tea6330_free(struct snd_i2c_device *device)
0275 {
0276     kfree(device->private_data);
0277 }
0278                                         
0279 int snd_tea6330t_update_mixer(struct snd_card *card,
0280                   struct snd_i2c_bus *bus,
0281                   int equalizer, int fader)
0282 {
0283     struct snd_i2c_device *device;
0284     struct tea6330t *tea;
0285     const struct snd_kcontrol_new *knew;
0286     unsigned int idx;
0287     int err;
0288     u8 default_treble, default_bass;
0289     unsigned char bytes[7];
0290 
0291     tea = kzalloc(sizeof(*tea), GFP_KERNEL);
0292     if (tea == NULL)
0293         return -ENOMEM;
0294     err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device);
0295     if (err < 0) {
0296         kfree(tea);
0297         return err;
0298     }
0299     tea->device = device;
0300     tea->bus = bus;
0301     tea->equalizer = equalizer;
0302     tea->fader = fader;
0303     device->private_data = tea;
0304     device->private_free = snd_tea6330_free;
0305 
0306     snd_i2c_lock(bus);
0307 
0308     /* turn fader off and handle equalizer */
0309     tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
0310     tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
0311     /* initialize mixer */
0312     if (!tea->equalizer) {
0313         tea->max_bass = 9;
0314         tea->max_treble = 8;
0315         default_bass = 3 + 4;
0316         tea->bass = 4;
0317         default_treble = 3 + 4;
0318         tea->treble = 4;
0319     } else {
0320         tea->max_bass = 5;
0321         tea->max_treble = 0;
0322         default_bass = 7 + 4;
0323         tea->bass = 4;
0324         default_treble = 3;
0325         tea->treble = 0;
0326     }
0327     tea->mleft = tea->mright = 0x14;
0328     tea->regs[TEA6330T_SADDR_BASS] = default_bass;
0329     tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
0330 
0331     /* compose I2C message and put the hardware to initial state */
0332     bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
0333     for (idx = 0; idx < 6; idx++)
0334         bytes[idx+1] = tea->regs[idx];
0335     err = snd_i2c_sendbytes(device, bytes, 7);
0336     if (err < 0)
0337         goto __error;
0338 
0339     strcat(card->mixername, ",TEA6330T");
0340     err = snd_component_add(card, "TEA6330T");
0341     if (err < 0)
0342         goto __error;
0343 
0344     for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
0345         knew = &snd_tea6330t_controls[idx];
0346         if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
0347             continue;
0348         err = snd_ctl_add(card, snd_ctl_new1(knew, tea));
0349         if (err < 0)
0350             goto __error;
0351     }
0352 
0353     snd_i2c_unlock(bus);
0354     return 0;
0355     
0356       __error:
0357         snd_i2c_unlock(bus);
0358         snd_i2c_device_free(device);
0359         return err;
0360 }
0361 
0362 EXPORT_SYMBOL(snd_tea6330t_detect);
0363 EXPORT_SYMBOL(snd_tea6330t_update_mixer);