2 * Hardware monitoring driver for UCD90xxx Sequencer and System Health
5 * Copyright (C) 2011 Ericsson AB.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/debugfs.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/pmbus.h>
31 #include <linux/gpio.h>
32 #include <linux/gpio/driver.h>
35 enum chips
{ ucd9000
, ucd90120
, ucd90124
, ucd90160
, ucd9090
, ucd90910
};
37 #define UCD9000_MONITOR_CONFIG 0xd5
38 #define UCD9000_NUM_PAGES 0xd6
39 #define UCD9000_FAN_CONFIG_INDEX 0xe7
40 #define UCD9000_FAN_CONFIG 0xe8
41 #define UCD9000_MFR_STATUS 0xf3
42 #define UCD9000_GPIO_SELECT 0xfa
43 #define UCD9000_GPIO_CONFIG 0xfb
44 #define UCD9000_DEVICE_ID 0xfd
46 /* GPIO CONFIG bits */
47 #define UCD9000_GPIO_CONFIG_ENABLE BIT(0)
48 #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1)
49 #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2)
50 #define UCD9000_GPIO_CONFIG_STATUS BIT(3)
51 #define UCD9000_GPIO_INPUT 0
52 #define UCD9000_GPIO_OUTPUT 1
54 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
55 #define UCD9000_MON_PAGE(x) ((x) & 0x0f)
57 #define UCD9000_MON_VOLTAGE 1
58 #define UCD9000_MON_TEMPERATURE 2
59 #define UCD9000_MON_CURRENT 3
60 #define UCD9000_MON_VOLTAGE_HW 4
62 #define UCD9000_NUM_FAN 4
64 #define UCD9000_GPIO_NAME_LEN 16
65 #define UCD9090_NUM_GPIOS 23
66 #define UCD901XX_NUM_GPIOS 26
67 #define UCD90910_NUM_GPIOS 26
69 #define UCD9000_DEBUGFS_NAME_LEN 24
70 #define UCD9000_GPI_COUNT 8
73 u8 fan_data
[UCD9000_NUM_FAN
][I2C_SMBUS_BLOCK_MAX
];
74 struct pmbus_driver_info info
;
76 struct gpio_chip gpio
;
78 struct dentry
*debugfs
;
80 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
82 struct ucd9000_debugfs_entry
{
83 struct i2c_client
*client
;
87 static int ucd9000_get_fan_config(struct i2c_client
*client
, int fan
)
90 struct ucd9000_data
*data
91 = to_ucd9000_data(pmbus_get_driver_info(client
));
93 if (data
->fan_data
[fan
][3] & 1)
94 fan_config
|= PB_FAN_2_INSTALLED
; /* Use lower bit position */
96 /* Pulses/revolution */
97 fan_config
|= (data
->fan_data
[fan
][3] & 0x06) >> 1;
102 static int ucd9000_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
108 case PMBUS_FAN_CONFIG_12
:
112 ret
= ucd9000_get_fan_config(client
, 0);
115 fan_config
= ret
<< 4;
116 ret
= ucd9000_get_fan_config(client
, 1);
122 case PMBUS_FAN_CONFIG_34
:
126 ret
= ucd9000_get_fan_config(client
, 2);
129 fan_config
= ret
<< 4;
130 ret
= ucd9000_get_fan_config(client
, 3);
143 static const struct i2c_device_id ucd9000_id
[] = {
144 {"ucd9000", ucd9000
},
145 {"ucd90120", ucd90120
},
146 {"ucd90124", ucd90124
},
147 {"ucd90160", ucd90160
},
148 {"ucd9090", ucd9090
},
149 {"ucd90910", ucd90910
},
152 MODULE_DEVICE_TABLE(i2c
, ucd9000_id
);
154 static const struct of_device_id ucd9000_of_match
[] = {
156 .compatible
= "ti,ucd9000",
157 .data
= (void *)ucd9000
160 .compatible
= "ti,ucd90120",
161 .data
= (void *)ucd90120
164 .compatible
= "ti,ucd90124",
165 .data
= (void *)ucd90124
168 .compatible
= "ti,ucd90160",
169 .data
= (void *)ucd90160
172 .compatible
= "ti,ucd9090",
173 .data
= (void *)ucd9090
176 .compatible
= "ti,ucd90910",
177 .data
= (void *)ucd90910
181 MODULE_DEVICE_TABLE(of
, ucd9000_of_match
);
183 #ifdef CONFIG_GPIOLIB
184 static int ucd9000_gpio_read_config(struct i2c_client
*client
,
189 /* No page set required */
190 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_SELECT
, offset
);
194 return i2c_smbus_read_byte_data(client
, UCD9000_GPIO_CONFIG
);
197 static int ucd9000_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
199 struct i2c_client
*client
= gpiochip_get_data(gc
);
202 ret
= ucd9000_gpio_read_config(client
, offset
);
206 return !!(ret
& UCD9000_GPIO_CONFIG_STATUS
);
209 static void ucd9000_gpio_set(struct gpio_chip
*gc
, unsigned int offset
,
212 struct i2c_client
*client
= gpiochip_get_data(gc
);
215 ret
= ucd9000_gpio_read_config(client
, offset
);
217 dev_dbg(&client
->dev
, "failed to read GPIO %d config: %d\n",
223 if (ret
& UCD9000_GPIO_CONFIG_STATUS
)
226 ret
|= UCD9000_GPIO_CONFIG_STATUS
;
228 if (!(ret
& UCD9000_GPIO_CONFIG_STATUS
))
231 ret
&= ~UCD9000_GPIO_CONFIG_STATUS
;
234 ret
|= UCD9000_GPIO_CONFIG_ENABLE
;
236 /* Page set not required */
237 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, ret
);
239 dev_dbg(&client
->dev
, "Failed to write GPIO %d config: %d\n",
244 ret
&= ~UCD9000_GPIO_CONFIG_ENABLE
;
246 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, ret
);
248 dev_dbg(&client
->dev
, "Failed to write GPIO %d config: %d\n",
252 static int ucd9000_gpio_get_direction(struct gpio_chip
*gc
,
255 struct i2c_client
*client
= gpiochip_get_data(gc
);
258 ret
= ucd9000_gpio_read_config(client
, offset
);
262 return !(ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
);
265 static int ucd9000_gpio_set_direction(struct gpio_chip
*gc
,
266 unsigned int offset
, bool direction_out
,
269 struct i2c_client
*client
= gpiochip_get_data(gc
);
270 int ret
, config
, out_val
;
272 ret
= ucd9000_gpio_read_config(client
, offset
);
277 out_val
= requested_out
? UCD9000_GPIO_CONFIG_OUT_VALUE
: 0;
279 if (ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
) {
280 if ((ret
& UCD9000_GPIO_CONFIG_OUT_VALUE
) == out_val
)
283 ret
|= UCD9000_GPIO_CONFIG_OUT_ENABLE
;
287 ret
|= UCD9000_GPIO_CONFIG_OUT_VALUE
;
289 ret
&= ~UCD9000_GPIO_CONFIG_OUT_VALUE
;
292 if (!(ret
& UCD9000_GPIO_CONFIG_OUT_ENABLE
))
295 ret
&= ~UCD9000_GPIO_CONFIG_OUT_ENABLE
;
298 ret
|= UCD9000_GPIO_CONFIG_ENABLE
;
301 /* Page set not required */
302 ret
= i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, config
);
306 config
&= ~UCD9000_GPIO_CONFIG_ENABLE
;
308 return i2c_smbus_write_byte_data(client
, UCD9000_GPIO_CONFIG
, config
);
311 static int ucd9000_gpio_direction_input(struct gpio_chip
*gc
,
314 return ucd9000_gpio_set_direction(gc
, offset
, UCD9000_GPIO_INPUT
, 0);
317 static int ucd9000_gpio_direction_output(struct gpio_chip
*gc
,
318 unsigned int offset
, int val
)
320 return ucd9000_gpio_set_direction(gc
, offset
, UCD9000_GPIO_OUTPUT
,
324 static void ucd9000_probe_gpio(struct i2c_client
*client
,
325 const struct i2c_device_id
*mid
,
326 struct ucd9000_data
*data
)
330 switch (mid
->driver_data
) {
332 data
->gpio
.ngpio
= UCD9090_NUM_GPIOS
;
337 data
->gpio
.ngpio
= UCD901XX_NUM_GPIOS
;
340 data
->gpio
.ngpio
= UCD90910_NUM_GPIOS
;
343 return; /* GPIO support is optional. */
347 * Pinmux support has not been added to the new gpio_chip.
348 * This support should be added when possible given the mux
349 * behavior of these IO devices.
351 data
->gpio
.label
= client
->name
;
352 data
->gpio
.get_direction
= ucd9000_gpio_get_direction
;
353 data
->gpio
.direction_input
= ucd9000_gpio_direction_input
;
354 data
->gpio
.direction_output
= ucd9000_gpio_direction_output
;
355 data
->gpio
.get
= ucd9000_gpio_get
;
356 data
->gpio
.set
= ucd9000_gpio_set
;
357 data
->gpio
.can_sleep
= true;
358 data
->gpio
.base
= -1;
359 data
->gpio
.parent
= &client
->dev
;
361 rc
= devm_gpiochip_add_data(&client
->dev
, &data
->gpio
, client
);
363 dev_warn(&client
->dev
, "Could not add gpiochip: %d\n", rc
);
366 static void ucd9000_probe_gpio(struct i2c_client
*client
,
367 const struct i2c_device_id
*mid
,
368 struct ucd9000_data
*data
)
371 #endif /* CONFIG_GPIOLIB */
373 #ifdef CONFIG_DEBUG_FS
374 static int ucd9000_get_mfr_status(struct i2c_client
*client
, u8
*buffer
)
376 int ret
= pmbus_set_page(client
, 0);
381 return i2c_smbus_read_block_data(client
, UCD9000_MFR_STATUS
, buffer
);
384 static int ucd9000_debugfs_show_mfr_status_bit(void *data
, u64
*val
)
386 struct ucd9000_debugfs_entry
*entry
= data
;
387 struct i2c_client
*client
= entry
->client
;
388 u8 buffer
[I2C_SMBUS_BLOCK_MAX
];
391 ret
= ucd9000_get_mfr_status(client
, buffer
);
396 * Attribute only created for devices with gpi fault bits at bits
397 * 16-23, which is the second byte of the response.
399 *val
= !!(buffer
[1] & BIT(entry
->index
));
403 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit
,
404 ucd9000_debugfs_show_mfr_status_bit
, NULL
, "%1lld\n");
406 static ssize_t
ucd9000_debugfs_read_mfr_status(struct file
*file
,
407 char __user
*buf
, size_t count
,
410 struct i2c_client
*client
= file
->private_data
;
411 u8 buffer
[I2C_SMBUS_BLOCK_MAX
];
412 char str
[(I2C_SMBUS_BLOCK_MAX
* 2) + 2];
416 rc
= ucd9000_get_mfr_status(client
, buffer
);
420 res
= bin2hex(str
, buffer
, min(rc
, I2C_SMBUS_BLOCK_MAX
));
424 return simple_read_from_buffer(buf
, count
, ppos
, str
, res
- str
);
427 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops
= {
428 .llseek
= noop_llseek
,
429 .read
= ucd9000_debugfs_read_mfr_status
,
433 static int ucd9000_init_debugfs(struct i2c_client
*client
,
434 const struct i2c_device_id
*mid
,
435 struct ucd9000_data
*data
)
437 struct dentry
*debugfs
;
438 struct ucd9000_debugfs_entry
*entries
;
440 char name
[UCD9000_DEBUGFS_NAME_LEN
];
442 debugfs
= pmbus_get_debugfs_dir(client
);
446 data
->debugfs
= debugfs_create_dir(client
->name
, debugfs
);
451 * Of the chips this driver supports, only the UCD9090, UCD90160,
452 * and UCD90910 report GPI faults in their MFR_STATUS register, so only
453 * create the GPI fault debugfs attributes for those chips.
455 if (mid
->driver_data
== ucd9090
|| mid
->driver_data
== ucd90160
||
456 mid
->driver_data
== ucd90910
) {
457 entries
= devm_kcalloc(&client
->dev
,
458 UCD9000_GPI_COUNT
, sizeof(*entries
),
463 for (i
= 0; i
< UCD9000_GPI_COUNT
; i
++) {
464 entries
[i
].client
= client
;
465 entries
[i
].index
= i
;
466 scnprintf(name
, UCD9000_DEBUGFS_NAME_LEN
,
467 "gpi%d_alarm", i
+ 1);
468 debugfs_create_file(name
, 0444, data
->debugfs
,
470 &ucd9000_debugfs_mfr_status_bit
);
474 scnprintf(name
, UCD9000_DEBUGFS_NAME_LEN
, "mfr_status");
475 debugfs_create_file(name
, 0444, data
->debugfs
, client
,
476 &ucd9000_debugfs_show_mfr_status_fops
);
481 static int ucd9000_init_debugfs(struct i2c_client
*client
,
482 const struct i2c_device_id
*mid
,
483 struct ucd9000_data
*data
)
487 #endif /* CONFIG_DEBUG_FS */
489 static int ucd9000_probe(struct i2c_client
*client
,
490 const struct i2c_device_id
*id
)
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
;
499 if (!i2c_check_functionality(client
->adapter
,
500 I2C_FUNC_SMBUS_BYTE_DATA
|
501 I2C_FUNC_SMBUS_BLOCK_DATA
))
504 ret
= i2c_smbus_read_block_data(client
, UCD9000_DEVICE_ID
,
507 dev_err(&client
->dev
, "Failed to read device ID\n");
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
)))
518 dev_err(&client
->dev
, "Unsupported device\n");
522 if (client
->dev
.of_node
)
523 chip
= (enum chips
)of_device_get_match_data(&client
->dev
);
525 chip
= id
->driver_data
;
527 if (chip
!= ucd9000
&& chip
!= mid
->driver_data
)
528 dev_notice(&client
->dev
,
529 "Device mismatch: Configured %s, detected %s\n",
530 id
->name
, mid
->name
);
532 data
= devm_kzalloc(&client
->dev
, sizeof(struct ucd9000_data
),
538 ret
= i2c_smbus_read_byte_data(client
, UCD9000_NUM_PAGES
);
540 dev_err(&client
->dev
,
541 "Failed to read number of active pages\n");
546 dev_err(&client
->dev
, "No pages configured\n");
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
,
557 dev_err(&client
->dev
, "Failed to read configuration data\n");
560 for (i
= 0; i
< ret
; i
++) {
561 int page
= UCD9000_MON_PAGE(block_buffer
[i
]);
563 if (page
>= info
->pages
)
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
;
572 case UCD9000_MON_TEMPERATURE
:
573 info
->func
[page
] |= PMBUS_HAVE_TEMP2
574 | PMBUS_HAVE_STATUS_TEMP
;
576 case UCD9000_MON_CURRENT
:
577 info
->func
[page
] |= PMBUS_HAVE_IOUT
578 | PMBUS_HAVE_STATUS_IOUT
;
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
,
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
, mid
, info
);
609 ret
= ucd9000_init_debugfs(client
, mid
, data
);
611 dev_warn(&client
->dev
, "Failed to register debugfs: %d\n",
617 /* This is the driver that will be inserted */
618 static struct i2c_driver ucd9000_driver
= {
621 .of_match_table
= of_match_ptr(ucd9000_of_match
),
623 .probe
= ucd9000_probe
,
624 .remove
= pmbus_do_remove
,
625 .id_table
= ucd9000_id
,
628 module_i2c_driver(ucd9000_driver
);
630 MODULE_AUTHOR("Guenter Roeck");
631 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
632 MODULE_LICENSE("GPL");