blob: 567b7c521f38836f605b4dafdffd731690fb03d3 [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 */
Boyang Yu62456182019-06-28 19:06:36 +0000185#define LM90_PAUSE_FOR_CONFIG (1 << 8) /* Pause conversion for config */
Nate Case23b2d472008-10-17 17:51:10 +0200186
Wei Ni072de492013-11-15 10:40:38 +0100187/* LM90 status */
188#define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
189#define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
190#define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
191#define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
192#define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
193#define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
194#define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
195
196#define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
197#define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
198#define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
199#define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
200#define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
201#define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
202#define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
203
Nate Case23b2d472008-10-17 17:51:10 +0200204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Driver data (common to all clients)
206 */
207
Jean Delvare9b0e8522008-07-16 19:30:15 +0200208static const struct i2c_device_id lm90_id[] = {
209 { "adm1032", adm1032 },
210 { "adt7461", adt7461 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200211 { "adt7461a", adt7461 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100212 { "g781", g781 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200213 { "lm90", lm90 },
214 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100215 { "lm89", lm86 },
216 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200217 { "max6646", max6646 },
218 { "max6647", max6646 },
219 { "max6649", max6646 },
Josh Lehan229d4952020-05-13 11:42:48 -0700220 { "max6654", max6654 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200221 { "max6657", max6657 },
222 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200223 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200224 { "max6680", max6680 },
225 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200226 { "max6695", max6696 },
227 { "max6696", max6696 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200228 { "nct1008", adt7461 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100229 { "w83l771", w83l771 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000230 { "sa56004", sa56004 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100231 { "tmp451", tmp451 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200232 { }
233};
234MODULE_DEVICE_TABLE(i2c, lm90_id);
235
Guenter Roeck787afaa2019-04-04 06:21:17 -0700236static const struct of_device_id __maybe_unused lm90_of_match[] = {
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -0300237 {
238 .compatible = "adi,adm1032",
239 .data = (void *)adm1032
240 },
241 {
242 .compatible = "adi,adt7461",
243 .data = (void *)adt7461
244 },
245 {
246 .compatible = "adi,adt7461a",
247 .data = (void *)adt7461
248 },
249 {
250 .compatible = "gmt,g781",
251 .data = (void *)g781
252 },
253 {
254 .compatible = "national,lm90",
255 .data = (void *)lm90
256 },
257 {
258 .compatible = "national,lm86",
259 .data = (void *)lm86
260 },
261 {
262 .compatible = "national,lm89",
263 .data = (void *)lm86
264 },
265 {
266 .compatible = "national,lm99",
267 .data = (void *)lm99
268 },
269 {
270 .compatible = "dallas,max6646",
271 .data = (void *)max6646
272 },
273 {
274 .compatible = "dallas,max6647",
275 .data = (void *)max6646
276 },
277 {
278 .compatible = "dallas,max6649",
279 .data = (void *)max6646
280 },
281 {
Josh Lehan229d4952020-05-13 11:42:48 -0700282 .compatible = "dallas,max6654",
283 .data = (void *)max6654
284 },
285 {
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -0300286 .compatible = "dallas,max6657",
287 .data = (void *)max6657
288 },
289 {
290 .compatible = "dallas,max6658",
291 .data = (void *)max6657
292 },
293 {
294 .compatible = "dallas,max6659",
295 .data = (void *)max6659
296 },
297 {
298 .compatible = "dallas,max6680",
299 .data = (void *)max6680
300 },
301 {
302 .compatible = "dallas,max6681",
303 .data = (void *)max6680
304 },
305 {
306 .compatible = "dallas,max6695",
307 .data = (void *)max6696
308 },
309 {
310 .compatible = "dallas,max6696",
311 .data = (void *)max6696
312 },
313 {
314 .compatible = "onnn,nct1008",
315 .data = (void *)adt7461
316 },
317 {
318 .compatible = "winbond,w83l771",
319 .data = (void *)w83l771
320 },
321 {
322 .compatible = "nxp,sa56004",
323 .data = (void *)sa56004
324 },
325 {
326 .compatible = "ti,tmp451",
327 .data = (void *)tmp451
328 },
329 { },
330};
331MODULE_DEVICE_TABLE(of, lm90_of_match);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200334 * chip type specific parameters
335 */
336struct lm90_params {
337 u32 flags; /* Capabilities */
338 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
339 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200340 u8 max_convrate; /* Maximum conversion rate register value */
Jean Delvarea095f682011-07-27 23:22:25 -0700341 u8 reg_local_ext; /* Extended local temp register (optional) */
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200342};
343
344static const struct lm90_params lm90_params[] = {
345 [adm1032] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200346 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
347 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200348 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200349 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200350 },
351 [adt7461] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200352 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
353 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200354 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200355 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200356 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100357 [g781] = {
358 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
359 | LM90_HAVE_BROKEN_ALERT,
360 .alert_alarms = 0x7c,
361 .max_convrate = 8,
362 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200363 [lm86] = {
364 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
365 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200366 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200367 },
368 [lm90] = {
369 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
370 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200371 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200372 },
373 [lm99] = {
374 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
375 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200376 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200377 },
378 [max6646] = {
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200379 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200380 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000381 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200382 },
Josh Lehan229d4952020-05-13 11:42:48 -0700383 [max6654] = {
384 .alert_alarms = 0x7c,
385 .max_convrate = 7,
386 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
387 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200388 [max6657] = {
Boyang Yu62456182019-06-28 19:06:36 +0000389 .flags = LM90_PAUSE_FOR_CONFIG,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200390 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200391 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000392 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200393 },
394 [max6659] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700395 .flags = LM90_HAVE_EMERGENCY,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200396 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200397 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000398 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200399 },
400 [max6680] = {
401 .flags = LM90_HAVE_OFFSET,
402 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200403 .max_convrate = 7,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200404 },
405 [max6696] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700406 .flags = LM90_HAVE_EMERGENCY
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200407 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
Guenter Roecke41fae2b2013-11-15 10:40:38 +0100408 .alert_alarms = 0x1c7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200409 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000410 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200411 },
412 [w83l771] = {
413 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
414 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200415 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200416 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000417 [sa56004] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700418 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000419 .alert_alarms = 0x7b,
420 .max_convrate = 9,
421 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
422 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100423 [tmp451] = {
424 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
425 | LM90_HAVE_BROKEN_ALERT,
426 .alert_alarms = 0x7c,
427 .max_convrate = 9,
428 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700429 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200430};
431
432/*
Wei Ni40465d92013-11-15 10:40:39 +0100433 * TEMP8 register index
434 */
435enum lm90_temp8_reg_index {
436 LOCAL_LOW = 0,
437 LOCAL_HIGH,
438 LOCAL_CRIT,
439 REMOTE_CRIT,
440 LOCAL_EMERG, /* max6659 and max6695/96 */
441 REMOTE_EMERG, /* max6659 and max6695/96 */
442 REMOTE2_CRIT, /* max6695/96 only */
443 REMOTE2_EMERG, /* max6695/96 only */
444 TEMP8_REG_NUM
445};
446
447/*
448 * TEMP11 register index
449 */
450enum lm90_temp11_reg_index {
451 REMOTE_TEMP = 0,
452 REMOTE_LOW,
453 REMOTE_HIGH,
454 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
455 LOCAL_TEMP,
456 REMOTE2_TEMP, /* max6695/96 only */
457 REMOTE2_LOW, /* max6695/96 only */
458 REMOTE2_HIGH, /* max6695/96 only */
459 TEMP11_REG_NUM
460};
461
462/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * Client data (each client gets its own)
464 */
465
466struct lm90_data {
Guenter Roeck1de8b252014-04-04 18:01:35 +0200467 struct i2c_client *client;
Dmitry Osipenko94dbd232021-06-19 00:54:53 +0300468 struct device *hwmon_dev;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700469 u32 channel_config[4];
470 struct hwmon_channel_info temp_info;
471 const struct hwmon_channel_info *info[3];
472 struct hwmon_chip_info chip;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100473 struct mutex update_lock;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700474 bool valid; /* true if register values are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned long last_updated; /* in jiffies */
476 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200477 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Wolfram Sang38bab982016-06-05 09:35:43 +0200479 unsigned int update_interval; /* in milliseconds */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200480
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700481 u8 config; /* Current configuration register value */
Jean Delvare95238362010-03-05 22:17:14 +0100482 u8 config_orig; /* Original configuration register value */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200483 u8 convrate_orig; /* Original conversion rate register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200484 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
485 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200486 u8 max_convrate; /* Maximum conversion rate */
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000487 u8 reg_local_ext; /* local extension register offset */
Jean Delvare95238362010-03-05 22:17:14 +0100488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* registers values */
Wei Ni40465d92013-11-15 10:40:39 +0100490 s8 temp8[TEMP8_REG_NUM];
491 s16 temp11[TEMP11_REG_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200493 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494};
495
496/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200497 * Support functions
498 */
499
500/*
501 * The ADM1032 supports PEC but not on write byte transactions, so we need
502 * to explicitly ask for a transaction without PEC.
503 */
504static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
505{
506 return i2c_smbus_xfer(client->adapter, client->addr,
507 client->flags & ~I2C_CLIENT_PEC,
508 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
509}
510
511/*
512 * It is assumed that client->update_lock is held (unless we are in
513 * detection or initialization steps). This matters when PEC is enabled,
514 * because we don't want the address pointer to change between the write
515 * byte and the read byte transactions.
516 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700517static int lm90_read_reg(struct i2c_client *client, u8 reg)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200518{
519 int err;
520
521 if (client->flags & I2C_CLIENT_PEC) {
522 err = adm1032_write_byte(client, reg);
523 if (err >= 0)
524 err = i2c_smbus_read_byte(client);
525 } else
526 err = i2c_smbus_read_byte_data(client, reg);
527
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700528 return err;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200529}
530
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700531static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200532{
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700533 int oldh, newh, l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200534
535 /*
536 * There is a trick here. We have to read two registers to have the
537 * sensor temperature, but we have to beware a conversion could occur
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300538 * between the readings. The datasheet says we should either use
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200539 * the one-shot conversion register, which we don't want to do
540 * (disables hardware monitoring) or monitor the busy bit, which is
541 * impossible (we can't read the values and monitor that bit at the
542 * exact same time). So the solution used here is to read the high
543 * byte once, then the low byte, then the high byte again. If the new
544 * high byte matches the old one, then we have a valid reading. Else
545 * we have to read the low byte again, and now we believe we have a
546 * correct reading.
547 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700548 oldh = lm90_read_reg(client, regh);
549 if (oldh < 0)
550 return oldh;
551 l = lm90_read_reg(client, regl);
552 if (l < 0)
553 return l;
554 newh = lm90_read_reg(client, regh);
555 if (newh < 0)
556 return newh;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200557 if (oldh != newh) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700558 l = lm90_read_reg(client, regl);
559 if (l < 0)
560 return l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200561 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700562 return (newh << 8) | l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200563}
564
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700565static int lm90_update_confreg(struct lm90_data *data, u8 config)
566{
567 if (data->config != config) {
568 int err;
569
570 err = i2c_smbus_write_byte_data(data->client,
571 LM90_REG_W_CONFIG1,
572 config);
573 if (err)
574 return err;
575 data->config = config;
576 }
577 return 0;
578}
579
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200580/*
581 * client->update_lock must be held when calling this function (unless we are
582 * in detection or initialization steps), and while a remote channel other
583 * than channel 0 is selected. Also, calling code must make sure to re-select
584 * external channel 0 before releasing the lock. This is necessary because
585 * various registers have different meanings as a result of selecting a
586 * non-default remote channel.
587 */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700588static int lm90_select_remote_channel(struct lm90_data *data, int channel)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200589{
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700590 int err = 0;
591
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200592 if (data->kind == max6696) {
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700593 u8 config = data->config & ~0x08;
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700594
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200595 if (channel)
596 config |= 0x08;
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700597 err = lm90_update_confreg(data, config);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200598 }
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700599 return err;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200600}
601
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700602static int lm90_write_convrate(struct lm90_data *data, int val)
Boyang Yu62456182019-06-28 19:06:36 +0000603{
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700604 u8 config = data->config;
Boyang Yu62456182019-06-28 19:06:36 +0000605 int err;
Boyang Yu62456182019-06-28 19:06:36 +0000606
607 /* Save config and pause conversion */
608 if (data->flags & LM90_PAUSE_FOR_CONFIG) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700609 err = lm90_update_confreg(data, config | 0x40);
610 if (err < 0)
611 return err;
Boyang Yu62456182019-06-28 19:06:36 +0000612 }
613
614 /* Set conv rate */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700615 err = i2c_smbus_write_byte_data(data->client, LM90_REG_W_CONVRATE, val);
Boyang Yu62456182019-06-28 19:06:36 +0000616
617 /* Revert change to config */
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700618 lm90_update_confreg(data, config);
Boyang Yu62456182019-06-28 19:06:36 +0000619
620 return err;
621}
622
Guenter Roeck0c01b642010-10-28 20:31:44 +0200623/*
624 * Set conversion rate.
625 * client->update_lock must be held when calling this function (unless we are
626 * in detection or initialization steps).
627 */
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700628static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
629 unsigned int interval)
Guenter Roeck0c01b642010-10-28 20:31:44 +0200630{
Guenter Roeck0c01b642010-10-28 20:31:44 +0200631 unsigned int update_interval;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700632 int i, err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200633
634 /* Shift calculations to avoid rounding errors */
635 interval <<= 6;
636
637 /* find the nearest update rate */
638 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
639 i < data->max_convrate; i++, update_interval >>= 1)
640 if (interval >= update_interval * 3 / 4)
641 break;
642
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700643 err = lm90_write_convrate(data, i);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200644 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700645 return err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200646}
647
Guenter Roeck10bfef42016-06-13 19:26:45 -0700648static int lm90_update_limits(struct device *dev)
649{
650 struct lm90_data *data = dev_get_drvdata(dev);
651 struct i2c_client *client = data->client;
652 int val;
653
654 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
655 if (val < 0)
656 return val;
657 data->temp8[LOCAL_CRIT] = val;
658
659 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
660 if (val < 0)
661 return val;
662 data->temp8[REMOTE_CRIT] = val;
663
664 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
665 if (val < 0)
666 return val;
667 data->temp_hyst = val;
668
Guenter Roeckbe9d6372016-07-25 14:47:18 -0700669 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700670 if (val < 0)
671 return val;
672 data->temp11[REMOTE_LOW] = val << 8;
673
674 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
675 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
676 if (val < 0)
677 return val;
678 data->temp11[REMOTE_LOW] |= val;
679 }
680
681 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
682 if (val < 0)
683 return val;
684 data->temp11[REMOTE_HIGH] = val << 8;
685
686 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
687 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
688 if (val < 0)
689 return val;
690 data->temp11[REMOTE_HIGH] |= val;
691 }
692
693 if (data->flags & LM90_HAVE_OFFSET) {
694 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
695 LM90_REG_R_REMOTE_OFFSL);
696 if (val < 0)
697 return val;
698 data->temp11[REMOTE_OFFSET] = val;
699 }
700
701 if (data->flags & LM90_HAVE_EMERGENCY) {
702 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
703 if (val < 0)
704 return val;
705 data->temp8[LOCAL_EMERG] = val;
706
707 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
708 if (val < 0)
709 return val;
710 data->temp8[REMOTE_EMERG] = val;
711 }
712
713 if (data->kind == max6696) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700714 val = lm90_select_remote_channel(data, 1);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700715 if (val < 0)
716 return val;
717
718 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
719 if (val < 0)
720 return val;
721 data->temp8[REMOTE2_CRIT] = val;
722
723 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
724 if (val < 0)
725 return val;
726 data->temp8[REMOTE2_EMERG] = val;
727
728 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
729 if (val < 0)
730 return val;
731 data->temp11[REMOTE2_LOW] = val << 8;
732
733 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
734 if (val < 0)
735 return val;
736 data->temp11[REMOTE2_HIGH] = val << 8;
737
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700738 lm90_select_remote_channel(data, 0);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700739 }
740
741 return 0;
742}
743
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700744static int lm90_update_device(struct device *dev)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200745{
Guenter Roeck1de8b252014-04-04 18:01:35 +0200746 struct lm90_data *data = dev_get_drvdata(dev);
747 struct i2c_client *client = data->client;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200748 unsigned long next_update;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700749 int val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200750
Guenter Roeck10bfef42016-06-13 19:26:45 -0700751 if (!data->valid) {
752 val = lm90_update_limits(dev);
753 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700754 return val;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700755 }
756
Jean Delvare78c2c2f2013-07-08 14:18:24 +0200757 next_update = data->last_updated +
758 msecs_to_jiffies(data->update_interval);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200759 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200760 dev_dbg(&client->dev, "Updating lm90 data.\n");
Guenter Roeck10bfef42016-06-13 19:26:45 -0700761
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700762 data->valid = false;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700763
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700764 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
765 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700766 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700767 data->temp8[LOCAL_LOW] = val;
768
769 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
770 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700771 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700772 data->temp8[LOCAL_HIGH] = val;
773
Jean Delvarea095f682011-07-27 23:22:25 -0700774 if (data->reg_local_ext) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700775 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
776 data->reg_local_ext);
777 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700778 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700779 data->temp11[LOCAL_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200780 } else {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700781 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
782 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700783 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700784 data->temp11[LOCAL_TEMP] = val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200785 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700786 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
787 LM90_REG_R_REMOTE_TEMPL);
788 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700789 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700790 data->temp11[REMOTE_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200791
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700792 val = lm90_read_reg(client, LM90_REG_R_STATUS);
793 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700794 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700795 data->alarms = val; /* lower 8 bit of alarms */
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200796
797 if (data->kind == max6696) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700798 val = lm90_select_remote_channel(data, 1);
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700799 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700800 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700801
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700802 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
803 LM90_REG_R_REMOTE_TEMPL);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700804 if (val < 0) {
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700805 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700806 return val;
807 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700808 data->temp11[REMOTE2_TEMP] = val;
809
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700810 lm90_select_remote_channel(data, 0);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200811
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700812 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
813 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700814 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700815 data->alarms |= val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200816 }
817
Guenter Roeckf36ffea2012-03-23 10:02:18 +0100818 /*
819 * Re-enable ALERT# output if it was originally enabled and
820 * relevant alarms are all clear
821 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700822 if (!(data->config_orig & 0x80) &&
823 !(data->alarms & data->alert_alarms)) {
Guenter Roeckb849e5d2019-06-30 15:14:19 -0700824 if (data->config & 0x80) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200825 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
Guenter Roeck7a1d2202019-06-30 15:40:34 -0700826 lm90_update_confreg(data, data->config & ~0x80);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200827 }
828 }
829
830 data->last_updated = jiffies;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700831 data->valid = true;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200832 }
833
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700834 return 0;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200835}
836
837/*
Nate Casecea50fe2008-10-17 17:51:10 +0200838 * Conversions
839 * For local temperatures and limits, critical limits and the hysteresis
840 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
841 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200842 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
843 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200844 */
845
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200846static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200847{
848 return val * 1000;
849}
850
Ben Hutchings271dabf2008-10-17 17:51:11 +0200851static inline int temp_from_u8(u8 val)
852{
853 return val * 1000;
854}
855
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200856static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200857{
858 return val / 32 * 125;
859}
860
Ben Hutchings271dabf2008-10-17 17:51:11 +0200861static inline int temp_from_u16(u16 val)
862{
863 return val / 32 * 125;
864}
865
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200866static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200867{
868 if (val <= -128000)
869 return -128;
870 if (val >= 127000)
871 return 127;
872 if (val < 0)
873 return (val - 500) / 1000;
874 return (val + 500) / 1000;
875}
876
Ben Hutchings271dabf2008-10-17 17:51:11 +0200877static u8 temp_to_u8(long val)
878{
879 if (val <= 0)
880 return 0;
881 if (val >= 255000)
882 return 255;
883 return (val + 500) / 1000;
884}
885
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200886static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200887{
888 if (val <= -128000)
889 return 0x8000;
890 if (val >= 127875)
891 return 0x7FE0;
892 if (val < 0)
893 return (val - 62) / 125 * 32;
894 return (val + 62) / 125 * 32;
895}
896
897static u8 hyst_to_reg(long val)
898{
899 if (val <= 0)
900 return 0;
901 if (val >= 30500)
902 return 31;
903 return (val + 500) / 1000;
904}
905
906/*
Nate Case23b2d472008-10-17 17:51:10 +0200907 * ADT7461 in compatibility mode is almost identical to LM90 except that
908 * attempts to write values that are outside the range 0 < temp < 127 are
909 * treated as the boundary value.
910 *
911 * ADT7461 in "extended mode" operation uses unsigned integers offset by
912 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200913 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200914static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200915{
Nate Case23b2d472008-10-17 17:51:10 +0200916 if (data->flags & LM90_FLAG_ADT7461_EXT)
917 return (val - 64) * 1000;
Guenter Roeck589f7072016-06-18 19:58:26 -0700918 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200919}
920
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200921static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200922{
Nate Case23b2d472008-10-17 17:51:10 +0200923 if (data->flags & LM90_FLAG_ADT7461_EXT)
924 return (val - 0x4000) / 64 * 250;
Guenter Roeck589f7072016-06-18 19:58:26 -0700925 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200926}
927
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200928static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200929{
930 if (data->flags & LM90_FLAG_ADT7461_EXT) {
931 if (val <= -64000)
932 return 0;
933 if (val >= 191000)
934 return 0xFF;
935 return (val + 500 + 64000) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200936 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700937 if (val <= 0)
938 return 0;
939 if (val >= 127000)
940 return 127;
941 return (val + 500) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200942}
943
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200944static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200945{
946 if (data->flags & LM90_FLAG_ADT7461_EXT) {
947 if (val <= -64000)
948 return 0;
949 if (val >= 191750)
950 return 0xFFC0;
951 return (val + 64000 + 125) / 250 * 64;
Nate Case23b2d472008-10-17 17:51:10 +0200952 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700953 if (val <= 0)
954 return 0;
955 if (val >= 127750)
956 return 0x7FC0;
957 return (val + 125) / 250 * 64;
Nate Casecea50fe2008-10-17 17:51:10 +0200958}
959
Jean Delvarec3df5802005-10-26 21:39:40 +0200960/* pec used for ADM1032 only */
Julia Lawalle57959a2016-12-22 13:05:22 +0100961static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
Jean Delvarec3df5802005-10-26 21:39:40 +0200962 char *buf)
963{
964 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700965
Jean Delvarec3df5802005-10-26 21:39:40 +0200966 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
967}
968
Julia Lawalle57959a2016-12-22 13:05:22 +0100969static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
970 const char *buf, size_t count)
Jean Delvarec3df5802005-10-26 21:39:40 +0200971{
972 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200973 long val;
974 int err;
975
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100976 err = kstrtol(buf, 10, &val);
Guenter Roeck11e57812010-10-28 20:31:42 +0200977 if (err < 0)
978 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200979
980 switch (val) {
981 case 0:
982 client->flags &= ~I2C_CLIENT_PEC;
983 break;
984 case 1:
985 client->flags |= I2C_CLIENT_PEC;
986 break;
987 default:
988 return -EINVAL;
989 }
990
991 return count;
992}
993
Julia Lawalle57959a2016-12-22 13:05:22 +0100994static DEVICE_ATTR_RW(pec);
Jean Delvarec3df5802005-10-26 21:39:40 +0200995
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700996static int lm90_get_temp11(struct lm90_data *data, int index)
997{
998 s16 temp11 = data->temp11[index];
999 int temp;
1000
1001 if (data->kind == adt7461 || data->kind == tmp451)
1002 temp = temp_from_u16_adt7461(data, temp11);
1003 else if (data->kind == max6646)
1004 temp = temp_from_u16(temp11);
1005 else
1006 temp = temp_from_s16(temp11);
1007
1008 /* +16 degrees offset for temp2 for the LM99 */
1009 if (data->kind == lm99 && index <= 2)
1010 temp += 16000;
1011
1012 return temp;
1013}
1014
1015static int lm90_set_temp11(struct lm90_data *data, int index, long val)
1016{
1017 static struct reg {
1018 u8 high;
1019 u8 low;
1020 } reg[] = {
1021 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1022 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
1023 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
1024 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
1025 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
1026 };
1027 struct i2c_client *client = data->client;
1028 struct reg *regp = &reg[index];
1029 int err;
1030
1031 /* +16 degrees offset for temp2 for the LM99 */
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001032 if (data->kind == lm99 && index <= 2) {
1033 /* prevent integer underflow */
1034 val = max(val, -128000l);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001035 val -= 16000;
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001036 }
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001037
1038 if (data->kind == adt7461 || data->kind == tmp451)
1039 data->temp11[index] = temp_to_u16_adt7461(data, val);
1040 else if (data->kind == max6646)
1041 data->temp11[index] = temp_to_u8(val) << 8;
1042 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1043 data->temp11[index] = temp_to_s16(val);
1044 else
1045 data->temp11[index] = temp_to_s8(val) << 8;
1046
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001047 lm90_select_remote_channel(data, index >= 3);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001048 err = i2c_smbus_write_byte_data(client, regp->high,
1049 data->temp11[index] >> 8);
1050 if (err < 0)
1051 return err;
1052 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1053 err = i2c_smbus_write_byte_data(client, regp->low,
1054 data->temp11[index] & 0xff);
1055
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001056 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001057 return err;
1058}
1059
1060static int lm90_get_temp8(struct lm90_data *data, int index)
1061{
1062 s8 temp8 = data->temp8[index];
1063 int temp;
1064
1065 if (data->kind == adt7461 || data->kind == tmp451)
1066 temp = temp_from_u8_adt7461(data, temp8);
1067 else if (data->kind == max6646)
1068 temp = temp_from_u8(temp8);
1069 else
1070 temp = temp_from_s8(temp8);
1071
1072 /* +16 degrees offset for temp2 for the LM99 */
1073 if (data->kind == lm99 && index == 3)
1074 temp += 16000;
1075
1076 return temp;
1077}
1078
1079static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1080{
1081 static const u8 reg[TEMP8_REG_NUM] = {
1082 LM90_REG_W_LOCAL_LOW,
1083 LM90_REG_W_LOCAL_HIGH,
1084 LM90_REG_W_LOCAL_CRIT,
1085 LM90_REG_W_REMOTE_CRIT,
1086 MAX6659_REG_W_LOCAL_EMERG,
1087 MAX6659_REG_W_REMOTE_EMERG,
1088 LM90_REG_W_REMOTE_CRIT,
1089 MAX6659_REG_W_REMOTE_EMERG,
1090 };
1091 struct i2c_client *client = data->client;
1092 int err;
1093
1094 /* +16 degrees offset for temp2 for the LM99 */
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001095 if (data->kind == lm99 && index == 3) {
1096 /* prevent integer underflow */
1097 val = max(val, -128000l);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001098 val -= 16000;
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001099 }
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001100
1101 if (data->kind == adt7461 || data->kind == tmp451)
1102 data->temp8[index] = temp_to_u8_adt7461(data, val);
1103 else if (data->kind == max6646)
1104 data->temp8[index] = temp_to_u8(val);
1105 else
1106 data->temp8[index] = temp_to_s8(val);
1107
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001108 lm90_select_remote_channel(data, index >= 6);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001109 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001110 lm90_select_remote_channel(data, 0);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001111
1112 return err;
1113}
1114
1115static int lm90_get_temphyst(struct lm90_data *data, int index)
1116{
1117 int temp;
1118
1119 if (data->kind == adt7461 || data->kind == tmp451)
1120 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1121 else if (data->kind == max6646)
1122 temp = temp_from_u8(data->temp8[index]);
1123 else
1124 temp = temp_from_s8(data->temp8[index]);
1125
1126 /* +16 degrees offset for temp2 for the LM99 */
1127 if (data->kind == lm99 && index == 3)
1128 temp += 16000;
1129
1130 return temp - temp_from_s8(data->temp_hyst);
1131}
1132
1133static int lm90_set_temphyst(struct lm90_data *data, long val)
1134{
1135 struct i2c_client *client = data->client;
1136 int temp;
1137 int err;
1138
1139 if (data->kind == adt7461 || data->kind == tmp451)
1140 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1141 else if (data->kind == max6646)
1142 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1143 else
1144 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1145
Dmitry Osipenkob50aa492021-06-23 07:22:30 +03001146 /* prevent integer underflow */
1147 val = max(val, -128000l);
1148
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001149 data->temp_hyst = hyst_to_reg(temp - val);
1150 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1151 data->temp_hyst);
1152 return err;
1153}
1154
1155static const u8 lm90_temp_index[3] = {
1156 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1157};
1158
1159static const u8 lm90_temp_min_index[3] = {
1160 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1161};
1162
1163static const u8 lm90_temp_max_index[3] = {
1164 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1165};
1166
1167static const u8 lm90_temp_crit_index[3] = {
1168 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1169};
1170
1171static const u8 lm90_temp_emerg_index[3] = {
1172 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1173};
1174
1175static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
Michael Wallee9572fd2017-01-02 17:53:39 +01001176static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001177static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1178static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1179static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1180
1181static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1182{
1183 struct lm90_data *data = dev_get_drvdata(dev);
1184 int err;
1185
1186 mutex_lock(&data->update_lock);
1187 err = lm90_update_device(dev);
1188 mutex_unlock(&data->update_lock);
1189 if (err)
1190 return err;
1191
1192 switch (attr) {
1193 case hwmon_temp_input:
1194 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1195 break;
1196 case hwmon_temp_min_alarm:
1197 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1198 break;
1199 case hwmon_temp_max_alarm:
1200 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1201 break;
1202 case hwmon_temp_crit_alarm:
1203 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1204 break;
1205 case hwmon_temp_emergency_alarm:
1206 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1207 break;
1208 case hwmon_temp_fault:
1209 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1210 break;
1211 case hwmon_temp_min:
1212 if (channel == 0)
1213 *val = lm90_get_temp8(data,
1214 lm90_temp_min_index[channel]);
1215 else
1216 *val = lm90_get_temp11(data,
1217 lm90_temp_min_index[channel]);
1218 break;
1219 case hwmon_temp_max:
1220 if (channel == 0)
1221 *val = lm90_get_temp8(data,
1222 lm90_temp_max_index[channel]);
1223 else
1224 *val = lm90_get_temp11(data,
1225 lm90_temp_max_index[channel]);
1226 break;
1227 case hwmon_temp_crit:
1228 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1229 break;
1230 case hwmon_temp_crit_hyst:
1231 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1232 break;
1233 case hwmon_temp_emergency:
1234 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1235 break;
1236 case hwmon_temp_emergency_hyst:
1237 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1238 break;
1239 case hwmon_temp_offset:
1240 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1241 break;
1242 default:
1243 return -EOPNOTSUPP;
1244 }
1245 return 0;
1246}
1247
1248static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1249{
1250 struct lm90_data *data = dev_get_drvdata(dev);
1251 int err;
1252
1253 mutex_lock(&data->update_lock);
1254
1255 err = lm90_update_device(dev);
1256 if (err)
1257 goto error;
1258
1259 switch (attr) {
1260 case hwmon_temp_min:
1261 if (channel == 0)
1262 err = lm90_set_temp8(data,
1263 lm90_temp_min_index[channel],
1264 val);
1265 else
1266 err = lm90_set_temp11(data,
1267 lm90_temp_min_index[channel],
1268 val);
1269 break;
1270 case hwmon_temp_max:
1271 if (channel == 0)
1272 err = lm90_set_temp8(data,
1273 lm90_temp_max_index[channel],
1274 val);
1275 else
1276 err = lm90_set_temp11(data,
1277 lm90_temp_max_index[channel],
1278 val);
1279 break;
1280 case hwmon_temp_crit:
1281 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1282 break;
1283 case hwmon_temp_crit_hyst:
1284 err = lm90_set_temphyst(data, val);
1285 break;
1286 case hwmon_temp_emergency:
1287 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1288 break;
1289 case hwmon_temp_offset:
1290 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1291 break;
1292 default:
1293 err = -EOPNOTSUPP;
1294 break;
1295 }
1296error:
1297 mutex_unlock(&data->update_lock);
1298
1299 return err;
1300}
1301
1302static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1303{
1304 switch (attr) {
1305 case hwmon_temp_input:
1306 case hwmon_temp_min_alarm:
1307 case hwmon_temp_max_alarm:
1308 case hwmon_temp_crit_alarm:
1309 case hwmon_temp_emergency_alarm:
1310 case hwmon_temp_emergency_hyst:
1311 case hwmon_temp_fault:
Guenter Roeck33348512018-12-10 14:02:12 -08001312 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001313 case hwmon_temp_min:
1314 case hwmon_temp_max:
1315 case hwmon_temp_crit:
1316 case hwmon_temp_emergency:
1317 case hwmon_temp_offset:
Guenter Roeck33348512018-12-10 14:02:12 -08001318 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001319 case hwmon_temp_crit_hyst:
1320 if (channel == 0)
Guenter Roeck33348512018-12-10 14:02:12 -08001321 return 0644;
1322 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001323 default:
1324 return 0;
1325 }
1326}
1327
1328static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1329{
1330 struct lm90_data *data = dev_get_drvdata(dev);
1331 int err;
1332
1333 mutex_lock(&data->update_lock);
1334 err = lm90_update_device(dev);
1335 mutex_unlock(&data->update_lock);
1336 if (err)
1337 return err;
1338
1339 switch (attr) {
1340 case hwmon_chip_update_interval:
1341 *val = data->update_interval;
1342 break;
1343 case hwmon_chip_alarms:
1344 *val = data->alarms;
1345 break;
1346 default:
1347 return -EOPNOTSUPP;
1348 }
1349
1350 return 0;
1351}
1352
1353static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1354{
1355 struct lm90_data *data = dev_get_drvdata(dev);
1356 struct i2c_client *client = data->client;
1357 int err;
1358
1359 mutex_lock(&data->update_lock);
1360
1361 err = lm90_update_device(dev);
1362 if (err)
1363 goto error;
1364
1365 switch (attr) {
1366 case hwmon_chip_update_interval:
1367 err = lm90_set_convrate(client, data,
1368 clamp_val(val, 0, 100000));
1369 break;
1370 default:
1371 err = -EOPNOTSUPP;
1372 break;
1373 }
1374error:
1375 mutex_unlock(&data->update_lock);
1376
1377 return err;
1378}
1379
1380static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1381{
1382 switch (attr) {
1383 case hwmon_chip_update_interval:
Guenter Roeck33348512018-12-10 14:02:12 -08001384 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001385 case hwmon_chip_alarms:
Guenter Roeck33348512018-12-10 14:02:12 -08001386 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001387 default:
1388 return 0;
1389 }
1390}
1391
1392static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1393 u32 attr, int channel, long *val)
1394{
1395 switch (type) {
1396 case hwmon_chip:
1397 return lm90_chip_read(dev, attr, channel, val);
1398 case hwmon_temp:
1399 return lm90_temp_read(dev, attr, channel, val);
1400 default:
1401 return -EOPNOTSUPP;
1402 }
1403}
1404
1405static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1406 u32 attr, int channel, long val)
1407{
1408 switch (type) {
1409 case hwmon_chip:
1410 return lm90_chip_write(dev, attr, channel, val);
1411 case hwmon_temp:
1412 return lm90_temp_write(dev, attr, channel, val);
1413 default:
1414 return -EOPNOTSUPP;
1415 }
1416}
1417
1418static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1419 u32 attr, int channel)
1420{
1421 switch (type) {
1422 case hwmon_chip:
1423 return lm90_chip_is_visible(data, attr, channel);
1424 case hwmon_temp:
1425 return lm90_temp_is_visible(data, attr, channel);
1426 default:
1427 return 0;
1428 }
1429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jean Delvare9b0e8522008-07-16 19:30:15 +02001431/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001432static int lm90_detect(struct i2c_client *client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001433 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001435 struct i2c_adapter *adapter = client->adapter;
1436 int address = client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001437 const char *name = NULL;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001438 int man_id, chip_id, config1, config2, convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001441 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Jean Delvare8f2fa772009-12-09 20:35:53 +01001443 /* detection and identification */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001444 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1445 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1446 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1447 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1448 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001449 return -ENODEV;
1450
Jean Delvaref90be422011-07-24 20:37:05 +02001451 if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
Jean Delvareb2589ab2011-11-04 12:00:47 +01001452 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1453 if (config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001454 return -ENODEV;
Jean Delvaref90be422011-07-24 20:37:05 +02001455 } else
Jean Delvareb2589ab2011-11-04 12:00:47 +01001456 config2 = 0; /* Make compiler happy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Jean Delvaref90be422011-07-24 20:37:05 +02001458 if ((address == 0x4C || address == 0x4D)
1459 && man_id == 0x01) { /* National Semiconductor */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001460 if ((config1 & 0x2A) == 0x00
1461 && (config2 & 0xF8) == 0x00
1462 && convrate <= 0x09) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001463 if (address == 0x4C
1464 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1465 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001467 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1468 name = "lm99";
1469 dev_info(&adapter->dev,
1470 "Assuming LM99 chip at 0x%02x\n",
1471 address);
1472 dev_info(&adapter->dev,
1473 "If it is an LM89, instantiate it "
1474 "with the new_device sysfs "
1475 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001476 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001477 if (address == 0x4C
1478 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1479 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001482 } else
1483 if ((address == 0x4C || address == 0x4D)
1484 && man_id == 0x41) { /* Analog Devices */
1485 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001486 && (config1 & 0x3F) == 0x00
1487 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001488 name = "adm1032";
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001489 /*
1490 * The ADM1032 supports PEC, but only if combined
1491 * transactions are not used.
1492 */
Jean Delvare8f2fa772009-12-09 20:35:53 +01001493 if (i2c_check_functionality(adapter,
1494 I2C_FUNC_SMBUS_BYTE))
1495 info->flags |= I2C_CLIENT_PEC;
1496 } else
1497 if (chip_id == 0x51 /* ADT7461 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001498 && (config1 & 0x1B) == 0x00
1499 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001500 name = "adt7461";
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001501 } else
1502 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001503 && (config1 & 0x1B) == 0x00
1504 && convrate <= 0x0A) {
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001505 name = "adt7461a";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001506 }
1507 } else
1508 if (man_id == 0x4D) { /* Maxim */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001509 int emerg, emerg2, status2;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001510
1511 /*
1512 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1513 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1514 * exists, both readings will reflect the same value. Otherwise,
1515 * the readings will be different.
1516 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001517 emerg = i2c_smbus_read_byte_data(client,
1518 MAX6659_REG_R_REMOTE_EMERG);
1519 man_id = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001520 LM90_REG_R_MAN_ID);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001521 emerg2 = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001522 MAX6659_REG_R_REMOTE_EMERG);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001523 status2 = i2c_smbus_read_byte_data(client,
1524 MAX6696_REG_R_STATUS2);
1525 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001526 return -ENODEV;
1527
Jean Delvare8f2fa772009-12-09 20:35:53 +01001528 /*
1529 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1530 * register. Reading from that address will return the last
1531 * read value, which in our case is those of the man_id
1532 * register. Likewise, the config1 register seems to lack a
1533 * low nibble, so the value will be those of the previous
1534 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001535 * MAX6659 has a third set of upper temperature limit registers.
1536 * Those registers also return values on MAX6657 and MAX6658,
1537 * thus the only way to detect MAX6659 is by its address.
1538 * For this reason it will be mis-detected as MAX6657 if its
1539 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001540 */
1541 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001542 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvareb2589ab2011-11-04 12:00:47 +01001543 && (config1 & 0x1F) == (man_id & 0x0F)
1544 && convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001545 if (address == 0x4C)
1546 name = "max6657";
1547 else
1548 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001549 } else
1550 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001551 * Even though MAX6695 and MAX6696 do not have a chip ID
1552 * register, reading it returns 0x01. Bit 4 of the config1
1553 * register is unused and should return zero when read. Bit 0 of
1554 * the status2 register is unused and should return zero when
1555 * read.
1556 *
1557 * MAX6695 and MAX6696 have an additional set of temperature
1558 * limit registers. We can detect those chips by checking if
1559 * one of those registers exists.
1560 */
1561 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001562 && (config1 & 0x10) == 0x00
1563 && (status2 & 0x01) == 0x00
1564 && emerg == emerg2
1565 && convrate <= 0x07) {
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001566 name = "max6696";
1567 } else
1568 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001569 * The chip_id register of the MAX6680 and MAX6681 holds the
1570 * revision of the chip. The lowest bit of the config1 register
1571 * is unused and should return zero when read, so should the
1572 * second to last bit of config1 (software reset).
1573 */
1574 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001575 && (config1 & 0x03) == 0x00
1576 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001577 name = "max6680";
1578 } else
1579 /*
1580 * The chip_id register of the MAX6646/6647/6649 holds the
1581 * revision of the chip. The lowest 6 bits of the config1
1582 * register are unused and should return zero when read.
1583 */
1584 if (chip_id == 0x59
Jean Delvareb2589ab2011-11-04 12:00:47 +01001585 && (config1 & 0x3f) == 0x00
1586 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001587 name = "max6646";
Josh Lehan229d4952020-05-13 11:42:48 -07001588 } else
1589 /*
1590 * The chip_id of the MAX6654 holds the revision of the chip.
1591 * The lowest 3 bits of the config1 register are unused and
1592 * should return zero when read.
1593 */
1594 if (chip_id == 0x08
1595 && (config1 & 0x07) == 0x00
1596 && convrate <= 0x07) {
1597 name = "max6654";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001599 } else
1600 if (address == 0x4C
1601 && man_id == 0x5C) { /* Winbond/Nuvoton */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001602 if ((config1 & 0x2A) == 0x00
1603 && (config2 & 0xF8) == 0x00) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001604 if (chip_id == 0x01 /* W83L771W/G */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001605 && convrate <= 0x09) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001606 name = "w83l771";
1607 } else
1608 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001609 && convrate <= 0x08) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001610 name = "w83l771";
1611 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001612 }
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001613 } else
Jean Delvare6d101c52011-07-24 20:36:15 +02001614 if (address >= 0x48 && address <= 0x4F
1615 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
Jean Delvare6d101c52011-07-24 20:36:15 +02001616 if (chip_id == 0x00
Jean Delvareb2589ab2011-11-04 12:00:47 +01001617 && (config1 & 0x2A) == 0x00
1618 && (config2 & 0xFE) == 0x00
1619 && convrate <= 0x09) {
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001620 name = "sa56004";
1621 }
Guenter Roeckae544f62012-03-23 10:02:18 +01001622 } else
1623 if ((address == 0x4C || address == 0x4D)
1624 && man_id == 0x47) { /* GMT */
1625 if (chip_id == 0x01 /* G781 */
1626 && (config1 & 0x3F) == 0x00
1627 && convrate <= 0x08)
1628 name = "g781";
Wei Ni1daaceb2013-11-15 10:40:39 +01001629 } else
1630 if (address == 0x4C
1631 && man_id == 0x55) { /* Texas Instruments */
1632 int local_ext;
1633
1634 local_ext = i2c_smbus_read_byte_data(client,
1635 TMP451_REG_R_LOCAL_TEMPL);
1636
1637 if (chip_id == 0x00 /* TMP451 */
1638 && (config1 & 0x1B) == 0x00
1639 && convrate <= 0x09
1640 && (local_ext & 0x0F) == 0x00)
1641 name = "tmp451";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
Jean Delvare8f2fa772009-12-09 20:35:53 +01001644 if (!name) { /* identification failed */
1645 dev_dbg(&adapter->dev,
1646 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1647 "chip_id=0x%02X)\n", address, man_id, chip_id);
1648 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001650
Jean Delvare9b0e8522008-07-16 19:30:15 +02001651 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jean Delvare9b0e8522008-07-16 19:30:15 +02001653 return 0;
1654}
1655
Guenter Roeck1f17a442016-06-13 06:19:11 -07001656static void lm90_restore_conf(void *_data)
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001657{
Guenter Roeck1f17a442016-06-13 06:19:11 -07001658 struct lm90_data *data = _data;
1659 struct i2c_client *client = data->client;
1660
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001661 /* Restore initial configuration */
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001662 lm90_write_convrate(data, data->convrate_orig);
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001663 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1664 data->config_orig);
1665}
1666
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001667static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001668{
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001669 int config, convrate;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001670
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001671 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1672 if (convrate < 0)
1673 return convrate;
Guenter Roeck0c01b642010-10-28 20:31:44 +02001674 data->convrate_orig = convrate;
1675
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001676 /*
1677 * Start the conversions.
1678 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001679 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1680 if (config < 0)
1681 return config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001682 data->config_orig = config;
Guenter Roeckb849e5d2019-06-30 15:14:19 -07001683 data->config = config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001684
Boyang Yu62456182019-06-28 19:06:36 +00001685 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
1686
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001687 /* Check Temperature Range Select */
Wei Ni1daaceb2013-11-15 10:40:39 +01001688 if (data->kind == adt7461 || data->kind == tmp451) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001689 if (config & 0x04)
1690 data->flags |= LM90_FLAG_ADT7461_EXT;
1691 }
1692
1693 /*
1694 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1695 * 0.125 degree resolution) and range (0x08, extend range
1696 * to -64 degree) mode for the remote temperature sensor.
1697 */
1698 if (data->kind == max6680)
1699 config |= 0x18;
1700
1701 /*
Josh Lehan229d4952020-05-13 11:42:48 -07001702 * Put MAX6654 into extended range (0x20, extend minimum range from
1703 * 0 degrees to -64 degrees). Note that extended resolution is not
1704 * possible on the MAX6654 unless conversion rate is set to 1 Hz or
1705 * slower, which is intentionally not done by default.
1706 */
1707 if (data->kind == max6654)
1708 config |= 0x20;
1709
1710 /*
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001711 * Select external channel 0 for max6695/96
1712 */
1713 if (data->kind == max6696)
1714 config &= ~0x08;
1715
Dmitry Osipenko2abdc352021-06-19 00:54:54 +03001716 /*
1717 * Interrupt is enabled by default on reset, but it may be disabled
1718 * by bootloader, unmask it.
1719 */
1720 if (client->irq)
1721 config &= ~0x80;
1722
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001723 config &= 0xBF; /* run */
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001724 lm90_update_confreg(data, config);
Guenter Roeck1f17a442016-06-13 06:19:11 -07001725
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001726 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001727}
1728
Wei Ni072de492013-11-15 10:40:38 +01001729static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1730{
1731 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001732 int st, st2 = 0;
Wei Ni072de492013-11-15 10:40:38 +01001733
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001734 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1735 if (st < 0)
1736 return false;
Wei Ni072de492013-11-15 10:40:38 +01001737
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001738 if (data->kind == max6696) {
1739 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1740 if (st2 < 0)
1741 return false;
1742 }
Wei Ni072de492013-11-15 10:40:38 +01001743
1744 *status = st | (st2 << 8);
1745
1746 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1747 return false;
1748
1749 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1750 (st2 & MAX6696_STATUS2_LOT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001751 dev_dbg(&client->dev,
1752 "temp%d out of range, please check!\n", 1);
Wei Ni072de492013-11-15 10:40:38 +01001753 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1754 (st2 & MAX6696_STATUS2_ROT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001755 dev_dbg(&client->dev,
1756 "temp%d out of range, please check!\n", 2);
Wei Ni072de492013-11-15 10:40:38 +01001757 if (st & LM90_STATUS_ROPEN)
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001758 dev_dbg(&client->dev,
1759 "temp%d diode open, please check!\n", 2);
Wei Ni072de492013-11-15 10:40:38 +01001760 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1761 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001762 dev_dbg(&client->dev,
1763 "temp%d out of range, please check!\n", 3);
Wei Ni072de492013-11-15 10:40:38 +01001764 if (st2 & MAX6696_STATUS2_R2OPEN)
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001765 dev_dbg(&client->dev,
1766 "temp%d diode open, please check!\n", 3);
1767
1768 if (st & LM90_STATUS_LLOW)
1769 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1770 hwmon_temp_min, 0);
1771 if (st & LM90_STATUS_RLOW)
1772 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1773 hwmon_temp_min, 1);
1774 if (st2 & MAX6696_STATUS2_R2LOW)
1775 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1776 hwmon_temp_min, 2);
1777 if (st & LM90_STATUS_LHIGH)
1778 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1779 hwmon_temp_max, 0);
1780 if (st & LM90_STATUS_RHIGH)
1781 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1782 hwmon_temp_max, 1);
1783 if (st2 & MAX6696_STATUS2_R2HIGH)
1784 hwmon_notify_event(data->hwmon_dev, hwmon_temp,
1785 hwmon_temp_max, 2);
Wei Ni072de492013-11-15 10:40:38 +01001786
1787 return true;
1788}
1789
Wei Ni109b1282013-11-15 10:40:39 +01001790static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1791{
1792 struct i2c_client *client = dev_id;
1793 u16 status;
1794
1795 if (lm90_is_tripped(client, &status))
1796 return IRQ_HANDLED;
1797 else
1798 return IRQ_NONE;
1799}
1800
Guenter Roeck1f17a442016-06-13 06:19:11 -07001801static void lm90_remove_pec(void *dev)
1802{
1803 device_remove_file(dev, &dev_attr_pec);
1804}
1805
1806static void lm90_regulator_disable(void *regulator)
1807{
1808 regulator_disable(regulator);
1809}
1810
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001811
1812static const struct hwmon_ops lm90_ops = {
1813 .is_visible = lm90_is_visible,
1814 .read = lm90_read,
1815 .write = lm90_write,
1816};
1817
Stephen Kitt67487032020-08-13 18:02:22 +02001818static int lm90_probe(struct i2c_client *client)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001819{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001820 struct device *dev = &client->dev;
Wolfram Sange67776c2019-06-10 11:51:54 +02001821 struct i2c_adapter *adapter = client->adapter;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001822 struct hwmon_channel_info *info;
Wei Ni3e0f9642013-11-15 10:40:39 +01001823 struct regulator *regulator;
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001824 struct device *hwmon_dev;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001825 struct lm90_data *data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001826 int err;
1827
Wei Ni3e0f9642013-11-15 10:40:39 +01001828 regulator = devm_regulator_get(dev, "vcc");
1829 if (IS_ERR(regulator))
1830 return PTR_ERR(regulator);
1831
1832 err = regulator_enable(regulator);
1833 if (err < 0) {
Guenter Roeckd89fa682014-04-04 18:01:35 +02001834 dev_err(dev, "Failed to enable regulator: %d\n", err);
Wei Ni3e0f9642013-11-15 10:40:39 +01001835 return err;
1836 }
1837
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001838 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1839 if (err)
1840 return err;
Guenter Roeck1f17a442016-06-13 06:19:11 -07001841
Guenter Roeckd89fa682014-04-04 18:01:35 +02001842 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
Guenter Roeck20f426f2012-06-02 09:58:10 -07001843 if (!data)
1844 return -ENOMEM;
1845
Guenter Roeck1de8b252014-04-04 18:01:35 +02001846 data->client = client;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001847 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001848 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jean Delvare9b0e8522008-07-16 19:30:15 +02001850 /* Set the device type */
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03001851 if (client->dev.of_node)
1852 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1853 else
Stephen Kitt67487032020-08-13 18:02:22 +02001854 data->kind = i2c_match_id(lm90_id, client)->driver_data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001855 if (data->kind == adm1032) {
1856 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
Jean Delvareb2589ab2011-11-04 12:00:47 +01001857 client->flags &= ~I2C_CLIENT_PEC;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001860 /*
1861 * Different devices have different alarm bits triggering the
1862 * ALERT# output
1863 */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001864 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001865
Guenter Roeck88073bb2010-10-28 20:31:43 +02001866 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001867 data->flags = lm90_params[data->kind].flags;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001868
1869 data->chip.ops = &lm90_ops;
1870 data->chip.info = data->info;
1871
Guenter Roecka4d41e62019-03-31 10:53:48 -07001872 data->info[0] = HWMON_CHANNEL_INFO(chip,
1873 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001874 data->info[1] = &data->temp_info;
1875
1876 info = &data->temp_info;
1877 info->type = hwmon_temp;
1878 info->config = data->channel_config;
1879
1880 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1881 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1882 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1883 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1884 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1885 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1886
1887 if (data->flags & LM90_HAVE_OFFSET)
1888 data->channel_config[1] |= HWMON_T_OFFSET;
1889
1890 if (data->flags & LM90_HAVE_EMERGENCY) {
1891 data->channel_config[0] |= HWMON_T_EMERGENCY |
1892 HWMON_T_EMERGENCY_HYST;
1893 data->channel_config[1] |= HWMON_T_EMERGENCY |
1894 HWMON_T_EMERGENCY_HYST;
1895 }
1896
1897 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1898 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1899 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1900 }
1901
1902 if (data->flags & LM90_HAVE_TEMP3) {
1903 data->channel_config[2] = HWMON_T_INPUT |
1904 HWMON_T_MIN | HWMON_T_MAX |
1905 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1906 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1907 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1908 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1909 HWMON_T_FAULT;
1910 }
1911
Jean Delvarea095f682011-07-27 23:22:25 -07001912 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001913
Guenter Roeck0c01b642010-10-28 20:31:44 +02001914 /* Set maximum conversion rate */
1915 data->max_convrate = lm90_params[data->kind].max_convrate;
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* Initialize the LM90 chip */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001918 err = lm90_init_client(client, data);
1919 if (err < 0) {
1920 dev_err(dev, "Failed to initialize device\n");
1921 return err;
1922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001924 /*
1925 * The 'pec' attribute is attached to the i2c device and thus created
1926 * separately.
1927 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001928 if (client->flags & I2C_CLIENT_PEC) {
1929 err = device_create_file(dev, &dev_attr_pec);
Guenter Roeck11e57812010-10-28 20:31:42 +02001930 if (err)
Guenter Roeck1f17a442016-06-13 06:19:11 -07001931 return err;
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001932 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1933 if (err)
1934 return err;
Jean Delvare0e39e012006-09-24 21:16:40 +02001935 }
Guenter Roeck084489e2014-04-04 18:01:35 +02001936
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001937 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1938 data, &data->chip,
1939 NULL);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001940 if (IS_ERR(hwmon_dev))
1941 return PTR_ERR(hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001942
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001943 data->hwmon_dev = hwmon_dev;
1944
Wei Ni109b1282013-11-15 10:40:39 +01001945 if (client->irq) {
1946 dev_dbg(dev, "IRQ: %d\n", client->irq);
1947 err = devm_request_threaded_irq(dev, client->irq,
1948 NULL, lm90_irq_thread,
Dmitry Osipenkod97fb832021-06-19 00:54:52 +03001949 IRQF_ONESHOT, "lm90", client);
Wei Ni109b1282013-11-15 10:40:39 +01001950 if (err < 0) {
1951 dev_err(dev, "cannot request IRQ %d\n", client->irq);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001952 return err;
Wei Ni109b1282013-11-15 10:40:39 +01001953 }
1954 }
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001959static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1960 unsigned int flag)
Jean Delvare53de3342010-03-05 22:17:15 +01001961{
Wei Ni072de492013-11-15 10:40:38 +01001962 u16 alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001963
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001964 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1965 return;
1966
Wei Ni072de492013-11-15 10:40:38 +01001967 if (lm90_is_tripped(client, &alarms)) {
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001968 /*
1969 * Disable ALERT# output, because these chips don't implement
1970 * SMBus alert correctly; they should only hold the alert line
1971 * low briefly.
1972 */
Wei Ni072de492013-11-15 10:40:38 +01001973 struct lm90_data *data = i2c_get_clientdata(client);
1974
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001975 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1976 (alarms & data->alert_alarms)) {
Jean Delvare53de3342010-03-05 22:17:15 +01001977 dev_dbg(&client->dev, "Disabling ALERT#\n");
Guenter Roeck7a1d2202019-06-30 15:40:34 -07001978 lm90_update_confreg(data, data->config | 0x80);
Jean Delvare53de3342010-03-05 22:17:15 +01001979 }
Wei Ni072de492013-11-15 10:40:38 +01001980 } else {
Dmitry Osipenko94dbd232021-06-19 00:54:53 +03001981 dev_dbg(&client->dev, "Everything OK\n");
Jean Delvare53de3342010-03-05 22:17:15 +01001982 }
1983}
1984
Dmitry Osipenko4c7f85a2021-06-19 00:54:55 +03001985static int __maybe_unused lm90_suspend(struct device *dev)
1986{
1987 struct lm90_data *data = dev_get_drvdata(dev);
1988 struct i2c_client *client = data->client;
1989
1990 if (client->irq)
1991 disable_irq(client->irq);
1992
1993 return 0;
1994}
1995
1996static int __maybe_unused lm90_resume(struct device *dev)
1997{
1998 struct lm90_data *data = dev_get_drvdata(dev);
1999 struct i2c_client *client = data->client;
2000
2001 if (client->irq)
2002 enable_irq(client->irq);
2003
2004 return 0;
2005}
2006
2007static SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
2008
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002009static struct i2c_driver lm90_driver = {
2010 .class = I2C_CLASS_HWMON,
2011 .driver = {
2012 .name = "lm90",
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03002013 .of_match_table = of_match_ptr(lm90_of_match),
Dmitry Osipenko4c7f85a2021-06-19 00:54:55 +03002014 .pm = &lm90_pm_ops,
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002015 },
Stephen Kitt67487032020-08-13 18:02:22 +02002016 .probe_new = lm90_probe,
Guenter Roeck15b66ab2010-10-28 20:31:43 +02002017 .alert = lm90_alert,
2018 .id_table = lm90_id,
2019 .detect = lm90_detect,
2020 .address_list = normal_i2c,
2021};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Axel Linf0967ee2012-01-20 15:38:18 +08002023module_i2c_driver(lm90_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Jean Delvare7c81c60f2014-01-29 20:40:08 +01002025MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026MODULE_DESCRIPTION("LM90/ADM1032 driver");
2027MODULE_LICENSE("GPL");