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