|
16 | 16 | #include <linux/of_device.h> |
17 | 17 | #include <linux/pm_runtime.h> |
18 | 18 | #include <linux/power_supply.h> |
| 19 | +#include <linux/power/bq24190_charger.h> |
| 20 | +#include <linux/regulator/driver.h> |
| 21 | +#include <linux/regulator/machine.h> |
19 | 22 | #include <linux/workqueue.h> |
20 | 23 | #include <linux/gpio.h> |
21 | 24 | #include <linux/i2c.h> |
@@ -513,6 +516,111 @@ static int bq24190_sysfs_create_group(struct bq24190_dev_info *bdi) |
513 | 516 | static inline void bq24190_sysfs_remove_group(struct bq24190_dev_info *bdi) {} |
514 | 517 | #endif |
515 | 518 |
|
| 519 | +#ifdef CONFIG_REGULATOR |
| 520 | +static int bq24190_set_charge_mode(struct regulator_dev *dev, u8 val) |
| 521 | +{ |
| 522 | + struct bq24190_dev_info *bdi = rdev_get_drvdata(dev); |
| 523 | + int ret; |
| 524 | + |
| 525 | + ret = pm_runtime_get_sync(bdi->dev); |
| 526 | + if (ret < 0) { |
| 527 | + dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); |
| 528 | + pm_runtime_put_noidle(bdi->dev); |
| 529 | + return ret; |
| 530 | + } |
| 531 | + |
| 532 | + ret = bq24190_write_mask(bdi, BQ24190_REG_POC, |
| 533 | + BQ24190_REG_POC_CHG_CONFIG_MASK, |
| 534 | + BQ24190_REG_POC_CHG_CONFIG_SHIFT, val); |
| 535 | + |
| 536 | + pm_runtime_mark_last_busy(bdi->dev); |
| 537 | + pm_runtime_put_autosuspend(bdi->dev); |
| 538 | + |
| 539 | + return ret; |
| 540 | +} |
| 541 | + |
| 542 | +static int bq24190_vbus_enable(struct regulator_dev *dev) |
| 543 | +{ |
| 544 | + return bq24190_set_charge_mode(dev, BQ24190_REG_POC_CHG_CONFIG_OTG); |
| 545 | +} |
| 546 | + |
| 547 | +static int bq24190_vbus_disable(struct regulator_dev *dev) |
| 548 | +{ |
| 549 | + return bq24190_set_charge_mode(dev, BQ24190_REG_POC_CHG_CONFIG_CHARGE); |
| 550 | +} |
| 551 | + |
| 552 | +static int bq24190_vbus_is_enabled(struct regulator_dev *dev) |
| 553 | +{ |
| 554 | + struct bq24190_dev_info *bdi = rdev_get_drvdata(dev); |
| 555 | + int ret; |
| 556 | + u8 val; |
| 557 | + |
| 558 | + ret = pm_runtime_get_sync(bdi->dev); |
| 559 | + if (ret < 0) { |
| 560 | + dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret); |
| 561 | + pm_runtime_put_noidle(bdi->dev); |
| 562 | + return ret; |
| 563 | + } |
| 564 | + |
| 565 | + ret = bq24190_read_mask(bdi, BQ24190_REG_POC, |
| 566 | + BQ24190_REG_POC_CHG_CONFIG_MASK, |
| 567 | + BQ24190_REG_POC_CHG_CONFIG_SHIFT, &val); |
| 568 | + |
| 569 | + pm_runtime_mark_last_busy(bdi->dev); |
| 570 | + pm_runtime_put_autosuspend(bdi->dev); |
| 571 | + |
| 572 | + return ret ? ret : val == BQ24190_REG_POC_CHG_CONFIG_OTG; |
| 573 | +} |
| 574 | + |
| 575 | +static const struct regulator_ops bq24190_vbus_ops = { |
| 576 | + .enable = bq24190_vbus_enable, |
| 577 | + .disable = bq24190_vbus_disable, |
| 578 | + .is_enabled = bq24190_vbus_is_enabled, |
| 579 | +}; |
| 580 | + |
| 581 | +static const struct regulator_desc bq24190_vbus_desc = { |
| 582 | + .name = "usb_otg_vbus", |
| 583 | + .type = REGULATOR_VOLTAGE, |
| 584 | + .owner = THIS_MODULE, |
| 585 | + .ops = &bq24190_vbus_ops, |
| 586 | + .fixed_uV = 5000000, |
| 587 | + .n_voltages = 1, |
| 588 | +}; |
| 589 | + |
| 590 | +static const struct regulator_init_data bq24190_vbus_init_data = { |
| 591 | + .constraints = { |
| 592 | + .valid_ops_mask = REGULATOR_CHANGE_STATUS, |
| 593 | + }, |
| 594 | +}; |
| 595 | + |
| 596 | +static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) |
| 597 | +{ |
| 598 | + struct bq24190_platform_data *pdata = bdi->dev->platform_data; |
| 599 | + struct regulator_config cfg = { }; |
| 600 | + struct regulator_dev *reg; |
| 601 | + int ret = 0; |
| 602 | + |
| 603 | + cfg.dev = bdi->dev; |
| 604 | + if (pdata && pdata->regulator_init_data) |
| 605 | + cfg.init_data = pdata->regulator_init_data; |
| 606 | + else |
| 607 | + cfg.init_data = &bq24190_vbus_init_data; |
| 608 | + cfg.driver_data = bdi; |
| 609 | + reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg); |
| 610 | + if (IS_ERR(reg)) { |
| 611 | + ret = PTR_ERR(reg); |
| 612 | + dev_err(bdi->dev, "Can't register regulator: %d\n", ret); |
| 613 | + } |
| 614 | + |
| 615 | + return ret; |
| 616 | +} |
| 617 | +#else |
| 618 | +static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi) |
| 619 | +{ |
| 620 | + return 0; |
| 621 | +} |
| 622 | +#endif |
| 623 | + |
516 | 624 | static int bq24190_set_config(struct bq24190_dev_info *bdi) |
517 | 625 | { |
518 | 626 | int ret; |
@@ -1740,6 +1848,10 @@ static int bq24190_probe(struct i2c_client *client, |
1740 | 1848 | goto out_sysfs; |
1741 | 1849 | } |
1742 | 1850 |
|
| 1851 | + ret = bq24190_register_vbus_regulator(bdi); |
| 1852 | + if (ret < 0) |
| 1853 | + goto out_sysfs; |
| 1854 | + |
1743 | 1855 | if (bdi->extcon) { |
1744 | 1856 | INIT_DELAYED_WORK(&bdi->extcon_work, bq24190_extcon_work); |
1745 | 1857 | bdi->extcon_nb.notifier_call = bq24190_extcon_event; |
|
0 commit comments