xref: /linux/sound/soc/codecs/tas2781-comlib.c (revision c7546e2c3cb739a3c1a2f5acaf9bb629d401afe5)
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 tasdev_chn_switch(struct tasdevice_priv *tas_priv,
92 	unsigned short chn)
93 {
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;
98 
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.
105 		 */
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;
110 		}
111 		return 1;
112 	}
113 	return 0;
114 }
115 EXPORT_SYMBOL_GPL(tasdev_chn_switch);
116 
117 int tasdevice_dev_read(struct tasdevice_priv *tas_priv,
118 	unsigned short chn, unsigned int reg, unsigned int *val)
119 {
120 	int ret = 0;
121 
122 	if (chn < tas_priv->ndev) {
123 		struct regmap *map = tas_priv->regmap;
124 
125 		ret = tasdevice_change_chn_book(tas_priv, chn,
126 			TASDEVICE_BOOK_ID(reg));
127 		if (ret < 0)
128 			goto out;
129 
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);
137 	}
138 
139 out:
140 	return ret;
141 }
142 EXPORT_SYMBOL_GPL(tasdevice_dev_read);
143 
144 int tasdevice_dev_write(struct tasdevice_priv *tas_priv,
145 	unsigned short chn, unsigned int reg, unsigned int value)
146 {
147 	int ret = 0;
148 
149 	if (chn < tas_priv->ndev) {
150 		struct regmap *map = tas_priv->regmap;
151 
152 		ret = tasdevice_change_chn_book(tas_priv, chn,
153 			TASDEVICE_BOOK_ID(reg));
154 		if (ret < 0)
155 			goto out;
156 
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);
165 	}
166 
167 out:
168 	return ret;
169 }
170 EXPORT_SYMBOL_GPL(tasdevice_dev_write);
171 
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)
176 {
177 	int ret = 0;
178 
179 	if (chn < tas_priv->ndev) {
180 		struct regmap *map = tas_priv->regmap;
181 
182 		ret = tasdevice_change_chn_book(tas_priv, chn,
183 			TASDEVICE_BOOK_ID(reg));
184 		if (ret < 0)
185 			goto out;
186 
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);
195 	}
196 
197 out:
198 	return ret;
199 }
200 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_write);
201 
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)
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_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);
222 
223 out:
224 	return ret;
225 }
226 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read);
227 
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)
231 {
232 	int ret = 0;
233 
234 	if (chn < tas_priv->ndev) {
235 		struct regmap *map = tas_priv->regmap;
236 
237 		ret = tasdevice_change_chn_book(tas_priv, chn,
238 			TASDEVICE_BOOK_ID(reg));
239 		if (ret < 0)
240 			goto out;
241 
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;
250 	}
251 
252 out:
253 	return ret;
254 }
255 EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits);
256 
257 struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c)
258 {
259 	struct tasdevice_priv *tas_priv;
260 
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;
266 
267 	return tas_priv;
268 }
269 EXPORT_SYMBOL_GPL(tasdevice_kzalloc);
270 
271 void tasdevice_reset(struct tasdevice_priv *tas_dev)
272 {
273 	int ret, i;
274 
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);
288 		}
289 	}
290 	usleep_range(1000, 1050);
291 }
292 EXPORT_SYMBOL_GPL(tasdevice_reset);
293 
294 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec,
295 	struct module *module,
296 	void (*cont)(const struct firmware *fw, void *context))
297 {
298 	int ret = 0;
299 
300 	/* Codec Lock Hold to ensure that codec_probe and firmware parsing and
301 	 * loading do not simultaneously execute.
302 	 */
303 	mutex_lock(&tas_priv->codec_lock);
304 
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);
320 
321 	/* Codec Lock Release*/
322 	mutex_unlock(&tas_priv->codec_lock);
323 	return ret;
324 }
325 EXPORT_SYMBOL_GPL(tascodec_init);
326 
327 int tasdevice_init(struct tasdevice_priv *tas_priv)
328 {
329 	int ret = 0;
330 	int i;
331 
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;
339 	}
340 
341 	tas_priv->cur_prog = -1;
342 	tas_priv->cur_conf = -1;
343 
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;
348 	}
349 
350 	mutex_init(&tas_priv->codec_lock);
351 
352 out:
353 	return ret;
354 }
355 EXPORT_SYMBOL_GPL(tasdevice_init);
356 
357 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog)
358 {
359 	struct tasdevice_data *tas_dt;
360 	struct tasdev_blk *blk;
361 	unsigned int i;
362 
363 	if (!prog)
364 		return;
365 
366 	tas_dt = &(prog->dev_data);
367 
368 	if (!tas_dt->dev_blks)
369 		return;
370 
371 	for (i = 0; i < tas_dt->nr_blk; i++) {
372 		blk = &(tas_dt->dev_blks[i]);
373 		kfree(blk->data);
374 	}
375 	kfree(tas_dt->dev_blks);
376 }
377 
378 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog,
379 	unsigned short nr)
380 {
381 	int i;
382 
383 	for (i = 0; i < nr; i++)
384 		tasdev_dsp_prog_blk_remove(&prog[i]);
385 	kfree(prog);
386 }
387 
388 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg)
389 {
390 	struct tasdevice_data *tas_dt;
391 	struct tasdev_blk *blk;
392 	unsigned int i;
393 
394 	if (cfg) {
395 		tas_dt = &(cfg->dev_data);
396 
397 		if (!tas_dt->dev_blks)
398 			return;
399 
400 		for (i = 0; i < tas_dt->nr_blk; i++) {
401 			blk = &(tas_dt->dev_blks[i]);
402 			kfree(blk->data);
403 		}
404 		kfree(tas_dt->dev_blks);
405 	}
406 }
407 
408 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config,
409 	unsigned short nr)
410 {
411 	int i;
412 
413 	for (i = 0; i < nr; i++)
414 		tasdev_dsp_cfg_blk_remove(&config[i]);
415 	kfree(config);
416 }
417 
418 void tasdevice_dsp_remove(void *context)
419 {
420 	struct tasdevice_priv *tas_dev = (struct tasdevice_priv *) context;
421 	struct tasdevice_fw *tas_fmw = tas_dev->fmw;
422 
423 	if (!tas_dev->fmw)
424 		return;
425 
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;
434 }
435 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove);
436 
437 void tasdevice_remove(struct tasdevice_priv *tas_priv)
438 {
439 	mutex_destroy(&tas_priv->codec_lock);
440 }
441 EXPORT_SYMBOL_GPL(tasdevice_remove);
442 
443 int tasdevice_save_calibration(struct tasdevice_priv *tas_priv)
444 {
445 	if (tas_priv->save_calibration)
446 		return tas_priv->save_calibration(tas_priv);
447 	return -EINVAL;
448 }
449 EXPORT_SYMBOL_GPL(tasdevice_save_calibration);
450 
451 void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv)
452 {
453 	if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz)
454 		tas_priv->apply_calibration(tas_priv);
455 }
456 EXPORT_SYMBOL_GPL(tasdevice_apply_calibration);
457 
458 static int tasdevice_clamp(int val, int max, unsigned int invert)
459 {
460 	if (val > max)
461 		val = max;
462 	if (invert)
463 		val = max - val;
464 	if (val < 0)
465 		val = 0;
466 	return val;
467 }
468 
469 int tasdevice_amp_putvol(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;
474 	int max = mc->max;
475 	int err_cnt = 0;
476 	int val, i, ret;
477 
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);
488 	}
489 
490 	/* All the devices set error, return 0 */
491 	return (err_cnt == tas_priv->ndev) ? 0 : 1;
492 }
493 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol);
494 
495 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv,
496 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
497 {
498 	unsigned int invert = mc->invert;
499 	unsigned char mask = 0;
500 	int max = mc->max;
501 	int ret = 0;
502 	int val;
503 
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;
509 	}
510 
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;
516 
517 out:
518 	return ret;
519 
520 }
521 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol);
522 
523 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv,
524 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
525 {
526 	unsigned int invert = mc->invert;
527 	int max = mc->max;
528 	int err_cnt = 0;
529 	int ret;
530 	int val, i;
531 
532 	val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert);
533 
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);
542 	}
543 
544 	/* All the devices set error, return 0 */
545 	return (err_cnt == tas_priv->ndev) ? 0 : 1;
546 
547 }
548 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol);
549 
550 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv,
551 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
552 {
553 	unsigned int invert = mc->invert;
554 	int max = mc->max;
555 	int ret, val;
556 
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;
563 	}
564 
565 	val = tasdevice_clamp(val, max, invert);
566 	ucontrol->value.integer.value[0] = val;
567 
568 out:
569 	return ret;
570 
571 }
572 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol);
573 
574 MODULE_DESCRIPTION("TAS2781 common library");
575 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
576 MODULE_LICENSE("GPL");
577