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