0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/debugfs.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 #include <linux/init.h>
0014 #include <linux/err.h>
0015 #include <linux/slab.h>
0016 #include <linux/i2c.h>
0017 #include <linux/pmbus.h>
0018 #include <linux/gpio/driver.h>
0019 #include "pmbus.h"
0020
0021 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090,
0022 ucd90910 };
0023
0024 #define UCD9000_MONITOR_CONFIG 0xd5
0025 #define UCD9000_NUM_PAGES 0xd6
0026 #define UCD9000_FAN_CONFIG_INDEX 0xe7
0027 #define UCD9000_FAN_CONFIG 0xe8
0028 #define UCD9000_MFR_STATUS 0xf3
0029 #define UCD9000_GPIO_SELECT 0xfa
0030 #define UCD9000_GPIO_CONFIG 0xfb
0031 #define UCD9000_DEVICE_ID 0xfd
0032
0033
0034 #define UCD9000_GPIO_CONFIG_ENABLE BIT(0)
0035 #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1)
0036 #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2)
0037 #define UCD9000_GPIO_CONFIG_STATUS BIT(3)
0038 #define UCD9000_GPIO_INPUT 0
0039 #define UCD9000_GPIO_OUTPUT 1
0040
0041 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
0042 #define UCD9000_MON_PAGE(x) ((x) & 0x1f)
0043
0044 #define UCD9000_MON_VOLTAGE 1
0045 #define UCD9000_MON_TEMPERATURE 2
0046 #define UCD9000_MON_CURRENT 3
0047 #define UCD9000_MON_VOLTAGE_HW 4
0048
0049 #define UCD9000_NUM_FAN 4
0050
0051 #define UCD9000_GPIO_NAME_LEN 16
0052 #define UCD9090_NUM_GPIOS 23
0053 #define UCD901XX_NUM_GPIOS 26
0054 #define UCD90320_NUM_GPIOS 84
0055 #define UCD90910_NUM_GPIOS 26
0056
0057 #define UCD9000_DEBUGFS_NAME_LEN 24
0058 #define UCD9000_GPI_COUNT 8
0059 #define UCD90320_GPI_COUNT 32
0060
0061 struct ucd9000_data {
0062 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
0063 struct pmbus_driver_info info;
0064 #ifdef CONFIG_GPIOLIB
0065 struct gpio_chip gpio;
0066 #endif
0067 struct dentry *debugfs;
0068 };
0069 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
0070
0071 struct ucd9000_debugfs_entry {
0072 struct i2c_client *client;
0073 u8 index;
0074 };
0075
0076 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
0077 {
0078 int fan_config = 0;
0079 struct ucd9000_data *data
0080 = to_ucd9000_data(pmbus_get_driver_info(client));
0081
0082 if (data->fan_data[fan][3] & 1)
0083 fan_config |= PB_FAN_2_INSTALLED;
0084
0085
0086 fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
0087
0088 return fan_config;
0089 }
0090
0091 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
0092 {
0093 int ret = 0;
0094 int fan_config;
0095
0096 switch (reg) {
0097 case PMBUS_FAN_CONFIG_12:
0098 if (page > 0)
0099 return -ENXIO;
0100
0101 ret = ucd9000_get_fan_config(client, 0);
0102 if (ret < 0)
0103 return ret;
0104 fan_config = ret << 4;
0105 ret = ucd9000_get_fan_config(client, 1);
0106 if (ret < 0)
0107 return ret;
0108 fan_config |= ret;
0109 ret = fan_config;
0110 break;
0111 case PMBUS_FAN_CONFIG_34:
0112 if (page > 0)
0113 return -ENXIO;
0114
0115 ret = ucd9000_get_fan_config(client, 2);
0116 if (ret < 0)
0117 return ret;
0118 fan_config = ret << 4;
0119 ret = ucd9000_get_fan_config(client, 3);
0120 if (ret < 0)
0121 return ret;
0122 fan_config |= ret;
0123 ret = fan_config;
0124 break;
0125 default:
0126 ret = -ENODATA;
0127 break;
0128 }
0129 return ret;
0130 }
0131
0132 static const struct i2c_device_id ucd9000_id[] = {
0133 {"ucd9000", ucd9000},
0134 {"ucd90120", ucd90120},
0135 {"ucd90124", ucd90124},
0136 {"ucd90160", ucd90160},
0137 {"ucd90320", ucd90320},
0138 {"ucd9090", ucd9090},
0139 {"ucd90910", ucd90910},
0140 {}
0141 };
0142 MODULE_DEVICE_TABLE(i2c, ucd9000_id);
0143
0144 static const struct of_device_id __maybe_unused ucd9000_of_match[] = {
0145 {
0146 .compatible = "ti,ucd9000",
0147 .data = (void *)ucd9000
0148 },
0149 {
0150 .compatible = "ti,ucd90120",
0151 .data = (void *)ucd90120
0152 },
0153 {
0154 .compatible = "ti,ucd90124",
0155 .data = (void *)ucd90124
0156 },
0157 {
0158 .compatible = "ti,ucd90160",
0159 .data = (void *)ucd90160
0160 },
0161 {
0162 .compatible = "ti,ucd90320",
0163 .data = (void *)ucd90320
0164 },
0165 {
0166 .compatible = "ti,ucd9090",
0167 .data = (void *)ucd9090
0168 },
0169 {
0170 .compatible = "ti,ucd90910",
0171 .data = (void *)ucd90910
0172 },
0173 { },
0174 };
0175 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
0176
0177 #ifdef CONFIG_GPIOLIB
0178 static int ucd9000_gpio_read_config(struct i2c_client *client,
0179 unsigned int offset)
0180 {
0181 int ret;
0182
0183
0184 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
0185 if (ret < 0)
0186 return ret;
0187
0188 return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG);
0189 }
0190
0191 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset)
0192 {
0193 struct i2c_client *client = gpiochip_get_data(gc);
0194 int ret;
0195
0196 ret = ucd9000_gpio_read_config(client, offset);
0197 if (ret < 0)
0198 return ret;
0199
0200 return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
0201 }
0202
0203 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
0204 int value)
0205 {
0206 struct i2c_client *client = gpiochip_get_data(gc);
0207 int ret;
0208
0209 ret = ucd9000_gpio_read_config(client, offset);
0210 if (ret < 0) {
0211 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
0212 offset, ret);
0213 return;
0214 }
0215
0216 if (value) {
0217 if (ret & UCD9000_GPIO_CONFIG_STATUS)
0218 return;
0219
0220 ret |= UCD9000_GPIO_CONFIG_STATUS;
0221 } else {
0222 if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
0223 return;
0224
0225 ret &= ~UCD9000_GPIO_CONFIG_STATUS;
0226 }
0227
0228 ret |= UCD9000_GPIO_CONFIG_ENABLE;
0229
0230
0231 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
0232 if (ret < 0) {
0233 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
0234 offset, ret);
0235 return;
0236 }
0237
0238 ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
0239
0240 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
0241 if (ret < 0)
0242 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
0243 offset, ret);
0244 }
0245
0246 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
0247 unsigned int offset)
0248 {
0249 struct i2c_client *client = gpiochip_get_data(gc);
0250 int ret;
0251
0252 ret = ucd9000_gpio_read_config(client, offset);
0253 if (ret < 0)
0254 return ret;
0255
0256 return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE);
0257 }
0258
0259 static int ucd9000_gpio_set_direction(struct gpio_chip *gc,
0260 unsigned int offset, bool direction_out,
0261 int requested_out)
0262 {
0263 struct i2c_client *client = gpiochip_get_data(gc);
0264 int ret, config, out_val;
0265
0266 ret = ucd9000_gpio_read_config(client, offset);
0267 if (ret < 0)
0268 return ret;
0269
0270 if (direction_out) {
0271 out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0;
0272
0273 if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) {
0274 if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val)
0275 return 0;
0276 } else {
0277 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
0278 }
0279
0280 if (out_val)
0281 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
0282 else
0283 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
0284
0285 } else {
0286 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
0287 return 0;
0288
0289 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
0290 }
0291
0292 ret |= UCD9000_GPIO_CONFIG_ENABLE;
0293 config = ret;
0294
0295
0296 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
0297 if (ret < 0)
0298 return ret;
0299
0300 config &= ~UCD9000_GPIO_CONFIG_ENABLE;
0301
0302 return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
0303 }
0304
0305 static int ucd9000_gpio_direction_input(struct gpio_chip *gc,
0306 unsigned int offset)
0307 {
0308 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0);
0309 }
0310
0311 static int ucd9000_gpio_direction_output(struct gpio_chip *gc,
0312 unsigned int offset, int val)
0313 {
0314 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT,
0315 val);
0316 }
0317
0318 static void ucd9000_probe_gpio(struct i2c_client *client,
0319 const struct i2c_device_id *mid,
0320 struct ucd9000_data *data)
0321 {
0322 int rc;
0323
0324 switch (mid->driver_data) {
0325 case ucd9090:
0326 data->gpio.ngpio = UCD9090_NUM_GPIOS;
0327 break;
0328 case ucd90120:
0329 case ucd90124:
0330 case ucd90160:
0331 data->gpio.ngpio = UCD901XX_NUM_GPIOS;
0332 break;
0333 case ucd90320:
0334 data->gpio.ngpio = UCD90320_NUM_GPIOS;
0335 break;
0336 case ucd90910:
0337 data->gpio.ngpio = UCD90910_NUM_GPIOS;
0338 break;
0339 default:
0340 return;
0341 }
0342
0343
0344
0345
0346
0347
0348 data->gpio.label = client->name;
0349 data->gpio.get_direction = ucd9000_gpio_get_direction;
0350 data->gpio.direction_input = ucd9000_gpio_direction_input;
0351 data->gpio.direction_output = ucd9000_gpio_direction_output;
0352 data->gpio.get = ucd9000_gpio_get;
0353 data->gpio.set = ucd9000_gpio_set;
0354 data->gpio.can_sleep = true;
0355 data->gpio.base = -1;
0356 data->gpio.parent = &client->dev;
0357
0358 rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client);
0359 if (rc)
0360 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
0361 }
0362 #else
0363 static void ucd9000_probe_gpio(struct i2c_client *client,
0364 const struct i2c_device_id *mid,
0365 struct ucd9000_data *data)
0366 {
0367 }
0368 #endif
0369
0370 #ifdef CONFIG_DEBUG_FS
0371 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer)
0372 {
0373 int ret = pmbus_set_page(client, 0, 0xff);
0374
0375 if (ret < 0)
0376 return ret;
0377
0378 return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer);
0379 }
0380
0381 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val)
0382 {
0383 struct ucd9000_debugfs_entry *entry = data;
0384 struct i2c_client *client = entry->client;
0385 u8 buffer[I2C_SMBUS_BLOCK_MAX];
0386 int ret, i;
0387
0388 ret = ucd9000_get_mfr_status(client, buffer);
0389 if (ret < 0)
0390 return ret;
0391
0392
0393
0394
0395 i = ret - 3 - entry->index / 8;
0396 if (i >= 0)
0397 *val = !!(buffer[i] & BIT(entry->index % 8));
0398
0399 return 0;
0400 }
0401 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit,
0402 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n");
0403
0404 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file,
0405 char __user *buf, size_t count,
0406 loff_t *ppos)
0407 {
0408 struct i2c_client *client = file->private_data;
0409 u8 buffer[I2C_SMBUS_BLOCK_MAX];
0410 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
0411 char *res;
0412 int rc;
0413
0414 rc = ucd9000_get_mfr_status(client, buffer);
0415 if (rc < 0)
0416 return rc;
0417
0418 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
0419 *res++ = '\n';
0420 *res = 0;
0421
0422 return simple_read_from_buffer(buf, count, ppos, str, res - str);
0423 }
0424
0425 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = {
0426 .llseek = noop_llseek,
0427 .read = ucd9000_debugfs_read_mfr_status,
0428 .open = simple_open,
0429 };
0430
0431 static int ucd9000_init_debugfs(struct i2c_client *client,
0432 const struct i2c_device_id *mid,
0433 struct ucd9000_data *data)
0434 {
0435 struct dentry *debugfs;
0436 struct ucd9000_debugfs_entry *entries;
0437 int i, gpi_count;
0438 char name[UCD9000_DEBUGFS_NAME_LEN];
0439
0440 debugfs = pmbus_get_debugfs_dir(client);
0441 if (!debugfs)
0442 return -ENOENT;
0443
0444 data->debugfs = debugfs_create_dir(client->name, debugfs);
0445 if (!data->debugfs)
0446 return -ENOENT;
0447
0448
0449
0450
0451
0452
0453
0454 if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 ||
0455 mid->driver_data == ucd90320 || mid->driver_data == ucd90910) {
0456 gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT
0457 : UCD9000_GPI_COUNT;
0458 entries = devm_kcalloc(&client->dev,
0459 gpi_count, sizeof(*entries),
0460 GFP_KERNEL);
0461 if (!entries)
0462 return -ENOMEM;
0463
0464 for (i = 0; i < gpi_count; i++) {
0465 entries[i].client = client;
0466 entries[i].index = i;
0467 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN,
0468 "gpi%d_alarm", i + 1);
0469 debugfs_create_file(name, 0444, data->debugfs,
0470 &entries[i],
0471 &ucd9000_debugfs_mfr_status_bit);
0472 }
0473 }
0474
0475 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status");
0476 debugfs_create_file(name, 0444, data->debugfs, client,
0477 &ucd9000_debugfs_show_mfr_status_fops);
0478
0479 return 0;
0480 }
0481 #else
0482 static int ucd9000_init_debugfs(struct i2c_client *client,
0483 const struct i2c_device_id *mid,
0484 struct ucd9000_data *data)
0485 {
0486 return 0;
0487 }
0488 #endif
0489
0490 static int ucd9000_probe(struct i2c_client *client)
0491 {
0492 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
0493 struct ucd9000_data *data;
0494 struct pmbus_driver_info *info;
0495 const struct i2c_device_id *mid;
0496 enum chips chip;
0497 int i, ret;
0498
0499 if (!i2c_check_functionality(client->adapter,
0500 I2C_FUNC_SMBUS_BYTE_DATA |
0501 I2C_FUNC_SMBUS_BLOCK_DATA))
0502 return -ENODEV;
0503
0504 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
0505 block_buffer);
0506 if (ret < 0) {
0507 dev_err(&client->dev, "Failed to read device ID\n");
0508 return ret;
0509 }
0510 block_buffer[ret] = '\0';
0511 dev_info(&client->dev, "Device ID %s\n", block_buffer);
0512
0513 for (mid = ucd9000_id; mid->name[0]; mid++) {
0514 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
0515 break;
0516 }
0517 if (!mid->name[0]) {
0518 dev_err(&client->dev, "Unsupported device\n");
0519 return -ENODEV;
0520 }
0521
0522 if (client->dev.of_node)
0523 chip = (enum chips)of_device_get_match_data(&client->dev);
0524 else
0525 chip = mid->driver_data;
0526
0527 if (chip != ucd9000 && strcmp(client->name, mid->name) != 0)
0528 dev_notice(&client->dev,
0529 "Device mismatch: Configured %s, detected %s\n",
0530 client->name, mid->name);
0531
0532 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
0533 GFP_KERNEL);
0534 if (!data)
0535 return -ENOMEM;
0536 info = &data->info;
0537
0538 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
0539 if (ret < 0) {
0540 dev_err(&client->dev,
0541 "Failed to read number of active pages\n");
0542 return ret;
0543 }
0544 info->pages = ret;
0545 if (!info->pages) {
0546 dev_err(&client->dev, "No pages configured\n");
0547 return -ENODEV;
0548 }
0549
0550
0551 info->func[0] = PMBUS_HAVE_TEMP;
0552
0553
0554 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
0555 block_buffer);
0556 if (ret <= 0) {
0557 dev_err(&client->dev, "Failed to read configuration data\n");
0558 return -ENODEV;
0559 }
0560 for (i = 0; i < ret; i++) {
0561 int page = UCD9000_MON_PAGE(block_buffer[i]);
0562
0563 if (page >= info->pages)
0564 continue;
0565
0566 switch (UCD9000_MON_TYPE(block_buffer[i])) {
0567 case UCD9000_MON_VOLTAGE:
0568 case UCD9000_MON_VOLTAGE_HW:
0569 info->func[page] |= PMBUS_HAVE_VOUT
0570 | PMBUS_HAVE_STATUS_VOUT;
0571 break;
0572 case UCD9000_MON_TEMPERATURE:
0573 info->func[page] |= PMBUS_HAVE_TEMP2
0574 | PMBUS_HAVE_STATUS_TEMP;
0575 break;
0576 case UCD9000_MON_CURRENT:
0577 info->func[page] |= PMBUS_HAVE_IOUT
0578 | PMBUS_HAVE_STATUS_IOUT;
0579 break;
0580 default:
0581 break;
0582 }
0583 }
0584
0585
0586 if (mid->driver_data == ucd90124) {
0587 for (i = 0; i < UCD9000_NUM_FAN; i++) {
0588 i2c_smbus_write_byte_data(client,
0589 UCD9000_FAN_CONFIG_INDEX, i);
0590 ret = i2c_smbus_read_block_data(client,
0591 UCD9000_FAN_CONFIG,
0592 data->fan_data[i]);
0593 if (ret < 0)
0594 return ret;
0595 }
0596 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
0597
0598 info->read_byte_data = ucd9000_read_byte_data;
0599 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
0600 | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
0601 }
0602
0603 ucd9000_probe_gpio(client, mid, data);
0604
0605 ret = pmbus_do_probe(client, info);
0606 if (ret)
0607 return ret;
0608
0609 ret = ucd9000_init_debugfs(client, mid, data);
0610 if (ret)
0611 dev_warn(&client->dev, "Failed to register debugfs: %d\n",
0612 ret);
0613
0614 return 0;
0615 }
0616
0617
0618 static struct i2c_driver ucd9000_driver = {
0619 .driver = {
0620 .name = "ucd9000",
0621 .of_match_table = of_match_ptr(ucd9000_of_match),
0622 },
0623 .probe_new = ucd9000_probe,
0624 .id_table = ucd9000_id,
0625 };
0626
0627 module_i2c_driver(ucd9000_driver);
0628
0629 MODULE_AUTHOR("Guenter Roeck");
0630 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
0631 MODULE_LICENSE("GPL");
0632 MODULE_IMPORT_NS(PMBUS);