blob: 79166b8bbcdd653eb63acf20f6023f6e87af4af8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +02005 */
6
7#include <common.h>
8#include <fdtdec.h>
9#include <errno.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.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
Simon Glassc8db7b42016-01-21 19:43:55 -070019#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Simon Glass2c2d2c22017-05-18 20:09:32 -060020int pmic_bind_children(struct udevice *pmic, ofnode parent,
Przemyslaw Marczak10314982015-05-13 13:38:26 +020021 const struct pmic_child_info *child_info)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020022{
23 const struct pmic_child_info *info;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020024 struct driver *drv;
25 struct udevice *child;
26 const char *node_name;
Felix Bracke23c3882017-11-27 09:14:16 +010027 const char *reg_name;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020028 int bind_count = 0;
Simon Glass2c2d2c22017-05-18 20:09:32 -060029 ofnode node;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020030 int prefix_len;
31 int ret;
32
33 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
Simon Glassdd79d6e2017-01-17 16:52:55 -070034 dev_of_offset(pmic));
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020035
Simon Glass28529762017-08-05 15:45:54 -060036 ofnode_for_each_subnode(node, parent) {
Simon Glass2c2d2c22017-05-18 20:09:32 -060037 node_name = ofnode_get_name(node);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020038
Simon Glass2c2d2c22017-05-18 20:09:32 -060039 debug("* Found child node: '%s'\n", node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020040
41 child = NULL;
Przemyslaw Marczak10314982015-05-13 13:38:26 +020042 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glass869b0492015-06-23 15:39:16 -060043 debug(" - compatible prefix: '%s'\n", info->prefix);
44
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020045 prefix_len = strlen(info->prefix);
Felix Bracke23c3882017-11-27 09:14:16 +010046 if (strncmp(info->prefix, node_name, prefix_len)) {
47 reg_name = ofnode_read_string(node,
48 "regulator-name");
49 if (!reg_name)
50 continue;
51 if (strncmp(info->prefix, reg_name, prefix_len))
52 continue;
53 }
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020054
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020055 drv = lists_driver_lookup_name(info->driver);
56 if (!drv) {
57 debug(" - driver: '%s' not found!\n",
58 info->driver);
59 continue;
60 }
61
62 debug(" - found child driver: '%s'\n", drv->name);
63
Simon Glass2c2d2c22017-05-18 20:09:32 -060064 ret = device_bind_with_driver_data(pmic, drv, node_name,
65 0, node, &child);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020066 if (ret) {
67 debug(" - child binding error: %d\n", ret);
68 continue;
69 }
70
71 debug(" - bound child device: '%s'\n", child->name);
72
Simon Glass869b0492015-06-23 15:39:16 -060073 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020074
75 debug(" - set 'child->driver_data': %lu\n",
76 child->driver_data);
77 break;
78 }
79
80 if (child)
81 bind_count++;
82 else
83 debug(" - compatible prefix not found\n");
84 }
85
Simon Glass2c2d2c22017-05-18 20:09:32 -060086 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020087 return bind_count;
88}
Simon Glassc8db7b42016-01-21 19:43:55 -070089#endif
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020090
91int pmic_get(const char *name, struct udevice **devp)
92{
93 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
94}
95
96int pmic_reg_count(struct udevice *dev)
97{
98 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak10314982015-05-13 13:38:26 +020099
100 if (!ops || !ops->reg_count)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200101 return -ENOSYS;
102
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200103 return ops->reg_count(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200104}
105
106int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
107{
108 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200109
110 if (!buffer)
111 return -EFAULT;
112
113 if (!ops || !ops->read)
114 return -ENOSYS;
115
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200116 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200117}
118
119int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
120{
121 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200122
123 if (!buffer)
124 return -EFAULT;
125
126 if (!ops || !ops->write)
127 return -ENOSYS;
128
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200129 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200130}
131
Simon Glassc3f21c32015-06-23 15:39:01 -0600132int pmic_reg_read(struct udevice *dev, uint reg)
133{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200134 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
135 u32 val = 0;
Simon Glassc3f21c32015-06-23 15:39:01 -0600136 int ret;
137
Lukasz Majewskie1066932018-05-15 16:26:36 +0200138 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
139 debug("Wrong transmission size [%d]\n", priv->trans_len);
140 return -EINVAL;
141 }
Simon Glassc3f21c32015-06-23 15:39:01 -0600142
Lukasz Majewskie1066932018-05-15 16:26:36 +0200143 debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len);
144 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
145 debug(", value=%x, ret=%d\n", val, ret);
146
147 return ret ? ret : val;
Simon Glassc3f21c32015-06-23 15:39:01 -0600148}
149
150int pmic_reg_write(struct udevice *dev, uint reg, uint value)
151{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200152 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700153 int ret;
154
Lukasz Majewskie1066932018-05-15 16:26:36 +0200155 if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) {
156 debug("Wrong transmission size [%d]\n", priv->trans_len);
157 return -EINVAL;
158 }
159
160 debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value,
161 priv->trans_len);
162 ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700163 debug(", ret=%d\n", ret);
Simon Glassc3f21c32015-06-23 15:39:01 -0600164
Simon Glass46ad8cb2016-01-21 19:43:58 -0700165 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600166}
167
168int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
169{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200170 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
171 u32 val = 0;
Simon Glassc3f21c32015-06-23 15:39:01 -0600172 int ret;
173
Lukasz Majewskie1066932018-05-15 16:26:36 +0200174 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
175 debug("Wrong transmission size [%d]\n", priv->trans_len);
176 return -EINVAL;
177 }
178
179 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glassc3f21c32015-06-23 15:39:01 -0600180 if (ret < 0)
181 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600182
Lukasz Majewskie1066932018-05-15 16:26:36 +0200183 val = (val & ~clr) | set;
184 return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glassc3f21c32015-06-23 15:39:01 -0600185}
186
Lukasz Majewski767a10a2018-05-15 16:26:35 +0200187static int pmic_pre_probe(struct udevice *dev)
188{
189 struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev);
190
191 pmic_priv->trans_len = 1;
192 return 0;
193}
194
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200195UCLASS_DRIVER(pmic) = {
196 .id = UCLASS_PMIC,
197 .name = "pmic",
Lukasz Majewski767a10a2018-05-15 16:26:35 +0200198 .pre_probe = pmic_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700199 .per_device_auto = sizeof(struct uc_pmic_priv),
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200200};