blob: 6fac9e034c483861dc82b3e0468b9bbb362f562d [file] [log] [blame]
Peter Ujfalusi493b67e2009-10-09 15:55:41 +03001/*
2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
3 *
4 * Copyright (C) Nokia Corporation
5 *
Peter Ujfalusib4079ef2011-05-03 18:12:41 +03006 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +03007 *
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 Koskinen7c4e6492009-12-09 12:05:50 +020028#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030030#include <sound/tpa6130a2-plat.h>
31#include <sound/soc.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030032#include <sound/tlv.h>
Sachin Kamatee5e4532014-04-04 11:29:13 +053033#include <linux/of.h>
Sebastian Reichelf95a4882013-10-23 14:03:28 +020034#include <linux/of_gpio.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030035
36#include "tpa6130a2.h"
37
Peter Ujfalusi0722d052011-08-30 14:39:54 +030038enum tpa_model {
39 TPA6130A2,
40 TPA6140A2,
41};
42
Mark Brownebab1b12009-10-09 19:13:47 +010043static struct i2c_client *tpa6130a2_client;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030044
45/* This struct is used to save the context */
46struct tpa6130a2_data {
47 struct mutex mutex;
48 unsigned char regs[TPA6130A2_CACHEREGNUM];
Jarkko Nikulaad8332c2010-05-19 10:52:28 +030049 struct regulator *supply;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030050 int power_gpio;
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020051 u8 power_state:1;
Peter Ujfalusie5e5b312010-05-04 11:08:18 +030052 enum tpa_model id;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030053};
54
55static int tpa6130a2_i2c_read(int reg)
56{
57 struct tpa6130a2_data *data;
58 int val;
59
Takashi Iwai773392b2013-11-05 18:39:51 +010060 if (WARN_ON(!tpa6130a2_client))
61 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030062 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
78static int tpa6130a2_i2c_write(int reg, u8 value)
79{
80 struct tpa6130a2_data *data;
81 int val = 0;
82
Takashi Iwai773392b2013-11-05 18:39:51 +010083 if (WARN_ON(!tpa6130a2_client))
84 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030085 data = i2c_get_clientdata(tpa6130a2_client);
86
87 if (data->power_state) {
88 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
Axel Lin7a479b02010-11-23 14:14:07 +080089 if (val < 0) {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030090 dev_err(&tpa6130a2_client->dev, "Write failed\n");
Axel Lin7a479b02010-11-23 14:14:07 +080091 return val;
92 }
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030093 }
94
95 /* Either powered on or off, we save the context */
96 data->regs[reg] = value;
97
98 return val;
99}
100
101static u8 tpa6130a2_read(int reg)
102{
103 struct tpa6130a2_data *data;
104
Takashi Iwai773392b2013-11-05 18:39:51 +0100105 if (WARN_ON(!tpa6130a2_client))
106 return 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300107 data = i2c_get_clientdata(tpa6130a2_client);
108
109 return data->regs[reg];
110}
111
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300112static int tpa6130a2_initialize(void)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300113{
114 struct tpa6130a2_data *data;
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300115 int i, ret = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300116
Takashi Iwai773392b2013-11-05 18:39:51 +0100117 if (WARN_ON(!tpa6130a2_client))
118 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300119 data = i2c_get_clientdata(tpa6130a2_client);
120
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300121 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 Ujfalusi493b67e2009-10-09 15:55:41 +0300128}
129
Peter Ujfalusid5876ce2010-11-30 16:00:00 +0200130static int tpa6130a2_power(u8 power)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300131{
132 struct tpa6130a2_data *data;
133 u8 val;
Jarkko Nikula75e3f312010-11-03 16:39:00 +0200134 int ret = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300135
Takashi Iwai773392b2013-11-05 18:39:51 +0100136 if (WARN_ON(!tpa6130a2_client))
137 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300138 data = i2c_get_clientdata(tpa6130a2_client);
139
140 mutex_lock(&data->mutex);
Peter Ujfalusid5876ce2010-11-30 16:00:00 +0200141 if (power == data->power_state)
142 goto exit;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200143
Peter Ujfalusid5876ce2010-11-30 16:00:00 +0200144 if (power) {
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300145 ret = regulator_enable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200146 if (ret != 0) {
147 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300148 "Failed to enable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200149 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300150 }
Jarkko Nikula0656f6c2010-11-08 10:57:57 +0200151 /* Power on */
152 if (data->power_gpio >= 0)
153 gpio_set_value(data->power_gpio, 1);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200154
155 data->power_state = 1;
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300156 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 Ujfalusid5876ce2010-11-30 16:00:00 +0200166 } else {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300167 /* set SWS */
168 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
169 val |= TPA6130A2_SWS;
170 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200171
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300172 /* Power off */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200173 if (data->power_gpio >= 0)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300174 gpio_set_value(data->power_gpio, 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200175
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300176 ret = regulator_disable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200177 if (ret != 0) {
178 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300179 "Failed to disable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200180 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300181 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200182
183 data->power_state = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300184 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200185
186exit:
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300187 mutex_unlock(&data->mutex);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200188 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300189}
190
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300191static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300192 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300199 int max = mc->max;
200 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300201 unsigned int invert = mc->invert;
202
Takashi Iwai773392b2013-11-05 18:39:51 +0100203 if (WARN_ON(!tpa6130a2_client))
204 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300205 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300214 max - ucontrol->value.integer.value[0];
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300215
216 mutex_unlock(&data->mutex);
217 return 0;
218}
219
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300220static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300221 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300228 int max = mc->max;
229 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300230 unsigned int invert = mc->invert;
231 unsigned int val = (ucontrol->value.integer.value[0] & mask);
232 unsigned int val_reg;
233
Takashi Iwai773392b2013-11-05 18:39:51 +0100234 if (WARN_ON(!tpa6130a2_client))
235 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300236 data = i2c_get_clientdata(tpa6130a2_client);
237
238 if (invert)
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300239 val = max - val;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300240
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 */
262static const unsigned int tpa6130_tlv[] = {
263 TLV_DB_RANGE_HEAD(10),
264 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
265 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
266 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
267 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
268 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
269 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
270 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
271 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
272 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
273 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
274};
275
276static const struct snd_kcontrol_new tpa6130a2_controls[] = {
277 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
278 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300279 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300280 tpa6130_tlv),
281};
282
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300283static const unsigned int tpa6140_tlv[] = {
284 TLV_DB_RANGE_HEAD(3),
285 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
286 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
287 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
288};
289
290static const struct snd_kcontrol_new tpa6140a2_controls[] = {
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300291 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
292 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300293 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300294 tpa6140_tlv),
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300295};
296
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300297/*
298 * Enable or disable channel (left or right)
299 * The bit number for mute and amplifier are the same per channel:
300 * bit 6: Right channel
301 * bit 7: Left channel
302 * in both registers.
303 */
304static void tpa6130a2_channel_enable(u8 channel, int enable)
305{
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300306 u8 val;
307
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300308 if (enable) {
309 /* Enable channel */
310 /* Enable amplifier */
311 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
312 val |= channel;
Peter Ujfalusid534bac2010-11-30 16:00:01 +0200313 val &= ~TPA6130A2_SWS;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300314 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
315
316 /* Unmute channel */
317 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
318 val &= ~channel;
319 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
320 } else {
321 /* Disable channel */
322 /* Mute channel */
323 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
324 val |= channel;
325 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
326
327 /* Disable amplifier */
328 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
329 val &= ~channel;
330 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
331 }
332}
333
Peter Ujfalusi39646872010-12-02 09:29:56 +0200334int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300335{
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200336 int ret = 0;
Peter Ujfalusi39646872010-12-02 09:29:56 +0200337 if (enable) {
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200338 ret = tpa6130a2_power(1);
Peter Ujfalusi39646872010-12-02 09:29:56 +0200339 if (ret < 0)
340 return ret;
341 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
342 1);
343 } else {
344 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
345 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200346 ret = tpa6130a2_power(0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300347 }
Peter Ujfalusi39646872010-12-02 09:29:56 +0200348
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200349 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300350}
Peter Ujfalusi39646872010-12-02 09:29:56 +0200351EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300352
353int tpa6130a2_add_controls(struct snd_soc_codec *codec)
354{
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300355 struct tpa6130a2_data *data;
356
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300357 if (tpa6130a2_client == NULL)
358 return -ENODEV;
359
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300360 data = i2c_get_clientdata(tpa6130a2_client);
361
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300362 if (data->id == TPA6140A2)
Liam Girdwood022658b2012-02-03 17:43:09 +0000363 return snd_soc_add_codec_controls(codec, tpa6140a2_controls,
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300364 ARRAY_SIZE(tpa6140a2_controls));
365 else
Liam Girdwood022658b2012-02-03 17:43:09 +0000366 return snd_soc_add_codec_controls(codec, tpa6130a2_controls,
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300367 ARRAY_SIZE(tpa6130a2_controls));
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300368}
369EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
370
Bill Pemberton7a79e942012-12-07 09:26:37 -0500371static int tpa6130a2_probe(struct i2c_client *client,
372 const struct i2c_device_id *id)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300373{
374 struct device *dev;
375 struct tpa6130a2_data *data;
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200376 struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
377 struct device_node *np = client->dev.of_node;
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300378 const char *regulator;
379 int ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300380
381 dev = &client->dev;
382
Axel Lin6945e9f2011-12-29 12:12:29 +0800383 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Sachin Kamat656e3432014-06-20 15:29:02 +0530384 if (!data)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300385 return -ENOMEM;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300386
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200387 if (pdata) {
388 data->power_gpio = pdata->power_gpio;
389 } else if (np) {
390 data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
391 } else {
392 dev_err(dev, "Platform data not set\n");
393 dump_stack();
394 return -ENODEV;
395 }
396
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300397 tpa6130a2_client = client;
398
399 i2c_set_clientdata(tpa6130a2_client, data);
400
Peter Ujfalusi07441002011-08-30 14:39:52 +0300401 data->id = id->driver_data;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300402
403 mutex_init(&data->mutex);
404
405 /* Set default register values */
406 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
407 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
408 TPA6130A2_MUTE_L;
409
410 if (data->power_gpio >= 0) {
Sachin Kamatd06080c2012-12-07 16:32:26 +0530411 ret = devm_gpio_request(dev, data->power_gpio,
412 "tpa6130a2 enable");
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300413 if (ret < 0) {
414 dev_err(dev, "Failed to request power GPIO (%d)\n",
415 data->power_gpio);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200416 goto err_gpio;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300417 }
418 gpio_direction_output(data->power_gpio, 0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300419 }
420
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300421 switch (data->id) {
Ilkka Koskinen21383012010-01-08 17:48:31 +0200422 default:
423 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
Peter Ujfalusi07441002011-08-30 14:39:52 +0300424 data->id);
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300425 case TPA6130A2:
426 regulator = "Vdd";
427 break;
428 case TPA6140A2:
429 regulator = "AVdd";
430 break;
Ilkka Koskinen21383012010-01-08 17:48:31 +0200431 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200432
Sachin Kamatd06080c2012-12-07 16:32:26 +0530433 data->supply = devm_regulator_get(dev, regulator);
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300434 if (IS_ERR(data->supply)) {
435 ret = PTR_ERR(data->supply);
436 dev_err(dev, "Failed to request supply: %d\n", ret);
Sachin Kamatd06080c2012-12-07 16:32:26 +0530437 goto err_gpio;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200438 }
439
440 ret = tpa6130a2_power(1);
441 if (ret != 0)
Sachin Kamatd06080c2012-12-07 16:32:26 +0530442 goto err_gpio;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200443
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300444
445 /* Read version */
446 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
447 TPA6130A2_VERSION_MASK;
448 if ((ret != 1) && (ret != 2))
449 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
450
451 /* Disable the chip */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200452 ret = tpa6130a2_power(0);
453 if (ret != 0)
Sachin Kamatd06080c2012-12-07 16:32:26 +0530454 goto err_gpio;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300455
456 return 0;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200457
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200458err_gpio:
Mark Brownebab1b12009-10-09 19:13:47 +0100459 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300460
461 return ret;
462}
463
Bill Pemberton7a79e942012-12-07 09:26:37 -0500464static int tpa6130a2_remove(struct i2c_client *client)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300465{
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300466 tpa6130a2_power(0);
Mark Brownebab1b12009-10-09 19:13:47 +0100467 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300468
469 return 0;
470}
471
472static const struct i2c_device_id tpa6130a2_id[] = {
Peter Ujfalusi07441002011-08-30 14:39:52 +0300473 { "tpa6130a2", TPA6130A2 },
474 { "tpa6140a2", TPA6140A2 },
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300475 { }
476};
477MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
478
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200479#if IS_ENABLED(CONFIG_OF)
480static const struct of_device_id tpa6130a2_of_match[] = {
481 { .compatible = "ti,tpa6130a2", },
482 { .compatible = "ti,tpa6140a2" },
483 {},
484};
485MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
486#endif
487
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300488static struct i2c_driver tpa6130a2_i2c_driver = {
489 .driver = {
490 .name = "tpa6130a2",
491 .owner = THIS_MODULE,
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200492 .of_match_table = of_match_ptr(tpa6130a2_of_match),
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300493 },
494 .probe = tpa6130a2_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500495 .remove = tpa6130a2_remove,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300496 .id_table = tpa6130a2_id,
497};
498
Sachin Kamatf062e2b2012-08-06 17:25:38 +0530499module_i2c_driver(tpa6130a2_i2c_driver);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300500
Peter Ujfalusib4079ef2011-05-03 18:12:41 +0300501MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300502MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
503MODULE_LICENSE("GPL");