blob: d2c24309567303667dfcae14d79a6809e4ce1e7b [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 *
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 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>
32#include <sound/soc-dapm.h>
33#include <sound/tlv.h>
34
35#include "tpa6130a2.h"
36
Mark Brownebab1b12009-10-09 19:13:47 +010037static struct i2c_client *tpa6130a2_client;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030038
39/* This struct is used to save the context */
40struct tpa6130a2_data {
41 struct mutex mutex;
42 unsigned char regs[TPA6130A2_CACHEREGNUM];
Jarkko Nikulaad8332c2010-05-19 10:52:28 +030043 struct regulator *supply;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030044 int power_gpio;
45 unsigned char power_state;
Peter Ujfalusie5e5b312010-05-04 11:08:18 +030046 enum tpa_model id;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030047};
48
49static 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
71static 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 Lin7a479b02010-11-23 14:14:07 +080081 if (val < 0) {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030082 dev_err(&tpa6130a2_client->dev, "Write failed\n");
Axel Lin7a479b02010-11-23 14:14:07 +080083 return val;
84 }
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030085 }
86
87 /* Either powered on or off, we save the context */
88 data->regs[reg] = value;
89
90 return val;
91}
92
93static 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 Ujfalusi872a64d2010-10-21 15:03:03 +0300103static int tpa6130a2_initialize(void)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300104{
105 struct tpa6130a2_data *data;
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300106 int i, ret = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300107
108 BUG_ON(tpa6130a2_client == NULL);
109 data = i2c_get_clientdata(tpa6130a2_client);
110
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300111 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 Ujfalusi493b67e2009-10-09 15:55:41 +0300118}
119
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200120static int tpa6130a2_power(int power)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300121{
122 struct tpa6130a2_data *data;
123 u8 val;
Jarkko Nikula75e3f312010-11-03 16:39:00 +0200124 int ret = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300125
126 BUG_ON(tpa6130a2_client == NULL);
127 data = i2c_get_clientdata(tpa6130a2_client);
128
129 mutex_lock(&data->mutex);
Jarkko Nikula63f75262010-10-28 14:05:40 +0300130 if (power && !data->power_state) {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300131 /* Power on */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200132 if (data->power_gpio >= 0)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300133 gpio_set_value(data->power_gpio, 1);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200134
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300135 ret = regulator_enable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200136 if (ret != 0) {
137 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300138 "Failed to enable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200139 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300140 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200141
142 data->power_state = 1;
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300143 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 Koskinen7c4e6492009-12-09 12:05:50 +0200153
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300154 /* Clear SWS */
155 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
156 val &= ~TPA6130A2_SWS;
157 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
Jarkko Nikula63f75262010-10-28 14:05:40 +0300158 } else if (!power && data->power_state) {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300159 /* set SWS */
160 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
161 val |= TPA6130A2_SWS;
162 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200163
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300164 /* Power off */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200165 if (data->power_gpio >= 0)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300166 gpio_set_value(data->power_gpio, 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200167
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300168 ret = regulator_disable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200169 if (ret != 0) {
170 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300171 "Failed to disable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200172 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300173 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200174
175 data->power_state = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300176 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200177
178exit:
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300179 mutex_unlock(&data->mutex);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200180 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300181}
182
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300183static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300184 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300191 int max = mc->max;
192 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300193 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300205 max - ucontrol->value.integer.value[0];
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300206
207 mutex_unlock(&data->mutex);
208 return 0;
209}
210
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300211static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300212 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300219 int max = mc->max;
220 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300221 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300229 val = max - val;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300230
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 */
252static 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
266static 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 Ujfalusibd843ed2010-05-07 14:24:11 +0300269 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300270 tpa6130_tlv),
271};
272
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300273static 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
280static const struct snd_kcontrol_new tpa6140a2_controls[] = {
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300281 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
282 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300283 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300284 tpa6140_tlv),
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300285};
286
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300287/*
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 */
294static void tpa6130a2_channel_enable(u8 channel, int enable)
295{
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300296 u8 val;
297
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300298 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
323static 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
337static 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
351static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
352 struct snd_kcontrol *kcontrol, int event)
353{
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200354 int ret = 0;
355
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300356 switch (event) {
357 case SND_SOC_DAPM_POST_PMU:
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200358 ret = tpa6130a2_power(1);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300359 break;
360 case SND_SOC_DAPM_POST_PMD:
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200361 ret = tpa6130a2_power(0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300362 break;
363 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200364 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300365}
366
367static 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 Nikula266d38c2010-05-19 13:55:26 +0300378 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
379 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300380};
381
382static 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
390int tpa6130a2_add_controls(struct snd_soc_codec *codec)
391{
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300392 struct tpa6130a2_data *data;
393
Peter Ujfalusi872a64d2010-10-21 15:03:03 +0300394 if (tpa6130a2_client == NULL)
395 return -ENODEV;
396
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300397 data = i2c_get_clientdata(tpa6130a2_client);
398
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300399 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 Ujfalusie5e5b312010-05-04 11:08:18 +0300404 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 Ujfalusi493b67e2009-10-09 15:55:41 +0300410
411}
412EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
413
Mark Brown735fe4c2010-01-12 14:13:00 +0000414static int __devinit tpa6130a2_probe(struct i2c_client *client,
415 const struct i2c_device_id *id)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300416{
417 struct device *dev;
418 struct tpa6130a2_data *data;
419 struct tpa6130a2_platform_data *pdata;
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300420 const char *regulator;
421 int ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300422
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 Brownebab1b12009-10-09 19:13:47 +0100441 pdata = client->dev.platform_data;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300442 data->power_gpio = pdata->power_gpio;
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300443 data->id = pdata->id;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300444
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 Koskinen7c4e6492009-12-09 12:05:50 +0200457 goto err_gpio;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300458 }
459 gpio_direction_output(data->power_gpio, 0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300460 }
461
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300462 switch (data->id) {
Ilkka Koskinen21383012010-01-08 17:48:31 +0200463 default:
464 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
465 pdata->id);
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300466 case TPA6130A2:
467 regulator = "Vdd";
468 break;
469 case TPA6140A2:
470 regulator = "AVdd";
471 break;
Ilkka Koskinen21383012010-01-08 17:48:31 +0200472 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200473
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300474 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 Koskinen7c4e6492009-12-09 12:05:50 +0200478 goto err_regulator;
479 }
480
481 ret = tpa6130a2_power(1);
482 if (ret != 0)
483 goto err_power;
484
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300485
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 Koskinen7c4e6492009-12-09 12:05:50 +0200493 ret = tpa6130a2_power(0);
494 if (ret != 0)
495 goto err_power;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300496
497 return 0;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200498
499err_power:
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300500 regulator_put(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200501err_regulator:
502 if (data->power_gpio >= 0)
503 gpio_free(data->power_gpio);
504err_gpio:
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300505 kfree(data);
506 i2c_set_clientdata(tpa6130a2_client, NULL);
Mark Brownebab1b12009-10-09 19:13:47 +0100507 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300508
509 return ret;
510}
511
Mark Brown735fe4c2010-01-12 14:13:00 +0000512static int __devexit tpa6130a2_remove(struct i2c_client *client)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300513{
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 Koskinen7c4e6492009-12-09 12:05:50 +0200520
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300521 regulator_put(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200522
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300523 kfree(data);
Mark Brownebab1b12009-10-09 19:13:47 +0100524 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300525
526 return 0;
527}
528
529static const struct i2c_device_id tpa6130a2_id[] = {
530 { "tpa6130a2", 0 },
531 { }
532};
533MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
534
535static 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
545static int __init tpa6130a2_init(void)
546{
547 return i2c_add_driver(&tpa6130a2_i2c_driver);
548}
549
550static void __exit tpa6130a2_exit(void)
551{
552 i2c_del_driver(&tpa6130a2_i2c_driver);
553}
554
555MODULE_AUTHOR("Peter Ujfalusi");
556MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
557MODULE_LICENSE("GPL");
558
559module_init(tpa6130a2_init);
560module_exit(tpa6130a2_exit);