blob: db68c766f5d738e27041d8692b35c8d72902fe1a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +02002/*
3 * Copyright (C) 2014-2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +02005 */
6
7#include <common.h>
8#include <fdtdec.h>
9#include <errno.h>
10#include <dm.h>
Simon Glasse15bb3e2015-06-23 15:39:16 -060011#include <vsprintf.h>
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020012#include <dm/lists.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
15#include <power/pmic.h>
16#include <linux/ctype.h>
17
Simon Glass1f2b4b02016-01-21 19:43:55 -070018#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Simon Glass7a869e62017-05-18 20:09:32 -060019int pmic_bind_children(struct udevice *pmic, ofnode parent,
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020020 const struct pmic_child_info *child_info)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020021{
22 const struct pmic_child_info *info;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020023 struct driver *drv;
24 struct udevice *child;
25 const char *node_name;
Felix Brackbf802f52017-11-27 09:14:16 +010026 const char *reg_name;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020027 int bind_count = 0;
Simon Glass7a869e62017-05-18 20:09:32 -060028 ofnode node;
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020029 int prefix_len;
30 int ret;
31
32 debug("%s for '%s' at node offset: %d\n", __func__, pmic->name,
Simon Glasse160f7d2017-01-17 16:52:55 -070033 dev_of_offset(pmic));
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020034
Simon Glass3991f422017-08-05 15:45:54 -060035 ofnode_for_each_subnode(node, parent) {
Simon Glass7a869e62017-05-18 20:09:32 -060036 node_name = ofnode_get_name(node);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020037
Simon Glass7a869e62017-05-18 20:09:32 -060038 debug("* Found child node: '%s'\n", node_name);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020039
40 child = NULL;
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +020041 for (info = child_info; info->prefix && info->driver; info++) {
Simon Glasse15bb3e2015-06-23 15:39:16 -060042 debug(" - compatible prefix: '%s'\n", info->prefix);
43
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020044 prefix_len = strlen(info->prefix);
Felix Brackbf802f52017-11-27 09:14:16 +010045 if (strncmp(info->prefix, node_name, prefix_len)) {
46 reg_name = ofnode_read_string(node,
47 "regulator-name");
48 if (!reg_name)
49 continue;
50 if (strncmp(info->prefix, reg_name, prefix_len))
51 continue;
52 }
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020053
Przemyslaw Marczak4d9057e2015-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
Simon Glass7a869e62017-05-18 20:09:32 -060063 ret = device_bind_with_driver_data(pmic, drv, node_name,
64 0, node, &child);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020065 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 Glasse15bb3e2015-06-23 15:39:16 -060072 child->driver_data = trailing_strtol(node_name);
Przemyslaw Marczak4d9057e2015-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
Simon Glass7a869e62017-05-18 20:09:32 -060085 debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +020086 return bind_count;
87}
Simon Glass1f2b4b02016-01-21 19:43:55 -070088#endif
Przemyslaw Marczak4d9057e2015-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 Marczakf415a3e2015-05-13 13:38:26 +020098
99 if (!ops || !ops->reg_count)
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200100 return -ENOSYS;
101
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200102 return ops->reg_count(dev);
Przemyslaw Marczak4d9057e2015-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 Marczak4d9057e2015-04-20 20:07:41 +0200108
109 if (!buffer)
110 return -EFAULT;
111
112 if (!ops || !ops->read)
113 return -ENOSYS;
114
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200115 return ops->read(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-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 Marczak4d9057e2015-04-20 20:07:41 +0200121
122 if (!buffer)
123 return -EFAULT;
124
125 if (!ops || !ops->write)
126 return -ENOSYS;
127
Przemyslaw Marczakf415a3e2015-05-13 13:38:26 +0200128 return ops->write(dev, reg, buffer, len);
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200129}
130
Simon Glass6c69c7f2015-06-23 15:39:01 -0600131int pmic_reg_read(struct udevice *dev, uint reg)
132{
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200133 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
134 u32 val = 0;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600135 int ret;
136
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200137 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
138 debug("Wrong transmission size [%d]\n", priv->trans_len);
139 return -EINVAL;
140 }
Simon Glass6c69c7f2015-06-23 15:39:01 -0600141
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200142 debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len);
143 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
144 debug(", value=%x, ret=%d\n", val, ret);
145
146 return ret ? ret : val;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600147}
148
149int pmic_reg_write(struct udevice *dev, uint reg, uint value)
150{
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200151 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7d577992016-01-21 19:43:58 -0700152 int ret;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600153
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200154 if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) {
155 debug("Wrong transmission size [%d]\n", priv->trans_len);
156 return -EINVAL;
157 }
158
159 debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value,
160 priv->trans_len);
161 ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len);
Simon Glass7d577992016-01-21 19:43:58 -0700162 debug(", ret=%d\n", ret);
163
164 return ret;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600165}
166
167int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set)
168{
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200169 struct uc_pmic_priv *priv = dev_get_uclass_priv(dev);
170 u32 val = 0;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600171 int ret;
172
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200173 if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) {
174 debug("Wrong transmission size [%d]\n", priv->trans_len);
175 return -EINVAL;
176 }
177
178 ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glass6c69c7f2015-06-23 15:39:01 -0600179 if (ret < 0)
180 return ret;
Simon Glass6c69c7f2015-06-23 15:39:01 -0600181
Lukasz Majewski655b24c2018-05-15 16:26:36 +0200182 val = (val & ~clr) | set;
183 return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len);
Simon Glass6c69c7f2015-06-23 15:39:01 -0600184}
185
Lukasz Majewskib8a6d672018-05-15 16:26:35 +0200186static int pmic_pre_probe(struct udevice *dev)
187{
188 struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev);
189
190 pmic_priv->trans_len = 1;
191 return 0;
192}
193
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200194UCLASS_DRIVER(pmic) = {
195 .id = UCLASS_PMIC,
196 .name = "pmic",
Lukasz Majewskib8a6d672018-05-15 16:26:35 +0200197 .pre_probe = pmic_pre_probe,
198 .per_device_auto_alloc_size = sizeof(struct uc_pmic_priv),
Przemyslaw Marczak4d9057e2015-04-20 20:07:41 +0200199};