xref: /openbmc/linux/drivers/hwmon/jc42.c (revision bca6a1ada0464b7179f34f9af62bcdfd60c223e7)
14453d736SGuenter Roeck /*
24453d736SGuenter Roeck  * jc42.c - driver for Jedec JC42.4 compliant temperature sensors
34453d736SGuenter Roeck  *
44453d736SGuenter Roeck  * Copyright (c) 2010  Ericsson AB.
54453d736SGuenter Roeck  *
64453d736SGuenter Roeck  * Derived from lm77.c by Andras BALI <drewie@freemail.hu>.
74453d736SGuenter Roeck  *
84453d736SGuenter Roeck  * JC42.4 compliant temperature sensors are typically used on memory modules.
94453d736SGuenter Roeck  *
104453d736SGuenter Roeck  * This program is free software; you can redistribute it and/or modify
114453d736SGuenter Roeck  * it under the terms of the GNU General Public License as published by
124453d736SGuenter Roeck  * the Free Software Foundation; either version 2 of the License, or
134453d736SGuenter Roeck  * (at your option) any later version.
144453d736SGuenter Roeck  *
154453d736SGuenter Roeck  * This program is distributed in the hope that it will be useful,
164453d736SGuenter Roeck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
174453d736SGuenter Roeck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
184453d736SGuenter Roeck  * GNU General Public License for more details.
194453d736SGuenter Roeck  *
204453d736SGuenter Roeck  * You should have received a copy of the GNU General Public License
214453d736SGuenter Roeck  * along with this program; if not, write to the Free Software
224453d736SGuenter Roeck  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
234453d736SGuenter Roeck  */
244453d736SGuenter Roeck 
254453d736SGuenter Roeck #include <linux/module.h>
264453d736SGuenter Roeck #include <linux/init.h>
274453d736SGuenter Roeck #include <linux/slab.h>
284453d736SGuenter Roeck #include <linux/jiffies.h>
294453d736SGuenter Roeck #include <linux/i2c.h>
304453d736SGuenter Roeck #include <linux/hwmon.h>
314453d736SGuenter Roeck #include <linux/hwmon-sysfs.h>
324453d736SGuenter Roeck #include <linux/err.h>
334453d736SGuenter Roeck #include <linux/mutex.h>
344453d736SGuenter Roeck 
354453d736SGuenter Roeck /* Addresses to scan */
364453d736SGuenter Roeck static const unsigned short normal_i2c[] = {
374453d736SGuenter Roeck 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, I2C_CLIENT_END };
384453d736SGuenter Roeck 
394453d736SGuenter Roeck /* JC42 registers. All registers are 16 bit. */
404453d736SGuenter Roeck #define JC42_REG_CAP		0x00
414453d736SGuenter Roeck #define JC42_REG_CONFIG		0x01
424453d736SGuenter Roeck #define JC42_REG_TEMP_UPPER	0x02
434453d736SGuenter Roeck #define JC42_REG_TEMP_LOWER	0x03
444453d736SGuenter Roeck #define JC42_REG_TEMP_CRITICAL	0x04
454453d736SGuenter Roeck #define JC42_REG_TEMP		0x05
464453d736SGuenter Roeck #define JC42_REG_MANID		0x06
474453d736SGuenter Roeck #define JC42_REG_DEVICEID	0x07
484453d736SGuenter Roeck 
494453d736SGuenter Roeck /* Status bits in temperature register */
504453d736SGuenter Roeck #define JC42_ALARM_CRIT_BIT	15
514453d736SGuenter Roeck #define JC42_ALARM_MAX_BIT	14
524453d736SGuenter Roeck #define JC42_ALARM_MIN_BIT	13
534453d736SGuenter Roeck 
544453d736SGuenter Roeck /* Configuration register defines */
554453d736SGuenter Roeck #define JC42_CFG_CRIT_ONLY	(1 << 2)
562c6315daSClemens Ladisch #define JC42_CFG_TCRIT_LOCK	(1 << 6)
572c6315daSClemens Ladisch #define JC42_CFG_EVENT_LOCK	(1 << 7)
584453d736SGuenter Roeck #define JC42_CFG_SHUTDOWN	(1 << 8)
594453d736SGuenter Roeck #define JC42_CFG_HYST_SHIFT	9
602ccc8731SJean Delvare #define JC42_CFG_HYST_MASK	(0x03 << 9)
614453d736SGuenter Roeck 
624453d736SGuenter Roeck /* Capabilities */
634453d736SGuenter Roeck #define JC42_CAP_RANGE		(1 << 2)
644453d736SGuenter Roeck 
654453d736SGuenter Roeck /* Manufacturer IDs */
664453d736SGuenter Roeck #define ADT_MANID		0x11d4  /* Analog Devices */
671bd612a2SGuenter Roeck #define ATMEL_MANID		0x001f  /* Atmel */
68175c490cSGuenter Roeck #define ATMEL_MANID2		0x1114	/* Atmel */
694453d736SGuenter Roeck #define MAX_MANID		0x004d  /* Maxim */
704453d736SGuenter Roeck #define IDT_MANID		0x00b3  /* IDT */
714453d736SGuenter Roeck #define MCP_MANID		0x0054  /* Microchip */
724453d736SGuenter Roeck #define NXP_MANID		0x1131  /* NXP Semiconductors */
734453d736SGuenter Roeck #define ONS_MANID		0x1b09  /* ON Semiconductor */
744453d736SGuenter Roeck #define STM_MANID		0x104a  /* ST Microelectronics */
754453d736SGuenter Roeck 
764453d736SGuenter Roeck /* Supported chips */
774453d736SGuenter Roeck 
784453d736SGuenter Roeck /* Analog Devices */
794453d736SGuenter Roeck #define ADT7408_DEVID		0x0801
804453d736SGuenter Roeck #define ADT7408_DEVID_MASK	0xffff
814453d736SGuenter Roeck 
821bd612a2SGuenter Roeck /* Atmel */
831bd612a2SGuenter Roeck #define AT30TS00_DEVID		0x8201
841bd612a2SGuenter Roeck #define AT30TS00_DEVID_MASK	0xffff
851bd612a2SGuenter Roeck 
86175c490cSGuenter Roeck #define AT30TSE004_DEVID	0x2200
87175c490cSGuenter Roeck #define AT30TSE004_DEVID_MASK	0xffff
88175c490cSGuenter Roeck 
894453d736SGuenter Roeck /* IDT */
904453d736SGuenter Roeck #define TS3000B3_DEVID		0x2903  /* Also matches TSE2002B3 */
914453d736SGuenter Roeck #define TS3000B3_DEVID_MASK	0xffff
924453d736SGuenter Roeck 
931bd612a2SGuenter Roeck #define TS3000GB2_DEVID		0x2912  /* Also matches TSE2002GB2 */
941bd612a2SGuenter Roeck #define TS3000GB2_DEVID_MASK	0xffff
951bd612a2SGuenter Roeck 
964453d736SGuenter Roeck /* Maxim */
974453d736SGuenter Roeck #define MAX6604_DEVID		0x3e00
984453d736SGuenter Roeck #define MAX6604_DEVID_MASK	0xffff
994453d736SGuenter Roeck 
1004453d736SGuenter Roeck /* Microchip */
1011bd612a2SGuenter Roeck #define MCP9804_DEVID		0x0200
1021bd612a2SGuenter Roeck #define MCP9804_DEVID_MASK	0xfffc
1031bd612a2SGuenter Roeck 
1044453d736SGuenter Roeck #define MCP98242_DEVID		0x2000
1054453d736SGuenter Roeck #define MCP98242_DEVID_MASK	0xfffc
1064453d736SGuenter Roeck 
1074453d736SGuenter Roeck #define MCP98243_DEVID		0x2100
1084453d736SGuenter Roeck #define MCP98243_DEVID_MASK	0xfffc
1094453d736SGuenter Roeck 
110d4768280SGuenter Roeck #define MCP98244_DEVID		0x2200
111d4768280SGuenter Roeck #define MCP98244_DEVID_MASK	0xfffc
112d4768280SGuenter Roeck 
1134453d736SGuenter Roeck #define MCP9843_DEVID		0x0000	/* Also matches mcp9805 */
1144453d736SGuenter Roeck #define MCP9843_DEVID_MASK	0xfffe
1154453d736SGuenter Roeck 
1164453d736SGuenter Roeck /* NXP */
1174453d736SGuenter Roeck #define SE97_DEVID		0xa200
1184453d736SGuenter Roeck #define SE97_DEVID_MASK		0xfffc
1194453d736SGuenter Roeck 
1204453d736SGuenter Roeck #define SE98_DEVID		0xa100
1214453d736SGuenter Roeck #define SE98_DEVID_MASK		0xfffc
1224453d736SGuenter Roeck 
1234453d736SGuenter Roeck /* ON Semiconductor */
1244453d736SGuenter Roeck #define CAT6095_DEVID		0x0800	/* Also matches CAT34TS02 */
1254453d736SGuenter Roeck #define CAT6095_DEVID_MASK	0xffe0
1264453d736SGuenter Roeck 
1274453d736SGuenter Roeck /* ST Microelectronics */
1284453d736SGuenter Roeck #define STTS424_DEVID		0x0101
1294453d736SGuenter Roeck #define STTS424_DEVID_MASK	0xffff
1304453d736SGuenter Roeck 
1314453d736SGuenter Roeck #define STTS424E_DEVID		0x0000
1324453d736SGuenter Roeck #define STTS424E_DEVID_MASK	0xfffe
1334453d736SGuenter Roeck 
1344de86126SJean Delvare #define STTS2002_DEVID		0x0300
1354de86126SJean Delvare #define STTS2002_DEVID_MASK	0xffff
1364de86126SJean Delvare 
137175c490cSGuenter Roeck #define STTS2004_DEVID		0x2201
138175c490cSGuenter Roeck #define STTS2004_DEVID_MASK	0xffff
139175c490cSGuenter Roeck 
1404de86126SJean Delvare #define STTS3000_DEVID		0x0200
1414de86126SJean Delvare #define STTS3000_DEVID_MASK	0xffff
1424de86126SJean Delvare 
1434453d736SGuenter Roeck static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
1444453d736SGuenter Roeck 
1454453d736SGuenter Roeck struct jc42_chips {
1464453d736SGuenter Roeck 	u16 manid;
1474453d736SGuenter Roeck 	u16 devid;
1484453d736SGuenter Roeck 	u16 devid_mask;
1494453d736SGuenter Roeck };
1504453d736SGuenter Roeck 
1514453d736SGuenter Roeck static struct jc42_chips jc42_chips[] = {
1524453d736SGuenter Roeck 	{ ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
1531bd612a2SGuenter Roeck 	{ ATMEL_MANID, AT30TS00_DEVID, AT30TS00_DEVID_MASK },
154175c490cSGuenter Roeck 	{ ATMEL_MANID2, AT30TSE004_DEVID, AT30TSE004_DEVID_MASK },
1554453d736SGuenter Roeck 	{ IDT_MANID, TS3000B3_DEVID, TS3000B3_DEVID_MASK },
1561bd612a2SGuenter Roeck 	{ IDT_MANID, TS3000GB2_DEVID, TS3000GB2_DEVID_MASK },
1574453d736SGuenter Roeck 	{ MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
1581bd612a2SGuenter Roeck 	{ MCP_MANID, MCP9804_DEVID, MCP9804_DEVID_MASK },
1594453d736SGuenter Roeck 	{ MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
1604453d736SGuenter Roeck 	{ MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
161d4768280SGuenter Roeck 	{ MCP_MANID, MCP98244_DEVID, MCP98244_DEVID_MASK },
1624453d736SGuenter Roeck 	{ MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
1634453d736SGuenter Roeck 	{ NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
1644453d736SGuenter Roeck 	{ ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
1654453d736SGuenter Roeck 	{ NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
1664453d736SGuenter Roeck 	{ STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
1674453d736SGuenter Roeck 	{ STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
1684de86126SJean Delvare 	{ STM_MANID, STTS2002_DEVID, STTS2002_DEVID_MASK },
169175c490cSGuenter Roeck 	{ STM_MANID, STTS2004_DEVID, STTS2004_DEVID_MASK },
1704de86126SJean Delvare 	{ STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK },
1714453d736SGuenter Roeck };
1724453d736SGuenter Roeck 
17310192bc6SGuenter Roeck enum temp_index {
17410192bc6SGuenter Roeck 	t_input = 0,
17510192bc6SGuenter Roeck 	t_crit,
17610192bc6SGuenter Roeck 	t_min,
17710192bc6SGuenter Roeck 	t_max,
17810192bc6SGuenter Roeck 	t_num_temp
17910192bc6SGuenter Roeck };
18010192bc6SGuenter Roeck 
18110192bc6SGuenter Roeck static const u8 temp_regs[t_num_temp] = {
18210192bc6SGuenter Roeck 	[t_input] = JC42_REG_TEMP,
18310192bc6SGuenter Roeck 	[t_crit] = JC42_REG_TEMP_CRITICAL,
18410192bc6SGuenter Roeck 	[t_min] = JC42_REG_TEMP_LOWER,
18510192bc6SGuenter Roeck 	[t_max] = JC42_REG_TEMP_UPPER,
18610192bc6SGuenter Roeck };
18710192bc6SGuenter Roeck 
1884453d736SGuenter Roeck /* Each client has this additional data */
1894453d736SGuenter Roeck struct jc42_data {
19062f9a57cSGuenter Roeck 	struct i2c_client *client;
1914453d736SGuenter Roeck 	struct mutex	update_lock;	/* protect register access */
1924453d736SGuenter Roeck 	bool		extended;	/* true if extended range supported */
1934453d736SGuenter Roeck 	bool		valid;
1944453d736SGuenter Roeck 	unsigned long	last_updated;	/* In jiffies */
1954453d736SGuenter Roeck 	u16		orig_config;	/* original configuration */
1964453d736SGuenter Roeck 	u16		config;		/* current configuration */
19710192bc6SGuenter Roeck 	u16		temp[t_num_temp];/* Temperatures */
1984453d736SGuenter Roeck };
1994453d736SGuenter Roeck 
2004453d736SGuenter Roeck #define JC42_TEMP_MIN_EXTENDED	(-40000)
2014453d736SGuenter Roeck #define JC42_TEMP_MIN		0
2024453d736SGuenter Roeck #define JC42_TEMP_MAX		125000
2034453d736SGuenter Roeck 
2044453d736SGuenter Roeck static u16 jc42_temp_to_reg(int temp, bool extended)
2054453d736SGuenter Roeck {
2062a844c14SGuenter Roeck 	int ntemp = clamp_val(temp,
2074453d736SGuenter Roeck 			      extended ? JC42_TEMP_MIN_EXTENDED :
2084453d736SGuenter Roeck 			      JC42_TEMP_MIN, JC42_TEMP_MAX);
2094453d736SGuenter Roeck 
2104453d736SGuenter Roeck 	/* convert from 0.001 to 0.0625 resolution */
2114453d736SGuenter Roeck 	return (ntemp * 2 / 125) & 0x1fff;
2124453d736SGuenter Roeck }
2134453d736SGuenter Roeck 
2144453d736SGuenter Roeck static int jc42_temp_from_reg(s16 reg)
2154453d736SGuenter Roeck {
216*bca6a1adSGuenter Roeck 	reg = sign_extend32(reg, 12);
2174453d736SGuenter Roeck 
2184453d736SGuenter Roeck 	/* convert from 0.0625 to 0.001 resolution */
2194453d736SGuenter Roeck 	return reg * 125 / 2;
2204453d736SGuenter Roeck }
2214453d736SGuenter Roeck 
222d397276bSGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev)
223d397276bSGuenter Roeck {
224d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
225d397276bSGuenter Roeck 	struct i2c_client *client = data->client;
226d397276bSGuenter Roeck 	struct jc42_data *ret = data;
22710192bc6SGuenter Roeck 	int i, val;
228d397276bSGuenter Roeck 
229d397276bSGuenter Roeck 	mutex_lock(&data->update_lock);
230d397276bSGuenter Roeck 
231d397276bSGuenter Roeck 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
23210192bc6SGuenter Roeck 		for (i = 0; i < t_num_temp; i++) {
23310192bc6SGuenter Roeck 			val = i2c_smbus_read_word_swapped(client, temp_regs[i]);
234d397276bSGuenter Roeck 			if (val < 0) {
235d397276bSGuenter Roeck 				ret = ERR_PTR(val);
236d397276bSGuenter Roeck 				goto abort;
237d397276bSGuenter Roeck 			}
23810192bc6SGuenter Roeck 			data->temp[i] = val;
239d397276bSGuenter Roeck 		}
240d397276bSGuenter Roeck 		data->last_updated = jiffies;
241d397276bSGuenter Roeck 		data->valid = true;
242d397276bSGuenter Roeck 	}
243d397276bSGuenter Roeck abort:
244d397276bSGuenter Roeck 	mutex_unlock(&data->update_lock);
245d397276bSGuenter Roeck 	return ret;
246d397276bSGuenter Roeck }
247d397276bSGuenter Roeck 
24810192bc6SGuenter Roeck /* sysfs functions */
2494453d736SGuenter Roeck 
25010192bc6SGuenter Roeck static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
25110192bc6SGuenter Roeck 			 char *buf)
25210192bc6SGuenter Roeck {
25310192bc6SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
25410192bc6SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
25510192bc6SGuenter Roeck 	if (IS_ERR(data))
25610192bc6SGuenter Roeck 		return PTR_ERR(data);
25710192bc6SGuenter Roeck 	return sprintf(buf, "%d\n",
25810192bc6SGuenter Roeck 		       jc42_temp_from_reg(data->temp[attr->index]));
2594453d736SGuenter Roeck }
2604453d736SGuenter Roeck 
26110192bc6SGuenter Roeck static ssize_t show_temp_hyst(struct device *dev,
26210192bc6SGuenter Roeck 			      struct device_attribute *devattr, char *buf)
2634453d736SGuenter Roeck {
26410192bc6SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
2654453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
2664453d736SGuenter Roeck 	int temp, hyst;
2674453d736SGuenter Roeck 
2684453d736SGuenter Roeck 	if (IS_ERR(data))
2694453d736SGuenter Roeck 		return PTR_ERR(data);
2704453d736SGuenter Roeck 
27110192bc6SGuenter Roeck 	temp = jc42_temp_from_reg(data->temp[attr->index]);
2722ccc8731SJean Delvare 	hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
2732ccc8731SJean Delvare 			       >> JC42_CFG_HYST_SHIFT];
2744453d736SGuenter Roeck 	return sprintf(buf, "%d\n", temp - hyst);
2754453d736SGuenter Roeck }
2764453d736SGuenter Roeck 
27710192bc6SGuenter Roeck static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
27810192bc6SGuenter Roeck 			const char *buf, size_t count)
2794453d736SGuenter Roeck {
28010192bc6SGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
28110192bc6SGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
28210192bc6SGuenter Roeck 	int err, ret = count;
28310192bc6SGuenter Roeck 	int nr = attr->index;
28410192bc6SGuenter Roeck 	long val;
2854453d736SGuenter Roeck 
28610192bc6SGuenter Roeck 	if (kstrtol(buf, 10, &val) < 0)
28710192bc6SGuenter Roeck 		return -EINVAL;
28810192bc6SGuenter Roeck 	mutex_lock(&data->update_lock);
28910192bc6SGuenter Roeck 	data->temp[nr] = jc42_temp_to_reg(val, data->extended);
29010192bc6SGuenter Roeck 	err = i2c_smbus_write_word_swapped(data->client, temp_regs[nr],
29110192bc6SGuenter Roeck 					   data->temp[nr]);
29210192bc6SGuenter Roeck 	if (err < 0)
29310192bc6SGuenter Roeck 		ret = err;
29410192bc6SGuenter Roeck 	mutex_unlock(&data->update_lock);
29510192bc6SGuenter Roeck 	return ret;
2964453d736SGuenter Roeck }
2974453d736SGuenter Roeck 
2985d577dbaSGuenter Roeck /*
2995d577dbaSGuenter Roeck  * JC42.4 compliant chips only support four hysteresis values.
3005d577dbaSGuenter Roeck  * Pick best choice and go from there.
3015d577dbaSGuenter Roeck  */
3024453d736SGuenter Roeck static ssize_t set_temp_crit_hyst(struct device *dev,
3034453d736SGuenter Roeck 				  struct device_attribute *attr,
3044453d736SGuenter Roeck 				  const char *buf, size_t count)
3054453d736SGuenter Roeck {
30662f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
307e8667296SClemens Ladisch 	unsigned long val;
3084453d736SGuenter Roeck 	int diff, hyst;
3094453d736SGuenter Roeck 	int err;
3104453d736SGuenter Roeck 	int ret = count;
3114453d736SGuenter Roeck 
312179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val) < 0)
3134453d736SGuenter Roeck 		return -EINVAL;
3144453d736SGuenter Roeck 
31510192bc6SGuenter Roeck 	diff = jc42_temp_from_reg(data->temp[t_crit]) - val;
3164453d736SGuenter Roeck 	hyst = 0;
3174453d736SGuenter Roeck 	if (diff > 0) {
3184453d736SGuenter Roeck 		if (diff < 2250)
3194453d736SGuenter Roeck 			hyst = 1;	/* 1.5 degrees C */
3204453d736SGuenter Roeck 		else if (diff < 4500)
3214453d736SGuenter Roeck 			hyst = 2;	/* 3.0 degrees C */
3224453d736SGuenter Roeck 		else
3234453d736SGuenter Roeck 			hyst = 3;	/* 6.0 degrees C */
3244453d736SGuenter Roeck 	}
3254453d736SGuenter Roeck 
3264453d736SGuenter Roeck 	mutex_lock(&data->update_lock);
3272ccc8731SJean Delvare 	data->config = (data->config & ~JC42_CFG_HYST_MASK)
3284453d736SGuenter Roeck 	  | (hyst << JC42_CFG_HYST_SHIFT);
32962f9a57cSGuenter Roeck 	err = i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
33090f4102cSJean Delvare 					   data->config);
3314453d736SGuenter Roeck 	if (err < 0)
3324453d736SGuenter Roeck 		ret = err;
3334453d736SGuenter Roeck 	mutex_unlock(&data->update_lock);
3344453d736SGuenter Roeck 	return ret;
3354453d736SGuenter Roeck }
3364453d736SGuenter Roeck 
3374453d736SGuenter Roeck static ssize_t show_alarm(struct device *dev,
3384453d736SGuenter Roeck 			  struct device_attribute *attr, char *buf)
3394453d736SGuenter Roeck {
3404453d736SGuenter Roeck 	u16 bit = to_sensor_dev_attr(attr)->index;
3414453d736SGuenter Roeck 	struct jc42_data *data = jc42_update_device(dev);
3424453d736SGuenter Roeck 	u16 val;
3434453d736SGuenter Roeck 
3444453d736SGuenter Roeck 	if (IS_ERR(data))
3454453d736SGuenter Roeck 		return PTR_ERR(data);
3464453d736SGuenter Roeck 
34710192bc6SGuenter Roeck 	val = data->temp[t_input];
3484453d736SGuenter Roeck 	if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY))
3494453d736SGuenter Roeck 		val = 0;
3504453d736SGuenter Roeck 	return sprintf(buf, "%u\n", (val >> bit) & 1);
3514453d736SGuenter Roeck }
3524453d736SGuenter Roeck 
35310192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
35410192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, set_temp, t_crit);
35510192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, set_temp, t_min);
35610192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, set_temp, t_max);
3574453d736SGuenter Roeck 
35810192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_hyst,
35910192bc6SGuenter Roeck 			  set_temp_crit_hyst, t_crit);
36010192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max);
3614453d736SGuenter Roeck 
3624453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
3634453d736SGuenter Roeck 			  JC42_ALARM_CRIT_BIT);
3644453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
3654453d736SGuenter Roeck 			  JC42_ALARM_MIN_BIT);
3664453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
3674453d736SGuenter Roeck 			  JC42_ALARM_MAX_BIT);
3684453d736SGuenter Roeck 
3694453d736SGuenter Roeck static struct attribute *jc42_attributes[] = {
37010192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_input.dev_attr.attr,
37110192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
37210192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_min.dev_attr.attr,
37310192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_max.dev_attr.attr,
37410192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
37510192bc6SGuenter Roeck 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
3764453d736SGuenter Roeck 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
3774453d736SGuenter Roeck 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
3784453d736SGuenter Roeck 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
3794453d736SGuenter Roeck 	NULL
3804453d736SGuenter Roeck };
3814453d736SGuenter Roeck 
382587a1f16SAl Viro static umode_t jc42_attribute_mode(struct kobject *kobj,
3832c6315daSClemens Ladisch 				  struct attribute *attr, int index)
3842c6315daSClemens Ladisch {
3852c6315daSClemens Ladisch 	struct device *dev = container_of(kobj, struct device, kobj);
38662f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
3872c6315daSClemens Ladisch 	unsigned int config = data->config;
3882c6315daSClemens Ladisch 	bool readonly;
3892c6315daSClemens Ladisch 
39010192bc6SGuenter Roeck 	if (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr)
3912c6315daSClemens Ladisch 		readonly = config & JC42_CFG_TCRIT_LOCK;
39210192bc6SGuenter Roeck 	else if (attr == &sensor_dev_attr_temp1_min.dev_attr.attr ||
39310192bc6SGuenter Roeck 		 attr == &sensor_dev_attr_temp1_max.dev_attr.attr)
3942c6315daSClemens Ladisch 		readonly = config & JC42_CFG_EVENT_LOCK;
39510192bc6SGuenter Roeck 	else if (attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr)
3962c6315daSClemens Ladisch 		readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK);
3972c6315daSClemens Ladisch 	else
3982c6315daSClemens Ladisch 		readonly = true;
3992c6315daSClemens Ladisch 
4002c6315daSClemens Ladisch 	return S_IRUGO | (readonly ? 0 : S_IWUSR);
4012c6315daSClemens Ladisch }
4022c6315daSClemens Ladisch 
4034453d736SGuenter Roeck static const struct attribute_group jc42_group = {
4044453d736SGuenter Roeck 	.attrs = jc42_attributes,
4052c6315daSClemens Ladisch 	.is_visible = jc42_attribute_mode,
4064453d736SGuenter Roeck };
40762f9a57cSGuenter Roeck __ATTRIBUTE_GROUPS(jc42);
4084453d736SGuenter Roeck 
4094453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */
410f15df57dSGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info)
4114453d736SGuenter Roeck {
412f15df57dSGuenter Roeck 	struct i2c_adapter *adapter = client->adapter;
4134453d736SGuenter Roeck 	int i, config, cap, manid, devid;
4144453d736SGuenter Roeck 
4154453d736SGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
4164453d736SGuenter Roeck 				     I2C_FUNC_SMBUS_WORD_DATA))
4174453d736SGuenter Roeck 		return -ENODEV;
4184453d736SGuenter Roeck 
419f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
420f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
421f15df57dSGuenter Roeck 	manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID);
422f15df57dSGuenter Roeck 	devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID);
4234453d736SGuenter Roeck 
4244453d736SGuenter Roeck 	if (cap < 0 || config < 0 || manid < 0 || devid < 0)
4254453d736SGuenter Roeck 		return -ENODEV;
4264453d736SGuenter Roeck 
4274453d736SGuenter Roeck 	if ((cap & 0xff00) || (config & 0xf800))
4284453d736SGuenter Roeck 		return -ENODEV;
4294453d736SGuenter Roeck 
4304453d736SGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
4314453d736SGuenter Roeck 		struct jc42_chips *chip = &jc42_chips[i];
4324453d736SGuenter Roeck 		if (manid == chip->manid &&
4334453d736SGuenter Roeck 		    (devid & chip->devid_mask) == chip->devid) {
4344453d736SGuenter Roeck 			strlcpy(info->type, "jc42", I2C_NAME_SIZE);
4354453d736SGuenter Roeck 			return 0;
4364453d736SGuenter Roeck 		}
4374453d736SGuenter Roeck 	}
4384453d736SGuenter Roeck 	return -ENODEV;
4394453d736SGuenter Roeck }
4404453d736SGuenter Roeck 
441f15df57dSGuenter Roeck static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id)
4424453d736SGuenter Roeck {
443f15df57dSGuenter Roeck 	struct device *dev = &client->dev;
44462f9a57cSGuenter Roeck 	struct device *hwmon_dev;
44562f9a57cSGuenter Roeck 	struct jc42_data *data;
44662f9a57cSGuenter Roeck 	int config, cap;
4474453d736SGuenter Roeck 
448f15df57dSGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL);
449f15df57dSGuenter Roeck 	if (!data)
450f15df57dSGuenter Roeck 		return -ENOMEM;
4514453d736SGuenter Roeck 
45262f9a57cSGuenter Roeck 	data->client = client;
453f15df57dSGuenter Roeck 	i2c_set_clientdata(client, data);
4544453d736SGuenter Roeck 	mutex_init(&data->update_lock);
4554453d736SGuenter Roeck 
456f15df57dSGuenter Roeck 	cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
457f15df57dSGuenter Roeck 	if (cap < 0)
458f15df57dSGuenter Roeck 		return cap;
459f15df57dSGuenter Roeck 
4604453d736SGuenter Roeck 	data->extended = !!(cap & JC42_CAP_RANGE);
4614453d736SGuenter Roeck 
462f15df57dSGuenter Roeck 	config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
463f15df57dSGuenter Roeck 	if (config < 0)
464f15df57dSGuenter Roeck 		return config;
465f15df57dSGuenter Roeck 
4664453d736SGuenter Roeck 	data->orig_config = config;
4674453d736SGuenter Roeck 	if (config & JC42_CFG_SHUTDOWN) {
4684453d736SGuenter Roeck 		config &= ~JC42_CFG_SHUTDOWN;
469f15df57dSGuenter Roeck 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
4704453d736SGuenter Roeck 	}
4714453d736SGuenter Roeck 	data->config = config;
4724453d736SGuenter Roeck 
47362f9a57cSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
47462f9a57cSGuenter Roeck 							   data,
47562f9a57cSGuenter Roeck 							   jc42_groups);
476650a2c02SFengguang Wu 	return PTR_ERR_OR_ZERO(hwmon_dev);
4774453d736SGuenter Roeck }
4784453d736SGuenter Roeck 
4794453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client)
4804453d736SGuenter Roeck {
4814453d736SGuenter Roeck 	struct jc42_data *data = i2c_get_clientdata(client);
4825953e276SJean Delvare 
4835953e276SJean Delvare 	/* Restore original configuration except hysteresis */
4845953e276SJean Delvare 	if ((data->config & ~JC42_CFG_HYST_MASK) !=
4855953e276SJean Delvare 	    (data->orig_config & ~JC42_CFG_HYST_MASK)) {
4865953e276SJean Delvare 		int config;
4875953e276SJean Delvare 
4885953e276SJean Delvare 		config = (data->orig_config & ~JC42_CFG_HYST_MASK)
4895953e276SJean Delvare 		  | (data->config & JC42_CFG_HYST_MASK);
4905953e276SJean Delvare 		i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
4915953e276SJean Delvare 	}
4924453d736SGuenter Roeck 	return 0;
4934453d736SGuenter Roeck }
4944453d736SGuenter Roeck 
495d397276bSGuenter Roeck #ifdef CONFIG_PM
496d397276bSGuenter Roeck 
497d397276bSGuenter Roeck static int jc42_suspend(struct device *dev)
4984453d736SGuenter Roeck {
49962f9a57cSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5004453d736SGuenter Roeck 
501d397276bSGuenter Roeck 	data->config |= JC42_CFG_SHUTDOWN;
502d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
503d397276bSGuenter Roeck 				     data->config);
504d397276bSGuenter Roeck 	return 0;
505d397276bSGuenter Roeck }
5064453d736SGuenter Roeck 
507d397276bSGuenter Roeck static int jc42_resume(struct device *dev)
508d397276bSGuenter Roeck {
509d397276bSGuenter Roeck 	struct jc42_data *data = dev_get_drvdata(dev);
5104453d736SGuenter Roeck 
511d397276bSGuenter Roeck 	data->config &= ~JC42_CFG_SHUTDOWN;
512d397276bSGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
513d397276bSGuenter Roeck 				     data->config);
514d397276bSGuenter Roeck 	return 0;
5154453d736SGuenter Roeck }
5164453d736SGuenter Roeck 
517d397276bSGuenter Roeck static const struct dev_pm_ops jc42_dev_pm_ops = {
518d397276bSGuenter Roeck 	.suspend = jc42_suspend,
519d397276bSGuenter Roeck 	.resume = jc42_resume,
520d397276bSGuenter Roeck };
5214453d736SGuenter Roeck 
522d397276bSGuenter Roeck #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
523d397276bSGuenter Roeck #else
524d397276bSGuenter Roeck #define JC42_DEV_PM_OPS NULL
525d397276bSGuenter Roeck #endif /* CONFIG_PM */
5264453d736SGuenter Roeck 
527d397276bSGuenter Roeck static const struct i2c_device_id jc42_id[] = {
528d397276bSGuenter Roeck 	{ "jc42", 0 },
529d397276bSGuenter Roeck 	{ }
530d397276bSGuenter Roeck };
531d397276bSGuenter Roeck MODULE_DEVICE_TABLE(i2c, jc42_id);
532d397276bSGuenter Roeck 
533d397276bSGuenter Roeck static struct i2c_driver jc42_driver = {
534d397276bSGuenter Roeck 	.class		= I2C_CLASS_SPD,
535d397276bSGuenter Roeck 	.driver = {
536d397276bSGuenter Roeck 		.name	= "jc42",
537d397276bSGuenter Roeck 		.pm = JC42_DEV_PM_OPS,
538d397276bSGuenter Roeck 	},
539d397276bSGuenter Roeck 	.probe		= jc42_probe,
540d397276bSGuenter Roeck 	.remove		= jc42_remove,
541d397276bSGuenter Roeck 	.id_table	= jc42_id,
542d397276bSGuenter Roeck 	.detect		= jc42_detect,
543d397276bSGuenter Roeck 	.address_list	= normal_i2c,
544d397276bSGuenter Roeck };
5454453d736SGuenter Roeck 
546f0967eeaSAxel Lin module_i2c_driver(jc42_driver);
5474453d736SGuenter Roeck 
548bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
5494453d736SGuenter Roeck MODULE_DESCRIPTION("JC42 driver");
5504453d736SGuenter Roeck MODULE_LICENSE("GPL");
551