blob: 1a1d7bfcb30b06a6548ef6bcdc671387ff891f8a [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
4 * monitoring
Jean Delvare7c81c60f2014-01-29 20:40:08 +01005 * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Delvarea874a102008-10-17 17:51:10 +020010 * temperature) and a 3-4 deg accuracy.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 Delvare97ae60b2008-10-26 17:04:39 +010016 * degrees, which allows for higher temperatures measurement.
Steven Cole44bbe872005-05-03 18:21:25 -060017 * Note that there is no way to differentiate between both chips.
Jean Delvare97ae60b2008-10-26 17:04:39 +010018 * When device is auto-detected, the driver will assume an LM99.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
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 Torvalds1da177e2005-04-16 15:20:36 -070023 *
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 Delvarea874a102008-10-17 17:51:10 +020026 * that are not handled by this driver. Among others, it has a higher
27 * accuracy than the LM90, much like the LM86 does.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
29 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
Jean Delvarea874a102008-10-17 17:51:10 +020030 * chips made by Maxim. These chips are similar to the LM86.
Steven Cole44bbe872005-05-03 18:21:25 -060031 * Note that there is no easy way to differentiate between the three
Guenter Roeck69487082010-10-28 20:31:43 +020032 * 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 Torvalds1da177e2005-04-16 15:20:36 -070037 *
Josh Lehan229d4952020-05-13 11:42:48 -070038 * This driver also supports the MAX6654 chip made by Maxim. This chip can
39 * be at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is
40 * otherwise similar to MAX6657/MAX6658/MAX6659. Extended range is available
41 * by setting the configuration register accordingly, and is done during
42 * initialization. Extended precision is only available at conversion rates
43 * of 1 Hz and slower. Note that extended precision is not enabled by
44 * default, as this driver initializes all chips to 2 Hz by design.
45 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010046 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
47 * MAX6692 chips made by Maxim. These are again similar to the LM86,
48 * but they use unsigned temperature values and can report temperatures
49 * from 0 to 145 degrees.
Ben Hutchings271dabf2008-10-17 17:51:11 +020050 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040051 * This driver also supports the MAX6680 and MAX6681, two other sensor
52 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020053 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
54 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040055 *
Guenter Roeck06e1c0a2010-10-28 20:31:43 +020056 * This driver also supports the MAX6695 and MAX6696, two other sensor
57 * chips made by Maxim. These are also quite similar to other Maxim
58 * chips, but support three temperature sensors instead of two. MAX6695
59 * and MAX6696 only differ in the pinout so they can be treated identically.
60 *
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020061 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
62 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
63 * and extended mode. They are mostly compatible with LM90 except for a data
64 * format difference for the temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
Stijn Devriendt2ef01792011-06-06 10:40:45 +000066 * This driver also supports the SA56004 from Philips. This device is
67 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
68 *
Guenter Roeckae544f62012-03-23 10:02:18 +010069 * This driver also supports the G781 from GMT. This device is compatible
70 * with the ADM1032.
71 *
Wei Ni1daaceb2013-11-15 10:40:39 +010072 * This driver also supports TMP451 from Texas Instruments. This device is
73 * supported in both compatibility and extended mode. It's mostly compatible
74 * with ADT7461 except for local temperature low byte register and max
75 * conversion rate.
76 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * Since the LM90 was the first chipset supported by this driver, most
78 * comments will refer to this chipset, but are actually general and
79 * concern all supported chipsets, unless mentioned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/slab.h>
85#include <linux/jiffies.h>
86#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040087#include <linux/hwmon.h>
88#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010089#include <linux/mutex.h>
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -030090#include <linux/of_device.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020091#include <linux/sysfs.h>
Wei Ni109b1282013-11-15 10:40:39 +010092#include <linux/interrupt.h>
Wei Ni3e0f9642013-11-15 10:40:39 +010093#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * Addresses to scan
97 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040098 * MAX6659, MAX6680 and MAX6681.
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020099 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
100 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
101 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
102 * have address 0x4d.
Ben Hutchings271dabf2008-10-17 17:51:11 +0200103 * MAX6647 has address 0x4e.
Guenter Roeck13c84952010-10-28 20:31:43 +0200104 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Josh Lehan229d4952020-05-13 11:42:48 -0700105 * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
106 * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000107 * SA56004 can have address 0x48 through 0x4F.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
109
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500110static const unsigned short normal_i2c[] = {
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000111 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
112 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Guenter Roeck13c84952010-10-28 20:31:43 +0200114enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
Josh Lehan229d4952020-05-13 11:42:48 -0700115 max6646, w83l771, max6696, sa56004, g781, tmp451, max6654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * The LM90 registers
119 */
120
121#define LM90_REG_R_MAN_ID 0xFE
122#define LM90_REG_R_CHIP_ID 0xFF
123#define LM90_REG_R_CONFIG1 0x03
124#define LM90_REG_W_CONFIG1 0x09
125#define LM90_REG_R_CONFIG2 0xBF
126#define LM90_REG_W_CONFIG2 0xBF
127#define LM90_REG_R_CONVRATE 0x04
128#define LM90_REG_W_CONVRATE 0x0A
129#define LM90_REG_R_STATUS 0x02
130#define LM90_REG_R_LOCAL_TEMP 0x00
131#define LM90_REG_R_LOCAL_HIGH 0x05
132#define LM90_REG_W_LOCAL_HIGH 0x0B
133#define LM90_REG_R_LOCAL_LOW 0x06
134#define LM90_REG_W_LOCAL_LOW 0x0C
135#define LM90_REG_R_LOCAL_CRIT 0x20
136#define LM90_REG_W_LOCAL_CRIT 0x20
137#define LM90_REG_R_REMOTE_TEMPH 0x01
138#define LM90_REG_R_REMOTE_TEMPL 0x10
139#define LM90_REG_R_REMOTE_OFFSH 0x11
140#define LM90_REG_W_REMOTE_OFFSH 0x11
141#define LM90_REG_R_REMOTE_OFFSL 0x12
142#define LM90_REG_W_REMOTE_OFFSL 0x12
143#define LM90_REG_R_REMOTE_HIGHH 0x07
144#define LM90_REG_W_REMOTE_HIGHH 0x0D
145#define LM90_REG_R_REMOTE_HIGHL 0x13
146#define LM90_REG_W_REMOTE_HIGHL 0x13
147#define LM90_REG_R_REMOTE_LOWH 0x08
148#define LM90_REG_W_REMOTE_LOWH 0x0E
149#define LM90_REG_R_REMOTE_LOWL 0x14
150#define LM90_REG_W_REMOTE_LOWL 0x14
151#define LM90_REG_R_REMOTE_CRIT 0x19
152#define LM90_REG_W_REMOTE_CRIT 0x19
153#define LM90_REG_R_TCRIT_HYST 0x21
154#define LM90_REG_W_TCRIT_HYST 0x21
155
Josh Lehan229d4952020-05-13 11:42:48 -0700156/* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200157
158#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200159#define MAX6696_REG_R_STATUS2 0x12
Guenter Roeck69487082010-10-28 20:31:43 +0200160#define MAX6659_REG_R_REMOTE_EMERG 0x16
161#define MAX6659_REG_W_REMOTE_EMERG 0x16
162#define MAX6659_REG_R_LOCAL_EMERG 0x17
163#define MAX6659_REG_W_LOCAL_EMERG 0x17
Jean Delvaref65e1702008-10-17 17:51:09 +0200164
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000165/* SA56004 registers */
166
167#define SA56004_REG_R_LOCAL_TEMPL 0x22
168
Guenter Roeck0c01b642010-10-28 20:31:44 +0200169#define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
170
Wei Ni1daaceb2013-11-15 10:40:39 +0100171/* TMP451 registers */
172#define TMP451_REG_R_LOCAL_TEMPL 0x15
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Nate Case23b2d472008-10-17 17:51:10 +0200175 * Device flags
176 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200177#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
178/* Device features */
179#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200180#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200181#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200182#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
183#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Guenter Roeck11793242010-10-28 20:31:44 +0200184#define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
Guenter Roeckf347e242021-10-21 01:49:50 -0700185#define LM90_HAVE_EXTENDED_TEMP (1 << 8) /* extended temperature support*/
186#define LM90_PAUSE_FOR_CONFIG (1 << 9) /* Pause conversion for config */
Nate Case23b2d472008-10-17 17:51:10 +0200187
Wei Ni072de492013-11-15 10:40:38 +0100188/* LM90 status */
189#define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
190#define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
191#define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
192#define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
193#define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
194#define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
195#define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
196
197#define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
198#define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
199#define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
200#define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
201#define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
202#define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
203#define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
204
Nate Case23b2d472008-10-17 17:51:10 +0200205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * Driver data (common to all clients)
207 */
208
Jean Delvare9b0e8522008-07-16 19:30:15 +0200209static const struct i2c_device_id lm90_id[] = {
210 { "adm1032", adm1032 },
211 { "adt7461", adt7461 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200212 { "adt7461a", adt7461 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100213 { "g781", g781 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200214 { "lm90", lm90 },
215 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100216 { "lm89", lm86 },
217 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200218 { "max6646", max6646 },
219 { "max6647", max6646 },
220 { "max6649", max6646 },
Josh Lehan229d4952020-05-13 11:42:48 -0700221 { "max6654", max6654 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200222 { "max6657", max6657 },
223 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200224 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200225 { "max6680", max6680 },
226 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200227 { "max6695", max6696 },
228 { "max6696", max6696 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200229 { "nct1008", adt7461 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100230 { "w83l771", w83l771 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000231 { "sa56004", sa56004 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100232 { "tmp451", tmp451 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200233 { }
234};
235MODULE_DEVICE_TABLE(i2c, lm90_id);
236
Guenter Roeck787afaa2019-04-04 06:21:17 -0700237static const struct of_device_id __maybe_unused lm90_of_match[] = {
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -0300238 {
239 .compatible = "adi,adm1032",
240 .data = (void *)adm1032
241 },
242 {
243 .compatible = "adi,adt7461",
244 .data = (void *)adt7461
245 },
246 {
247 .compatible = "adi,adt7461a",
248 .data = (void *)adt7461
249 },
250 {
251 .compatible = "gmt,g781",
252 .data = (void *)g781
253 },
254 {
255 .compatible = "national,lm90",
256 .data = (void *)lm90
257 },
258 {
259 .compatible = "national,lm86",
260 .data = (void *)lm86
261 },
262 {
263 .compatible = "national,lm89",
264 .data = (void *)lm86
265 },
266 {
267 .compatible = "national,lm99",
268 .data = (void *)lm99
269 },
270 {
271 .compatible = "dallas,max6646",
272 .data = (void *)max6646
273 },
274 {
275 .compatible = "dallas,max6647",
276 .data = (void *)max6646
277 },
278 {
279 .compatible = "dallas,max6649",
280 .data = (void *)max6646
281 },
282 {
Josh Lehan229d4952020-05-13 11:42:48 -0700283 .compatible = "dallas,max6654",
284 .data = (void *)max6654
285 },
286 {
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -0300287 .compatible = "dallas,max6657",
288 .data = (void *)max6657
289 },
290 {
291 .compatible = "dallas,max6658",
292 .data = (void *)max6657
293 },
294 {
295 .compatible = "dallas,max6659",
296 .data = (void *)max6659
297 },
298 {
299 .compatible = "dallas,max6680",
300 .data = (void *)max6680
301 },
302 {
303 .compatible = "dallas,max6681",
304 .data = (void *)max6680
305 },
306 {
307 .compatible = "dallas,max6695",
308 .data = (void *)max6696
309 },
310 {
311 .compatible = "dallas,max6696",
312 .data = (void *)max6696
313 },
314 {
315 .compatible = "onnn,nct1008",
316 .data = (void *)adt7461
317 },
318 {
319 .compatible = "winbond,w83l771",
320 .data = (void *)w83l771
321 },
322 {
323 .compatible = "nxp,sa56004",
324 .data = (void *)sa56004
325 },
326 {
327 .compatible = "ti,tmp451",
328 .data = (void *)tmp451
329 },
330 { },
331};
332MODULE_DEVICE_TABLE(of, lm90_of_match);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200335 * chip type specific parameters
336 */
337struct lm90_params {
338 u32 flags; /* Capabilities */
339 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
340 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200341 u8 max_convrate; /* Maximum conversion rate register value */
Jean Delvarea095f682011-07-27 23:22:25 -0700342 u8 reg_local_ext; /* Extended local temp register (optional) */
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200343};
344
345static const struct lm90_params lm90_params[] = {
346 [adm1032] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200347 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
348 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200349 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200350 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200351 },
352 [adt7461] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200353 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
Guenter Roeckf347e242021-10-21 01:49:50 -0700354 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200355 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200356 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200357 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100358 [g781] = {
359 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
360 | LM90_HAVE_BROKEN_ALERT,
361 .alert_alarms = 0x7c,
362 .max_convrate = 8,
363 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200364 [lm86] = {
365 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
366 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200367 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200368 },
369 [lm90] = {
370 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
371 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200372 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200373 },
374 [lm99] = {
375 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
376 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200377 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200378 },
379 [max6646] = {
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200380 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200381 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000382 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200383 },
Josh Lehan229d4952020-05-13 11:42:48 -0700384 [max6654] = {
385 .alert_alarms = 0x7c,
386 .max_convrate = 7,
387 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
388 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200389 [max6657] = {
Boyang Yu62456182019-06-28 19:06:36 +0000390 .flags = LM90_PAUSE_FOR_CONFIG,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200391 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200392 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000393 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200394 },
395 [max6659] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700396 .flags = LM90_HAVE_EMERGENCY,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200397 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200398 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000399 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200400 },
401 [max6680] = {
402 .flags = LM90_HAVE_OFFSET,
403 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200404 .max_convrate = 7,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200405 },
406 [max6696] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700407 .flags = LM90_HAVE_EMERGENCY
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200408 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
Guenter Roecke41fae2b2013-11-15 10:40:38 +0100409 .alert_alarms = 0x1c7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200410 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000411 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200412 },
413 [w83l771] = {
414 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
415 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200416 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200417 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000418 [sa56004] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700419 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000420 .alert_alarms = 0x7b,
421 .max_convrate = 9,
422 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
423 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100424 [tmp451] = {
425 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
Guenter Roeckf347e242021-10-21 01:49:50 -0700426 | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP,
Wei Ni1daaceb2013-11-15 10:40:39 +0100427 .alert_alarms = 0x7c,
428 .max_convrate = 9,
429 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700430 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200431};
432
433/*
Wei Ni40465d92013-11-15 10:40:39 +0100434 * TEMP8 register index
435 */
436enum lm90_temp8_reg_index {
437 LOCAL_LOW = 0,
438 LOCAL_HIGH,
439 LOCAL_CRIT,
440 REMOTE_CRIT,
441 LOCAL_EMERG, /* max6659 and max6695/96 */
442 REMOTE_EMERG, /* max6659 and max6695/96 */
443 REMOTE2_CRIT, /* max6695/96 only */
444 REMOTE2_EMERG, /* max6695/96 only */
445 TEMP8_REG_NUM
446};
447
448/*
449 * TEMP11 register index
450 */
451enum lm90_temp11_reg_index {
452 REMOTE_TEMP = 0,
453 REMOTE_LOW,
454 REMOTE_HIGH,
455 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
456 LOCAL_TEMP,
457 REMOTE2_TEMP, /* max6695/96 only */
458 REMOTE2_LOW, /* max6695/96 only */
459 REMOTE2_HIGH, /* max6695/96 only */
460 TEMP11_REG_NUM
461};
462
463/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Client data (each client gets its own)
465 */
466
467struct lm90_data {
Guenter Roeck1de8b252014-04-04 18:01:35 +0200468 struct i2c_client *client;
Dmitry Osipenko94dbd232021-06-19 00:54:53 +0300469 struct device *hwmon_dev;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700470 u32 channel_config[4];
471 struct hwmon_channel_info temp_info;
472 const struct hwmon_channel_info *info[3];
473 struct hwmon_chip_info chip;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100474 struct mutex update_lock;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700475 bool valid; /* true if register values are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned long last_updated; /* in jiffies */
477 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200478 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Wolfram Sang38bab982016-06-05 09:35:43 +0200480 unsigned int update_interval; /* in milliseconds */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200481
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700482 u8 config; /* Current configuration register value */
Jean Delvare95238362010-03-05 22:17:14 +0100483 u8 config_orig; /* Original configuration register value */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200484 u8 convrate_orig; /* Original conversion rate register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200485 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
486 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200487 u8 max_convrate; /* Maximum conversion rate */
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000488 u8 reg_local_ext; /* local extension register offset */
Jean Delvare95238362010-03-05 22:17:14 +0100489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* registers values */
Wei Ni40465d92013-11-15 10:40:39 +0100491 s8 temp8[TEMP8_REG_NUM];
492 s16 temp11[TEMP11_REG_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200494 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495};
496
497/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200498 * Support functions
499 */
500
501/*
502 * The ADM1032 supports PEC but not on write byte transactions, so we need
503 * to explicitly ask for a transaction without PEC.
504 */
505static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
506{
507 return i2c_smbus_xfer(client->adapter, client->addr,
508 client->flags & ~I2C_CLIENT_PEC,
509 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
510}
511
512/*
513 * It is assumed that client->update_lock is held (unless we are in
514 * detection or initialization steps). This matters when PEC is enabled,
515 * because we don't want the address pointer to change between the write
516 * byte and the read byte transactions.
517 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700518static int lm90_read_reg(struct i2c_client *client, u8 reg)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200519{
520 int err;
521
522 if (client->flags & I2C_CLIENT_PEC) {
523 err = adm1032_write_byte(client, reg);
524 if (err >= 0)
525 err = i2c_smbus_read_byte(client);
526 } else
527 err = i2c_smbus_read_byte_data(client, reg);
528
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700529 return err;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200530}
531
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700532static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200533{
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700534 int oldh, newh, l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200535
536 /*
537 * There is a trick here. We have to read two registers to have the
538 * sensor temperature, but we have to beware a conversion could occur
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300539 * between the readings. The datasheet says we should either use
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200540 * the one-shot conversion register, which we don't want to do
541 * (disables hardware monitoring) or monitor the busy bit, which is
542 * impossible (we can't read the values and monitor that bit at the
543 * exact same time). So the solution used here is to read the high
544 * byte once, then the low byte, then the high byte again. If the new
545 * high byte matches the old one, then we have a valid reading. Else
546 * we have to read the low byte again, and now we believe we have a
547 * correct reading.
548 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700549 oldh = lm90_read_reg(client, regh);
550 if (oldh < 0)
551 return oldh;
552 l = lm90_read_reg(client, regl);
553 if (l < 0)
554 return l;
555 newh = lm90_read_reg(client, regh);
556 if (newh < 0)
557 return newh;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200558 if (oldh != newh) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700559 l = lm90_read_reg(client, regl);
560 if (l < 0)
561 return l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200562 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700563 return (newh << 8) | l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200564}
565
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700566static int lm90_update_confreg(struct lm90_data *data, u8 config)
567{
568 if (data->config != config) {
569 int err;
570
571 err = i2c_smbus_write_byte_data(data->client,
572 LM90_REG_W_CONFIG1,
573 config);
574 if (err)
575 return err;
576 data->config = config;
577 }
578 return 0;
579}
580
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200581/*
582 * client->update_lock must be held when calling this function (unless we are
583 * in detection or initialization steps), and while a remote channel other
584 * than channel 0 is selected. Also, calling code must make sure to re-select
585 * external channel 0 before releasing the lock. This is necessary because
586 * various registers have different meanings as a result of selecting a
587 * non-default remote channel.
588 */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700589static int lm90_select_remote_channel(struct lm90_data *data, int channel)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200590{
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700591 int err = 0;
592
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200593 if (data->kind == max6696) {
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700594 u8 config = data->config & ~0x08;
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700595
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200596 if (channel)
597 config |= 0x08;
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700598 err = lm90_update_confreg(data, config);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200599 }
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700600 return err;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200601}
602
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700603static int lm90_write_convrate(struct lm90_data *data, int val)
Boyang Yu62456182019-06-28 19:06:36 +0000604{
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700605 u8 config = data->config;
Boyang Yu62456182019-06-28 19:06:36 +0000606 int err;
Boyang Yu62456182019-06-28 19:06:36 +0000607
608 /* Save config and pause conversion */
609 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700610 err = lm90_update_confreg(data, config | 0x40);
611 if (err < 0)
612 return err;
Boyang Yu62456182019-06-28 19:06:36 +0000613 }
614
615 /* Set conv rate */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700616 err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
Boyang Yu62456182019-06-28 19:06:36 +0000617
618 /* Revert change to config */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700619 lm90_update_confreg(data, config);
Boyang Yu62456182019-06-28 19:06:36 +0000620
621 return err;
622}
623
Guenter Roeck0c01b642010-10-28 20:31:44 +0200624/*
625 * Set conversion rate.
626 * client->update_lock must be held when calling this function (unless we are
627 * in detection or initialization steps).
628 */
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700629static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
630 unsigned int interval)
Guenter Roeck0c01b642010-10-28 20:31:44 +0200631{
Guenter Roeck0c01b642010-10-28 20:31:44 +0200632 unsigned int update_interval;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700633 int i, err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200634
635 /* Shift calculations to avoid rounding errors */
636 interval <<= 6;
637
638 /* find the nearest update rate */
639 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
640 i < data->max_convrate; i++, update_interval >>= 1)
641 if (interval >= update_interval * 3 / 4)
642 break;
643
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700644 err = lm90_write_convrate(data, i);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200645 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700646 return err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200647}
648
Guenter Roeck10bfef42016-06-13 19:26:45 -0700649static int lm90_update_limits(struct device *dev)
650{
651 struct lm90_data *data = dev_get_drvdata(dev);
652 struct i2c_client *client = data->client;
653 int val;
654
655 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
656 if (val < 0)
657 return val;
658 data->temp8[LOCAL_CRIT] = val;
659
660 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
661 if (val < 0)
662 return val;
663 data->temp8[REMOTE_CRIT] = val;
664
665 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
666 if (val < 0)
667 return val;
668 data->temp_hyst = val;
669
Guenter Roeckbe9d6372016-07-25 14:47:18 -0700670 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700671 if (val < 0)
672 return val;
673 data->temp11[REMOTE_LOW] = val << 8;
674
675 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
676 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
677 if (val < 0)
678 return val;
679 data->temp11[REMOTE_LOW] |= val;
680 }
681
682 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
683 if (val < 0)
684 return val;
685 data->temp11[REMOTE_HIGH] = val << 8;
686
687 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
688 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
689 if (val < 0)
690 return val;
691 data->temp11[REMOTE_HIGH] |= val;
692 }
693
694 if (data->flags & LM90_HAVE_OFFSET) {
695 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
696 LM90_REG_R_REMOTE_OFFSL);
697 if (val < 0)
698 return val;
699 data->temp11[REMOTE_OFFSET] = val;
700 }
701
702 if (data->flags & LM90_HAVE_EMERGENCY) {
703 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
704 if (val < 0)
705 return val;
706 data->temp8[LOCAL_EMERG] = val;
707
708 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
709 if (val < 0)
710 return val;
711 data->temp8[REMOTE_EMERG] = val;
712 }
713
714 if (data->kind == max6696) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700715 val = lm90_select_remote_channel(data, 1);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700716 if (val < 0)
717 return val;
718
719 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
720 if (val < 0)
721 return val;
722 data->temp8[REMOTE2_CRIT] = val;
723
724 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
725 if (val < 0)
726 return val;
727 data->temp8[REMOTE2_EMERG] = val;
728
729 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
730 if (val < 0)
731 return val;
732 data->temp11[REMOTE2_LOW] = val << 8;
733
734 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
735 if (val < 0)
736 return val;
737 data->temp11[REMOTE2_HIGH] = val << 8;
738
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700739 lm90_select_remote_channel(data, 0);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700740 }
741
742 return 0;
743}
744
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700745static int lm90_update_device(struct device *dev)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200746{
Guenter Roeck1de8b252014-04-04 18:01:35 +0200747 struct lm90_data *data = dev_get_drvdata(dev);
748 struct i2c_client *client = data->client;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200749 unsigned long next_update;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700750 int val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200751
Guenter Roeck10bfef42016-06-13 19:26:45 -0700752 if (!data->valid) {
753 val = lm90_update_limits(dev);
754 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700755 return val;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700756 }
757
Jean Delvare78c2c2f2013-07-08 14:18:24 +0200758 next_update = data->last_updated +
759 msecs_to_jiffies(data->update_interval);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200760 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200761 dev_dbg(&client->dev, "Updating lm90 data.\n");
Guenter Roeck10bfef42016-06-13 19:26:45 -0700762
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700763 data->valid = false;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700764
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700765 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
766 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700767 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700768 data->temp8[LOCAL_LOW] = val;
769
770 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
771 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700772 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700773 data->temp8[LOCAL_HIGH] = val;
774
Jean Delvarea095f682011-07-27 23:22:25 -0700775 if (data->reg_local_ext) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700776 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
777 data->reg_local_ext);
778 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700779 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700780 data->temp11[LOCAL_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200781 } else {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700782 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
783 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700784 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700785 data->temp11[LOCAL_TEMP] = val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200786 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700787 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
788 LM90_REG_R_REMOTE_TEMPL);
789 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700790 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700791 data->temp11[REMOTE_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200792
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700793 val = lm90_read_reg(client, LM90_REG_R_STATUS);
794 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700795 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700796 data->alarms = val; /* lower 8 bit of alarms */
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200797
798 if (data->kind == max6696) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700799 val = lm90_select_remote_channel(data, 1);
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700800 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700801 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700802
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700803 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
804 LM90_REG_R_REMOTE_TEMPL);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700805 if (val < 0) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700806 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700807 return val;
808 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700809 data->temp11[REMOTE2_TEMP] = val;
810
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700811 lm90_select_remote_channel(data, 0);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200812
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700813 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
814 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700815 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700816 data->alarms |= val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200817 }
818
Guenter Roeckf36ffea2012-03-23 10:02:18 +0100819 /*
820 * Re-enable ALERT# output if it was originally enabled and
821 * relevant alarms are all clear
822 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700823 if (!(data->config_orig & 0x80) &&
824 !(data->alarms & data->alert_alarms)) {
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700825 if (data->config & 0x80) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200826 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700827 lm90_update_confreg(data, data->config & ~0x80);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200828 }
829 }
830
831 data->last_updated = jiffies;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700832 data->valid = true;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200833 }
834
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700835 return 0;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200836}
837
838/*
Nate Casecea50fe2008-10-17 17:51:10 +0200839 * Conversions
840 * For local temperatures and limits, critical limits and the hysteresis
841 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
842 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200843 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
844 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200845 */
846
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200847static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200848{
849 return val * 1000;
850}
851
Ben Hutchings271dabf2008-10-17 17:51:11 +0200852static inline int temp_from_u8(u8 val)
853{
854 return val * 1000;
855}
856
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200857static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200858{
859 return val / 32 * 125;
860}
861
Ben Hutchings271dabf2008-10-17 17:51:11 +0200862static inline int temp_from_u16(u16 val)
863{
864 return val / 32 * 125;
865}
866
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200867static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200868{
869 if (val <= -128000)
870 return -128;
871 if (val >= 127000)
872 return 127;
873 if (val < 0)
874 return (val - 500) / 1000;
875 return (val + 500) / 1000;
876}
877
Ben Hutchings271dabf2008-10-17 17:51:11 +0200878static u8 temp_to_u8(long val)
879{
880 if (val <= 0)
881 return 0;
882 if (val >= 255000)
883 return 255;
884 return (val + 500) / 1000;
885}
886
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200887static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200888{
889 if (val <= -128000)
890 return 0x8000;
891 if (val >= 127875)
892 return 0x7FE0;
893 if (val < 0)
894 return (val - 62) / 125 * 32;
895 return (val + 62) / 125 * 32;
896}
897
898static u8 hyst_to_reg(long val)
899{
900 if (val <= 0)
901 return 0;
902 if (val >= 30500)
903 return 31;
904 return (val + 500) / 1000;
905}
906
907/*
Nate Case23b2d472008-10-17 17:51:10 +0200908 * ADT7461 in compatibility mode is almost identical to LM90 except that
909 * attempts to write values that are outside the range 0 < temp < 127 are
910 * treated as the boundary value.
911 *
912 * ADT7461 in "extended mode" operation uses unsigned integers offset by
913 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200914 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200915static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200916{
Nate Case23b2d472008-10-17 17:51:10 +0200917 if (data->flags & LM90_FLAG_ADT7461_EXT)
918 return (val - 64) * 1000;
Guenter Roeck589f7072016-06-18 19:58:26 -0700919 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200920}
921
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200922static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200923{
Nate Case23b2d472008-10-17 17:51:10 +0200924 if (data->flags & LM90_FLAG_ADT7461_EXT)
925 return (val - 0x4000) / 64 * 250;
Guenter Roeck589f7072016-06-18 19:58:26 -0700926 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200927}
928
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200929static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200930{
931 if (data->flags & LM90_FLAG_ADT7461_EXT) {
932 if (val <= -64000)
933 return 0;
934 if (val >= 191000)
935 return 0xFF;
936 return (val + 500 + 64000) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200937 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700938 if (val <= 0)
939 return 0;
940 if (val >= 127000)
941 return 127;
942 return (val + 500) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200943}
944
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200945static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200946{
947 if (data->flags & LM90_FLAG_ADT7461_EXT) {
948 if (val <= -64000)
949 return 0;
950 if (val >= 191750)
951 return 0xFFC0;
952 return (val + 64000 + 125) / 250 * 64;
Nate Case23b2d472008-10-17 17:51:10 +0200953 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700954 if (val <= 0)
955 return 0;
956 if (val >= 127750)
957 return 0x7FC0;
958 return (val + 125) / 250 * 64;
Nate Casecea50fe2008-10-17 17:51:10 +0200959}
960
Jean Delvarec3df5802005-10-26 21:39:40 +0200961/* pec used for ADM1032 only */
Julia Lawalle57959a2016-12-22 13:05:22 +0100962static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
Jean Delvarec3df5802005-10-26 21:39:40 +0200963 char *buf)
964{
965 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700966
Jean Delvarec3df5802005-10-26 21:39:40 +0200967 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
968}
969
Julia Lawalle57959a2016-12-22 13:05:22 +0100970static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
971 const char *buf, size_t count)
Jean Delvarec3df5802005-10-26 21:39:40 +0200972{
973 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200974 long val;
975 int err;
976
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100977 err = kstrtol(buf, 10, &val);
Guenter Roeck11e57812010-10-28 20:31:42 +0200978 if (err < 0)
979 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200980
981 switch (val) {
982 case 0:
983 client->flags &= ~I2C_CLIENT_PEC;
984 break;
985 case 1:
986 client->flags |= I2C_CLIENT_PEC;
987 break;
988 default:
989 return -EINVAL;
990 }
991
992 return count;
993}
994
Julia Lawalle57959a2016-12-22 13:05:22 +0100995static DEVICE_ATTR_RW(pec);
Jean Delvarec3df5802005-10-26 21:39:40 +0200996
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700997static int lm90_get_temp11(struct lm90_data *data, int index)
998{
999 s16 temp11 = data->temp11[index];
1000 int temp;
1001
Guenter Roeckf347e242021-10-21 01:49:50 -07001002 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001003 temp = temp_from_u16_adt7461(data, temp11);
1004 else if (data->kind == max6646)
1005 temp = temp_from_u16(temp11);
1006 else
1007 temp = temp_from_s16(temp11);
1008
1009 /* +16 degrees offset for temp2 for the LM99 */
1010 if (data->kind == lm99 && index <= 2)
1011 temp += 16000;
1012
1013 return temp;
1014}
1015
1016static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1017{
1018 static struct reg {
1019 u8 high;
1020 u8 low;
1021 } reg[] = {
1022 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1023 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1024 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1025 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1026 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1027 };
1028 struct i2c_client *client = data->client;
1029 struct reg *regp = &reg[index];
1030 int err;
1031
1032 /* +16 degrees offset for temp2 for the LM99 */
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001033 if (data->kind == lm99 && index <= 2) {
1034 /* prevent integer underflow */
1035 val = max(val, -128000l);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001036 val -= 16000;
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001037 }
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001038
Guenter Roeckf347e242021-10-21 01:49:50 -07001039 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001040 data->temp11[index] = temp_to_u16_adt7461(data, val);
1041 else if (data->kind == max6646)
1042 data->temp11[index] = temp_to_u8(val) << 8;
1043 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1044 data->temp11[index] = temp_to_s16(val);
1045 else
1046 data->temp11[index] = temp_to_s8(val) << 8;
1047
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001048 lm90_select_remote_channel(data, index >= 3);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001049 err = i2c_smbus_write_byte_data(client, regp->high,
1050 data->temp11[index] >> 8);
1051 if (err < 0)
1052 return err;
1053 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1054 err = i2c_smbus_write_byte_data(client, regp->low,
1055 data->temp11[index] & 0xff);
1056
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001057 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001058 return err;
1059}
1060
1061static int lm90_get_temp8(struct lm90_data *data, int index)
1062{
1063 s8 temp8 = data->temp8[index];
1064 int temp;
1065
Guenter Roeckf347e242021-10-21 01:49:50 -07001066 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001067 temp = temp_from_u8_adt7461(data, temp8);
1068 else if (data->kind == max6646)
1069 temp = temp_from_u8(temp8);
1070 else
1071 temp = temp_from_s8(temp8);
1072
1073 /* +16 degrees offset for temp2 for the LM99 */
1074 if (data->kind == lm99 && index == 3)
1075 temp += 16000;
1076
1077 return temp;
1078}
1079
1080static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1081{
1082 static const u8 reg[TEMP8_REG_NUM] = {
1083 LM90_REG_W_LOCAL_LOW,
1084 LM90_REG_W_LOCAL_HIGH,
1085 LM90_REG_W_LOCAL_CRIT,
1086 LM90_REG_W_REMOTE_CRIT,
1087 MAX6659_REG_W_LOCAL_EMERG,
1088 MAX6659_REG_W_REMOTE_EMERG,
1089 LM90_REG_W_REMOTE_CRIT,
1090 MAX6659_REG_W_REMOTE_EMERG,
1091 };
1092 struct i2c_client *client = data->client;
1093 int err;
1094
1095 /* +16 degrees offset for temp2 for the LM99 */
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001096 if (data->kind == lm99 && index == 3) {
1097 /* prevent integer underflow */
1098 val = max(val, -128000l);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001099 val -= 16000;
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001100 }
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001101
Guenter Roeckf347e242021-10-21 01:49:50 -07001102 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001103 data->temp8[index] = temp_to_u8_adt7461(data, val);
1104 else if (data->kind == max6646)
1105 data->temp8[index] = temp_to_u8(val);
1106 else
1107 data->temp8[index] = temp_to_s8(val);
1108
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001109 lm90_select_remote_channel(data, index >= 6);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001110 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001111 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001112
1113 return err;
1114}
1115
1116static int lm90_get_temphyst(struct lm90_data *data, int index)
1117{
1118 int temp;
1119
Guenter Roeckf347e242021-10-21 01:49:50 -07001120 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001121 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1122 else if (data->kind == max6646)
1123 temp = temp_from_u8(data->temp8[index]);
1124 else
1125 temp = temp_from_s8(data->temp8[index]);
1126
1127 /* +16 degrees offset for temp2 for the LM99 */
1128 if (data->kind == lm99 && index == 3)
1129 temp += 16000;
1130
1131 return temp - temp_from_s8(data->temp_hyst);
1132}
1133
1134static int lm90_set_temphyst(struct lm90_data *data, long val)
1135{
1136 struct i2c_client *client = data->client;
1137 int temp;
1138 int err;
1139
Guenter Roeckf347e242021-10-21 01:49:50 -07001140 if (data->flags & LM90_HAVE_EXTENDED_TEMP)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001141 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1142 else if (data->kind == max6646)
1143 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1144 else
1145 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1146
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001147 /* prevent integer underflow */
1148 val = max(val, -128000l);
1149
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001150 data->temp_hyst = hyst_to_reg(temp - val);
1151 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1152 data->temp_hyst);
1153 return err;
1154}
1155
1156static const u8 lm90_temp_index[3] = {
1157 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1158};
1159
1160static const u8 lm90_temp_min_index[3] = {
1161 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1162};
1163
1164static const u8 lm90_temp_max_index[3] = {
1165 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1166};
1167
1168static const u8 lm90_temp_crit_index[3] = {
1169 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1170};
1171
1172static const u8 lm90_temp_emerg_index[3] = {
1173 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1174};
1175
1176static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
Michael Wallee9572fd2017-01-02 17:53:39 +01001177static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001178static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1179static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1180static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1181
1182static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1183{
1184 struct lm90_data *data = dev_get_drvdata(dev);
1185 int err;
1186
1187 mutex_lock(&data->update_lock);
1188 err = lm90_update_device(dev);
1189 mutex_unlock(&data->update_lock);
1190 if (err)
1191 return err;
1192
1193 switch (attr) {
1194 case hwmon_temp_input:
1195 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1196 break;
1197 case hwmon_temp_min_alarm:
1198 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1199 break;
1200 case hwmon_temp_max_alarm:
1201 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1202 break;
1203 case hwmon_temp_crit_alarm:
1204 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1205 break;
1206 case hwmon_temp_emergency_alarm:
1207 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1208 break;
1209 case hwmon_temp_fault:
1210 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1211 break;
1212 case hwmon_temp_min:
1213 if (channel == 0)
1214 *val = lm90_get_temp8(data,
1215 lm90_temp_min_index[channel]);
1216 else
1217 *val = lm90_get_temp11(data,
1218 lm90_temp_min_index[channel]);
1219 break;
1220 case hwmon_temp_max:
1221 if (channel == 0)
1222 *val = lm90_get_temp8(data,
1223 lm90_temp_max_index[channel]);
1224 else
1225 *val = lm90_get_temp11(data,
1226 lm90_temp_max_index[channel]);
1227 break;
1228 case hwmon_temp_crit:
1229 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1230 break;
1231 case hwmon_temp_crit_hyst:
1232 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1233 break;
1234 case hwmon_temp_emergency:
1235 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1236 break;
1237 case hwmon_temp_emergency_hyst:
1238 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1239 break;
1240 case hwmon_temp_offset:
1241 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1242 break;
1243 default:
1244 return -EOPNOTSUPP;
1245 }
1246 return 0;
1247}
1248
1249static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1250{
1251 struct lm90_data *data = dev_get_drvdata(dev);
1252 int err;
1253
1254 mutex_lock(&data->update_lock);
1255
1256 err = lm90_update_device(dev);
1257 if (err)
1258 goto error;
1259
1260 switch (attr) {
1261 case hwmon_temp_min:
1262 if (channel == 0)
1263 err = lm90_set_temp8(data,
1264 lm90_temp_min_index[channel],
1265 val);
1266 else
1267 err = lm90_set_temp11(data,
1268 lm90_temp_min_index[channel],
1269 val);
1270 break;
1271 case hwmon_temp_max:
1272 if (channel == 0)
1273 err = lm90_set_temp8(data,
1274 lm90_temp_max_index[channel],
1275 val);
1276 else
1277 err = lm90_set_temp11(data,
1278 lm90_temp_max_index[channel],
1279 val);
1280 break;
1281 case hwmon_temp_crit:
1282 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1283 break;
1284 case hwmon_temp_crit_hyst:
1285 err = lm90_set_temphyst(data, val);
1286 break;
1287 case hwmon_temp_emergency:
1288 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1289 break;
1290 case hwmon_temp_offset:
1291 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1292 break;
1293 default:
1294 err = -EOPNOTSUPP;
1295 break;
1296 }
1297error:
1298 mutex_unlock(&data->update_lock);
1299
1300 return err;
1301}
1302
1303static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1304{
1305 switch (attr) {
1306 case hwmon_temp_input:
1307 case hwmon_temp_min_alarm:
1308 case hwmon_temp_max_alarm:
1309 case hwmon_temp_crit_alarm:
1310 case hwmon_temp_emergency_alarm:
1311 case hwmon_temp_emergency_hyst:
1312 case hwmon_temp_fault:
Guenter Roeck33348512018-12-10 14:02:12 -08001313 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001314 case hwmon_temp_min:
1315 case hwmon_temp_max:
1316 case hwmon_temp_crit:
1317 case hwmon_temp_emergency:
1318 case hwmon_temp_offset:
Guenter Roeck33348512018-12-10 14:02:12 -08001319 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001320 case hwmon_temp_crit_hyst:
1321 if (channel == 0)
Guenter Roeck33348512018-12-10 14:02:12 -08001322 return 0644;
1323 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001324 default:
1325 return 0;
1326 }
1327}
1328
1329static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1330{
1331 struct lm90_data *data = dev_get_drvdata(dev);
1332 int err;
1333
1334 mutex_lock(&data->update_lock);
1335 err = lm90_update_device(dev);
1336 mutex_unlock(&data->update_lock);
1337 if (err)
1338 return err;
1339
1340 switch (attr) {
1341 case hwmon_chip_update_interval:
1342 *val = data->update_interval;
1343 break;
1344 case hwmon_chip_alarms:
1345 *val = data->alarms;
1346 break;
1347 default:
1348 return -EOPNOTSUPP;
1349 }
1350
1351 return 0;
1352}
1353
1354static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1355{
1356 struct lm90_data *data = dev_get_drvdata(dev);
1357 struct i2c_client *client = data->client;
1358 int err;
1359
1360 mutex_lock(&data->update_lock);
1361
1362 err = lm90_update_device(dev);
1363 if (err)
1364 goto error;
1365
1366 switch (attr) {
1367 case hwmon_chip_update_interval:
1368 err = lm90_set_convrate(client, data,
1369 clamp_val(val, 0, 100000));
1370 break;
1371 default:
1372 err = -EOPNOTSUPP;
1373 break;
1374 }
1375error:
1376 mutex_unlock(&data->update_lock);
1377
1378 return err;
1379}
1380
1381static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1382{
1383 switch (attr) {
1384 case hwmon_chip_update_interval:
Guenter Roeck33348512018-12-10 14:02:12 -08001385 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001386 case hwmon_chip_alarms:
Guenter Roeck33348512018-12-10 14:02:12 -08001387 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001388 default:
1389 return 0;
1390 }
1391}
1392
1393static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1394 u32 attr, int channel, long *val)
1395{
1396 switch (type) {
1397 case hwmon_chip:
1398 return lm90_chip_read(dev, attr, channel, val);
1399 case hwmon_temp:
1400 return lm90_temp_read(dev, attr, channel, val);
1401 default:
1402 return -EOPNOTSUPP;
1403 }
1404}
1405
1406static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1407 u32 attr, int channel, long val)
1408{
1409 switch (type) {
1410 case hwmon_chip:
1411 return lm90_chip_write(dev, attr, channel, val);
1412 case hwmon_temp:
1413 return lm90_temp_write(dev, attr, channel, val);
1414 default:
1415 return -EOPNOTSUPP;
1416 }
1417}
1418
1419static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1420 u32 attr, int channel)
1421{
1422 switch (type) {
1423 case hwmon_chip:
1424 return lm90_chip_is_visible(data, attr, channel);
1425 case hwmon_temp:
1426 return lm90_temp_is_visible(data, attr, channel);
1427 default:
1428 return 0;
1429 }
1430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jean Delvare9b0e8522008-07-16 19:30:15 +02001432/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001433static int lm90_detect(struct i2c_client *client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001434 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001436 struct i2c_adapter *adapter = client->adapter;
1437 int address = client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001438 const char *name = NULL;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001439 int man_id, chip_id, config1, config2, convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001442 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Jean Delvare8f2fa772009-12-09 20:35:53 +01001444 /* detection and identification */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001445 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1446 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1447 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1448 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1449 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001450 return -ENODEV;
1451
Jean Delvaref90be422011-07-24 20:37:05 +02001452 if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
Jean Delvareb2589ab2011-11-04 12:00:47 +01001453 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1454 if (config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001455 return -ENODEV;
Jean Delvaref90be422011-07-24 20:37:05 +02001456 } else
Jean Delvareb2589ab2011-11-04 12:00:47 +01001457 config2 = 0; /* Make compiler happy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jean Delvaref90be422011-07-24 20:37:05 +02001459 if ((address == 0x4C || address == 0x4D)
1460 && man_id == 0x01) { /* National Semiconductor */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001461 if ((config1 & 0x2A) == 0x00
1462 && (config2 & 0xF8) == 0x00
1463 && convrate <= 0x09) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001464 if (address == 0x4C
1465 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1466 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001468 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1469 name = "lm99";
1470 dev_info(&adapter->dev,
1471 "Assuming LM99 chip at 0x%02x\n",
1472 address);
1473 dev_info(&adapter->dev,
1474 "If it is an LM89, instantiate it "
1475 "with the new_device sysfs "
1476 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001477 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001478 if (address == 0x4C
1479 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1480 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001483 } else
1484 if ((address == 0x4C || address == 0x4D)
1485 && man_id == 0x41) { /* Analog Devices */
1486 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001487 && (config1 & 0x3F) == 0x00
1488 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001489 name = "adm1032";
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001490 /*
1491 * The ADM1032 supports PEC, but only if combined
1492 * transactions are not used.
1493 */
Jean Delvare8f2fa772009-12-09 20:35:53 +01001494 if (i2c_check_functionality(adapter,
1495 I2C_FUNC_SMBUS_BYTE))
1496 info->flags |= I2C_CLIENT_PEC;
1497 } else
1498 if (chip_id == 0x51 /* ADT7461 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001499 && (config1 & 0x1B) == 0x00
1500 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001501 name = "adt7461";
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001502 } else
1503 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001504 && (config1 & 0x1B) == 0x00
1505 && convrate <= 0x0A) {
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001506 name = "adt7461a";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001507 }
1508 } else
1509 if (man_id == 0x4D) { /* Maxim */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001510 int emerg, emerg2, status2;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001511
1512 /*
1513 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1514 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1515 * exists, both readings will reflect the same value. Otherwise,
1516 * the readings will be different.
1517 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001518 emerg = i2c_smbus_read_byte_data(client,
1519 MAX6659_REG_R_REMOTE_EMERG);
1520 man_id = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001521 LM90_REG_R_MAN_ID);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001522 emerg2 = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001523 MAX6659_REG_R_REMOTE_EMERG);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001524 status2 = i2c_smbus_read_byte_data(client,
1525 MAX6696_REG_R_STATUS2);
1526 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001527 return -ENODEV;
1528
Jean Delvare8f2fa772009-12-09 20:35:53 +01001529 /*
1530 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1531 * register. Reading from that address will return the last
1532 * read value, which in our case is those of the man_id
1533 * register. Likewise, the config1 register seems to lack a
1534 * low nibble, so the value will be those of the previous
1535 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001536 * MAX6659 has a third set of upper temperature limit registers.
1537 * Those registers also return values on MAX6657 and MAX6658,
1538 * thus the only way to detect MAX6659 is by its address.
1539 * For this reason it will be mis-detected as MAX6657 if its
1540 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001541 */
1542 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001543 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvareb2589ab2011-11-04 12:00:47 +01001544 && (config1 & 0x1F) == (man_id & 0x0F)
1545 && convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001546 if (address == 0x4C)
1547 name = "max6657";
1548 else
1549 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001550 } else
1551 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001552 * Even though MAX6695 and MAX6696 do not have a chip ID
1553 * register, reading it returns 0x01. Bit 4 of the config1
1554 * register is unused and should return zero when read. Bit 0 of
1555 * the status2 register is unused and should return zero when
1556 * read.
1557 *
1558 * MAX6695 and MAX6696 have an additional set of temperature
1559 * limit registers. We can detect those chips by checking if
1560 * one of those registers exists.
1561 */
1562 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001563 && (config1 & 0x10) == 0x00
1564 && (status2 & 0x01) == 0x00
1565 && emerg == emerg2
1566 && convrate <= 0x07) {
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001567 name = "max6696";
1568 } else
1569 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001570 * The chip_id register of the MAX6680 and MAX6681 holds the
1571 * revision of the chip. The lowest bit of the config1 register
1572 * is unused and should return zero when read, so should the
1573 * second to last bit of config1 (software reset).
1574 */
1575 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001576 && (config1 & 0x03) == 0x00
1577 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001578 name = "max6680";
1579 } else
1580 /*
1581 * The chip_id register of the MAX6646/6647/6649 holds the
1582 * revision of the chip. The lowest 6 bits of the config1
1583 * register are unused and should return zero when read.
1584 */
1585 if (chip_id == 0x59
Jean Delvareb2589ab2011-11-04 12:00:47 +01001586 && (config1 & 0x3f) == 0x00
1587 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001588 name = "max6646";
Josh Lehan229d4952020-05-13 11:42:48 -07001589 } else
1590 /*
1591 * The chip_id of the MAX6654 holds the revision of the chip.
1592 * The lowest 3 bits of the config1 register are unused and
1593 * should return zero when read.
1594 */
1595 if (chip_id == 0x08
1596 && (config1 & 0x07) == 0x00
1597 && convrate <= 0x07) {
1598 name = "max6654";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001600 } else
1601 if (address == 0x4C
1602 && man_id == 0x5C) { /* Winbond/Nuvoton */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001603 if ((config1 & 0x2A) == 0x00
1604 && (config2 & 0xF8) == 0x00) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001605 if (chip_id == 0x01 /* W83L771W/G */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001606 && convrate <= 0x09) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001607 name = "w83l771";
1608 } else
1609 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001610 && convrate <= 0x08) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001611 name = "w83l771";
1612 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001613 }
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001614 } else
Jean Delvare6d101c52011-07-24 20:36:15 +02001615 if (address >= 0x48 && address <= 0x4F
1616 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
Jean Delvare6d101c52011-07-24 20:36:15 +02001617 if (chip_id == 0x00
Jean Delvareb2589ab2011-11-04 12:00:47 +01001618 && (config1 & 0x2A) == 0x00
1619 && (config2 & 0xFE) == 0x00
1620 && convrate <= 0x09) {
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001621 name = "sa56004";
1622 }
Guenter Roeckae544f62012-03-23 10:02:18 +01001623 } else
1624 if ((address == 0x4C || address == 0x4D)
1625 && man_id == 0x47) { /* GMT */
1626 if (chip_id == 0x01 /* G781 */
1627 && (config1 & 0x3F) == 0x00
1628 && convrate <= 0x08)
1629 name = "g781";
Wei Ni1daaceb2013-11-15 10:40:39 +01001630 } else
1631 if (address == 0x4C
1632 && man_id == 0x55) { /* Texas Instruments */
1633 int local_ext;
1634
1635 local_ext = i2c_smbus_read_byte_data(client,
1636 TMP451_REG_R_LOCAL_TEMPL);
1637
1638 if (chip_id == 0x00 /* TMP451 */
1639 && (config1 & 0x1B) == 0x00
1640 && convrate <= 0x09
1641 && (local_ext & 0x0F) == 0x00)
1642 name = "tmp451";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Jean Delvare8f2fa772009-12-09 20:35:53 +01001645 if (!name) { /* identification failed */
1646 dev_dbg(&adapter->dev,
1647 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1648 "chip_id=0x%02X)\n", address, man_id, chip_id);
1649 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001651
Jean Delvare9b0e8522008-07-16 19:30:15 +02001652 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jean Delvare9b0e8522008-07-16 19:30:15 +02001654 return 0;
1655}
1656
Guenter Roeck1f17a442016-06-13 06:19:11 -07001657static void lm90_restore_conf(void *_data)
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001658{
Guenter Roeck1f17a442016-06-13 06:19:11 -07001659 struct lm90_data *data = _data;
1660 struct i2c_client *client = data->client;
1661
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001662 /* Restore initial configuration */
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001663 lm90_write_convrate(data, data->convrate_orig);
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001664 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1665 data->config_orig);
1666}
1667
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001668static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001669{
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001670 int config, convrate;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001671
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001672 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1673 if (convrate < 0)
1674 return convrate;
Guenter Roeck0c01b642010-10-28 20:31:44 +02001675 data->convrate_orig = convrate;
1676
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001677 /*
1678 * Start the conversions.
1679 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001680 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1681 if (config < 0)
1682 return config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001683 data->config_orig = config;
Guenter Roeckb849e5d2019-06-30 15:14:19 -07001684 data->config = config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001685
Boyang Yu62456182019-06-28 19:06:36 +00001686 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1687
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001688 /* Check Temperature Range Select */
Guenter Roeckf347e242021-10-21 01:49:50 -07001689 if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001690 if (config & 0x04)
1691 data->flags |= LM90_FLAG_ADT7461_EXT;
1692 }
1693
1694 /*
1695 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1696 * 0.125 degree resolution) and range (0x08, extend range
1697 * to -64 degree) mode for the remote temperature sensor.
1698 */
1699 if (data->kind == max6680)
1700 config |= 0x18;
1701
1702 /*
Josh Lehan229d4952020-05-13 11:42:48 -07001703 * Put MAX6654 into extended range (0x20, extend minimum range from
1704 * 0 degrees to -64 degrees). Note that extended resolution is not
1705 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1706 * slower, which is intentionally not done by default.
1707 */
1708 if (data->kind == max6654)
1709 config |= 0x20;
1710
1711 /*
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001712 * Select external channel 0 for max6695/96
1713 */
1714 if (data->kind == max6696)
1715 config &= ~0x08;
1716
Dmitry Osipenko2abdc352021-06-19 00:54:54 +03001717 /*
1718 * Interrupt is enabled by default on reset, but it may be disabled
1719 * by bootloader, unmask it.
1720 */
1721 if (client->irq)
1722 config &= ~0x80;
1723
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001724 config &= 0xBF; /* run */
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001725 lm90_update_confreg(data, config);
Guenter Roeck1f17a442016-06-13 06:19:11 -07001726
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001727 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001728}
1729
Wei Ni072de492013-11-15 10:40:38 +01001730static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1731{
1732 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001733 int st, st2 = 0;
Wei Ni072de492013-11-15 10:40:38 +01001734
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001735 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1736 if (st < 0)
1737 return false;
Wei Ni072de492013-11-15 10:40:38 +01001738
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001739 if (data->kind == max6696) {
1740 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1741 if (st2 < 0)
1742 return false;
1743 }
Wei Ni072de492013-11-15 10:40:38 +01001744
1745 *status = st | (st2 << 8);
1746
1747 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1748 return false;
1749
1750 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1751 (st2 & MAX6696_STATUS2_LOT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001752 dev_dbg(&client->dev,
1753 "temp%d out of range, please check!\n", 1);
Wei Ni072de492013-11-15 10:40:38 +01001754 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1755 (st2 & MAX6696_STATUS2_ROT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001756 dev_dbg(&client->dev,
1757 "temp%d out of range, please check!\n", 2);
Wei Ni072de492013-11-15 10:40:38 +01001758 if (st & LM90_STATUS_ROPEN)
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001759 dev_dbg(&client->dev,
1760 "temp%d diode open, please check!\n", 2);
Wei Ni072de492013-11-15 10:40:38 +01001761 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1762 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001763 dev_dbg(&client->dev,
1764 "temp%d out of range, please check!\n", 3);
Wei Ni072de492013-11-15 10:40:38 +01001765 if (st2 & MAX6696_STATUS2_R2OPEN)
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001766 dev_dbg(&client->dev,
1767 "temp%d diode open, please check!\n", 3);
1768
1769 if (st & LM90_STATUS_LLOW)
1770 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1771 hwmon_temp_min, 0);
1772 if (st & LM90_STATUS_RLOW)
1773 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1774 hwmon_temp_min, 1);
1775 if (st2 & MAX6696_STATUS2_R2LOW)
1776 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1777 hwmon_temp_min, 2);
1778 if (st & LM90_STATUS_LHIGH)
1779 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1780 hwmon_temp_max, 0);
1781 if (st & LM90_STATUS_RHIGH)
1782 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1783 hwmon_temp_max, 1);
1784 if (st2 & MAX6696_STATUS2_R2HIGH)
1785 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1786 hwmon_temp_max, 2);
Wei Ni072de492013-11-15 10:40:38 +01001787
1788 return true;
1789}
1790
Wei Ni109b1282013-11-15 10:40:39 +01001791static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1792{
1793 struct i2c_client *client = dev_id;
1794 u16 status;
1795
1796 if (lm90_is_tripped(client, &status))
1797 return IRQ_HANDLED;
1798 else
1799 return IRQ_NONE;
1800}
1801
Guenter Roeck1f17a442016-06-13 06:19:11 -07001802static void lm90_remove_pec(void *dev)
1803{
1804 device_remove_file(dev, &dev_attr_pec);
1805}
1806
1807static void lm90_regulator_disable(void *regulator)
1808{
1809 regulator_disable(regulator);
1810}
1811
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001812
1813static const struct hwmon_ops lm90_ops = {
1814 .is_visible = lm90_is_visible,
1815 .read = lm90_read,
1816 .write = lm90_write,
1817};
1818
Stephen Kitt67487032020-08-13 18:02:22 +02001819static int lm90_probe(struct i2c_client *client)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001820{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001821 struct device *dev = &client->dev;
Wolfram Sange67776c2019-06-10 11:51:54 +02001822 struct i2c_adapter *adapter = client->adapter;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001823 struct hwmon_channel_info *info;
Wei Ni3e0f9642013-11-15 10:40:39 +01001824 struct regulator *regulator;
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001825 struct device *hwmon_dev;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001826 struct lm90_data *data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001827 int err;
1828
Wei Ni3e0f9642013-11-15 10:40:39 +01001829 regulator = devm_regulator_get(dev, "vcc");
1830 if (IS_ERR(regulator))
1831 return PTR_ERR(regulator);
1832
1833 err = regulator_enable(regulator);
1834 if (err < 0) {
Guenter Roeckd89fa682014-04-04 18:01:35 +02001835 dev_err(dev, "Failed to enable regulator: %d\n", err);
Wei Ni3e0f9642013-11-15 10:40:39 +01001836 return err;
1837 }
1838
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001839 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1840 if (err)
1841 return err;
Guenter Roeck1f17a442016-06-13 06:19:11 -07001842
Guenter Roeckd89fa682014-04-04 18:01:35 +02001843 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
Guenter Roeck20f426f2012-06-02 09:58:10 -07001844 if (!data)
1845 return -ENOMEM;
1846
Guenter Roeck1de8b252014-04-04 18:01:35 +02001847 data->client = client;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001848 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001849 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Jean Delvare9b0e8522008-07-16 19:30:15 +02001851 /* Set the device type */
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03001852 if (client->dev.of_node)
1853 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1854 else
Stephen Kitt67487032020-08-13 18:02:22 +02001855 data->kind = i2c_match_id(lm90_id, client)->driver_data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001856 if (data->kind == adm1032) {
1857 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
Jean Delvareb2589ab2011-11-04 12:00:47 +01001858 client->flags &= ~I2C_CLIENT_PEC;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001861 /*
1862 * Different devices have different alarm bits triggering the
1863 * ALERT# output
1864 */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001865 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001866
Guenter Roeck88073bb2010-10-28 20:31:43 +02001867 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001868 data->flags = lm90_params[data->kind].flags;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001869
1870 data->chip.ops = &lm90_ops;
1871 data->chip.info = data->info;
1872
Guenter Roecka4d41e62019-03-31 10:53:48 -07001873 data->info[0] = HWMON_CHANNEL_INFO(chip,
1874 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001875 data->info[1] = &data->temp_info;
1876
1877 info = &data->temp_info;
1878 info->type = hwmon_temp;
1879 info->config = data->channel_config;
1880
1881 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1882 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1883 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1884 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1885 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1886 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1887
1888 if (data->flags & LM90_HAVE_OFFSET)
1889 data->channel_config[1] |= HWMON_T_OFFSET;
1890
1891 if (data->flags & LM90_HAVE_EMERGENCY) {
1892 data->channel_config[0] |= HWMON_T_EMERGENCY |
1893 HWMON_T_EMERGENCY_HYST;
1894 data->channel_config[1] |= HWMON_T_EMERGENCY |
1895 HWMON_T_EMERGENCY_HYST;
1896 }
1897
1898 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1899 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1900 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1901 }
1902
1903 if (data->flags & LM90_HAVE_TEMP3) {
1904 data->channel_config[2] = HWMON_T_INPUT |
1905 HWMON_T_MIN | HWMON_T_MAX |
1906 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1907 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1908 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1909 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1910 HWMON_T_FAULT;
1911 }
1912
Jean Delvarea095f682011-07-27 23:22:25 -07001913 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001914
Guenter Roeck0c01b642010-10-28 20:31:44 +02001915 /* Set maximum conversion rate */
1916 data->max_convrate = lm90_params[data->kind].max_convrate;
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* Initialize the LM90 chip */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001919 err = lm90_init_client(client, data);
1920 if (err < 0) {
1921 dev_err(dev, "Failed to initialize device\n");
1922 return err;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001925 /*
1926 * The 'pec' attribute is attached to the i2c device and thus created
1927 * separately.
1928 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001929 if (client->flags & I2C_CLIENT_PEC) {
1930 err = device_create_file(dev, &dev_attr_pec);
Guenter Roeck11e57812010-10-28 20:31:42 +02001931 if (err)
Guenter Roeck1f17a442016-06-13 06:19:11 -07001932 return err;
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001933 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1934 if (err)
1935 return err;
Jean Delvare0e39e012006-09-24 21:16:40 +02001936 }
Guenter Roeck084489e2014-04-04 18:01:35 +02001937
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001938 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1939 data, &data->chip,
1940 NULL);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001941 if (IS_ERR(hwmon_dev))
1942 return PTR_ERR(hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001943
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001944 data->hwmon_dev = hwmon_dev;
1945
Wei Ni109b1282013-11-15 10:40:39 +01001946 if (client->irq) {
1947 dev_dbg(dev, "IRQ: %d\n", client->irq);
1948 err = devm_request_threaded_irq(dev, client->irq,
1949 NULL, lm90_irq_thread,
Dmitry Osipenkod97fb832021-06-19 00:54:52 +03001950 IRQF_ONESHOT, "lm90", client);
Wei Ni109b1282013-11-15 10:40:39 +01001951 if (err < 0) {
1952 dev_err(dev, "cannot request IRQ %d\n", client->irq);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001953 return err;
Wei Ni109b1282013-11-15 10:40:39 +01001954 }
1955 }
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001960static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1961 unsigned int flag)
Jean Delvare53de3342010-03-05 22:17:15 +01001962{
Wei Ni072de492013-11-15 10:40:38 +01001963 u16 alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001964
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001965 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1966 return;
1967
Wei Ni072de492013-11-15 10:40:38 +01001968 if (lm90_is_tripped(client, &alarms)) {
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001969 /*
1970 * Disable ALERT# output, because these chips don't implement
1971 * SMBus alert correctly; they should only hold the alert line
1972 * low briefly.
1973 */
Wei Ni072de492013-11-15 10:40:38 +01001974 struct lm90_data *data = i2c_get_clientdata(client);
1975
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001976 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1977 (alarms & data->alert_alarms)) {
Jean Delvare53de3342010-03-05 22:17:15 +01001978 dev_dbg(&client->dev, "Disabling ALERT#\n");
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001979 lm90_update_confreg(data, data->config | 0x80);
Jean Delvare53de3342010-03-05 22:17:15 +01001980 }
Wei Ni072de492013-11-15 10:40:38 +01001981 } else {
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001982 dev_dbg(&client->dev, "Everything OK\n");
Jean Delvare53de3342010-03-05 22:17:15 +01001983 }
1984}
1985
Dmitry Osipenko4c7f85a2021-06-19 00:54:55 +03001986static int __maybe_unused lm90_suspend(struct device *dev)
1987{
1988 struct lm90_data *data = dev_get_drvdata(dev);
1989 struct i2c_client *client = data->client;
1990
1991 if (client->irq)
1992 disable_irq(client->irq);
1993
1994 return 0;
1995}
1996
1997static int __maybe_unused lm90_resume(struct device *dev)
1998{
1999 struct lm90_data *data = dev_get_drvdata(dev);
2000 struct i2c_client *client = data->client;
2001
2002 if (client->irq)
2003 enable_irq(client->irq);
2004
2005 return 0;
2006}
2007
2008static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2009
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002010static struct i2c_driver lm90_driver = {
2011 .class = I2C_CLASS_HWMON,
2012 .driver = {
2013 .name = "lm90",
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03002014 .of_match_table = of_match_ptr(lm90_of_match),
Dmitry Osipenko4c7f85a2021-06-19 00:54:55 +03002015 .pm = &lm90_pm_ops,
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002016 },
Stephen Kitt67487032020-08-13 18:02:22 +02002017 .probe_new = lm90_probe,
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002018 .alert = lm90_alert,
2019 .id_table = lm90_id,
2020 .detect = lm90_detect,
2021 .address_list = normal_i2c,
2022};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Axel Linf0967ee2012-01-20 15:38:18 +08002024module_i2c_driver(lm90_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Jean Delvare7c81c60f2014-01-29 20:40:08 +01002026MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027MODULE_DESCRIPTION("LM90/ADM1032 driver");
2028MODULE_LICENSE("GPL");