Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014-2015 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_PMIC |
| 8 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <errno.h> |
| 12 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 14 | #include <vsprintf.h> |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 15 | #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 Glass | c8db7b4 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 21 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 22 | int pmic_bind_children(struct udevice *pmic, ofnode parent, |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 23 | const struct pmic_child_info *child_info) |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 24 | { |
| 25 | const struct pmic_child_info *info; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 26 | struct driver *drv; |
| 27 | struct udevice *child; |
| 28 | const char *node_name; |
Felix Brack | e23c388 | 2017-11-27 09:14:16 +0100 | [diff] [blame] | 29 | const char *reg_name; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 30 | int bind_count = 0; |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 31 | ofnode node; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 32 | int prefix_len; |
| 33 | int ret; |
| 34 | |
| 35 | debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 36 | dev_of_offset(pmic)); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 37 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 38 | ofnode_for_each_subnode(node, parent) { |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 39 | node_name = ofnode_get_name(node); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 40 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 41 | debug("* Found child node: '%s'\n", node_name); |
Patrick Delaunay | c550c2ba | 2022-10-26 15:05:10 +0200 | [diff] [blame] | 42 | if (!ofnode_is_enabled(node)) { |
| 43 | debug(" - ignoring disabled device\n"); |
| 44 | continue; |
| 45 | } |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 46 | |
| 47 | child = NULL; |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 48 | for (info = child_info; info->prefix && info->driver; info++) { |
Simon Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 49 | debug(" - compatible prefix: '%s'\n", info->prefix); |
| 50 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 51 | prefix_len = strlen(info->prefix); |
Felix Brack | e23c388 | 2017-11-27 09:14:16 +0100 | [diff] [blame] | 52 | if (strncmp(info->prefix, node_name, prefix_len)) { |
| 53 | reg_name = ofnode_read_string(node, |
| 54 | "regulator-name"); |
| 55 | if (!reg_name) |
| 56 | continue; |
| 57 | if (strncmp(info->prefix, reg_name, prefix_len)) |
| 58 | continue; |
| 59 | } |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 60 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 61 | drv = lists_driver_lookup_name(info->driver); |
| 62 | if (!drv) { |
| 63 | debug(" - driver: '%s' not found!\n", |
| 64 | info->driver); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | debug(" - found child driver: '%s'\n", drv->name); |
| 69 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 70 | ret = device_bind_with_driver_data(pmic, drv, node_name, |
| 71 | 0, node, &child); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 72 | if (ret) { |
| 73 | debug(" - child binding error: %d\n", ret); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | debug(" - bound child device: '%s'\n", child->name); |
| 78 | |
Simon Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 79 | child->driver_data = trailing_strtol(node_name); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 80 | |
| 81 | debug(" - set 'child->driver_data': %lu\n", |
| 82 | child->driver_data); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (child) |
| 87 | bind_count++; |
| 88 | else |
| 89 | debug(" - compatible prefix not found\n"); |
| 90 | } |
| 91 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 92 | debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 93 | return bind_count; |
| 94 | } |
Simon Glass | c8db7b4 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 95 | #endif |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 96 | |
| 97 | int pmic_get(const char *name, struct udevice **devp) |
| 98 | { |
| 99 | return uclass_get_device_by_name(UCLASS_PMIC, name, devp); |
| 100 | } |
| 101 | |
| 102 | int pmic_reg_count(struct udevice *dev) |
| 103 | { |
| 104 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 105 | |
| 106 | if (!ops || !ops->reg_count) |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 107 | return -ENOSYS; |
| 108 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 109 | return ops->reg_count(dev); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int pmic_read(struct udevice *dev, uint reg, uint8_t *buffer, int len) |
| 113 | { |
| 114 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 115 | |
| 116 | if (!buffer) |
| 117 | return -EFAULT; |
| 118 | |
| 119 | if (!ops || !ops->read) |
| 120 | return -ENOSYS; |
| 121 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 122 | return ops->read(dev, reg, buffer, len); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int pmic_write(struct udevice *dev, uint reg, const uint8_t *buffer, int len) |
| 126 | { |
| 127 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 128 | |
| 129 | if (!buffer) |
| 130 | return -EFAULT; |
| 131 | |
| 132 | if (!ops || !ops->write) |
| 133 | return -ENOSYS; |
| 134 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 135 | return ops->write(dev, reg, buffer, len); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 138 | int pmic_reg_read(struct udevice *dev, uint reg) |
| 139 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 140 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
| 141 | u32 val = 0; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 142 | int ret; |
| 143 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 144 | if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) { |
| 145 | debug("Wrong transmission size [%d]\n", priv->trans_len); |
| 146 | return -EINVAL; |
| 147 | } |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 148 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 149 | debug("%s: reg=%x priv->trans_len:%d", __func__, reg, priv->trans_len); |
| 150 | ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len); |
| 151 | debug(", value=%x, ret=%d\n", val, ret); |
| 152 | |
| 153 | return ret ? ret : val; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int pmic_reg_write(struct udevice *dev, uint reg, uint value) |
| 157 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 158 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 159 | int ret; |
| 160 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 161 | if (priv->trans_len < 1 || priv->trans_len > sizeof(value)) { |
| 162 | debug("Wrong transmission size [%d]\n", priv->trans_len); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | debug("%s: reg=%x, value=%x priv->trans_len:%d", __func__, reg, value, |
| 167 | priv->trans_len); |
| 168 | ret = pmic_write(dev, reg, (uint8_t *)&value, priv->trans_len); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 169 | debug(", ret=%d\n", ret); |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 170 | |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 171 | return ret; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set) |
| 175 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 176 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
| 177 | u32 val = 0; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 178 | int ret; |
| 179 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 180 | if (priv->trans_len < 1 || priv->trans_len > sizeof(val)) { |
| 181 | debug("Wrong transmission size [%d]\n", priv->trans_len); |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
| 185 | ret = pmic_read(dev, reg, (uint8_t *)&val, priv->trans_len); |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 186 | if (ret < 0) |
| 187 | return ret; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 188 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 189 | val = (val & ~clr) | set; |
| 190 | return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len); |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 191 | } |
| 192 | |
Lukasz Majewski | 767a10a | 2018-05-15 16:26:35 +0200 | [diff] [blame] | 193 | static int pmic_pre_probe(struct udevice *dev) |
| 194 | { |
| 195 | struct uc_pmic_priv *pmic_priv = dev_get_uclass_priv(dev); |
| 196 | |
| 197 | pmic_priv->trans_len = 1; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 201 | UCLASS_DRIVER(pmic) = { |
| 202 | .id = UCLASS_PMIC, |
| 203 | .name = "pmic", |
Lukasz Majewski | 767a10a | 2018-05-15 16:26:35 +0200 | [diff] [blame] | 204 | .pre_probe = pmic_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 205 | .per_device_auto = sizeof(struct uc_pmic_priv), |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 206 | }; |