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