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