1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Hardware monitoring driver for UCD90xxx Sequencer and System Health
6 * Copyright (C) 2011 Ericsson AB.
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/driver.h>
21 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090,
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
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
41 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
42 #define UCD9000_MON_PAGE(x) ((x) & 0x1f)
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
49 #define UCD9000_NUM_FAN 4
51 #define UCD9000_GPIO_NAME_LEN 16
52 #define UCD9090_NUM_GPIOS 23
53 #define UCD901XX_NUM_GPIOS 26
54 #define UCD90320_NUM_GPIOS 84
55 #define UCD90910_NUM_GPIOS 26
57 #define UCD9000_DEBUGFS_NAME_LEN 24
58 #define UCD9000_GPI_COUNT 8
59 #define UCD90320_GPI_COUNT 32
62 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
63 struct pmbus_driver_info info;
65 struct gpio_chip gpio;
67 struct dentry *debugfs;
69 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
71 struct ucd9000_debugfs_entry {
72 struct i2c_client *client;
76 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
79 struct ucd9000_data *data
80 = to_ucd9000_data(pmbus_get_driver_info(client));
82 if (data->fan_data[fan][3] & 1)
83 fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */
85 /* Pulses/revolution */
86 fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
91 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
97 case PMBUS_FAN_CONFIG_12:
101 ret = ucd9000_get_fan_config(client, 0);
104 fan_config = ret << 4;
105 ret = ucd9000_get_fan_config(client, 1);
111 case PMBUS_FAN_CONFIG_34:
115 ret = ucd9000_get_fan_config(client, 2);
118 fan_config = ret << 4;
119 ret = ucd9000_get_fan_config(client, 3);
132 static const struct i2c_device_id ucd9000_id[] = {
133 {"ucd9000", ucd9000},
134 {"ucd90120", ucd90120},
135 {"ucd90124", ucd90124},
136 {"ucd90160", ucd90160},
137 {"ucd90320", ucd90320},
138 {"ucd9090", ucd9090},
139 {"ucd90910", ucd90910},
142 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
144 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
146 .compatible = "ti,ucd9000",
147 .data = (void *)ucd9000
150 .compatible = "ti,ucd90120",
151 .data = (void *)ucd90120
154 .compatible = "ti,ucd90124",
155 .data = (void *)ucd90124
158 .compatible = "ti,ucd90160",
159 .data = (void *)ucd90160
162 .compatible = "ti,ucd90320",
163 .data = (void *)ucd90320
166 .compatible = "ti,ucd9090",
167 .data = (void *)ucd9090
170 .compatible = "ti,ucd90910",
171 .data = (void *)ucd90910
175 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
177 #ifdef CONFIG_GPIOLIB
178 static int ucd9000_gpio_read_config(struct i2c_client *client,
183 /* No page set required */
184 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
188 return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
191 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
193 struct i2c_client *client = gpiochip_get_data(gc);
196 ret = ucd9000_gpio_read_config(client, offset);
200 return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
203 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
206 struct i2c_client *client = gpiochip_get_data(gc);
209 ret = ucd9000_gpio_read_config(client, offset);
211 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
217 if (ret & UCD9000_GPIO_CONFIG_STATUS)
220 ret |= UCD9000_GPIO_CONFIG_STATUS;
222 if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
225 ret &= ~UCD9000_GPIO_CONFIG_STATUS;
228 ret |= UCD9000_GPIO_CONFIG_ENABLE;
230 /* Page set not required */
231 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
233 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
238 ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
240 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
242 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
246 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
249 struct i2c_client *client = gpiochip_get_data(gc);
252 ret = ucd9000_gpio_read_config(client, offset);
256 return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
259 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
260 unsigned int offset, bool direction_out,
263 struct i2c_client *client = gpiochip_get_data(gc);
264 int ret, config, out_val;
266 ret = ucd9000_gpio_read_config(client, offset);
271 out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
273 if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
274 if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
277 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
281 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
283 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
286 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
289 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
292 ret |= UCD9000_GPIO_CONFIG_ENABLE;
295 /* Page set not required */
296 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
300 config &= ~UCD9000_GPIO_CONFIG_ENABLE;
302 return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
305 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
308 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
311 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
312 unsigned int offset, int val)
314 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
318 static void ucd9000_probe_gpio(struct i2c_client *client,
319 const struct i2c_device_id *mid,
320 struct ucd9000_data *data)
324 switch (mid->driver_data) {
326 data->gpio.ngpio = UCD9090_NUM_GPIOS;
331 data->gpio.ngpio = UCD901XX_NUM_GPIOS;
334 data->gpio.ngpio = UCD90320_NUM_GPIOS;
337 data->gpio.ngpio = UCD90910_NUM_GPIOS;
340 return; /* GPIO support is optional. */
344 * Pinmux support has not been added to the new gpio_chip.
345 * This support should be added when possible given the mux
346 * behavior of these IO devices.
348 data->gpio.label = client->name;
349 data->gpio.get_direction = ucd9000_gpio_get_direction;
350 data->gpio.direction_input = ucd9000_gpio_direction_input;
351 data->gpio.direction_output = ucd9000_gpio_direction_output;
352 data->gpio.get = ucd9000_gpio_get;
353 data->gpio.set = ucd9000_gpio_set;
354 data->gpio.can_sleep = true;
355 data->gpio.base = -1;
356 data->gpio.parent = &client->dev;
358 rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
360 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
363 static void ucd9000_probe_gpio(struct i2c_client *client,
364 const struct i2c_device_id *mid,
365 struct ucd9000_data *data)
368 #endif /* CONFIG_GPIOLIB */
370 #ifdef CONFIG_DEBUG_FS
371 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
373 int ret = pmbus_set_page(client, 0, 0xff);
378 return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
381 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
383 struct ucd9000_debugfs_entry *entry = data;
384 struct i2c_client *client = entry->client;
385 u8 buffer[I2C_SMBUS_BLOCK_MAX];
388 ret = ucd9000_get_mfr_status(client, buffer);
393 * GPI fault bits are in sets of 8, two bytes from end of response.
395 i = ret - 3 - entry->index / 8;
397 *val = !!(buffer[i] & BIT(entry->index % 8));
401 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
402 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
404 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
405 char __user *buf, size_t count,
408 struct i2c_client *client = file->private_data;
409 u8 buffer[I2C_SMBUS_BLOCK_MAX];
410 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
414 rc = ucd9000_get_mfr_status(client, buffer);
418 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
422 return simple_read_from_buffer(buf, count, ppos, str, res - str);
425 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
426 .llseek = noop_llseek,
427 .read = ucd9000_debugfs_read_mfr_status,
431 static int ucd9000_init_debugfs(struct i2c_client *client,
432 const struct i2c_device_id *mid,
433 struct ucd9000_data *data)
435 struct dentry *debugfs;
436 struct ucd9000_debugfs_entry *entries;
438 char name[UCD9000_DEBUGFS_NAME_LEN];
440 debugfs = pmbus_get_debugfs_dir(client);
444 data->debugfs = debugfs_create_dir(client->name, debugfs);
449 * Of the chips this driver supports, only the UCD9090, UCD90160,
450 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS
451 * register, so only create the GPI fault debugfs attributes for those
454 if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
455 mid->driver_data == ucd90320 || mid->driver_data == ucd90910) {
456 gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT
458 entries = devm_kcalloc(&client->dev,
459 gpi_count, sizeof(*entries),
464 for (i = 0; i < gpi_count; i++) {
465 entries[i].client = client;
466 entries[i].index = i;
467 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
468 "gpi%d_alarm", i + 1);
469 debugfs_create_file(name, 0444, data->debugfs,
471 &ucd9000_debugfs_mfr_status_bit);
475 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
476 debugfs_create_file(name, 0444, data->debugfs, client,
477 &ucd9000_debugfs_show_mfr_status_fops);
482 static int ucd9000_init_debugfs(struct i2c_client *client,
483 const struct i2c_device_id *mid,
484 struct ucd9000_data *data)
488 #endif /* CONFIG_DEBUG_FS */
490 static int ucd9000_probe(struct i2c_client *client,
491 const struct i2c_device_id *id)
493 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
494 struct ucd9000_data *data;
495 struct pmbus_driver_info *info;
496 const struct i2c_device_id *mid;
500 if (!i2c_check_functionality(client->adapter,
501 I2C_FUNC_SMBUS_BYTE_DATA |
502 I2C_FUNC_SMBUS_BLOCK_DATA))
505 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
508 dev_err(&client->dev, "Failed to read device ID\n");
511 block_buffer[ret] = '\0';
512 dev_info(&client->dev, "Device ID %s\n", block_buffer);
514 for (mid = ucd9000_id; mid->name[0]; mid++) {
515 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
519 dev_err(&client->dev, "Unsupported device\n");
523 if (client->dev.of_node)
524 chip = (enum chips)of_device_get_match_data(&client->dev);
526 chip = id->driver_data;
528 if (chip != ucd9000 && chip != mid->driver_data)
529 dev_notice(&client->dev,
530 "Device mismatch: Configured %s, detected %s\n",
531 id->name, mid->name);
533 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
539 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
541 dev_err(&client->dev,
542 "Failed to read number of active pages\n");
547 dev_err(&client->dev, "No pages configured\n");
551 /* The internal temperature sensor is always active */
552 info->func[0] = PMBUS_HAVE_TEMP;
554 /* Everything else is configurable */
555 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
558 dev_err(&client->dev, "Failed to read configuration data\n");
561 for (i = 0; i < ret; i++) {
562 int page = UCD9000_MON_PAGE(block_buffer[i]);
564 if (page >= info->pages)
567 switch (UCD9000_MON_TYPE(block_buffer[i])) {
568 case UCD9000_MON_VOLTAGE:
569 case UCD9000_MON_VOLTAGE_HW:
570 info->func[page] |= PMBUS_HAVE_VOUT
571 | PMBUS_HAVE_STATUS_VOUT;
573 case UCD9000_MON_TEMPERATURE:
574 info->func[page] |= PMBUS_HAVE_TEMP2
575 | PMBUS_HAVE_STATUS_TEMP;
577 case UCD9000_MON_CURRENT:
578 info->func[page] |= PMBUS_HAVE_IOUT
579 | PMBUS_HAVE_STATUS_IOUT;
586 /* Fan configuration */
587 if (mid->driver_data == ucd90124) {
588 for (i = 0; i < UCD9000_NUM_FAN; i++) {
589 i2c_smbus_write_byte_data(client,
590 UCD9000_FAN_CONFIG_INDEX, i);
591 ret = i2c_smbus_read_block_data(client,
597 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
599 info->read_byte_data = ucd9000_read_byte_data;
600 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
601 | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
604 ucd9000_probe_gpio(client, mid, data);
606 ret = pmbus_do_probe(client, mid, info);
610 ret = ucd9000_init_debugfs(client, mid, data);
612 dev_warn(&client->dev, "Failed to register debugfs: %d\n",
618 /* This is the driver that will be inserted */
619 static struct i2c_driver ucd9000_driver = {
622 .of_match_table = of_match_ptr(ucd9000_of_match),
624 .probe = ucd9000_probe,
625 .remove = pmbus_do_remove,
626 .id_table = ucd9000_id,
629 module_i2c_driver(ucd9000_driver);
631 MODULE_AUTHOR("Guenter Roeck");
632 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
633 MODULE_LICENSE("GPL");