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