blob: 41df29f59b0e5dd011f8258c228b14650a174f5f [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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020032#include <linux/jiffies.h>
Guenter Roeck502b5a02010-09-29 20:12:08 -070033
34/*
35 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
36 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
37 *
38 * The devices are nominally PMBus compliant. However, most standard PMBus
39 * commands are not supported. Specifically, all hardware monitoring and
40 * status reporting commands are non-standard. For this reason, a standard
41 * PMBus driver can not be used.
42 *
43 * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
44 * To ensure device access, this driver should only be used as client driver
45 * to the pca9541 I2C master selector driver.
46 */
47
48/* Command codes */
49#define PEM_OPERATION 0x01
50#define PEM_CLEAR_INFO_FLAGS 0x03
51#define PEM_VOUT_COMMAND 0x21
52#define PEM_VOUT_OV_FAULT_LIMIT 0x40
53#define PEM_READ_DATA_STRING 0xd0
54#define PEM_READ_INPUT_STRING 0xdc
55#define PEM_READ_FIRMWARE_REV 0xdd
56#define PEM_READ_RUN_TIMER 0xde
57#define PEM_FAN_HI_SPEED 0xdf
58#define PEM_FAN_NORMAL_SPEED 0xe0
59#define PEM_READ_FAN_SPEED 0xe1
60
61/* offsets in data string */
62#define PEM_DATA_STATUS_2 0
63#define PEM_DATA_STATUS_1 1
64#define PEM_DATA_ALARM_2 2
65#define PEM_DATA_ALARM_1 3
66#define PEM_DATA_VOUT_LSB 4
67#define PEM_DATA_VOUT_MSB 5
68#define PEM_DATA_CURRENT 6
69#define PEM_DATA_TEMP 7
70
71/* Virtual entries, to report constants */
72#define PEM_DATA_TEMP_MAX 10
73#define PEM_DATA_TEMP_CRIT 11
74
75/* offsets in input string */
76#define PEM_INPUT_VOLTAGE 0
77#define PEM_INPUT_POWER_LSB 1
78#define PEM_INPUT_POWER_MSB 2
79
80/* offsets in fan data */
81#define PEM_FAN_ADJUSTMENT 0
82#define PEM_FAN_FAN1 1
83#define PEM_FAN_FAN2 2
84#define PEM_FAN_FAN3 3
85
86/* Status register bits */
87#define STS1_OUTPUT_ON (1 << 0)
88#define STS1_LEDS_FLASHING (1 << 1)
89#define STS1_EXT_FAULT (1 << 2)
90#define STS1_SERVICE_LED_ON (1 << 3)
91#define STS1_SHUTDOWN_OCCURRED (1 << 4)
92#define STS1_INT_FAULT (1 << 5)
93#define STS1_ISOLATION_TEST_OK (1 << 6)
94
95#define STS2_ENABLE_PIN_HI (1 << 0)
96#define STS2_DATA_OUT_RANGE (1 << 1)
97#define STS2_RESTARTED_OK (1 << 1)
98#define STS2_ISOLATION_TEST_FAIL (1 << 3)
99#define STS2_HIGH_POWER_CAP (1 << 4)
100#define STS2_INVALID_INSTR (1 << 5)
101#define STS2_WILL_RESTART (1 << 6)
102#define STS2_PEC_ERR (1 << 7)
103
104/* Alarm register bits */
105#define ALRM1_VIN_OUT_LIMIT (1 << 0)
106#define ALRM1_VOUT_OUT_LIMIT (1 << 1)
107#define ALRM1_OV_VOLT_SHUTDOWN (1 << 2)
108#define ALRM1_VIN_OVERCURRENT (1 << 3)
109#define ALRM1_TEMP_WARNING (1 << 4)
110#define ALRM1_TEMP_SHUTDOWN (1 << 5)
111#define ALRM1_PRIMARY_FAULT (1 << 6)
112#define ALRM1_POWER_LIMIT (1 << 7)
113
114#define ALRM2_5V_OUT_LIMIT (1 << 1)
115#define ALRM2_TEMP_FAULT (1 << 2)
116#define ALRM2_OV_LOW (1 << 3)
117#define ALRM2_DCDC_TEMP_HIGH (1 << 4)
118#define ALRM2_PRI_TEMP_HIGH (1 << 5)
119#define ALRM2_NO_PRIMARY (1 << 6)
120#define ALRM2_FAN_FAULT (1 << 7)
121
122#define FIRMWARE_REV_LEN 4
123#define DATA_STRING_LEN 9
124#define INPUT_STRING_LEN 5 /* 4 for most devices */
125#define FAN_SPEED_LEN 5
126
127struct pem_data {
128 struct device *hwmon_dev;
129
130 struct mutex update_lock;
131 bool valid;
132 bool fans_supported;
133 int input_length;
134 unsigned long last_updated; /* in jiffies */
135
136 u8 firmware_rev[FIRMWARE_REV_LEN];
137 u8 data_string[DATA_STRING_LEN];
138 u8 input_string[INPUT_STRING_LEN];
139 u8 fan_speed[FAN_SPEED_LEN];
140};
141
142static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
143 int data_len)
144{
145 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
146 int result;
147
148 result = i2c_smbus_read_block_data(client, command, block_buffer);
149 if (unlikely(result < 0))
150 goto abort;
151 if (unlikely(result == 0xff || result != data_len)) {
152 result = -EIO;
153 goto abort;
154 }
155 memcpy(data, block_buffer, data_len);
156 result = 0;
157abort:
158 return result;
159}
160
161static struct pem_data *pem_update_device(struct device *dev)
162{
163 struct i2c_client *client = to_i2c_client(dev);
164 struct pem_data *data = i2c_get_clientdata(client);
165 struct pem_data *ret = data;
166
167 mutex_lock(&data->update_lock);
168
169 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
170 int result;
171
172 /* Read data string */
173 result = pem_read_block(client, PEM_READ_DATA_STRING,
174 data->data_string,
175 sizeof(data->data_string));
176 if (unlikely(result < 0)) {
177 ret = ERR_PTR(result);
178 goto abort;
179 }
180
181 /* Read input string */
182 if (data->input_length) {
183 result = pem_read_block(client, PEM_READ_INPUT_STRING,
184 data->input_string,
185 data->input_length);
186 if (unlikely(result < 0)) {
187 ret = ERR_PTR(result);
188 goto abort;
189 }
190 }
191
192 /* Read fan speeds */
193 if (data->fans_supported) {
194 result = pem_read_block(client, PEM_READ_FAN_SPEED,
195 data->fan_speed,
196 sizeof(data->fan_speed));
197 if (unlikely(result < 0)) {
198 ret = ERR_PTR(result);
199 goto abort;
200 }
201 }
202
203 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
204
205 data->last_updated = jiffies;
206 data->valid = 1;
207 }
208abort:
209 mutex_unlock(&data->update_lock);
210 return ret;
211}
212
213static long pem_get_data(u8 *data, int len, int index)
214{
215 long val;
216
217 switch (index) {
218 case PEM_DATA_VOUT_LSB:
219 val = (data[index] + (data[index+1] << 8)) * 5 / 2;
220 break;
221 case PEM_DATA_CURRENT:
222 val = data[index] * 200;
223 break;
224 case PEM_DATA_TEMP:
225 val = data[index] * 1000;
226 break;
227 case PEM_DATA_TEMP_MAX:
228 val = 97 * 1000; /* 97 degrees C per datasheet */
229 break;
230 case PEM_DATA_TEMP_CRIT:
231 val = 107 * 1000; /* 107 degrees C per datasheet */
232 break;
233 default:
234 WARN_ON_ONCE(1);
235 val = 0;
236 }
237 return val;
238}
239
240static long pem_get_input(u8 *data, int len, int index)
241{
242 long val;
243
244 switch (index) {
245 case PEM_INPUT_VOLTAGE:
246 if (len == INPUT_STRING_LEN)
247 val = (data[index] + (data[index+1] << 8) - 75) * 1000;
248 else
249 val = (data[index] - 75) * 1000;
250 break;
251 case PEM_INPUT_POWER_LSB:
252 if (len == INPUT_STRING_LEN)
253 index++;
254 val = (data[index] + (data[index+1] << 8)) * 1000000L;
255 break;
256 default:
257 WARN_ON_ONCE(1);
258 val = 0;
259 }
260 return val;
261}
262
263static long pem_get_fan(u8 *data, int len, int index)
264{
265 long val;
266
267 switch (index) {
268 case PEM_FAN_FAN1:
269 case PEM_FAN_FAN2:
270 case PEM_FAN_FAN3:
271 val = data[index] * 100;
272 break;
273 default:
274 WARN_ON_ONCE(1);
275 val = 0;
276 }
277 return val;
278}
279
280/*
281 * Show boolean, either a fault or an alarm.
282 * .nr points to the register, .index is the bit mask to check
283 */
284static ssize_t pem_show_bool(struct device *dev,
285 struct device_attribute *da, char *buf)
286{
287 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
288 struct pem_data *data = pem_update_device(dev);
289 u8 status;
290
291 if (IS_ERR(data))
292 return PTR_ERR(data);
293
294 status = data->data_string[attr->nr] & attr->index;
295 return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
296}
297
298static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
299 char *buf)
300{
301 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
302 struct pem_data *data = pem_update_device(dev);
303 long value;
304
305 if (IS_ERR(data))
306 return PTR_ERR(data);
307
308 value = pem_get_data(data->data_string, sizeof(data->data_string),
309 attr->index);
310
311 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
312}
313
314static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
315 char *buf)
316{
317 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
318 struct pem_data *data = pem_update_device(dev);
319 long value;
320
321 if (IS_ERR(data))
322 return PTR_ERR(data);
323
324 value = pem_get_input(data->input_string, sizeof(data->input_string),
325 attr->index);
326
327 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
328}
329
330static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
331 char *buf)
332{
333 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
334 struct pem_data *data = pem_update_device(dev);
335 long value;
336
337 if (IS_ERR(data))
338 return PTR_ERR(data);
339
340 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
341 attr->index);
342
343 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
344}
345
346/* Voltages */
347static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
348 PEM_DATA_VOUT_LSB);
Guenter Roeckd668a8b2011-03-15 16:34:07 -0700349static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
Guenter Roeck502b5a02010-09-29 20:12:08 -0700350 PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
351static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
352 PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
353static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
354 PEM_INPUT_VOLTAGE);
355static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL,
356 PEM_DATA_ALARM_1,
357 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
358
359/* Currents */
360static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
361 PEM_DATA_CURRENT);
362static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
363 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
364
365/* Power */
366static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
367 PEM_INPUT_POWER_LSB);
368static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
369 PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
370
371/* Fans */
372static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
373 PEM_FAN_FAN1);
374static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
375 PEM_FAN_FAN2);
376static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
377 PEM_FAN_FAN3);
378static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
379 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
380
381/* Temperatures */
382static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
383 PEM_DATA_TEMP);
384static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
385 PEM_DATA_TEMP_MAX);
386static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
387 PEM_DATA_TEMP_CRIT);
388static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
389 PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
390static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
391 PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
392static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
393 PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
394
395static struct attribute *pem_attributes[] = {
396 &sensor_dev_attr_in1_input.dev_attr.attr,
Guenter Roeckd668a8b2011-03-15 16:34:07 -0700397 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Guenter Roeck502b5a02010-09-29 20:12:08 -0700398 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
399 &sensor_dev_attr_in2_alarm.dev_attr.attr,
400
401 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
402
403 &sensor_dev_attr_power1_alarm.dev_attr.attr,
404
405 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
406
407 &sensor_dev_attr_temp1_input.dev_attr.attr,
408 &sensor_dev_attr_temp1_max.dev_attr.attr,
409 &sensor_dev_attr_temp1_crit.dev_attr.attr,
410 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
411 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
412 &sensor_dev_attr_temp1_fault.dev_attr.attr,
413
414 NULL,
415};
416
417static const struct attribute_group pem_group = {
418 .attrs = pem_attributes,
419};
420
421static struct attribute *pem_input_attributes[] = {
422 &sensor_dev_attr_in2_input.dev_attr.attr,
423 &sensor_dev_attr_curr1_input.dev_attr.attr,
424 &sensor_dev_attr_power1_input.dev_attr.attr,
425};
426
427static const struct attribute_group pem_input_group = {
428 .attrs = pem_input_attributes,
429};
430
431static struct attribute *pem_fan_attributes[] = {
432 &sensor_dev_attr_fan1_input.dev_attr.attr,
433 &sensor_dev_attr_fan2_input.dev_attr.attr,
434 &sensor_dev_attr_fan3_input.dev_attr.attr,
435};
436
437static const struct attribute_group pem_fan_group = {
438 .attrs = pem_fan_attributes,
439};
440
441static int pem_probe(struct i2c_client *client,
442 const struct i2c_device_id *id)
443{
444 struct i2c_adapter *adapter = client->adapter;
445 struct pem_data *data;
446 int ret;
447
448 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
449 | I2C_FUNC_SMBUS_WRITE_BYTE))
450 return -ENODEV;
451
Guenter Roeck07404aa2012-02-22 08:56:45 -0800452 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700453 if (!data)
454 return -ENOMEM;
455
456 i2c_set_clientdata(client, data);
457 mutex_init(&data->update_lock);
458
459 /*
460 * We use the next two commands to determine if the device is really
461 * there.
462 */
463 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
464 data->firmware_rev, sizeof(data->firmware_rev));
465 if (ret < 0)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800466 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700467
468 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
469 if (ret < 0)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800470 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700471
472 dev_info(&client->dev, "Firmware revision %d.%d.%d\n",
473 data->firmware_rev[0], data->firmware_rev[1],
474 data->firmware_rev[2]);
475
476 /* Register sysfs hooks */
477 ret = sysfs_create_group(&client->dev.kobj, &pem_group);
478 if (ret)
Guenter Roeck07404aa2012-02-22 08:56:45 -0800479 return ret;
Guenter Roeck502b5a02010-09-29 20:12:08 -0700480
481 /*
482 * Check if input readings are supported.
483 * This is the case if we can read input data,
484 * and if the returned data is not all zeros.
485 * Note that input alarms are always supported.
486 */
487 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
488 data->input_string,
489 sizeof(data->input_string) - 1);
490 if (!ret && (data->input_string[0] || data->input_string[1] ||
491 data->input_string[2]))
492 data->input_length = sizeof(data->input_string) - 1;
493 else if (ret < 0) {
494 /* Input string is one byte longer for some devices */
495 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
496 data->input_string,
497 sizeof(data->input_string));
498 if (!ret && (data->input_string[0] || data->input_string[1] ||
499 data->input_string[2] || data->input_string[3]))
500 data->input_length = sizeof(data->input_string);
501 }
502 ret = 0;
503 if (data->input_length) {
504 ret = sysfs_create_group(&client->dev.kobj, &pem_input_group);
505 if (ret)
506 goto out_remove_groups;
507 }
508
509 /*
510 * Check if fan speed readings are supported.
511 * This is the case if we can read fan speed data,
512 * and if the returned data is not all zeros.
513 * Note that the fan alarm is always supported.
514 */
515 ret = pem_read_block(client, PEM_READ_FAN_SPEED,
516 data->fan_speed,
517 sizeof(data->fan_speed));
518 if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
519 data->fan_speed[2] || data->fan_speed[3])) {
520 data->fans_supported = true;
521 ret = sysfs_create_group(&client->dev.kobj, &pem_fan_group);
522 if (ret)
523 goto out_remove_groups;
524 }
525
526 data->hwmon_dev = hwmon_device_register(&client->dev);
527 if (IS_ERR(data->hwmon_dev)) {
528 ret = PTR_ERR(data->hwmon_dev);
529 goto out_remove_groups;
530 }
531
532 return 0;
533
534out_remove_groups:
535 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
536 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
537 sysfs_remove_group(&client->dev.kobj, &pem_group);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700538 return ret;
539}
540
541static int pem_remove(struct i2c_client *client)
542{
543 struct pem_data *data = i2c_get_clientdata(client);
544
545 hwmon_device_unregister(data->hwmon_dev);
546
547 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
548 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
549 sysfs_remove_group(&client->dev.kobj, &pem_group);
550
Guenter Roeck502b5a02010-09-29 20:12:08 -0700551 return 0;
552}
553
554static const struct i2c_device_id pem_id[] = {
555 {"lineage_pem", 0},
556 {}
557};
558MODULE_DEVICE_TABLE(i2c, pem_id);
559
560static struct i2c_driver pem_driver = {
561 .driver = {
562 .name = "lineage_pem",
563 },
564 .probe = pem_probe,
565 .remove = pem_remove,
566 .id_table = pem_id,
567};
568
Axel Linf0967ee2012-01-20 15:38:18 +0800569module_i2c_driver(pem_driver);
Guenter Roeck502b5a02010-09-29 20:12:08 -0700570
Guenter Roeckbb9a80e2012-06-22 12:07:25 -0700571MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
Guenter Roeck502b5a02010-09-29 20:12:08 -0700572MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
573MODULE_LICENSE("GPL");