blob: 64964e4e963ddb6291499f272d907331a88cc5c1 [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)
Simon Glass2c2d2c22017-05-18 20:09:32 -060022int pmic_bind_children(struct udevice *pmic, ofnode parent,
Przemyslaw Marczak10314982015-05-13 13:38:26 +020023 const struct pmic_child_info *child_info)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020024{
25 const struct pmic_child_info *info;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020026 struct driver *drv;
27 struct udevice *child;
28 const char *node_name;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020029 int bind_count = 0;
Simon Glass2c2d2c22017-05-18 20:09:32 -060030 ofnode node;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020031 int prefix_len;
32 int ret;
33
34 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
Simon Glassdd79d6e2017-01-17 16:52:55 -070035 dev_of_offset(pmic));
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020036
Simon Glass28529762017-08-05 15:45:54 -060037 ofnode_for_each_subnode(node, parent) {
Simon Glass2c2d2c22017-05-18 20:09:32 -060038 node_name = ofnode_get_name(node);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020039
Simon Glass2c2d2c22017-05-18 20:09:32 -060040 debug("* Found child node: '%s'\n", node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020041
42 child = NULL;
Przemyslaw Marczak10314982015-05-13 13:38:26 +020043 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glass869b0492015-06-23 15:39:16 -060044 debug(" - compatible prefix: '%s'\n", info->prefix);
45
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020046 prefix_len = strlen(info->prefix);
Simon Glassad28e002015-06-23 15:38:56 -060047 if (strncmp(info->prefix, node_name, prefix_len))
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020048 continue;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020049
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020050 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
Simon Glass2c2d2c22017-05-18 20:09:32 -060059 ret = device_bind_with_driver_data(pmic, drv, node_name,
60 0, node, &child);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020061 if (ret) {
62 debug(" - child binding error: %d\n", ret);
63 continue;
64 }
65
66 debug(" - bound child device: '%s'\n", child->name);
67
Simon Glass869b0492015-06-23 15:39:16 -060068 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020069
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
Simon Glass2c2d2c22017-05-18 20:09:32 -060081 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020082 return bind_count;
83}
Simon Glassc8db7b42016-01-21 19:43:55 -070084#endif
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020085
86int pmic_get(const char *name, struct udevice **devp)
87{
88 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
89}
90
91int pmic_reg_count(struct udevice *dev)
92{
93 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak10314982015-05-13 13:38:26 +020094
95 if (!ops || !ops->reg_count)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020096 return -ENOSYS;
97
Przemyslaw Marczak10314982015-05-13 13:38:26 +020098 return ops->reg_count(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020099}
100
101int 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);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200104
105 if (!buffer)
106 return -EFAULT;
107
108 if (!ops || !ops->read)
109 return -ENOSYS;
110
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200111 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200112}
113
114int 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);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200117
118 if (!buffer)
119 return -EFAULT;
120
121 if (!ops || !ops->write)
122 return -ENOSYS;
123
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200124 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200125}
126
Simon Glassc3f21c32015-06-23 15:39:01 -0600127int pmic_reg_read(struct udevice *dev, uint reg)
128{
129 u8 byte;
130 int ret;
131
Simon Glass46ad8cb2016-01-21 19:43:58 -0700132 debug("%s: reg=%x", __func__, reg);
Simon Glassc3f21c32015-06-23 15:39:01 -0600133 ret = pmic_read(dev, reg, &byte, 1);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700134 debug(", value=%x, ret=%d\n", byte, ret);
Simon Glassc3f21c32015-06-23 15:39:01 -0600135
136 return ret ? ret : byte;
137}
138
139int pmic_reg_write(struct udevice *dev, uint reg, uint value)
140{
141 u8 byte = value;
Simon Glass46ad8cb2016-01-21 19:43:58 -0700142 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);
Simon Glassc3f21c32015-06-23 15:39:01 -0600147
Simon Glass46ad8cb2016-01-21 19:43:58 -0700148 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600149}
150
151int 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
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200164UCLASS_DRIVER(pmic) = {
165 .id = UCLASS_PMIC,
166 .name = "pmic",
167};