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