Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm90.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Based on the lm83 driver. The LM90 is a sensor chip made by National |
| 7 | * Semiconductor. It reports up to two temperatures (its own plus up to |
| 8 | * one external one) with a 0.125 deg resolution (1 deg for local |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 9 | * temperature) and a 3-4 deg accuracy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This driver also supports the LM89 and LM99, two other sensor chips |
| 12 | * made by National Semiconductor. Both have an increased remote |
| 13 | * temperature measurement accuracy (1 degree), and the LM99 |
| 14 | * additionally shifts remote temperatures (measured and limits) by 16 |
| 15 | * degrees, which allows for higher temperatures measurement. The |
| 16 | * driver doesn't handle it since it can be done easily in user-space. |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 17 | * Note that there is no way to differentiate between both chips. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
| 19 | * This driver also supports the LM86, another sensor chip made by |
| 20 | * National Semiconductor. It is exactly similar to the LM90 except it |
| 21 | * has a higher accuracy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
| 23 | * This driver also supports the ADM1032, a sensor chip made by Analog |
| 24 | * Devices. That chip is similar to the LM90, with a few differences |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 25 | * that are not handled by this driver. Among others, it has a higher |
| 26 | * accuracy than the LM90, much like the LM86 does. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * |
| 28 | * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 29 | * chips made by Maxim. These chips are similar to the LM86. |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 30 | * Note that there is no easy way to differentiate between the three |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * variants. The extra address and features of the MAX6659 are not |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 32 | * supported by this driver. These chips lack the remote temperature |
| 33 | * offset feature. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 35 | * This driver also supports the MAX6680 and MAX6681, two other sensor |
| 36 | * chips made by Maxim. These are quite similar to the other Maxim |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 37 | * chips. The MAX6680 and MAX6681 only differ in the pinout so they can |
| 38 | * be treated identically. |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 39 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * This driver also supports the ADT7461 chip from Analog Devices but |
| 41 | * only in its "compatability mode". If an ADT7461 chip is found but |
| 42 | * is configured in non-compatible mode (where its temperature |
| 43 | * register values are decoded differently) it is ignored by this |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 44 | * driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * |
| 46 | * Since the LM90 was the first chipset supported by this driver, most |
| 47 | * comments will refer to this chipset, but are actually general and |
| 48 | * concern all supported chipsets, unless mentioned otherwise. |
| 49 | * |
| 50 | * This program is free software; you can redistribute it and/or modify |
| 51 | * it under the terms of the GNU General Public License as published by |
| 52 | * the Free Software Foundation; either version 2 of the License, or |
| 53 | * (at your option) any later version. |
| 54 | * |
| 55 | * This program is distributed in the hope that it will be useful, |
| 56 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 57 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 58 | * GNU General Public License for more details. |
| 59 | * |
| 60 | * You should have received a copy of the GNU General Public License |
| 61 | * along with this program; if not, write to the Free Software |
| 62 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 63 | */ |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/module.h> |
| 66 | #include <linux/init.h> |
| 67 | #include <linux/slab.h> |
| 68 | #include <linux/jiffies.h> |
| 69 | #include <linux/i2c.h> |
Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 70 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 71 | #include <linux/hwmon.h> |
| 72 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 73 | #include <linux/mutex.h> |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 74 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Addresses to scan |
| 78 | * Address is fully defined internally and cannot be changed except for |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 79 | * MAX6659, MAX6680 and MAX6681. |
Jean Delvare | 90209b4 | 2005-10-26 22:20:21 +0200 | [diff] [blame] | 80 | * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658 |
| 81 | * have address 0x4c. |
| 82 | * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported). |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 84 | * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, |
| 85 | * 0x4c, 0x4d or 0x4e. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | */ |
| 87 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 88 | static const unsigned short normal_i2c[] = { |
| 89 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Insmod parameters |
| 93 | */ |
| 94 | |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 95 | I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * The LM90 registers |
| 99 | */ |
| 100 | |
| 101 | #define LM90_REG_R_MAN_ID 0xFE |
| 102 | #define LM90_REG_R_CHIP_ID 0xFF |
| 103 | #define LM90_REG_R_CONFIG1 0x03 |
| 104 | #define LM90_REG_W_CONFIG1 0x09 |
| 105 | #define LM90_REG_R_CONFIG2 0xBF |
| 106 | #define LM90_REG_W_CONFIG2 0xBF |
| 107 | #define LM90_REG_R_CONVRATE 0x04 |
| 108 | #define LM90_REG_W_CONVRATE 0x0A |
| 109 | #define LM90_REG_R_STATUS 0x02 |
| 110 | #define LM90_REG_R_LOCAL_TEMP 0x00 |
| 111 | #define LM90_REG_R_LOCAL_HIGH 0x05 |
| 112 | #define LM90_REG_W_LOCAL_HIGH 0x0B |
| 113 | #define LM90_REG_R_LOCAL_LOW 0x06 |
| 114 | #define LM90_REG_W_LOCAL_LOW 0x0C |
| 115 | #define LM90_REG_R_LOCAL_CRIT 0x20 |
| 116 | #define LM90_REG_W_LOCAL_CRIT 0x20 |
| 117 | #define LM90_REG_R_REMOTE_TEMPH 0x01 |
| 118 | #define LM90_REG_R_REMOTE_TEMPL 0x10 |
| 119 | #define LM90_REG_R_REMOTE_OFFSH 0x11 |
| 120 | #define LM90_REG_W_REMOTE_OFFSH 0x11 |
| 121 | #define LM90_REG_R_REMOTE_OFFSL 0x12 |
| 122 | #define LM90_REG_W_REMOTE_OFFSL 0x12 |
| 123 | #define LM90_REG_R_REMOTE_HIGHH 0x07 |
| 124 | #define LM90_REG_W_REMOTE_HIGHH 0x0D |
| 125 | #define LM90_REG_R_REMOTE_HIGHL 0x13 |
| 126 | #define LM90_REG_W_REMOTE_HIGHL 0x13 |
| 127 | #define LM90_REG_R_REMOTE_LOWH 0x08 |
| 128 | #define LM90_REG_W_REMOTE_LOWH 0x0E |
| 129 | #define LM90_REG_R_REMOTE_LOWL 0x14 |
| 130 | #define LM90_REG_W_REMOTE_LOWL 0x14 |
| 131 | #define LM90_REG_R_REMOTE_CRIT 0x19 |
| 132 | #define LM90_REG_W_REMOTE_CRIT 0x19 |
| 133 | #define LM90_REG_R_TCRIT_HYST 0x21 |
| 134 | #define LM90_REG_W_TCRIT_HYST 0x21 |
| 135 | |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 136 | /* MAX6657-specific registers */ |
| 137 | |
| 138 | #define MAX6657_REG_R_LOCAL_TEMPL 0x11 |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * Functions declaration |
| 142 | */ |
| 143 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 144 | static int lm90_detect(struct i2c_client *client, int kind, |
| 145 | struct i2c_board_info *info); |
| 146 | static int lm90_probe(struct i2c_client *client, |
| 147 | const struct i2c_device_id *id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | static void lm90_init_client(struct i2c_client *client); |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 149 | static int lm90_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static struct lm90_data *lm90_update_device(struct device *dev); |
| 151 | |
| 152 | /* |
| 153 | * Driver data (common to all clients) |
| 154 | */ |
| 155 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 156 | static const struct i2c_device_id lm90_id[] = { |
| 157 | { "adm1032", adm1032 }, |
| 158 | { "adt7461", adt7461 }, |
| 159 | { "lm90", lm90 }, |
| 160 | { "lm86", lm86 }, |
| 161 | { "lm89", lm99 }, |
| 162 | { "lm99", lm99 }, /* Missing temperature offset */ |
| 163 | { "max6657", max6657 }, |
| 164 | { "max6658", max6657 }, |
| 165 | { "max6659", max6657 }, |
| 166 | { "max6680", max6680 }, |
| 167 | { "max6681", max6680 }, |
| 168 | { } |
| 169 | }; |
| 170 | MODULE_DEVICE_TABLE(i2c, lm90_id); |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | static struct i2c_driver lm90_driver = { |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 173 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 174 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 175 | .name = "lm90", |
| 176 | }, |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 177 | .probe = lm90_probe, |
| 178 | .remove = lm90_remove, |
| 179 | .id_table = lm90_id, |
| 180 | .detect = lm90_detect, |
| 181 | .address_data = &addr_data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * Client data (each client gets its own) |
| 186 | */ |
| 187 | |
| 188 | struct lm90_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 189 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 190 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | char valid; /* zero until following fields are valid */ |
| 192 | unsigned long last_updated; /* in jiffies */ |
| 193 | int kind; |
| 194 | |
| 195 | /* registers values */ |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 196 | s8 temp8[4]; /* 0: local low limit |
| 197 | 1: local high limit |
| 198 | 2: local critical limit |
| 199 | 3: remote critical limit */ |
| 200 | s16 temp11[5]; /* 0: remote input |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 201 | 1: remote low limit |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 202 | 2: remote high limit |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 203 | 3: remote offset (except max6657) |
| 204 | 4: local input */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | u8 temp_hyst; |
| 206 | u8 alarms; /* bitvector */ |
| 207 | }; |
| 208 | |
| 209 | /* |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 210 | * Conversions |
| 211 | * For local temperatures and limits, critical limits and the hysteresis |
| 212 | * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. |
| 213 | * For remote temperatures and limits, it uses signed 11-bit values with |
| 214 | * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
| 215 | */ |
| 216 | |
| 217 | static inline int temp1_from_reg(s8 val) |
| 218 | { |
| 219 | return val * 1000; |
| 220 | } |
| 221 | |
| 222 | static inline int temp2_from_reg(s16 val) |
| 223 | { |
| 224 | return val / 32 * 125; |
| 225 | } |
| 226 | |
| 227 | static s8 temp1_to_reg(long val) |
| 228 | { |
| 229 | if (val <= -128000) |
| 230 | return -128; |
| 231 | if (val >= 127000) |
| 232 | return 127; |
| 233 | if (val < 0) |
| 234 | return (val - 500) / 1000; |
| 235 | return (val + 500) / 1000; |
| 236 | } |
| 237 | |
| 238 | static s16 temp2_to_reg(long val) |
| 239 | { |
| 240 | if (val <= -128000) |
| 241 | return 0x8000; |
| 242 | if (val >= 127875) |
| 243 | return 0x7FE0; |
| 244 | if (val < 0) |
| 245 | return (val - 62) / 125 * 32; |
| 246 | return (val + 62) / 125 * 32; |
| 247 | } |
| 248 | |
| 249 | static u8 hyst_to_reg(long val) |
| 250 | { |
| 251 | if (val <= 0) |
| 252 | return 0; |
| 253 | if (val >= 30500) |
| 254 | return 31; |
| 255 | return (val + 500) / 1000; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * ADT7461 is almost identical to LM90 except that attempts to write |
| 260 | * values that are outside the range 0 < temp < 127 are treated as |
| 261 | * the boundary value. |
| 262 | */ |
| 263 | static u8 temp1_to_reg_adt7461(long val) |
| 264 | { |
| 265 | if (val <= 0) |
| 266 | return 0; |
| 267 | if (val >= 127000) |
| 268 | return 127; |
| 269 | return (val + 500) / 1000; |
| 270 | } |
| 271 | |
| 272 | static u16 temp2_to_reg_adt7461(long val) |
| 273 | { |
| 274 | if (val <= 0) |
| 275 | return 0; |
| 276 | if (val >= 127750) |
| 277 | return 0x7FC0; |
| 278 | return (val + 125) / 250 * 64; |
| 279 | } |
| 280 | |
| 281 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * Sysfs stuff |
| 283 | */ |
| 284 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 285 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, |
| 286 | char *buf) |
| 287 | { |
| 288 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 289 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 290 | return sprintf(buf, "%d\n", temp1_from_reg(data->temp8[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 293 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, |
| 294 | const char *buf, size_t count) |
| 295 | { |
| 296 | static const u8 reg[4] = { |
| 297 | LM90_REG_W_LOCAL_LOW, |
| 298 | LM90_REG_W_LOCAL_HIGH, |
| 299 | LM90_REG_W_LOCAL_CRIT, |
| 300 | LM90_REG_W_REMOTE_CRIT, |
| 301 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 303 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 304 | struct i2c_client *client = to_i2c_client(dev); |
| 305 | struct lm90_data *data = i2c_get_clientdata(client); |
| 306 | long val = simple_strtol(buf, NULL, 10); |
| 307 | int nr = attr->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 309 | mutex_lock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 310 | if (data->kind == adt7461) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 311 | data->temp8[nr] = temp1_to_reg_adt7461(val); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 312 | else |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 313 | data->temp8[nr] = temp1_to_reg(val); |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 314 | i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 315 | mutex_unlock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 316 | return count; |
| 317 | } |
| 318 | |
| 319 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, |
| 320 | char *buf) |
| 321 | { |
| 322 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 323 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 324 | return sprintf(buf, "%d\n", temp2_from_reg(data->temp11[attr->index])); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, |
| 328 | const char *buf, size_t count) |
| 329 | { |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 330 | static const u8 reg[6] = { |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 331 | LM90_REG_W_REMOTE_LOWH, |
| 332 | LM90_REG_W_REMOTE_LOWL, |
| 333 | LM90_REG_W_REMOTE_HIGHH, |
| 334 | LM90_REG_W_REMOTE_HIGHL, |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 335 | LM90_REG_W_REMOTE_OFFSH, |
| 336 | LM90_REG_W_REMOTE_OFFSL, |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 340 | struct i2c_client *client = to_i2c_client(dev); |
| 341 | struct lm90_data *data = i2c_get_clientdata(client); |
| 342 | long val = simple_strtol(buf, NULL, 10); |
| 343 | int nr = attr->index; |
| 344 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 345 | mutex_lock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 346 | if (data->kind == adt7461) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 347 | data->temp11[nr] = temp2_to_reg_adt7461(val); |
Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 348 | else if (data->kind == max6657 || data->kind == max6680) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 349 | data->temp11[nr] = temp1_to_reg(val) << 8; |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 350 | else |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 351 | data->temp11[nr] = temp2_to_reg(val); |
Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 352 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 353 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
| 354 | data->temp11[nr] >> 8); |
Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 355 | if (data->kind != max6657 && data->kind != max6680) |
| 356 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], |
| 357 | data->temp11[nr] & 0xff); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 358 | mutex_unlock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 359 | return count; |
| 360 | } |
| 361 | |
| 362 | static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr, |
| 363 | char *buf) |
| 364 | { |
| 365 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 366 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 367 | return sprintf(buf, "%d\n", temp1_from_reg(data->temp8[attr->index]) |
| 368 | - temp1_from_reg(data->temp_hyst)); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, |
| 372 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | struct i2c_client *client = to_i2c_client(dev); |
| 375 | struct lm90_data *data = i2c_get_clientdata(client); |
| 376 | long val = simple_strtol(buf, NULL, 10); |
| 377 | long hyst; |
| 378 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 379 | mutex_lock(&data->update_lock); |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 380 | hyst = temp1_from_reg(data->temp8[2]) - val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame^] | 382 | hyst_to_reg(hyst)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 383 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return count; |
| 385 | } |
| 386 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 387 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 388 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | struct lm90_data *data = lm90_update_device(dev); |
| 391 | return sprintf(buf, "%d\n", data->alarms); |
| 392 | } |
| 393 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 394 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
| 395 | *devattr, char *buf) |
| 396 | { |
| 397 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 398 | struct lm90_data *data = lm90_update_device(dev); |
| 399 | int bitnr = attr->index; |
| 400 | |
| 401 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); |
| 402 | } |
| 403 | |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 404 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 405 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
| 406 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 407 | set_temp8, 0); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 408 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
| 409 | set_temp11, 1); |
| 410 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 411 | set_temp8, 1); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 412 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
| 413 | set_temp11, 2); |
| 414 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 415 | set_temp8, 2); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 416 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 417 | set_temp8, 3); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 418 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 419 | set_temphyst, 2); |
| 420 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 421 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
| 422 | set_temp11, 3); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 423 | |
| 424 | /* Individual alarm files */ |
| 425 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 426 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 427 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 428 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 429 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 430 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 431 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 432 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 434 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 435 | static struct attribute *lm90_attributes[] = { |
| 436 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 437 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 438 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 439 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 440 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 441 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 442 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 443 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 444 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 445 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 446 | |
| 447 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 448 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 449 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 450 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 451 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 452 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 453 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 454 | &dev_attr_alarms.attr, |
| 455 | NULL |
| 456 | }; |
| 457 | |
| 458 | static const struct attribute_group lm90_group = { |
| 459 | .attrs = lm90_attributes, |
| 460 | }; |
| 461 | |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 462 | /* pec used for ADM1032 only */ |
| 463 | static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, |
| 464 | char *buf) |
| 465 | { |
| 466 | struct i2c_client *client = to_i2c_client(dev); |
| 467 | return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); |
| 468 | } |
| 469 | |
| 470 | static ssize_t set_pec(struct device *dev, struct device_attribute *dummy, |
| 471 | const char *buf, size_t count) |
| 472 | { |
| 473 | struct i2c_client *client = to_i2c_client(dev); |
| 474 | long val = simple_strtol(buf, NULL, 10); |
| 475 | |
| 476 | switch (val) { |
| 477 | case 0: |
| 478 | client->flags &= ~I2C_CLIENT_PEC; |
| 479 | break; |
| 480 | case 1: |
| 481 | client->flags |= I2C_CLIENT_PEC; |
| 482 | break; |
| 483 | default: |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | return count; |
| 488 | } |
| 489 | |
| 490 | static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec); |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * Real code |
| 494 | */ |
| 495 | |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 496 | /* The ADM1032 supports PEC but not on write byte transactions, so we need |
Jean Delvare | 0966415 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 497 | to explicitly ask for a transaction without PEC. */ |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 498 | static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value) |
| 499 | { |
| 500 | return i2c_smbus_xfer(client->adapter, client->addr, |
| 501 | client->flags & ~I2C_CLIENT_PEC, |
| 502 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
| 503 | } |
| 504 | |
| 505 | /* It is assumed that client->update_lock is held (unless we are in |
| 506 | detection or initialization steps). This matters when PEC is enabled, |
| 507 | because we don't want the address pointer to change between the write |
| 508 | byte and the read byte transactions. */ |
Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 509 | static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value) |
| 510 | { |
| 511 | int err; |
| 512 | |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 513 | if (client->flags & I2C_CLIENT_PEC) { |
| 514 | err = adm1032_write_byte(client, reg); |
| 515 | if (err >= 0) |
| 516 | err = i2c_smbus_read_byte(client); |
| 517 | } else |
| 518 | err = i2c_smbus_read_byte_data(client, reg); |
Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 519 | |
| 520 | if (err < 0) { |
| 521 | dev_warn(&client->dev, "Register %#02x read failed (%d)\n", |
| 522 | reg, err); |
| 523 | return err; |
| 524 | } |
| 525 | *value = err; |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 530 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 531 | static int lm90_detect(struct i2c_client *new_client, int kind, |
| 532 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 534 | struct i2c_adapter *adapter = new_client->adapter; |
| 535 | int address = new_client->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | const char *name = ""; |
| 537 | |
| 538 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 539 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | * Now we do the remaining detection. A negative kind means that |
| 543 | * the driver was loaded with no force parameter (default), so we |
| 544 | * must both detect and identify the chip. A zero kind means that |
| 545 | * the driver was loaded with the force parameter, the detection |
| 546 | * step shall be skipped. A positive kind means that the driver |
| 547 | * was loaded with the force parameter and a given kind of chip is |
| 548 | * requested, so both the detection and the identification steps |
| 549 | * are skipped. |
| 550 | */ |
| 551 | |
| 552 | /* Default to an LM90 if forced */ |
| 553 | if (kind == 0) |
| 554 | kind = lm90; |
| 555 | |
| 556 | if (kind < 0) { /* detection and identification */ |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 557 | int man_id, chip_id, reg_config1, reg_convrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 559 | if ((man_id = i2c_smbus_read_byte_data(new_client, |
| 560 | LM90_REG_R_MAN_ID)) < 0 |
| 561 | || (chip_id = i2c_smbus_read_byte_data(new_client, |
| 562 | LM90_REG_R_CHIP_ID)) < 0 |
| 563 | || (reg_config1 = i2c_smbus_read_byte_data(new_client, |
| 564 | LM90_REG_R_CONFIG1)) < 0 |
| 565 | || (reg_convrate = i2c_smbus_read_byte_data(new_client, |
| 566 | LM90_REG_R_CONVRATE)) < 0) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 567 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 569 | if ((address == 0x4C || address == 0x4D) |
| 570 | && man_id == 0x01) { /* National Semiconductor */ |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 571 | int reg_config2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 573 | if ((reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 574 | LM90_REG_R_CONFIG2)) < 0) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 575 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | if ((reg_config1 & 0x2A) == 0x00 |
| 578 | && (reg_config2 & 0xF8) == 0x00 |
| 579 | && reg_convrate <= 0x09) { |
| 580 | if (address == 0x4C |
| 581 | && (chip_id & 0xF0) == 0x20) { /* LM90 */ |
| 582 | kind = lm90; |
| 583 | } else |
| 584 | if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */ |
| 585 | kind = lm99; |
| 586 | } else |
| 587 | if (address == 0x4C |
| 588 | && (chip_id & 0xF0) == 0x10) { /* LM86 */ |
| 589 | kind = lm86; |
| 590 | } |
| 591 | } |
| 592 | } else |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 593 | if ((address == 0x4C || address == 0x4D) |
| 594 | && man_id == 0x41) { /* Analog Devices */ |
Jean Delvare | 90209b4 | 2005-10-26 22:20:21 +0200 | [diff] [blame] | 595 | if ((chip_id & 0xF0) == 0x40 /* ADM1032 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | && (reg_config1 & 0x3F) == 0x00 |
| 597 | && reg_convrate <= 0x0A) { |
| 598 | kind = adm1032; |
| 599 | } else |
Jean Delvare | 90209b4 | 2005-10-26 22:20:21 +0200 | [diff] [blame] | 600 | if (chip_id == 0x51 /* ADT7461 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | && (reg_config1 & 0x1F) == 0x00 /* check compat mode */ |
| 602 | && reg_convrate <= 0x0A) { |
| 603 | kind = adt7461; |
| 604 | } |
| 605 | } else |
| 606 | if (man_id == 0x4D) { /* Maxim */ |
| 607 | /* |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 608 | * The MAX6657, MAX6658 and MAX6659 do NOT have a |
| 609 | * chip_id register. Reading from that address will |
| 610 | * return the last read value, which in our case is |
| 611 | * those of the man_id register. Likewise, the config1 |
| 612 | * register seems to lack a low nibble, so the value |
| 613 | * will be those of the previous read, so in our case |
| 614 | * those of the man_id register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | */ |
| 616 | if (chip_id == man_id |
Guillaume Chazarain | f5744e3 | 2007-07-27 01:04:22 +0200 | [diff] [blame] | 617 | && (address == 0x4C || address == 0x4D) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | && (reg_config1 & 0x1F) == (man_id & 0x0F) |
| 619 | && reg_convrate <= 0x09) { |
| 620 | kind = max6657; |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 621 | } else |
| 622 | /* The chip_id register of the MAX6680 and MAX6681 |
| 623 | * holds the revision of the chip. |
| 624 | * the lowest bit of the config1 register is unused |
| 625 | * and should return zero when read, so should the |
| 626 | * second to last bit of config1 (software reset) |
| 627 | */ |
| 628 | if (chip_id == 0x01 |
| 629 | && (reg_config1 & 0x03) == 0x00 |
| 630 | && reg_convrate <= 0x07) { |
| 631 | kind = max6680; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | if (kind <= 0) { /* identification failed */ |
| 636 | dev_info(&adapter->dev, |
| 637 | "Unsupported chip (man_id=0x%02X, " |
| 638 | "chip_id=0x%02X).\n", man_id, chip_id); |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 639 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 643 | /* Fill the i2c board info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (kind == lm90) { |
| 645 | name = "lm90"; |
| 646 | } else if (kind == adm1032) { |
| 647 | name = "adm1032"; |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 648 | /* The ADM1032 supports PEC, but only if combined |
| 649 | transactions are not used. */ |
| 650 | if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 651 | info->flags |= I2C_CLIENT_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } else if (kind == lm99) { |
| 653 | name = "lm99"; |
| 654 | } else if (kind == lm86) { |
| 655 | name = "lm86"; |
| 656 | } else if (kind == max6657) { |
| 657 | name = "max6657"; |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 658 | } else if (kind == max6680) { |
| 659 | name = "max6680"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } else if (kind == adt7461) { |
| 661 | name = "adt7461"; |
| 662 | } |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 663 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static int lm90_probe(struct i2c_client *new_client, |
| 669 | const struct i2c_device_id *id) |
| 670 | { |
| 671 | struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent); |
| 672 | struct lm90_data *data; |
| 673 | int err; |
| 674 | |
| 675 | data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL); |
| 676 | if (!data) { |
| 677 | err = -ENOMEM; |
| 678 | goto exit; |
| 679 | } |
| 680 | i2c_set_clientdata(new_client, data); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 681 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 683 | /* Set the device type */ |
| 684 | data->kind = id->driver_data; |
| 685 | if (data->kind == adm1032) { |
| 686 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 687 | new_client->flags &= ~I2C_CLIENT_PEC; |
| 688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | /* Initialize the LM90 chip */ |
| 691 | lm90_init_client(new_client); |
| 692 | |
| 693 | /* Register sysfs hooks */ |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 694 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group))) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 695 | goto exit_free; |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 696 | if (new_client->flags & I2C_CLIENT_PEC) { |
| 697 | if ((err = device_create_file(&new_client->dev, |
| 698 | &dev_attr_pec))) |
| 699 | goto exit_remove_files; |
| 700 | } |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 701 | if (data->kind != max6657) { |
| 702 | if ((err = device_create_file(&new_client->dev, |
| 703 | &sensor_dev_attr_temp2_offset.dev_attr))) |
| 704 | goto exit_remove_files; |
| 705 | } |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 706 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 707 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 708 | if (IS_ERR(data->hwmon_dev)) { |
| 709 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 710 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return 0; |
| 714 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 715 | exit_remove_files: |
| 716 | sysfs_remove_group(&new_client->dev.kobj, &lm90_group); |
| 717 | device_remove_file(&new_client->dev, &dev_attr_pec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | exit_free: |
| 719 | kfree(data); |
| 720 | exit: |
| 721 | return err; |
| 722 | } |
| 723 | |
| 724 | static void lm90_init_client(struct i2c_client *client) |
| 725 | { |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 726 | u8 config, config_orig; |
| 727 | struct lm90_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * Start the conversions. |
| 731 | */ |
| 732 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, |
| 733 | 5); /* 2 Hz */ |
Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 734 | if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) { |
| 735 | dev_warn(&client->dev, "Initialization failed!\n"); |
| 736 | return; |
| 737 | } |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 738 | config_orig = config; |
| 739 | |
| 740 | /* |
| 741 | * Put MAX6680/MAX8881 into extended resolution (bit 0x10, |
| 742 | * 0.125 degree resolution) and range (0x08, extend range |
| 743 | * to -64 degree) mode for the remote temperature sensor. |
| 744 | */ |
| 745 | if (data->kind == max6680) { |
| 746 | config |= 0x18; |
| 747 | } |
| 748 | |
| 749 | config &= 0xBF; /* run */ |
| 750 | if (config != config_orig) /* Only write if changed */ |
| 751 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 754 | static int lm90_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 756 | struct lm90_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 758 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 759 | sysfs_remove_group(&client->dev.kobj, &lm90_group); |
| 760 | device_remove_file(&client->dev, &dev_attr_pec); |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 761 | if (data->kind != max6657) |
| 762 | device_remove_file(&client->dev, |
| 763 | &sensor_dev_attr_temp2_offset.dev_attr); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 764 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 765 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 769 | static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) |
| 770 | { |
| 771 | int err; |
| 772 | u8 oldh, newh, l; |
| 773 | |
| 774 | /* |
| 775 | * There is a trick here. We have to read two registers to have the |
| 776 | * sensor temperature, but we have to beware a conversion could occur |
| 777 | * inbetween the readings. The datasheet says we should either use |
| 778 | * the one-shot conversion register, which we don't want to do |
| 779 | * (disables hardware monitoring) or monitor the busy bit, which is |
| 780 | * impossible (we can't read the values and monitor that bit at the |
| 781 | * exact same time). So the solution used here is to read the high |
| 782 | * byte once, then the low byte, then the high byte again. If the new |
| 783 | * high byte matches the old one, then we have a valid reading. Else |
| 784 | * we have to read the low byte again, and now we believe we have a |
| 785 | * correct reading. |
| 786 | */ |
| 787 | if ((err = lm90_read_reg(client, regh, &oldh)) |
| 788 | || (err = lm90_read_reg(client, regl, &l)) |
| 789 | || (err = lm90_read_reg(client, regh, &newh))) |
| 790 | return err; |
| 791 | if (oldh != newh) { |
| 792 | err = lm90_read_reg(client, regl, &l); |
| 793 | if (err) |
| 794 | return err; |
| 795 | } |
| 796 | *value = (newh << 8) | l; |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | static struct lm90_data *lm90_update_device(struct device *dev) |
| 802 | { |
| 803 | struct i2c_client *client = to_i2c_client(dev); |
| 804 | struct lm90_data *data = i2c_get_clientdata(client); |
| 805 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 806 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) { |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 809 | u8 h, l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | dev_dbg(&client->dev, "Updating lm90 data.\n"); |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 812 | lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]); |
| 813 | lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]); |
| 814 | lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]); |
| 815 | lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]); |
Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 816 | lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 818 | if (data->kind == max6657) { |
| 819 | lm90_read16(client, LM90_REG_R_LOCAL_TEMP, |
| 820 | MAX6657_REG_R_LOCAL_TEMPL, |
| 821 | &data->temp11[4]); |
| 822 | } else { |
| 823 | if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, |
| 824 | &h) == 0) |
| 825 | data->temp11[4] = h << 8; |
| 826 | } |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 827 | lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, |
| 828 | LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 830 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) { |
| 831 | data->temp11[1] = h << 8; |
| 832 | if (data->kind != max6657 && data->kind != max6680 |
| 833 | && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, |
| 834 | &l) == 0) |
| 835 | data->temp11[1] |= l; |
| 836 | } |
| 837 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) { |
| 838 | data->temp11[2] = h << 8; |
| 839 | if (data->kind != max6657 && data->kind != max6680 |
| 840 | && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, |
| 841 | &l) == 0) |
| 842 | data->temp11[2] |= l; |
| 843 | } |
| 844 | |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 845 | if (data->kind != max6657) { |
| 846 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH, |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 847 | &h) == 0 |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 848 | && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL, |
| 849 | &l) == 0) |
Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 850 | data->temp11[3] = (h << 8) | l; |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 851 | } |
Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 852 | lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | data->last_updated = jiffies; |
| 855 | data->valid = 1; |
| 856 | } |
| 857 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 858 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | return data; |
| 861 | } |
| 862 | |
| 863 | static int __init sensors_lm90_init(void) |
| 864 | { |
| 865 | return i2c_add_driver(&lm90_driver); |
| 866 | } |
| 867 | |
| 868 | static void __exit sensors_lm90_exit(void) |
| 869 | { |
| 870 | i2c_del_driver(&lm90_driver); |
| 871 | } |
| 872 | |
| 873 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 874 | MODULE_DESCRIPTION("LM90/ADM1032 driver"); |
| 875 | MODULE_LICENSE("GPL"); |
| 876 | |
| 877 | module_init(sensors_lm90_init); |
| 878 | module_exit(sensors_lm90_exit); |