blob: 7211026aecbea883c7ad42a0262d9d9f8cb56306 [file] [log] [blame]
Przemyslaw Marczak9a8bc952015-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 Glass869b0492015-06-23 15:39:16 -060012#include <vsprintf.h>
Przemyslaw Marczak9a8bc952015-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
Simon Glassc8db7b42016-01-21 19:43:55 -070021#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Przemyslaw Marczak10314982015-05-13 13:38:26 +020022int pmic_bind_children(struct udevice *pmic, int offset,
23 const struct pmic_child_info *child_info)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020024{
25 const struct pmic_child_info *info;
26 const void *blob = gd->fdt_blob;
27 struct driver *drv;
28 struct udevice *child;
29 const char *node_name;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020030 int bind_count = 0;
31 int node;
32 int prefix_len;
33 int ret;
34
35 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
36 pmic->of_offset);
37
38 for (node = fdt_first_subnode(blob, offset);
39 node > 0;
40 node = fdt_next_subnode(blob, node)) {
Simon Glass869b0492015-06-23 15:39:16 -060041 node_name = fdt_get_name(blob, node, NULL);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020042
43 debug("* Found child node: '%s' at offset:%d\n", node_name,
44 node);
45
46 child = NULL;
Przemyslaw Marczak10314982015-05-13 13:38:26 +020047 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glass869b0492015-06-23 15:39:16 -060048 debug(" - compatible prefix: '%s'\n", info->prefix);
49
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020050 prefix_len = strlen(info->prefix);
Simon Glassad28e002015-06-23 15:38:56 -060051 if (strncmp(info->prefix, node_name, prefix_len))
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020052 continue;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020053
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020054 drv = lists_driver_lookup_name(info->driver);
55 if (!drv) {
56 debug(" - driver: '%s' not found!\n",
57 info->driver);
58 continue;
59 }
60
61 debug(" - found child driver: '%s'\n", drv->name);
62
63 ret = device_bind(pmic, drv, node_name, NULL,
64 node, &child);
65 if (ret) {
66 debug(" - child binding error: %d\n", ret);
67 continue;
68 }
69
70 debug(" - bound child device: '%s'\n", child->name);
71
Simon Glass869b0492015-06-23 15:39:16 -060072 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020073
74 debug(" - set 'child->driver_data': %lu\n",
75 child->driver_data);
76 break;
77 }
78
79 if (child)
80 bind_count++;
81 else
82 debug(" - compatible prefix not found\n");
83 }
84
85 debug("Bound: %d childs for PMIC: '%s'\n", bind_count, pmic->name);
86 return bind_count;
87}
Simon Glassc8db7b42016-01-21 19:43:55 -070088#endif
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020089
90int pmic_get(const char *name, struct udevice **devp)
91{
92 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
93}
94
95int pmic_reg_count(struct udevice *dev)
96{
97 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak10314982015-05-13 13:38:26 +020098
99 if (!ops || !ops->reg_count)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200100 return -ENOSYS;
101
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200102 return ops->reg_count(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200103}
104
105int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
106{
107 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200108
109 if (!buffer)
110 return -EFAULT;
111
112 if (!ops || !ops->read)
113 return -ENOSYS;
114
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200115 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200116}
117
118int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
119{
120 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200121
122 if (!buffer)
123 return -EFAULT;
124
125 if (!ops || !ops->write)
126 return -ENOSYS;
127
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200128 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200129}
130
Simon Glassc3f21c32015-06-23 15:39:01 -0600131int pmic_reg_read(struct udevice *dev, uint reg)
132{
133 u8 byte;
134 int ret;
135
Simon Glass46ad8cb2016-01-21 19:43:58 -0700136 debug("%s: reg=%x", __func__, reg);
Simon Glassc3f21c32015-06-23 15:39:01 -0600137 ret = pmic_read(dev, reg, &byte, 1);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700138 debug(", value=%x, ret=%d\n", byte, ret);
Simon Glassc3f21c32015-06-23 15:39:01 -0600139
140 return ret ? ret : byte;
141}
142
143int pmic_reg_write(struct udevice *dev, uint reg, uint value)
144{
145 u8 byte = value;
Simon Glass46ad8cb2016-01-21 19:43:58 -0700146 int ret;
147
148 debug("%s: reg=%x, value=%x", __func__, reg, value);
149 ret = pmic_write(dev, reg, &byte, 1);
150 debug(", ret=%d\n", ret);
Simon Glassc3f21c32015-06-23 15:39:01 -0600151
Simon Glass46ad8cb2016-01-21 19:43:58 -0700152 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600153}
154
155int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
156{
157 u8 byte;
158 int ret;
159
160 ret = pmic_reg_read(dev, reg);
161 if (ret < 0)
162 return ret;
163 byte = (ret & ~clr) | set;
164
165 return pmic_reg_write(dev, reg, byte);
166}
167
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200168UCLASS_DRIVER(pmic) = {
169 .id = UCLASS_PMIC,
170 .name = "pmic",
171};