1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // fs210x.c -- Driver for the FS2104/5S Audio Amplifier 4 // 5 // Copyright (C) 2016-2025 Shanghai FourSemi Semiconductor Co.,Ltd. 6 7 #include <linux/cleanup.h> 8 #include <linux/clk.h> 9 #include <linux/delay.h> 10 #include <linux/i2c.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/of.h> 15 #include <linux/regmap.h> 16 #include <linux/regulator/consumer.h> 17 #include <linux/workqueue.h> 18 #include <sound/soc.h> 19 #include <sound/tlv.h> 20 21 #include "fs210x.h" 22 #include "fs-amp-lib.h" 23 24 #define FS210X_DEFAULT_FWM_NAME "fs210x_fwm.bin" 25 #define FS210X_DEFAULT_DAI_NAME "fs210x-aif" 26 #define FS2105S_DEVICE_ID 0x20 /* FS2105S */ 27 #define FS210X_DEVICE_ID 0x45 /* FS2104 */ 28 #define FS210X_REG_MAX 0xF8 29 #define FS210X_INIT_SCENE 0 30 #define FS210X_DEFAULT_SCENE 1 31 #define FS210X_START_DELAY_MS 5 32 #define FS210X_FAULT_CHECK_INTERVAL_MS 2000 33 #define FS2105S_RATES (SNDRV_PCM_RATE_32000 | \ 34 SNDRV_PCM_RATE_44100 | \ 35 SNDRV_PCM_RATE_48000 | \ 36 SNDRV_PCM_RATE_88200 | \ 37 SNDRV_PCM_RATE_96000) 38 #define FS210X_RATES (SNDRV_PCM_RATE_16000 | FS2105S_RATES) 39 #define FS210X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ 40 SNDRV_PCM_FMTBIT_S24_LE | \ 41 SNDRV_PCM_FMTBIT_S24_3LE | \ 42 SNDRV_PCM_FMTBIT_S32_LE) 43 #define FS210X_NUM_SUPPLIES ARRAY_SIZE(fs210x_supply_names) 44 45 static const char *const fs210x_supply_names[] = { 46 "pvdd", 47 "dvdd", 48 }; 49 50 struct fs210x_platform_data { 51 const char *fwm_name; 52 }; 53 54 struct fs210x_priv { 55 struct i2c_client *i2c; 56 struct device *dev; 57 struct regmap *regmap; 58 struct fs210x_platform_data pdata; 59 struct regulator_bulk_data supplies[FS210X_NUM_SUPPLIES]; 60 struct gpio_desc *gpio_sdz; 61 struct delayed_work start_work; 62 struct delayed_work fault_check_work; 63 struct fs_amp_lib amp_lib; 64 const struct fs_amp_scene *cur_scene; 65 struct clk *clk_bclk; 66 /* 67 * @lock: Mutex ensuring exclusive access for critical device operations 68 * 69 * This lock serializes access between the following actions: 70 * - Device initialization procedures(probe) 71 * - Enable/disable device(DAPM event) 72 * - Suspend/resume device(PM) 73 * - Runtime scene switching(control) 74 * - Scheduling/execution of delayed works items(delayed works) 75 */ 76 struct mutex lock; 77 unsigned int check_interval_ms; 78 unsigned int bclk; 79 unsigned int srate; 80 int scene_id; 81 u16 devid; 82 bool is_inited; 83 bool is_suspended; 84 bool is_bclk_on; 85 bool is_playing; 86 }; 87 88 static const unsigned int fs2105s_rates[] = { 89 32000, 44100, 48000, 88200, 96000 90 }; 91 92 static const struct snd_pcm_hw_constraint_list fs2105s_constraints = { 93 .count = ARRAY_SIZE(fs2105s_rates), 94 .list = fs2105s_rates, 95 }; 96 97 static const unsigned int fs210x_rates[] = { 98 16000, 32000, 44100, 48000, 88200, 96000 99 }; 100 101 static const struct snd_pcm_hw_constraint_list fs210x_constraints = { 102 .count = ARRAY_SIZE(fs210x_rates), 103 .list = fs210x_rates, 104 }; 105 106 static const struct fs_pll_div fs210x_pll_div[] = { 107 /* bclk, pll1, pll2, pll3 */ 108 { 512000, 0x006C, 0x0120, 0x0001 }, 109 { 768000, 0x016C, 0x00C0, 0x0001 }, 110 { 1024000, 0x016C, 0x0090, 0x0001 }, 111 { 1536000, 0x016C, 0x0060, 0x0001 }, 112 { 2048000, 0x016C, 0x0090, 0x0002 }, 113 { 2304000, 0x016C, 0x0080, 0x0002 }, 114 { 3072000, 0x016C, 0x0090, 0x0003 }, 115 { 4096000, 0x016C, 0x0090, 0x0004 }, 116 { 4608000, 0x016C, 0x0080, 0x0004 }, 117 { 6144000, 0x016C, 0x0090, 0x0006 }, 118 { 8192000, 0x016C, 0x0090, 0x0008 }, 119 { 9216000, 0x016C, 0x0090, 0x0009 }, 120 { 12288000, 0x016C, 0x0090, 0x000C }, 121 { 16384000, 0x016C, 0x0090, 0x0010 }, 122 { 18432000, 0x016C, 0x0090, 0x0012 }, 123 { 24576000, 0x016C, 0x0090, 0x0018 }, 124 { 1411200, 0x016C, 0x0060, 0x0001 }, 125 { 2116800, 0x016C, 0x0080, 0x0002 }, 126 { 2822400, 0x016C, 0x0090, 0x0003 }, 127 { 4233600, 0x016C, 0x0080, 0x0004 }, 128 { 5644800, 0x016C, 0x0090, 0x0006 }, 129 { 8467200, 0x016C, 0x0090, 0x0009 }, 130 { 11289600, 0x016C, 0x0090, 0x000C }, 131 { 16934400, 0x016C, 0x0090, 0x0012 }, 132 { 22579200, 0x016C, 0x0090, 0x0018 }, 133 { 2000000, 0x017C, 0x0093, 0x0002 }, 134 }; 135 136 static int fs210x_bclk_set(struct fs210x_priv *fs210x, bool on) 137 { 138 int ret = 0; 139 140 if (!fs210x || !fs210x->dev) 141 return -EINVAL; 142 143 if ((fs210x->is_bclk_on ^ on) == 0) 144 return 0; 145 146 if (on) { 147 clk_set_rate(fs210x->clk_bclk, fs210x->bclk); 148 ret = clk_prepare_enable(fs210x->clk_bclk); 149 fs210x->is_bclk_on = true; 150 fsleep(2000); /* >= 2ms */ 151 } else { 152 clk_disable_unprepare(fs210x->clk_bclk); 153 fs210x->is_bclk_on = false; 154 } 155 156 return ret; 157 } 158 159 static int fs210x_reg_write(struct fs210x_priv *fs210x, 160 u8 reg, u16 val) 161 { 162 int ret; 163 164 ret = regmap_write(fs210x->regmap, reg, val); 165 if (ret) { 166 dev_err(fs210x->dev, "Failed to write %02Xh: %d\n", reg, ret); 167 return ret; 168 } 169 170 return 0; 171 } 172 173 static int fs210x_reg_read(struct fs210x_priv *fs210x, 174 u8 reg, u16 *pval) 175 { 176 unsigned int val; 177 int ret; 178 179 ret = regmap_read(fs210x->regmap, reg, &val); 180 if (ret) { 181 dev_err(fs210x->dev, "Failed to read %02Xh: %d\n", reg, ret); 182 return ret; 183 } 184 185 *pval = (u16)val; 186 187 return 0; 188 } 189 190 static int fs210x_reg_update_bits(struct fs210x_priv *fs210x, 191 u8 reg, u16 mask, u16 val) 192 { 193 int ret; 194 195 ret = regmap_update_bits(fs210x->regmap, reg, mask, val); 196 if (ret) { 197 dev_err(fs210x->dev, "Failed to update %02Xh: %d\n", reg, ret); 198 return ret; 199 } 200 201 return 0; 202 } 203 204 static int fs210x_reg_bulk_write(struct fs210x_priv *fs210x, 205 u8 reg, const void *val, u32 size) 206 { 207 int ret; 208 209 ret = regmap_bulk_write(fs210x->regmap, reg, val, size / 2); 210 if (ret) { 211 dev_err(fs210x->dev, "Failed to bulk write %02Xh: %d\n", 212 reg, ret); 213 return ret; 214 } 215 216 return 0; 217 } 218 219 static inline int fs210x_write_reg_val(struct fs210x_priv *fs210x, 220 const struct fs_reg_val *regv) 221 { 222 return fs210x_reg_write(fs210x, regv->reg, regv->val); 223 } 224 225 static inline int fs210x_write_reg_bits(struct fs210x_priv *fs210x, 226 const struct fs_reg_bits *regu) 227 { 228 return fs210x_reg_update_bits(fs210x, 229 regu->reg, 230 regu->mask, 231 regu->val); 232 } 233 234 static inline int fs210x_set_cmd_pkg(struct fs210x_priv *fs210x, 235 const struct fs_cmd_pkg *pkg, 236 unsigned int *offset) 237 { 238 int delay_us; 239 240 if (pkg->cmd >= 0x00 && pkg->cmd <= FS210X_REG_MAX) { 241 *offset = sizeof(pkg->regv); 242 return fs210x_write_reg_val(fs210x, &pkg->regv); 243 } else if (pkg->cmd == FS_CMD_UPDATE) { 244 *offset = sizeof(pkg->regb); 245 return fs210x_write_reg_bits(fs210x, &pkg->regb); 246 } else if (pkg->cmd == FS_CMD_DELAY) { 247 if (pkg->regv.val > FS_CMD_DELAY_MS_MAX) 248 return -EOPNOTSUPP; 249 delay_us = pkg->regv.val * 1000; /* ms -> us */ 250 fsleep(delay_us); 251 *offset = sizeof(pkg->regv); 252 return 0; 253 } 254 255 dev_err(fs210x->dev, "Invalid pkg cmd: %d\n", pkg->cmd); 256 257 return -EOPNOTSUPP; 258 } 259 260 static int fs210x_reg_write_table(struct fs210x_priv *fs210x, 261 const struct fs_reg_table *reg) 262 { 263 const struct fs_cmd_pkg *pkg; 264 unsigned int index, offset; 265 int ret; 266 267 if (!fs210x || !fs210x->dev) 268 return -EINVAL; 269 270 if (!reg || reg->size == 0) 271 return -EFAULT; 272 273 for (index = 0; index < reg->size; index += offset) { 274 pkg = (struct fs_cmd_pkg *)(reg->buf + index); 275 ret = fs210x_set_cmd_pkg(fs210x, pkg, &offset); 276 if (ret) { 277 dev_err(fs210x->dev, "Failed to set cmd pkg: %02X-%d\n", 278 pkg->cmd, ret); 279 return ret; 280 } 281 } 282 283 if (index != reg->size) { 284 dev_err(fs210x->dev, "Invalid reg table size: %d-%d\n", 285 index, reg->size); 286 return -EFAULT; 287 } 288 289 return 0; 290 } 291 292 static int fs210x_dev_play(struct fs210x_priv *fs210x) 293 { 294 int ret; 295 296 if (!fs210x->is_inited) 297 return -EFAULT; 298 299 if (fs210x->is_playing) 300 return 0; 301 302 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL, 303 FS210X_11H_DPS_PLAY); 304 if (!ret) 305 fs210x->is_playing = true; 306 307 fsleep(10000); /* >= 10ms */ 308 309 return ret; 310 } 311 312 static int fs210x_dev_stop(struct fs210x_priv *fs210x) 313 { 314 int ret; 315 316 if (!fs210x->is_inited) 317 return -EFAULT; 318 319 if (!fs210x->is_playing) 320 return 0; 321 322 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL, 323 FS210X_11H_DPS_PWDN); 324 fs210x->is_playing = false; 325 326 fsleep(30000); /* >= 30ms */ 327 328 return ret; 329 } 330 331 static int fs210x_set_reg_table(struct fs210x_priv *fs210x, 332 const struct fs_amp_scene *scene) 333 { 334 const struct fs_amp_scene *cur_scene; 335 const struct fs_reg_table *reg; 336 337 if (!fs210x || !fs210x->dev || !scene) 338 return -EINVAL; 339 340 cur_scene = fs210x->cur_scene; 341 if (!scene->reg || cur_scene == scene) { 342 dev_dbg(fs210x->dev, "Skip writing reg table\n"); 343 return 0; 344 } 345 346 reg = scene->reg; 347 dev_dbg(fs210x->dev, "reg table size: %d\n", reg->size); 348 349 return fs210x_reg_write_table(fs210x, reg); 350 } 351 352 static int fs210x_set_woofer_table(struct fs210x_priv *fs210x) 353 { 354 const struct fs_file_table *woofer; 355 const struct fs_fwm_table *table; 356 int ret; 357 358 if (!fs210x || !fs210x->dev) 359 return -EINVAL; 360 361 /* NOTE: fs2105s has woofer ram only */ 362 if (fs210x->devid != FS2105S_DEVICE_ID) 363 return 0; 364 365 table = fs210x->amp_lib.table[FS_INDEX_WOOFER]; 366 if (!table) { 367 dev_dbg(fs210x->dev, "Skip writing woofer table\n"); 368 return 0; 369 } 370 371 woofer = (struct fs_file_table *)table->buf; 372 dev_dbg(fs210x->dev, "woofer table size: %d\n", woofer->size); 373 /* Unit of woofer data is u32(4 bytes) */ 374 if (woofer->size == 0 || (woofer->size & 0x3)) { 375 dev_err(fs210x->dev, "Invalid woofer size: %d\n", 376 woofer->size); 377 return -EINVAL; 378 } 379 380 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA, 381 FS2105S_46H_CAM_BURST_W); 382 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL, 383 woofer->buf, woofer->size); 384 385 return ret; 386 } 387 388 static int fs210x_set_effect_table(struct fs210x_priv *fs210x, 389 const struct fs_amp_scene *scene) 390 { 391 const struct fs_amp_scene *cur_scene; 392 const struct fs_file_table *effect; 393 int half_size; 394 int ret; 395 396 if (!fs210x || !fs210x->dev || !scene) 397 return -EINVAL; 398 399 cur_scene = fs210x->cur_scene; 400 if (!scene->effect || cur_scene == scene) { 401 dev_dbg(fs210x->dev, "Skip writing effect table\n"); 402 return 0; 403 } 404 405 effect = scene->effect; 406 dev_dbg(fs210x->dev, "effect table size: %d\n", effect->size); 407 408 /* Unit of effect data is u32(4 bytes), 2 channels */ 409 if (effect->size == 0 || (effect->size & 0x7)) { 410 dev_err(fs210x->dev, "Invalid effect size: %d\n", 411 effect->size); 412 return -EINVAL; 413 } 414 415 half_size = effect->size / 2; 416 417 /* Left channel */ 418 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA, 419 FS210X_46H_CAM_BURST_L); 420 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL, 421 effect->buf, half_size); 422 if (ret) 423 return ret; 424 425 /* Right channel */ 426 ret = fs210x_reg_write(fs210x, FS210X_46H_DACEQA, 427 FS210X_46H_CAM_BURST_R); 428 ret |= fs210x_reg_bulk_write(fs210x, FS210X_42H_DACEQWL, 429 effect->buf + half_size, half_size); 430 431 return ret; 432 } 433 434 static int fs210x_access_dsp_ram(struct fs210x_priv *fs210x, bool enable) 435 { 436 int ret; 437 438 if (!fs210x || !fs210x->dev) 439 return -EINVAL; 440 441 if (enable) { 442 ret = fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL, 443 FS210X_11H_DPS_HIZ); 444 ret |= fs210x_reg_write(fs210x, FS210X_0BH_ACCKEY, 445 FS210X_0BH_ACCKEY_ON); 446 } else { 447 ret = fs210x_reg_write(fs210x, FS210X_0BH_ACCKEY, 448 FS210X_0BH_ACCKEY_OFF); 449 ret |= fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL, 450 FS210X_11H_DPS_PWDN); 451 } 452 453 fsleep(10000); /* >= 10ms */ 454 455 return ret; 456 } 457 458 static int fs210x_write_dsp_effect(struct fs210x_priv *fs210x, 459 const struct fs_amp_scene *scene, 460 int scene_id) 461 { 462 int ret; 463 464 if (!fs210x || !scene) 465 return -EINVAL; 466 467 ret = fs210x_access_dsp_ram(fs210x, true); 468 if (ret) { 469 dev_err(fs210x->dev, "Failed to access dsp: %d\n", ret); 470 goto tag_exit; 471 } 472 473 ret = fs210x_set_effect_table(fs210x, scene); 474 if (ret) { 475 dev_err(fs210x->dev, "Failed to set effect: %d\n", ret); 476 goto tag_exit; 477 } 478 479 if (scene_id == FS210X_INIT_SCENE) 480 ret = fs210x_set_woofer_table(fs210x); 481 482 tag_exit: 483 fs210x_reg_write(fs210x, FS210X_46H_DACEQA, 484 FS210X_46H_CAM_CLEAR); 485 fs210x_access_dsp_ram(fs210x, false); 486 487 return ret; 488 } 489 490 static int fs210x_check_scene(struct fs210x_priv *fs210x, 491 int scene_id, bool *skip_set) 492 { 493 struct fs_amp_lib *amp_lib; 494 495 if (!fs210x || !skip_set) 496 return -EINVAL; 497 498 amp_lib = &fs210x->amp_lib; 499 if (amp_lib->scene_count == 0 || !amp_lib->scene) { 500 dev_err(fs210x->dev, "There's no scene data\n"); 501 return -EINVAL; 502 } 503 504 if (scene_id < 0 || scene_id >= amp_lib->scene_count) { 505 dev_err(fs210x->dev, "Invalid scene_id: %d\n", scene_id); 506 return -EINVAL; 507 } 508 509 if (fs210x->scene_id == scene_id) { 510 dev_dbg(fs210x->dev, "Skip to set same scene\n"); 511 return 0; 512 } 513 514 *skip_set = false; 515 516 return 0; 517 } 518 519 static int fs210x_set_scene(struct fs210x_priv *fs210x, int scene_id) 520 { 521 const struct fs_amp_scene *scene; 522 bool skip_set = true; 523 bool is_playing; 524 int ret; 525 526 if (!fs210x || !fs210x->dev) 527 return -EINVAL; 528 529 ret = fs210x_check_scene(fs210x, scene_id, &skip_set); 530 if (ret || skip_set) 531 return ret; 532 533 scene = fs210x->amp_lib.scene + scene_id; 534 dev_info(fs210x->dev, "Switch scene.%d: %s\n", 535 scene_id, scene->name); 536 537 is_playing = fs210x->is_playing; 538 if (is_playing) 539 fs210x_dev_stop(fs210x); 540 541 ret = fs210x_set_reg_table(fs210x, scene); 542 if (ret) { 543 dev_err(fs210x->dev, "Failed to set reg: %d\n", ret); 544 return ret; 545 } 546 547 ret = fs210x_write_dsp_effect(fs210x, scene, scene_id); 548 if (ret) { 549 dev_err(fs210x->dev, "Failed to write ram: %d\n", ret); 550 return ret; 551 } 552 553 fs210x->cur_scene = scene; 554 fs210x->scene_id = scene_id; 555 556 if (is_playing) 557 fs210x_dev_play(fs210x); 558 559 return 0; 560 } 561 562 static int fs210x_init_chip(struct fs210x_priv *fs210x) 563 { 564 int scene_id; 565 int ret; 566 567 regcache_cache_bypass(fs210x->regmap, true); 568 569 if (!fs210x->gpio_sdz) { 570 /* Gpio is not found, i2c reset */ 571 ret = fs210x_reg_write(fs210x, FS210X_10H_PWRCTRL, 572 FS210X_10H_I2C_RESET); 573 if (ret) 574 goto tag_power_down; 575 } else { 576 /* gpio reset, deactivate */ 577 gpiod_set_value_cansleep(fs210x->gpio_sdz, 0); 578 } 579 580 fsleep(10000); /* >= 10ms */ 581 582 /* Backup scene id */ 583 scene_id = fs210x->scene_id; 584 fs210x->scene_id = -1; 585 586 /* Init registers/RAM by init scene */ 587 ret = fs210x_set_scene(fs210x, FS210X_INIT_SCENE); 588 if (ret) 589 goto tag_power_down; 590 591 /* 592 * If the firmware has effect scene(s), 593 * we load effect scene by default scene or scene_id 594 */ 595 if (fs210x->amp_lib.scene_count > 1) { 596 if (scene_id < FS210X_DEFAULT_SCENE) 597 scene_id = FS210X_DEFAULT_SCENE; 598 ret = fs210x_set_scene(fs210x, scene_id); 599 if (ret) 600 goto tag_power_down; 601 } 602 603 tag_power_down: 604 /* Power down the device */ 605 ret |= fs210x_reg_write(fs210x, FS210X_11H_SYSCTRL, 606 FS210X_11H_DPS_PWDN); 607 fsleep(10000); /* >= 10ms */ 608 609 regcache_cache_bypass(fs210x->regmap, false); 610 if (!ret) { 611 regcache_cache_only(fs210x->regmap, false); 612 regcache_mark_dirty(fs210x->regmap); 613 ret = regcache_sync(fs210x->regmap); 614 if (!ret) 615 fs210x->is_inited = true; 616 } 617 618 return ret; 619 } 620 621 static int fs210x_set_i2s_params(struct fs210x_priv *fs210x) 622 { 623 const struct fs_i2s_srate params[] = { 624 { 16000, 0x3 }, 625 { 32000, 0x7 }, 626 { 44100, 0x8 }, 627 { 48000, 0x9 }, 628 { 88200, 0xA }, 629 { 96000, 0xB }, 630 }; 631 u16 val; 632 int i, ret; 633 634 for (i = 0; i < ARRAY_SIZE(params); i++) { 635 if (params[i].srate != fs210x->srate) 636 continue; 637 val = params[i].i2ssr << FS210X_17H_I2SSR_SHIFT; 638 ret = fs210x_reg_update_bits(fs210x, 639 FS210X_17H_I2SCTRL, 640 FS210X_17H_I2SSR_MASK, 641 val); 642 return ret; 643 } 644 645 dev_err(fs210x->dev, "Invalid sample rate: %d\n", fs210x->srate); 646 647 return -EINVAL; 648 } 649 650 static int fs210x_get_pll_div(struct fs210x_priv *fs210x, 651 const struct fs_pll_div **pll_div) 652 { 653 int i; 654 655 if (!fs210x || !pll_div) 656 return -EINVAL; 657 658 for (i = 0; i < ARRAY_SIZE(fs210x_pll_div); i++) { 659 if (fs210x_pll_div[i].bclk != fs210x->bclk) 660 continue; 661 *pll_div = fs210x_pll_div + i; 662 return 0; 663 } 664 665 dev_err(fs210x->dev, "No PLL table for bclk: %d\n", fs210x->bclk); 666 667 return -EFAULT; 668 } 669 670 static int fs210x_set_hw_params(struct fs210x_priv *fs210x) 671 { 672 const struct fs_pll_div *pll_div; 673 int ret; 674 675 ret = fs210x_set_i2s_params(fs210x); 676 if (ret) { 677 dev_err(fs210x->dev, "Failed to set i2s params: %d\n", ret); 678 return ret; 679 } 680 681 /* Set pll params */ 682 ret = fs210x_get_pll_div(fs210x, &pll_div); 683 if (ret) 684 return ret; 685 686 ret = fs210x_reg_write(fs210x, FS210X_A1H_PLLCTRL1, pll_div->pll1); 687 ret |= fs210x_reg_write(fs210x, FS210X_A2H_PLLCTRL2, pll_div->pll2); 688 ret |= fs210x_reg_write(fs210x, FS210X_A3H_PLLCTRL3, pll_div->pll3); 689 690 return ret; 691 } 692 693 static int fs210x_dai_startup(struct snd_pcm_substream *substream, 694 struct snd_soc_dai *dai) 695 { 696 const struct snd_pcm_hw_constraint_list *list; 697 struct fs210x_priv *fs210x; 698 int ret; 699 700 fs210x = snd_soc_component_get_drvdata(dai->component); 701 if (!fs210x) { 702 pr_err("dai_startup: fs210x is null\n"); 703 return -EINVAL; 704 } 705 706 if (!substream->runtime) 707 return 0; 708 709 ret = snd_pcm_hw_constraint_mask64(substream->runtime, 710 SNDRV_PCM_HW_PARAM_FORMAT, 711 FS210X_FORMATS); 712 if (ret < 0) { 713 dev_err(fs210x->dev, 714 "Failed to set hw param format: %d\n", ret); 715 return ret; 716 } 717 718 if (fs210x->devid == FS2105S_DEVICE_ID) 719 list = &fs2105s_constraints; 720 else 721 list = &fs210x_constraints; 722 723 ret = snd_pcm_hw_constraint_list(substream->runtime, 0, 724 SNDRV_PCM_HW_PARAM_RATE, 725 list); 726 if (ret < 0) { 727 dev_err(fs210x->dev, 728 "Failed to set hw param rate: %d\n", ret); 729 return ret; 730 } 731 732 return 0; 733 } 734 735 static int fs210x_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 736 { 737 struct fs210x_priv *fs210x; 738 739 fs210x = snd_soc_component_get_drvdata(dai->component); 740 741 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 742 case SND_SOC_DAIFMT_CBC_CFC: 743 /* Only supports consumer mode */ 744 break; 745 default: 746 dev_err(fs210x->dev, "Only supports consumer mode\n"); 747 return -EINVAL; 748 } 749 750 return 0; 751 } 752 753 static int fs210x_dai_hw_params(struct snd_pcm_substream *substream, 754 struct snd_pcm_hw_params *params, 755 struct snd_soc_dai *dai) 756 { 757 struct fs210x_priv *fs210x; 758 int chn_num; 759 int ret; 760 761 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) 762 return 0; 763 764 fs210x = snd_soc_component_get_drvdata(dai->component); 765 766 fs210x->srate = params_rate(params); 767 fs210x->bclk = snd_soc_params_to_bclk(params); 768 chn_num = params_channels(params); 769 if (chn_num == 1) /* mono */ 770 fs210x->bclk *= 2; /* I2S bus has 2 channels */ 771 772 /* The FS2105S can't support 16kHz sample rate. */ 773 if (fs210x->devid == FS2105S_DEVICE_ID && fs210x->srate == 16000) 774 return -EOPNOTSUPP; 775 776 scoped_guard(mutex, &fs210x->lock) 777 ret = fs210x_set_hw_params(fs210x); 778 if (ret) 779 dev_err(fs210x->dev, "Failed to set hw params: %d\n", ret); 780 781 return ret; 782 } 783 784 static int fs210x_dai_mute(struct snd_soc_dai *dai, int mute, int stream) 785 { 786 struct fs210x_priv *fs210x; 787 unsigned long delay; 788 789 if (stream != SNDRV_PCM_STREAM_PLAYBACK) 790 return 0; 791 792 fs210x = snd_soc_component_get_drvdata(dai->component); 793 794 scoped_guard(mutex, &fs210x->lock) { 795 if (!fs210x->is_inited || fs210x->is_suspended) 796 return 0; 797 } 798 799 if (mute) { 800 cancel_delayed_work_sync(&fs210x->fault_check_work); 801 cancel_delayed_work_sync(&fs210x->start_work); 802 } else { 803 delay = msecs_to_jiffies(fs210x->check_interval_ms); 804 schedule_delayed_work(&fs210x->fault_check_work, delay); 805 } 806 807 return 0; 808 } 809 810 static int fs210x_dai_trigger(struct snd_pcm_substream *substream, 811 int cmd, struct snd_soc_dai *dai) 812 { 813 struct fs210x_priv *fs210x; 814 815 fs210x = snd_soc_component_get_drvdata(dai->component); 816 817 scoped_guard(mutex, &fs210x->lock) { 818 if (!fs210x->is_inited || fs210x->is_suspended || fs210x->is_playing) 819 return 0; 820 } 821 822 switch (cmd) { 823 case SNDRV_PCM_TRIGGER_START: 824 case SNDRV_PCM_TRIGGER_RESUME: 825 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 826 /* 827 * According to the power up/down sequence of FS210x, 828 * it requests the I2S clock has been present 829 * and stable(>= 2ms) before playing. 830 */ 831 schedule_delayed_work(&fs210x->start_work, 832 msecs_to_jiffies(FS210X_START_DELAY_MS)); 833 break; 834 835 default: 836 break; 837 } 838 839 return 0; 840 } 841 842 static void fs210x_start_work(struct work_struct *work) 843 { 844 struct fs210x_priv *fs210x; 845 int ret; 846 847 fs210x = container_of(work, struct fs210x_priv, start_work.work); 848 849 guard(mutex)(&fs210x->lock); 850 851 ret = fs210x_dev_play(fs210x); 852 if (ret) 853 dev_err(fs210x->dev, "Failed to start playing: %d\n", ret); 854 } 855 856 static void fs210x_fault_check_work(struct work_struct *work) 857 { 858 struct fs210x_priv *fs210x; 859 u16 status; 860 int ret; 861 862 fs210x = container_of(work, struct fs210x_priv, fault_check_work.work); 863 864 scoped_guard(mutex, &fs210x->lock) { 865 if (!fs210x->is_inited || fs210x->is_suspended || !fs210x->is_playing) 866 return; 867 868 ret = fs210x_reg_read(fs210x, FS210X_05H_ANASTAT, &status); 869 } 870 871 if (ret) 872 return; 873 874 if (!(status & FS210X_05H_PVDD_MASK)) 875 dev_err(fs210x->dev, "PVDD fault\n"); 876 if (status & FS210X_05H_OCDL_MASK) 877 dev_err(fs210x->dev, "OC detected\n"); 878 if (status & FS210X_05H_UVDL_MASK) 879 dev_err(fs210x->dev, "UV detected\n"); 880 if (status & FS210X_05H_OVDL_MASK) 881 dev_err(fs210x->dev, "OV detected\n"); 882 if (status & FS210X_05H_OTPDL_MASK) 883 dev_err(fs210x->dev, "OT detected\n"); 884 if (status & FS210X_05H_OCRDL_MASK) 885 dev_err(fs210x->dev, "OCR detected\n"); 886 if (status & FS210X_05H_OCLDL_MASK) 887 dev_err(fs210x->dev, "OCL detected\n"); 888 if (status & FS210X_05H_DCRDL_MASK) 889 dev_err(fs210x->dev, "DCR detected\n"); 890 if (status & FS210X_05H_DCLDL_MASK) 891 dev_err(fs210x->dev, "DCL detected\n"); 892 if (status & FS210X_05H_SRDL_MASK) 893 dev_err(fs210x->dev, "SR detected\n"); 894 if (status & FS210X_05H_OTWDL_MASK) 895 dev_err(fs210x->dev, "OTW detected\n"); 896 if (!(status & FS210X_05H_AMPS_MASK)) 897 dev_dbg(fs210x->dev, "Amplifier unready\n"); 898 if (!(status & FS210X_05H_PLLS_MASK)) 899 dev_err(fs210x->dev, "PLL unlock\n"); 900 if (!(status & FS210X_05H_ANAS_MASK)) 901 dev_err(fs210x->dev, "Analog power fault\n"); 902 903 schedule_delayed_work(&fs210x->fault_check_work, 904 msecs_to_jiffies(fs210x->check_interval_ms)); 905 } 906 907 static int fs210x_get_drvdata_from_kctrl(struct snd_kcontrol *kctrl, 908 struct fs210x_priv **fs210x) 909 { 910 struct snd_soc_component *cmpnt; 911 912 if (!kctrl) { 913 pr_err("fs210x: kcontrol is null\n"); 914 return -EINVAL; 915 } 916 917 cmpnt = snd_kcontrol_chip(kctrl); 918 if (!cmpnt) { 919 pr_err("fs210x: component is null\n"); 920 return -EINVAL; 921 } 922 923 *fs210x = snd_soc_component_get_drvdata(cmpnt); 924 925 return 0; 926 } 927 928 static int fs210x_effect_scene_info(struct snd_kcontrol *kcontrol, 929 struct snd_ctl_elem_info *uinfo) 930 { 931 const struct fs_amp_scene *scene; 932 struct fs210x_priv *fs210x; 933 const char *name = "N/A"; 934 int idx, count; 935 int ret; 936 937 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x); 938 if (ret || !fs210x->dev) { 939 pr_err("scene_effect_info: fs210x is null\n"); 940 return -EINVAL; 941 } 942 943 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 944 uinfo->count = 1; 945 946 count = fs210x->amp_lib.scene_count - 1; /* Skip init scene */ 947 if (count < 1) { 948 uinfo->value.enumerated.items = 0; 949 return 0; 950 } 951 952 uinfo->value.enumerated.items = count; 953 if (uinfo->value.enumerated.item >= count) 954 uinfo->value.enumerated.item = count - 1; 955 956 idx = uinfo->value.enumerated.item; 957 scene = fs210x->amp_lib.scene + idx + 1; 958 if (scene->name) 959 name = scene->name; 960 961 strscpy(uinfo->value.enumerated.name, name); 962 963 return 0; 964 } 965 966 static int fs210x_effect_scene_get(struct snd_kcontrol *kcontrol, 967 struct snd_ctl_elem_value *ucontrol) 968 { 969 struct fs210x_priv *fs210x; 970 int index; 971 int ret; 972 973 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x); 974 if (ret || !fs210x->dev) { 975 pr_err("scene_effect_get: fs210x is null\n"); 976 return -EINVAL; 977 } 978 979 /* The id of effect scene is from 1 to N. */ 980 if (fs210x->scene_id < 1) 981 return -EINVAL; 982 983 guard(mutex)(&fs210x->lock); 984 /* 985 * FS210x has scene(s) as below: 986 * init scene: id = 0 987 * effect scene(s): id = 1~N (optional) 988 * effect_index = scene_id - 1 989 */ 990 index = fs210x->scene_id - 1; 991 ucontrol->value.integer.value[0] = index; 992 993 return 0; 994 } 995 996 static int fs210x_effect_scene_put(struct snd_kcontrol *kcontrol, 997 struct snd_ctl_elem_value *ucontrol) 998 { 999 struct fs210x_priv *fs210x; 1000 int scene_id, scene_count; 1001 bool is_changed = false; 1002 int ret; 1003 1004 ret = fs210x_get_drvdata_from_kctrl(kcontrol, &fs210x); 1005 if (ret || !fs210x->dev) { 1006 pr_err("scene_effect_put: fs210x is null\n"); 1007 return -EINVAL; 1008 } 1009 1010 guard(mutex)(&fs210x->lock); 1011 1012 /* 1013 * FS210x has scene(s) as below: 1014 * init scene: id = 0 (It's set in fs210x_init_chip() only) 1015 * effect scene(s): id = 1~N (optional) 1016 * scene_id = effect_index + 1. 1017 */ 1018 scene_id = ucontrol->value.integer.value[0] + 1; 1019 scene_count = fs210x->amp_lib.scene_count - 1; /* Skip init scene */ 1020 if (scene_id < 1 || scene_id > scene_count) 1021 return -ERANGE; 1022 1023 if (scene_id != fs210x->scene_id) 1024 is_changed = true; 1025 1026 if (fs210x->is_suspended) { 1027 fs210x->scene_id = scene_id; 1028 return is_changed; 1029 } 1030 1031 ret = fs210x_set_scene(fs210x, scene_id); 1032 if (ret) 1033 dev_err(fs210x->dev, "Failed to set scene: %d\n", ret); 1034 1035 if (!ret && is_changed) 1036 return 1; 1037 1038 return ret; 1039 } 1040 1041 static int fs210x_playback_event(struct snd_soc_dapm_widget *w, 1042 struct snd_kcontrol *kc, int event) 1043 { 1044 struct snd_soc_component *cmpnt = snd_soc_dapm_to_component(w->dapm); 1045 struct fs210x_priv *fs210x = snd_soc_component_get_drvdata(cmpnt); 1046 int ret = 0; 1047 1048 guard(mutex)(&fs210x->lock); 1049 1050 if (fs210x->is_suspended) 1051 return 0; 1052 1053 switch (event) { 1054 case SND_SOC_DAPM_PRE_PMU: 1055 /* 1056 * If there is no bclk for us to set the clock output, 1057 * we will enable the device(start_work) in dai trigger. 1058 */ 1059 if (!fs210x->clk_bclk) 1060 break; 1061 fs210x_bclk_set(fs210x, true); 1062 ret = fs210x_dev_play(fs210x); 1063 break; 1064 case SND_SOC_DAPM_POST_PMD: 1065 ret = fs210x_dev_stop(fs210x); 1066 fs210x_bclk_set(fs210x, false); 1067 break; 1068 default: 1069 break; 1070 } 1071 1072 return ret; 1073 } 1074 1075 static const struct snd_soc_dai_ops fs210x_dai_ops = { 1076 .startup = fs210x_dai_startup, 1077 .set_fmt = fs210x_dai_set_fmt, 1078 .hw_params = fs210x_dai_hw_params, 1079 .mute_stream = fs210x_dai_mute, 1080 .trigger = fs210x_dai_trigger, 1081 }; 1082 1083 static const struct snd_soc_dai_driver fs210x_dai = { 1084 .name = FS210X_DEFAULT_DAI_NAME, 1085 .playback = { 1086 .stream_name = "Playback", 1087 .channels_min = 1, 1088 .channels_max = 2, 1089 .rates = FS210X_RATES, 1090 .formats = FS210X_FORMATS, 1091 }, 1092 .capture = { 1093 .stream_name = "Capture", 1094 .channels_min = 1, 1095 .channels_max = 2, 1096 .rates = FS210X_RATES, 1097 .formats = FS210X_FORMATS, 1098 }, 1099 .ops = &fs210x_dai_ops, 1100 .symmetric_rate = 1, 1101 .symmetric_sample_bits = 1, 1102 }; 1103 1104 static const DECLARE_TLV_DB_SCALE(fs2105s_vol_tlv, -9709, 19, 1); 1105 static const DECLARE_TLV_DB_SCALE(fs210x_vol_tlv, -13357, 19, 1); 1106 1107 static const struct snd_kcontrol_new fs2105s_vol_control[] = { 1108 SOC_DOUBLE_R_TLV("PCM Playback Volume", 1109 FS210X_39H_LVOLCTRL, FS210X_3AH_RVOLCTRL, 1110 7, 0x1FF, 0, fs2105s_vol_tlv), 1111 }; 1112 1113 static const struct snd_kcontrol_new fs210x_vol_control[] = { 1114 SOC_DOUBLE_R_TLV("PCM Playback Volume", 1115 FS210X_39H_LVOLCTRL, FS210X_3AH_RVOLCTRL, 1116 6, 0x2BF, 0, fs210x_vol_tlv), 1117 }; 1118 1119 static const struct snd_kcontrol_new fs210x_controls[] = { 1120 SOC_DOUBLE("DAC Mute Switch", FS210X_30H_DACCTRL, 4, 8, 1, 0), 1121 SOC_DOUBLE("DAC Fade Switch", FS210X_30H_DACCTRL, 5, 9, 1, 0), 1122 }; 1123 1124 static const struct snd_kcontrol_new fs210x_scene_control[] = { 1125 FS_SOC_ENUM_EXT("Effect Scene", 1126 fs210x_effect_scene_info, 1127 fs210x_effect_scene_get, 1128 fs210x_effect_scene_put), 1129 }; 1130 1131 static const struct snd_soc_dapm_widget fs210x_dapm_widgets[] = { 1132 SND_SOC_DAPM_AIF_IN_E("AIF IN", "Playback", 0, SND_SOC_NOPM, 0, 0, 1133 fs210x_playback_event, 1134 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 1135 SND_SOC_DAPM_AIF_OUT("AIF OUT", "Capture", 0, SND_SOC_NOPM, 0, 0), 1136 SND_SOC_DAPM_OUTPUT("OUTL"), 1137 SND_SOC_DAPM_OUTPUT("OUTR"), 1138 SND_SOC_DAPM_INPUT("SDO"), 1139 }; 1140 1141 static const struct snd_soc_dapm_route fs210x_dapm_routes[] = { 1142 { "OUTL", NULL, "AIF IN" }, 1143 { "OUTR", NULL, "AIF IN" }, 1144 { "AIF OUT", NULL, "SDO" }, 1145 }; 1146 1147 static int fs210x_add_mixer_controls(struct fs210x_priv *fs210x, 1148 struct snd_soc_component *cmpnt) 1149 { 1150 const struct snd_kcontrol_new *kctrl; 1151 int count; 1152 int ret; 1153 1154 if (!fs210x || !cmpnt) 1155 return -EINVAL; 1156 1157 if (fs210x->devid == FS2105S_DEVICE_ID) { 1158 kctrl = fs2105s_vol_control; 1159 count = ARRAY_SIZE(fs2105s_vol_control); 1160 } else { 1161 kctrl = fs210x_vol_control; 1162 count = ARRAY_SIZE(fs210x_vol_control); 1163 } 1164 1165 ret = snd_soc_add_component_controls(cmpnt, kctrl, count); 1166 if (ret) 1167 return ret; 1168 1169 /* 1170 * If the firmware has no scene or only init scene, 1171 * we skip adding this mixer control. 1172 */ 1173 if (fs210x->amp_lib.scene_count < 2) 1174 return 0; 1175 1176 kctrl = fs210x_scene_control; 1177 count = ARRAY_SIZE(fs210x_scene_control); 1178 1179 return snd_soc_add_component_controls(cmpnt, kctrl, count); 1180 } 1181 1182 static int fs210x_probe(struct snd_soc_component *cmpnt) 1183 { 1184 struct fs210x_priv *fs210x; 1185 int ret; 1186 1187 fs210x = snd_soc_component_get_drvdata(cmpnt); 1188 if (!fs210x || !fs210x->dev) 1189 return -EINVAL; 1190 1191 fs210x->amp_lib.dev = fs210x->dev; 1192 fs210x->amp_lib.devid = fs210x->devid; 1193 1194 ret = fs_amp_load_firmware(&fs210x->amp_lib, fs210x->pdata.fwm_name); 1195 if (ret) 1196 return ret; 1197 1198 ret = fs210x_add_mixer_controls(fs210x, cmpnt); 1199 if (ret) 1200 return ret; 1201 1202 guard(mutex)(&fs210x->lock); 1203 1204 return fs210x_init_chip(fs210x); 1205 } 1206 1207 static void fs210x_remove(struct snd_soc_component *cmpnt) 1208 { 1209 struct fs210x_priv *fs210x; 1210 1211 fs210x = snd_soc_component_get_drvdata(cmpnt); 1212 if (!fs210x || !fs210x->dev) 1213 return; 1214 1215 cancel_delayed_work_sync(&fs210x->start_work); 1216 cancel_delayed_work_sync(&fs210x->fault_check_work); 1217 } 1218 1219 #ifdef CONFIG_PM 1220 static int fs210x_suspend(struct snd_soc_component *cmpnt) 1221 { 1222 struct fs210x_priv *fs210x; 1223 int ret; 1224 1225 fs210x = snd_soc_component_get_drvdata(cmpnt); 1226 if (!fs210x || !fs210x->dev) 1227 return -EINVAL; 1228 1229 regcache_cache_only(fs210x->regmap, true); 1230 1231 scoped_guard(mutex, &fs210x->lock) { 1232 fs210x->cur_scene = NULL; 1233 fs210x->is_inited = false; 1234 fs210x->is_playing = false; 1235 fs210x->is_suspended = true; 1236 1237 gpiod_set_value_cansleep(fs210x->gpio_sdz, 1); /* Active */ 1238 fsleep(30000); /* >= 30ms */ 1239 } 1240 1241 cancel_delayed_work_sync(&fs210x->start_work); 1242 cancel_delayed_work_sync(&fs210x->fault_check_work); 1243 1244 ret = regulator_bulk_disable(FS210X_NUM_SUPPLIES, fs210x->supplies); 1245 if (ret) { 1246 dev_err(fs210x->dev, "Failed to suspend: %d\n", ret); 1247 return ret; 1248 } 1249 1250 return 0; 1251 } 1252 1253 static int fs210x_resume(struct snd_soc_component *cmpnt) 1254 { 1255 struct fs210x_priv *fs210x; 1256 int ret; 1257 1258 fs210x = snd_soc_component_get_drvdata(cmpnt); 1259 if (!fs210x || !fs210x->dev) 1260 return -EINVAL; 1261 1262 ret = regulator_bulk_enable(FS210X_NUM_SUPPLIES, fs210x->supplies); 1263 if (ret) { 1264 dev_err(fs210x->dev, "Failed to enable supplies: %d\n", ret); 1265 return ret; 1266 } 1267 1268 guard(mutex)(&fs210x->lock); 1269 1270 fs210x->is_suspended = false; 1271 1272 return fs210x_init_chip(fs210x); 1273 } 1274 #else 1275 #define fs210x_suspend NULL 1276 #define fs210x_resume NULL 1277 #endif // CONFIG_PM 1278 1279 static bool fs210x_volatile_registers(struct device *dev, unsigned int reg) 1280 { 1281 switch (reg) { 1282 case FS210X_00H_STATUS ... FS210X_0FH_I2CADDR: 1283 case FS210X_ABH_INTSTAT: 1284 case FS210X_ACH_INTSTATR: 1285 return true; 1286 default: 1287 return false; 1288 } 1289 } 1290 1291 static const struct snd_soc_component_driver fs210x_soc_component_dev = { 1292 .probe = fs210x_probe, 1293 .remove = fs210x_remove, 1294 .suspend = fs210x_suspend, 1295 .resume = fs210x_resume, 1296 .controls = fs210x_controls, 1297 .num_controls = ARRAY_SIZE(fs210x_controls), 1298 .dapm_widgets = fs210x_dapm_widgets, 1299 .num_dapm_widgets = ARRAY_SIZE(fs210x_dapm_widgets), 1300 .dapm_routes = fs210x_dapm_routes, 1301 .num_dapm_routes = ARRAY_SIZE(fs210x_dapm_routes), 1302 }; 1303 1304 static const struct regmap_config fs210x_regmap = { 1305 .reg_bits = 8, 1306 .val_bits = 16, 1307 .max_register = FS210X_REG_MAX, 1308 .val_format_endian = REGMAP_ENDIAN_BIG, 1309 .cache_type = REGCACHE_MAPLE, 1310 .volatile_reg = fs210x_volatile_registers, 1311 }; 1312 1313 static int fs210x_detect_device(struct fs210x_priv *fs210x) 1314 { 1315 u16 devid; 1316 int ret; 1317 1318 ret = fs210x_reg_read(fs210x, FS210X_03H_DEVID, &devid); 1319 if (ret) 1320 return ret; 1321 1322 fs210x->devid = HI_U16(devid); 1323 1324 switch (fs210x->devid) { 1325 case FS210X_DEVICE_ID: 1326 dev_info(fs210x->dev, "FS2104 detected\n"); 1327 break; 1328 case FS2105S_DEVICE_ID: 1329 dev_info(fs210x->dev, "FS2105S detected\n"); 1330 break; 1331 default: 1332 dev_err(fs210x->dev, "DEVID: 0x%04X dismatch\n", devid); 1333 return -ENODEV; 1334 } 1335 1336 return 0; 1337 } 1338 1339 static int fs210x_parse_dts(struct fs210x_priv *fs210x, 1340 struct fs210x_platform_data *pdata) 1341 { 1342 struct device_node *node = fs210x->dev->of_node; 1343 int i, ret; 1344 1345 if (!node) 1346 return 0; 1347 1348 ret = of_property_read_string(node, "firmware-name", &pdata->fwm_name); 1349 if (ret) 1350 pdata->fwm_name = FS210X_DEFAULT_FWM_NAME; 1351 1352 fs210x->gpio_sdz = devm_gpiod_get_optional(fs210x->dev, 1353 "reset", GPIOD_OUT_HIGH); 1354 if (IS_ERR(fs210x->gpio_sdz)) 1355 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->gpio_sdz), 1356 "Failed to get reset-gpios\n"); 1357 1358 for (i = 0; i < FS210X_NUM_SUPPLIES; i++) 1359 fs210x->supplies[i].supply = fs210x_supply_names[i]; 1360 1361 ret = devm_regulator_bulk_get(fs210x->dev, 1362 ARRAY_SIZE(fs210x->supplies), 1363 fs210x->supplies); 1364 if (ret) 1365 return dev_err_probe(fs210x->dev, ret, 1366 "Failed to get supplies\n"); 1367 1368 return 0; 1369 } 1370 1371 static void fs210x_deinit(struct fs210x_priv *fs210x) 1372 { 1373 gpiod_set_value_cansleep(fs210x->gpio_sdz, 1); /* Active */ 1374 fsleep(10000); /* >= 10ms */ 1375 1376 regulator_bulk_disable(FS210X_NUM_SUPPLIES, fs210x->supplies); 1377 } 1378 1379 static int fs210x_init(struct fs210x_priv *fs210x) 1380 { 1381 int ret; 1382 1383 ret = fs210x_parse_dts(fs210x, &fs210x->pdata); 1384 if (ret) 1385 return ret; 1386 1387 fs210x->clk_bclk = devm_clk_get_optional(fs210x->dev, "bclk"); 1388 if (IS_ERR(fs210x->clk_bclk)) 1389 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->clk_bclk), 1390 "Failed to get bclk\n"); 1391 1392 ret = regulator_bulk_enable(FS210X_NUM_SUPPLIES, fs210x->supplies); 1393 if (ret) 1394 return dev_err_probe(fs210x->dev, ret, 1395 "Failed to enable supplies\n"); 1396 1397 /* Make sure the SDZ pin is pulled down enough time. */ 1398 fsleep(10000); /* >= 10ms */ 1399 gpiod_set_value_cansleep(fs210x->gpio_sdz, 0); /* Deactivate */ 1400 fsleep(10000); /* >= 10ms */ 1401 1402 ret = fs210x_detect_device(fs210x); 1403 if (ret) { 1404 fs210x_deinit(fs210x); 1405 return ret; 1406 } 1407 1408 fs210x->scene_id = -1; /* Invalid scene */ 1409 fs210x->cur_scene = NULL; 1410 fs210x->is_playing = false; 1411 fs210x->is_inited = false; 1412 fs210x->is_suspended = false; 1413 fs210x->check_interval_ms = FS210X_FAULT_CHECK_INTERVAL_MS; 1414 1415 INIT_DELAYED_WORK(&fs210x->fault_check_work, fs210x_fault_check_work); 1416 INIT_DELAYED_WORK(&fs210x->start_work, fs210x_start_work); 1417 mutex_init(&fs210x->lock); 1418 1419 return 0; 1420 } 1421 1422 static int fs210x_register_snd_component(struct fs210x_priv *fs210x) 1423 { 1424 struct snd_soc_dai_driver *dai_drv; 1425 static int instance_id; 1426 int ret; 1427 1428 dai_drv = devm_kmemdup(fs210x->dev, &fs210x_dai, 1429 sizeof(fs210x_dai), GFP_KERNEL); 1430 if (!dai_drv) 1431 return -ENOMEM; 1432 1433 dai_drv->name = devm_kasprintf(fs210x->dev, 1434 GFP_KERNEL, "%s-%d", 1435 dai_drv->name, instance_id); 1436 if (!dai_drv->name) 1437 return -ENOMEM; 1438 1439 instance_id++; 1440 1441 if (fs210x->devid == FS2105S_DEVICE_ID) { 1442 dai_drv->playback.rates = FS2105S_RATES; 1443 dai_drv->capture.rates = FS2105S_RATES; 1444 } 1445 1446 ret = snd_soc_register_component(fs210x->dev, 1447 &fs210x_soc_component_dev, 1448 dai_drv, 1); 1449 return ret; 1450 } 1451 1452 static ssize_t check_interval_ms_show(struct device *dev, 1453 struct device_attribute *devattr, 1454 char *buf) 1455 { 1456 struct fs210x_priv *fs210x = dev_get_drvdata(dev); 1457 1458 return sysfs_emit(buf, "%d\n", fs210x->check_interval_ms); 1459 } 1460 1461 static ssize_t check_interval_ms_store(struct device *dev, 1462 struct device_attribute *devattr, 1463 const char *buf, 1464 size_t count) 1465 { 1466 struct fs210x_priv *fs210x = dev_get_drvdata(dev); 1467 int ret; 1468 1469 ret = kstrtouint(buf, 10, &fs210x->check_interval_ms); 1470 if (ret) 1471 return -EINVAL; 1472 1473 return (ssize_t)count; 1474 } 1475 1476 static DEVICE_ATTR_RW(check_interval_ms); 1477 1478 static struct attribute *fs210x_attrs[] = { 1479 &dev_attr_check_interval_ms.attr, 1480 NULL, 1481 }; 1482 1483 static struct attribute_group fs210x_attr_group = { 1484 .attrs = fs210x_attrs, 1485 }; 1486 1487 static int fs210x_i2c_probe(struct i2c_client *client) 1488 { 1489 struct fs210x_priv *fs210x; 1490 int ret; 1491 1492 fs210x = devm_kzalloc_obj(&client->dev, *fs210x); 1493 if (!fs210x) 1494 return -ENOMEM; 1495 1496 fs210x->i2c = client; 1497 fs210x->dev = &client->dev; 1498 i2c_set_clientdata(client, fs210x); 1499 1500 fs210x->regmap = devm_regmap_init_i2c(client, &fs210x_regmap); 1501 if (IS_ERR(fs210x->regmap)) 1502 return dev_err_probe(fs210x->dev, PTR_ERR(fs210x->regmap), 1503 "Failed to get regmap\n"); 1504 1505 ret = fs210x_init(fs210x); 1506 if (ret) 1507 return ret; 1508 1509 ret = devm_device_add_group(fs210x->dev, &fs210x_attr_group); 1510 if (ret) { 1511 fs210x_deinit(fs210x); 1512 return dev_err_probe(fs210x->dev, ret, 1513 "Failed to create sysfs group\n"); 1514 } 1515 1516 ret = fs210x_register_snd_component(fs210x); 1517 if (ret) { 1518 fs210x_deinit(fs210x); 1519 return dev_err_probe(fs210x->dev, ret, 1520 "Failed to register component\n"); 1521 } 1522 1523 return 0; 1524 } 1525 1526 static void fs210x_i2c_remove(struct i2c_client *client) 1527 { 1528 struct fs210x_priv *fs210x = i2c_get_clientdata(client); 1529 1530 snd_soc_unregister_component(fs210x->dev); 1531 fs210x_deinit(fs210x); 1532 } 1533 1534 static const struct i2c_device_id fs210x_i2c_id[] = { 1535 { .name = "fs2104" }, 1536 { .name = "fs2105s" }, 1537 { } 1538 }; 1539 MODULE_DEVICE_TABLE(i2c, fs210x_i2c_id); 1540 1541 static const struct of_device_id fs210x_of_match[] = { 1542 { .compatible = "foursemi,fs2105s", }, 1543 {}, 1544 }; 1545 MODULE_DEVICE_TABLE(of, fs210x_of_match); 1546 1547 static struct i2c_driver fs210x_i2c_driver = { 1548 .driver = { 1549 .name = "fs210x", 1550 .of_match_table = fs210x_of_match, 1551 }, 1552 .id_table = fs210x_i2c_id, 1553 .probe = fs210x_i2c_probe, 1554 .remove = fs210x_i2c_remove, 1555 }; 1556 1557 module_i2c_driver(fs210x_i2c_driver); 1558 1559 MODULE_AUTHOR("Nick Li <nick.li@foursemi.com>"); 1560 MODULE_DESCRIPTION("FS2104/5S Audio Amplifier Driver"); 1561 MODULE_LICENSE("GPL"); 1562