1 // SPDX-License-Identifier: GPL-2.0
3 // TAS2563/TAS2781 Common functions for HDA and ASoC Audio drivers
5 // Copyright 2023 - 2024 Texas Instruments, Inc.
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
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>
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>
24 #define TASDEVICE_CRC8_POLYNOMIAL 0x4d
26 static const struct regmap_range_cfg tasdevice_ranges
[] = {
29 .range_max
= 256 * 128,
30 .selector_reg
= TASDEVICE_PAGE_SELECT
,
31 .selector_mask
= 0xff,
38 static const struct regmap_config tasdevice_regmap
= {
41 .cache_type
= REGCACHE_NONE
,
42 .ranges
= tasdevice_ranges
,
43 .num_ranges
= ARRAY_SIZE(tasdevice_ranges
),
44 .max_register
= 256 * 128,
47 static int tasdevice_change_chn_book(struct tasdevice_priv
*tas_priv
,
48 unsigned short chn
, int book
)
50 struct i2c_client
*client
= (struct i2c_client
*)tas_priv
->client
;
53 if (chn
< tas_priv
->ndev
) {
54 struct tasdevice
*tasdev
= &tas_priv
->tasdevice
[chn
];
55 struct regmap
*map
= tas_priv
->regmap
;
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.
64 ret
= regmap_write(map
, TASDEVICE_PAGE_SELECT
, 0);
66 dev_err(tas_priv
->dev
, "%s, E=%d channel:%d\n",
72 if (tasdev
->cur_book
!= book
) {
73 ret
= regmap_write(map
, TASDEVICE_BOOKCTL_REG
, book
);
75 dev_err(tas_priv
->dev
, "%s, E=%d\n",
79 tasdev
->cur_book
= book
;
83 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
91 int tasdev_chn_switch(struct tasdevice_priv
*tas_priv
,
94 struct i2c_client
*client
= (struct i2c_client
*)tas_priv
->client
;
95 struct tasdevice
*tasdev
= &tas_priv
->tasdevice
[chn
];
96 struct regmap
*map
= tas_priv
->regmap
;
99 if (client
->addr
!= tasdev
->dev_addr
) {
100 client
->addr
= tasdev
->dev_addr
;
101 /* All devices share the same regmap, clear the page
102 * inside regmap once switching to another device.
103 * Register 0 at any pages and any books inside tas2781
104 * is the same one for page-switching.
106 ret
= regmap_write(map
, TASDEVICE_PAGE_SELECT
, 0);
108 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
115 EXPORT_SYMBOL_GPL(tasdev_chn_switch
);
117 int tasdevice_dev_read(struct tasdevice_priv
*tas_priv
,
118 unsigned short chn
, unsigned int reg
, unsigned int *val
)
122 if (chn
< tas_priv
->ndev
) {
123 struct regmap
*map
= tas_priv
->regmap
;
125 ret
= tasdevice_change_chn_book(tas_priv
, chn
,
126 TASDEVICE_BOOK_ID(reg
));
130 ret
= regmap_read(map
, TASDEVICE_PGRG(reg
), val
);
132 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
135 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
142 EXPORT_SYMBOL_GPL(tasdevice_dev_read
);
144 int tasdevice_dev_write(struct tasdevice_priv
*tas_priv
,
145 unsigned short chn
, unsigned int reg
, unsigned int value
)
149 if (chn
< tas_priv
->ndev
) {
150 struct regmap
*map
= tas_priv
->regmap
;
152 ret
= tasdevice_change_chn_book(tas_priv
, chn
,
153 TASDEVICE_BOOK_ID(reg
));
157 ret
= regmap_write(map
, TASDEVICE_PGRG(reg
),
160 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
163 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
170 EXPORT_SYMBOL_GPL(tasdevice_dev_write
);
172 int tasdevice_dev_bulk_write(
173 struct tasdevice_priv
*tas_priv
, unsigned short chn
,
174 unsigned int reg
, unsigned char *data
,
179 if (chn
< tas_priv
->ndev
) {
180 struct regmap
*map
= tas_priv
->regmap
;
182 ret
= tasdevice_change_chn_book(tas_priv
, chn
,
183 TASDEVICE_BOOK_ID(reg
));
187 ret
= regmap_bulk_write(map
, TASDEVICE_PGRG(reg
),
190 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
193 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
200 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_write
);
202 int tasdevice_dev_bulk_read(struct tasdevice_priv
*tas_priv
,
203 unsigned short chn
, unsigned int reg
, unsigned char *data
,
208 if (chn
< tas_priv
->ndev
) {
209 struct regmap
*map
= tas_priv
->regmap
;
211 ret
= tasdevice_change_chn_book(tas_priv
, chn
,
212 TASDEVICE_BOOK_ID(reg
));
216 ret
= regmap_bulk_read(map
, TASDEVICE_PGRG(reg
), data
, len
);
218 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
220 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
226 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read
);
228 int tasdevice_dev_update_bits(
229 struct tasdevice_priv
*tas_priv
, unsigned short chn
,
230 unsigned int reg
, unsigned int mask
, unsigned int value
)
234 if (chn
< tas_priv
->ndev
) {
235 struct regmap
*map
= tas_priv
->regmap
;
237 ret
= tasdevice_change_chn_book(tas_priv
, chn
,
238 TASDEVICE_BOOK_ID(reg
));
242 ret
= regmap_update_bits(map
, TASDEVICE_PGRG(reg
),
245 dev_err(tas_priv
->dev
, "%s, E=%d\n", __func__
, ret
);
247 dev_err(tas_priv
->dev
, "%s, no such channel(%d)\n", __func__
,
255 EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits
);
257 struct tasdevice_priv
*tasdevice_kzalloc(struct i2c_client
*i2c
)
259 struct tasdevice_priv
*tas_priv
;
261 tas_priv
= devm_kzalloc(&i2c
->dev
, sizeof(*tas_priv
), GFP_KERNEL
);
264 tas_priv
->dev
= &i2c
->dev
;
265 tas_priv
->client
= (void *)i2c
;
269 EXPORT_SYMBOL_GPL(tasdevice_kzalloc
);
271 void tasdevice_reset(struct tasdevice_priv
*tas_dev
)
275 if (tas_dev
->reset
) {
276 gpiod_set_value_cansleep(tas_dev
->reset
, 0);
277 usleep_range(500, 1000);
278 gpiod_set_value_cansleep(tas_dev
->reset
, 1);
280 for (i
= 0; i
< tas_dev
->ndev
; i
++) {
281 ret
= tasdevice_dev_write(tas_dev
, i
,
282 TASDEVICE_REG_SWRESET
,
283 TASDEVICE_REG_SWRESET_RESET
);
285 dev_err(tas_dev
->dev
,
286 "dev %d swreset fail, %d\n",
290 usleep_range(1000, 1050);
292 EXPORT_SYMBOL_GPL(tasdevice_reset
);
294 int tascodec_init(struct tasdevice_priv
*tas_priv
, void *codec
,
295 struct module
*module
,
296 void (*cont
)(const struct firmware
*fw
, void *context
))
300 /* Codec Lock Hold to ensure that codec_probe and firmware parsing and
301 * loading do not simultaneously execute.
303 mutex_lock(&tas_priv
->codec_lock
);
305 if (tas_priv
->name_prefix
)
306 scnprintf(tas_priv
->rca_binaryname
, 64, "%s-%sRCA%d.bin",
307 tas_priv
->name_prefix
, tas_priv
->dev_name
,
310 scnprintf(tas_priv
->rca_binaryname
, 64, "%sRCA%d.bin",
311 tas_priv
->dev_name
, tas_priv
->ndev
);
312 crc8_populate_msb(tas_priv
->crc8_lkp_tbl
, TASDEVICE_CRC8_POLYNOMIAL
);
313 tas_priv
->codec
= codec
;
314 ret
= request_firmware_nowait(module
, FW_ACTION_UEVENT
,
315 tas_priv
->rca_binaryname
, tas_priv
->dev
, GFP_KERNEL
, tas_priv
,
318 dev_err(tas_priv
->dev
, "request_firmware_nowait err:0x%08x\n",
321 /* Codec Lock Release*/
322 mutex_unlock(&tas_priv
->codec_lock
);
325 EXPORT_SYMBOL_GPL(tascodec_init
);
327 int tasdevice_init(struct tasdevice_priv
*tas_priv
)
332 tas_priv
->regmap
= devm_regmap_init_i2c(tas_priv
->client
,
334 if (IS_ERR(tas_priv
->regmap
)) {
335 ret
= PTR_ERR(tas_priv
->regmap
);
336 dev_err(tas_priv
->dev
, "Failed to allocate register map: %d\n",
341 tas_priv
->cur_prog
= -1;
342 tas_priv
->cur_conf
= -1;
344 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
345 tas_priv
->tasdevice
[i
].cur_book
= -1;
346 tas_priv
->tasdevice
[i
].cur_prog
= -1;
347 tas_priv
->tasdevice
[i
].cur_conf
= -1;
350 mutex_init(&tas_priv
->codec_lock
);
355 EXPORT_SYMBOL_GPL(tasdevice_init
);
357 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog
*prog
)
359 struct tasdevice_data
*tas_dt
;
360 struct tasdev_blk
*blk
;
366 tas_dt
= &(prog
->dev_data
);
368 if (!tas_dt
->dev_blks
)
371 for (i
= 0; i
< tas_dt
->nr_blk
; i
++) {
372 blk
= &(tas_dt
->dev_blks
[i
]);
375 kfree(tas_dt
->dev_blks
);
378 static void tasdev_dsp_prog_remove(struct tasdevice_prog
*prog
,
383 for (i
= 0; i
< nr
; i
++)
384 tasdev_dsp_prog_blk_remove(&prog
[i
]);
388 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config
*cfg
)
390 struct tasdevice_data
*tas_dt
;
391 struct tasdev_blk
*blk
;
395 tas_dt
= &(cfg
->dev_data
);
397 if (!tas_dt
->dev_blks
)
400 for (i
= 0; i
< tas_dt
->nr_blk
; i
++) {
401 blk
= &(tas_dt
->dev_blks
[i
]);
404 kfree(tas_dt
->dev_blks
);
408 static void tasdev_dsp_cfg_remove(struct tasdevice_config
*config
,
413 for (i
= 0; i
< nr
; i
++)
414 tasdev_dsp_cfg_blk_remove(&config
[i
]);
418 void tasdevice_dsp_remove(void *context
)
420 struct tasdevice_priv
*tas_dev
= (struct tasdevice_priv
*) context
;
421 struct tasdevice_fw
*tas_fmw
= tas_dev
->fmw
;
426 if (tas_fmw
->programs
)
427 tasdev_dsp_prog_remove(tas_fmw
->programs
,
428 tas_fmw
->nr_programs
);
429 if (tas_fmw
->configs
)
430 tasdev_dsp_cfg_remove(tas_fmw
->configs
,
431 tas_fmw
->nr_configurations
);
435 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove
);
437 void tasdevice_remove(struct tasdevice_priv
*tas_priv
)
439 mutex_destroy(&tas_priv
->codec_lock
);
441 EXPORT_SYMBOL_GPL(tasdevice_remove
);
443 int tasdevice_save_calibration(struct tasdevice_priv
*tas_priv
)
445 if (tas_priv
->save_calibration
)
446 return tas_priv
->save_calibration(tas_priv
);
449 EXPORT_SYMBOL_GPL(tasdevice_save_calibration
);
451 void tasdevice_apply_calibration(struct tasdevice_priv
*tas_priv
)
453 if (tas_priv
->apply_calibration
&& tas_priv
->cali_data
.total_sz
)
454 tas_priv
->apply_calibration(tas_priv
);
456 EXPORT_SYMBOL_GPL(tasdevice_apply_calibration
);
458 static int tasdevice_clamp(int val
, int max
, unsigned int invert
)
469 int tasdevice_amp_putvol(struct tasdevice_priv
*tas_priv
,
470 struct snd_ctl_elem_value
*ucontrol
, struct soc_mixer_control
*mc
)
472 unsigned int invert
= mc
->invert
;
478 mask
= (1 << fls(max
)) - 1;
480 val
= tasdevice_clamp(ucontrol
->value
.integer
.value
[0], max
, invert
);
481 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
482 ret
= tasdevice_dev_update_bits(tas_priv
, i
,
483 mc
->reg
, mask
, (unsigned int)(val
<< mc
->shift
));
487 dev_err(tas_priv
->dev
, "set AMP vol error in dev %d\n", i
);
490 /* All the devices set error, return 0 */
491 return (err_cnt
== tas_priv
->ndev
) ? 0 : 1;
493 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol
);
495 int tasdevice_amp_getvol(struct tasdevice_priv
*tas_priv
,
496 struct snd_ctl_elem_value
*ucontrol
, struct soc_mixer_control
*mc
)
498 unsigned int invert
= mc
->invert
;
499 unsigned char mask
= 0;
504 /* Read the primary device */
505 ret
= tasdevice_dev_read(tas_priv
, 0, mc
->reg
, &val
);
507 dev_err(tas_priv
->dev
, "%s, get AMP vol error\n", __func__
);
511 mask
= (1 << fls(max
)) - 1;
513 val
= (val
& mask
) >> mc
->shift
;
514 val
= tasdevice_clamp(val
, max
, invert
);
515 ucontrol
->value
.integer
.value
[0] = val
;
521 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol
);
523 int tasdevice_digital_putvol(struct tasdevice_priv
*tas_priv
,
524 struct snd_ctl_elem_value
*ucontrol
, struct soc_mixer_control
*mc
)
526 unsigned int invert
= mc
->invert
;
532 val
= tasdevice_clamp(ucontrol
->value
.integer
.value
[0], max
, invert
);
534 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
535 ret
= tasdevice_dev_write(tas_priv
, i
, mc
->reg
,
540 dev_err(tas_priv
->dev
,
541 "set digital vol err in dev %d\n", i
);
544 /* All the devices set error, return 0 */
545 return (err_cnt
== tas_priv
->ndev
) ? 0 : 1;
548 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol
);
550 int tasdevice_digital_getvol(struct tasdevice_priv
*tas_priv
,
551 struct snd_ctl_elem_value
*ucontrol
, struct soc_mixer_control
*mc
)
553 unsigned int invert
= mc
->invert
;
557 /* Read the primary device as the whole */
558 ret
= tasdevice_dev_read(tas_priv
, 0, mc
->reg
, &val
);
560 dev_err(tas_priv
->dev
, "%s, get digital vol error\n",
565 val
= tasdevice_clamp(val
, max
, invert
);
566 ucontrol
->value
.integer
.value
[0] = val
;
572 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol
);
574 MODULE_DESCRIPTION("TAS2781 common library");
575 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
576 MODULE_LICENSE("GPL");