]> git.sur5r.net Git - u-boot/blob - drivers/power/pmic/pmic-uclass.c
dbab3e3ff9fced25d7d8fe36a5962ed29d3d2eed
[u-boot] / drivers / power / pmic / pmic-uclass.c
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 <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
18 DECLARE_GLOBAL_DATA_PTR;
19
20 static 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
30 int pmic_bind_children(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                 for (info = child_info; info->prefix && info->driver; info++) {
57                         prefix_len = strlen(info->prefix);
58                         if (strncmp(info->prefix, node_name, prefix_len))
59                                 continue;
60
61                         debug("  - compatible prefix: '%s'\n", info->prefix);
62
63                         drv = lists_driver_lookup_name(info->driver);
64                         if (!drv) {
65                                 debug("  - driver: '%s' not found!\n",
66                                       info->driver);
67                                 continue;
68                         }
69
70                         debug("  - found child driver: '%s'\n", drv->name);
71
72                         ret = device_bind(pmic, drv, node_name, NULL,
73                                           node, &child);
74                         if (ret) {
75                                 debug("  - child binding error: %d\n", ret);
76                                 continue;
77                         }
78
79                         debug("  - bound child device: '%s'\n", child->name);
80
81                         child->driver_data = str_get_num(node_name +
82                                                          prefix_len,
83                                                          node_name +
84                                                          node_name_len);
85
86                         debug("  - set 'child->driver_data': %lu\n",
87                               child->driver_data);
88                         break;
89                 }
90
91                 if (child)
92                         bind_count++;
93                 else
94                         debug("  - compatible prefix not found\n");
95         }
96
97         debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
98         return bind_count;
99 }
100
101 int pmic_get(const char *name, struct udevice **devp)
102 {
103         return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
104 }
105
106 int pmic_reg_count(struct udevice *dev)
107 {
108         const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
109
110         if (!ops || !ops->reg_count)
111                 return -ENOSYS;
112
113         return ops->reg_count(dev);
114 }
115
116 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
117 {
118         const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
119
120         if (!buffer)
121                 return -EFAULT;
122
123         if (!ops || !ops->read)
124                 return -ENOSYS;
125
126         return ops->read(dev, reg, buffer, len);
127 }
128
129 int pmic_write(struct udevice *dev, uint reg, const 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->write)
137                 return -ENOSYS;
138
139         return ops->write(dev, reg, buffer, len);
140 }
141
142 int pmic_reg_read(struct udevice *dev, uint reg)
143 {
144         u8 byte;
145         int ret;
146
147         ret = pmic_read(dev, reg, &byte, 1);
148         debug("%s: reg=%x, value=%x\n", __func__, reg, byte);
149
150         return ret ? ret : byte;
151 }
152
153 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
154 {
155         u8 byte = value;
156
157         debug("%s: reg=%x, value=%x\n", __func__, reg, value);
158         return pmic_read(dev, reg, &byte, 1);
159 }
160
161 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
162 {
163         u8 byte;
164         int ret;
165
166         ret = pmic_reg_read(dev, reg);
167         if (ret < 0)
168                 return ret;
169         byte = (ret & ~clr) | set;
170
171         return pmic_reg_write(dev, reg, byte);
172 }
173
174 UCLASS_DRIVER(pmic) = {
175         .id             = UCLASS_PMIC,
176         .name           = "pmic",
177 };