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 | * |
Peter Ujfalusi | b4079ef | 2011-05-03 18:12:41 +0300 | [diff] [blame] | 6 | * Author: Peter Ujfalusi <peter.ujfalusi@ti.com> |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 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> |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 32 | #include <sound/tlv.h> |
Sachin Kamat | ee5e453 | 2014-04-04 11:29:13 +0530 | [diff] [blame] | 33 | #include <linux/of.h> |
Sebastian Reichel | f95a488 | 2013-10-23 14:03:28 +0200 | [diff] [blame] | 34 | #include <linux/of_gpio.h> |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 35 | |
| 36 | #include "tpa6130a2.h" |
| 37 | |
Peter Ujfalusi | 0722d05 | 2011-08-30 14:39:54 +0300 | [diff] [blame] | 38 | enum tpa_model { |
| 39 | TPA6130A2, |
| 40 | TPA6140A2, |
| 41 | }; |
| 42 | |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 43 | static struct i2c_client *tpa6130a2_client; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 44 | |
| 45 | /* This struct is used to save the context */ |
| 46 | struct tpa6130a2_data { |
| 47 | struct mutex mutex; |
| 48 | unsigned char regs[TPA6130A2_CACHEREGNUM]; |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 49 | struct regulator *supply; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 50 | int power_gpio; |
Peter Ujfalusi | d5876ce | 2010-11-30 16:00:00 +0200 | [diff] [blame] | 51 | u8 power_state:1; |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 52 | enum tpa_model id; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static int tpa6130a2_i2c_read(int reg) |
| 56 | { |
| 57 | struct tpa6130a2_data *data; |
| 58 | int val; |
| 59 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 60 | if (WARN_ON(!tpa6130a2_client)) |
| 61 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 62 | data = i2c_get_clientdata(tpa6130a2_client); |
| 63 | |
| 64 | /* If powered off, return the cached value */ |
| 65 | if (data->power_state) { |
| 66 | val = i2c_smbus_read_byte_data(tpa6130a2_client, reg); |
| 67 | if (val < 0) |
| 68 | dev_err(&tpa6130a2_client->dev, "Read failed\n"); |
| 69 | else |
| 70 | data->regs[reg] = val; |
| 71 | } else { |
| 72 | val = data->regs[reg]; |
| 73 | } |
| 74 | |
| 75 | return val; |
| 76 | } |
| 77 | |
| 78 | static int tpa6130a2_i2c_write(int reg, u8 value) |
| 79 | { |
| 80 | struct tpa6130a2_data *data; |
| 81 | int val = 0; |
| 82 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 83 | if (WARN_ON(!tpa6130a2_client)) |
| 84 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 85 | data = i2c_get_clientdata(tpa6130a2_client); |
| 86 | |
| 87 | if (data->power_state) { |
| 88 | val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value); |
Axel Lin | 7a479b0 | 2010-11-23 14:14:07 +0800 | [diff] [blame] | 89 | if (val < 0) { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 90 | dev_err(&tpa6130a2_client->dev, "Write failed\n"); |
Axel Lin | 7a479b0 | 2010-11-23 14:14:07 +0800 | [diff] [blame] | 91 | return val; |
| 92 | } |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Either powered on or off, we save the context */ |
| 96 | data->regs[reg] = value; |
| 97 | |
| 98 | return val; |
| 99 | } |
| 100 | |
| 101 | static u8 tpa6130a2_read(int reg) |
| 102 | { |
| 103 | struct tpa6130a2_data *data; |
| 104 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 105 | if (WARN_ON(!tpa6130a2_client)) |
| 106 | return 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 107 | data = i2c_get_clientdata(tpa6130a2_client); |
| 108 | |
| 109 | return data->regs[reg]; |
| 110 | } |
| 111 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 112 | static int tpa6130a2_initialize(void) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 113 | { |
| 114 | struct tpa6130a2_data *data; |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 115 | int i, ret = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 116 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 117 | if (WARN_ON(!tpa6130a2_client)) |
| 118 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 119 | data = i2c_get_clientdata(tpa6130a2_client); |
| 120 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 121 | for (i = 1; i < TPA6130A2_REG_VERSION; i++) { |
| 122 | ret = tpa6130a2_i2c_write(i, data->regs[i]); |
| 123 | if (ret < 0) |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 128 | } |
| 129 | |
Peter Ujfalusi | d5876ce | 2010-11-30 16:00:00 +0200 | [diff] [blame] | 130 | static int tpa6130a2_power(u8 power) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 131 | { |
| 132 | struct tpa6130a2_data *data; |
| 133 | u8 val; |
Jarkko Nikula | 75e3f31 | 2010-11-03 16:39:00 +0200 | [diff] [blame] | 134 | int ret = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 135 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 136 | if (WARN_ON(!tpa6130a2_client)) |
| 137 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 138 | data = i2c_get_clientdata(tpa6130a2_client); |
| 139 | |
| 140 | mutex_lock(&data->mutex); |
Peter Ujfalusi | d5876ce | 2010-11-30 16:00:00 +0200 | [diff] [blame] | 141 | if (power == data->power_state) |
| 142 | goto exit; |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 143 | |
Peter Ujfalusi | d5876ce | 2010-11-30 16:00:00 +0200 | [diff] [blame] | 144 | if (power) { |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 145 | ret = regulator_enable(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 146 | if (ret != 0) { |
| 147 | dev_err(&tpa6130a2_client->dev, |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 148 | "Failed to enable supply: %d\n", ret); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 149 | goto exit; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 150 | } |
Jarkko Nikula | 0656f6c | 2010-11-08 10:57:57 +0200 | [diff] [blame] | 151 | /* Power on */ |
| 152 | if (data->power_gpio >= 0) |
| 153 | gpio_set_value(data->power_gpio, 1); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 154 | |
| 155 | data->power_state = 1; |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 156 | ret = tpa6130a2_initialize(); |
| 157 | if (ret < 0) { |
| 158 | dev_err(&tpa6130a2_client->dev, |
| 159 | "Failed to initialize chip\n"); |
| 160 | if (data->power_gpio >= 0) |
| 161 | gpio_set_value(data->power_gpio, 0); |
| 162 | regulator_disable(data->supply); |
| 163 | data->power_state = 0; |
| 164 | goto exit; |
| 165 | } |
Peter Ujfalusi | d5876ce | 2010-11-30 16:00:00 +0200 | [diff] [blame] | 166 | } else { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 167 | /* set SWS */ |
| 168 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 169 | val |= TPA6130A2_SWS; |
| 170 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 171 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 172 | /* Power off */ |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 173 | if (data->power_gpio >= 0) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 174 | gpio_set_value(data->power_gpio, 0); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 175 | |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 176 | ret = regulator_disable(data->supply); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 177 | if (ret != 0) { |
| 178 | dev_err(&tpa6130a2_client->dev, |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 179 | "Failed to disable supply: %d\n", ret); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 180 | goto exit; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 181 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 182 | |
| 183 | data->power_state = 0; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 184 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 185 | |
| 186 | exit: |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 187 | mutex_unlock(&data->mutex); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 188 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 189 | } |
| 190 | |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 191 | static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 192 | struct snd_ctl_elem_value *ucontrol) |
| 193 | { |
| 194 | struct soc_mixer_control *mc = |
| 195 | (struct soc_mixer_control *)kcontrol->private_value; |
| 196 | struct tpa6130a2_data *data; |
| 197 | unsigned int reg = mc->reg; |
| 198 | unsigned int shift = mc->shift; |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 199 | int max = mc->max; |
| 200 | unsigned int mask = (1 << fls(max)) - 1; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 201 | unsigned int invert = mc->invert; |
| 202 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 203 | if (WARN_ON(!tpa6130a2_client)) |
| 204 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 205 | data = i2c_get_clientdata(tpa6130a2_client); |
| 206 | |
| 207 | mutex_lock(&data->mutex); |
| 208 | |
| 209 | ucontrol->value.integer.value[0] = |
| 210 | (tpa6130a2_read(reg) >> shift) & mask; |
| 211 | |
| 212 | if (invert) |
| 213 | ucontrol->value.integer.value[0] = |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 214 | max - ucontrol->value.integer.value[0]; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 215 | |
| 216 | mutex_unlock(&data->mutex); |
| 217 | return 0; |
| 218 | } |
| 219 | |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 220 | static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 221 | struct snd_ctl_elem_value *ucontrol) |
| 222 | { |
| 223 | struct soc_mixer_control *mc = |
| 224 | (struct soc_mixer_control *)kcontrol->private_value; |
| 225 | struct tpa6130a2_data *data; |
| 226 | unsigned int reg = mc->reg; |
| 227 | unsigned int shift = mc->shift; |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 228 | int max = mc->max; |
| 229 | unsigned int mask = (1 << fls(max)) - 1; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 230 | unsigned int invert = mc->invert; |
| 231 | unsigned int val = (ucontrol->value.integer.value[0] & mask); |
| 232 | unsigned int val_reg; |
| 233 | |
Takashi Iwai | 773392b | 2013-11-05 18:39:51 +0100 | [diff] [blame] | 234 | if (WARN_ON(!tpa6130a2_client)) |
| 235 | return -EINVAL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 236 | data = i2c_get_clientdata(tpa6130a2_client); |
| 237 | |
| 238 | if (invert) |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 239 | val = max - val; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 240 | |
| 241 | mutex_lock(&data->mutex); |
| 242 | |
| 243 | val_reg = tpa6130a2_read(reg); |
| 244 | if (((val_reg >> shift) & mask) == val) { |
| 245 | mutex_unlock(&data->mutex); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | val_reg &= ~(mask << shift); |
| 250 | val_reg |= val << shift; |
| 251 | tpa6130a2_i2c_write(reg, val_reg); |
| 252 | |
| 253 | mutex_unlock(&data->mutex); |
| 254 | |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going |
| 260 | * down in gain. |
| 261 | */ |
Lars-Peter Clausen | 4e0e5f8 | 2015-08-02 17:19:55 +0200 | [diff] [blame] | 262 | static const DECLARE_TLV_DB_RANGE(tpa6130_tlv, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 263 | 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0), |
| 264 | 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0), |
| 265 | 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0), |
| 266 | 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0), |
| 267 | 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0), |
| 268 | 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0), |
| 269 | 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0), |
| 270 | 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0), |
| 271 | 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0), |
Lars-Peter Clausen | 4e0e5f8 | 2015-08-02 17:19:55 +0200 | [diff] [blame] | 272 | 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0) |
| 273 | ); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 274 | |
| 275 | static const struct snd_kcontrol_new tpa6130a2_controls[] = { |
| 276 | SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume", |
| 277 | TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0, |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 278 | tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 279 | tpa6130_tlv), |
| 280 | }; |
| 281 | |
Lars-Peter Clausen | 4e0e5f8 | 2015-08-02 17:19:55 +0200 | [diff] [blame] | 282 | static const DECLARE_TLV_DB_RANGE(tpa6140_tlv, |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 283 | 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0), |
| 284 | 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0), |
Lars-Peter Clausen | 4e0e5f8 | 2015-08-02 17:19:55 +0200 | [diff] [blame] | 285 | 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0) |
| 286 | ); |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 287 | |
| 288 | static const struct snd_kcontrol_new tpa6140a2_controls[] = { |
Peter Ujfalusi | 826e962 | 2010-05-07 14:24:10 +0300 | [diff] [blame] | 289 | SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume", |
| 290 | TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0, |
Peter Ujfalusi | bd843ed | 2010-05-07 14:24:11 +0300 | [diff] [blame] | 291 | tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
Peter Ujfalusi | 826e962 | 2010-05-07 14:24:10 +0300 | [diff] [blame] | 292 | tpa6140_tlv), |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 293 | }; |
| 294 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 295 | /* |
| 296 | * Enable or disable channel (left or right) |
| 297 | * The bit number for mute and amplifier are the same per channel: |
| 298 | * bit 6: Right channel |
| 299 | * bit 7: Left channel |
| 300 | * in both registers. |
| 301 | */ |
| 302 | static void tpa6130a2_channel_enable(u8 channel, int enable) |
| 303 | { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 304 | u8 val; |
| 305 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 306 | if (enable) { |
| 307 | /* Enable channel */ |
| 308 | /* Enable amplifier */ |
| 309 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 310 | val |= channel; |
Peter Ujfalusi | d534bac | 2010-11-30 16:00:01 +0200 | [diff] [blame] | 311 | val &= ~TPA6130A2_SWS; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 312 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
| 313 | |
| 314 | /* Unmute channel */ |
| 315 | val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE); |
| 316 | val &= ~channel; |
| 317 | tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val); |
| 318 | } else { |
| 319 | /* Disable channel */ |
| 320 | /* Mute channel */ |
| 321 | val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE); |
| 322 | val |= channel; |
| 323 | tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val); |
| 324 | |
| 325 | /* Disable amplifier */ |
| 326 | val = tpa6130a2_read(TPA6130A2_REG_CONTROL); |
| 327 | val &= ~channel; |
| 328 | tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val); |
| 329 | } |
| 330 | } |
| 331 | |
Peter Ujfalusi | 3964687 | 2010-12-02 09:29:56 +0200 | [diff] [blame] | 332 | int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 333 | { |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 334 | int ret = 0; |
Peter Ujfalusi | 3964687 | 2010-12-02 09:29:56 +0200 | [diff] [blame] | 335 | if (enable) { |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 336 | ret = tpa6130a2_power(1); |
Peter Ujfalusi | 3964687 | 2010-12-02 09:29:56 +0200 | [diff] [blame] | 337 | if (ret < 0) |
| 338 | return ret; |
| 339 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L, |
| 340 | 1); |
| 341 | } else { |
| 342 | tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L, |
| 343 | 0); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 344 | ret = tpa6130a2_power(0); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 345 | } |
Peter Ujfalusi | 3964687 | 2010-12-02 09:29:56 +0200 | [diff] [blame] | 346 | |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 347 | return ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 348 | } |
Peter Ujfalusi | 3964687 | 2010-12-02 09:29:56 +0200 | [diff] [blame] | 349 | EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 350 | |
| 351 | int tpa6130a2_add_controls(struct snd_soc_codec *codec) |
| 352 | { |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 353 | struct tpa6130a2_data *data; |
| 354 | |
Peter Ujfalusi | 872a64d | 2010-10-21 15:03:03 +0300 | [diff] [blame] | 355 | if (tpa6130a2_client == NULL) |
| 356 | return -ENODEV; |
| 357 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 358 | data = i2c_get_clientdata(tpa6130a2_client); |
| 359 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 360 | if (data->id == TPA6140A2) |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 361 | return snd_soc_add_codec_controls(codec, tpa6140a2_controls, |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 362 | ARRAY_SIZE(tpa6140a2_controls)); |
| 363 | else |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 364 | return snd_soc_add_codec_controls(codec, tpa6130a2_controls, |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 365 | ARRAY_SIZE(tpa6130a2_controls)); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 366 | } |
| 367 | EXPORT_SYMBOL_GPL(tpa6130a2_add_controls); |
| 368 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 369 | static int tpa6130a2_probe(struct i2c_client *client, |
| 370 | const struct i2c_device_id *id) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 371 | { |
| 372 | struct device *dev; |
| 373 | struct tpa6130a2_data *data; |
Sebastian Reichel | f95a488 | 2013-10-23 14:03:28 +0200 | [diff] [blame] | 374 | struct tpa6130a2_platform_data *pdata = client->dev.platform_data; |
| 375 | struct device_node *np = client->dev.of_node; |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 376 | const char *regulator; |
| 377 | int ret; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 378 | |
| 379 | dev = &client->dev; |
| 380 | |
Axel Lin | 6945e9f | 2011-12-29 12:12:29 +0800 | [diff] [blame] | 381 | data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
Sachin Kamat | 656e343 | 2014-06-20 15:29:02 +0530 | [diff] [blame] | 382 | if (!data) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 383 | return -ENOMEM; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 384 | |
Sebastian Reichel | f95a488 | 2013-10-23 14:03:28 +0200 | [diff] [blame] | 385 | if (pdata) { |
| 386 | data->power_gpio = pdata->power_gpio; |
| 387 | } else if (np) { |
| 388 | data->power_gpio = of_get_named_gpio(np, "power-gpio", 0); |
| 389 | } else { |
| 390 | dev_err(dev, "Platform data not set\n"); |
| 391 | dump_stack(); |
| 392 | return -ENODEV; |
| 393 | } |
| 394 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 395 | tpa6130a2_client = client; |
| 396 | |
| 397 | i2c_set_clientdata(tpa6130a2_client, data); |
| 398 | |
Peter Ujfalusi | 0744100 | 2011-08-30 14:39:52 +0300 | [diff] [blame] | 399 | data->id = id->driver_data; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 400 | |
| 401 | mutex_init(&data->mutex); |
| 402 | |
| 403 | /* Set default register values */ |
| 404 | data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS; |
| 405 | data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R | |
| 406 | TPA6130A2_MUTE_L; |
| 407 | |
| 408 | if (data->power_gpio >= 0) { |
Sachin Kamat | d06080c | 2012-12-07 16:32:26 +0530 | [diff] [blame] | 409 | ret = devm_gpio_request(dev, data->power_gpio, |
| 410 | "tpa6130a2 enable"); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 411 | if (ret < 0) { |
| 412 | dev_err(dev, "Failed to request power GPIO (%d)\n", |
| 413 | data->power_gpio); |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 414 | goto err_gpio; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 415 | } |
| 416 | gpio_direction_output(data->power_gpio, 0); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 417 | } |
| 418 | |
Peter Ujfalusi | e5e5b31 | 2010-05-04 11:08:18 +0300 | [diff] [blame] | 419 | switch (data->id) { |
Ilkka Koskinen | 2138301 | 2010-01-08 17:48:31 +0200 | [diff] [blame] | 420 | default: |
| 421 | dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n", |
Peter Ujfalusi | 0744100 | 2011-08-30 14:39:52 +0300 | [diff] [blame] | 422 | data->id); |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 423 | case TPA6130A2: |
| 424 | regulator = "Vdd"; |
| 425 | break; |
| 426 | case TPA6140A2: |
| 427 | regulator = "AVdd"; |
| 428 | break; |
Ilkka Koskinen | 2138301 | 2010-01-08 17:48:31 +0200 | [diff] [blame] | 429 | } |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 430 | |
Sachin Kamat | d06080c | 2012-12-07 16:32:26 +0530 | [diff] [blame] | 431 | data->supply = devm_regulator_get(dev, regulator); |
Jarkko Nikula | ad8332c | 2010-05-19 10:52:28 +0300 | [diff] [blame] | 432 | if (IS_ERR(data->supply)) { |
| 433 | ret = PTR_ERR(data->supply); |
| 434 | dev_err(dev, "Failed to request supply: %d\n", ret); |
Sachin Kamat | d06080c | 2012-12-07 16:32:26 +0530 | [diff] [blame] | 435 | goto err_gpio; |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | ret = tpa6130a2_power(1); |
| 439 | if (ret != 0) |
Sachin Kamat | d06080c | 2012-12-07 16:32:26 +0530 | [diff] [blame] | 440 | goto err_gpio; |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 441 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 442 | |
| 443 | /* Read version */ |
| 444 | ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) & |
| 445 | TPA6130A2_VERSION_MASK; |
| 446 | if ((ret != 1) && (ret != 2)) |
| 447 | dev_warn(dev, "UNTESTED version detected (%d)\n", ret); |
| 448 | |
| 449 | /* Disable the chip */ |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 450 | ret = tpa6130a2_power(0); |
| 451 | if (ret != 0) |
Sachin Kamat | d06080c | 2012-12-07 16:32:26 +0530 | [diff] [blame] | 452 | goto err_gpio; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 453 | |
| 454 | return 0; |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 455 | |
Ilkka Koskinen | 7c4e649 | 2009-12-09 12:05:50 +0200 | [diff] [blame] | 456 | err_gpio: |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 457 | tpa6130a2_client = NULL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 462 | static int tpa6130a2_remove(struct i2c_client *client) |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 463 | { |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 464 | tpa6130a2_power(0); |
Mark Brown | ebab1b1 | 2009-10-09 19:13:47 +0100 | [diff] [blame] | 465 | tpa6130a2_client = NULL; |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static const struct i2c_device_id tpa6130a2_id[] = { |
Peter Ujfalusi | 0744100 | 2011-08-30 14:39:52 +0300 | [diff] [blame] | 471 | { "tpa6130a2", TPA6130A2 }, |
| 472 | { "tpa6140a2", TPA6140A2 }, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 473 | { } |
| 474 | }; |
| 475 | MODULE_DEVICE_TABLE(i2c, tpa6130a2_id); |
| 476 | |
Sebastian Reichel | f95a488 | 2013-10-23 14:03:28 +0200 | [diff] [blame] | 477 | #if IS_ENABLED(CONFIG_OF) |
| 478 | static const struct of_device_id tpa6130a2_of_match[] = { |
| 479 | { .compatible = "ti,tpa6130a2", }, |
| 480 | { .compatible = "ti,tpa6140a2" }, |
| 481 | {}, |
| 482 | }; |
| 483 | MODULE_DEVICE_TABLE(of, tpa6130a2_of_match); |
| 484 | #endif |
| 485 | |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 486 | static struct i2c_driver tpa6130a2_i2c_driver = { |
| 487 | .driver = { |
| 488 | .name = "tpa6130a2", |
Sebastian Reichel | f95a488 | 2013-10-23 14:03:28 +0200 | [diff] [blame] | 489 | .of_match_table = of_match_ptr(tpa6130a2_of_match), |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 490 | }, |
| 491 | .probe = tpa6130a2_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 492 | .remove = tpa6130a2_remove, |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 493 | .id_table = tpa6130a2_id, |
| 494 | }; |
| 495 | |
Sachin Kamat | f062e2b | 2012-08-06 17:25:38 +0530 | [diff] [blame] | 496 | module_i2c_driver(tpa6130a2_i2c_driver); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 497 | |
Peter Ujfalusi | b4079ef | 2011-05-03 18:12:41 +0300 | [diff] [blame] | 498 | MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>"); |
Peter Ujfalusi | 493b67e | 2009-10-09 15:55:41 +0300 | [diff] [blame] | 499 | MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver"); |
| 500 | MODULE_LICENSE("GPL"); |