linux/sound/i2c/tea6330t.c
<<
>>
Prefs
   1/*
   2 *  Routines for control of the TEA6330T circuit via i2c bus
   3 *  Sound fader control circuit for car radios by Philips Semiconductors
   4 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   5 *
   6 *
   7 *   This program is free software; you can redistribute it and/or modify
   8 *   it under the terms of the GNU General Public License as published by
   9 *   the Free Software Foundation; either version 2 of the License, or
  10 *   (at your option) any later version.
  11 *
  12 *   This program is distributed in the hope that it will be useful,
  13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 *   GNU General Public License for more details.
  16 *
  17 *   You should have received a copy of the GNU General Public License
  18 *   along with this program; if not, write to the Free Software
  19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20 *
  21 */
  22
  23#include <linux/init.h>
  24#include <linux/slab.h>
  25#include <sound/core.h>
  26#include <sound/control.h>
  27#include <sound/tea6330t.h>
  28
  29MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  30MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
  31MODULE_LICENSE("GPL");
  32
  33#define TEA6330T_ADDR                   (0x80>>1) /* fixed address */
  34
  35#define TEA6330T_SADDR_VOLUME_LEFT      0x00    /* volume left */
  36#define TEA6330T_SADDR_VOLUME_RIGHT     0x01    /* volume right */
  37#define TEA6330T_SADDR_BASS             0x02    /* bass control */
  38#define TEA6330T_SADDR_TREBLE           0x03    /* treble control */
  39#define TEA6330T_SADDR_FADER            0x04    /* fader control */
  40#define   TEA6330T_MFN                  0x20    /* mute control for selected channels */
  41#define   TEA6330T_FCH                  0x10    /* select fader channels - front or rear */
  42#define TEA6330T_SADDR_AUDIO_SWITCH     0x05    /* audio switch */
  43#define   TEA6330T_GMU                  0x80    /* mute control, general mute */
  44#define   TEA6330T_EQN                  0x40    /* equalizer switchover (0=equalizer-on) */
  45
  46
  47struct tea6330t {
  48        struct snd_i2c_device *device;
  49        struct snd_i2c_bus *bus;
  50        int equalizer;
  51        int fader;
  52        unsigned char regs[8];
  53        unsigned char mleft, mright;
  54        unsigned char bass, treble;
  55        unsigned char max_bass, max_treble;
  56};
  57
  58
  59int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
  60{
  61        int res;
  62
  63        snd_i2c_lock(bus);
  64        res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
  65        snd_i2c_unlock(bus);
  66        return res;
  67}
  68
  69#if 0
  70static void snd_tea6330t_set(struct tea6330t *tea,
  71                             unsigned char addr, unsigned char value)
  72{
  73#if 0
  74        printk(KERN_DEBUG "set - 0x%x/0x%x\n", addr, value);
  75#endif
  76        snd_i2c_write(tea->bus, TEA6330T_ADDR, addr, value, 1);
  77}
  78#endif
  79
  80#define TEA6330T_MASTER_VOLUME(xname, xindex) \
  81{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  82  .info = snd_tea6330t_info_master_volume, \
  83  .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
  84
  85static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
  86                                           struct snd_ctl_elem_info *uinfo)
  87{
  88        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  89        uinfo->count = 2;
  90        uinfo->value.integer.min = 0;
  91        uinfo->value.integer.max = 43;
  92        return 0;
  93}
  94
  95static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
  96                                          struct snd_ctl_elem_value *ucontrol)
  97{
  98        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
  99        
 100        snd_i2c_lock(tea->bus);
 101        ucontrol->value.integer.value[0] = tea->mleft - 0x14;
 102        ucontrol->value.integer.value[1] = tea->mright - 0x14;
 103        snd_i2c_unlock(tea->bus);
 104        return 0;
 105}
 106
 107static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
 108                                          struct snd_ctl_elem_value *ucontrol)
 109{
 110        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 111        int change, count, err;
 112        unsigned char bytes[3];
 113        unsigned char val1, val2;
 114        
 115        val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
 116        val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
 117        snd_i2c_lock(tea->bus);
 118        change = val1 != tea->mleft || val2 != tea->mright;
 119        tea->mleft = val1;
 120        tea->mright = val2;
 121        count = 0;
 122        if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
 123                bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
 124                bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
 125        }
 126        if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
 127                if (count == 0)
 128                        bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
 129                bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
 130        }
 131        if (count > 0) {
 132                if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
 133                        change = err;
 134        }
 135        snd_i2c_unlock(tea->bus);
 136        return change;
 137}
 138
 139#define TEA6330T_MASTER_SWITCH(xname, xindex) \
 140{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
 141  .info = snd_tea6330t_info_master_switch, \
 142  .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
 143
 144#define snd_tea6330t_info_master_switch         snd_ctl_boolean_stereo_info
 145
 146static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
 147                                          struct snd_ctl_elem_value *ucontrol)
 148{
 149        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 150        
 151        snd_i2c_lock(tea->bus);
 152        ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
 153        ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
 154        snd_i2c_unlock(tea->bus);
 155        return 0;
 156}
 157
 158static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
 159                                          struct snd_ctl_elem_value *ucontrol)
 160{
 161        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 162        int change, err;
 163        unsigned char bytes[3];
 164        unsigned char oval1, oval2, val1, val2;
 165        
 166        val1 = ucontrol->value.integer.value[0] & 1;
 167        val2 = ucontrol->value.integer.value[1] & 1;
 168        snd_i2c_lock(tea->bus);
 169        oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
 170        oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
 171        change = val1 != oval1 || val2 != oval2;
 172        tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
 173        tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
 174        bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
 175        bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
 176        bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
 177        if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
 178                change = err;
 179        snd_i2c_unlock(tea->bus);
 180        return change;
 181}
 182
 183#define TEA6330T_BASS(xname, xindex) \
 184{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
 185  .info = snd_tea6330t_info_bass, \
 186  .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
 187
 188static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
 189                                  struct snd_ctl_elem_info *uinfo)
 190{
 191        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 192
 193        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 194        uinfo->count = 1;
 195        uinfo->value.integer.min = 0;
 196        uinfo->value.integer.max = tea->max_bass;
 197        return 0;
 198}
 199
 200static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
 201                                 struct snd_ctl_elem_value *ucontrol)
 202{
 203        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 204        
 205        ucontrol->value.integer.value[0] = tea->bass;
 206        return 0;
 207}
 208
 209static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
 210                                 struct snd_ctl_elem_value *ucontrol)
 211{
 212        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 213        int change, err;
 214        unsigned char bytes[2];
 215        unsigned char val1;
 216        
 217        val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
 218        snd_i2c_lock(tea->bus);
 219        tea->bass = val1;
 220        val1 += tea->equalizer ? 7 : 3;
 221        change = tea->regs[TEA6330T_SADDR_BASS] != val1;
 222        bytes[0] = TEA6330T_SADDR_BASS;
 223        bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
 224        if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
 225                change = err;
 226        snd_i2c_unlock(tea->bus);
 227        return change;
 228}
 229
 230#define TEA6330T_TREBLE(xname, xindex) \
 231{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
 232  .info = snd_tea6330t_info_treble, \
 233  .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
 234
 235static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
 236                                    struct snd_ctl_elem_info *uinfo)
 237{
 238        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 239
 240        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 241        uinfo->count = 1;
 242        uinfo->value.integer.min = 0;
 243        uinfo->value.integer.max = tea->max_treble;
 244        return 0;
 245}
 246
 247static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
 248                                   struct snd_ctl_elem_value *ucontrol)
 249{
 250        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 251        
 252        ucontrol->value.integer.value[0] = tea->treble;
 253        return 0;
 254}
 255
 256static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
 257                                   struct snd_ctl_elem_value *ucontrol)
 258{
 259        struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
 260        int change, err;
 261        unsigned char bytes[2];
 262        unsigned char val1;
 263        
 264        val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
 265        snd_i2c_lock(tea->bus);
 266        tea->treble = val1;
 267        val1 += 3;
 268        change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
 269        bytes[0] = TEA6330T_SADDR_TREBLE;
 270        bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
 271        if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
 272                change = err;
 273        snd_i2c_unlock(tea->bus);
 274        return change;
 275}
 276
 277static struct snd_kcontrol_new snd_tea6330t_controls[] = {
 278TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
 279TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
 280TEA6330T_BASS("Tone Control - Bass", 0),
 281TEA6330T_TREBLE("Tone Control - Treble", 0)
 282};
 283
 284static void snd_tea6330_free(struct snd_i2c_device *device)
 285{
 286        kfree(device->private_data);
 287}
 288                                        
 289int snd_tea6330t_update_mixer(struct snd_card *card,
 290                              struct snd_i2c_bus *bus,
 291                              int equalizer, int fader)
 292{
 293        struct snd_i2c_device *device;
 294        struct tea6330t *tea;
 295        struct snd_kcontrol_new *knew;
 296        unsigned int idx;
 297        int err = -ENOMEM;
 298        u8 default_treble, default_bass;
 299        unsigned char bytes[7];
 300
 301        tea = kzalloc(sizeof(*tea), GFP_KERNEL);
 302        if (tea == NULL)
 303                return -ENOMEM;
 304        if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
 305                kfree(tea);
 306                return err;
 307        }
 308        tea->device = device;
 309        tea->bus = bus;
 310        tea->equalizer = equalizer;
 311        tea->fader = fader;
 312        device->private_data = tea;
 313        device->private_free = snd_tea6330_free;
 314
 315        snd_i2c_lock(bus);
 316
 317        /* turn fader off and handle equalizer */
 318        tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
 319        tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
 320        /* initialize mixer */
 321        if (!tea->equalizer) {
 322                tea->max_bass = 9;
 323                tea->max_treble = 8;
 324                default_bass = 3 + 4;
 325                tea->bass = 4;
 326                default_treble = 3 + 4;
 327                tea->treble = 4;
 328        } else {
 329                tea->max_bass = 5;
 330                tea->max_treble = 0;
 331                default_bass = 7 + 4;
 332                tea->bass = 4;
 333                default_treble = 3;
 334                tea->treble = 0;
 335        }
 336        tea->mleft = tea->mright = 0x14;
 337        tea->regs[TEA6330T_SADDR_BASS] = default_bass;
 338        tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
 339
 340        /* compose I2C message and put the hardware to initial state */
 341        bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
 342        for (idx = 0; idx < 6; idx++)
 343                bytes[idx+1] = tea->regs[idx];
 344        if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
 345                goto __error;
 346
 347        strcat(card->mixername, ",TEA6330T");
 348        if ((err = snd_component_add(card, "TEA6330T")) < 0)
 349                goto __error;
 350
 351        for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
 352                knew = &snd_tea6330t_controls[idx];
 353                if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
 354                        continue;
 355                if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
 356                        goto __error;
 357        }
 358
 359        snd_i2c_unlock(bus);
 360        return 0;
 361        
 362      __error:
 363        snd_i2c_unlock(bus);
 364        snd_i2c_device_free(device);
 365        return err;
 366}
 367
 368EXPORT_SYMBOL(snd_tea6330t_detect);
 369EXPORT_SYMBOL(snd_tea6330t_update_mixer);
 370
 371/*
 372 *  INIT part
 373 */
 374
 375static int __init alsa_tea6330t_init(void)
 376{
 377        return 0;
 378}
 379
 380static void __exit alsa_tea6330t_exit(void)
 381{
 382}
 383
 384module_init(alsa_tea6330t_init)
 385module_exit(alsa_tea6330t_exit)
 386