blob: d82d3da51d5048978333c9809c38a0d81df8e82d [file] [log] [blame]
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +02001/*
2 * Copyright (C) 2014-2015 Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <fdtdec.h>
10#include <errno.h>
11#include <dm.h>
12#include <dm/lists.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
15#include <power/pmic.h>
16#include <linux/ctype.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static ulong str_get_num(const char *ptr, const char *maxptr)
21{
22 if (!ptr || !maxptr)
23 return 0;
24
25 while (!isdigit(*ptr) && ptr++ < maxptr);
26
27 return simple_strtoul(ptr, NULL, 0);
28}
29
30int pmic_bind_childs(struct udevice *pmic, int offset,
31 const struct pmic_child_info *child_info)
32{
33 const struct pmic_child_info *info;
34 const void *blob = gd->fdt_blob;
35 struct driver *drv;
36 struct udevice *child;
37 const char *node_name;
38 int node_name_len;
39 int bind_count = 0;
40 int node;
41 int prefix_len;
42 int ret;
43
44 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
45 pmic->of_offset);
46
47 for (node = fdt_first_subnode(blob, offset);
48 node > 0;
49 node = fdt_next_subnode(blob, node)) {
50 node_name = fdt_get_name(blob, node, &node_name_len);
51
52 debug("* Found child node: '%s' at offset:%d\n", node_name,
53 node);
54
55 child = NULL;
56 info = child_info;
57 while (info->prefix) {
58 prefix_len = strlen(info->prefix);
59 if (strncasecmp(info->prefix, node_name, prefix_len) ||
60 !info->driver) {
61 info++;
62 continue;
63 }
64
65 debug(" - compatible prefix: '%s'\n", info->prefix);
66
67 drv = lists_driver_lookup_name(info->driver);
68 if (!drv) {
69 debug(" - driver: '%s' not found!\n",
70 info->driver);
71 continue;
72 }
73
74 debug(" - found child driver: '%s'\n", drv->name);
75
76 ret = device_bind(pmic, drv, node_name, NULL,
77 node, &child);
78 if (ret) {
79 debug(" - child binding error: %d\n", ret);
80 continue;
81 }
82
83 debug(" - bound child device: '%s'\n", child->name);
84
85 child->driver_data = str_get_num(node_name +
86 prefix_len,
87 node_name +
88 node_name_len);
89
90 debug(" - set 'child->driver_data': %lu\n",
91 child->driver_data);
92 break;
93 }
94
95 if (child)
96 bind_count++;
97 else
98 debug(" - compatible prefix not found\n");
99 }
100
101 debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
102 return bind_count;
103}
104
105int pmic_get(const char *name, struct udevice **devp)
106{
107 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
108}
109
110int pmic_reg_count(struct udevice *dev)
111{
112 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
113 if (!ops)
114 return -ENOSYS;
115
116 return ops->reg_count;
117}
118
119int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
120{
121 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
122 int ret;
123
124 if (!buffer)
125 return -EFAULT;
126
127 if (!ops || !ops->read)
128 return -ENOSYS;
129
130 ret = ops->read(dev, reg, buffer, len);
131 if (ret)
132 return ret;
133
134 return 0;
135}
136
137int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
138{
139 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
140 int ret;
141
142 if (!buffer)
143 return -EFAULT;
144
145 if (!ops || !ops->write)
146 return -ENOSYS;
147
148 ret = ops->write(dev, reg, buffer, len);
149 if (ret)
150 return ret;
151
152 return 0;
153}
154
155UCLASS_DRIVER(pmic) = {
156 .id = UCLASS_PMIC,
157 .name = "pmic",
158};