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