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 (regval) {
902 ret = pmbus_write_byte_data(client, page, reg, regval);
903 if (ret)
904 goto unlock;
905 }
906 if (s1 && s2) {
907 s64 v1, v2;
908
909 if (s1->data < 0) {
910 ret = s1->data;
911 goto unlock;
912 }
913 if (s2->data < 0) {
914 ret = s2->data;
915 goto unlock;
916 }
917
918 v1 = pmbus_reg2data(data, s1);
919 v2 = pmbus_reg2data(data, s2);
920 ret = !!(regval && v1 >= v2);
921 } else {
922 ret = !!regval;
923 }
924 unlock:
925 mutex_unlock(&data->update_lock);
926 return ret;
927 }
928
pmbus_show_boolean(struct device * dev,struct device_attribute * da,char * buf)929 static ssize_t pmbus_show_boolean(struct device *dev,
930 struct device_attribute *da, char *buf)
931 {
932 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
933 struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
934 struct i2c_client *client = to_i2c_client(dev->parent);
935 int val;
936
937 val = pmbus_get_boolean(client, boolean, attr->index);
938 if (val < 0)
939 return val;
940 return snprintf(buf, PAGE_SIZE, "%d\n", val);
941 }
942
pmbus_show_sensor(struct device * dev,struct device_attribute * devattr,char * buf)943 static ssize_t pmbus_show_sensor(struct device *dev,
944 struct device_attribute *devattr, char *buf)
945 {
946 struct i2c_client *client = to_i2c_client(dev->parent);
947 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
948 struct pmbus_data *data = i2c_get_clientdata(client);
949 ssize_t ret;
950
951 mutex_lock(&data->update_lock);
952 pmbus_update_sensor_data(client, sensor);
953 if (sensor->data < 0)
954 ret = sensor->data;
955 else
956 ret = snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
957 mutex_unlock(&data->update_lock);
958 return ret;
959 }
960
pmbus_set_sensor(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)961 static ssize_t pmbus_set_sensor(struct device *dev,
962 struct device_attribute *devattr,
963 const char *buf, size_t count)
964 {
965 struct i2c_client *client = to_i2c_client(dev->parent);
966 struct pmbus_data *data = i2c_get_clientdata(client);
967 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
968 ssize_t rv = count;
969 s64 val;
970 int ret;
971 u16 regval;
972
973 if (kstrtos64(buf, 10, &val) < 0)
974 return -EINVAL;
975
976 mutex_lock(&data->update_lock);
977 regval = pmbus_data2reg(data, sensor, val);
978 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
979 if (ret < 0)
980 rv = ret;
981 else
982 sensor->data = regval;
983 mutex_unlock(&data->update_lock);
984 return rv;
985 }
986
pmbus_show_label(struct device * dev,struct device_attribute * da,char * buf)987 static ssize_t pmbus_show_label(struct device *dev,
988 struct device_attribute *da, char *buf)
989 {
990 struct pmbus_label *label = to_pmbus_label(da);
991
992 return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
993 }
994
pmbus_add_attribute(struct pmbus_data * data,struct attribute * attr)995 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
996 {
997 if (data->num_attributes >= data->max_attributes - 1) {
998 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
999 void *new_attrs = devm_krealloc(data->dev, data->group.attrs,
1000 new_max_attrs * sizeof(void *),
1001 GFP_KERNEL);
1002 if (!new_attrs)
1003 return -ENOMEM;
1004 data->group.attrs = new_attrs;
1005 data->max_attributes = new_max_attrs;
1006 }
1007
1008 data->group.attrs[data->num_attributes++] = attr;
1009 data->group.attrs[data->num_attributes] = NULL;
1010 return 0;
1011 }
1012
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))1013 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
1014 const char *name,
1015 umode_t mode,
1016 ssize_t (*show)(struct device *dev,
1017 struct device_attribute *attr,
1018 char *buf),
1019 ssize_t (*store)(struct device *dev,
1020 struct device_attribute *attr,
1021 const char *buf, size_t count))
1022 {
1023 sysfs_attr_init(&dev_attr->attr);
1024 dev_attr->attr.name = name;
1025 dev_attr->attr.mode = mode;
1026 dev_attr->show = show;
1027 dev_attr->store = store;
1028 }
1029
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)1030 static void pmbus_attr_init(struct sensor_device_attribute *a,
1031 const char *name,
1032 umode_t mode,
1033 ssize_t (*show)(struct device *dev,
1034 struct device_attribute *attr,
1035 char *buf),
1036 ssize_t (*store)(struct device *dev,
1037 struct device_attribute *attr,
1038 const char *buf, size_t count),
1039 int idx)
1040 {
1041 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
1042 a->index = idx;
1043 }
1044
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)1045 static int pmbus_add_boolean(struct pmbus_data *data,
1046 const char *name, const char *type, int seq,
1047 struct pmbus_sensor *s1,
1048 struct pmbus_sensor *s2,
1049 u8 page, u16 reg, u16 mask)
1050 {
1051 struct pmbus_boolean *boolean;
1052 struct sensor_device_attribute *a;
1053
1054 if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n"))
1055 return -EINVAL;
1056
1057 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
1058 if (!boolean)
1059 return -ENOMEM;
1060
1061 a = &boolean->attribute;
1062
1063 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
1064 name, seq, type);
1065 boolean->s1 = s1;
1066 boolean->s2 = s2;
1067 pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL,
1068 pb_reg_to_index(page, reg, mask));
1069
1070 return pmbus_add_attribute(data, &a->dev_attr.attr);
1071 }
1072
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)1073 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
1074 const char *name, const char *type,
1075 int seq, int page, int phase,
1076 int reg,
1077 enum pmbus_sensor_classes class,
1078 bool update, bool readonly,
1079 bool convert)
1080 {
1081 struct pmbus_sensor *sensor;
1082 struct device_attribute *a;
1083
1084 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
1085 if (!sensor)
1086 return NULL;
1087 a = &sensor->attribute;
1088
1089 if (type)
1090 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
1091 name, seq, type);
1092 else
1093 snprintf(sensor->name, sizeof(sensor->name), "%s%d",
1094 name, seq);
1095
1096 if (data->flags & PMBUS_WRITE_PROTECTED)
1097 readonly = true;
1098
1099 sensor->page = page;
1100 sensor->phase = phase;
1101 sensor->reg = reg;
1102 sensor->class = class;
1103 sensor->update = update;
1104 sensor->convert = convert;
1105 sensor->data = -ENODATA;
1106 pmbus_dev_attr_init(a, sensor->name,
1107 readonly ? 0444 : 0644,
1108 pmbus_show_sensor, pmbus_set_sensor);
1109
1110 if (pmbus_add_attribute(data, &a->attr))
1111 return NULL;
1112
1113 sensor->next = data->sensors;
1114 data->sensors = sensor;
1115
1116 return sensor;
1117 }
1118
pmbus_add_label(struct pmbus_data * data,const char * name,int seq,const char * lstring,int index,int phase)1119 static int pmbus_add_label(struct pmbus_data *data,
1120 const char *name, int seq,
1121 const char *lstring, int index, int phase)
1122 {
1123 struct pmbus_label *label;
1124 struct device_attribute *a;
1125
1126 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
1127 if (!label)
1128 return -ENOMEM;
1129
1130 a = &label->attribute;
1131
1132 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
1133 if (!index) {
1134 if (phase == 0xff)
1135 strncpy(label->label, lstring,
1136 sizeof(label->label) - 1);
1137 else
1138 snprintf(label->label, sizeof(label->label), "%s.%d",
1139 lstring, phase);
1140 } else {
1141 if (phase == 0xff)
1142 snprintf(label->label, sizeof(label->label), "%s%d",
1143 lstring, index);
1144 else
1145 snprintf(label->label, sizeof(label->label), "%s%d.%d",
1146 lstring, index, phase);
1147 }
1148
1149 pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL);
1150 return pmbus_add_attribute(data, &a->attr);
1151 }
1152
1153 /*
1154 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1155 */
1156
1157 /*
1158 * The pmbus_limit_attr structure describes a single limit attribute
1159 * and its associated alarm attribute.
1160 */
1161 struct pmbus_limit_attr {
1162 u16 reg; /* Limit register */
1163 u16 sbit; /* Alarm attribute status bit */
1164 bool update; /* True if register needs updates */
1165 bool low; /* True if low limit; for limits with compare
1166 functions only */
1167 const char *attr; /* Attribute name */
1168 const char *alarm; /* Alarm attribute name */
1169 };
1170
1171 /*
1172 * The pmbus_sensor_attr structure describes one sensor attribute. This
1173 * description includes a reference to the associated limit attributes.
1174 */
1175 struct pmbus_sensor_attr {
1176 u16 reg; /* sensor register */
1177 u16 gbit; /* generic status bit */
1178 u8 nlimit; /* # of limit registers */
1179 enum pmbus_sensor_classes class;/* sensor class */
1180 const char *label; /* sensor label */
1181 bool paged; /* true if paged sensor */
1182 bool update; /* true if update needed */
1183 bool compare; /* true if compare function needed */
1184 u32 func; /* sensor mask */
1185 u32 sfunc; /* sensor status mask */
1186 int sreg; /* status register */
1187 const struct pmbus_limit_attr *limit;/* limit registers */
1188 };
1189
1190 /*
1191 * Add a set of limit attributes and, if supported, the associated
1192 * alarm attributes.
1193 * returns 0 if no alarm register found, 1 if an alarm register was found,
1194 * < 0 on errors.
1195 */
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)1196 static int pmbus_add_limit_attrs(struct i2c_client *client,
1197 struct pmbus_data *data,
1198 const struct pmbus_driver_info *info,
1199 const char *name, int index, int page,
1200 struct pmbus_sensor *base,
1201 const struct pmbus_sensor_attr *attr)
1202 {
1203 const struct pmbus_limit_attr *l = attr->limit;
1204 int nlimit = attr->nlimit;
1205 int have_alarm = 0;
1206 int i, ret;
1207 struct pmbus_sensor *curr;
1208
1209 for (i = 0; i < nlimit; i++) {
1210 if (pmbus_check_word_register(client, page, l->reg)) {
1211 curr = pmbus_add_sensor(data, name, l->attr, index,
1212 page, 0xff, l->reg, attr->class,
1213 attr->update || l->update,
1214 false, true);
1215 if (!curr)
1216 return -ENOMEM;
1217 if (l->sbit && (info->func[page] & attr->sfunc)) {
1218 ret = pmbus_add_boolean(data, name,
1219 l->alarm, index,
1220 attr->compare ? l->low ? curr : base
1221 : NULL,
1222 attr->compare ? l->low ? base : curr
1223 : NULL,
1224 page, attr->sreg, l->sbit);
1225 if (ret)
1226 return ret;
1227 have_alarm = 1;
1228 }
1229 }
1230 l++;
1231 }
1232 return have_alarm;
1233 }
1234
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)1235 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1236 struct pmbus_data *data,
1237 const struct pmbus_driver_info *info,
1238 const char *name,
1239 int index, int page, int phase,
1240 const struct pmbus_sensor_attr *attr,
1241 bool paged)
1242 {
1243 struct pmbus_sensor *base;
1244 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */
1245 int ret;
1246
1247 if (attr->label) {
1248 ret = pmbus_add_label(data, name, index, attr->label,
1249 paged ? page + 1 : 0, phase);
1250 if (ret)
1251 return ret;
1252 }
1253 base = pmbus_add_sensor(data, name, "input", index, page, phase,
1254 attr->reg, attr->class, true, true, true);
1255 if (!base)
1256 return -ENOMEM;
1257 /* No limit and alarm attributes for phase specific sensors */
1258 if (attr->sfunc && phase == 0xff) {
1259 ret = pmbus_add_limit_attrs(client, data, info, name,
1260 index, page, base, attr);
1261 if (ret < 0)
1262 return ret;
1263 /*
1264 * Add generic alarm attribute only if there are no individual
1265 * alarm attributes, if there is a global alarm bit, and if
1266 * the generic status register (word or byte, depending on
1267 * which global bit is set) for this page is accessible.
1268 */
1269 if (!ret && attr->gbit &&
1270 (!upper || (upper && data->has_status_word)) &&
1271 pmbus_check_status_register(client, page)) {
1272 ret = pmbus_add_boolean(data, name, "alarm", index,
1273 NULL, NULL,
1274 page, PMBUS_STATUS_WORD,
1275 attr->gbit);
1276 if (ret)
1277 return ret;
1278 }
1279 }
1280 return 0;
1281 }
1282
pmbus_sensor_is_paged(const struct pmbus_driver_info * info,const struct pmbus_sensor_attr * attr)1283 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
1284 const struct pmbus_sensor_attr *attr)
1285 {
1286 int p;
1287
1288 if (attr->paged)
1289 return true;
1290
1291 /*
1292 * Some attributes may be present on more than one page despite
1293 * not being marked with the paged attribute. If that is the case,
1294 * then treat the sensor as being paged and add the page suffix to the
1295 * attribute name.
1296 * We don't just add the paged attribute to all such attributes, in
1297 * order to maintain the un-suffixed labels in the case where the
1298 * attribute is only on page 0.
1299 */
1300 for (p = 1; p < info->pages; p++) {
1301 if (info->func[p] & attr->func)
1302 return true;
1303 }
1304 return false;
1305 }
1306
pmbus_add_sensor_attrs(struct i2c_client * client,struct pmbus_data * data,const char * name,const struct pmbus_sensor_attr * attrs,int nattrs)1307 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1308 struct pmbus_data *data,
1309 const char *name,
1310 const struct pmbus_sensor_attr *attrs,
1311 int nattrs)
1312 {
1313 const struct pmbus_driver_info *info = data->info;
1314 int index, i;
1315 int ret;
1316
1317 index = 1;
1318 for (i = 0; i < nattrs; i++) {
1319 int page, pages;
1320 bool paged = pmbus_sensor_is_paged(info, attrs);
1321
1322 pages = paged ? info->pages : 1;
1323 for (page = 0; page < pages; page++) {
1324 if (!(info->func[page] & attrs->func))
1325 continue;
1326 ret = pmbus_add_sensor_attrs_one(client, data, info,
1327 name, index, page,
1328 0xff, attrs, paged);
1329 if (ret)
1330 return ret;
1331 index++;
1332 if (info->phases[page]) {
1333 int phase;
1334
1335 for (phase = 0; phase < info->phases[page];
1336 phase++) {
1337 if (!(info->pfunc[phase] & attrs->func))
1338 continue;
1339 ret = pmbus_add_sensor_attrs_one(client,
1340 data, info, name, index, page,
1341 phase, attrs, paged);
1342 if (ret)
1343 return ret;
1344 index++;
1345 }
1346 }
1347 }
1348 attrs++;
1349 }
1350 return 0;
1351 }
1352
1353 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1354 {
1355 .reg = PMBUS_VIN_UV_WARN_LIMIT,
1356 .attr = "min",
1357 .alarm = "min_alarm",
1358 .sbit = PB_VOLTAGE_UV_WARNING,
1359 }, {
1360 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
1361 .attr = "lcrit",
1362 .alarm = "lcrit_alarm",
1363 .sbit = PB_VOLTAGE_UV_FAULT | PB_VOLTAGE_VIN_OFF,
1364 }, {
1365 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1366 .attr = "max",
1367 .alarm = "max_alarm",
1368 .sbit = PB_VOLTAGE_OV_WARNING,
1369 }, {
1370 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1371 .attr = "crit",
1372 .alarm = "crit_alarm",
1373 .sbit = PB_VOLTAGE_OV_FAULT,
1374 }, {
1375 .reg = PMBUS_VIRT_READ_VIN_AVG,
1376 .update = true,
1377 .attr = "average",
1378 }, {
1379 .reg = PMBUS_VIRT_READ_VIN_MIN,
1380 .update = true,
1381 .attr = "lowest",
1382 }, {
1383 .reg = PMBUS_VIRT_READ_VIN_MAX,
1384 .update = true,
1385 .attr = "highest",
1386 }, {
1387 .reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1388 .attr = "reset_history",
1389 }, {
1390 .reg = PMBUS_MFR_VIN_MIN,
1391 .attr = "rated_min",
1392 }, {
1393 .reg = PMBUS_MFR_VIN_MAX,
1394 .attr = "rated_max",
1395 },
1396 };
1397
1398 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1399 {
1400 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1401 .attr = "min",
1402 .alarm = "min_alarm",
1403 .sbit = PB_VOLTAGE_UV_WARNING,
1404 }, {
1405 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1406 .attr = "lcrit",
1407 .alarm = "lcrit_alarm",
1408 .sbit = PB_VOLTAGE_UV_FAULT,
1409 }, {
1410 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1411 .attr = "max",
1412 .alarm = "max_alarm",
1413 .sbit = PB_VOLTAGE_OV_WARNING,
1414 }, {
1415 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1416 .attr = "crit",
1417 .alarm = "crit_alarm",
1418 .sbit = PB_VOLTAGE_OV_FAULT,
1419 }
1420 };
1421
1422 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1423 {
1424 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1425 .attr = "min",
1426 .alarm = "min_alarm",
1427 .sbit = PB_VOLTAGE_UV_WARNING,
1428 }, {
1429 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1430 .attr = "lcrit",
1431 .alarm = "lcrit_alarm",
1432 .sbit = PB_VOLTAGE_UV_FAULT,
1433 }, {
1434 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1435 .attr = "max",
1436 .alarm = "max_alarm",
1437 .sbit = PB_VOLTAGE_OV_WARNING,
1438 }, {
1439 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1440 .attr = "crit",
1441 .alarm = "crit_alarm",
1442 .sbit = PB_VOLTAGE_OV_FAULT,
1443 }, {
1444 .reg = PMBUS_VIRT_READ_VOUT_AVG,
1445 .update = true,
1446 .attr = "average",
1447 }, {
1448 .reg = PMBUS_VIRT_READ_VOUT_MIN,
1449 .update = true,
1450 .attr = "lowest",
1451 }, {
1452 .reg = PMBUS_VIRT_READ_VOUT_MAX,
1453 .update = true,
1454 .attr = "highest",
1455 }, {
1456 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1457 .attr = "reset_history",
1458 }, {
1459 .reg = PMBUS_MFR_VOUT_MIN,
1460 .attr = "rated_min",
1461 }, {
1462 .reg = PMBUS_MFR_VOUT_MAX,
1463 .attr = "rated_max",
1464 },
1465 };
1466
1467 static const struct pmbus_sensor_attr voltage_attributes[] = {
1468 {
1469 .reg = PMBUS_READ_VIN,
1470 .class = PSC_VOLTAGE_IN,
1471 .label = "vin",
1472 .func = PMBUS_HAVE_VIN,
1473 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1474 .sreg = PMBUS_STATUS_INPUT,
1475 .gbit = PB_STATUS_VIN_UV,
1476 .limit = vin_limit_attrs,
1477 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1478 }, {
1479 .reg = PMBUS_VIRT_READ_VMON,
1480 .class = PSC_VOLTAGE_IN,
1481 .label = "vmon",
1482 .func = PMBUS_HAVE_VMON,
1483 .sfunc = PMBUS_HAVE_STATUS_VMON,
1484 .sreg = PMBUS_VIRT_STATUS_VMON,
1485 .limit = vmon_limit_attrs,
1486 .nlimit = ARRAY_SIZE(vmon_limit_attrs),
1487 }, {
1488 .reg = PMBUS_READ_VCAP,
1489 .class = PSC_VOLTAGE_IN,
1490 .label = "vcap",
1491 .func = PMBUS_HAVE_VCAP,
1492 }, {
1493 .reg = PMBUS_READ_VOUT,
1494 .class = PSC_VOLTAGE_OUT,
1495 .label = "vout",
1496 .paged = true,
1497 .func = PMBUS_HAVE_VOUT,
1498 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1499 .sreg = PMBUS_STATUS_VOUT,
1500 .gbit = PB_STATUS_VOUT_OV,
1501 .limit = vout_limit_attrs,
1502 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1503 }
1504 };
1505
1506 /* Current attributes */
1507
1508 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1509 {
1510 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1511 .attr = "max",
1512 .alarm = "max_alarm",
1513 .sbit = PB_IIN_OC_WARNING,
1514 }, {
1515 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1516 .attr = "crit",
1517 .alarm = "crit_alarm",
1518 .sbit = PB_IIN_OC_FAULT,
1519 }, {
1520 .reg = PMBUS_VIRT_READ_IIN_AVG,
1521 .update = true,
1522 .attr = "average",
1523 }, {
1524 .reg = PMBUS_VIRT_READ_IIN_MIN,
1525 .update = true,
1526 .attr = "lowest",
1527 }, {
1528 .reg = PMBUS_VIRT_READ_IIN_MAX,
1529 .update = true,
1530 .attr = "highest",
1531 }, {
1532 .reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1533 .attr = "reset_history",
1534 }, {
1535 .reg = PMBUS_MFR_IIN_MAX,
1536 .attr = "rated_max",
1537 },
1538 };
1539
1540 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1541 {
1542 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1543 .attr = "max",
1544 .alarm = "max_alarm",
1545 .sbit = PB_IOUT_OC_WARNING,
1546 }, {
1547 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1548 .attr = "lcrit",
1549 .alarm = "lcrit_alarm",
1550 .sbit = PB_IOUT_UC_FAULT,
1551 }, {
1552 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1553 .attr = "crit",
1554 .alarm = "crit_alarm",
1555 .sbit = PB_IOUT_OC_FAULT,
1556 }, {
1557 .reg = PMBUS_VIRT_READ_IOUT_AVG,
1558 .update = true,
1559 .attr = "average",
1560 }, {
1561 .reg = PMBUS_VIRT_READ_IOUT_MIN,
1562 .update = true,
1563 .attr = "lowest",
1564 }, {
1565 .reg = PMBUS_VIRT_READ_IOUT_MAX,
1566 .update = true,
1567 .attr = "highest",
1568 }, {
1569 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1570 .attr = "reset_history",
1571 }, {
1572 .reg = PMBUS_MFR_IOUT_MAX,
1573 .attr = "rated_max",
1574 },
1575 };
1576
1577 static const struct pmbus_sensor_attr current_attributes[] = {
1578 {
1579 .reg = PMBUS_READ_IIN,
1580 .class = PSC_CURRENT_IN,
1581 .label = "iin",
1582 .func = PMBUS_HAVE_IIN,
1583 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1584 .sreg = PMBUS_STATUS_INPUT,
1585 .gbit = PB_STATUS_INPUT,
1586 .limit = iin_limit_attrs,
1587 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1588 }, {
1589 .reg = PMBUS_READ_IOUT,
1590 .class = PSC_CURRENT_OUT,
1591 .label = "iout",
1592 .paged = true,
1593 .func = PMBUS_HAVE_IOUT,
1594 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1595 .sreg = PMBUS_STATUS_IOUT,
1596 .gbit = PB_STATUS_IOUT_OC,
1597 .limit = iout_limit_attrs,
1598 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1599 }
1600 };
1601
1602 /* Power attributes */
1603
1604 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1605 {
1606 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1607 .attr = "max",
1608 .alarm = "alarm",
1609 .sbit = PB_PIN_OP_WARNING,
1610 }, {
1611 .reg = PMBUS_VIRT_READ_PIN_AVG,
1612 .update = true,
1613 .attr = "average",
1614 }, {
1615 .reg = PMBUS_VIRT_READ_PIN_MIN,
1616 .update = true,
1617 .attr = "input_lowest",
1618 }, {
1619 .reg = PMBUS_VIRT_READ_PIN_MAX,
1620 .update = true,
1621 .attr = "input_highest",
1622 }, {
1623 .reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1624 .attr = "reset_history",
1625 }, {
1626 .reg = PMBUS_MFR_PIN_MAX,
1627 .attr = "rated_max",
1628 },
1629 };
1630
1631 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1632 {
1633 .reg = PMBUS_POUT_MAX,
1634 .attr = "cap",
1635 .alarm = "cap_alarm",
1636 .sbit = PB_POWER_LIMITING,
1637 }, {
1638 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1639 .attr = "max",
1640 .alarm = "max_alarm",
1641 .sbit = PB_POUT_OP_WARNING,
1642 }, {
1643 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1644 .attr = "crit",
1645 .alarm = "crit_alarm",
1646 .sbit = PB_POUT_OP_FAULT,
1647 }, {
1648 .reg = PMBUS_VIRT_READ_POUT_AVG,
1649 .update = true,
1650 .attr = "average",
1651 }, {
1652 .reg = PMBUS_VIRT_READ_POUT_MIN,
1653 .update = true,
1654 .attr = "input_lowest",
1655 }, {
1656 .reg = PMBUS_VIRT_READ_POUT_MAX,
1657 .update = true,
1658 .attr = "input_highest",
1659 }, {
1660 .reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1661 .attr = "reset_history",
1662 }, {
1663 .reg = PMBUS_MFR_POUT_MAX,
1664 .attr = "rated_max",
1665 },
1666 };
1667
1668 static const struct pmbus_sensor_attr power_attributes[] = {
1669 {
1670 .reg = PMBUS_READ_PIN,
1671 .class = PSC_POWER,
1672 .label = "pin",
1673 .func = PMBUS_HAVE_PIN,
1674 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1675 .sreg = PMBUS_STATUS_INPUT,
1676 .gbit = PB_STATUS_INPUT,
1677 .limit = pin_limit_attrs,
1678 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1679 }, {
1680 .reg = PMBUS_READ_POUT,
1681 .class = PSC_POWER,
1682 .label = "pout",
1683 .paged = true,
1684 .func = PMBUS_HAVE_POUT,
1685 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1686 .sreg = PMBUS_STATUS_IOUT,
1687 .limit = pout_limit_attrs,
1688 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1689 }
1690 };
1691
1692 /* Temperature atributes */
1693
1694 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1695 {
1696 .reg = PMBUS_UT_WARN_LIMIT,
1697 .low = true,
1698 .attr = "min",
1699 .alarm = "min_alarm",
1700 .sbit = PB_TEMP_UT_WARNING,
1701 }, {
1702 .reg = PMBUS_UT_FAULT_LIMIT,
1703 .low = true,
1704 .attr = "lcrit",
1705 .alarm = "lcrit_alarm",
1706 .sbit = PB_TEMP_UT_FAULT,
1707 }, {
1708 .reg = PMBUS_OT_WARN_LIMIT,
1709 .attr = "max",
1710 .alarm = "max_alarm",
1711 .sbit = PB_TEMP_OT_WARNING,
1712 }, {
1713 .reg = PMBUS_OT_FAULT_LIMIT,
1714 .attr = "crit",
1715 .alarm = "crit_alarm",
1716 .sbit = PB_TEMP_OT_FAULT,
1717 }, {
1718 .reg = PMBUS_VIRT_READ_TEMP_MIN,
1719 .attr = "lowest",
1720 }, {
1721 .reg = PMBUS_VIRT_READ_TEMP_AVG,
1722 .attr = "average",
1723 }, {
1724 .reg = PMBUS_VIRT_READ_TEMP_MAX,
1725 .attr = "highest",
1726 }, {
1727 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1728 .attr = "reset_history",
1729 }, {
1730 .reg = PMBUS_MFR_MAX_TEMP_1,
1731 .attr = "rated_max",
1732 },
1733 };
1734
1735 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1736 {
1737 .reg = PMBUS_UT_WARN_LIMIT,
1738 .low = true,
1739 .attr = "min",
1740 .alarm = "min_alarm",
1741 .sbit = PB_TEMP_UT_WARNING,
1742 }, {
1743 .reg = PMBUS_UT_FAULT_LIMIT,
1744 .low = true,
1745 .attr = "lcrit",
1746 .alarm = "lcrit_alarm",
1747 .sbit = PB_TEMP_UT_FAULT,
1748 }, {
1749 .reg = PMBUS_OT_WARN_LIMIT,
1750 .attr = "max",
1751 .alarm = "max_alarm",
1752 .sbit = PB_TEMP_OT_WARNING,
1753 }, {
1754 .reg = PMBUS_OT_FAULT_LIMIT,
1755 .attr = "crit",
1756 .alarm = "crit_alarm",
1757 .sbit = PB_TEMP_OT_FAULT,
1758 }, {
1759 .reg = PMBUS_VIRT_READ_TEMP2_MIN,
1760 .attr = "lowest",
1761 }, {
1762 .reg = PMBUS_VIRT_READ_TEMP2_AVG,
1763 .attr = "average",
1764 }, {
1765 .reg = PMBUS_VIRT_READ_TEMP2_MAX,
1766 .attr = "highest",
1767 }, {
1768 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1769 .attr = "reset_history",
1770 }, {
1771 .reg = PMBUS_MFR_MAX_TEMP_2,
1772 .attr = "rated_max",
1773 },
1774 };
1775
1776 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1777 {
1778 .reg = PMBUS_UT_WARN_LIMIT,
1779 .low = true,
1780 .attr = "min",
1781 .alarm = "min_alarm",
1782 .sbit = PB_TEMP_UT_WARNING,
1783 }, {
1784 .reg = PMBUS_UT_FAULT_LIMIT,
1785 .low = true,
1786 .attr = "lcrit",
1787 .alarm = "lcrit_alarm",
1788 .sbit = PB_TEMP_UT_FAULT,
1789 }, {
1790 .reg = PMBUS_OT_WARN_LIMIT,
1791 .attr = "max",
1792 .alarm = "max_alarm",
1793 .sbit = PB_TEMP_OT_WARNING,
1794 }, {
1795 .reg = PMBUS_OT_FAULT_LIMIT,
1796 .attr = "crit",
1797 .alarm = "crit_alarm",
1798 .sbit = PB_TEMP_OT_FAULT,
1799 }, {
1800 .reg = PMBUS_MFR_MAX_TEMP_3,
1801 .attr = "rated_max",
1802 },
1803 };
1804
1805 static const struct pmbus_sensor_attr temp_attributes[] = {
1806 {
1807 .reg = PMBUS_READ_TEMPERATURE_1,
1808 .class = PSC_TEMPERATURE,
1809 .paged = true,
1810 .update = true,
1811 .compare = true,
1812 .func = PMBUS_HAVE_TEMP,
1813 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1814 .sreg = PMBUS_STATUS_TEMPERATURE,
1815 .gbit = PB_STATUS_TEMPERATURE,
1816 .limit = temp_limit_attrs,
1817 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1818 }, {
1819 .reg = PMBUS_READ_TEMPERATURE_2,
1820 .class = PSC_TEMPERATURE,
1821 .paged = true,
1822 .update = true,
1823 .compare = true,
1824 .func = PMBUS_HAVE_TEMP2,
1825 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1826 .sreg = PMBUS_STATUS_TEMPERATURE,
1827 .gbit = PB_STATUS_TEMPERATURE,
1828 .limit = temp_limit_attrs2,
1829 .nlimit = ARRAY_SIZE(temp_limit_attrs2),
1830 }, {
1831 .reg = PMBUS_READ_TEMPERATURE_3,
1832 .class = PSC_TEMPERATURE,
1833 .paged = true,
1834 .update = true,
1835 .compare = true,
1836 .func = PMBUS_HAVE_TEMP3,
1837 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1838 .sreg = PMBUS_STATUS_TEMPERATURE,
1839 .gbit = PB_STATUS_TEMPERATURE,
1840 .limit = temp_limit_attrs3,
1841 .nlimit = ARRAY_SIZE(temp_limit_attrs3),
1842 }
1843 };
1844
1845 static const int pmbus_fan_registers[] = {
1846 PMBUS_READ_FAN_SPEED_1,
1847 PMBUS_READ_FAN_SPEED_2,
1848 PMBUS_READ_FAN_SPEED_3,
1849 PMBUS_READ_FAN_SPEED_4
1850 };
1851
1852 static const int pmbus_fan_status_registers[] = {
1853 PMBUS_STATUS_FAN_12,
1854 PMBUS_STATUS_FAN_12,
1855 PMBUS_STATUS_FAN_34,
1856 PMBUS_STATUS_FAN_34
1857 };
1858
1859 static const u32 pmbus_fan_flags[] = {
1860 PMBUS_HAVE_FAN12,
1861 PMBUS_HAVE_FAN12,
1862 PMBUS_HAVE_FAN34,
1863 PMBUS_HAVE_FAN34
1864 };
1865
1866 static const u32 pmbus_fan_status_flags[] = {
1867 PMBUS_HAVE_STATUS_FAN12,
1868 PMBUS_HAVE_STATUS_FAN12,
1869 PMBUS_HAVE_STATUS_FAN34,
1870 PMBUS_HAVE_STATUS_FAN34
1871 };
1872
1873 /* Fans */
1874
1875 /* 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)1876 static int pmbus_add_fan_ctrl(struct i2c_client *client,
1877 struct pmbus_data *data, int index, int page, int id,
1878 u8 config)
1879 {
1880 struct pmbus_sensor *sensor;
1881
1882 sensor = pmbus_add_sensor(data, "fan", "target", index, page,
1883 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
1884 false, false, true);
1885
1886 if (!sensor)
1887 return -ENOMEM;
1888
1889 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) ||
1890 (data->info->func[page] & PMBUS_HAVE_PWM34)))
1891 return 0;
1892
1893 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
1894 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM,
1895 false, false, true);
1896
1897 if (!sensor)
1898 return -ENOMEM;
1899
1900 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
1901 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
1902 true, false, false);
1903
1904 if (!sensor)
1905 return -ENOMEM;
1906
1907 return 0;
1908 }
1909
pmbus_add_fan_attributes(struct i2c_client * client,struct pmbus_data * data)1910 static int pmbus_add_fan_attributes(struct i2c_client *client,
1911 struct pmbus_data *data)
1912 {
1913 const struct pmbus_driver_info *info = data->info;
1914 int index = 1;
1915 int page;
1916 int ret;
1917
1918 for (page = 0; page < info->pages; page++) {
1919 int f;
1920
1921 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1922 int regval;
1923
1924 if (!(info->func[page] & pmbus_fan_flags[f]))
1925 break;
1926
1927 if (!pmbus_check_word_register(client, page,
1928 pmbus_fan_registers[f]))
1929 break;
1930
1931 /*
1932 * Skip fan if not installed.
1933 * Each fan configuration register covers multiple fans,
1934 * so we have to do some magic.
1935 */
1936 regval = _pmbus_read_byte_data(client, page,
1937 pmbus_fan_config_registers[f]);
1938 if (regval < 0 ||
1939 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1940 continue;
1941
1942 if (pmbus_add_sensor(data, "fan", "input", index,
1943 page, 0xff, pmbus_fan_registers[f],
1944 PSC_FAN, true, true, true) == NULL)
1945 return -ENOMEM;
1946
1947 /* Fan control */
1948 if (pmbus_check_word_register(client, page,
1949 pmbus_fan_command_registers[f])) {
1950 ret = pmbus_add_fan_ctrl(client, data, index,
1951 page, f, regval);
1952 if (ret < 0)
1953 return ret;
1954 }
1955
1956 /*
1957 * Each fan status register covers multiple fans,
1958 * so we have to do some magic.
1959 */
1960 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1961 pmbus_check_byte_register(client,
1962 page, pmbus_fan_status_registers[f])) {
1963 int reg;
1964
1965 if (f > 1) /* fan 3, 4 */
1966 reg = PMBUS_STATUS_FAN_34;
1967 else
1968 reg = PMBUS_STATUS_FAN_12;
1969 ret = pmbus_add_boolean(data, "fan",
1970 "alarm", index, NULL, NULL, page, reg,
1971 PB_FAN_FAN1_WARNING >> (f & 1));
1972 if (ret)
1973 return ret;
1974 ret = pmbus_add_boolean(data, "fan",
1975 "fault", index, NULL, NULL, page, reg,
1976 PB_FAN_FAN1_FAULT >> (f & 1));
1977 if (ret)
1978 return ret;
1979 }
1980 index++;
1981 }
1982 }
1983 return 0;
1984 }
1985
1986 struct pmbus_samples_attr {
1987 int reg;
1988 char *name;
1989 };
1990
1991 struct pmbus_samples_reg {
1992 int page;
1993 struct pmbus_samples_attr *attr;
1994 struct device_attribute dev_attr;
1995 };
1996
1997 static struct pmbus_samples_attr pmbus_samples_registers[] = {
1998 {
1999 .reg = PMBUS_VIRT_SAMPLES,
2000 .name = "samples",
2001 }, {
2002 .reg = PMBUS_VIRT_IN_SAMPLES,
2003 .name = "in_samples",
2004 }, {
2005 .reg = PMBUS_VIRT_CURR_SAMPLES,
2006 .name = "curr_samples",
2007 }, {
2008 .reg = PMBUS_VIRT_POWER_SAMPLES,
2009 .name = "power_samples",
2010 }, {
2011 .reg = PMBUS_VIRT_TEMP_SAMPLES,
2012 .name = "temp_samples",
2013 }
2014 };
2015
2016 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2017
pmbus_show_samples(struct device * dev,struct device_attribute * devattr,char * buf)2018 static ssize_t pmbus_show_samples(struct device *dev,
2019 struct device_attribute *devattr, char *buf)
2020 {
2021 int val;
2022 struct i2c_client *client = to_i2c_client(dev->parent);
2023 struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2024 struct pmbus_data *data = i2c_get_clientdata(client);
2025
2026 mutex_lock(&data->update_lock);
2027 val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
2028 mutex_unlock(&data->update_lock);
2029 if (val < 0)
2030 return val;
2031
2032 return snprintf(buf, PAGE_SIZE, "%d\n", val);
2033 }
2034
pmbus_set_samples(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)2035 static ssize_t pmbus_set_samples(struct device *dev,
2036 struct device_attribute *devattr,
2037 const char *buf, size_t count)
2038 {
2039 int ret;
2040 long val;
2041 struct i2c_client *client = to_i2c_client(dev->parent);
2042 struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2043 struct pmbus_data *data = i2c_get_clientdata(client);
2044
2045 if (kstrtol(buf, 0, &val) < 0)
2046 return -EINVAL;
2047
2048 mutex_lock(&data->update_lock);
2049 ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val);
2050 mutex_unlock(&data->update_lock);
2051
2052 return ret ? : count;
2053 }
2054
pmbus_add_samples_attr(struct pmbus_data * data,int page,struct pmbus_samples_attr * attr)2055 static int pmbus_add_samples_attr(struct pmbus_data *data, int page,
2056 struct pmbus_samples_attr *attr)
2057 {
2058 struct pmbus_samples_reg *reg;
2059
2060 reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL);
2061 if (!reg)
2062 return -ENOMEM;
2063
2064 reg->attr = attr;
2065 reg->page = page;
2066
2067 pmbus_dev_attr_init(®->dev_attr, attr->name, 0644,
2068 pmbus_show_samples, pmbus_set_samples);
2069
2070 return pmbus_add_attribute(data, ®->dev_attr.attr);
2071 }
2072
pmbus_add_samples_attributes(struct i2c_client * client,struct pmbus_data * data)2073 static int pmbus_add_samples_attributes(struct i2c_client *client,
2074 struct pmbus_data *data)
2075 {
2076 const struct pmbus_driver_info *info = data->info;
2077 int s;
2078
2079 if (!(info->func[0] & PMBUS_HAVE_SAMPLES))
2080 return 0;
2081
2082 for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) {
2083 struct pmbus_samples_attr *attr;
2084 int ret;
2085
2086 attr = &pmbus_samples_registers[s];
2087 if (!pmbus_check_word_register(client, 0, attr->reg))
2088 continue;
2089
2090 ret = pmbus_add_samples_attr(data, 0, attr);
2091 if (ret)
2092 return ret;
2093 }
2094
2095 return 0;
2096 }
2097
pmbus_find_attributes(struct i2c_client * client,struct pmbus_data * data)2098 static int pmbus_find_attributes(struct i2c_client *client,
2099 struct pmbus_data *data)
2100 {
2101 int ret;
2102
2103 /* Voltage sensors */
2104 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
2105 ARRAY_SIZE(voltage_attributes));
2106 if (ret)
2107 return ret;
2108
2109 /* Current sensors */
2110 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
2111 ARRAY_SIZE(current_attributes));
2112 if (ret)
2113 return ret;
2114
2115 /* Power sensors */
2116 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
2117 ARRAY_SIZE(power_attributes));
2118 if (ret)
2119 return ret;
2120
2121 /* Temperature sensors */
2122 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
2123 ARRAY_SIZE(temp_attributes));
2124 if (ret)
2125 return ret;
2126
2127 /* Fans */
2128 ret = pmbus_add_fan_attributes(client, data);
2129 if (ret)
2130 return ret;
2131
2132 ret = pmbus_add_samples_attributes(client, data);
2133 return ret;
2134 }
2135
2136 /*
2137 * Identify chip parameters.
2138 * This function is called for all chips.
2139 */
pmbus_identify_common(struct i2c_client * client,struct pmbus_data * data,int page)2140 static int pmbus_identify_common(struct i2c_client *client,
2141 struct pmbus_data *data, int page)
2142 {
2143 int vout_mode = -1;
2144
2145 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
2146 vout_mode = _pmbus_read_byte_data(client, page,
2147 PMBUS_VOUT_MODE);
2148 if (vout_mode >= 0 && vout_mode != 0xff) {
2149 /*
2150 * Not all chips support the VOUT_MODE command,
2151 * so a failure to read it is not an error.
2152 */
2153 switch (vout_mode >> 5) {
2154 case 0: /* linear mode */
2155 if (data->info->format[PSC_VOLTAGE_OUT] != linear)
2156 return -ENODEV;
2157
2158 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
2159 break;
2160 case 1: /* VID mode */
2161 if (data->info->format[PSC_VOLTAGE_OUT] != vid)
2162 return -ENODEV;
2163 break;
2164 case 2: /* direct mode */
2165 if (data->info->format[PSC_VOLTAGE_OUT] != direct)
2166 return -ENODEV;
2167 break;
2168 default:
2169 return -ENODEV;
2170 }
2171 }
2172
2173 pmbus_clear_fault_page(client, page);
2174 return 0;
2175 }
2176
pmbus_read_status_byte(struct i2c_client * client,int page)2177 static int pmbus_read_status_byte(struct i2c_client *client, int page)
2178 {
2179 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
2180 }
2181
pmbus_read_status_word(struct i2c_client * client,int page)2182 static int pmbus_read_status_word(struct i2c_client *client, int page)
2183 {
2184 return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD);
2185 }
2186
pmbus_init_common(struct i2c_client * client,struct pmbus_data * data,struct pmbus_driver_info * info)2187 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
2188 struct pmbus_driver_info *info)
2189 {
2190 struct device *dev = &client->dev;
2191 int page, ret;
2192
2193 /*
2194 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2195 * to use PMBUS_STATUS_BYTE instead if that is the case.
2196 * Bail out if both registers are not supported.
2197 */
2198 data->read_status = pmbus_read_status_word;
2199 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
2200 if (ret < 0 || ret == 0xffff) {
2201 data->read_status = pmbus_read_status_byte;
2202 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
2203 if (ret < 0 || ret == 0xff) {
2204 dev_err(dev, "PMBus status register not found\n");
2205 return -ENODEV;
2206 }
2207 } else {
2208 data->has_status_word = true;
2209 }
2210
2211 /* Enable PEC if the controller supports it */
2212 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2213 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
2214 client->flags |= I2C_CLIENT_PEC;
2215
2216 /*
2217 * Check if the chip is write protected. If it is, we can not clear
2218 * faults, and we should not try it. Also, in that case, writes into
2219 * limit registers need to be disabled.
2220 */
2221 ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT);
2222 if (ret > 0 && (ret & PB_WP_ANY))
2223 data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK;
2224
2225 if (data->info->pages)
2226 pmbus_clear_faults(client);
2227 else
2228 pmbus_clear_fault_page(client, -1);
2229
2230 if (info->identify) {
2231 ret = (*info->identify)(client, info);
2232 if (ret < 0) {
2233 dev_err(dev, "Chip identification failed\n");
2234 return ret;
2235 }
2236 }
2237
2238 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
2239 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
2240 return -ENODEV;
2241 }
2242
2243 for (page = 0; page < info->pages; page++) {
2244 ret = pmbus_identify_common(client, data, page);
2245 if (ret < 0) {
2246 dev_err(dev, "Failed to identify chip capabilities\n");
2247 return ret;
2248 }
2249 }
2250 return 0;
2251 }
2252
2253 #if IS_ENABLED(CONFIG_REGULATOR)
pmbus_regulator_is_enabled(struct regulator_dev * rdev)2254 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
2255 {
2256 struct device *dev = rdev_get_dev(rdev);
2257 struct i2c_client *client = to_i2c_client(dev->parent);
2258 struct pmbus_data *data = i2c_get_clientdata(client);
2259 u8 page = rdev_get_id(rdev);
2260 int ret;
2261
2262 mutex_lock(&data->update_lock);
2263 ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
2264 mutex_unlock(&data->update_lock);
2265
2266 if (ret < 0)
2267 return ret;
2268
2269 return !!(ret & PB_OPERATION_CONTROL_ON);
2270 }
2271
_pmbus_regulator_on_off(struct regulator_dev * rdev,bool enable)2272 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
2273 {
2274 struct device *dev = rdev_get_dev(rdev);
2275 struct i2c_client *client = to_i2c_client(dev->parent);
2276 struct pmbus_data *data = i2c_get_clientdata(client);
2277 u8 page = rdev_get_id(rdev);
2278 int ret;
2279
2280 mutex_lock(&data->update_lock);
2281 ret = pmbus_update_byte_data(client, page, PMBUS_OPERATION,
2282 PB_OPERATION_CONTROL_ON,
2283 enable ? PB_OPERATION_CONTROL_ON : 0);
2284 mutex_unlock(&data->update_lock);
2285
2286 return ret;
2287 }
2288
pmbus_regulator_enable(struct regulator_dev * rdev)2289 static int pmbus_regulator_enable(struct regulator_dev *rdev)
2290 {
2291 return _pmbus_regulator_on_off(rdev, 1);
2292 }
2293
pmbus_regulator_disable(struct regulator_dev * rdev)2294 static int pmbus_regulator_disable(struct regulator_dev *rdev)
2295 {
2296 return _pmbus_regulator_on_off(rdev, 0);
2297 }
2298
2299 const struct regulator_ops pmbus_regulator_ops = {
2300 .enable = pmbus_regulator_enable,
2301 .disable = pmbus_regulator_disable,
2302 .is_enabled = pmbus_regulator_is_enabled,
2303 };
2304 EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
2305
pmbus_regulator_register(struct pmbus_data * data)2306 static int pmbus_regulator_register(struct pmbus_data *data)
2307 {
2308 struct device *dev = data->dev;
2309 const struct pmbus_driver_info *info = data->info;
2310 const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2311 struct regulator_dev *rdev;
2312 int i;
2313
2314 for (i = 0; i < info->num_regulators; i++) {
2315 struct regulator_config config = { };
2316
2317 config.dev = dev;
2318 config.driver_data = data;
2319
2320 if (pdata && pdata->reg_init_data)
2321 config.init_data = &pdata->reg_init_data[i];
2322
2323 rdev = devm_regulator_register(dev, &info->reg_desc[i],
2324 &config);
2325 if (IS_ERR(rdev)) {
2326 dev_err(dev, "Failed to register %s regulator\n",
2327 info->reg_desc[i].name);
2328 return PTR_ERR(rdev);
2329 }
2330 }
2331
2332 return 0;
2333 }
2334 #else
pmbus_regulator_register(struct pmbus_data * data)2335 static int pmbus_regulator_register(struct pmbus_data *data)
2336 {
2337 return 0;
2338 }
2339 #endif
2340
2341 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */
2342
2343 #if IS_ENABLED(CONFIG_DEBUG_FS)
pmbus_debugfs_get(void * data,u64 * val)2344 static int pmbus_debugfs_get(void *data, u64 *val)
2345 {
2346 int rc;
2347 struct pmbus_debugfs_entry *entry = data;
2348
2349 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
2350 if (rc < 0)
2351 return rc;
2352
2353 *val = rc;
2354
2355 return 0;
2356 }
2357 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
2358 "0x%02llx\n");
2359
pmbus_debugfs_get_status(void * data,u64 * val)2360 static int pmbus_debugfs_get_status(void *data, u64 *val)
2361 {
2362 int rc;
2363 struct pmbus_debugfs_entry *entry = data;
2364 struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
2365
2366 rc = pdata->read_status(entry->client, entry->page);
2367 if (rc < 0)
2368 return rc;
2369
2370 *val = rc;
2371
2372 return 0;
2373 }
2374 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
2375 NULL, "0x%04llx\n");
2376
pmbus_debugfs_get_pec(void * data,u64 * val)2377 static int pmbus_debugfs_get_pec(void *data, u64 *val)
2378 {
2379 struct i2c_client *client = data;
2380
2381 *val = !!(client->flags & I2C_CLIENT_PEC);
2382
2383 return 0;
2384 }
2385
pmbus_debugfs_set_pec(void * data,u64 val)2386 static int pmbus_debugfs_set_pec(void *data, u64 val)
2387 {
2388 int rc;
2389 struct i2c_client *client = data;
2390
2391 if (!val) {
2392 client->flags &= ~I2C_CLIENT_PEC;
2393 return 0;
2394 }
2395
2396 if (val != 1)
2397 return -EINVAL;
2398
2399 rc = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2400 if (rc < 0)
2401 return rc;
2402
2403 if (!(rc & PB_CAPABILITY_ERROR_CHECK))
2404 return -EOPNOTSUPP;
2405
2406 client->flags |= I2C_CLIENT_PEC;
2407
2408 return 0;
2409 }
2410 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_pec, pmbus_debugfs_get_pec,
2411 pmbus_debugfs_set_pec, "%llu\n");
2412
pmbus_init_debugfs(struct i2c_client * client,struct pmbus_data * data)2413 static int pmbus_init_debugfs(struct i2c_client *client,
2414 struct pmbus_data *data)
2415 {
2416 int i, idx = 0;
2417 char name[PMBUS_NAME_SIZE];
2418 struct pmbus_debugfs_entry *entries;
2419
2420 if (!pmbus_debugfs_dir)
2421 return -ENODEV;
2422
2423 /*
2424 * Create the debugfs directory for this device. Use the hwmon device
2425 * name to avoid conflicts (hwmon numbers are globally unique).
2426 */
2427 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
2428 pmbus_debugfs_dir);
2429 if (IS_ERR_OR_NULL(data->debugfs)) {
2430 data->debugfs = NULL;
2431 return -ENODEV;
2432 }
2433
2434 /* Allocate the max possible entries we need. */
2435 entries = devm_kcalloc(data->dev,
2436 data->info->pages * 10, sizeof(*entries),
2437 GFP_KERNEL);
2438 if (!entries)
2439 return -ENOMEM;
2440
2441 debugfs_create_file("pec", 0664, data->debugfs, client,
2442 &pmbus_debugfs_ops_pec);
2443
2444 for (i = 0; i < data->info->pages; ++i) {
2445 /* Check accessibility of status register if it's not page 0 */
2446 if (!i || pmbus_check_status_register(client, i)) {
2447 /* No need to set reg as we have special read op. */
2448 entries[idx].client = client;
2449 entries[idx].page = i;
2450 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
2451 debugfs_create_file(name, 0444, data->debugfs,
2452 &entries[idx++],
2453 &pmbus_debugfs_ops_status);
2454 }
2455
2456 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
2457 entries[idx].client = client;
2458 entries[idx].page = i;
2459 entries[idx].reg = PMBUS_STATUS_VOUT;
2460 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
2461 debugfs_create_file(name, 0444, data->debugfs,
2462 &entries[idx++],
2463 &pmbus_debugfs_ops);
2464 }
2465
2466 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
2467 entries[idx].client = client;
2468 entries[idx].page = i;
2469 entries[idx].reg = PMBUS_STATUS_IOUT;
2470 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
2471 debugfs_create_file(name, 0444, data->debugfs,
2472 &entries[idx++],
2473 &pmbus_debugfs_ops);
2474 }
2475
2476 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
2477 entries[idx].client = client;
2478 entries[idx].page = i;
2479 entries[idx].reg = PMBUS_STATUS_INPUT;
2480 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
2481 debugfs_create_file(name, 0444, data->debugfs,
2482 &entries[idx++],
2483 &pmbus_debugfs_ops);
2484 }
2485
2486 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
2487 entries[idx].client = client;
2488 entries[idx].page = i;
2489 entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
2490 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
2491 debugfs_create_file(name, 0444, data->debugfs,
2492 &entries[idx++],
2493 &pmbus_debugfs_ops);
2494 }
2495
2496 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
2497 entries[idx].client = client;
2498 entries[idx].page = i;
2499 entries[idx].reg = PMBUS_STATUS_CML;
2500 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
2501 debugfs_create_file(name, 0444, data->debugfs,
2502 &entries[idx++],
2503 &pmbus_debugfs_ops);
2504 }
2505
2506 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
2507 entries[idx].client = client;
2508 entries[idx].page = i;
2509 entries[idx].reg = PMBUS_STATUS_OTHER;
2510 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
2511 debugfs_create_file(name, 0444, data->debugfs,
2512 &entries[idx++],
2513 &pmbus_debugfs_ops);
2514 }
2515
2516 if (pmbus_check_byte_register(client, i,
2517 PMBUS_STATUS_MFR_SPECIFIC)) {
2518 entries[idx].client = client;
2519 entries[idx].page = i;
2520 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
2521 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
2522 debugfs_create_file(name, 0444, data->debugfs,
2523 &entries[idx++],
2524 &pmbus_debugfs_ops);
2525 }
2526
2527 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
2528 entries[idx].client = client;
2529 entries[idx].page = i;
2530 entries[idx].reg = PMBUS_STATUS_FAN_12;
2531 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
2532 debugfs_create_file(name, 0444, data->debugfs,
2533 &entries[idx++],
2534 &pmbus_debugfs_ops);
2535 }
2536
2537 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
2538 entries[idx].client = client;
2539 entries[idx].page = i;
2540 entries[idx].reg = PMBUS_STATUS_FAN_34;
2541 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
2542 debugfs_create_file(name, 0444, data->debugfs,
2543 &entries[idx++],
2544 &pmbus_debugfs_ops);
2545 }
2546 }
2547
2548 return 0;
2549 }
2550 #else
pmbus_init_debugfs(struct i2c_client * client,struct pmbus_data * data)2551 static int pmbus_init_debugfs(struct i2c_client *client,
2552 struct pmbus_data *data)
2553 {
2554 return 0;
2555 }
2556 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
2557
pmbus_do_probe(struct i2c_client * client,struct pmbus_driver_info * info)2558 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info)
2559 {
2560 struct device *dev = &client->dev;
2561 const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2562 struct pmbus_data *data;
2563 size_t groups_num = 0;
2564 int ret;
2565
2566 if (!info)
2567 return -ENODEV;
2568
2569 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
2570 | I2C_FUNC_SMBUS_BYTE_DATA
2571 | I2C_FUNC_SMBUS_WORD_DATA))
2572 return -ENODEV;
2573
2574 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
2575 if (!data)
2576 return -ENOMEM;
2577
2578 if (info->groups)
2579 while (info->groups[groups_num])
2580 groups_num++;
2581
2582 data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
2583 GFP_KERNEL);
2584 if (!data->groups)
2585 return -ENOMEM;
2586
2587 i2c_set_clientdata(client, data);
2588 mutex_init(&data->update_lock);
2589 data->dev = dev;
2590
2591 if (pdata)
2592 data->flags = pdata->flags;
2593 data->info = info;
2594 data->currpage = -1;
2595 data->currphase = -1;
2596
2597 ret = pmbus_init_common(client, data, info);
2598 if (ret < 0)
2599 return ret;
2600
2601 ret = pmbus_find_attributes(client, data);
2602 if (ret)
2603 return ret;
2604
2605 /*
2606 * If there are no attributes, something is wrong.
2607 * Bail out instead of trying to register nothing.
2608 */
2609 if (!data->num_attributes) {
2610 dev_err(dev, "No attributes found\n");
2611 return -ENODEV;
2612 }
2613
2614 data->groups[0] = &data->group;
2615 memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
2616 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2617 client->name, data, data->groups);
2618 if (IS_ERR(data->hwmon_dev)) {
2619 dev_err(dev, "Failed to register hwmon device\n");
2620 return PTR_ERR(data->hwmon_dev);
2621 }
2622
2623 ret = pmbus_regulator_register(data);
2624 if (ret)
2625 return ret;
2626
2627 ret = pmbus_init_debugfs(client, data);
2628 if (ret)
2629 dev_warn(dev, "Failed to register debugfs\n");
2630
2631 return 0;
2632 }
2633 EXPORT_SYMBOL_GPL(pmbus_do_probe);
2634
pmbus_do_remove(struct i2c_client * client)2635 int pmbus_do_remove(struct i2c_client *client)
2636 {
2637 struct pmbus_data *data = i2c_get_clientdata(client);
2638
2639 debugfs_remove_recursive(data->debugfs);
2640
2641 return 0;
2642 }
2643 EXPORT_SYMBOL_GPL(pmbus_do_remove);
2644
pmbus_get_debugfs_dir(struct i2c_client * client)2645 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
2646 {
2647 struct pmbus_data *data = i2c_get_clientdata(client);
2648
2649 return data->debugfs;
2650 }
2651 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir);
2652
pmbus_core_init(void)2653 static int __init pmbus_core_init(void)
2654 {
2655 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
2656 if (IS_ERR(pmbus_debugfs_dir))
2657 pmbus_debugfs_dir = NULL;
2658
2659 return 0;
2660 }
2661
pmbus_core_exit(void)2662 static void __exit pmbus_core_exit(void)
2663 {
2664 debugfs_remove_recursive(pmbus_debugfs_dir);
2665 }
2666
2667 module_init(pmbus_core_init);
2668 module_exit(pmbus_core_exit);
2669
2670 MODULE_AUTHOR("Guenter Roeck");
2671 MODULE_DESCRIPTION("PMBus core driver");
2672 MODULE_LICENSE("GPL");
2673