blob: 0c8145792d54e1886ec46d6d8155ed15e1a30b09 [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 Iwaia5ce8892007-07-23 15:42:26 +020094#define daca_info_deemphasis snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Takashi Iwai65b29f52005-11-17 15:09:46 +010096static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
97 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Takashi Iwai65b29f52005-11-17 15:09:46 +010099 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
100 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (! (mix = chip->mixer_data))
102 return -ENODEV;
103 ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
104 return 0;
105}
106
Takashi Iwai65b29f52005-11-17 15:09:46 +0100107static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100110 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
111 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int change;
113
114 if (! (mix = chip->mixer_data))
115 return -ENODEV;
116 change = mix->deemphasis != ucontrol->value.integer.value[0];
117 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100118 mix->deemphasis = !!ucontrol->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 daca_set_volume(mix);
120 }
121 return change;
122}
123
124/* output volume */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100125static int daca_info_volume(struct snd_kcontrol *kcontrol,
126 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
129 uinfo->count = 2;
130 uinfo->value.integer.min = 0;
131 uinfo->value.integer.max = DACA_VOL_MAX;
132 return 0;
133}
134
Takashi Iwai65b29f52005-11-17 15:09:46 +0100135static int daca_get_volume(struct snd_kcontrol *kcontrol,
136 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100138 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
139 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (! (mix = chip->mixer_data))
141 return -ENODEV;
142 ucontrol->value.integer.value[0] = mix->left_vol;
143 ucontrol->value.integer.value[1] = mix->right_vol;
144 return 0;
145}
146
Takashi Iwai65b29f52005-11-17 15:09:46 +0100147static int daca_put_volume(struct snd_kcontrol *kcontrol,
148 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100150 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
151 struct pmac_daca *mix;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100152 unsigned int vol[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int change;
154
155 if (! (mix = chip->mixer_data))
156 return -ENODEV;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100157 vol[0] = ucontrol->value.integer.value[0];
158 vol[1] = ucontrol->value.integer.value[1];
159 if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
160 return -EINVAL;
161 change = mix->left_vol != vol[0] ||
162 mix->right_vol != vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100164 mix->left_vol = vol[0];
165 mix->right_vol = vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 daca_set_volume(mix);
167 }
168 return change;
169}
170
171/* amplifier switch */
172#define daca_info_amp daca_info_deemphasis
173
Takashi Iwai65b29f52005-11-17 15:09:46 +0100174static int daca_get_amp(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100177 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
178 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (! (mix = chip->mixer_data))
180 return -ENODEV;
181 ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
182 return 0;
183}
184
Takashi Iwai65b29f52005-11-17 15:09:46 +0100185static int daca_put_amp(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100188 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
189 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int change;
191
192 if (! (mix = chip->mixer_data))
193 return -ENODEV;
194 change = mix->amp_on != ucontrol->value.integer.value[0];
195 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100196 mix->amp_on = !!ucontrol->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
198 mix->amp_on ? 0x05 : 0x04);
199 }
200 return change;
201}
202
Takashi Iwai65b29f52005-11-17 15:09:46 +0100203static struct snd_kcontrol_new daca_mixers[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
205 .name = "Deemphasis Switch",
206 .info = daca_info_deemphasis,
207 .get = daca_get_deemphasis,
208 .put = daca_put_deemphasis
209 },
210 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
211 .name = "Master Playback Volume",
212 .info = daca_info_volume,
213 .get = daca_get_volume,
214 .put = daca_put_volume
215 },
216 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
217 .name = "Power Amplifier Switch",
218 .info = daca_info_amp,
219 .get = daca_get_amp,
220 .put = daca_put_amp
221 },
222};
223
224
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700225#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +0100226static void daca_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100228 struct pmac_daca *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
230 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
231 mix->amp_on ? 0x05 : 0x04);
232 daca_set_volume(mix);
233}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700234#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236
Takashi Iwai65b29f52005-11-17 15:09:46 +0100237static void daca_cleanup(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100239 struct pmac_daca *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (! mix)
241 return;
242 snd_pmac_keywest_cleanup(&mix->i2c);
243 kfree(mix);
244 chip->mixer_data = NULL;
245}
246
247/* exported */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100248int __init snd_pmac_daca_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int i, err;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100251 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253#ifdef CONFIG_KMOD
254 if (current->fs->root)
Benjamin Herrenschmidt4d6c5882006-04-21 15:04:22 +1000255 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#endif /* CONFIG_KMOD */
257
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200258 mix = kzalloc(sizeof(*mix), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (! mix)
260 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 chip->mixer_data = mix;
262 chip->mixer_free = daca_cleanup;
263 mix->amp_on = 1; /* default on */
264
265 mix->i2c.addr = DACA_I2C_ADDR;
266 mix->i2c.init_client = daca_init_client;
267 mix->i2c.name = "DACA";
268 if ((err = snd_pmac_keywest_init(&mix->i2c)) < 0)
269 return err;
270
271 /*
272 * build mixers
273 */
274 strcpy(chip->card->mixername, "PowerMac DACA");
275
276 for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
277 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip))) < 0)
278 return err;
279 }
280
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700281#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 chip->resume = daca_resume;
283#endif
284
285 return 0;
286}