blob: aa09ebd9ffb8cfe9aed528a5e8f8d315bd9097a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PMac DACA lowlevel functions
3 *
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22#include <sound/driver.h>
23#include <linux/init.h>
24#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kmod.h>
26#include <linux/slab.h>
27#include <sound/core.h>
28#include "pmac.h"
29
30/* i2c address */
31#define DACA_I2C_ADDR 0x4d
32
33/* registers */
34#define DACA_REG_SR 0x01
35#define DACA_REG_AVOL 0x02
36#define DACA_REG_GCFG 0x03
37
38/* maximum volume value */
39#define DACA_VOL_MAX 0x38
40
41
Takashi Iwai65b29f52005-11-17 15:09:46 +010042struct pmac_daca {
43 struct pmac_keywest i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int left_vol, right_vol;
45 unsigned int deemphasis : 1;
46 unsigned int amp_on : 1;
Takashi Iwai65b29f52005-11-17 15:09:46 +010047};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
50/*
51 * initialize / detect DACA
52 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010053static int daca_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 unsigned short wdata = 0x00;
56 /* SR: no swap, 1bit delay, 32-48kHz */
57 /* GCFG: power amp inverted, DAC on */
58 if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
59 i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
60 return -EINVAL;
61 return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
62 2, (unsigned char*)&wdata);
63}
64
65/*
66 * update volume
67 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010068static int daca_set_volume(struct pmac_daca *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 unsigned char data[2];
71
72 if (! mix->i2c.client)
73 return -ENODEV;
74
75 if (mix->left_vol > DACA_VOL_MAX)
76 data[0] = DACA_VOL_MAX;
77 else
78 data[0] = mix->left_vol;
79 if (mix->right_vol > DACA_VOL_MAX)
80 data[1] = DACA_VOL_MAX;
81 else
82 data[1] = mix->right_vol;
83 data[1] |= mix->deemphasis ? 0x40 : 0;
84 if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
85 2, data) < 0) {
86 snd_printk("failed to set volume \n");
87 return -EINVAL;
88 }
89 return 0;
90}
91
92
93/* deemphasis switch */
Takashi Iwai65b29f52005-11-17 15:09:46 +010094static int daca_info_deemphasis(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
98 uinfo->count = 1;
99 uinfo->value.integer.min = 0;
100 uinfo->value.integer.max = 1;
101 return 0;
102}
103
Takashi Iwai65b29f52005-11-17 15:09:46 +0100104static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
105 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100107 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
108 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (! (mix = chip->mixer_data))
110 return -ENODEV;
111 ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
112 return 0;
113}
114
Takashi Iwai65b29f52005-11-17 15:09:46 +0100115static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
116 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100118 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
119 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int change;
121
122 if (! (mix = chip->mixer_data))
123 return -ENODEV;
124 change = mix->deemphasis != ucontrol->value.integer.value[0];
125 if (change) {
126 mix->deemphasis = ucontrol->value.integer.value[0];
127 daca_set_volume(mix);
128 }
129 return change;
130}
131
132/* output volume */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100133static int daca_info_volume(struct snd_kcontrol *kcontrol,
134 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
137 uinfo->count = 2;
138 uinfo->value.integer.min = 0;
139 uinfo->value.integer.max = DACA_VOL_MAX;
140 return 0;
141}
142
Takashi Iwai65b29f52005-11-17 15:09:46 +0100143static int daca_get_volume(struct snd_kcontrol *kcontrol,
144 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100146 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
147 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (! (mix = chip->mixer_data))
149 return -ENODEV;
150 ucontrol->value.integer.value[0] = mix->left_vol;
151 ucontrol->value.integer.value[1] = mix->right_vol;
152 return 0;
153}
154
Takashi Iwai65b29f52005-11-17 15:09:46 +0100155static int daca_put_volume(struct snd_kcontrol *kcontrol,
156 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100158 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
159 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int change;
161
162 if (! (mix = chip->mixer_data))
163 return -ENODEV;
164 change = mix->left_vol != ucontrol->value.integer.value[0] ||
165 mix->right_vol != ucontrol->value.integer.value[1];
166 if (change) {
167 mix->left_vol = ucontrol->value.integer.value[0];
168 mix->right_vol = ucontrol->value.integer.value[1];
169 daca_set_volume(mix);
170 }
171 return change;
172}
173
174/* amplifier switch */
175#define daca_info_amp daca_info_deemphasis
176
Takashi Iwai65b29f52005-11-17 15:09:46 +0100177static int daca_get_amp(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100180 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
181 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (! (mix = chip->mixer_data))
183 return -ENODEV;
184 ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
185 return 0;
186}
187
Takashi Iwai65b29f52005-11-17 15:09:46 +0100188static int daca_put_amp(struct snd_kcontrol *kcontrol,
189 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100191 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
192 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int change;
194
195 if (! (mix = chip->mixer_data))
196 return -ENODEV;
197 change = mix->amp_on != ucontrol->value.integer.value[0];
198 if (change) {
199 mix->amp_on = ucontrol->value.integer.value[0];
200 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
201 mix->amp_on ? 0x05 : 0x04);
202 }
203 return change;
204}
205
Takashi Iwai65b29f52005-11-17 15:09:46 +0100206static struct snd_kcontrol_new daca_mixers[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
208 .name = "Deemphasis Switch",
209 .info = daca_info_deemphasis,
210 .get = daca_get_deemphasis,
211 .put = daca_put_deemphasis
212 },
213 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
214 .name = "Master Playback Volume",
215 .info = daca_info_volume,
216 .get = daca_get_volume,
217 .put = daca_put_volume
218 },
219 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
220 .name = "Power Amplifier Switch",
221 .info = daca_info_amp,
222 .get = daca_get_amp,
223 .put = daca_put_amp
224 },
225};
226
227
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700228#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +0100229static void daca_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100231 struct pmac_daca *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
233 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
234 mix->amp_on ? 0x05 : 0x04);
235 daca_set_volume(mix);
236}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700237#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239
Takashi Iwai65b29f52005-11-17 15:09:46 +0100240static void daca_cleanup(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100242 struct pmac_daca *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (! mix)
244 return;
245 snd_pmac_keywest_cleanup(&mix->i2c);
246 kfree(mix);
247 chip->mixer_data = NULL;
248}
249
250/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100251int __init snd_pmac_daca_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 int i, err;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100254 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256#ifdef CONFIG_KMOD
257 if (current->fs->root)
258 request_module("i2c-keywest");
259#endif /* CONFIG_KMOD */
260
261 mix = kmalloc(sizeof(*mix), GFP_KERNEL);
262 if (! mix)
263 return -ENOMEM;
264 memset(mix, 0, sizeof(*mix));
265 chip->mixer_data = mix;
266 chip->mixer_free = daca_cleanup;
267 mix->amp_on = 1; /* default on */
268
269 mix->i2c.addr = DACA_I2C_ADDR;
270 mix->i2c.init_client = daca_init_client;
271 mix->i2c.name = "DACA";
272 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
273 return err;
274
275 /*
276 * build mixers
277 */
278 strcpy(chip->card->mixername, "PowerMac DACA");
279
280 for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
281 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
282 return err;
283 }
284
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700285#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 chip->resume = daca_resume;
287#endif
288
289 return 0;
290}