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