29 #include <linux/i2c.h>
31 #include <linux/slab.h>
33 #include <linux/string.h>
37 #include <asm/machdep.h>
45 #define DBG(fmt...) printk(KERN_DEBUG fmt)
50 #define IS_G4DA (of_machine_is_compatible("PowerMac3,4"))
53 #define TAS_I2C_ADDR 0x34
56 #define TAS_REG_MCS 0x01
57 #define TAS_REG_DRC 0x02
58 #define TAS_REG_VOL 0x04
59 #define TAS_REG_TREBLE 0x05
60 #define TAS_REG_BASS 0x06
61 #define TAS_REG_INPUT1 0x07
62 #define TAS_REG_INPUT2 0x08
65 #define TAS_REG_PCM TAS_REG_INPUT1
68 #define TAS_REG_LMIX TAS_REG_INPUT1
69 #define TAS_REG_RMIX TAS_REG_INPUT2
70 #define TAS_REG_MCS2 0x43
71 #define TAS_REG_ACS 0x40
131 DBG(
"(W) i2c error %d\n", err);
142 static int tumbler_init_client(
struct pmac_keywest *i2c)
144 static unsigned int regs[] = {
149 DBG(
"(I) tumbler init client\n");
150 return send_init_client(i2c, regs);
153 static int snapper_init_client(
struct pmac_keywest *i2c)
155 static unsigned int regs[] = {
164 DBG(
"(I) snapper init client\n");
165 return send_init_client(i2c, regs);
171 #define do_gpio_write(gp, val) \
172 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
173 #define do_gpio_read(gp) \
174 pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
175 #define tumbler_gpio_free(gp)
183 DBG(
"(I) gpio %x write %d\n", gp->
addr, active);
198 static int read_audio_gpio(
struct pmac_gpio *gp)
204 ret = (ret & 0x02) !=0;
213 unsigned char block[6];
214 unsigned int left_vol, right_vol;
216 if (! mix->
i2c.client)
223 if (left_vol >=
ARRAY_SIZE(master_volume_table))
224 left_vol =
ARRAY_SIZE(master_volume_table) - 1;
225 left_vol = master_volume_table[left_vol];
231 if (right_vol >=
ARRAY_SIZE(master_volume_table))
232 right_vol =
ARRAY_SIZE(master_volume_table) - 1;
233 right_vol = master_volume_table[right_vol];
236 block[0] = (left_vol >> 16) & 0xff;
237 block[1] = (left_vol >> 8) & 0xff;
238 block[2] = (left_vol >> 0) & 0xff;
240 block[3] = (right_vol >> 16) & 0xff;
241 block[4] = (right_vol >> 8) & 0xff;
242 block[5] = (right_vol >> 0) & 0xff;
249 DBG(
"(I) succeeded to set volume (%u, %u)\n", left_vol, right_vol);
255 static int tumbler_info_master_volume(
struct snd_kcontrol *kcontrol,
265 static int tumbler_get_master_volume(
struct snd_kcontrol *kcontrol,
276 static int tumbler_put_master_volume(
struct snd_kcontrol *kcontrol,
284 vol[0] = ucontrol->
value.integer.value[0];
285 vol[1] = ucontrol->
value.integer.value[1];
286 if (vol[0] >=
ARRAY_SIZE(master_volume_table) ||
294 tumbler_set_master_volume(mix);
300 static int tumbler_get_master_switch(
struct snd_kcontrol *kcontrol,
311 static int tumbler_put_master_switch(
struct snd_kcontrol *kcontrol,
323 tumbler_set_master_volume(mix);
333 #define TAS3001_DRC_MAX 0x5f
337 unsigned char val[2];
339 if (! mix->
i2c.client)
360 DBG(
"(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
368 #define TAS3004_DRC_MAX 0xef
372 unsigned char val[6];
374 if (! mix->
i2c.client)
397 DBG(
"(I) succeeded to set DRC (%u, %u)\n", val[0], val[1]);
401 static int tumbler_info_drc_value(
struct snd_kcontrol *kcontrol,
413 static int tumbler_get_drc_value(
struct snd_kcontrol *kcontrol,
424 static int tumbler_put_drc_value(
struct snd_kcontrol *kcontrol,
434 val = ucontrol->
value.integer.value[0];
446 tumbler_set_drc(mix);
448 snapper_set_drc(mix);
453 static int tumbler_get_drc_switch(
struct snd_kcontrol *kcontrol,
464 static int tumbler_put_drc_switch(
struct snd_kcontrol *kcontrol,
477 tumbler_set_drc(mix);
479 snapper_set_drc(mix);
497 static int tumbler_set_mono_volume(
struct pmac_tumbler *mix,
500 unsigned char block[4];
504 if (! mix->
i2c.client)
508 if (vol >= info->
max)
511 for (i = 0; i < info->
bytes; i++)
512 block[i] = (vol >> ((info->
bytes - i - 1) * 8)) & 0xff;
514 info->
bytes, block) < 0) {
522 static int tumbler_info_mono(
struct snd_kcontrol *kcontrol,
534 static int tumbler_get_mono(
struct snd_kcontrol *kcontrol,
546 static int tumbler_put_mono(
struct snd_kcontrol *kcontrol,
557 vol = ucontrol->
value.integer.value[0];
558 if (vol >= info->
max)
563 tumbler_set_mono_volume(mix, info);
574 .table = mixer_volume_table,
582 .table = bass_volume_table,
590 .table = treble_volume_table,
599 .table = snapper_bass_volume_table,
606 .max =
ARRAY_SIZE(snapper_treble_volume_table),
607 .table = snapper_treble_volume_table,
611 #define DEFINE_MONO(xname,type) { \
612 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
614 .info = tumbler_info_mono, \
615 .get = tumbler_get_mono, \
616 .put = tumbler_put_mono, \
617 .private_value = (unsigned long)(&tumbler_##type##_vol_info), \
620 #define DEFINE_SNAPPER_MONO(xname,type) { \
621 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
623 .info = tumbler_info_mono, \
624 .get = tumbler_get_mono, \
625 .put = tumbler_put_mono, \
626 .private_value = (unsigned long)(&snapper_##type##_vol_info), \
637 unsigned char block[9];
645 for (i = 0; i < 3; i++) {
647 vol = mixer_volume_table[
vol];
648 for (j = 0; j < 3; j++)
649 block[i * 3 + j] = (vol >> ((2 - j) * 8)) & 0xff;
661 if (! mix->
i2c.client)
663 if (snapper_set_mix_vol1(mix, idx, 0,
TAS_REG_LMIX) < 0 ||
669 static int snapper_info_mix(
struct snd_kcontrol *kcontrol,
679 static int snapper_get_mix(
struct snd_kcontrol *kcontrol,
692 static int snapper_put_mix(
struct snd_kcontrol *kcontrol,
703 vol[0] = ucontrol->
value.integer.value[0];
704 vol[1] = ucontrol->
value.integer.value[1];
705 if (vol[0] >=
ARRAY_SIZE(mixer_volume_table) ||
713 snapper_set_mix_vol(mix, idx);
726 static int tumbler_get_mute_switch(
struct snd_kcontrol *kcontrol,
746 ucontrol->
value.integer.value[0] = !check_audio_gpio(gp);
750 static int tumbler_put_mute_switch(
struct snd_kcontrol *kcontrol,
757 #ifdef PMAC_SUPPORT_AUTOMUTE
775 val = ! check_audio_gpio(gp);
776 if (val != ucontrol->
value.integer.value[0]) {
777 write_audio_gpio(gp, ! ucontrol->
value.integer.value[0]);
783 static int snapper_set_capture_source(
struct pmac_tumbler *mix)
785 if (! mix->
i2c.client)
794 static int snapper_info_capture_source(
struct snd_kcontrol *kcontrol,
797 static char *texts[2] = {
809 static int snapper_get_capture_source(
struct snd_kcontrol *kcontrol,
819 static int snapper_put_capture_source(
struct snd_kcontrol *kcontrol,
829 snapper_set_capture_source(mix);
834 #define DEFINE_SNAPPER_MIX(xname,idx,ofs) { \
835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,\
837 .info = snapper_info_mix, \
838 .get = snapper_get_mix, \
839 .put = snapper_put_mix, \
841 .private_value = ofs, \
849 .name =
"Master Playback Volume",
850 .info = tumbler_info_master_volume,
851 .get = tumbler_get_master_volume,
852 .put = tumbler_put_master_volume
855 .name =
"Master Playback Switch",
857 .get = tumbler_get_master_switch,
858 .put = tumbler_put_master_switch
865 .info = tumbler_info_drc_value,
866 .get = tumbler_get_drc_value,
867 .put = tumbler_put_drc_value
873 .name =
"Master Playback Volume",
874 .info = tumbler_info_master_volume,
875 .get = tumbler_get_master_volume,
876 .put = tumbler_put_master_volume
879 .name =
"Master Playback Switch",
881 .get = tumbler_get_master_switch,
882 .put = tumbler_put_master_switch
892 .info = tumbler_info_drc_value,
893 .get = tumbler_get_drc_value,
894 .put = tumbler_put_drc_value
897 .name =
"Input Source",
898 .info = snapper_info_capture_source,
899 .get = snapper_get_capture_source,
900 .put = snapper_put_capture_source
906 .name =
"Headphone Playback Switch",
908 .get = tumbler_get_mute_switch,
909 .put = tumbler_put_mute_switch,
914 .name =
"Speaker Playback Switch",
916 .get = tumbler_get_mute_switch,
917 .put = tumbler_put_mute_switch,
922 .name =
"Line Out Playback Switch",
924 .get = tumbler_get_mute_switch,
925 .put = tumbler_put_mute_switch,
930 .name =
"DRC Switch",
932 .get = tumbler_get_drc_switch,
933 .put = tumbler_put_drc_switch
937 #ifdef PMAC_SUPPORT_AUTOMUTE
941 static int tumbler_detect_headphone(
struct snd_pmac *chip)
947 detect |= read_audio_gpio(&mix->
hp_detect);
951 static int tumbler_detect_lineout(
struct snd_pmac *chip)
961 static void check_mute(
struct snd_pmac *chip,
struct pmac_gpio *gp,
int val,
int do_notify,
964 if (check_audio_gpio(gp) != val) {
965 write_audio_gpio(gp, val);
973 static struct snd_pmac *device_change_chip;
977 struct snd_pmac *chip = device_change_chip;
979 int headphone, lineout;
988 headphone = tumbler_detect_headphone(chip);
989 lineout = tumbler_detect_lineout(chip);
991 DBG(
"headphone: %d, lineout: %d\n", headphone, lineout);
993 if (headphone || lineout) {
1021 #ifdef CONFIG_SND_POWERMAC_AUTO_DRC
1027 tumbler_set_drc(mix);
1029 snapper_set_drc(mix);
1033 tumbler_set_master_volume(mix);
1036 static void tumbler_update_automute(
struct snd_pmac *chip,
int do_notify)
1082 static struct device_node *find_compatible_audio_device(
const char *name)
1101 static long tumbler_find_device(
const char *
device,
const char *platform,
1102 struct pmac_gpio *gp,
int is_compatible)
1110 node = find_compatible_audio_device(device);
1112 node = find_audio_device(device);
1114 DBG(
"(W) cannot find audio device %s !\n", device);
1123 DBG(
"(E) cannot find address for device %s !\n", device);
1124 snd_printd(
"cannot find address for device %s\n", device);
1134 gp->
addr = addr & 0x0000ffff;
1144 && !
strncmp(device,
"keywest-gpio1", 13);
1154 if (prop[3] == 0x9 && prop[4] == 0x9) {
1158 if (prop[3] == 0x1 && prop[4] == 0x1) {
1165 DBG(
"(I) GPIO device %s found, offset: %x, active state: %d !\n",
1174 static void tumbler_reset_audio(
struct snd_pmac *chip)
1179 DBG(
"(I) codec anded reset !\n");
1180 write_audio_gpio(&mix->
hp_mute, 0);
1181 write_audio_gpio(&mix->
amp_mute, 0);
1183 write_audio_gpio(&mix->
hp_mute, 1);
1184 write_audio_gpio(&mix->
amp_mute, 1);
1186 write_audio_gpio(&mix->
hp_mute, 0);
1187 write_audio_gpio(&mix->
amp_mute, 0);
1190 DBG(
"(I) codec normal reset !\n");
1203 static void tumbler_suspend(
struct snd_pmac *chip)
1216 tumbler_set_master_volume(mix);
1218 write_audio_gpio(&mix->
amp_mute, 1);
1219 write_audio_gpio(&mix->
hp_mute, 1);
1226 write_audio_gpio(&mix->
amp_mute, 1);
1227 write_audio_gpio(&mix->
hp_mute, 1);
1233 static void tumbler_resume(
struct snd_pmac *chip)
1242 tumbler_reset_audio(chip);
1243 if (mix->
i2c.client && mix->
i2c.init_client) {
1244 if (mix->
i2c.init_client(&mix->
i2c) < 0)
1249 tumbler_set_mono_volume(mix, &tumbler_pcm_vol_info);
1250 tumbler_set_mono_volume(mix, &tumbler_bass_vol_info);
1251 tumbler_set_mono_volume(mix, &tumbler_treble_vol_info);
1252 tumbler_set_drc(mix);
1257 tumbler_set_mono_volume(mix, &snapper_bass_vol_info);
1258 tumbler_set_mono_volume(mix, &snapper_treble_vol_info);
1259 snapper_set_drc(mix);
1260 snapper_set_capture_source(mix);
1262 tumbler_set_master_volume(mix);
1284 if (tumbler_find_device(
"audio-hw-reset",
1285 "platform-do-hw-reset",
1287 tumbler_find_device(
"hw-reset",
1288 "platform-do-hw-reset",
1290 if (tumbler_find_device(
"amp-mute",
1291 "platform-do-amp-mute",
1293 tumbler_find_device(
"amp-mute",
1294 "platform-do-amp-mute",
1296 if (tumbler_find_device(
"headphone-mute",
1297 "platform-do-headphone-mute",
1299 tumbler_find_device(
"headphone-mute",
1300 "platform-do-headphone-mute",
1302 if (tumbler_find_device(
"line-output-mute",
1303 "platform-do-lineout-mute",
1305 tumbler_find_device(
"line-output-mute",
1306 "platform-do-lineout-mute",
1308 irq = tumbler_find_device(
"headphone-detect",
1311 irq = tumbler_find_device(
"headphone-detect",
1314 irq = tumbler_find_device(
"keywest-gpio15",
1317 irq = tumbler_find_device(
"line-output-detect",
1320 irq = tumbler_find_device(
"line-output-detect",
1323 irq = tumbler_find_device(
"keywest-gpio16",
1327 tumbler_reset_audio(chip);
1332 static void tumbler_cleanup(
struct snd_pmac *chip)
1360 request_module(
"i2c-powermac");
1372 for (np = chip->
node->child; np; np = np->
sibling) {
1381 if ((err = tumbler_init(chip)) < 0)
1386 if (tas_node ==
NULL)
1388 if (tas_node ==
NULL)
1395 mix->
i2c.addr = (*paddr) >> 1;
1398 of_node_put(tas_node);
1400 DBG(
"(I) TAS i2c address is: %x\n", mix->
i2c.addr);
1403 mix->
i2c.init_client = tumbler_init_client;
1404 mix->
i2c.name =
"TAS3001c";
1405 chipname =
"Tumbler";
1407 mix->
i2c.init_client = snapper_init_client;
1408 mix->
i2c.name =
"TAS3004";
1409 chipname =
"Snapper";
1418 sprintf(chip->
card->mixername,
"PowerMac %s", chipname);
1421 for (i = 0; i <
ARRAY_SIZE(tumbler_mixers); i++) {
1426 for (i = 0; i <
ARRAY_SIZE(snapper_mixers); i++) {
1453 tumbler_set_drc(mix);
1455 snapper_set_drc(mix);
1458 chip->suspend = tumbler_suspend;
1459 chip->resume = tumbler_resume;
1462 INIT_WORK(&device_change, device_change_handler);
1463 device_change_chip =
chip;
1465 #ifdef PMAC_SUPPORT_AUTOMUTE
1471 tumbler_update_automute(chip, 0);
1477 "Sound Headphone Detection", chip)) < 0)
1486 "Sound Lineout Detection", chip)) < 0)