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