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/cleanup.h> 10 #include <linux/crc8.h> 11 #include <linux/firmware.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/i2c.h> 14 #include <linux/init.h> 15 #include <linux/interrupt.h> 16 #include <linux/module.h> 17 #include <linux/of.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 #include <sound/tas2781-comlib-i2c.h> 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_update_bits( 118 struct tasdevice_priv *tas_priv, unsigned short chn, 119 unsigned int reg, unsigned int mask, 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 = tas_priv->change_chn_book(tas_priv, chn, 127 TASDEVICE_BOOK_ID(reg)); 128 if (ret < 0) 129 goto out; 130 131 ret = regmap_update_bits(map, TASDEVICE_PGRG(reg), 132 mask, value); 133 if (ret < 0) 134 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 135 } else { 136 dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 137 chn); 138 ret = -EINVAL; 139 } 140 141 out: 142 return ret; 143 } 144 EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits); 145 146 struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c) 147 { 148 struct tasdevice_priv *tas_priv; 149 150 tas_priv = devm_kzalloc(&i2c->dev, sizeof(*tas_priv), GFP_KERNEL); 151 if (!tas_priv) 152 return NULL; 153 tas_priv->dev = &i2c->dev; 154 tas_priv->client = (void *)i2c; 155 156 return tas_priv; 157 } 158 EXPORT_SYMBOL_GPL(tasdevice_kzalloc); 159 160 int tasdevice_init(struct tasdevice_priv *tas_priv) 161 { 162 int ret = 0; 163 int i; 164 165 tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client, 166 &tasdevice_regmap); 167 if (IS_ERR(tas_priv->regmap)) { 168 ret = PTR_ERR(tas_priv->regmap); 169 dev_err(tas_priv->dev, "Failed to allocate register map: %d\n", 170 ret); 171 goto out; 172 } 173 174 tas_priv->cur_prog = -1; 175 tas_priv->cur_conf = -1; 176 tas_priv->isspi = false; 177 178 for (i = 0; i < tas_priv->ndev; i++) { 179 tas_priv->tasdevice[i].cur_book = -1; 180 tas_priv->tasdevice[i].cur_prog = -1; 181 tas_priv->tasdevice[i].cur_conf = -1; 182 } 183 184 tas_priv->update_bits = tasdevice_dev_update_bits; 185 tas_priv->change_chn_book = tasdevice_change_chn_book; 186 tas_priv->dev_read = tasdevice_dev_read; 187 tas_priv->dev_bulk_read = tasdevice_dev_bulk_read; 188 189 mutex_init(&tas_priv->codec_lock); 190 191 out: 192 return ret; 193 } 194 EXPORT_SYMBOL_GPL(tasdevice_init); 195 196 static int tasdevice_clamp(int val, int max, unsigned int invert) 197 { 198 if (val > max) 199 val = max; 200 if (invert) 201 val = max - val; 202 if (val < 0) 203 val = 0; 204 return val; 205 } 206 207 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 208 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 209 { 210 unsigned int invert = mc->invert; 211 unsigned char mask; 212 int max = mc->max; 213 int err_cnt = 0; 214 int val, i, ret; 215 216 mask = (1 << fls(max)) - 1; 217 mask <<= mc->shift; 218 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 219 for (i = 0; i < tas_priv->ndev; i++) { 220 ret = tasdevice_dev_update_bits(tas_priv, i, 221 mc->reg, mask, (unsigned int)(val << mc->shift)); 222 if (!ret) 223 continue; 224 err_cnt++; 225 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i); 226 } 227 228 /* All the devices set error, return 0 */ 229 return (err_cnt == tas_priv->ndev) ? 0 : 1; 230 } 231 EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); 232 233 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 234 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 235 { 236 unsigned int invert = mc->invert; 237 unsigned char mask = 0; 238 int max = mc->max; 239 int ret = 0; 240 int val; 241 242 /* Read the primary device */ 243 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 244 if (ret) { 245 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 246 goto out; 247 } 248 249 mask = (1 << fls(max)) - 1; 250 mask <<= mc->shift; 251 val = (val & mask) >> mc->shift; 252 val = tasdevice_clamp(val, max, invert); 253 ucontrol->value.integer.value[0] = val; 254 255 out: 256 return ret; 257 258 } 259 EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); 260 261 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 262 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 263 { 264 unsigned int invert = mc->invert; 265 int max = mc->max; 266 int ret, val; 267 268 /* Read the primary device as the whole */ 269 ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 270 if (ret) { 271 dev_err(tas_priv->dev, "%s, get digital vol error\n", 272 __func__); 273 goto out; 274 } 275 276 val = tasdevice_clamp(val, max, invert); 277 ucontrol->value.integer.value[0] = val; 278 279 out: 280 return ret; 281 282 } 283 EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); 284 285 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 286 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 287 { 288 unsigned int invert = mc->invert; 289 int max = mc->max; 290 int err_cnt = 0; 291 int ret; 292 int val, i; 293 294 val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 295 296 for (i = 0; i < tas_priv->ndev; i++) { 297 ret = tasdevice_dev_write(tas_priv, i, mc->reg, 298 (unsigned int)val); 299 if (!ret) 300 continue; 301 err_cnt++; 302 dev_err(tas_priv->dev, 303 "set digital vol err in dev %d\n", i); 304 } 305 306 /* All the devices set error, return 0 */ 307 return (err_cnt == tas_priv->ndev) ? 0 : 1; 308 309 } 310 EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); 311 312 void tasdevice_reset(struct tasdevice_priv *tas_dev) 313 { 314 int ret, i; 315 316 if (tas_dev->reset) { 317 gpiod_set_value_cansleep(tas_dev->reset, 0); 318 usleep_range(500, 1000); 319 gpiod_set_value_cansleep(tas_dev->reset, 1); 320 } else { 321 for (i = 0; i < tas_dev->ndev; i++) { 322 ret = tasdevice_dev_write(tas_dev, i, 323 TASDEVICE_REG_SWRESET, 324 tas_dev->chip_id >= TAS5802 ? 325 TAS5825_REG_SWRESET_RESET : 326 TASDEVICE_REG_SWRESET_RESET); 327 if (ret < 0) 328 dev_err(tas_dev->dev, 329 "dev %d swreset fail, %d\n", 330 i, ret); 331 } 332 } 333 usleep_range(1000, 1050); 334 } 335 EXPORT_SYMBOL_GPL(tasdevice_reset); 336 337 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 338 struct module *module, 339 void (*cont)(const struct firmware *fw, void *context)) 340 { 341 int ret = 0; 342 343 /* Codec Lock Hold to ensure that codec_probe and firmware parsing and 344 * loading do not simultaneously execute. 345 */ 346 guard(mutex)(&tas_priv->codec_lock); 347 348 if (tas_priv->name_prefix) 349 scnprintf(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin", 350 tas_priv->name_prefix, tas_priv->dev_name, 351 tas_priv->ndev); 352 else 353 scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", 354 tas_priv->dev_name, tas_priv->ndev); 355 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 356 tas_priv->codec = codec; 357 ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 358 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 359 cont); 360 if (ret) 361 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 362 ret); 363 364 return ret; 365 } 366 EXPORT_SYMBOL_GPL(tascodec_init); 367 368 MODULE_DESCRIPTION("TAS2781 common library for I2C"); 369 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>"); 370 MODULE_LICENSE("GPL"); 371