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.h>
19 #include <linux/gpio/driver.h>
22 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd9090, ucd90910 };
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) & 0x0f)
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 UCD90910_NUM_GPIOS 26
56 #define UCD9000_DEBUGFS_NAME_LEN 24
57 #define UCD9000_GPI_COUNT 8
60 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
61 struct pmbus_driver_info info;
63 struct gpio_chip gpio;
65 struct dentry *debugfs;
67 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
69 struct ucd9000_debugfs_entry {
70 struct i2c_client *client;
74 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
77 struct ucd9000_data *data
78 = to_ucd9000_data(pmbus_get_driver_info(client));
80 if (data->fan_data[fan][3] & 1)
81 fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */
83 /* Pulses/revolution */
84 fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
89 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
95 case PMBUS_FAN_CONFIG_12:
99 ret = ucd9000_get_fan_config(client, 0);
102 fan_config = ret << 4;
103 ret = ucd9000_get_fan_config(client, 1);
109 case PMBUS_FAN_CONFIG_34:
113 ret = ucd9000_get_fan_config(client, 2);
116 fan_config = ret << 4;
117 ret = ucd9000_get_fan_config(client, 3);
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},
139 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
141 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
143 .compatible = "ti,ucd9000",
144 .data = (void *)ucd9000
147 .compatible = "ti,ucd90120",
148 .data = (void *)ucd90120
151 .compatible = "ti,ucd90124",
152 .data = (void *)ucd90124
155 .compatible = "ti,ucd90160",
156 .data = (void *)ucd90160
159 .compatible = "ti,ucd9090",
160 .data = (void *)ucd9090
163 .compatible = "ti,ucd90910",
164 .data = (void *)ucd90910
168 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
170 #ifdef CONFIG_GPIOLIB
171 static int ucd9000_gpio_read_config(struct i2c_client *client,
176 /* No page set required */
177 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
181 return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
184 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
186 struct i2c_client *client = gpiochip_get_data(gc);
189 ret = ucd9000_gpio_read_config(client, offset);
193 return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
196 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
199 struct i2c_client *client = gpiochip_get_data(gc);
202 ret = ucd9000_gpio_read_config(client, offset);
204 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
210 if (ret & UCD9000_GPIO_CONFIG_STATUS)
213 ret |= UCD9000_GPIO_CONFIG_STATUS;
215 if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
218 ret &= ~UCD9000_GPIO_CONFIG_STATUS;
221 ret |= UCD9000_GPIO_CONFIG_ENABLE;
223 /* Page set not required */
224 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
226 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
231 ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
233 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
235 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
239 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
242 struct i2c_client *client = gpiochip_get_data(gc);
245 ret = ucd9000_gpio_read_config(client, offset);
249 return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
252 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
253 unsigned int offset, bool direction_out,
256 struct i2c_client *client = gpiochip_get_data(gc);
257 int ret, config, out_val;
259 ret = ucd9000_gpio_read_config(client, offset);
264 out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
266 if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
267 if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
270 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
274 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
276 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
279 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
282 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
285 ret |= UCD9000_GPIO_CONFIG_ENABLE;
288 /* Page set not required */
289 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
293 config &= ~UCD9000_GPIO_CONFIG_ENABLE;
295 return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
298 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
301 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
304 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
305 unsigned int offset, int val)
307 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
311 static void ucd9000_probe_gpio(struct i2c_client *client,
312 const struct i2c_device_id *mid,
313 struct ucd9000_data *data)
317 switch (mid->driver_data) {
319 data->gpio.ngpio = UCD9090_NUM_GPIOS;
324 data->gpio.ngpio = UCD901XX_NUM_GPIOS;
327 data->gpio.ngpio = UCD90910_NUM_GPIOS;
330 return; /* GPIO support is optional. */
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.
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;
348 rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
350 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
353 static void ucd9000_probe_gpio(struct i2c_client *client,
354 const struct i2c_device_id *mid,
355 struct ucd9000_data *data)
358 #endif /* CONFIG_GPIOLIB */
360 #ifdef CONFIG_DEBUG_FS
361 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
363 int ret = pmbus_set_page(client, 0);
368 return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
371 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
373 struct ucd9000_debugfs_entry *entry = data;
374 struct i2c_client *client = entry->client;
375 u8 buffer[I2C_SMBUS_BLOCK_MAX];
378 ret = ucd9000_get_mfr_status(client, buffer);
383 * Attribute only created for devices with gpi fault bits at bits
384 * 16-23, which is the second byte of the response.
386 *val = !!(buffer[1] & BIT(entry->index));
390 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
391 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
393 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
394 char __user *buf, size_t count,
397 struct i2c_client *client = file->private_data;
398 u8 buffer[I2C_SMBUS_BLOCK_MAX];
399 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
403 rc = ucd9000_get_mfr_status(client, buffer);
407 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
411 return simple_read_from_buffer(buf, count, ppos, str, res - str);
414 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
415 .llseek = noop_llseek,
416 .read = ucd9000_debugfs_read_mfr_status,
420 static int ucd9000_init_debugfs(struct i2c_client *client,
421 const struct i2c_device_id *mid,
422 struct ucd9000_data *data)
424 struct dentry *debugfs;
425 struct ucd9000_debugfs_entry *entries;
427 char name[UCD9000_DEBUGFS_NAME_LEN];
429 debugfs = pmbus_get_debugfs_dir(client);
433 data->debugfs = debugfs_create_dir(client->name, debugfs);
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.
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),
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,
457 &ucd9000_debugfs_mfr_status_bit);
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);
468 static int ucd9000_init_debugfs(struct i2c_client *client,
469 const struct i2c_device_id *mid,
470 struct ucd9000_data *data)
474 #endif /* CONFIG_DEBUG_FS */
476 static int ucd9000_probe(struct i2c_client *client,
477 const struct i2c_device_id *id)
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;
486 if (!i2c_check_functionality(client->adapter,
487 I2C_FUNC_SMBUS_BYTE_DATA |
488 I2C_FUNC_SMBUS_BLOCK_DATA))
491 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
494 dev_err(&client->dev, "Failed to read device ID\n");
497 block_buffer[ret] = '\0';
498 dev_info(&client->dev, "Device ID %s\n", block_buffer);
500 for (mid = ucd9000_id; mid->name[0]; mid++) {
501 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
505 dev_err(&client->dev, "Unsupported device\n");
509 if (client->dev.of_node)
510 chip = (enum chips)of_device_get_match_data(&client->dev);
512 chip = id->driver_data;
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);
519 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
525 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
527 dev_err(&client->dev,
528 "Failed to read number of active pages\n");
533 dev_err(&client->dev, "No pages configured\n");
537 /* The internal temperature sensor is always active */
538 info->func[0] = PMBUS_HAVE_TEMP;
540 /* Everything else is configurable */
541 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
544 dev_err(&client->dev, "Failed to read configuration data\n");
547 for (i = 0; i < ret; i++) {
548 int page = UCD9000_MON_PAGE(block_buffer[i]);
550 if (page >= info->pages)
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;
559 case UCD9000_MON_TEMPERATURE:
560 info->func[page] |= PMBUS_HAVE_TEMP2
561 | PMBUS_HAVE_STATUS_TEMP;
563 case UCD9000_MON_CURRENT:
564 info->func[page] |= PMBUS_HAVE_IOUT
565 | PMBUS_HAVE_STATUS_IOUT;
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,
583 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
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;
590 ucd9000_probe_gpio(client, mid, data);
592 ret = pmbus_do_probe(client, mid, info);
596 ret = ucd9000_init_debugfs(client, mid, data);
598 dev_warn(&client->dev, "Failed to register debugfs: %d\n",
604 /* This is the driver that will be inserted */
605 static struct i2c_driver ucd9000_driver = {
608 .of_match_table = of_match_ptr(ucd9000_of_match),
610 .probe = ucd9000_probe,
611 .remove = pmbus_do_remove,
612 .id_table = ucd9000_id,
615 module_i2c_driver(ucd9000_driver);
617 MODULE_AUTHOR("Guenter Roeck");
618 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
619 MODULE_LICENSE("GPL");