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