Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fdtdec.h> |
| 8 | #include <errno.h> |
| 9 | #include <dm.h> |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 12 | #include <linux/err.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 15 | #include <asm-generic/gpio.h> |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 16 | #include <power/pmic.h> |
| 17 | #include <power/regulator.h> |
| 18 | #include <power/pca9450.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | static const struct pmic_child_info pmic_children_info[] = { |
| 23 | /* buck */ |
| 24 | { .prefix = "b", .driver = PCA9450_REGULATOR_DRIVER}, |
Marek Vasut | 1d51e77 | 2022-05-20 05:10:16 +0200 | [diff] [blame] | 25 | { .prefix = "B", .driver = PCA9450_REGULATOR_DRIVER}, |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 26 | /* ldo */ |
| 27 | { .prefix = "l", .driver = PCA9450_REGULATOR_DRIVER}, |
Marek Vasut | 1d51e77 | 2022-05-20 05:10:16 +0200 | [diff] [blame] | 28 | { .prefix = "L", .driver = PCA9450_REGULATOR_DRIVER}, |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 29 | { }, |
| 30 | }; |
| 31 | |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 32 | struct pca9450_priv { |
| 33 | struct gpio_desc *sd_vsel_gpio; |
| 34 | }; |
| 35 | |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 36 | static int pca9450_reg_count(struct udevice *dev) |
| 37 | { |
| 38 | return PCA9450_REG_NUM; |
| 39 | } |
| 40 | |
| 41 | static int pca9450_write(struct udevice *dev, uint reg, const uint8_t *buff, |
| 42 | int len) |
| 43 | { |
| 44 | if (dm_i2c_write(dev, reg, buff, len)) { |
| 45 | pr_err("write error to device: %p register: %#x!", dev, reg); |
| 46 | return -EIO; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int pca9450_read(struct udevice *dev, uint reg, uint8_t *buff, |
| 53 | int len) |
| 54 | { |
| 55 | if (dm_i2c_read(dev, reg, buff, len)) { |
| 56 | pr_err("read error from device: %p register: %#x!", dev, reg); |
| 57 | return -EIO; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int pca9450_bind(struct udevice *dev) |
| 64 | { |
| 65 | int children; |
| 66 | ofnode regulators_node; |
| 67 | |
| 68 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 69 | if (!ofnode_valid(regulators_node)) { |
| 70 | debug("%s: %s regulators subnode not found!", __func__, |
| 71 | dev->name); |
| 72 | return -ENXIO; |
| 73 | } |
| 74 | |
| 75 | debug("%s: '%s' - found regulators subnode\n", __func__, dev->name); |
| 76 | |
| 77 | children = pmic_bind_children(dev, regulators_node, |
| 78 | pmic_children_info); |
| 79 | if (!children) |
| 80 | debug("%s: %s - no child found\n", __func__, dev->name); |
| 81 | |
| 82 | /* Always return success for this device */ |
| 83 | return 0; |
| 84 | } |
| 85 | |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 86 | static int pca9450_probe(struct udevice *dev) |
| 87 | { |
| 88 | struct pca9450_priv *priv = dev_get_priv(dev); |
Marek Vasut | 910c7a8 | 2022-12-09 20:35:46 +0100 | [diff] [blame] | 89 | unsigned int reset_ctrl; |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 90 | int ret = 0; |
| 91 | |
| 92 | if (CONFIG_IS_ENABLED(DM_GPIO) && CONFIG_IS_ENABLED(DM_REGULATOR_PCA9450)) { |
| 93 | priv->sd_vsel_gpio = devm_gpiod_get_optional(dev, "sd-vsel", |
| 94 | GPIOD_IS_OUT | |
| 95 | GPIOD_IS_OUT_ACTIVE); |
| 96 | if (IS_ERR(priv->sd_vsel_gpio)) { |
| 97 | ret = PTR_ERR(priv->sd_vsel_gpio); |
| 98 | dev_err(dev, "Failed to request SD_VSEL GPIO: %d\n", ret); |
Marek Vasut | 910c7a8 | 2022-12-09 20:35:46 +0100 | [diff] [blame] | 99 | if (ret) |
| 100 | return ret; |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Marek Vasut | 910c7a8 | 2022-12-09 20:35:46 +0100 | [diff] [blame] | 104 | if (ofnode_read_bool(dev_ofnode(dev), "nxp,wdog_b-warm-reset")) |
| 105 | reset_ctrl = PCA9450_PMIC_RESET_WDOG_B_CFG_WARM; |
| 106 | else |
| 107 | reset_ctrl = PCA9450_PMIC_RESET_WDOG_B_CFG_COLD_LDO12; |
| 108 | |
| 109 | return pmic_clrsetbits(dev, PCA9450_RESET_CTRL, |
| 110 | PCA9450_PMIC_RESET_WDOG_B_CFG_MASK, reset_ctrl); |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 113 | static struct dm_pmic_ops pca9450_ops = { |
| 114 | .reg_count = pca9450_reg_count, |
| 115 | .read = pca9450_read, |
| 116 | .write = pca9450_write, |
| 117 | }; |
| 118 | |
| 119 | static const struct udevice_id pca9450_ids[] = { |
Marek Vasut | 326337f | 2022-05-20 05:10:17 +0200 | [diff] [blame] | 120 | { .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, }, |
| 121 | { .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, }, |
| 122 | { .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, }, |
Ye Li | 69c573c | 2023-02-03 18:24:36 +0800 | [diff] [blame] | 123 | { .compatible = "nxp,pca9451a", .data = NXP_CHIP_TYPE_PCA9451A, }, |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 124 | { } |
| 125 | }; |
| 126 | |
| 127 | U_BOOT_DRIVER(pmic_pca9450) = { |
| 128 | .name = "pca9450 pmic", |
| 129 | .id = UCLASS_PMIC, |
| 130 | .of_match = pca9450_ids, |
| 131 | .bind = pca9450_bind, |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 132 | .probe = pca9450_probe, |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 133 | .ops = &pca9450_ops, |
Frieder Schrempf | 2add051 | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 134 | .priv_auto = sizeof(struct pca9450_priv), |
Ye Li | 22172f6 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 135 | }; |