1 // SPDX-License-Identifier: GPL-2.0
3 // TAS2781 Common functions for HDA and ASoC Audio drivers
5 // Copyright 2023 - 2024 Texas Instruments, Inc.
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_gpio.h>
18 #include <linux/of_irq.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/tas2781.h>
25 #define TASDEVICE_CRC8_POLYNOMIAL 0x4d
27 static const struct regmap_range_cfg tasdevice_ranges[] = {
30 .range_max = 256 * 128,
31 .selector_reg = TASDEVICE_PAGE_SELECT,
32 .selector_mask = 0xff,
39 static const struct regmap_config tasdevice_regmap = {
42 .cache_type = REGCACHE_NONE,
43 .ranges = tasdevice_ranges,
44 .num_ranges = ARRAY_SIZE(tasdevice_ranges),
45 .max_register = 256 * 128,
48 static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv,
49 unsigned short chn, int book)
51 struct i2c_client *client = (struct i2c_client *)tas_priv->client;
54 if (chn < tas_priv->ndev) {
55 struct tasdevice *tasdev = &tas_priv->tasdevice[chn];
56 struct regmap *map = tas_priv->regmap;
58 if (client->addr != tasdev->dev_addr) {
59 client->addr = tasdev->dev_addr;
60 /* All tas2781s share the same regmap, clear the page
61 * inside regmap once switching to another tas2781.
62 * Register 0 at any pages and any books inside tas2781
63 * is the same one for page-switching.
65 ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0);
67 dev_err(tas_priv->dev, "%s, E=%d\n",
73 if (tasdev->cur_book != book) {
74 ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);
76 dev_err(tas_priv->dev, "%s, E=%d\n",
80 tasdev->cur_book = book;
84 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
92 int tasdevice_dev_read(struct tasdevice_priv *tas_priv,
93 unsigned short chn, unsigned int reg, unsigned int *val)
97 if (chn < tas_priv->ndev) {
98 struct regmap *map = tas_priv->regmap;
100 ret = tasdevice_change_chn_book(tas_priv, chn,
101 TASDEVICE_BOOK_ID(reg));
105 ret = regmap_read(map, TASDEVICE_PGRG(reg), val);
107 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
110 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
117 EXPORT_SYMBOL_GPL(tasdevice_dev_read);
119 int tasdevice_dev_write(struct tasdevice_priv *tas_priv,
120 unsigned short chn, unsigned int reg, unsigned int value)
124 if (chn < tas_priv->ndev) {
125 struct regmap *map = tas_priv->regmap;
127 ret = tasdevice_change_chn_book(tas_priv, chn,
128 TASDEVICE_BOOK_ID(reg));
132 ret = regmap_write(map, TASDEVICE_PGRG(reg),
135 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
138 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
145 EXPORT_SYMBOL_GPL(tasdevice_dev_write);
147 int tasdevice_dev_bulk_write(
148 struct tasdevice_priv *tas_priv, unsigned short chn,
149 unsigned int reg, unsigned char *data,
154 if (chn < tas_priv->ndev) {
155 struct regmap *map = tas_priv->regmap;
157 ret = tasdevice_change_chn_book(tas_priv, chn,
158 TASDEVICE_BOOK_ID(reg));
162 ret = regmap_bulk_write(map, TASDEVICE_PGRG(reg),
165 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
168 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
175 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_write);
177 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv,
178 unsigned short chn, unsigned int reg, unsigned char *data,
183 if (chn < tas_priv->ndev) {
184 struct regmap *map = tas_priv->regmap;
186 ret = tasdevice_change_chn_book(tas_priv, chn,
187 TASDEVICE_BOOK_ID(reg));
191 ret = regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len);
193 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
195 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
201 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read);
203 int tasdevice_dev_update_bits(
204 struct tasdevice_priv *tas_priv, unsigned short chn,
205 unsigned int reg, unsigned int mask, unsigned int value)
209 if (chn < tas_priv->ndev) {
210 struct regmap *map = tas_priv->regmap;
212 ret = tasdevice_change_chn_book(tas_priv, chn,
213 TASDEVICE_BOOK_ID(reg));
217 ret = regmap_update_bits(map, TASDEVICE_PGRG(reg),
220 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
222 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
230 EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits);
232 struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c)
234 struct tasdevice_priv *tas_priv;
236 tas_priv = devm_kzalloc(&i2c->dev, sizeof(*tas_priv), GFP_KERNEL);
239 tas_priv->dev = &i2c->dev;
240 tas_priv->client = (void *)i2c;
244 EXPORT_SYMBOL_GPL(tasdevice_kzalloc);
246 void tasdevice_reset(struct tasdevice_priv *tas_dev)
250 if (tas_dev->reset) {
251 gpiod_set_value_cansleep(tas_dev->reset, 0);
252 usleep_range(500, 1000);
253 gpiod_set_value_cansleep(tas_dev->reset, 1);
255 for (i = 0; i < tas_dev->ndev; i++) {
256 ret = tasdevice_dev_write(tas_dev, i,
257 TASDEVICE_REG_SWRESET,
258 TASDEVICE_REG_SWRESET_RESET);
260 dev_err(tas_dev->dev,
261 "dev %d swreset fail, %d\n",
265 usleep_range(1000, 1050);
267 EXPORT_SYMBOL_GPL(tasdevice_reset);
269 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec,
270 struct module *module,
271 void (*cont)(const struct firmware *fw, void *context))
275 /* Codec Lock Hold to ensure that codec_probe and firmware parsing and
276 * loading do not simultaneously execute.
278 mutex_lock(&tas_priv->codec_lock);
280 if (tas_priv->name_prefix)
281 scnprintf(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin",
282 tas_priv->name_prefix, tas_priv->dev_name,
285 scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin",
286 tas_priv->dev_name, tas_priv->ndev);
287 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
288 tas_priv->codec = codec;
289 ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
290 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
293 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
296 /* Codec Lock Release*/
297 mutex_unlock(&tas_priv->codec_lock);
300 EXPORT_SYMBOL_GPL(tascodec_init);
302 int tasdevice_init(struct tasdevice_priv *tas_priv)
307 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client,
309 if (IS_ERR(tas_priv->regmap)) {
310 ret = PTR_ERR(tas_priv->regmap);
311 dev_err(tas_priv->dev, "Failed to allocate register map: %d\n",
316 tas_priv->cur_prog = -1;
317 tas_priv->cur_conf = -1;
319 for (i = 0; i < tas_priv->ndev; i++) {
320 tas_priv->tasdevice[i].cur_book = -1;
321 tas_priv->tasdevice[i].cur_prog = -1;
322 tas_priv->tasdevice[i].cur_conf = -1;
325 mutex_init(&tas_priv->codec_lock);
330 EXPORT_SYMBOL_GPL(tasdevice_init);
332 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog)
334 struct tasdevice_data *tas_dt;
335 struct tasdev_blk *blk;
341 tas_dt = &(prog->dev_data);
343 if (!tas_dt->dev_blks)
346 for (i = 0; i < tas_dt->nr_blk; i++) {
347 blk = &(tas_dt->dev_blks[i]);
350 kfree(tas_dt->dev_blks);
353 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog,
358 for (i = 0; i < nr; i++)
359 tasdev_dsp_prog_blk_remove(&prog[i]);
363 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg)
365 struct tasdevice_data *tas_dt;
366 struct tasdev_blk *blk;
370 tas_dt = &(cfg->dev_data);
372 if (!tas_dt->dev_blks)
375 for (i = 0; i < tas_dt->nr_blk; i++) {
376 blk = &(tas_dt->dev_blks[i]);
379 kfree(tas_dt->dev_blks);
383 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config,
388 for (i = 0; i < nr; i++)
389 tasdev_dsp_cfg_blk_remove(&config[i]);
393 void tasdevice_dsp_remove(void *context)
395 struct tasdevice_priv *tas_dev = (struct tasdevice_priv *) context;
396 struct tasdevice_fw *tas_fmw = tas_dev->fmw;
401 if (tas_fmw->programs)
402 tasdev_dsp_prog_remove(tas_fmw->programs,
403 tas_fmw->nr_programs);
404 if (tas_fmw->configs)
405 tasdev_dsp_cfg_remove(tas_fmw->configs,
406 tas_fmw->nr_configurations);
410 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove);
412 void tasdevice_remove(struct tasdevice_priv *tas_priv)
414 if (gpio_is_valid(tas_priv->irq_info.irq_gpio))
415 gpio_free(tas_priv->irq_info.irq_gpio);
416 mutex_destroy(&tas_priv->codec_lock);
418 EXPORT_SYMBOL_GPL(tasdevice_remove);
420 int tasdevice_save_calibration(struct tasdevice_priv *tas_priv)
422 if (tas_priv->save_calibration)
423 return tas_priv->save_calibration(tas_priv);
426 EXPORT_SYMBOL_GPL(tasdevice_save_calibration);
428 void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv)
430 if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz)
431 tas_priv->apply_calibration(tas_priv);
433 EXPORT_SYMBOL_GPL(tasdevice_apply_calibration);
435 static int tasdevice_clamp(int val, int max, unsigned int invert)
446 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv,
447 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
449 unsigned int invert = mc->invert;
455 mask = (1 << fls(max)) - 1;
457 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert);
458 for (i = 0; i < tas_priv->ndev; i++) {
459 ret = tasdevice_dev_update_bits(tas_priv, i,
460 mc->reg, mask, (unsigned int)(val << mc->shift));
464 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i);
467 /* All the devices set error, return 0 */
468 return (err_cnt == tas_priv->ndev) ? 0 : 1;
470 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol);
472 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv,
473 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
475 unsigned int invert = mc->invert;
476 unsigned char mask = 0;
481 /* Read the primary device */
482 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
484 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
488 mask = (1 << fls(max)) - 1;
490 val = (val & mask) >> mc->shift;
491 val = tasdevice_clamp(val, max, invert);
492 ucontrol->value.integer.value[0] = val;
498 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol);
500 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv,
501 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
503 unsigned int invert = mc->invert;
509 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert);
511 for (i = 0; i < tas_priv->ndev; i++) {
512 ret = tasdevice_dev_write(tas_priv, i, mc->reg,
517 dev_err(tas_priv->dev,
518 "set digital vol err in dev %d\n", i);
521 /* All the devices set error, return 0 */
522 return (err_cnt == tas_priv->ndev) ? 0 : 1;
525 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol);
527 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv,
528 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
530 unsigned int invert = mc->invert;
534 /* Read the primary device as the whole */
535 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
537 dev_err(tas_priv->dev, "%s, get digital vol error\n",
542 val = tasdevice_clamp(val, max, invert);
543 ucontrol->value.integer.value[0] = val;
549 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol);
551 MODULE_DESCRIPTION("TAS2781 common library");
553 MODULE_LICENSE("GPL");