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 70#define TEA6330T_MASTER_VOLUME(xname, xindex) \ 71{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 72 .info = snd_tea6330t_info_master_volume, \ 73 .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume } 74 75static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol, 76 struct snd_ctl_elem_info *uinfo) 77{ 78 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 79 uinfo->count = 2; 80 uinfo->value.integer.min = 0; 81 uinfo->value.integer.max = 43; 82 return 0; 83} 84 85static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol, 86 struct snd_ctl_elem_value *ucontrol) 87{ 88 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 89 90 snd_i2c_lock(tea->bus); 91 ucontrol->value.integer.value[0] = tea->mleft - 0x14; 92 ucontrol->value.integer.value[1] = tea->mright - 0x14; 93 snd_i2c_unlock(tea->bus); 94 return 0; 95} 96 97static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol, 98 struct snd_ctl_elem_value *ucontrol) 99{ 100 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 101 int change, count, err; 102 unsigned char bytes[3]; 103 unsigned char val1, val2; 104 105 val1 = (ucontrol->value.integer.value[0] % 44) + 0x14; 106 val2 = (ucontrol->value.integer.value[1] % 44) + 0x14; 107 snd_i2c_lock(tea->bus); 108 change = val1 != tea->mleft || val2 != tea->mright; 109 tea->mleft = val1; 110 tea->mright = val2; 111 count = 0; 112 if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) { 113 bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT; 114 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft; 115 } 116 if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) { 117 if (count == 0) 118 bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT; 119 bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright; 120 } 121 if (count > 0) { 122 if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0) 123 change = err; 124 } 125 snd_i2c_unlock(tea->bus); 126 return change; 127} 128 129#define TEA6330T_MASTER_SWITCH(xname, xindex) \ 130{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 131 .info = snd_tea6330t_info_master_switch, \ 132 .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch } 133 134#define snd_tea6330t_info_master_switch snd_ctl_boolean_stereo_info 135 136static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol, 137 struct snd_ctl_elem_value *ucontrol) 138{ 139 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 140 141 snd_i2c_lock(tea->bus); 142 ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1; 143 ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1; 144 snd_i2c_unlock(tea->bus); 145 return 0; 146} 147 148static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol, 149 struct snd_ctl_elem_value *ucontrol) 150{ 151 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 152 int change, err; 153 unsigned char bytes[3]; 154 unsigned char oval1, oval2, val1, val2; 155 156 val1 = ucontrol->value.integer.value[0] & 1; 157 val2 = ucontrol->value.integer.value[1] & 1; 158 snd_i2c_lock(tea->bus); 159 oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1; 160 oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1; 161 change = val1 != oval1 || val2 != oval2; 162 tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0; 163 tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0; 164 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT; 165 bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT]; 166 bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT]; 167 if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0) 168 change = err; 169 snd_i2c_unlock(tea->bus); 170 return change; 171} 172 173#define TEA6330T_BASS(xname, xindex) \ 174{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 175 .info = snd_tea6330t_info_bass, \ 176 .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass } 177 178static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol, 179 struct snd_ctl_elem_info *uinfo) 180{ 181 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 182 183 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 184 uinfo->count = 1; 185 uinfo->value.integer.min = 0; 186 uinfo->value.integer.max = tea->max_bass; 187 return 0; 188} 189 190static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol, 191 struct snd_ctl_elem_value *ucontrol) 192{ 193 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 194 195 ucontrol->value.integer.value[0] = tea->bass; 196 return 0; 197} 198 199static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol, 200 struct snd_ctl_elem_value *ucontrol) 201{ 202 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 203 int change, err; 204 unsigned char bytes[2]; 205 unsigned char val1; 206 207 val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1); 208 snd_i2c_lock(tea->bus); 209 tea->bass = val1; 210 val1 += tea->equalizer ? 7 : 3; 211 change = tea->regs[TEA6330T_SADDR_BASS] != val1; 212 bytes[0] = TEA6330T_SADDR_BASS; 213 bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1; 214 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0) 215 change = err; 216 snd_i2c_unlock(tea->bus); 217 return change; 218} 219 220#define TEA6330T_TREBLE(xname, xindex) \ 221{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 222 .info = snd_tea6330t_info_treble, \ 223 .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble } 224 225static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol, 226 struct snd_ctl_elem_info *uinfo) 227{ 228 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 229 230 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 231 uinfo->count = 1; 232 uinfo->value.integer.min = 0; 233 uinfo->value.integer.max = tea->max_treble; 234 return 0; 235} 236 237static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol, 238 struct snd_ctl_elem_value *ucontrol) 239{ 240 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 241 242 ucontrol->value.integer.value[0] = tea->treble; 243 return 0; 244} 245 246static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol, 247 struct snd_ctl_elem_value *ucontrol) 248{ 249 struct tea6330t *tea = snd_kcontrol_chip(kcontrol); 250 int change, err; 251 unsigned char bytes[2]; 252 unsigned char val1; 253 254 val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1); 255 snd_i2c_lock(tea->bus); 256 tea->treble = val1; 257 val1 += 3; 258 change = tea->regs[TEA6330T_SADDR_TREBLE] != val1; 259 bytes[0] = TEA6330T_SADDR_TREBLE; 260 bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1; 261 if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0) 262 change = err; 263 snd_i2c_unlock(tea->bus); 264 return change; 265} 266 267static struct snd_kcontrol_new snd_tea6330t_controls[] = { 268TEA6330T_MASTER_SWITCH("Master Playback Switch", 0), 269TEA6330T_MASTER_VOLUME("Master Playback Volume", 0), 270TEA6330T_BASS("Tone Control - Bass", 0), 271TEA6330T_TREBLE("Tone Control - Treble", 0) 272}; 273 274static void snd_tea6330_free(struct snd_i2c_device *device) 275{ 276 kfree(device->private_data); 277} 278 279int snd_tea6330t_update_mixer(struct snd_card *card, 280 struct snd_i2c_bus *bus, 281 int equalizer, int fader) 282{ 283 struct snd_i2c_device *device; 284 struct tea6330t *tea; 285 struct snd_kcontrol_new *knew; 286 unsigned int idx; 287 int err = -ENOMEM; 288 u8 default_treble, default_bass; 289 unsigned char bytes[7]; 290 291 tea = kzalloc(sizeof(*tea), GFP_KERNEL); 292 if (tea == NULL) 293 return -ENOMEM; 294 if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) { 295 kfree(tea); 296 return err; 297 } 298 tea->device = device; 299 tea->bus = bus; 300 tea->equalizer = equalizer; 301 tea->fader = fader; 302 device->private_data = tea; 303 device->private_free = snd_tea6330_free; 304 305 snd_i2c_lock(bus); 306 307 /* turn fader off and handle equalizer */ 308 tea->regs[TEA6330T_SADDR_FADER] = 0x3f; 309 tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN; 310 /* initialize mixer */ 311 if (!tea->equalizer) { 312 tea->max_bass = 9; 313 tea->max_treble = 8; 314 default_bass = 3 + 4; 315 tea->bass = 4; 316 default_treble = 3 + 4; 317 tea->treble = 4; 318 } else { 319 tea->max_bass = 5; 320 tea->max_treble = 0; 321 default_bass = 7 + 4; 322 tea->bass = 4; 323 default_treble = 3; 324 tea->treble = 0; 325 } 326 tea->mleft = tea->mright = 0x14; 327 tea->regs[TEA6330T_SADDR_BASS] = default_bass; 328 tea->regs[TEA6330T_SADDR_TREBLE] = default_treble; 329 330 /* compose I2C message and put the hardware to initial state */ 331 bytes[0] = TEA6330T_SADDR_VOLUME_LEFT; 332 for (idx = 0; idx < 6; idx++) 333 bytes[idx+1] = tea->regs[idx]; 334 if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0) 335 goto __error; 336 337 strcat(card->mixername, ",TEA6330T"); 338 if ((err = snd_component_add(card, "TEA6330T")) < 0) 339 goto __error; 340 341 for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) { 342 knew = &snd_tea6330t_controls[idx]; 343 if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble")) 344 continue; 345 if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0) 346 goto __error; 347 } 348 349 snd_i2c_unlock(bus); 350 return 0; 351 352 __error: 353 snd_i2c_unlock(bus); 354 snd_i2c_device_free(device); 355 return err; 356} 357 358EXPORT_SYMBOL(snd_tea6330t_detect); 359EXPORT_SYMBOL(snd_tea6330t_update_mixer); 360 361/* 362 * INIT part 363 */ 364 365static int __init alsa_tea6330t_init(void) 366{ 367 return 0; 368} 369 370static void __exit alsa_tea6330t_exit(void) 371{ 372} 373 374module_init(alsa_tea6330t_init) 375module_exit(alsa_tea6330t_exit) 376