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