PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / hwmon / lineage-pem.c
blobebbb9f4f27a3357714f9c654979d73def306c766
1 /*
2 * Driver for Lineage Compact Power Line series of power entry modules.
4 * Copyright (C) 2010, 2011 Ericsson AB.
6 * Documentation:
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>
32 #include <linux/jiffies.h>
35 * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
36 * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
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.
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.
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
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
71 /* Virtual entries, to report constants */
72 #define PEM_DATA_TEMP_MAX 10
73 #define PEM_DATA_TEMP_CRIT 11
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
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
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)
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)
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)
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)
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
127 struct pem_data {
128 struct device *hwmon_dev;
130 struct mutex update_lock;
131 bool valid;
132 bool fans_supported;
133 int input_length;
134 unsigned long last_updated; /* in jiffies */
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];
142 static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
143 int data_len)
145 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
146 int result;
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;
155 memcpy(data, block_buffer, data_len);
156 result = 0;
157 abort:
158 return result;
161 static struct pem_data *pem_update_device(struct device *dev)
163 struct i2c_client *client = to_i2c_client(dev);
164 struct pem_data *data = i2c_get_clientdata(client);
165 struct pem_data *ret = data;
167 mutex_lock(&data->update_lock);
169 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
170 int result;
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;
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;
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;
203 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
205 data->last_updated = jiffies;
206 data->valid = 1;
208 abort:
209 mutex_unlock(&data->update_lock);
210 return ret;
213 static long pem_get_data(u8 *data, int len, int index)
215 long val;
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;
237 return val;
240 static long pem_get_input(u8 *data, int len, int index)
242 long val;
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;
260 return val;
263 static long pem_get_fan(u8 *data, int len, int index)
265 long val;
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;
277 return val;
281 * Show boolean, either a fault or an alarm.
282 * .nr points to the register, .index is the bit mask to check
284 static ssize_t pem_show_bool(struct device *dev,
285 struct device_attribute *da, char *buf)
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;
291 if (IS_ERR(data))
292 return PTR_ERR(data);
294 status = data->data_string[attr->nr] & attr->index;
295 return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
298 static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
299 char *buf)
301 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
302 struct pem_data *data = pem_update_device(dev);
303 long value;
305 if (IS_ERR(data))
306 return PTR_ERR(data);
308 value = pem_get_data(data->data_string, sizeof(data->data_string),
309 attr->index);
311 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
314 static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
315 char *buf)
317 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
318 struct pem_data *data = pem_update_device(dev);
319 long value;
321 if (IS_ERR(data))
322 return PTR_ERR(data);
324 value = pem_get_input(data->input_string, sizeof(data->input_string),
325 attr->index);
327 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
330 static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
331 char *buf)
333 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
334 struct pem_data *data = pem_update_device(dev);
335 long value;
337 if (IS_ERR(data))
338 return PTR_ERR(data);
340 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
341 attr->index);
343 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
346 /* Voltages */
347 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
348 PEM_DATA_VOUT_LSB);
349 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
350 PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
351 static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
352 PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
353 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
354 PEM_INPUT_VOLTAGE);
355 static 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);
359 /* Currents */
360 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
361 PEM_DATA_CURRENT);
362 static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
363 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
365 /* Power */
366 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
367 PEM_INPUT_POWER_LSB);
368 static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
369 PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
371 /* Fans */
372 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
373 PEM_FAN_FAN1);
374 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
375 PEM_FAN_FAN2);
376 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
377 PEM_FAN_FAN3);
378 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
379 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
381 /* Temperatures */
382 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
383 PEM_DATA_TEMP);
384 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
385 PEM_DATA_TEMP_MAX);
386 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
387 PEM_DATA_TEMP_CRIT);
388 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
389 PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
390 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
391 PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
392 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
393 PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
395 static struct attribute *pem_attributes[] = {
396 &sensor_dev_attr_in1_input.dev_attr.attr,
397 &sensor_dev_attr_in1_alarm.dev_attr.attr,
398 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
399 &sensor_dev_attr_in2_alarm.dev_attr.attr,
401 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
403 &sensor_dev_attr_power1_alarm.dev_attr.attr,
405 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
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,
414 NULL,
417 static const struct attribute_group pem_group = {
418 .attrs = pem_attributes,
421 static 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 NULL
428 static const struct attribute_group pem_input_group = {
429 .attrs = pem_input_attributes,
432 static struct attribute *pem_fan_attributes[] = {
433 &sensor_dev_attr_fan1_input.dev_attr.attr,
434 &sensor_dev_attr_fan2_input.dev_attr.attr,
435 &sensor_dev_attr_fan3_input.dev_attr.attr,
436 NULL
439 static const struct attribute_group pem_fan_group = {
440 .attrs = pem_fan_attributes,
443 static int pem_probe(struct i2c_client *client,
444 const struct i2c_device_id *id)
446 struct i2c_adapter *adapter = client->adapter;
447 struct pem_data *data;
448 int ret;
450 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
451 | I2C_FUNC_SMBUS_WRITE_BYTE))
452 return -ENODEV;
454 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
455 if (!data)
456 return -ENOMEM;
458 i2c_set_clientdata(client, data);
459 mutex_init(&data->update_lock);
462 * We use the next two commands to determine if the device is really
463 * there.
465 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
466 data->firmware_rev, sizeof(data->firmware_rev));
467 if (ret < 0)
468 return ret;
470 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
471 if (ret < 0)
472 return ret;
474 dev_info(&client->dev, "Firmware revision %d.%d.%d\n",
475 data->firmware_rev[0], data->firmware_rev[1],
476 data->firmware_rev[2]);
478 /* Register sysfs hooks */
479 ret = sysfs_create_group(&client->dev.kobj, &pem_group);
480 if (ret)
481 return ret;
484 * Check if input readings are supported.
485 * This is the case if we can read input data,
486 * and if the returned data is not all zeros.
487 * Note that input alarms are always supported.
489 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
490 data->input_string,
491 sizeof(data->input_string) - 1);
492 if (!ret && (data->input_string[0] || data->input_string[1] ||
493 data->input_string[2]))
494 data->input_length = sizeof(data->input_string) - 1;
495 else if (ret < 0) {
496 /* Input string is one byte longer for some devices */
497 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
498 data->input_string,
499 sizeof(data->input_string));
500 if (!ret && (data->input_string[0] || data->input_string[1] ||
501 data->input_string[2] || data->input_string[3]))
502 data->input_length = sizeof(data->input_string);
504 ret = 0;
505 if (data->input_length) {
506 ret = sysfs_create_group(&client->dev.kobj, &pem_input_group);
507 if (ret)
508 goto out_remove_groups;
512 * Check if fan speed readings are supported.
513 * This is the case if we can read fan speed data,
514 * and if the returned data is not all zeros.
515 * Note that the fan alarm is always supported.
517 ret = pem_read_block(client, PEM_READ_FAN_SPEED,
518 data->fan_speed,
519 sizeof(data->fan_speed));
520 if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
521 data->fan_speed[2] || data->fan_speed[3])) {
522 data->fans_supported = true;
523 ret = sysfs_create_group(&client->dev.kobj, &pem_fan_group);
524 if (ret)
525 goto out_remove_groups;
528 data->hwmon_dev = hwmon_device_register(&client->dev);
529 if (IS_ERR(data->hwmon_dev)) {
530 ret = PTR_ERR(data->hwmon_dev);
531 goto out_remove_groups;
534 return 0;
536 out_remove_groups:
537 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
538 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
539 sysfs_remove_group(&client->dev.kobj, &pem_group);
540 return ret;
543 static int pem_remove(struct i2c_client *client)
545 struct pem_data *data = i2c_get_clientdata(client);
547 hwmon_device_unregister(data->hwmon_dev);
549 sysfs_remove_group(&client->dev.kobj, &pem_input_group);
550 sysfs_remove_group(&client->dev.kobj, &pem_fan_group);
551 sysfs_remove_group(&client->dev.kobj, &pem_group);
553 return 0;
556 static const struct i2c_device_id pem_id[] = {
557 {"lineage_pem", 0},
560 MODULE_DEVICE_TABLE(i2c, pem_id);
562 static struct i2c_driver pem_driver = {
563 .driver = {
564 .name = "lineage_pem",
566 .probe = pem_probe,
567 .remove = pem_remove,
568 .id_table = pem_id,
571 module_i2c_driver(pem_driver);
573 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
574 MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
575 MODULE_LICENSE("GPL");