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 | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 4 | * Copyright (C) 2003-2010 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 |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 15 | * degrees, which allows for higher temperatures measurement. |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 16 | * Note that there is no way to differentiate between both chips. |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 17 | * When device is auto-detected, the driver will assume an LM99. |
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 |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 31 | * variants. We use the device address to detect MAX6659, which will result |
| 32 | * in a detection as max6657 if it is on address 0x4c. The extra address |
| 33 | * and features of the MAX6659 are only supported if the chip is configured |
| 34 | * explicitly as max6659, or if its address is not 0x4c. |
| 35 | * These chips lack the remote temperature offset feature. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * |
Darrick J. Wong | 1a51e06 | 2009-03-12 13:36:38 +0100 | [diff] [blame] | 37 | * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and |
| 38 | * MAX6692 chips made by Maxim. These are again similar to the LM86, |
| 39 | * but they use unsigned temperature values and can report temperatures |
| 40 | * from 0 to 145 degrees. |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 41 | * |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 42 | * This driver also supports the MAX6680 and MAX6681, two other sensor |
| 43 | * chips made by Maxim. These are quite similar to the other Maxim |
Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 44 | * chips. The MAX6680 and MAX6681 only differ in the pinout so they can |
| 45 | * be treated identically. |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 46 | * |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 47 | * This driver also supports the MAX6695 and MAX6696, two other sensor |
| 48 | * chips made by Maxim. These are also quite similar to other Maxim |
| 49 | * chips, but support three temperature sensors instead of two. MAX6695 |
| 50 | * and MAX6696 only differ in the pinout so they can be treated identically. |
| 51 | * |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 52 | * This driver also supports the ADT7461 chip from Analog Devices. |
| 53 | * It's supported in both compatibility and extended mode. It is mostly |
| 54 | * compatible with LM90 except for a data format difference for the |
| 55 | * temperature value registers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * |
| 57 | * Since the LM90 was the first chipset supported by this driver, most |
| 58 | * comments will refer to this chipset, but are actually general and |
| 59 | * concern all supported chipsets, unless mentioned otherwise. |
| 60 | * |
| 61 | * This program is free software; you can redistribute it and/or modify |
| 62 | * it under the terms of the GNU General Public License as published by |
| 63 | * the Free Software Foundation; either version 2 of the License, or |
| 64 | * (at your option) any later version. |
| 65 | * |
| 66 | * This program is distributed in the hope that it will be useful, |
| 67 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 68 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 69 | * GNU General Public License for more details. |
| 70 | * |
| 71 | * You should have received a copy of the GNU General Public License |
| 72 | * along with this program; if not, write to the Free Software |
| 73 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 74 | */ |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/module.h> |
| 77 | #include <linux/init.h> |
| 78 | #include <linux/slab.h> |
| 79 | #include <linux/jiffies.h> |
| 80 | #include <linux/i2c.h> |
Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 81 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 82 | #include <linux/hwmon.h> |
| 83 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 84 | #include <linux/mutex.h> |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 85 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Addresses to scan |
| 89 | * Address is fully defined internally and cannot be changed except for |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 90 | * MAX6659, MAX6680 and MAX6681. |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 91 | * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657 |
| 92 | * and MAX6658 have address 0x4c. |
| 93 | * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d. |
| 94 | * MAX6647 has address 0x4e. |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 95 | * MAX6659 can have address 0x4c, 0x4d or 0x4e. |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 96 | * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, |
| 97 | * 0x4c, 0x4d or 0x4e. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | */ |
| 99 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 100 | static const unsigned short normal_i2c[] = { |
| 101 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 103 | enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680, |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 104 | max6646, w83l771, max6696 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * The LM90 registers |
| 108 | */ |
| 109 | |
| 110 | #define LM90_REG_R_MAN_ID 0xFE |
| 111 | #define LM90_REG_R_CHIP_ID 0xFF |
| 112 | #define LM90_REG_R_CONFIG1 0x03 |
| 113 | #define LM90_REG_W_CONFIG1 0x09 |
| 114 | #define LM90_REG_R_CONFIG2 0xBF |
| 115 | #define LM90_REG_W_CONFIG2 0xBF |
| 116 | #define LM90_REG_R_CONVRATE 0x04 |
| 117 | #define LM90_REG_W_CONVRATE 0x0A |
| 118 | #define LM90_REG_R_STATUS 0x02 |
| 119 | #define LM90_REG_R_LOCAL_TEMP 0x00 |
| 120 | #define LM90_REG_R_LOCAL_HIGH 0x05 |
| 121 | #define LM90_REG_W_LOCAL_HIGH 0x0B |
| 122 | #define LM90_REG_R_LOCAL_LOW 0x06 |
| 123 | #define LM90_REG_W_LOCAL_LOW 0x0C |
| 124 | #define LM90_REG_R_LOCAL_CRIT 0x20 |
| 125 | #define LM90_REG_W_LOCAL_CRIT 0x20 |
| 126 | #define LM90_REG_R_REMOTE_TEMPH 0x01 |
| 127 | #define LM90_REG_R_REMOTE_TEMPL 0x10 |
| 128 | #define LM90_REG_R_REMOTE_OFFSH 0x11 |
| 129 | #define LM90_REG_W_REMOTE_OFFSH 0x11 |
| 130 | #define LM90_REG_R_REMOTE_OFFSL 0x12 |
| 131 | #define LM90_REG_W_REMOTE_OFFSL 0x12 |
| 132 | #define LM90_REG_R_REMOTE_HIGHH 0x07 |
| 133 | #define LM90_REG_W_REMOTE_HIGHH 0x0D |
| 134 | #define LM90_REG_R_REMOTE_HIGHL 0x13 |
| 135 | #define LM90_REG_W_REMOTE_HIGHL 0x13 |
| 136 | #define LM90_REG_R_REMOTE_LOWH 0x08 |
| 137 | #define LM90_REG_W_REMOTE_LOWH 0x0E |
| 138 | #define LM90_REG_R_REMOTE_LOWL 0x14 |
| 139 | #define LM90_REG_W_REMOTE_LOWL 0x14 |
| 140 | #define LM90_REG_R_REMOTE_CRIT 0x19 |
| 141 | #define LM90_REG_W_REMOTE_CRIT 0x19 |
| 142 | #define LM90_REG_R_TCRIT_HYST 0x21 |
| 143 | #define LM90_REG_W_TCRIT_HYST 0x21 |
| 144 | |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 145 | /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */ |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 146 | |
| 147 | #define MAX6657_REG_R_LOCAL_TEMPL 0x11 |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 148 | #define MAX6696_REG_R_STATUS2 0x12 |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 149 | #define MAX6659_REG_R_REMOTE_EMERG 0x16 |
| 150 | #define MAX6659_REG_W_REMOTE_EMERG 0x16 |
| 151 | #define MAX6659_REG_R_LOCAL_EMERG 0x17 |
| 152 | #define MAX6659_REG_W_LOCAL_EMERG 0x17 |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | /* |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 155 | * Device flags |
| 156 | */ |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 157 | #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */ |
| 158 | /* Device features */ |
| 159 | #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */ |
| 160 | #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */ |
| 161 | #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */ |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 162 | #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */ |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 163 | #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */ |
| 164 | #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */ |
Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame^] | 165 | #define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */ |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 166 | |
| 167 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * Driver data (common to all clients) |
| 169 | */ |
| 170 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 171 | static const struct i2c_device_id lm90_id[] = { |
| 172 | { "adm1032", adm1032 }, |
| 173 | { "adt7461", adt7461 }, |
| 174 | { "lm90", lm90 }, |
| 175 | { "lm86", lm86 }, |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 176 | { "lm89", lm86 }, |
| 177 | { "lm99", lm99 }, |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 178 | { "max6646", max6646 }, |
| 179 | { "max6647", max6646 }, |
| 180 | { "max6649", max6646 }, |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 181 | { "max6657", max6657 }, |
| 182 | { "max6658", max6657 }, |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 183 | { "max6659", max6659 }, |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 184 | { "max6680", max6680 }, |
| 185 | { "max6681", max6680 }, |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 186 | { "max6695", max6696 }, |
| 187 | { "max6696", max6696 }, |
Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 188 | { "w83l771", w83l771 }, |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 189 | { } |
| 190 | }; |
| 191 | MODULE_DEVICE_TABLE(i2c, lm90_id); |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 194 | * chip type specific parameters |
| 195 | */ |
| 196 | struct lm90_params { |
| 197 | u32 flags; /* Capabilities */ |
| 198 | u16 alert_alarms; /* Which alarm bits trigger ALERT# */ |
| 199 | /* Upper 8 bits for max6695/96 */ |
| 200 | }; |
| 201 | |
| 202 | static const struct lm90_params lm90_params[] = { |
| 203 | [adm1032] = { |
Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame^] | 204 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT |
| 205 | | LM90_HAVE_BROKEN_ALERT, |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 206 | .alert_alarms = 0x7c, |
| 207 | }, |
| 208 | [adt7461] = { |
Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame^] | 209 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT |
| 210 | | LM90_HAVE_BROKEN_ALERT, |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 211 | .alert_alarms = 0x7c, |
| 212 | }, |
| 213 | [lm86] = { |
| 214 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, |
| 215 | .alert_alarms = 0x7b, |
| 216 | }, |
| 217 | [lm90] = { |
| 218 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, |
| 219 | .alert_alarms = 0x7b, |
| 220 | }, |
| 221 | [lm99] = { |
| 222 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, |
| 223 | .alert_alarms = 0x7b, |
| 224 | }, |
| 225 | [max6646] = { |
| 226 | .flags = LM90_HAVE_LOCAL_EXT, |
| 227 | .alert_alarms = 0x7c, |
| 228 | }, |
| 229 | [max6657] = { |
| 230 | .flags = LM90_HAVE_LOCAL_EXT, |
| 231 | .alert_alarms = 0x7c, |
| 232 | }, |
| 233 | [max6659] = { |
| 234 | .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY, |
| 235 | .alert_alarms = 0x7c, |
| 236 | }, |
| 237 | [max6680] = { |
| 238 | .flags = LM90_HAVE_OFFSET, |
| 239 | .alert_alarms = 0x7c, |
| 240 | }, |
| 241 | [max6696] = { |
| 242 | .flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY |
| 243 | | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3, |
| 244 | .alert_alarms = 0x187c, |
| 245 | }, |
| 246 | [w83l771] = { |
| 247 | .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, |
| 248 | .alert_alarms = 0x7c, |
| 249 | }, |
| 250 | }; |
| 251 | |
| 252 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * Client data (each client gets its own) |
| 254 | */ |
| 255 | |
| 256 | struct lm90_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 257 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 258 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | char valid; /* zero until following fields are valid */ |
| 260 | unsigned long last_updated; /* in jiffies */ |
| 261 | int kind; |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 262 | u32 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 264 | u8 config_orig; /* Original configuration register value */ |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 265 | u16 alert_alarms; /* Which alarm bits trigger ALERT# */ |
| 266 | /* Upper 8 bits for max6695/96 */ |
Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | /* registers values */ |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 269 | s8 temp8[8]; /* 0: local low limit |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 270 | 1: local high limit |
| 271 | 2: local critical limit |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 272 | 3: remote critical limit |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 273 | 4: local emergency limit (max6659 and max6695/96) |
| 274 | 5: remote emergency limit (max6659 and max6695/96) |
| 275 | 6: remote 2 critical limit (max6695/96 only) |
| 276 | 7: remote 2 emergency limit (max6695/96 only) */ |
| 277 | s16 temp11[8]; /* 0: remote input |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 278 | 1: remote low limit |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 279 | 2: remote high limit |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 280 | 3: remote offset (except max6646, max6657/58/59, |
| 281 | and max6695/96) |
| 282 | 4: local input |
| 283 | 5: remote 2 input (max6695/96 only) |
| 284 | 6: remote 2 low limit (max6695/96 only) |
| 285 | 7: remote 2 high limit (ma6695/96 only) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | u8 temp_hyst; |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 287 | u16 alarms; /* bitvector (upper 8 bits for max6695/96) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | /* |
Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 291 | * Support functions |
| 292 | */ |
| 293 | |
| 294 | /* |
| 295 | * The ADM1032 supports PEC but not on write byte transactions, so we need |
| 296 | * to explicitly ask for a transaction without PEC. |
| 297 | */ |
| 298 | static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value) |
| 299 | { |
| 300 | return i2c_smbus_xfer(client->adapter, client->addr, |
| 301 | client->flags & ~I2C_CLIENT_PEC, |
| 302 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * It is assumed that client->update_lock is held (unless we are in |
| 307 | * detection or initialization steps). This matters when PEC is enabled, |
| 308 | * because we don't want the address pointer to change between the write |
| 309 | * byte and the read byte transactions. |
| 310 | */ |
| 311 | static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value) |
| 312 | { |
| 313 | int err; |
| 314 | |
| 315 | if (client->flags & I2C_CLIENT_PEC) { |
| 316 | err = adm1032_write_byte(client, reg); |
| 317 | if (err >= 0) |
| 318 | err = i2c_smbus_read_byte(client); |
| 319 | } else |
| 320 | err = i2c_smbus_read_byte_data(client, reg); |
| 321 | |
| 322 | if (err < 0) { |
| 323 | dev_warn(&client->dev, "Register %#02x read failed (%d)\n", |
| 324 | reg, err); |
| 325 | return err; |
| 326 | } |
| 327 | *value = err; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) |
| 333 | { |
| 334 | int err; |
| 335 | u8 oldh, newh, l; |
| 336 | |
| 337 | /* |
| 338 | * There is a trick here. We have to read two registers to have the |
| 339 | * sensor temperature, but we have to beware a conversion could occur |
| 340 | * inbetween the readings. The datasheet says we should either use |
| 341 | * the one-shot conversion register, which we don't want to do |
| 342 | * (disables hardware monitoring) or monitor the busy bit, which is |
| 343 | * impossible (we can't read the values and monitor that bit at the |
| 344 | * exact same time). So the solution used here is to read the high |
| 345 | * byte once, then the low byte, then the high byte again. If the new |
| 346 | * high byte matches the old one, then we have a valid reading. Else |
| 347 | * we have to read the low byte again, and now we believe we have a |
| 348 | * correct reading. |
| 349 | */ |
| 350 | if ((err = lm90_read_reg(client, regh, &oldh)) |
| 351 | || (err = lm90_read_reg(client, regl, &l)) |
| 352 | || (err = lm90_read_reg(client, regh, &newh))) |
| 353 | return err; |
| 354 | if (oldh != newh) { |
| 355 | err = lm90_read_reg(client, regl, &l); |
| 356 | if (err) |
| 357 | return err; |
| 358 | } |
| 359 | *value = (newh << 8) | l; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * client->update_lock must be held when calling this function (unless we are |
| 366 | * in detection or initialization steps), and while a remote channel other |
| 367 | * than channel 0 is selected. Also, calling code must make sure to re-select |
| 368 | * external channel 0 before releasing the lock. This is necessary because |
| 369 | * various registers have different meanings as a result of selecting a |
| 370 | * non-default remote channel. |
| 371 | */ |
| 372 | static inline void lm90_select_remote_channel(struct i2c_client *client, |
| 373 | struct lm90_data *data, |
| 374 | int channel) |
| 375 | { |
| 376 | u8 config; |
| 377 | |
| 378 | if (data->kind == max6696) { |
| 379 | lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); |
| 380 | config &= ~0x08; |
| 381 | if (channel) |
| 382 | config |= 0x08; |
| 383 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, |
| 384 | config); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static struct lm90_data *lm90_update_device(struct device *dev) |
| 389 | { |
| 390 | struct i2c_client *client = to_i2c_client(dev); |
| 391 | struct lm90_data *data = i2c_get_clientdata(client); |
| 392 | |
| 393 | mutex_lock(&data->update_lock); |
| 394 | |
| 395 | if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10) |
| 396 | || !data->valid) { |
| 397 | u8 h, l; |
| 398 | u8 alarms; |
| 399 | |
| 400 | dev_dbg(&client->dev, "Updating lm90 data.\n"); |
| 401 | lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]); |
| 402 | lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]); |
| 403 | lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]); |
| 404 | lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]); |
| 405 | lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst); |
| 406 | |
| 407 | if (data->flags & LM90_HAVE_LOCAL_EXT) { |
| 408 | lm90_read16(client, LM90_REG_R_LOCAL_TEMP, |
| 409 | MAX6657_REG_R_LOCAL_TEMPL, |
| 410 | &data->temp11[4]); |
| 411 | } else { |
| 412 | if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, |
| 413 | &h) == 0) |
| 414 | data->temp11[4] = h << 8; |
| 415 | } |
| 416 | lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, |
| 417 | LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]); |
| 418 | |
| 419 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) { |
| 420 | data->temp11[1] = h << 8; |
| 421 | if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) |
| 422 | && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, |
| 423 | &l) == 0) |
| 424 | data->temp11[1] |= l; |
| 425 | } |
| 426 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) { |
| 427 | data->temp11[2] = h << 8; |
| 428 | if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) |
| 429 | && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, |
| 430 | &l) == 0) |
| 431 | data->temp11[2] |= l; |
| 432 | } |
| 433 | |
| 434 | if (data->flags & LM90_HAVE_OFFSET) { |
| 435 | if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH, |
| 436 | &h) == 0 |
| 437 | && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL, |
| 438 | &l) == 0) |
| 439 | data->temp11[3] = (h << 8) | l; |
| 440 | } |
| 441 | if (data->flags & LM90_HAVE_EMERGENCY) { |
| 442 | lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG, |
| 443 | &data->temp8[4]); |
| 444 | lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG, |
| 445 | &data->temp8[5]); |
| 446 | } |
| 447 | lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); |
| 448 | data->alarms = alarms; /* save as 16 bit value */ |
| 449 | |
| 450 | if (data->kind == max6696) { |
| 451 | lm90_select_remote_channel(client, data, 1); |
| 452 | lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, |
| 453 | &data->temp8[6]); |
| 454 | lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG, |
| 455 | &data->temp8[7]); |
| 456 | lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, |
| 457 | LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]); |
| 458 | if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h)) |
| 459 | data->temp11[6] = h << 8; |
| 460 | if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h)) |
| 461 | data->temp11[7] = h << 8; |
| 462 | lm90_select_remote_channel(client, data, 0); |
| 463 | |
| 464 | if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2, |
| 465 | &alarms)) |
| 466 | data->alarms |= alarms << 8; |
| 467 | } |
| 468 | |
| 469 | /* Re-enable ALERT# output if it was originally enabled and |
| 470 | * relevant alarms are all clear */ |
| 471 | if ((data->config_orig & 0x80) == 0 |
| 472 | && (data->alarms & data->alert_alarms) == 0) { |
| 473 | u8 config; |
| 474 | |
| 475 | lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); |
| 476 | if (config & 0x80) { |
| 477 | dev_dbg(&client->dev, "Re-enabling ALERT#\n"); |
| 478 | i2c_smbus_write_byte_data(client, |
| 479 | LM90_REG_W_CONFIG1, |
| 480 | config & ~0x80); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | data->last_updated = jiffies; |
| 485 | data->valid = 1; |
| 486 | } |
| 487 | |
| 488 | mutex_unlock(&data->update_lock); |
| 489 | |
| 490 | return data; |
| 491 | } |
| 492 | |
| 493 | /* |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 494 | * Conversions |
| 495 | * For local temperatures and limits, critical limits and the hysteresis |
| 496 | * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. |
| 497 | * For remote temperatures and limits, it uses signed 11-bit values with |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 498 | * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some |
| 499 | * Maxim chips use unsigned values. |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 500 | */ |
| 501 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 502 | static inline int temp_from_s8(s8 val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 503 | { |
| 504 | return val * 1000; |
| 505 | } |
| 506 | |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 507 | static inline int temp_from_u8(u8 val) |
| 508 | { |
| 509 | return val * 1000; |
| 510 | } |
| 511 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 512 | static inline int temp_from_s16(s16 val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 513 | { |
| 514 | return val / 32 * 125; |
| 515 | } |
| 516 | |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 517 | static inline int temp_from_u16(u16 val) |
| 518 | { |
| 519 | return val / 32 * 125; |
| 520 | } |
| 521 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 522 | static s8 temp_to_s8(long val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 523 | { |
| 524 | if (val <= -128000) |
| 525 | return -128; |
| 526 | if (val >= 127000) |
| 527 | return 127; |
| 528 | if (val < 0) |
| 529 | return (val - 500) / 1000; |
| 530 | return (val + 500) / 1000; |
| 531 | } |
| 532 | |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 533 | static u8 temp_to_u8(long val) |
| 534 | { |
| 535 | if (val <= 0) |
| 536 | return 0; |
| 537 | if (val >= 255000) |
| 538 | return 255; |
| 539 | return (val + 500) / 1000; |
| 540 | } |
| 541 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 542 | static s16 temp_to_s16(long val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 543 | { |
| 544 | if (val <= -128000) |
| 545 | return 0x8000; |
| 546 | if (val >= 127875) |
| 547 | return 0x7FE0; |
| 548 | if (val < 0) |
| 549 | return (val - 62) / 125 * 32; |
| 550 | return (val + 62) / 125 * 32; |
| 551 | } |
| 552 | |
| 553 | static u8 hyst_to_reg(long val) |
| 554 | { |
| 555 | if (val <= 0) |
| 556 | return 0; |
| 557 | if (val >= 30500) |
| 558 | return 31; |
| 559 | return (val + 500) / 1000; |
| 560 | } |
| 561 | |
| 562 | /* |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 563 | * ADT7461 in compatibility mode is almost identical to LM90 except that |
| 564 | * attempts to write values that are outside the range 0 < temp < 127 are |
| 565 | * treated as the boundary value. |
| 566 | * |
| 567 | * ADT7461 in "extended mode" operation uses unsigned integers offset by |
| 568 | * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC. |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 569 | */ |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 570 | static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 571 | { |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 572 | if (data->flags & LM90_FLAG_ADT7461_EXT) |
| 573 | return (val - 64) * 1000; |
| 574 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 575 | return temp_from_s8(val); |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 578 | static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val) |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 579 | { |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 580 | if (data->flags & LM90_FLAG_ADT7461_EXT) |
| 581 | return (val - 0x4000) / 64 * 250; |
| 582 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 583 | return temp_from_s16(val); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 586 | static u8 temp_to_u8_adt7461(struct lm90_data *data, long val) |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 587 | { |
| 588 | if (data->flags & LM90_FLAG_ADT7461_EXT) { |
| 589 | if (val <= -64000) |
| 590 | return 0; |
| 591 | if (val >= 191000) |
| 592 | return 0xFF; |
| 593 | return (val + 500 + 64000) / 1000; |
| 594 | } else { |
| 595 | if (val <= 0) |
| 596 | return 0; |
| 597 | if (val >= 127000) |
| 598 | return 127; |
| 599 | return (val + 500) / 1000; |
| 600 | } |
| 601 | } |
| 602 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 603 | static u16 temp_to_u16_adt7461(struct lm90_data *data, long val) |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 604 | { |
| 605 | if (data->flags & LM90_FLAG_ADT7461_EXT) { |
| 606 | if (val <= -64000) |
| 607 | return 0; |
| 608 | if (val >= 191750) |
| 609 | return 0xFFC0; |
| 610 | return (val + 64000 + 125) / 250 * 64; |
| 611 | } else { |
| 612 | if (val <= 0) |
| 613 | return 0; |
| 614 | if (val >= 127750) |
| 615 | return 0x7FC0; |
| 616 | return (val + 125) / 250 * 64; |
| 617 | } |
Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | * Sysfs stuff |
| 622 | */ |
| 623 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 624 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, |
| 625 | char *buf) |
| 626 | { |
| 627 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 628 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 629 | int temp; |
| 630 | |
| 631 | if (data->kind == adt7461) |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 632 | temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 633 | else if (data->kind == max6646) |
| 634 | temp = temp_from_u8(data->temp8[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 635 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 636 | temp = temp_from_s8(data->temp8[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 637 | |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 638 | /* +16 degrees offset for temp2 for the LM99 */ |
| 639 | if (data->kind == lm99 && attr->index == 3) |
| 640 | temp += 16000; |
| 641 | |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 642 | return sprintf(buf, "%d\n", temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 645 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, |
| 646 | const char *buf, size_t count) |
| 647 | { |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 648 | static const u8 reg[8] = { |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 649 | LM90_REG_W_LOCAL_LOW, |
| 650 | LM90_REG_W_LOCAL_HIGH, |
| 651 | LM90_REG_W_LOCAL_CRIT, |
| 652 | LM90_REG_W_REMOTE_CRIT, |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 653 | MAX6659_REG_W_LOCAL_EMERG, |
| 654 | MAX6659_REG_W_REMOTE_EMERG, |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 655 | LM90_REG_W_REMOTE_CRIT, |
| 656 | MAX6659_REG_W_REMOTE_EMERG, |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 657 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 659 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 660 | struct i2c_client *client = to_i2c_client(dev); |
| 661 | struct lm90_data *data = i2c_get_clientdata(client); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 662 | int nr = attr->index; |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 663 | long val; |
| 664 | int err; |
| 665 | |
| 666 | err = strict_strtol(buf, 10, &val); |
| 667 | if (err < 0) |
| 668 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 670 | /* +16 degrees offset for temp2 for the LM99 */ |
| 671 | if (data->kind == lm99 && attr->index == 3) |
| 672 | val -= 16000; |
| 673 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 674 | mutex_lock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 675 | if (data->kind == adt7461) |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 676 | data->temp8[nr] = temp_to_u8_adt7461(data, val); |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 677 | else if (data->kind == max6646) |
| 678 | data->temp8[nr] = temp_to_u8(val); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 679 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 680 | data->temp8[nr] = temp_to_s8(val); |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 681 | |
| 682 | lm90_select_remote_channel(client, data, nr >= 6); |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 683 | i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 684 | lm90_select_remote_channel(client, data, 0); |
| 685 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 686 | mutex_unlock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 687 | return count; |
| 688 | } |
| 689 | |
| 690 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, |
| 691 | char *buf) |
| 692 | { |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 693 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 694 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 695 | int temp; |
| 696 | |
| 697 | if (data->kind == adt7461) |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 698 | temp = temp_from_u16_adt7461(data, data->temp11[attr->index]); |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 699 | else if (data->kind == max6646) |
| 700 | temp = temp_from_u16(data->temp11[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 701 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 702 | temp = temp_from_s16(data->temp11[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 703 | |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 704 | /* +16 degrees offset for temp2 for the LM99 */ |
| 705 | if (data->kind == lm99 && attr->index <= 2) |
| 706 | temp += 16000; |
| 707 | |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 708 | return sprintf(buf, "%d\n", temp); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, |
| 712 | const char *buf, size_t count) |
| 713 | { |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 714 | struct { |
| 715 | u8 high; |
| 716 | u8 low; |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 717 | int channel; |
| 718 | } reg[5] = { |
| 719 | { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 }, |
| 720 | { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 }, |
| 721 | { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 }, |
| 722 | { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 }, |
| 723 | { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 } |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 724 | }; |
| 725 | |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 726 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 727 | struct i2c_client *client = to_i2c_client(dev); |
| 728 | struct lm90_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 729 | int nr = attr->nr; |
| 730 | int index = attr->index; |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 731 | long val; |
| 732 | int err; |
| 733 | |
| 734 | err = strict_strtol(buf, 10, &val); |
| 735 | if (err < 0) |
| 736 | return err; |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 737 | |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 738 | /* +16 degrees offset for temp2 for the LM99 */ |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 739 | if (data->kind == lm99 && index <= 2) |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 740 | val -= 16000; |
| 741 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 742 | mutex_lock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 743 | if (data->kind == adt7461) |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 744 | data->temp11[index] = temp_to_u16_adt7461(data, val); |
Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 745 | else if (data->kind == max6646) |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 746 | data->temp11[index] = temp_to_u8(val) << 8; |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 747 | else if (data->flags & LM90_HAVE_REM_LIMIT_EXT) |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 748 | data->temp11[index] = temp_to_s16(val); |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 749 | else |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 750 | data->temp11[index] = temp_to_s8(val) << 8; |
Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 751 | |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 752 | lm90_select_remote_channel(client, data, reg[nr].channel); |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 753 | i2c_smbus_write_byte_data(client, reg[nr].high, |
| 754 | data->temp11[index] >> 8); |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 755 | if (data->flags & LM90_HAVE_REM_LIMIT_EXT) |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 756 | i2c_smbus_write_byte_data(client, reg[nr].low, |
| 757 | data->temp11[index] & 0xff); |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 758 | lm90_select_remote_channel(client, data, 0); |
| 759 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 760 | mutex_unlock(&data->update_lock); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 761 | return count; |
| 762 | } |
| 763 | |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 764 | static ssize_t show_temphyst(struct device *dev, |
| 765 | struct device_attribute *devattr, |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 766 | char *buf) |
| 767 | { |
| 768 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 769 | struct lm90_data *data = lm90_update_device(dev); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 770 | int temp; |
| 771 | |
| 772 | if (data->kind == adt7461) |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 773 | temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); |
Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 774 | else if (data->kind == max6646) |
| 775 | temp = temp_from_u8(data->temp8[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 776 | else |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 777 | temp = temp_from_s8(data->temp8[attr->index]); |
Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 778 | |
Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 779 | /* +16 degrees offset for temp2 for the LM99 */ |
| 780 | if (data->kind == lm99 && attr->index == 3) |
| 781 | temp += 16000; |
| 782 | |
Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 783 | return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst)); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, |
| 787 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
| 789 | struct i2c_client *client = to_i2c_client(dev); |
| 790 | struct lm90_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 791 | long val; |
| 792 | int err; |
Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 793 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 795 | err = strict_strtol(buf, 10, &val); |
| 796 | if (err < 0) |
| 797 | return err; |
| 798 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 799 | mutex_lock(&data->update_lock); |
Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 800 | if (data->kind == adt7461) |
| 801 | temp = temp_from_u8_adt7461(data, data->temp8[2]); |
| 802 | else if (data->kind == max6646) |
| 803 | temp = temp_from_u8(data->temp8[2]); |
| 804 | else |
| 805 | temp = temp_from_s8(data->temp8[2]); |
| 806 | |
| 807 | data->temp_hyst = hyst_to_reg(temp - val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, |
Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 809 | data->temp_hyst); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 810 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | return count; |
| 812 | } |
| 813 | |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 814 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 815 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
| 817 | struct lm90_data *data = lm90_update_device(dev); |
| 818 | return sprintf(buf, "%d\n", data->alarms); |
| 819 | } |
| 820 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 821 | static ssize_t show_alarm(struct device *dev, struct device_attribute |
| 822 | *devattr, char *buf) |
| 823 | { |
| 824 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 825 | struct lm90_data *data = lm90_update_device(dev); |
| 826 | int bitnr = attr->index; |
| 827 | |
| 828 | return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); |
| 829 | } |
| 830 | |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 831 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4); |
| 832 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 833 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 834 | set_temp8, 0); |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 835 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
| 836 | set_temp11, 0, 1); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 837 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 838 | set_temp8, 1); |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 839 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
| 840 | set_temp11, 1, 2); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 841 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 842 | set_temp8, 2); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 843 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, |
Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 844 | set_temp8, 3); |
Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 845 | 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] | 846 | set_temphyst, 2); |
| 847 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); |
Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 848 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
| 849 | set_temp11, 2, 3); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 850 | |
| 851 | /* Individual alarm files */ |
| 852 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 853 | 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] | 854 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 855 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 856 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 857 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 858 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 859 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 861 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 862 | static struct attribute *lm90_attributes[] = { |
| 863 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 864 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 865 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 866 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 867 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 868 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 869 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 870 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 871 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 872 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 873 | |
| 874 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 875 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 876 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 877 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 878 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 879 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 880 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 881 | &dev_attr_alarms.attr, |
| 882 | NULL |
| 883 | }; |
| 884 | |
| 885 | static const struct attribute_group lm90_group = { |
| 886 | .attrs = lm90_attributes, |
| 887 | }; |
| 888 | |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 889 | /* |
| 890 | * Additional attributes for devices with emergency sensors |
| 891 | */ |
| 892 | static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8, |
| 893 | set_temp8, 4); |
| 894 | static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8, |
| 895 | set_temp8, 5); |
| 896 | static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst, |
| 897 | NULL, 4); |
| 898 | static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst, |
| 899 | NULL, 5); |
| 900 | |
| 901 | static struct attribute *lm90_emergency_attributes[] = { |
| 902 | &sensor_dev_attr_temp1_emergency.dev_attr.attr, |
| 903 | &sensor_dev_attr_temp2_emergency.dev_attr.attr, |
| 904 | &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr, |
| 905 | &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr, |
| 906 | NULL |
| 907 | }; |
| 908 | |
| 909 | static const struct attribute_group lm90_emergency_group = { |
| 910 | .attrs = lm90_emergency_attributes, |
| 911 | }; |
| 912 | |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 913 | static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15); |
| 914 | static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13); |
| 915 | |
| 916 | static struct attribute *lm90_emergency_alarm_attributes[] = { |
| 917 | &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, |
| 918 | &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, |
| 919 | NULL |
| 920 | }; |
| 921 | |
| 922 | static const struct attribute_group lm90_emergency_alarm_group = { |
| 923 | .attrs = lm90_emergency_alarm_attributes, |
| 924 | }; |
| 925 | |
| 926 | /* |
| 927 | * Additional attributes for devices with 3 temperature sensors |
| 928 | */ |
| 929 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5); |
| 930 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11, |
| 931 | set_temp11, 3, 6); |
| 932 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11, |
| 933 | set_temp11, 4, 7); |
| 934 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8, |
| 935 | set_temp8, 6); |
| 936 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6); |
| 937 | static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8, |
| 938 | set_temp8, 7); |
| 939 | static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst, |
| 940 | NULL, 7); |
| 941 | |
| 942 | static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 943 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10); |
| 944 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 945 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12); |
| 946 | static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14); |
| 947 | |
| 948 | static struct attribute *lm90_temp3_attributes[] = { |
| 949 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 950 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 951 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 952 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 953 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 954 | &sensor_dev_attr_temp3_emergency.dev_attr.attr, |
| 955 | &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr, |
| 956 | |
| 957 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 958 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 959 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 960 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 961 | &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr, |
| 962 | NULL |
| 963 | }; |
| 964 | |
| 965 | static const struct attribute_group lm90_temp3_group = { |
| 966 | .attrs = lm90_temp3_attributes, |
| 967 | }; |
| 968 | |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 969 | /* pec used for ADM1032 only */ |
| 970 | static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, |
| 971 | char *buf) |
| 972 | { |
| 973 | struct i2c_client *client = to_i2c_client(dev); |
| 974 | return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); |
| 975 | } |
| 976 | |
| 977 | static ssize_t set_pec(struct device *dev, struct device_attribute *dummy, |
| 978 | const char *buf, size_t count) |
| 979 | { |
| 980 | struct i2c_client *client = to_i2c_client(dev); |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 981 | long val; |
| 982 | int err; |
| 983 | |
| 984 | err = strict_strtol(buf, 10, &val); |
| 985 | if (err < 0) |
| 986 | return err; |
Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 987 | |
| 988 | switch (val) { |
| 989 | case 0: |
| 990 | client->flags &= ~I2C_CLIENT_PEC; |
| 991 | break; |
| 992 | case 1: |
| 993 | client->flags |= I2C_CLIENT_PEC; |
| 994 | break; |
| 995 | default: |
| 996 | return -EINVAL; |
| 997 | } |
| 998 | |
| 999 | return count; |
| 1000 | } |
| 1001 | |
| 1002 | static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec); |
| 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | /* |
| 1005 | * Real code |
| 1006 | */ |
| 1007 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1008 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1009 | static int lm90_detect(struct i2c_client *new_client, |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1010 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1012 | struct i2c_adapter *adapter = new_client->adapter; |
| 1013 | int address = new_client->addr; |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1014 | const char *name = NULL; |
| 1015 | int man_id, chip_id, reg_config1, reg_convrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1018 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1020 | /* detection and identification */ |
| 1021 | if ((man_id = i2c_smbus_read_byte_data(new_client, |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1022 | LM90_REG_R_MAN_ID)) < 0 |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1023 | || (chip_id = i2c_smbus_read_byte_data(new_client, |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1024 | LM90_REG_R_CHIP_ID)) < 0 |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1025 | || (reg_config1 = i2c_smbus_read_byte_data(new_client, |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1026 | LM90_REG_R_CONFIG1)) < 0 |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1027 | || (reg_convrate = i2c_smbus_read_byte_data(new_client, |
Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1028 | LM90_REG_R_CONVRATE)) < 0) |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1029 | return -ENODEV; |
| 1030 | |
| 1031 | if ((address == 0x4C || address == 0x4D) |
| 1032 | && man_id == 0x01) { /* National Semiconductor */ |
| 1033 | int reg_config2; |
| 1034 | |
| 1035 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 1036 | LM90_REG_R_CONFIG2); |
| 1037 | if (reg_config2 < 0) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1038 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1040 | if ((reg_config1 & 0x2A) == 0x00 |
| 1041 | && (reg_config2 & 0xF8) == 0x00 |
| 1042 | && reg_convrate <= 0x09) { |
| 1043 | if (address == 0x4C |
| 1044 | && (chip_id & 0xF0) == 0x20) { /* LM90 */ |
| 1045 | name = "lm90"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } else |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1047 | if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */ |
| 1048 | name = "lm99"; |
| 1049 | dev_info(&adapter->dev, |
| 1050 | "Assuming LM99 chip at 0x%02x\n", |
| 1051 | address); |
| 1052 | dev_info(&adapter->dev, |
| 1053 | "If it is an LM89, instantiate it " |
| 1054 | "with the new_device sysfs " |
| 1055 | "interface\n"); |
Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1056 | } else |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1057 | if (address == 0x4C |
| 1058 | && (chip_id & 0xF0) == 0x10) { /* LM86 */ |
| 1059 | name = "lm86"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1062 | } else |
| 1063 | if ((address == 0x4C || address == 0x4D) |
| 1064 | && man_id == 0x41) { /* Analog Devices */ |
| 1065 | if ((chip_id & 0xF0) == 0x40 /* ADM1032 */ |
| 1066 | && (reg_config1 & 0x3F) == 0x00 |
| 1067 | && reg_convrate <= 0x0A) { |
| 1068 | name = "adm1032"; |
| 1069 | /* The ADM1032 supports PEC, but only if combined |
| 1070 | transactions are not used. */ |
| 1071 | if (i2c_check_functionality(adapter, |
| 1072 | I2C_FUNC_SMBUS_BYTE)) |
| 1073 | info->flags |= I2C_CLIENT_PEC; |
| 1074 | } else |
| 1075 | if (chip_id == 0x51 /* ADT7461 */ |
| 1076 | && (reg_config1 & 0x1B) == 0x00 |
| 1077 | && reg_convrate <= 0x0A) { |
| 1078 | name = "adt7461"; |
| 1079 | } |
| 1080 | } else |
| 1081 | if (man_id == 0x4D) { /* Maxim */ |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1082 | int reg_emerg, reg_emerg2, reg_status2; |
| 1083 | |
| 1084 | /* |
| 1085 | * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read |
| 1086 | * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG |
| 1087 | * exists, both readings will reflect the same value. Otherwise, |
| 1088 | * the readings will be different. |
| 1089 | */ |
| 1090 | if ((reg_emerg = i2c_smbus_read_byte_data(new_client, |
| 1091 | MAX6659_REG_R_REMOTE_EMERG)) < 0 |
| 1092 | || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0 |
| 1093 | || (reg_emerg2 = i2c_smbus_read_byte_data(new_client, |
| 1094 | MAX6659_REG_R_REMOTE_EMERG)) < 0 |
| 1095 | || (reg_status2 = i2c_smbus_read_byte_data(new_client, |
| 1096 | MAX6696_REG_R_STATUS2)) < 0) |
| 1097 | return -ENODEV; |
| 1098 | |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1099 | /* |
| 1100 | * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id |
| 1101 | * register. Reading from that address will return the last |
| 1102 | * read value, which in our case is those of the man_id |
| 1103 | * register. Likewise, the config1 register seems to lack a |
| 1104 | * low nibble, so the value will be those of the previous |
| 1105 | * read, so in our case those of the man_id register. |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1106 | * MAX6659 has a third set of upper temperature limit registers. |
| 1107 | * Those registers also return values on MAX6657 and MAX6658, |
| 1108 | * thus the only way to detect MAX6659 is by its address. |
| 1109 | * For this reason it will be mis-detected as MAX6657 if its |
| 1110 | * address is 0x4C. |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1111 | */ |
| 1112 | if (chip_id == man_id |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1113 | && (address == 0x4C || address == 0x4D || address == 0x4E) |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1114 | && (reg_config1 & 0x1F) == (man_id & 0x0F) |
| 1115 | && reg_convrate <= 0x09) { |
Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1116 | if (address == 0x4C) |
| 1117 | name = "max6657"; |
| 1118 | else |
| 1119 | name = "max6659"; |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1120 | } else |
| 1121 | /* |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1122 | * Even though MAX6695 and MAX6696 do not have a chip ID |
| 1123 | * register, reading it returns 0x01. Bit 4 of the config1 |
| 1124 | * register is unused and should return zero when read. Bit 0 of |
| 1125 | * the status2 register is unused and should return zero when |
| 1126 | * read. |
| 1127 | * |
| 1128 | * MAX6695 and MAX6696 have an additional set of temperature |
| 1129 | * limit registers. We can detect those chips by checking if |
| 1130 | * one of those registers exists. |
| 1131 | */ |
| 1132 | if (chip_id == 0x01 |
| 1133 | && (reg_config1 & 0x10) == 0x00 |
| 1134 | && (reg_status2 & 0x01) == 0x00 |
| 1135 | && reg_emerg == reg_emerg2 |
| 1136 | && reg_convrate <= 0x07) { |
| 1137 | name = "max6696"; |
| 1138 | } else |
| 1139 | /* |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1140 | * The chip_id register of the MAX6680 and MAX6681 holds the |
| 1141 | * revision of the chip. The lowest bit of the config1 register |
| 1142 | * is unused and should return zero when read, so should the |
| 1143 | * second to last bit of config1 (software reset). |
| 1144 | */ |
| 1145 | if (chip_id == 0x01 |
| 1146 | && (reg_config1 & 0x03) == 0x00 |
| 1147 | && reg_convrate <= 0x07) { |
| 1148 | name = "max6680"; |
| 1149 | } else |
| 1150 | /* |
| 1151 | * The chip_id register of the MAX6646/6647/6649 holds the |
| 1152 | * revision of the chip. The lowest 6 bits of the config1 |
| 1153 | * register are unused and should return zero when read. |
| 1154 | */ |
| 1155 | if (chip_id == 0x59 |
| 1156 | && (reg_config1 & 0x3f) == 0x00 |
| 1157 | && reg_convrate <= 0x07) { |
| 1158 | name = "max6646"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 1160 | } else |
| 1161 | if (address == 0x4C |
| 1162 | && man_id == 0x5C) { /* Winbond/Nuvoton */ |
| 1163 | if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */ |
| 1164 | && (reg_config1 & 0x2A) == 0x00 |
| 1165 | && reg_convrate <= 0x08) { |
| 1166 | name = "w83l771"; |
| 1167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1170 | if (!name) { /* identification failed */ |
| 1171 | dev_dbg(&adapter->dev, |
| 1172 | "Unsupported chip at 0x%02x (man_id=0x%02X, " |
| 1173 | "chip_id=0x%02X)\n", address, man_id, chip_id); |
| 1174 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | } |
Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1176 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1177 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
| 1181 | |
Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1182 | static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data) |
| 1183 | { |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1184 | if (data->flags & LM90_HAVE_TEMP3) |
| 1185 | sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group); |
| 1186 | if (data->flags & LM90_HAVE_EMERGENCY_ALARM) |
| 1187 | sysfs_remove_group(&client->dev.kobj, |
| 1188 | &lm90_emergency_alarm_group); |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1189 | if (data->flags & LM90_HAVE_EMERGENCY) |
| 1190 | sysfs_remove_group(&client->dev.kobj, |
| 1191 | &lm90_emergency_group); |
Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1192 | if (data->flags & LM90_HAVE_OFFSET) |
| 1193 | device_remove_file(&client->dev, |
| 1194 | &sensor_dev_attr_temp2_offset.dev_attr); |
| 1195 | device_remove_file(&client->dev, &dev_attr_pec); |
| 1196 | sysfs_remove_group(&client->dev.kobj, &lm90_group); |
| 1197 | } |
| 1198 | |
Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1199 | static void lm90_init_client(struct i2c_client *client) |
| 1200 | { |
| 1201 | u8 config; |
| 1202 | struct lm90_data *data = i2c_get_clientdata(client); |
| 1203 | |
| 1204 | /* |
| 1205 | * Start the conversions. |
| 1206 | */ |
| 1207 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, |
| 1208 | 5); /* 2 Hz */ |
| 1209 | if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) { |
| 1210 | dev_warn(&client->dev, "Initialization failed!\n"); |
| 1211 | return; |
| 1212 | } |
| 1213 | data->config_orig = config; |
| 1214 | |
| 1215 | /* Check Temperature Range Select */ |
| 1216 | if (data->kind == adt7461) { |
| 1217 | if (config & 0x04) |
| 1218 | data->flags |= LM90_FLAG_ADT7461_EXT; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Put MAX6680/MAX8881 into extended resolution (bit 0x10, |
| 1223 | * 0.125 degree resolution) and range (0x08, extend range |
| 1224 | * to -64 degree) mode for the remote temperature sensor. |
| 1225 | */ |
| 1226 | if (data->kind == max6680) |
| 1227 | config |= 0x18; |
| 1228 | |
| 1229 | /* |
| 1230 | * Select external channel 0 for max6695/96 |
| 1231 | */ |
| 1232 | if (data->kind == max6696) |
| 1233 | config &= ~0x08; |
| 1234 | |
| 1235 | config &= 0xBF; /* run */ |
| 1236 | if (config != data->config_orig) /* Only write if changed */ |
| 1237 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); |
| 1238 | } |
| 1239 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1240 | static int lm90_probe(struct i2c_client *new_client, |
| 1241 | const struct i2c_device_id *id) |
| 1242 | { |
| 1243 | struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent); |
| 1244 | struct lm90_data *data; |
| 1245 | int err; |
| 1246 | |
| 1247 | data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL); |
| 1248 | if (!data) { |
| 1249 | err = -ENOMEM; |
| 1250 | goto exit; |
| 1251 | } |
| 1252 | i2c_set_clientdata(new_client, data); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1253 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1255 | /* Set the device type */ |
| 1256 | data->kind = id->driver_data; |
| 1257 | if (data->kind == adm1032) { |
| 1258 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 1259 | new_client->flags &= ~I2C_CLIENT_PEC; |
| 1260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1262 | /* Different devices have different alarm bits triggering the |
| 1263 | * ALERT# output */ |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1264 | data->alert_alarms = lm90_params[data->kind].alert_alarms; |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1265 | |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1266 | /* Set chip capabilities */ |
Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1267 | data->flags = lm90_params[data->kind].flags; |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | /* Initialize the LM90 chip */ |
| 1270 | lm90_init_client(new_client); |
| 1271 | |
| 1272 | /* Register sysfs hooks */ |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1273 | err = sysfs_create_group(&new_client->dev.kobj, &lm90_group); |
| 1274 | if (err) |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1275 | goto exit_free; |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1276 | if (new_client->flags & I2C_CLIENT_PEC) { |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1277 | err = device_create_file(&new_client->dev, &dev_attr_pec); |
| 1278 | if (err) |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1279 | goto exit_remove_files; |
| 1280 | } |
Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1281 | if (data->flags & LM90_HAVE_OFFSET) { |
Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1282 | err = device_create_file(&new_client->dev, |
| 1283 | &sensor_dev_attr_temp2_offset.dev_attr); |
| 1284 | if (err) |
Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 1285 | goto exit_remove_files; |
| 1286 | } |
Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1287 | if (data->flags & LM90_HAVE_EMERGENCY) { |
| 1288 | err = sysfs_create_group(&new_client->dev.kobj, |
| 1289 | &lm90_emergency_group); |
| 1290 | if (err) |
| 1291 | goto exit_remove_files; |
| 1292 | } |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1293 | if (data->flags & LM90_HAVE_EMERGENCY_ALARM) { |
| 1294 | err = sysfs_create_group(&new_client->dev.kobj, |
| 1295 | &lm90_emergency_alarm_group); |
| 1296 | if (err) |
| 1297 | goto exit_remove_files; |
| 1298 | } |
| 1299 | if (data->flags & LM90_HAVE_TEMP3) { |
| 1300 | err = sysfs_create_group(&new_client->dev.kobj, |
| 1301 | &lm90_temp3_group); |
| 1302 | if (err) |
| 1303 | goto exit_remove_files; |
| 1304 | } |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1305 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1306 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 1307 | if (IS_ERR(data->hwmon_dev)) { |
| 1308 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1309 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1310 | } |
| 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return 0; |
| 1313 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1314 | exit_remove_files: |
Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1315 | lm90_remove_files(new_client, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | exit_free: |
| 1317 | kfree(data); |
| 1318 | exit: |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1322 | static int lm90_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1324 | struct lm90_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1326 | hwmon_device_unregister(data->hwmon_dev); |
Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1327 | lm90_remove_files(client, data); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1328 | |
Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 1329 | /* Restore initial configuration */ |
| 1330 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, |
| 1331 | data->config_orig); |
| 1332 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1333 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1337 | static void lm90_alert(struct i2c_client *client, unsigned int flag) |
| 1338 | { |
| 1339 | struct lm90_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1340 | u8 config, alarms, alarms2 = 0; |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1341 | |
| 1342 | lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1343 | |
| 1344 | if (data->kind == max6696) |
| 1345 | lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2); |
| 1346 | |
| 1347 | if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) { |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1348 | dev_info(&client->dev, "Everything OK\n"); |
| 1349 | } else { |
| 1350 | if (alarms & 0x61) |
| 1351 | dev_warn(&client->dev, |
| 1352 | "temp%d out of range, please check!\n", 1); |
| 1353 | if (alarms & 0x1a) |
| 1354 | dev_warn(&client->dev, |
| 1355 | "temp%d out of range, please check!\n", 2); |
| 1356 | if (alarms & 0x04) |
| 1357 | dev_warn(&client->dev, |
| 1358 | "temp%d diode open, please check!\n", 2); |
| 1359 | |
Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1360 | if (alarms2 & 0x18) |
| 1361 | dev_warn(&client->dev, |
| 1362 | "temp%d out of range, please check!\n", 3); |
| 1363 | |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1364 | /* Disable ALERT# output, because these chips don't implement |
| 1365 | SMBus alert correctly; they should only hold the alert line |
| 1366 | low briefly. */ |
Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame^] | 1367 | if ((data->flags & LM90_HAVE_BROKEN_ALERT) |
Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1368 | && (alarms & data->alert_alarms)) { |
| 1369 | dev_dbg(&client->dev, "Disabling ALERT#\n"); |
| 1370 | lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); |
| 1371 | i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, |
| 1372 | config | 0x80); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1377 | static struct i2c_driver lm90_driver = { |
| 1378 | .class = I2C_CLASS_HWMON, |
| 1379 | .driver = { |
| 1380 | .name = "lm90", |
| 1381 | }, |
| 1382 | .probe = lm90_probe, |
| 1383 | .remove = lm90_remove, |
| 1384 | .alert = lm90_alert, |
| 1385 | .id_table = lm90_id, |
| 1386 | .detect = lm90_detect, |
| 1387 | .address_list = normal_i2c, |
| 1388 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | static int __init sensors_lm90_init(void) |
| 1391 | { |
| 1392 | return i2c_add_driver(&lm90_driver); |
| 1393 | } |
| 1394 | |
| 1395 | static void __exit sensors_lm90_exit(void) |
| 1396 | { |
| 1397 | i2c_del_driver(&lm90_driver); |
| 1398 | } |
| 1399 | |
| 1400 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 1401 | MODULE_DESCRIPTION("LM90/ADM1032 driver"); |
| 1402 | MODULE_LICENSE("GPL"); |
| 1403 | |
| 1404 | module_init(sensors_lm90_init); |
| 1405 | module_exit(sensors_lm90_exit); |