| 1 | /* |
|---|
| 2 | * lm90.c - Part of lm_sensors, Linux kernel modules for hardware |
|---|
| 3 | * monitoring |
|---|
| 4 | * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org> |
|---|
| 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 |
|---|
| 9 | * temperature) and a 3-4 deg accuracy. Complete datasheet can be |
|---|
| 10 | * obtained from National's website at: |
|---|
| 11 | * http://www.national.com/pf/LM/LM90.html |
|---|
| 12 | * |
|---|
| 13 | * This driver also supports the LM89 and LM99, two other sensor chips |
|---|
| 14 | * made by National Semiconductor. Both have an increased remote |
|---|
| 15 | * temperature measurement accuracy (1 degree), and the LM99 |
|---|
| 16 | * additionally shifts remote temperatures (measured and limits) by 16 |
|---|
| 17 | * degrees, which allows for higher temperatures measurement. The |
|---|
| 18 | * driver doesn't handle it since it can be done easily in user-space. |
|---|
| 19 | * Complete datasheets can be obtained from National's website at: |
|---|
| 20 | * http://www.national.com/pf/LM/LM89.html |
|---|
| 21 | * http://www.national.com/pf/LM/LM99.html |
|---|
| 22 | * Note that there is no way to differenciate between both chips. |
|---|
| 23 | * |
|---|
| 24 | * This driver also supports the LM86, another sensor chip made by |
|---|
| 25 | * National Semiconductor. It is exactly similar to the LM90 except it |
|---|
| 26 | * has a higher accuracy. |
|---|
| 27 | * Complete datasheet can be obtained from National's website at: |
|---|
| 28 | * http://www.national.com/pf/LM/LM86.html |
|---|
| 29 | * |
|---|
| 30 | * This driver also supports the ADM1032, a sensor chip made by Analog |
|---|
| 31 | * Devices. That chip is similar to the LM90, with a few differences |
|---|
| 32 | * that are not handled by this driver. Complete datasheet can be |
|---|
| 33 | * obtained from Analog's website at: |
|---|
| 34 | * http://products.analog.com/products/info.asp?product=ADM1032 |
|---|
| 35 | * Among others, it has a higher accuracy than the LM90, much like the |
|---|
| 36 | * LM86 does. |
|---|
| 37 | * |
|---|
| 38 | * This driver also supports the MAX6657 and MAX6658, sensor chips made |
|---|
| 39 | * by Maxim. These chips are similar to the LM86. Complete datasheet |
|---|
| 40 | * can be obtained at Maxim's website at: |
|---|
| 41 | * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578 |
|---|
| 42 | * Note that there is no way to differenciate between both chips (but |
|---|
| 43 | * no need either). |
|---|
| 44 | * |
|---|
| 45 | * Since the LM90 was the first chipset supported by this driver, most |
|---|
| 46 | * comments will refer to this chipset, but are actually general and |
|---|
| 47 | * concern all supported chipsets, unless mentioned otherwise. |
|---|
| 48 | * |
|---|
| 49 | * This program is free software; you can redistribute it and/or modify |
|---|
| 50 | * it under the terms of the GNU General Public License as published by |
|---|
| 51 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 52 | * (at your option) any later version. |
|---|
| 53 | * |
|---|
| 54 | * This program is distributed in the hope that it will be useful, |
|---|
| 55 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 56 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 57 | * GNU General Public License for more details. |
|---|
| 58 | * |
|---|
| 59 | * You should have received a copy of the GNU General Public License |
|---|
| 60 | * along with this program; if not, write to the Free Software |
|---|
| 61 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 62 | */ |
|---|
| 63 | |
|---|
| 64 | #include <linux/module.h> |
|---|
| 65 | #include <linux/slab.h> |
|---|
| 66 | #include <linux/i2c.h> |
|---|
| 67 | #include <linux/i2c-proc.h> |
|---|
| 68 | #include <linux/init.h> |
|---|
| 69 | #include "version.h" |
|---|
| 70 | |
|---|
| 71 | #ifndef I2C_DRIVERID_LM90 |
|---|
| 72 | #define I2C_DRIVERID_LM90 1042 |
|---|
| 73 | #endif |
|---|
| 74 | |
|---|
| 75 | /* |
|---|
| 76 | * Addresses to scan |
|---|
| 77 | * Address is fully defined internally and cannot be changed. |
|---|
| 78 | * LM86, LM89, LM90, LM99, ADM1032, MAX6657 and MAX6658 have address 0x4c. |
|---|
| 79 | * LM89-1, and LM99-1 have address 0x4d. |
|---|
| 80 | */ |
|---|
| 81 | |
|---|
| 82 | static unsigned short normal_i2c[] = { 0x4c, 0x4d, SENSORS_I2C_END }; |
|---|
| 83 | static unsigned short normal_i2c_range[] = { SENSORS_I2C_END }; |
|---|
| 84 | static unsigned int normal_isa[] = { SENSORS_ISA_END }; |
|---|
| 85 | static unsigned int normal_isa_range[] = { SENSORS_ISA_END }; |
|---|
| 86 | |
|---|
| 87 | /* |
|---|
| 88 | * Insmod parameters |
|---|
| 89 | */ |
|---|
| 90 | |
|---|
| 91 | SENSORS_INSMOD_5(lm90, adm1032, lm99, lm86, max6657); |
|---|
| 92 | |
|---|
| 93 | /* |
|---|
| 94 | * The LM90 registers |
|---|
| 95 | */ |
|---|
| 96 | |
|---|
| 97 | #define LM90_REG_R_MAN_ID 0xFE |
|---|
| 98 | #define LM90_REG_R_CHIP_ID 0xFF |
|---|
| 99 | #define LM90_REG_R_CONFIG1 0x03 |
|---|
| 100 | #define LM90_REG_W_CONFIG1 0x09 |
|---|
| 101 | #define LM90_REG_R_CONFIG2 0xBF |
|---|
| 102 | #define LM90_REG_W_CONFIG2 0xBF |
|---|
| 103 | #define LM90_REG_R_CONVRATE 0x04 |
|---|
| 104 | #define LM90_REG_W_CONVRATE 0x0A |
|---|
| 105 | #define LM90_REG_R_STATUS 0x02 |
|---|
| 106 | #define LM90_REG_R_LOCAL_TEMP 0x00 |
|---|
| 107 | #define LM90_REG_R_LOCAL_HIGH 0x05 |
|---|
| 108 | #define LM90_REG_W_LOCAL_HIGH 0x0B |
|---|
| 109 | #define LM90_REG_R_LOCAL_LOW 0x06 |
|---|
| 110 | #define LM90_REG_W_LOCAL_LOW 0x0C |
|---|
| 111 | #define LM90_REG_R_LOCAL_CRIT 0x20 |
|---|
| 112 | #define LM90_REG_W_LOCAL_CRIT 0x20 |
|---|
| 113 | #define LM90_REG_R_REMOTE_TEMPH 0x01 |
|---|
| 114 | #define LM90_REG_R_REMOTE_TEMPL 0x10 |
|---|
| 115 | #define LM90_REG_R_REMOTE_OFFSH 0x11 |
|---|
| 116 | #define LM90_REG_W_REMOTE_OFFSH 0x11 |
|---|
| 117 | #define LM90_REG_R_REMOTE_OFFSL 0x12 |
|---|
| 118 | #define LM90_REG_W_REMOTE_OFFSL 0x12 |
|---|
| 119 | #define LM90_REG_R_REMOTE_HIGHH 0x07 |
|---|
| 120 | #define LM90_REG_W_REMOTE_HIGHH 0x0D |
|---|
| 121 | #define LM90_REG_R_REMOTE_HIGHL 0x13 |
|---|
| 122 | #define LM90_REG_W_REMOTE_HIGHL 0x13 |
|---|
| 123 | #define LM90_REG_R_REMOTE_LOWH 0x08 |
|---|
| 124 | #define LM90_REG_W_REMOTE_LOWH 0x0E |
|---|
| 125 | #define LM90_REG_R_REMOTE_LOWL 0x14 |
|---|
| 126 | #define LM90_REG_W_REMOTE_LOWL 0x14 |
|---|
| 127 | #define LM90_REG_R_REMOTE_CRIT 0x19 |
|---|
| 128 | #define LM90_REG_W_REMOTE_CRIT 0x19 |
|---|
| 129 | #define LM90_REG_R_TCRIT_HYST 0x21 |
|---|
| 130 | #define LM90_REG_W_TCRIT_HYST 0x21 |
|---|
| 131 | |
|---|
| 132 | /* |
|---|
| 133 | * Conversions and various macros |
|---|
| 134 | * For local temperatures and limits, critical limits and the hysteresis |
|---|
| 135 | * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celcius. |
|---|
| 136 | * For remote temperatures and limits, it uses signed 11-bit values with |
|---|
| 137 | * LSB = 0.125 degree Celcius, left-justified in 16-bit registers. |
|---|
| 138 | */ |
|---|
| 139 | |
|---|
| 140 | #define TEMP1_FROM_REG(val) (val) |
|---|
| 141 | #define TEMP1_TO_REG(val) ((val) <= -128 ? -128 : \ |
|---|
| 142 | (val) >= 127 ? 127 : (val)) |
|---|
| 143 | #define TEMP2_FROM_REG(val) ((val) / 32 * 125 / 100) |
|---|
| 144 | #define TEMP2_TO_REG(val) ((val) <= -1280 ? 0x8000 : \ |
|---|
| 145 | (val) >= 1270 ? 0x7FE0 : \ |
|---|
| 146 | ((val) * 100 / 125 * 32)) |
|---|
| 147 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ |
|---|
| 148 | (val) >= 31 ? 31 : (val)) |
|---|
| 149 | |
|---|
| 150 | /* |
|---|
| 151 | * Functions declaration |
|---|
| 152 | */ |
|---|
| 153 | |
|---|
| 154 | static int lm90_attach_adapter(struct i2c_adapter *adapter); |
|---|
| 155 | static int lm90_detect(struct i2c_adapter *adapter, int address, |
|---|
| 156 | unsigned short flags, int kind); |
|---|
| 157 | static void lm90_init_client(struct i2c_client *client); |
|---|
| 158 | static int lm90_detach_client(struct i2c_client *client); |
|---|
| 159 | static void lm90_local_temp(struct i2c_client *client, int operation, |
|---|
| 160 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 161 | static void lm90_remote_temp(struct i2c_client *client, int operation, |
|---|
| 162 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 163 | static void lm90_local_tcrit(struct i2c_client *client, int operation, |
|---|
| 164 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 165 | static void lm90_remote_tcrit(struct i2c_client *client, int operation, |
|---|
| 166 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 167 | static void lm90_local_hyst(struct i2c_client *client, int operation, |
|---|
| 168 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 169 | static void lm90_remote_hyst(struct i2c_client *client, int operation, |
|---|
| 170 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 171 | static void lm90_alarms(struct i2c_client *client, int operation, |
|---|
| 172 | int ctl_name, int *nrels_mag, long *results); |
|---|
| 173 | |
|---|
| 174 | /* |
|---|
| 175 | * Driver data (common to all clients) |
|---|
| 176 | */ |
|---|
| 177 | |
|---|
| 178 | static struct i2c_driver lm90_driver = { |
|---|
| 179 | .name = "LM90/ADM1032 sensor driver", |
|---|
| 180 | .id = I2C_DRIVERID_LM90, |
|---|
| 181 | .flags = I2C_DF_NOTIFY, |
|---|
| 182 | .attach_adapter = lm90_attach_adapter, |
|---|
| 183 | .detach_client = lm90_detach_client |
|---|
| 184 | }; |
|---|
| 185 | |
|---|
| 186 | /* |
|---|
| 187 | * Client data (each client gets its own) |
|---|
| 188 | */ |
|---|
| 189 | |
|---|
| 190 | struct lm90_data |
|---|
| 191 | { |
|---|
| 192 | struct i2c_client client; |
|---|
| 193 | int sysctl_id; |
|---|
| 194 | |
|---|
| 195 | struct semaphore update_lock; |
|---|
| 196 | char valid; /* zero until following fields are valid */ |
|---|
| 197 | unsigned long last_updated; /* in jiffies */ |
|---|
| 198 | |
|---|
| 199 | /* registers values */ |
|---|
| 200 | s8 local_temp, local_high, local_low; |
|---|
| 201 | s16 remote_temp, remote_high, remote_low; /* combined */ |
|---|
| 202 | s8 local_crit, remote_crit; |
|---|
| 203 | u8 hyst; /* linked to two sysctl files (hyst1 RW, hyst2 RO) */ |
|---|
| 204 | u8 alarms; /* bitvector */ |
|---|
| 205 | }; |
|---|
| 206 | |
|---|
| 207 | /* |
|---|
| 208 | * Proc entries |
|---|
| 209 | * These files are created for each detected LM90. |
|---|
| 210 | */ |
|---|
| 211 | |
|---|
| 212 | /* -- SENSORS SYSCTL START -- */ |
|---|
| 213 | |
|---|
| 214 | #define LM90_SYSCTL_LOCAL_TEMP 1200 |
|---|
| 215 | #define LM90_SYSCTL_REMOTE_TEMP 1201 |
|---|
| 216 | #define LM90_SYSCTL_LOCAL_TCRIT 1204 |
|---|
| 217 | #define LM90_SYSCTL_REMOTE_TCRIT 1205 |
|---|
| 218 | #define LM90_SYSCTL_LOCAL_HYST 1207 |
|---|
| 219 | #define LM90_SYSCTL_REMOTE_HYST 1208 |
|---|
| 220 | #define LM90_SYSCTL_ALARMS 1210 |
|---|
| 221 | |
|---|
| 222 | #define LM90_ALARM_LOCAL_HIGH 0x40 |
|---|
| 223 | #define LM90_ALARM_LOCAL_LOW 0x20 |
|---|
| 224 | #define LM90_ALARM_LOCAL_CRIT 0x01 |
|---|
| 225 | #define LM90_ALARM_REMOTE_HIGH 0x10 |
|---|
| 226 | #define LM90_ALARM_REMOTE_LOW 0x08 |
|---|
| 227 | #define LM90_ALARM_REMOTE_CRIT 0x02 |
|---|
| 228 | #define LM90_ALARM_REMOTE_OPEN 0x04 |
|---|
| 229 | |
|---|
| 230 | /* -- SENSORS SYSCTL END -- */ |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | static ctl_table lm90_dir_table_template[] = |
|---|
| 234 | { |
|---|
| 235 | {LM90_SYSCTL_LOCAL_TEMP, "temp1", NULL, 0, 0644, NULL, |
|---|
| 236 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_temp}, |
|---|
| 237 | {LM90_SYSCTL_REMOTE_TEMP, "temp2", NULL, 0, 0644, NULL, |
|---|
| 238 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_temp}, |
|---|
| 239 | {LM90_SYSCTL_LOCAL_TCRIT, "tcrit1", NULL, 0, 0644, NULL, |
|---|
| 240 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_tcrit}, |
|---|
| 241 | {LM90_SYSCTL_REMOTE_TCRIT, "tcrit2", NULL, 0, 0644, NULL, |
|---|
| 242 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_tcrit}, |
|---|
| 243 | {LM90_SYSCTL_LOCAL_HYST, "hyst1", NULL, 0, 0644, NULL, |
|---|
| 244 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_hyst}, |
|---|
| 245 | {LM90_SYSCTL_REMOTE_HYST, "hyst2", NULL, 0, 0444, NULL, |
|---|
| 246 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_hyst}, |
|---|
| 247 | {LM90_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, |
|---|
| 248 | &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_alarms}, |
|---|
| 249 | {0} |
|---|
| 250 | }; |
|---|
| 251 | |
|---|
| 252 | /* |
|---|
| 253 | * Internal variables |
|---|
| 254 | */ |
|---|
| 255 | |
|---|
| 256 | static int lm90_id = 0; |
|---|
| 257 | |
|---|
| 258 | /* |
|---|
| 259 | * Real code |
|---|
| 260 | */ |
|---|
| 261 | |
|---|
| 262 | static int lm90_attach_adapter(struct i2c_adapter *adapter) |
|---|
| 263 | { |
|---|
| 264 | return i2c_detect(adapter, &addr_data, lm90_detect); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /* |
|---|
| 268 | * The following function does more than just detection. If detection |
|---|
| 269 | * succeeds, it also registers the new chip. |
|---|
| 270 | */ |
|---|
| 271 | static int lm90_detect(struct i2c_adapter *adapter, int address, |
|---|
| 272 | unsigned short flags, int kind) |
|---|
| 273 | { |
|---|
| 274 | struct i2c_client *new_client; |
|---|
| 275 | struct lm90_data *data; |
|---|
| 276 | int err = 0; |
|---|
| 277 | const char *type_name = ""; |
|---|
| 278 | const char *client_name = ""; |
|---|
| 279 | |
|---|
| 280 | #ifdef DEBUG |
|---|
| 281 | if (i2c_is_isa_adapter(adapter)) |
|---|
| 282 | { |
|---|
| 283 | printk("lm90.o: Called for an ISA bus adapter, aborting.\n"); |
|---|
| 284 | return 0; |
|---|
| 285 | } |
|---|
| 286 | #endif |
|---|
| 287 | |
|---|
| 288 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
|---|
| 289 | { |
|---|
| 290 | #ifdef DEBUG |
|---|
| 291 | printk("lm90.o: I2C bus doesn't support byte read mode, " |
|---|
| 292 | "skipping.\n"); |
|---|
| 293 | #endif |
|---|
| 294 | return 0; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) |
|---|
| 298 | { |
|---|
| 299 | printk("lm90.o: Out of memory in lm90_detect (new_client).\n"); |
|---|
| 300 | return -ENOMEM; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /* |
|---|
| 304 | * The common I2C client data is placed right before the |
|---|
| 305 | * LM90-specific data. The LM90-specific data is pointed to by the |
|---|
| 306 | * data field from the I2C client data. |
|---|
| 307 | */ |
|---|
| 308 | |
|---|
| 309 | new_client = &data->client; |
|---|
| 310 | new_client->addr = address; |
|---|
| 311 | new_client->data = data; |
|---|
| 312 | new_client->adapter = adapter; |
|---|
| 313 | new_client->driver = &lm90_driver; |
|---|
| 314 | new_client->flags = 0; |
|---|
| 315 | |
|---|
| 316 | /* |
|---|
| 317 | * Now we do the remaining detection. A negative kind means that |
|---|
| 318 | * the driver was loaded with no force parameter (default), so we |
|---|
| 319 | * must both detect and identify the chip. A zero kind means that |
|---|
| 320 | * the driver was loaded with the force parameter, the detection |
|---|
| 321 | * step shall be skipped. A positive kind means that the driver |
|---|
| 322 | * was loaded with the force parameter and a given kind of chip is |
|---|
| 323 | * requested, so both the detection and the identification steps |
|---|
| 324 | * are skipped. |
|---|
| 325 | */ |
|---|
| 326 | |
|---|
| 327 | /* Default to an LM90 if forced */ |
|---|
| 328 | if (kind == 0) |
|---|
| 329 | kind = lm90; |
|---|
| 330 | |
|---|
| 331 | if (kind < 0) /* detection and identification */ |
|---|
| 332 | { |
|---|
| 333 | u8 man_id, chip_id, reg_config1, reg_convrate; |
|---|
| 334 | |
|---|
| 335 | man_id = i2c_smbus_read_byte_data(new_client, |
|---|
| 336 | LM90_REG_R_MAN_ID); |
|---|
| 337 | chip_id = i2c_smbus_read_byte_data(new_client, |
|---|
| 338 | LM90_REG_R_CHIP_ID); |
|---|
| 339 | reg_config1 = i2c_smbus_read_byte_data(new_client, |
|---|
| 340 | LM90_REG_R_CONFIG1); |
|---|
| 341 | reg_convrate = i2c_smbus_read_byte_data(new_client, |
|---|
| 342 | LM90_REG_R_CONVRATE); |
|---|
| 343 | |
|---|
| 344 | if (man_id == 0x01) /* National Semiconductor */ |
|---|
| 345 | { |
|---|
| 346 | u8 reg_config2; |
|---|
| 347 | |
|---|
| 348 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
|---|
| 349 | LM90_REG_R_CONFIG2); |
|---|
| 350 | |
|---|
| 351 | if ((reg_config1 & 0x2A) == 0x00 |
|---|
| 352 | && (reg_config2 & 0xF8) == 0x00 |
|---|
| 353 | && reg_convrate <= 0x09) |
|---|
| 354 | { |
|---|
| 355 | if (address == 0x4C |
|---|
| 356 | && (chip_id & 0xF0) == 0x20) /* LM90 */ |
|---|
| 357 | kind = lm90; |
|---|
| 358 | else if ((chip_id & 0xF0) == 0x30) /* LM89/LM99 */ |
|---|
| 359 | kind = lm99; |
|---|
| 360 | else if (address == 0x4C |
|---|
| 361 | && (chip_id & 0xF0) == 0x10) /* LM86 */ |
|---|
| 362 | kind = lm99; |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | else if (man_id == 0x41) /* Analog Devices */ |
|---|
| 366 | { |
|---|
| 367 | if (address == 0x4C |
|---|
| 368 | && (chip_id & 0xF0) == 0x40 /* ADM1032 */ |
|---|
| 369 | && (reg_config1 & 0x3F) == 0x00 |
|---|
| 370 | && reg_convrate <= 0x0A) |
|---|
| 371 | kind = adm1032; |
|---|
| 372 | } |
|---|
| 373 | else if (man_id == 0x4D) /* Maxim */ |
|---|
| 374 | { |
|---|
| 375 | if (address == 0x4C |
|---|
| 376 | && (reg_config1 & 0x1F) == 0 |
|---|
| 377 | && reg_convrate <= 0x09) |
|---|
| 378 | kind = max6657; |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | if (kind <= 0) /* identification failed */ |
|---|
| 383 | { |
|---|
| 384 | printk("lm90.o: Unsupported chip.\n"); |
|---|
| 385 | goto ERROR1; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | if (kind == lm90) |
|---|
| 389 | { |
|---|
| 390 | type_name = "lm90"; |
|---|
| 391 | client_name = "LM90 chip"; |
|---|
| 392 | } |
|---|
| 393 | else if (kind == adm1032) |
|---|
| 394 | { |
|---|
| 395 | type_name = "adm1032"; |
|---|
| 396 | client_name = "ADM1032 chip"; |
|---|
| 397 | } |
|---|
| 398 | else if (kind == lm99) |
|---|
| 399 | { |
|---|
| 400 | type_name = "lm99"; |
|---|
| 401 | client_name = "LM99 chip"; |
|---|
| 402 | } |
|---|
| 403 | else if (kind == lm86) |
|---|
| 404 | { |
|---|
| 405 | type_name = "lm86"; |
|---|
| 406 | client_name = "LM86 chip"; |
|---|
| 407 | } |
|---|
| 408 | else if (kind == max6657) |
|---|
| 409 | { |
|---|
| 410 | type_name = "max6657"; |
|---|
| 411 | client_name = "MAX6657 chip"; |
|---|
| 412 | } |
|---|
| 413 | else |
|---|
| 414 | { |
|---|
| 415 | printk("lm90.o: Unknown kind %d.\n", kind); |
|---|
| 416 | goto ERROR1; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | /* |
|---|
| 420 | * OK, we got a valid chip so we can fill in the remaining client |
|---|
| 421 | * fields. |
|---|
| 422 | */ |
|---|
| 423 | |
|---|
| 424 | strcpy(new_client->name, client_name); |
|---|
| 425 | new_client->id = lm90_id++; |
|---|
| 426 | data->valid = 0; |
|---|
| 427 | init_MUTEX(&data->update_lock); |
|---|
| 428 | |
|---|
| 429 | /* |
|---|
| 430 | * Tell the I2C layer a new client has arrived. |
|---|
| 431 | */ |
|---|
| 432 | |
|---|
| 433 | if ((err = i2c_attach_client(new_client))) |
|---|
| 434 | { |
|---|
| 435 | #ifdef DEBUG |
|---|
| 436 | printk("lm90.o: Failed attaching client.\n"); |
|---|
| 437 | #endif |
|---|
| 438 | goto ERROR1; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | /* |
|---|
| 442 | * Register a new directory entry. |
|---|
| 443 | */ |
|---|
| 444 | |
|---|
| 445 | if ((err = i2c_register_entry(new_client, type_name, |
|---|
| 446 | lm90_dir_table_template, THIS_MODULE)) < 0) |
|---|
| 447 | { |
|---|
| 448 | #ifdef DEBUG |
|---|
| 449 | printk("lm90.o: Failed registering directory entry.\n"); |
|---|
| 450 | #endif |
|---|
| 451 | goto ERROR2; |
|---|
| 452 | } |
|---|
| 453 | data->sysctl_id = err; |
|---|
| 454 | |
|---|
| 455 | /* |
|---|
| 456 | * Initialize the LM90 chip. |
|---|
| 457 | */ |
|---|
| 458 | |
|---|
| 459 | lm90_init_client(new_client); |
|---|
| 460 | return 0; |
|---|
| 461 | |
|---|
| 462 | ERROR2: |
|---|
| 463 | i2c_detach_client(new_client); |
|---|
| 464 | ERROR1: |
|---|
| 465 | kfree(data); |
|---|
| 466 | return err; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | static void lm90_init_client(struct i2c_client *client) |
|---|
| 470 | { |
|---|
| 471 | u8 config; |
|---|
| 472 | |
|---|
| 473 | /* |
|---|
| 474 | * Start the conversions. |
|---|
| 475 | */ |
|---|
| 476 | |
|---|
| 477 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, |
|---|
| 478 | 5); /* 2 Hz */ |
|---|
| 479 | config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1); |
|---|
| 480 | if (config & 0x40) |
|---|
| 481 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, |
|---|
| 482 | config & 0xBF); /* run */ |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | static int lm90_detach_client(struct i2c_client *client) |
|---|
| 487 | { |
|---|
| 488 | int err; |
|---|
| 489 | |
|---|
| 490 | i2c_deregister_entry(((struct lm90_data *) (client->data))->sysctl_id); |
|---|
| 491 | if ((err = i2c_detach_client(client))) |
|---|
| 492 | { |
|---|
| 493 | printk("lm90.o: Client deregistration failed, client not " |
|---|
| 494 | "detached.\n"); |
|---|
| 495 | return err; |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | kfree(client->data); |
|---|
| 499 | return 0; |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | static void lm90_update_client(struct i2c_client *client) |
|---|
| 503 | { |
|---|
| 504 | struct lm90_data *data = client->data; |
|---|
| 505 | |
|---|
| 506 | down(&data->update_lock); |
|---|
| 507 | |
|---|
| 508 | if ((jiffies - data->last_updated > HZ * 2) || |
|---|
| 509 | (jiffies < data->last_updated) || !data->valid) |
|---|
| 510 | { |
|---|
| 511 | u8 oldh, newh; |
|---|
| 512 | #ifdef DEBUG |
|---|
| 513 | printk("lm90.o: Updating data.\n"); |
|---|
| 514 | #endif |
|---|
| 515 | |
|---|
| 516 | data->local_temp = |
|---|
| 517 | i2c_smbus_read_byte_data(client, LM90_REG_R_LOCAL_TEMP); |
|---|
| 518 | data->local_high = |
|---|
| 519 | i2c_smbus_read_byte_data(client, LM90_REG_R_LOCAL_HIGH); |
|---|
| 520 | data->local_low = |
|---|
| 521 | i2c_smbus_read_byte_data(client, LM90_REG_R_LOCAL_LOW); |
|---|
| 522 | data->local_crit = |
|---|
| 523 | i2c_smbus_read_byte_data(client, LM90_REG_R_LOCAL_CRIT); |
|---|
| 524 | |
|---|
| 525 | /* |
|---|
| 526 | * There is a trick here. We have to read two registers to |
|---|
| 527 | * have the remote sensor temperature, but we have to beware |
|---|
| 528 | * a conversion could occur inbetween the readings. The |
|---|
| 529 | * datasheet says we should either use the one-shot |
|---|
| 530 | * conversion register, which we don't want to do (disables |
|---|
| 531 | * hardware monitoring) or monitor the busy bit, which is |
|---|
| 532 | * impossible (we can't read the values and monitor that bit |
|---|
| 533 | * at the exact same time). So the solution used here is to |
|---|
| 534 | * read the high byte once, then the low byte, then the high |
|---|
| 535 | * byte again. If the new high byte matches the old one, |
|---|
| 536 | * then we have a valid reading. Else we have to read the low |
|---|
| 537 | * byte again, and now we believe we have a correct reading. |
|---|
| 538 | */ |
|---|
| 539 | |
|---|
| 540 | oldh = |
|---|
| 541 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_TEMPH); |
|---|
| 542 | data->remote_temp = |
|---|
| 543 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_TEMPL); |
|---|
| 544 | newh = |
|---|
| 545 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_TEMPH); |
|---|
| 546 | if (newh != oldh) |
|---|
| 547 | { |
|---|
| 548 | data->remote_temp = |
|---|
| 549 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_TEMPL); |
|---|
| 550 | #ifdef DEBUG |
|---|
| 551 | oldh = /* actually newer */ |
|---|
| 552 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_TEMPH); |
|---|
| 553 | if (newh != oldh) |
|---|
| 554 | printk("lm90.o: Remote temperature may be wrong.\n"); |
|---|
| 555 | #endif |
|---|
| 556 | } |
|---|
| 557 | data->remote_temp |= (newh << 8); |
|---|
| 558 | data->remote_high = |
|---|
| 559 | (i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_HIGHH) << 8) |
|---|
| 560 | + i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_HIGHL); |
|---|
| 561 | data->remote_low = |
|---|
| 562 | (i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_LOWH) << 8) |
|---|
| 563 | + i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_LOWL); |
|---|
| 564 | data->remote_crit = |
|---|
| 565 | i2c_smbus_read_byte_data(client, LM90_REG_R_REMOTE_CRIT); |
|---|
| 566 | |
|---|
| 567 | data->hyst = |
|---|
| 568 | i2c_smbus_read_byte_data(client, LM90_REG_R_TCRIT_HYST); |
|---|
| 569 | data->alarms = |
|---|
| 570 | i2c_smbus_read_byte_data(client, LM90_REG_R_STATUS); |
|---|
| 571 | |
|---|
| 572 | data->last_updated = jiffies; |
|---|
| 573 | data->valid = 1; |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | up(&data->update_lock); |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | static void lm90_local_temp(struct i2c_client *client, int operation, |
|---|
| 580 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 581 | { |
|---|
| 582 | struct lm90_data *data = client->data; |
|---|
| 583 | |
|---|
| 584 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 585 | *nrels_mag = 0; /* magnitude */ |
|---|
| 586 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 587 | { |
|---|
| 588 | lm90_update_client(client); |
|---|
| 589 | results[0] = TEMP1_FROM_REG(data->local_high); |
|---|
| 590 | results[1] = TEMP1_FROM_REG(data->local_low); |
|---|
| 591 | results[2] = TEMP1_FROM_REG(data->local_temp); |
|---|
| 592 | *nrels_mag = 3; |
|---|
| 593 | } |
|---|
| 594 | else if (operation == SENSORS_PROC_REAL_WRITE) |
|---|
| 595 | { |
|---|
| 596 | if (*nrels_mag >= 1) |
|---|
| 597 | { |
|---|
| 598 | data->local_high = TEMP1_TO_REG(results[0]); |
|---|
| 599 | i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_HIGH, |
|---|
| 600 | data->local_high); |
|---|
| 601 | } |
|---|
| 602 | if (*nrels_mag >= 2) |
|---|
| 603 | { |
|---|
| 604 | data->local_low = TEMP1_TO_REG(results[1]); |
|---|
| 605 | i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_LOW, |
|---|
| 606 | data->local_low); |
|---|
| 607 | } |
|---|
| 608 | } |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | static void lm90_remote_temp(struct i2c_client *client, int operation, |
|---|
| 612 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 613 | { |
|---|
| 614 | struct lm90_data *data = client->data; |
|---|
| 615 | |
|---|
| 616 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 617 | *nrels_mag = 1; /* magnitude */ |
|---|
| 618 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 619 | { |
|---|
| 620 | lm90_update_client(client); |
|---|
| 621 | results[0] = TEMP2_FROM_REG(data->remote_high); |
|---|
| 622 | results[1] = TEMP2_FROM_REG(data->remote_low); |
|---|
| 623 | results[2] = TEMP2_FROM_REG(data->remote_temp); |
|---|
| 624 | *nrels_mag = 3; |
|---|
| 625 | } |
|---|
| 626 | else if (operation == SENSORS_PROC_REAL_WRITE) |
|---|
| 627 | { |
|---|
| 628 | if (*nrels_mag >= 1) |
|---|
| 629 | { |
|---|
| 630 | data->remote_high = TEMP2_TO_REG(results[0]); |
|---|
| 631 | i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_HIGHH, |
|---|
| 632 | data->remote_high >> 8); |
|---|
| 633 | i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_HIGHL, |
|---|
| 634 | data->remote_high & 0xFF); |
|---|
| 635 | } |
|---|
| 636 | if (*nrels_mag >= 2) |
|---|
| 637 | { |
|---|
| 638 | data->remote_low = TEMP2_TO_REG(results[1]); |
|---|
| 639 | i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_LOWH, |
|---|
| 640 | data->remote_low >> 8); |
|---|
| 641 | i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_LOWL, |
|---|
| 642 | data->remote_low & 0xFF); |
|---|
| 643 | } |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | static void lm90_local_tcrit(struct i2c_client *client, int operation, |
|---|
| 648 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 649 | { |
|---|
| 650 | struct lm90_data *data = client->data; |
|---|
| 651 | |
|---|
| 652 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 653 | *nrels_mag = 0; /* magnitude */ |
|---|
| 654 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 655 | { |
|---|
| 656 | lm90_update_client(client); |
|---|
| 657 | results[0] = TEMP1_FROM_REG(data->local_crit); |
|---|
| 658 | *nrels_mag = 1; |
|---|
| 659 | } |
|---|
| 660 | else if (operation == SENSORS_PROC_REAL_WRITE) |
|---|
| 661 | { |
|---|
| 662 | if (*nrels_mag >= 1) |
|---|
| 663 | { |
|---|
| 664 | data->local_crit = TEMP1_TO_REG(results[0]); |
|---|
| 665 | i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_CRIT, |
|---|
| 666 | data->local_crit); |
|---|
| 667 | } |
|---|
| 668 | } |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | static void lm90_remote_tcrit(struct i2c_client *client, int operation, |
|---|
| 672 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 673 | { |
|---|
| 674 | struct lm90_data *data = client->data; |
|---|
| 675 | |
|---|
| 676 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 677 | *nrels_mag = 0; /* magnitude */ |
|---|
| 678 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 679 | { |
|---|
| 680 | lm90_update_client(client); |
|---|
| 681 | results[0] = TEMP1_FROM_REG(data->remote_crit); |
|---|
| 682 | *nrels_mag = 1; |
|---|
| 683 | } |
|---|
| 684 | else if (operation == SENSORS_PROC_REAL_WRITE) |
|---|
| 685 | { |
|---|
| 686 | if (*nrels_mag >= 1) |
|---|
| 687 | { |
|---|
| 688 | data->remote_crit = TEMP1_TO_REG(results[0]); |
|---|
| 689 | i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_CRIT, |
|---|
| 690 | data->remote_crit); |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | /* |
|---|
| 696 | * One quick note about hysteresis. Internally, the hysteresis value |
|---|
| 697 | * is held in a single register by the LM90, as a relative value. |
|---|
| 698 | * This relative value applies to both the local critical temperature |
|---|
| 699 | * and the remote critical temperature. Since all temperatures exported |
|---|
| 700 | * through procfs have to be absolute, we have to do some conversions. |
|---|
| 701 | * The solution retained here is to export two absolute values, one for |
|---|
| 702 | * each critical temperature. In order not to confuse the users too |
|---|
| 703 | * much, only one file is writable. Would we fail to do so, users |
|---|
| 704 | * would probably attempt to write to both files, as if they were |
|---|
| 705 | * independant, and since they aren't, they wouldn't understand why |
|---|
| 706 | * setting one affects the other one (and would probably claim there's |
|---|
| 707 | * a bug in the driver). |
|---|
| 708 | */ |
|---|
| 709 | |
|---|
| 710 | static void lm90_local_hyst(struct i2c_client *client, int operation, |
|---|
| 711 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 712 | { |
|---|
| 713 | struct lm90_data *data = client->data; |
|---|
| 714 | |
|---|
| 715 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 716 | *nrels_mag = 0; /* magnitude */ |
|---|
| 717 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 718 | { |
|---|
| 719 | lm90_update_client(client); |
|---|
| 720 | results[0] = TEMP1_FROM_REG(data->local_crit) - |
|---|
| 721 | TEMP1_FROM_REG(data->hyst); |
|---|
| 722 | *nrels_mag = 1; |
|---|
| 723 | } |
|---|
| 724 | else if (operation == SENSORS_PROC_REAL_WRITE) |
|---|
| 725 | { |
|---|
| 726 | if (*nrels_mag >= 1) |
|---|
| 727 | { |
|---|
| 728 | data->hyst = HYST_TO_REG(data->local_crit - results[0]); |
|---|
| 729 | i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, |
|---|
| 730 | data->hyst); |
|---|
| 731 | } |
|---|
| 732 | } |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | static void lm90_remote_hyst(struct i2c_client *client, int operation, |
|---|
| 736 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 737 | { |
|---|
| 738 | struct lm90_data *data = client->data; |
|---|
| 739 | |
|---|
| 740 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 741 | *nrels_mag = 0; /* magnitude */ |
|---|
| 742 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 743 | { |
|---|
| 744 | lm90_update_client(client); |
|---|
| 745 | results[0] = TEMP1_FROM_REG(data->remote_crit) - |
|---|
| 746 | TEMP1_FROM_REG(data->hyst); |
|---|
| 747 | *nrels_mag = 1; |
|---|
| 748 | } |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | static void lm90_alarms(struct i2c_client *client, int operation, |
|---|
| 752 | int ctl_name, int *nrels_mag, long *results) |
|---|
| 753 | { |
|---|
| 754 | struct lm90_data *data = client->data; |
|---|
| 755 | |
|---|
| 756 | if (operation == SENSORS_PROC_REAL_INFO) |
|---|
| 757 | *nrels_mag = 0; /* magnitude */ |
|---|
| 758 | else if (operation == SENSORS_PROC_REAL_READ) |
|---|
| 759 | { |
|---|
| 760 | lm90_update_client(client); |
|---|
| 761 | results[0] = data->alarms; |
|---|
| 762 | *nrels_mag = 1; |
|---|
| 763 | } |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | static int __init sm_lm90_init(void) |
|---|
| 767 | { |
|---|
| 768 | printk(KERN_INFO "lm90.o version %s (%s)\n", LM_VERSION, LM_DATE); |
|---|
| 769 | return i2c_add_driver(&lm90_driver); |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | static void __exit sm_lm90_exit(void) |
|---|
| 773 | { |
|---|
| 774 | i2c_del_driver(&lm90_driver); |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
|---|
| 778 | MODULE_DESCRIPTION("LM90/ADM1032 sensor driver"); |
|---|
| 779 | MODULE_LICENSE("GPL"); |
|---|
| 780 | |
|---|
| 781 | module_init(sm_lm90_init); |
|---|
| 782 | module_exit(sm_lm90_exit); |
|---|