18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for Lineage Compact Power Line series of power entry modules. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2010, 2011 Ericsson AB. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Documentation: 88c2ecf20Sopenharmony_ci * http://www.lineagepower.com/oem/pdf/CPLI2C.pdf 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 188c2ecf20Sopenharmony_ci#include <linux/hwmon-sysfs.h> 198c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * This driver supports various Lineage Compact Power Line DC/DC and AC/DC 238c2ecf20Sopenharmony_ci * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * The devices are nominally PMBus compliant. However, most standard PMBus 268c2ecf20Sopenharmony_ci * commands are not supported. Specifically, all hardware monitoring and 278c2ecf20Sopenharmony_ci * status reporting commands are non-standard. For this reason, a standard 288c2ecf20Sopenharmony_ci * PMBus driver can not be used. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541). 318c2ecf20Sopenharmony_ci * To ensure device access, this driver should only be used as client driver 328c2ecf20Sopenharmony_ci * to the pca9541 I2C master selector driver. 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* Command codes */ 368c2ecf20Sopenharmony_ci#define PEM_OPERATION 0x01 378c2ecf20Sopenharmony_ci#define PEM_CLEAR_INFO_FLAGS 0x03 388c2ecf20Sopenharmony_ci#define PEM_VOUT_COMMAND 0x21 398c2ecf20Sopenharmony_ci#define PEM_VOUT_OV_FAULT_LIMIT 0x40 408c2ecf20Sopenharmony_ci#define PEM_READ_DATA_STRING 0xd0 418c2ecf20Sopenharmony_ci#define PEM_READ_INPUT_STRING 0xdc 428c2ecf20Sopenharmony_ci#define PEM_READ_FIRMWARE_REV 0xdd 438c2ecf20Sopenharmony_ci#define PEM_READ_RUN_TIMER 0xde 448c2ecf20Sopenharmony_ci#define PEM_FAN_HI_SPEED 0xdf 458c2ecf20Sopenharmony_ci#define PEM_FAN_NORMAL_SPEED 0xe0 468c2ecf20Sopenharmony_ci#define PEM_READ_FAN_SPEED 0xe1 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* offsets in data string */ 498c2ecf20Sopenharmony_ci#define PEM_DATA_STATUS_2 0 508c2ecf20Sopenharmony_ci#define PEM_DATA_STATUS_1 1 518c2ecf20Sopenharmony_ci#define PEM_DATA_ALARM_2 2 528c2ecf20Sopenharmony_ci#define PEM_DATA_ALARM_1 3 538c2ecf20Sopenharmony_ci#define PEM_DATA_VOUT_LSB 4 548c2ecf20Sopenharmony_ci#define PEM_DATA_VOUT_MSB 5 558c2ecf20Sopenharmony_ci#define PEM_DATA_CURRENT 6 568c2ecf20Sopenharmony_ci#define PEM_DATA_TEMP 7 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* Virtual entries, to report constants */ 598c2ecf20Sopenharmony_ci#define PEM_DATA_TEMP_MAX 10 608c2ecf20Sopenharmony_ci#define PEM_DATA_TEMP_CRIT 11 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* offsets in input string */ 638c2ecf20Sopenharmony_ci#define PEM_INPUT_VOLTAGE 0 648c2ecf20Sopenharmony_ci#define PEM_INPUT_POWER_LSB 1 658c2ecf20Sopenharmony_ci#define PEM_INPUT_POWER_MSB 2 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* offsets in fan data */ 688c2ecf20Sopenharmony_ci#define PEM_FAN_ADJUSTMENT 0 698c2ecf20Sopenharmony_ci#define PEM_FAN_FAN1 1 708c2ecf20Sopenharmony_ci#define PEM_FAN_FAN2 2 718c2ecf20Sopenharmony_ci#define PEM_FAN_FAN3 3 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* Status register bits */ 748c2ecf20Sopenharmony_ci#define STS1_OUTPUT_ON (1 << 0) 758c2ecf20Sopenharmony_ci#define STS1_LEDS_FLASHING (1 << 1) 768c2ecf20Sopenharmony_ci#define STS1_EXT_FAULT (1 << 2) 778c2ecf20Sopenharmony_ci#define STS1_SERVICE_LED_ON (1 << 3) 788c2ecf20Sopenharmony_ci#define STS1_SHUTDOWN_OCCURRED (1 << 4) 798c2ecf20Sopenharmony_ci#define STS1_INT_FAULT (1 << 5) 808c2ecf20Sopenharmony_ci#define STS1_ISOLATION_TEST_OK (1 << 6) 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci#define STS2_ENABLE_PIN_HI (1 << 0) 838c2ecf20Sopenharmony_ci#define STS2_DATA_OUT_RANGE (1 << 1) 848c2ecf20Sopenharmony_ci#define STS2_RESTARTED_OK (1 << 1) 858c2ecf20Sopenharmony_ci#define STS2_ISOLATION_TEST_FAIL (1 << 3) 868c2ecf20Sopenharmony_ci#define STS2_HIGH_POWER_CAP (1 << 4) 878c2ecf20Sopenharmony_ci#define STS2_INVALID_INSTR (1 << 5) 888c2ecf20Sopenharmony_ci#define STS2_WILL_RESTART (1 << 6) 898c2ecf20Sopenharmony_ci#define STS2_PEC_ERR (1 << 7) 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* Alarm register bits */ 928c2ecf20Sopenharmony_ci#define ALRM1_VIN_OUT_LIMIT (1 << 0) 938c2ecf20Sopenharmony_ci#define ALRM1_VOUT_OUT_LIMIT (1 << 1) 948c2ecf20Sopenharmony_ci#define ALRM1_OV_VOLT_SHUTDOWN (1 << 2) 958c2ecf20Sopenharmony_ci#define ALRM1_VIN_OVERCURRENT (1 << 3) 968c2ecf20Sopenharmony_ci#define ALRM1_TEMP_WARNING (1 << 4) 978c2ecf20Sopenharmony_ci#define ALRM1_TEMP_SHUTDOWN (1 << 5) 988c2ecf20Sopenharmony_ci#define ALRM1_PRIMARY_FAULT (1 << 6) 998c2ecf20Sopenharmony_ci#define ALRM1_POWER_LIMIT (1 << 7) 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define ALRM2_5V_OUT_LIMIT (1 << 1) 1028c2ecf20Sopenharmony_ci#define ALRM2_TEMP_FAULT (1 << 2) 1038c2ecf20Sopenharmony_ci#define ALRM2_OV_LOW (1 << 3) 1048c2ecf20Sopenharmony_ci#define ALRM2_DCDC_TEMP_HIGH (1 << 4) 1058c2ecf20Sopenharmony_ci#define ALRM2_PRI_TEMP_HIGH (1 << 5) 1068c2ecf20Sopenharmony_ci#define ALRM2_NO_PRIMARY (1 << 6) 1078c2ecf20Sopenharmony_ci#define ALRM2_FAN_FAULT (1 << 7) 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#define FIRMWARE_REV_LEN 4 1108c2ecf20Sopenharmony_ci#define DATA_STRING_LEN 9 1118c2ecf20Sopenharmony_ci#define INPUT_STRING_LEN 5 /* 4 for most devices */ 1128c2ecf20Sopenharmony_ci#define FAN_SPEED_LEN 5 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistruct pem_data { 1158c2ecf20Sopenharmony_ci struct i2c_client *client; 1168c2ecf20Sopenharmony_ci const struct attribute_group *groups[4]; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci struct mutex update_lock; 1198c2ecf20Sopenharmony_ci bool valid; 1208c2ecf20Sopenharmony_ci bool fans_supported; 1218c2ecf20Sopenharmony_ci int input_length; 1228c2ecf20Sopenharmony_ci unsigned long last_updated; /* in jiffies */ 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci u8 firmware_rev[FIRMWARE_REV_LEN]; 1258c2ecf20Sopenharmony_ci u8 data_string[DATA_STRING_LEN]; 1268c2ecf20Sopenharmony_ci u8 input_string[INPUT_STRING_LEN]; 1278c2ecf20Sopenharmony_ci u8 fan_speed[FAN_SPEED_LEN]; 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int pem_read_block(struct i2c_client *client, u8 command, u8 *data, 1318c2ecf20Sopenharmony_ci int data_len) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci u8 block_buffer[I2C_SMBUS_BLOCK_MAX]; 1348c2ecf20Sopenharmony_ci int result; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci result = i2c_smbus_read_block_data(client, command, block_buffer); 1378c2ecf20Sopenharmony_ci if (unlikely(result < 0)) 1388c2ecf20Sopenharmony_ci goto abort; 1398c2ecf20Sopenharmony_ci if (unlikely(result == 0xff || result != data_len)) { 1408c2ecf20Sopenharmony_ci result = -EIO; 1418c2ecf20Sopenharmony_ci goto abort; 1428c2ecf20Sopenharmony_ci } 1438c2ecf20Sopenharmony_ci memcpy(data, block_buffer, data_len); 1448c2ecf20Sopenharmony_ci result = 0; 1458c2ecf20Sopenharmony_ciabort: 1468c2ecf20Sopenharmony_ci return result; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic struct pem_data *pem_update_device(struct device *dev) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct pem_data *data = dev_get_drvdata(dev); 1528c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 1538c2ecf20Sopenharmony_ci struct pem_data *ret = data; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci mutex_lock(&data->update_lock); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 1588c2ecf20Sopenharmony_ci int result; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci /* Read data string */ 1618c2ecf20Sopenharmony_ci result = pem_read_block(client, PEM_READ_DATA_STRING, 1628c2ecf20Sopenharmony_ci data->data_string, 1638c2ecf20Sopenharmony_ci sizeof(data->data_string)); 1648c2ecf20Sopenharmony_ci if (unlikely(result < 0)) { 1658c2ecf20Sopenharmony_ci ret = ERR_PTR(result); 1668c2ecf20Sopenharmony_ci goto abort; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* Read input string */ 1708c2ecf20Sopenharmony_ci if (data->input_length) { 1718c2ecf20Sopenharmony_ci result = pem_read_block(client, PEM_READ_INPUT_STRING, 1728c2ecf20Sopenharmony_ci data->input_string, 1738c2ecf20Sopenharmony_ci data->input_length); 1748c2ecf20Sopenharmony_ci if (unlikely(result < 0)) { 1758c2ecf20Sopenharmony_ci ret = ERR_PTR(result); 1768c2ecf20Sopenharmony_ci goto abort; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci /* Read fan speeds */ 1818c2ecf20Sopenharmony_ci if (data->fans_supported) { 1828c2ecf20Sopenharmony_ci result = pem_read_block(client, PEM_READ_FAN_SPEED, 1838c2ecf20Sopenharmony_ci data->fan_speed, 1848c2ecf20Sopenharmony_ci sizeof(data->fan_speed)); 1858c2ecf20Sopenharmony_ci if (unlikely(result < 0)) { 1868c2ecf20Sopenharmony_ci ret = ERR_PTR(result); 1878c2ecf20Sopenharmony_ci goto abort; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci data->last_updated = jiffies; 1948c2ecf20Sopenharmony_ci data->valid = 1; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ciabort: 1978c2ecf20Sopenharmony_ci mutex_unlock(&data->update_lock); 1988c2ecf20Sopenharmony_ci return ret; 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic long pem_get_data(u8 *data, int len, int index) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci long val; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci switch (index) { 2068c2ecf20Sopenharmony_ci case PEM_DATA_VOUT_LSB: 2078c2ecf20Sopenharmony_ci val = (data[index] + (data[index+1] << 8)) * 5 / 2; 2088c2ecf20Sopenharmony_ci break; 2098c2ecf20Sopenharmony_ci case PEM_DATA_CURRENT: 2108c2ecf20Sopenharmony_ci val = data[index] * 200; 2118c2ecf20Sopenharmony_ci break; 2128c2ecf20Sopenharmony_ci case PEM_DATA_TEMP: 2138c2ecf20Sopenharmony_ci val = data[index] * 1000; 2148c2ecf20Sopenharmony_ci break; 2158c2ecf20Sopenharmony_ci case PEM_DATA_TEMP_MAX: 2168c2ecf20Sopenharmony_ci val = 97 * 1000; /* 97 degrees C per datasheet */ 2178c2ecf20Sopenharmony_ci break; 2188c2ecf20Sopenharmony_ci case PEM_DATA_TEMP_CRIT: 2198c2ecf20Sopenharmony_ci val = 107 * 1000; /* 107 degrees C per datasheet */ 2208c2ecf20Sopenharmony_ci break; 2218c2ecf20Sopenharmony_ci default: 2228c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 2238c2ecf20Sopenharmony_ci val = 0; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci return val; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic long pem_get_input(u8 *data, int len, int index) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci long val; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci switch (index) { 2338c2ecf20Sopenharmony_ci case PEM_INPUT_VOLTAGE: 2348c2ecf20Sopenharmony_ci if (len == INPUT_STRING_LEN) 2358c2ecf20Sopenharmony_ci val = (data[index] + (data[index+1] << 8) - 75) * 1000; 2368c2ecf20Sopenharmony_ci else 2378c2ecf20Sopenharmony_ci val = (data[index] - 75) * 1000; 2388c2ecf20Sopenharmony_ci break; 2398c2ecf20Sopenharmony_ci case PEM_INPUT_POWER_LSB: 2408c2ecf20Sopenharmony_ci if (len == INPUT_STRING_LEN) 2418c2ecf20Sopenharmony_ci index++; 2428c2ecf20Sopenharmony_ci val = (data[index] + (data[index+1] << 8)) * 1000000L; 2438c2ecf20Sopenharmony_ci break; 2448c2ecf20Sopenharmony_ci default: 2458c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 2468c2ecf20Sopenharmony_ci val = 0; 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci return val; 2498c2ecf20Sopenharmony_ci} 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_cistatic long pem_get_fan(u8 *data, int len, int index) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci long val; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci switch (index) { 2568c2ecf20Sopenharmony_ci case PEM_FAN_FAN1: 2578c2ecf20Sopenharmony_ci case PEM_FAN_FAN2: 2588c2ecf20Sopenharmony_ci case PEM_FAN_FAN3: 2598c2ecf20Sopenharmony_ci val = data[index] * 100; 2608c2ecf20Sopenharmony_ci break; 2618c2ecf20Sopenharmony_ci default: 2628c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 2638c2ecf20Sopenharmony_ci val = 0; 2648c2ecf20Sopenharmony_ci } 2658c2ecf20Sopenharmony_ci return val; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci/* 2698c2ecf20Sopenharmony_ci * Show boolean, either a fault or an alarm. 2708c2ecf20Sopenharmony_ci * .nr points to the register, .index is the bit mask to check 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_cistatic ssize_t pem_bool_show(struct device *dev, struct device_attribute *da, 2738c2ecf20Sopenharmony_ci char *buf) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); 2768c2ecf20Sopenharmony_ci struct pem_data *data = pem_update_device(dev); 2778c2ecf20Sopenharmony_ci u8 status; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci if (IS_ERR(data)) 2808c2ecf20Sopenharmony_ci return PTR_ERR(data); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci status = data->data_string[attr->nr] & attr->index; 2838c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%d\n", !!status); 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_cistatic ssize_t pem_data_show(struct device *dev, struct device_attribute *da, 2878c2ecf20Sopenharmony_ci char *buf) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 2908c2ecf20Sopenharmony_ci struct pem_data *data = pem_update_device(dev); 2918c2ecf20Sopenharmony_ci long value; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci if (IS_ERR(data)) 2948c2ecf20Sopenharmony_ci return PTR_ERR(data); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci value = pem_get_data(data->data_string, sizeof(data->data_string), 2978c2ecf20Sopenharmony_ci attr->index); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%ld\n", value); 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_cistatic ssize_t pem_input_show(struct device *dev, struct device_attribute *da, 3038c2ecf20Sopenharmony_ci char *buf) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 3068c2ecf20Sopenharmony_ci struct pem_data *data = pem_update_device(dev); 3078c2ecf20Sopenharmony_ci long value; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3108c2ecf20Sopenharmony_ci return PTR_ERR(data); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci value = pem_get_input(data->input_string, sizeof(data->input_string), 3138c2ecf20Sopenharmony_ci attr->index); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%ld\n", value); 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic ssize_t pem_fan_show(struct device *dev, struct device_attribute *da, 3198c2ecf20Sopenharmony_ci char *buf) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 3228c2ecf20Sopenharmony_ci struct pem_data *data = pem_update_device(dev); 3238c2ecf20Sopenharmony_ci long value; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (IS_ERR(data)) 3268c2ecf20Sopenharmony_ci return PTR_ERR(data); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed), 3298c2ecf20Sopenharmony_ci attr->index); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci return snprintf(buf, PAGE_SIZE, "%ld\n", value); 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci/* Voltages */ 3358c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in1_input, pem_data, PEM_DATA_VOUT_LSB); 3368c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_alarm, pem_bool, PEM_DATA_ALARM_1, 3378c2ecf20Sopenharmony_ci ALRM1_VOUT_OUT_LIMIT); 3388c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in1_crit_alarm, pem_bool, PEM_DATA_ALARM_1, 3398c2ecf20Sopenharmony_ci ALRM1_OV_VOLT_SHUTDOWN); 3408c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(in2_input, pem_input, PEM_INPUT_VOLTAGE); 3418c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(in2_alarm, pem_bool, PEM_DATA_ALARM_1, 3428c2ecf20Sopenharmony_ci ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci/* Currents */ 3458c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(curr1_input, pem_data, PEM_DATA_CURRENT); 3468c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, pem_bool, PEM_DATA_ALARM_1, 3478c2ecf20Sopenharmony_ci ALRM1_VIN_OVERCURRENT); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci/* Power */ 3508c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(power1_input, pem_input, PEM_INPUT_POWER_LSB); 3518c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(power1_alarm, pem_bool, PEM_DATA_ALARM_1, 3528c2ecf20Sopenharmony_ci ALRM1_POWER_LIMIT); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci/* Fans */ 3558c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan1_input, pem_fan, PEM_FAN_FAN1); 3568c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan2_input, pem_fan, PEM_FAN_FAN2); 3578c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(fan3_input, pem_fan, PEM_FAN_FAN3); 3588c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, pem_bool, PEM_DATA_ALARM_2, 3598c2ecf20Sopenharmony_ci ALRM2_FAN_FAULT); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci/* Temperatures */ 3628c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_input, pem_data, PEM_DATA_TEMP); 3638c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_max, pem_data, PEM_DATA_TEMP_MAX); 3648c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_RO(temp1_crit, pem_data, PEM_DATA_TEMP_CRIT); 3658c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, pem_bool, PEM_DATA_ALARM_1, 3668c2ecf20Sopenharmony_ci ALRM1_TEMP_WARNING); 3678c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, pem_bool, PEM_DATA_ALARM_1, 3688c2ecf20Sopenharmony_ci ALRM1_TEMP_SHUTDOWN); 3698c2ecf20Sopenharmony_cistatic SENSOR_DEVICE_ATTR_2_RO(temp1_fault, pem_bool, PEM_DATA_ALARM_2, 3708c2ecf20Sopenharmony_ci ALRM2_TEMP_FAULT); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_cistatic struct attribute *pem_attributes[] = { 3738c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_input.dev_attr.attr, 3748c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_alarm.dev_attr.attr, 3758c2ecf20Sopenharmony_ci &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, 3768c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_alarm.dev_attr.attr, 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci &sensor_dev_attr_curr1_alarm.dev_attr.attr, 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci &sensor_dev_attr_power1_alarm.dev_attr.attr, 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_alarm.dev_attr.attr, 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_input.dev_attr.attr, 3858c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_max.dev_attr.attr, 3868c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_crit.dev_attr.attr, 3878c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_alarm.dev_attr.attr, 3888c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 3898c2ecf20Sopenharmony_ci &sensor_dev_attr_temp1_fault.dev_attr.attr, 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci NULL, 3928c2ecf20Sopenharmony_ci}; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_cistatic const struct attribute_group pem_group = { 3958c2ecf20Sopenharmony_ci .attrs = pem_attributes, 3968c2ecf20Sopenharmony_ci}; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic struct attribute *pem_input_attributes[] = { 3998c2ecf20Sopenharmony_ci &sensor_dev_attr_in2_input.dev_attr.attr, 4008c2ecf20Sopenharmony_ci &sensor_dev_attr_curr1_input.dev_attr.attr, 4018c2ecf20Sopenharmony_ci &sensor_dev_attr_power1_input.dev_attr.attr, 4028c2ecf20Sopenharmony_ci NULL 4038c2ecf20Sopenharmony_ci}; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic const struct attribute_group pem_input_group = { 4068c2ecf20Sopenharmony_ci .attrs = pem_input_attributes, 4078c2ecf20Sopenharmony_ci}; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_cistatic struct attribute *pem_fan_attributes[] = { 4108c2ecf20Sopenharmony_ci &sensor_dev_attr_fan1_input.dev_attr.attr, 4118c2ecf20Sopenharmony_ci &sensor_dev_attr_fan2_input.dev_attr.attr, 4128c2ecf20Sopenharmony_ci &sensor_dev_attr_fan3_input.dev_attr.attr, 4138c2ecf20Sopenharmony_ci NULL 4148c2ecf20Sopenharmony_ci}; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_cistatic const struct attribute_group pem_fan_group = { 4178c2ecf20Sopenharmony_ci .attrs = pem_fan_attributes, 4188c2ecf20Sopenharmony_ci}; 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_cistatic int pem_probe(struct i2c_client *client) 4218c2ecf20Sopenharmony_ci{ 4228c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 4238c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 4248c2ecf20Sopenharmony_ci struct device *hwmon_dev; 4258c2ecf20Sopenharmony_ci struct pem_data *data; 4268c2ecf20Sopenharmony_ci int ret, idx = 0; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA 4298c2ecf20Sopenharmony_ci | I2C_FUNC_SMBUS_WRITE_BYTE)) 4308c2ecf20Sopenharmony_ci return -ENODEV; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 4338c2ecf20Sopenharmony_ci if (!data) 4348c2ecf20Sopenharmony_ci return -ENOMEM; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci data->client = client; 4378c2ecf20Sopenharmony_ci mutex_init(&data->update_lock); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* 4408c2ecf20Sopenharmony_ci * We use the next two commands to determine if the device is really 4418c2ecf20Sopenharmony_ci * there. 4428c2ecf20Sopenharmony_ci */ 4438c2ecf20Sopenharmony_ci ret = pem_read_block(client, PEM_READ_FIRMWARE_REV, 4448c2ecf20Sopenharmony_ci data->firmware_rev, sizeof(data->firmware_rev)); 4458c2ecf20Sopenharmony_ci if (ret < 0) 4468c2ecf20Sopenharmony_ci return ret; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); 4498c2ecf20Sopenharmony_ci if (ret < 0) 4508c2ecf20Sopenharmony_ci return ret; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci dev_info(dev, "Firmware revision %d.%d.%d\n", 4538c2ecf20Sopenharmony_ci data->firmware_rev[0], data->firmware_rev[1], 4548c2ecf20Sopenharmony_ci data->firmware_rev[2]); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* sysfs hooks */ 4578c2ecf20Sopenharmony_ci data->groups[idx++] = &pem_group; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci /* 4608c2ecf20Sopenharmony_ci * Check if input readings are supported. 4618c2ecf20Sopenharmony_ci * This is the case if we can read input data, 4628c2ecf20Sopenharmony_ci * and if the returned data is not all zeros. 4638c2ecf20Sopenharmony_ci * Note that input alarms are always supported. 4648c2ecf20Sopenharmony_ci */ 4658c2ecf20Sopenharmony_ci ret = pem_read_block(client, PEM_READ_INPUT_STRING, 4668c2ecf20Sopenharmony_ci data->input_string, 4678c2ecf20Sopenharmony_ci sizeof(data->input_string) - 1); 4688c2ecf20Sopenharmony_ci if (!ret && (data->input_string[0] || data->input_string[1] || 4698c2ecf20Sopenharmony_ci data->input_string[2])) 4708c2ecf20Sopenharmony_ci data->input_length = sizeof(data->input_string) - 1; 4718c2ecf20Sopenharmony_ci else if (ret < 0) { 4728c2ecf20Sopenharmony_ci /* Input string is one byte longer for some devices */ 4738c2ecf20Sopenharmony_ci ret = pem_read_block(client, PEM_READ_INPUT_STRING, 4748c2ecf20Sopenharmony_ci data->input_string, 4758c2ecf20Sopenharmony_ci sizeof(data->input_string)); 4768c2ecf20Sopenharmony_ci if (!ret && (data->input_string[0] || data->input_string[1] || 4778c2ecf20Sopenharmony_ci data->input_string[2] || data->input_string[3])) 4788c2ecf20Sopenharmony_ci data->input_length = sizeof(data->input_string); 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci if (data->input_length) 4828c2ecf20Sopenharmony_ci data->groups[idx++] = &pem_input_group; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci /* 4858c2ecf20Sopenharmony_ci * Check if fan speed readings are supported. 4868c2ecf20Sopenharmony_ci * This is the case if we can read fan speed data, 4878c2ecf20Sopenharmony_ci * and if the returned data is not all zeros. 4888c2ecf20Sopenharmony_ci * Note that the fan alarm is always supported. 4898c2ecf20Sopenharmony_ci */ 4908c2ecf20Sopenharmony_ci ret = pem_read_block(client, PEM_READ_FAN_SPEED, 4918c2ecf20Sopenharmony_ci data->fan_speed, 4928c2ecf20Sopenharmony_ci sizeof(data->fan_speed)); 4938c2ecf20Sopenharmony_ci if (!ret && (data->fan_speed[0] || data->fan_speed[1] || 4948c2ecf20Sopenharmony_ci data->fan_speed[2] || data->fan_speed[3])) { 4958c2ecf20Sopenharmony_ci data->fans_supported = true; 4968c2ecf20Sopenharmony_ci data->groups[idx++] = &pem_fan_group; 4978c2ecf20Sopenharmony_ci } 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 5008c2ecf20Sopenharmony_ci data, data->groups); 5018c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(hwmon_dev); 5028c2ecf20Sopenharmony_ci} 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_cistatic const struct i2c_device_id pem_id[] = { 5058c2ecf20Sopenharmony_ci {"lineage_pem", 0}, 5068c2ecf20Sopenharmony_ci {} 5078c2ecf20Sopenharmony_ci}; 5088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, pem_id); 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_cistatic struct i2c_driver pem_driver = { 5118c2ecf20Sopenharmony_ci .driver = { 5128c2ecf20Sopenharmony_ci .name = "lineage_pem", 5138c2ecf20Sopenharmony_ci }, 5148c2ecf20Sopenharmony_ci .probe_new = pem_probe, 5158c2ecf20Sopenharmony_ci .id_table = pem_id, 5168c2ecf20Sopenharmony_ci}; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_cimodule_i2c_driver(pem_driver); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 5218c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver"); 5228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 523