1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // tas2781-lib.c -- TAS2781 Common functions for HDA and ASoC Audio drivers
4 //
5 // Copyright 2023 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_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>
24 
25 #define TASDEVICE_CRC8_POLYNOMIAL	0x4d
26 
27 static const struct regmap_range_cfg tasdevice_ranges[] = {
28 	{
29 		.range_min = 0,
30 		.range_max = 256 * 128,
31 		.selector_reg = TASDEVICE_PAGE_SELECT,
32 		.selector_mask = 0xff,
33 		.selector_shift = 0,
34 		.window_start = 0,
35 		.window_len = 128,
36 	},
37 };
38 
39 static const struct regmap_config tasdevice_regmap = {
40 	.reg_bits = 8,
41 	.val_bits = 8,
42 	.cache_type = REGCACHE_RBTREE,
43 	.ranges = tasdevice_ranges,
44 	.num_ranges = ARRAY_SIZE(tasdevice_ranges),
45 	.max_register = 256 * 128,
46 };
47 
48 static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv,
49 	unsigned short chn, int book)
50 {
51 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
52 	int ret = 0;
53 
54 	if (chn < tas_priv->ndev) {
55 		struct tasdevice *tasdev = &tas_priv->tasdevice[chn];
56 		struct regmap *map = tas_priv->regmap;
57 
58 		if (client->addr != tasdev->dev_addr) {
59 			client->addr = tasdev->dev_addr;
60 			if (tasdev->cur_book == book) {
61 				ret = regmap_write(map,
62 					TASDEVICE_PAGE_SELECT, 0);
63 				if (ret < 0) {
64 					dev_err(tas_priv->dev, "%s, E=%d\n",
65 						__func__, ret);
66 					goto out;
67 				}
68 			}
69 			goto out;
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 tas2781_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 				TAS2781_REG_SWRESET,
257 				TAS2781_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(tas2781_reset);
267 
268 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec,
269 	void (*cont)(const struct firmware *fw, void *context))
270 {
271 	int ret = 0;
272 
273 	/* Codec Lock Hold to ensure that codec_probe and firmware parsing and
274 	 * loading do not simultaneously execute.
275 	 */
276 	mutex_lock(&tas_priv->codec_lock);
277 
278 	scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin",
279 		tas_priv->dev_name, tas_priv->ndev);
280 	crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
281 	tas_priv->codec = codec;
282 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
283 		tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
284 		cont);
285 	if (ret)
286 		dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
287 			ret);
288 
289 	/* Codec Lock Release*/
290 	mutex_unlock(&tas_priv->codec_lock);
291 	return ret;
292 }
293 EXPORT_SYMBOL_GPL(tascodec_init);
294 
295 int tasdevice_init(struct tasdevice_priv *tas_priv)
296 {
297 	int ret = 0;
298 	int i;
299 
300 	tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client,
301 		&tasdevice_regmap);
302 	if (IS_ERR(tas_priv->regmap)) {
303 		ret = PTR_ERR(tas_priv->regmap);
304 		dev_err(tas_priv->dev, "Failed to allocate register map: %d\n",
305 			ret);
306 		goto out;
307 	}
308 
309 	tas_priv->cur_prog = -1;
310 	tas_priv->cur_conf = -1;
311 
312 	for (i = 0; i < tas_priv->ndev; i++) {
313 		tas_priv->tasdevice[i].cur_book = -1;
314 		tas_priv->tasdevice[i].cur_prog = -1;
315 		tas_priv->tasdevice[i].cur_conf = -1;
316 	}
317 
318 	dev_set_drvdata(tas_priv->dev, tas_priv);
319 
320 	mutex_init(&tas_priv->codec_lock);
321 
322 out:
323 	return ret;
324 }
325 EXPORT_SYMBOL_GPL(tasdevice_init);
326 
327 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog)
328 {
329 	struct tasdevice_data *tas_dt;
330 	struct tasdev_blk *blk;
331 	unsigned int i;
332 
333 	if (!prog)
334 		return;
335 
336 	tas_dt = &(prog->dev_data);
337 
338 	if (!tas_dt->dev_blks)
339 		return;
340 
341 	for (i = 0; i < tas_dt->nr_blk; i++) {
342 		blk = &(tas_dt->dev_blks[i]);
343 		kfree(blk->data);
344 	}
345 	kfree(tas_dt->dev_blks);
346 }
347 
348 static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog,
349 	unsigned short nr)
350 {
351 	int i;
352 
353 	for (i = 0; i < nr; i++)
354 		tasdev_dsp_prog_blk_remove(&prog[i]);
355 	kfree(prog);
356 }
357 
358 static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg)
359 {
360 	struct tasdevice_data *tas_dt;
361 	struct tasdev_blk *blk;
362 	unsigned int i;
363 
364 	if (cfg) {
365 		tas_dt = &(cfg->dev_data);
366 
367 		if (!tas_dt->dev_blks)
368 			return;
369 
370 		for (i = 0; i < tas_dt->nr_blk; i++) {
371 			blk = &(tas_dt->dev_blks[i]);
372 			kfree(blk->data);
373 		}
374 		kfree(tas_dt->dev_blks);
375 	}
376 }
377 
378 static void tasdev_dsp_cfg_remove(struct tasdevice_config *config,
379 	unsigned short nr)
380 {
381 	int i;
382 
383 	for (i = 0; i < nr; i++)
384 		tasdev_dsp_cfg_blk_remove(&config[i]);
385 	kfree(config);
386 }
387 
388 void tasdevice_dsp_remove(void *context)
389 {
390 	struct tasdevice_priv *tas_dev = (struct tasdevice_priv *) context;
391 	struct tasdevice_fw *tas_fmw = tas_dev->fmw;
392 
393 	if (!tas_dev->fmw)
394 		return;
395 
396 	if (tas_fmw->programs)
397 		tasdev_dsp_prog_remove(tas_fmw->programs,
398 			tas_fmw->nr_programs);
399 	if (tas_fmw->configs)
400 		tasdev_dsp_cfg_remove(tas_fmw->configs,
401 			tas_fmw->nr_configurations);
402 	kfree(tas_fmw);
403 	tas_dev->fmw = NULL;
404 }
405 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove);
406 
407 void tasdevice_remove(struct tasdevice_priv *tas_priv)
408 {
409 	if (gpio_is_valid(tas_priv->irq_info.irq_gpio))
410 		gpio_free(tas_priv->irq_info.irq_gpio);
411 	kfree(tas_priv->acpi_subsystem_id);
412 	mutex_destroy(&tas_priv->codec_lock);
413 }
414 EXPORT_SYMBOL_GPL(tasdevice_remove);
415 
416 static int tasdevice_clamp(int val, int max, unsigned int invert)
417 {
418 	if (val > max)
419 		val = max;
420 	if (invert)
421 		val = max - val;
422 	if (val < 0)
423 		val = 0;
424 	return val;
425 }
426 
427 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv,
428 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
429 {
430 	unsigned int invert = mc->invert;
431 	unsigned char mask;
432 	int max = mc->max;
433 	int err_cnt = 0;
434 	int val, i, ret;
435 
436 	mask = (1 << fls(max)) - 1;
437 	mask <<= mc->shift;
438 	val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert);
439 	for (i = 0; i < tas_priv->ndev; i++) {
440 		ret = tasdevice_dev_update_bits(tas_priv, i,
441 			mc->reg, mask, (unsigned int)(val << mc->shift));
442 		if (!ret)
443 			continue;
444 		err_cnt++;
445 		dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i);
446 	}
447 
448 	/* All the devices set error, return 0 */
449 	return (err_cnt == tas_priv->ndev) ? 0 : 1;
450 }
451 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol);
452 
453 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv,
454 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
455 {
456 	unsigned int invert = mc->invert;
457 	unsigned char mask = 0;
458 	int max = mc->max;
459 	int ret = 0;
460 	int val;
461 
462 	/* Read the primary device */
463 	ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
464 	if (ret) {
465 		dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
466 		goto out;
467 	}
468 
469 	mask = (1 << fls(max)) - 1;
470 	mask <<= mc->shift;
471 	val = (val & mask) >> mc->shift;
472 	val = tasdevice_clamp(val, max, invert);
473 	ucontrol->value.integer.value[0] = val;
474 
475 out:
476 	return ret;
477 
478 }
479 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol);
480 
481 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv,
482 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
483 {
484 	unsigned int invert = mc->invert;
485 	int max = mc->max;
486 	int err_cnt = 0;
487 	int ret;
488 	int val, i;
489 
490 	val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert);
491 
492 	for (i = 0; i < tas_priv->ndev; i++) {
493 		ret = tasdevice_dev_write(tas_priv, i, mc->reg,
494 			(unsigned int)val);
495 		if (!ret)
496 			continue;
497 		err_cnt++;
498 		dev_err(tas_priv->dev,
499 			"set digital vol err in dev %d\n", i);
500 	}
501 
502 	/* All the devices set error, return 0 */
503 	return (err_cnt == tas_priv->ndev) ? 0 : 1;
504 
505 }
506 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol);
507 
508 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv,
509 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
510 {
511 	unsigned int invert = mc->invert;
512 	int max = mc->max;
513 	int ret, val;
514 
515 	/* Read the primary device as the whole */
516 	ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val);
517 	if (ret) {
518 		dev_err(tas_priv->dev, "%s, get digital vol error\n",
519 			__func__);
520 		goto out;
521 	}
522 
523 	val = tasdevice_clamp(val, max, invert);
524 	ucontrol->value.integer.value[0] = val;
525 
526 out:
527 	return ret;
528 
529 }
530 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol);
531 
532 MODULE_DESCRIPTION("TAS2781 common library");
533 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
534 MODULE_LICENSE("GPL");
535