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