Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver |
| 3 | * |
| 4 | * Copyright (C) Nokia Corporation |
| 5 | * |
| 6 | * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/gpio.h> |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 28 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 30 | #include <sound/tpa6130a2-plat.h> |
| 31 | #include <sound/soc.h> |
| 32 | #include <sound/soc-dapm.h> |
| 33 | #include <sound/tlv.h> |
| 34 | |
| 35 | #include "tpa6130a2.h" |
| 36 | |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 37 | static struct i2c_client *tpa6130a2_client; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 38 | |
| 39 | /* This struct is used to save the context */ |
| 40 | struct tpa6130a2_data { |
| 41 | struct mutex mutex; |
| 42 | unsigned char regs[TPA6130A2_CACHEREGNUM]; |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 43 | struct regulator *supply; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 44 | int power_gpio; |
| 45 | unsigned char power_state; |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 46 | enum tpa_model id; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static int tpa6130a2_i2c_read(int reg) |
| 50 | { |
| 51 | struct tpa6130a2_data *data; |
| 52 | int val; |
| 53 | |
| 54 | BUG_ON(tpa6130a2_client == NULL); |
| 55 | data = i2c_get_clientdata(tpa6130a2_client); |
| 56 | |
| 57 | /* If powered off, return the cached value */ |
| 58 | if (data->power_state) { |
| 59 | val = i2c_smbus_read_byte_data(tpa6130a2_client, reg); |
| 60 | if (val < 0) |
| 61 | dev_err(&tpa6130a2_client->dev, "Read failed\n"); |
| 62 | else |
| 63 | data->regs[reg] = val; |
| 64 | } else { |
| 65 | val = data->regs[reg]; |
| 66 | } |
| 67 | |
| 68 | return val; |
| 69 | } |
| 70 | |
| 71 | static int tpa6130a2_i2c_write(int reg, u8 value) |
| 72 | { |
| 73 | struct tpa6130a2_data *data; |
| 74 | int val = 0; |
| 75 | |
| 76 | BUG_ON(tpa6130a2_client == NULL); |
| 77 | data = i2c_get_clientdata(tpa6130a2_client); |
| 78 | |
| 79 | if (data->power_state) { |
| 80 | val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value); |
Axel Lin | 7a479b0 | 2010-11-23 14:14:07 +0800 | [diff] [blame^] | 81 | if (val < 0) { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 82 | dev_err(&tpa6130a2_client->dev, "Write failed\n"); |
Axel Lin | 7a479b0 | 2010-11-23 14:14:07 +0800 | [diff] [blame^] | 83 | return val; |
| 84 | } |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Either powered on or off, we save the context */ |
| 88 | data->regs[reg] = value; |
| 89 | |
| 90 | return val; |
| 91 | } |
| 92 | |
| 93 | static u8 tpa6130a2_read(int reg) |
| 94 | { |
| 95 | struct tpa6130a2_data *data; |
| 96 | |
| 97 | BUG_ON(tpa6130a2_client == NULL); |
| 98 | data = i2c_get_clientdata(tpa6130a2_client); |
| 99 | |
| 100 | return data->regs[reg]; |
| 101 | } |
| 102 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 103 | static int tpa6130a2_initialize(void) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 104 | { |
| 105 | struct tpa6130a2_data *data; |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 106 | int i, ret = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 107 | |
| 108 | BUG_ON(tpa6130a2_client == NULL); |
| 109 | data = i2c_get_clientdata(tpa6130a2_client); |
| 110 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 111 | for (i = 1; i < TPA6130A2_REG_VERSION; i++) { |
| 112 | ret = tpa6130a2_i2c_write(i, data->regs[i]); |
| 113 | if (ret < 0) |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 118 | } |
| 119 | |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 120 | static int tpa6130a2_power(int power) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 121 | { |
| 122 | struct tpa6130a2_data *data; |
| 123 | u8 val; |
Jarkko Nikula | 75e3f31 | 2010-11-03 16:39:00 +0200 | [diff] [blame] | 124 | int ret = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 125 | |
| 126 | BUG_ON(tpa6130a2_client == NULL); |
| 127 | data = i2c_get_clientdata(tpa6130a2_client); |
| 128 | |
| 129 | mutex_lock(&data->mutex); |
Jarkko Nikula | 63f7526 | 2010-10-28 14:05:40 +0300 | [diff] [blame] | 130 | if (power && !data->power_state) { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 131 | /* Power on */ |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 132 | if (data->power_gpio >= 0) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 133 | gpio_set_value(data->power_gpio, 1); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 134 | |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 135 | ret = regulator_enable(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 136 | if (ret != 0) { |
| 137 | dev_err(&tpa6130a2_client->dev, |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 138 | "Failed to enable supply: %d\n", ret); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 139 | goto exit; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 140 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 141 | |
| 142 | data->power_state = 1; |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 143 | ret = tpa6130a2_initialize(); |
| 144 | if (ret < 0) { |
| 145 | dev_err(&tpa6130a2_client->dev, |
| 146 | "Failed to initialize chip\n"); |
| 147 | if (data->power_gpio >= 0) |
| 148 | gpio_set_value(data->power_gpio, 0); |
| 149 | regulator_disable(data->supply); |
| 150 | data->power_state = 0; |
| 151 | goto exit; |
| 152 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 153 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 154 | /* Clear SWS */ |
| 155 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 156 | val &= ~TPA6130A2_SWS; |
| 157 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
Jarkko Nikula | 63f7526 | 2010-10-28 14:05:40 +0300 | [diff] [blame] | 158 | } else if (!power && data->power_state) { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 159 | /* set SWS */ |
| 160 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 161 | val |= TPA6130A2_SWS; |
| 162 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 163 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 164 | /* Power off */ |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 165 | if (data->power_gpio >= 0) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 166 | gpio_set_value(data->power_gpio, 0); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 167 | |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 168 | ret = regulator_disable(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 169 | if (ret != 0) { |
| 170 | dev_err(&tpa6130a2_client->dev, |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 171 | "Failed to disable supply: %d\n", ret); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 172 | goto exit; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 173 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 174 | |
| 175 | data->power_state = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 176 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 177 | |
| 178 | exit: |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 179 | mutex_unlock(&data->mutex); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 180 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 181 | } |
| 182 | |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 183 | static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 184 | struct snd_ctl_elem_value *ucontrol) |
| 185 | { |
| 186 | struct soc_mixer_control *mc = |
| 187 | (struct soc_mixer_control *)kcontrol->private_value; |
| 188 | struct tpa6130a2_data *data; |
| 189 | unsigned int reg = mc->reg; |
| 190 | unsigned int shift = mc->shift; |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 191 | int max = mc->max; |
| 192 | unsigned int mask = (1 << fls(max)) - 1; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 193 | unsigned int invert = mc->invert; |
| 194 | |
| 195 | BUG_ON(tpa6130a2_client == NULL); |
| 196 | data = i2c_get_clientdata(tpa6130a2_client); |
| 197 | |
| 198 | mutex_lock(&data->mutex); |
| 199 | |
| 200 | ucontrol->value.integer.value[0] = |
| 201 | (tpa6130a2_read(reg) >> shift) & mask; |
| 202 | |
| 203 | if (invert) |
| 204 | ucontrol->value.integer.value[0] = |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 205 | max - ucontrol->value.integer.value[0]; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 206 | |
| 207 | mutex_unlock(&data->mutex); |
| 208 | return 0; |
| 209 | } |
| 210 | |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 211 | static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 212 | struct snd_ctl_elem_value *ucontrol) |
| 213 | { |
| 214 | struct soc_mixer_control *mc = |
| 215 | (struct soc_mixer_control *)kcontrol->private_value; |
| 216 | struct tpa6130a2_data *data; |
| 217 | unsigned int reg = mc->reg; |
| 218 | unsigned int shift = mc->shift; |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 219 | int max = mc->max; |
| 220 | unsigned int mask = (1 << fls(max)) - 1; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 221 | unsigned int invert = mc->invert; |
| 222 | unsigned int val = (ucontrol->value.integer.value[0] & mask); |
| 223 | unsigned int val_reg; |
| 224 | |
| 225 | BUG_ON(tpa6130a2_client == NULL); |
| 226 | data = i2c_get_clientdata(tpa6130a2_client); |
| 227 | |
| 228 | if (invert) |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 229 | val = max - val; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 230 | |
| 231 | mutex_lock(&data->mutex); |
| 232 | |
| 233 | val_reg = tpa6130a2_read(reg); |
| 234 | if (((val_reg >> shift) & mask) == val) { |
| 235 | mutex_unlock(&data->mutex); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | val_reg &= ~(mask << shift); |
| 240 | val_reg |= val << shift; |
| 241 | tpa6130a2_i2c_write(reg, val_reg); |
| 242 | |
| 243 | mutex_unlock(&data->mutex); |
| 244 | |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going |
| 250 | * down in gain. |
| 251 | */ |
| 252 | static const unsigned int tpa6130_tlv[] = { |
| 253 | TLV_DB_RANGE_HEAD(10), |
| 254 | 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0), |
| 255 | 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0), |
| 256 | 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0), |
| 257 | 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0), |
| 258 | 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0), |
| 259 | 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0), |
| 260 | 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0), |
| 261 | 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0), |
| 262 | 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0), |
| 263 | 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0), |
| 264 | }; |
| 265 | |
| 266 | static const struct snd_kcontrol_new tpa6130a2_controls[] = { |
| 267 | SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume", |
| 268 | TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0, |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 269 | tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 270 | tpa6130_tlv), |
| 271 | }; |
| 272 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 273 | static const unsigned int tpa6140_tlv[] = { |
| 274 | TLV_DB_RANGE_HEAD(3), |
| 275 | 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0), |
| 276 | 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0), |
| 277 | 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0), |
| 278 | }; |
| 279 | |
| 280 | static const struct snd_kcontrol_new tpa6140a2_controls[] = { |
Peter Ujfalusi | 826e962 | 2010-05-07 14:24:10 +0300 | [diff] [blame] | 281 | SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume", |
| 282 | TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0, |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 283 | tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
Peter Ujfalusi | 826e962 | 2010-05-07 14:24:10 +0300 | [diff] [blame] | 284 | tpa6140_tlv), |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 285 | }; |
| 286 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 287 | /* |
| 288 | * Enable or disable channel (left or right) |
| 289 | * The bit number for mute and amplifier are the same per channel: |
| 290 | * bit 6: Right channel |
| 291 | * bit 7: Left channel |
| 292 | * in both registers. |
| 293 | */ |
| 294 | static void tpa6130a2_channel_enable(u8 channel, int enable) |
| 295 | { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 296 | u8 val; |
| 297 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 298 | if (enable) { |
| 299 | /* Enable channel */ |
| 300 | /* Enable amplifier */ |
| 301 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 302 | val |= channel; |
| 303 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
| 304 | |
| 305 | /* Unmute channel */ |
| 306 | val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE); |
| 307 | val &= ~channel; |
| 308 | tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val); |
| 309 | } else { |
| 310 | /* Disable channel */ |
| 311 | /* Mute channel */ |
| 312 | val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE); |
| 313 | val |= channel; |
| 314 | tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val); |
| 315 | |
| 316 | /* Disable amplifier */ |
| 317 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 318 | val &= ~channel; |
| 319 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w, |
| 324 | struct snd_kcontrol *kcontrol, int event) |
| 325 | { |
| 326 | switch (event) { |
| 327 | case SND_SOC_DAPM_POST_PMU: |
| 328 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1); |
| 329 | break; |
| 330 | case SND_SOC_DAPM_POST_PMD: |
| 331 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0); |
| 332 | break; |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w, |
| 338 | struct snd_kcontrol *kcontrol, int event) |
| 339 | { |
| 340 | switch (event) { |
| 341 | case SND_SOC_DAPM_POST_PMU: |
| 342 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1); |
| 343 | break; |
| 344 | case SND_SOC_DAPM_POST_PMD: |
| 345 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0); |
| 346 | break; |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w, |
| 352 | struct snd_kcontrol *kcontrol, int event) |
| 353 | { |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 354 | int ret = 0; |
| 355 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 356 | switch (event) { |
| 357 | case SND_SOC_DAPM_POST_PMU: |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 358 | ret = tpa6130a2_power(1); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 359 | break; |
| 360 | case SND_SOC_DAPM_POST_PMD: |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 361 | ret = tpa6130a2_power(0); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 362 | break; |
| 363 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 364 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = { |
| 368 | SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM, |
| 369 | 0, 0, NULL, 0, tpa6130a2_left_event, |
| 370 | SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD), |
| 371 | SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM, |
| 372 | 0, 0, NULL, 0, tpa6130a2_right_event, |
| 373 | SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD), |
| 374 | SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM, |
| 375 | 0, 0, tpa6130a2_supply_event, |
| 376 | SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD), |
| 377 | /* Outputs */ |
Jarkko Nikula | 266d38c | 2010-05-19 13:55:26 +0300 | [diff] [blame] | 378 | SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"), |
| 379 | SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"), |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | static const struct snd_soc_dapm_route audio_map[] = { |
| 383 | {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"}, |
| 384 | {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"}, |
| 385 | |
| 386 | {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"}, |
| 387 | {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"}, |
| 388 | }; |
| 389 | |
| 390 | int tpa6130a2_add_controls(struct snd_soc_codec *codec) |
| 391 | { |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 392 | struct tpa6130a2_data *data; |
| 393 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 394 | if (tpa6130a2_client == NULL) |
| 395 | return -ENODEV; |
| 396 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 397 | data = i2c_get_clientdata(tpa6130a2_client); |
| 398 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 399 | snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets, |
| 400 | ARRAY_SIZE(tpa6130a2_dapm_widgets)); |
| 401 | |
| 402 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 403 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 404 | if (data->id == TPA6140A2) |
| 405 | return snd_soc_add_controls(codec, tpa6140a2_controls, |
| 406 | ARRAY_SIZE(tpa6140a2_controls)); |
| 407 | else |
| 408 | return snd_soc_add_controls(codec, tpa6130a2_controls, |
| 409 | ARRAY_SIZE(tpa6130a2_controls)); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 410 | |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(tpa6130a2_add_controls); |
| 413 | |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 414 | static int __devinit tpa6130a2_probe(struct i2c_client *client, |
| 415 | const struct i2c_device_id *id) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 416 | { |
| 417 | struct device *dev; |
| 418 | struct tpa6130a2_data *data; |
| 419 | struct tpa6130a2_platform_data *pdata; |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 420 | const char *regulator; |
| 421 | int ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 422 | |
| 423 | dev = &client->dev; |
| 424 | |
| 425 | if (client->dev.platform_data == NULL) { |
| 426 | dev_err(dev, "Platform data not set\n"); |
| 427 | dump_stack(); |
| 428 | return -ENODEV; |
| 429 | } |
| 430 | |
| 431 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 432 | if (data == NULL) { |
| 433 | dev_err(dev, "Can not allocate memory\n"); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
| 437 | tpa6130a2_client = client; |
| 438 | |
| 439 | i2c_set_clientdata(tpa6130a2_client, data); |
| 440 | |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 441 | pdata = client->dev.platform_data; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 442 | data->power_gpio = pdata->power_gpio; |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 443 | data->id = pdata->id; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 444 | |
| 445 | mutex_init(&data->mutex); |
| 446 | |
| 447 | /* Set default register values */ |
| 448 | data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS; |
| 449 | data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R | |
| 450 | TPA6130A2_MUTE_L; |
| 451 | |
| 452 | if (data->power_gpio >= 0) { |
| 453 | ret = gpio_request(data->power_gpio, "tpa6130a2 enable"); |
| 454 | if (ret < 0) { |
| 455 | dev_err(dev, "Failed to request power GPIO (%d)\n", |
| 456 | data->power_gpio); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 457 | goto err_gpio; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 458 | } |
| 459 | gpio_direction_output(data->power_gpio, 0); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 460 | } |
| 461 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 462 | switch (data->id) { |
Ilkka Koskinen | 2138301 | 2010-01-08 17:48:31 +0200 | [diff] [blame] | 463 | default: |
| 464 | dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n", |
| 465 | pdata->id); |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 466 | case TPA6130A2: |
| 467 | regulator = "Vdd"; |
| 468 | break; |
| 469 | case TPA6140A2: |
| 470 | regulator = "AVdd"; |
| 471 | break; |
Ilkka Koskinen | 2138301 | 2010-01-08 17:48:31 +0200 | [diff] [blame] | 472 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 473 | |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 474 | data->supply = regulator_get(dev, regulator); |
| 475 | if (IS_ERR(data->supply)) { |
| 476 | ret = PTR_ERR(data->supply); |
| 477 | dev_err(dev, "Failed to request supply: %d\n", ret); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 478 | goto err_regulator; |
| 479 | } |
| 480 | |
| 481 | ret = tpa6130a2_power(1); |
| 482 | if (ret != 0) |
| 483 | goto err_power; |
| 484 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 485 | |
| 486 | /* Read version */ |
| 487 | ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) & |
| 488 | TPA6130A2_VERSION_MASK; |
| 489 | if ((ret != 1) && (ret != 2)) |
| 490 | dev_warn(dev, "UNTESTED version detected (%d)\n", ret); |
| 491 | |
| 492 | /* Disable the chip */ |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 493 | ret = tpa6130a2_power(0); |
| 494 | if (ret != 0) |
| 495 | goto err_power; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 496 | |
| 497 | return 0; |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 498 | |
| 499 | err_power: |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 500 | regulator_put(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 501 | err_regulator: |
| 502 | if (data->power_gpio >= 0) |
| 503 | gpio_free(data->power_gpio); |
| 504 | err_gpio: |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 505 | kfree(data); |
| 506 | i2c_set_clientdata(tpa6130a2_client, NULL); |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 507 | tpa6130a2_client = NULL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
Mark Brown | 735fe4c | 2010-01-12 14:13:00 +0000 | [diff] [blame] | 512 | static int __devexit tpa6130a2_remove(struct i2c_client *client) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 513 | { |
| 514 | struct tpa6130a2_data *data = i2c_get_clientdata(client); |
| 515 | |
| 516 | tpa6130a2_power(0); |
| 517 | |
| 518 | if (data->power_gpio >= 0) |
| 519 | gpio_free(data->power_gpio); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 520 | |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 521 | regulator_put(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 522 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 523 | kfree(data); |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 524 | tpa6130a2_client = NULL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static const struct i2c_device_id tpa6130a2_id[] = { |
| 530 | { "tpa6130a2", 0 }, |
| 531 | { } |
| 532 | }; |
| 533 | MODULE_DEVICE_TABLE(i2c, tpa6130a2_id); |
| 534 | |
| 535 | static struct i2c_driver tpa6130a2_i2c_driver = { |
| 536 | .driver = { |
| 537 | .name = "tpa6130a2", |
| 538 | .owner = THIS_MODULE, |
| 539 | }, |
| 540 | .probe = tpa6130a2_probe, |
| 541 | .remove = __devexit_p(tpa6130a2_remove), |
| 542 | .id_table = tpa6130a2_id, |
| 543 | }; |
| 544 | |
| 545 | static int __init tpa6130a2_init(void) |
| 546 | { |
| 547 | return i2c_add_driver(&tpa6130a2_i2c_driver); |
| 548 | } |
| 549 | |
| 550 | static void __exit tpa6130a2_exit(void) |
| 551 | { |
| 552 | i2c_del_driver(&tpa6130a2_i2c_driver); |
| 553 | } |
| 554 | |
| 555 | MODULE_AUTHOR("Peter Ujfalusi"); |
| 556 | MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver"); |
| 557 | MODULE_LICENSE("GPL"); |
| 558 | |
| 559 | module_init(tpa6130a2_init); |
| 560 | module_exit(tpa6130a2_exit); |