1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // tas2781-lib.c -- TAS2781 Common functions for HDA and ASoC Audio drivers 4 // 5 // Copyright 2023 Texas Instruments, Inc. 6 // 7 // Author: Shenghao Ding <shenghao-ding@ti.com> 8 9 #include <linux/crc8.h> 10 #include <linux/firmware.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/i2c.h> 13 #include <linux/init.h> 14 #include <linux/interrupt.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_irq.h> 18 #include <linux/regmap.h> 19 #include <linux/slab.h> 20 #include <sound/pcm_params.h> 21 #include <sound/soc.h> 22 #include <sound/tas2781.h> 23 24 #define TASDEVICE_CRC8_POLYNOMIAL 0x4d 25 26 static const struct regmap_range_cfg tasdevice_ranges[] = { 27 { 28 .range_min = 0, 29 .range_max = 256 * 128, 30 .selector_reg = TASDEVICE_PAGE_SELECT, 31 .selector_mask = 0xff, 32 .selector_shift = 0, 33 .window_start = 0, 34 .window_len = 128, 35 }, 36 }; 37 38 static const struct regmap_config tasdevice_regmap = { 39 .reg_bits = 8, 40 .val_bits = 8, 41 .cache_type = REGCACHE_NONE, 42 .ranges = tasdevice_ranges, 43 .num_ranges = ARRAY_SIZE(tasdevice_ranges), 44 .max_register = 256 * 128, 45 }; 46 47 static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv, 48 unsigned short chn, int book) 49 { 50 struct i2c_client *client = (struct i2c_client *)tas_priv->client; 51 int ret = 0; 52 53 if (chn < tas_priv->ndev) { 54 struct tasdevice *tasdev = &tas_priv->tasdevice[chn]; 55 struct regmap *map = tas_priv->regmap; 56 57 if (client->addr != tasdev->dev_addr) { 58 client->addr = tasdev->dev_addr; 59 /* All tas2781s share the same regmap, clear the page 60 * inside regmap once switching to another tas2781. 61 * Register 0 at any pages and any books inside tas2781 62 * is the same one for page-switching. 63 */ 64 ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0); 65 if (ret < 0) { 66 dev_err(tas_priv->dev, "%s, E=%d\n", 67 __func__, ret); 68 goto out; 69 } 70 } 71 72 if (tasdev->cur_book != book) { 73 ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book); 74 if (ret < 0) { 75 dev_err(tas_priv->dev, "%s, E=%d\n", 76 __func__, ret); 77 goto out; 78 } 79 tasdev->cur_book = book; 80 } 81 } else { 82 ret = -EINVAL; 83 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 84 chn); 85 } 86 87 out: 88 return ret; 89 } 90 91 int tasdevice_dev_read(struct tasdevice_priv *tas_priv, 92 unsigned short chn, unsigned int reg, unsigned int *val) 93 { 94 int ret = 0; 95 96 if (chn < tas_priv->ndev) { 97 struct regmap *map = tas_priv->regmap; 98 99 ret = tasdevice_change_chn_book(tas_priv, chn, 100 TASDEVICE_BOOK_ID(reg)); 101 if (ret < 0) 102 goto out; 103 104 ret = regmap_read(map, TASDEVICE_PGRG(reg), val); 105 if (ret < 0) 106 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 107 } else { 108 ret = -EINVAL; 109 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 110 chn); 111 } 112 113 out: 114 return ret; 115 } 116 EXPORT_SYMBOL_GPL(tasdevice_dev_read); 117 118 int tasdevice_dev_write(struct tasdevice_priv *tas_priv, 119 unsigned short chn, unsigned int reg, unsigned int value) 120 { 121 int ret = 0; 122 123 if (chn < tas_priv->ndev) { 124 struct regmap *map = tas_priv->regmap; 125 126 ret = tasdevice_change_chn_book(tas_priv, chn, 127 TASDEVICE_BOOK_ID(reg)); 128 if (ret < 0) 129 goto out; 130 131 ret = regmap_write(map, TASDEVICE_PGRG(reg), 132 value); 133 if (ret < 0) 134 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 135 } else { 136 ret = -EINVAL; 137 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 138 chn); 139 } 140 141 out: 142 return ret; 143 } 144 EXPORT_SYMBOL_GPL(tasdevice_dev_write); 145 146 int tasdevice_dev_bulk_write( 147 struct tasdevice_priv *tas_priv, unsigned short chn, 148 unsigned int reg, unsigned char *data, 149 unsigned int len) 150 { 151 int ret = 0; 152 153 if (chn < tas_priv->ndev) { 154 struct regmap *map = tas_priv->regmap; 155 156 ret = tasdevice_change_chn_book(tas_priv, chn, 157 TASDEVICE_BOOK_ID(reg)); 158 if (ret < 0) 159 goto out; 160 161 ret = regmap_bulk_write(map, TASDEVICE_PGRG(reg), 162 data, len); 163 if (ret < 0) 164 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 165 } else { 166 ret = -EINVAL; 167 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 168 chn); 169 } 170 171 out: 172 return ret; 173 } 174 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_write); 175 176 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 177 unsigned short chn, unsigned int reg, unsigned char *data, 178 unsigned int len) 179 { 180 int ret = 0; 181 182 if (chn < tas_priv->ndev) { 183 struct regmap *map = tas_priv->regmap; 184 185 ret = tasdevice_change_chn_book(tas_priv, chn, 186 TASDEVICE_BOOK_ID(reg)); 187 if (ret < 0) 188 goto out; 189 190 ret = regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len); 191 if (ret < 0) 192 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 193 } else 194 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 195 chn); 196 197 out: 198 return ret; 199 } 200 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read); 201 202 int tasdevice_dev_update_bits( 203 struct tasdevice_priv *tas_priv, unsigned short chn, 204 unsigned int reg, unsigned int mask, unsigned int value) 205 { 206 int ret = 0; 207 208 if (chn < tas_priv->ndev) { 209 struct regmap *map = tas_priv->regmap; 210 211 ret = tasdevice_change_chn_book(tas_priv, chn, 212 TASDEVICE_BOOK_ID(reg)); 213 if (ret < 0) 214 goto out; 215 216 ret = regmap_update_bits(map, TASDEVICE_PGRG(reg), 217 mask, value); 218 if (ret < 0) 219 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 220 } else { 221 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 222 chn); 223 ret = -EINVAL; 224 } 225 226 out: 227 return ret; 228 } 229 EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits); 230 231 struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c) 232 { 233 struct tasdevice_priv *tas_priv; 234 235 tas_priv = devm_kzalloc(&i2c->dev, sizeof(*tas_priv), GFP_KERNEL); 236 if (!tas_priv) 237 return NULL; 238 tas_priv->dev = &i2c->dev; 239 tas_priv->client = (void *)i2c; 240 241 return tas_priv; 242 } 243 EXPORT_SYMBOL_GPL(tasdevice_kzalloc); 244 245 void tas2781_reset(struct tasdevice_priv *tas_dev) 246 { 247 int ret, i; 248 249 if (tas_dev->reset) { 250 gpiod_set_value_cansleep(tas_dev->reset, 0); 251 usleep_range(500, 1000); 252 gpiod_set_value_cansleep(tas_dev->reset, 1); 253 } else { 254 for (i = 0; i < tas_dev->ndev; i++) { 255 ret = tasdevice_dev_write(tas_dev, i, 256 TAS2781_REG_SWRESET, 257 TAS2781_REG_SWRESET_RESET); 258 if (ret < 0) 259 dev_err(tas_dev->dev, 260 "dev %d swreset fail, %d\n", 261 i, ret); 262 } 263 } 264 usleep_range(1000, 1050); 265 } 266 EXPORT_SYMBOL_GPL(tas2781_reset); 267 268 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 269 struct module *module, 270 void (*cont)(const struct firmware *fw, void *context)) 271 { 272 int ret = 0; 273 274 /* Codec Lock Hold to ensure that codec_probe and firmware parsing and 275 * loading do not simultaneously execute. 276 */ 277 mutex_lock(&tas_priv->codec_lock); 278 279 scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", 280 tas_priv->dev_name, tas_priv->ndev); 281 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 282 tas_priv->codec = codec; 283 ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 284 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 285 cont); 286 if (ret) 287 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 288 ret); 289 290 /* Codec Lock Release*/ 291 mutex_unlock(&tas_priv->codec_lock); 292 return ret; 293 } 294 EXPORT_SYMBOL_GPL(tascodec_init); 295 296 int tasdevice_init(struct tasdevice_priv *tas_priv) 297 { 298 int ret = 0; 299 int i; 300 301 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client, 302 &tasdevice_regmap); 303 if (IS_ERR(tas_priv->regmap)) { 304 ret = PTR_ERR(tas_priv->regmap); 305 dev_err(tas_priv->dev, "Failed to allocate register map: %d\n", 306 ret); 307 goto out; 308 } 309 310 tas_priv->cur_prog = -1; 311 tas_priv->cur_conf = -1; 312 313 for (i = 0; i < tas_priv->ndev; i++) { 314 tas_priv->tasdevice[i].cur_book = -1; 315 tas_priv->tasdevice[i].cur_prog = -1; 316 tas_priv->tasdevice[i].cur_conf = -1; 317 } 318 319 mutex_init(&tas_priv->codec_lock); 320 321 out: 322 return ret; 323 } 324 EXPORT_SYMBOL_GPL(tasdevice_init); 325 326 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog) 327 { 328 struct tasdevice_data *tas_dt; 329 struct tasdev_blk *blk; 330 unsigned int i; 331 332 if (!prog) 333 return; 334 335 tas_dt = &(prog->dev_data); 336 337 if (!tas_dt->dev_blks) 338 return; 339 340 for (i = 0; i < tas_dt->nr_blk; i++) { 341 blk = &(tas_dt->dev_blks[i]); 342 kfree(blk->data); 343 } 344 kfree(tas_dt->dev_blks); 345 } 346 347 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog, 348 unsigned short nr) 349 { 350 int i; 351 352 for (i = 0; i < nr; i++) 353 tasdev_dsp_prog_blk_remove(&prog[i]); 354 kfree(prog); 355 } 356 357 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg) 358 { 359 struct tasdevice_data *tas_dt; 360 struct tasdev_blk *blk; 361 unsigned int i; 362 363 if (cfg) { 364 tas_dt = &(cfg->dev_data); 365 366 if (!tas_dt->dev_blks) 367 return; 368 369 for (i = 0; i < tas_dt->nr_blk; i++) { 370 blk = &(tas_dt->dev_blks[i]); 371 kfree(blk->data); 372 } 373 kfree(tas_dt->dev_blks); 374 } 375 } 376 377 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config, 378 unsigned short nr) 379 { 380 int i; 381 382 for (i = 0; i < nr; i++) 383 tasdev_dsp_cfg_blk_remove(&config[i]); 384 kfree(config); 385 } 386 387 void tasdevice_dsp_remove(void *context) 388 { 389 struct tasdevice_priv *tas_dev = (struct tasdevice_priv *) context; 390 struct tasdevice_fw *tas_fmw = tas_dev->fmw; 391 392 if (!tas_dev->fmw) 393 return; 394 395 if (tas_fmw->programs) 396 tasdev_dsp_prog_remove(tas_fmw->programs, 397 tas_fmw->nr_programs); 398 if (tas_fmw->configs) 399 tasdev_dsp_cfg_remove(tas_fmw->configs, 400 tas_fmw->nr_configurations); 401 kfree(tas_fmw); 402 tas_dev->fmw = NULL; 403 } 404 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove); 405 406 void tasdevice_remove(struct tasdevice_priv *tas_priv) 407 { 408 mutex_destroy(&tas_priv->codec_lock); 409 } 410 EXPORT_SYMBOL_GPL(tasdevice_remove); 411 412 int tasdevice_save_calibration(struct tasdevice_priv *tas_priv) 413 { 414 if (tas_priv->save_calibration) 415 return tas_priv->save_calibration(tas_priv); 416 return -EINVAL; 417 } 418 EXPORT_SYMBOL_GPL(tasdevice_save_calibration); 419 420 void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv) 421 { 422 if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz) 423 tas_priv->apply_calibration(tas_priv); 424 } 425 EXPORT_SYMBOL_GPL(tasdevice_apply_calibration); 426 427 static int tasdevice_clamp(int val, int max, unsigned int invert) 428 { 429 if (val > max) 430 val = max; 431 if (invert) 432 val = max - val; 433 if (val < 0) 434 val = 0; 435 return val; 436 } 437 438 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 439 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 440 { 441 unsigned int invert = mc->invert; 442 unsigned char mask; 443 int max = mc->max; 444 int err_cnt = 0; 445 int val, i, ret; 446 447 mask = (1 << fls(max)) - 1; 448 mask <<= mc->shift; 449 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 450 for (i = 0; i < tas_priv->ndev; i++) { 451 ret = tasdevice_dev_update_bits(tas_priv, i, 452 mc->reg, mask, (unsigned int)(val << mc->shift)); 453 if (!ret) 454 continue; 455 err_cnt++; 456 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i); 457 } 458 459 /* All the devices set error, return 0 */ 460 return (err_cnt == tas_priv->ndev) ? 0 : 1; 461 } 462 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); 463 464 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 465 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 466 { 467 unsigned int invert = mc->invert; 468 unsigned char mask = 0; 469 int max = mc->max; 470 int ret = 0; 471 int val; 472 473 /* Read the primary device */ 474 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 475 if (ret) { 476 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 477 goto out; 478 } 479 480 mask = (1 << fls(max)) - 1; 481 mask <<= mc->shift; 482 val = (val & mask) >> mc->shift; 483 val = tasdevice_clamp(val, max, invert); 484 ucontrol->value.integer.value[0] = val; 485 486 out: 487 return ret; 488 489 } 490 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); 491 492 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 493 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 494 { 495 unsigned int invert = mc->invert; 496 int max = mc->max; 497 int err_cnt = 0; 498 int ret; 499 int val, i; 500 501 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 502 503 for (i = 0; i < tas_priv->ndev; i++) { 504 ret = tasdevice_dev_write(tas_priv, i, mc->reg, 505 (unsigned int)val); 506 if (!ret) 507 continue; 508 err_cnt++; 509 dev_err(tas_priv->dev, 510 "set digital vol err in dev %d\n", i); 511 } 512 513 /* All the devices set error, return 0 */ 514 return (err_cnt == tas_priv->ndev) ? 0 : 1; 515 516 } 517 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); 518 519 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 520 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 521 { 522 unsigned int invert = mc->invert; 523 int max = mc->max; 524 int ret, val; 525 526 /* Read the primary device as the whole */ 527 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 528 if (ret) { 529 dev_err(tas_priv->dev, "%s, get digital vol error\n", 530 __func__); 531 goto out; 532 } 533 534 val = tasdevice_clamp(val, max, invert); 535 ucontrol->value.integer.value[0] = val; 536 537 out: 538 return ret; 539 540 } 541 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); 542 543 MODULE_DESCRIPTION("TAS2781 common library"); 544 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>"); 545 MODULE_LICENSE("GPL"); 546