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