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