Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for Lineage Compact Power Line series of power entry modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010, 2011 Ericsson AB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Documentation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * The devices are nominally PMBus compliant. However, most standard PMBus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * commands are not supported. Specifically, all hardware monitoring and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * status reporting commands are non-standard. For this reason, a standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * PMBus driver can not be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * To ensure device access, this driver should only be used as client driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * to the pca9541 I2C master selector driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /* Command codes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define PEM_OPERATION		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define PEM_CLEAR_INFO_FLAGS	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define PEM_VOUT_COMMAND	0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define PEM_VOUT_OV_FAULT_LIMIT	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define PEM_READ_DATA_STRING	0xd0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define PEM_READ_INPUT_STRING	0xdc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define PEM_READ_FIRMWARE_REV	0xdd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define PEM_READ_RUN_TIMER	0xde
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define PEM_FAN_HI_SPEED	0xdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define PEM_FAN_NORMAL_SPEED	0xe0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define PEM_READ_FAN_SPEED	0xe1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* offsets in data string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define PEM_DATA_STATUS_2	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define PEM_DATA_STATUS_1	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define PEM_DATA_ALARM_2	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define PEM_DATA_ALARM_1	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define PEM_DATA_VOUT_LSB	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define PEM_DATA_VOUT_MSB	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define PEM_DATA_CURRENT	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define PEM_DATA_TEMP		7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /* Virtual entries, to report constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define PEM_DATA_TEMP_MAX	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define PEM_DATA_TEMP_CRIT	11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /* offsets in input string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #define PEM_INPUT_VOLTAGE	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define PEM_INPUT_POWER_LSB	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define PEM_INPUT_POWER_MSB	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) /* offsets in fan data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define PEM_FAN_ADJUSTMENT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define PEM_FAN_FAN1		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define PEM_FAN_FAN2		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define PEM_FAN_FAN3		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /* Status register bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define STS1_OUTPUT_ON		(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define STS1_LEDS_FLASHING	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define STS1_EXT_FAULT		(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #define STS1_SERVICE_LED_ON	(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define STS1_SHUTDOWN_OCCURRED	(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define STS1_INT_FAULT		(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define STS1_ISOLATION_TEST_OK	(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define STS2_ENABLE_PIN_HI	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define STS2_DATA_OUT_RANGE	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define STS2_RESTARTED_OK	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define STS2_ISOLATION_TEST_FAIL (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define STS2_HIGH_POWER_CAP	(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define STS2_INVALID_INSTR	(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define STS2_WILL_RESTART	(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #define STS2_PEC_ERR		(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /* Alarm register bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define ALRM1_VIN_OUT_LIMIT	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) #define ALRM1_VOUT_OUT_LIMIT	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define ALRM1_OV_VOLT_SHUTDOWN	(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define ALRM1_VIN_OVERCURRENT	(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define ALRM1_TEMP_WARNING	(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define ALRM1_TEMP_SHUTDOWN	(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #define ALRM1_PRIMARY_FAULT	(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #define ALRM1_POWER_LIMIT	(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define ALRM2_5V_OUT_LIMIT	(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define ALRM2_TEMP_FAULT	(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define ALRM2_OV_LOW		(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define ALRM2_DCDC_TEMP_HIGH	(1 << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define ALRM2_PRI_TEMP_HIGH	(1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define ALRM2_NO_PRIMARY	(1 << 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define ALRM2_FAN_FAULT		(1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define FIRMWARE_REV_LEN	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define DATA_STRING_LEN		9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define INPUT_STRING_LEN	5	/* 4 for most devices	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define FAN_SPEED_LEN		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct pem_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	const struct attribute_group *groups[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	bool fans_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int input_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned long last_updated;	/* in jiffies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	u8 firmware_rev[FIRMWARE_REV_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	u8 data_string[DATA_STRING_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	u8 input_string[INPUT_STRING_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	u8 fan_speed[FAN_SPEED_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			  int data_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	result = i2c_smbus_read_block_data(client, command, block_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (unlikely(result < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (unlikely(result == 0xff || result != data_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		result = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	memcpy(data, block_buffer, data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static struct pem_data *pem_update_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct pem_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct i2c_client *client = data->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct pem_data *ret = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		/* Read data string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		result = pem_read_block(client, PEM_READ_DATA_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 					data->data_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 					sizeof(data->data_string));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (unlikely(result < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			ret = ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		/* Read input string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (data->input_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			result = pem_read_block(client, PEM_READ_INPUT_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 						data->input_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 						data->input_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			if (unlikely(result < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				ret = ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		/* Read fan speeds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (data->fans_supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			result = pem_read_block(client, PEM_READ_FAN_SPEED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 						data->fan_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 						sizeof(data->fan_speed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			if (unlikely(result < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				ret = ERR_PTR(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		data->last_updated = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		data->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static long pem_get_data(u8 *data, int len, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	case PEM_DATA_VOUT_LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		val = (data[index] + (data[index+1] << 8)) * 5 / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	case PEM_DATA_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		val = data[index] * 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	case PEM_DATA_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		val = data[index] * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	case PEM_DATA_TEMP_MAX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		val = 97 * 1000;	/* 97 degrees C per datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	case PEM_DATA_TEMP_CRIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		val = 107 * 1000;	/* 107 degrees C per datasheet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static long pem_get_input(u8 *data, int len, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	case PEM_INPUT_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (len == INPUT_STRING_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			val = (data[index] + (data[index+1] << 8) - 75) * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			val = (data[index] - 75) * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	case PEM_INPUT_POWER_LSB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (len == INPUT_STRING_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		val = (data[index] + (data[index+1] << 8)) * 1000000L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static long pem_get_fan(u8 *data, int len, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	switch (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	case PEM_FAN_FAN1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	case PEM_FAN_FAN2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	case PEM_FAN_FAN3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		val = data[index] * 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  * Show boolean, either a fault or an alarm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  * .nr points to the register, .index is the bit mask to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static ssize_t pem_bool_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct pem_data *data = pem_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	status = data->data_string[attr->nr] & attr->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return snprintf(buf, PAGE_SIZE, "%d\n", !!status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static ssize_t pem_data_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct pem_data *data = pem_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	value = pem_get_data(data->data_string, sizeof(data->data_string),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			     attr->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return snprintf(buf, PAGE_SIZE, "%ld\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static ssize_t pem_input_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct pem_data *data = pem_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	value = pem_get_input(data->input_string, sizeof(data->input_string),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			      attr->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return snprintf(buf, PAGE_SIZE, "%ld\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static ssize_t pem_fan_show(struct device *dev, struct device_attribute *da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			    char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct pem_data *data = pem_update_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (IS_ERR(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return PTR_ERR(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			    attr->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return snprintf(buf, PAGE_SIZE, "%ld\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Voltages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) static SENSOR_DEVICE_ATTR_RO(in1_input, pem_data, PEM_DATA_VOUT_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			       ALRM1_VOUT_OUT_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static SENSOR_DEVICE_ATTR_2_RO(in1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			       ALRM1_OV_VOLT_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static SENSOR_DEVICE_ATTR_RO(in2_input, pem_input, PEM_INPUT_VOLTAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			       ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /* Currents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static SENSOR_DEVICE_ATTR_RO(curr1_input, pem_data, PEM_DATA_CURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			       ALRM1_VIN_OVERCURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /* Power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static SENSOR_DEVICE_ATTR_RO(power1_input, pem_input, PEM_INPUT_POWER_LSB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static SENSOR_DEVICE_ATTR_2_RO(power1_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			       ALRM1_POWER_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* Fans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static SENSOR_DEVICE_ATTR_RO(fan1_input, pem_fan, PEM_FAN_FAN1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static SENSOR_DEVICE_ATTR_RO(fan2_input, pem_fan, PEM_FAN_FAN2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static SENSOR_DEVICE_ATTR_RO(fan3_input, pem_fan, PEM_FAN_FAN3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, pem_bool, PEM_DATA_ALARM_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			       ALRM2_FAN_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* Temperatures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static SENSOR_DEVICE_ATTR_RO(temp1_input, pem_data, PEM_DATA_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static SENSOR_DEVICE_ATTR_RO(temp1_max, pem_data, PEM_DATA_TEMP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static SENSOR_DEVICE_ATTR_RO(temp1_crit, pem_data, PEM_DATA_TEMP_CRIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			       ALRM1_TEMP_WARNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			       ALRM1_TEMP_SHUTDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, pem_bool, PEM_DATA_ALARM_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			       ALRM2_TEMP_FAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static struct attribute *pem_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	&sensor_dev_attr_in1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	&sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	&sensor_dev_attr_curr1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	&sensor_dev_attr_power1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static const struct attribute_group pem_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	.attrs = pem_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static struct attribute *pem_input_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	&sensor_dev_attr_in2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	&sensor_dev_attr_curr1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	&sensor_dev_attr_power1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static const struct attribute_group pem_input_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	.attrs = pem_input_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct attribute *pem_fan_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	&sensor_dev_attr_fan1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	&sensor_dev_attr_fan2_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	&sensor_dev_attr_fan3_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static const struct attribute_group pem_fan_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.attrs = pem_fan_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static int pem_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct i2c_adapter *adapter = client->adapter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct pem_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	int ret, idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 				     | I2C_FUNC_SMBUS_WRITE_BYTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	 * We use the next two commands to determine if the device is really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	 * there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			     data->firmware_rev, sizeof(data->firmware_rev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	dev_info(dev, "Firmware revision %d.%d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		 data->firmware_rev[0], data->firmware_rev[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		 data->firmware_rev[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	/* sysfs hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	data->groups[idx++] = &pem_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	 * Check if input readings are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * This is the case if we can read input data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 * and if the returned data is not all zeros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	 * Note that input alarms are always supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	ret = pem_read_block(client, PEM_READ_INPUT_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			     data->input_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			     sizeof(data->input_string) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (!ret && (data->input_string[0] || data->input_string[1] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		     data->input_string[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		data->input_length = sizeof(data->input_string) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		/* Input string is one byte longer for some devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		ret = pem_read_block(client, PEM_READ_INPUT_STRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 				    data->input_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				    sizeof(data->input_string));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (!ret && (data->input_string[0] || data->input_string[1] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			    data->input_string[2] || data->input_string[3]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			data->input_length = sizeof(data->input_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (data->input_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		data->groups[idx++] = &pem_input_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	 * Check if fan speed readings are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	 * This is the case if we can read fan speed data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	 * and if the returned data is not all zeros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	 * Note that the fan alarm is always supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	ret = pem_read_block(client, PEM_READ_FAN_SPEED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			     data->fan_speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			     sizeof(data->fan_speed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		     data->fan_speed[2] || data->fan_speed[3])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		data->fans_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		data->groups[idx++] = &pem_fan_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 							   data, data->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static const struct i2c_device_id pem_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	{"lineage_pem", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) MODULE_DEVICE_TABLE(i2c, pem_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static struct i2c_driver pem_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		   .name = "lineage_pem",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		   },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	.probe_new = pem_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	.id_table = pem_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) module_i2c_driver(pem_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) MODULE_LICENSE("GPL");