Ye Li | c3ee909 | 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 | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
Frieder Schrempf | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 12 | #include <linux/err.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Frieder Schrempf | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 15 | #include <asm-generic/gpio.h> |
Ye Li | c3ee909 | 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 | 1d6dd08 | 2022-05-20 05:10:16 +0200 | [diff] [blame] | 25 | { .prefix = "B", .driver = PCA9450_REGULATOR_DRIVER}, |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 26 | /* ldo */ |
| 27 | { .prefix = "l", .driver = PCA9450_REGULATOR_DRIVER}, |
Marek Vasut | 1d6dd08 | 2022-05-20 05:10:16 +0200 | [diff] [blame] | 28 | { .prefix = "L", .driver = PCA9450_REGULATOR_DRIVER}, |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 29 | { }, |
| 30 | }; |
| 31 | |
Frieder Schrempf | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 32 | struct pca9450_priv { |
| 33 | struct gpio_desc *sd_vsel_gpio; |
| 34 | }; |
| 35 | |
Ye Li | c3ee909 | 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 | ccf51fb | 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); |
| 89 | int ret = 0; |
| 90 | |
| 91 | if (CONFIG_IS_ENABLED(DM_GPIO) && CONFIG_IS_ENABLED(DM_REGULATOR_PCA9450)) { |
| 92 | priv->sd_vsel_gpio = devm_gpiod_get_optional(dev, "sd-vsel", |
| 93 | GPIOD_IS_OUT | |
| 94 | GPIOD_IS_OUT_ACTIVE); |
| 95 | if (IS_ERR(priv->sd_vsel_gpio)) { |
| 96 | ret = PTR_ERR(priv->sd_vsel_gpio); |
| 97 | dev_err(dev, "Failed to request SD_VSEL GPIO: %d\n", ret); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 104 | static struct dm_pmic_ops pca9450_ops = { |
| 105 | .reg_count = pca9450_reg_count, |
| 106 | .read = pca9450_read, |
| 107 | .write = pca9450_write, |
| 108 | }; |
| 109 | |
| 110 | static const struct udevice_id pca9450_ids[] = { |
Marek Vasut | 5d9b83d | 2022-05-20 05:10:17 +0200 | [diff] [blame] | 111 | { .compatible = "nxp,pca9450a", .data = NXP_CHIP_TYPE_PCA9450A, }, |
| 112 | { .compatible = "nxp,pca9450b", .data = NXP_CHIP_TYPE_PCA9450BC, }, |
| 113 | { .compatible = "nxp,pca9450c", .data = NXP_CHIP_TYPE_PCA9450BC, }, |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 114 | { } |
| 115 | }; |
| 116 | |
| 117 | U_BOOT_DRIVER(pmic_pca9450) = { |
| 118 | .name = "pca9450 pmic", |
| 119 | .id = UCLASS_PMIC, |
| 120 | .of_match = pca9450_ids, |
| 121 | .bind = pca9450_bind, |
Frieder Schrempf | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 122 | .probe = pca9450_probe, |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 123 | .ops = &pca9450_ops, |
Frieder Schrempf | ccf51fb | 2022-06-27 13:00:58 +0200 | [diff] [blame] | 124 | .priv_auto = sizeof(struct pca9450_priv), |
Ye Li | c3ee909 | 2019-10-15 02:15:18 -0700 | [diff] [blame] | 125 | }; |