xref: /openbmc/u-boot/drivers/power/pmic/pmic-uclass.c (revision d024236e5a31a2b4b82cbcc98b31b8170fc88d28)
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 <power/pmic.h>
17 #include <linux/ctype.h>
18 
19 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
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 
91 int pmic_get(const char *name, struct udevice **devp)
92 {
93 	return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
94 }
95 
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 
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 
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 
132 int pmic_reg_read(struct udevice *dev, uint reg)
133 {
134 	u8 byte;
135 	int ret;
136 
137 	debug("%s: reg=%x", __func__, reg);
138 	ret = pmic_read(dev, reg, &byte, 1);
139 	debug(", value=%x, ret=%d\n", byte, ret);
140 
141 	return ret ? ret : byte;
142 }
143 
144 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
145 {
146 	u8 byte = value;
147 	int ret;
148 
149 	debug("%s: reg=%x, value=%x", __func__, reg, value);
150 	ret = pmic_write(dev, reg, &byte, 1);
151 	debug(", ret=%d\n", ret);
152 
153 	return ret;
154 }
155 
156 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
157 {
158 	u8 byte;
159 	int ret;
160 
161 	ret = pmic_reg_read(dev, reg);
162 	if (ret < 0)
163 		return ret;
164 	byte = (ret & ~clr) | set;
165 
166 	return pmic_reg_write(dev, reg, byte);
167 }
168 
169 UCLASS_DRIVER(pmic) = {
170 	.id		= UCLASS_PMIC,
171 	.name		= "pmic",
172 };
173