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