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