blob: 49709f30842ece7f029494ab189120ec818ddebd [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>
Simon Glasse15bb3e2015-06-23 15:39:16 -060012#include <vsprintf.h>
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020013#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
19DECLARE_GLOBAL_DATA_PTR;
20
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020021int pmic_bind_children(struct udevice *pmic, int offset,
22 const struct pmic_child_info *child_info)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020023{
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;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020029 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)) {
Simon Glasse15bb3e2015-06-23 15:39:16 -060040 node_name = fdt_get_name(blob, node, NULL);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020041
42 debug("* Found child node: '%s' at offset:%d\n", node_name,
43 node);
44
45 child = NULL;
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020046 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glasse15bb3e2015-06-23 15:39:16 -060047 debug(" - compatible prefix: '%s'\n", info->prefix);
48
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020049 prefix_len = strlen(info->prefix);
Simon Glass23ec2b52015-06-23 15:38:56 -060050 if (strncmp(info->prefix, node_name, prefix_len))
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020051 continue;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020052
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020053 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
Simon Glasse15bb3e2015-06-23 15:39:16 -060071 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020072
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
88int pmic_get(const char *name, struct udevice **devp)
89{
90 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
91}
92
93int pmic_reg_count(struct udevice *dev)
94{
95 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020096
97 if (!ops || !ops->reg_count)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020098 return -ENOSYS;
99
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200100 return ops->reg_count(dev);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200101}
102
103int 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);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200106
107 if (!buffer)
108 return -EFAULT;
109
110 if (!ops || !ops->read)
111 return -ENOSYS;
112
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200113 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200114}
115
116int 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);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200119
120 if (!buffer)
121 return -EFAULT;
122
123 if (!ops || !ops->write)
124 return -ENOSYS;
125
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200126 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200127}
128
Simon Glass6c69c7f2015-06-23 15:39:01 -0600129int 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
140int 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);
Simon Glasscd367d82015-07-02 18:16:03 -0600145 return pmic_write(dev, reg, &byte, 1);
Simon Glass6c69c7f2015-06-23 15:39:01 -0600146}
147
148int 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
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200161UCLASS_DRIVER(pmic) = {
162 .id = UCLASS_PMIC,
163 .name = "pmic",
164};