106160327SDavide Rizzo /* 20f1deb4bSDavide Rizzo * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com> 306160327SDavide Rizzo * 40f1deb4bSDavide Rizzo * The LM95241 is a sensor chip made by National Semiconductors. 50f1deb4bSDavide Rizzo * It reports up to three temperatures (its own plus up to two external ones). 60f1deb4bSDavide Rizzo * Complete datasheet can be obtained from National's website at: 706160327SDavide Rizzo * http://www.national.com/ds.cgi/LM/LM95241.pdf 806160327SDavide Rizzo * 906160327SDavide Rizzo * This program is free software; you can redistribute it and/or modify 1006160327SDavide Rizzo * it under the terms of the GNU General Public License as published by 1106160327SDavide Rizzo * the Free Software Foundation; either version 2 of the License, or 1206160327SDavide Rizzo * (at your option) any later version. 1306160327SDavide Rizzo * 1406160327SDavide Rizzo * This program is distributed in the hope that it will be useful, 1506160327SDavide Rizzo * but WITHOUT ANY WARRANTY; without even the implied warranty of 1606160327SDavide Rizzo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1706160327SDavide Rizzo * GNU General Public License for more details. 1806160327SDavide Rizzo * 1906160327SDavide Rizzo * You should have received a copy of the GNU General Public License 2006160327SDavide Rizzo * along with this program; if not, write to the Free Software 2106160327SDavide Rizzo * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 2206160327SDavide Rizzo */ 2306160327SDavide Rizzo 2406160327SDavide Rizzo #include <linux/module.h> 2506160327SDavide Rizzo #include <linux/init.h> 2606160327SDavide Rizzo #include <linux/slab.h> 2706160327SDavide Rizzo #include <linux/jiffies.h> 2806160327SDavide Rizzo #include <linux/i2c.h> 2906160327SDavide Rizzo #include <linux/hwmon.h> 3006160327SDavide Rizzo #include <linux/hwmon-sysfs.h> 3106160327SDavide Rizzo #include <linux/err.h> 3206160327SDavide Rizzo #include <linux/mutex.h> 3306160327SDavide Rizzo #include <linux/sysfs.h> 3406160327SDavide Rizzo 350f1deb4bSDavide Rizzo #define DEVNAME "lm95241" 360f1deb4bSDavide Rizzo 3706160327SDavide Rizzo static const unsigned short normal_i2c[] = { 3806160327SDavide Rizzo 0x19, 0x2a, 0x2b, I2C_CLIENT_END }; 3906160327SDavide Rizzo 4006160327SDavide Rizzo /* LM95241 registers */ 4106160327SDavide Rizzo #define LM95241_REG_R_MAN_ID 0xFE 4206160327SDavide Rizzo #define LM95241_REG_R_CHIP_ID 0xFF 4306160327SDavide Rizzo #define LM95241_REG_R_STATUS 0x02 4406160327SDavide Rizzo #define LM95241_REG_RW_CONFIG 0x03 4506160327SDavide Rizzo #define LM95241_REG_RW_REM_FILTER 0x06 4606160327SDavide Rizzo #define LM95241_REG_RW_TRUTHERM 0x07 4706160327SDavide Rizzo #define LM95241_REG_W_ONE_SHOT 0x0F 4806160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPH 0x10 4906160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPH 0x11 5006160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPH 0x12 5106160327SDavide Rizzo #define LM95241_REG_R_LOCAL_TEMPL 0x20 5206160327SDavide Rizzo #define LM95241_REG_R_REMOTE1_TEMPL 0x21 5306160327SDavide Rizzo #define LM95241_REG_R_REMOTE2_TEMPL 0x22 5406160327SDavide Rizzo #define LM95241_REG_RW_REMOTE_MODEL 0x30 5506160327SDavide Rizzo 5606160327SDavide Rizzo /* LM95241 specific bitfields */ 5706160327SDavide Rizzo #define CFG_STOP 0x40 5806160327SDavide Rizzo #define CFG_CR0076 0x00 5906160327SDavide Rizzo #define CFG_CR0182 0x10 6006160327SDavide Rizzo #define CFG_CR1000 0x20 6106160327SDavide Rizzo #define CFG_CR2700 0x30 6206160327SDavide Rizzo #define R1MS_SHIFT 0 6306160327SDavide Rizzo #define R2MS_SHIFT 2 6406160327SDavide Rizzo #define R1MS_MASK (0x01 << (R1MS_SHIFT)) 6506160327SDavide Rizzo #define R2MS_MASK (0x01 << (R2MS_SHIFT)) 6606160327SDavide Rizzo #define R1DF_SHIFT 1 6706160327SDavide Rizzo #define R2DF_SHIFT 2 6806160327SDavide Rizzo #define R1DF_MASK (0x01 << (R1DF_SHIFT)) 6906160327SDavide Rizzo #define R2DF_MASK (0x01 << (R2DF_SHIFT)) 7006160327SDavide Rizzo #define R1FE_MASK 0x01 7106160327SDavide Rizzo #define R2FE_MASK 0x05 7206160327SDavide Rizzo #define TT1_SHIFT 0 7306160327SDavide Rizzo #define TT2_SHIFT 4 7406160327SDavide Rizzo #define TT_OFF 0 7506160327SDavide Rizzo #define TT_ON 1 7606160327SDavide Rizzo #define TT_MASK 7 778c1d0419SGuenter Roeck #define NATSEMI_MAN_ID 0x01 788c1d0419SGuenter Roeck #define LM95231_CHIP_ID 0xA1 798c1d0419SGuenter Roeck #define LM95241_CHIP_ID 0xA4 8006160327SDavide Rizzo 810f1deb4bSDavide Rizzo static const u8 lm95241_reg_address[] = { 820f1deb4bSDavide Rizzo LM95241_REG_R_LOCAL_TEMPH, 830f1deb4bSDavide Rizzo LM95241_REG_R_LOCAL_TEMPL, 840f1deb4bSDavide Rizzo LM95241_REG_R_REMOTE1_TEMPH, 850f1deb4bSDavide Rizzo LM95241_REG_R_REMOTE1_TEMPL, 860f1deb4bSDavide Rizzo LM95241_REG_R_REMOTE2_TEMPH, 870f1deb4bSDavide Rizzo LM95241_REG_R_REMOTE2_TEMPL 880f1deb4bSDavide Rizzo }; 8906160327SDavide Rizzo 9006160327SDavide Rizzo /* Client data (each client gets its own) */ 9106160327SDavide Rizzo struct lm95241_data { 9206160327SDavide Rizzo struct device *hwmon_dev; 9306160327SDavide Rizzo struct mutex update_lock; 94bc482bf0SGuenter Roeck unsigned long last_updated, interval; /* in jiffies */ 9506160327SDavide Rizzo char valid; /* zero until following fields are valid */ 9606160327SDavide Rizzo /* registers values */ 970f1deb4bSDavide Rizzo u8 temp[ARRAY_SIZE(lm95241_reg_address)]; 9806160327SDavide Rizzo u8 config, model, trutherm; 9906160327SDavide Rizzo }; 10006160327SDavide Rizzo 1010f1deb4bSDavide Rizzo /* Conversions */ 1020c2a40e2SGuenter Roeck static int temp_from_reg_signed(u8 val_h, u8 val_l) 1030f1deb4bSDavide Rizzo { 1040c2a40e2SGuenter Roeck s16 val_hl = (val_h << 8) | val_l; 1050c2a40e2SGuenter Roeck return val_hl * 1000 / 256; 1060c2a40e2SGuenter Roeck } 1070c2a40e2SGuenter Roeck 1080c2a40e2SGuenter Roeck static int temp_from_reg_unsigned(u8 val_h, u8 val_l) 1090c2a40e2SGuenter Roeck { 1100c2a40e2SGuenter Roeck u16 val_hl = (val_h << 8) | val_l; 1110c2a40e2SGuenter Roeck return val_hl * 1000 / 256; 11206160327SDavide Rizzo } 1130f1deb4bSDavide Rizzo 1140f1deb4bSDavide Rizzo static struct lm95241_data *lm95241_update_device(struct device *dev) 1150f1deb4bSDavide Rizzo { 1160f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 1170f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 1180f1deb4bSDavide Rizzo 1190f1deb4bSDavide Rizzo mutex_lock(&data->update_lock); 1200f1deb4bSDavide Rizzo 1210f1deb4bSDavide Rizzo if (time_after(jiffies, data->last_updated + data->interval) || 1220f1deb4bSDavide Rizzo !data->valid) { 1230f1deb4bSDavide Rizzo int i; 1240f1deb4bSDavide Rizzo 1250f1deb4bSDavide Rizzo dev_dbg(&client->dev, "Updating lm95241 data.\n"); 1260f1deb4bSDavide Rizzo for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++) 1270f1deb4bSDavide Rizzo data->temp[i] 1280f1deb4bSDavide Rizzo = i2c_smbus_read_byte_data(client, 1290f1deb4bSDavide Rizzo lm95241_reg_address[i]); 1300f1deb4bSDavide Rizzo data->last_updated = jiffies; 1310f1deb4bSDavide Rizzo data->valid = 1; 1320f1deb4bSDavide Rizzo } 1330f1deb4bSDavide Rizzo 1340f1deb4bSDavide Rizzo mutex_unlock(&data->update_lock); 1350f1deb4bSDavide Rizzo 1360f1deb4bSDavide Rizzo return data; 1370f1deb4bSDavide Rizzo } 1380f1deb4bSDavide Rizzo 1390f1deb4bSDavide Rizzo /* Sysfs stuff */ 1400f1deb4bSDavide Rizzo static ssize_t show_input(struct device *dev, struct device_attribute *attr, 1410f1deb4bSDavide Rizzo char *buf) 1420f1deb4bSDavide Rizzo { 1430f1deb4bSDavide Rizzo struct lm95241_data *data = lm95241_update_device(dev); 1440c2a40e2SGuenter Roeck int index = to_sensor_dev_attr(attr)->index; 1450f1deb4bSDavide Rizzo 1460f1deb4bSDavide Rizzo return snprintf(buf, PAGE_SIZE - 1, "%d\n", 1470c2a40e2SGuenter Roeck index == 0 || (data->config & (1 << (index / 2))) ? 1480c2a40e2SGuenter Roeck temp_from_reg_signed(data->temp[index], data->temp[index + 1]) : 1490c2a40e2SGuenter Roeck temp_from_reg_unsigned(data->temp[index], 1500c2a40e2SGuenter Roeck data->temp[index + 1])); 1510f1deb4bSDavide Rizzo } 1520f1deb4bSDavide Rizzo 1530f1deb4bSDavide Rizzo static ssize_t show_type(struct device *dev, struct device_attribute *attr, 1540f1deb4bSDavide Rizzo char *buf) 1550f1deb4bSDavide Rizzo { 1560f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 1570f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 1580f1deb4bSDavide Rizzo 1590f1deb4bSDavide Rizzo return snprintf(buf, PAGE_SIZE - 1, 1600f1deb4bSDavide Rizzo data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n"); 1610f1deb4bSDavide Rizzo } 1620f1deb4bSDavide Rizzo 1630f1deb4bSDavide Rizzo static ssize_t set_type(struct device *dev, struct device_attribute *attr, 1640f1deb4bSDavide Rizzo const char *buf, size_t count) 1650f1deb4bSDavide Rizzo { 1660f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 1670f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 1680f1deb4bSDavide Rizzo unsigned long val; 1690f1deb4bSDavide Rizzo int shift; 1700f1deb4bSDavide Rizzo u8 mask = to_sensor_dev_attr(attr)->index; 1710f1deb4bSDavide Rizzo 172179c4fdbSFrans Meulenbroeks if (kstrtoul(buf, 10, &val) < 0) 1730f1deb4bSDavide Rizzo return -EINVAL; 1740f1deb4bSDavide Rizzo if (val != 1 && val != 2) 1750f1deb4bSDavide Rizzo return -EINVAL; 1760f1deb4bSDavide Rizzo 1770f1deb4bSDavide Rizzo shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT; 1780f1deb4bSDavide Rizzo 1790f1deb4bSDavide Rizzo mutex_lock(&data->update_lock); 1800f1deb4bSDavide Rizzo 1810f1deb4bSDavide Rizzo data->trutherm &= ~(TT_MASK << shift); 1820f1deb4bSDavide Rizzo if (val == 1) { 1830f1deb4bSDavide Rizzo data->model |= mask; 1840f1deb4bSDavide Rizzo data->trutherm |= (TT_ON << shift); 1850f1deb4bSDavide Rizzo } else { 1860f1deb4bSDavide Rizzo data->model &= ~mask; 1870f1deb4bSDavide Rizzo data->trutherm |= (TT_OFF << shift); 1880f1deb4bSDavide Rizzo } 1890f1deb4bSDavide Rizzo data->valid = 0; 1900f1deb4bSDavide Rizzo 1910f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, 1920f1deb4bSDavide Rizzo data->model); 1930f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, 1940f1deb4bSDavide Rizzo data->trutherm); 1950f1deb4bSDavide Rizzo 1960f1deb4bSDavide Rizzo mutex_unlock(&data->update_lock); 1970f1deb4bSDavide Rizzo 1980f1deb4bSDavide Rizzo return count; 1990f1deb4bSDavide Rizzo } 2000f1deb4bSDavide Rizzo 2010f1deb4bSDavide Rizzo static ssize_t show_min(struct device *dev, struct device_attribute *attr, 2020f1deb4bSDavide Rizzo char *buf) 2030f1deb4bSDavide Rizzo { 2040f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 2050f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 2060f1deb4bSDavide Rizzo 2070f1deb4bSDavide Rizzo return snprintf(buf, PAGE_SIZE - 1, 2080f1deb4bSDavide Rizzo data->config & to_sensor_dev_attr(attr)->index ? 2090f1deb4bSDavide Rizzo "-127000\n" : "0\n"); 2100f1deb4bSDavide Rizzo } 2110f1deb4bSDavide Rizzo 2120f1deb4bSDavide Rizzo static ssize_t set_min(struct device *dev, struct device_attribute *attr, 2130f1deb4bSDavide Rizzo const char *buf, size_t count) 2140f1deb4bSDavide Rizzo { 2150f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 2160f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 2170f1deb4bSDavide Rizzo long val; 2180f1deb4bSDavide Rizzo 219179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val) < 0) 2200f1deb4bSDavide Rizzo return -EINVAL; 2210f1deb4bSDavide Rizzo if (val < -128000) 2220f1deb4bSDavide Rizzo return -EINVAL; 2230f1deb4bSDavide Rizzo 2240f1deb4bSDavide Rizzo mutex_lock(&data->update_lock); 2250f1deb4bSDavide Rizzo 2260f1deb4bSDavide Rizzo if (val < 0) 2270f1deb4bSDavide Rizzo data->config |= to_sensor_dev_attr(attr)->index; 2280f1deb4bSDavide Rizzo else 2290f1deb4bSDavide Rizzo data->config &= ~to_sensor_dev_attr(attr)->index; 2300f1deb4bSDavide Rizzo data->valid = 0; 2310f1deb4bSDavide Rizzo 2320f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); 2330f1deb4bSDavide Rizzo 2340f1deb4bSDavide Rizzo mutex_unlock(&data->update_lock); 2350f1deb4bSDavide Rizzo 2360f1deb4bSDavide Rizzo return count; 2370f1deb4bSDavide Rizzo } 2380f1deb4bSDavide Rizzo 2390f1deb4bSDavide Rizzo static ssize_t show_max(struct device *dev, struct device_attribute *attr, 2400f1deb4bSDavide Rizzo char *buf) 2410f1deb4bSDavide Rizzo { 2420f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 2430f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 2440f1deb4bSDavide Rizzo 2450f1deb4bSDavide Rizzo return snprintf(buf, PAGE_SIZE - 1, 2460f1deb4bSDavide Rizzo data->config & to_sensor_dev_attr(attr)->index ? 2470f1deb4bSDavide Rizzo "127000\n" : "255000\n"); 2480f1deb4bSDavide Rizzo } 2490f1deb4bSDavide Rizzo 2500f1deb4bSDavide Rizzo static ssize_t set_max(struct device *dev, struct device_attribute *attr, 2510f1deb4bSDavide Rizzo const char *buf, size_t count) 2520f1deb4bSDavide Rizzo { 2530f1deb4bSDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 2540f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 2550f1deb4bSDavide Rizzo long val; 2560f1deb4bSDavide Rizzo 257179c4fdbSFrans Meulenbroeks if (kstrtol(buf, 10, &val) < 0) 2580f1deb4bSDavide Rizzo return -EINVAL; 2590f1deb4bSDavide Rizzo if (val >= 256000) 2600f1deb4bSDavide Rizzo return -EINVAL; 2610f1deb4bSDavide Rizzo 2620f1deb4bSDavide Rizzo mutex_lock(&data->update_lock); 2630f1deb4bSDavide Rizzo 2640f1deb4bSDavide Rizzo if (val <= 127000) 2650f1deb4bSDavide Rizzo data->config |= to_sensor_dev_attr(attr)->index; 2660f1deb4bSDavide Rizzo else 2670f1deb4bSDavide Rizzo data->config &= ~to_sensor_dev_attr(attr)->index; 2680f1deb4bSDavide Rizzo data->valid = 0; 2690f1deb4bSDavide Rizzo 2700f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); 2710f1deb4bSDavide Rizzo 2720f1deb4bSDavide Rizzo mutex_unlock(&data->update_lock); 2730f1deb4bSDavide Rizzo 2740f1deb4bSDavide Rizzo return count; 2750f1deb4bSDavide Rizzo } 27606160327SDavide Rizzo 277bc482bf0SGuenter Roeck static ssize_t show_interval(struct device *dev, struct device_attribute *attr, 27806160327SDavide Rizzo char *buf) 27906160327SDavide Rizzo { 28006160327SDavide Rizzo struct lm95241_data *data = lm95241_update_device(dev); 28106160327SDavide Rizzo 2820f1deb4bSDavide Rizzo return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval 2830f1deb4bSDavide Rizzo / HZ); 28406160327SDavide Rizzo } 28506160327SDavide Rizzo 286bc482bf0SGuenter Roeck static ssize_t set_interval(struct device *dev, struct device_attribute *attr, 28706160327SDavide Rizzo const char *buf, size_t count) 28806160327SDavide Rizzo { 28906160327SDavide Rizzo struct i2c_client *client = to_i2c_client(dev); 29006160327SDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 29161ec2da5SJean Delvare unsigned long val; 29206160327SDavide Rizzo 293179c4fdbSFrans Meulenbroeks if (kstrtoul(buf, 10, &val) < 0) 29461ec2da5SJean Delvare return -EINVAL; 29561ec2da5SJean Delvare 29661ec2da5SJean Delvare data->interval = val * HZ / 1000; 29706160327SDavide Rizzo 29806160327SDavide Rizzo return count; 29906160327SDavide Rizzo } 30006160327SDavide Rizzo 3010f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0); 3020f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2); 3030f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4); 3040f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type, 3050f1deb4bSDavide Rizzo R1MS_MASK); 3060f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type, 3070f1deb4bSDavide Rizzo R2MS_MASK); 3080f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min, 3090f1deb4bSDavide Rizzo R1DF_MASK); 3100f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min, 3110f1deb4bSDavide Rizzo R2DF_MASK); 3120f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max, 3130f1deb4bSDavide Rizzo R1DF_MASK); 3140f1deb4bSDavide Rizzo static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max, 3150f1deb4bSDavide Rizzo R2DF_MASK); 316bc482bf0SGuenter Roeck static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval, 317bc482bf0SGuenter Roeck set_interval); 31806160327SDavide Rizzo 31906160327SDavide Rizzo static struct attribute *lm95241_attributes[] = { 3200f1deb4bSDavide Rizzo &sensor_dev_attr_temp1_input.dev_attr.attr, 3210f1deb4bSDavide Rizzo &sensor_dev_attr_temp2_input.dev_attr.attr, 3220f1deb4bSDavide Rizzo &sensor_dev_attr_temp3_input.dev_attr.attr, 3230f1deb4bSDavide Rizzo &sensor_dev_attr_temp2_type.dev_attr.attr, 3240f1deb4bSDavide Rizzo &sensor_dev_attr_temp3_type.dev_attr.attr, 3250f1deb4bSDavide Rizzo &sensor_dev_attr_temp2_min.dev_attr.attr, 3260f1deb4bSDavide Rizzo &sensor_dev_attr_temp3_min.dev_attr.attr, 3270f1deb4bSDavide Rizzo &sensor_dev_attr_temp2_max.dev_attr.attr, 3280f1deb4bSDavide Rizzo &sensor_dev_attr_temp3_max.dev_attr.attr, 329bc482bf0SGuenter Roeck &dev_attr_update_interval.attr, 33006160327SDavide Rizzo NULL 33106160327SDavide Rizzo }; 33206160327SDavide Rizzo 33306160327SDavide Rizzo static const struct attribute_group lm95241_group = { 33406160327SDavide Rizzo .attrs = lm95241_attributes, 33506160327SDavide Rizzo }; 33606160327SDavide Rizzo 337797eaa4bSJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */ 338310ec792SJean Delvare static int lm95241_detect(struct i2c_client *new_client, 339797eaa4bSJean Delvare struct i2c_board_info *info) 34006160327SDavide Rizzo { 341797eaa4bSJean Delvare struct i2c_adapter *adapter = new_client->adapter; 34252df6440SJean Delvare const char *name; 3438c1d0419SGuenter Roeck int mfg_id, chip_id; 34406160327SDavide Rizzo 34506160327SDavide Rizzo if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 346797eaa4bSJean Delvare return -ENODEV; 34706160327SDavide Rizzo 3488c1d0419SGuenter Roeck mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID); 3498c1d0419SGuenter Roeck if (mfg_id != NATSEMI_MAN_ID) 3508c1d0419SGuenter Roeck return -ENODEV; 3518c1d0419SGuenter Roeck 3528c1d0419SGuenter Roeck chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID); 3538c1d0419SGuenter Roeck switch (chip_id) { 3548c1d0419SGuenter Roeck case LM95231_CHIP_ID: 3558c1d0419SGuenter Roeck name = "lm95231"; 3568c1d0419SGuenter Roeck break; 3578c1d0419SGuenter Roeck case LM95241_CHIP_ID: 3588c1d0419SGuenter Roeck name = "lm95241"; 3598c1d0419SGuenter Roeck break; 3608c1d0419SGuenter Roeck default: 361797eaa4bSJean Delvare return -ENODEV; 36206160327SDavide Rizzo } 36306160327SDavide Rizzo 364797eaa4bSJean Delvare /* Fill the i2c board info */ 365797eaa4bSJean Delvare strlcpy(info->type, name, I2C_NAME_SIZE); 366797eaa4bSJean Delvare return 0; 367797eaa4bSJean Delvare } 36806160327SDavide Rizzo 3690f1deb4bSDavide Rizzo static void lm95241_init_client(struct i2c_client *client) 3700f1deb4bSDavide Rizzo { 3710f1deb4bSDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 3720f1deb4bSDavide Rizzo 3730f1deb4bSDavide Rizzo data->interval = HZ; /* 1 sec default */ 3740f1deb4bSDavide Rizzo data->valid = 0; 3750f1deb4bSDavide Rizzo data->config = CFG_CR0076; 3760f1deb4bSDavide Rizzo data->model = 0; 3770f1deb4bSDavide Rizzo data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT); 3780f1deb4bSDavide Rizzo 3790f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); 3800f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER, 3810f1deb4bSDavide Rizzo R1FE_MASK | R2FE_MASK); 3820f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, 3830f1deb4bSDavide Rizzo data->trutherm); 3840f1deb4bSDavide Rizzo i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, 3850f1deb4bSDavide Rizzo data->model); 3860f1deb4bSDavide Rizzo } 3870f1deb4bSDavide Rizzo 388797eaa4bSJean Delvare static int lm95241_probe(struct i2c_client *new_client, 389797eaa4bSJean Delvare const struct i2c_device_id *id) 390797eaa4bSJean Delvare { 391797eaa4bSJean Delvare struct lm95241_data *data; 392797eaa4bSJean Delvare int err; 393797eaa4bSJean Delvare 394*1487bf70SGuenter Roeck data = devm_kzalloc(&new_client->dev, sizeof(struct lm95241_data), 395*1487bf70SGuenter Roeck GFP_KERNEL); 396*1487bf70SGuenter Roeck if (!data) 397*1487bf70SGuenter Roeck return -ENOMEM; 398797eaa4bSJean Delvare 399797eaa4bSJean Delvare i2c_set_clientdata(new_client, data); 40006160327SDavide Rizzo mutex_init(&data->update_lock); 40106160327SDavide Rizzo 40206160327SDavide Rizzo /* Initialize the LM95241 chip */ 40306160327SDavide Rizzo lm95241_init_client(new_client); 40406160327SDavide Rizzo 40506160327SDavide Rizzo /* Register sysfs hooks */ 40606160327SDavide Rizzo err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group); 40706160327SDavide Rizzo if (err) 408*1487bf70SGuenter Roeck return err; 40906160327SDavide Rizzo 41006160327SDavide Rizzo data->hwmon_dev = hwmon_device_register(&new_client->dev); 41106160327SDavide Rizzo if (IS_ERR(data->hwmon_dev)) { 41206160327SDavide Rizzo err = PTR_ERR(data->hwmon_dev); 41306160327SDavide Rizzo goto exit_remove_files; 41406160327SDavide Rizzo } 41506160327SDavide Rizzo 41606160327SDavide Rizzo return 0; 41706160327SDavide Rizzo 41806160327SDavide Rizzo exit_remove_files: 41906160327SDavide Rizzo sysfs_remove_group(&new_client->dev.kobj, &lm95241_group); 42006160327SDavide Rizzo return err; 42106160327SDavide Rizzo } 42206160327SDavide Rizzo 423797eaa4bSJean Delvare static int lm95241_remove(struct i2c_client *client) 42406160327SDavide Rizzo { 42506160327SDavide Rizzo struct lm95241_data *data = i2c_get_clientdata(client); 42606160327SDavide Rizzo 42706160327SDavide Rizzo hwmon_device_unregister(data->hwmon_dev); 42806160327SDavide Rizzo sysfs_remove_group(&client->dev.kobj, &lm95241_group); 42906160327SDavide Rizzo 43006160327SDavide Rizzo return 0; 43106160327SDavide Rizzo } 43206160327SDavide Rizzo 433797eaa4bSJean Delvare /* Driver data (common to all clients) */ 434797eaa4bSJean Delvare static const struct i2c_device_id lm95241_id[] = { 4358c1d0419SGuenter Roeck { "lm95231", 0 }, 4368c1d0419SGuenter Roeck { "lm95241", 0 }, 437797eaa4bSJean Delvare { } 438797eaa4bSJean Delvare }; 439797eaa4bSJean Delvare MODULE_DEVICE_TABLE(i2c, lm95241_id); 440797eaa4bSJean Delvare 441797eaa4bSJean Delvare static struct i2c_driver lm95241_driver = { 442797eaa4bSJean Delvare .class = I2C_CLASS_HWMON, 443797eaa4bSJean Delvare .driver = { 4440f1deb4bSDavide Rizzo .name = DEVNAME, 445797eaa4bSJean Delvare }, 446797eaa4bSJean Delvare .probe = lm95241_probe, 447797eaa4bSJean Delvare .remove = lm95241_remove, 448797eaa4bSJean Delvare .id_table = lm95241_id, 449797eaa4bSJean Delvare .detect = lm95241_detect, 450c3813d6aSJean Delvare .address_list = normal_i2c, 451797eaa4bSJean Delvare }; 452797eaa4bSJean Delvare 453f0967eeaSAxel Lin module_i2c_driver(lm95241_driver); 45406160327SDavide Rizzo 4550f1deb4bSDavide Rizzo MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>"); 45606160327SDavide Rizzo MODULE_DESCRIPTION("LM95241 sensor driver"); 45706160327SDavide Rizzo MODULE_LICENSE("GPL"); 458