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