xref: /openbmc/linux/drivers/hwmon/pmbus/ucd9000.c (revision 22d55f02)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for UCD90xxx Sequencer and System Health
4  * Controller series
5  *
6  * Copyright (C) 2011 Ericsson AB.
7  */
8 
9 #include <linux/debugfs.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/pmbus.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/driver.h>
20 #include "pmbus.h"
21 
22 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd9090, ucd90910 };
23 
24 #define UCD9000_MONITOR_CONFIG		0xd5
25 #define UCD9000_NUM_PAGES		0xd6
26 #define UCD9000_FAN_CONFIG_INDEX	0xe7
27 #define UCD9000_FAN_CONFIG		0xe8
28 #define UCD9000_MFR_STATUS		0xf3
29 #define UCD9000_GPIO_SELECT		0xfa
30 #define UCD9000_GPIO_CONFIG		0xfb
31 #define UCD9000_DEVICE_ID		0xfd
32 
33 /* GPIO CONFIG bits */
34 #define UCD9000_GPIO_CONFIG_ENABLE	BIT(0)
35 #define UCD9000_GPIO_CONFIG_OUT_ENABLE	BIT(1)
36 #define UCD9000_GPIO_CONFIG_OUT_VALUE	BIT(2)
37 #define UCD9000_GPIO_CONFIG_STATUS	BIT(3)
38 #define UCD9000_GPIO_INPUT		0
39 #define UCD9000_GPIO_OUTPUT		1
40 
41 #define UCD9000_MON_TYPE(x)	(((x) >> 5) & 0x07)
42 #define UCD9000_MON_PAGE(x)	((x) & 0x0f)
43 
44 #define UCD9000_MON_VOLTAGE	1
45 #define UCD9000_MON_TEMPERATURE	2
46 #define UCD9000_MON_CURRENT	3
47 #define UCD9000_MON_VOLTAGE_HW	4
48 
49 #define UCD9000_NUM_FAN		4
50 
51 #define UCD9000_GPIO_NAME_LEN	16
52 #define UCD9090_NUM_GPIOS	23
53 #define UCD901XX_NUM_GPIOS	26
54 #define UCD90910_NUM_GPIOS	26
55 
56 #define UCD9000_DEBUGFS_NAME_LEN	24
57 #define UCD9000_GPI_COUNT		8
58 
59 struct ucd9000_data {
60 	u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
61 	struct pmbus_driver_info info;
62 #ifdef CONFIG_GPIOLIB
63 	struct gpio_chip gpio;
64 #endif
65 	struct dentry *debugfs;
66 };
67 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
68 
69 struct ucd9000_debugfs_entry {
70 	struct i2c_client *client;
71 	u8 index;
72 };
73 
74 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
75 {
76 	int fan_config = 0;
77 	struct ucd9000_data *data
78 	  = to_ucd9000_data(pmbus_get_driver_info(client));
79 
80 	if (data->fan_data[fan][3] & 1)
81 		fan_config |= PB_FAN_2_INSTALLED;   /* Use lower bit position */
82 
83 	/* Pulses/revolution */
84 	fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
85 
86 	return fan_config;
87 }
88 
89 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
90 {
91 	int ret = 0;
92 	int fan_config;
93 
94 	switch (reg) {
95 	case PMBUS_FAN_CONFIG_12:
96 		if (page > 0)
97 			return -ENXIO;
98 
99 		ret = ucd9000_get_fan_config(client, 0);
100 		if (ret < 0)
101 			return ret;
102 		fan_config = ret << 4;
103 		ret = ucd9000_get_fan_config(client, 1);
104 		if (ret < 0)
105 			return ret;
106 		fan_config |= ret;
107 		ret = fan_config;
108 		break;
109 	case PMBUS_FAN_CONFIG_34:
110 		if (page > 0)
111 			return -ENXIO;
112 
113 		ret = ucd9000_get_fan_config(client, 2);
114 		if (ret < 0)
115 			return ret;
116 		fan_config = ret << 4;
117 		ret = ucd9000_get_fan_config(client, 3);
118 		if (ret < 0)
119 			return ret;
120 		fan_config |= ret;
121 		ret = fan_config;
122 		break;
123 	default:
124 		ret = -ENODATA;
125 		break;
126 	}
127 	return ret;
128 }
129 
130 static const struct i2c_device_id ucd9000_id[] = {
131 	{"ucd9000", ucd9000},
132 	{"ucd90120", ucd90120},
133 	{"ucd90124", ucd90124},
134 	{"ucd90160", ucd90160},
135 	{"ucd9090", ucd9090},
136 	{"ucd90910", ucd90910},
137 	{}
138 };
139 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
140 
141 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
142 	{
143 		.compatible = "ti,ucd9000",
144 		.data = (void *)ucd9000
145 	},
146 	{
147 		.compatible = "ti,ucd90120",
148 		.data = (void *)ucd90120
149 	},
150 	{
151 		.compatible = "ti,ucd90124",
152 		.data = (void *)ucd90124
153 	},
154 	{
155 		.compatible = "ti,ucd90160",
156 		.data = (void *)ucd90160
157 	},
158 	{
159 		.compatible = "ti,ucd9090",
160 		.data = (void *)ucd9090
161 	},
162 	{
163 		.compatible = "ti,ucd90910",
164 		.data = (void *)ucd90910
165 	},
166 	{ },
167 };
168 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
169 
170 #ifdef CONFIG_GPIOLIB
171 static int ucd9000_gpio_read_config(struct i2c_client *client,
172 				    unsigned int offset)
173 {
174 	int ret;
175 
176 	/* No page set required */
177 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
178 	if (ret < 0)
179 		return ret;
180 
181 	return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
182 }
183 
184 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
185 {
186 	struct i2c_client *client  = gpiochip_get_data(gc);
187 	int ret;
188 
189 	ret = ucd9000_gpio_read_config(client, offset);
190 	if (ret < 0)
191 		return ret;
192 
193 	return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
194 }
195 
196 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
197 			     int value)
198 {
199 	struct i2c_client *client = gpiochip_get_data(gc);
200 	int ret;
201 
202 	ret = ucd9000_gpio_read_config(client, offset);
203 	if (ret < 0) {
204 		dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
205 			offset, ret);
206 		return;
207 	}
208 
209 	if (value) {
210 		if (ret & UCD9000_GPIO_CONFIG_STATUS)
211 			return;
212 
213 		ret |= UCD9000_GPIO_CONFIG_STATUS;
214 	} else {
215 		if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
216 			return;
217 
218 		ret &= ~UCD9000_GPIO_CONFIG_STATUS;
219 	}
220 
221 	ret |= UCD9000_GPIO_CONFIG_ENABLE;
222 
223 	/* Page set not required */
224 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
225 	if (ret < 0) {
226 		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
227 			offset, ret);
228 		return;
229 	}
230 
231 	ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
232 
233 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
234 	if (ret < 0)
235 		dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
236 			offset, ret);
237 }
238 
239 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
240 				      unsigned int offset)
241 {
242 	struct i2c_client *client = gpiochip_get_data(gc);
243 	int ret;
244 
245 	ret = ucd9000_gpio_read_config(client, offset);
246 	if (ret < 0)
247 		return ret;
248 
249 	return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
250 }
251 
252 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
253 				      unsigned int offset, bool direction_out,
254 				      int requested_out)
255 {
256 	struct i2c_client *client = gpiochip_get_data(gc);
257 	int ret, config, out_val;
258 
259 	ret = ucd9000_gpio_read_config(client, offset);
260 	if (ret < 0)
261 		return ret;
262 
263 	if (direction_out) {
264 		out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
265 
266 		if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
267 			if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
268 				return 0;
269 		} else {
270 			ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
271 		}
272 
273 		if (out_val)
274 			ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
275 		else
276 			ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
277 
278 	} else {
279 		if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
280 			return 0;
281 
282 		ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
283 	}
284 
285 	ret |= UCD9000_GPIO_CONFIG_ENABLE;
286 	config = ret;
287 
288 	/* Page set not required */
289 	ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
290 	if (ret < 0)
291 		return ret;
292 
293 	config &= ~UCD9000_GPIO_CONFIG_ENABLE;
294 
295 	return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
296 }
297 
298 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
299 					unsigned int offset)
300 {
301 	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
302 }
303 
304 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
305 					 unsigned int offset, int val)
306 {
307 	return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
308 					  val);
309 }
310 
311 static void ucd9000_probe_gpio(struct i2c_client *client,
312 			       const struct i2c_device_id *mid,
313 			       struct ucd9000_data *data)
314 {
315 	int rc;
316 
317 	switch (mid->driver_data) {
318 	case ucd9090:
319 		data->gpio.ngpio = UCD9090_NUM_GPIOS;
320 		break;
321 	case ucd90120:
322 	case ucd90124:
323 	case ucd90160:
324 		data->gpio.ngpio = UCD901XX_NUM_GPIOS;
325 		break;
326 	case ucd90910:
327 		data->gpio.ngpio = UCD90910_NUM_GPIOS;
328 		break;
329 	default:
330 		return; /* GPIO support is optional. */
331 	}
332 
333 	/*
334 	 * Pinmux support has not been added to the new gpio_chip.
335 	 * This support should be added when possible given the mux
336 	 * behavior of these IO devices.
337 	 */
338 	data->gpio.label = client->name;
339 	data->gpio.get_direction = ucd9000_gpio_get_direction;
340 	data->gpio.direction_input = ucd9000_gpio_direction_input;
341 	data->gpio.direction_output = ucd9000_gpio_direction_output;
342 	data->gpio.get = ucd9000_gpio_get;
343 	data->gpio.set = ucd9000_gpio_set;
344 	data->gpio.can_sleep = true;
345 	data->gpio.base = -1;
346 	data->gpio.parent = &client->dev;
347 
348 	rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
349 	if (rc)
350 		dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
351 }
352 #else
353 static void ucd9000_probe_gpio(struct i2c_client *client,
354 			       const struct i2c_device_id *mid,
355 			       struct ucd9000_data *data)
356 {
357 }
358 #endif /* CONFIG_GPIOLIB */
359 
360 #ifdef CONFIG_DEBUG_FS
361 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
362 {
363 	int ret = pmbus_set_page(client, 0);
364 
365 	if (ret < 0)
366 		return ret;
367 
368 	return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
369 }
370 
371 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
372 {
373 	struct ucd9000_debugfs_entry *entry = data;
374 	struct i2c_client *client = entry->client;
375 	u8 buffer[I2C_SMBUS_BLOCK_MAX];
376 	int ret;
377 
378 	ret = ucd9000_get_mfr_status(client, buffer);
379 	if (ret < 0)
380 		return ret;
381 
382 	/*
383 	 * Attribute only created for devices with gpi fault bits at bits
384 	 * 16-23, which is the second byte of the response.
385 	 */
386 	*val = !!(buffer[1] & BIT(entry->index));
387 
388 	return 0;
389 }
390 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
391 			 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
392 
393 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
394 					       char __user *buf, size_t count,
395 					       loff_t *ppos)
396 {
397 	struct i2c_client *client = file->private_data;
398 	u8 buffer[I2C_SMBUS_BLOCK_MAX];
399 	char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
400 	char *res;
401 	int rc;
402 
403 	rc = ucd9000_get_mfr_status(client, buffer);
404 	if (rc < 0)
405 		return rc;
406 
407 	res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
408 	*res++ = '\n';
409 	*res = 0;
410 
411 	return simple_read_from_buffer(buf, count, ppos, str, res - str);
412 }
413 
414 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
415 	.llseek = noop_llseek,
416 	.read = ucd9000_debugfs_read_mfr_status,
417 	.open = simple_open,
418 };
419 
420 static int ucd9000_init_debugfs(struct i2c_client *client,
421 				const struct i2c_device_id *mid,
422 				struct ucd9000_data *data)
423 {
424 	struct dentry *debugfs;
425 	struct ucd9000_debugfs_entry *entries;
426 	int i;
427 	char name[UCD9000_DEBUGFS_NAME_LEN];
428 
429 	debugfs = pmbus_get_debugfs_dir(client);
430 	if (!debugfs)
431 		return -ENOENT;
432 
433 	data->debugfs = debugfs_create_dir(client->name, debugfs);
434 	if (!data->debugfs)
435 		return -ENOENT;
436 
437 	/*
438 	 * Of the chips this driver supports, only the UCD9090, UCD90160,
439 	 * and UCD90910 report GPI faults in their MFR_STATUS register, so only
440 	 * create the GPI fault debugfs attributes for those chips.
441 	 */
442 	if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
443 	    mid->driver_data == ucd90910) {
444 		entries = devm_kcalloc(&client->dev,
445 				       UCD9000_GPI_COUNT, sizeof(*entries),
446 				       GFP_KERNEL);
447 		if (!entries)
448 			return -ENOMEM;
449 
450 		for (i = 0; i < UCD9000_GPI_COUNT; i++) {
451 			entries[i].client = client;
452 			entries[i].index = i;
453 			scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
454 				  "gpi%d_alarm", i + 1);
455 			debugfs_create_file(name, 0444, data->debugfs,
456 					    &entries[i],
457 					    &ucd9000_debugfs_mfr_status_bit);
458 		}
459 	}
460 
461 	scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
462 	debugfs_create_file(name, 0444, data->debugfs, client,
463 			    &ucd9000_debugfs_show_mfr_status_fops);
464 
465 	return 0;
466 }
467 #else
468 static int ucd9000_init_debugfs(struct i2c_client *client,
469 				const struct i2c_device_id *mid,
470 				struct ucd9000_data *data)
471 {
472 	return 0;
473 }
474 #endif /* CONFIG_DEBUG_FS */
475 
476 static int ucd9000_probe(struct i2c_client *client,
477 			 const struct i2c_device_id *id)
478 {
479 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
480 	struct ucd9000_data *data;
481 	struct pmbus_driver_info *info;
482 	const struct i2c_device_id *mid;
483 	enum chips chip;
484 	int i, ret;
485 
486 	if (!i2c_check_functionality(client->adapter,
487 				     I2C_FUNC_SMBUS_BYTE_DATA |
488 				     I2C_FUNC_SMBUS_BLOCK_DATA))
489 		return -ENODEV;
490 
491 	ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
492 					block_buffer);
493 	if (ret < 0) {
494 		dev_err(&client->dev, "Failed to read device ID\n");
495 		return ret;
496 	}
497 	block_buffer[ret] = '\0';
498 	dev_info(&client->dev, "Device ID %s\n", block_buffer);
499 
500 	for (mid = ucd9000_id; mid->name[0]; mid++) {
501 		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
502 			break;
503 	}
504 	if (!mid->name[0]) {
505 		dev_err(&client->dev, "Unsupported device\n");
506 		return -ENODEV;
507 	}
508 
509 	if (client->dev.of_node)
510 		chip = (enum chips)of_device_get_match_data(&client->dev);
511 	else
512 		chip = id->driver_data;
513 
514 	if (chip != ucd9000 && chip != mid->driver_data)
515 		dev_notice(&client->dev,
516 			   "Device mismatch: Configured %s, detected %s\n",
517 			   id->name, mid->name);
518 
519 	data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
520 			    GFP_KERNEL);
521 	if (!data)
522 		return -ENOMEM;
523 	info = &data->info;
524 
525 	ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
526 	if (ret < 0) {
527 		dev_err(&client->dev,
528 			"Failed to read number of active pages\n");
529 		return ret;
530 	}
531 	info->pages = ret;
532 	if (!info->pages) {
533 		dev_err(&client->dev, "No pages configured\n");
534 		return -ENODEV;
535 	}
536 
537 	/* The internal temperature sensor is always active */
538 	info->func[0] = PMBUS_HAVE_TEMP;
539 
540 	/* Everything else is configurable */
541 	ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
542 					block_buffer);
543 	if (ret <= 0) {
544 		dev_err(&client->dev, "Failed to read configuration data\n");
545 		return -ENODEV;
546 	}
547 	for (i = 0; i < ret; i++) {
548 		int page = UCD9000_MON_PAGE(block_buffer[i]);
549 
550 		if (page >= info->pages)
551 			continue;
552 
553 		switch (UCD9000_MON_TYPE(block_buffer[i])) {
554 		case UCD9000_MON_VOLTAGE:
555 		case UCD9000_MON_VOLTAGE_HW:
556 			info->func[page] |= PMBUS_HAVE_VOUT
557 			  | PMBUS_HAVE_STATUS_VOUT;
558 			break;
559 		case UCD9000_MON_TEMPERATURE:
560 			info->func[page] |= PMBUS_HAVE_TEMP2
561 			  | PMBUS_HAVE_STATUS_TEMP;
562 			break;
563 		case UCD9000_MON_CURRENT:
564 			info->func[page] |= PMBUS_HAVE_IOUT
565 			  | PMBUS_HAVE_STATUS_IOUT;
566 			break;
567 		default:
568 			break;
569 		}
570 	}
571 
572 	/* Fan configuration */
573 	if (mid->driver_data == ucd90124) {
574 		for (i = 0; i < UCD9000_NUM_FAN; i++) {
575 			i2c_smbus_write_byte_data(client,
576 						  UCD9000_FAN_CONFIG_INDEX, i);
577 			ret = i2c_smbus_read_block_data(client,
578 							UCD9000_FAN_CONFIG,
579 							data->fan_data[i]);
580 			if (ret < 0)
581 				return ret;
582 		}
583 		i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
584 
585 		info->read_byte_data = ucd9000_read_byte_data;
586 		info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
587 		  | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
588 	}
589 
590 	ucd9000_probe_gpio(client, mid, data);
591 
592 	ret = pmbus_do_probe(client, mid, info);
593 	if (ret)
594 		return ret;
595 
596 	ret = ucd9000_init_debugfs(client, mid, data);
597 	if (ret)
598 		dev_warn(&client->dev, "Failed to register debugfs: %d\n",
599 			 ret);
600 
601 	return 0;
602 }
603 
604 /* This is the driver that will be inserted */
605 static struct i2c_driver ucd9000_driver = {
606 	.driver = {
607 		.name = "ucd9000",
608 		.of_match_table = of_match_ptr(ucd9000_of_match),
609 	},
610 	.probe = ucd9000_probe,
611 	.remove = pmbus_do_remove,
612 	.id_table = ucd9000_id,
613 };
614 
615 module_i2c_driver(ucd9000_driver);
616 
617 MODULE_AUTHOR("Guenter Roeck");
618 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
619 MODULE_LICENSE("GPL");
620