1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Lineage Compact Power Line series of power entry modules.
5 * Copyright (C) 2010, 2011 Ericsson AB.
8 * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/jiffies.h>
22 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
23 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
25 * The devices are nominally PMBus compliant. However, most standard PMBus
26 * commands are not supported. Specifically, all hardware monitoring and
27 * status reporting commands are non-standard. For this reason, a standard
28 * PMBus driver can not be used.
30 * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
31 * To ensure device access, this driver should only be used as client driver
32 * to the pca9541 I2C master selector driver.
36 #define PEM_OPERATION 0x01
37 #define PEM_CLEAR_INFO_FLAGS 0x03
38 #define PEM_VOUT_COMMAND 0x21
39 #define PEM_VOUT_OV_FAULT_LIMIT 0x40
40 #define PEM_READ_DATA_STRING 0xd0
41 #define PEM_READ_INPUT_STRING 0xdc
42 #define PEM_READ_FIRMWARE_REV 0xdd
43 #define PEM_READ_RUN_TIMER 0xde
44 #define PEM_FAN_HI_SPEED 0xdf
45 #define PEM_FAN_NORMAL_SPEED 0xe0
46 #define PEM_READ_FAN_SPEED 0xe1
48 /* offsets in data string */
49 #define PEM_DATA_STATUS_2 0
50 #define PEM_DATA_STATUS_1 1
51 #define PEM_DATA_ALARM_2 2
52 #define PEM_DATA_ALARM_1 3
53 #define PEM_DATA_VOUT_LSB 4
54 #define PEM_DATA_VOUT_MSB 5
55 #define PEM_DATA_CURRENT 6
56 #define PEM_DATA_TEMP 7
58 /* Virtual entries, to report constants */
59 #define PEM_DATA_TEMP_MAX 10
60 #define PEM_DATA_TEMP_CRIT 11
62 /* offsets in input string */
63 #define PEM_INPUT_VOLTAGE 0
64 #define PEM_INPUT_POWER_LSB 1
65 #define PEM_INPUT_POWER_MSB 2
67 /* offsets in fan data */
68 #define PEM_FAN_ADJUSTMENT 0
69 #define PEM_FAN_FAN1 1
70 #define PEM_FAN_FAN2 2
71 #define PEM_FAN_FAN3 3
73 /* Status register bits */
74 #define STS1_OUTPUT_ON (1 << 0)
75 #define STS1_LEDS_FLASHING (1 << 1)
76 #define STS1_EXT_FAULT (1 << 2)
77 #define STS1_SERVICE_LED_ON (1 << 3)
78 #define STS1_SHUTDOWN_OCCURRED (1 << 4)
79 #define STS1_INT_FAULT (1 << 5)
80 #define STS1_ISOLATION_TEST_OK (1 << 6)
82 #define STS2_ENABLE_PIN_HI (1 << 0)
83 #define STS2_DATA_OUT_RANGE (1 << 1)
84 #define STS2_RESTARTED_OK (1 << 1)
85 #define STS2_ISOLATION_TEST_FAIL (1 << 3)
86 #define STS2_HIGH_POWER_CAP (1 << 4)
87 #define STS2_INVALID_INSTR (1 << 5)
88 #define STS2_WILL_RESTART (1 << 6)
89 #define STS2_PEC_ERR (1 << 7)
91 /* Alarm register bits */
92 #define ALRM1_VIN_OUT_LIMIT (1 << 0)
93 #define ALRM1_VOUT_OUT_LIMIT (1 << 1)
94 #define ALRM1_OV_VOLT_SHUTDOWN (1 << 2)
95 #define ALRM1_VIN_OVERCURRENT (1 << 3)
96 #define ALRM1_TEMP_WARNING (1 << 4)
97 #define ALRM1_TEMP_SHUTDOWN (1 << 5)
98 #define ALRM1_PRIMARY_FAULT (1 << 6)
99 #define ALRM1_POWER_LIMIT (1 << 7)
101 #define ALRM2_5V_OUT_LIMIT (1 << 1)
102 #define ALRM2_TEMP_FAULT (1 << 2)
103 #define ALRM2_OV_LOW (1 << 3)
104 #define ALRM2_DCDC_TEMP_HIGH (1 << 4)
105 #define ALRM2_PRI_TEMP_HIGH (1 << 5)
106 #define ALRM2_NO_PRIMARY (1 << 6)
107 #define ALRM2_FAN_FAULT (1 << 7)
109 #define FIRMWARE_REV_LEN 4
110 #define DATA_STRING_LEN 9
111 #define INPUT_STRING_LEN 5 /* 4 for most devices */
112 #define FAN_SPEED_LEN 5
115 struct i2c_client *client;
116 const struct attribute_group *groups[4];
118 struct mutex update_lock;
122 unsigned long last_updated; /* in jiffies */
124 u8 firmware_rev[FIRMWARE_REV_LEN];
125 u8 data_string[DATA_STRING_LEN];
126 u8 input_string[INPUT_STRING_LEN];
127 u8 fan_speed[FAN_SPEED_LEN];
130 static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
133 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
136 result = i2c_smbus_read_block_data(client, command, block_buffer);
137 if (unlikely(result < 0))
139 if (unlikely(result == 0xff || result != data_len)) {
143 memcpy(data, block_buffer, data_len);
149 static struct pem_data *pem_update_device(struct device *dev)
151 struct pem_data *data = dev_get_drvdata(dev);
152 struct i2c_client *client = data->client;
153 struct pem_data *ret = data;
155 mutex_lock(&data->update_lock);
157 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
160 /* Read data string */
161 result = pem_read_block(client, PEM_READ_DATA_STRING,
163 sizeof(data->data_string));
164 if (unlikely(result < 0)) {
165 ret = ERR_PTR(result);
169 /* Read input string */
170 if (data->input_length) {
171 result = pem_read_block(client, PEM_READ_INPUT_STRING,
174 if (unlikely(result < 0)) {
175 ret = ERR_PTR(result);
180 /* Read fan speeds */
181 if (data->fans_supported) {
182 result = pem_read_block(client, PEM_READ_FAN_SPEED,
184 sizeof(data->fan_speed));
185 if (unlikely(result < 0)) {
186 ret = ERR_PTR(result);
191 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
193 data->last_updated = jiffies;
197 mutex_unlock(&data->update_lock);
201 static long pem_get_data(u8 *data, int len, int index)
206 case PEM_DATA_VOUT_LSB:
207 val = (data[index] + (data[index+1] << 8)) * 5 / 2;
209 case PEM_DATA_CURRENT:
210 val = data[index] * 200;
213 val = data[index] * 1000;
215 case PEM_DATA_TEMP_MAX:
216 val = 97 * 1000; /* 97 degrees C per datasheet */
218 case PEM_DATA_TEMP_CRIT:
219 val = 107 * 1000; /* 107 degrees C per datasheet */
228 static long pem_get_input(u8 *data, int len, int index)
233 case PEM_INPUT_VOLTAGE:
234 if (len == INPUT_STRING_LEN)
235 val = (data[index] + (data[index+1] << 8) - 75) * 1000;
237 val = (data[index] - 75) * 1000;
239 case PEM_INPUT_POWER_LSB:
240 if (len == INPUT_STRING_LEN)
242 val = (data[index] + (data[index+1] << 8)) * 1000000L;
251 static long pem_get_fan(u8 *data, int len, int index)
259 val = data[index] * 100;
269 * Show boolean, either a fault or an alarm.
270 * .nr points to the register, .index is the bit mask to check
272 static ssize_t pem_bool_show(struct device *dev, struct device_attribute *da,
275 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
276 struct pem_data *data = pem_update_device(dev);
280 return PTR_ERR(data);
282 status = data->data_string[attr->nr] & attr->index;
283 return sysfs_emit(buf, "%d\n", !!status);
286 static ssize_t pem_data_show(struct device *dev, struct device_attribute *da,
289 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
290 struct pem_data *data = pem_update_device(dev);
294 return PTR_ERR(data);
296 value = pem_get_data(data->data_string, sizeof(data->data_string),
299 return sysfs_emit(buf, "%ld\n", value);
302 static ssize_t pem_input_show(struct device *dev, struct device_attribute *da,
305 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
306 struct pem_data *data = pem_update_device(dev);
310 return PTR_ERR(data);
312 value = pem_get_input(data->input_string, sizeof(data->input_string),
315 return sysfs_emit(buf, "%ld\n", value);
318 static ssize_t pem_fan_show(struct device *dev, struct device_attribute *da,
321 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
322 struct pem_data *data = pem_update_device(dev);
326 return PTR_ERR(data);
328 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
331 return sysfs_emit(buf, "%ld\n", value);
335 static SENSOR_DEVICE_ATTR_RO(in1_input, pem_data, PEM_DATA_VOUT_LSB);
336 static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, pem_bool, PEM_DATA_ALARM_1,
337 ALRM1_VOUT_OUT_LIMIT);
338 static SENSOR_DEVICE_ATTR_2_RO(in1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
339 ALRM1_OV_VOLT_SHUTDOWN);
340 static SENSOR_DEVICE_ATTR_RO(in2_input, pem_input, PEM_INPUT_VOLTAGE);
341 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, pem_bool, PEM_DATA_ALARM_1,
342 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
345 static SENSOR_DEVICE_ATTR_RO(curr1_input, pem_data, PEM_DATA_CURRENT);
346 static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, pem_bool, PEM_DATA_ALARM_1,
347 ALRM1_VIN_OVERCURRENT);
350 static SENSOR_DEVICE_ATTR_RO(power1_input, pem_input, PEM_INPUT_POWER_LSB);
351 static SENSOR_DEVICE_ATTR_2_RO(power1_alarm, pem_bool, PEM_DATA_ALARM_1,
355 static SENSOR_DEVICE_ATTR_RO(fan1_input, pem_fan, PEM_FAN_FAN1);
356 static SENSOR_DEVICE_ATTR_RO(fan2_input, pem_fan, PEM_FAN_FAN2);
357 static SENSOR_DEVICE_ATTR_RO(fan3_input, pem_fan, PEM_FAN_FAN3);
358 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, pem_bool, PEM_DATA_ALARM_2,
362 static SENSOR_DEVICE_ATTR_RO(temp1_input, pem_data, PEM_DATA_TEMP);
363 static SENSOR_DEVICE_ATTR_RO(temp1_max, pem_data, PEM_DATA_TEMP_MAX);
364 static SENSOR_DEVICE_ATTR_RO(temp1_crit, pem_data, PEM_DATA_TEMP_CRIT);
365 static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, pem_bool, PEM_DATA_ALARM_1,
367 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
368 ALRM1_TEMP_SHUTDOWN);
369 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, pem_bool, PEM_DATA_ALARM_2,
372 static struct attribute *pem_attributes[] = {
373 &sensor_dev_attr_in1_input.dev_attr.attr,
374 &sensor_dev_attr_in1_alarm.dev_attr.attr,
375 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
376 &sensor_dev_attr_in2_alarm.dev_attr.attr,
378 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
380 &sensor_dev_attr_power1_alarm.dev_attr.attr,
382 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
384 &sensor_dev_attr_temp1_input.dev_attr.attr,
385 &sensor_dev_attr_temp1_max.dev_attr.attr,
386 &sensor_dev_attr_temp1_crit.dev_attr.attr,
387 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
388 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
389 &sensor_dev_attr_temp1_fault.dev_attr.attr,
394 static const struct attribute_group pem_group = {
395 .attrs = pem_attributes,
398 static struct attribute *pem_input_attributes[] = {
399 &sensor_dev_attr_in2_input.dev_attr.attr,
400 &sensor_dev_attr_curr1_input.dev_attr.attr,
401 &sensor_dev_attr_power1_input.dev_attr.attr,
405 static const struct attribute_group pem_input_group = {
406 .attrs = pem_input_attributes,
409 static struct attribute *pem_fan_attributes[] = {
410 &sensor_dev_attr_fan1_input.dev_attr.attr,
411 &sensor_dev_attr_fan2_input.dev_attr.attr,
412 &sensor_dev_attr_fan3_input.dev_attr.attr,
416 static const struct attribute_group pem_fan_group = {
417 .attrs = pem_fan_attributes,
420 static int pem_probe(struct i2c_client *client)
422 struct i2c_adapter *adapter = client->adapter;
423 struct device *dev = &client->dev;
424 struct device *hwmon_dev;
425 struct pem_data *data;
428 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
429 | I2C_FUNC_SMBUS_WRITE_BYTE))
432 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
436 data->client = client;
437 mutex_init(&data->update_lock);
440 * We use the next two commands to determine if the device is really
443 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
444 data->firmware_rev, sizeof(data->firmware_rev));
448 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
452 dev_info(dev, "Firmware revision %d.%d.%d\n",
453 data->firmware_rev[0], data->firmware_rev[1],
454 data->firmware_rev[2]);
457 data->groups[idx++] = &pem_group;
460 * Check if input readings are supported.
461 * This is the case if we can read input data,
462 * and if the returned data is not all zeros.
463 * Note that input alarms are always supported.
465 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
467 sizeof(data->input_string) - 1);
468 if (!ret && (data->input_string[0] || data->input_string[1] ||
469 data->input_string[2]))
470 data->input_length = sizeof(data->input_string) - 1;
472 /* Input string is one byte longer for some devices */
473 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
475 sizeof(data->input_string));
476 if (!ret && (data->input_string[0] || data->input_string[1] ||
477 data->input_string[2] || data->input_string[3]))
478 data->input_length = sizeof(data->input_string);
481 if (data->input_length)
482 data->groups[idx++] = &pem_input_group;
485 * Check if fan speed readings are supported.
486 * This is the case if we can read fan speed data,
487 * and if the returned data is not all zeros.
488 * Note that the fan alarm is always supported.
490 ret = pem_read_block(client, PEM_READ_FAN_SPEED,
492 sizeof(data->fan_speed));
493 if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
494 data->fan_speed[2] || data->fan_speed[3])) {
495 data->fans_supported = true;
496 data->groups[idx++] = &pem_fan_group;
499 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
501 return PTR_ERR_OR_ZERO(hwmon_dev);
504 static const struct i2c_device_id pem_id[] = {
508 MODULE_DEVICE_TABLE(i2c, pem_id);
510 static struct i2c_driver pem_driver = {
512 .name = "lineage_pem",
514 .probe_new = pem_probe,
518 module_i2c_driver(pem_driver);
520 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
521 MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
522 MODULE_LICENSE("GPL");