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