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 DECLARE_GLOBAL_DATA_PTR;
20 
21 #if CONFIG_IS_ENABLED(PMIC_CHILDREN)
22 int pmic_bind_children(struct udevice *pmic, ofnode parent,
23 		       const struct pmic_child_info *child_info)
24 {
25 	const struct pmic_child_info *info;
26 	struct driver *drv;
27 	struct udevice *child;
28 	const char *node_name;
29 	int bind_count = 0;
30 	ofnode node;
31 	int prefix_len;
32 	int ret;
33 
34 	debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
35 	      dev_of_offset(pmic));
36 
37 	for (node = ofnode_first_subnode(parent);
38 	     ofnode_valid(node);
39 	     node = ofnode_next_subnode(node)) {
40 		node_name = ofnode_get_name(node);
41 
42 		debug("* Found child node: '%s'\n", node_name);
43 
44 		child = NULL;
45 		for (info = child_info; info->prefix && info->driver; info++) {
46 			debug("  - compatible prefix: '%s'\n", info->prefix);
47 
48 			prefix_len = strlen(info->prefix);
49 			if (strncmp(info->prefix, node_name, prefix_len))
50 				continue;
51 
52 			drv = lists_driver_lookup_name(info->driver);
53 			if (!drv) {
54 				debug("  - driver: '%s' not found!\n",
55 				      info->driver);
56 				continue;
57 			}
58 
59 			debug("  - found child driver: '%s'\n", drv->name);
60 
61 			ret = device_bind_with_driver_data(pmic, drv, node_name,
62 							   0, node, &child);
63 			if (ret) {
64 				debug("  - child binding error: %d\n", ret);
65 				continue;
66 			}
67 
68 			debug("  - bound child device: '%s'\n", child->name);
69 
70 			child->driver_data = trailing_strtol(node_name);
71 
72 			debug("  - set 'child->driver_data': %lu\n",
73 			      child->driver_data);
74 			break;
75 		}
76 
77 		if (child)
78 			bind_count++;
79 		else
80 			debug("  - compatible prefix not found\n");
81 	}
82 
83 	debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
84 	return bind_count;
85 }
86 #endif
87 
88 int pmic_get(const char *name, struct udevice **devp)
89 {
90 	return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
91 }
92 
93 int pmic_reg_count(struct udevice *dev)
94 {
95 	const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
96 
97 	if (!ops || !ops->reg_count)
98 		return -ENOSYS;
99 
100 	return ops->reg_count(dev);
101 }
102 
103 int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
104 {
105 	const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
106 
107 	if (!buffer)
108 		return -EFAULT;
109 
110 	if (!ops || !ops->read)
111 		return -ENOSYS;
112 
113 	return ops->read(dev, reg, buffer, len);
114 }
115 
116 int pmic_write(struct udevice *dev, uint reg, const 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->write)
124 		return -ENOSYS;
125 
126 	return ops->write(dev, reg, buffer, len);
127 }
128 
129 int pmic_reg_read(struct udevice *dev, uint reg)
130 {
131 	u8 byte;
132 	int ret;
133 
134 	debug("%s: reg=%x", __func__, reg);
135 	ret = pmic_read(dev, reg, &byte, 1);
136 	debug(", value=%x, ret=%d\n", byte, ret);
137 
138 	return ret ? ret : byte;
139 }
140 
141 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
142 {
143 	u8 byte = value;
144 	int ret;
145 
146 	debug("%s: reg=%x, value=%x", __func__, reg, value);
147 	ret = pmic_write(dev, reg, &byte, 1);
148 	debug(", ret=%d\n", ret);
149 
150 	return ret;
151 }
152 
153 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
154 {
155 	u8 byte;
156 	int ret;
157 
158 	ret = pmic_reg_read(dev, reg);
159 	if (ret < 0)
160 		return ret;
161 	byte = (ret & ~clr) | set;
162 
163 	return pmic_reg_write(dev, reg, byte);
164 }
165 
166 UCLASS_DRIVER(pmic) = {
167 	.id		= UCLASS_PMIC,
168 	.name		= "pmic",
169 };
170