blob: d264937c7f5e09f8fa9c2cf453d6c4e4b37bb3bf [file] [log] [blame]
Guenter Roeck502b5a02010-09-29 20:12:08 -07001/*
2 * Driver for Lineage Compact Power Line series of power entry modules.
3 *
4 * Copyright (C) 2010, 2011 Ericsson AB.
5 *
6 * Documentation:
7 * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
8 *
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.
13 *
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.
18 *
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.
22 */
23
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>
32
33/*
34 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
35 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
36 *
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.
41 *
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.
45 */
46
47/* Command codes */
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
59
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
69
70/* Virtual entries, to report constants */
71#define PEM_DATA_TEMP_MAX 10
72#define PEM_DATA_TEMP_CRIT 11
73
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
78
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
84
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)
93
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)
102
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)
112
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)
120
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
125
126struct pem_data {
127 struct device *hwmon_dev;
128
129 struct mutex update_lock;
130 bool valid;
131 bool fans_supported;
132 int input_length;
133 unsigned long last_updated; /* in jiffies */
134
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];
139};
140
141static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
142 int data_len)
143{
144 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
145 int result;
146
147 result = i2c_smbus_read_block_data(client, command, block_buffer);
148 if (unlikely(result < 0))
149 goto abort;
150 if (unlikely(result == 0xff || result != data_len)) {
151 result = -EIO;
152 goto abort;
153 }
154 memcpy(data, block_buffer, data_len);
155 result = 0;
156abort:
157 return result;
158}
159
160static struct pem_data *pem_update_device(struct device *dev)
161{
162 struct i2c_client *client = to_i2c_client(dev);
163 struct pem_data *data = i2c_get_clientdata(client);
164 struct pem_data *ret = data;
165
166 mutex_lock(&data->update_lock);
167
168 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
169 int result;
170
171 /* Read data string */
172 result = pem_read_block(client, PEM_READ_DATA_STRING,
173 data->data_string,
174 sizeof(data->data_string));
175 if (unlikely(result < 0)) {
176 ret = ERR_PTR(result);
177 goto abort;
178 }
179
180 /* Read input string */
181 if (data->input_length) {
182 result = pem_read_block(client, PEM_READ_INPUT_STRING,
183 data->input_string,
184 data->input_length);
185 if (unlikely(result < 0)) {
186 ret = ERR_PTR(result);
187 goto abort;
188 }
189 }
190
191 /* Read fan speeds */
192 if (data->fans_supported) {
193 result = pem_read_block(client, PEM_READ_FAN_SPEED,
194 data->fan_speed,
195 sizeof(data->fan_speed));
196 if (unlikely(result < 0)) {
197 ret = ERR_PTR(result);
198 goto abort;
199 }
200 }
201
202 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
203
204 data->last_updated = jiffies;
205 data->valid = 1;
206 }
207abort:
208 mutex_unlock(&data->update_lock);
209 return ret;
210}
211
212static long pem_get_data(u8 *data, int len, int index)
213{
214 long val;
215
216 switch (index) {
217 case PEM_DATA_VOUT_LSB:
218 val = (data[index] + (data[index+1] << 8)) * 5 / 2;
219 break;
220 case PEM_DATA_CURRENT:
221 val = data[index] * 200;
222 break;
223 case PEM_DATA_TEMP:
224 val = data[index] * 1000;
225 break;
226 case PEM_DATA_TEMP_MAX:
227 val = 97 * 1000; /* 97 degrees C per datasheet */
228 break;
229 case PEM_DATA_TEMP_CRIT:
230 val = 107 * 1000; /* 107 degrees C per datasheet */
231 break;
232 default:
233 WARN_ON_ONCE(1);
234 val = 0;
235 }
236 return val;
237}
238
239static long pem_get_input(u8 *data, int len, int index)
240{
241 long val;
242
243 switch (index) {
244 case PEM_INPUT_VOLTAGE:
245 if (len == INPUT_STRING_LEN)
246 val = (data[index] + (data[index+1] << 8) - 75) * 1000;
247 else
248 val = (data[index] - 75) * 1000;
249 break;
250 case PEM_INPUT_POWER_LSB:
251 if (len == INPUT_STRING_LEN)
252 index++;
253 val = (data[index] + (data[index+1] << 8)) * 1000000L;
254 break;
255 default:
256 WARN_ON_ONCE(1);
257 val = 0;
258 }
259 return val;
260}
261
262static long pem_get_fan(u8 *data, int len, int index)
263{
264 long val;
265
266 switch (index) {
267 case PEM_FAN_FAN1:
268 case PEM_FAN_FAN2:
269 case PEM_FAN_FAN3:
270 val = data[index] * 100;
271 break;
272 default:
273 WARN_ON_ONCE(1);
274 val = 0;
275 }
276 return val;
277}
278
279/*
280 * Show boolean, either a fault or an alarm.
281 * .nr points to the register, .index is the bit mask to check
282 */
283static ssize_t pem_show_bool(struct device *dev,
284 struct device_attribute *da, char *buf)
285{
286 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
287 struct pem_data *data = pem_update_device(dev);
288 u8 status;
289
290 if (IS_ERR(data))
291 return PTR_ERR(data);
292
293 status = data->data_string[attr->nr] & attr->index;
294 return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
295}
296
297static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
298 char *buf)
299{
300 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
301 struct pem_data *data = pem_update_device(dev);
302 long value;
303
304 if (IS_ERR(data))
305 return PTR_ERR(data);
306
307 value = pem_get_data(data->data_string, sizeof(data->data_string),
308 attr->index);
309
310 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
311}
312
313static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
314 char *buf)
315{
316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
317 struct pem_data *data = pem_update_device(dev);
318 long value;
319
320 if (IS_ERR(data))
321 return PTR_ERR(data);
322
323 value = pem_get_input(data->input_string, sizeof(data->input_string),
324 attr->index);
325
326 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
327}
328
329static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
330 char *buf)
331{
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
333 struct pem_data *data = pem_update_device(dev);
334 long value;
335
336 if (IS_ERR(data))
337 return PTR_ERR(data);
338
339 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
340 attr->index);
341
342 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
343}
344
345/* Voltages */
346static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
347 PEM_DATA_VOUT_LSB);
Guenter Roeckd668a8b2011-03-15 16:34:07 -0700348static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
Guenter Roeck502b5a02010-09-29 20:12:08 -0700349 PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
350static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
351 PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
352static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
353 PEM_INPUT_VOLTAGE);
354static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL,
355 PEM_DATA_ALARM_1,
356 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
357
358/* Currents */
359static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
360 PEM_DATA_CURRENT);
361static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
362 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
363
364/* Power */
365static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
366 PEM_INPUT_POWER_LSB);
367static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
368 PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
369
370/* Fans */
371static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
372 PEM_FAN_FAN1);
373static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
374 PEM_FAN_FAN2);
375static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
376 PEM_FAN_FAN3);
377static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
378 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
379
380/* Temperatures */
381static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
382 PEM_DATA_TEMP);
383static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
384 PEM_DATA_TEMP_MAX);
385static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
386 PEM_DATA_TEMP_CRIT);
387static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
388 PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
389static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
390 PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
391static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
392 PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
393
394static struct attribute *pem_attributes[] = {
395 &sensor_dev_attr_in1_input.dev_attr.attr,
Guenter Roeckd668a8b2011-03-15 16:34:07 -0700396 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck502b5a02010-09-29 20:12:08 -0700397 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
398 &sensor_dev_attr_in2_alarm.dev_attr.attr,
399
400 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
401
402 &sensor_dev_attr_power1_alarm.dev_attr.attr,
403
404 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
405
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,
412
413 NULL,
414};
415
416static const struct attribute_group pem_group = {
417 .attrs = pem_attributes,
418};
419
420static 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,
424};
425
426static const struct attribute_group pem_input_group = {
427 .attrs = pem_input_attributes,
428};
429
430static 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,
434};
435
436static const struct attribute_group pem_fan_group = {
437 .attrs = pem_fan_attributes,
438};
439
440static int pem_probe(struct i2c_client *client,
441 const struct i2c_device_id *id)
442{
443 struct i2c_adapter *adapter = client->adapter;
444 struct pem_data *data;
445 int ret;
446
447 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
448 | I2C_FUNC_SMBUS_WRITE_BYTE))
449 return -ENODEV;
450
Guenter Roeck07404aa2012-02-22 08:56:45 -0800451 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700452 if (!data)
453 return -ENOMEM;
454
455 i2c_set_clientdata(client, data);
456 mutex_init(&data->update_lock);
457
458 /*
459 * We use the next two commands to determine if the device is really
460 * there.
461 */
462 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
463 data->firmware_rev, sizeof(data->firmware_rev));
464 if (ret < 0)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800465 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700466
467 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
468 if (ret < 0)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800469 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700470
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]);
474
475 /* Register sysfs hooks */
476 ret = sysfs_create_group(&client->dev.kobj, &pem_group);
477 if (ret)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800478 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700479
480 /*
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.
485 */
486 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
487 data->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;
492 else if (ret < 0) {
493 /* Input string is one byte longer for some devices */
494 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
495 data->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);
500 }
501 ret = 0;
502 if (data->input_length) {
503 ret = sysfs_create_group(&client->dev.kobj, &pem_input_group);
504 if (ret)
505 goto out_remove_groups;
506 }
507
508 /*
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.
513 */
514 ret = pem_read_block(client, PEM_READ_FAN_SPEED,
515 data->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);
521 if (ret)
522 goto out_remove_groups;
523 }
524
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;
529 }
530
531 return 0;
532
533out_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);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700537 return ret;
538}
539
540static int pem_remove(struct i2c_client *client)
541{
542 struct pem_data *data = i2c_get_clientdata(client);
543
544 hwmon_device_unregister(data->hwmon_dev);
545
546 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
547 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
548 sysfs_remove_group(&client->dev.kobj, &pem_group);
549
Guenter Roeck502b5a02010-09-29 20:12:08 -0700550 return 0;
551}
552
553static const struct i2c_device_id pem_id[] = {
554 {"lineage_pem", 0},
555 {}
556};
557MODULE_DEVICE_TABLE(i2c, pem_id);
558
559static struct i2c_driver pem_driver = {
560 .driver = {
561 .name = "lineage_pem",
562 },
563 .probe = pem_probe,
564 .remove = pem_remove,
565 .id_table = pem_id,
566};
567
Axel Linf0967ee2012-01-20 15:38:18 +0800568module_i2c_driver(pem_driver);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700569
570MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
571MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
572MODULE_LICENSE("GPL");