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_childs(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,
57 while (info->prefix) {
58 prefix_len = strlen(info->prefix);
59 if (strncasecmp(info->prefix, node_name, prefix_len) ||
65 debug(" - compatible prefix: '%s'\n", info->prefix);
67 drv = lists_driver_lookup_name(info->driver);
69 debug(" - driver: '%s' not found!\n",
74 debug(" - found child driver: '%s'\n", drv->name);
76 ret = device_bind(pmic, drv, node_name, NULL,
79 debug(" - child binding error: %d\n", ret);
83 debug(" - bound child device: '%s'\n", child->name);
85 child->driver_data = str_get_num(node_name +
90 debug(" - set 'child->driver_data': %lu\n",
98 debug(" - compatible prefix not found\n");
101 debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
105 int pmic_get(const char *name, struct udevice **devp)
107 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
110 int pmic_reg_count(struct udevice *dev)
112 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
116 return ops->reg_count;
119 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
121 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
127 if (!ops || !ops->read)
130 ret = ops->read(dev, reg, buffer, len);
137 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
139 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
145 if (!ops || !ops->write)
148 ret = ops->write(dev, reg, buffer, len);
155 UCLASS_DRIVER(pmic) = {