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