Thomas Gleixner | d686935 | 2019-06-03 07:45:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Apple Onboard Audio driver for tas codec |
| 4 | * |
| 5 | * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> |
| 6 | * |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 7 | * Open questions: |
| 8 | * - How to distinguish between 3004 and versions? |
| 9 | * |
| 10 | * FIXMEs: |
| 11 | * - This codec driver doesn't honour the 'connected' |
| 12 | * property of the aoa_codec struct, hence if |
| 13 | * it is used in machines where not everything is |
| 14 | * connected it will display wrong mixer elements. |
| 15 | * - Driver assumes that the microphone is always |
| 16 | * monaureal and connected to the right channel of |
| 17 | * the input. This should also be a codec-dependent |
| 18 | * flag, maybe the codec should have 3 different |
| 19 | * bits for the three different possibilities how |
| 20 | * it can be hooked up... |
| 21 | * But as long as I don't see any hardware hooked |
| 22 | * up that way... |
| 23 | * - As Apple notes in their code, the tas3004 seems |
| 24 | * to delay the right channel by one sample. You can |
| 25 | * see this when for example recording stereo in |
| 26 | * audacity, or recording the tas output via cable |
| 27 | * on another machine (use a sinus generator or so). |
| 28 | * I tried programming the BiQuads but couldn't |
| 29 | * make the delay work, maybe someone can read the |
| 30 | * datasheet and fix it. The relevant Apple comment |
| 31 | * is in AppleTAS3004Audio.cpp lines 1637 ff. Note |
| 32 | * that their comment describing how they program |
| 33 | * the filters sucks... |
| 34 | * |
| 35 | * Other things: |
| 36 | * - this should actually register *two* aoa_codec |
| 37 | * structs since it has two inputs. Then it must |
| 38 | * use the prepare callback to forbid running the |
| 39 | * secondary output on a different clock. |
| 40 | * Also, whatever bus knows how to do this must |
| 41 | * provide two soundbus_dev devices and the fabric |
| 42 | * must be able to link them correctly. |
| 43 | * |
| 44 | * I don't even know if Apple ever uses the second |
| 45 | * port on the tas3004 though, I don't think their |
| 46 | * i2s controllers can even do it. OTOH, they all |
| 47 | * derive the clocks from common clocks, so it |
| 48 | * might just be possible. The framework allows the |
| 49 | * codec to refine the transfer_info items in the |
| 50 | * usable callback, so we can simply remove the |
| 51 | * rates the second instance is not using when it |
| 52 | * actually is in use. |
| 53 | * Maybe we'll need to make the sound busses have |
| 54 | * a 'clock group id' value so the codec can |
| 55 | * determine if the two outputs can be driven at |
| 56 | * the same time. But that is likely overkill, up |
| 57 | * to the fabric to not link them up incorrectly, |
| 58 | * and up to the hardware designer to not wire |
| 59 | * them up in some weird unusable way. |
| 60 | */ |
| 61 | #include <stddef.h> |
| 62 | #include <linux/i2c.h> |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 63 | #include <asm/pmac_low_i2c.h> |
| 64 | #include <asm/prom.h> |
| 65 | #include <linux/delay.h> |
| 66 | #include <linux/module.h> |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 67 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 68 | #include <linux/slab.h> |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 69 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 70 | MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); |
| 71 | MODULE_LICENSE("GPL"); |
| 72 | MODULE_DESCRIPTION("tas codec driver for snd-aoa"); |
| 73 | |
Johannes Berg | 888dcb7 | 2008-10-23 15:47:56 +0200 | [diff] [blame] | 74 | #include "tas.h" |
| 75 | #include "tas-gain-table.h" |
| 76 | #include "tas-basstreble.h" |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 77 | #include "../aoa.h" |
| 78 | #include "../soundbus/soundbus.h" |
| 79 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 80 | #define PFX "snd-aoa-codec-tas: " |
| 81 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 82 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 83 | struct tas { |
| 84 | struct aoa_codec codec; |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 85 | struct i2c_client *i2c; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 86 | u32 mute_l:1, mute_r:1 , |
| 87 | controls_created:1 , |
| 88 | drc_enabled:1, |
| 89 | hw_enabled:1; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 90 | u8 cached_volume_l, cached_volume_r; |
| 91 | u8 mixer_l[3], mixer_r[3]; |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 92 | u8 bass, treble; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 93 | u8 acr; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 94 | int drc_range; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 95 | /* protects hardware access against concurrency from |
| 96 | * userspace when hitting controls and during |
| 97 | * codec init/suspend/resume */ |
| 98 | struct mutex mtx; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 101 | static int tas_reset_init(struct tas *tas); |
| 102 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 103 | static struct tas *codec_to_tas(struct aoa_codec *codec) |
| 104 | { |
| 105 | return container_of(codec, struct tas, codec); |
| 106 | } |
| 107 | |
| 108 | static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data) |
| 109 | { |
| 110 | if (len == 1) |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 111 | return i2c_smbus_write_byte_data(tas->i2c, reg, *data); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 112 | else |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 113 | return i2c_smbus_write_i2c_block_data(tas->i2c, reg, len, data); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 116 | static void tas3004_set_drc(struct tas *tas) |
| 117 | { |
| 118 | unsigned char val[6]; |
| 119 | |
| 120 | if (tas->drc_enabled) |
| 121 | val[0] = 0x50; /* 3:1 above threshold */ |
| 122 | else |
| 123 | val[0] = 0x51; /* disabled */ |
| 124 | val[1] = 0x02; /* 1:1 below threshold */ |
| 125 | if (tas->drc_range > 0xef) |
| 126 | val[2] = 0xef; |
| 127 | else if (tas->drc_range < 0) |
| 128 | val[2] = 0x00; |
| 129 | else |
| 130 | val[2] = tas->drc_range; |
| 131 | val[3] = 0xb0; |
| 132 | val[4] = 0x60; |
| 133 | val[5] = 0xa0; |
| 134 | |
| 135 | tas_write_reg(tas, TAS_REG_DRC, 6, val); |
| 136 | } |
| 137 | |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 138 | static void tas_set_treble(struct tas *tas) |
| 139 | { |
| 140 | u8 tmp; |
| 141 | |
| 142 | tmp = tas3004_treble(tas->treble); |
| 143 | tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp); |
| 144 | } |
| 145 | |
| 146 | static void tas_set_bass(struct tas *tas) |
| 147 | { |
| 148 | u8 tmp; |
| 149 | |
| 150 | tmp = tas3004_bass(tas->bass); |
| 151 | tas_write_reg(tas, TAS_REG_BASS, 1, &tmp); |
| 152 | } |
| 153 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 154 | static void tas_set_volume(struct tas *tas) |
| 155 | { |
| 156 | u8 block[6]; |
| 157 | int tmp; |
| 158 | u8 left, right; |
| 159 | |
| 160 | left = tas->cached_volume_l; |
| 161 | right = tas->cached_volume_r; |
| 162 | |
| 163 | if (left > 177) left = 177; |
| 164 | if (right > 177) right = 177; |
| 165 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 166 | if (tas->mute_l) left = 0; |
| 167 | if (tas->mute_r) right = 0; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 168 | |
| 169 | /* analysing the volume and mixer tables shows |
| 170 | * that they are similar enough when we shift |
| 171 | * the mixer table down by 4 bits. The error |
Lucas De Marchi | e9c5499 | 2011-04-26 23:28:26 -0700 | [diff] [blame] | 172 | * is miniscule, in just one item the error |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 173 | * is 1, at a value of 0x07f17b (mixer table |
| 174 | * value is 0x07f17a) */ |
| 175 | tmp = tas_gaintable[left]; |
| 176 | block[0] = tmp>>20; |
| 177 | block[1] = tmp>>12; |
| 178 | block[2] = tmp>>4; |
| 179 | tmp = tas_gaintable[right]; |
| 180 | block[3] = tmp>>20; |
| 181 | block[4] = tmp>>12; |
| 182 | block[5] = tmp>>4; |
| 183 | tas_write_reg(tas, TAS_REG_VOL, 6, block); |
| 184 | } |
| 185 | |
| 186 | static void tas_set_mixer(struct tas *tas) |
| 187 | { |
| 188 | u8 block[9]; |
| 189 | int tmp, i; |
| 190 | u8 val; |
| 191 | |
| 192 | for (i=0;i<3;i++) { |
| 193 | val = tas->mixer_l[i]; |
| 194 | if (val > 177) val = 177; |
| 195 | tmp = tas_gaintable[val]; |
| 196 | block[3*i+0] = tmp>>16; |
| 197 | block[3*i+1] = tmp>>8; |
| 198 | block[3*i+2] = tmp; |
| 199 | } |
| 200 | tas_write_reg(tas, TAS_REG_LMIX, 9, block); |
| 201 | |
| 202 | for (i=0;i<3;i++) { |
| 203 | val = tas->mixer_r[i]; |
| 204 | if (val > 177) val = 177; |
| 205 | tmp = tas_gaintable[val]; |
| 206 | block[3*i+0] = tmp>>16; |
| 207 | block[3*i+1] = tmp>>8; |
| 208 | block[3*i+2] = tmp; |
| 209 | } |
| 210 | tas_write_reg(tas, TAS_REG_RMIX, 9, block); |
| 211 | } |
| 212 | |
| 213 | /* alsa stuff */ |
| 214 | |
| 215 | static int tas_dev_register(struct snd_device *dev) |
| 216 | { |
| 217 | return 0; |
| 218 | } |
| 219 | |
Takashi Iwai | e6f2a61 | 2020-01-03 09:16:27 +0100 | [diff] [blame] | 220 | static const struct snd_device_ops ops = { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 221 | .dev_register = tas_dev_register, |
| 222 | }; |
| 223 | |
| 224 | static int tas_snd_vol_info(struct snd_kcontrol *kcontrol, |
| 225 | struct snd_ctl_elem_info *uinfo) |
| 226 | { |
| 227 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 228 | uinfo->count = 2; |
| 229 | uinfo->value.integer.min = 0; |
| 230 | uinfo->value.integer.max = 177; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int tas_snd_vol_get(struct snd_kcontrol *kcontrol, |
| 235 | struct snd_ctl_elem_value *ucontrol) |
| 236 | { |
| 237 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 238 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 239 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 240 | ucontrol->value.integer.value[0] = tas->cached_volume_l; |
| 241 | ucontrol->value.integer.value[1] = tas->cached_volume_r; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 242 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int tas_snd_vol_put(struct snd_kcontrol *kcontrol, |
| 247 | struct snd_ctl_elem_value *ucontrol) |
| 248 | { |
| 249 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 250 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 251 | if (ucontrol->value.integer.value[0] < 0 || |
| 252 | ucontrol->value.integer.value[0] > 177) |
| 253 | return -EINVAL; |
| 254 | if (ucontrol->value.integer.value[1] < 0 || |
| 255 | ucontrol->value.integer.value[1] > 177) |
| 256 | return -EINVAL; |
| 257 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 258 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 259 | if (tas->cached_volume_l == ucontrol->value.integer.value[0] |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 260 | && tas->cached_volume_r == ucontrol->value.integer.value[1]) { |
| 261 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 262 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 263 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 264 | |
| 265 | tas->cached_volume_l = ucontrol->value.integer.value[0]; |
| 266 | tas->cached_volume_r = ucontrol->value.integer.value[1]; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 267 | if (tas->hw_enabled) |
| 268 | tas_set_volume(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 269 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 270 | return 1; |
| 271 | } |
| 272 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 273 | static const struct snd_kcontrol_new volume_control = { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 274 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 275 | .name = "Master Playback Volume", |
| 276 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 277 | .info = tas_snd_vol_info, |
| 278 | .get = tas_snd_vol_get, |
| 279 | .put = tas_snd_vol_put, |
| 280 | }; |
| 281 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 282 | #define tas_snd_mute_info snd_ctl_boolean_stereo_info |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 283 | |
| 284 | static int tas_snd_mute_get(struct snd_kcontrol *kcontrol, |
| 285 | struct snd_ctl_elem_value *ucontrol) |
| 286 | { |
| 287 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 288 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 289 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 290 | ucontrol->value.integer.value[0] = !tas->mute_l; |
| 291 | ucontrol->value.integer.value[1] = !tas->mute_r; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 292 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int tas_snd_mute_put(struct snd_kcontrol *kcontrol, |
| 297 | struct snd_ctl_elem_value *ucontrol) |
| 298 | { |
| 299 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 300 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 301 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 302 | if (tas->mute_l == !ucontrol->value.integer.value[0] |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 303 | && tas->mute_r == !ucontrol->value.integer.value[1]) { |
| 304 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 305 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 306 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 307 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 308 | tas->mute_l = !ucontrol->value.integer.value[0]; |
| 309 | tas->mute_r = !ucontrol->value.integer.value[1]; |
| 310 | if (tas->hw_enabled) |
| 311 | tas_set_volume(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 312 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 316 | static const struct snd_kcontrol_new mute_control = { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 317 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 318 | .name = "Master Playback Switch", |
| 319 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 320 | .info = tas_snd_mute_info, |
| 321 | .get = tas_snd_mute_get, |
| 322 | .put = tas_snd_mute_put, |
| 323 | }; |
| 324 | |
| 325 | static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol, |
| 326 | struct snd_ctl_elem_info *uinfo) |
| 327 | { |
| 328 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 329 | uinfo->count = 2; |
| 330 | uinfo->value.integer.min = 0; |
| 331 | uinfo->value.integer.max = 177; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol, |
| 336 | struct snd_ctl_elem_value *ucontrol) |
| 337 | { |
| 338 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 339 | int idx = kcontrol->private_value; |
| 340 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 341 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 342 | ucontrol->value.integer.value[0] = tas->mixer_l[idx]; |
| 343 | ucontrol->value.integer.value[1] = tas->mixer_r[idx]; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 344 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol, |
| 350 | struct snd_ctl_elem_value *ucontrol) |
| 351 | { |
| 352 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 353 | int idx = kcontrol->private_value; |
| 354 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 355 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 356 | if (tas->mixer_l[idx] == ucontrol->value.integer.value[0] |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 357 | && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) { |
| 358 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 359 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 360 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 361 | |
| 362 | tas->mixer_l[idx] = ucontrol->value.integer.value[0]; |
| 363 | tas->mixer_r[idx] = ucontrol->value.integer.value[1]; |
| 364 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 365 | if (tas->hw_enabled) |
| 366 | tas_set_mixer(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 367 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | #define MIXER_CONTROL(n,descr,idx) \ |
Takashi Iwai | dde5199 | 2020-01-03 09:16:48 +0100 | [diff] [blame] | 372 | static const struct snd_kcontrol_new n##_control = { \ |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 373 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 374 | .name = descr " Playback Volume", \ |
| 375 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \ |
| 376 | .info = tas_snd_mixer_info, \ |
| 377 | .get = tas_snd_mixer_get, \ |
| 378 | .put = tas_snd_mixer_put, \ |
| 379 | .private_value = idx, \ |
| 380 | } |
| 381 | |
Johannes Berg | 14b4296 | 2006-07-10 04:44:38 -0700 | [diff] [blame] | 382 | MIXER_CONTROL(pcm1, "PCM", 0); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 383 | MIXER_CONTROL(monitor, "Monitor", 2); |
| 384 | |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 385 | static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol, |
| 386 | struct snd_ctl_elem_info *uinfo) |
| 387 | { |
| 388 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 389 | uinfo->count = 1; |
| 390 | uinfo->value.integer.min = 0; |
| 391 | uinfo->value.integer.max = TAS3004_DRC_MAX; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol, |
| 396 | struct snd_ctl_elem_value *ucontrol) |
| 397 | { |
| 398 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 399 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 400 | mutex_lock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 401 | ucontrol->value.integer.value[0] = tas->drc_range; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 402 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol, |
| 407 | struct snd_ctl_elem_value *ucontrol) |
| 408 | { |
| 409 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 410 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 411 | if (ucontrol->value.integer.value[0] < 0 || |
| 412 | ucontrol->value.integer.value[0] > TAS3004_DRC_MAX) |
| 413 | return -EINVAL; |
| 414 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 415 | mutex_lock(&tas->mtx); |
| 416 | if (tas->drc_range == ucontrol->value.integer.value[0]) { |
| 417 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 418 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 419 | } |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 420 | |
| 421 | tas->drc_range = ucontrol->value.integer.value[0]; |
| 422 | if (tas->hw_enabled) |
| 423 | tas3004_set_drc(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 424 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 425 | return 1; |
| 426 | } |
| 427 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 428 | static const struct snd_kcontrol_new drc_range_control = { |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 429 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 430 | .name = "DRC Range", |
| 431 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 432 | .info = tas_snd_drc_range_info, |
| 433 | .get = tas_snd_drc_range_get, |
| 434 | .put = tas_snd_drc_range_put, |
| 435 | }; |
| 436 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 437 | #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 438 | |
| 439 | static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol, |
| 440 | struct snd_ctl_elem_value *ucontrol) |
| 441 | { |
| 442 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 443 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 444 | mutex_lock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 445 | ucontrol->value.integer.value[0] = tas->drc_enabled; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 446 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol, |
| 451 | struct snd_ctl_elem_value *ucontrol) |
| 452 | { |
| 453 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 454 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 455 | mutex_lock(&tas->mtx); |
| 456 | if (tas->drc_enabled == ucontrol->value.integer.value[0]) { |
| 457 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 458 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 459 | } |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 460 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 461 | tas->drc_enabled = !!ucontrol->value.integer.value[0]; |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 462 | if (tas->hw_enabled) |
| 463 | tas3004_set_drc(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 464 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 468 | static const struct snd_kcontrol_new drc_switch_control = { |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 469 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 470 | .name = "DRC Range Switch", |
| 471 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 472 | .info = tas_snd_drc_switch_info, |
| 473 | .get = tas_snd_drc_switch_get, |
| 474 | .put = tas_snd_drc_switch_put, |
| 475 | }; |
| 476 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 477 | static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol, |
| 478 | struct snd_ctl_elem_info *uinfo) |
| 479 | { |
Takashi Iwai | 04eeb60 | 2014-10-20 18:10:39 +0200 | [diff] [blame] | 480 | static const char * const texts[] = { "Line-In", "Microphone" }; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 481 | |
Takashi Iwai | 04eeb60 | 2014-10-20 18:10:39 +0200 | [diff] [blame] | 482 | return snd_ctl_enum_info(uinfo, 1, 2, texts); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol, |
| 486 | struct snd_ctl_elem_value *ucontrol) |
| 487 | { |
| 488 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 489 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 490 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 491 | ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 492 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol, |
| 497 | struct snd_ctl_elem_value *ucontrol) |
| 498 | { |
| 499 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 500 | int oldacr; |
| 501 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 502 | if (ucontrol->value.enumerated.item[0] > 1) |
| 503 | return -EINVAL; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 504 | mutex_lock(&tas->mtx); |
| 505 | oldacr = tas->acr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 506 | |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 507 | /* |
| 508 | * Despite what the data sheet says in one place, the |
| 509 | * TAS_ACR_B_MONAUREAL bit forces mono output even when |
| 510 | * input A (line in) is selected. |
| 511 | */ |
| 512 | tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 513 | if (ucontrol->value.enumerated.item[0]) |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 514 | tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL | |
| 515 | TAS_ACR_B_MON_SEL_RIGHT; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 516 | if (oldacr == tas->acr) { |
| 517 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 518 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 519 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 520 | if (tas->hw_enabled) |
| 521 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 522 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 523 | return 1; |
| 524 | } |
| 525 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 526 | static const struct snd_kcontrol_new capture_source_control = { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 527 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 528 | /* If we name this 'Input Source', it properly shows up in |
| 529 | * alsamixer as a selection, * but it's shown under the |
| 530 | * 'Playback' category. |
| 531 | * If I name it 'Capture Source', it shows up in strange |
| 532 | * ways (two bools of which one can be selected at a |
| 533 | * time) but at least it's shown in the 'Capture' |
| 534 | * category. |
| 535 | * I was told that this was due to backward compatibility, |
| 536 | * but I don't understand then why the mangling is *not* |
| 537 | * done when I name it "Input Source"..... |
| 538 | */ |
| 539 | .name = "Capture Source", |
| 540 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 541 | .info = tas_snd_capture_source_info, |
| 542 | .get = tas_snd_capture_source_get, |
| 543 | .put = tas_snd_capture_source_put, |
| 544 | }; |
| 545 | |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 546 | static int tas_snd_treble_info(struct snd_kcontrol *kcontrol, |
| 547 | struct snd_ctl_elem_info *uinfo) |
| 548 | { |
| 549 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 550 | uinfo->count = 1; |
| 551 | uinfo->value.integer.min = TAS3004_TREBLE_MIN; |
| 552 | uinfo->value.integer.max = TAS3004_TREBLE_MAX; |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int tas_snd_treble_get(struct snd_kcontrol *kcontrol, |
| 557 | struct snd_ctl_elem_value *ucontrol) |
| 558 | { |
| 559 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 560 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 561 | mutex_lock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 562 | ucontrol->value.integer.value[0] = tas->treble; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 563 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int tas_snd_treble_put(struct snd_kcontrol *kcontrol, |
| 568 | struct snd_ctl_elem_value *ucontrol) |
| 569 | { |
| 570 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 571 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 572 | if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN || |
| 573 | ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX) |
| 574 | return -EINVAL; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 575 | mutex_lock(&tas->mtx); |
| 576 | if (tas->treble == ucontrol->value.integer.value[0]) { |
| 577 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 578 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 579 | } |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 580 | |
| 581 | tas->treble = ucontrol->value.integer.value[0]; |
| 582 | if (tas->hw_enabled) |
| 583 | tas_set_treble(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 584 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 585 | return 1; |
| 586 | } |
| 587 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 588 | static const struct snd_kcontrol_new treble_control = { |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 589 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 590 | .name = "Treble", |
| 591 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 592 | .info = tas_snd_treble_info, |
| 593 | .get = tas_snd_treble_get, |
| 594 | .put = tas_snd_treble_put, |
| 595 | }; |
| 596 | |
| 597 | static int tas_snd_bass_info(struct snd_kcontrol *kcontrol, |
| 598 | struct snd_ctl_elem_info *uinfo) |
| 599 | { |
| 600 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 601 | uinfo->count = 1; |
| 602 | uinfo->value.integer.min = TAS3004_BASS_MIN; |
| 603 | uinfo->value.integer.max = TAS3004_BASS_MAX; |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int tas_snd_bass_get(struct snd_kcontrol *kcontrol, |
| 608 | struct snd_ctl_elem_value *ucontrol) |
| 609 | { |
| 610 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 611 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 612 | mutex_lock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 613 | ucontrol->value.integer.value[0] = tas->bass; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 614 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int tas_snd_bass_put(struct snd_kcontrol *kcontrol, |
| 619 | struct snd_ctl_elem_value *ucontrol) |
| 620 | { |
| 621 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 622 | |
Takashi Iwai | 498ade1 | 2007-11-15 16:16:32 +0100 | [diff] [blame] | 623 | if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN || |
| 624 | ucontrol->value.integer.value[0] > TAS3004_BASS_MAX) |
| 625 | return -EINVAL; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 626 | mutex_lock(&tas->mtx); |
| 627 | if (tas->bass == ucontrol->value.integer.value[0]) { |
| 628 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 629 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 630 | } |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 631 | |
| 632 | tas->bass = ucontrol->value.integer.value[0]; |
| 633 | if (tas->hw_enabled) |
| 634 | tas_set_bass(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 635 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 636 | return 1; |
| 637 | } |
| 638 | |
Bhumika Goyal | 905e46a | 2017-05-27 20:16:15 +0530 | [diff] [blame] | 639 | static const struct snd_kcontrol_new bass_control = { |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 640 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 641 | .name = "Bass", |
| 642 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 643 | .info = tas_snd_bass_info, |
| 644 | .get = tas_snd_bass_get, |
| 645 | .put = tas_snd_bass_put, |
| 646 | }; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 647 | |
| 648 | static struct transfer_info tas_transfers[] = { |
| 649 | { |
| 650 | /* input */ |
Roel Kluin | c7d03bc | 2008-08-20 10:31:38 +0200 | [diff] [blame] | 651 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 652 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
| 653 | .transfer_in = 1, |
| 654 | }, |
| 655 | { |
| 656 | /* output */ |
Roel Kluin | c7d03bc | 2008-08-20 10:31:38 +0200 | [diff] [blame] | 657 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 658 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
| 659 | .transfer_in = 0, |
| 660 | }, |
| 661 | {} |
| 662 | }; |
| 663 | |
| 664 | static int tas_usable(struct codec_info_item *cii, |
| 665 | struct transfer_info *ti, |
| 666 | struct transfer_info *out) |
| 667 | { |
| 668 | return 1; |
| 669 | } |
| 670 | |
| 671 | static int tas_reset_init(struct tas *tas) |
| 672 | { |
| 673 | u8 tmp; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 674 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 675 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); |
| 676 | msleep(5); |
| 677 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
| 678 | msleep(5); |
| 679 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1); |
| 680 | msleep(20); |
| 681 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
| 682 | msleep(10); |
| 683 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 684 | |
| 685 | tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT; |
| 686 | if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 687 | goto outerr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 688 | |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 689 | tas->acr |= TAS_ACR_ANALOG_PDOWN; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 690 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 691 | goto outerr; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 692 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 693 | tmp = 0; |
| 694 | if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 695 | goto outerr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 696 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 697 | tas3004_set_drc(tas); |
| 698 | |
| 699 | /* Set treble & bass to 0dB */ |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 700 | tas->treble = TAS3004_TREBLE_ZERO; |
| 701 | tas->bass = TAS3004_BASS_ZERO; |
| 702 | tas_set_treble(tas); |
| 703 | tas_set_bass(tas); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 704 | |
| 705 | tas->acr &= ~TAS_ACR_ANALOG_PDOWN; |
| 706 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 707 | goto outerr; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 708 | |
| 709 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 710 | outerr: |
| 711 | return -ENODEV; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock) |
| 715 | { |
| 716 | struct tas *tas = cii->codec_data; |
| 717 | |
| 718 | switch(clock) { |
| 719 | case CLOCK_SWITCH_PREPARE_SLAVE: |
| 720 | /* Clocks are going away, mute mute mute */ |
| 721 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); |
| 722 | tas->hw_enabled = 0; |
| 723 | break; |
| 724 | case CLOCK_SWITCH_SLAVE: |
| 725 | /* Clocks are back, re-init the codec */ |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 726 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 727 | tas_reset_init(tas); |
| 728 | tas_set_volume(tas); |
| 729 | tas_set_mixer(tas); |
| 730 | tas->hw_enabled = 1; |
| 731 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 732 | mutex_unlock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 733 | break; |
| 734 | default: |
| 735 | /* doesn't happen as of now */ |
| 736 | return -EINVAL; |
| 737 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Stephen Rothwell | 1f28960 | 2007-07-23 12:10:07 +0200 | [diff] [blame] | 741 | #ifdef CONFIG_PM |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 742 | /* we are controlled via i2c and assume that is always up |
| 743 | * If that wasn't the case, we'd have to suspend once |
| 744 | * our i2c device is suspended, and then take note of that! */ |
| 745 | static int tas_suspend(struct tas *tas) |
| 746 | { |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 747 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 748 | tas->hw_enabled = 0; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 749 | tas->acr |= TAS_ACR_ANALOG_PDOWN; |
| 750 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 751 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int tas_resume(struct tas *tas) |
| 756 | { |
| 757 | /* reset codec */ |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 758 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 759 | tas_reset_init(tas); |
| 760 | tas_set_volume(tas); |
| 761 | tas_set_mixer(tas); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 762 | tas->hw_enabled = 1; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 763 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 767 | static int _tas_suspend(struct codec_info_item *cii, pm_message_t state) |
| 768 | { |
| 769 | return tas_suspend(cii->codec_data); |
| 770 | } |
| 771 | |
| 772 | static int _tas_resume(struct codec_info_item *cii) |
| 773 | { |
| 774 | return tas_resume(cii->codec_data); |
| 775 | } |
Stephen Rothwell | 1f28960 | 2007-07-23 12:10:07 +0200 | [diff] [blame] | 776 | #else /* CONFIG_PM */ |
| 777 | #define _tas_suspend NULL |
| 778 | #define _tas_resume NULL |
| 779 | #endif /* CONFIG_PM */ |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 780 | |
| 781 | static struct codec_info tas_codec_info = { |
| 782 | .transfers = tas_transfers, |
| 783 | /* in theory, we can drive it at 512 too... |
| 784 | * but so far the framework doesn't allow |
| 785 | * for that and I don't see much point in it. */ |
| 786 | .sysclock_factor = 256, |
| 787 | /* same here, could be 32 for just one 16 bit format */ |
| 788 | .bus_factor = 64, |
| 789 | .owner = THIS_MODULE, |
| 790 | .usable = tas_usable, |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 791 | .switch_clock = tas_switch_clock, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 792 | .suspend = _tas_suspend, |
| 793 | .resume = _tas_resume, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | static int tas_init_codec(struct aoa_codec *codec) |
| 797 | { |
| 798 | struct tas *tas = codec_to_tas(codec); |
| 799 | int err; |
| 800 | |
| 801 | if (!tas->codec.gpio || !tas->codec.gpio->methods) { |
| 802 | printk(KERN_ERR PFX "gpios not assigned!!\n"); |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 806 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 807 | if (tas_reset_init(tas)) { |
| 808 | printk(KERN_ERR PFX "tas failed to initialise\n"); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 809 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 810 | return -ENXIO; |
| 811 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 812 | tas->hw_enabled = 1; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 813 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 814 | |
| 815 | if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev, |
| 816 | aoa_get_card(), |
| 817 | &tas_codec_info, tas)) { |
| 818 | printk(KERN_ERR PFX "error attaching tas to soundbus\n"); |
| 819 | return -ENODEV; |
| 820 | } |
| 821 | |
Takashi Iwai | d915178 | 2014-02-04 11:16:31 +0100 | [diff] [blame] | 822 | if (aoa_snd_device_new(SNDRV_DEV_CODEC, tas, &ops)) { |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 823 | printk(KERN_ERR PFX "failed to create tas snd device!\n"); |
| 824 | return -ENODEV; |
| 825 | } |
| 826 | err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas)); |
| 827 | if (err) |
| 828 | goto error; |
| 829 | |
| 830 | err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas)); |
| 831 | if (err) |
| 832 | goto error; |
| 833 | |
| 834 | err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas)); |
| 835 | if (err) |
| 836 | goto error; |
| 837 | |
| 838 | err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas)); |
| 839 | if (err) |
| 840 | goto error; |
| 841 | |
| 842 | err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas)); |
| 843 | if (err) |
| 844 | goto error; |
| 845 | |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 846 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas)); |
| 847 | if (err) |
| 848 | goto error; |
| 849 | |
| 850 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas)); |
| 851 | if (err) |
| 852 | goto error; |
| 853 | |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 854 | err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas)); |
| 855 | if (err) |
| 856 | goto error; |
| 857 | |
| 858 | err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas)); |
| 859 | if (err) |
| 860 | goto error; |
| 861 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 862 | return 0; |
| 863 | error: |
| 864 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); |
| 865 | snd_device_free(aoa_get_card(), tas); |
| 866 | return err; |
| 867 | } |
| 868 | |
| 869 | static void tas_exit_codec(struct aoa_codec *codec) |
| 870 | { |
| 871 | struct tas *tas = codec_to_tas(codec); |
| 872 | |
| 873 | if (!tas->codec.soundbus_dev) |
| 874 | return; |
| 875 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); |
| 876 | } |
Johannes Berg | 888dcb7 | 2008-10-23 15:47:56 +0200 | [diff] [blame] | 877 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 878 | |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 879 | static int tas_i2c_probe(struct i2c_client *client, |
| 880 | const struct i2c_device_id *id) |
| 881 | { |
Andreas Schwab | 26b0d14 | 2012-06-09 15:58:56 +0200 | [diff] [blame] | 882 | struct device_node *node = client->dev.of_node; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 883 | struct tas *tas; |
| 884 | |
| 885 | tas = kzalloc(sizeof(struct tas), GFP_KERNEL); |
| 886 | |
| 887 | if (!tas) |
| 888 | return -ENOMEM; |
| 889 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 890 | mutex_init(&tas->mtx); |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 891 | tas->i2c = client; |
| 892 | i2c_set_clientdata(client, tas); |
| 893 | |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 894 | /* seems that half is a saner default */ |
| 895 | tas->drc_range = TAS3004_DRC_MAX / 2; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 896 | |
Jean Delvare | 023ff3e | 2007-03-27 11:50:19 +0200 | [diff] [blame] | 897 | strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 898 | tas->codec.owner = THIS_MODULE; |
| 899 | tas->codec.init = tas_init_codec; |
| 900 | tas->codec.exit = tas_exit_codec; |
| 901 | tas->codec.node = of_node_get(node); |
| 902 | |
| 903 | if (aoa_codec_register(&tas->codec)) { |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 904 | goto fail; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 905 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 906 | printk(KERN_DEBUG |
Rob Herring | 0be04a6 | 2017-08-07 18:29:19 -0500 | [diff] [blame] | 907 | "snd-aoa-codec-tas: tas found, addr 0x%02x on %pOF\n", |
| 908 | (unsigned int)client->addr, node); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 909 | return 0; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 910 | fail: |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 911 | mutex_destroy(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 912 | kfree(tas); |
| 913 | return -EINVAL; |
| 914 | } |
| 915 | |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 916 | static int tas_i2c_remove(struct i2c_client *client) |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 917 | { |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 918 | struct tas *tas = i2c_get_clientdata(client); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 919 | u8 tmp = TAS_ACR_ANALOG_PDOWN; |
| 920 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 921 | aoa_codec_unregister(&tas->codec); |
| 922 | of_node_put(tas->codec.node); |
| 923 | |
| 924 | /* power down codec chip */ |
| 925 | tas_write_reg(tas, TAS_REG_ACR, 1, &tmp); |
| 926 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 927 | mutex_destroy(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 928 | kfree(tas); |
| 929 | return 0; |
| 930 | } |
| 931 | |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 932 | static const struct i2c_device_id tas_i2c_id[] = { |
Andreas Schwab | 26b0d14 | 2012-06-09 15:58:56 +0200 | [diff] [blame] | 933 | { "MAC,tas3004", 0 }, |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 934 | { } |
| 935 | }; |
Andreas Schwab | 26b0d14 | 2012-06-09 15:58:56 +0200 | [diff] [blame] | 936 | MODULE_DEVICE_TABLE(i2c,tas_i2c_id); |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 937 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 938 | static struct i2c_driver tas_driver = { |
| 939 | .driver = { |
| 940 | .name = "aoa_codec_tas", |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 941 | }, |
Jean Delvare | cfbf1ee | 2009-04-20 22:54:25 +0200 | [diff] [blame] | 942 | .probe = tas_i2c_probe, |
| 943 | .remove = tas_i2c_remove, |
| 944 | .id_table = tas_i2c_id, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 945 | }; |
| 946 | |
Axel Lin | 98654d3 | 2012-01-27 15:23:51 +0800 | [diff] [blame] | 947 | module_i2c_driver(tas_driver); |