1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // TAS2563/TAS2781 Common functions for HDA and ASoC Audio drivers 4 // 5 // Copyright 2023 - 2024 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 channel:%d\n", 67 __func__, ret, chn); 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 tasdevice_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 TASDEVICE_REG_SWRESET, 257 TASDEVICE_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(tasdevice_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 if (tas_priv->name_prefix) 280 scnprintf(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin", 281 tas_priv->name_prefix, tas_priv->dev_name, 282 tas_priv->ndev); 283 else 284 scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", 285 tas_priv->dev_name, tas_priv->ndev); 286 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 287 tas_priv->codec = codec; 288 ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 289 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 290 cont); 291 if (ret) 292 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 293 ret); 294 295 /* Codec Lock Release*/ 296 mutex_unlock(&tas_priv->codec_lock); 297 return ret; 298 } 299 EXPORT_SYMBOL_GPL(tascodec_init); 300 301 int tasdevice_init(struct tasdevice_priv *tas_priv) 302 { 303 int ret = 0; 304 int i; 305 306 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client, 307 &tasdevice_regmap); 308 if (IS_ERR(tas_priv->regmap)) { 309 ret = PTR_ERR(tas_priv->regmap); 310 dev_err(tas_priv->dev, "Failed to allocate register map: %d\n", 311 ret); 312 goto out; 313 } 314 315 tas_priv->cur_prog = -1; 316 tas_priv->cur_conf = -1; 317 318 for (i = 0; i < tas_priv->ndev; i++) { 319 tas_priv->tasdevice[i].cur_book = -1; 320 tas_priv->tasdevice[i].cur_prog = -1; 321 tas_priv->tasdevice[i].cur_conf = -1; 322 } 323 324 mutex_init(&tas_priv->codec_lock); 325 326 out: 327 return ret; 328 } 329 EXPORT_SYMBOL_GPL(tasdevice_init); 330 331 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog) 332 { 333 struct tasdevice_data *tas_dt; 334 struct tasdev_blk *blk; 335 unsigned int i; 336 337 if (!prog) 338 return; 339 340 tas_dt = &(prog->dev_data); 341 342 if (!tas_dt->dev_blks) 343 return; 344 345 for (i = 0; i < tas_dt->nr_blk; i++) { 346 blk = &(tas_dt->dev_blks[i]); 347 kfree(blk->data); 348 } 349 kfree(tas_dt->dev_blks); 350 } 351 352 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog, 353 unsigned short nr) 354 { 355 int i; 356 357 for (i = 0; i < nr; i++) 358 tasdev_dsp_prog_blk_remove(&prog[i]); 359 kfree(prog); 360 } 361 362 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg) 363 { 364 struct tasdevice_data *tas_dt; 365 struct tasdev_blk *blk; 366 unsigned int i; 367 368 if (cfg) { 369 tas_dt = &(cfg->dev_data); 370 371 if (!tas_dt->dev_blks) 372 return; 373 374 for (i = 0; i < tas_dt->nr_blk; i++) { 375 blk = &(tas_dt->dev_blks[i]); 376 kfree(blk->data); 377 } 378 kfree(tas_dt->dev_blks); 379 } 380 } 381 382 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config, 383 unsigned short nr) 384 { 385 int i; 386 387 for (i = 0; i < nr; i++) 388 tasdev_dsp_cfg_blk_remove(&config[i]); 389 kfree(config); 390 } 391 392 void tasdevice_dsp_remove(void *context) 393 { 394 struct tasdevice_priv *tas_dev = (struct tasdevice_priv *) context; 395 struct tasdevice_fw *tas_fmw = tas_dev->fmw; 396 397 if (!tas_dev->fmw) 398 return; 399 400 if (tas_fmw->programs) 401 tasdev_dsp_prog_remove(tas_fmw->programs, 402 tas_fmw->nr_programs); 403 if (tas_fmw->configs) 404 tasdev_dsp_cfg_remove(tas_fmw->configs, 405 tas_fmw->nr_configurations); 406 kfree(tas_fmw); 407 tas_dev->fmw = NULL; 408 } 409 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove); 410 411 void tasdevice_remove(struct tasdevice_priv *tas_priv) 412 { 413 mutex_destroy(&tas_priv->codec_lock); 414 } 415 EXPORT_SYMBOL_GPL(tasdevice_remove); 416 417 int tasdevice_save_calibration(struct tasdevice_priv *tas_priv) 418 { 419 if (tas_priv->save_calibration) 420 return tas_priv->save_calibration(tas_priv); 421 return -EINVAL; 422 } 423 EXPORT_SYMBOL_GPL(tasdevice_save_calibration); 424 425 void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv) 426 { 427 if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz) 428 tas_priv->apply_calibration(tas_priv); 429 } 430 EXPORT_SYMBOL_GPL(tasdevice_apply_calibration); 431 432 static int tasdevice_clamp(int val, int max, unsigned int invert) 433 { 434 if (val > max) 435 val = max; 436 if (invert) 437 val = max - val; 438 if (val < 0) 439 val = 0; 440 return val; 441 } 442 443 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 444 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 445 { 446 unsigned int invert = mc->invert; 447 unsigned char mask; 448 int max = mc->max; 449 int err_cnt = 0; 450 int val, i, ret; 451 452 mask = (1 << fls(max)) - 1; 453 mask <<= mc->shift; 454 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 455 for (i = 0; i < tas_priv->ndev; i++) { 456 ret = tasdevice_dev_update_bits(tas_priv, i, 457 mc->reg, mask, (unsigned int)(val << mc->shift)); 458 if (!ret) 459 continue; 460 err_cnt++; 461 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i); 462 } 463 464 /* All the devices set error, return 0 */ 465 return (err_cnt == tas_priv->ndev) ? 0 : 1; 466 } 467 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); 468 469 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 470 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 471 { 472 unsigned int invert = mc->invert; 473 unsigned char mask = 0; 474 int max = mc->max; 475 int ret = 0; 476 int val; 477 478 /* Read the primary device */ 479 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 480 if (ret) { 481 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 482 goto out; 483 } 484 485 mask = (1 << fls(max)) - 1; 486 mask <<= mc->shift; 487 val = (val & mask) >> mc->shift; 488 val = tasdevice_clamp(val, max, invert); 489 ucontrol->value.integer.value[0] = val; 490 491 out: 492 return ret; 493 494 } 495 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); 496 497 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 498 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 499 { 500 unsigned int invert = mc->invert; 501 int max = mc->max; 502 int err_cnt = 0; 503 int ret; 504 int val, i; 505 506 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 507 508 for (i = 0; i < tas_priv->ndev; i++) { 509 ret = tasdevice_dev_write(tas_priv, i, mc->reg, 510 (unsigned int)val); 511 if (!ret) 512 continue; 513 err_cnt++; 514 dev_err(tas_priv->dev, 515 "set digital vol err in dev %d\n", i); 516 } 517 518 /* All the devices set error, return 0 */ 519 return (err_cnt == tas_priv->ndev) ? 0 : 1; 520 521 } 522 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); 523 524 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 525 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 526 { 527 unsigned int invert = mc->invert; 528 int max = mc->max; 529 int ret, val; 530 531 /* Read the primary device as the whole */ 532 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 533 if (ret) { 534 dev_err(tas_priv->dev, "%s, get digital vol error\n", 535 __func__); 536 goto out; 537 } 538 539 val = tasdevice_clamp(val, max, invert); 540 ucontrol->value.integer.value[0] = val; 541 542 out: 543 return ret; 544 545 } 546 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); 547 548 MODULE_DESCRIPTION("TAS2781 common library"); 549 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>"); 550 MODULE_LICENSE("GPL"); 551