]>
Commit | Line | Data |
---|---|---|
442aba78 GR |
1 | /* |
2 | * Hardware monitoring driver for PMBus devices | |
3 | * | |
4 | * Copyright (c) 2010, 2011 Ericsson AB. | |
aebcbbfc | 5 | * Copyright (c) 2012 Guenter Roeck |
442aba78 GR |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/kernel.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/err.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/i2c.h> | |
28 | #include <linux/hwmon.h> | |
29 | #include <linux/hwmon-sysfs.h> | |
dcd8f392 | 30 | #include <linux/jiffies.h> |
442aba78 | 31 | #include <linux/i2c/pmbus.h> |
ddbb4db4 AT |
32 | #include <linux/regulator/driver.h> |
33 | #include <linux/regulator/machine.h> | |
442aba78 GR |
34 | #include "pmbus.h" |
35 | ||
36 | /* | |
85cfb3a8 GR |
37 | * Number of additional attribute pointers to allocate |
38 | * with each call to krealloc | |
442aba78 | 39 | */ |
85cfb3a8 | 40 | #define PMBUS_ATTR_ALLOC_SIZE 32 |
442aba78 | 41 | |
442aba78 GR |
42 | /* |
43 | * Index into status register array, per status register group | |
44 | */ | |
45 | #define PB_STATUS_BASE 0 | |
46 | #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES) | |
47 | #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES) | |
48 | #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES) | |
49 | #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES) | |
aebcbbfc GR |
50 | #define PB_STATUS_TEMP_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES) |
51 | #define PB_STATUS_INPUT_BASE (PB_STATUS_TEMP_BASE + PMBUS_PAGES) | |
52 | #define PB_STATUS_VMON_BASE (PB_STATUS_INPUT_BASE + 1) | |
53 | ||
54 | #define PB_NUM_STATUS_REG (PB_STATUS_VMON_BASE + 1) | |
442aba78 | 55 | |
2bd05bf4 GR |
56 | #define PMBUS_NAME_SIZE 24 |
57 | ||
442aba78 | 58 | struct pmbus_sensor { |
e1e081a7 | 59 | struct pmbus_sensor *next; |
2bd05bf4 | 60 | char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ |
e1e081a7 | 61 | struct device_attribute attribute; |
442aba78 | 62 | u8 page; /* page number */ |
6f183d33 | 63 | u16 reg; /* register */ |
442aba78 GR |
64 | enum pmbus_sensor_classes class; /* sensor class */ |
65 | bool update; /* runtime sensor update needed */ | |
66 | int data; /* Sensor data. | |
67 | Negative if there was a read error */ | |
68 | }; | |
e1e081a7 GR |
69 | #define to_pmbus_sensor(_attr) \ |
70 | container_of(_attr, struct pmbus_sensor, attribute) | |
442aba78 GR |
71 | |
72 | struct pmbus_boolean { | |
2bd05bf4 | 73 | char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ |
442aba78 | 74 | struct sensor_device_attribute attribute; |
663834f3 GR |
75 | struct pmbus_sensor *s1; |
76 | struct pmbus_sensor *s2; | |
442aba78 | 77 | }; |
663834f3 GR |
78 | #define to_pmbus_boolean(_attr) \ |
79 | container_of(_attr, struct pmbus_boolean, attribute) | |
442aba78 GR |
80 | |
81 | struct pmbus_label { | |
2bd05bf4 | 82 | char name[PMBUS_NAME_SIZE]; /* sysfs label name */ |
0328461e | 83 | struct device_attribute attribute; |
2bd05bf4 | 84 | char label[PMBUS_NAME_SIZE]; /* label */ |
442aba78 | 85 | }; |
0328461e GR |
86 | #define to_pmbus_label(_attr) \ |
87 | container_of(_attr, struct pmbus_label, attribute) | |
442aba78 GR |
88 | |
89 | struct pmbus_data { | |
0328461e | 90 | struct device *dev; |
442aba78 GR |
91 | struct device *hwmon_dev; |
92 | ||
93 | u32 flags; /* from platform data */ | |
94 | ||
daa436e6 GR |
95 | int exponent[PMBUS_PAGES]; |
96 | /* linear mode: exponent for output voltages */ | |
442aba78 GR |
97 | |
98 | const struct pmbus_driver_info *info; | |
99 | ||
100 | int max_attributes; | |
101 | int num_attributes; | |
442aba78 | 102 | struct attribute_group group; |
c3b7cddc | 103 | const struct attribute_group *groups[2]; |
442aba78 | 104 | |
442aba78 | 105 | struct pmbus_sensor *sensors; |
442aba78 GR |
106 | |
107 | struct mutex update_lock; | |
108 | bool valid; | |
109 | unsigned long last_updated; /* in jiffies */ | |
110 | ||
111 | /* | |
112 | * A single status register covers multiple attributes, | |
113 | * so we keep them all together. | |
114 | */ | |
442aba78 | 115 | u8 status[PB_NUM_STATUS_REG]; |
16c6d01f | 116 | u8 status_register; |
442aba78 GR |
117 | |
118 | u8 currpage; | |
119 | }; | |
120 | ||
ce603b18 GR |
121 | void pmbus_clear_cache(struct i2c_client *client) |
122 | { | |
123 | struct pmbus_data *data = i2c_get_clientdata(client); | |
124 | ||
125 | data->valid = false; | |
126 | } | |
127 | EXPORT_SYMBOL_GPL(pmbus_clear_cache); | |
128 | ||
442aba78 GR |
129 | int pmbus_set_page(struct i2c_client *client, u8 page) |
130 | { | |
131 | struct pmbus_data *data = i2c_get_clientdata(client); | |
132 | int rv = 0; | |
133 | int newpage; | |
134 | ||
135 | if (page != data->currpage) { | |
136 | rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); | |
137 | newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE); | |
138 | if (newpage != page) | |
179144a0 | 139 | rv = -EIO; |
442aba78 GR |
140 | else |
141 | data->currpage = page; | |
142 | } | |
143 | return rv; | |
144 | } | |
145 | EXPORT_SYMBOL_GPL(pmbus_set_page); | |
146 | ||
03e9bd8d | 147 | int pmbus_write_byte(struct i2c_client *client, int page, u8 value) |
442aba78 GR |
148 | { |
149 | int rv; | |
150 | ||
9c1ed894 GR |
151 | if (page >= 0) { |
152 | rv = pmbus_set_page(client, page); | |
153 | if (rv < 0) | |
154 | return rv; | |
155 | } | |
442aba78 GR |
156 | |
157 | return i2c_smbus_write_byte(client, value); | |
158 | } | |
03e9bd8d | 159 | EXPORT_SYMBOL_GPL(pmbus_write_byte); |
442aba78 | 160 | |
044cd3a5 GR |
161 | /* |
162 | * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if | |
84fb029f | 163 | * a device specific mapping function exists and calls it if necessary. |
044cd3a5 GR |
164 | */ |
165 | static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) | |
166 | { | |
167 | struct pmbus_data *data = i2c_get_clientdata(client); | |
168 | const struct pmbus_driver_info *info = data->info; | |
169 | int status; | |
170 | ||
171 | if (info->write_byte) { | |
172 | status = info->write_byte(client, page, value); | |
173 | if (status != -ENODATA) | |
174 | return status; | |
175 | } | |
176 | return pmbus_write_byte(client, page, value); | |
177 | } | |
178 | ||
46243f3a | 179 | int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word) |
442aba78 GR |
180 | { |
181 | int rv; | |
182 | ||
183 | rv = pmbus_set_page(client, page); | |
184 | if (rv < 0) | |
185 | return rv; | |
186 | ||
187 | return i2c_smbus_write_word_data(client, reg, word); | |
188 | } | |
46243f3a GR |
189 | EXPORT_SYMBOL_GPL(pmbus_write_word_data); |
190 | ||
191 | /* | |
192 | * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if | |
193 | * a device specific mapping function exists and calls it if necessary. | |
194 | */ | |
195 | static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, | |
196 | u16 word) | |
197 | { | |
198 | struct pmbus_data *data = i2c_get_clientdata(client); | |
199 | const struct pmbus_driver_info *info = data->info; | |
200 | int status; | |
201 | ||
202 | if (info->write_word_data) { | |
203 | status = info->write_word_data(client, page, reg, word); | |
204 | if (status != -ENODATA) | |
205 | return status; | |
206 | } | |
207 | if (reg >= PMBUS_VIRT_BASE) | |
179144a0 | 208 | return -ENXIO; |
46243f3a GR |
209 | return pmbus_write_word_data(client, page, reg, word); |
210 | } | |
442aba78 GR |
211 | |
212 | int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg) | |
213 | { | |
214 | int rv; | |
215 | ||
216 | rv = pmbus_set_page(client, page); | |
217 | if (rv < 0) | |
218 | return rv; | |
219 | ||
220 | return i2c_smbus_read_word_data(client, reg); | |
221 | } | |
222 | EXPORT_SYMBOL_GPL(pmbus_read_word_data); | |
223 | ||
46243f3a GR |
224 | /* |
225 | * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if | |
226 | * a device specific mapping function exists and calls it if necessary. | |
227 | */ | |
228 | static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg) | |
229 | { | |
230 | struct pmbus_data *data = i2c_get_clientdata(client); | |
231 | const struct pmbus_driver_info *info = data->info; | |
232 | int status; | |
233 | ||
234 | if (info->read_word_data) { | |
235 | status = info->read_word_data(client, page, reg); | |
236 | if (status != -ENODATA) | |
237 | return status; | |
238 | } | |
6f183d33 | 239 | if (reg >= PMBUS_VIRT_BASE) |
179144a0 | 240 | return -ENXIO; |
46243f3a GR |
241 | return pmbus_read_word_data(client, page, reg); |
242 | } | |
243 | ||
9c1ed894 | 244 | int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) |
442aba78 GR |
245 | { |
246 | int rv; | |
247 | ||
9c1ed894 GR |
248 | if (page >= 0) { |
249 | rv = pmbus_set_page(client, page); | |
250 | if (rv < 0) | |
251 | return rv; | |
252 | } | |
442aba78 GR |
253 | |
254 | return i2c_smbus_read_byte_data(client, reg); | |
255 | } | |
1061d851 | 256 | EXPORT_SYMBOL_GPL(pmbus_read_byte_data); |
442aba78 | 257 | |
11c11998 AT |
258 | int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) |
259 | { | |
260 | int rv; | |
261 | ||
262 | rv = pmbus_set_page(client, page); | |
263 | if (rv < 0) | |
264 | return rv; | |
265 | ||
266 | return i2c_smbus_write_byte_data(client, reg, value); | |
267 | } | |
268 | EXPORT_SYMBOL_GPL(pmbus_write_byte_data); | |
269 | ||
270 | int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg, | |
271 | u8 mask, u8 value) | |
272 | { | |
273 | unsigned int tmp; | |
274 | int rv; | |
275 | ||
276 | rv = pmbus_read_byte_data(client, page, reg); | |
277 | if (rv < 0) | |
278 | return rv; | |
279 | ||
280 | tmp = (rv & ~mask) | (value & mask); | |
281 | ||
282 | if (tmp != rv) | |
283 | rv = pmbus_write_byte_data(client, page, reg, tmp); | |
284 | ||
285 | return rv; | |
286 | } | |
287 | EXPORT_SYMBOL_GPL(pmbus_update_byte_data); | |
288 | ||
c6bfb767 GR |
289 | /* |
290 | * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if | |
291 | * a device specific mapping function exists and calls it if necessary. | |
292 | */ | |
293 | static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) | |
294 | { | |
295 | struct pmbus_data *data = i2c_get_clientdata(client); | |
296 | const struct pmbus_driver_info *info = data->info; | |
297 | int status; | |
298 | ||
299 | if (info->read_byte_data) { | |
300 | status = info->read_byte_data(client, page, reg); | |
301 | if (status != -ENODATA) | |
302 | return status; | |
303 | } | |
304 | return pmbus_read_byte_data(client, page, reg); | |
305 | } | |
306 | ||
442aba78 GR |
307 | static void pmbus_clear_fault_page(struct i2c_client *client, int page) |
308 | { | |
044cd3a5 | 309 | _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); |
442aba78 GR |
310 | } |
311 | ||
312 | void pmbus_clear_faults(struct i2c_client *client) | |
313 | { | |
314 | struct pmbus_data *data = i2c_get_clientdata(client); | |
315 | int i; | |
316 | ||
317 | for (i = 0; i < data->info->pages; i++) | |
318 | pmbus_clear_fault_page(client, i); | |
319 | } | |
320 | EXPORT_SYMBOL_GPL(pmbus_clear_faults); | |
321 | ||
9c1ed894 | 322 | static int pmbus_check_status_cml(struct i2c_client *client) |
442aba78 | 323 | { |
16c6d01f | 324 | struct pmbus_data *data = i2c_get_clientdata(client); |
442aba78 GR |
325 | int status, status2; |
326 | ||
16c6d01f | 327 | status = _pmbus_read_byte_data(client, -1, data->status_register); |
442aba78 | 328 | if (status < 0 || (status & PB_STATUS_CML)) { |
da8e48ab | 329 | status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); |
442aba78 | 330 | if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) |
179144a0 | 331 | return -EIO; |
442aba78 GR |
332 | } |
333 | return 0; | |
334 | } | |
335 | ||
f880b12c GR |
336 | static bool pmbus_check_register(struct i2c_client *client, |
337 | int (*func)(struct i2c_client *client, | |
338 | int page, int reg), | |
339 | int page, int reg) | |
442aba78 GR |
340 | { |
341 | int rv; | |
342 | struct pmbus_data *data = i2c_get_clientdata(client); | |
343 | ||
f880b12c | 344 | rv = func(client, page, reg); |
442aba78 | 345 | if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) |
9c1ed894 GR |
346 | rv = pmbus_check_status_cml(client); |
347 | pmbus_clear_fault_page(client, -1); | |
442aba78 GR |
348 | return rv >= 0; |
349 | } | |
f880b12c GR |
350 | |
351 | bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) | |
352 | { | |
353 | return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); | |
354 | } | |
442aba78 GR |
355 | EXPORT_SYMBOL_GPL(pmbus_check_byte_register); |
356 | ||
357 | bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) | |
358 | { | |
f880b12c | 359 | return pmbus_check_register(client, _pmbus_read_word_data, page, reg); |
442aba78 GR |
360 | } |
361 | EXPORT_SYMBOL_GPL(pmbus_check_word_register); | |
362 | ||
363 | const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) | |
364 | { | |
365 | struct pmbus_data *data = i2c_get_clientdata(client); | |
366 | ||
367 | return data->info; | |
368 | } | |
369 | EXPORT_SYMBOL_GPL(pmbus_get_driver_info); | |
370 | ||
f880b12c GR |
371 | static struct _pmbus_status { |
372 | u32 func; | |
373 | u16 base; | |
374 | u16 reg; | |
375 | } pmbus_status[] = { | |
376 | { PMBUS_HAVE_STATUS_VOUT, PB_STATUS_VOUT_BASE, PMBUS_STATUS_VOUT }, | |
377 | { PMBUS_HAVE_STATUS_IOUT, PB_STATUS_IOUT_BASE, PMBUS_STATUS_IOUT }, | |
378 | { PMBUS_HAVE_STATUS_TEMP, PB_STATUS_TEMP_BASE, | |
379 | PMBUS_STATUS_TEMPERATURE }, | |
380 | { PMBUS_HAVE_STATUS_FAN12, PB_STATUS_FAN_BASE, PMBUS_STATUS_FAN_12 }, | |
381 | { PMBUS_HAVE_STATUS_FAN34, PB_STATUS_FAN34_BASE, PMBUS_STATUS_FAN_34 }, | |
382 | }; | |
383 | ||
442aba78 GR |
384 | static struct pmbus_data *pmbus_update_device(struct device *dev) |
385 | { | |
c3b7cddc | 386 | struct i2c_client *client = to_i2c_client(dev->parent); |
442aba78 GR |
387 | struct pmbus_data *data = i2c_get_clientdata(client); |
388 | const struct pmbus_driver_info *info = data->info; | |
e1e081a7 | 389 | struct pmbus_sensor *sensor; |
442aba78 GR |
390 | |
391 | mutex_lock(&data->update_lock); | |
392 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | |
f880b12c | 393 | int i, j; |
442aba78 | 394 | |
f880b12c | 395 | for (i = 0; i < info->pages; i++) { |
442aba78 | 396 | data->status[PB_STATUS_BASE + i] |
da8e48ab | 397 | = _pmbus_read_byte_data(client, i, |
16c6d01f | 398 | data->status_register); |
f880b12c GR |
399 | for (j = 0; j < ARRAY_SIZE(pmbus_status); j++) { |
400 | struct _pmbus_status *s = &pmbus_status[j]; | |
401 | ||
402 | if (!(info->func[i] & s->func)) | |
403 | continue; | |
404 | data->status[s->base + i] | |
405 | = _pmbus_read_byte_data(client, i, | |
406 | s->reg); | |
407 | } | |
954df676 GR |
408 | } |
409 | ||
442aba78 GR |
410 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) |
411 | data->status[PB_STATUS_INPUT_BASE] | |
2cfa6aed GR |
412 | = _pmbus_read_byte_data(client, 0, |
413 | PMBUS_STATUS_INPUT); | |
442aba78 | 414 | |
aebcbbfc GR |
415 | if (info->func[0] & PMBUS_HAVE_STATUS_VMON) |
416 | data->status[PB_STATUS_VMON_BASE] | |
417 | = _pmbus_read_byte_data(client, 0, | |
418 | PMBUS_VIRT_STATUS_VMON); | |
419 | ||
e1e081a7 | 420 | for (sensor = data->sensors; sensor; sensor = sensor->next) { |
442aba78 GR |
421 | if (!data->valid || sensor->update) |
422 | sensor->data | |
46243f3a GR |
423 | = _pmbus_read_word_data(client, |
424 | sensor->page, | |
425 | sensor->reg); | |
442aba78 GR |
426 | } |
427 | pmbus_clear_faults(client); | |
428 | data->last_updated = jiffies; | |
429 | data->valid = 1; | |
430 | } | |
431 | mutex_unlock(&data->update_lock); | |
432 | return data; | |
433 | } | |
434 | ||
435 | /* | |
436 | * Convert linear sensor values to milli- or micro-units | |
437 | * depending on sensor type. | |
438 | */ | |
f450c150 GR |
439 | static long pmbus_reg2data_linear(struct pmbus_data *data, |
440 | struct pmbus_sensor *sensor) | |
442aba78 | 441 | { |
9f6ad1ce GR |
442 | s16 exponent; |
443 | s32 mantissa; | |
442aba78 GR |
444 | long val; |
445 | ||
9f6ad1ce | 446 | if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ |
daa436e6 | 447 | exponent = data->exponent[sensor->page]; |
9f6ad1ce GR |
448 | mantissa = (u16) sensor->data; |
449 | } else { /* LINEAR11 */ | |
1af1f531 GR |
450 | exponent = ((s16)sensor->data) >> 11; |
451 | mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; | |
442aba78 GR |
452 | } |
453 | ||
454 | val = mantissa; | |
455 | ||
456 | /* scale result to milli-units for all sensors except fans */ | |
457 | if (sensor->class != PSC_FAN) | |
458 | val = val * 1000L; | |
459 | ||
460 | /* scale result to micro-units for power sensors */ | |
461 | if (sensor->class == PSC_POWER) | |
462 | val = val * 1000L; | |
463 | ||
464 | if (exponent >= 0) | |
465 | val <<= exponent; | |
466 | else | |
467 | val >>= -exponent; | |
468 | ||
f450c150 | 469 | return val; |
442aba78 GR |
470 | } |
471 | ||
472 | /* | |
473 | * Convert direct sensor values to milli- or micro-units | |
474 | * depending on sensor type. | |
475 | */ | |
f450c150 GR |
476 | static long pmbus_reg2data_direct(struct pmbus_data *data, |
477 | struct pmbus_sensor *sensor) | |
442aba78 GR |
478 | { |
479 | long val = (s16) sensor->data; | |
480 | long m, b, R; | |
481 | ||
482 | m = data->info->m[sensor->class]; | |
483 | b = data->info->b[sensor->class]; | |
484 | R = data->info->R[sensor->class]; | |
485 | ||
486 | if (m == 0) | |
487 | return 0; | |
488 | ||
489 | /* X = 1/m * (Y * 10^-R - b) */ | |
490 | R = -R; | |
491 | /* scale result to milli-units for everything but fans */ | |
492 | if (sensor->class != PSC_FAN) { | |
493 | R += 3; | |
494 | b *= 1000; | |
495 | } | |
496 | ||
497 | /* scale result to micro-units for power sensors */ | |
498 | if (sensor->class == PSC_POWER) { | |
499 | R += 3; | |
500 | b *= 1000; | |
501 | } | |
502 | ||
503 | while (R > 0) { | |
504 | val *= 10; | |
505 | R--; | |
506 | } | |
507 | while (R < 0) { | |
508 | val = DIV_ROUND_CLOSEST(val, 10); | |
509 | R++; | |
510 | } | |
511 | ||
f450c150 | 512 | return (val - b) / m; |
442aba78 GR |
513 | } |
514 | ||
1061d851 GR |
515 | /* |
516 | * Convert VID sensor values to milli- or micro-units | |
517 | * depending on sensor type. | |
1061d851 GR |
518 | */ |
519 | static long pmbus_reg2data_vid(struct pmbus_data *data, | |
520 | struct pmbus_sensor *sensor) | |
521 | { | |
522 | long val = sensor->data; | |
068c2270 GR |
523 | long rv = 0; |
524 | ||
525 | switch (data->info->vrm_version) { | |
526 | case vr11: | |
527 | if (val >= 0x02 && val <= 0xb2) | |
528 | rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); | |
529 | break; | |
530 | case vr12: | |
531 | if (val >= 0x01) | |
532 | rv = 250 + (val - 1) * 5; | |
533 | break; | |
534 | } | |
535 | return rv; | |
1061d851 GR |
536 | } |
537 | ||
f450c150 | 538 | static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) |
442aba78 | 539 | { |
f450c150 | 540 | long val; |
442aba78 | 541 | |
1061d851 GR |
542 | switch (data->info->format[sensor->class]) { |
543 | case direct: | |
442aba78 | 544 | val = pmbus_reg2data_direct(data, sensor); |
1061d851 GR |
545 | break; |
546 | case vid: | |
547 | val = pmbus_reg2data_vid(data, sensor); | |
548 | break; | |
549 | case linear: | |
550 | default: | |
442aba78 | 551 | val = pmbus_reg2data_linear(data, sensor); |
1061d851 GR |
552 | break; |
553 | } | |
442aba78 GR |
554 | return val; |
555 | } | |
556 | ||
557 | #define MAX_MANTISSA (1023 * 1000) | |
558 | #define MIN_MANTISSA (511 * 1000) | |
559 | ||
560 | static u16 pmbus_data2reg_linear(struct pmbus_data *data, | |
daa436e6 | 561 | struct pmbus_sensor *sensor, long val) |
442aba78 | 562 | { |
9f6ad1ce | 563 | s16 exponent = 0, mantissa; |
442aba78 GR |
564 | bool negative = false; |
565 | ||
566 | /* simple case */ | |
567 | if (val == 0) | |
568 | return 0; | |
569 | ||
daa436e6 | 570 | if (sensor->class == PSC_VOLTAGE_OUT) { |
9f6ad1ce GR |
571 | /* LINEAR16 does not support negative voltages */ |
572 | if (val < 0) | |
573 | return 0; | |
574 | ||
442aba78 GR |
575 | /* |
576 | * For a static exponents, we don't have a choice | |
577 | * but to adjust the value to it. | |
578 | */ | |
daa436e6 GR |
579 | if (data->exponent[sensor->page] < 0) |
580 | val <<= -data->exponent[sensor->page]; | |
442aba78 | 581 | else |
daa436e6 | 582 | val >>= data->exponent[sensor->page]; |
442aba78 | 583 | val = DIV_ROUND_CLOSEST(val, 1000); |
9f6ad1ce GR |
584 | return val & 0xffff; |
585 | } | |
586 | ||
587 | if (val < 0) { | |
588 | negative = true; | |
589 | val = -val; | |
442aba78 GR |
590 | } |
591 | ||
592 | /* Power is in uW. Convert to mW before converting. */ | |
daa436e6 | 593 | if (sensor->class == PSC_POWER) |
442aba78 GR |
594 | val = DIV_ROUND_CLOSEST(val, 1000L); |
595 | ||
596 | /* | |
597 | * For simplicity, convert fan data to milli-units | |
598 | * before calculating the exponent. | |
599 | */ | |
daa436e6 | 600 | if (sensor->class == PSC_FAN) |
442aba78 GR |
601 | val = val * 1000; |
602 | ||
603 | /* Reduce large mantissa until it fits into 10 bit */ | |
604 | while (val >= MAX_MANTISSA && exponent < 15) { | |
605 | exponent++; | |
606 | val >>= 1; | |
607 | } | |
608 | /* Increase small mantissa to improve precision */ | |
609 | while (val < MIN_MANTISSA && exponent > -15) { | |
610 | exponent--; | |
611 | val <<= 1; | |
612 | } | |
613 | ||
614 | /* Convert mantissa from milli-units to units */ | |
615 | mantissa = DIV_ROUND_CLOSEST(val, 1000); | |
616 | ||
617 | /* Ensure that resulting number is within range */ | |
618 | if (mantissa > 0x3ff) | |
619 | mantissa = 0x3ff; | |
620 | ||
621 | /* restore sign */ | |
622 | if (negative) | |
623 | mantissa = -mantissa; | |
624 | ||
625 | /* Convert to 5 bit exponent, 11 bit mantissa */ | |
626 | return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); | |
627 | } | |
628 | ||
629 | static u16 pmbus_data2reg_direct(struct pmbus_data *data, | |
daa436e6 | 630 | struct pmbus_sensor *sensor, long val) |
442aba78 GR |
631 | { |
632 | long m, b, R; | |
633 | ||
daa436e6 GR |
634 | m = data->info->m[sensor->class]; |
635 | b = data->info->b[sensor->class]; | |
636 | R = data->info->R[sensor->class]; | |
442aba78 GR |
637 | |
638 | /* Power is in uW. Adjust R and b. */ | |
daa436e6 | 639 | if (sensor->class == PSC_POWER) { |
442aba78 GR |
640 | R -= 3; |
641 | b *= 1000; | |
642 | } | |
643 | ||
644 | /* Calculate Y = (m * X + b) * 10^R */ | |
daa436e6 | 645 | if (sensor->class != PSC_FAN) { |
442aba78 GR |
646 | R -= 3; /* Adjust R and b for data in milli-units */ |
647 | b *= 1000; | |
648 | } | |
649 | val = val * m + b; | |
650 | ||
651 | while (R > 0) { | |
652 | val *= 10; | |
653 | R--; | |
654 | } | |
655 | while (R < 0) { | |
656 | val = DIV_ROUND_CLOSEST(val, 10); | |
657 | R++; | |
658 | } | |
659 | ||
660 | return val; | |
661 | } | |
662 | ||
1061d851 | 663 | static u16 pmbus_data2reg_vid(struct pmbus_data *data, |
daa436e6 | 664 | struct pmbus_sensor *sensor, long val) |
1061d851 | 665 | { |
2a844c14 | 666 | val = clamp_val(val, 500, 1600); |
1061d851 GR |
667 | |
668 | return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625); | |
669 | } | |
670 | ||
442aba78 | 671 | static u16 pmbus_data2reg(struct pmbus_data *data, |
daa436e6 | 672 | struct pmbus_sensor *sensor, long val) |
442aba78 GR |
673 | { |
674 | u16 regval; | |
675 | ||
daa436e6 | 676 | switch (data->info->format[sensor->class]) { |
1061d851 | 677 | case direct: |
daa436e6 | 678 | regval = pmbus_data2reg_direct(data, sensor, val); |
1061d851 GR |
679 | break; |
680 | case vid: | |
daa436e6 | 681 | regval = pmbus_data2reg_vid(data, sensor, val); |
1061d851 GR |
682 | break; |
683 | case linear: | |
684 | default: | |
daa436e6 | 685 | regval = pmbus_data2reg_linear(data, sensor, val); |
1061d851 GR |
686 | break; |
687 | } | |
442aba78 GR |
688 | return regval; |
689 | } | |
690 | ||
691 | /* | |
692 | * Return boolean calculated from converted data. | |
663834f3 GR |
693 | * <index> defines a status register index and mask. |
694 | * The mask is in the lower 8 bits, the register index is in bits 8..23. | |
442aba78 | 695 | * |
663834f3 GR |
696 | * The associated pmbus_boolean structure contains optional pointers to two |
697 | * sensor attributes. If specified, those attributes are compared against each | |
698 | * other to determine if a limit has been exceeded. | |
442aba78 | 699 | * |
663834f3 GR |
700 | * If the sensor attribute pointers are NULL, the function returns true if |
701 | * (status[reg] & mask) is true. | |
702 | * | |
703 | * If sensor attribute pointers are provided, a comparison against a specified | |
704 | * limit has to be performed to determine the boolean result. | |
442aba78 | 705 | * In this case, the function returns true if v1 >= v2 (where v1 and v2 are |
663834f3 | 706 | * sensor values referenced by sensor attribute pointers s1 and s2). |
442aba78 GR |
707 | * |
708 | * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. | |
709 | * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. | |
710 | * | |
711 | * If a negative value is stored in any of the referenced registers, this value | |
712 | * reflects an error code which will be returned. | |
713 | */ | |
663834f3 GR |
714 | static int pmbus_get_boolean(struct pmbus_data *data, struct pmbus_boolean *b, |
715 | int index) | |
442aba78 | 716 | { |
663834f3 GR |
717 | struct pmbus_sensor *s1 = b->s1; |
718 | struct pmbus_sensor *s2 = b->s2; | |
719 | u16 reg = (index >> 8) & 0xffff; | |
442aba78 | 720 | u8 mask = index & 0xff; |
d7ee1115 | 721 | int ret, status; |
442aba78 GR |
722 | u8 regval; |
723 | ||
724 | status = data->status[reg]; | |
725 | if (status < 0) | |
726 | return status; | |
727 | ||
728 | regval = status & mask; | |
663834f3 | 729 | if (!s1 && !s2) { |
d7ee1115 | 730 | ret = !!regval; |
663834f3 | 731 | } else if (!s1 || !s2) { |
af78fdf4 | 732 | WARN(1, "Bad boolean descriptor %p: s1=%p, s2=%p\n", b, s1, s2); |
663834f3 GR |
733 | return 0; |
734 | } else { | |
f450c150 | 735 | long v1, v2; |
442aba78 | 736 | |
663834f3 GR |
737 | if (s1->data < 0) |
738 | return s1->data; | |
739 | if (s2->data < 0) | |
740 | return s2->data; | |
442aba78 | 741 | |
663834f3 GR |
742 | v1 = pmbus_reg2data(data, s1); |
743 | v2 = pmbus_reg2data(data, s2); | |
d7ee1115 | 744 | ret = !!(regval && v1 >= v2); |
442aba78 | 745 | } |
d7ee1115 | 746 | return ret; |
442aba78 GR |
747 | } |
748 | ||
749 | static ssize_t pmbus_show_boolean(struct device *dev, | |
750 | struct device_attribute *da, char *buf) | |
751 | { | |
752 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | |
663834f3 | 753 | struct pmbus_boolean *boolean = to_pmbus_boolean(attr); |
442aba78 GR |
754 | struct pmbus_data *data = pmbus_update_device(dev); |
755 | int val; | |
442aba78 | 756 | |
663834f3 | 757 | val = pmbus_get_boolean(data, boolean, attr->index); |
d7ee1115 GR |
758 | if (val < 0) |
759 | return val; | |
442aba78 GR |
760 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
761 | } | |
762 | ||
763 | static ssize_t pmbus_show_sensor(struct device *dev, | |
e1e081a7 | 764 | struct device_attribute *devattr, char *buf) |
442aba78 | 765 | { |
442aba78 | 766 | struct pmbus_data *data = pmbus_update_device(dev); |
e1e081a7 | 767 | struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); |
442aba78 | 768 | |
442aba78 GR |
769 | if (sensor->data < 0) |
770 | return sensor->data; | |
771 | ||
f450c150 | 772 | return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor)); |
442aba78 GR |
773 | } |
774 | ||
775 | static ssize_t pmbus_set_sensor(struct device *dev, | |
776 | struct device_attribute *devattr, | |
777 | const char *buf, size_t count) | |
778 | { | |
c3b7cddc | 779 | struct i2c_client *client = to_i2c_client(dev->parent); |
442aba78 | 780 | struct pmbus_data *data = i2c_get_clientdata(client); |
e1e081a7 | 781 | struct pmbus_sensor *sensor = to_pmbus_sensor(devattr); |
442aba78 GR |
782 | ssize_t rv = count; |
783 | long val = 0; | |
784 | int ret; | |
785 | u16 regval; | |
786 | ||
0117c3f2 | 787 | if (kstrtol(buf, 10, &val) < 0) |
442aba78 GR |
788 | return -EINVAL; |
789 | ||
790 | mutex_lock(&data->update_lock); | |
daa436e6 | 791 | regval = pmbus_data2reg(data, sensor, val); |
46243f3a | 792 | ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); |
442aba78 GR |
793 | if (ret < 0) |
794 | rv = ret; | |
795 | else | |
e1e081a7 | 796 | sensor->data = regval; |
442aba78 GR |
797 | mutex_unlock(&data->update_lock); |
798 | return rv; | |
799 | } | |
800 | ||
801 | static ssize_t pmbus_show_label(struct device *dev, | |
802 | struct device_attribute *da, char *buf) | |
803 | { | |
0328461e GR |
804 | struct pmbus_label *label = to_pmbus_label(da); |
805 | ||
806 | return snprintf(buf, PAGE_SIZE, "%s\n", label->label); | |
807 | } | |
442aba78 | 808 | |
85cfb3a8 GR |
809 | static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) |
810 | { | |
811 | if (data->num_attributes >= data->max_attributes - 1) { | |
6975404f DW |
812 | int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; |
813 | void *new_attrs = krealloc(data->group.attrs, | |
814 | new_max_attrs * sizeof(void *), | |
815 | GFP_KERNEL); | |
816 | if (!new_attrs) | |
85cfb3a8 | 817 | return -ENOMEM; |
6975404f DW |
818 | data->group.attrs = new_attrs; |
819 | data->max_attributes = new_max_attrs; | |
85cfb3a8 GR |
820 | } |
821 | ||
822 | data->group.attrs[data->num_attributes++] = attr; | |
823 | data->group.attrs[data->num_attributes] = NULL; | |
824 | return 0; | |
825 | } | |
826 | ||
0328461e GR |
827 | static void pmbus_dev_attr_init(struct device_attribute *dev_attr, |
828 | const char *name, | |
829 | umode_t mode, | |
830 | ssize_t (*show)(struct device *dev, | |
831 | struct device_attribute *attr, | |
832 | char *buf), | |
833 | ssize_t (*store)(struct device *dev, | |
834 | struct device_attribute *attr, | |
835 | const char *buf, size_t count)) | |
836 | { | |
837 | sysfs_attr_init(&dev_attr->attr); | |
838 | dev_attr->attr.name = name; | |
839 | dev_attr->attr.mode = mode; | |
840 | dev_attr->show = show; | |
841 | dev_attr->store = store; | |
442aba78 GR |
842 | } |
843 | ||
973018b1 GR |
844 | static void pmbus_attr_init(struct sensor_device_attribute *a, |
845 | const char *name, | |
846 | umode_t mode, | |
847 | ssize_t (*show)(struct device *dev, | |
848 | struct device_attribute *attr, | |
849 | char *buf), | |
850 | ssize_t (*store)(struct device *dev, | |
851 | struct device_attribute *attr, | |
852 | const char *buf, size_t count), | |
853 | int idx) | |
854 | { | |
0328461e | 855 | pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); |
973018b1 GR |
856 | a->index = idx; |
857 | } | |
442aba78 | 858 | |
0328461e GR |
859 | static int pmbus_add_boolean(struct pmbus_data *data, |
860 | const char *name, const char *type, int seq, | |
663834f3 GR |
861 | struct pmbus_sensor *s1, |
862 | struct pmbus_sensor *s2, | |
863 | u16 reg, u8 mask) | |
442aba78 GR |
864 | { |
865 | struct pmbus_boolean *boolean; | |
973018b1 | 866 | struct sensor_device_attribute *a; |
442aba78 | 867 | |
0328461e GR |
868 | boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); |
869 | if (!boolean) | |
870 | return -ENOMEM; | |
442aba78 | 871 | |
973018b1 | 872 | a = &boolean->attribute; |
442aba78 GR |
873 | |
874 | snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", | |
875 | name, seq, type); | |
663834f3 GR |
876 | boolean->s1 = s1; |
877 | boolean->s2 = s2; | |
973018b1 | 878 | pmbus_attr_init(a, boolean->name, S_IRUGO, pmbus_show_boolean, NULL, |
663834f3 | 879 | (reg << 8) | mask); |
0328461e | 880 | |
85cfb3a8 | 881 | return pmbus_add_attribute(data, &a->dev_attr.attr); |
442aba78 GR |
882 | } |
883 | ||
663834f3 GR |
884 | static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, |
885 | const char *name, const char *type, | |
886 | int seq, int page, int reg, | |
887 | enum pmbus_sensor_classes class, | |
888 | bool update, bool readonly) | |
442aba78 GR |
889 | { |
890 | struct pmbus_sensor *sensor; | |
e1e081a7 | 891 | struct device_attribute *a; |
442aba78 | 892 | |
e1e081a7 GR |
893 | sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); |
894 | if (!sensor) | |
895 | return NULL; | |
973018b1 GR |
896 | a = &sensor->attribute; |
897 | ||
442aba78 GR |
898 | snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", |
899 | name, seq, type); | |
900 | sensor->page = page; | |
901 | sensor->reg = reg; | |
902 | sensor->class = class; | |
903 | sensor->update = update; | |
e1e081a7 GR |
904 | pmbus_dev_attr_init(a, sensor->name, |
905 | readonly ? S_IRUGO : S_IRUGO | S_IWUSR, | |
906 | pmbus_show_sensor, pmbus_set_sensor); | |
973018b1 | 907 | |
85cfb3a8 GR |
908 | if (pmbus_add_attribute(data, &a->attr)) |
909 | return NULL; | |
910 | ||
e1e081a7 GR |
911 | sensor->next = data->sensors; |
912 | data->sensors = sensor; | |
663834f3 GR |
913 | |
914 | return sensor; | |
442aba78 GR |
915 | } |
916 | ||
0328461e GR |
917 | static int pmbus_add_label(struct pmbus_data *data, |
918 | const char *name, int seq, | |
919 | const char *lstring, int index) | |
442aba78 GR |
920 | { |
921 | struct pmbus_label *label; | |
0328461e | 922 | struct device_attribute *a; |
442aba78 | 923 | |
0328461e GR |
924 | label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); |
925 | if (!label) | |
926 | return -ENOMEM; | |
442aba78 | 927 | |
973018b1 GR |
928 | a = &label->attribute; |
929 | ||
442aba78 GR |
930 | snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); |
931 | if (!index) | |
932 | strncpy(label->label, lstring, sizeof(label->label) - 1); | |
933 | else | |
934 | snprintf(label->label, sizeof(label->label), "%s%d", lstring, | |
935 | index); | |
936 | ||
0328461e | 937 | pmbus_dev_attr_init(a, label->name, S_IRUGO, pmbus_show_label, NULL); |
85cfb3a8 | 938 | return pmbus_add_attribute(data, &a->attr); |
442aba78 GR |
939 | } |
940 | ||
941 | /* | |
942 | * Search for attributes. Allocate sensors, booleans, and labels as needed. | |
943 | */ | |
442aba78 | 944 | |
b4ce237b GR |
945 | /* |
946 | * The pmbus_limit_attr structure describes a single limit attribute | |
947 | * and its associated alarm attribute. | |
948 | */ | |
949 | struct pmbus_limit_attr { | |
6f183d33 | 950 | u16 reg; /* Limit register */ |
f880b12c | 951 | u16 sbit; /* Alarm attribute status bit */ |
6f183d33 | 952 | bool update; /* True if register needs updates */ |
40257b95 GR |
953 | bool low; /* True if low limit; for limits with compare |
954 | functions only */ | |
b4ce237b GR |
955 | const char *attr; /* Attribute name */ |
956 | const char *alarm; /* Alarm attribute name */ | |
b4ce237b GR |
957 | }; |
958 | ||
959 | /* | |
960 | * The pmbus_sensor_attr structure describes one sensor attribute. This | |
961 | * description includes a reference to the associated limit attributes. | |
962 | */ | |
963 | struct pmbus_sensor_attr { | |
aebcbbfc | 964 | u16 reg; /* sensor register */ |
f880b12c GR |
965 | u8 gbit; /* generic status bit */ |
966 | u8 nlimit; /* # of limit registers */ | |
b4ce237b GR |
967 | enum pmbus_sensor_classes class;/* sensor class */ |
968 | const char *label; /* sensor label */ | |
969 | bool paged; /* true if paged sensor */ | |
970 | bool update; /* true if update needed */ | |
971 | bool compare; /* true if compare function needed */ | |
972 | u32 func; /* sensor mask */ | |
973 | u32 sfunc; /* sensor status mask */ | |
974 | int sbase; /* status base register */ | |
b4ce237b | 975 | const struct pmbus_limit_attr *limit;/* limit registers */ |
b4ce237b GR |
976 | }; |
977 | ||
978 | /* | |
979 | * Add a set of limit attributes and, if supported, the associated | |
980 | * alarm attributes. | |
0328461e GR |
981 | * returns 0 if no alarm register found, 1 if an alarm register was found, |
982 | * < 0 on errors. | |
b4ce237b | 983 | */ |
0328461e GR |
984 | static int pmbus_add_limit_attrs(struct i2c_client *client, |
985 | struct pmbus_data *data, | |
986 | const struct pmbus_driver_info *info, | |
987 | const char *name, int index, int page, | |
663834f3 | 988 | struct pmbus_sensor *base, |
0328461e | 989 | const struct pmbus_sensor_attr *attr) |
b4ce237b GR |
990 | { |
991 | const struct pmbus_limit_attr *l = attr->limit; | |
992 | int nlimit = attr->nlimit; | |
0328461e | 993 | int have_alarm = 0; |
663834f3 GR |
994 | int i, ret; |
995 | struct pmbus_sensor *curr; | |
b4ce237b GR |
996 | |
997 | for (i = 0; i < nlimit; i++) { | |
998 | if (pmbus_check_word_register(client, page, l->reg)) { | |
663834f3 GR |
999 | curr = pmbus_add_sensor(data, name, l->attr, index, |
1000 | page, l->reg, attr->class, | |
1001 | attr->update || l->update, | |
1002 | false); | |
e1e081a7 GR |
1003 | if (!curr) |
1004 | return -ENOMEM; | |
6f183d33 | 1005 | if (l->sbit && (info->func[page] & attr->sfunc)) { |
663834f3 GR |
1006 | ret = pmbus_add_boolean(data, name, |
1007 | l->alarm, index, | |
1008 | attr->compare ? l->low ? curr : base | |
1009 | : NULL, | |
1010 | attr->compare ? l->low ? base : curr | |
1011 | : NULL, | |
1012 | attr->sbase + page, l->sbit); | |
1013 | if (ret) | |
1014 | return ret; | |
0328461e | 1015 | have_alarm = 1; |
442aba78 GR |
1016 | } |
1017 | } | |
b4ce237b | 1018 | l++; |
442aba78 | 1019 | } |
b4ce237b GR |
1020 | return have_alarm; |
1021 | } | |
442aba78 | 1022 | |
0328461e GR |
1023 | static int pmbus_add_sensor_attrs_one(struct i2c_client *client, |
1024 | struct pmbus_data *data, | |
1025 | const struct pmbus_driver_info *info, | |
1026 | const char *name, | |
1027 | int index, int page, | |
1028 | const struct pmbus_sensor_attr *attr) | |
b4ce237b | 1029 | { |
663834f3 | 1030 | struct pmbus_sensor *base; |
0328461e | 1031 | int ret; |
b4ce237b | 1032 | |
0328461e GR |
1033 | if (attr->label) { |
1034 | ret = pmbus_add_label(data, name, index, attr->label, | |
1035 | attr->paged ? page + 1 : 0); | |
1036 | if (ret) | |
1037 | return ret; | |
1038 | } | |
663834f3 GR |
1039 | base = pmbus_add_sensor(data, name, "input", index, page, attr->reg, |
1040 | attr->class, true, true); | |
e1e081a7 GR |
1041 | if (!base) |
1042 | return -ENOMEM; | |
b4ce237b | 1043 | if (attr->sfunc) { |
0328461e | 1044 | ret = pmbus_add_limit_attrs(client, data, info, name, |
663834f3 | 1045 | index, page, base, attr); |
0328461e GR |
1046 | if (ret < 0) |
1047 | return ret; | |
442aba78 GR |
1048 | /* |
1049 | * Add generic alarm attribute only if there are no individual | |
c6bfb767 GR |
1050 | * alarm attributes, if there is a global alarm bit, and if |
1051 | * the generic status register for this page is accessible. | |
442aba78 | 1052 | */ |
0328461e GR |
1053 | if (!ret && attr->gbit && |
1054 | pmbus_check_byte_register(client, page, | |
16c6d01f | 1055 | data->status_register)) { |
663834f3 GR |
1056 | ret = pmbus_add_boolean(data, name, "alarm", index, |
1057 | NULL, NULL, | |
1058 | PB_STATUS_BASE + page, | |
1059 | attr->gbit); | |
0328461e GR |
1060 | if (ret) |
1061 | return ret; | |
1062 | } | |
442aba78 | 1063 | } |
0328461e | 1064 | return 0; |
b4ce237b | 1065 | } |
442aba78 | 1066 | |
0328461e GR |
1067 | static int pmbus_add_sensor_attrs(struct i2c_client *client, |
1068 | struct pmbus_data *data, | |
1069 | const char *name, | |
1070 | const struct pmbus_sensor_attr *attrs, | |
1071 | int nattrs) | |
b4ce237b GR |
1072 | { |
1073 | const struct pmbus_driver_info *info = data->info; | |
1074 | int index, i; | |
0328461e | 1075 | int ret; |
442aba78 | 1076 | |
b4ce237b GR |
1077 | index = 1; |
1078 | for (i = 0; i < nattrs; i++) { | |
1079 | int page, pages; | |
1080 | ||
1081 | pages = attrs->paged ? info->pages : 1; | |
1082 | for (page = 0; page < pages; page++) { | |
1083 | if (!(info->func[page] & attrs->func)) | |
1084 | continue; | |
0328461e GR |
1085 | ret = pmbus_add_sensor_attrs_one(client, data, info, |
1086 | name, index, page, | |
1087 | attrs); | |
1088 | if (ret) | |
1089 | return ret; | |
b4ce237b | 1090 | index++; |
442aba78 | 1091 | } |
b4ce237b | 1092 | attrs++; |
442aba78 | 1093 | } |
0328461e | 1094 | return 0; |
b4ce237b | 1095 | } |
442aba78 | 1096 | |
b4ce237b GR |
1097 | static const struct pmbus_limit_attr vin_limit_attrs[] = { |
1098 | { | |
1099 | .reg = PMBUS_VIN_UV_WARN_LIMIT, | |
1100 | .attr = "min", | |
1101 | .alarm = "min_alarm", | |
1102 | .sbit = PB_VOLTAGE_UV_WARNING, | |
1103 | }, { | |
1104 | .reg = PMBUS_VIN_UV_FAULT_LIMIT, | |
1105 | .attr = "lcrit", | |
1106 | .alarm = "lcrit_alarm", | |
1107 | .sbit = PB_VOLTAGE_UV_FAULT, | |
1108 | }, { | |
1109 | .reg = PMBUS_VIN_OV_WARN_LIMIT, | |
1110 | .attr = "max", | |
1111 | .alarm = "max_alarm", | |
1112 | .sbit = PB_VOLTAGE_OV_WARNING, | |
1113 | }, { | |
1114 | .reg = PMBUS_VIN_OV_FAULT_LIMIT, | |
1115 | .attr = "crit", | |
1116 | .alarm = "crit_alarm", | |
1117 | .sbit = PB_VOLTAGE_OV_FAULT, | |
6f183d33 GR |
1118 | }, { |
1119 | .reg = PMBUS_VIRT_READ_VIN_AVG, | |
1120 | .update = true, | |
1121 | .attr = "average", | |
1122 | }, { | |
1123 | .reg = PMBUS_VIRT_READ_VIN_MIN, | |
1124 | .update = true, | |
1125 | .attr = "lowest", | |
1126 | }, { | |
1127 | .reg = PMBUS_VIRT_READ_VIN_MAX, | |
1128 | .update = true, | |
1129 | .attr = "highest", | |
1130 | }, { | |
1131 | .reg = PMBUS_VIRT_RESET_VIN_HISTORY, | |
1132 | .attr = "reset_history", | |
b4ce237b GR |
1133 | }, |
1134 | }; | |
1135 | ||
aebcbbfc GR |
1136 | static const struct pmbus_limit_attr vmon_limit_attrs[] = { |
1137 | { | |
1138 | .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, | |
1139 | .attr = "min", | |
1140 | .alarm = "min_alarm", | |
1141 | .sbit = PB_VOLTAGE_UV_WARNING, | |
1142 | }, { | |
1143 | .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, | |
1144 | .attr = "lcrit", | |
1145 | .alarm = "lcrit_alarm", | |
1146 | .sbit = PB_VOLTAGE_UV_FAULT, | |
1147 | }, { | |
1148 | .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, | |
1149 | .attr = "max", | |
1150 | .alarm = "max_alarm", | |
1151 | .sbit = PB_VOLTAGE_OV_WARNING, | |
1152 | }, { | |
1153 | .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, | |
1154 | .attr = "crit", | |
1155 | .alarm = "crit_alarm", | |
1156 | .sbit = PB_VOLTAGE_OV_FAULT, | |
1157 | } | |
1158 | }; | |
1159 | ||
b4ce237b GR |
1160 | static const struct pmbus_limit_attr vout_limit_attrs[] = { |
1161 | { | |
1162 | .reg = PMBUS_VOUT_UV_WARN_LIMIT, | |
1163 | .attr = "min", | |
1164 | .alarm = "min_alarm", | |
1165 | .sbit = PB_VOLTAGE_UV_WARNING, | |
1166 | }, { | |
1167 | .reg = PMBUS_VOUT_UV_FAULT_LIMIT, | |
1168 | .attr = "lcrit", | |
1169 | .alarm = "lcrit_alarm", | |
1170 | .sbit = PB_VOLTAGE_UV_FAULT, | |
1171 | }, { | |
1172 | .reg = PMBUS_VOUT_OV_WARN_LIMIT, | |
1173 | .attr = "max", | |
1174 | .alarm = "max_alarm", | |
1175 | .sbit = PB_VOLTAGE_OV_WARNING, | |
1176 | }, { | |
1177 | .reg = PMBUS_VOUT_OV_FAULT_LIMIT, | |
1178 | .attr = "crit", | |
1179 | .alarm = "crit_alarm", | |
1180 | .sbit = PB_VOLTAGE_OV_FAULT, | |
6f183d33 GR |
1181 | }, { |
1182 | .reg = PMBUS_VIRT_READ_VOUT_AVG, | |
1183 | .update = true, | |
1184 | .attr = "average", | |
1185 | }, { | |
1186 | .reg = PMBUS_VIRT_READ_VOUT_MIN, | |
1187 | .update = true, | |
1188 | .attr = "lowest", | |
1189 | }, { | |
1190 | .reg = PMBUS_VIRT_READ_VOUT_MAX, | |
1191 | .update = true, | |
1192 | .attr = "highest", | |
1193 | }, { | |
1194 | .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, | |
1195 | .attr = "reset_history", | |
442aba78 | 1196 | } |
b4ce237b | 1197 | }; |
442aba78 | 1198 | |
b4ce237b GR |
1199 | static const struct pmbus_sensor_attr voltage_attributes[] = { |
1200 | { | |
1201 | .reg = PMBUS_READ_VIN, | |
1202 | .class = PSC_VOLTAGE_IN, | |
1203 | .label = "vin", | |
1204 | .func = PMBUS_HAVE_VIN, | |
1205 | .sfunc = PMBUS_HAVE_STATUS_INPUT, | |
1206 | .sbase = PB_STATUS_INPUT_BASE, | |
1207 | .gbit = PB_STATUS_VIN_UV, | |
1208 | .limit = vin_limit_attrs, | |
1209 | .nlimit = ARRAY_SIZE(vin_limit_attrs), | |
aebcbbfc GR |
1210 | }, { |
1211 | .reg = PMBUS_VIRT_READ_VMON, | |
1212 | .class = PSC_VOLTAGE_IN, | |
1213 | .label = "vmon", | |
1214 | .func = PMBUS_HAVE_VMON, | |
1215 | .sfunc = PMBUS_HAVE_STATUS_VMON, | |
1216 | .sbase = PB_STATUS_VMON_BASE, | |
1217 | .limit = vmon_limit_attrs, | |
1218 | .nlimit = ARRAY_SIZE(vmon_limit_attrs), | |
b4ce237b GR |
1219 | }, { |
1220 | .reg = PMBUS_READ_VCAP, | |
1221 | .class = PSC_VOLTAGE_IN, | |
1222 | .label = "vcap", | |
1223 | .func = PMBUS_HAVE_VCAP, | |
1224 | }, { | |
1225 | .reg = PMBUS_READ_VOUT, | |
1226 | .class = PSC_VOLTAGE_OUT, | |
1227 | .label = "vout", | |
1228 | .paged = true, | |
1229 | .func = PMBUS_HAVE_VOUT, | |
1230 | .sfunc = PMBUS_HAVE_STATUS_VOUT, | |
1231 | .sbase = PB_STATUS_VOUT_BASE, | |
1232 | .gbit = PB_STATUS_VOUT_OV, | |
1233 | .limit = vout_limit_attrs, | |
1234 | .nlimit = ARRAY_SIZE(vout_limit_attrs), | |
442aba78 | 1235 | } |
b4ce237b | 1236 | }; |
442aba78 | 1237 | |
b4ce237b GR |
1238 | /* Current attributes */ |
1239 | ||
1240 | static const struct pmbus_limit_attr iin_limit_attrs[] = { | |
1241 | { | |
1242 | .reg = PMBUS_IIN_OC_WARN_LIMIT, | |
1243 | .attr = "max", | |
1244 | .alarm = "max_alarm", | |
1245 | .sbit = PB_IIN_OC_WARNING, | |
1246 | }, { | |
1247 | .reg = PMBUS_IIN_OC_FAULT_LIMIT, | |
1248 | .attr = "crit", | |
1249 | .alarm = "crit_alarm", | |
1250 | .sbit = PB_IIN_OC_FAULT, | |
6f183d33 GR |
1251 | }, { |
1252 | .reg = PMBUS_VIRT_READ_IIN_AVG, | |
1253 | .update = true, | |
1254 | .attr = "average", | |
1255 | }, { | |
1256 | .reg = PMBUS_VIRT_READ_IIN_MIN, | |
1257 | .update = true, | |
1258 | .attr = "lowest", | |
1259 | }, { | |
1260 | .reg = PMBUS_VIRT_READ_IIN_MAX, | |
1261 | .update = true, | |
1262 | .attr = "highest", | |
1263 | }, { | |
1264 | .reg = PMBUS_VIRT_RESET_IIN_HISTORY, | |
1265 | .attr = "reset_history", | |
b4ce237b GR |
1266 | } |
1267 | }; | |
442aba78 | 1268 | |
b4ce237b GR |
1269 | static const struct pmbus_limit_attr iout_limit_attrs[] = { |
1270 | { | |
1271 | .reg = PMBUS_IOUT_OC_WARN_LIMIT, | |
1272 | .attr = "max", | |
1273 | .alarm = "max_alarm", | |
1274 | .sbit = PB_IOUT_OC_WARNING, | |
1275 | }, { | |
1276 | .reg = PMBUS_IOUT_UC_FAULT_LIMIT, | |
1277 | .attr = "lcrit", | |
1278 | .alarm = "lcrit_alarm", | |
1279 | .sbit = PB_IOUT_UC_FAULT, | |
1280 | }, { | |
1281 | .reg = PMBUS_IOUT_OC_FAULT_LIMIT, | |
1282 | .attr = "crit", | |
1283 | .alarm = "crit_alarm", | |
1284 | .sbit = PB_IOUT_OC_FAULT, | |
6f183d33 GR |
1285 | }, { |
1286 | .reg = PMBUS_VIRT_READ_IOUT_AVG, | |
1287 | .update = true, | |
1288 | .attr = "average", | |
1289 | }, { | |
1290 | .reg = PMBUS_VIRT_READ_IOUT_MIN, | |
1291 | .update = true, | |
1292 | .attr = "lowest", | |
1293 | }, { | |
1294 | .reg = PMBUS_VIRT_READ_IOUT_MAX, | |
1295 | .update = true, | |
1296 | .attr = "highest", | |
1297 | }, { | |
1298 | .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, | |
1299 | .attr = "reset_history", | |
b4ce237b GR |
1300 | } |
1301 | }; | |
442aba78 | 1302 | |
b4ce237b GR |
1303 | static const struct pmbus_sensor_attr current_attributes[] = { |
1304 | { | |
1305 | .reg = PMBUS_READ_IIN, | |
1306 | .class = PSC_CURRENT_IN, | |
1307 | .label = "iin", | |
1308 | .func = PMBUS_HAVE_IIN, | |
1309 | .sfunc = PMBUS_HAVE_STATUS_INPUT, | |
1310 | .sbase = PB_STATUS_INPUT_BASE, | |
1311 | .limit = iin_limit_attrs, | |
1312 | .nlimit = ARRAY_SIZE(iin_limit_attrs), | |
1313 | }, { | |
1314 | .reg = PMBUS_READ_IOUT, | |
1315 | .class = PSC_CURRENT_OUT, | |
1316 | .label = "iout", | |
1317 | .paged = true, | |
1318 | .func = PMBUS_HAVE_IOUT, | |
1319 | .sfunc = PMBUS_HAVE_STATUS_IOUT, | |
1320 | .sbase = PB_STATUS_IOUT_BASE, | |
1321 | .gbit = PB_STATUS_IOUT_OC, | |
1322 | .limit = iout_limit_attrs, | |
1323 | .nlimit = ARRAY_SIZE(iout_limit_attrs), | |
442aba78 | 1324 | } |
b4ce237b | 1325 | }; |
442aba78 | 1326 | |
b4ce237b | 1327 | /* Power attributes */ |
442aba78 | 1328 | |
b4ce237b GR |
1329 | static const struct pmbus_limit_attr pin_limit_attrs[] = { |
1330 | { | |
1331 | .reg = PMBUS_PIN_OP_WARN_LIMIT, | |
1332 | .attr = "max", | |
1333 | .alarm = "alarm", | |
1334 | .sbit = PB_PIN_OP_WARNING, | |
6f183d33 GR |
1335 | }, { |
1336 | .reg = PMBUS_VIRT_READ_PIN_AVG, | |
1337 | .update = true, | |
1338 | .attr = "average", | |
48065a13 GR |
1339 | }, { |
1340 | .reg = PMBUS_VIRT_READ_PIN_MIN, | |
1341 | .update = true, | |
1342 | .attr = "input_lowest", | |
6f183d33 GR |
1343 | }, { |
1344 | .reg = PMBUS_VIRT_READ_PIN_MAX, | |
1345 | .update = true, | |
1346 | .attr = "input_highest", | |
1347 | }, { | |
1348 | .reg = PMBUS_VIRT_RESET_PIN_HISTORY, | |
1349 | .attr = "reset_history", | |
b4ce237b GR |
1350 | } |
1351 | }; | |
442aba78 | 1352 | |
b4ce237b GR |
1353 | static const struct pmbus_limit_attr pout_limit_attrs[] = { |
1354 | { | |
1355 | .reg = PMBUS_POUT_MAX, | |
1356 | .attr = "cap", | |
1357 | .alarm = "cap_alarm", | |
1358 | .sbit = PB_POWER_LIMITING, | |
1359 | }, { | |
1360 | .reg = PMBUS_POUT_OP_WARN_LIMIT, | |
1361 | .attr = "max", | |
1362 | .alarm = "max_alarm", | |
1363 | .sbit = PB_POUT_OP_WARNING, | |
1364 | }, { | |
1365 | .reg = PMBUS_POUT_OP_FAULT_LIMIT, | |
1366 | .attr = "crit", | |
1367 | .alarm = "crit_alarm", | |
1368 | .sbit = PB_POUT_OP_FAULT, | |
60b873e3 GR |
1369 | }, { |
1370 | .reg = PMBUS_VIRT_READ_POUT_AVG, | |
1371 | .update = true, | |
1372 | .attr = "average", | |
48065a13 GR |
1373 | }, { |
1374 | .reg = PMBUS_VIRT_READ_POUT_MIN, | |
1375 | .update = true, | |
1376 | .attr = "input_lowest", | |
60b873e3 GR |
1377 | }, { |
1378 | .reg = PMBUS_VIRT_READ_POUT_MAX, | |
1379 | .update = true, | |
1380 | .attr = "input_highest", | |
1381 | }, { | |
1382 | .reg = PMBUS_VIRT_RESET_POUT_HISTORY, | |
1383 | .attr = "reset_history", | |
b4ce237b GR |
1384 | } |
1385 | }; | |
b49547a5 | 1386 | |
b4ce237b GR |
1387 | static const struct pmbus_sensor_attr power_attributes[] = { |
1388 | { | |
1389 | .reg = PMBUS_READ_PIN, | |
1390 | .class = PSC_POWER, | |
1391 | .label = "pin", | |
1392 | .func = PMBUS_HAVE_PIN, | |
1393 | .sfunc = PMBUS_HAVE_STATUS_INPUT, | |
1394 | .sbase = PB_STATUS_INPUT_BASE, | |
1395 | .limit = pin_limit_attrs, | |
1396 | .nlimit = ARRAY_SIZE(pin_limit_attrs), | |
1397 | }, { | |
1398 | .reg = PMBUS_READ_POUT, | |
1399 | .class = PSC_POWER, | |
1400 | .label = "pout", | |
1401 | .paged = true, | |
1402 | .func = PMBUS_HAVE_POUT, | |
1403 | .sfunc = PMBUS_HAVE_STATUS_IOUT, | |
1404 | .sbase = PB_STATUS_IOUT_BASE, | |
1405 | .limit = pout_limit_attrs, | |
1406 | .nlimit = ARRAY_SIZE(pout_limit_attrs), | |
1407 | } | |
1408 | }; | |
442aba78 | 1409 | |
b4ce237b GR |
1410 | /* Temperature atributes */ |
1411 | ||
1412 | static const struct pmbus_limit_attr temp_limit_attrs[] = { | |
6f183d33 GR |
1413 | { |
1414 | .reg = PMBUS_UT_WARN_LIMIT, | |
40257b95 | 1415 | .low = true, |
6f183d33 GR |
1416 | .attr = "min", |
1417 | .alarm = "min_alarm", | |
1418 | .sbit = PB_TEMP_UT_WARNING, | |
1419 | }, { | |
1420 | .reg = PMBUS_UT_FAULT_LIMIT, | |
40257b95 | 1421 | .low = true, |
6f183d33 GR |
1422 | .attr = "lcrit", |
1423 | .alarm = "lcrit_alarm", | |
1424 | .sbit = PB_TEMP_UT_FAULT, | |
1425 | }, { | |
1426 | .reg = PMBUS_OT_WARN_LIMIT, | |
1427 | .attr = "max", | |
1428 | .alarm = "max_alarm", | |
1429 | .sbit = PB_TEMP_OT_WARNING, | |
1430 | }, { | |
1431 | .reg = PMBUS_OT_FAULT_LIMIT, | |
1432 | .attr = "crit", | |
1433 | .alarm = "crit_alarm", | |
1434 | .sbit = PB_TEMP_OT_FAULT, | |
1435 | }, { | |
1436 | .reg = PMBUS_VIRT_READ_TEMP_MIN, | |
1437 | .attr = "lowest", | |
60b873e3 GR |
1438 | }, { |
1439 | .reg = PMBUS_VIRT_READ_TEMP_AVG, | |
1440 | .attr = "average", | |
6f183d33 GR |
1441 | }, { |
1442 | .reg = PMBUS_VIRT_READ_TEMP_MAX, | |
1443 | .attr = "highest", | |
1444 | }, { | |
1445 | .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, | |
1446 | .attr = "reset_history", | |
1447 | } | |
1448 | }; | |
1449 | ||
3d790287 GR |
1450 | static const struct pmbus_limit_attr temp_limit_attrs2[] = { |
1451 | { | |
1452 | .reg = PMBUS_UT_WARN_LIMIT, | |
1453 | .low = true, | |
1454 | .attr = "min", | |
1455 | .alarm = "min_alarm", | |
1456 | .sbit = PB_TEMP_UT_WARNING, | |
1457 | }, { | |
1458 | .reg = PMBUS_UT_FAULT_LIMIT, | |
1459 | .low = true, | |
1460 | .attr = "lcrit", | |
1461 | .alarm = "lcrit_alarm", | |
1462 | .sbit = PB_TEMP_UT_FAULT, | |
1463 | }, { | |
1464 | .reg = PMBUS_OT_WARN_LIMIT, | |
1465 | .attr = "max", | |
1466 | .alarm = "max_alarm", | |
1467 | .sbit = PB_TEMP_OT_WARNING, | |
1468 | }, { | |
1469 | .reg = PMBUS_OT_FAULT_LIMIT, | |
1470 | .attr = "crit", | |
1471 | .alarm = "crit_alarm", | |
1472 | .sbit = PB_TEMP_OT_FAULT, | |
1473 | }, { | |
1474 | .reg = PMBUS_VIRT_READ_TEMP2_MIN, | |
1475 | .attr = "lowest", | |
60b873e3 GR |
1476 | }, { |
1477 | .reg = PMBUS_VIRT_READ_TEMP2_AVG, | |
1478 | .attr = "average", | |
3d790287 GR |
1479 | }, { |
1480 | .reg = PMBUS_VIRT_READ_TEMP2_MAX, | |
1481 | .attr = "highest", | |
1482 | }, { | |
1483 | .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, | |
1484 | .attr = "reset_history", | |
1485 | } | |
1486 | }; | |
1487 | ||
1488 | static const struct pmbus_limit_attr temp_limit_attrs3[] = { | |
b4ce237b GR |
1489 | { |
1490 | .reg = PMBUS_UT_WARN_LIMIT, | |
40257b95 | 1491 | .low = true, |
b4ce237b GR |
1492 | .attr = "min", |
1493 | .alarm = "min_alarm", | |
1494 | .sbit = PB_TEMP_UT_WARNING, | |
1495 | }, { | |
1496 | .reg = PMBUS_UT_FAULT_LIMIT, | |
40257b95 | 1497 | .low = true, |
b4ce237b GR |
1498 | .attr = "lcrit", |
1499 | .alarm = "lcrit_alarm", | |
1500 | .sbit = PB_TEMP_UT_FAULT, | |
1501 | }, { | |
1502 | .reg = PMBUS_OT_WARN_LIMIT, | |
1503 | .attr = "max", | |
1504 | .alarm = "max_alarm", | |
1505 | .sbit = PB_TEMP_OT_WARNING, | |
1506 | }, { | |
1507 | .reg = PMBUS_OT_FAULT_LIMIT, | |
1508 | .attr = "crit", | |
1509 | .alarm = "crit_alarm", | |
1510 | .sbit = PB_TEMP_OT_FAULT, | |
1511 | } | |
1512 | }; | |
442aba78 | 1513 | |
b4ce237b GR |
1514 | static const struct pmbus_sensor_attr temp_attributes[] = { |
1515 | { | |
1516 | .reg = PMBUS_READ_TEMPERATURE_1, | |
1517 | .class = PSC_TEMPERATURE, | |
1518 | .paged = true, | |
1519 | .update = true, | |
1520 | .compare = true, | |
1521 | .func = PMBUS_HAVE_TEMP, | |
1522 | .sfunc = PMBUS_HAVE_STATUS_TEMP, | |
1523 | .sbase = PB_STATUS_TEMP_BASE, | |
1524 | .gbit = PB_STATUS_TEMPERATURE, | |
1525 | .limit = temp_limit_attrs, | |
1526 | .nlimit = ARRAY_SIZE(temp_limit_attrs), | |
1527 | }, { | |
1528 | .reg = PMBUS_READ_TEMPERATURE_2, | |
1529 | .class = PSC_TEMPERATURE, | |
1530 | .paged = true, | |
1531 | .update = true, | |
1532 | .compare = true, | |
1533 | .func = PMBUS_HAVE_TEMP2, | |
1534 | .sfunc = PMBUS_HAVE_STATUS_TEMP, | |
1535 | .sbase = PB_STATUS_TEMP_BASE, | |
1536 | .gbit = PB_STATUS_TEMPERATURE, | |
3d790287 GR |
1537 | .limit = temp_limit_attrs2, |
1538 | .nlimit = ARRAY_SIZE(temp_limit_attrs2), | |
b4ce237b GR |
1539 | }, { |
1540 | .reg = PMBUS_READ_TEMPERATURE_3, | |
1541 | .class = PSC_TEMPERATURE, | |
1542 | .paged = true, | |
1543 | .update = true, | |
1544 | .compare = true, | |
1545 | .func = PMBUS_HAVE_TEMP3, | |
1546 | .sfunc = PMBUS_HAVE_STATUS_TEMP, | |
1547 | .sbase = PB_STATUS_TEMP_BASE, | |
1548 | .gbit = PB_STATUS_TEMPERATURE, | |
3d790287 GR |
1549 | .limit = temp_limit_attrs3, |
1550 | .nlimit = ARRAY_SIZE(temp_limit_attrs3), | |
442aba78 | 1551 | } |
b4ce237b GR |
1552 | }; |
1553 | ||
1554 | static const int pmbus_fan_registers[] = { | |
1555 | PMBUS_READ_FAN_SPEED_1, | |
1556 | PMBUS_READ_FAN_SPEED_2, | |
1557 | PMBUS_READ_FAN_SPEED_3, | |
1558 | PMBUS_READ_FAN_SPEED_4 | |
1559 | }; | |
1560 | ||
1561 | static const int pmbus_fan_config_registers[] = { | |
1562 | PMBUS_FAN_CONFIG_12, | |
1563 | PMBUS_FAN_CONFIG_12, | |
1564 | PMBUS_FAN_CONFIG_34, | |
1565 | PMBUS_FAN_CONFIG_34 | |
1566 | }; | |
1567 | ||
1568 | static const int pmbus_fan_status_registers[] = { | |
1569 | PMBUS_STATUS_FAN_12, | |
1570 | PMBUS_STATUS_FAN_12, | |
1571 | PMBUS_STATUS_FAN_34, | |
1572 | PMBUS_STATUS_FAN_34 | |
1573 | }; | |
1574 | ||
1575 | static const u32 pmbus_fan_flags[] = { | |
1576 | PMBUS_HAVE_FAN12, | |
1577 | PMBUS_HAVE_FAN12, | |
1578 | PMBUS_HAVE_FAN34, | |
1579 | PMBUS_HAVE_FAN34 | |
1580 | }; | |
1581 | ||
1582 | static const u32 pmbus_fan_status_flags[] = { | |
1583 | PMBUS_HAVE_STATUS_FAN12, | |
1584 | PMBUS_HAVE_STATUS_FAN12, | |
1585 | PMBUS_HAVE_STATUS_FAN34, | |
1586 | PMBUS_HAVE_STATUS_FAN34 | |
1587 | }; | |
1588 | ||
1589 | /* Fans */ | |
0328461e GR |
1590 | static int pmbus_add_fan_attributes(struct i2c_client *client, |
1591 | struct pmbus_data *data) | |
b4ce237b GR |
1592 | { |
1593 | const struct pmbus_driver_info *info = data->info; | |
1594 | int index = 1; | |
1595 | int page; | |
0328461e | 1596 | int ret; |
442aba78 | 1597 | |
442aba78 | 1598 | for (page = 0; page < info->pages; page++) { |
954df676 | 1599 | int f; |
442aba78 | 1600 | |
954df676 | 1601 | for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { |
442aba78 GR |
1602 | int regval; |
1603 | ||
954df676 GR |
1604 | if (!(info->func[page] & pmbus_fan_flags[f])) |
1605 | break; | |
1606 | ||
442aba78 | 1607 | if (!pmbus_check_word_register(client, page, |
6586b14c | 1608 | pmbus_fan_registers[f])) |
442aba78 GR |
1609 | break; |
1610 | ||
1611 | /* | |
1612 | * Skip fan if not installed. | |
1613 | * Each fan configuration register covers multiple fans, | |
1614 | * so we have to do some magic. | |
1615 | */ | |
6586b14c | 1616 | regval = _pmbus_read_byte_data(client, page, |
442aba78 GR |
1617 | pmbus_fan_config_registers[f]); |
1618 | if (regval < 0 || | |
1619 | (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) | |
1620 | continue; | |
1621 | ||
e1e081a7 GR |
1622 | if (pmbus_add_sensor(data, "fan", "input", index, |
1623 | page, pmbus_fan_registers[f], | |
1624 | PSC_FAN, true, true) == NULL) | |
1625 | return -ENOMEM; | |
442aba78 GR |
1626 | |
1627 | /* | |
1628 | * Each fan status register covers multiple fans, | |
1629 | * so we have to do some magic. | |
1630 | */ | |
954df676 GR |
1631 | if ((info->func[page] & pmbus_fan_status_flags[f]) && |
1632 | pmbus_check_byte_register(client, | |
1633 | page, pmbus_fan_status_registers[f])) { | |
442aba78 GR |
1634 | int base; |
1635 | ||
1636 | if (f > 1) /* fan 3, 4 */ | |
954df676 | 1637 | base = PB_STATUS_FAN34_BASE + page; |
442aba78 GR |
1638 | else |
1639 | base = PB_STATUS_FAN_BASE + page; | |
663834f3 GR |
1640 | ret = pmbus_add_boolean(data, "fan", |
1641 | "alarm", index, NULL, NULL, base, | |
442aba78 | 1642 | PB_FAN_FAN1_WARNING >> (f & 1)); |
0328461e GR |
1643 | if (ret) |
1644 | return ret; | |
663834f3 GR |
1645 | ret = pmbus_add_boolean(data, "fan", |
1646 | "fault", index, NULL, NULL, base, | |
442aba78 | 1647 | PB_FAN_FAN1_FAULT >> (f & 1)); |
0328461e GR |
1648 | if (ret) |
1649 | return ret; | |
442aba78 | 1650 | } |
b4ce237b | 1651 | index++; |
442aba78 GR |
1652 | } |
1653 | } | |
0328461e | 1654 | return 0; |
442aba78 GR |
1655 | } |
1656 | ||
0328461e GR |
1657 | static int pmbus_find_attributes(struct i2c_client *client, |
1658 | struct pmbus_data *data) | |
b4ce237b | 1659 | { |
0328461e GR |
1660 | int ret; |
1661 | ||
b4ce237b | 1662 | /* Voltage sensors */ |
0328461e GR |
1663 | ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, |
1664 | ARRAY_SIZE(voltage_attributes)); | |
1665 | if (ret) | |
1666 | return ret; | |
b4ce237b GR |
1667 | |
1668 | /* Current sensors */ | |
0328461e GR |
1669 | ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, |
1670 | ARRAY_SIZE(current_attributes)); | |
1671 | if (ret) | |
1672 | return ret; | |
b4ce237b GR |
1673 | |
1674 | /* Power sensors */ | |
0328461e GR |
1675 | ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, |
1676 | ARRAY_SIZE(power_attributes)); | |
1677 | if (ret) | |
1678 | return ret; | |
b4ce237b GR |
1679 | |
1680 | /* Temperature sensors */ | |
0328461e GR |
1681 | ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, |
1682 | ARRAY_SIZE(temp_attributes)); | |
1683 | if (ret) | |
1684 | return ret; | |
b4ce237b GR |
1685 | |
1686 | /* Fans */ | |
0328461e GR |
1687 | ret = pmbus_add_fan_attributes(client, data); |
1688 | return ret; | |
b4ce237b GR |
1689 | } |
1690 | ||
442aba78 GR |
1691 | /* |
1692 | * Identify chip parameters. | |
1693 | * This function is called for all chips. | |
1694 | */ | |
1695 | static int pmbus_identify_common(struct i2c_client *client, | |
daa436e6 | 1696 | struct pmbus_data *data, int page) |
442aba78 | 1697 | { |
1af1f531 | 1698 | int vout_mode = -1; |
442aba78 | 1699 | |
daa436e6 GR |
1700 | if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) |
1701 | vout_mode = _pmbus_read_byte_data(client, page, | |
1702 | PMBUS_VOUT_MODE); | |
83274c68 | 1703 | if (vout_mode >= 0 && vout_mode != 0xff) { |
442aba78 GR |
1704 | /* |
1705 | * Not all chips support the VOUT_MODE command, | |
1706 | * so a failure to read it is not an error. | |
1707 | */ | |
1708 | switch (vout_mode >> 5) { | |
1709 | case 0: /* linear mode */ | |
1061d851 | 1710 | if (data->info->format[PSC_VOLTAGE_OUT] != linear) |
442aba78 GR |
1711 | return -ENODEV; |
1712 | ||
daa436e6 | 1713 | data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; |
442aba78 | 1714 | break; |
1061d851 GR |
1715 | case 1: /* VID mode */ |
1716 | if (data->info->format[PSC_VOLTAGE_OUT] != vid) | |
1717 | return -ENODEV; | |
1718 | break; | |
442aba78 | 1719 | case 2: /* direct mode */ |
1061d851 | 1720 | if (data->info->format[PSC_VOLTAGE_OUT] != direct) |
442aba78 GR |
1721 | return -ENODEV; |
1722 | break; | |
1723 | default: | |
1724 | return -ENODEV; | |
1725 | } | |
1726 | } | |
1727 | ||
daa436e6 | 1728 | pmbus_clear_fault_page(client, page); |
442aba78 GR |
1729 | return 0; |
1730 | } | |
1731 | ||
16c6d01f GR |
1732 | static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, |
1733 | struct pmbus_driver_info *info) | |
1734 | { | |
1735 | struct device *dev = &client->dev; | |
daa436e6 | 1736 | int page, ret; |
16c6d01f GR |
1737 | |
1738 | /* | |
1739 | * Some PMBus chips don't support PMBUS_STATUS_BYTE, so try | |
1740 | * to use PMBUS_STATUS_WORD instead if that is the case. | |
1741 | * Bail out if both registers are not supported. | |
1742 | */ | |
1743 | data->status_register = PMBUS_STATUS_BYTE; | |
1744 | ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); | |
1745 | if (ret < 0 || ret == 0xff) { | |
1746 | data->status_register = PMBUS_STATUS_WORD; | |
1747 | ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); | |
1748 | if (ret < 0 || ret == 0xffff) { | |
1749 | dev_err(dev, "PMBus status register not found\n"); | |
1750 | return -ENODEV; | |
1751 | } | |
1752 | } | |
1753 | ||
d830e27d GR |
1754 | /* Enable PEC if the controller supports it */ |
1755 | ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); | |
1756 | if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) | |
1757 | client->flags |= I2C_CLIENT_PEC; | |
1758 | ||
16c6d01f GR |
1759 | pmbus_clear_faults(client); |
1760 | ||
1761 | if (info->identify) { | |
1762 | ret = (*info->identify)(client, info); | |
1763 | if (ret < 0) { | |
1764 | dev_err(dev, "Chip identification failed\n"); | |
1765 | return ret; | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | if (info->pages <= 0 || info->pages > PMBUS_PAGES) { | |
1770 | dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); | |
1771 | return -ENODEV; | |
1772 | } | |
1773 | ||
daa436e6 GR |
1774 | for (page = 0; page < info->pages; page++) { |
1775 | ret = pmbus_identify_common(client, data, page); | |
1776 | if (ret < 0) { | |
1777 | dev_err(dev, "Failed to identify chip capabilities\n"); | |
1778 | return ret; | |
1779 | } | |
16c6d01f GR |
1780 | } |
1781 | return 0; | |
1782 | } | |
1783 | ||
ddbb4db4 AT |
1784 | #if IS_ENABLED(CONFIG_REGULATOR) |
1785 | static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) | |
1786 | { | |
1787 | struct device *dev = rdev_get_dev(rdev); | |
1788 | struct i2c_client *client = to_i2c_client(dev->parent); | |
1789 | u8 page = rdev_get_id(rdev); | |
1790 | int ret; | |
1791 | ||
1792 | ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION); | |
1793 | if (ret < 0) | |
1794 | return ret; | |
1795 | ||
1796 | return !!(ret & PB_OPERATION_CONTROL_ON); | |
1797 | } | |
1798 | ||
1799 | static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) | |
1800 | { | |
1801 | struct device *dev = rdev_get_dev(rdev); | |
1802 | struct i2c_client *client = to_i2c_client(dev->parent); | |
1803 | u8 page = rdev_get_id(rdev); | |
1804 | ||
1805 | return pmbus_update_byte_data(client, page, PMBUS_OPERATION, | |
1806 | PB_OPERATION_CONTROL_ON, | |
1807 | enable ? PB_OPERATION_CONTROL_ON : 0); | |
1808 | } | |
1809 | ||
1810 | static int pmbus_regulator_enable(struct regulator_dev *rdev) | |
1811 | { | |
1812 | return _pmbus_regulator_on_off(rdev, 1); | |
1813 | } | |
1814 | ||
1815 | static int pmbus_regulator_disable(struct regulator_dev *rdev) | |
1816 | { | |
1817 | return _pmbus_regulator_on_off(rdev, 0); | |
1818 | } | |
1819 | ||
a07d7311 | 1820 | const struct regulator_ops pmbus_regulator_ops = { |
ddbb4db4 AT |
1821 | .enable = pmbus_regulator_enable, |
1822 | .disable = pmbus_regulator_disable, | |
1823 | .is_enabled = pmbus_regulator_is_enabled, | |
1824 | }; | |
1825 | EXPORT_SYMBOL_GPL(pmbus_regulator_ops); | |
1826 | ||
1827 | static int pmbus_regulator_register(struct pmbus_data *data) | |
1828 | { | |
1829 | struct device *dev = data->dev; | |
1830 | const struct pmbus_driver_info *info = data->info; | |
1831 | const struct pmbus_platform_data *pdata = dev_get_platdata(dev); | |
1832 | struct regulator_dev *rdev; | |
1833 | int i; | |
1834 | ||
1835 | for (i = 0; i < info->num_regulators; i++) { | |
1836 | struct regulator_config config = { }; | |
1837 | ||
1838 | config.dev = dev; | |
1839 | config.driver_data = data; | |
1840 | ||
1841 | if (pdata && pdata->reg_init_data) | |
1842 | config.init_data = &pdata->reg_init_data[i]; | |
1843 | ||
1844 | rdev = devm_regulator_register(dev, &info->reg_desc[i], | |
1845 | &config); | |
1846 | if (IS_ERR(rdev)) { | |
1847 | dev_err(dev, "Failed to register %s regulator\n", | |
1848 | info->reg_desc[i].name); | |
1849 | return PTR_ERR(rdev); | |
1850 | } | |
1851 | } | |
1852 | ||
1853 | return 0; | |
1854 | } | |
1855 | #else | |
1856 | static int pmbus_regulator_register(struct pmbus_data *data) | |
1857 | { | |
1858 | return 0; | |
1859 | } | |
1860 | #endif | |
1861 | ||
442aba78 GR |
1862 | int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id, |
1863 | struct pmbus_driver_info *info) | |
1864 | { | |
c2a58351 | 1865 | struct device *dev = &client->dev; |
a8b3a3a5 | 1866 | const struct pmbus_platform_data *pdata = dev_get_platdata(dev); |
442aba78 GR |
1867 | struct pmbus_data *data; |
1868 | int ret; | |
1869 | ||
77493ef6 | 1870 | if (!info) |
442aba78 | 1871 | return -ENODEV; |
442aba78 GR |
1872 | |
1873 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE | |
1874 | | I2C_FUNC_SMBUS_BYTE_DATA | |
1875 | | I2C_FUNC_SMBUS_WORD_DATA)) | |
1876 | return -ENODEV; | |
1877 | ||
c2a58351 | 1878 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
77493ef6 | 1879 | if (!data) |
442aba78 | 1880 | return -ENOMEM; |
442aba78 GR |
1881 | |
1882 | i2c_set_clientdata(client, data); | |
1883 | mutex_init(&data->update_lock); | |
0328461e | 1884 | data->dev = dev; |
442aba78 | 1885 | |
442aba78 GR |
1886 | if (pdata) |
1887 | data->flags = pdata->flags; | |
1888 | data->info = info; | |
1889 | ||
16c6d01f GR |
1890 | ret = pmbus_init_common(client, data, info); |
1891 | if (ret < 0) | |
8b313ca7 | 1892 | return ret; |
442aba78 | 1893 | |
0328461e GR |
1894 | ret = pmbus_find_attributes(client, data); |
1895 | if (ret) | |
85cfb3a8 | 1896 | goto out_kfree; |
442aba78 GR |
1897 | |
1898 | /* | |
1899 | * If there are no attributes, something is wrong. | |
1900 | * Bail out instead of trying to register nothing. | |
1901 | */ | |
1902 | if (!data->num_attributes) { | |
c2a58351 | 1903 | dev_err(dev, "No attributes found\n"); |
85cfb3a8 GR |
1904 | ret = -ENODEV; |
1905 | goto out_kfree; | |
442aba78 GR |
1906 | } |
1907 | ||
c3b7cddc GR |
1908 | data->groups[0] = &data->group; |
1909 | data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name, | |
1910 | data, data->groups); | |
442aba78 GR |
1911 | if (IS_ERR(data->hwmon_dev)) { |
1912 | ret = PTR_ERR(data->hwmon_dev); | |
c2a58351 | 1913 | dev_err(dev, "Failed to register hwmon device\n"); |
c3b7cddc | 1914 | goto out_kfree; |
442aba78 | 1915 | } |
ddbb4db4 AT |
1916 | |
1917 | ret = pmbus_regulator_register(data); | |
1918 | if (ret) | |
1919 | goto out_unregister; | |
1920 | ||
442aba78 GR |
1921 | return 0; |
1922 | ||
ddbb4db4 AT |
1923 | out_unregister: |
1924 | hwmon_device_unregister(data->hwmon_dev); | |
85cfb3a8 GR |
1925 | out_kfree: |
1926 | kfree(data->group.attrs); | |
442aba78 GR |
1927 | return ret; |
1928 | } | |
1929 | EXPORT_SYMBOL_GPL(pmbus_do_probe); | |
1930 | ||
dd285ad7 | 1931 | int pmbus_do_remove(struct i2c_client *client) |
442aba78 GR |
1932 | { |
1933 | struct pmbus_data *data = i2c_get_clientdata(client); | |
1934 | hwmon_device_unregister(data->hwmon_dev); | |
85cfb3a8 | 1935 | kfree(data->group.attrs); |
dd285ad7 | 1936 | return 0; |
442aba78 GR |
1937 | } |
1938 | EXPORT_SYMBOL_GPL(pmbus_do_remove); | |
1939 | ||
1940 | MODULE_AUTHOR("Guenter Roeck"); | |
1941 | MODULE_DESCRIPTION("PMBus core driver"); | |
1942 | MODULE_LICENSE("GPL"); |