blob: 5dcf6d8079d270e8b5c03f580316131e9137e6f6 [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
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_PMIC
8
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +02009#include <common.h>
10#include <fdtdec.h>
11#include <errno.h>
12#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass869b0492015-06-23 15:39:16 -060014#include <vsprintf.h>
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020015#include <dm/lists.h>
16#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
18#include <power/pmic.h>
19#include <linux/ctype.h>
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;
Felix Bracke23c3882017-11-27 09:14:16 +010029 const char *reg_name;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020030 int bind_count = 0;
Simon Glass2c2d2c22017-05-18 20:09:32 -060031 ofnode node;
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020032 int prefix_len;
33 int ret;
34
35 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
Simon Glassdd79d6e2017-01-17 16:52:55 -070036 dev_of_offset(pmic));
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020037
Simon Glass28529762017-08-05 15:45:54 -060038 ofnode_for_each_subnode(node, parent) {
Simon Glass2c2d2c22017-05-18 20:09:32 -060039 node_name = ofnode_get_name(node);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020040
Simon Glass2c2d2c22017-05-18 20:09:32 -060041 debug("* Found child node: '%s'\n", node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020042
43 child = NULL;
Przemyslaw Marczak10314982015-05-13 13:38:26 +020044 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glass869b0492015-06-23 15:39:16 -060045 debug(" - compatible prefix: '%s'\n", info->prefix);
46
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020047 prefix_len = strlen(info->prefix);
Felix Bracke23c3882017-11-27 09:14:16 +010048 if (strncmp(info->prefix, node_name, prefix_len)) {
49 reg_name = ofnode_read_string(node,
50 "regulator-name");
51 if (!reg_name)
52 continue;
53 if (strncmp(info->prefix, reg_name, prefix_len))
54 continue;
55 }
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020056
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020057 drv = lists_driver_lookup_name(info->driver);
58 if (!drv) {
59 debug(" - driver: '%s' not found!\n",
60 info->driver);
61 continue;
62 }
63
64 debug(" - found child driver: '%s'\n", drv->name);
65
Simon Glass2c2d2c22017-05-18 20:09:32 -060066 ret = device_bind_with_driver_data(pmic, drv, node_name,
67 0, node, &child);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020068 if (ret) {
69 debug(" - child binding error: %d\n", ret);
70 continue;
71 }
72
73 debug(" - bound child device: '%s'\n", child->name);
74
Simon Glass869b0492015-06-23 15:39:16 -060075 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020076
77 debug(" - set 'child->driver_data': %lu\n",
78 child->driver_data);
79 break;
80 }
81
82 if (child)
83 bind_count++;
84 else
85 debug(" - compatible prefix not found\n");
86 }
87
Simon Glass2c2d2c22017-05-18 20:09:32 -060088 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020089 return bind_count;
90}
Simon Glassc8db7b42016-01-21 19:43:55 -070091#endif
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +020092
93int pmic_get(const char *name, struct udevice **devp)
94{
95 return uclass_get_device_by_name(UCLASS_PMIC, name, devp);
96}
97
98int pmic_reg_count(struct udevice *dev)
99{
100 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200101
102 if (!ops || !ops->reg_count)
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200103 return -ENOSYS;
104
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200105 return ops->reg_count(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200106}
107
108int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len)
109{
110 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200111
112 if (!buffer)
113 return -EFAULT;
114
115 if (!ops || !ops->read)
116 return -ENOSYS;
117
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200118 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200119}
120
121int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len)
122{
123 const struct dm_pmic_ops *ops = dev_get_driver_ops(dev);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200124
125 if (!buffer)
126 return -EFAULT;
127
128 if (!ops || !ops->write)
129 return -ENOSYS;
130
Przemyslaw Marczak10314982015-05-13 13:38:26 +0200131 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200132}
133
Simon Glassc3f21c32015-06-23 15:39:01 -0600134int pmic_reg_read(struct udevice *dev, uint reg)
135{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200136 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
137 u32 val = 0;
Simon Glassc3f21c32015-06-23 15:39:01 -0600138 int ret;
139
Lukasz Majewskie1066932018-05-15 16:26:36 +0200140 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
141 debug("Wrong transmission size [%d]\n", priv->trans_len);
142 return -EINVAL;
143 }
Simon Glassc3f21c32015-06-23 15:39:01 -0600144
Lukasz Majewskie1066932018-05-15 16:26:36 +0200145 debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len);
146 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
147 debug(", value=%x, ret=%d\n", val, ret);
148
149 return ret ? ret : val;
Simon Glassc3f21c32015-06-23 15:39:01 -0600150}
151
152int pmic_reg_write(struct udevice *dev, uint reg, uint value)
153{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200154 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700155 int ret;
156
Lukasz Majewskie1066932018-05-15 16:26:36 +0200157 if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) {
158 debug("Wrong transmission size [%d]\n", priv->trans_len);
159 return -EINVAL;
160 }
161
162 debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value,
163 priv->trans_len);
164 ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len);
Simon Glass46ad8cb2016-01-21 19:43:58 -0700165 debug(", ret=%d\n", ret);
Simon Glassc3f21c32015-06-23 15:39:01 -0600166
Simon Glass46ad8cb2016-01-21 19:43:58 -0700167 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600168}
169
170int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
171{
Lukasz Majewskie1066932018-05-15 16:26:36 +0200172 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
173 u32 val = 0;
Simon Glassc3f21c32015-06-23 15:39:01 -0600174 int ret;
175
Lukasz Majewskie1066932018-05-15 16:26:36 +0200176 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
177 debug("Wrong transmission size [%d]\n", priv->trans_len);
178 return -EINVAL;
179 }
180
181 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glassc3f21c32015-06-23 15:39:01 -0600182 if (ret < 0)
183 return ret;
Simon Glassc3f21c32015-06-23 15:39:01 -0600184
Lukasz Majewskie1066932018-05-15 16:26:36 +0200185 val = (val & ~clr) | set;
186 return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glassc3f21c32015-06-23 15:39:01 -0600187}
188
Lukasz Majewski767a10a2018-05-15 16:26:35 +0200189static int pmic_pre_probe(struct udevice *dev)
190{
191 struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev);
192
193 pmic_priv->trans_len = 1;
194 return 0;
195}
196
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200197UCLASS_DRIVER(pmic) = {
198 .id = UCLASS_PMIC,
199 .name = "pmic",
Lukasz Majewski767a10a2018-05-15 16:26:35 +0200200 .pre_probe = pmic_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700201 .per_device_auto = sizeof(struct uc_pmic_priv),
Przemyslaw Marczak9a8bc952015-04-20 20:07:41 +0200202};