blob: 4da9278dd58af1d029bfe9c8bfdd5ca506404792 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * PMac DACA lowlevel functions
4 *
5 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kmod.h>
12#include <linux/slab.h>
13#include <sound/core.h>
14#include "pmac.h"
15
16/* i2c address */
17#define DACA_I2C_ADDR 0x4d
18
19/* registers */
20#define DACA_REG_SR 0x01
21#define DACA_REG_AVOL 0x02
22#define DACA_REG_GCFG 0x03
23
24/* maximum volume value */
25#define DACA_VOL_MAX 0x38
26
27
Takashi Iwai65b29f52005-11-17 15:09:46 +010028struct pmac_daca {
29 struct pmac_keywest i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int left_vol, right_vol;
31 unsigned int deemphasis : 1;
32 unsigned int amp_on : 1;
Takashi Iwai65b29f52005-11-17 15:09:46 +010033};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35
36/*
37 * initialize / detect DACA
38 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010039static int daca_init_client(struct pmac_keywest *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 unsigned short wdata = 0x00;
42 /* SR: no swap, 1bit delay, 32-48kHz */
43 /* GCFG: power amp inverted, DAC on */
44 if (i2c_smbus_write_byte_data(i2c->client, DACA_REG_SR, 0x08) < 0 ||
45 i2c_smbus_write_byte_data(i2c->client, DACA_REG_GCFG, 0x05) < 0)
46 return -EINVAL;
47 return i2c_smbus_write_block_data(i2c->client, DACA_REG_AVOL,
48 2, (unsigned char*)&wdata);
49}
50
51/*
52 * update volume
53 */
Takashi Iwai65b29f52005-11-17 15:09:46 +010054static int daca_set_volume(struct pmac_daca *mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 unsigned char data[2];
57
58 if (! mix->i2c.client)
59 return -ENODEV;
60
61 if (mix->left_vol > DACA_VOL_MAX)
62 data[0] = DACA_VOL_MAX;
63 else
64 data[0] = mix->left_vol;
65 if (mix->right_vol > DACA_VOL_MAX)
66 data[1] = DACA_VOL_MAX;
67 else
68 data[1] = mix->right_vol;
69 data[1] |= mix->deemphasis ? 0x40 : 0;
70 if (i2c_smbus_write_block_data(mix->i2c.client, DACA_REG_AVOL,
71 2, data) < 0) {
Takashi Iwai6da67112009-02-05 16:02:46 +010072 snd_printk(KERN_ERR "failed to set volume \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EINVAL;
74 }
75 return 0;
76}
77
78
79/* deemphasis switch */
Takashi Iwaia5ce8892007-07-23 15:42:26 +020080#define daca_info_deemphasis snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Takashi Iwai65b29f52005-11-17 15:09:46 +010082static int daca_get_deemphasis(struct snd_kcontrol *kcontrol,
83 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Takashi Iwai65b29f52005-11-17 15:09:46 +010085 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
86 struct pmac_daca *mix;
Takashi Iwaie73ad382021-06-08 16:05:38 +020087 mix = chip->mixer_data;
88 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return -ENODEV;
90 ucontrol->value.integer.value[0] = mix->deemphasis ? 1 : 0;
91 return 0;
92}
93
Takashi Iwai65b29f52005-11-17 15:09:46 +010094static int daca_put_deemphasis(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Takashi Iwai65b29f52005-11-17 15:09:46 +010097 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
98 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int change;
100
Takashi Iwaie73ad382021-06-08 16:05:38 +0200101 mix = chip->mixer_data;
102 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -ENODEV;
104 change = mix->deemphasis != ucontrol->value.integer.value[0];
105 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100106 mix->deemphasis = !!ucontrol->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 daca_set_volume(mix);
108 }
109 return change;
110}
111
112/* output volume */
Takashi Iwai65b29f52005-11-17 15:09:46 +0100113static int daca_info_volume(struct snd_kcontrol *kcontrol,
114 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
117 uinfo->count = 2;
118 uinfo->value.integer.min = 0;
119 uinfo->value.integer.max = DACA_VOL_MAX;
120 return 0;
121}
122
Takashi Iwai65b29f52005-11-17 15:09:46 +0100123static int daca_get_volume(struct snd_kcontrol *kcontrol,
124 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100126 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
127 struct pmac_daca *mix;
Takashi Iwaie73ad382021-06-08 16:05:38 +0200128 mix = chip->mixer_data;
129 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENODEV;
131 ucontrol->value.integer.value[0] = mix->left_vol;
132 ucontrol->value.integer.value[1] = mix->right_vol;
133 return 0;
134}
135
Takashi Iwai65b29f52005-11-17 15:09:46 +0100136static int daca_put_volume(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100139 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
140 struct pmac_daca *mix;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100141 unsigned int vol[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int change;
143
Takashi Iwaie73ad382021-06-08 16:05:38 +0200144 mix = chip->mixer_data;
145 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -ENODEV;
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100147 vol[0] = ucontrol->value.integer.value[0];
148 vol[1] = ucontrol->value.integer.value[1];
149 if (vol[0] > DACA_VOL_MAX || vol[1] > DACA_VOL_MAX)
150 return -EINVAL;
151 change = mix->left_vol != vol[0] ||
152 mix->right_vol != vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100154 mix->left_vol = vol[0];
155 mix->right_vol = vol[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 daca_set_volume(mix);
157 }
158 return change;
159}
160
161/* amplifier switch */
162#define daca_info_amp daca_info_deemphasis
163
Takashi Iwai65b29f52005-11-17 15:09:46 +0100164static int daca_get_amp(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100167 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
168 struct pmac_daca *mix;
Takashi Iwaie73ad382021-06-08 16:05:38 +0200169 mix = chip->mixer_data;
170 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -ENODEV;
172 ucontrol->value.integer.value[0] = mix->amp_on ? 1 : 0;
173 return 0;
174}
175
Takashi Iwai65b29f52005-11-17 15:09:46 +0100176static int daca_put_amp(struct snd_kcontrol *kcontrol,
177 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100179 struct snd_pmac *chip = snd_kcontrol_chip(kcontrol);
180 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int change;
182
Takashi Iwaie73ad382021-06-08 16:05:38 +0200183 mix = chip->mixer_data;
184 if (!mix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -ENODEV;
186 change = mix->amp_on != ucontrol->value.integer.value[0];
187 if (change) {
Takashi Iwaid4079ac2007-11-15 16:14:12 +0100188 mix->amp_on = !!ucontrol->value.integer.value[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
190 mix->amp_on ? 0x05 : 0x04);
191 }
192 return change;
193}
194
Takashi Iwaic031b0c2020-01-03 09:16:54 +0100195static const struct snd_kcontrol_new daca_mixers[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
197 .name = "Deemphasis Switch",
198 .info = daca_info_deemphasis,
199 .get = daca_get_deemphasis,
200 .put = daca_put_deemphasis
201 },
202 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
203 .name = "Master Playback Volume",
204 .info = daca_info_volume,
205 .get = daca_get_volume,
206 .put = daca_put_volume
207 },
208 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
209 .name = "Power Amplifier Switch",
210 .info = daca_info_amp,
211 .get = daca_get_amp,
212 .put = daca_put_amp
213 },
214};
215
216
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700217#ifdef CONFIG_PM
Takashi Iwai65b29f52005-11-17 15:09:46 +0100218static void daca_resume(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai65b29f52005-11-17 15:09:46 +0100220 struct pmac_daca *mix = chip->mixer_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_SR, 0x08);
222 i2c_smbus_write_byte_data(mix->i2c.client, DACA_REG_GCFG,
223 mix->amp_on ? 0x05 : 0x04);
224 daca_set_volume(mix);
225}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700226#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228
Takashi Iwai65b29f52005-11-17 15:09:46 +0100229static void daca_cleanup(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 if (! mix)
233 return;
234 snd_pmac_keywest_cleanup(&mix->i2c);
235 kfree(mix);
236 chip->mixer_data = NULL;
237}
238
239/* exported */
Bill Pemberton15afafc2012-12-06 12:35:23 -0500240int snd_pmac_daca_init(struct snd_pmac *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int i, err;
Takashi Iwai65b29f52005-11-17 15:09:46 +0100243 struct pmac_daca *mix;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jan Blunck0d63e4f2008-02-14 19:34:28 -0800245 request_module("i2c-powermac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200247 mix = kzalloc(sizeof(*mix), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (! mix)
249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 chip->mixer_data = mix;
251 chip->mixer_free = daca_cleanup;
252 mix->amp_on = 1; /* default on */
253
254 mix->i2c.addr = DACA_I2C_ADDR;
255 mix->i2c.init_client = daca_init_client;
256 mix->i2c.name = "DACA";
Takashi Iwaie73ad382021-06-08 16:05:38 +0200257 err = snd_pmac_keywest_init(&mix->i2c);
258 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return err;
260
261 /*
262 * build mixers
263 */
264 strcpy(chip->card->mixername, "PowerMac DACA");
265
266 for (i = 0; i < ARRAY_SIZE(daca_mixers); i++) {
Takashi Iwaie73ad382021-06-08 16:05:38 +0200267 err = snd_ctl_add(chip->card, snd_ctl_new1(&daca_mixers[i], chip));
268 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return err;
270 }
271
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700272#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 chip->resume = daca_resume;
274#endif
275
276 return 0;
277}