| 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 <[email protected]> |
| 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 |
|
| 25 |
static 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 |
|
| 37 |
static 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 |
|
| 46 |
static 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, 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, 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 |
|
| 86 |
out: |
| 87 |
return ret; |
| 88 |
} |
| 89 |
|
| 90 |
int 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, 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 |
} |
| 114 |
EXPORT_SYMBOL_GPL(tasdev_chn_switch); |
| 115 |
|
| 116 |
int 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, 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 |
|
| 140 |
out: |
| 141 |
return ret; |
| 142 |
} |
| 143 |
EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits); |
| 144 |
|
| 145 |
struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c) |
| 146 |
{ |
| 147 |
struct tasdevice_priv *tas_priv; |
| 148 |
|
| 149 |
tas_priv = devm_kzalloc(&i2c->dev, 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 |
} |
| 157 |
EXPORT_SYMBOL_GPL(tasdevice_kzalloc); |
| 158 |
|
| 159 |
int 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(tas_priv->regmap)) { |
| 167 |
ret = PTR_ERR(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 |
|
| 190 |
out: |
| 191 |
return ret; |
| 192 |
} |
| 193 |
EXPORT_SYMBOL_GPL(tasdevice_init); |
| 194 |
|
| 195 |
static 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 |
|
| 206 |
int 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(max)) - 1; |
| 216 |
mask <<= mc->shift; |
| 217 |
val = tasdevice_clamp(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 |
} |
| 230 |
EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); |
| 231 |
|
| 232 |
int 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, 0, mc->reg, &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(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 |
|
| 254 |
out: |
| 255 |
return ret; |
| 256 |
|
| 257 |
} |
| 258 |
EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); |
| 259 |
|
| 260 |
int 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, 0, mc->reg, &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 |
|
| 278 |
out: |
| 279 |
return ret; |
| 280 |
|
| 281 |
} |
| 282 |
EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); |
| 283 |
|
| 284 |
int 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(ucontrol->value.integer.value[0], max, invert); |
| 294 |
|
| 295 |
for (i = 0; i < tas_priv->ndev; i++) { |
| 296 |
ret = tasdevice_dev_write(tas_priv, i, mc->reg, |
| 297 |
(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 |
} |
| 309 |
EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); |
| 310 |
|
| 311 |
void tasdevice_reset(struct tasdevice_priv *tas_dev) |
| 312 |
{ |
| 313 |
int ret, i; |
| 314 |
|
| 315 |
if (tas_dev->reset) { |
| 316 |
gpiod_set_value_cansleep(tas_dev->reset, 0); |
| 317 |
usleep_range(500, 1000); |
| 318 |
gpiod_set_value_cansleep(tas_dev->reset, 1); |
| 319 |
} else { |
| 320 |
for (i = 0; i < tas_dev->ndev; i++) { |
| 321 |
ret = tasdevice_dev_write(tas_dev, i, |
| 322 |
TASDEVICE_REG_SWRESET, |
| 323 |
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(1000, 1050); |
| 333 |
} |
| 334 |
EXPORT_SYMBOL_GPL(tasdevice_reset); |
| 335 |
|
| 336 |
int 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(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin", |
| 349 |
tas_priv->name_prefix, tas_priv->dev_name, |
| 350 |
tas_priv->ndev); |
| 351 |
else |
| 352 |
scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", |
| 353 |
tas_priv->dev_name, tas_priv->ndev); |
| 354 |
crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); |
| 355 |
tas_priv->codec = codec; |
| 356 |
ret = request_firmware_nowait(module, FW_ACTION_UEVENT, |
| 357 |
tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, 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(&tas_priv->codec_lock); |
| 365 |
return ret; |
| 366 |
} |
| 367 |
EXPORT_SYMBOL_GPL(tascodec_init); |
| 368 |
|
| 369 |
MODULE_DESCRIPTION("TAS2781 common library for I2C"); |
| 370 |
MODULE_AUTHOR("Shenghao Ding, TI, <[email protected]>"); |
| 371 |
MODULE_LICENSE("GPL"); |