1// SPDX-License-Identifier: GPL-2.0
2//
3// TAS2563/TAS2781 Common functions for HDA and ASoC Audio drivers based on I2C
4//
5// Copyright 2025 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#include <sound/tas2781-comlib-i2c.h>
24
25static const struct regmap_range_cfg tasdevice_ranges[] = {
26 {
27 .range_min = 0,
28 .range_max = 256 * 128,
29 .selector_reg = TASDEVICE_PAGE_SELECT,
30 .selector_mask = 0xff,
31 .selector_shift = 0,
32 .window_start = 0,
33 .window_len = 128,
34 },
35};
36
37static const struct regmap_config tasdevice_regmap = {
38 .reg_bits = 8,
39 .val_bits = 8,
40 .cache_type = REGCACHE_NONE,
41 .ranges = tasdevice_ranges,
42 .num_ranges = ARRAY_SIZE(tasdevice_ranges),
43 .max_register = 256 * 128,
44};
45
46static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv,
47 unsigned short chn, int book)
48{
49 struct i2c_client *client = (struct i2c_client *)tas_priv->client;
50 int ret = 0;
51
52 if (chn < tas_priv->ndev) {
53 struct tasdevice *tasdev = &tas_priv->tasdevice[chn];
54 struct regmap *map = tas_priv->regmap;
55
56 if (client->addr != tasdev->dev_addr) {
57 client->addr = tasdev->dev_addr;
58 /* All tas2781s share the same regmap, clear the page
59 * inside regmap once switching to another tas2781.
60 * Register 0 at any pages and any books inside tas2781
61 * is the same one for page-switching.
62 */
63 ret = regmap_write(map, TASDEVICE_PAGE_SELECT, val: 0);
64 if (ret < 0) {
65 dev_err(tas_priv->dev, "%s, E=%d channel:%d\n",
66 __func__, ret, chn);
67 goto out;
68 }
69 }
70
71 if (tasdev->cur_book != book) {
72 ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, val: book);
73 if (ret < 0) {
74 dev_err(tas_priv->dev, "%s, E=%d\n",
75 __func__, ret);
76 goto out;
77 }
78 tasdev->cur_book = book;
79 }
80 } else {
81 ret = -EINVAL;
82 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
83 chn);
84 }
85
86out:
87 return ret;
88}
89
90int tasdev_chn_switch(struct tasdevice_priv *tas_priv,
91 unsigned short chn)
92{
93 struct i2c_client *client = (struct i2c_client *)tas_priv->client;
94 struct tasdevice *tasdev = &tas_priv->tasdevice[chn];
95 struct regmap *map = tas_priv->regmap;
96 int ret;
97
98 if (client->addr != tasdev->dev_addr) {
99 client->addr = tasdev->dev_addr;
100 /* All devices share the same regmap, clear the page
101 * inside regmap once switching to another device.
102 * Register 0 at any pages and any books inside tas2781
103 * is the same one for page-switching.
104 */
105 ret = regmap_write(map, TASDEVICE_PAGE_SELECT, val: 0);
106 if (ret < 0) {
107 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
108 return ret;
109 }
110 return 1;
111 }
112 return 0;
113}
114EXPORT_SYMBOL_GPL(tasdev_chn_switch);
115
116int tasdevice_dev_update_bits(
117 struct tasdevice_priv *tas_priv, unsigned short chn,
118 unsigned int reg, unsigned int mask, unsigned int value)
119{
120 int ret = 0;
121
122 if (chn < tas_priv->ndev) {
123 struct regmap *map = tas_priv->regmap;
124
125 ret = tas_priv->change_chn_book(tas_priv, chn,
126 TASDEVICE_BOOK_ID(reg));
127 if (ret < 0)
128 goto out;
129
130 ret = regmap_update_bits(map, TASDEVICE_PGRG(reg),
131 mask, val: value);
132 if (ret < 0)
133 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
134 } else {
135 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__,
136 chn);
137 ret = -EINVAL;
138 }
139
140out:
141 return ret;
142}
143EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits);
144
145struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c)
146{
147 struct tasdevice_priv *tas_priv;
148
149 tas_priv = devm_kzalloc(dev: &i2c->dev, size: sizeof(*tas_priv), GFP_KERNEL);
150 if (!tas_priv)
151 return NULL;
152 tas_priv->dev = &i2c->dev;
153 tas_priv->client = (void *)i2c;
154
155 return tas_priv;
156}
157EXPORT_SYMBOL_GPL(tasdevice_kzalloc);
158
159int tasdevice_init(struct tasdevice_priv *tas_priv)
160{
161 int ret = 0;
162 int i;
163
164 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client,
165 &tasdevice_regmap);
166 if (IS_ERR(ptr: tas_priv->regmap)) {
167 ret = PTR_ERR(ptr: tas_priv->regmap);
168 dev_err(tas_priv->dev, "Failed to allocate register map: %d\n",
169 ret);
170 goto out;
171 }
172
173 tas_priv->cur_prog = -1;
174 tas_priv->cur_conf = -1;
175 tas_priv->isspi = false;
176
177 for (i = 0; i < tas_priv->ndev; i++) {
178 tas_priv->tasdevice[i].cur_book = -1;
179 tas_priv->tasdevice[i].cur_prog = -1;
180 tas_priv->tasdevice[i].cur_conf = -1;
181 }
182
183 tas_priv->update_bits = tasdevice_dev_update_bits;
184 tas_priv->change_chn_book = tasdevice_change_chn_book;
185 tas_priv->dev_read = tasdevice_dev_read;
186 tas_priv->dev_bulk_read = tasdevice_dev_bulk_read;
187
188 mutex_init(&tas_priv->codec_lock);
189
190out:
191 return ret;
192}
193EXPORT_SYMBOL_GPL(tasdevice_init);
194
195static int tasdevice_clamp(int val, int max, unsigned int invert)
196{
197 if (val > max)
198 val = max;
199 if (invert)
200 val = max - val;
201 if (val < 0)
202 val = 0;
203 return val;
204}
205
206int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv,
207 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
208{
209 unsigned int invert = mc->invert;
210 unsigned char mask;
211 int max = mc->max;
212 int err_cnt = 0;
213 int val, i, ret;
214
215 mask = (1 << fls(x: max)) - 1;
216 mask <<= mc->shift;
217 val = tasdevice_clamp(val: ucontrol->value.integer.value[0], max, invert);
218 for (i = 0; i < tas_priv->ndev; i++) {
219 ret = tasdevice_dev_update_bits(tas_priv, i,
220 mc->reg, mask, (unsigned int)(val << mc->shift));
221 if (!ret)
222 continue;
223 err_cnt++;
224 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i);
225 }
226
227 /* All the devices set error, return 0 */
228 return (err_cnt == tas_priv->ndev) ? 0 : 1;
229}
230EXPORT_SYMBOL_GPL(tasdevice_amp_putvol);
231
232int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv,
233 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
234{
235 unsigned int invert = mc->invert;
236 unsigned char mask = 0;
237 int max = mc->max;
238 int ret = 0;
239 int val;
240
241 /* Read the primary device */
242 ret = tasdevice_dev_read(tas_priv, chn: 0, reg: mc->reg, value: &val);
243 if (ret) {
244 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
245 goto out;
246 }
247
248 mask = (1 << fls(x: max)) - 1;
249 mask <<= mc->shift;
250 val = (val & mask) >> mc->shift;
251 val = tasdevice_clamp(val, max, invert);
252 ucontrol->value.integer.value[0] = val;
253
254out:
255 return ret;
256
257}
258EXPORT_SYMBOL_GPL(tasdevice_amp_getvol);
259
260int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv,
261 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
262{
263 unsigned int invert = mc->invert;
264 int max = mc->max;
265 int ret, val;
266
267 /* Read the primary device as the whole */
268 ret = tasdevice_dev_read(tas_priv, chn: 0, reg: mc->reg, value: &val);
269 if (ret) {
270 dev_err(tas_priv->dev, "%s, get digital vol error\n",
271 __func__);
272 goto out;
273 }
274
275 val = tasdevice_clamp(val, max, invert);
276 ucontrol->value.integer.value[0] = val;
277
278out:
279 return ret;
280
281}
282EXPORT_SYMBOL_GPL(tasdevice_digital_getvol);
283
284int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv,
285 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
286{
287 unsigned int invert = mc->invert;
288 int max = mc->max;
289 int err_cnt = 0;
290 int ret;
291 int val, i;
292
293 val = tasdevice_clamp(val: ucontrol->value.integer.value[0], max, invert);
294
295 for (i = 0; i < tas_priv->ndev; i++) {
296 ret = tasdevice_dev_write(tas_priv, chn: i, reg: mc->reg,
297 value: (unsigned int)val);
298 if (!ret)
299 continue;
300 err_cnt++;
301 dev_err(tas_priv->dev,
302 "set digital vol err in dev %d\n", i);
303 }
304
305 /* All the devices set error, return 0 */
306 return (err_cnt == tas_priv->ndev) ? 0 : 1;
307
308}
309EXPORT_SYMBOL_GPL(tasdevice_digital_putvol);
310
311void tasdevice_reset(struct tasdevice_priv *tas_dev)
312{
313 int ret, i;
314
315 if (tas_dev->reset) {
316 gpiod_set_value_cansleep(desc: tas_dev->reset, value: 0);
317 usleep_range(min: 500, max: 1000);
318 gpiod_set_value_cansleep(desc: tas_dev->reset, value: 1);
319 } else {
320 for (i = 0; i < tas_dev->ndev; i++) {
321 ret = tasdevice_dev_write(tas_priv: tas_dev, chn: i,
322 TASDEVICE_REG_SWRESET,
323 value: tas_dev->chip_id >= TAS5802 ?
324 TAS5825_REG_SWRESET_RESET :
325 TASDEVICE_REG_SWRESET_RESET);
326 if (ret < 0)
327 dev_err(tas_dev->dev,
328 "dev %d swreset fail, %d\n",
329 i, ret);
330 }
331 }
332 usleep_range(min: 1000, max: 1050);
333}
334EXPORT_SYMBOL_GPL(tasdevice_reset);
335
336int tascodec_init(struct tasdevice_priv *tas_priv, void *codec,
337 struct module *module,
338 void (*cont)(const struct firmware *fw, void *context))
339{
340 int ret = 0;
341
342 /* Codec Lock Hold to ensure that codec_probe and firmware parsing and
343 * loading do not simultaneously execute.
344 */
345 mutex_lock(&tas_priv->codec_lock);
346
347 if (tas_priv->name_prefix)
348 scnprintf(buf: tas_priv->rca_binaryname, size: 64, fmt: "%s-%sRCA%d.bin",
349 tas_priv->name_prefix, tas_priv->dev_name,
350 tas_priv->ndev);
351 else
352 scnprintf(buf: tas_priv->rca_binaryname, size: 64, fmt: "%sRCA%d.bin",
353 tas_priv->dev_name, tas_priv->ndev);
354 crc8_populate_msb(table: tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
355 tas_priv->codec = codec;
356 ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
357 name: tas_priv->rca_binaryname, device: tas_priv->dev, GFP_KERNEL, context: tas_priv,
358 cont);
359 if (ret)
360 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
361 ret);
362
363 /* Codec Lock Release*/
364 mutex_unlock(lock: &tas_priv->codec_lock);
365 return ret;
366}
367EXPORT_SYMBOL_GPL(tascodec_init);
368
369MODULE_DESCRIPTION("TAS2781 common library for I2C");
370MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
371MODULE_LICENSE("GPL");
372

source code of linux/sound/soc/codecs/tas2781-comlib-i2c.c