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