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> 32502b5a01SGuenter Roeck 33502b5a01SGuenter Roeck /* 34502b5a01SGuenter Roeck * This driver supports various Lineage Compact Power Line DC/DC and AC/DC 35502b5a01SGuenter Roeck * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others. 36502b5a01SGuenter Roeck * 37502b5a01SGuenter Roeck * The devices are nominally PMBus compliant. However, most standard PMBus 38502b5a01SGuenter Roeck * commands are not supported. Specifically, all hardware monitoring and 39502b5a01SGuenter Roeck * status reporting commands are non-standard. For this reason, a standard 40502b5a01SGuenter Roeck * PMBus driver can not be used. 41502b5a01SGuenter Roeck * 42502b5a01SGuenter Roeck * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541). 43502b5a01SGuenter Roeck * To ensure device access, this driver should only be used as client driver 44502b5a01SGuenter Roeck * to the pca9541 I2C master selector driver. 45502b5a01SGuenter Roeck */ 46502b5a01SGuenter Roeck 47502b5a01SGuenter Roeck /* Command codes */ 48502b5a01SGuenter Roeck #define PEM_OPERATION 0x01 49502b5a01SGuenter Roeck #define PEM_CLEAR_INFO_FLAGS 0x03 50502b5a01SGuenter Roeck #define PEM_VOUT_COMMAND 0x21 51502b5a01SGuenter Roeck #define PEM_VOUT_OV_FAULT_LIMIT 0x40 52502b5a01SGuenter Roeck #define PEM_READ_DATA_STRING 0xd0 53502b5a01SGuenter Roeck #define PEM_READ_INPUT_STRING 0xdc 54502b5a01SGuenter Roeck #define PEM_READ_FIRMWARE_REV 0xdd 55502b5a01SGuenter Roeck #define PEM_READ_RUN_TIMER 0xde 56502b5a01SGuenter Roeck #define PEM_FAN_HI_SPEED 0xdf 57502b5a01SGuenter Roeck #define PEM_FAN_NORMAL_SPEED 0xe0 58502b5a01SGuenter Roeck #define PEM_READ_FAN_SPEED 0xe1 59502b5a01SGuenter Roeck 60502b5a01SGuenter Roeck /* offsets in data string */ 61502b5a01SGuenter Roeck #define PEM_DATA_STATUS_2 0 62502b5a01SGuenter Roeck #define PEM_DATA_STATUS_1 1 63502b5a01SGuenter Roeck #define PEM_DATA_ALARM_2 2 64502b5a01SGuenter Roeck #define PEM_DATA_ALARM_1 3 65502b5a01SGuenter Roeck #define PEM_DATA_VOUT_LSB 4 66502b5a01SGuenter Roeck #define PEM_DATA_VOUT_MSB 5 67502b5a01SGuenter Roeck #define PEM_DATA_CURRENT 6 68502b5a01SGuenter Roeck #define PEM_DATA_TEMP 7 69502b5a01SGuenter Roeck 70502b5a01SGuenter Roeck /* Virtual entries, to report constants */ 71502b5a01SGuenter Roeck #define PEM_DATA_TEMP_MAX 10 72502b5a01SGuenter Roeck #define PEM_DATA_TEMP_CRIT 11 73502b5a01SGuenter Roeck 74502b5a01SGuenter Roeck /* offsets in input string */ 75502b5a01SGuenter Roeck #define PEM_INPUT_VOLTAGE 0 76502b5a01SGuenter Roeck #define PEM_INPUT_POWER_LSB 1 77502b5a01SGuenter Roeck #define PEM_INPUT_POWER_MSB 2 78502b5a01SGuenter Roeck 79502b5a01SGuenter Roeck /* offsets in fan data */ 80502b5a01SGuenter Roeck #define PEM_FAN_ADJUSTMENT 0 81502b5a01SGuenter Roeck #define PEM_FAN_FAN1 1 82502b5a01SGuenter Roeck #define PEM_FAN_FAN2 2 83502b5a01SGuenter Roeck #define PEM_FAN_FAN3 3 84502b5a01SGuenter Roeck 85502b5a01SGuenter Roeck /* Status register bits */ 86502b5a01SGuenter Roeck #define STS1_OUTPUT_ON (1 << 0) 87502b5a01SGuenter Roeck #define STS1_LEDS_FLASHING (1 << 1) 88502b5a01SGuenter Roeck #define STS1_EXT_FAULT (1 << 2) 89502b5a01SGuenter Roeck #define STS1_SERVICE_LED_ON (1 << 3) 90502b5a01SGuenter Roeck #define STS1_SHUTDOWN_OCCURRED (1 << 4) 91502b5a01SGuenter Roeck #define STS1_INT_FAULT (1 << 5) 92502b5a01SGuenter Roeck #define STS1_ISOLATION_TEST_OK (1 << 6) 93502b5a01SGuenter Roeck 94502b5a01SGuenter Roeck #define STS2_ENABLE_PIN_HI (1 << 0) 95502b5a01SGuenter Roeck #define STS2_DATA_OUT_RANGE (1 << 1) 96502b5a01SGuenter Roeck #define STS2_RESTARTED_OK (1 << 1) 97502b5a01SGuenter Roeck #define STS2_ISOLATION_TEST_FAIL (1 << 3) 98502b5a01SGuenter Roeck #define STS2_HIGH_POWER_CAP (1 << 4) 99502b5a01SGuenter Roeck #define STS2_INVALID_INSTR (1 << 5) 100502b5a01SGuenter Roeck #define STS2_WILL_RESTART (1 << 6) 101502b5a01SGuenter Roeck #define STS2_PEC_ERR (1 << 7) 102502b5a01SGuenter Roeck 103502b5a01SGuenter Roeck /* Alarm register bits */ 104502b5a01SGuenter Roeck #define ALRM1_VIN_OUT_LIMIT (1 << 0) 105502b5a01SGuenter Roeck #define ALRM1_VOUT_OUT_LIMIT (1 << 1) 106502b5a01SGuenter Roeck #define ALRM1_OV_VOLT_SHUTDOWN (1 << 2) 107502b5a01SGuenter Roeck #define ALRM1_VIN_OVERCURRENT (1 << 3) 108502b5a01SGuenter Roeck #define ALRM1_TEMP_WARNING (1 << 4) 109502b5a01SGuenter Roeck #define ALRM1_TEMP_SHUTDOWN (1 << 5) 110502b5a01SGuenter Roeck #define ALRM1_PRIMARY_FAULT (1 << 6) 111502b5a01SGuenter Roeck #define ALRM1_POWER_LIMIT (1 << 7) 112502b5a01SGuenter Roeck 113502b5a01SGuenter Roeck #define ALRM2_5V_OUT_LIMIT (1 << 1) 114502b5a01SGuenter Roeck #define ALRM2_TEMP_FAULT (1 << 2) 115502b5a01SGuenter Roeck #define ALRM2_OV_LOW (1 << 3) 116502b5a01SGuenter Roeck #define ALRM2_DCDC_TEMP_HIGH (1 << 4) 117502b5a01SGuenter Roeck #define ALRM2_PRI_TEMP_HIGH (1 << 5) 118502b5a01SGuenter Roeck #define ALRM2_NO_PRIMARY (1 << 6) 119502b5a01SGuenter Roeck #define ALRM2_FAN_FAULT (1 << 7) 120502b5a01SGuenter Roeck 121502b5a01SGuenter Roeck #define FIRMWARE_REV_LEN 4 122502b5a01SGuenter Roeck #define DATA_STRING_LEN 9 123502b5a01SGuenter Roeck #define INPUT_STRING_LEN 5 /* 4 for most devices */ 124502b5a01SGuenter Roeck #define FAN_SPEED_LEN 5 125502b5a01SGuenter Roeck 126502b5a01SGuenter Roeck struct pem_data { 127502b5a01SGuenter Roeck struct device *hwmon_dev; 128502b5a01SGuenter Roeck 129502b5a01SGuenter Roeck struct mutex update_lock; 130502b5a01SGuenter Roeck bool valid; 131502b5a01SGuenter Roeck bool fans_supported; 132502b5a01SGuenter Roeck int input_length; 133502b5a01SGuenter Roeck unsigned long last_updated; /* in jiffies */ 134502b5a01SGuenter Roeck 135502b5a01SGuenter Roeck u8 firmware_rev[FIRMWARE_REV_LEN]; 136502b5a01SGuenter Roeck u8 data_string[DATA_STRING_LEN]; 137502b5a01SGuenter Roeck u8 input_string[INPUT_STRING_LEN]; 138502b5a01SGuenter Roeck u8 fan_speed[FAN_SPEED_LEN]; 139502b5a01SGuenter Roeck }; 140502b5a01SGuenter Roeck 141502b5a01SGuenter Roeck static int pem_read_block(struct i2c_client *client, u8 command, u8 *data, 142502b5a01SGuenter Roeck int data_len) 143502b5a01SGuenter Roeck { 144502b5a01SGuenter Roeck u8 block_buffer[I2C_SMBUS_BLOCK_MAX]; 145502b5a01SGuenter Roeck int result; 146502b5a01SGuenter Roeck 147502b5a01SGuenter Roeck result = i2c_smbus_read_block_data(client, command, block_buffer); 148502b5a01SGuenter Roeck if (unlikely(result < 0)) 149502b5a01SGuenter Roeck goto abort; 150502b5a01SGuenter Roeck if (unlikely(result == 0xff || result != data_len)) { 151502b5a01SGuenter Roeck result = -EIO; 152502b5a01SGuenter Roeck goto abort; 153502b5a01SGuenter Roeck } 154502b5a01SGuenter Roeck memcpy(data, block_buffer, data_len); 155502b5a01SGuenter Roeck result = 0; 156502b5a01SGuenter Roeck abort: 157502b5a01SGuenter Roeck return result; 158502b5a01SGuenter Roeck } 159502b5a01SGuenter Roeck 160502b5a01SGuenter Roeck static struct pem_data *pem_update_device(struct device *dev) 161502b5a01SGuenter Roeck { 162502b5a01SGuenter Roeck struct i2c_client *client = to_i2c_client(dev); 163502b5a01SGuenter Roeck struct pem_data *data = i2c_get_clientdata(client); 164502b5a01SGuenter Roeck struct pem_data *ret = data; 165502b5a01SGuenter Roeck 166502b5a01SGuenter Roeck mutex_lock(&data->update_lock); 167502b5a01SGuenter Roeck 168502b5a01SGuenter Roeck if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 169502b5a01SGuenter Roeck int result; 170502b5a01SGuenter Roeck 171502b5a01SGuenter Roeck /* Read data string */ 172502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_DATA_STRING, 173502b5a01SGuenter Roeck data->data_string, 174502b5a01SGuenter Roeck sizeof(data->data_string)); 175502b5a01SGuenter Roeck if (unlikely(result < 0)) { 176502b5a01SGuenter Roeck ret = ERR_PTR(result); 177502b5a01SGuenter Roeck goto abort; 178502b5a01SGuenter Roeck } 179502b5a01SGuenter Roeck 180502b5a01SGuenter Roeck /* Read input string */ 181502b5a01SGuenter Roeck if (data->input_length) { 182502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_INPUT_STRING, 183502b5a01SGuenter Roeck data->input_string, 184502b5a01SGuenter Roeck data->input_length); 185502b5a01SGuenter Roeck if (unlikely(result < 0)) { 186502b5a01SGuenter Roeck ret = ERR_PTR(result); 187502b5a01SGuenter Roeck goto abort; 188502b5a01SGuenter Roeck } 189502b5a01SGuenter Roeck } 190502b5a01SGuenter Roeck 191502b5a01SGuenter Roeck /* Read fan speeds */ 192502b5a01SGuenter Roeck if (data->fans_supported) { 193502b5a01SGuenter Roeck result = pem_read_block(client, PEM_READ_FAN_SPEED, 194502b5a01SGuenter Roeck data->fan_speed, 195502b5a01SGuenter Roeck sizeof(data->fan_speed)); 196502b5a01SGuenter Roeck if (unlikely(result < 0)) { 197502b5a01SGuenter Roeck ret = ERR_PTR(result); 198502b5a01SGuenter Roeck goto abort; 199502b5a01SGuenter Roeck } 200502b5a01SGuenter Roeck } 201502b5a01SGuenter Roeck 202502b5a01SGuenter Roeck i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 203502b5a01SGuenter Roeck 204502b5a01SGuenter Roeck data->last_updated = jiffies; 205502b5a01SGuenter Roeck data->valid = 1; 206502b5a01SGuenter Roeck } 207502b5a01SGuenter Roeck abort: 208502b5a01SGuenter Roeck mutex_unlock(&data->update_lock); 209502b5a01SGuenter Roeck return ret; 210502b5a01SGuenter Roeck } 211502b5a01SGuenter Roeck 212502b5a01SGuenter Roeck static long pem_get_data(u8 *data, int len, int index) 213502b5a01SGuenter Roeck { 214502b5a01SGuenter Roeck long val; 215502b5a01SGuenter Roeck 216502b5a01SGuenter Roeck switch (index) { 217502b5a01SGuenter Roeck case PEM_DATA_VOUT_LSB: 218502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8)) * 5 / 2; 219502b5a01SGuenter Roeck break; 220502b5a01SGuenter Roeck case PEM_DATA_CURRENT: 221502b5a01SGuenter Roeck val = data[index] * 200; 222502b5a01SGuenter Roeck break; 223502b5a01SGuenter Roeck case PEM_DATA_TEMP: 224502b5a01SGuenter Roeck val = data[index] * 1000; 225502b5a01SGuenter Roeck break; 226502b5a01SGuenter Roeck case PEM_DATA_TEMP_MAX: 227502b5a01SGuenter Roeck val = 97 * 1000; /* 97 degrees C per datasheet */ 228502b5a01SGuenter Roeck break; 229502b5a01SGuenter Roeck case PEM_DATA_TEMP_CRIT: 230502b5a01SGuenter Roeck val = 107 * 1000; /* 107 degrees C per datasheet */ 231502b5a01SGuenter Roeck break; 232502b5a01SGuenter Roeck default: 233502b5a01SGuenter Roeck WARN_ON_ONCE(1); 234502b5a01SGuenter Roeck val = 0; 235502b5a01SGuenter Roeck } 236502b5a01SGuenter Roeck return val; 237502b5a01SGuenter Roeck } 238502b5a01SGuenter Roeck 239502b5a01SGuenter Roeck static long pem_get_input(u8 *data, int len, int index) 240502b5a01SGuenter Roeck { 241502b5a01SGuenter Roeck long val; 242502b5a01SGuenter Roeck 243502b5a01SGuenter Roeck switch (index) { 244502b5a01SGuenter Roeck case PEM_INPUT_VOLTAGE: 245502b5a01SGuenter Roeck if (len == INPUT_STRING_LEN) 246502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8) - 75) * 1000; 247502b5a01SGuenter Roeck else 248502b5a01SGuenter Roeck val = (data[index] - 75) * 1000; 249502b5a01SGuenter Roeck break; 250502b5a01SGuenter Roeck case PEM_INPUT_POWER_LSB: 251502b5a01SGuenter Roeck if (len == INPUT_STRING_LEN) 252502b5a01SGuenter Roeck index++; 253502b5a01SGuenter Roeck val = (data[index] + (data[index+1] << 8)) * 1000000L; 254502b5a01SGuenter Roeck break; 255502b5a01SGuenter Roeck default: 256502b5a01SGuenter Roeck WARN_ON_ONCE(1); 257502b5a01SGuenter Roeck val = 0; 258502b5a01SGuenter Roeck } 259502b5a01SGuenter Roeck return val; 260502b5a01SGuenter Roeck } 261502b5a01SGuenter Roeck 262502b5a01SGuenter Roeck static long pem_get_fan(u8 *data, int len, int index) 263502b5a01SGuenter Roeck { 264502b5a01SGuenter Roeck long val; 265502b5a01SGuenter Roeck 266502b5a01SGuenter Roeck switch (index) { 267502b5a01SGuenter Roeck case PEM_FAN_FAN1: 268502b5a01SGuenter Roeck case PEM_FAN_FAN2: 269502b5a01SGuenter Roeck case PEM_FAN_FAN3: 270502b5a01SGuenter Roeck val = data[index] * 100; 271502b5a01SGuenter Roeck break; 272502b5a01SGuenter Roeck default: 273502b5a01SGuenter Roeck WARN_ON_ONCE(1); 274502b5a01SGuenter Roeck val = 0; 275502b5a01SGuenter Roeck } 276502b5a01SGuenter Roeck return val; 277502b5a01SGuenter Roeck } 278502b5a01SGuenter Roeck 279502b5a01SGuenter Roeck /* 280502b5a01SGuenter Roeck * Show boolean, either a fault or an alarm. 281502b5a01SGuenter Roeck * .nr points to the register, .index is the bit mask to check 282502b5a01SGuenter Roeck */ 283502b5a01SGuenter Roeck static ssize_t pem_show_bool(struct device *dev, 284502b5a01SGuenter Roeck struct device_attribute *da, char *buf) 285502b5a01SGuenter Roeck { 286502b5a01SGuenter Roeck struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); 287502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 288502b5a01SGuenter Roeck u8 status; 289502b5a01SGuenter Roeck 290502b5a01SGuenter Roeck if (IS_ERR(data)) 291502b5a01SGuenter Roeck return PTR_ERR(data); 292502b5a01SGuenter Roeck 293502b5a01SGuenter Roeck status = data->data_string[attr->nr] & attr->index; 294502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", !!status); 295502b5a01SGuenter Roeck } 296502b5a01SGuenter Roeck 297502b5a01SGuenter Roeck static ssize_t pem_show_data(struct device *dev, struct device_attribute *da, 298502b5a01SGuenter Roeck char *buf) 299502b5a01SGuenter Roeck { 300502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 301502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 302502b5a01SGuenter Roeck long value; 303502b5a01SGuenter Roeck 304502b5a01SGuenter Roeck if (IS_ERR(data)) 305502b5a01SGuenter Roeck return PTR_ERR(data); 306502b5a01SGuenter Roeck 307502b5a01SGuenter Roeck value = pem_get_data(data->data_string, sizeof(data->data_string), 308502b5a01SGuenter Roeck attr->index); 309502b5a01SGuenter Roeck 310502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 311502b5a01SGuenter Roeck } 312502b5a01SGuenter Roeck 313502b5a01SGuenter Roeck static ssize_t pem_show_input(struct device *dev, struct device_attribute *da, 314502b5a01SGuenter Roeck char *buf) 315502b5a01SGuenter Roeck { 316502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 317502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 318502b5a01SGuenter Roeck long value; 319502b5a01SGuenter Roeck 320502b5a01SGuenter Roeck if (IS_ERR(data)) 321502b5a01SGuenter Roeck return PTR_ERR(data); 322502b5a01SGuenter Roeck 323502b5a01SGuenter Roeck value = pem_get_input(data->input_string, sizeof(data->input_string), 324502b5a01SGuenter Roeck attr->index); 325502b5a01SGuenter Roeck 326502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 327502b5a01SGuenter Roeck } 328502b5a01SGuenter Roeck 329502b5a01SGuenter Roeck static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da, 330502b5a01SGuenter Roeck char *buf) 331502b5a01SGuenter Roeck { 332502b5a01SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 333502b5a01SGuenter Roeck struct pem_data *data = pem_update_device(dev); 334502b5a01SGuenter Roeck long value; 335502b5a01SGuenter Roeck 336502b5a01SGuenter Roeck if (IS_ERR(data)) 337502b5a01SGuenter Roeck return PTR_ERR(data); 338502b5a01SGuenter Roeck 339502b5a01SGuenter Roeck value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed), 340502b5a01SGuenter Roeck attr->index); 341502b5a01SGuenter Roeck 342502b5a01SGuenter Roeck return snprintf(buf, PAGE_SIZE, "%ld\n", value); 343502b5a01SGuenter Roeck } 344502b5a01SGuenter Roeck 345502b5a01SGuenter Roeck /* Voltages */ 346502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL, 347502b5a01SGuenter Roeck PEM_DATA_VOUT_LSB); 348*d668a8b0SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL, 349502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT); 350502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL, 351502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN); 352502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL, 353502b5a01SGuenter Roeck PEM_INPUT_VOLTAGE); 354502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL, 355502b5a01SGuenter Roeck PEM_DATA_ALARM_1, 356502b5a01SGuenter Roeck ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT); 357502b5a01SGuenter Roeck 358502b5a01SGuenter Roeck /* Currents */ 359502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL, 360502b5a01SGuenter Roeck PEM_DATA_CURRENT); 361502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL, 362502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT); 363502b5a01SGuenter Roeck 364502b5a01SGuenter Roeck /* Power */ 365502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL, 366502b5a01SGuenter Roeck PEM_INPUT_POWER_LSB); 367502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL, 368502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT); 369502b5a01SGuenter Roeck 370502b5a01SGuenter Roeck /* Fans */ 371502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL, 372502b5a01SGuenter Roeck PEM_FAN_FAN1); 373502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL, 374502b5a01SGuenter Roeck PEM_FAN_FAN2); 375502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL, 376502b5a01SGuenter Roeck PEM_FAN_FAN3); 377502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL, 378502b5a01SGuenter Roeck PEM_DATA_ALARM_2, ALRM2_FAN_FAULT); 379502b5a01SGuenter Roeck 380502b5a01SGuenter Roeck /* Temperatures */ 381502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL, 382502b5a01SGuenter Roeck PEM_DATA_TEMP); 383502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL, 384502b5a01SGuenter Roeck PEM_DATA_TEMP_MAX); 385502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL, 386502b5a01SGuenter Roeck PEM_DATA_TEMP_CRIT); 387502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL, 388502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING); 389502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL, 390502b5a01SGuenter Roeck PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN); 391502b5a01SGuenter Roeck static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL, 392502b5a01SGuenter Roeck PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT); 393502b5a01SGuenter Roeck 394502b5a01SGuenter Roeck static struct attribute *pem_attributes[] = { 395502b5a01SGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr, 396*d668a8b0SGuenter Roeck &sensor_dev_attr_in1_alarm.dev_attr.attr, 397502b5a01SGuenter Roeck &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, 398502b5a01SGuenter Roeck &sensor_dev_attr_in2_alarm.dev_attr.attr, 399502b5a01SGuenter Roeck 400502b5a01SGuenter Roeck &sensor_dev_attr_curr1_alarm.dev_attr.attr, 401502b5a01SGuenter Roeck 402502b5a01SGuenter Roeck &sensor_dev_attr_power1_alarm.dev_attr.attr, 403502b5a01SGuenter Roeck 404502b5a01SGuenter Roeck &sensor_dev_attr_fan1_alarm.dev_attr.attr, 405502b5a01SGuenter Roeck 406502b5a01SGuenter Roeck &sensor_dev_attr_temp1_input.dev_attr.attr, 407502b5a01SGuenter Roeck &sensor_dev_attr_temp1_max.dev_attr.attr, 408502b5a01SGuenter Roeck &sensor_dev_attr_temp1_crit.dev_attr.attr, 409502b5a01SGuenter Roeck &sensor_dev_attr_temp1_alarm.dev_attr.attr, 410502b5a01SGuenter Roeck &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 411502b5a01SGuenter Roeck &sensor_dev_attr_temp1_fault.dev_attr.attr, 412502b5a01SGuenter Roeck 413502b5a01SGuenter Roeck NULL, 414502b5a01SGuenter Roeck }; 415502b5a01SGuenter Roeck 416502b5a01SGuenter Roeck static const struct attribute_group pem_group = { 417502b5a01SGuenter Roeck .attrs = pem_attributes, 418502b5a01SGuenter Roeck }; 419502b5a01SGuenter Roeck 420502b5a01SGuenter Roeck static struct attribute *pem_input_attributes[] = { 421502b5a01SGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr, 422502b5a01SGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr, 423502b5a01SGuenter Roeck &sensor_dev_attr_power1_input.dev_attr.attr, 424502b5a01SGuenter Roeck }; 425502b5a01SGuenter Roeck 426502b5a01SGuenter Roeck static const struct attribute_group pem_input_group = { 427502b5a01SGuenter Roeck .attrs = pem_input_attributes, 428502b5a01SGuenter Roeck }; 429502b5a01SGuenter Roeck 430502b5a01SGuenter Roeck static struct attribute *pem_fan_attributes[] = { 431502b5a01SGuenter Roeck &sensor_dev_attr_fan1_input.dev_attr.attr, 432502b5a01SGuenter Roeck &sensor_dev_attr_fan2_input.dev_attr.attr, 433502b5a01SGuenter Roeck &sensor_dev_attr_fan3_input.dev_attr.attr, 434502b5a01SGuenter Roeck }; 435502b5a01SGuenter Roeck 436502b5a01SGuenter Roeck static const struct attribute_group pem_fan_group = { 437502b5a01SGuenter Roeck .attrs = pem_fan_attributes, 438502b5a01SGuenter Roeck }; 439502b5a01SGuenter Roeck 440502b5a01SGuenter Roeck static int pem_probe(struct i2c_client *client, 441502b5a01SGuenter Roeck const struct i2c_device_id *id) 442502b5a01SGuenter Roeck { 443502b5a01SGuenter Roeck struct i2c_adapter *adapter = client->adapter; 444502b5a01SGuenter Roeck struct pem_data *data; 445502b5a01SGuenter Roeck int ret; 446502b5a01SGuenter Roeck 447502b5a01SGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA 448502b5a01SGuenter Roeck | I2C_FUNC_SMBUS_WRITE_BYTE)) 449502b5a01SGuenter Roeck return -ENODEV; 450502b5a01SGuenter Roeck 451502b5a01SGuenter Roeck data = kzalloc(sizeof(*data), GFP_KERNEL); 452502b5a01SGuenter Roeck if (!data) 453502b5a01SGuenter Roeck return -ENOMEM; 454502b5a01SGuenter Roeck 455502b5a01SGuenter Roeck i2c_set_clientdata(client, data); 456502b5a01SGuenter Roeck mutex_init(&data->update_lock); 457502b5a01SGuenter Roeck 458502b5a01SGuenter Roeck /* 459502b5a01SGuenter Roeck * We use the next two commands to determine if the device is really 460502b5a01SGuenter Roeck * there. 461502b5a01SGuenter Roeck */ 462502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_FIRMWARE_REV, 463502b5a01SGuenter Roeck data->firmware_rev, sizeof(data->firmware_rev)); 464502b5a01SGuenter Roeck if (ret < 0) 465502b5a01SGuenter Roeck goto out_kfree; 466502b5a01SGuenter Roeck 467502b5a01SGuenter Roeck ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 468502b5a01SGuenter Roeck if (ret < 0) 469502b5a01SGuenter Roeck goto out_kfree; 470502b5a01SGuenter Roeck 471502b5a01SGuenter Roeck dev_info(&client->dev, "Firmware revision %d.%d.%d\n", 472502b5a01SGuenter Roeck data->firmware_rev[0], data->firmware_rev[1], 473502b5a01SGuenter Roeck data->firmware_rev[2]); 474502b5a01SGuenter Roeck 475502b5a01SGuenter Roeck /* Register sysfs hooks */ 476502b5a01SGuenter Roeck ret = sysfs_create_group(&client->dev.kobj, &pem_group); 477502b5a01SGuenter Roeck if (ret) 478502b5a01SGuenter Roeck goto out_kfree; 479502b5a01SGuenter Roeck 480502b5a01SGuenter Roeck /* 481502b5a01SGuenter Roeck * Check if input readings are supported. 482502b5a01SGuenter Roeck * This is the case if we can read input data, 483502b5a01SGuenter Roeck * and if the returned data is not all zeros. 484502b5a01SGuenter Roeck * Note that input alarms are always supported. 485502b5a01SGuenter Roeck */ 486502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_INPUT_STRING, 487502b5a01SGuenter Roeck data->input_string, 488502b5a01SGuenter Roeck sizeof(data->input_string) - 1); 489502b5a01SGuenter Roeck if (!ret && (data->input_string[0] || data->input_string[1] || 490502b5a01SGuenter Roeck data->input_string[2])) 491502b5a01SGuenter Roeck data->input_length = sizeof(data->input_string) - 1; 492502b5a01SGuenter Roeck else if (ret < 0) { 493502b5a01SGuenter Roeck /* Input string is one byte longer for some devices */ 494502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_INPUT_STRING, 495502b5a01SGuenter Roeck data->input_string, 496502b5a01SGuenter Roeck sizeof(data->input_string)); 497502b5a01SGuenter Roeck if (!ret && (data->input_string[0] || data->input_string[1] || 498502b5a01SGuenter Roeck data->input_string[2] || data->input_string[3])) 499502b5a01SGuenter Roeck data->input_length = sizeof(data->input_string); 500502b5a01SGuenter Roeck } 501502b5a01SGuenter Roeck ret = 0; 502502b5a01SGuenter Roeck if (data->input_length) { 503502b5a01SGuenter Roeck ret = sysfs_create_group(&client->dev.kobj, &pem_input_group); 504502b5a01SGuenter Roeck if (ret) 505502b5a01SGuenter Roeck goto out_remove_groups; 506502b5a01SGuenter Roeck } 507502b5a01SGuenter Roeck 508502b5a01SGuenter Roeck /* 509502b5a01SGuenter Roeck * Check if fan speed readings are supported. 510502b5a01SGuenter Roeck * This is the case if we can read fan speed data, 511502b5a01SGuenter Roeck * and if the returned data is not all zeros. 512502b5a01SGuenter Roeck * Note that the fan alarm is always supported. 513502b5a01SGuenter Roeck */ 514502b5a01SGuenter Roeck ret = pem_read_block(client, PEM_READ_FAN_SPEED, 515502b5a01SGuenter Roeck data->fan_speed, 516502b5a01SGuenter Roeck sizeof(data->fan_speed)); 517502b5a01SGuenter Roeck if (!ret && (data->fan_speed[0] || data->fan_speed[1] || 518502b5a01SGuenter Roeck data->fan_speed[2] || data->fan_speed[3])) { 519502b5a01SGuenter Roeck data->fans_supported = true; 520502b5a01SGuenter Roeck ret = sysfs_create_group(&client->dev.kobj, &pem_fan_group); 521502b5a01SGuenter Roeck if (ret) 522502b5a01SGuenter Roeck goto out_remove_groups; 523502b5a01SGuenter Roeck } 524502b5a01SGuenter Roeck 525502b5a01SGuenter Roeck data->hwmon_dev = hwmon_device_register(&client->dev); 526502b5a01SGuenter Roeck if (IS_ERR(data->hwmon_dev)) { 527502b5a01SGuenter Roeck ret = PTR_ERR(data->hwmon_dev); 528502b5a01SGuenter Roeck goto out_remove_groups; 529502b5a01SGuenter Roeck } 530502b5a01SGuenter Roeck 531502b5a01SGuenter Roeck return 0; 532502b5a01SGuenter Roeck 533502b5a01SGuenter Roeck out_remove_groups: 534502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_input_group); 535502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_fan_group); 536502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_group); 537502b5a01SGuenter Roeck out_kfree: 538502b5a01SGuenter Roeck kfree(data); 539502b5a01SGuenter Roeck return ret; 540502b5a01SGuenter Roeck } 541502b5a01SGuenter Roeck 542502b5a01SGuenter Roeck static int pem_remove(struct i2c_client *client) 543502b5a01SGuenter Roeck { 544502b5a01SGuenter Roeck struct pem_data *data = i2c_get_clientdata(client); 545502b5a01SGuenter Roeck 546502b5a01SGuenter Roeck hwmon_device_unregister(data->hwmon_dev); 547502b5a01SGuenter Roeck 548502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_input_group); 549502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_fan_group); 550502b5a01SGuenter Roeck sysfs_remove_group(&client->dev.kobj, &pem_group); 551502b5a01SGuenter Roeck 552502b5a01SGuenter Roeck kfree(data); 553502b5a01SGuenter Roeck return 0; 554502b5a01SGuenter Roeck } 555502b5a01SGuenter Roeck 556502b5a01SGuenter Roeck static const struct i2c_device_id pem_id[] = { 557502b5a01SGuenter Roeck {"lineage_pem", 0}, 558502b5a01SGuenter Roeck {} 559502b5a01SGuenter Roeck }; 560502b5a01SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pem_id); 561502b5a01SGuenter Roeck 562502b5a01SGuenter Roeck static struct i2c_driver pem_driver = { 563502b5a01SGuenter Roeck .driver = { 564502b5a01SGuenter Roeck .name = "lineage_pem", 565502b5a01SGuenter Roeck }, 566502b5a01SGuenter Roeck .probe = pem_probe, 567502b5a01SGuenter Roeck .remove = pem_remove, 568502b5a01SGuenter Roeck .id_table = pem_id, 569502b5a01SGuenter Roeck }; 570502b5a01SGuenter Roeck 571502b5a01SGuenter Roeck static int __init pem_init(void) 572502b5a01SGuenter Roeck { 573502b5a01SGuenter Roeck return i2c_add_driver(&pem_driver); 574502b5a01SGuenter Roeck } 575502b5a01SGuenter Roeck 576502b5a01SGuenter Roeck static void __exit pem_exit(void) 577502b5a01SGuenter Roeck { 578502b5a01SGuenter Roeck i2c_del_driver(&pem_driver); 579502b5a01SGuenter Roeck } 580502b5a01SGuenter Roeck 581502b5a01SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>"); 582502b5a01SGuenter Roeck MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver"); 583502b5a01SGuenter Roeck MODULE_LICENSE("GPL"); 584502b5a01SGuenter Roeck 585502b5a01SGuenter Roeck module_init(pem_init); 586502b5a01SGuenter Roeck module_exit(pem_exit); 587