2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 #include <power/pmic.h>
16 #include <linux/ctype.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 static ulong str_get_num(const char *ptr, const char *maxptr)
25 while (!isdigit(*ptr) && ptr++ < maxptr);
27 return simple_strtoul(ptr, NULL, 0);
30 int pmic_bind_children(struct udevice *pmic, int offset,
31 const struct pmic_child_info *child_info)
33 const struct pmic_child_info *info;
34 const void *blob = gd->fdt_blob;
36 struct udevice *child;
37 const char *node_name;
44 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
47 for (node = fdt_first_subnode(blob, offset);
49 node = fdt_next_subnode(blob, node)) {
50 node_name = fdt_get_name(blob, node, &node_name_len);
52 debug("* Found child node: '%s' at offset:%d\n", node_name,
56 for (info = child_info; info->prefix && info->driver; info++) {
57 prefix_len = strlen(info->prefix);
58 if (strncmp(info->prefix, node_name, prefix_len))
61 debug(" - compatible prefix: '%s'\n", info->prefix);
63 drv = lists_driver_lookup_name(info->driver);
65 debug(" - driver: '%s' not found!\n",
70 debug(" - found child driver: '%s'\n", drv->name);
72 ret = device_bind(pmic, drv, node_name, NULL,
75 debug(" - child binding error: %d\n", ret);
79 debug(" - bound child device: '%s'\n", child->name);
81 child->driver_data = str_get_num(node_name +
86 debug(" - set 'child->driver_data': %lu\n",
94 debug(" - compatible prefix not found\n");
97 debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
101 int pmic_get(const char *name, struct udevice **devp)
103 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
106 int pmic_reg_count(struct udevice *dev)
108 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
110 if (!ops || !ops->reg_count)
113 return ops->reg_count(dev);
116 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
118 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
123 if (!ops || !ops->read)
126 return ops->read(dev, reg, buffer, len);
129 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
131 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
136 if (!ops || !ops->write)
139 return ops->write(dev, reg, buffer, len);
142 int pmic_reg_read(struct udevice *dev, uint reg)
147 ret = pmic_read(dev, reg, &byte, 1);
148 debug("%s: reg=%x, value=%x\n", __func__, reg, byte);
150 return ret ? ret : byte;
153 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
157 debug("%s: reg=%x, value=%x\n", __func__, reg, value);
158 return pmic_read(dev, reg, &byte, 1);
161 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
166 ret = pmic_reg_read(dev, reg);
169 byte = (ret & ~clr) | set;
171 return pmic_reg_write(dev, reg, byte);
174 UCLASS_DRIVER(pmic) = {