1 /*
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 <vsprintf.h>
13 #include <dm/lists.h>
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
16 #include <dm/of_access.h>
17 #include <power/pmic.h>
18 #include <linux/ctype.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
pmic_bind_children(struct udevice * pmic,ofnode parent,const struct pmic_child_info * child_info)23 int pmic_bind_children(struct udevice *pmic, ofnode parent,
24 const struct pmic_child_info *child_info)
25 {
26 const struct pmic_child_info *info;
27 struct driver *drv;
28 struct udevice *child;
29 const char *node_name;
30 const char *reg_name;
31 int bind_count = 0;
32 ofnode node;
33 int ret;
34 bool enable;
35
36 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
37 dev_of_offset(pmic));
38
39 ofnode_for_each_subnode(node, parent) {
40 node_name = ofnode_get_name(node);
41
42 debug("* Found child node: '%s'\n", node_name);
43
44 if (ofnode_is_np(node))
45 enable = of_device_is_available(ofnode_to_np(node));
46 else
47 enable = fdtdec_get_is_enabled(gd->fdt_blob,
48 ofnode_to_offset(node));
49 if (!enable) {
50 debug("* But '%s' is disabled\n", node_name);
51 continue;
52 }
53
54 child = NULL;
55 for (info = child_info; info->prefix && info->driver; info++) {
56 debug(" - compatible prefix: '%s'\n", info->prefix);
57
58 if (!strstr(node_name, info->prefix)) {
59 reg_name = ofnode_read_string(node,
60 "regulator-name");
61 if (!reg_name)
62 continue;
63 if (!strstr(reg_name, info->prefix))
64 continue;
65 }
66
67 /*
68 * If some child info->prefix are the same, try to
69 * distinguish them by parent addr.
70 *
71 * Example: pmic@20, pmic@1a...
72 */
73 if (info->addr) {
74 if (!strstr(dev_read_name(pmic), info->addr))
75 continue;
76 }
77
78 drv = lists_driver_lookup_name(info->driver);
79 if (!drv) {
80 debug(" - driver: '%s' not found!\n",
81 info->driver);
82 continue;
83 }
84
85 debug(" - found child driver: '%s'\n", drv->name);
86
87 ret = device_bind_with_driver_data(pmic, drv, node_name,
88 0, node, &child);
89 if (ret) {
90 debug(" - child binding error: %d\n", ret);
91 continue;
92 }
93
94 debug(" - bound child device: '%s'\n", child->name);
95
96 child->driver_data = trailing_strtol(node_name);
97
98 debug(" - set 'child->driver_data': %lu\n",
99 child->driver_data);
100 break;
101 }
102
103 if (child)
104 bind_count++;
105 else
106 debug(" - compatible prefix not found\n");
107 }
108
109 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
110 return bind_count;
111 }
112 #endif
113
pmic_get(const char * name,struct udevice ** devp)114 int pmic_get(const char *name, struct udevice **devp)
115 {
116 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
117 }
118
pmic_reg_count(struct udevice * dev)119 int pmic_reg_count(struct udevice *dev)
120 {
121 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
122
123 if (!ops || !ops->reg_count)
124 return -ENOSYS;
125
126 return ops->reg_count(dev);
127 }
128
pmic_read(struct udevice * dev,uint reg,uint8_t * buffer,int len)129 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
130 {
131 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
132
133 if (!buffer)
134 return -EFAULT;
135
136 if (!ops || !ops->read)
137 return -ENOSYS;
138
139 return ops->read(dev, reg, buffer, len);
140 }
141
pmic_write(struct udevice * dev,uint reg,const uint8_t * buffer,int len)142 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
143 {
144 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
145
146 if (!buffer)
147 return -EFAULT;
148
149 if (!ops || !ops->write)
150 return -ENOSYS;
151
152 return ops->write(dev, reg, buffer, len);
153 }
154
pmic_reg_read(struct udevice * dev,uint reg)155 int pmic_reg_read(struct udevice *dev, uint reg)
156 {
157 u8 byte;
158 int ret;
159
160 debug("%s: reg=%x", __func__, reg);
161 ret = pmic_read(dev, reg, &byte, 1);
162 debug(", value=%x, ret=%d\n", byte, ret);
163
164 return ret ? ret : byte;
165 }
166
pmic_reg_write(struct udevice * dev,uint reg,uint value)167 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
168 {
169 u8 byte = value;
170 int ret;
171
172 debug("%s: reg=%x, value=%x", __func__, reg, value);
173 ret = pmic_write(dev, reg, &byte, 1);
174 debug(", ret=%d\n", ret);
175
176 return ret;
177 }
178
pmic_clrsetbits(struct udevice * dev,uint reg,uint clr,uint set)179 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
180 {
181 u8 byte;
182 int ret;
183
184 ret = pmic_reg_read(dev, reg);
185 if (ret < 0)
186 return ret;
187 byte = (ret & ~clr) | set;
188
189 return pmic_reg_write(dev, reg, byte);
190 }
191
pmic_suspend(struct udevice * dev)192 int pmic_suspend(struct udevice *dev)
193 {
194 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
195
196 if (!ops || !ops->suspend)
197 return -ENOSYS;
198
199 return ops->suspend(dev);
200 }
201
pmic_resume(struct udevice * dev)202 int pmic_resume(struct udevice *dev)
203 {
204 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
205
206 if (!ops || !ops->resume)
207 return -ENOSYS;
208
209 return ops->resume(dev);
210 }
211
pmic_shutdown(struct udevice * dev)212 int pmic_shutdown(struct udevice *dev)
213 {
214 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
215
216 if (!ops || !ops->shutdown)
217 return -ENOSYS;
218
219 return ops->shutdown(dev);
220 }
221
222 UCLASS_DRIVER(pmic) = {
223 .id = UCLASS_PMIC,
224 .name = "pmic",
225 };
226