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