1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
5 */
6
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <vsprintf.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 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
pmic_bind_children(struct udevice * pmic,ofnode parent,const struct pmic_child_info * child_info)19 int pmic_bind_children(struct udevice *pmic, ofnode parent,
20 const struct pmic_child_info *child_info)
21 {
22 const struct pmic_child_info *info;
23 struct driver *drv;
24 struct udevice *child;
25 const char *node_name;
26 const char *reg_name;
27 int bind_count = 0;
28 ofnode node;
29 int prefix_len;
30 int ret;
31
32 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
33 dev_of_offset(pmic));
34
35 ofnode_for_each_subnode(node, parent) {
36 node_name = ofnode_get_name(node);
37
38 debug("* Found child node: '%s'\n", node_name);
39
40 child = NULL;
41 for (info = child_info; info->prefix && info->driver; info++) {
42 debug(" - compatible prefix: '%s'\n", info->prefix);
43
44 prefix_len = strlen(info->prefix);
45 if (strncmp(info->prefix, node_name, prefix_len)) {
46 reg_name = ofnode_read_string(node,
47 "regulator-name");
48 if (!reg_name)
49 continue;
50 if (strncmp(info->prefix, reg_name, prefix_len))
51 continue;
52 }
53
54 drv = lists_driver_lookup_name(info->driver);
55 if (!drv) {
56 debug(" - driver: '%s' not found!\n",
57 info->driver);
58 continue;
59 }
60
61 debug(" - found child driver: '%s'\n", drv->name);
62
63 ret = device_bind_with_driver_data(pmic, drv, node_name,
64 0, node, &child);
65 if (ret) {
66 debug(" - child binding error: %d\n", ret);
67 continue;
68 }
69
70 debug(" - bound child device: '%s'\n", child->name);
71
72 child->driver_data = trailing_strtol(node_name);
73
74 debug(" - set 'child->driver_data': %lu\n",
75 child->driver_data);
76 break;
77 }
78
79 if (child)
80 bind_count++;
81 else
82 debug(" - compatible prefix not found\n");
83 }
84
85 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
86 return bind_count;
87 }
88 #endif
89
pmic_get(const char * name,struct udevice ** devp)90 int pmic_get(const char *name, struct udevice **devp)
91 {
92 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
93 }
94
pmic_reg_count(struct udevice * dev)95 int pmic_reg_count(struct udevice *dev)
96 {
97 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
98
99 if (!ops || !ops->reg_count)
100 return -ENOSYS;
101
102 return ops->reg_count(dev);
103 }
104
pmic_read(struct udevice * dev,uint reg,uint8_t * buffer,int len)105 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
106 {
107 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
108
109 if (!buffer)
110 return -EFAULT;
111
112 if (!ops || !ops->read)
113 return -ENOSYS;
114
115 return ops->read(dev, reg, buffer, len);
116 }
117
pmic_write(struct udevice * dev,uint reg,const uint8_t * buffer,int len)118 int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
119 {
120 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
121
122 if (!buffer)
123 return -EFAULT;
124
125 if (!ops || !ops->write)
126 return -ENOSYS;
127
128 return ops->write(dev, reg, buffer, len);
129 }
130
pmic_reg_read(struct udevice * dev,uint reg)131 int pmic_reg_read(struct udevice *dev, uint reg)
132 {
133 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
134 u32 val = 0;
135 int ret;
136
137 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
138 debug("Wrong transmission size [%d]\n", priv->trans_len);
139 return -EINVAL;
140 }
141
142 debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len);
143 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
144 debug(", value=%x, ret=%d\n", val, ret);
145
146 return ret ? ret : val;
147 }
148
pmic_reg_write(struct udevice * dev,uint reg,uint value)149 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
150 {
151 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
152 int ret;
153
154 if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) {
155 debug("Wrong transmission size [%d]\n", priv->trans_len);
156 return -EINVAL;
157 }
158
159 debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value,
160 priv->trans_len);
161 ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len);
162 debug(", ret=%d\n", ret);
163
164 return ret;
165 }
166
pmic_clrsetbits(struct udevice * dev,uint reg,uint clr,uint set)167 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
168 {
169 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
170 u32 val = 0;
171 int ret;
172
173 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
174 debug("Wrong transmission size [%d]\n", priv->trans_len);
175 return -EINVAL;
176 }
177
178 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
179 if (ret < 0)
180 return ret;
181
182 val = (val & ~clr) | set;
183 return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len);
184 }
185
pmic_pre_probe(struct udevice * dev)186 static int pmic_pre_probe(struct udevice *dev)
187 {
188 struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev);
189
190 pmic_priv->trans_len = 1;
191 return 0;
192 }
193
194 UCLASS_DRIVER(pmic) = {
195 .id = UCLASS_PMIC,
196 .name = "pmic",
197 .pre_probe = pmic_pre_probe,
198 .per_device_auto_alloc_size = sizeof(struct uc_pmic_priv),
199 };
200