1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2026 Free Mobile, Vincent Jardin
*
* Shared UCLASS_REGULATOR operations over the PMBus 1.x framework.
* See pmbus_helper.h for the API surface and doc/develop/pmbus.rst
* for the porting guide.
*
* No code in this file may reference a specific chip family or
* board. Chip specific quirks (vendor registers, VID coercion,
* ADDR pin auto promotion, byte reversed MFR strings, etc.) belong
* in the per chip driver under drivers/power/regulator/<chip>.c.
*/
#include <dm.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <i2c.h>
#include <log.h>
#include <pmbus.h>
#include <vsprintf.h>
#include <linux/types.h>
#include <power/regulator.h>
#include "pmbus_helper.h"
static int pmbus_regulator_select_page(struct pmbus_regulator_priv *priv)
{
u8 p;
if (priv->page <= 0)
return 0;
p = (u8)priv->page;
return dm_i2c_write(priv->i2c_dev, PMBUS_PAGE, &p, 1);
}
static int pmbus_regulator_get_value(struct udevice *dev)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
u8 vout_mode = 0;
u16 raw = 0;
s64 uv;
int ret;
ret = pmbus_regulator_select_page(priv);
if (ret)
return ret;
pmbus_read_byte(priv->i2c_dev, PMBUS_VOUT_MODE, &vout_mode);
if (pmbus_read_word(priv->i2c_dev, PMBUS_READ_VOUT, &raw))
return -EIO;
if (priv->info)
uv = pmbus_reg2data(priv->info, PSC_VOLTAGE_OUT, raw, vout_mode);
else
uv = pmbus_reg2data_linear16(raw, vout_mode);
if (uv > INT_MAX)
uv = INT_MAX;
if (uv < INT_MIN)
uv = INT_MIN;
return (int)uv;
}
static int pmbus_regulator_set_value(struct udevice *dev, int uV)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
u8 vout_mode = 0;
u8 buf[2];
u16 raw;
int ret;
ret = pmbus_regulator_select_page(priv);
if (ret)
return ret;
if (pmbus_read_byte(priv->i2c_dev, PMBUS_VOUT_MODE, &vout_mode))
return -EIO;
/*
* Dispatch on the chip's VOUT_MODE selector. LINEAR16 and DIRECT
* are wired today; VID and IEEE754 return -ENOSYS until their
* encoders land. For DIRECT, the m / b / R triple comes from the
* chip's pmbus_driver_info[PSC_VOLTAGE_OUT]; if the per chip
* driver did not populate them, the encoder cannot run.
*/
switch (vout_mode & PB_VOUT_MODE_MODE_MASK) {
case PB_VOUT_MODE_LINEAR:
raw = pmbus_data2reg_linear16((s64)uV, vout_mode);
break;
case PB_VOUT_MODE_DIRECT:
if (!priv->info ||
priv->info->format[PSC_VOLTAGE_OUT] != pmbus_fmt_direct)
return -ENODATA;
raw = pmbus_data2reg_direct((s64)uV,
priv->info->m[PSC_VOLTAGE_OUT],
priv->info->b[PSC_VOLTAGE_OUT],
priv->info->R[PSC_VOLTAGE_OUT]);
break;
default:
return -ENOSYS;
}
buf[0] = (u8)(raw & 0xff);
buf[1] = (u8)((raw >> 8) & 0xff);
return dm_i2c_write(priv->i2c_dev, PMBUS_VOUT_COMMAND, buf, 2);
}
static int pmbus_regulator_get_enable(struct udevice *dev)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
u8 op = 0;
int ret;
ret = pmbus_regulator_select_page(priv);
if (ret)
return ret;
if (pmbus_read_byte(priv->i2c_dev, PMBUS_OPERATION, &op))
return -EIO;
return (op & PB_OPERATION_ON) ? 1 : 0;
}
static int pmbus_regulator_set_enable(struct udevice *dev, bool enable)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
u8 op = 0;
int ret;
ret = pmbus_regulator_select_page(priv);
if (ret)
return ret;
if (pmbus_read_byte(priv->i2c_dev, PMBUS_OPERATION, &op))
return -EIO;
if (enable)
op |= PB_OPERATION_ON;
else
op &= (u8)~PB_OPERATION_ON;
return dm_i2c_write(priv->i2c_dev, PMBUS_OPERATION, &op, 1);
}
const struct dm_regulator_ops pmbus_regulator_ops = {
.get_value = pmbus_regulator_get_value,
.set_value = pmbus_regulator_set_value,
.get_enable = pmbus_regulator_get_enable,
.set_enable = pmbus_regulator_set_enable,
};
int pmbus_regulator_read_temp(struct udevice *reg_dev, int *temp_mc)
{
struct pmbus_regulator_priv *priv;
u16 raw = 0;
s64 udeg;
int ret;
if (!reg_dev || !temp_mc)
return -EINVAL;
priv = dev_get_priv(reg_dev);
if (!priv || !priv->i2c_dev)
return -ENODEV;
ret = pmbus_regulator_select_page(priv);
if (ret)
return ret;
if (pmbus_read_word(priv->i2c_dev, PMBUS_READ_TEMPERATURE_1, &raw))
return -EIO;
/*
* vout_mode is meaningless for the temperature class. With a
* chip info record the dispatcher honours its per-class format
* (DIRECT m/b/R for MPS, LINEAR11 for spec-compliant parts);
* without one, fall back to the PMBus 1.x standard LINEAR11.
*/
if (priv->info)
udeg = pmbus_reg2data(priv->info, PSC_TEMPERATURE, raw, 0);
else
udeg = pmbus_reg2data_linear11(raw);
*temp_mc = (int)(udeg / 1000);
return 0;
}
enum pmbus_data_format
pmbus_regulator_identify_vout(struct udevice *i2c_dev,
struct pmbus_driver_info *info)
{
u8 vout_mode = 0;
if (pmbus_read_byte(i2c_dev, PMBUS_VOUT_MODE, &vout_mode))
return info->format[PSC_VOLTAGE_OUT];
switch (vout_mode & PB_VOUT_MODE_MODE_MASK) {
case PB_VOUT_MODE_LINEAR:
info->format[PSC_VOLTAGE_OUT] = pmbus_fmt_linear;
break;
case PB_VOUT_MODE_VID:
info->format[PSC_VOLTAGE_OUT] = pmbus_fmt_vid;
break;
case PB_VOUT_MODE_DIRECT:
info->format[PSC_VOLTAGE_OUT] = pmbus_fmt_direct;
info->m[PSC_VOLTAGE_OUT] = 1;
info->b[PSC_VOLTAGE_OUT] = 0;
info->R[PSC_VOLTAGE_OUT] = 0;
break;
case PB_VOUT_MODE_IEEE754:
info->format[PSC_VOLTAGE_OUT] = pmbus_fmt_ieee754;
break;
default:
break;
}
return info->format[PSC_VOLTAGE_OUT];
}
const struct pmbus_driver_info *pmbus_regulator_info_by_addr(int bus_seq,
u8 addr)
{
struct uclass *uc;
struct udevice *r;
if (uclass_get(UCLASS_REGULATOR, &uc))
return NULL;
uclass_foreach_dev(r, uc) {
struct udevice *parent = dev_get_parent(r);
struct pmbus_regulator_priv *priv;
int ra;
if (!parent || device_get_uclass_id(parent) != UCLASS_I2C)
continue;
if (dev_seq(parent) != bus_seq)
continue;
ra = dev_read_addr(r);
if (ra < 0 || (u8)ra != addr)
continue;
/*
* Address matches. Only chips driven through this helper
* carry a pmbus_regulator_priv at the head of their priv;
* identify them by their shared ops vector so we never
* misread a foreign regulator's private layout.
*/
if (!r->driver || r->driver->ops != &pmbus_regulator_ops)
return NULL;
if (device_probe(r))
return NULL;
priv = dev_get_priv(r);
return priv ? priv->info : NULL;
}
return NULL;
}
/*
* Spawn the generic UCLASS_THERMAL companion (drivers/thermal/
* pmbus_thermal.c) as a child of this regulator so READ_TEMPERATURE_1
* is reachable through the standard `temperature list` / `temperature
* get` interface. Named "<regulator-name>-temp" so several PMBus rails
* on one board produce distinct, descriptive device names. Failure is
* non-fatal: the chip still works as a UCLASS_REGULATOR.
*/
static void pmbus_regulator_bind_thermal(struct udevice *dev)
{
struct udevice *therm;
const char *rname;
char name[48];
if (!IS_ENABLED(CONFIG_PMBUS_THERMAL))
return;
if (device_bind_driver(dev, "pmbus_thermal", "pmbus-temp", &therm))
return;
rname = dev_read_string(dev, "regulator-name");
snprintf(name, sizeof(name), "%s-temp", rname ? rname : dev->name);
device_set_name(therm, name);
}
int pmbus_regulator_probe_common(struct udevice *dev,
const struct pmbus_driver_info *info,
int page)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
int chip_addr;
int ret;
chip_addr = dev_read_addr(dev);
if (chip_addr < 0)
return -EINVAL;
ret = i2c_get_chip(dev_get_parent(dev), (u32)chip_addr, 1, &priv->i2c_dev);
if (ret)
return ret;
priv->info = info;
priv->page = page;
if (page > 0) {
u8 p = (u8)page;
ret = dm_i2c_write(priv->i2c_dev, PMBUS_PAGE, &p, 1);
if (ret)
return ret;
}
pmbus_regulator_bind_thermal(dev);
return 0;
}
int pmbus_regulator_apply_voltage_scale(struct udevice *dev,
u32 fb_divider_permille)
{
struct pmbus_regulator_priv *priv = dev_get_priv(dev);
u8 buf[2];
if (fb_divider_permille == 0)
return 0;
buf[0] = (u8)(fb_divider_permille & 0xff);
buf[1] = (u8)((fb_divider_permille >> 8) & 0xff);
return dm_i2c_write(priv->i2c_dev, PMBUS_VOUT_SCALE_LOOP, buf, 2);
}
|