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