printf: Remove unused 'bprintf'
[drm/drm-misc.git] / sound / soc / codecs / tas2781-comlib.c
blob1e0b3aa95749deaeb3e06f999e5fcb7035c0dd8d
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>
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>
24 #define TASDEVICE_CRC8_POLYNOMIAL 0x4d
26 static const struct regmap_range_cfg tasdevice_ranges[] = {
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,
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,
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;
51 int ret = 0;
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);
65 if (ret < 0) {
66 dev_err(tas_priv->dev, "%s, E=%d channel:%d\n",
67 __func__, ret, chn);
68 goto out;
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;
79 tasdev->cur_book = book;
81 } else {
82 ret = -EINVAL;
83 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
84 chn);
87 out:
88 return ret;
91 int tasdev_chn_switch(struct tasdevice_priv *tas_priv,
92 unsigned short chn)
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;
97 int ret;
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);
107 if (ret < 0) {
108 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
109 return ret;
111 return 1;
113 return 0;
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)
120 int ret = 0;
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));
127 if (ret < 0)
128 goto out;
130 ret = regmap_read(map, TASDEVICE_PGRG(reg), val);
131 if (ret < 0)
132 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
133 } else {
134 ret = -EINVAL;
135 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
136 chn);
139 out:
140 return ret;
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)
147 int ret = 0;
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));
154 if (ret < 0)
155 goto out;
157 ret = regmap_write(map, TASDEVICE_PGRG(reg),
158 value);
159 if (ret < 0)
160 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
161 } else {
162 ret = -EINVAL;
163 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
164 chn);
167 out:
168 return ret;
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,
175 unsigned int len)
177 int ret = 0;
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));
184 if (ret < 0)
185 goto out;
187 ret = regmap_bulk_write(map, TASDEVICE_PGRG(reg),
188 data, len);
189 if (ret < 0)
190 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
191 } else {
192 ret = -EINVAL;
193 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
194 chn);
197 out:
198 return ret;
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,
204 unsigned int len)
206 int ret = 0;
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));
213 if (ret < 0)
214 goto out;
216 ret = regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len);
217 if (ret < 0)
218 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
219 } else
220 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
221 chn);
223 out:
224 return ret;
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)
232 int ret = 0;
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));
239 if (ret < 0)
240 goto out;
242 ret = regmap_update_bits(map, TASDEVICE_PGRG(reg),
243 mask, value);
244 if (ret < 0)
245 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
246 } else {
247 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
248 chn);
249 ret = -EINVAL;
252 out:
253 return ret;
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);
262 if (!tas_priv)
263 return NULL;
264 tas_priv->dev = &i2c->dev;
265 tas_priv->client = (void *)i2c;
267 return tas_priv;
269 EXPORT_SYMBOL_GPL(tasdevice_kzalloc);
271 void tasdevice_reset(struct tasdevice_priv *tas_dev)
273 int ret, i;
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);
279 } else {
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);
284 if (ret < 0)
285 dev_err(tas_dev->dev,
286 "dev %d swreset fail, %d\n",
287 i, ret);
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))
298 int ret = 0;
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,
308 tas_priv->ndev);
309 else
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,
316 cont);
317 if (ret)
318 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
319 ret);
321 /* Codec Lock Release*/
322 mutex_unlock(&tas_priv->codec_lock);
323 return ret;
325 EXPORT_SYMBOL_GPL(tascodec_init);
327 int tasdevice_init(struct tasdevice_priv *tas_priv)
329 int ret = 0;
330 int i;
332 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client,
333 &tasdevice_regmap);
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",
337 ret);
338 goto out;
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);
352 out:
353 return ret;
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;
361 unsigned int i;
363 if (!prog)
364 return;
366 tas_dt = &(prog->dev_data);
368 if (!tas_dt->dev_blks)
369 return;
371 for (i = 0; i < tas_dt->nr_blk; i++) {
372 blk = &(tas_dt->dev_blks[i]);
373 kfree(blk->data);
375 kfree(tas_dt->dev_blks);
378 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog,
379 unsigned short nr)
381 int i;
383 for (i = 0; i < nr; i++)
384 tasdev_dsp_prog_blk_remove(&prog[i]);
385 kfree(prog);
388 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg)
390 struct tasdevice_data *tas_dt;
391 struct tasdev_blk *blk;
392 unsigned int i;
394 if (cfg) {
395 tas_dt = &(cfg->dev_data);
397 if (!tas_dt->dev_blks)
398 return;
400 for (i = 0; i < tas_dt->nr_blk; i++) {
401 blk = &(tas_dt->dev_blks[i]);
402 kfree(blk->data);
404 kfree(tas_dt->dev_blks);
408 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config,
409 unsigned short nr)
411 int i;
413 for (i = 0; i < nr; i++)
414 tasdev_dsp_cfg_blk_remove(&config[i]);
415 kfree(config);
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;
423 if (!tas_dev->fmw)
424 return;
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);
432 kfree(tas_fmw);
433 tas_dev->fmw = NULL;
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);
447 return -EINVAL;
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)
460 if (val > max)
461 val = max;
462 if (invert)
463 val = max - val;
464 if (val < 0)
465 val = 0;
466 return val;
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;
473 unsigned char mask;
474 int max = mc->max;
475 int err_cnt = 0;
476 int val, i, ret;
478 mask = (1 << fls(max)) - 1;
479 mask <<= mc->shift;
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));
484 if (!ret)
485 continue;
486 err_cnt++;
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;
500 int max = mc->max;
501 int ret = 0;
502 int val;
504 /* Read the primary device */
505 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
506 if (ret) {
507 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
508 goto out;
511 mask = (1 << fls(max)) - 1;
512 mask <<= mc->shift;
513 val = (val & mask) >> mc->shift;
514 val = tasdevice_clamp(val, max, invert);
515 ucontrol->value.integer.value[0] = val;
517 out:
518 return ret;
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;
527 int max = mc->max;
528 int err_cnt = 0;
529 int ret;
530 int val, i;
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,
536 (unsigned int)val);
537 if (!ret)
538 continue;
539 err_cnt++;
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;
554 int max = mc->max;
555 int ret, val;
557 /* Read the primary device as the whole */
558 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
559 if (ret) {
560 dev_err(tas_priv->dev, "%s, get digital vol error\n",
561 __func__);
562 goto out;
565 val = tasdevice_clamp(val, max, invert);
566 ucontrol->value.integer.value[0] = val;
568 out:
569 return ret;
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");