blob: 2c85410b1bf539d33d9ed7d7ffe2419ef9b85727 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay3cba4512018-03-12 10:46:12 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay3cba4512018-03-12 10:46:12 +01004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <i2c.h>
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020010#include <misc.h>
Patrick Delaunay537581d2019-02-04 11:26:19 +010011#include <sysreset.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Patrick Delaunay537581d2019-02-04 11:26:19 +010013#include <dm/device.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Patrick Delaunay537581d2019-02-04 11:26:19 +010015#include <dm/lists.h>
Patrick Delaunay3cba4512018-03-12 10:46:12 +010016#include <power/pmic.h>
Patrick Delaunay91be5942019-02-04 11:26:16 +010017#include <power/stpmic1.h>
Patrick Delaunay3cba4512018-03-12 10:46:12 +010018
Patrick Delaunayd79218f2019-02-04 11:26:17 +010019#define STPMIC1_NUM_OF_REGS 0x100
Patrick Delaunay3cba4512018-03-12 10:46:12 +010020
Patrick Delaunayd592d132019-02-04 11:26:22 +010021#define STPMIC1_NVM_SIZE 8
22#define STPMIC1_NVM_POLL_TIMEOUT 100000
23#define STPMIC1_NVM_START_ADDRESS 0xf8
24
25enum pmic_nvm_op {
26 SHADOW_READ,
27 SHADOW_WRITE,
28 NVM_READ,
29 NVM_WRITE,
30};
31
Patrick Delaunayd79218f2019-02-04 11:26:17 +010032#if CONFIG_IS_ENABLED(DM_REGULATOR)
33static 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" },
37 { .prefix = "pwr_sw", .driver = "stpmic1_pwr_sw" },
38 { .prefix = "boost", .driver = "stpmic1_boost" },
Patrice Chotard0de05412018-04-26 17:13:10 +020039 { },
40};
Patrick Delaunayd79218f2019-02-04 11:26:17 +010041#endif /* DM_REGULATOR */
Patrice Chotard0de05412018-04-26 17:13:10 +020042
Patrick Delaunayd79218f2019-02-04 11:26:17 +010043static int stpmic1_reg_count(struct udevice *dev)
Patrick Delaunay3cba4512018-03-12 10:46:12 +010044{
Patrick Delaunayd79218f2019-02-04 11:26:17 +010045 return STPMIC1_NUM_OF_REGS;
Patrick Delaunay3cba4512018-03-12 10:46:12 +010046}
47
Patrick Delaunayd79218f2019-02-04 11:26:17 +010048static int stpmic1_write(struct udevice *dev, uint reg, const uint8_t *buff,
49 int len)
Patrick Delaunay3cba4512018-03-12 10:46:12 +010050{
51 int ret;
52
53 ret = dm_i2c_write(dev, reg, buff, len);
54 if (ret)
55 dev_err(dev, "%s: failed to write register %#x :%d",
56 __func__, reg, ret);
57
58 return ret;
59}
60
Patrick Delaunayd79218f2019-02-04 11:26:17 +010061static int stpmic1_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Patrick Delaunay3cba4512018-03-12 10:46:12 +010062{
63 int ret;
64
65 ret = dm_i2c_read(dev, reg, buff, len);
66 if (ret)
67 dev_err(dev, "%s: failed to read register %#x : %d",
68 __func__, reg, ret);
69
70 return ret;
71}
72
Patrick Delaunayd79218f2019-02-04 11:26:17 +010073static int stpmic1_bind(struct udevice *dev)
Patrice Chotard0de05412018-04-26 17:13:10 +020074{
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020075 int ret;
Patrick Delaunayd79218f2019-02-04 11:26:17 +010076#if CONFIG_IS_ENABLED(DM_REGULATOR)
Patrice Chotard0de05412018-04-26 17:13:10 +020077 ofnode regulators_node;
78 int children;
79
80 regulators_node = dev_read_subnode(dev, "regulators");
81 if (!ofnode_valid(regulators_node)) {
Patrick Delaunayd79218f2019-02-04 11:26:17 +010082 dev_dbg(dev, "regulators subnode not found!");
Patrice Chotard0de05412018-04-26 17:13:10 +020083 return -ENXIO;
84 }
85 dev_dbg(dev, "found regulators subnode\n");
86
87 children = pmic_bind_children(dev, regulators_node,
Patrick Delaunayd79218f2019-02-04 11:26:17 +010088 stpmic1_children_info);
Patrice Chotard0de05412018-04-26 17:13:10 +020089 if (!children)
90 dev_dbg(dev, "no child found\n");
Patrick Delaunayd79218f2019-02-04 11:26:17 +010091#endif /* DM_REGULATOR */
Patrice Chotard0de05412018-04-26 17:13:10 +020092
Patrick Delaunayc9a5d392019-08-02 13:08:03 +020093 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
94 ret = device_bind_driver(dev, "stpmic1-nvm",
95 "stpmic1-nvm", NULL);
96 if (ret)
97 return ret;
98 }
99
Patrick Delaunay537581d2019-02-04 11:26:19 +0100100 if (CONFIG_IS_ENABLED(SYSRESET))
101 return device_bind_driver(dev, "stpmic1-sysreset",
102 "stpmic1-sysreset", NULL);
103
Patrice Chotard0de05412018-04-26 17:13:10 +0200104 return 0;
105}
106
Patrick Delaunayd79218f2019-02-04 11:26:17 +0100107static struct dm_pmic_ops stpmic1_ops = {
108 .reg_count = stpmic1_reg_count,
109 .read = stpmic1_read,
110 .write = stpmic1_write,
Patrick Delaunay3cba4512018-03-12 10:46:12 +0100111};
112
Patrick Delaunayd79218f2019-02-04 11:26:17 +0100113static const struct udevice_id stpmic1_ids[] = {
114 { .compatible = "st,stpmic1" },
Patrick Delaunay3cba4512018-03-12 10:46:12 +0100115 { }
116};
117
Patrick Delaunayd79218f2019-02-04 11:26:17 +0100118U_BOOT_DRIVER(pmic_stpmic1) = {
119 .name = "stpmic1_pmic",
Patrick Delaunay3cba4512018-03-12 10:46:12 +0100120 .id = UCLASS_PMIC,
Patrick Delaunayd79218f2019-02-04 11:26:17 +0100121 .of_match = stpmic1_ids,
122 .bind = stpmic1_bind,
123 .ops = &stpmic1_ops,
Patrick Delaunay3cba4512018-03-12 10:46:12 +0100124};
Patrick Delaunay537581d2019-02-04 11:26:19 +0100125
Patrick Delaunayd592d132019-02-04 11:26:22 +0100126#ifndef CONFIG_SPL_BUILD
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200127static int stpmic1_nvm_rw(struct udevice *dev, u8 addr, u8 *buf, int buf_len,
128 enum pmic_nvm_op op)
Patrick Delaunayd592d132019-02-04 11:26:22 +0100129{
Patrick Delaunayd592d132019-02-04 11:26:22 +0100130 unsigned long timeout;
131 u8 cmd = STPMIC1_NVM_CMD_READ;
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200132 int ret, len = buf_len;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100133
134 if (addr < STPMIC1_NVM_START_ADDRESS)
135 return -EACCES;
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200136 if (addr + buf_len > STPMIC1_NVM_START_ADDRESS + STPMIC1_NVM_SIZE)
137 len = STPMIC1_NVM_START_ADDRESS + STPMIC1_NVM_SIZE - addr;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100138
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200139 if (op == SHADOW_READ) {
140 ret = pmic_read(dev, addr, buf, len);
141 if (ret < 0)
142 return ret;
143 else
144 return len;
145 }
Patrick Delaunayd592d132019-02-04 11:26:22 +0100146
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200147 if (op == SHADOW_WRITE) {
148 ret = pmic_write(dev, addr, buf, len);
149 if (ret < 0)
150 return ret;
151 else
152 return len;
153 }
Patrick Delaunayd592d132019-02-04 11:26:22 +0100154
155 if (op == NVM_WRITE) {
156 cmd = STPMIC1_NVM_CMD_PROGRAM;
157
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200158 ret = pmic_write(dev, addr, buf, len);
Patrick Delaunayd592d132019-02-04 11:26:22 +0100159 if (ret < 0)
160 return ret;
161 }
162
163 ret = pmic_reg_read(dev, STPMIC1_NVM_CR);
164 if (ret < 0)
165 return ret;
166
167 ret = pmic_reg_write(dev, STPMIC1_NVM_CR, ret | cmd);
168 if (ret < 0)
169 return ret;
170
171 timeout = timer_get_us() + STPMIC1_NVM_POLL_TIMEOUT;
172 for (;;) {
173 ret = pmic_reg_read(dev, STPMIC1_NVM_SR);
174 if (ret < 0)
175 return ret;
176
177 if (!(ret & STPMIC1_NVM_BUSY))
178 break;
179
180 if (time_after(timer_get_us(), timeout))
181 break;
182 }
183
184 if (ret & STPMIC1_NVM_BUSY)
185 return -ETIMEDOUT;
186
187 if (op == NVM_READ) {
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200188 ret = pmic_read(dev, addr, buf, len);
Patrick Delaunayd592d132019-02-04 11:26:22 +0100189 if (ret < 0)
190 return ret;
191 }
192
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200193 return len;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100194}
195
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200196static int stpmic1_nvm_read(struct udevice *dev, int offset,
197 void *buf, int size)
Patrick Delaunayd592d132019-02-04 11:26:22 +0100198{
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200199 enum pmic_nvm_op op = NVM_READ;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100200
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200201 if (offset < 0) {
202 op = SHADOW_READ;
203 offset = -offset;
204 }
Patrick Delaunayd592d132019-02-04 11:26:22 +0100205
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200206 return stpmic1_nvm_rw(dev->parent, offset, buf, size, op);
Patrick Delaunayd592d132019-02-04 11:26:22 +0100207}
208
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200209static int stpmic1_nvm_write(struct udevice *dev, int offset,
210 const void *buf, int size)
Patrick Delaunayd592d132019-02-04 11:26:22 +0100211{
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200212 enum pmic_nvm_op op = NVM_WRITE;
Patrick Delaunayd592d132019-02-04 11:26:22 +0100213
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200214 if (offset < 0) {
215 op = SHADOW_WRITE;
216 offset = -offset;
217 }
Patrick Delaunayd592d132019-02-04 11:26:22 +0100218
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200219 return stpmic1_nvm_rw(dev->parent, offset, (void *)buf, size, op);
Patrick Delaunayd592d132019-02-04 11:26:22 +0100220}
221
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200222static const struct misc_ops stpmic1_nvm_ops = {
223 .read = stpmic1_nvm_read,
224 .write = stpmic1_nvm_write,
225};
Patrick Delaunayd592d132019-02-04 11:26:22 +0100226
Patrick Delaunayc9a5d392019-08-02 13:08:03 +0200227U_BOOT_DRIVER(stpmic1_nvm) = {
228 .name = "stpmic1-nvm",
229 .id = UCLASS_MISC,
230 .ops = &stpmic1_nvm_ops,
231};
Patrick Delaunayd592d132019-02-04 11:26:22 +0100232#endif /* CONFIG_SPL_BUILD */
233
Patrick Delaunay537581d2019-02-04 11:26:19 +0100234#ifdef CONFIG_SYSRESET
235static int stpmic1_sysreset_request(struct udevice *dev, enum sysreset_t type)
236{
Patrick Delaunay8844c4a2019-08-02 13:08:04 +0200237 struct udevice *pmic_dev = dev->parent;
Patrick Delaunay537581d2019-02-04 11:26:19 +0100238 int ret;
239
Patrick Delaunayf6d02202019-05-20 09:47:07 +0200240 if (type != SYSRESET_POWER && type != SYSRESET_POWER_OFF)
Patrick Delaunay537581d2019-02-04 11:26:19 +0100241 return -EPROTONOSUPPORT;
242
Patrick Delaunay537581d2019-02-04 11:26:19 +0100243 ret = pmic_reg_read(pmic_dev, STPMIC1_MAIN_CR);
244 if (ret < 0)
245 return ret;
246
Patrick Delaunayf6d02202019-05-20 09:47:07 +0200247 ret |= STPMIC1_SWOFF;
248 ret &= ~STPMIC1_RREQ_EN;
249 /* request Power Cycle */
250 if (type == SYSRESET_POWER)
251 ret |= STPMIC1_RREQ_EN;
252
253 ret = pmic_reg_write(pmic_dev, STPMIC1_MAIN_CR, ret);
Patrick Delaunay537581d2019-02-04 11:26:19 +0100254 if (ret < 0)
255 return ret;
256
257 return -EINPROGRESS;
258}
259
260static struct sysreset_ops stpmic1_sysreset_ops = {
261 .request = stpmic1_sysreset_request,
262};
263
264U_BOOT_DRIVER(stpmic1_sysreset) = {
265 .name = "stpmic1-sysreset",
266 .id = UCLASS_SYSRESET,
267 .ops = &stpmic1_sysreset_ops,
268};
269#endif