Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / pmbus / ucd9000.c
bloba15e6fe3e425a3cab4cfbb3cb9011bdb4fcca517
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Hardware monitoring driver for UCD90xxx Sequencer and System Health
4 * Controller series
6 * Copyright (C) 2011 Ericsson AB.
7 */
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>
19 #include "pmbus.h"
21 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090,
22 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) & 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
61 struct ucd9000_data {
62 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
63 struct pmbus_driver_info info;
64 #ifdef CONFIG_GPIOLIB
65 struct gpio_chip gpio;
66 #endif
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;
73 u8 index;
76 static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
78 int fan_config = 0;
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;
88 return fan_config;
91 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
93 int ret = 0;
94 int fan_config;
96 switch (reg) {
97 case PMBUS_FAN_CONFIG_12:
98 if (page > 0)
99 return -ENXIO;
101 ret = ucd9000_get_fan_config(client, 0);
102 if (ret < 0)
103 return ret;
104 fan_config = ret << 4;
105 ret = ucd9000_get_fan_config(client, 1);
106 if (ret < 0)
107 return ret;
108 fan_config |= ret;
109 ret = fan_config;
110 break;
111 case PMBUS_FAN_CONFIG_34:
112 if (page > 0)
113 return -ENXIO;
115 ret = ucd9000_get_fan_config(client, 2);
116 if (ret < 0)
117 return ret;
118 fan_config = ret << 4;
119 ret = ucd9000_get_fan_config(client, 3);
120 if (ret < 0)
121 return ret;
122 fan_config |= ret;
123 ret = fan_config;
124 break;
125 default:
126 ret = -ENODATA;
127 break;
129 return ret;
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
173 { },
175 MODULE_DEVICE_TABLE(of, ucd9000_of_match);
177 #ifdef CONFIG_GPIOLIB
178 static int ucd9000_gpio_read_config(struct i2c_client *client,
179 unsigned int offset)
181 int ret;
183 /* No page set required */
184 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset);
185 if (ret < 0)
186 return ret;
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);
194 int ret;
196 ret = ucd9000_gpio_read_config(client, offset);
197 if (ret < 0)
198 return ret;
200 return !!(ret & UCD9000_GPIO_CONFIG_STATUS);
203 static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset,
204 int value)
206 struct i2c_client *client = gpiochip_get_data(gc);
207 int ret;
209 ret = ucd9000_gpio_read_config(client, offset);
210 if (ret < 0) {
211 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n",
212 offset, ret);
213 return;
216 if (value) {
217 if (ret & UCD9000_GPIO_CONFIG_STATUS)
218 return;
220 ret |= UCD9000_GPIO_CONFIG_STATUS;
221 } else {
222 if (!(ret & UCD9000_GPIO_CONFIG_STATUS))
223 return;
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);
232 if (ret < 0) {
233 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
234 offset, ret);
235 return;
238 ret &= ~UCD9000_GPIO_CONFIG_ENABLE;
240 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret);
241 if (ret < 0)
242 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n",
243 offset, ret);
246 static int ucd9000_gpio_get_direction(struct gpio_chip *gc,
247 unsigned int offset)
249 struct i2c_client *client = gpiochip_get_data(gc);
250 int ret;
252 ret = ucd9000_gpio_read_config(client, offset);
253 if (ret < 0)
254 return ret;
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,
261 int requested_out)
263 struct i2c_client *client = gpiochip_get_data(gc);
264 int ret, config, out_val;
266 ret = ucd9000_gpio_read_config(client, offset);
267 if (ret < 0)
268 return ret;
270 if (direction_out) {
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)
275 return 0;
276 } else {
277 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE;
280 if (out_val)
281 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE;
282 else
283 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE;
285 } else {
286 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE))
287 return 0;
289 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE;
292 ret |= UCD9000_GPIO_CONFIG_ENABLE;
293 config = ret;
295 /* Page set not required */
296 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config);
297 if (ret < 0)
298 return ret;
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,
306 unsigned int offset)
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,
315 val);
318 static void ucd9000_probe_gpio(struct i2c_client *client,
319 const struct i2c_device_id *mid,
320 struct ucd9000_data *data)
322 int rc;
324 switch (mid->driver_data) {
325 case ucd9090:
326 data->gpio.ngpio = UCD9090_NUM_GPIOS;
327 break;
328 case ucd90120:
329 case ucd90124:
330 case ucd90160:
331 data->gpio.ngpio = UCD901XX_NUM_GPIOS;
332 break;
333 case ucd90320:
334 data->gpio.ngpio = UCD90320_NUM_GPIOS;
335 break;
336 case ucd90910:
337 data->gpio.ngpio = UCD90910_NUM_GPIOS;
338 break;
339 default:
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);
359 if (rc)
360 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc);
362 #else
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);
375 if (ret < 0)
376 return ret;
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];
386 int ret, i;
388 ret = ucd9000_get_mfr_status(client, buffer);
389 if (ret < 0)
390 return ret;
393 * GPI fault bits are in sets of 8, two bytes from end of response.
395 i = ret - 3 - entry->index / 8;
396 if (i >= 0)
397 *val = !!(buffer[i] & BIT(entry->index % 8));
399 return 0;
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,
406 loff_t *ppos)
408 struct i2c_client *client = file->private_data;
409 u8 buffer[I2C_SMBUS_BLOCK_MAX];
410 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2];
411 char *res;
412 int rc;
414 rc = ucd9000_get_mfr_status(client, buffer);
415 if (rc < 0)
416 return rc;
418 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX));
419 *res++ = '\n';
420 *res = 0;
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,
428 .open = simple_open,
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;
437 int i, gpi_count;
438 char name[UCD9000_DEBUGFS_NAME_LEN];
440 debugfs = pmbus_get_debugfs_dir(client);
441 if (!debugfs)
442 return -ENOENT;
444 data->debugfs = debugfs_create_dir(client->name, debugfs);
445 if (!data->debugfs)
446 return -ENOENT;
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
452 * chips.
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
457 : UCD9000_GPI_COUNT;
458 entries = devm_kcalloc(&client->dev,
459 gpi_count, sizeof(*entries),
460 GFP_KERNEL);
461 if (!entries)
462 return -ENOMEM;
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,
470 &entries[i],
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);
479 return 0;
481 #else
482 static int ucd9000_init_debugfs(struct i2c_client *client,
483 const struct i2c_device_id *mid,
484 struct ucd9000_data *data)
486 return 0;
488 #endif /* CONFIG_DEBUG_FS */
490 static int ucd9000_probe(struct i2c_client *client)
492 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
493 struct ucd9000_data *data;
494 struct pmbus_driver_info *info;
495 const struct i2c_device_id *mid;
496 enum chips chip;
497 int i, ret;
499 if (!i2c_check_functionality(client->adapter,
500 I2C_FUNC_SMBUS_BYTE_DATA |
501 I2C_FUNC_SMBUS_BLOCK_DATA))
502 return -ENODEV;
504 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
505 block_buffer);
506 if (ret < 0) {
507 dev_err(&client->dev, "Failed to read device ID\n");
508 return ret;
510 block_buffer[ret] = '\0';
511 dev_info(&client->dev, "Device ID %s\n", block_buffer);
513 for (mid = ucd9000_id; mid->name[0]; mid++) {
514 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
515 break;
517 if (!mid->name[0]) {
518 dev_err(&client->dev, "Unsupported device\n");
519 return -ENODEV;
522 if (client->dev.of_node)
523 chip = (enum chips)of_device_get_match_data(&client->dev);
524 else
525 chip = mid->driver_data;
527 if (chip != ucd9000 && strcmp(client->name, mid->name) != 0)
528 dev_notice(&client->dev,
529 "Device mismatch: Configured %s, detected %s\n",
530 client->name, mid->name);
532 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
533 GFP_KERNEL);
534 if (!data)
535 return -ENOMEM;
536 info = &data->info;
538 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
539 if (ret < 0) {
540 dev_err(&client->dev,
541 "Failed to read number of active pages\n");
542 return ret;
544 info->pages = ret;
545 if (!info->pages) {
546 dev_err(&client->dev, "No pages configured\n");
547 return -ENODEV;
550 /* The internal temperature sensor is always active */
551 info->func[0] = PMBUS_HAVE_TEMP;
553 /* Everything else is configurable */
554 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
555 block_buffer);
556 if (ret <= 0) {
557 dev_err(&client->dev, "Failed to read configuration data\n");
558 return -ENODEV;
560 for (i = 0; i < ret; i++) {
561 int page = UCD9000_MON_PAGE(block_buffer[i]);
563 if (page >= info->pages)
564 continue;
566 switch (UCD9000_MON_TYPE(block_buffer[i])) {
567 case UCD9000_MON_VOLTAGE:
568 case UCD9000_MON_VOLTAGE_HW:
569 info->func[page] |= PMBUS_HAVE_VOUT
570 | PMBUS_HAVE_STATUS_VOUT;
571 break;
572 case UCD9000_MON_TEMPERATURE:
573 info->func[page] |= PMBUS_HAVE_TEMP2
574 | PMBUS_HAVE_STATUS_TEMP;
575 break;
576 case UCD9000_MON_CURRENT:
577 info->func[page] |= PMBUS_HAVE_IOUT
578 | PMBUS_HAVE_STATUS_IOUT;
579 break;
580 default:
581 break;
585 /* Fan configuration */
586 if (mid->driver_data == ucd90124) {
587 for (i = 0; i < UCD9000_NUM_FAN; i++) {
588 i2c_smbus_write_byte_data(client,
589 UCD9000_FAN_CONFIG_INDEX, i);
590 ret = i2c_smbus_read_block_data(client,
591 UCD9000_FAN_CONFIG,
592 data->fan_data[i]);
593 if (ret < 0)
594 return ret;
596 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
598 info->read_byte_data = ucd9000_read_byte_data;
599 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
600 | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
603 ucd9000_probe_gpio(client, mid, data);
605 ret = pmbus_do_probe(client, info);
606 if (ret)
607 return ret;
609 ret = ucd9000_init_debugfs(client, mid, data);
610 if (ret)
611 dev_warn(&client->dev, "Failed to register debugfs: %d\n",
612 ret);
614 return 0;
617 /* This is the driver that will be inserted */
618 static struct i2c_driver ucd9000_driver = {
619 .driver = {
620 .name = "ucd9000",
621 .of_match_table = of_match_ptr(ucd9000_of_match),
623 .probe_new = ucd9000_probe,
624 .id_table = ucd9000_id,
627 module_i2c_driver(ucd9000_driver);
629 MODULE_AUTHOR("Guenter Roeck");
630 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
631 MODULE_LICENSE("GPL");