| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 1 | /* | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com> | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 3 | * | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 4 | * The LM95241 is a sensor chip made by National Semiconductors. | 
|  | 5 | * It reports up to three temperatures (its own plus up to two external ones). | 
|  | 6 | * Complete datasheet can be obtained from National's website at: | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 7 | *   http://www.national.com/ds.cgi/LM/LM95241.pdf | 
|  | 8 | * | 
|  | 9 | * This program is free software; you can redistribute it and/or modify | 
|  | 10 | * it under the terms of the GNU General Public License as published by | 
|  | 11 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 12 | * (at your option) any later version. | 
|  | 13 | * | 
|  | 14 | * This program is distributed in the hope that it will be useful, | 
|  | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | * GNU General Public License for more details. | 
|  | 18 | * | 
|  | 19 | * You should have received a copy of the GNU General Public License | 
|  | 20 | * along with this program; if not, write to the Free Software | 
|  | 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 22 | */ | 
|  | 23 |  | 
|  | 24 | #include <linux/module.h> | 
|  | 25 | #include <linux/init.h> | 
|  | 26 | #include <linux/slab.h> | 
|  | 27 | #include <linux/jiffies.h> | 
|  | 28 | #include <linux/i2c.h> | 
|  | 29 | #include <linux/hwmon.h> | 
|  | 30 | #include <linux/hwmon-sysfs.h> | 
|  | 31 | #include <linux/err.h> | 
|  | 32 | #include <linux/mutex.h> | 
|  | 33 | #include <linux/sysfs.h> | 
|  | 34 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 35 | #define DEVNAME "lm95241" | 
|  | 36 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 37 | static const unsigned short normal_i2c[] = { | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 38 | 0x19, 0x2a, 0x2b, I2C_CLIENT_END }; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 39 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 40 | /* LM95241 registers */ | 
|  | 41 | #define LM95241_REG_R_MAN_ID		0xFE | 
|  | 42 | #define LM95241_REG_R_CHIP_ID		0xFF | 
|  | 43 | #define LM95241_REG_R_STATUS		0x02 | 
|  | 44 | #define LM95241_REG_RW_CONFIG		0x03 | 
|  | 45 | #define LM95241_REG_RW_REM_FILTER	0x06 | 
|  | 46 | #define LM95241_REG_RW_TRUTHERM		0x07 | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 47 | #define LM95241_REG_W_ONE_SHOT		0x0F | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 48 | #define LM95241_REG_R_LOCAL_TEMPH	0x10 | 
|  | 49 | #define LM95241_REG_R_REMOTE1_TEMPH	0x11 | 
|  | 50 | #define LM95241_REG_R_REMOTE2_TEMPH	0x12 | 
|  | 51 | #define LM95241_REG_R_LOCAL_TEMPL	0x20 | 
|  | 52 | #define LM95241_REG_R_REMOTE1_TEMPL	0x21 | 
|  | 53 | #define LM95241_REG_R_REMOTE2_TEMPL	0x22 | 
|  | 54 | #define LM95241_REG_RW_REMOTE_MODEL	0x30 | 
|  | 55 |  | 
|  | 56 | /* LM95241 specific bitfields */ | 
|  | 57 | #define CFG_STOP 0x40 | 
|  | 58 | #define CFG_CR0076 0x00 | 
|  | 59 | #define CFG_CR0182 0x10 | 
|  | 60 | #define CFG_CR1000 0x20 | 
|  | 61 | #define CFG_CR2700 0x30 | 
|  | 62 | #define R1MS_SHIFT 0 | 
|  | 63 | #define R2MS_SHIFT 2 | 
|  | 64 | #define R1MS_MASK (0x01 << (R1MS_SHIFT)) | 
|  | 65 | #define R2MS_MASK (0x01 << (R2MS_SHIFT)) | 
|  | 66 | #define R1DF_SHIFT 1 | 
|  | 67 | #define R2DF_SHIFT 2 | 
|  | 68 | #define R1DF_MASK (0x01 << (R1DF_SHIFT)) | 
|  | 69 | #define R2DF_MASK (0x01 << (R2DF_SHIFT)) | 
|  | 70 | #define R1FE_MASK 0x01 | 
|  | 71 | #define R2FE_MASK 0x05 | 
|  | 72 | #define TT1_SHIFT 0 | 
|  | 73 | #define TT2_SHIFT 4 | 
|  | 74 | #define TT_OFF 0 | 
|  | 75 | #define TT_ON 1 | 
|  | 76 | #define TT_MASK 7 | 
| Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 77 | #define NATSEMI_MAN_ID	0x01 | 
|  | 78 | #define LM95231_CHIP_ID	0xA1 | 
|  | 79 | #define LM95241_CHIP_ID	0xA4 | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 80 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 81 | static const u8 lm95241_reg_address[] = { | 
|  | 82 | LM95241_REG_R_LOCAL_TEMPH, | 
|  | 83 | LM95241_REG_R_LOCAL_TEMPL, | 
|  | 84 | LM95241_REG_R_REMOTE1_TEMPH, | 
|  | 85 | LM95241_REG_R_REMOTE1_TEMPL, | 
|  | 86 | LM95241_REG_R_REMOTE2_TEMPH, | 
|  | 87 | LM95241_REG_R_REMOTE2_TEMPL | 
|  | 88 | }; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 89 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 90 | /* Client data (each client gets its own) */ | 
|  | 91 | struct lm95241_data { | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 92 | struct device *hwmon_dev; | 
|  | 93 | struct mutex update_lock; | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 94 | unsigned long last_updated, interval;	/* in jiffies */ | 
|  | 95 | char valid;		/* zero until following fields are valid */ | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 96 | /* registers values */ | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 97 | u8 temp[ARRAY_SIZE(lm95241_reg_address)]; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 98 | u8 config, model, trutherm; | 
|  | 99 | }; | 
|  | 100 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 101 | /* Conversions */ | 
| Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 102 | static int temp_from_reg_signed(u8 val_h, u8 val_l) | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 103 | { | 
| Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 104 | s16 val_hl = (val_h << 8) | val_l; | 
|  | 105 | return val_hl * 1000 / 256; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | static int temp_from_reg_unsigned(u8 val_h, u8 val_l) | 
|  | 109 | { | 
|  | 110 | u16 val_hl = (val_h << 8) | val_l; | 
|  | 111 | return val_hl * 1000 / 256; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 112 | } | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 113 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 114 | static struct lm95241_data *lm95241_update_device(struct device *dev) | 
|  | 115 | { | 
|  | 116 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 117 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 118 |  | 
|  | 119 | mutex_lock(&data->update_lock); | 
|  | 120 |  | 
|  | 121 | if (time_after(jiffies, data->last_updated + data->interval) || | 
|  | 122 | !data->valid) { | 
|  | 123 | int i; | 
|  | 124 |  | 
|  | 125 | dev_dbg(&client->dev, "Updating lm95241 data.\n"); | 
|  | 126 | for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++) | 
|  | 127 | data->temp[i] | 
|  | 128 | = i2c_smbus_read_byte_data(client, | 
|  | 129 | lm95241_reg_address[i]); | 
|  | 130 | data->last_updated = jiffies; | 
|  | 131 | data->valid = 1; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | mutex_unlock(&data->update_lock); | 
|  | 135 |  | 
|  | 136 | return data; | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | /* Sysfs stuff */ | 
|  | 140 | static ssize_t show_input(struct device *dev, struct device_attribute *attr, | 
|  | 141 | char *buf) | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 142 | { | 
|  | 143 | struct lm95241_data *data = lm95241_update_device(dev); | 
| Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 144 | int index = to_sensor_dev_attr(attr)->index; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 145 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 146 | return snprintf(buf, PAGE_SIZE - 1, "%d\n", | 
| Guenter Roeck | 0c2a40e | 2011-06-30 02:09:37 -0700 | [diff] [blame] | 147 | index == 0 || (data->config & (1 << (index / 2))) ? | 
|  | 148 | temp_from_reg_signed(data->temp[index], data->temp[index + 1]) : | 
|  | 149 | temp_from_reg_unsigned(data->temp[index], | 
|  | 150 | data->temp[index + 1])); | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
|  | 153 | static ssize_t show_type(struct device *dev, struct device_attribute *attr, | 
|  | 154 | char *buf) | 
|  | 155 | { | 
|  | 156 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 157 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 158 |  | 
|  | 159 | return snprintf(buf, PAGE_SIZE - 1, | 
|  | 160 | data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n"); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | static ssize_t set_type(struct device *dev, struct device_attribute *attr, | 
|  | 164 | const char *buf, size_t count) | 
|  | 165 | { | 
|  | 166 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 167 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 168 | unsigned long val; | 
|  | 169 | int shift; | 
|  | 170 | u8 mask = to_sensor_dev_attr(attr)->index; | 
|  | 171 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 172 | if (kstrtoul(buf, 10, &val) < 0) | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 173 | return -EINVAL; | 
|  | 174 | if (val != 1 && val != 2) | 
|  | 175 | return -EINVAL; | 
|  | 176 |  | 
|  | 177 | shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT; | 
|  | 178 |  | 
|  | 179 | mutex_lock(&data->update_lock); | 
|  | 180 |  | 
|  | 181 | data->trutherm &= ~(TT_MASK << shift); | 
|  | 182 | if (val == 1) { | 
|  | 183 | data->model |= mask; | 
|  | 184 | data->trutherm |= (TT_ON << shift); | 
|  | 185 | } else { | 
|  | 186 | data->model &= ~mask; | 
|  | 187 | data->trutherm |= (TT_OFF << shift); | 
|  | 188 | } | 
|  | 189 | data->valid = 0; | 
|  | 190 |  | 
|  | 191 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, | 
|  | 192 | data->model); | 
|  | 193 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, | 
|  | 194 | data->trutherm); | 
|  | 195 |  | 
|  | 196 | mutex_unlock(&data->update_lock); | 
|  | 197 |  | 
|  | 198 | return count; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | static ssize_t show_min(struct device *dev, struct device_attribute *attr, | 
|  | 202 | char *buf) | 
|  | 203 | { | 
|  | 204 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 205 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 206 |  | 
|  | 207 | return snprintf(buf, PAGE_SIZE - 1, | 
|  | 208 | data->config & to_sensor_dev_attr(attr)->index ? | 
|  | 209 | "-127000\n" : "0\n"); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | static ssize_t set_min(struct device *dev, struct device_attribute *attr, | 
|  | 213 | const char *buf, size_t count) | 
|  | 214 | { | 
|  | 215 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 216 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 217 | long val; | 
|  | 218 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 219 | if (kstrtol(buf, 10, &val) < 0) | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 220 | return -EINVAL; | 
|  | 221 | if (val < -128000) | 
|  | 222 | return -EINVAL; | 
|  | 223 |  | 
|  | 224 | mutex_lock(&data->update_lock); | 
|  | 225 |  | 
|  | 226 | if (val < 0) | 
|  | 227 | data->config |= to_sensor_dev_attr(attr)->index; | 
|  | 228 | else | 
|  | 229 | data->config &= ~to_sensor_dev_attr(attr)->index; | 
|  | 230 | data->valid = 0; | 
|  | 231 |  | 
|  | 232 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); | 
|  | 233 |  | 
|  | 234 | mutex_unlock(&data->update_lock); | 
|  | 235 |  | 
|  | 236 | return count; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | static ssize_t show_max(struct device *dev, struct device_attribute *attr, | 
|  | 240 | char *buf) | 
|  | 241 | { | 
|  | 242 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 243 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 244 |  | 
|  | 245 | return snprintf(buf, PAGE_SIZE - 1, | 
|  | 246 | data->config & to_sensor_dev_attr(attr)->index ? | 
|  | 247 | "127000\n" : "255000\n"); | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | static ssize_t set_max(struct device *dev, struct device_attribute *attr, | 
|  | 251 | const char *buf, size_t count) | 
|  | 252 | { | 
|  | 253 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 254 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 255 | long val; | 
|  | 256 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 257 | if (kstrtol(buf, 10, &val) < 0) | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 258 | return -EINVAL; | 
|  | 259 | if (val >= 256000) | 
|  | 260 | return -EINVAL; | 
|  | 261 |  | 
|  | 262 | mutex_lock(&data->update_lock); | 
|  | 263 |  | 
|  | 264 | if (val <= 127000) | 
|  | 265 | data->config |= to_sensor_dev_attr(attr)->index; | 
|  | 266 | else | 
|  | 267 | data->config &= ~to_sensor_dev_attr(attr)->index; | 
|  | 268 | data->valid = 0; | 
|  | 269 |  | 
|  | 270 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); | 
|  | 271 |  | 
|  | 272 | mutex_unlock(&data->update_lock); | 
|  | 273 |  | 
|  | 274 | return count; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | static ssize_t show_interval(struct device *dev, struct device_attribute *attr, | 
|  | 278 | char *buf) | 
|  | 279 | { | 
|  | 280 | struct lm95241_data *data = lm95241_update_device(dev); | 
|  | 281 |  | 
|  | 282 | return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval | 
|  | 283 | / HZ); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| Guenter Roeck | bc482bf | 2010-09-17 17:24:15 +0200 | [diff] [blame] | 286 | static ssize_t set_interval(struct device *dev, struct device_attribute *attr, | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 287 | const char *buf, size_t count) | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 288 | { | 
|  | 289 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 290 | struct lm95241_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | 61ec2da | 2010-11-15 21:38:56 +0100 | [diff] [blame] | 291 | unsigned long val; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 292 |  | 
| Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 293 | if (kstrtoul(buf, 10, &val) < 0) | 
| Jean Delvare | 61ec2da | 2010-11-15 21:38:56 +0100 | [diff] [blame] | 294 | return -EINVAL; | 
|  | 295 |  | 
|  | 296 | data->interval = val * HZ / 1000; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 297 |  | 
|  | 298 | return count; | 
|  | 299 | } | 
|  | 300 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 301 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0); | 
|  | 302 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2); | 
|  | 303 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4); | 
|  | 304 | static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type, | 
|  | 305 | R1MS_MASK); | 
|  | 306 | static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type, | 
|  | 307 | R2MS_MASK); | 
|  | 308 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min, | 
|  | 309 | R1DF_MASK); | 
|  | 310 | static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min, | 
|  | 311 | R2DF_MASK); | 
|  | 312 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max, | 
|  | 313 | R1DF_MASK); | 
|  | 314 | static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max, | 
|  | 315 | R2DF_MASK); | 
| Guenter Roeck | bc482bf | 2010-09-17 17:24:15 +0200 | [diff] [blame] | 316 | static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval, | 
|  | 317 | set_interval); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 318 |  | 
|  | 319 | static struct attribute *lm95241_attributes[] = { | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 320 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 321 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 
|  | 322 | &sensor_dev_attr_temp3_input.dev_attr.attr, | 
|  | 323 | &sensor_dev_attr_temp2_type.dev_attr.attr, | 
|  | 324 | &sensor_dev_attr_temp3_type.dev_attr.attr, | 
|  | 325 | &sensor_dev_attr_temp2_min.dev_attr.attr, | 
|  | 326 | &sensor_dev_attr_temp3_min.dev_attr.attr, | 
|  | 327 | &sensor_dev_attr_temp2_max.dev_attr.attr, | 
|  | 328 | &sensor_dev_attr_temp3_max.dev_attr.attr, | 
| Guenter Roeck | bc482bf | 2010-09-17 17:24:15 +0200 | [diff] [blame] | 329 | &dev_attr_update_interval.attr, | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 330 | NULL | 
|  | 331 | }; | 
|  | 332 |  | 
|  | 333 | static const struct attribute_group lm95241_group = { | 
|  | 334 | .attrs = lm95241_attributes, | 
|  | 335 | }; | 
|  | 336 |  | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 337 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 338 | static int lm95241_detect(struct i2c_client *new_client, | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 339 | struct i2c_board_info *info) | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 340 | { | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 341 | struct i2c_adapter *adapter = new_client->adapter; | 
| Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 342 | const char *name; | 
| Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 343 | int mfg_id, chip_id; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 344 |  | 
|  | 345 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 346 | return -ENODEV; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 347 |  | 
| Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 348 | mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID); | 
|  | 349 | if (mfg_id != NATSEMI_MAN_ID) | 
|  | 350 | return -ENODEV; | 
|  | 351 |  | 
|  | 352 | chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID); | 
|  | 353 | switch (chip_id) { | 
|  | 354 | case LM95231_CHIP_ID: | 
|  | 355 | name = "lm95231"; | 
|  | 356 | break; | 
|  | 357 | case LM95241_CHIP_ID: | 
|  | 358 | name = "lm95241"; | 
|  | 359 | break; | 
|  | 360 | default: | 
| Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 361 | return -ENODEV; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 364 | /* Fill the i2c board info */ | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 365 | strlcpy(info->type, name, I2C_NAME_SIZE); | 
|  | 366 | return 0; | 
|  | 367 | } | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 368 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 369 | static void lm95241_init_client(struct i2c_client *client) | 
|  | 370 | { | 
|  | 371 | struct lm95241_data *data = i2c_get_clientdata(client); | 
|  | 372 |  | 
|  | 373 | data->interval = HZ;	/* 1 sec default */ | 
|  | 374 | data->valid = 0; | 
|  | 375 | data->config = CFG_CR0076; | 
|  | 376 | data->model = 0; | 
|  | 377 | data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT); | 
|  | 378 |  | 
|  | 379 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); | 
|  | 380 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER, | 
|  | 381 | R1FE_MASK | R2FE_MASK); | 
|  | 382 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, | 
|  | 383 | data->trutherm); | 
|  | 384 | i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, | 
|  | 385 | data->model); | 
|  | 386 | } | 
|  | 387 |  | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 388 | static int lm95241_probe(struct i2c_client *new_client, | 
|  | 389 | const struct i2c_device_id *id) | 
|  | 390 | { | 
|  | 391 | struct lm95241_data *data; | 
|  | 392 | int err; | 
|  | 393 |  | 
|  | 394 | data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL); | 
|  | 395 | if (!data) { | 
|  | 396 | err = -ENOMEM; | 
|  | 397 | goto exit; | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | i2c_set_clientdata(new_client, data); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 401 | mutex_init(&data->update_lock); | 
|  | 402 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 403 | /* Initialize the LM95241 chip */ | 
|  | 404 | lm95241_init_client(new_client); | 
|  | 405 |  | 
|  | 406 | /* Register sysfs hooks */ | 
|  | 407 | err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group); | 
|  | 408 | if (err) | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 409 | goto exit_free; | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 410 |  | 
|  | 411 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
|  | 412 | if (IS_ERR(data->hwmon_dev)) { | 
|  | 413 | err = PTR_ERR(data->hwmon_dev); | 
|  | 414 | goto exit_remove_files; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | return 0; | 
|  | 418 |  | 
|  | 419 | exit_remove_files: | 
|  | 420 | sysfs_remove_group(&new_client->dev.kobj, &lm95241_group); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 421 | exit_free: | 
|  | 422 | kfree(data); | 
|  | 423 | exit: | 
|  | 424 | return err; | 
|  | 425 | } | 
|  | 426 |  | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 427 | static int lm95241_remove(struct i2c_client *client) | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 428 | { | 
|  | 429 | struct lm95241_data *data = i2c_get_clientdata(client); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 430 |  | 
|  | 431 | hwmon_device_unregister(data->hwmon_dev); | 
|  | 432 | sysfs_remove_group(&client->dev.kobj, &lm95241_group); | 
|  | 433 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 434 | kfree(data); | 
|  | 435 | return 0; | 
|  | 436 | } | 
|  | 437 |  | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 438 | /* Driver data (common to all clients) */ | 
|  | 439 | static const struct i2c_device_id lm95241_id[] = { | 
| Guenter Roeck | 8c1d041 | 2011-07-05 13:31:48 -0700 | [diff] [blame] | 440 | { "lm95231", 0 }, | 
|  | 441 | { "lm95241", 0 }, | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 442 | { } | 
|  | 443 | }; | 
|  | 444 | MODULE_DEVICE_TABLE(i2c, lm95241_id); | 
|  | 445 |  | 
|  | 446 | static struct i2c_driver lm95241_driver = { | 
|  | 447 | .class		= I2C_CLASS_HWMON, | 
|  | 448 | .driver = { | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 449 | .name	= DEVNAME, | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 450 | }, | 
|  | 451 | .probe		= lm95241_probe, | 
|  | 452 | .remove		= lm95241_remove, | 
|  | 453 | .id_table	= lm95241_id, | 
|  | 454 | .detect		= lm95241_detect, | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 455 | .address_list	= normal_i2c, | 
| Jean Delvare | 797eaa4 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 456 | }; | 
|  | 457 |  | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 458 | static int __init sensors_lm95241_init(void) | 
|  | 459 | { | 
|  | 460 | return i2c_add_driver(&lm95241_driver); | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | static void __exit sensors_lm95241_exit(void) | 
|  | 464 | { | 
|  | 465 | i2c_del_driver(&lm95241_driver); | 
|  | 466 | } | 
|  | 467 |  | 
| Davide Rizzo | 0f1deb4 | 2010-11-18 07:23:00 -0800 | [diff] [blame] | 468 | MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>"); | 
| Davide Rizzo | 0616032 | 2009-03-31 15:24:27 -0700 | [diff] [blame] | 469 | MODULE_DESCRIPTION("LM95241 sensor driver"); | 
|  | 470 | MODULE_LICENSE("GPL"); | 
|  | 471 |  | 
|  | 472 | module_init(sensors_lm95241_init); | 
|  | 473 | module_exit(sensors_lm95241_exit); |