1502b5a01SGuenter Roeck /* 2502b5a01SGuenter Roeck * Driver for Lineage Compact Power Line series of power entry modules. 3502b5a01SGuenter Roeck * 4502b5a01SGuenter Roeck * Copyright (C) 2010, 2011 Ericsson AB. 5502b5a01SGuenter Roeck * 6502b5a01SGuenter Roeck * Documentation: 7502b5a01SGuenter Roeck * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf 8502b5a01SGuenter Roeck * 9502b5a01SGuenter Roeck * This program is free software; you can redistribute it and/or modify 10502b5a01SGuenter Roeck * it under the terms of the GNU General Public License as published by 11502b5a01SGuenter Roeck * the Free Software Foundation; either version 2 of the License, or 12502b5a01SGuenter Roeck * (at your option) any later version. 13502b5a01SGuenter Roeck * 14502b5a01SGuenter Roeck * This program is distributed in the hope that it will be useful, 15502b5a01SGuenter Roeck * but WITHOUT ANY WARRANTY; without even the implied warranty of 16502b5a01SGuenter Roeck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17502b5a01SGuenter Roeck * GNU General Public License for more details. 18502b5a01SGuenter Roeck * 19502b5a01SGuenter Roeck * You should have received a copy of the GNU General Public License 20502b5a01SGuenter Roeck * along with this program; if not, write to the Free Software 21502b5a01SGuenter Roeck * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22502b5a01SGuenter Roeck */ 23502b5a01SGuenter Roeck 24502b5a01SGuenter Roeck #include <linux/kernel.h> 25502b5a01SGuenter Roeck #include <linux/module.h> 26502b5a01SGuenter Roeck #include <linux/init.h> 27502b5a01SGuenter Roeck #include <linux/err.h> 28502b5a01SGuenter Roeck #include <linux/slab.h> 29502b5a01SGuenter Roeck #include <linux/i2c.h> 30502b5a01SGuenter Roeck #include <linux/hwmon.h> 31502b5a01SGuenter Roeck #include <linux/hwmon-sysfs.h> 32dcd8f392SJean Delvare #include <linux/jiffies.h> 33502b5a01SGuenter Roeck 34502b5a01SGuenter Roeck /* 35502b5a01SGuenter Roeck * This driver supports various Lineage Compact Power Line DC/DC and AC/DC 36502b5a01SGuenter Roeck * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others. 37502b5a01SGuenter Roeck * 38502b5a01SGuenter Roeck * The devices are nominally PMBus compliant. However, most standard PMBus 39502b5a01SGuenter Roeck * commands are not supported. Specifically, all hardware monitoring and 40502b5a01SGuenter Roeck * status reporting commands are non-standard. For this reason, a standard 41502b5a01SGuenter Roeck * PMBus driver can not be used. 42502b5a01SGuenter Roeck * 43502b5a01SGuenter Roeck * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541). 44502b5a01SGuenter Roeck * To ensure device access, this driver should only be used as client driver 45502b5a01SGuenter Roeck * to the pca9541 I2C master selector driver. 46502b5a01SGuenter Roeck */ 47502b5a01SGuenter Roeck 48502b5a01SGuenter Roeck /* Command codes */ 49502b5a01SGuenter Roeck #define PEM_OPERATION 0x01 50502b5a01SGuenter Roeck #define PEM_CLEAR_INFO_FLAGS 0x03 51502b5a01SGuenter Roeck #define PEM_VOUT_COMMAND 0x21 52502b5a01SGuenter Roeck #define PEM_VOUT_OV_FAULT_LIMIT 0x40 53502b5a01SGuenter Roeck #define PEM_READ_DATA_STRING 0xd0 54502b5a01SGuenter Roeck #define PEM_READ_INPUT_STRING 0xdc 55502b5a01SGuenter Roeck #define PEM_READ_FIRMWARE_REV 0xdd 56502b5a01SGuenter Roeck #define PEM_READ_RUN_TIMER 0xde 57502b5a01SGuenter Roeck #define PEM_FAN_HI_SPEED 0xdf 58502b5a01SGuenter Roeck #define PEM_FAN_NORMAL_SPEED 0xe0 59502b5a01SGuenter Roeck #define PEM_READ_FAN_SPEED 0xe1 60502b5a01SGuenter Roeck 61502b5a01SGuenter Roeck /* offsets in data string */ 62502b5a01SGuenter Roeck #define PEM_DATA_STATUS_2 0 63502b5a01SGuenter Roeck #define PEM_DATA_STATUS_1 1 64502b5a01SGuenter Roeck #define PEM_DATA_ALARM_2 2 65502b5a01SGuenter Roeck #define PEM_DATA_ALARM_1 3 66502b5a01SGuenter Roeck #define PEM_DATA_VOUT_LSB 4 67502b5a01SGuenter Roeck #define PEM_DATA_VOUT_MSB 5 68502b5a01SGuenter Roeck #define PEM_DATA_CURRENT 6 69502b5a01SGuenter Roeck #define PEM_DATA_TEMP 7 70502b5a01SGuenter Roeck 71502b5a01SGuenter Roeck /* Virtual entries, to report constants */ 72502b5a01SGuenter Roeck #define PEM_DATA_TEMP_MAX 10 73502b5a01SGuenter Roeck #define PEM_DATA_TEMP_CRIT 11 74502b5a01SGuenter Roeck 75502b5a01SGuenter Roeck /* offsets in input string */ 76502b5a01SGuenter Roeck #define PEM_INPUT_VOLTAGE 0 77502b5a01SGuenter Roeck #define PEM_INPUT_POWER_LSB 1 78502b5a01SGuenter Roeck #define PEM_INPUT_POWER_MSB 2 79502b5a01SGuenter Roeck 80502b5a01SGuenter Roeck /* offsets in fan data */ 81502b5a01SGuenter Roeck #define PEM_FAN_ADJUSTMENT 0 82502b5a01SGuenter Roeck #define PEM_FAN_FAN1 1 83502b5a01SGuenter Roeck #define PEM_FAN_FAN2 2 84502b5a01SGuenter Roeck #define PEM_FAN_FAN3 3 85502b5a01SGuenter Roeck 86502b5a01SGuenter Roeck /* Status register bits */ 87502b5a01SGuenter Roeck #define STS1_OUTPUT_ON (1 << 0) 88502b5a01SGuenter Roeck #define STS1_LEDS_FLASHING (1 << 1) 89502b5a01SGuenter Roeck #define STS1_EXT_FAULT (1 << 2) 90502b5a01SGuenter Roeck #define STS1_SERVICE_LED_ON (1 << 3) 91502b5a01SGuenter Roeck #define STS1_SHUTDOWN_OCCURRED (1 << 4) 92502b5a01SGuenter Roeck #define STS1_INT_FAULT (1 << 5) 93502b5a01SGuenter Roeck #define STS1_ISOLATION_TEST_OK (1 << 6) 94502b5a01SGuenter Roeck 95502b5a01SGuenter Roeck #define STS2_ENABLE_PIN_HI (1 << 0) 96502b5a01SGuenter Roeck #define STS2_DATA_OUT_RANGE (1 << 1) 97502b5a01SGuenter Roeck #define STS2_RESTARTED_OK (1 << 1) 98502b5a01SGuenter Roeck #define STS2_ISOLATION_TEST_FAIL (1 << 3) 99502b5a01SGuenter Roeck #define STS2_HIGH_POWER_CAP (1 << 4) 100502b5a01SGuenter Roeck #define STS2_INVALID_INSTR (1 << 5) 101502b5a01SGuenter Roeck #define STS2_WILL_RESTART (1 << 6) 102502b5a01SGuenter Roeck #define STS2_PEC_ERR (1 << 7) 103502b5a01SGuenter Roeck 104502b5a01SGuenter Roeck /* Alarm register bits */ 105502b5a01SGuenter Roeck #define ALRM1_VIN_OUT_LIMIT (1 << 0) 106502b5a01SGuenter Roeck #define ALRM1_VOUT_OUT_LIMIT (1 << 1) 107502b5a01SGuenter Roeck #define ALRM1_OV_VOLT_SHUTDOWN (1 << 2) 108502b5a01SGuenter Roeck #define ALRM1_VIN_OVERCURRENT (1 << 3) 109502b5a01SGuenter Roeck #define ALRM1_TEMP_WARNING (1 << 4) 110502b5a01SGuenter Roeck #define ALRM1_TEMP_SHUTDOWN (1 << 5) 111502b5a01SGuenter Roeck #define ALRM1_PRIMARY_FAULT (1 << 6) 112502b5a01SGuenter Roeck #define ALRM1_POWER_LIMIT (1 << 7) 113502b5a01SGuenter Roeck 114502b5a01SGuenter Roeck #define ALRM2_5V_OUT_LIMIT (1 << 1) 115502b5a01SGuenter Roeck #define ALRM2_TEMP_FAULT (1 << 2) 116502b5a01SGuenter Roeck #define ALRM2_OV_LOW (1 << 3) 117502b5a01SGuenter Roeck #define ALRM2_DCDC_TEMP_HIGH (1 << 4) 118502b5a01SGuenter Roeck #define ALRM2_PRI_TEMP_HIGH (1 << 5) 119502b5a01SGuenter Roeck #define ALRM2_NO_PRIMARY (1 << 6) 120502b5a01SGuenter Roeck #define ALRM2_FAN_FAULT (1 << 7) 121502b5a01SGuenter Roeck 122502b5a01SGuenter Roeck #define FIRMWARE_REV_LEN 4 123502b5a01SGuenter Roeck #define DATA_STRING_LEN 9 124502b5a01SGuenter Roeck #define INPUT_STRING_LEN 5 /* 4 for most devices */ 125502b5a01SGuenter Roeck #define FAN_SPEED_LEN 5 126502b5a01SGuenter Roeck 127502b5a01SGuenter Roeck struct pem_data { 128*82803252SAxel Lin struct i2c_client *client; 129*82803252SAxel Lin const struct attribute_group *groups[4]; 130502b5a01SGuenter Roeck 131502b5a01SGuenter Roeck struct mutex update_lock; 132502b5a01SGuenter Roeck bool valid; 133502b5a01SGuenter Roeck bool fans_supported; 134502b5a01SGuenter Roeck int input_length; 135502b5a01SGuenter Roeck unsigned long last_updated; /* in jiffies */ 136502b5a01SGuenter Roeck 137502b5a01SGuenter Roeck u8 firmware_rev[FIRMWARE_REV_LEN]; 138502b5a01SGuenter Roeck u8 data_string[DATA_STRING_LEN]; 139502b5a01SGuenter Roeck u8 input_string[INPUT_STRING_LEN]; 140502b5a01SGuenter Roeck u8 fan_speed[FAN_SPEED_LEN]; 141502b5a01SGuenter Roeck }; 142502b5a01SGuenter Roeck 143502b5a01SGuenter Roeck static int pem_read_block(struct i2c_client *client, u8 command, u8 *data, 144502b5a01SGuenter Roeck int data_len) 145502b5a01SGuenter Roeck { 146502b5a01SGuenter Roeck u8 block_buffer[I2C_SMBUS_BLOCK_MAX]; 147502b5a01SGuenter Roeck int result; 148502b5a01SGuenter Roeck 149502b5a01SGuenter Roeck result = i2c_smbus_read_block_data(client, command, block_buffer); 150502b5a01SGuenter Roeck if (unlikely(result < 0)) 151502b5a01SGuenter Roeck goto abort; 152502b5a01SGuenter Roeck if (unlikely(result == 0xff || result != data_len)) { 153502b5a01SGuenter Roeck result = -EIO; 154502b5a01SGuenter Roeck goto abort; 155502b5a01SGuenter Roeck } 156502b5a01SGuenter Roeck memcpy(data, block_buffer, data_len); 157502b5a01SGuenter Roeck result = 0; 158502b5a01SGuenter Roeck abort: 159502b5a01SGuenter Roeck return result; 160502b5a01SGuenter Roeck } 161502b5a01SGuenter Roeck 162502b5a01SGuenter Roeck static struct pem_data *pem_update_device(struct device *dev) 163502b5a01SGuenter Roeck { 164*82803252SAxel Lin struct pem_data *data = dev_get_drvdata(dev); 165*82803252SAxel Lin struct i2c_client *client = data->client; 166502b5a01SGuenter Roeck struct pem_data *ret = data; 167502b5a01SGuenter Roeck 168502b5a01SGuenter Roeck mutex_lock(&data->update_lock); 169502b5a01SGuenter Roeck 170502b5a01SGuenter Roeck if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 171502b5a01SGuenter Roeck int result; 172502b5a01SGuenter Roeck 173502b5a01SGuenter Roeck /* Read data string */ 174502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_DATA_STRING, 175502b5a01SGuenter Roeck data->data_string, 176502b5a01SGuenter Roeck sizeof(data->data_string)); 177502b5a01SGuenter Roeck if (unlikely(result < 0)) { 178502b5a01SGuenter Roeck ret = ERR_PTR(result); 179502b5a01SGuenter Roeck goto abort; 180502b5a01SGuenter Roeck } 181502b5a01SGuenter Roeck 182502b5a01SGuenter Roeck /* Read input string */ 183502b5a01SGuenter Roeck if (data->input_length) { 184502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_INPUT_STRING, 185502b5a01SGuenter Roeck data->input_string, 186502b5a01SGuenter Roeck data->input_length); 187502b5a01SGuenter Roeck if (unlikely(result < 0)) { 188502b5a01SGuenter Roeck ret = ERR_PTR(result); 189502b5a01SGuenter Roeck goto abort; 190502b5a01SGuenter Roeck } 191502b5a01SGuenter Roeck } 192502b5a01SGuenter Roeck 193502b5a01SGuenter Roeck /* Read fan speeds */ 194502b5a01SGuenter Roeck if (data->fans_supported) { 195502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_FAN_SPEED, 196502b5a01SGuenter Roeck data->fan_speed, 197502b5a01SGuenter Roeck sizeof(data->fan_speed)); 198502b5a01SGuenter Roeck if (unlikely(result < 0)) { 199502b5a01SGuenter Roeck ret = ERR_PTR(result); 200502b5a01SGuenter Roeck goto abort; 201502b5a01SGuenter Roeck } 202502b5a01SGuenter Roeck } 203502b5a01SGuenter Roeck 204502b5a01SGuenter Roeck i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 205502b5a01SGuenter Roeck 206502b5a01SGuenter Roeck data->last_updated = jiffies; 207502b5a01SGuenter Roeck data->valid = 1; 208502b5a01SGuenter Roeck } 209502b5a01SGuenter Roeck abort: 210502b5a01SGuenter Roeck mutex_unlock(&data->update_lock); 211502b5a01SGuenter Roeck return ret; 212502b5a01SGuenter Roeck } 213502b5a01SGuenter Roeck 214502b5a01SGuenter Roeck static long pem_get_data(u8 *data, int len, int index) 215502b5a01SGuenter Roeck { 216502b5a01SGuenter Roeck long val; 217502b5a01SGuenter Roeck 218502b5a01SGuenter Roeck switch (index) { 219502b5a01SGuenter Roeck case PEM_DATA_VOUT_LSB: 220502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8)) * 5 / 2; 221502b5a01SGuenter Roeck break; 222502b5a01SGuenter Roeck case PEM_DATA_CURRENT: 223502b5a01SGuenter Roeck val = data[index] * 200; 224502b5a01SGuenter Roeck break; 225502b5a01SGuenter Roeck case PEM_DATA_TEMP: 226502b5a01SGuenter Roeck val = data[index] * 1000; 227502b5a01SGuenter Roeck break; 228502b5a01SGuenter Roeck case PEM_DATA_TEMP_MAX: 229502b5a01SGuenter Roeck val = 97 * 1000; /* 97 degrees C per datasheet */ 230502b5a01SGuenter Roeck break; 231502b5a01SGuenter Roeck case PEM_DATA_TEMP_CRIT: 232502b5a01SGuenter Roeck val = 107 * 1000; /* 107 degrees C per datasheet */ 233502b5a01SGuenter Roeck break; 234502b5a01SGuenter Roeck default: 235502b5a01SGuenter Roeck WARN_ON_ONCE(1); 236502b5a01SGuenter Roeck val = 0; 237502b5a01SGuenter Roeck } 238502b5a01SGuenter Roeck return val; 239502b5a01SGuenter Roeck } 240502b5a01SGuenter Roeck 241502b5a01SGuenter Roeck static long pem_get_input(u8 *data, int len, int index) 242502b5a01SGuenter Roeck { 243502b5a01SGuenter Roeck long val; 244502b5a01SGuenter Roeck 245502b5a01SGuenter Roeck switch (index) { 246502b5a01SGuenter Roeck case PEM_INPUT_VOLTAGE: 247502b5a01SGuenter Roeck if (len == INPUT_STRING_LEN) 248502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8) - 75) * 1000; 249502b5a01SGuenter Roeck else 250502b5a01SGuenter Roeck val = (data[index] - 75) * 1000; 251502b5a01SGuenter Roeck break; 252502b5a01SGuenter Roeck case PEM_INPUT_POWER_LSB: 253502b5a01SGuenter Roeck if (len == INPUT_STRING_LEN) 254502b5a01SGuenter Roeck index++; 255502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8)) * 1000000L; 256502b5a01SGuenter Roeck break; 257502b5a01SGuenter Roeck default: 258502b5a01SGuenter Roeck WARN_ON_ONCE(1); 259502b5a01SGuenter Roeck val = 0; 260502b5a01SGuenter Roeck } 261502b5a01SGuenter Roeck return val; 262502b5a01SGuenter Roeck } 263502b5a01SGuenter Roeck 264502b5a01SGuenter Roeck static long pem_get_fan(u8 *data, int len, int index) 265502b5a01SGuenter Roeck { 266502b5a01SGuenter Roeck long val; 267502b5a01SGuenter Roeck 268502b5a01SGuenter Roeck switch (index) { 269502b5a01SGuenter Roeck case PEM_FAN_FAN1: 270502b5a01SGuenter Roeck case PEM_FAN_FAN2: 271502b5a01SGuenter Roeck case PEM_FAN_FAN3: 272502b5a01SGuenter Roeck val = data[index] * 100; 273502b5a01SGuenter Roeck break; 274502b5a01SGuenter Roeck default: 275502b5a01SGuenter Roeck WARN_ON_ONCE(1); 276502b5a01SGuenter Roeck val = 0; 277502b5a01SGuenter Roeck } 278502b5a01SGuenter Roeck return val; 279502b5a01SGuenter Roeck } 280502b5a01SGuenter Roeck 281502b5a01SGuenter Roeck /* 282502b5a01SGuenter Roeck * Show boolean, either a fault or an alarm. 283502b5a01SGuenter Roeck * .nr points to the register, .index is the bit mask to check 284502b5a01SGuenter Roeck */ 285502b5a01SGuenter Roeck static ssize_t pem_show_bool(struct device *dev, 286502b5a01SGuenter Roeck struct device_attribute *da, char *buf) 287502b5a01SGuenter Roeck { 288502b5a01SGuenter Roeck struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); 289502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 290502b5a01SGuenter Roeck u8 status; 291502b5a01SGuenter Roeck 292502b5a01SGuenter Roeck if (IS_ERR(data)) 293502b5a01SGuenter Roeck return PTR_ERR(data); 294502b5a01SGuenter Roeck 295502b5a01SGuenter Roeck status = data->data_string[attr->nr] & attr->index; 296502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", !!status); 297502b5a01SGuenter Roeck } 298502b5a01SGuenter Roeck 299502b5a01SGuenter Roeck static ssize_t pem_show_data(struct device *dev, struct device_attribute *da, 300502b5a01SGuenter Roeck char *buf) 301502b5a01SGuenter Roeck { 302502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 303502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 304502b5a01SGuenter Roeck long value; 305502b5a01SGuenter Roeck 306502b5a01SGuenter Roeck if (IS_ERR(data)) 307502b5a01SGuenter Roeck return PTR_ERR(data); 308502b5a01SGuenter Roeck 309502b5a01SGuenter Roeck value = pem_get_data(data->data_string, sizeof(data->data_string), 310502b5a01SGuenter Roeck attr->index); 311502b5a01SGuenter Roeck 312502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 313502b5a01SGuenter Roeck } 314502b5a01SGuenter Roeck 315502b5a01SGuenter Roeck static ssize_t pem_show_input(struct device *dev, struct device_attribute *da, 316502b5a01SGuenter Roeck char *buf) 317502b5a01SGuenter Roeck { 318502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 319502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 320502b5a01SGuenter Roeck long value; 321502b5a01SGuenter Roeck 322502b5a01SGuenter Roeck if (IS_ERR(data)) 323502b5a01SGuenter Roeck return PTR_ERR(data); 324502b5a01SGuenter Roeck 325502b5a01SGuenter Roeck value = pem_get_input(data->input_string, sizeof(data->input_string), 326502b5a01SGuenter Roeck attr->index); 327502b5a01SGuenter Roeck 328502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 329502b5a01SGuenter Roeck } 330502b5a01SGuenter Roeck 331502b5a01SGuenter Roeck static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da, 332502b5a01SGuenter Roeck char *buf) 333502b5a01SGuenter Roeck { 334502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 335502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 336502b5a01SGuenter Roeck long value; 337502b5a01SGuenter Roeck 338502b5a01SGuenter Roeck if (IS_ERR(data)) 339502b5a01SGuenter Roeck return PTR_ERR(data); 340502b5a01SGuenter Roeck 341502b5a01SGuenter Roeck value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed), 342502b5a01SGuenter Roeck attr->index); 343502b5a01SGuenter Roeck 344502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 345502b5a01SGuenter Roeck } 346502b5a01SGuenter Roeck 347502b5a01SGuenter Roeck /* Voltages */ 348502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL, 349502b5a01SGuenter Roeck PEM_DATA_VOUT_LSB); 350d668a8b0SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL, 351502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT); 352502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL, 353502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN); 354502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL, 355502b5a01SGuenter Roeck PEM_INPUT_VOLTAGE); 356502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL, 357502b5a01SGuenter Roeck PEM_DATA_ALARM_1, 358502b5a01SGuenter Roeck ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT); 359502b5a01SGuenter Roeck 360502b5a01SGuenter Roeck /* Currents */ 361502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL, 362502b5a01SGuenter Roeck PEM_DATA_CURRENT); 363502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL, 364502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT); 365502b5a01SGuenter Roeck 366502b5a01SGuenter Roeck /* Power */ 367502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL, 368502b5a01SGuenter Roeck PEM_INPUT_POWER_LSB); 369502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL, 370502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT); 371502b5a01SGuenter Roeck 372502b5a01SGuenter Roeck /* Fans */ 373502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL, 374502b5a01SGuenter Roeck PEM_FAN_FAN1); 375502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL, 376502b5a01SGuenter Roeck PEM_FAN_FAN2); 377502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL, 378502b5a01SGuenter Roeck PEM_FAN_FAN3); 379502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL, 380502b5a01SGuenter Roeck PEM_DATA_ALARM_2, ALRM2_FAN_FAULT); 381502b5a01SGuenter Roeck 382502b5a01SGuenter Roeck /* Temperatures */ 383502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL, 384502b5a01SGuenter Roeck PEM_DATA_TEMP); 385502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL, 386502b5a01SGuenter Roeck PEM_DATA_TEMP_MAX); 387502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL, 388502b5a01SGuenter Roeck PEM_DATA_TEMP_CRIT); 389502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL, 390502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING); 391502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL, 392502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN); 393502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL, 394502b5a01SGuenter Roeck PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT); 395502b5a01SGuenter Roeck 396502b5a01SGuenter Roeck static struct attribute *pem_attributes[] = { 397502b5a01SGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr, 398d668a8b0SGuenter Roeck &sensor_dev_attr_in1_alarm.dev_attr.attr, 399502b5a01SGuenter Roeck &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, 400502b5a01SGuenter Roeck &sensor_dev_attr_in2_alarm.dev_attr.attr, 401502b5a01SGuenter Roeck 402502b5a01SGuenter Roeck &sensor_dev_attr_curr1_alarm.dev_attr.attr, 403502b5a01SGuenter Roeck 404502b5a01SGuenter Roeck &sensor_dev_attr_power1_alarm.dev_attr.attr, 405502b5a01SGuenter Roeck 406502b5a01SGuenter Roeck &sensor_dev_attr_fan1_alarm.dev_attr.attr, 407502b5a01SGuenter Roeck 408502b5a01SGuenter Roeck &sensor_dev_attr_temp1_input.dev_attr.attr, 409502b5a01SGuenter Roeck &sensor_dev_attr_temp1_max.dev_attr.attr, 410502b5a01SGuenter Roeck &sensor_dev_attr_temp1_crit.dev_attr.attr, 411502b5a01SGuenter Roeck &sensor_dev_attr_temp1_alarm.dev_attr.attr, 412502b5a01SGuenter Roeck &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 413502b5a01SGuenter Roeck &sensor_dev_attr_temp1_fault.dev_attr.attr, 414502b5a01SGuenter Roeck 415502b5a01SGuenter Roeck NULL, 416502b5a01SGuenter Roeck }; 417502b5a01SGuenter Roeck 418502b5a01SGuenter Roeck static const struct attribute_group pem_group = { 419502b5a01SGuenter Roeck .attrs = pem_attributes, 420502b5a01SGuenter Roeck }; 421502b5a01SGuenter Roeck 422502b5a01SGuenter Roeck static struct attribute *pem_input_attributes[] = { 423502b5a01SGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr, 424502b5a01SGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr, 425502b5a01SGuenter Roeck &sensor_dev_attr_power1_input.dev_attr.attr, 426df069079SAxel Lin NULL 427502b5a01SGuenter Roeck }; 428502b5a01SGuenter Roeck 429502b5a01SGuenter Roeck static const struct attribute_group pem_input_group = { 430502b5a01SGuenter Roeck .attrs = pem_input_attributes, 431502b5a01SGuenter Roeck }; 432502b5a01SGuenter Roeck 433502b5a01SGuenter Roeck static struct attribute *pem_fan_attributes[] = { 434502b5a01SGuenter Roeck &sensor_dev_attr_fan1_input.dev_attr.attr, 435502b5a01SGuenter Roeck &sensor_dev_attr_fan2_input.dev_attr.attr, 436502b5a01SGuenter Roeck &sensor_dev_attr_fan3_input.dev_attr.attr, 437df069079SAxel Lin NULL 438502b5a01SGuenter Roeck }; 439502b5a01SGuenter Roeck 440502b5a01SGuenter Roeck static const struct attribute_group pem_fan_group = { 441502b5a01SGuenter Roeck .attrs = pem_fan_attributes, 442502b5a01SGuenter Roeck }; 443502b5a01SGuenter Roeck 444502b5a01SGuenter Roeck static int pem_probe(struct i2c_client *client, 445502b5a01SGuenter Roeck const struct i2c_device_id *id) 446502b5a01SGuenter Roeck { 447502b5a01SGuenter Roeck struct i2c_adapter *adapter = client->adapter; 448*82803252SAxel Lin struct device *dev = &client->dev; 449*82803252SAxel Lin struct device *hwmon_dev; 450502b5a01SGuenter Roeck struct pem_data *data; 451*82803252SAxel Lin int ret, idx = 0; 452502b5a01SGuenter Roeck 453502b5a01SGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA 454502b5a01SGuenter Roeck | I2C_FUNC_SMBUS_WRITE_BYTE)) 455502b5a01SGuenter Roeck return -ENODEV; 456502b5a01SGuenter Roeck 457*82803252SAxel Lin data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 458502b5a01SGuenter Roeck if (!data) 459502b5a01SGuenter Roeck return -ENOMEM; 460502b5a01SGuenter Roeck 461*82803252SAxel Lin data->client = client; 462502b5a01SGuenter Roeck mutex_init(&data->update_lock); 463502b5a01SGuenter Roeck 464502b5a01SGuenter Roeck /* 465502b5a01SGuenter Roeck * We use the next two commands to determine if the device is really 466502b5a01SGuenter Roeck * there. 467502b5a01SGuenter Roeck */ 468502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_FIRMWARE_REV, 469502b5a01SGuenter Roeck data->firmware_rev, sizeof(data->firmware_rev)); 470502b5a01SGuenter Roeck if (ret < 0) 47107404aabSGuenter Roeck return ret; 472502b5a01SGuenter Roeck 473502b5a01SGuenter Roeck ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 474502b5a01SGuenter Roeck if (ret < 0) 47507404aabSGuenter Roeck return ret; 476502b5a01SGuenter Roeck 477*82803252SAxel Lin dev_info(dev, "Firmware revision %d.%d.%d\n", 478502b5a01SGuenter Roeck data->firmware_rev[0], data->firmware_rev[1], 479502b5a01SGuenter Roeck data->firmware_rev[2]); 480502b5a01SGuenter Roeck 481*82803252SAxel Lin /* sysfs hooks */ 482*82803252SAxel Lin data->groups[idx++] = &pem_group; 483502b5a01SGuenter Roeck 484502b5a01SGuenter Roeck /* 485502b5a01SGuenter Roeck * Check if input readings are supported. 486502b5a01SGuenter Roeck * This is the case if we can read input data, 487502b5a01SGuenter Roeck * and if the returned data is not all zeros. 488502b5a01SGuenter Roeck * Note that input alarms are always supported. 489502b5a01SGuenter Roeck */ 490502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_INPUT_STRING, 491502b5a01SGuenter Roeck data->input_string, 492502b5a01SGuenter Roeck sizeof(data->input_string) - 1); 493502b5a01SGuenter Roeck if (!ret && (data->input_string[0] || data->input_string[1] || 494502b5a01SGuenter Roeck data->input_string[2])) 495502b5a01SGuenter Roeck data->input_length = sizeof(data->input_string) - 1; 496502b5a01SGuenter Roeck else if (ret < 0) { 497502b5a01SGuenter Roeck /* Input string is one byte longer for some devices */ 498502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_INPUT_STRING, 499502b5a01SGuenter Roeck data->input_string, 500502b5a01SGuenter Roeck sizeof(data->input_string)); 501502b5a01SGuenter Roeck if (!ret && (data->input_string[0] || data->input_string[1] || 502502b5a01SGuenter Roeck data->input_string[2] || data->input_string[3])) 503502b5a01SGuenter Roeck data->input_length = sizeof(data->input_string); 504502b5a01SGuenter Roeck } 505*82803252SAxel Lin 506*82803252SAxel Lin if (data->input_length) 507*82803252SAxel Lin data->groups[idx++] = &pem_input_group; 508502b5a01SGuenter Roeck 509502b5a01SGuenter Roeck /* 510502b5a01SGuenter Roeck * Check if fan speed readings are supported. 511502b5a01SGuenter Roeck * This is the case if we can read fan speed data, 512502b5a01SGuenter Roeck * and if the returned data is not all zeros. 513502b5a01SGuenter Roeck * Note that the fan alarm is always supported. 514502b5a01SGuenter Roeck */ 515502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_FAN_SPEED, 516502b5a01SGuenter Roeck data->fan_speed, 517502b5a01SGuenter Roeck sizeof(data->fan_speed)); 518502b5a01SGuenter Roeck if (!ret && (data->fan_speed[0] || data->fan_speed[1] || 519502b5a01SGuenter Roeck data->fan_speed[2] || data->fan_speed[3])) { 520502b5a01SGuenter Roeck data->fans_supported = true; 521*82803252SAxel Lin data->groups[idx++] = &pem_fan_group; 522502b5a01SGuenter Roeck } 523502b5a01SGuenter Roeck 524*82803252SAxel Lin hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 525*82803252SAxel Lin data, data->groups); 526*82803252SAxel Lin return PTR_ERR_OR_ZERO(hwmon_dev); 527502b5a01SGuenter Roeck } 528502b5a01SGuenter Roeck 529502b5a01SGuenter Roeck static const struct i2c_device_id pem_id[] = { 530502b5a01SGuenter Roeck {"lineage_pem", 0}, 531502b5a01SGuenter Roeck {} 532502b5a01SGuenter Roeck }; 533502b5a01SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pem_id); 534502b5a01SGuenter Roeck 535502b5a01SGuenter Roeck static struct i2c_driver pem_driver = { 536502b5a01SGuenter Roeck .driver = { 537502b5a01SGuenter Roeck .name = "lineage_pem", 538502b5a01SGuenter Roeck }, 539502b5a01SGuenter Roeck .probe = pem_probe, 540502b5a01SGuenter Roeck .id_table = pem_id, 541502b5a01SGuenter Roeck }; 542502b5a01SGuenter Roeck 543f0967eeaSAxel Lin module_i2c_driver(pem_driver); 544502b5a01SGuenter Roeck 545bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 546502b5a01SGuenter Roeck MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver"); 547502b5a01SGuenter Roeck MODULE_LICENSE("GPL"); 548