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 int pmic_bind_children(struct udevice *pmic, int offset,
22 		       const struct pmic_child_info *child_info)
23 {
24 	const struct pmic_child_info *info;
25 	const void *blob = gd->fdt_blob;
26 	struct driver *drv;
27 	struct udevice *child;
28 	const char *node_name;
29 	int bind_count = 0;
30 	int node;
31 	int prefix_len;
32 	int ret;
33 
34 	debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
35 	      pmic->of_offset);
36 
37 	for (node = fdt_first_subnode(blob, offset);
38 	     node > 0;
39 	     node = fdt_next_subnode(blob, node)) {
40 		node_name = fdt_get_name(blob, node, NULL);
41 
42 		debug("* Found child node: '%s' at offset:%d\n", node_name,
43 								 node);
44 
45 		child = NULL;
46 		for (info = child_info; info->prefix && info->driver; info++) {
47 			debug("  - compatible prefix: '%s'\n", info->prefix);
48 
49 			prefix_len = strlen(info->prefix);
50 			if (strncmp(info->prefix, node_name, prefix_len))
51 				continue;
52 
53 			drv = lists_driver_lookup_name(info->driver);
54 			if (!drv) {
55 				debug("  - driver: '%s' not found!\n",
56 				      info->driver);
57 				continue;
58 			}
59 
60 			debug("  - found child driver: '%s'\n", drv->name);
61 
62 			ret = device_bind(pmic, drv, node_name, NULL,
63 					  node, &child);
64 			if (ret) {
65 				debug("  - child binding error: %d\n", ret);
66 				continue;
67 			}
68 
69 			debug("  - bound child device: '%s'\n", child->name);
70 
71 			child->driver_data = trailing_strtol(node_name);
72 
73 			debug("  - set 'child->driver_data': %lu\n",
74 			      child->driver_data);
75 			break;
76 		}
77 
78 		if (child)
79 			bind_count++;
80 		else
81 			debug("  - compatible prefix not found\n");
82 	}
83 
84 	debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
85 	return bind_count;
86 }
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 	ret = pmic_read(dev, reg, &byte, 1);
135 	debug("%s: reg=%x, value=%x\n", __func__, reg, byte);
136 
137 	return ret ? ret : byte;
138 }
139 
140 int pmic_reg_write(struct udevice *dev, uint reg, uint value)
141 {
142 	u8 byte = value;
143 
144 	debug("%s: reg=%x, value=%x\n", __func__, reg, value);
145 	return pmic_write(dev, reg, &byte, 1);
146 }
147 
148 int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
149 {
150 	u8 byte;
151 	int ret;
152 
153 	ret = pmic_reg_read(dev, reg);
154 	if (ret < 0)
155 		return ret;
156 	byte = (ret & ~clr) | set;
157 
158 	return pmic_reg_write(dev, reg, byte);
159 }
160 
161 UCLASS_DRIVER(pmic) = {
162 	.id		= UCLASS_PMIC,
163 	.name		= "pmic",
164 };
165