blob: 0bbe98cd8a29b5783b0fe057a1c7b2d26a5032cb [file] [log] [blame]
Ye Li22172f62019-10-15 02:15:18 -07001// 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 Schrempf2add0512022-06-27 13:00:58 +020010#include <dm/device_compat.h>
Ye Li22172f62019-10-15 02:15:18 -070011#include <i2c.h>
Frieder Schrempf2add0512022-06-27 13:00:58 +020012#include <linux/err.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Frieder Schrempf2add0512022-06-27 13:00:58 +020015#include <asm-generic/gpio.h>
Simon Glass1e94b462023-09-14 18:21:46 -060016#include <linux/printk.h>
Ye Li22172f62019-10-15 02:15:18 -070017#include <power/pmic.h>
18#include <power/regulator.h>
19#include <power/pca9450.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23static const struct pmic_child_info pmic_children_info[] = {
24 /* buck */
25 { .prefix = "b", .driver = PCA9450_REGULATOR_DRIVER},
Marek Vasut1d51e772022-05-20 05:10:16 +020026 { .prefix = "B", .driver = PCA9450_REGULATOR_DRIVER},
Ye Li22172f62019-10-15 02:15:18 -070027 /* ldo */
28 { .prefix = "l", .driver = PCA9450_REGULATOR_DRIVER},
Marek Vasut1d51e772022-05-20 05:10:16 +020029 { .prefix = "L", .driver = PCA9450_REGULATOR_DRIVER},
Ye Li22172f62019-10-15 02:15:18 -070030 { },
31};
32
Frieder Schrempf2add0512022-06-27 13:00:58 +020033struct pca9450_priv {
34 struct gpio_desc *sd_vsel_gpio;
35};
36
Ye Li22172f62019-10-15 02:15:18 -070037static int pca9450_reg_count(struct udevice *dev)
38{
39 return PCA9450_REG_NUM;
40}
41
42static 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
53static 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
64static 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 Schrempf2add0512022-06-27 13:00:58 +020087static int pca9450_probe(struct udevice *dev)
88{
89 struct pca9450_priv *priv = dev_get_priv(dev);
Marek Vasut910c7a82022-12-09 20:35:46 +010090 unsigned int reset_ctrl;
Frieder Schrempf2add0512022-06-27 13:00:58 +020091 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 Vasut910c7a82022-12-09 20:35:46 +0100100 if (ret)
101 return ret;
Frieder Schrempf2add0512022-06-27 13:00:58 +0200102 }
103 }
104
Marek Vasut910c7a82022-12-09 20:35:46 +0100105 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 Schrempf2add0512022-06-27 13:00:58 +0200112}
113
Ye Li22172f62019-10-15 02:15:18 -0700114static struct dm_pmic_ops pca9450_ops = {
115 .reg_count = pca9450_reg_count,
116 .read = pca9450_read,
117 .write = pca9450_write,
118};
119
120static const struct udevice_id pca9450_ids[] = {
Marek Vasut326337f2022-05-20 05:10:17 +0200121 { .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 Li69c573c2023-02-03 18:24:36 +0800124 { .compatible = "nxp,pca9451a", .data = NXP_CHIP_TYPE_PCA9451A, },
Ye Li22172f62019-10-15 02:15:18 -0700125 { }
126};
127
128U_BOOT_DRIVER(pmic_pca9450) = {
129 .name = "pca9450 pmic",
130 .id = UCLASS_PMIC,
131 .of_match = pca9450_ids,
132 .bind = pca9450_bind,
Frieder Schrempf2add0512022-06-27 13:00:58 +0200133 .probe = pca9450_probe,
Ye Li22172f62019-10-15 02:15:18 -0700134 .ops = &pca9450_ops,
Frieder Schrempf2add0512022-06-27 13:00:58 +0200135 .priv_auto = sizeof(struct pca9450_priv),
Ye Li22172f62019-10-15 02:15:18 -0700136};