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