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