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