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, ucd9090, ucd90910 };
23 #define UCD9000_MONITOR_CONFIG 0xd5
24 #define UCD9000_NUM_PAGES 0xd6
25 #define UCD9000_FAN_CONFIG_INDEX 0xe7
26 #define UCD9000_FAN_CONFIG 0xe8
27 #define UCD9000_MFR_STATUS 0xf3
28 #define UCD9000_GPIO_SELECT 0xfa
29 #define UCD9000_GPIO_CONFIG 0xfb
30 #define UCD9000_DEVICE_ID 0xfd
32 /* GPIO CONFIG bits */
33 #define UCD9000_GPIO_CONFIG_ENABLE BIT(0)
34 #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1)
35 #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2)
36 #define UCD9000_GPIO_CONFIG_STATUS BIT(3)
37 #define UCD9000_GPIO_INPUT 0
38 #define UCD9000_GPIO_OUTPUT 1
40 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
41 #define UCD9000_MON_PAGE(x) ((x) & 0x0f)
43 #define UCD9000_MON_VOLTAGE 1
44 #define UCD9000_MON_TEMPERATURE 2
45 #define UCD9000_MON_CURRENT 3
46 #define UCD9000_MON_VOLTAGE_HW 4
48 #define UCD9000_NUM_FAN 4
50 #define UCD9000_GPIO_NAME_LEN 16
51 #define UCD9090_NUM_GPIOS 23
52 #define UCD901XX_NUM_GPIOS 26
53 #define UCD90910_NUM_GPIOS 26
55 #define UCD9000_DEBUGFS_NAME_LEN 24
56 #define UCD9000_GPI_COUNT 8
59 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
60 struct pmbus_driver_info info;
62 struct gpio_chip gpio;
64 struct dentry *debugfs;
66 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
68 struct ucd9000_debugfs_entry {
69 struct i2c_client *client;
73 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
76 struct ucd9000_data *data
77 = to_ucd9000_data(pmbus_get_driver_info(client));
79 if (data->fan_data[fan][3] & 1)
80 fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */
82 /* Pulses/revolution */
83 fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
88 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
94 case PMBUS_FAN_CONFIG_12:
98 ret = ucd9000_get_fan_config(client, 0);
101 fan_config = ret << 4;
102 ret = ucd9000_get_fan_config(client, 1);
108 case PMBUS_FAN_CONFIG_34:
112 ret = ucd9000_get_fan_config(client, 2);
115 fan_config = ret << 4;
116 ret = ucd9000_get_fan_config(client, 3);
129 static const struct i2c_device_id ucd9000_id[] = {
130 {"ucd9000", ucd9000},
131 {"ucd90120", ucd90120},
132 {"ucd90124", ucd90124},
133 {"ucd90160", ucd90160},
134 {"ucd9090", ucd9090},
135 {"ucd90910", ucd90910},
138 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
140 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
142 .compatible = "ti,ucd9000",
143 .data = (void *)ucd9000
146 .compatible = "ti,ucd90120",
147 .data = (void *)ucd90120
150 .compatible = "ti,ucd90124",
151 .data = (void *)ucd90124
154 .compatible = "ti,ucd90160",
155 .data = (void *)ucd90160
158 .compatible = "ti,ucd9090",
159 .data = (void *)ucd9090
162 .compatible = "ti,ucd90910",
163 .data = (void *)ucd90910
167 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
169 #ifdef CONFIG_GPIOLIB
170 static int ucd9000_gpio_read_config(struct i2c_client *client,
175 /* No page set required */
176 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
180 return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
183 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
185 struct i2c_client *client = gpiochip_get_data(gc);
188 ret = ucd9000_gpio_read_config(client, offset);
192 return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
195 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
198 struct i2c_client *client = gpiochip_get_data(gc);
201 ret = ucd9000_gpio_read_config(client, offset);
203 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
209 if (ret & UCD9000_GPIO_CONFIG_STATUS)
212 ret |= UCD9000_GPIO_CONFIG_STATUS;
214 if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
217 ret &= ~UCD9000_GPIO_CONFIG_STATUS;
220 ret |= UCD9000_GPIO_CONFIG_ENABLE;
222 /* Page set not required */
223 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
225 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
230 ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
232 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
234 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
238 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
241 struct i2c_client *client = gpiochip_get_data(gc);
244 ret = ucd9000_gpio_read_config(client, offset);
248 return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
251 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
252 unsigned int offset, bool direction_out,
255 struct i2c_client *client = gpiochip_get_data(gc);
256 int ret, config, out_val;
258 ret = ucd9000_gpio_read_config(client, offset);
263 out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
265 if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
266 if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
269 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
273 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
275 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
278 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
281 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
284 ret |= UCD9000_GPIO_CONFIG_ENABLE;
287 /* Page set not required */
288 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
292 config &= ~UCD9000_GPIO_CONFIG_ENABLE;
294 return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
297 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
300 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
303 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
304 unsigned int offset, int val)
306 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
310 static void ucd9000_probe_gpio(struct i2c_client *client,
311 const struct i2c_device_id *mid,
312 struct ucd9000_data *data)
316 switch (mid->driver_data) {
318 data->gpio.ngpio = UCD9090_NUM_GPIOS;
323 data->gpio.ngpio = UCD901XX_NUM_GPIOS;
326 data->gpio.ngpio = UCD90910_NUM_GPIOS;
329 return; /* GPIO support is optional. */
333 * Pinmux support has not been added to the new gpio_chip.
334 * This support should be added when possible given the mux
335 * behavior of these IO devices.
337 data->gpio.label = client->name;
338 data->gpio.get_direction = ucd9000_gpio_get_direction;
339 data->gpio.direction_input = ucd9000_gpio_direction_input;
340 data->gpio.direction_output = ucd9000_gpio_direction_output;
341 data->gpio.get = ucd9000_gpio_get;
342 data->gpio.set = ucd9000_gpio_set;
343 data->gpio.can_sleep = true;
344 data->gpio.base = -1;
345 data->gpio.parent = &client->dev;
347 rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
349 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
352 static void ucd9000_probe_gpio(struct i2c_client *client,
353 const struct i2c_device_id *mid,
354 struct ucd9000_data *data)
357 #endif /* CONFIG_GPIOLIB */
359 #ifdef CONFIG_DEBUG_FS
360 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
362 int ret = pmbus_set_page(client, 0);
367 return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
370 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
372 struct ucd9000_debugfs_entry *entry = data;
373 struct i2c_client *client = entry->client;
374 u8 buffer[I2C_SMBUS_BLOCK_MAX];
377 ret = ucd9000_get_mfr_status(client, buffer);
382 * Attribute only created for devices with gpi fault bits at bits
383 * 16-23, which is the second byte of the response.
385 *val = !!(buffer[1] & BIT(entry->index));
389 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
390 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
392 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
393 char __user *buf, size_t count,
396 struct i2c_client *client = file->private_data;
397 u8 buffer[I2C_SMBUS_BLOCK_MAX];
398 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
402 rc = ucd9000_get_mfr_status(client, buffer);
406 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
410 return simple_read_from_buffer(buf, count, ppos, str, res - str);
413 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
414 .llseek = noop_llseek,
415 .read = ucd9000_debugfs_read_mfr_status,
419 static int ucd9000_init_debugfs(struct i2c_client *client,
420 const struct i2c_device_id *mid,
421 struct ucd9000_data *data)
423 struct dentry *debugfs;
424 struct ucd9000_debugfs_entry *entries;
426 char name[UCD9000_DEBUGFS_NAME_LEN];
428 debugfs = pmbus_get_debugfs_dir(client);
432 data->debugfs = debugfs_create_dir(client->name, debugfs);
437 * Of the chips this driver supports, only the UCD9090, UCD90160,
438 * and UCD90910 report GPI faults in their MFR_STATUS register, so only
439 * create the GPI fault debugfs attributes for those chips.
441 if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
442 mid->driver_data == ucd90910) {
443 entries = devm_kcalloc(&client->dev,
444 UCD9000_GPI_COUNT, sizeof(*entries),
449 for (i = 0; i < UCD9000_GPI_COUNT; i++) {
450 entries[i].client = client;
451 entries[i].index = i;
452 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
453 "gpi%d_alarm", i + 1);
454 debugfs_create_file(name, 0444, data->debugfs,
456 &ucd9000_debugfs_mfr_status_bit);
460 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
461 debugfs_create_file(name, 0444, data->debugfs, client,
462 &ucd9000_debugfs_show_mfr_status_fops);
467 static int ucd9000_init_debugfs(struct i2c_client *client,
468 const struct i2c_device_id *mid,
469 struct ucd9000_data *data)
473 #endif /* CONFIG_DEBUG_FS */
475 static int ucd9000_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
478 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
479 struct ucd9000_data *data;
480 struct pmbus_driver_info *info;
481 const struct i2c_device_id *mid;
485 if (!i2c_check_functionality(client->adapter,
486 I2C_FUNC_SMBUS_BYTE_DATA |
487 I2C_FUNC_SMBUS_BLOCK_DATA))
490 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
493 dev_err(&client->dev, "Failed to read device ID\n");
496 block_buffer[ret] = '\0';
497 dev_info(&client->dev, "Device ID %s\n", block_buffer);
499 for (mid = ucd9000_id; mid->name[0]; mid++) {
500 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
504 dev_err(&client->dev, "Unsupported device\n");
508 if (client->dev.of_node)
509 chip = (enum chips)of_device_get_match_data(&client->dev);
511 chip = id->driver_data;
513 if (chip != ucd9000 && chip != mid->driver_data)
514 dev_notice(&client->dev,
515 "Device mismatch: Configured %s, detected %s\n",
516 id->name, mid->name);
518 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
524 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
526 dev_err(&client->dev,
527 "Failed to read number of active pages\n");
532 dev_err(&client->dev, "No pages configured\n");
536 /* The internal temperature sensor is always active */
537 info->func[0] = PMBUS_HAVE_TEMP;
539 /* Everything else is configurable */
540 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
543 dev_err(&client->dev, "Failed to read configuration data\n");
546 for (i = 0; i < ret; i++) {
547 int page = UCD9000_MON_PAGE(block_buffer[i]);
549 if (page >= info->pages)
552 switch (UCD9000_MON_TYPE(block_buffer[i])) {
553 case UCD9000_MON_VOLTAGE:
554 case UCD9000_MON_VOLTAGE_HW:
555 info->func[page] |= PMBUS_HAVE_VOUT
556 | PMBUS_HAVE_STATUS_VOUT;
558 case UCD9000_MON_TEMPERATURE:
559 info->func[page] |= PMBUS_HAVE_TEMP2
560 | PMBUS_HAVE_STATUS_TEMP;
562 case UCD9000_MON_CURRENT:
563 info->func[page] |= PMBUS_HAVE_IOUT
564 | PMBUS_HAVE_STATUS_IOUT;
571 /* Fan configuration */
572 if (mid->driver_data == ucd90124) {
573 for (i = 0; i < UCD9000_NUM_FAN; i++) {
574 i2c_smbus_write_byte_data(client,
575 UCD9000_FAN_CONFIG_INDEX, i);
576 ret = i2c_smbus_read_block_data(client,
582 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
584 info->read_byte_data = ucd9000_read_byte_data;
585 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
586 | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
589 ucd9000_probe_gpio(client, mid, data);
591 ret = pmbus_do_probe(client, mid, info);
595 ret = ucd9000_init_debugfs(client, mid, data);
597 dev_warn(&client->dev, "Failed to register debugfs: %d\n",
603 /* This is the driver that will be inserted */
604 static struct i2c_driver ucd9000_driver = {
607 .of_match_table = of_match_ptr(ucd9000_of_match),
609 .probe = ucd9000_probe,
610 .remove = pmbus_do_remove,
611 .id_table = ucd9000_id,
614 module_i2c_driver(ucd9000_driver);
616 MODULE_AUTHOR("Guenter Roeck");
617 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
618 MODULE_LICENSE("GPL");