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 */ 68*175c490cSGuenter 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 86*175c490cSGuenter Roeck #define AT30TSE004_DEVID 0x2200 87*175c490cSGuenter Roeck #define AT30TSE004_DEVID_MASK 0xffff 88*175c490cSGuenter 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 137*175c490cSGuenter Roeck #define STTS2004_DEVID 0x2201 138*175c490cSGuenter Roeck #define STTS2004_DEVID_MASK 0xffff 139*175c490cSGuenter 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 }, 154*175c490cSGuenter 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 }, 169*175c490cSGuenter 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 { 2164453d736SGuenter Roeck reg &= 0x1fff; 2174453d736SGuenter Roeck 2184453d736SGuenter Roeck /* sign extend register */ 2194453d736SGuenter Roeck if (reg & 0x1000) 2204453d736SGuenter Roeck reg |= 0xf000; 2214453d736SGuenter Roeck 2224453d736SGuenter Roeck /* convert from 0.0625 to 0.001 resolution */ 2234453d736SGuenter Roeck return reg * 125 / 2; 2244453d736SGuenter Roeck } 2254453d736SGuenter Roeck 226d397276bSGuenter Roeck static struct jc42_data *jc42_update_device(struct device *dev) 227d397276bSGuenter Roeck { 228d397276bSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 229d397276bSGuenter Roeck struct i2c_client *client = data->client; 230d397276bSGuenter Roeck struct jc42_data *ret = data; 23110192bc6SGuenter Roeck int i, val; 232d397276bSGuenter Roeck 233d397276bSGuenter Roeck mutex_lock(&data->update_lock); 234d397276bSGuenter Roeck 235d397276bSGuenter Roeck if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 23610192bc6SGuenter Roeck for (i = 0; i < t_num_temp; i++) { 23710192bc6SGuenter Roeck val = i2c_smbus_read_word_swapped(client, temp_regs[i]); 238d397276bSGuenter Roeck if (val < 0) { 239d397276bSGuenter Roeck ret = ERR_PTR(val); 240d397276bSGuenter Roeck goto abort; 241d397276bSGuenter Roeck } 24210192bc6SGuenter Roeck data->temp[i] = val; 243d397276bSGuenter Roeck } 244d397276bSGuenter Roeck data->last_updated = jiffies; 245d397276bSGuenter Roeck data->valid = true; 246d397276bSGuenter Roeck } 247d397276bSGuenter Roeck abort: 248d397276bSGuenter Roeck mutex_unlock(&data->update_lock); 249d397276bSGuenter Roeck return ret; 250d397276bSGuenter Roeck } 251d397276bSGuenter Roeck 25210192bc6SGuenter Roeck /* sysfs functions */ 2534453d736SGuenter Roeck 25410192bc6SGuenter Roeck static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, 25510192bc6SGuenter Roeck char *buf) 25610192bc6SGuenter Roeck { 25710192bc6SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 25810192bc6SGuenter Roeck struct jc42_data *data = jc42_update_device(dev); 25910192bc6SGuenter Roeck if (IS_ERR(data)) 26010192bc6SGuenter Roeck return PTR_ERR(data); 26110192bc6SGuenter Roeck return sprintf(buf, "%d\n", 26210192bc6SGuenter Roeck jc42_temp_from_reg(data->temp[attr->index])); 2634453d736SGuenter Roeck } 2644453d736SGuenter Roeck 26510192bc6SGuenter Roeck static ssize_t show_temp_hyst(struct device *dev, 26610192bc6SGuenter Roeck struct device_attribute *devattr, char *buf) 2674453d736SGuenter Roeck { 26810192bc6SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 2694453d736SGuenter Roeck struct jc42_data *data = jc42_update_device(dev); 2704453d736SGuenter Roeck int temp, hyst; 2714453d736SGuenter Roeck 2724453d736SGuenter Roeck if (IS_ERR(data)) 2734453d736SGuenter Roeck return PTR_ERR(data); 2744453d736SGuenter Roeck 27510192bc6SGuenter Roeck temp = jc42_temp_from_reg(data->temp[attr->index]); 2762ccc8731SJean Delvare hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK) 2772ccc8731SJean Delvare >> JC42_CFG_HYST_SHIFT]; 2784453d736SGuenter Roeck return sprintf(buf, "%d\n", temp - hyst); 2794453d736SGuenter Roeck } 2804453d736SGuenter Roeck 28110192bc6SGuenter Roeck static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, 28210192bc6SGuenter Roeck const char *buf, size_t count) 2834453d736SGuenter Roeck { 28410192bc6SGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 28510192bc6SGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 28610192bc6SGuenter Roeck int err, ret = count; 28710192bc6SGuenter Roeck int nr = attr->index; 28810192bc6SGuenter Roeck long val; 2894453d736SGuenter Roeck 29010192bc6SGuenter Roeck if (kstrtol(buf, 10, &val) < 0) 29110192bc6SGuenter Roeck return -EINVAL; 29210192bc6SGuenter Roeck mutex_lock(&data->update_lock); 29310192bc6SGuenter Roeck data->temp[nr] = jc42_temp_to_reg(val, data->extended); 29410192bc6SGuenter Roeck err = i2c_smbus_write_word_swapped(data->client, temp_regs[nr], 29510192bc6SGuenter Roeck data->temp[nr]); 29610192bc6SGuenter Roeck if (err < 0) 29710192bc6SGuenter Roeck ret = err; 29810192bc6SGuenter Roeck mutex_unlock(&data->update_lock); 29910192bc6SGuenter Roeck return ret; 3004453d736SGuenter Roeck } 3014453d736SGuenter Roeck 3025d577dbaSGuenter Roeck /* 3035d577dbaSGuenter Roeck * JC42.4 compliant chips only support four hysteresis values. 3045d577dbaSGuenter Roeck * Pick best choice and go from there. 3055d577dbaSGuenter Roeck */ 3064453d736SGuenter Roeck static ssize_t set_temp_crit_hyst(struct device *dev, 3074453d736SGuenter Roeck struct device_attribute *attr, 3084453d736SGuenter Roeck const char *buf, size_t count) 3094453d736SGuenter Roeck { 31062f9a57cSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 311e8667296SClemens Ladisch unsigned long val; 3124453d736SGuenter Roeck int diff, hyst; 3134453d736SGuenter Roeck int err; 3144453d736SGuenter Roeck int ret = count; 3154453d736SGuenter Roeck 316179c4fdbSFrans Meulenbroeks if (kstrtoul(buf, 10, &val) < 0) 3174453d736SGuenter Roeck return -EINVAL; 3184453d736SGuenter Roeck 31910192bc6SGuenter Roeck diff = jc42_temp_from_reg(data->temp[t_crit]) - val; 3204453d736SGuenter Roeck hyst = 0; 3214453d736SGuenter Roeck if (diff > 0) { 3224453d736SGuenter Roeck if (diff < 2250) 3234453d736SGuenter Roeck hyst = 1; /* 1.5 degrees C */ 3244453d736SGuenter Roeck else if (diff < 4500) 3254453d736SGuenter Roeck hyst = 2; /* 3.0 degrees C */ 3264453d736SGuenter Roeck else 3274453d736SGuenter Roeck hyst = 3; /* 6.0 degrees C */ 3284453d736SGuenter Roeck } 3294453d736SGuenter Roeck 3304453d736SGuenter Roeck mutex_lock(&data->update_lock); 3312ccc8731SJean Delvare data->config = (data->config & ~JC42_CFG_HYST_MASK) 3324453d736SGuenter Roeck | (hyst << JC42_CFG_HYST_SHIFT); 33362f9a57cSGuenter Roeck err = i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG, 33490f4102cSJean Delvare data->config); 3354453d736SGuenter Roeck if (err < 0) 3364453d736SGuenter Roeck ret = err; 3374453d736SGuenter Roeck mutex_unlock(&data->update_lock); 3384453d736SGuenter Roeck return ret; 3394453d736SGuenter Roeck } 3404453d736SGuenter Roeck 3414453d736SGuenter Roeck static ssize_t show_alarm(struct device *dev, 3424453d736SGuenter Roeck struct device_attribute *attr, char *buf) 3434453d736SGuenter Roeck { 3444453d736SGuenter Roeck u16 bit = to_sensor_dev_attr(attr)->index; 3454453d736SGuenter Roeck struct jc42_data *data = jc42_update_device(dev); 3464453d736SGuenter Roeck u16 val; 3474453d736SGuenter Roeck 3484453d736SGuenter Roeck if (IS_ERR(data)) 3494453d736SGuenter Roeck return PTR_ERR(data); 3504453d736SGuenter Roeck 35110192bc6SGuenter Roeck val = data->temp[t_input]; 3524453d736SGuenter Roeck if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY)) 3534453d736SGuenter Roeck val = 0; 3544453d736SGuenter Roeck return sprintf(buf, "%u\n", (val >> bit) & 1); 3554453d736SGuenter Roeck } 3564453d736SGuenter Roeck 35710192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input); 35810192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, set_temp, t_crit); 35910192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, set_temp, t_min); 36010192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, set_temp, t_max); 3614453d736SGuenter Roeck 36210192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, show_temp_hyst, 36310192bc6SGuenter Roeck set_temp_crit_hyst, t_crit); 36410192bc6SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max); 3654453d736SGuenter Roeck 3664453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 3674453d736SGuenter Roeck JC42_ALARM_CRIT_BIT); 3684453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 3694453d736SGuenter Roeck JC42_ALARM_MIN_BIT); 3704453d736SGuenter Roeck static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 3714453d736SGuenter Roeck JC42_ALARM_MAX_BIT); 3724453d736SGuenter Roeck 3734453d736SGuenter Roeck static struct attribute *jc42_attributes[] = { 37410192bc6SGuenter Roeck &sensor_dev_attr_temp1_input.dev_attr.attr, 37510192bc6SGuenter Roeck &sensor_dev_attr_temp1_crit.dev_attr.attr, 37610192bc6SGuenter Roeck &sensor_dev_attr_temp1_min.dev_attr.attr, 37710192bc6SGuenter Roeck &sensor_dev_attr_temp1_max.dev_attr.attr, 37810192bc6SGuenter Roeck &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 37910192bc6SGuenter Roeck &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 3804453d736SGuenter Roeck &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 3814453d736SGuenter Roeck &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 3824453d736SGuenter Roeck &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 3834453d736SGuenter Roeck NULL 3844453d736SGuenter Roeck }; 3854453d736SGuenter Roeck 386587a1f16SAl Viro static umode_t jc42_attribute_mode(struct kobject *kobj, 3872c6315daSClemens Ladisch struct attribute *attr, int index) 3882c6315daSClemens Ladisch { 3892c6315daSClemens Ladisch struct device *dev = container_of(kobj, struct device, kobj); 39062f9a57cSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 3912c6315daSClemens Ladisch unsigned int config = data->config; 3922c6315daSClemens Ladisch bool readonly; 3932c6315daSClemens Ladisch 39410192bc6SGuenter Roeck if (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr) 3952c6315daSClemens Ladisch readonly = config & JC42_CFG_TCRIT_LOCK; 39610192bc6SGuenter Roeck else if (attr == &sensor_dev_attr_temp1_min.dev_attr.attr || 39710192bc6SGuenter Roeck attr == &sensor_dev_attr_temp1_max.dev_attr.attr) 3982c6315daSClemens Ladisch readonly = config & JC42_CFG_EVENT_LOCK; 39910192bc6SGuenter Roeck else if (attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr) 4002c6315daSClemens Ladisch readonly = config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK); 4012c6315daSClemens Ladisch else 4022c6315daSClemens Ladisch readonly = true; 4032c6315daSClemens Ladisch 4042c6315daSClemens Ladisch return S_IRUGO | (readonly ? 0 : S_IWUSR); 4052c6315daSClemens Ladisch } 4062c6315daSClemens Ladisch 4074453d736SGuenter Roeck static const struct attribute_group jc42_group = { 4084453d736SGuenter Roeck .attrs = jc42_attributes, 4092c6315daSClemens Ladisch .is_visible = jc42_attribute_mode, 4104453d736SGuenter Roeck }; 41162f9a57cSGuenter Roeck __ATTRIBUTE_GROUPS(jc42); 4124453d736SGuenter Roeck 4134453d736SGuenter Roeck /* Return 0 if detection is successful, -ENODEV otherwise */ 414f15df57dSGuenter Roeck static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info) 4154453d736SGuenter Roeck { 416f15df57dSGuenter Roeck struct i2c_adapter *adapter = client->adapter; 4174453d736SGuenter Roeck int i, config, cap, manid, devid; 4184453d736SGuenter Roeck 4194453d736SGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 4204453d736SGuenter Roeck I2C_FUNC_SMBUS_WORD_DATA)) 4214453d736SGuenter Roeck return -ENODEV; 4224453d736SGuenter Roeck 423f15df57dSGuenter Roeck cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP); 424f15df57dSGuenter Roeck config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG); 425f15df57dSGuenter Roeck manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID); 426f15df57dSGuenter Roeck devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID); 4274453d736SGuenter Roeck 4284453d736SGuenter Roeck if (cap < 0 || config < 0 || manid < 0 || devid < 0) 4294453d736SGuenter Roeck return -ENODEV; 4304453d736SGuenter Roeck 4314453d736SGuenter Roeck if ((cap & 0xff00) || (config & 0xf800)) 4324453d736SGuenter Roeck return -ENODEV; 4334453d736SGuenter Roeck 4344453d736SGuenter Roeck for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) { 4354453d736SGuenter Roeck struct jc42_chips *chip = &jc42_chips[i]; 4364453d736SGuenter Roeck if (manid == chip->manid && 4374453d736SGuenter Roeck (devid & chip->devid_mask) == chip->devid) { 4384453d736SGuenter Roeck strlcpy(info->type, "jc42", I2C_NAME_SIZE); 4394453d736SGuenter Roeck return 0; 4404453d736SGuenter Roeck } 4414453d736SGuenter Roeck } 4424453d736SGuenter Roeck return -ENODEV; 4434453d736SGuenter Roeck } 4444453d736SGuenter Roeck 445f15df57dSGuenter Roeck static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id) 4464453d736SGuenter Roeck { 447f15df57dSGuenter Roeck struct device *dev = &client->dev; 44862f9a57cSGuenter Roeck struct device *hwmon_dev; 44962f9a57cSGuenter Roeck struct jc42_data *data; 45062f9a57cSGuenter Roeck int config, cap; 4514453d736SGuenter Roeck 452f15df57dSGuenter Roeck data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL); 453f15df57dSGuenter Roeck if (!data) 454f15df57dSGuenter Roeck return -ENOMEM; 4554453d736SGuenter Roeck 45662f9a57cSGuenter Roeck data->client = client; 457f15df57dSGuenter Roeck i2c_set_clientdata(client, data); 4584453d736SGuenter Roeck mutex_init(&data->update_lock); 4594453d736SGuenter Roeck 460f15df57dSGuenter Roeck cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP); 461f15df57dSGuenter Roeck if (cap < 0) 462f15df57dSGuenter Roeck return cap; 463f15df57dSGuenter Roeck 4644453d736SGuenter Roeck data->extended = !!(cap & JC42_CAP_RANGE); 4654453d736SGuenter Roeck 466f15df57dSGuenter Roeck config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG); 467f15df57dSGuenter Roeck if (config < 0) 468f15df57dSGuenter Roeck return config; 469f15df57dSGuenter Roeck 4704453d736SGuenter Roeck data->orig_config = config; 4714453d736SGuenter Roeck if (config & JC42_CFG_SHUTDOWN) { 4724453d736SGuenter Roeck config &= ~JC42_CFG_SHUTDOWN; 473f15df57dSGuenter Roeck i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config); 4744453d736SGuenter Roeck } 4754453d736SGuenter Roeck data->config = config; 4764453d736SGuenter Roeck 47762f9a57cSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 47862f9a57cSGuenter Roeck data, 47962f9a57cSGuenter Roeck jc42_groups); 480650a2c02SFengguang Wu return PTR_ERR_OR_ZERO(hwmon_dev); 4814453d736SGuenter Roeck } 4824453d736SGuenter Roeck 4834453d736SGuenter Roeck static int jc42_remove(struct i2c_client *client) 4844453d736SGuenter Roeck { 4854453d736SGuenter Roeck struct jc42_data *data = i2c_get_clientdata(client); 4865953e276SJean Delvare 4875953e276SJean Delvare /* Restore original configuration except hysteresis */ 4885953e276SJean Delvare if ((data->config & ~JC42_CFG_HYST_MASK) != 4895953e276SJean Delvare (data->orig_config & ~JC42_CFG_HYST_MASK)) { 4905953e276SJean Delvare int config; 4915953e276SJean Delvare 4925953e276SJean Delvare config = (data->orig_config & ~JC42_CFG_HYST_MASK) 4935953e276SJean Delvare | (data->config & JC42_CFG_HYST_MASK); 4945953e276SJean Delvare i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config); 4955953e276SJean Delvare } 4964453d736SGuenter Roeck return 0; 4974453d736SGuenter Roeck } 4984453d736SGuenter Roeck 499d397276bSGuenter Roeck #ifdef CONFIG_PM 500d397276bSGuenter Roeck 501d397276bSGuenter Roeck static int jc42_suspend(struct device *dev) 5024453d736SGuenter Roeck { 50362f9a57cSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 5044453d736SGuenter Roeck 505d397276bSGuenter Roeck data->config |= JC42_CFG_SHUTDOWN; 506d397276bSGuenter Roeck i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG, 507d397276bSGuenter Roeck data->config); 508d397276bSGuenter Roeck return 0; 509d397276bSGuenter Roeck } 5104453d736SGuenter Roeck 511d397276bSGuenter Roeck static int jc42_resume(struct device *dev) 512d397276bSGuenter Roeck { 513d397276bSGuenter Roeck struct jc42_data *data = dev_get_drvdata(dev); 5144453d736SGuenter Roeck 515d397276bSGuenter Roeck data->config &= ~JC42_CFG_SHUTDOWN; 516d397276bSGuenter Roeck i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG, 517d397276bSGuenter Roeck data->config); 518d397276bSGuenter Roeck return 0; 5194453d736SGuenter Roeck } 5204453d736SGuenter Roeck 521d397276bSGuenter Roeck static const struct dev_pm_ops jc42_dev_pm_ops = { 522d397276bSGuenter Roeck .suspend = jc42_suspend, 523d397276bSGuenter Roeck .resume = jc42_resume, 524d397276bSGuenter Roeck }; 5254453d736SGuenter Roeck 526d397276bSGuenter Roeck #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops) 527d397276bSGuenter Roeck #else 528d397276bSGuenter Roeck #define JC42_DEV_PM_OPS NULL 529d397276bSGuenter Roeck #endif /* CONFIG_PM */ 5304453d736SGuenter Roeck 531d397276bSGuenter Roeck static const struct i2c_device_id jc42_id[] = { 532d397276bSGuenter Roeck { "jc42", 0 }, 533d397276bSGuenter Roeck { } 534d397276bSGuenter Roeck }; 535d397276bSGuenter Roeck MODULE_DEVICE_TABLE(i2c, jc42_id); 536d397276bSGuenter Roeck 537d397276bSGuenter Roeck static struct i2c_driver jc42_driver = { 538d397276bSGuenter Roeck .class = I2C_CLASS_SPD, 539d397276bSGuenter Roeck .driver = { 540d397276bSGuenter Roeck .name = "jc42", 541d397276bSGuenter Roeck .pm = JC42_DEV_PM_OPS, 542d397276bSGuenter Roeck }, 543d397276bSGuenter Roeck .probe = jc42_probe, 544d397276bSGuenter Roeck .remove = jc42_remove, 545d397276bSGuenter Roeck .id_table = jc42_id, 546d397276bSGuenter Roeck .detect = jc42_detect, 547d397276bSGuenter Roeck .address_list = normal_i2c, 548d397276bSGuenter Roeck }; 5494453d736SGuenter Roeck 550f0967eeaSAxel Lin module_i2c_driver(jc42_driver); 5514453d736SGuenter Roeck 552bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 5534453d736SGuenter Roeck MODULE_DESCRIPTION("JC42 driver"); 5544453d736SGuenter Roeck MODULE_LICENSE("GPL"); 555