2 * Driver for Lineage Compact Power Line series of power entry modules.
4 * Copyright (C) 2010, 2011 Ericsson AB.
7 * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/err.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
34 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
35 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
37 * The devices are nominally PMBus compliant. However, most standard PMBus
38 * commands are not supported. Specifically, all hardware monitoring and
39 * status reporting commands are non-standard. For this reason, a standard
40 * PMBus driver can not be used.
42 * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
43 * To ensure device access, this driver should only be used as client driver
44 * to the pca9541 I2C master selector driver.
48 #define PEM_OPERATION 0x01
49 #define PEM_CLEAR_INFO_FLAGS 0x03
50 #define PEM_VOUT_COMMAND 0x21
51 #define PEM_VOUT_OV_FAULT_LIMIT 0x40
52 #define PEM_READ_DATA_STRING 0xd0
53 #define PEM_READ_INPUT_STRING 0xdc
54 #define PEM_READ_FIRMWARE_REV 0xdd
55 #define PEM_READ_RUN_TIMER 0xde
56 #define PEM_FAN_HI_SPEED 0xdf
57 #define PEM_FAN_NORMAL_SPEED 0xe0
58 #define PEM_READ_FAN_SPEED 0xe1
60 /* offsets in data string */
61 #define PEM_DATA_STATUS_2 0
62 #define PEM_DATA_STATUS_1 1
63 #define PEM_DATA_ALARM_2 2
64 #define PEM_DATA_ALARM_1 3
65 #define PEM_DATA_VOUT_LSB 4
66 #define PEM_DATA_VOUT_MSB 5
67 #define PEM_DATA_CURRENT 6
68 #define PEM_DATA_TEMP 7
70 /* Virtual entries, to report constants */
71 #define PEM_DATA_TEMP_MAX 10
72 #define PEM_DATA_TEMP_CRIT 11
74 /* offsets in input string */
75 #define PEM_INPUT_VOLTAGE 0
76 #define PEM_INPUT_POWER_LSB 1
77 #define PEM_INPUT_POWER_MSB 2
79 /* offsets in fan data */
80 #define PEM_FAN_ADJUSTMENT 0
81 #define PEM_FAN_FAN1 1
82 #define PEM_FAN_FAN2 2
83 #define PEM_FAN_FAN3 3
85 /* Status register bits */
86 #define STS1_OUTPUT_ON (1 << 0)
87 #define STS1_LEDS_FLASHING (1 << 1)
88 #define STS1_EXT_FAULT (1 << 2)
89 #define STS1_SERVICE_LED_ON (1 << 3)
90 #define STS1_SHUTDOWN_OCCURRED (1 << 4)
91 #define STS1_INT_FAULT (1 << 5)
92 #define STS1_ISOLATION_TEST_OK (1 << 6)
94 #define STS2_ENABLE_PIN_HI (1 << 0)
95 #define STS2_DATA_OUT_RANGE (1 << 1)
96 #define STS2_RESTARTED_OK (1 << 1)
97 #define STS2_ISOLATION_TEST_FAIL (1 << 3)
98 #define STS2_HIGH_POWER_CAP (1 << 4)
99 #define STS2_INVALID_INSTR (1 << 5)
100 #define STS2_WILL_RESTART (1 << 6)
101 #define STS2_PEC_ERR (1 << 7)
103 /* Alarm register bits */
104 #define ALRM1_VIN_OUT_LIMIT (1 << 0)
105 #define ALRM1_VOUT_OUT_LIMIT (1 << 1)
106 #define ALRM1_OV_VOLT_SHUTDOWN (1 << 2)
107 #define ALRM1_VIN_OVERCURRENT (1 << 3)
108 #define ALRM1_TEMP_WARNING (1 << 4)
109 #define ALRM1_TEMP_SHUTDOWN (1 << 5)
110 #define ALRM1_PRIMARY_FAULT (1 << 6)
111 #define ALRM1_POWER_LIMIT (1 << 7)
113 #define ALRM2_5V_OUT_LIMIT (1 << 1)
114 #define ALRM2_TEMP_FAULT (1 << 2)
115 #define ALRM2_OV_LOW (1 << 3)
116 #define ALRM2_DCDC_TEMP_HIGH (1 << 4)
117 #define ALRM2_PRI_TEMP_HIGH (1 << 5)
118 #define ALRM2_NO_PRIMARY (1 << 6)
119 #define ALRM2_FAN_FAULT (1 << 7)
121 #define FIRMWARE_REV_LEN 4
122 #define DATA_STRING_LEN 9
123 #define INPUT_STRING_LEN 5 /* 4 for most devices */
124 #define FAN_SPEED_LEN 5
127 struct device
*hwmon_dev
;
129 struct mutex update_lock
;
133 unsigned long last_updated
; /* in jiffies */
135 u8 firmware_rev
[FIRMWARE_REV_LEN
];
136 u8 data_string
[DATA_STRING_LEN
];
137 u8 input_string
[INPUT_STRING_LEN
];
138 u8 fan_speed
[FAN_SPEED_LEN
];
141 static int pem_read_block(struct i2c_client
*client
, u8 command
, u8
*data
,
144 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
];
147 result
= i2c_smbus_read_block_data(client
, command
, block_buffer
);
148 if (unlikely(result
< 0))
150 if (unlikely(result
== 0xff || result
!= data_len
)) {
154 memcpy(data
, block_buffer
, data_len
);
160 static struct pem_data
*pem_update_device(struct device
*dev
)
162 struct i2c_client
*client
= to_i2c_client(dev
);
163 struct pem_data
*data
= i2c_get_clientdata(client
);
164 struct pem_data
*ret
= data
;
166 mutex_lock(&data
->update_lock
);
168 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
171 /* Read data string */
172 result
= pem_read_block(client
, PEM_READ_DATA_STRING
,
174 sizeof(data
->data_string
));
175 if (unlikely(result
< 0)) {
176 ret
= ERR_PTR(result
);
180 /* Read input string */
181 if (data
->input_length
) {
182 result
= pem_read_block(client
, PEM_READ_INPUT_STRING
,
185 if (unlikely(result
< 0)) {
186 ret
= ERR_PTR(result
);
191 /* Read fan speeds */
192 if (data
->fans_supported
) {
193 result
= pem_read_block(client
, PEM_READ_FAN_SPEED
,
195 sizeof(data
->fan_speed
));
196 if (unlikely(result
< 0)) {
197 ret
= ERR_PTR(result
);
202 i2c_smbus_write_byte(client
, PEM_CLEAR_INFO_FLAGS
);
204 data
->last_updated
= jiffies
;
208 mutex_unlock(&data
->update_lock
);
212 static long pem_get_data(u8
*data
, int len
, int index
)
217 case PEM_DATA_VOUT_LSB
:
218 val
= (data
[index
] + (data
[index
+1] << 8)) * 5 / 2;
220 case PEM_DATA_CURRENT
:
221 val
= data
[index
] * 200;
224 val
= data
[index
] * 1000;
226 case PEM_DATA_TEMP_MAX
:
227 val
= 97 * 1000; /* 97 degrees C per datasheet */
229 case PEM_DATA_TEMP_CRIT
:
230 val
= 107 * 1000; /* 107 degrees C per datasheet */
239 static long pem_get_input(u8
*data
, int len
, int index
)
244 case PEM_INPUT_VOLTAGE
:
245 if (len
== INPUT_STRING_LEN
)
246 val
= (data
[index
] + (data
[index
+1] << 8) - 75) * 1000;
248 val
= (data
[index
] - 75) * 1000;
250 case PEM_INPUT_POWER_LSB
:
251 if (len
== INPUT_STRING_LEN
)
253 val
= (data
[index
] + (data
[index
+1] << 8)) * 1000000L;
262 static long pem_get_fan(u8
*data
, int len
, int index
)
270 val
= data
[index
] * 100;
280 * Show boolean, either a fault or an alarm.
281 * .nr points to the register, .index is the bit mask to check
283 static ssize_t
pem_show_bool(struct device
*dev
,
284 struct device_attribute
*da
, char *buf
)
286 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(da
);
287 struct pem_data
*data
= pem_update_device(dev
);
291 return PTR_ERR(data
);
293 status
= data
->data_string
[attr
->nr
] & attr
->index
;
294 return snprintf(buf
, PAGE_SIZE
, "%d\n", !!status
);
297 static ssize_t
pem_show_data(struct device
*dev
, struct device_attribute
*da
,
300 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
301 struct pem_data
*data
= pem_update_device(dev
);
305 return PTR_ERR(data
);
307 value
= pem_get_data(data
->data_string
, sizeof(data
->data_string
),
310 return snprintf(buf
, PAGE_SIZE
, "%ld\n", value
);
313 static ssize_t
pem_show_input(struct device
*dev
, struct device_attribute
*da
,
316 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
317 struct pem_data
*data
= pem_update_device(dev
);
321 return PTR_ERR(data
);
323 value
= pem_get_input(data
->input_string
, sizeof(data
->input_string
),
326 return snprintf(buf
, PAGE_SIZE
, "%ld\n", value
);
329 static ssize_t
pem_show_fan(struct device
*dev
, struct device_attribute
*da
,
332 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
333 struct pem_data
*data
= pem_update_device(dev
);
337 return PTR_ERR(data
);
339 value
= pem_get_fan(data
->fan_speed
, sizeof(data
->fan_speed
),
342 return snprintf(buf
, PAGE_SIZE
, "%ld\n", value
);
346 static SENSOR_DEVICE_ATTR(in1_input
, S_IRUGO
, pem_show_data
, NULL
,
348 static SENSOR_DEVICE_ATTR_2(in1_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
349 PEM_DATA_ALARM_1
, ALRM1_VOUT_OUT_LIMIT
);
350 static SENSOR_DEVICE_ATTR_2(in1_crit_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
351 PEM_DATA_ALARM_1
, ALRM1_OV_VOLT_SHUTDOWN
);
352 static SENSOR_DEVICE_ATTR(in2_input
, S_IRUGO
, pem_show_input
, NULL
,
354 static SENSOR_DEVICE_ATTR_2(in2_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
356 ALRM1_VIN_OUT_LIMIT
| ALRM1_PRIMARY_FAULT
);
359 static SENSOR_DEVICE_ATTR(curr1_input
, S_IRUGO
, pem_show_data
, NULL
,
361 static SENSOR_DEVICE_ATTR_2(curr1_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
362 PEM_DATA_ALARM_1
, ALRM1_VIN_OVERCURRENT
);
365 static SENSOR_DEVICE_ATTR(power1_input
, S_IRUGO
, pem_show_input
, NULL
,
366 PEM_INPUT_POWER_LSB
);
367 static SENSOR_DEVICE_ATTR_2(power1_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
368 PEM_DATA_ALARM_1
, ALRM1_POWER_LIMIT
);
371 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, pem_show_fan
, NULL
,
373 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, pem_show_fan
, NULL
,
375 static SENSOR_DEVICE_ATTR(fan3_input
, S_IRUGO
, pem_show_fan
, NULL
,
377 static SENSOR_DEVICE_ATTR_2(fan1_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
378 PEM_DATA_ALARM_2
, ALRM2_FAN_FAULT
);
381 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, pem_show_data
, NULL
,
383 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
, pem_show_data
, NULL
,
385 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IRUGO
, pem_show_data
, NULL
,
387 static SENSOR_DEVICE_ATTR_2(temp1_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
388 PEM_DATA_ALARM_1
, ALRM1_TEMP_WARNING
);
389 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm
, S_IRUGO
, pem_show_bool
, NULL
,
390 PEM_DATA_ALARM_1
, ALRM1_TEMP_SHUTDOWN
);
391 static SENSOR_DEVICE_ATTR_2(temp1_fault
, S_IRUGO
, pem_show_bool
, NULL
,
392 PEM_DATA_ALARM_2
, ALRM2_TEMP_FAULT
);
394 static struct attribute
*pem_attributes
[] = {
395 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
396 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
397 &sensor_dev_attr_in1_crit_alarm
.dev_attr
.attr
,
398 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
400 &sensor_dev_attr_curr1_alarm
.dev_attr
.attr
,
402 &sensor_dev_attr_power1_alarm
.dev_attr
.attr
,
404 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
406 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
407 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
408 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
409 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
410 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
411 &sensor_dev_attr_temp1_fault
.dev_attr
.attr
,
416 static const struct attribute_group pem_group
= {
417 .attrs
= pem_attributes
,
420 static struct attribute
*pem_input_attributes
[] = {
421 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
422 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
423 &sensor_dev_attr_power1_input
.dev_attr
.attr
,
426 static const struct attribute_group pem_input_group
= {
427 .attrs
= pem_input_attributes
,
430 static struct attribute
*pem_fan_attributes
[] = {
431 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
432 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
433 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
436 static const struct attribute_group pem_fan_group
= {
437 .attrs
= pem_fan_attributes
,
440 static int pem_probe(struct i2c_client
*client
,
441 const struct i2c_device_id
*id
)
443 struct i2c_adapter
*adapter
= client
->adapter
;
444 struct pem_data
*data
;
447 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BLOCK_DATA
448 | I2C_FUNC_SMBUS_WRITE_BYTE
))
451 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
455 i2c_set_clientdata(client
, data
);
456 mutex_init(&data
->update_lock
);
459 * We use the next two commands to determine if the device is really
462 ret
= pem_read_block(client
, PEM_READ_FIRMWARE_REV
,
463 data
->firmware_rev
, sizeof(data
->firmware_rev
));
467 ret
= i2c_smbus_write_byte(client
, PEM_CLEAR_INFO_FLAGS
);
471 dev_info(&client
->dev
, "Firmware revision %d.%d.%d\n",
472 data
->firmware_rev
[0], data
->firmware_rev
[1],
473 data
->firmware_rev
[2]);
475 /* Register sysfs hooks */
476 ret
= sysfs_create_group(&client
->dev
.kobj
, &pem_group
);
481 * Check if input readings are supported.
482 * This is the case if we can read input data,
483 * and if the returned data is not all zeros.
484 * Note that input alarms are always supported.
486 ret
= pem_read_block(client
, PEM_READ_INPUT_STRING
,
488 sizeof(data
->input_string
) - 1);
489 if (!ret
&& (data
->input_string
[0] || data
->input_string
[1] ||
490 data
->input_string
[2]))
491 data
->input_length
= sizeof(data
->input_string
) - 1;
493 /* Input string is one byte longer for some devices */
494 ret
= pem_read_block(client
, PEM_READ_INPUT_STRING
,
496 sizeof(data
->input_string
));
497 if (!ret
&& (data
->input_string
[0] || data
->input_string
[1] ||
498 data
->input_string
[2] || data
->input_string
[3]))
499 data
->input_length
= sizeof(data
->input_string
);
502 if (data
->input_length
) {
503 ret
= sysfs_create_group(&client
->dev
.kobj
, &pem_input_group
);
505 goto out_remove_groups
;
509 * Check if fan speed readings are supported.
510 * This is the case if we can read fan speed data,
511 * and if the returned data is not all zeros.
512 * Note that the fan alarm is always supported.
514 ret
= pem_read_block(client
, PEM_READ_FAN_SPEED
,
516 sizeof(data
->fan_speed
));
517 if (!ret
&& (data
->fan_speed
[0] || data
->fan_speed
[1] ||
518 data
->fan_speed
[2] || data
->fan_speed
[3])) {
519 data
->fans_supported
= true;
520 ret
= sysfs_create_group(&client
->dev
.kobj
, &pem_fan_group
);
522 goto out_remove_groups
;
525 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
526 if (IS_ERR(data
->hwmon_dev
)) {
527 ret
= PTR_ERR(data
->hwmon_dev
);
528 goto out_remove_groups
;
534 sysfs_remove_group(&client
->dev
.kobj
, &pem_input_group
);
535 sysfs_remove_group(&client
->dev
.kobj
, &pem_fan_group
);
536 sysfs_remove_group(&client
->dev
.kobj
, &pem_group
);
542 static int pem_remove(struct i2c_client
*client
)
544 struct pem_data
*data
= i2c_get_clientdata(client
);
546 hwmon_device_unregister(data
->hwmon_dev
);
548 sysfs_remove_group(&client
->dev
.kobj
, &pem_input_group
);
549 sysfs_remove_group(&client
->dev
.kobj
, &pem_fan_group
);
550 sysfs_remove_group(&client
->dev
.kobj
, &pem_group
);
556 static const struct i2c_device_id pem_id
[] = {
560 MODULE_DEVICE_TABLE(i2c
, pem_id
);
562 static struct i2c_driver pem_driver
= {
564 .name
= "lineage_pem",
567 .remove
= pem_remove
,
571 static int __init
pem_init(void)
573 return i2c_add_driver(&pem_driver
);
576 static void __exit
pem_exit(void)
578 i2c_del_driver(&pem_driver
);
581 MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
582 MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
583 MODULE_LICENSE("GPL");
585 module_init(pem_init
);
586 module_exit(pem_exit
);