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