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