Tom Rini | 8b0c8a1 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <errno.h> |
| 8 | #include <i2c.h> |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 9 | #include <misc.h> |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 10 | #include <sysreset.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 11 | #include <time.h> |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 12 | #include <dm/device.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 14 | #include <dm/lists.h> |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 15 | #include <power/pmic.h> |
Patrick Delaunay | 91be594 | 2019-02-04 11:26:16 +0100 | [diff] [blame] | 16 | #include <power/stpmic1.h> |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 17 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 18 | #define STPMIC1_NUM_OF_REGS 0x100 |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 19 | |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 20 | #define STPMIC1_NVM_SIZE 8 |
| 21 | #define STPMIC1_NVM_POLL_TIMEOUT 100000 |
| 22 | #define STPMIC1_NVM_START_ADDRESS 0xf8 |
| 23 | |
| 24 | enum pmic_nvm_op { |
| 25 | SHADOW_READ, |
| 26 | SHADOW_WRITE, |
| 27 | NVM_READ, |
| 28 | NVM_WRITE, |
| 29 | }; |
| 30 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 31 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 32 | static const struct pmic_child_info stpmic1_children_info[] = { |
| 33 | { .prefix = "ldo", .driver = "stpmic1_ldo" }, |
| 34 | { .prefix = "buck", .driver = "stpmic1_buck" }, |
| 35 | { .prefix = "vref_ddr", .driver = "stpmic1_vref_ddr" }, |
Patrick Delaunay | 7d129dc | 2023-04-27 15:36:38 +0200 | [diff] [blame] | 36 | { .prefix = "vref-ddr", .driver = "stpmic1_vref_ddr" }, |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 37 | { .prefix = "pwr_sw", .driver = "stpmic1_pwr_sw" }, |
Patrick Delaunay | 7d129dc | 2023-04-27 15:36:38 +0200 | [diff] [blame] | 38 | { .prefix = "pwr-sw", .driver = "stpmic1_pwr_sw" }, |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 39 | { .prefix = "boost", .driver = "stpmic1_boost" }, |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 40 | { }, |
| 41 | }; |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 42 | #endif /* DM_REGULATOR */ |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 43 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 44 | static int stpmic1_reg_count(struct udevice *dev) |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 45 | { |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 46 | return STPMIC1_NUM_OF_REGS; |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 49 | static int stpmic1_write(struct udevice *dev, uint reg, const uint8_t *buff, |
| 50 | int len) |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | ret = dm_i2c_write(dev, reg, buff, len); |
| 55 | if (ret) |
| 56 | dev_err(dev, "%s: failed to write register %#x :%d", |
| 57 | __func__, reg, ret); |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 62 | static int stpmic1_read(struct udevice *dev, uint reg, uint8_t *buff, int len) |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | ret = dm_i2c_read(dev, reg, buff, len); |
| 67 | if (ret) |
| 68 | dev_err(dev, "%s: failed to read register %#x : %d", |
| 69 | __func__, reg, ret); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 74 | static int stpmic1_bind(struct udevice *dev) |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 75 | { |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 76 | int ret; |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 77 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 78 | ofnode regulators_node; |
| 79 | int children; |
| 80 | |
| 81 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 82 | if (!ofnode_valid(regulators_node)) { |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 83 | dev_dbg(dev, "regulators subnode not found!"); |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 84 | return -ENXIO; |
| 85 | } |
| 86 | dev_dbg(dev, "found regulators subnode\n"); |
| 87 | |
| 88 | children = pmic_bind_children(dev, regulators_node, |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 89 | stpmic1_children_info); |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 90 | if (!children) |
| 91 | dev_dbg(dev, "no child found\n"); |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 92 | #endif /* DM_REGULATOR */ |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 93 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 94 | if (!IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 95 | ret = device_bind_driver(dev, "stpmic1-nvm", |
| 96 | "stpmic1-nvm", NULL); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | } |
| 100 | |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 101 | if (CONFIG_IS_ENABLED(SYSRESET)) |
| 102 | return device_bind_driver(dev, "stpmic1-sysreset", |
| 103 | "stpmic1-sysreset", NULL); |
| 104 | |
Patrice Chotard | 0de0541 | 2018-04-26 17:13:10 +0200 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 108 | static struct dm_pmic_ops stpmic1_ops = { |
| 109 | .reg_count = stpmic1_reg_count, |
| 110 | .read = stpmic1_read, |
| 111 | .write = stpmic1_write, |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 114 | static const struct udevice_id stpmic1_ids[] = { |
| 115 | { .compatible = "st,stpmic1" }, |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 116 | { } |
| 117 | }; |
| 118 | |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 119 | U_BOOT_DRIVER(pmic_stpmic1) = { |
| 120 | .name = "stpmic1_pmic", |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 121 | .id = UCLASS_PMIC, |
Patrick Delaunay | d79218f | 2019-02-04 11:26:17 +0100 | [diff] [blame] | 122 | .of_match = stpmic1_ids, |
| 123 | .bind = stpmic1_bind, |
| 124 | .ops = &stpmic1_ops, |
Patrick Delaunay | 3cba451 | 2018-03-12 10:46:12 +0100 | [diff] [blame] | 125 | }; |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 126 | |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 127 | #ifndef CONFIG_SPL_BUILD |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 128 | static int stpmic1_nvm_rw(struct udevice *dev, u8 addr, u8 *buf, int buf_len, |
| 129 | enum pmic_nvm_op op) |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 130 | { |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 131 | unsigned long timeout; |
| 132 | u8 cmd = STPMIC1_NVM_CMD_READ; |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 133 | int ret, len = buf_len; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 134 | |
| 135 | if (addr < STPMIC1_NVM_START_ADDRESS) |
| 136 | return -EACCES; |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 137 | if (addr + buf_len > STPMIC1_NVM_START_ADDRESS + STPMIC1_NVM_SIZE) |
| 138 | len = STPMIC1_NVM_START_ADDRESS + STPMIC1_NVM_SIZE - addr; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 139 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 140 | if (op == SHADOW_READ) { |
| 141 | ret = pmic_read(dev, addr, buf, len); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | else |
| 145 | return len; |
| 146 | } |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 147 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 148 | if (op == SHADOW_WRITE) { |
| 149 | ret = pmic_write(dev, addr, buf, len); |
| 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | else |
| 153 | return len; |
| 154 | } |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 155 | |
| 156 | if (op == NVM_WRITE) { |
| 157 | cmd = STPMIC1_NVM_CMD_PROGRAM; |
| 158 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 159 | ret = pmic_write(dev, addr, buf, len); |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | ret = pmic_reg_read(dev, STPMIC1_NVM_CR); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
| 168 | ret = pmic_reg_write(dev, STPMIC1_NVM_CR, ret | cmd); |
| 169 | if (ret < 0) |
| 170 | return ret; |
| 171 | |
| 172 | timeout = timer_get_us() + STPMIC1_NVM_POLL_TIMEOUT; |
| 173 | for (;;) { |
| 174 | ret = pmic_reg_read(dev, STPMIC1_NVM_SR); |
| 175 | if (ret < 0) |
| 176 | return ret; |
| 177 | |
| 178 | if (!(ret & STPMIC1_NVM_BUSY)) |
| 179 | break; |
| 180 | |
| 181 | if (time_after(timer_get_us(), timeout)) |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | if (ret & STPMIC1_NVM_BUSY) |
| 186 | return -ETIMEDOUT; |
| 187 | |
| 188 | if (op == NVM_READ) { |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 189 | ret = pmic_read(dev, addr, buf, len); |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | } |
| 193 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 194 | return len; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 197 | static int stpmic1_nvm_read(struct udevice *dev, int offset, |
| 198 | void *buf, int size) |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 199 | { |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 200 | enum pmic_nvm_op op = NVM_READ; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 201 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 202 | if (offset < 0) { |
| 203 | op = SHADOW_READ; |
| 204 | offset = -offset; |
| 205 | } |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 206 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 207 | return stpmic1_nvm_rw(dev->parent, offset, buf, size, op); |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 210 | static int stpmic1_nvm_write(struct udevice *dev, int offset, |
| 211 | const void *buf, int size) |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 212 | { |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 213 | enum pmic_nvm_op op = NVM_WRITE; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 214 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 215 | if (offset < 0) { |
| 216 | op = SHADOW_WRITE; |
| 217 | offset = -offset; |
| 218 | } |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 219 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 220 | return stpmic1_nvm_rw(dev->parent, offset, (void *)buf, size, op); |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 223 | static const struct misc_ops stpmic1_nvm_ops = { |
| 224 | .read = stpmic1_nvm_read, |
| 225 | .write = stpmic1_nvm_write, |
| 226 | }; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 227 | |
Patrick Delaunay | c9a5d39 | 2019-08-02 13:08:03 +0200 | [diff] [blame] | 228 | U_BOOT_DRIVER(stpmic1_nvm) = { |
| 229 | .name = "stpmic1-nvm", |
| 230 | .id = UCLASS_MISC, |
| 231 | .ops = &stpmic1_nvm_ops, |
| 232 | }; |
Patrick Delaunay | d592d13 | 2019-02-04 11:26:22 +0100 | [diff] [blame] | 233 | #endif /* CONFIG_SPL_BUILD */ |
| 234 | |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 235 | #ifdef CONFIG_SYSRESET |
| 236 | static int stpmic1_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 237 | { |
Patrick Delaunay | 8844c4a | 2019-08-02 13:08:04 +0200 | [diff] [blame] | 238 | struct udevice *pmic_dev = dev->parent; |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 239 | int ret; |
| 240 | |
Patrick Delaunay | f6d0220 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 241 | if (type != SYSRESET_POWER && type != SYSRESET_POWER_OFF) |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 242 | return -EPROTONOSUPPORT; |
| 243 | |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 244 | ret = pmic_reg_read(pmic_dev, STPMIC1_MAIN_CR); |
| 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | |
Patrick Delaunay | f6d0220 | 2019-05-20 09:47:07 +0200 | [diff] [blame] | 248 | ret |= STPMIC1_SWOFF; |
| 249 | ret &= ~STPMIC1_RREQ_EN; |
| 250 | /* request Power Cycle */ |
| 251 | if (type == SYSRESET_POWER) |
| 252 | ret |= STPMIC1_RREQ_EN; |
| 253 | |
| 254 | ret = pmic_reg_write(pmic_dev, STPMIC1_MAIN_CR, ret); |
Patrick Delaunay | 537581d | 2019-02-04 11:26:19 +0100 | [diff] [blame] | 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
| 258 | return -EINPROGRESS; |
| 259 | } |
| 260 | |
| 261 | static struct sysreset_ops stpmic1_sysreset_ops = { |
| 262 | .request = stpmic1_sysreset_request, |
| 263 | }; |
| 264 | |
| 265 | U_BOOT_DRIVER(stpmic1_sysreset) = { |
| 266 | .name = "stpmic1-sysreset", |
| 267 | .id = UCLASS_SYSRESET, |
| 268 | .ops = &stpmic1_sysreset_ops, |
| 269 | }; |
| 270 | #endif |