2 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <power/pmic.h>
12 #include <power/regulator.h>
15 #include <linux/bitops.h>
16 #include <linux/ioport.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct pbias_reg_info {
26 unsigned int enable_time;
31 struct regmap *regmap;
35 static const struct pmic_child_info pmic_children_info[] = {
36 { .prefix = "pbias", .driver = "pbias_regulator"},
40 static int pbias_write(struct udevice *dev, uint reg, const uint8_t *buff,
43 struct pbias_priv *priv = dev_get_priv(dev);
44 u32 val = *(u32 *)buff;
49 return regmap_write(priv->regmap, priv->offset, val);
52 static int pbias_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
54 struct pbias_priv *priv = dev_get_priv(dev);
59 return regmap_read(priv->regmap, priv->offset, (u32 *)buff);
62 static int pbias_ofdata_to_platdata(struct udevice *dev)
64 struct pbias_priv *priv = dev_get_priv(dev);
65 struct udevice *syscon;
66 struct regmap *regmap;
70 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
73 pr_err("%s: unable to find syscon device (%d)\n", __func__,
78 regmap = syscon_get_regmap(syscon);
80 pr_err("%s: unable to find regmap (%ld)\n", __func__,
82 return PTR_ERR(regmap);
84 priv->regmap = regmap;
86 err = dev_read_resource(dev, 0, &res);
88 pr_err("%s: unable to find offset (%d)\n", __func__, err);
91 priv->offset = res.start;
96 static int pbias_bind(struct udevice *dev)
100 children = pmic_bind_children(dev, dev->node, pmic_children_info);
102 debug("%s: %s - no child found\n", __func__, dev->name);
107 static struct dm_pmic_ops pbias_ops = {
109 .write = pbias_write,
112 static const struct udevice_id pbias_ids[] = {
113 { .compatible = "ti,pbias-dra7" },
117 U_BOOT_DRIVER(pbias_pmic) = {
118 .name = "pbias_pmic",
120 .of_match = pbias_ids,
123 .ofdata_to_platdata = pbias_ofdata_to_platdata,
124 .priv_auto_alloc_size = sizeof(struct pbias_priv),
127 static const struct pbias_reg_info pbias_mmc_omap2430 = {
129 .enable_mask = BIT(1),
133 .name = "pbias_mmc_omap2430"
136 static const struct pbias_reg_info pbias_sim_omap3 = {
138 .enable_mask = BIT(9),
141 .name = "pbias_sim_omap3"
144 static const struct pbias_reg_info pbias_mmc_omap4 = {
145 .enable = BIT(26) | BIT(22),
146 .enable_mask = BIT(26) | BIT(25) | BIT(22),
147 .disable_val = BIT(25),
150 .name = "pbias_mmc_omap4"
153 static const struct pbias_reg_info pbias_mmc_omap5 = {
154 .enable = BIT(27) | BIT(26),
155 .enable_mask = BIT(27) | BIT(25) | BIT(26),
156 .disable_val = BIT(25),
159 .name = "pbias_mmc_omap5"
162 static const struct pbias_reg_info *pbias_reg_infos[] = {
170 static int pbias_regulator_probe(struct udevice *dev)
172 const struct pbias_reg_info **p = pbias_reg_infos;
173 struct dm_regulator_uclass_platdata *uc_pdata;
175 uc_pdata = dev_get_uclass_platdata(dev);
180 rc = dev_read_stringlist_search(dev, "regulator-name",
183 debug("found regulator %s\n", (*p)->name);
185 } else if (rc != -ENODATA) {
195 while (dev_read_string_index(dev, "regulator-name", i++, &s) >= 0)
196 debug("%s'%s' ", (i > 1) ? ", " : "", s);
197 debug("%s not supported\n", (i > 2) ? "are" : "is");
201 uc_pdata->type = REGULATOR_TYPE_OTHER;
202 dev->priv = (void *)*p;
207 static int pbias_regulator_get_value(struct udevice *dev)
209 const struct pbias_reg_info *p = dev_get_priv(dev);
213 rc = pmic_read(dev->parent, 0, (uint8_t *)®, sizeof(reg));
217 debug("%s voltage id %s\n", p->name,
218 (reg & p->vmode) ? "3.0v" : "1.8v");
219 return (reg & p->vmode) ? 3000000 : 1800000;
222 static int pbias_regulator_set_value(struct udevice *dev, int uV)
224 const struct pbias_reg_info *p = dev_get_priv(dev);
228 debug("Setting %s voltage to %s\n", p->name,
229 (reg & p->vmode) ? "3.0v" : "1.8v");
231 rc = pmic_read(dev->parent, 0, (uint8_t *)®, sizeof(reg));
237 else if (uV == 1800000)
242 return pmic_write(dev->parent, 0, (uint8_t *)®, sizeof(reg));
245 static int pbias_regulator_get_enable(struct udevice *dev)
247 const struct pbias_reg_info *p = dev_get_priv(dev);
251 rc = pmic_read(dev->parent, 0, (uint8_t *)®, sizeof(reg));
255 debug("%s id %s\n", p->name,
256 (reg & p->enable_mask) == (p->disable_val) ? "on" : "off");
258 return (reg & p->enable_mask) == (p->disable_val);
261 static int pbias_regulator_set_enable(struct udevice *dev, bool enable)
263 const struct pbias_reg_info *p = dev_get_priv(dev);
267 debug("Turning %s %s\n", enable ? "on" : "off", p->name);
269 rc = pmic_read(dev->parent, 0, (uint8_t *)®, sizeof(reg));
273 reg &= ~p->enable_mask;
277 reg |= p->disable_val;
279 rc = pmic_write(dev->parent, 0, (uint8_t *)®, sizeof(reg));
284 udelay(p->enable_time);
289 static const struct dm_regulator_ops pbias_regulator_ops = {
290 .get_value = pbias_regulator_get_value,
291 .set_value = pbias_regulator_set_value,
292 .get_enable = pbias_regulator_get_enable,
293 .set_enable = pbias_regulator_set_enable,
296 U_BOOT_DRIVER(pbias_regulator) = {
297 .name = "pbias_regulator",
298 .id = UCLASS_REGULATOR,
299 .ops = &pbias_regulator_ops,
300 .probe = pbias_regulator_probe,