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