xref: /linux/drivers/hwmon/lineage-pem.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2502b5a01SGuenter Roeck /*
3502b5a01SGuenter Roeck  * Driver for Lineage Compact Power Line series of power entry modules.
4502b5a01SGuenter Roeck  *
5502b5a01SGuenter Roeck  * Copyright (C) 2010, 2011 Ericsson AB.
6502b5a01SGuenter Roeck  *
7502b5a01SGuenter Roeck  * Documentation:
8502b5a01SGuenter Roeck  *  http://www.lineagepower.com/oem/pdf/CPLI2C.pdf
9502b5a01SGuenter Roeck  */
10502b5a01SGuenter Roeck 
11502b5a01SGuenter Roeck #include <linux/kernel.h>
12502b5a01SGuenter Roeck #include <linux/module.h>
13502b5a01SGuenter Roeck #include <linux/init.h>
14502b5a01SGuenter Roeck #include <linux/err.h>
15502b5a01SGuenter Roeck #include <linux/slab.h>
16502b5a01SGuenter Roeck #include <linux/i2c.h>
17502b5a01SGuenter Roeck #include <linux/hwmon.h>
18502b5a01SGuenter Roeck #include <linux/hwmon-sysfs.h>
19dcd8f392SJean Delvare #include <linux/jiffies.h>
20502b5a01SGuenter Roeck 
21502b5a01SGuenter Roeck /*
22502b5a01SGuenter Roeck  * This driver supports various Lineage Compact Power Line DC/DC and AC/DC
23502b5a01SGuenter Roeck  * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others.
24502b5a01SGuenter Roeck  *
25502b5a01SGuenter Roeck  * The devices are nominally PMBus compliant. However, most standard PMBus
26502b5a01SGuenter Roeck  * commands are not supported. Specifically, all hardware monitoring and
27502b5a01SGuenter Roeck  * status reporting commands are non-standard. For this reason, a standard
28502b5a01SGuenter Roeck  * PMBus driver can not be used.
29502b5a01SGuenter Roeck  *
30502b5a01SGuenter Roeck  * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541).
31502b5a01SGuenter Roeck  * To ensure device access, this driver should only be used as client driver
32502b5a01SGuenter Roeck  * to the pca9541 I2C master selector driver.
33502b5a01SGuenter Roeck  */
34502b5a01SGuenter Roeck 
35502b5a01SGuenter Roeck /* Command codes */
36502b5a01SGuenter Roeck #define PEM_OPERATION		0x01
37502b5a01SGuenter Roeck #define PEM_CLEAR_INFO_FLAGS	0x03
38502b5a01SGuenter Roeck #define PEM_VOUT_COMMAND	0x21
39502b5a01SGuenter Roeck #define PEM_VOUT_OV_FAULT_LIMIT	0x40
40502b5a01SGuenter Roeck #define PEM_READ_DATA_STRING	0xd0
41502b5a01SGuenter Roeck #define PEM_READ_INPUT_STRING	0xdc
42502b5a01SGuenter Roeck #define PEM_READ_FIRMWARE_REV	0xdd
43502b5a01SGuenter Roeck #define PEM_READ_RUN_TIMER	0xde
44502b5a01SGuenter Roeck #define PEM_FAN_HI_SPEED	0xdf
45502b5a01SGuenter Roeck #define PEM_FAN_NORMAL_SPEED	0xe0
46502b5a01SGuenter Roeck #define PEM_READ_FAN_SPEED	0xe1
47502b5a01SGuenter Roeck 
48502b5a01SGuenter Roeck /* offsets in data string */
49502b5a01SGuenter Roeck #define PEM_DATA_STATUS_2	0
50502b5a01SGuenter Roeck #define PEM_DATA_STATUS_1	1
51502b5a01SGuenter Roeck #define PEM_DATA_ALARM_2	2
52502b5a01SGuenter Roeck #define PEM_DATA_ALARM_1	3
53502b5a01SGuenter Roeck #define PEM_DATA_VOUT_LSB	4
54502b5a01SGuenter Roeck #define PEM_DATA_VOUT_MSB	5
55502b5a01SGuenter Roeck #define PEM_DATA_CURRENT	6
56502b5a01SGuenter Roeck #define PEM_DATA_TEMP		7
57502b5a01SGuenter Roeck 
58502b5a01SGuenter Roeck /* Virtual entries, to report constants */
59502b5a01SGuenter Roeck #define PEM_DATA_TEMP_MAX	10
60502b5a01SGuenter Roeck #define PEM_DATA_TEMP_CRIT	11
61502b5a01SGuenter Roeck 
62502b5a01SGuenter Roeck /* offsets in input string */
63502b5a01SGuenter Roeck #define PEM_INPUT_VOLTAGE	0
64502b5a01SGuenter Roeck #define PEM_INPUT_POWER_LSB	1
65502b5a01SGuenter Roeck #define PEM_INPUT_POWER_MSB	2
66502b5a01SGuenter Roeck 
67502b5a01SGuenter Roeck /* offsets in fan data */
68502b5a01SGuenter Roeck #define PEM_FAN_ADJUSTMENT	0
69502b5a01SGuenter Roeck #define PEM_FAN_FAN1		1
70502b5a01SGuenter Roeck #define PEM_FAN_FAN2		2
71502b5a01SGuenter Roeck #define PEM_FAN_FAN3		3
72502b5a01SGuenter Roeck 
73502b5a01SGuenter Roeck /* Status register bits */
74502b5a01SGuenter Roeck #define STS1_OUTPUT_ON		(1 << 0)
75502b5a01SGuenter Roeck #define STS1_LEDS_FLASHING	(1 << 1)
76502b5a01SGuenter Roeck #define STS1_EXT_FAULT		(1 << 2)
77502b5a01SGuenter Roeck #define STS1_SERVICE_LED_ON	(1 << 3)
78502b5a01SGuenter Roeck #define STS1_SHUTDOWN_OCCURRED	(1 << 4)
79502b5a01SGuenter Roeck #define STS1_INT_FAULT		(1 << 5)
80502b5a01SGuenter Roeck #define STS1_ISOLATION_TEST_OK	(1 << 6)
81502b5a01SGuenter Roeck 
82502b5a01SGuenter Roeck #define STS2_ENABLE_PIN_HI	(1 << 0)
83502b5a01SGuenter Roeck #define STS2_DATA_OUT_RANGE	(1 << 1)
84502b5a01SGuenter Roeck #define STS2_RESTARTED_OK	(1 << 1)
85502b5a01SGuenter Roeck #define STS2_ISOLATION_TEST_FAIL (1 << 3)
86502b5a01SGuenter Roeck #define STS2_HIGH_POWER_CAP	(1 << 4)
87502b5a01SGuenter Roeck #define STS2_INVALID_INSTR	(1 << 5)
88502b5a01SGuenter Roeck #define STS2_WILL_RESTART	(1 << 6)
89502b5a01SGuenter Roeck #define STS2_PEC_ERR		(1 << 7)
90502b5a01SGuenter Roeck 
91502b5a01SGuenter Roeck /* Alarm register bits */
92502b5a01SGuenter Roeck #define ALRM1_VIN_OUT_LIMIT	(1 << 0)
93502b5a01SGuenter Roeck #define ALRM1_VOUT_OUT_LIMIT	(1 << 1)
94502b5a01SGuenter Roeck #define ALRM1_OV_VOLT_SHUTDOWN	(1 << 2)
95502b5a01SGuenter Roeck #define ALRM1_VIN_OVERCURRENT	(1 << 3)
96502b5a01SGuenter Roeck #define ALRM1_TEMP_WARNING	(1 << 4)
97502b5a01SGuenter Roeck #define ALRM1_TEMP_SHUTDOWN	(1 << 5)
98502b5a01SGuenter Roeck #define ALRM1_PRIMARY_FAULT	(1 << 6)
99502b5a01SGuenter Roeck #define ALRM1_POWER_LIMIT	(1 << 7)
100502b5a01SGuenter Roeck 
101502b5a01SGuenter Roeck #define ALRM2_5V_OUT_LIMIT	(1 << 1)
102502b5a01SGuenter Roeck #define ALRM2_TEMP_FAULT	(1 << 2)
103502b5a01SGuenter Roeck #define ALRM2_OV_LOW		(1 << 3)
104502b5a01SGuenter Roeck #define ALRM2_DCDC_TEMP_HIGH	(1 << 4)
105502b5a01SGuenter Roeck #define ALRM2_PRI_TEMP_HIGH	(1 << 5)
106502b5a01SGuenter Roeck #define ALRM2_NO_PRIMARY	(1 << 6)
107502b5a01SGuenter Roeck #define ALRM2_FAN_FAULT		(1 << 7)
108502b5a01SGuenter Roeck 
109502b5a01SGuenter Roeck #define FIRMWARE_REV_LEN	4
110502b5a01SGuenter Roeck #define DATA_STRING_LEN		9
111502b5a01SGuenter Roeck #define INPUT_STRING_LEN	5	/* 4 for most devices	*/
112502b5a01SGuenter Roeck #define FAN_SPEED_LEN		5
113502b5a01SGuenter Roeck 
114502b5a01SGuenter Roeck struct pem_data {
11582803252SAxel Lin 	struct i2c_client *client;
11682803252SAxel Lin 	const struct attribute_group *groups[4];
117502b5a01SGuenter Roeck 
118502b5a01SGuenter Roeck 	struct mutex update_lock;
119502b5a01SGuenter Roeck 	bool valid;
120502b5a01SGuenter Roeck 	bool fans_supported;
121502b5a01SGuenter Roeck 	int input_length;
122502b5a01SGuenter Roeck 	unsigned long last_updated;	/* in jiffies */
123502b5a01SGuenter Roeck 
124502b5a01SGuenter Roeck 	u8 firmware_rev[FIRMWARE_REV_LEN];
125502b5a01SGuenter Roeck 	u8 data_string[DATA_STRING_LEN];
126502b5a01SGuenter Roeck 	u8 input_string[INPUT_STRING_LEN];
127502b5a01SGuenter Roeck 	u8 fan_speed[FAN_SPEED_LEN];
128502b5a01SGuenter Roeck };
129502b5a01SGuenter Roeck 
pem_read_block(struct i2c_client * client,u8 command,u8 * data,int data_len)130502b5a01SGuenter Roeck static int pem_read_block(struct i2c_client *client, u8 command, u8 *data,
131502b5a01SGuenter Roeck 			  int data_len)
132502b5a01SGuenter Roeck {
133502b5a01SGuenter Roeck 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX];
134502b5a01SGuenter Roeck 	int result;
135502b5a01SGuenter Roeck 
136502b5a01SGuenter Roeck 	result = i2c_smbus_read_block_data(client, command, block_buffer);
137502b5a01SGuenter Roeck 	if (unlikely(result < 0))
138502b5a01SGuenter Roeck 		goto abort;
139502b5a01SGuenter Roeck 	if (unlikely(result == 0xff || result != data_len)) {
140502b5a01SGuenter Roeck 		result = -EIO;
141502b5a01SGuenter Roeck 		goto abort;
142502b5a01SGuenter Roeck 	}
143502b5a01SGuenter Roeck 	memcpy(data, block_buffer, data_len);
144502b5a01SGuenter Roeck 	result = 0;
145502b5a01SGuenter Roeck abort:
146502b5a01SGuenter Roeck 	return result;
147502b5a01SGuenter Roeck }
148502b5a01SGuenter Roeck 
pem_update_device(struct device * dev)149502b5a01SGuenter Roeck static struct pem_data *pem_update_device(struct device *dev)
150502b5a01SGuenter Roeck {
15182803252SAxel Lin 	struct pem_data *data = dev_get_drvdata(dev);
15282803252SAxel Lin 	struct i2c_client *client = data->client;
153502b5a01SGuenter Roeck 	struct pem_data *ret = data;
154502b5a01SGuenter Roeck 
155502b5a01SGuenter Roeck 	mutex_lock(&data->update_lock);
156502b5a01SGuenter Roeck 
157502b5a01SGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
158502b5a01SGuenter Roeck 		int result;
159502b5a01SGuenter Roeck 
160502b5a01SGuenter Roeck 		/* Read data string */
161502b5a01SGuenter Roeck 		result = pem_read_block(client, PEM_READ_DATA_STRING,
162502b5a01SGuenter Roeck 					data->data_string,
163502b5a01SGuenter Roeck 					sizeof(data->data_string));
164502b5a01SGuenter Roeck 		if (unlikely(result < 0)) {
165502b5a01SGuenter Roeck 			ret = ERR_PTR(result);
166502b5a01SGuenter Roeck 			goto abort;
167502b5a01SGuenter Roeck 		}
168502b5a01SGuenter Roeck 
169502b5a01SGuenter Roeck 		/* Read input string */
170502b5a01SGuenter Roeck 		if (data->input_length) {
171502b5a01SGuenter Roeck 			result = pem_read_block(client, PEM_READ_INPUT_STRING,
172502b5a01SGuenter Roeck 						data->input_string,
173502b5a01SGuenter Roeck 						data->input_length);
174502b5a01SGuenter Roeck 			if (unlikely(result < 0)) {
175502b5a01SGuenter Roeck 				ret = ERR_PTR(result);
176502b5a01SGuenter Roeck 				goto abort;
177502b5a01SGuenter Roeck 			}
178502b5a01SGuenter Roeck 		}
179502b5a01SGuenter Roeck 
180502b5a01SGuenter Roeck 		/* Read fan speeds */
181502b5a01SGuenter Roeck 		if (data->fans_supported) {
182502b5a01SGuenter Roeck 			result = pem_read_block(client, PEM_READ_FAN_SPEED,
183502b5a01SGuenter Roeck 						data->fan_speed,
184502b5a01SGuenter Roeck 						sizeof(data->fan_speed));
185502b5a01SGuenter Roeck 			if (unlikely(result < 0)) {
186502b5a01SGuenter Roeck 				ret = ERR_PTR(result);
187502b5a01SGuenter Roeck 				goto abort;
188502b5a01SGuenter Roeck 			}
189502b5a01SGuenter Roeck 		}
190502b5a01SGuenter Roeck 
191502b5a01SGuenter Roeck 		i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
192502b5a01SGuenter Roeck 
193502b5a01SGuenter Roeck 		data->last_updated = jiffies;
194952a11caSPaul Fertser 		data->valid = true;
195502b5a01SGuenter Roeck 	}
196502b5a01SGuenter Roeck abort:
197502b5a01SGuenter Roeck 	mutex_unlock(&data->update_lock);
198502b5a01SGuenter Roeck 	return ret;
199502b5a01SGuenter Roeck }
200502b5a01SGuenter Roeck 
pem_get_data(u8 * data,int len,int index)201502b5a01SGuenter Roeck static long pem_get_data(u8 *data, int len, int index)
202502b5a01SGuenter Roeck {
203502b5a01SGuenter Roeck 	long val;
204502b5a01SGuenter Roeck 
205502b5a01SGuenter Roeck 	switch (index) {
206502b5a01SGuenter Roeck 	case PEM_DATA_VOUT_LSB:
207502b5a01SGuenter Roeck 		val = (data[index] + (data[index+1] << 8)) * 5 / 2;
208502b5a01SGuenter Roeck 		break;
209502b5a01SGuenter Roeck 	case PEM_DATA_CURRENT:
210502b5a01SGuenter Roeck 		val = data[index] * 200;
211502b5a01SGuenter Roeck 		break;
212502b5a01SGuenter Roeck 	case PEM_DATA_TEMP:
213502b5a01SGuenter Roeck 		val = data[index] * 1000;
214502b5a01SGuenter Roeck 		break;
215502b5a01SGuenter Roeck 	case PEM_DATA_TEMP_MAX:
216502b5a01SGuenter Roeck 		val = 97 * 1000;	/* 97 degrees C per datasheet */
217502b5a01SGuenter Roeck 		break;
218502b5a01SGuenter Roeck 	case PEM_DATA_TEMP_CRIT:
219502b5a01SGuenter Roeck 		val = 107 * 1000;	/* 107 degrees C per datasheet */
220502b5a01SGuenter Roeck 		break;
221502b5a01SGuenter Roeck 	default:
222502b5a01SGuenter Roeck 		WARN_ON_ONCE(1);
223502b5a01SGuenter Roeck 		val = 0;
224502b5a01SGuenter Roeck 	}
225502b5a01SGuenter Roeck 	return val;
226502b5a01SGuenter Roeck }
227502b5a01SGuenter Roeck 
pem_get_input(u8 * data,int len,int index)228502b5a01SGuenter Roeck static long pem_get_input(u8 *data, int len, int index)
229502b5a01SGuenter Roeck {
230502b5a01SGuenter Roeck 	long val;
231502b5a01SGuenter Roeck 
232502b5a01SGuenter Roeck 	switch (index) {
233502b5a01SGuenter Roeck 	case PEM_INPUT_VOLTAGE:
234502b5a01SGuenter Roeck 		if (len == INPUT_STRING_LEN)
235502b5a01SGuenter Roeck 			val = (data[index] + (data[index+1] << 8) - 75) * 1000;
236502b5a01SGuenter Roeck 		else
237502b5a01SGuenter Roeck 			val = (data[index] - 75) * 1000;
238502b5a01SGuenter Roeck 		break;
239502b5a01SGuenter Roeck 	case PEM_INPUT_POWER_LSB:
240502b5a01SGuenter Roeck 		if (len == INPUT_STRING_LEN)
241502b5a01SGuenter Roeck 			index++;
242502b5a01SGuenter Roeck 		val = (data[index] + (data[index+1] << 8)) * 1000000L;
243502b5a01SGuenter Roeck 		break;
244502b5a01SGuenter Roeck 	default:
245502b5a01SGuenter Roeck 		WARN_ON_ONCE(1);
246502b5a01SGuenter Roeck 		val = 0;
247502b5a01SGuenter Roeck 	}
248502b5a01SGuenter Roeck 	return val;
249502b5a01SGuenter Roeck }
250502b5a01SGuenter Roeck 
pem_get_fan(u8 * data,int len,int index)251502b5a01SGuenter Roeck static long pem_get_fan(u8 *data, int len, int index)
252502b5a01SGuenter Roeck {
253502b5a01SGuenter Roeck 	long val;
254502b5a01SGuenter Roeck 
255502b5a01SGuenter Roeck 	switch (index) {
256502b5a01SGuenter Roeck 	case PEM_FAN_FAN1:
257502b5a01SGuenter Roeck 	case PEM_FAN_FAN2:
258502b5a01SGuenter Roeck 	case PEM_FAN_FAN3:
259502b5a01SGuenter Roeck 		val = data[index] * 100;
260502b5a01SGuenter Roeck 		break;
261502b5a01SGuenter Roeck 	default:
262502b5a01SGuenter Roeck 		WARN_ON_ONCE(1);
263502b5a01SGuenter Roeck 		val = 0;
264502b5a01SGuenter Roeck 	}
265502b5a01SGuenter Roeck 	return val;
266502b5a01SGuenter Roeck }
267502b5a01SGuenter Roeck 
268502b5a01SGuenter Roeck /*
269502b5a01SGuenter Roeck  * Show boolean, either a fault or an alarm.
270502b5a01SGuenter Roeck  * .nr points to the register, .index is the bit mask to check
271502b5a01SGuenter Roeck  */
pem_bool_show(struct device * dev,struct device_attribute * da,char * buf)2726ccf6a83SGuenter Roeck static ssize_t pem_bool_show(struct device *dev, struct device_attribute *da,
2736ccf6a83SGuenter Roeck 			     char *buf)
274502b5a01SGuenter Roeck {
275502b5a01SGuenter Roeck 	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
276502b5a01SGuenter Roeck 	struct pem_data *data = pem_update_device(dev);
277502b5a01SGuenter Roeck 	u8 status;
278502b5a01SGuenter Roeck 
279502b5a01SGuenter Roeck 	if (IS_ERR(data))
280502b5a01SGuenter Roeck 		return PTR_ERR(data);
281502b5a01SGuenter Roeck 
282502b5a01SGuenter Roeck 	status = data->data_string[attr->nr] & attr->index;
2831f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%d\n", !!status);
284502b5a01SGuenter Roeck }
285502b5a01SGuenter Roeck 
pem_data_show(struct device * dev,struct device_attribute * da,char * buf)2866ccf6a83SGuenter Roeck static ssize_t pem_data_show(struct device *dev, struct device_attribute *da,
287502b5a01SGuenter Roeck 			     char *buf)
288502b5a01SGuenter Roeck {
289502b5a01SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
290502b5a01SGuenter Roeck 	struct pem_data *data = pem_update_device(dev);
291502b5a01SGuenter Roeck 	long value;
292502b5a01SGuenter Roeck 
293502b5a01SGuenter Roeck 	if (IS_ERR(data))
294502b5a01SGuenter Roeck 		return PTR_ERR(data);
295502b5a01SGuenter Roeck 
296502b5a01SGuenter Roeck 	value = pem_get_data(data->data_string, sizeof(data->data_string),
297502b5a01SGuenter Roeck 			     attr->index);
298502b5a01SGuenter Roeck 
2991f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%ld\n", value);
300502b5a01SGuenter Roeck }
301502b5a01SGuenter Roeck 
pem_input_show(struct device * dev,struct device_attribute * da,char * buf)3026ccf6a83SGuenter Roeck static ssize_t pem_input_show(struct device *dev, struct device_attribute *da,
303502b5a01SGuenter Roeck 			      char *buf)
304502b5a01SGuenter Roeck {
305502b5a01SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
306502b5a01SGuenter Roeck 	struct pem_data *data = pem_update_device(dev);
307502b5a01SGuenter Roeck 	long value;
308502b5a01SGuenter Roeck 
309502b5a01SGuenter Roeck 	if (IS_ERR(data))
310502b5a01SGuenter Roeck 		return PTR_ERR(data);
311502b5a01SGuenter Roeck 
312502b5a01SGuenter Roeck 	value = pem_get_input(data->input_string, sizeof(data->input_string),
313502b5a01SGuenter Roeck 			      attr->index);
314502b5a01SGuenter Roeck 
3151f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%ld\n", value);
316502b5a01SGuenter Roeck }
317502b5a01SGuenter Roeck 
pem_fan_show(struct device * dev,struct device_attribute * da,char * buf)3186ccf6a83SGuenter Roeck static ssize_t pem_fan_show(struct device *dev, struct device_attribute *da,
319502b5a01SGuenter Roeck 			    char *buf)
320502b5a01SGuenter Roeck {
321502b5a01SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
322502b5a01SGuenter Roeck 	struct pem_data *data = pem_update_device(dev);
323502b5a01SGuenter Roeck 	long value;
324502b5a01SGuenter Roeck 
325502b5a01SGuenter Roeck 	if (IS_ERR(data))
326502b5a01SGuenter Roeck 		return PTR_ERR(data);
327502b5a01SGuenter Roeck 
328502b5a01SGuenter Roeck 	value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed),
329502b5a01SGuenter Roeck 			    attr->index);
330502b5a01SGuenter Roeck 
3311f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%ld\n", value);
332502b5a01SGuenter Roeck }
333502b5a01SGuenter Roeck 
334502b5a01SGuenter Roeck /* Voltages */
3356ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, pem_data, PEM_DATA_VOUT_LSB);
3366ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, pem_bool, PEM_DATA_ALARM_1,
3376ccf6a83SGuenter Roeck 			       ALRM1_VOUT_OUT_LIMIT);
3386ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
3396ccf6a83SGuenter Roeck 			       ALRM1_OV_VOLT_SHUTDOWN);
3406ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, pem_input, PEM_INPUT_VOLTAGE);
3416ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, pem_bool, PEM_DATA_ALARM_1,
342502b5a01SGuenter Roeck 			       ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT);
343502b5a01SGuenter Roeck 
344502b5a01SGuenter Roeck /* Currents */
3456ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, pem_data, PEM_DATA_CURRENT);
3466ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(curr1_alarm, pem_bool, PEM_DATA_ALARM_1,
3476ccf6a83SGuenter Roeck 			       ALRM1_VIN_OVERCURRENT);
348502b5a01SGuenter Roeck 
349502b5a01SGuenter Roeck /* Power */
3506ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(power1_input, pem_input, PEM_INPUT_POWER_LSB);
3516ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(power1_alarm, pem_bool, PEM_DATA_ALARM_1,
3526ccf6a83SGuenter Roeck 			       ALRM1_POWER_LIMIT);
353502b5a01SGuenter Roeck 
354502b5a01SGuenter Roeck /* Fans */
3556ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, pem_fan, PEM_FAN_FAN1);
3566ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, pem_fan, PEM_FAN_FAN2);
3576ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, pem_fan, PEM_FAN_FAN3);
3586ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, pem_bool, PEM_DATA_ALARM_2,
3596ccf6a83SGuenter Roeck 			       ALRM2_FAN_FAULT);
360502b5a01SGuenter Roeck 
361502b5a01SGuenter Roeck /* Temperatures */
3626ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, pem_data, PEM_DATA_TEMP);
3636ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max, pem_data, PEM_DATA_TEMP_MAX);
3646ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_crit, pem_data, PEM_DATA_TEMP_CRIT);
3656ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, pem_bool, PEM_DATA_ALARM_1,
3666ccf6a83SGuenter Roeck 			       ALRM1_TEMP_WARNING);
3676ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, pem_bool, PEM_DATA_ALARM_1,
3686ccf6a83SGuenter Roeck 			       ALRM1_TEMP_SHUTDOWN);
3696ccf6a83SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, pem_bool, PEM_DATA_ALARM_2,
3706ccf6a83SGuenter Roeck 			       ALRM2_TEMP_FAULT);
371502b5a01SGuenter Roeck 
372502b5a01SGuenter Roeck static struct attribute *pem_attributes[] = {
373502b5a01SGuenter Roeck 	&sensor_dev_attr_in1_input.dev_attr.attr,
374d668a8b0SGuenter Roeck 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
375502b5a01SGuenter Roeck 	&sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
376502b5a01SGuenter Roeck 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
377502b5a01SGuenter Roeck 
378502b5a01SGuenter Roeck 	&sensor_dev_attr_curr1_alarm.dev_attr.attr,
379502b5a01SGuenter Roeck 
380502b5a01SGuenter Roeck 	&sensor_dev_attr_power1_alarm.dev_attr.attr,
381502b5a01SGuenter Roeck 
382502b5a01SGuenter Roeck 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
383502b5a01SGuenter Roeck 
384502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_input.dev_attr.attr,
385502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_max.dev_attr.attr,
386502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
387502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
388502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
389502b5a01SGuenter Roeck 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
390502b5a01SGuenter Roeck 
391502b5a01SGuenter Roeck 	NULL,
392502b5a01SGuenter Roeck };
393502b5a01SGuenter Roeck 
394502b5a01SGuenter Roeck static const struct attribute_group pem_group = {
395502b5a01SGuenter Roeck 	.attrs = pem_attributes,
396502b5a01SGuenter Roeck };
397502b5a01SGuenter Roeck 
398502b5a01SGuenter Roeck static struct attribute *pem_input_attributes[] = {
399502b5a01SGuenter Roeck 	&sensor_dev_attr_in2_input.dev_attr.attr,
400502b5a01SGuenter Roeck 	&sensor_dev_attr_curr1_input.dev_attr.attr,
401502b5a01SGuenter Roeck 	&sensor_dev_attr_power1_input.dev_attr.attr,
402df069079SAxel Lin 	NULL
403502b5a01SGuenter Roeck };
404502b5a01SGuenter Roeck 
405502b5a01SGuenter Roeck static const struct attribute_group pem_input_group = {
406502b5a01SGuenter Roeck 	.attrs = pem_input_attributes,
407502b5a01SGuenter Roeck };
408502b5a01SGuenter Roeck 
409502b5a01SGuenter Roeck static struct attribute *pem_fan_attributes[] = {
410502b5a01SGuenter Roeck 	&sensor_dev_attr_fan1_input.dev_attr.attr,
411502b5a01SGuenter Roeck 	&sensor_dev_attr_fan2_input.dev_attr.attr,
412502b5a01SGuenter Roeck 	&sensor_dev_attr_fan3_input.dev_attr.attr,
413df069079SAxel Lin 	NULL
414502b5a01SGuenter Roeck };
415502b5a01SGuenter Roeck 
416502b5a01SGuenter Roeck static const struct attribute_group pem_fan_group = {
417502b5a01SGuenter Roeck 	.attrs = pem_fan_attributes,
418502b5a01SGuenter Roeck };
419502b5a01SGuenter Roeck 
pem_probe(struct i2c_client * client)42067487038SStephen Kitt static int pem_probe(struct i2c_client *client)
421502b5a01SGuenter Roeck {
422502b5a01SGuenter Roeck 	struct i2c_adapter *adapter = client->adapter;
42382803252SAxel Lin 	struct device *dev = &client->dev;
42482803252SAxel Lin 	struct device *hwmon_dev;
425502b5a01SGuenter Roeck 	struct pem_data *data;
42682803252SAxel Lin 	int ret, idx = 0;
427502b5a01SGuenter Roeck 
428502b5a01SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA
429502b5a01SGuenter Roeck 				     | I2C_FUNC_SMBUS_WRITE_BYTE))
430502b5a01SGuenter Roeck 		return -ENODEV;
431502b5a01SGuenter Roeck 
43282803252SAxel Lin 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
433502b5a01SGuenter Roeck 	if (!data)
434502b5a01SGuenter Roeck 		return -ENOMEM;
435502b5a01SGuenter Roeck 
43682803252SAxel Lin 	data->client = client;
437502b5a01SGuenter Roeck 	mutex_init(&data->update_lock);
438502b5a01SGuenter Roeck 
439502b5a01SGuenter Roeck 	/*
440502b5a01SGuenter Roeck 	 * We use the next two commands to determine if the device is really
441502b5a01SGuenter Roeck 	 * there.
442502b5a01SGuenter Roeck 	 */
443502b5a01SGuenter Roeck 	ret = pem_read_block(client, PEM_READ_FIRMWARE_REV,
444502b5a01SGuenter Roeck 			     data->firmware_rev, sizeof(data->firmware_rev));
445502b5a01SGuenter Roeck 	if (ret < 0)
44607404aabSGuenter Roeck 		return ret;
447502b5a01SGuenter Roeck 
448502b5a01SGuenter Roeck 	ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS);
449502b5a01SGuenter Roeck 	if (ret < 0)
45007404aabSGuenter Roeck 		return ret;
451502b5a01SGuenter Roeck 
45282803252SAxel Lin 	dev_info(dev, "Firmware revision %d.%d.%d\n",
453502b5a01SGuenter Roeck 		 data->firmware_rev[0], data->firmware_rev[1],
454502b5a01SGuenter Roeck 		 data->firmware_rev[2]);
455502b5a01SGuenter Roeck 
45682803252SAxel Lin 	/* sysfs hooks */
45782803252SAxel Lin 	data->groups[idx++] = &pem_group;
458502b5a01SGuenter Roeck 
459502b5a01SGuenter Roeck 	/*
460502b5a01SGuenter Roeck 	 * Check if input readings are supported.
461502b5a01SGuenter Roeck 	 * This is the case if we can read input data,
462502b5a01SGuenter Roeck 	 * and if the returned data is not all zeros.
463502b5a01SGuenter Roeck 	 * Note that input alarms are always supported.
464502b5a01SGuenter Roeck 	 */
465502b5a01SGuenter Roeck 	ret = pem_read_block(client, PEM_READ_INPUT_STRING,
466502b5a01SGuenter Roeck 			     data->input_string,
467502b5a01SGuenter Roeck 			     sizeof(data->input_string) - 1);
468502b5a01SGuenter Roeck 	if (!ret && (data->input_string[0] || data->input_string[1] ||
469502b5a01SGuenter Roeck 		     data->input_string[2]))
470502b5a01SGuenter Roeck 		data->input_length = sizeof(data->input_string) - 1;
471502b5a01SGuenter Roeck 	else if (ret < 0) {
472502b5a01SGuenter Roeck 		/* Input string is one byte longer for some devices */
473502b5a01SGuenter Roeck 		ret = pem_read_block(client, PEM_READ_INPUT_STRING,
474502b5a01SGuenter Roeck 				    data->input_string,
475502b5a01SGuenter Roeck 				    sizeof(data->input_string));
476502b5a01SGuenter Roeck 		if (!ret && (data->input_string[0] || data->input_string[1] ||
477502b5a01SGuenter Roeck 			    data->input_string[2] || data->input_string[3]))
478502b5a01SGuenter Roeck 			data->input_length = sizeof(data->input_string);
479502b5a01SGuenter Roeck 	}
48082803252SAxel Lin 
48182803252SAxel Lin 	if (data->input_length)
48282803252SAxel Lin 		data->groups[idx++] = &pem_input_group;
483502b5a01SGuenter Roeck 
484502b5a01SGuenter Roeck 	/*
485502b5a01SGuenter Roeck 	 * Check if fan speed readings are supported.
486502b5a01SGuenter Roeck 	 * This is the case if we can read fan speed data,
487502b5a01SGuenter Roeck 	 * and if the returned data is not all zeros.
488502b5a01SGuenter Roeck 	 * Note that the fan alarm is always supported.
489502b5a01SGuenter Roeck 	 */
490502b5a01SGuenter Roeck 	ret = pem_read_block(client, PEM_READ_FAN_SPEED,
491502b5a01SGuenter Roeck 			     data->fan_speed,
492502b5a01SGuenter Roeck 			     sizeof(data->fan_speed));
493502b5a01SGuenter Roeck 	if (!ret && (data->fan_speed[0] || data->fan_speed[1] ||
494502b5a01SGuenter Roeck 		     data->fan_speed[2] || data->fan_speed[3])) {
495502b5a01SGuenter Roeck 		data->fans_supported = true;
49682803252SAxel Lin 		data->groups[idx++] = &pem_fan_group;
497502b5a01SGuenter Roeck 	}
498502b5a01SGuenter Roeck 
49982803252SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
50082803252SAxel Lin 							   data, data->groups);
50182803252SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
502502b5a01SGuenter Roeck }
503502b5a01SGuenter Roeck 
504502b5a01SGuenter Roeck static const struct i2c_device_id pem_id[] = {
505*d8a66f36SUwe Kleine-König 	{"lineage_pem"},
506502b5a01SGuenter Roeck 	{}
507502b5a01SGuenter Roeck };
508502b5a01SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pem_id);
509502b5a01SGuenter Roeck 
510502b5a01SGuenter Roeck static struct i2c_driver pem_driver = {
511502b5a01SGuenter Roeck 	.driver = {
512502b5a01SGuenter Roeck 		   .name = "lineage_pem",
513502b5a01SGuenter Roeck 		   },
5141975d167SUwe Kleine-König 	.probe = pem_probe,
515502b5a01SGuenter Roeck 	.id_table = pem_id,
516502b5a01SGuenter Roeck };
517502b5a01SGuenter Roeck 
518f0967eeaSAxel Lin module_i2c_driver(pem_driver);
519502b5a01SGuenter Roeck 
520bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
521502b5a01SGuenter Roeck MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver");
522502b5a01SGuenter Roeck MODULE_LICENSE("GPL");
523