staging: fix mei build when PM is not enabled
[zen-stable.git] / drivers / hwmon / lineage-pem.c
blob58eded27f385f4814783c2702281512958c239f6
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>
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.
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
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
126 struct pem_data {
127 struct device *hwmon_dev;
129 struct mutex update_lock;
130 bool valid;
131 bool fans_supported;
132 int input_length;
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,
142 int data_len)
144 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
145 int result;
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;
154 memcpy(data, block_buffer, data_len);
155 result = 0;
156 abort:
157 return result;
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) {
169 int result;
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;
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;
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;
202 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
204 data->last_updated = jiffies;
205 data->valid = 1;
207 abort:
208 mutex_unlock(&data->update_lock);
209 return ret;
212 static long pem_get_data(u8 *data, int len, int index)
214 long val;
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;
236 return val;
239 static long pem_get_input(u8 *data, int len, int index)
241 long val;
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;
259 return val;
262 static long pem_get_fan(u8 *data, int len, int index)
264 long val;
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;
276 return val;
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);
288 u8 status;
290 if (IS_ERR(data))
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,
298 char *buf)
300 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
301 struct pem_data *data = pem_update_device(dev);
302 long value;
304 if (IS_ERR(data))
305 return PTR_ERR(data);
307 value = pem_get_data(data->data_string, sizeof(data->data_string),
308 attr->index);
310 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
313 static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
314 char *buf)
316 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
317 struct pem_data *data = pem_update_device(dev);
318 long value;
320 if (IS_ERR(data))
321 return PTR_ERR(data);
323 value = pem_get_input(data->input_string, sizeof(data->input_string),
324 attr->index);
326 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
329 static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
330 char *buf)
332 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
333 struct pem_data *data = pem_update_device(dev);
334 long value;
336 if (IS_ERR(data))
337 return PTR_ERR(data);
339 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
340 attr->index);
342 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
345 /* Voltages */
346 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
347 PEM_DATA_VOUT_LSB);
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,
353 PEM_INPUT_VOLTAGE);
354 static 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);
358 /* Currents */
359 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
360 PEM_DATA_CURRENT);
361 static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
362 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
364 /* Power */
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);
370 /* Fans */
371 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
372 PEM_FAN_FAN1);
373 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
374 PEM_FAN_FAN2);
375 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
376 PEM_FAN_FAN3);
377 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
378 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
380 /* Temperatures */
381 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
382 PEM_DATA_TEMP);
383 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
384 PEM_DATA_TEMP_MAX);
385 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
386 PEM_DATA_TEMP_CRIT);
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,
413 NULL,
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;
445 int ret;
447 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
448 | I2C_FUNC_SMBUS_WRITE_BYTE))
449 return -ENODEV;
451 data = kzalloc(sizeof(*data), GFP_KERNEL);
452 if (!data)
453 return -ENOMEM;
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
460 * there.
462 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
463 data->firmware_rev, sizeof(data->firmware_rev));
464 if (ret < 0)
465 goto out_kfree;
467 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
468 if (ret < 0)
469 goto out_kfree;
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);
477 if (ret)
478 goto out_kfree;
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,
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);
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;
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,
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;
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;
531 return 0;
533 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);
537 out_kfree:
538 kfree(data);
539 return ret;
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);
552 kfree(data);
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 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);