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/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/pmbus.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/timekeeping.h>
23 enum chips
{ ucd9000
, ucd90120
, ucd90124
, ucd90160
, ucd90320
, ucd9090
,
26 #define UCD9000_MONITOR_CONFIG 0xd5
27 #define UCD9000_NUM_PAGES 0xd6
28 #define UCD9000_FAN_CONFIG_INDEX 0xe7
29 #define UCD9000_FAN_CONFIG 0xe8
30 #define UCD9000_MFR_STATUS 0xf3
31 #define UCD9000_GPIO_SELECT 0xfa
32 #define UCD9000_GPIO_CONFIG 0xfb
33 #define UCD9000_DEVICE_ID 0xfd
35 /* GPIO CONFIG bits */
36 #define UCD9000_GPIO_CONFIG_ENABLE BIT(0)
37 #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1)
38 #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2)
39 #define UCD9000_GPIO_CONFIG_STATUS BIT(3)
40 #define UCD9000_GPIO_INPUT 0
41 #define UCD9000_GPIO_OUTPUT 1
43 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
44 #define UCD9000_MON_PAGE(x) ((x) & 0x1f)
46 #define UCD9000_MON_VOLTAGE 1
47 #define UCD9000_MON_TEMPERATURE 2
48 #define UCD9000_MON_CURRENT 3
49 #define UCD9000_MON_VOLTAGE_HW 4
51 #define UCD9000_NUM_FAN 4
53 #define UCD9000_GPIO_NAME_LEN 16
54 #define UCD9090_NUM_GPIOS 23
55 #define UCD901XX_NUM_GPIOS 26
56 #define UCD90320_NUM_GPIOS 84
57 #define UCD90910_NUM_GPIOS 26
59 #define UCD9000_DEBUGFS_NAME_LEN 24
60 #define UCD9000_GPI_COUNT 8
61 #define UCD90320_GPI_COUNT 32
64 u8 fan_data
[UCD9000_NUM_FAN
][I2C_SMBUS_BLOCK_MAX
];
65 struct pmbus_driver_info info
;
67 struct gpio_chip gpio
;
69 struct dentry
*debugfs
;
71 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
73 struct ucd9000_debugfs_entry
{
74 struct i2c_client
*client
;
79 * It has been observed that the UCD90320 randomly fails register access when
80 * doing another access right on the back of a register write. To mitigate this
81 * make sure that there is a minimum delay between a write access and the
82 * following access. The 500 is based on experimental data. At a delay of
83 * 350us the issue seems to go away. Add a bit of extra margin to allow for
84 * system to system differences.
86 #define UCD90320_WAIT_DELAY_US 500
88 static int ucd9000_get_fan_config(struct i2c_client
*client
, int fan
)
91 struct ucd9000_data
*data
92 = to_ucd9000_data(pmbus_get_driver_info(client
));
94 if (data
->fan_data
[fan
][3] & 1)
95 fan_config
|= PB_FAN_2_INSTALLED
; /* Use lower bit position */
97 /* Pulses/revolution */
98 fan_config
|= (data
->fan_data
[fan
][3] & 0x06) >> 1;
103 static int ucd9000_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
109 case PMBUS_FAN_CONFIG_12
:
113 ret
= ucd9000_get_fan_config(client
, 0);
116 fan_config
= ret
<< 4;
117 ret
= ucd9000_get_fan_config(client
, 1);
123 case PMBUS_FAN_CONFIG_34
:
127 ret
= ucd9000_get_fan_config(client
, 2);
130 fan_config
= ret
<< 4;
131 ret
= ucd9000_get_fan_config(client
, 3);
144 static const struct i2c_device_id ucd9000_id
[] = {
145 {"ucd9000", ucd9000
},
146 {"ucd90120", ucd90120
},
147 {"ucd90124", ucd90124
},
148 {"ucd90160", ucd90160
},
149 {"ucd90320", ucd90320
},
150 {"ucd9090", ucd9090
},
151 {"ucd90910", ucd90910
},
154 MODULE_DEVICE_TABLE(i2c
, ucd9000_id
);
156 static const struct of_device_id __maybe_unused ucd9000_of_match
[] = {
158 .compatible
= "ti,ucd9000",
159 .data
= (void *)ucd9000
162 .compatible
= "ti,ucd90120",
163 .data
= (void *)ucd90120
166 .compatible
= "ti,ucd90124",
167 .data
= (void *)ucd90124
170 .compatible
= "ti,ucd90160",
171 .data
= (void *)ucd90160
174 .compatible
= "ti,ucd90320",
175 .data
= (void *)ucd90320
178 .compatible
= "ti,ucd9090",
179 .data
= (void *)ucd9090
182 .compatible
= "ti,ucd90910",
183 .data
= (void *)ucd90910
187 MODULE_DEVICE_TABLE(of
, ucd9000_of_match
);
189 #ifdef CONFIG_GPIOLIB
190 static int ucd9000_gpio_read_config(struct i2c_client
*client
,
195 /* No page set required */
196 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_SELECT
, offset
);
200 return i2c_smbus_read_byte_data(client
, UCD9000_GPIO_CONFIG
);
203 static int ucd9000_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
205 struct i2c_client
*client
= gpiochip_get_data(gc
);
208 ret
= ucd9000_gpio_read_config(client
, offset
);
212 return !!(ret
& UCD9000_GPIO_CONFIG_STATUS
);
215 static void ucd9000_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
218 struct i2c_client
*client
= gpiochip_get_data(gc
);
221 ret
= ucd9000_gpio_read_config(client
, offset
);
223 dev_dbg(&client
->dev
, "failed to read GPIO %d config: %d\n",
229 if (ret
& UCD9000_GPIO_CONFIG_STATUS
)
232 ret
|= UCD9000_GPIO_CONFIG_STATUS
;
234 if (!(ret
& UCD9000_GPIO_CONFIG_STATUS
))
237 ret
&= ~UCD9000_GPIO_CONFIG_STATUS
;
240 ret
|= UCD9000_GPIO_CONFIG_ENABLE
;
242 /* Page set not required */
243 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, ret
);
245 dev_dbg(&client
->dev
, "Failed to write GPIO %d config: %d\n",
250 ret
&= ~UCD9000_GPIO_CONFIG_ENABLE
;
252 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, ret
);
254 dev_dbg(&client
->dev
, "Failed to write GPIO %d config: %d\n",
258 static int ucd9000_gpio_get_direction(struct gpio_chip
*gc
,
261 struct i2c_client
*client
= gpiochip_get_data(gc
);
264 ret
= ucd9000_gpio_read_config(client
, offset
);
268 return !(ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
);
271 static int ucd9000_gpio_set_direction(struct gpio_chip
*gc
,
272 unsigned int offset
, bool direction_out
,
275 struct i2c_client
*client
= gpiochip_get_data(gc
);
276 int ret
, config
, out_val
;
278 ret
= ucd9000_gpio_read_config(client
, offset
);
283 out_val
= requested_out
? UCD9000_GPIO_CONFIG_OUT_VALUE
: 0;
285 if (ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
) {
286 if ((ret
& UCD9000_GPIO_CONFIG_OUT_VALUE
) == out_val
)
289 ret
|= UCD9000_GPIO_CONFIG_OUT_ENABLE
;
293 ret
|= UCD9000_GPIO_CONFIG_OUT_VALUE
;
295 ret
&= ~UCD9000_GPIO_CONFIG_OUT_VALUE
;
298 if (!(ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
))
301 ret
&= ~UCD9000_GPIO_CONFIG_OUT_ENABLE
;
304 ret
|= UCD9000_GPIO_CONFIG_ENABLE
;
307 /* Page set not required */
308 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, config
);
312 config
&= ~UCD9000_GPIO_CONFIG_ENABLE
;
314 return i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, config
);
317 static int ucd9000_gpio_direction_input(struct gpio_chip
*gc
,
320 return ucd9000_gpio_set_direction(gc
, offset
, UCD9000_GPIO_INPUT
, 0);
323 static int ucd9000_gpio_direction_output(struct gpio_chip
*gc
,
324 unsigned int offset
, int val
)
326 return ucd9000_gpio_set_direction(gc
, offset
, UCD9000_GPIO_OUTPUT
,
330 static void ucd9000_probe_gpio(struct i2c_client
*client
,
331 const struct i2c_device_id
*mid
,
332 struct ucd9000_data
*data
)
336 switch (mid
->driver_data
) {
338 data
->gpio
.ngpio
= UCD9090_NUM_GPIOS
;
343 data
->gpio
.ngpio
= UCD901XX_NUM_GPIOS
;
346 data
->gpio
.ngpio
= UCD90320_NUM_GPIOS
;
349 data
->gpio
.ngpio
= UCD90910_NUM_GPIOS
;
352 return; /* GPIO support is optional. */
356 * Pinmux support has not been added to the new gpio_chip.
357 * This support should be added when possible given the mux
358 * behavior of these IO devices.
360 data
->gpio
.label
= client
->name
;
361 data
->gpio
.get_direction
= ucd9000_gpio_get_direction
;
362 data
->gpio
.direction_input
= ucd9000_gpio_direction_input
;
363 data
->gpio
.direction_output
= ucd9000_gpio_direction_output
;
364 data
->gpio
.get
= ucd9000_gpio_get
;
365 data
->gpio
.set
= ucd9000_gpio_set
;
366 data
->gpio
.can_sleep
= true;
367 data
->gpio
.base
= -1;
368 data
->gpio
.parent
= &client
->dev
;
370 rc
= devm_gpiochip_add_data(&client
->dev
, &data
->gpio
, client
);
372 dev_warn(&client
->dev
, "Could not add gpiochip: %d\n", rc
);
375 static void ucd9000_probe_gpio(struct i2c_client
*client
,
376 const struct i2c_device_id
*mid
,
377 struct ucd9000_data
*data
)
380 #endif /* CONFIG_GPIOLIB */
382 #ifdef CONFIG_DEBUG_FS
383 static int ucd9000_get_mfr_status(struct i2c_client
*client
, u8
*buffer
)
385 int ret
= pmbus_set_page(client
, 0, 0xff);
390 return i2c_smbus_read_block_data(client
, UCD9000_MFR_STATUS
, buffer
);
393 static int ucd9000_debugfs_show_mfr_status_bit(void *data
, u64
*val
)
395 struct ucd9000_debugfs_entry
*entry
= data
;
396 struct i2c_client
*client
= entry
->client
;
397 u8 buffer
[I2C_SMBUS_BLOCK_MAX
];
400 ret
= ucd9000_get_mfr_status(client
, buffer
);
405 * GPI fault bits are in sets of 8, two bytes from end of response.
407 i
= ret
- 3 - entry
->index
/ 8;
409 *val
= !!(buffer
[i
] & BIT(entry
->index
% 8));
413 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit
,
414 ucd9000_debugfs_show_mfr_status_bit
, NULL
, "%1lld\n");
416 static ssize_t
ucd9000_debugfs_read_mfr_status(struct file
*file
,
417 char __user
*buf
, size_t count
,
420 struct i2c_client
*client
= file
->private_data
;
421 u8 buffer
[I2C_SMBUS_BLOCK_MAX
];
422 char str
[(I2C_SMBUS_BLOCK_MAX
* 2) + 2];
426 rc
= ucd9000_get_mfr_status(client
, buffer
);
430 res
= bin2hex(str
, buffer
, min(rc
, I2C_SMBUS_BLOCK_MAX
));
434 return simple_read_from_buffer(buf
, count
, ppos
, str
, res
- str
);
437 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops
= {
438 .llseek
= noop_llseek
,
439 .read
= ucd9000_debugfs_read_mfr_status
,
443 static int ucd9000_init_debugfs(struct i2c_client
*client
,
444 const struct i2c_device_id
*mid
,
445 struct ucd9000_data
*data
)
447 struct dentry
*debugfs
;
448 struct ucd9000_debugfs_entry
*entries
;
450 char name
[UCD9000_DEBUGFS_NAME_LEN
];
452 debugfs
= pmbus_get_debugfs_dir(client
);
456 data
->debugfs
= debugfs_create_dir(client
->name
, debugfs
);
459 * Of the chips this driver supports, only the UCD9090, UCD90160,
460 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS
461 * register, so only create the GPI fault debugfs attributes for those
464 if (mid
->driver_data
== ucd9090
|| mid
->driver_data
== ucd90160
||
465 mid
->driver_data
== ucd90320
|| mid
->driver_data
== ucd90910
) {
466 gpi_count
= mid
->driver_data
== ucd90320
? UCD90320_GPI_COUNT
468 entries
= devm_kcalloc(&client
->dev
,
469 gpi_count
, sizeof(*entries
),
474 for (i
= 0; i
< gpi_count
; i
++) {
475 entries
[i
].client
= client
;
476 entries
[i
].index
= i
;
477 scnprintf(name
, UCD9000_DEBUGFS_NAME_LEN
,
478 "gpi%d_alarm", i
+ 1);
479 debugfs_create_file(name
, 0444, data
->debugfs
,
481 &ucd9000_debugfs_mfr_status_bit
);
485 scnprintf(name
, UCD9000_DEBUGFS_NAME_LEN
, "mfr_status");
486 debugfs_create_file(name
, 0444, data
->debugfs
, client
,
487 &ucd9000_debugfs_show_mfr_status_fops
);
492 static int ucd9000_init_debugfs(struct i2c_client
*client
,
493 const struct i2c_device_id
*mid
,
494 struct ucd9000_data
*data
)
498 #endif /* CONFIG_DEBUG_FS */
500 static int ucd9000_probe(struct i2c_client
*client
)
502 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
503 struct ucd9000_data
*data
;
504 struct pmbus_driver_info
*info
;
505 const struct i2c_device_id
*mid
;
509 if (!i2c_check_functionality(client
->adapter
,
510 I2C_FUNC_SMBUS_BYTE_DATA
|
511 I2C_FUNC_SMBUS_BLOCK_DATA
))
514 ret
= i2c_smbus_read_block_data(client
, UCD9000_DEVICE_ID
,
517 dev_err(&client
->dev
, "Failed to read device ID\n");
520 block_buffer
[ret
] = '\0';
521 dev_info(&client
->dev
, "Device ID %s\n", block_buffer
);
523 for (mid
= ucd9000_id
; mid
->name
[0]; mid
++) {
524 if (!strncasecmp(mid
->name
, block_buffer
, strlen(mid
->name
)))
528 dev_err(&client
->dev
, "Unsupported device\n");
532 if (client
->dev
.of_node
)
533 chip
= (uintptr_t)of_device_get_match_data(&client
->dev
);
535 chip
= mid
->driver_data
;
537 if (chip
!= ucd9000
&& strcmp(client
->name
, mid
->name
) != 0)
538 dev_notice(&client
->dev
,
539 "Device mismatch: Configured %s, detected %s\n",
540 client
->name
, mid
->name
);
542 data
= devm_kzalloc(&client
->dev
, sizeof(struct ucd9000_data
),
548 ret
= i2c_smbus_read_byte_data(client
, UCD9000_NUM_PAGES
);
550 dev_err(&client
->dev
,
551 "Failed to read number of active pages\n");
556 dev_err(&client
->dev
, "No pages configured\n");
560 /* The internal temperature sensor is always active */
561 info
->func
[0] = PMBUS_HAVE_TEMP
;
563 /* Everything else is configurable */
564 ret
= i2c_smbus_read_block_data(client
, UCD9000_MONITOR_CONFIG
,
567 dev_err(&client
->dev
, "Failed to read configuration data\n");
570 for (i
= 0; i
< ret
; i
++) {
571 int page
= UCD9000_MON_PAGE(block_buffer
[i
]);
573 if (page
>= info
->pages
)
576 switch (UCD9000_MON_TYPE(block_buffer
[i
])) {
577 case UCD9000_MON_VOLTAGE
:
578 case UCD9000_MON_VOLTAGE_HW
:
579 info
->func
[page
] |= PMBUS_HAVE_VOUT
580 | PMBUS_HAVE_STATUS_VOUT
;
582 case UCD9000_MON_TEMPERATURE
:
583 info
->func
[page
] |= PMBUS_HAVE_TEMP2
584 | PMBUS_HAVE_STATUS_TEMP
;
586 case UCD9000_MON_CURRENT
:
587 info
->func
[page
] |= PMBUS_HAVE_IOUT
588 | PMBUS_HAVE_STATUS_IOUT
;
595 /* Fan configuration */
596 if (mid
->driver_data
== ucd90124
) {
597 for (i
= 0; i
< UCD9000_NUM_FAN
; i
++) {
598 i2c_smbus_write_byte_data(client
,
599 UCD9000_FAN_CONFIG_INDEX
, i
);
600 ret
= i2c_smbus_read_block_data(client
,
606 i2c_smbus_write_byte_data(client
, UCD9000_FAN_CONFIG_INDEX
, 0);
608 info
->read_byte_data
= ucd9000_read_byte_data
;
609 info
->func
[0] |= PMBUS_HAVE_FAN12
| PMBUS_HAVE_STATUS_FAN12
610 | PMBUS_HAVE_FAN34
| PMBUS_HAVE_STATUS_FAN34
;
611 } else if (mid
->driver_data
== ucd90320
) {
612 /* Delay SMBus operations after a write */
613 info
->write_delay
= UCD90320_WAIT_DELAY_US
;
616 ucd9000_probe_gpio(client
, mid
, data
);
618 ret
= pmbus_do_probe(client
, info
);
622 ret
= ucd9000_init_debugfs(client
, mid
, data
);
624 dev_warn(&client
->dev
, "Failed to register debugfs: %d\n",
630 /* This is the driver that will be inserted */
631 static struct i2c_driver ucd9000_driver
= {
634 .of_match_table
= of_match_ptr(ucd9000_of_match
),
636 .probe
= ucd9000_probe
,
637 .id_table
= ucd9000_id
,
640 module_i2c_driver(ucd9000_driver
);
642 MODULE_AUTHOR("Guenter Roeck");
643 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
644 MODULE_LICENSE("GPL");
645 MODULE_IMPORT_NS("PMBUS");