1 /* 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 #include <linux/jiffies.h> 33 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 127 struct pem_data { 128 struct i2c_client *client; 129 const struct attribute_group *groups[4]; 130 131 struct mutex update_lock; 132 bool valid; 133 bool fans_supported; 134 int input_length; 135 unsigned long last_updated; /* in jiffies */ 136 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]; 141 }; 142 143 static int pem_read_block(struct i2c_client *client, u8 command, u8 *data, 144 int data_len) 145 { 146 u8 block_buffer[I2C_SMBUS_BLOCK_MAX]; 147 int result; 148 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; 155 } 156 memcpy(data, block_buffer, data_len); 157 result = 0; 158 abort: 159 return result; 160 } 161 162 static struct pem_data *pem_update_device(struct device *dev) 163 { 164 struct pem_data *data = dev_get_drvdata(dev); 165 struct i2c_client *client = data->client; 166 struct pem_data *ret = data; 167 168 mutex_lock(&data->update_lock); 169 170 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 171 int result; 172 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; 180 } 181 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; 190 } 191 } 192 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; 201 } 202 } 203 204 i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 205 206 data->last_updated = jiffies; 207 data->valid = 1; 208 } 209 abort: 210 mutex_unlock(&data->update_lock); 211 return ret; 212 } 213 214 static long pem_get_data(u8 *data, int len, int index) 215 { 216 long val; 217 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; 237 } 238 return val; 239 } 240 241 static long pem_get_input(u8 *data, int len, int index) 242 { 243 long val; 244 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; 260 } 261 return val; 262 } 263 264 static long pem_get_fan(u8 *data, int len, int index) 265 { 266 long val; 267 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; 277 } 278 return val; 279 } 280 281 /* 282 * Show boolean, either a fault or an alarm. 283 * .nr points to the register, .index is the bit mask to check 284 */ 285 static ssize_t pem_bool_show(struct device *dev, struct device_attribute *da, 286 char *buf) 287 { 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; 291 292 if (IS_ERR(data)) 293 return PTR_ERR(data); 294 295 status = data->data_string[attr->nr] & attr->index; 296 return snprintf(buf, PAGE_SIZE, "%d\n", !!status); 297 } 298 299 static ssize_t pem_data_show(struct device *dev, struct device_attribute *da, 300 char *buf) 301 { 302 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 303 struct pem_data *data = pem_update_device(dev); 304 long value; 305 306 if (IS_ERR(data)) 307 return PTR_ERR(data); 308 309 value = pem_get_data(data->data_string, sizeof(data->data_string), 310 attr->index); 311 312 return snprintf(buf, PAGE_SIZE, "%ld\n", value); 313 } 314 315 static ssize_t pem_input_show(struct device *dev, struct device_attribute *da, 316 char *buf) 317 { 318 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 319 struct pem_data *data = pem_update_device(dev); 320 long value; 321 322 if (IS_ERR(data)) 323 return PTR_ERR(data); 324 325 value = pem_get_input(data->input_string, sizeof(data->input_string), 326 attr->index); 327 328 return snprintf(buf, PAGE_SIZE, "%ld\n", value); 329 } 330 331 static ssize_t pem_fan_show(struct device *dev, struct device_attribute *da, 332 char *buf) 333 { 334 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 335 struct pem_data *data = pem_update_device(dev); 336 long value; 337 338 if (IS_ERR(data)) 339 return PTR_ERR(data); 340 341 value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed), 342 attr->index); 343 344 return snprintf(buf, PAGE_SIZE, "%ld\n", value); 345 } 346 347 /* Voltages */ 348 static SENSOR_DEVICE_ATTR_RO(in1_input, pem_data, PEM_DATA_VOUT_LSB); 349 static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, pem_bool, PEM_DATA_ALARM_1, 350 ALRM1_VOUT_OUT_LIMIT); 351 static SENSOR_DEVICE_ATTR_2_RO(in1_crit_alarm, pem_bool, PEM_DATA_ALARM_1, 352 ALRM1_OV_VOLT_SHUTDOWN); 353 static SENSOR_DEVICE_ATTR_RO(in2_input, pem_input, PEM_INPUT_VOLTAGE); 354 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, pem_bool, PEM_DATA_ALARM_1, 355 ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT); 356 357 /* Currents */ 358 static SENSOR_DEVICE_ATTR_RO(curr1_input, pem_data, PEM_DATA_CURRENT); 359 static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, pem_bool, PEM_DATA_ALARM_1, 360 ALRM1_VIN_OVERCURRENT); 361 362 /* Power */ 363 static SENSOR_DEVICE_ATTR_RO(power1_input, pem_input, PEM_INPUT_POWER_LSB); 364 static SENSOR_DEVICE_ATTR_2_RO(power1_alarm, pem_bool, PEM_DATA_ALARM_1, 365 ALRM1_POWER_LIMIT); 366 367 /* Fans */ 368 static SENSOR_DEVICE_ATTR_RO(fan1_input, pem_fan, PEM_FAN_FAN1); 369 static SENSOR_DEVICE_ATTR_RO(fan2_input, pem_fan, PEM_FAN_FAN2); 370 static SENSOR_DEVICE_ATTR_RO(fan3_input, pem_fan, PEM_FAN_FAN3); 371 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, pem_bool, PEM_DATA_ALARM_2, 372 ALRM2_FAN_FAULT); 373 374 /* Temperatures */ 375 static SENSOR_DEVICE_ATTR_RO(temp1_input, pem_data, PEM_DATA_TEMP); 376 static SENSOR_DEVICE_ATTR_RO(temp1_max, pem_data, PEM_DATA_TEMP_MAX); 377 static SENSOR_DEVICE_ATTR_RO(temp1_crit, pem_data, PEM_DATA_TEMP_CRIT); 378 static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, pem_bool, PEM_DATA_ALARM_1, 379 ALRM1_TEMP_WARNING); 380 static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, pem_bool, PEM_DATA_ALARM_1, 381 ALRM1_TEMP_SHUTDOWN); 382 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, pem_bool, PEM_DATA_ALARM_2, 383 ALRM2_TEMP_FAULT); 384 385 static struct attribute *pem_attributes[] = { 386 &sensor_dev_attr_in1_input.dev_attr.attr, 387 &sensor_dev_attr_in1_alarm.dev_attr.attr, 388 &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, 389 &sensor_dev_attr_in2_alarm.dev_attr.attr, 390 391 &sensor_dev_attr_curr1_alarm.dev_attr.attr, 392 393 &sensor_dev_attr_power1_alarm.dev_attr.attr, 394 395 &sensor_dev_attr_fan1_alarm.dev_attr.attr, 396 397 &sensor_dev_attr_temp1_input.dev_attr.attr, 398 &sensor_dev_attr_temp1_max.dev_attr.attr, 399 &sensor_dev_attr_temp1_crit.dev_attr.attr, 400 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 401 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 402 &sensor_dev_attr_temp1_fault.dev_attr.attr, 403 404 NULL, 405 }; 406 407 static const struct attribute_group pem_group = { 408 .attrs = pem_attributes, 409 }; 410 411 static struct attribute *pem_input_attributes[] = { 412 &sensor_dev_attr_in2_input.dev_attr.attr, 413 &sensor_dev_attr_curr1_input.dev_attr.attr, 414 &sensor_dev_attr_power1_input.dev_attr.attr, 415 NULL 416 }; 417 418 static const struct attribute_group pem_input_group = { 419 .attrs = pem_input_attributes, 420 }; 421 422 static struct attribute *pem_fan_attributes[] = { 423 &sensor_dev_attr_fan1_input.dev_attr.attr, 424 &sensor_dev_attr_fan2_input.dev_attr.attr, 425 &sensor_dev_attr_fan3_input.dev_attr.attr, 426 NULL 427 }; 428 429 static const struct attribute_group pem_fan_group = { 430 .attrs = pem_fan_attributes, 431 }; 432 433 static int pem_probe(struct i2c_client *client, 434 const struct i2c_device_id *id) 435 { 436 struct i2c_adapter *adapter = client->adapter; 437 struct device *dev = &client->dev; 438 struct device *hwmon_dev; 439 struct pem_data *data; 440 int ret, idx = 0; 441 442 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA 443 | I2C_FUNC_SMBUS_WRITE_BYTE)) 444 return -ENODEV; 445 446 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 447 if (!data) 448 return -ENOMEM; 449 450 data->client = client; 451 mutex_init(&data->update_lock); 452 453 /* 454 * We use the next two commands to determine if the device is really 455 * there. 456 */ 457 ret = pem_read_block(client, PEM_READ_FIRMWARE_REV, 458 data->firmware_rev, sizeof(data->firmware_rev)); 459 if (ret < 0) 460 return ret; 461 462 ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 463 if (ret < 0) 464 return ret; 465 466 dev_info(dev, "Firmware revision %d.%d.%d\n", 467 data->firmware_rev[0], data->firmware_rev[1], 468 data->firmware_rev[2]); 469 470 /* sysfs hooks */ 471 data->groups[idx++] = &pem_group; 472 473 /* 474 * Check if input readings are supported. 475 * This is the case if we can read input data, 476 * and if the returned data is not all zeros. 477 * Note that input alarms are always supported. 478 */ 479 ret = pem_read_block(client, PEM_READ_INPUT_STRING, 480 data->input_string, 481 sizeof(data->input_string) - 1); 482 if (!ret && (data->input_string[0] || data->input_string[1] || 483 data->input_string[2])) 484 data->input_length = sizeof(data->input_string) - 1; 485 else if (ret < 0) { 486 /* Input string is one byte longer for some devices */ 487 ret = pem_read_block(client, PEM_READ_INPUT_STRING, 488 data->input_string, 489 sizeof(data->input_string)); 490 if (!ret && (data->input_string[0] || data->input_string[1] || 491 data->input_string[2] || data->input_string[3])) 492 data->input_length = sizeof(data->input_string); 493 } 494 495 if (data->input_length) 496 data->groups[idx++] = &pem_input_group; 497 498 /* 499 * Check if fan speed readings are supported. 500 * This is the case if we can read fan speed data, 501 * and if the returned data is not all zeros. 502 * Note that the fan alarm is always supported. 503 */ 504 ret = pem_read_block(client, PEM_READ_FAN_SPEED, 505 data->fan_speed, 506 sizeof(data->fan_speed)); 507 if (!ret && (data->fan_speed[0] || data->fan_speed[1] || 508 data->fan_speed[2] || data->fan_speed[3])) { 509 data->fans_supported = true; 510 data->groups[idx++] = &pem_fan_group; 511 } 512 513 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 514 data, data->groups); 515 return PTR_ERR_OR_ZERO(hwmon_dev); 516 } 517 518 static const struct i2c_device_id pem_id[] = { 519 {"lineage_pem", 0}, 520 {} 521 }; 522 MODULE_DEVICE_TABLE(i2c, pem_id); 523 524 static struct i2c_driver pem_driver = { 525 .driver = { 526 .name = "lineage_pem", 527 }, 528 .probe = pem_probe, 529 .id_table = pem_id, 530 }; 531 532 module_i2c_driver(pem_driver); 533 534 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 535 MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver"); 536 MODULE_LICENSE("GPL"); 537