mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / sound / soc / codecs / tpa6130a2.c
blobc58bee8346cef2223e4b0cb5b15cbc76991ccca5
1 /*
2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
4 * Copyright (C) Nokia Corporation
6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
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.
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.
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
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>
34 #include "tpa6130a2.h"
36 enum tpa_model {
37 TPA6130A2,
38 TPA6140A2,
41 static struct i2c_client *tpa6130a2_client;
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;
53 static int tpa6130a2_i2c_read(int reg)
55 struct tpa6130a2_data *data;
56 int val;
58 BUG_ON(tpa6130a2_client == NULL);
59 data = i2c_get_clientdata(tpa6130a2_client);
61 /* If powered off, return the cached value */
62 if (data->power_state) {
63 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
64 if (val < 0)
65 dev_err(&tpa6130a2_client->dev, "Read failed\n");
66 else
67 data->regs[reg] = val;
68 } else {
69 val = data->regs[reg];
72 return val;
75 static int tpa6130a2_i2c_write(int reg, u8 value)
77 struct tpa6130a2_data *data;
78 int val = 0;
80 BUG_ON(tpa6130a2_client == NULL);
81 data = i2c_get_clientdata(tpa6130a2_client);
83 if (data->power_state) {
84 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
85 if (val < 0) {
86 dev_err(&tpa6130a2_client->dev, "Write failed\n");
87 return val;
91 /* Either powered on or off, we save the context */
92 data->regs[reg] = value;
94 return val;
97 static u8 tpa6130a2_read(int reg)
99 struct tpa6130a2_data *data;
101 BUG_ON(tpa6130a2_client == NULL);
102 data = i2c_get_clientdata(tpa6130a2_client);
104 return data->regs[reg];
107 static int tpa6130a2_initialize(void)
109 struct tpa6130a2_data *data;
110 int i, ret = 0;
112 BUG_ON(tpa6130a2_client == NULL);
113 data = i2c_get_clientdata(tpa6130a2_client);
115 for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
116 ret = tpa6130a2_i2c_write(i, data->regs[i]);
117 if (ret < 0)
118 break;
121 return ret;
124 static int tpa6130a2_power(u8 power)
126 struct tpa6130a2_data *data;
127 u8 val;
128 int ret = 0;
130 BUG_ON(tpa6130a2_client == NULL);
131 data = i2c_get_clientdata(tpa6130a2_client);
133 mutex_lock(&data->mutex);
134 if (power == data->power_state)
135 goto exit;
137 if (power) {
138 ret = regulator_enable(data->supply);
139 if (ret != 0) {
140 dev_err(&tpa6130a2_client->dev,
141 "Failed to enable supply: %d\n", ret);
142 goto exit;
144 /* Power on */
145 if (data->power_gpio >= 0)
146 gpio_set_value(data->power_gpio, 1);
148 data->power_state = 1;
149 ret = tpa6130a2_initialize();
150 if (ret < 0) {
151 dev_err(&tpa6130a2_client->dev,
152 "Failed to initialize chip\n");
153 if (data->power_gpio >= 0)
154 gpio_set_value(data->power_gpio, 0);
155 regulator_disable(data->supply);
156 data->power_state = 0;
157 goto exit;
159 } else {
160 /* set SWS */
161 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
162 val |= TPA6130A2_SWS;
163 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
165 /* Power off */
166 if (data->power_gpio >= 0)
167 gpio_set_value(data->power_gpio, 0);
169 ret = regulator_disable(data->supply);
170 if (ret != 0) {
171 dev_err(&tpa6130a2_client->dev,
172 "Failed to disable supply: %d\n", ret);
173 goto exit;
176 data->power_state = 0;
179 exit:
180 mutex_unlock(&data->mutex);
181 return ret;
184 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
187 struct soc_mixer_control *mc =
188 (struct soc_mixer_control *)kcontrol->private_value;
189 struct tpa6130a2_data *data;
190 unsigned int reg = mc->reg;
191 unsigned int shift = mc->shift;
192 int max = mc->max;
193 unsigned int mask = (1 << fls(max)) - 1;
194 unsigned int invert = mc->invert;
196 BUG_ON(tpa6130a2_client == NULL);
197 data = i2c_get_clientdata(tpa6130a2_client);
199 mutex_lock(&data->mutex);
201 ucontrol->value.integer.value[0] =
202 (tpa6130a2_read(reg) >> shift) & mask;
204 if (invert)
205 ucontrol->value.integer.value[0] =
206 max - ucontrol->value.integer.value[0];
208 mutex_unlock(&data->mutex);
209 return 0;
212 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
215 struct soc_mixer_control *mc =
216 (struct soc_mixer_control *)kcontrol->private_value;
217 struct tpa6130a2_data *data;
218 unsigned int reg = mc->reg;
219 unsigned int shift = mc->shift;
220 int max = mc->max;
221 unsigned int mask = (1 << fls(max)) - 1;
222 unsigned int invert = mc->invert;
223 unsigned int val = (ucontrol->value.integer.value[0] & mask);
224 unsigned int val_reg;
226 BUG_ON(tpa6130a2_client == NULL);
227 data = i2c_get_clientdata(tpa6130a2_client);
229 if (invert)
230 val = max - val;
232 mutex_lock(&data->mutex);
234 val_reg = tpa6130a2_read(reg);
235 if (((val_reg >> shift) & mask) == val) {
236 mutex_unlock(&data->mutex);
237 return 0;
240 val_reg &= ~(mask << shift);
241 val_reg |= val << shift;
242 tpa6130a2_i2c_write(reg, val_reg);
244 mutex_unlock(&data->mutex);
246 return 1;
250 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
251 * down in gain.
253 static const unsigned int tpa6130_tlv[] = {
254 TLV_DB_RANGE_HEAD(10),
255 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
256 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
257 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
258 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
259 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
260 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
261 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
262 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
263 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
264 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
267 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
268 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
269 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
270 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
271 tpa6130_tlv),
274 static const unsigned int tpa6140_tlv[] = {
275 TLV_DB_RANGE_HEAD(3),
276 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
277 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
278 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
281 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
282 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
283 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
284 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
285 tpa6140_tlv),
289 * Enable or disable channel (left or right)
290 * The bit number for mute and amplifier are the same per channel:
291 * bit 6: Right channel
292 * bit 7: Left channel
293 * in both registers.
295 static void tpa6130a2_channel_enable(u8 channel, int enable)
297 u8 val;
299 if (enable) {
300 /* Enable channel */
301 /* Enable amplifier */
302 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
303 val |= channel;
304 val &= ~TPA6130A2_SWS;
305 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
307 /* Unmute channel */
308 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
309 val &= ~channel;
310 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
311 } else {
312 /* Disable channel */
313 /* Mute channel */
314 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
315 val |= channel;
316 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
318 /* Disable amplifier */
319 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
320 val &= ~channel;
321 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
325 int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
327 int ret = 0;
328 if (enable) {
329 ret = tpa6130a2_power(1);
330 if (ret < 0)
331 return ret;
332 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
334 } else {
335 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
337 ret = tpa6130a2_power(0);
340 return ret;
342 EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
344 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
346 struct tpa6130a2_data *data;
348 if (tpa6130a2_client == NULL)
349 return -ENODEV;
351 data = i2c_get_clientdata(tpa6130a2_client);
353 if (data->id == TPA6140A2)
354 return snd_soc_add_codec_controls(codec, tpa6140a2_controls,
355 ARRAY_SIZE(tpa6140a2_controls));
356 else
357 return snd_soc_add_codec_controls(codec, tpa6130a2_controls,
358 ARRAY_SIZE(tpa6130a2_controls));
360 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
362 static int tpa6130a2_probe(struct i2c_client *client,
363 const struct i2c_device_id *id)
365 struct device *dev;
366 struct tpa6130a2_data *data;
367 struct tpa6130a2_platform_data *pdata;
368 const char *regulator;
369 int ret;
371 dev = &client->dev;
373 if (client->dev.platform_data == NULL) {
374 dev_err(dev, "Platform data not set\n");
375 dump_stack();
376 return -ENODEV;
379 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
380 if (data == NULL) {
381 dev_err(dev, "Can not allocate memory\n");
382 return -ENOMEM;
385 tpa6130a2_client = client;
387 i2c_set_clientdata(tpa6130a2_client, data);
389 pdata = client->dev.platform_data;
390 data->power_gpio = pdata->power_gpio;
391 data->id = id->driver_data;
393 mutex_init(&data->mutex);
395 /* Set default register values */
396 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
397 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
398 TPA6130A2_MUTE_L;
400 if (data->power_gpio >= 0) {
401 ret = devm_gpio_request(dev, data->power_gpio,
402 "tpa6130a2 enable");
403 if (ret < 0) {
404 dev_err(dev, "Failed to request power GPIO (%d)\n",
405 data->power_gpio);
406 goto err_gpio;
408 gpio_direction_output(data->power_gpio, 0);
411 switch (data->id) {
412 default:
413 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
414 data->id);
415 case TPA6130A2:
416 regulator = "Vdd";
417 break;
418 case TPA6140A2:
419 regulator = "AVdd";
420 break;
423 data->supply = devm_regulator_get(dev, regulator);
424 if (IS_ERR(data->supply)) {
425 ret = PTR_ERR(data->supply);
426 dev_err(dev, "Failed to request supply: %d\n", ret);
427 goto err_gpio;
430 ret = tpa6130a2_power(1);
431 if (ret != 0)
432 goto err_gpio;
435 /* Read version */
436 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
437 TPA6130A2_VERSION_MASK;
438 if ((ret != 1) && (ret != 2))
439 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
441 /* Disable the chip */
442 ret = tpa6130a2_power(0);
443 if (ret != 0)
444 goto err_gpio;
446 return 0;
448 err_gpio:
449 tpa6130a2_client = NULL;
451 return ret;
454 static int tpa6130a2_remove(struct i2c_client *client)
456 tpa6130a2_power(0);
457 tpa6130a2_client = NULL;
459 return 0;
462 static const struct i2c_device_id tpa6130a2_id[] = {
463 { "tpa6130a2", TPA6130A2 },
464 { "tpa6140a2", TPA6140A2 },
467 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
469 static struct i2c_driver tpa6130a2_i2c_driver = {
470 .driver = {
471 .name = "tpa6130a2",
472 .owner = THIS_MODULE,
474 .probe = tpa6130a2_probe,
475 .remove = tpa6130a2_remove,
476 .id_table = tpa6130a2_id,
479 module_i2c_driver(tpa6130a2_i2c_driver);
481 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
482 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
483 MODULE_LICENSE("GPL");