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