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