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> |
| 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 | |
| 74 | #include "snd-aoa-codec-tas.h" |
| 75 | #include "snd-aoa-codec-tas-gain-table.h" |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 76 | #include "snd-aoa-codec-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; |
| 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) |
| 111 | return i2c_smbus_write_byte_data(&tas->i2c, reg, *data); |
| 112 | else |
| 113 | return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data); |
| 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 |
| 172 | * is miniscule, in just one item the error |
| 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 | |
| 220 | static struct snd_device_ops ops = { |
| 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 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 251 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 252 | if (tas->cached_volume_l == ucontrol->value.integer.value[0] |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 253 | && tas->cached_volume_r == ucontrol->value.integer.value[1]) { |
| 254 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 255 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 256 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 257 | |
| 258 | tas->cached_volume_l = ucontrol->value.integer.value[0]; |
| 259 | tas->cached_volume_r = ucontrol->value.integer.value[1]; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 260 | if (tas->hw_enabled) |
| 261 | tas_set_volume(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 262 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | static struct snd_kcontrol_new volume_control = { |
| 267 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 268 | .name = "Master Playback Volume", |
| 269 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 270 | .info = tas_snd_vol_info, |
| 271 | .get = tas_snd_vol_get, |
| 272 | .put = tas_snd_vol_put, |
| 273 | }; |
| 274 | |
| 275 | static int tas_snd_mute_info(struct snd_kcontrol *kcontrol, |
| 276 | struct snd_ctl_elem_info *uinfo) |
| 277 | { |
| 278 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 279 | uinfo->count = 2; |
| 280 | uinfo->value.integer.min = 0; |
| 281 | uinfo->value.integer.max = 1; |
| 282 | return 0; |
| 283 | } |
| 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 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 412 | mutex_lock(&tas->mtx); |
| 413 | if (tas->drc_range == ucontrol->value.integer.value[0]) { |
| 414 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 415 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 416 | } |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 417 | |
| 418 | tas->drc_range = ucontrol->value.integer.value[0]; |
| 419 | if (tas->hw_enabled) |
| 420 | tas3004_set_drc(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 421 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | static struct snd_kcontrol_new drc_range_control = { |
| 426 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 427 | .name = "DRC Range", |
| 428 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 429 | .info = tas_snd_drc_range_info, |
| 430 | .get = tas_snd_drc_range_get, |
| 431 | .put = tas_snd_drc_range_put, |
| 432 | }; |
| 433 | |
| 434 | static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol, |
| 435 | struct snd_ctl_elem_info *uinfo) |
| 436 | { |
| 437 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 438 | uinfo->count = 1; |
| 439 | uinfo->value.integer.min = 0; |
| 440 | uinfo->value.integer.max = 1; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol, |
| 445 | struct snd_ctl_elem_value *ucontrol) |
| 446 | { |
| 447 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 448 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 449 | mutex_lock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 450 | ucontrol->value.integer.value[0] = tas->drc_enabled; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 451 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol, |
| 456 | struct snd_ctl_elem_value *ucontrol) |
| 457 | { |
| 458 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 459 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 460 | mutex_lock(&tas->mtx); |
| 461 | if (tas->drc_enabled == ucontrol->value.integer.value[0]) { |
| 462 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 463 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 464 | } |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 465 | |
| 466 | tas->drc_enabled = ucontrol->value.integer.value[0]; |
| 467 | if (tas->hw_enabled) |
| 468 | tas3004_set_drc(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 469 | mutex_unlock(&tas->mtx); |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 470 | return 1; |
| 471 | } |
| 472 | |
| 473 | static struct snd_kcontrol_new drc_switch_control = { |
| 474 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 475 | .name = "DRC Range Switch", |
| 476 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 477 | .info = tas_snd_drc_switch_info, |
| 478 | .get = tas_snd_drc_switch_get, |
| 479 | .put = tas_snd_drc_switch_put, |
| 480 | }; |
| 481 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 482 | static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol, |
| 483 | struct snd_ctl_elem_info *uinfo) |
| 484 | { |
| 485 | static char *texts[] = { "Line-In", "Microphone" }; |
| 486 | |
| 487 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 488 | uinfo->count = 1; |
| 489 | uinfo->value.enumerated.items = 2; |
| 490 | if (uinfo->value.enumerated.item > 1) |
| 491 | uinfo->value.enumerated.item = 1; |
| 492 | strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol, |
| 497 | struct snd_ctl_elem_value *ucontrol) |
| 498 | { |
| 499 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 500 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 501 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 502 | ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 503 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol, |
| 508 | struct snd_ctl_elem_value *ucontrol) |
| 509 | { |
| 510 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 511 | int oldacr; |
| 512 | |
| 513 | mutex_lock(&tas->mtx); |
| 514 | oldacr = tas->acr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 515 | |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 516 | /* |
| 517 | * Despite what the data sheet says in one place, the |
| 518 | * TAS_ACR_B_MONAUREAL bit forces mono output even when |
| 519 | * input A (line in) is selected. |
| 520 | */ |
| 521 | tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 522 | if (ucontrol->value.enumerated.item[0]) |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 523 | tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL | |
| 524 | TAS_ACR_B_MON_SEL_RIGHT; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 525 | if (oldacr == tas->acr) { |
| 526 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 527 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 528 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 529 | if (tas->hw_enabled) |
| 530 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 531 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | static struct snd_kcontrol_new capture_source_control = { |
| 536 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 537 | /* If we name this 'Input Source', it properly shows up in |
| 538 | * alsamixer as a selection, * but it's shown under the |
| 539 | * 'Playback' category. |
| 540 | * If I name it 'Capture Source', it shows up in strange |
| 541 | * ways (two bools of which one can be selected at a |
| 542 | * time) but at least it's shown in the 'Capture' |
| 543 | * category. |
| 544 | * I was told that this was due to backward compatibility, |
| 545 | * but I don't understand then why the mangling is *not* |
| 546 | * done when I name it "Input Source"..... |
| 547 | */ |
| 548 | .name = "Capture Source", |
| 549 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 550 | .info = tas_snd_capture_source_info, |
| 551 | .get = tas_snd_capture_source_get, |
| 552 | .put = tas_snd_capture_source_put, |
| 553 | }; |
| 554 | |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 555 | static int tas_snd_treble_info(struct snd_kcontrol *kcontrol, |
| 556 | struct snd_ctl_elem_info *uinfo) |
| 557 | { |
| 558 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 559 | uinfo->count = 1; |
| 560 | uinfo->value.integer.min = TAS3004_TREBLE_MIN; |
| 561 | uinfo->value.integer.max = TAS3004_TREBLE_MAX; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int tas_snd_treble_get(struct snd_kcontrol *kcontrol, |
| 566 | struct snd_ctl_elem_value *ucontrol) |
| 567 | { |
| 568 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 569 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 570 | mutex_lock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 571 | ucontrol->value.integer.value[0] = tas->treble; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 572 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int tas_snd_treble_put(struct snd_kcontrol *kcontrol, |
| 577 | struct snd_ctl_elem_value *ucontrol) |
| 578 | { |
| 579 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 580 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 581 | mutex_lock(&tas->mtx); |
| 582 | if (tas->treble == ucontrol->value.integer.value[0]) { |
| 583 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 584 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 585 | } |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 586 | |
| 587 | tas->treble = ucontrol->value.integer.value[0]; |
| 588 | if (tas->hw_enabled) |
| 589 | tas_set_treble(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 590 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | static struct snd_kcontrol_new treble_control = { |
| 595 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 596 | .name = "Treble", |
| 597 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 598 | .info = tas_snd_treble_info, |
| 599 | .get = tas_snd_treble_get, |
| 600 | .put = tas_snd_treble_put, |
| 601 | }; |
| 602 | |
| 603 | static int tas_snd_bass_info(struct snd_kcontrol *kcontrol, |
| 604 | struct snd_ctl_elem_info *uinfo) |
| 605 | { |
| 606 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 607 | uinfo->count = 1; |
| 608 | uinfo->value.integer.min = TAS3004_BASS_MIN; |
| 609 | uinfo->value.integer.max = TAS3004_BASS_MAX; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int tas_snd_bass_get(struct snd_kcontrol *kcontrol, |
| 614 | struct snd_ctl_elem_value *ucontrol) |
| 615 | { |
| 616 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 617 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 618 | mutex_lock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 619 | ucontrol->value.integer.value[0] = tas->bass; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 620 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int tas_snd_bass_put(struct snd_kcontrol *kcontrol, |
| 625 | struct snd_ctl_elem_value *ucontrol) |
| 626 | { |
| 627 | struct tas *tas = snd_kcontrol_chip(kcontrol); |
| 628 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 629 | mutex_lock(&tas->mtx); |
| 630 | if (tas->bass == ucontrol->value.integer.value[0]) { |
| 631 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 632 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 633 | } |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 634 | |
| 635 | tas->bass = ucontrol->value.integer.value[0]; |
| 636 | if (tas->hw_enabled) |
| 637 | tas_set_bass(tas); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 638 | mutex_unlock(&tas->mtx); |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | static struct snd_kcontrol_new bass_control = { |
| 643 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 644 | .name = "Bass", |
| 645 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, |
| 646 | .info = tas_snd_bass_info, |
| 647 | .get = tas_snd_bass_get, |
| 648 | .put = tas_snd_bass_put, |
| 649 | }; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 650 | |
| 651 | static struct transfer_info tas_transfers[] = { |
| 652 | { |
| 653 | /* input */ |
| 654 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE | |
| 655 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE, |
| 656 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
| 657 | .transfer_in = 1, |
| 658 | }, |
| 659 | { |
| 660 | /* output */ |
| 661 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE | |
| 662 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE, |
| 663 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
| 664 | .transfer_in = 0, |
| 665 | }, |
| 666 | {} |
| 667 | }; |
| 668 | |
| 669 | static int tas_usable(struct codec_info_item *cii, |
| 670 | struct transfer_info *ti, |
| 671 | struct transfer_info *out) |
| 672 | { |
| 673 | return 1; |
| 674 | } |
| 675 | |
| 676 | static int tas_reset_init(struct tas *tas) |
| 677 | { |
| 678 | u8 tmp; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 679 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 680 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); |
| 681 | msleep(5); |
| 682 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
| 683 | msleep(5); |
| 684 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1); |
| 685 | msleep(20); |
| 686 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
| 687 | msleep(10); |
| 688 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 689 | |
| 690 | tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT; |
| 691 | if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 692 | goto outerr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 693 | |
Paul Mackerras | 80b8d5d | 2006-10-31 15:24:45 +0100 | [diff] [blame] | 694 | tas->acr |= TAS_ACR_ANALOG_PDOWN; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 695 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 696 | goto outerr; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 697 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 698 | tmp = 0; |
| 699 | if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 700 | goto outerr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 701 | |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 702 | tas3004_set_drc(tas); |
| 703 | |
| 704 | /* Set treble & bass to 0dB */ |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 705 | tas->treble = TAS3004_TREBLE_ZERO; |
| 706 | tas->bass = TAS3004_BASS_ZERO; |
| 707 | tas_set_treble(tas); |
| 708 | tas_set_bass(tas); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 709 | |
| 710 | tas->acr &= ~TAS_ACR_ANALOG_PDOWN; |
| 711 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 712 | goto outerr; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 713 | |
| 714 | return 0; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 715 | outerr: |
| 716 | return -ENODEV; |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock) |
| 720 | { |
| 721 | struct tas *tas = cii->codec_data; |
| 722 | |
| 723 | switch(clock) { |
| 724 | case CLOCK_SWITCH_PREPARE_SLAVE: |
| 725 | /* Clocks are going away, mute mute mute */ |
| 726 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); |
| 727 | tas->hw_enabled = 0; |
| 728 | break; |
| 729 | case CLOCK_SWITCH_SLAVE: |
| 730 | /* Clocks are back, re-init the codec */ |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 731 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 732 | tas_reset_init(tas); |
| 733 | tas_set_volume(tas); |
| 734 | tas_set_mixer(tas); |
| 735 | tas->hw_enabled = 1; |
| 736 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 737 | mutex_unlock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 738 | break; |
| 739 | default: |
| 740 | /* doesn't happen as of now */ |
| 741 | return -EINVAL; |
| 742 | } |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* we are controlled via i2c and assume that is always up |
| 747 | * If that wasn't the case, we'd have to suspend once |
| 748 | * our i2c device is suspended, and then take note of that! */ |
| 749 | static int tas_suspend(struct tas *tas) |
| 750 | { |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 751 | mutex_lock(&tas->mtx); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 752 | tas->hw_enabled = 0; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 753 | tas->acr |= TAS_ACR_ANALOG_PDOWN; |
| 754 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 755 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int tas_resume(struct tas *tas) |
| 760 | { |
| 761 | /* reset codec */ |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 762 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 763 | tas_reset_init(tas); |
| 764 | tas_set_volume(tas); |
| 765 | tas_set_mixer(tas); |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 766 | tas->hw_enabled = 1; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 767 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | #ifdef CONFIG_PM |
| 772 | static int _tas_suspend(struct codec_info_item *cii, pm_message_t state) |
| 773 | { |
| 774 | return tas_suspend(cii->codec_data); |
| 775 | } |
| 776 | |
| 777 | static int _tas_resume(struct codec_info_item *cii) |
| 778 | { |
| 779 | return tas_resume(cii->codec_data); |
| 780 | } |
| 781 | #endif |
| 782 | |
| 783 | static struct codec_info tas_codec_info = { |
| 784 | .transfers = tas_transfers, |
| 785 | /* in theory, we can drive it at 512 too... |
| 786 | * but so far the framework doesn't allow |
| 787 | * for that and I don't see much point in it. */ |
| 788 | .sysclock_factor = 256, |
| 789 | /* same here, could be 32 for just one 16 bit format */ |
| 790 | .bus_factor = 64, |
| 791 | .owner = THIS_MODULE, |
| 792 | .usable = tas_usable, |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 793 | .switch_clock = tas_switch_clock, |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 794 | #ifdef CONFIG_PM |
| 795 | .suspend = _tas_suspend, |
| 796 | .resume = _tas_resume, |
| 797 | #endif |
| 798 | }; |
| 799 | |
| 800 | static int tas_init_codec(struct aoa_codec *codec) |
| 801 | { |
| 802 | struct tas *tas = codec_to_tas(codec); |
| 803 | int err; |
| 804 | |
| 805 | if (!tas->codec.gpio || !tas->codec.gpio->methods) { |
| 806 | printk(KERN_ERR PFX "gpios not assigned!!\n"); |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 810 | mutex_lock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 811 | if (tas_reset_init(tas)) { |
| 812 | printk(KERN_ERR PFX "tas failed to initialise\n"); |
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 | return -ENXIO; |
| 815 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 816 | tas->hw_enabled = 1; |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 817 | mutex_unlock(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 818 | |
| 819 | if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev, |
| 820 | aoa_get_card(), |
| 821 | &tas_codec_info, tas)) { |
| 822 | printk(KERN_ERR PFX "error attaching tas to soundbus\n"); |
| 823 | return -ENODEV; |
| 824 | } |
| 825 | |
| 826 | if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) { |
| 827 | printk(KERN_ERR PFX "failed to create tas snd device!\n"); |
| 828 | return -ENODEV; |
| 829 | } |
| 830 | err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas)); |
| 831 | if (err) |
| 832 | goto error; |
| 833 | |
| 834 | err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas)); |
| 835 | if (err) |
| 836 | goto error; |
| 837 | |
| 838 | err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas)); |
| 839 | if (err) |
| 840 | goto error; |
| 841 | |
| 842 | err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas)); |
| 843 | if (err) |
| 844 | goto error; |
| 845 | |
| 846 | err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas)); |
| 847 | if (err) |
| 848 | goto error; |
| 849 | |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 850 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas)); |
| 851 | if (err) |
| 852 | goto error; |
| 853 | |
| 854 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas)); |
| 855 | if (err) |
| 856 | goto error; |
| 857 | |
Johannes Berg | 5009932 | 2006-07-10 04:44:41 -0700 | [diff] [blame] | 858 | err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas)); |
| 859 | if (err) |
| 860 | goto error; |
| 861 | |
| 862 | err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas)); |
| 863 | if (err) |
| 864 | goto error; |
| 865 | |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 866 | return 0; |
| 867 | error: |
| 868 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); |
| 869 | snd_device_free(aoa_get_card(), tas); |
| 870 | return err; |
| 871 | } |
| 872 | |
| 873 | static void tas_exit_codec(struct aoa_codec *codec) |
| 874 | { |
| 875 | struct tas *tas = codec_to_tas(codec); |
| 876 | |
| 877 | if (!tas->codec.soundbus_dev) |
| 878 | return; |
| 879 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); |
| 880 | } |
| 881 | |
| 882 | |
| 883 | static struct i2c_driver tas_driver; |
| 884 | |
| 885 | static int tas_create(struct i2c_adapter *adapter, |
| 886 | struct device_node *node, |
| 887 | int addr) |
| 888 | { |
| 889 | struct tas *tas; |
| 890 | |
| 891 | tas = kzalloc(sizeof(struct tas), GFP_KERNEL); |
| 892 | |
| 893 | if (!tas) |
| 894 | return -ENOMEM; |
| 895 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 896 | mutex_init(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 897 | tas->i2c.driver = &tas_driver; |
| 898 | tas->i2c.adapter = adapter; |
| 899 | tas->i2c.addr = addr; |
Johannes Berg | 9b8f52f | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 900 | /* seems that half is a saner default */ |
| 901 | tas->drc_range = TAS3004_DRC_MAX / 2; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 902 | strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1); |
| 903 | |
| 904 | if (i2c_attach_client(&tas->i2c)) { |
| 905 | printk(KERN_ERR PFX "failed to attach to i2c\n"); |
| 906 | goto fail; |
| 907 | } |
| 908 | |
| 909 | strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1); |
| 910 | tas->codec.owner = THIS_MODULE; |
| 911 | tas->codec.init = tas_init_codec; |
| 912 | tas->codec.exit = tas_exit_codec; |
| 913 | tas->codec.node = of_node_get(node); |
| 914 | |
| 915 | if (aoa_codec_register(&tas->codec)) { |
| 916 | goto detach; |
| 917 | } |
Benjamin Herrenschmidt | 6a4f578 | 2006-07-10 04:44:39 -0700 | [diff] [blame] | 918 | printk(KERN_DEBUG |
| 919 | "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n", |
| 920 | addr, node->full_name); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 921 | return 0; |
| 922 | detach: |
| 923 | i2c_detach_client(&tas->i2c); |
| 924 | fail: |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 925 | mutex_destroy(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 926 | kfree(tas); |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | static int tas_i2c_attach(struct i2c_adapter *adapter) |
| 931 | { |
| 932 | struct device_node *busnode, *dev = NULL; |
| 933 | struct pmac_i2c_bus *bus; |
| 934 | |
| 935 | bus = pmac_i2c_adapter_to_bus(adapter); |
| 936 | if (bus == NULL) |
| 937 | return -ENODEV; |
| 938 | busnode = pmac_i2c_get_bus_node(bus); |
| 939 | |
| 940 | while ((dev = of_get_next_child(busnode, dev)) != NULL) { |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 941 | if (of_device_is_compatible(dev, "tas3004")) { |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame] | 942 | const u32 *addr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 943 | printk(KERN_DEBUG PFX "found tas3004\n"); |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 944 | addr = of_get_property(dev, "reg", NULL); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 945 | if (!addr) |
| 946 | continue; |
| 947 | return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f); |
| 948 | } |
| 949 | /* older machines have no 'codec' node with a 'compatible' |
| 950 | * property that says 'tas3004', they just have a 'deq' |
| 951 | * node without any such property... */ |
| 952 | if (strcmp(dev->name, "deq") == 0) { |
Stephen Rothwell | a7edd0e | 2007-04-03 10:52:17 +1000 | [diff] [blame] | 953 | const u32 *_addr; |
| 954 | u32 addr; |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 955 | printk(KERN_DEBUG PFX "found 'deq' node\n"); |
Stephen Rothwell | c4f55b3 | 2007-04-03 22:39:14 +1000 | [diff] [blame] | 956 | _addr = of_get_property(dev, "i2c-address", NULL); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 957 | if (!_addr) |
| 958 | continue; |
| 959 | addr = ((*_addr) >> 1) & 0x7f; |
| 960 | /* now, if the address doesn't match any of the two |
| 961 | * that a tas3004 can have, we cannot handle this. |
| 962 | * I doubt it ever happens but hey. */ |
| 963 | if (addr != 0x34 && addr != 0x35) |
| 964 | continue; |
| 965 | return tas_create(adapter, dev, addr); |
| 966 | } |
| 967 | } |
| 968 | return -ENODEV; |
| 969 | } |
| 970 | |
| 971 | static int tas_i2c_detach(struct i2c_client *client) |
| 972 | { |
| 973 | struct tas *tas = container_of(client, struct tas, i2c); |
| 974 | int err; |
| 975 | u8 tmp = TAS_ACR_ANALOG_PDOWN; |
| 976 | |
| 977 | if ((err = i2c_detach_client(client))) |
| 978 | return err; |
| 979 | aoa_codec_unregister(&tas->codec); |
| 980 | of_node_put(tas->codec.node); |
| 981 | |
| 982 | /* power down codec chip */ |
| 983 | tas_write_reg(tas, TAS_REG_ACR, 1, &tmp); |
| 984 | |
Johannes Berg | 3071920 | 2006-09-17 21:59:25 +0200 | [diff] [blame] | 985 | mutex_destroy(&tas->mtx); |
Johannes Berg | f3d9478 | 2006-06-21 15:42:43 +0200 | [diff] [blame] | 986 | kfree(tas); |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | static struct i2c_driver tas_driver = { |
| 991 | .driver = { |
| 992 | .name = "aoa_codec_tas", |
| 993 | .owner = THIS_MODULE, |
| 994 | }, |
| 995 | .attach_adapter = tas_i2c_attach, |
| 996 | .detach_client = tas_i2c_detach, |
| 997 | }; |
| 998 | |
| 999 | static int __init tas_init(void) |
| 1000 | { |
| 1001 | return i2c_add_driver(&tas_driver); |
| 1002 | } |
| 1003 | |
| 1004 | static void __exit tas_exit(void) |
| 1005 | { |
| 1006 | i2c_del_driver(&tas_driver); |
| 1007 | } |
| 1008 | |
| 1009 | module_init(tas_init); |
| 1010 | module_exit(tas_exit); |