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