blob: 812ac13baa314d50cc8f00b11464f8258c60637f [file] [log] [blame]
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +02001/*
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 <dm/lists.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
15#include <power/pmic.h>
16#include <linux/ctype.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static ulong str_get_num(const char *ptr, const char *maxptr)
21{
22 if (!ptr || !maxptr)
23 return 0;
24
25 while (!isdigit(*ptr) && ptr++ < maxptr);
26
27 return simple_strtoul(ptr, NULL, 0);
28}
29
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020030int pmic_bind_children(struct udevice *pmic, int offset,
31 const struct pmic_child_info *child_info)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020032{
33 const struct pmic_child_info *info;
34 const void *blob = gd->fdt_blob;
35 struct driver *drv;
36 struct udevice *child;
37 const char *node_name;
38 int node_name_len;
39 int bind_count = 0;
40 int node;
41 int prefix_len;
42 int ret;
43
44 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
45 pmic->of_offset);
46
47 for (node = fdt_first_subnode(blob, offset);
48 node > 0;
49 node = fdt_next_subnode(blob, node)) {
50 node_name = fdt_get_name(blob, node, &node_name_len);
51
52 debug("* Found child node: '%s' at offset:%d\n", node_name,
53 node);
54
55 child = NULL;
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020056 for (info = child_info; info->prefix && info->driver; info++) {
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020057 prefix_len = strlen(info->prefix);
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020058 if (strncasecmp(info->prefix, node_name, prefix_len))
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020059 continue;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020060
61 debug(" - compatible prefix: '%s'\n", info->prefix);
62
63 drv = lists_driver_lookup_name(info->driver);
64 if (!drv) {
65 debug(" - driver: '%s' not found!\n",
66 info->driver);
67 continue;
68 }
69
70 debug(" - found child driver: '%s'\n", drv->name);
71
72 ret = device_bind(pmic, drv, node_name, NULL,
73 node, &child);
74 if (ret) {
75 debug(" - child binding error: %d\n", ret);
76 continue;
77 }
78
79 debug(" - bound child device: '%s'\n", child->name);
80
81 child->driver_data = str_get_num(node_name +
82 prefix_len,
83 node_name +
84 node_name_len);
85
86 debug(" - set 'child->driver_data': %lu\n",
87 child->driver_data);
88 break;
89 }
90
91 if (child)
92 bind_count++;
93 else
94 debug(" - compatible prefix not found\n");
95 }
96
97 debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
98 return bind_count;
99}
100
101int pmic_get(const char *name, struct udevice **devp)
102{
103 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
104}
105
106int pmic_reg_count(struct udevice *dev)
107{
108 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200109
110 if (!ops || !ops->reg_count)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200111 return -ENOSYS;
112
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200113 return ops->reg_count(dev);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200114}
115
116int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
117{
118 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200119
120 if (!buffer)
121 return -EFAULT;
122
123 if (!ops || !ops->read)
124 return -ENOSYS;
125
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200126 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200127}
128
129int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
130{
131 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200132
133 if (!buffer)
134 return -EFAULT;
135
136 if (!ops || !ops->write)
137 return -ENOSYS;
138
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200139 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200140}
141
142UCLASS_DRIVER(pmic) = {
143 .id = UCLASS_PMIC,
144 .name = "pmic",
145};