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