Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[cascardo/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@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                         return val;
84                 }
85         }
86
87         /* Either powered on or off, we save the context */
88         data->regs[reg] = value;
89
90         return val;
91 }
92
93 static 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
103 static int tpa6130a2_initialize(void)
104 {
105         struct tpa6130a2_data *data;
106         int i, ret = 0;
107
108         BUG_ON(tpa6130a2_client == NULL);
109         data = i2c_get_clientdata(tpa6130a2_client);
110
111         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;
118 }
119
120 static int tpa6130a2_power(int power)
121 {
122         struct  tpa6130a2_data *data;
123         u8      val;
124         int     ret = 0;
125
126         BUG_ON(tpa6130a2_client == NULL);
127         data = i2c_get_clientdata(tpa6130a2_client);
128
129         mutex_lock(&data->mutex);
130         if (power && !data->power_state) {
131                 /* Power on */
132                 if (data->power_gpio >= 0)
133                         gpio_set_value(data->power_gpio, 1);
134
135                 ret = regulator_enable(data->supply);
136                 if (ret != 0) {
137                         dev_err(&tpa6130a2_client->dev,
138                                 "Failed to enable supply: %d\n", ret);
139                         goto exit;
140                 }
141
142                 data->power_state = 1;
143                 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                 }
153
154                 /* Clear SWS */
155                 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
156                 val &= ~TPA6130A2_SWS;
157                 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
158         } else if (!power && data->power_state) {
159                 /* set SWS */
160                 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
161                 val |= TPA6130A2_SWS;
162                 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
163
164                 /* Power off */
165                 if (data->power_gpio >= 0)
166                         gpio_set_value(data->power_gpio, 0);
167
168                 ret = regulator_disable(data->supply);
169                 if (ret != 0) {
170                         dev_err(&tpa6130a2_client->dev,
171                                 "Failed to disable supply: %d\n", ret);
172                         goto exit;
173                 }
174
175                 data->power_state = 0;
176         }
177
178 exit:
179         mutex_unlock(&data->mutex);
180         return ret;
181 }
182
183 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
184                 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;
191         int max = mc->max;
192         unsigned int mask = (1 << fls(max)) - 1;
193         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] =
205                         max - ucontrol->value.integer.value[0];
206
207         mutex_unlock(&data->mutex);
208         return 0;
209 }
210
211 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
212                 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;
219         int max = mc->max;
220         unsigned int mask = (1 << fls(max)) - 1;
221         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)
229                 val = max - val;
230
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  */
252 static 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
266 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
267         SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
268                        TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
269                        tpa6130a2_get_volsw, tpa6130a2_put_volsw,
270                        tpa6130_tlv),
271 };
272
273 static 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
280 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
281         SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
282                        TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
283                        tpa6130a2_get_volsw, tpa6130a2_put_volsw,
284                        tpa6140_tlv),
285 };
286
287 /*
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  */
294 static void tpa6130a2_channel_enable(u8 channel, int enable)
295 {
296         u8      val;
297
298         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
323 static 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
337 static 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
351 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
352                 struct snd_kcontrol *kcontrol, int event)
353 {
354         int ret = 0;
355
356         switch (event) {
357         case SND_SOC_DAPM_POST_PMU:
358                 ret = tpa6130a2_power(1);
359                 break;
360         case SND_SOC_DAPM_POST_PMD:
361                 ret = tpa6130a2_power(0);
362                 break;
363         }
364         return ret;
365 }
366
367 static 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 */
378         SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
379         SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
380 };
381
382 static 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
390 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
391 {
392         struct  tpa6130a2_data *data;
393
394         if (tpa6130a2_client == NULL)
395                 return -ENODEV;
396
397         data = i2c_get_clientdata(tpa6130a2_client);
398
399         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
404         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));
410
411 }
412 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
413
414 static int __devinit tpa6130a2_probe(struct i2c_client *client,
415                                      const struct i2c_device_id *id)
416 {
417         struct device *dev;
418         struct tpa6130a2_data *data;
419         struct tpa6130a2_platform_data *pdata;
420         const char *regulator;
421         int ret;
422
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
441         pdata = client->dev.platform_data;
442         data->power_gpio = pdata->power_gpio;
443         data->id = pdata->id;
444
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);
457                         goto err_gpio;
458                 }
459                 gpio_direction_output(data->power_gpio, 0);
460         }
461
462         switch (data->id) {
463         default:
464                 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
465                          pdata->id);
466         case TPA6130A2:
467                 regulator = "Vdd";
468                 break;
469         case TPA6140A2:
470                 regulator = "AVdd";
471                 break;
472         }
473
474         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);
478                 goto err_regulator;
479         }
480
481         ret = tpa6130a2_power(1);
482         if (ret != 0)
483                 goto err_power;
484
485
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 */
493         ret = tpa6130a2_power(0);
494         if (ret != 0)
495                 goto err_power;
496
497         return 0;
498
499 err_power:
500         regulator_put(data->supply);
501 err_regulator:
502         if (data->power_gpio >= 0)
503                 gpio_free(data->power_gpio);
504 err_gpio:
505         kfree(data);
506         i2c_set_clientdata(tpa6130a2_client, NULL);
507         tpa6130a2_client = NULL;
508
509         return ret;
510 }
511
512 static int __devexit tpa6130a2_remove(struct i2c_client *client)
513 {
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);
520
521         regulator_put(data->supply);
522
523         kfree(data);
524         tpa6130a2_client = NULL;
525
526         return 0;
527 }
528
529 static const struct i2c_device_id tpa6130a2_id[] = {
530         { "tpa6130a2", 0 },
531         { }
532 };
533 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
534
535 static 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
545 static int __init tpa6130a2_init(void)
546 {
547         return i2c_add_driver(&tpa6130a2_i2c_driver);
548 }
549
550 static void __exit tpa6130a2_exit(void)
551 {
552         i2c_del_driver(&tpa6130a2_i2c_driver);
553 }
554
555 MODULE_AUTHOR("Peter Ujfalusi");
556 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
557 MODULE_LICENSE("GPL");
558
559 module_init(tpa6130a2_init);
560 module_exit(tpa6130a2_exit);