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 | |
| 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <errno.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 12 | #include <vsprintf.h> |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 13 | #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 Glass | c8db7b4 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 19 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 20 | int pmic_bind_children(struct udevice *pmic, ofnode parent, |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 21 | const struct pmic_child_info *child_info) |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 22 | { |
| 23 | const struct pmic_child_info *info; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 24 | struct driver *drv; |
| 25 | struct udevice *child; |
| 26 | const char *node_name; |
Felix Brack | e23c388 | 2017-11-27 09:14:16 +0100 | [diff] [blame] | 27 | const char *reg_name; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 28 | int bind_count = 0; |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 29 | ofnode node; |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 30 | int prefix_len; |
| 31 | int ret; |
| 32 | |
| 33 | debug("%s for '%s' at node offset: %d\n", __func__, pmic->name, |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 34 | dev_of_offset(pmic)); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 35 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 36 | ofnode_for_each_subnode(node, parent) { |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 37 | node_name = ofnode_get_name(node); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 38 | |
Simon Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 39 | debug("* Found child node: '%s'\n", node_name); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 40 | |
| 41 | child = NULL; |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 42 | for (info = child_info; info->prefix && info->driver; info++) { |
Simon Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 43 | debug(" - compatible prefix: '%s'\n", info->prefix); |
| 44 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 45 | prefix_len = strlen(info->prefix); |
Felix Brack | e23c388 | 2017-11-27 09:14:16 +0100 | [diff] [blame] | 46 | 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 Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 54 | |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 55 | 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 Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 64 | ret = device_bind_with_driver_data(pmic, drv, node_name, |
| 65 | 0, node, &child); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 66 | 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 Glass | 869b049 | 2015-06-23 15:39:16 -0600 | [diff] [blame] | 73 | child->driver_data = trailing_strtol(node_name); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 74 | |
| 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 Glass | 2c2d2c2 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 86 | debug("Bound: %d children for PMIC: '%s'\n", bind_count, pmic->name); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 87 | return bind_count; |
| 88 | } |
Simon Glass | c8db7b4 | 2016-01-21 19:43:55 -0700 | [diff] [blame] | 89 | #endif |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 90 | |
| 91 | int pmic_get(const char *name, struct udevice **devp) |
| 92 | { |
| 93 | return uclass_get_device_by_name(UCLASS_PMIC, name, devp); |
| 94 | } |
| 95 | |
| 96 | int pmic_reg_count(struct udevice *dev) |
| 97 | { |
| 98 | const struct dm_pmic_ops *ops = dev_get_driver_ops(dev); |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 99 | |
| 100 | if (!ops || !ops->reg_count) |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 101 | return -ENOSYS; |
| 102 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 103 | return ops->reg_count(dev); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int 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 Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 109 | |
| 110 | if (!buffer) |
| 111 | return -EFAULT; |
| 112 | |
| 113 | if (!ops || !ops->read) |
| 114 | return -ENOSYS; |
| 115 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 116 | return ops->read(dev, reg, buffer, len); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int 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 Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 122 | |
| 123 | if (!buffer) |
| 124 | return -EFAULT; |
| 125 | |
| 126 | if (!ops || !ops->write) |
| 127 | return -ENOSYS; |
| 128 | |
Przemyslaw Marczak | 1031498 | 2015-05-13 13:38:26 +0200 | [diff] [blame] | 129 | return ops->write(dev, reg, buffer, len); |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 132 | int pmic_reg_read(struct udevice *dev, uint reg) |
| 133 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 134 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
| 135 | u32 val = 0; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 136 | int ret; |
| 137 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 138 | 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 Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 142 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 143 | 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 Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int pmic_reg_write(struct udevice *dev, uint reg, uint value) |
| 151 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 152 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 153 | int ret; |
| 154 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 155 | 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 Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 163 | debug(", ret=%d\n", ret); |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 164 | |
Simon Glass | 46ad8cb | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 165 | return ret; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int pmic_clrsetbits(struct udevice *dev, uint reg, uint clr, uint set) |
| 169 | { |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 170 | struct uc_pmic_priv *priv = dev_get_uclass_priv(dev); |
| 171 | u32 val = 0; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 172 | int ret; |
| 173 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 174 | 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 Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 180 | if (ret < 0) |
| 181 | return ret; |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 182 | |
Lukasz Majewski | e106693 | 2018-05-15 16:26:36 +0200 | [diff] [blame] | 183 | val = (val & ~clr) | set; |
| 184 | return pmic_write(dev, reg, (uint8_t *)&val, priv->trans_len); |
Simon Glass | c3f21c3 | 2015-06-23 15:39:01 -0600 | [diff] [blame] | 185 | } |
| 186 | |
Lukasz Majewski | 767a10a | 2018-05-15 16:26:35 +0200 | [diff] [blame] | 187 | static 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 Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 195 | UCLASS_DRIVER(pmic) = { |
| 196 | .id = UCLASS_PMIC, |
| 197 | .name = "pmic", |
Lukasz Majewski | 767a10a | 2018-05-15 16:26:35 +0200 | [diff] [blame] | 198 | .pre_probe = pmic_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 199 | .per_device_auto = sizeof(struct uc_pmic_priv), |
Przemyslaw Marczak | 9a8bc95 | 2015-04-20 20:07:41 +0200 | [diff] [blame] | 200 | }; |