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