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