pinctrl: mcp23s08: generalize irq property handling
[linux-stable.git] / drivers / hwmon / lineage-pem.c
blob84d791bdb62d6d60995d530a6c61f6a8ea21cd12
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 i2c_client *client;
129 const struct attribute_group *groups[4];
131 struct mutex update_lock;
132 bool valid;
133 bool fans_supported;
134 int input_length;
135 unsigned long last_updated; /* in jiffies */
137 u8 firmware_rev[FIRMWARE_REV_LEN];
138 u8 data_string[DATA_STRING_LEN];
139 u8 input_string[INPUT_STRING_LEN];
140 u8 fan_speed[FAN_SPEED_LEN];
143 static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
144 int data_len)
146 u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
147 int result;
149 result = i2c_smbus_read_block_data(client, command, block_buffer);
150 if (unlikely(result < 0))
151 goto abort;
152 if (unlikely(result == 0xff || result != data_len)) {
153 result = -EIO;
154 goto abort;
156 memcpy(data, block_buffer, data_len);
157 result = 0;
158 abort:
159 return result;
162 static struct pem_data *pem_update_device(struct device *dev)
164 struct pem_data *data = dev_get_drvdata(dev);
165 struct i2c_client *client = data->client;
166 struct pem_data *ret = data;
168 mutex_lock(&data->update_lock);
170 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
171 int result;
173 /* Read data string */
174 result = pem_read_block(client, PEM_READ_DATA_STRING,
175 data->data_string,
176 sizeof(data->data_string));
177 if (unlikely(result < 0)) {
178 ret = ERR_PTR(result);
179 goto abort;
182 /* Read input string */
183 if (data->input_length) {
184 result = pem_read_block(client, PEM_READ_INPUT_STRING,
185 data->input_string,
186 data->input_length);
187 if (unlikely(result < 0)) {
188 ret = ERR_PTR(result);
189 goto abort;
193 /* Read fan speeds */
194 if (data->fans_supported) {
195 result = pem_read_block(client, PEM_READ_FAN_SPEED,
196 data->fan_speed,
197 sizeof(data->fan_speed));
198 if (unlikely(result < 0)) {
199 ret = ERR_PTR(result);
200 goto abort;
204 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
206 data->last_updated = jiffies;
207 data->valid = 1;
209 abort:
210 mutex_unlock(&data->update_lock);
211 return ret;
214 static long pem_get_data(u8 *data, int len, int index)
216 long val;
218 switch (index) {
219 case PEM_DATA_VOUT_LSB:
220 val = (data[index] + (data[index+1] << 8)) * 5 / 2;
221 break;
222 case PEM_DATA_CURRENT:
223 val = data[index] * 200;
224 break;
225 case PEM_DATA_TEMP:
226 val = data[index] * 1000;
227 break;
228 case PEM_DATA_TEMP_MAX:
229 val = 97 * 1000; /* 97 degrees C per datasheet */
230 break;
231 case PEM_DATA_TEMP_CRIT:
232 val = 107 * 1000; /* 107 degrees C per datasheet */
233 break;
234 default:
235 WARN_ON_ONCE(1);
236 val = 0;
238 return val;
241 static long pem_get_input(u8 *data, int len, int index)
243 long val;
245 switch (index) {
246 case PEM_INPUT_VOLTAGE:
247 if (len == INPUT_STRING_LEN)
248 val = (data[index] + (data[index+1] << 8) - 75) * 1000;
249 else
250 val = (data[index] - 75) * 1000;
251 break;
252 case PEM_INPUT_POWER_LSB:
253 if (len == INPUT_STRING_LEN)
254 index++;
255 val = (data[index] + (data[index+1] << 8)) * 1000000L;
256 break;
257 default:
258 WARN_ON_ONCE(1);
259 val = 0;
261 return val;
264 static long pem_get_fan(u8 *data, int len, int index)
266 long val;
268 switch (index) {
269 case PEM_FAN_FAN1:
270 case PEM_FAN_FAN2:
271 case PEM_FAN_FAN3:
272 val = data[index] * 100;
273 break;
274 default:
275 WARN_ON_ONCE(1);
276 val = 0;
278 return val;
282 * Show boolean, either a fault or an alarm.
283 * .nr points to the register, .index is the bit mask to check
285 static ssize_t pem_show_bool(struct device *dev,
286 struct device_attribute *da, char *buf)
288 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
289 struct pem_data *data = pem_update_device(dev);
290 u8 status;
292 if (IS_ERR(data))
293 return PTR_ERR(data);
295 status = data->data_string[attr->nr] & attr->index;
296 return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
299 static ssize_t pem_show_data(struct device *dev, struct device_attribute *da,
300 char *buf)
302 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
303 struct pem_data *data = pem_update_device(dev);
304 long value;
306 if (IS_ERR(data))
307 return PTR_ERR(data);
309 value = pem_get_data(data->data_string, sizeof(data->data_string),
310 attr->index);
312 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
315 static ssize_t pem_show_input(struct device *dev, struct device_attribute *da,
316 char *buf)
318 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
319 struct pem_data *data = pem_update_device(dev);
320 long value;
322 if (IS_ERR(data))
323 return PTR_ERR(data);
325 value = pem_get_input(data->input_string, sizeof(data->input_string),
326 attr->index);
328 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
331 static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da,
332 char *buf)
334 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
335 struct pem_data *data = pem_update_device(dev);
336 long value;
338 if (IS_ERR(data))
339 return PTR_ERR(data);
341 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
342 attr->index);
344 return snprintf(buf, PAGE_SIZE, "%ld\n", value);
347 /* Voltages */
348 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL,
349 PEM_DATA_VOUT_LSB);
350 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL,
351 PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT);
352 static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
353 PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN);
354 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL,
355 PEM_INPUT_VOLTAGE);
356 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL,
357 PEM_DATA_ALARM_1,
358 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
360 /* Currents */
361 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL,
362 PEM_DATA_CURRENT);
363 static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL,
364 PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT);
366 /* Power */
367 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL,
368 PEM_INPUT_POWER_LSB);
369 static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL,
370 PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT);
372 /* Fans */
373 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL,
374 PEM_FAN_FAN1);
375 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL,
376 PEM_FAN_FAN2);
377 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL,
378 PEM_FAN_FAN3);
379 static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL,
380 PEM_DATA_ALARM_2, ALRM2_FAN_FAULT);
382 /* Temperatures */
383 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL,
384 PEM_DATA_TEMP);
385 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL,
386 PEM_DATA_TEMP_MAX);
387 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL,
388 PEM_DATA_TEMP_CRIT);
389 static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL,
390 PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING);
391 static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL,
392 PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN);
393 static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL,
394 PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT);
396 static struct attribute *pem_attributes[] = {
397 &sensor_dev_attr_in1_input.dev_attr.attr,
398 &sensor_dev_attr_in1_alarm.dev_attr.attr,
399 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
400 &sensor_dev_attr_in2_alarm.dev_attr.attr,
402 &sensor_dev_attr_curr1_alarm.dev_attr.attr,
404 &sensor_dev_attr_power1_alarm.dev_attr.attr,
406 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
408 &sensor_dev_attr_temp1_input.dev_attr.attr,
409 &sensor_dev_attr_temp1_max.dev_attr.attr,
410 &sensor_dev_attr_temp1_crit.dev_attr.attr,
411 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
412 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
413 &sensor_dev_attr_temp1_fault.dev_attr.attr,
415 NULL,
418 static const struct attribute_group pem_group = {
419 .attrs = pem_attributes,
422 static struct attribute *pem_input_attributes[] = {
423 &sensor_dev_attr_in2_input.dev_attr.attr,
424 &sensor_dev_attr_curr1_input.dev_attr.attr,
425 &sensor_dev_attr_power1_input.dev_attr.attr,
426 NULL
429 static const struct attribute_group pem_input_group = {
430 .attrs = pem_input_attributes,
433 static struct attribute *pem_fan_attributes[] = {
434 &sensor_dev_attr_fan1_input.dev_attr.attr,
435 &sensor_dev_attr_fan2_input.dev_attr.attr,
436 &sensor_dev_attr_fan3_input.dev_attr.attr,
437 NULL
440 static const struct attribute_group pem_fan_group = {
441 .attrs = pem_fan_attributes,
444 static int pem_probe(struct i2c_client *client,
445 const struct i2c_device_id *id)
447 struct i2c_adapter *adapter = client->adapter;
448 struct device *dev = &client->dev;
449 struct device *hwmon_dev;
450 struct pem_data *data;
451 int ret, idx = 0;
453 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
454 | I2C_FUNC_SMBUS_WRITE_BYTE))
455 return -ENODEV;
457 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
458 if (!data)
459 return -ENOMEM;
461 data->client = client;
462 mutex_init(&data->update_lock);
465 * We use the next two commands to determine if the device is really
466 * there.
468 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
469 data->firmware_rev, sizeof(data->firmware_rev));
470 if (ret < 0)
471 return ret;
473 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
474 if (ret < 0)
475 return ret;
477 dev_info(dev, "Firmware revision %d.%d.%d\n",
478 data->firmware_rev[0], data->firmware_rev[1],
479 data->firmware_rev[2]);
481 /* sysfs hooks */
482 data->groups[idx++] = &pem_group;
485 * Check if input readings are supported.
486 * This is the case if we can read input data,
487 * and if the returned data is not all zeros.
488 * Note that input alarms are always supported.
490 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
491 data->input_string,
492 sizeof(data->input_string) - 1);
493 if (!ret && (data->input_string[0] || data->input_string[1] ||
494 data->input_string[2]))
495 data->input_length = sizeof(data->input_string) - 1;
496 else if (ret < 0) {
497 /* Input string is one byte longer for some devices */
498 ret = pem_read_block(client, PEM_READ_INPUT_STRING,
499 data->input_string,
500 sizeof(data->input_string));
501 if (!ret && (data->input_string[0] || data->input_string[1] ||
502 data->input_string[2] || data->input_string[3]))
503 data->input_length = sizeof(data->input_string);
506 if (data->input_length)
507 data->groups[idx++] = &pem_input_group;
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.
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 data->groups[idx++] = &pem_fan_group;
524 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
525 data, data->groups);
526 return PTR_ERR_OR_ZERO(hwmon_dev);
529 static const struct i2c_device_id pem_id[] = {
530 {"lineage_pem", 0},
533 MODULE_DEVICE_TABLE(i2c, pem_id);
535 static struct i2c_driver pem_driver = {
536 .driver = {
537 .name = "lineage_pem",
539 .probe = pem_probe,
540 .id_table = pem_id,
543 module_i2c_driver(pem_driver);
545 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
546 MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
547 MODULE_LICENSE("GPL");