blob: e562a578f20e6f6decc5937a930541624851dc5d [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 *
Darrick J. Wong1a51e062009-03-12 13:36:38 +010038 * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
39 * MAX6692 chips made by Maxim. These are again similar to the LM86,
40 * but they use unsigned temperature values and can report temperatures
41 * from 0 to 145 degrees.
Ben Hutchings271dabf2008-10-17 17:51:11 +020042 *
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040043 * This driver also supports the MAX6680 and MAX6681, two other sensor
44 * chips made by Maxim. These are quite similar to the other Maxim
Jean Delvarea874a102008-10-17 17:51:10 +020045 * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
46 * be treated identically.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040047 *
Guenter Roeck06e1c0a2010-10-28 20:31:43 +020048 * This driver also supports the MAX6695 and MAX6696, two other sensor
49 * chips made by Maxim. These are also quite similar to other Maxim
50 * chips, but support three temperature sensors instead of two. MAX6695
51 * and MAX6696 only differ in the pinout so they can be treated identically.
52 *
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020053 * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
54 * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
55 * and extended mode. They are mostly compatible with LM90 except for a data
56 * format difference for the temperature value registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
Stijn Devriendt2ef01792011-06-06 10:40:45 +000058 * This driver also supports the SA56004 from Philips. This device is
59 * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
60 *
Guenter Roeckae544f62012-03-23 10:02:18 +010061 * This driver also supports the G781 from GMT. This device is compatible
62 * with the ADM1032.
63 *
Wei Ni1daaceb2013-11-15 10:40:39 +010064 * This driver also supports TMP451 from Texas Instruments. This device is
65 * supported in both compatibility and extended mode. It's mostly compatible
66 * with ADT7461 except for local temperature low byte register and max
67 * conversion rate.
68 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Since the LM90 was the first chipset supported by this driver, most
70 * comments will refer to this chipset, but are actually general and
71 * concern all supported chipsets, unless mentioned otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/module.h>
75#include <linux/init.h>
76#include <linux/slab.h>
77#include <linux/jiffies.h>
78#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040079#include <linux/hwmon.h>
80#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010081#include <linux/mutex.h>
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -030082#include <linux/of_device.h>
Jean Delvare0e39e012006-09-24 21:16:40 +020083#include <linux/sysfs.h>
Wei Ni109b1282013-11-15 10:40:39 +010084#include <linux/interrupt.h>
Wei Ni3e0f9642013-11-15 10:40:39 +010085#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * Addresses to scan
89 * Address is fully defined internally and cannot be changed except for
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040090 * MAX6659, MAX6680 and MAX6681.
Guenter Roeck5a4e5e62011-04-29 16:33:35 +020091 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
92 * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c.
93 * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
94 * have address 0x4d.
Ben Hutchings271dabf2008-10-17 17:51:11 +020095 * MAX6647 has address 0x4e.
Guenter Roeck13c84952010-10-28 20:31:43 +020096 * MAX6659 can have address 0x4c, 0x4d or 0x4e.
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -040097 * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
98 * 0x4c, 0x4d or 0x4e.
Stijn Devriendt2ef01792011-06-06 10:40:45 +000099 * SA56004 can have address 0x48 through 0x4F.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500102static const unsigned short normal_i2c[] = {
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000103 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
104 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Guenter Roeck13c84952010-10-28 20:31:43 +0200106enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
Wei Ni1daaceb2013-11-15 10:40:39 +0100107 max6646, w83l771, max6696, sa56004, g781, tmp451 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
110 * The LM90 registers
111 */
112
113#define LM90_REG_R_MAN_ID 0xFE
114#define LM90_REG_R_CHIP_ID 0xFF
115#define LM90_REG_R_CONFIG1 0x03
116#define LM90_REG_W_CONFIG1 0x09
117#define LM90_REG_R_CONFIG2 0xBF
118#define LM90_REG_W_CONFIG2 0xBF
119#define LM90_REG_R_CONVRATE 0x04
120#define LM90_REG_W_CONVRATE 0x0A
121#define LM90_REG_R_STATUS 0x02
122#define LM90_REG_R_LOCAL_TEMP 0x00
123#define LM90_REG_R_LOCAL_HIGH 0x05
124#define LM90_REG_W_LOCAL_HIGH 0x0B
125#define LM90_REG_R_LOCAL_LOW 0x06
126#define LM90_REG_W_LOCAL_LOW 0x0C
127#define LM90_REG_R_LOCAL_CRIT 0x20
128#define LM90_REG_W_LOCAL_CRIT 0x20
129#define LM90_REG_R_REMOTE_TEMPH 0x01
130#define LM90_REG_R_REMOTE_TEMPL 0x10
131#define LM90_REG_R_REMOTE_OFFSH 0x11
132#define LM90_REG_W_REMOTE_OFFSH 0x11
133#define LM90_REG_R_REMOTE_OFFSL 0x12
134#define LM90_REG_W_REMOTE_OFFSL 0x12
135#define LM90_REG_R_REMOTE_HIGHH 0x07
136#define LM90_REG_W_REMOTE_HIGHH 0x0D
137#define LM90_REG_R_REMOTE_HIGHL 0x13
138#define LM90_REG_W_REMOTE_HIGHL 0x13
139#define LM90_REG_R_REMOTE_LOWH 0x08
140#define LM90_REG_W_REMOTE_LOWH 0x0E
141#define LM90_REG_R_REMOTE_LOWL 0x14
142#define LM90_REG_W_REMOTE_LOWL 0x14
143#define LM90_REG_R_REMOTE_CRIT 0x19
144#define LM90_REG_W_REMOTE_CRIT 0x19
145#define LM90_REG_R_TCRIT_HYST 0x21
146#define LM90_REG_W_TCRIT_HYST 0x21
147
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200148/* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
Jean Delvaref65e1702008-10-17 17:51:09 +0200149
150#define MAX6657_REG_R_LOCAL_TEMPL 0x11
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200151#define MAX6696_REG_R_STATUS2 0x12
Guenter Roeck69487082010-10-28 20:31:43 +0200152#define MAX6659_REG_R_REMOTE_EMERG 0x16
153#define MAX6659_REG_W_REMOTE_EMERG 0x16
154#define MAX6659_REG_R_LOCAL_EMERG 0x17
155#define MAX6659_REG_W_LOCAL_EMERG 0x17
Jean Delvaref65e1702008-10-17 17:51:09 +0200156
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000157/* SA56004 registers */
158
159#define SA56004_REG_R_LOCAL_TEMPL 0x22
160
Guenter Roeck0c01b642010-10-28 20:31:44 +0200161#define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
162
Wei Ni1daaceb2013-11-15 10:40:39 +0100163/* TMP451 registers */
164#define TMP451_REG_R_LOCAL_TEMPL 0x15
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Nate Case23b2d472008-10-17 17:51:10 +0200167 * Device flags
168 */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200169#define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
170/* Device features */
171#define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
Guenter Roeck88073bb2010-10-28 20:31:43 +0200172#define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
Guenter Roeck69487082010-10-28 20:31:43 +0200173#define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200174#define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
175#define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
Guenter Roeck11793242010-10-28 20:31:44 +0200176#define LM90_HAVE_BROKEN_ALERT (1 << 7) /* Broken alert */
Nate Case23b2d472008-10-17 17:51:10 +0200177
Wei Ni072de492013-11-15 10:40:38 +0100178/* LM90 status */
179#define LM90_STATUS_LTHRM (1 << 0) /* local THERM limit tripped */
180#define LM90_STATUS_RTHRM (1 << 1) /* remote THERM limit tripped */
181#define LM90_STATUS_ROPEN (1 << 2) /* remote is an open circuit */
182#define LM90_STATUS_RLOW (1 << 3) /* remote low temp limit tripped */
183#define LM90_STATUS_RHIGH (1 << 4) /* remote high temp limit tripped */
184#define LM90_STATUS_LLOW (1 << 5) /* local low temp limit tripped */
185#define LM90_STATUS_LHIGH (1 << 6) /* local high temp limit tripped */
186
187#define MAX6696_STATUS2_R2THRM (1 << 1) /* remote2 THERM limit tripped */
188#define MAX6696_STATUS2_R2OPEN (1 << 2) /* remote2 is an open circuit */
189#define MAX6696_STATUS2_R2LOW (1 << 3) /* remote2 low temp limit tripped */
190#define MAX6696_STATUS2_R2HIGH (1 << 4) /* remote2 high temp limit tripped */
191#define MAX6696_STATUS2_ROT2 (1 << 5) /* remote emergency limit tripped */
192#define MAX6696_STATUS2_R2OT2 (1 << 6) /* remote2 emergency limit tripped */
193#define MAX6696_STATUS2_LOT2 (1 << 7) /* local emergency limit tripped */
194
Nate Case23b2d472008-10-17 17:51:10 +0200195/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Driver data (common to all clients)
197 */
198
Jean Delvare9b0e8522008-07-16 19:30:15 +0200199static const struct i2c_device_id lm90_id[] = {
200 { "adm1032", adm1032 },
201 { "adt7461", adt7461 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200202 { "adt7461a", adt7461 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100203 { "g781", g781 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200204 { "lm90", lm90 },
205 { "lm86", lm86 },
Jean Delvare97ae60b2008-10-26 17:04:39 +0100206 { "lm89", lm86 },
207 { "lm99", lm99 },
Ben Hutchings271dabf2008-10-17 17:51:11 +0200208 { "max6646", max6646 },
209 { "max6647", max6646 },
210 { "max6649", max6646 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200211 { "max6657", max6657 },
212 { "max6658", max6657 },
Guenter Roeck13c84952010-10-28 20:31:43 +0200213 { "max6659", max6659 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200214 { "max6680", max6680 },
215 { "max6681", max6680 },
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200216 { "max6695", max6696 },
217 { "max6696", max6696 },
Guenter Roeck5a4e5e62011-04-29 16:33:35 +0200218 { "nct1008", adt7461 },
Jean Delvare6771ea12010-03-05 22:17:13 +0100219 { "w83l771", w83l771 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000220 { "sa56004", sa56004 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100221 { "tmp451", tmp451 },
Jean Delvare9b0e8522008-07-16 19:30:15 +0200222 { }
223};
224MODULE_DEVICE_TABLE(i2c, lm90_id);
225
Guenter Roeck787afaa2019-04-04 06:21:17 -0700226static const struct of_device_id __maybe_unused lm90_of_match[] = {
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -0300227 {
228 .compatible = "adi,adm1032",
229 .data = (void *)adm1032
230 },
231 {
232 .compatible = "adi,adt7461",
233 .data = (void *)adt7461
234 },
235 {
236 .compatible = "adi,adt7461a",
237 .data = (void *)adt7461
238 },
239 {
240 .compatible = "gmt,g781",
241 .data = (void *)g781
242 },
243 {
244 .compatible = "national,lm90",
245 .data = (void *)lm90
246 },
247 {
248 .compatible = "national,lm86",
249 .data = (void *)lm86
250 },
251 {
252 .compatible = "national,lm89",
253 .data = (void *)lm86
254 },
255 {
256 .compatible = "national,lm99",
257 .data = (void *)lm99
258 },
259 {
260 .compatible = "dallas,max6646",
261 .data = (void *)max6646
262 },
263 {
264 .compatible = "dallas,max6647",
265 .data = (void *)max6646
266 },
267 {
268 .compatible = "dallas,max6649",
269 .data = (void *)max6646
270 },
271 {
272 .compatible = "dallas,max6657",
273 .data = (void *)max6657
274 },
275 {
276 .compatible = "dallas,max6658",
277 .data = (void *)max6657
278 },
279 {
280 .compatible = "dallas,max6659",
281 .data = (void *)max6659
282 },
283 {
284 .compatible = "dallas,max6680",
285 .data = (void *)max6680
286 },
287 {
288 .compatible = "dallas,max6681",
289 .data = (void *)max6680
290 },
291 {
292 .compatible = "dallas,max6695",
293 .data = (void *)max6696
294 },
295 {
296 .compatible = "dallas,max6696",
297 .data = (void *)max6696
298 },
299 {
300 .compatible = "onnn,nct1008",
301 .data = (void *)adt7461
302 },
303 {
304 .compatible = "winbond,w83l771",
305 .data = (void *)w83l771
306 },
307 {
308 .compatible = "nxp,sa56004",
309 .data = (void *)sa56004
310 },
311 {
312 .compatible = "ti,tmp451",
313 .data = (void *)tmp451
314 },
315 { },
316};
317MODULE_DEVICE_TABLE(of, lm90_of_match);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200320 * chip type specific parameters
321 */
322struct lm90_params {
323 u32 flags; /* Capabilities */
324 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
325 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200326 u8 max_convrate; /* Maximum conversion rate register value */
Jean Delvarea095f682011-07-27 23:22:25 -0700327 u8 reg_local_ext; /* Extended local temp register (optional) */
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200328};
329
330static const struct lm90_params lm90_params[] = {
331 [adm1032] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200332 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
333 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200334 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200335 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200336 },
337 [adt7461] = {
Guenter Roeck11793242010-10-28 20:31:44 +0200338 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
339 | LM90_HAVE_BROKEN_ALERT,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200340 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200341 .max_convrate = 10,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200342 },
Guenter Roeckae544f62012-03-23 10:02:18 +0100343 [g781] = {
344 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
345 | LM90_HAVE_BROKEN_ALERT,
346 .alert_alarms = 0x7c,
347 .max_convrate = 8,
348 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200349 [lm86] = {
350 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
351 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200352 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200353 },
354 [lm90] = {
355 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
356 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200357 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200358 },
359 [lm99] = {
360 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
361 .alert_alarms = 0x7b,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200362 .max_convrate = 9,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200363 },
364 [max6646] = {
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200365 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200366 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000367 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200368 },
369 [max6657] = {
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200370 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200371 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000372 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200373 },
374 [max6659] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700375 .flags = LM90_HAVE_EMERGENCY,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200376 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200377 .max_convrate = 8,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000378 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200379 },
380 [max6680] = {
381 .flags = LM90_HAVE_OFFSET,
382 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200383 .max_convrate = 7,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200384 },
385 [max6696] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700386 .flags = LM90_HAVE_EMERGENCY
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200387 | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3,
Guenter Roecke41fae2b2013-11-15 10:40:38 +0100388 .alert_alarms = 0x1c7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200389 .max_convrate = 6,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000390 .reg_local_ext = MAX6657_REG_R_LOCAL_TEMPL,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200391 },
392 [w83l771] = {
393 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
394 .alert_alarms = 0x7c,
Guenter Roeck0c01b642010-10-28 20:31:44 +0200395 .max_convrate = 8,
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200396 },
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000397 [sa56004] = {
Jean Delvarea095f682011-07-27 23:22:25 -0700398 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT,
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000399 .alert_alarms = 0x7b,
400 .max_convrate = 9,
401 .reg_local_ext = SA56004_REG_R_LOCAL_TEMPL,
402 },
Wei Ni1daaceb2013-11-15 10:40:39 +0100403 [tmp451] = {
404 .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
405 | LM90_HAVE_BROKEN_ALERT,
406 .alert_alarms = 0x7c,
407 .max_convrate = 9,
408 .reg_local_ext = TMP451_REG_R_LOCAL_TEMPL,
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700409 },
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200410};
411
412/*
Wei Ni40465d92013-11-15 10:40:39 +0100413 * TEMP8 register index
414 */
415enum lm90_temp8_reg_index {
416 LOCAL_LOW = 0,
417 LOCAL_HIGH,
418 LOCAL_CRIT,
419 REMOTE_CRIT,
420 LOCAL_EMERG, /* max6659 and max6695/96 */
421 REMOTE_EMERG, /* max6659 and max6695/96 */
422 REMOTE2_CRIT, /* max6695/96 only */
423 REMOTE2_EMERG, /* max6695/96 only */
424 TEMP8_REG_NUM
425};
426
427/*
428 * TEMP11 register index
429 */
430enum lm90_temp11_reg_index {
431 REMOTE_TEMP = 0,
432 REMOTE_LOW,
433 REMOTE_HIGH,
434 REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
435 LOCAL_TEMP,
436 REMOTE2_TEMP, /* max6695/96 only */
437 REMOTE2_LOW, /* max6695/96 only */
438 REMOTE2_HIGH, /* max6695/96 only */
439 TEMP11_REG_NUM
440};
441
442/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * Client data (each client gets its own)
444 */
445
446struct lm90_data {
Guenter Roeck1de8b252014-04-04 18:01:35 +0200447 struct i2c_client *client;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700448 u32 channel_config[4];
449 struct hwmon_channel_info temp_info;
450 const struct hwmon_channel_info *info[3];
451 struct hwmon_chip_info chip;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100452 struct mutex update_lock;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700453 bool valid; /* true if register values are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned long last_updated; /* in jiffies */
455 int kind;
Guenter Roeck4667bcb2010-10-28 20:31:43 +0200456 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Wolfram Sang38bab982016-06-05 09:35:43 +0200458 unsigned int update_interval; /* in milliseconds */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200459
Jean Delvare95238362010-03-05 22:17:14 +0100460 u8 config_orig; /* Original configuration register value */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200461 u8 convrate_orig; /* Original conversion rate register value */
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200462 u16 alert_alarms; /* Which alarm bits trigger ALERT# */
463 /* Upper 8 bits for max6695/96 */
Guenter Roeck0c01b642010-10-28 20:31:44 +0200464 u8 max_convrate; /* Maximum conversion rate */
Stijn Devriendt2ef01792011-06-06 10:40:45 +0000465 u8 reg_local_ext; /* local extension register offset */
Jean Delvare95238362010-03-05 22:17:14 +0100466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* registers values */
Wei Ni40465d92013-11-15 10:40:39 +0100468 s8 temp8[TEMP8_REG_NUM];
469 s16 temp11[TEMP11_REG_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 u8 temp_hyst;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +0200471 u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472};
473
474/*
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200475 * Support functions
476 */
477
478/*
479 * The ADM1032 supports PEC but not on write byte transactions, so we need
480 * to explicitly ask for a transaction without PEC.
481 */
482static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
483{
484 return i2c_smbus_xfer(client->adapter, client->addr,
485 client->flags & ~I2C_CLIENT_PEC,
486 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
487}
488
489/*
490 * It is assumed that client->update_lock is held (unless we are in
491 * detection or initialization steps). This matters when PEC is enabled,
492 * because we don't want the address pointer to change between the write
493 * byte and the read byte transactions.
494 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700495static int lm90_read_reg(struct i2c_client *client, u8 reg)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200496{
497 int err;
498
499 if (client->flags & I2C_CLIENT_PEC) {
500 err = adm1032_write_byte(client, reg);
501 if (err >= 0)
502 err = i2c_smbus_read_byte(client);
503 } else
504 err = i2c_smbus_read_byte_data(client, reg);
505
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700506 return err;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200507}
508
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700509static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200510{
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700511 int oldh, newh, l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200512
513 /*
514 * There is a trick here. We have to read two registers to have the
515 * sensor temperature, but we have to beware a conversion could occur
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300516 * between the readings. The datasheet says we should either use
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200517 * the one-shot conversion register, which we don't want to do
518 * (disables hardware monitoring) or monitor the busy bit, which is
519 * impossible (we can't read the values and monitor that bit at the
520 * exact same time). So the solution used here is to read the high
521 * byte once, then the low byte, then the high byte again. If the new
522 * high byte matches the old one, then we have a valid reading. Else
523 * we have to read the low byte again, and now we believe we have a
524 * correct reading.
525 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700526 oldh = lm90_read_reg(client, regh);
527 if (oldh < 0)
528 return oldh;
529 l = lm90_read_reg(client, regl);
530 if (l < 0)
531 return l;
532 newh = lm90_read_reg(client, regh);
533 if (newh < 0)
534 return newh;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200535 if (oldh != newh) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700536 l = lm90_read_reg(client, regl);
537 if (l < 0)
538 return l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200539 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700540 return (newh << 8) | l;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200541}
542
543/*
544 * client->update_lock must be held when calling this function (unless we are
545 * in detection or initialization steps), and while a remote channel other
546 * than channel 0 is selected. Also, calling code must make sure to re-select
547 * external channel 0 before releasing the lock. This is necessary because
548 * various registers have different meanings as a result of selecting a
549 * non-default remote channel.
550 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700551static inline int lm90_select_remote_channel(struct i2c_client *client,
552 struct lm90_data *data,
553 int channel)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200554{
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700555 int config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200556
557 if (data->kind == max6696) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700558 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
559 if (config < 0)
560 return config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200561 config &= ~0x08;
562 if (channel)
563 config |= 0x08;
564 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
565 config);
566 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700567 return 0;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200568}
569
Guenter Roeck0c01b642010-10-28 20:31:44 +0200570/*
571 * Set conversion rate.
572 * client->update_lock must be held when calling this function (unless we are
573 * in detection or initialization steps).
574 */
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700575static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
576 unsigned int interval)
Guenter Roeck0c01b642010-10-28 20:31:44 +0200577{
Guenter Roeck0c01b642010-10-28 20:31:44 +0200578 unsigned int update_interval;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700579 int i, err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200580
581 /* Shift calculations to avoid rounding errors */
582 interval <<= 6;
583
584 /* find the nearest update rate */
585 for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
586 i < data->max_convrate; i++, update_interval >>= 1)
587 if (interval >= update_interval * 3 / 4)
588 break;
589
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700590 err = i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200591 data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700592 return err;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200593}
594
Guenter Roeck10bfef42016-06-13 19:26:45 -0700595static int lm90_update_limits(struct device *dev)
596{
597 struct lm90_data *data = dev_get_drvdata(dev);
598 struct i2c_client *client = data->client;
599 int val;
600
601 val = lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT);
602 if (val < 0)
603 return val;
604 data->temp8[LOCAL_CRIT] = val;
605
606 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
607 if (val < 0)
608 return val;
609 data->temp8[REMOTE_CRIT] = val;
610
611 val = lm90_read_reg(client, LM90_REG_R_TCRIT_HYST);
612 if (val < 0)
613 return val;
614 data->temp_hyst = val;
615
Guenter Roeckbe9d6372016-07-25 14:47:18 -0700616 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
Guenter Roeck10bfef42016-06-13 19:26:45 -0700617 if (val < 0)
618 return val;
619 data->temp11[REMOTE_LOW] = val << 8;
620
621 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
622 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL);
623 if (val < 0)
624 return val;
625 data->temp11[REMOTE_LOW] |= val;
626 }
627
628 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
629 if (val < 0)
630 return val;
631 data->temp11[REMOTE_HIGH] = val << 8;
632
633 if (data->flags & LM90_HAVE_REM_LIMIT_EXT) {
634 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL);
635 if (val < 0)
636 return val;
637 data->temp11[REMOTE_HIGH] |= val;
638 }
639
640 if (data->flags & LM90_HAVE_OFFSET) {
641 val = lm90_read16(client, LM90_REG_R_REMOTE_OFFSH,
642 LM90_REG_R_REMOTE_OFFSL);
643 if (val < 0)
644 return val;
645 data->temp11[REMOTE_OFFSET] = val;
646 }
647
648 if (data->flags & LM90_HAVE_EMERGENCY) {
649 val = lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG);
650 if (val < 0)
651 return val;
652 data->temp8[LOCAL_EMERG] = val;
653
654 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
655 if (val < 0)
656 return val;
657 data->temp8[REMOTE_EMERG] = val;
658 }
659
660 if (data->kind == max6696) {
661 val = lm90_select_remote_channel(client, data, 1);
662 if (val < 0)
663 return val;
664
665 val = lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT);
666 if (val < 0)
667 return val;
668 data->temp8[REMOTE2_CRIT] = val;
669
670 val = lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG);
671 if (val < 0)
672 return val;
673 data->temp8[REMOTE2_EMERG] = val;
674
675 val = lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH);
676 if (val < 0)
677 return val;
678 data->temp11[REMOTE2_LOW] = val << 8;
679
680 val = lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH);
681 if (val < 0)
682 return val;
683 data->temp11[REMOTE2_HIGH] = val << 8;
684
685 lm90_select_remote_channel(client, data, 0);
686 }
687
688 return 0;
689}
690
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700691static int lm90_update_device(struct device *dev)
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200692{
Guenter Roeck1de8b252014-04-04 18:01:35 +0200693 struct lm90_data *data = dev_get_drvdata(dev);
694 struct i2c_client *client = data->client;
Guenter Roeck0c01b642010-10-28 20:31:44 +0200695 unsigned long next_update;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700696 int val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200697
Guenter Roeck10bfef42016-06-13 19:26:45 -0700698 if (!data->valid) {
699 val = lm90_update_limits(dev);
700 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700701 return val;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700702 }
703
Jean Delvare78c2c2f2013-07-08 14:18:24 +0200704 next_update = data->last_updated +
705 msecs_to_jiffies(data->update_interval);
Guenter Roeck0c01b642010-10-28 20:31:44 +0200706 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200707 dev_dbg(&client->dev, "Updating lm90 data.\n");
Guenter Roeck10bfef42016-06-13 19:26:45 -0700708
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700709 data->valid = false;
Guenter Roeck10bfef42016-06-13 19:26:45 -0700710
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700711 val = lm90_read_reg(client, LM90_REG_R_LOCAL_LOW);
712 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700713 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700714 data->temp8[LOCAL_LOW] = val;
715
716 val = lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH);
717 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700718 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700719 data->temp8[LOCAL_HIGH] = val;
720
Jean Delvarea095f682011-07-27 23:22:25 -0700721 if (data->reg_local_ext) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700722 val = lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
723 data->reg_local_ext);
724 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700725 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700726 data->temp11[LOCAL_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200727 } else {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700728 val = lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP);
729 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700730 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700731 data->temp11[LOCAL_TEMP] = val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200732 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700733 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
734 LM90_REG_R_REMOTE_TEMPL);
735 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700736 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700737 data->temp11[REMOTE_TEMP] = val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200738
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700739 val = lm90_read_reg(client, LM90_REG_R_STATUS);
740 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700741 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700742 data->alarms = val; /* lower 8 bit of alarms */
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200743
744 if (data->kind == max6696) {
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700745 val = lm90_select_remote_channel(client, data, 1);
746 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700747 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700748
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700749 val = lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
750 LM90_REG_R_REMOTE_TEMPL);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700751 if (val < 0) {
752 lm90_select_remote_channel(client, data, 0);
753 return val;
754 }
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700755 data->temp11[REMOTE2_TEMP] = val;
756
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200757 lm90_select_remote_channel(client, data, 0);
758
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700759 val = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
760 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700761 return val;
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700762 data->alarms |= val << 8;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200763 }
764
Guenter Roeckf36ffea2012-03-23 10:02:18 +0100765 /*
766 * Re-enable ALERT# output if it was originally enabled and
767 * relevant alarms are all clear
768 */
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700769 if (!(data->config_orig & 0x80) &&
770 !(data->alarms & data->alert_alarms)) {
771 val = lm90_read_reg(client, LM90_REG_R_CONFIG1);
772 if (val < 0)
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700773 return val;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200774
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700775 if (val & 0x80) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200776 dev_dbg(&client->dev, "Re-enabling ALERT#\n");
777 i2c_smbus_write_byte_data(client,
778 LM90_REG_W_CONFIG1,
Guenter Roeck37ad04d2016-06-13 06:57:37 -0700779 val & ~0x80);
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200780 }
781 }
782
783 data->last_updated = jiffies;
Guenter Roeck2f83ab72016-06-18 15:39:08 -0700784 data->valid = true;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200785 }
786
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700787 return 0;
Guenter Roeck15b66ab2010-10-28 20:31:43 +0200788}
789
790/*
Nate Casecea50fe2008-10-17 17:51:10 +0200791 * Conversions
792 * For local temperatures and limits, critical limits and the hysteresis
793 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
794 * For remote temperatures and limits, it uses signed 11-bit values with
Ben Hutchings271dabf2008-10-17 17:51:11 +0200795 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
796 * Maxim chips use unsigned values.
Nate Casecea50fe2008-10-17 17:51:10 +0200797 */
798
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200799static inline int temp_from_s8(s8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200800{
801 return val * 1000;
802}
803
Ben Hutchings271dabf2008-10-17 17:51:11 +0200804static inline int temp_from_u8(u8 val)
805{
806 return val * 1000;
807}
808
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200809static inline int temp_from_s16(s16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200810{
811 return val / 32 * 125;
812}
813
Ben Hutchings271dabf2008-10-17 17:51:11 +0200814static inline int temp_from_u16(u16 val)
815{
816 return val / 32 * 125;
817}
818
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200819static s8 temp_to_s8(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200820{
821 if (val <= -128000)
822 return -128;
823 if (val >= 127000)
824 return 127;
825 if (val < 0)
826 return (val - 500) / 1000;
827 return (val + 500) / 1000;
828}
829
Ben Hutchings271dabf2008-10-17 17:51:11 +0200830static u8 temp_to_u8(long val)
831{
832 if (val <= 0)
833 return 0;
834 if (val >= 255000)
835 return 255;
836 return (val + 500) / 1000;
837}
838
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200839static s16 temp_to_s16(long val)
Nate Casecea50fe2008-10-17 17:51:10 +0200840{
841 if (val <= -128000)
842 return 0x8000;
843 if (val >= 127875)
844 return 0x7FE0;
845 if (val < 0)
846 return (val - 62) / 125 * 32;
847 return (val + 62) / 125 * 32;
848}
849
850static u8 hyst_to_reg(long val)
851{
852 if (val <= 0)
853 return 0;
854 if (val >= 30500)
855 return 31;
856 return (val + 500) / 1000;
857}
858
859/*
Nate Case23b2d472008-10-17 17:51:10 +0200860 * ADT7461 in compatibility mode is almost identical to LM90 except that
861 * attempts to write values that are outside the range 0 < temp < 127 are
862 * treated as the boundary value.
863 *
864 * ADT7461 in "extended mode" operation uses unsigned integers offset by
865 * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
Nate Casecea50fe2008-10-17 17:51:10 +0200866 */
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200867static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200868{
Nate Case23b2d472008-10-17 17:51:10 +0200869 if (data->flags & LM90_FLAG_ADT7461_EXT)
870 return (val - 64) * 1000;
Guenter Roeck589f7072016-06-18 19:58:26 -0700871 return temp_from_s8(val);
Nate Casecea50fe2008-10-17 17:51:10 +0200872}
873
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200874static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
Nate Casecea50fe2008-10-17 17:51:10 +0200875{
Nate Case23b2d472008-10-17 17:51:10 +0200876 if (data->flags & LM90_FLAG_ADT7461_EXT)
877 return (val - 0x4000) / 64 * 250;
Guenter Roeck589f7072016-06-18 19:58:26 -0700878 return temp_from_s16(val);
Nate Case23b2d472008-10-17 17:51:10 +0200879}
880
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200881static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200882{
883 if (data->flags & LM90_FLAG_ADT7461_EXT) {
884 if (val <= -64000)
885 return 0;
886 if (val >= 191000)
887 return 0xFF;
888 return (val + 500 + 64000) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200889 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700890 if (val <= 0)
891 return 0;
892 if (val >= 127000)
893 return 127;
894 return (val + 500) / 1000;
Nate Case23b2d472008-10-17 17:51:10 +0200895}
896
Ben Hutchings9d4d3832008-10-17 17:51:10 +0200897static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
Nate Case23b2d472008-10-17 17:51:10 +0200898{
899 if (data->flags & LM90_FLAG_ADT7461_EXT) {
900 if (val <= -64000)
901 return 0;
902 if (val >= 191750)
903 return 0xFFC0;
904 return (val + 64000 + 125) / 250 * 64;
Nate Case23b2d472008-10-17 17:51:10 +0200905 }
Guenter Roeck589f7072016-06-18 19:58:26 -0700906 if (val <= 0)
907 return 0;
908 if (val >= 127750)
909 return 0x7FC0;
910 return (val + 125) / 250 * 64;
Nate Casecea50fe2008-10-17 17:51:10 +0200911}
912
Jean Delvarec3df5802005-10-26 21:39:40 +0200913/* pec used for ADM1032 only */
Julia Lawalle57959a2016-12-22 13:05:22 +0100914static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
Jean Delvarec3df5802005-10-26 21:39:40 +0200915 char *buf)
916{
917 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700918
Jean Delvarec3df5802005-10-26 21:39:40 +0200919 return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
920}
921
Julia Lawalle57959a2016-12-22 13:05:22 +0100922static ssize_t pec_store(struct device *dev, struct device_attribute *dummy,
923 const char *buf, size_t count)
Jean Delvarec3df5802005-10-26 21:39:40 +0200924{
925 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck11e57812010-10-28 20:31:42 +0200926 long val;
927 int err;
928
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100929 err = kstrtol(buf, 10, &val);
Guenter Roeck11e57812010-10-28 20:31:42 +0200930 if (err < 0)
931 return err;
Jean Delvarec3df5802005-10-26 21:39:40 +0200932
933 switch (val) {
934 case 0:
935 client->flags &= ~I2C_CLIENT_PEC;
936 break;
937 case 1:
938 client->flags |= I2C_CLIENT_PEC;
939 break;
940 default:
941 return -EINVAL;
942 }
943
944 return count;
945}
946
Julia Lawalle57959a2016-12-22 13:05:22 +0100947static DEVICE_ATTR_RW(pec);
Jean Delvarec3df5802005-10-26 21:39:40 +0200948
Guenter Roeckeb1c8f42016-06-18 19:56:08 -0700949static int lm90_get_temp11(struct lm90_data *data, int index)
950{
951 s16 temp11 = data->temp11[index];
952 int temp;
953
954 if (data->kind == adt7461 || data->kind == tmp451)
955 temp = temp_from_u16_adt7461(data, temp11);
956 else if (data->kind == max6646)
957 temp = temp_from_u16(temp11);
958 else
959 temp = temp_from_s16(temp11);
960
961 /* +16 degrees offset for temp2 for the LM99 */
962 if (data->kind == lm99 && index <= 2)
963 temp += 16000;
964
965 return temp;
966}
967
968static int lm90_set_temp11(struct lm90_data *data, int index, long val)
969{
970 static struct reg {
971 u8 high;
972 u8 low;
973 } reg[] = {
974 [REMOTE_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
975 [REMOTE_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL },
976 [REMOTE_OFFSET] = { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL },
977 [REMOTE2_LOW] = { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL },
978 [REMOTE2_HIGH] = { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL }
979 };
980 struct i2c_client *client = data->client;
981 struct reg *regp = &reg[index];
982 int err;
983
984 /* +16 degrees offset for temp2 for the LM99 */
985 if (data->kind == lm99 && index <= 2)
986 val -= 16000;
987
988 if (data->kind == adt7461 || data->kind == tmp451)
989 data->temp11[index] = temp_to_u16_adt7461(data, val);
990 else if (data->kind == max6646)
991 data->temp11[index] = temp_to_u8(val) << 8;
992 else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
993 data->temp11[index] = temp_to_s16(val);
994 else
995 data->temp11[index] = temp_to_s8(val) << 8;
996
997 lm90_select_remote_channel(client, data, index >= 3);
998 err = i2c_smbus_write_byte_data(client, regp->high,
999 data->temp11[index] >> 8);
1000 if (err < 0)
1001 return err;
1002 if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
1003 err = i2c_smbus_write_byte_data(client, regp->low,
1004 data->temp11[index] & 0xff);
1005
1006 lm90_select_remote_channel(client, data, 0);
1007 return err;
1008}
1009
1010static int lm90_get_temp8(struct lm90_data *data, int index)
1011{
1012 s8 temp8 = data->temp8[index];
1013 int temp;
1014
1015 if (data->kind == adt7461 || data->kind == tmp451)
1016 temp = temp_from_u8_adt7461(data, temp8);
1017 else if (data->kind == max6646)
1018 temp = temp_from_u8(temp8);
1019 else
1020 temp = temp_from_s8(temp8);
1021
1022 /* +16 degrees offset for temp2 for the LM99 */
1023 if (data->kind == lm99 && index == 3)
1024 temp += 16000;
1025
1026 return temp;
1027}
1028
1029static int lm90_set_temp8(struct lm90_data *data, int index, long val)
1030{
1031 static const u8 reg[TEMP8_REG_NUM] = {
1032 LM90_REG_W_LOCAL_LOW,
1033 LM90_REG_W_LOCAL_HIGH,
1034 LM90_REG_W_LOCAL_CRIT,
1035 LM90_REG_W_REMOTE_CRIT,
1036 MAX6659_REG_W_LOCAL_EMERG,
1037 MAX6659_REG_W_REMOTE_EMERG,
1038 LM90_REG_W_REMOTE_CRIT,
1039 MAX6659_REG_W_REMOTE_EMERG,
1040 };
1041 struct i2c_client *client = data->client;
1042 int err;
1043
1044 /* +16 degrees offset for temp2 for the LM99 */
1045 if (data->kind == lm99 && index == 3)
1046 val -= 16000;
1047
1048 if (data->kind == adt7461 || data->kind == tmp451)
1049 data->temp8[index] = temp_to_u8_adt7461(data, val);
1050 else if (data->kind == max6646)
1051 data->temp8[index] = temp_to_u8(val);
1052 else
1053 data->temp8[index] = temp_to_s8(val);
1054
1055 lm90_select_remote_channel(client, data, index >= 6);
1056 err = i2c_smbus_write_byte_data(client, reg[index], data->temp8[index]);
1057 lm90_select_remote_channel(client, data, 0);
1058
1059 return err;
1060}
1061
1062static int lm90_get_temphyst(struct lm90_data *data, int index)
1063{
1064 int temp;
1065
1066 if (data->kind == adt7461 || data->kind == tmp451)
1067 temp = temp_from_u8_adt7461(data, data->temp8[index]);
1068 else if (data->kind == max6646)
1069 temp = temp_from_u8(data->temp8[index]);
1070 else
1071 temp = temp_from_s8(data->temp8[index]);
1072
1073 /* +16 degrees offset for temp2 for the LM99 */
1074 if (data->kind == lm99 && index == 3)
1075 temp += 16000;
1076
1077 return temp - temp_from_s8(data->temp_hyst);
1078}
1079
1080static int lm90_set_temphyst(struct lm90_data *data, long val)
1081{
1082 struct i2c_client *client = data->client;
1083 int temp;
1084 int err;
1085
1086 if (data->kind == adt7461 || data->kind == tmp451)
1087 temp = temp_from_u8_adt7461(data, data->temp8[LOCAL_CRIT]);
1088 else if (data->kind == max6646)
1089 temp = temp_from_u8(data->temp8[LOCAL_CRIT]);
1090 else
1091 temp = temp_from_s8(data->temp8[LOCAL_CRIT]);
1092
1093 data->temp_hyst = hyst_to_reg(temp - val);
1094 err = i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
1095 data->temp_hyst);
1096 return err;
1097}
1098
1099static const u8 lm90_temp_index[3] = {
1100 LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
1101};
1102
1103static const u8 lm90_temp_min_index[3] = {
1104 LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
1105};
1106
1107static const u8 lm90_temp_max_index[3] = {
1108 LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
1109};
1110
1111static const u8 lm90_temp_crit_index[3] = {
1112 LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
1113};
1114
1115static const u8 lm90_temp_emerg_index[3] = {
1116 LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
1117};
1118
1119static const u8 lm90_min_alarm_bits[3] = { 5, 3, 11 };
Michael Wallee9572fd2017-01-02 17:53:39 +01001120static const u8 lm90_max_alarm_bits[3] = { 6, 4, 12 };
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001121static const u8 lm90_crit_alarm_bits[3] = { 0, 1, 9 };
1122static const u8 lm90_emergency_alarm_bits[3] = { 15, 13, 14 };
1123static const u8 lm90_fault_bits[3] = { 0, 2, 10 };
1124
1125static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
1126{
1127 struct lm90_data *data = dev_get_drvdata(dev);
1128 int err;
1129
1130 mutex_lock(&data->update_lock);
1131 err = lm90_update_device(dev);
1132 mutex_unlock(&data->update_lock);
1133 if (err)
1134 return err;
1135
1136 switch (attr) {
1137 case hwmon_temp_input:
1138 *val = lm90_get_temp11(data, lm90_temp_index[channel]);
1139 break;
1140 case hwmon_temp_min_alarm:
1141 *val = (data->alarms >> lm90_min_alarm_bits[channel]) & 1;
1142 break;
1143 case hwmon_temp_max_alarm:
1144 *val = (data->alarms >> lm90_max_alarm_bits[channel]) & 1;
1145 break;
1146 case hwmon_temp_crit_alarm:
1147 *val = (data->alarms >> lm90_crit_alarm_bits[channel]) & 1;
1148 break;
1149 case hwmon_temp_emergency_alarm:
1150 *val = (data->alarms >> lm90_emergency_alarm_bits[channel]) & 1;
1151 break;
1152 case hwmon_temp_fault:
1153 *val = (data->alarms >> lm90_fault_bits[channel]) & 1;
1154 break;
1155 case hwmon_temp_min:
1156 if (channel == 0)
1157 *val = lm90_get_temp8(data,
1158 lm90_temp_min_index[channel]);
1159 else
1160 *val = lm90_get_temp11(data,
1161 lm90_temp_min_index[channel]);
1162 break;
1163 case hwmon_temp_max:
1164 if (channel == 0)
1165 *val = lm90_get_temp8(data,
1166 lm90_temp_max_index[channel]);
1167 else
1168 *val = lm90_get_temp11(data,
1169 lm90_temp_max_index[channel]);
1170 break;
1171 case hwmon_temp_crit:
1172 *val = lm90_get_temp8(data, lm90_temp_crit_index[channel]);
1173 break;
1174 case hwmon_temp_crit_hyst:
1175 *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel]);
1176 break;
1177 case hwmon_temp_emergency:
1178 *val = lm90_get_temp8(data, lm90_temp_emerg_index[channel]);
1179 break;
1180 case hwmon_temp_emergency_hyst:
1181 *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel]);
1182 break;
1183 case hwmon_temp_offset:
1184 *val = lm90_get_temp11(data, REMOTE_OFFSET);
1185 break;
1186 default:
1187 return -EOPNOTSUPP;
1188 }
1189 return 0;
1190}
1191
1192static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
1193{
1194 struct lm90_data *data = dev_get_drvdata(dev);
1195 int err;
1196
1197 mutex_lock(&data->update_lock);
1198
1199 err = lm90_update_device(dev);
1200 if (err)
1201 goto error;
1202
1203 switch (attr) {
1204 case hwmon_temp_min:
1205 if (channel == 0)
1206 err = lm90_set_temp8(data,
1207 lm90_temp_min_index[channel],
1208 val);
1209 else
1210 err = lm90_set_temp11(data,
1211 lm90_temp_min_index[channel],
1212 val);
1213 break;
1214 case hwmon_temp_max:
1215 if (channel == 0)
1216 err = lm90_set_temp8(data,
1217 lm90_temp_max_index[channel],
1218 val);
1219 else
1220 err = lm90_set_temp11(data,
1221 lm90_temp_max_index[channel],
1222 val);
1223 break;
1224 case hwmon_temp_crit:
1225 err = lm90_set_temp8(data, lm90_temp_crit_index[channel], val);
1226 break;
1227 case hwmon_temp_crit_hyst:
1228 err = lm90_set_temphyst(data, val);
1229 break;
1230 case hwmon_temp_emergency:
1231 err = lm90_set_temp8(data, lm90_temp_emerg_index[channel], val);
1232 break;
1233 case hwmon_temp_offset:
1234 err = lm90_set_temp11(data, REMOTE_OFFSET, val);
1235 break;
1236 default:
1237 err = -EOPNOTSUPP;
1238 break;
1239 }
1240error:
1241 mutex_unlock(&data->update_lock);
1242
1243 return err;
1244}
1245
1246static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
1247{
1248 switch (attr) {
1249 case hwmon_temp_input:
1250 case hwmon_temp_min_alarm:
1251 case hwmon_temp_max_alarm:
1252 case hwmon_temp_crit_alarm:
1253 case hwmon_temp_emergency_alarm:
1254 case hwmon_temp_emergency_hyst:
1255 case hwmon_temp_fault:
Guenter Roeck33348512018-12-10 14:02:12 -08001256 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001257 case hwmon_temp_min:
1258 case hwmon_temp_max:
1259 case hwmon_temp_crit:
1260 case hwmon_temp_emergency:
1261 case hwmon_temp_offset:
Guenter Roeck33348512018-12-10 14:02:12 -08001262 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001263 case hwmon_temp_crit_hyst:
1264 if (channel == 0)
Guenter Roeck33348512018-12-10 14:02:12 -08001265 return 0644;
1266 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001267 default:
1268 return 0;
1269 }
1270}
1271
1272static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
1273{
1274 struct lm90_data *data = dev_get_drvdata(dev);
1275 int err;
1276
1277 mutex_lock(&data->update_lock);
1278 err = lm90_update_device(dev);
1279 mutex_unlock(&data->update_lock);
1280 if (err)
1281 return err;
1282
1283 switch (attr) {
1284 case hwmon_chip_update_interval:
1285 *val = data->update_interval;
1286 break;
1287 case hwmon_chip_alarms:
1288 *val = data->alarms;
1289 break;
1290 default:
1291 return -EOPNOTSUPP;
1292 }
1293
1294 return 0;
1295}
1296
1297static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
1298{
1299 struct lm90_data *data = dev_get_drvdata(dev);
1300 struct i2c_client *client = data->client;
1301 int err;
1302
1303 mutex_lock(&data->update_lock);
1304
1305 err = lm90_update_device(dev);
1306 if (err)
1307 goto error;
1308
1309 switch (attr) {
1310 case hwmon_chip_update_interval:
1311 err = lm90_set_convrate(client, data,
1312 clamp_val(val, 0, 100000));
1313 break;
1314 default:
1315 err = -EOPNOTSUPP;
1316 break;
1317 }
1318error:
1319 mutex_unlock(&data->update_lock);
1320
1321 return err;
1322}
1323
1324static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
1325{
1326 switch (attr) {
1327 case hwmon_chip_update_interval:
Guenter Roeck33348512018-12-10 14:02:12 -08001328 return 0644;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001329 case hwmon_chip_alarms:
Guenter Roeck33348512018-12-10 14:02:12 -08001330 return 0444;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001331 default:
1332 return 0;
1333 }
1334}
1335
1336static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
1337 u32 attr, int channel, long *val)
1338{
1339 switch (type) {
1340 case hwmon_chip:
1341 return lm90_chip_read(dev, attr, channel, val);
1342 case hwmon_temp:
1343 return lm90_temp_read(dev, attr, channel, val);
1344 default:
1345 return -EOPNOTSUPP;
1346 }
1347}
1348
1349static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
1350 u32 attr, int channel, long val)
1351{
1352 switch (type) {
1353 case hwmon_chip:
1354 return lm90_chip_write(dev, attr, channel, val);
1355 case hwmon_temp:
1356 return lm90_temp_write(dev, attr, channel, val);
1357 default:
1358 return -EOPNOTSUPP;
1359 }
1360}
1361
1362static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
1363 u32 attr, int channel)
1364{
1365 switch (type) {
1366 case hwmon_chip:
1367 return lm90_chip_is_visible(data, attr, channel);
1368 case hwmon_temp:
1369 return lm90_temp_is_visible(data, attr, channel);
1370 default:
1371 return 0;
1372 }
1373}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Jean Delvare9b0e8522008-07-16 19:30:15 +02001375/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001376static int lm90_detect(struct i2c_client *client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001377 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001379 struct i2c_adapter *adapter = client->adapter;
1380 int address = client->addr;
Jean Delvare8f2fa772009-12-09 20:35:53 +01001381 const char *name = NULL;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001382 int man_id, chip_id, config1, config2, convrate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare9b0e8522008-07-16 19:30:15 +02001385 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Jean Delvare8f2fa772009-12-09 20:35:53 +01001387 /* detection and identification */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001388 man_id = i2c_smbus_read_byte_data(client, LM90_REG_R_MAN_ID);
1389 chip_id = i2c_smbus_read_byte_data(client, LM90_REG_R_CHIP_ID);
1390 config1 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
1391 convrate = i2c_smbus_read_byte_data(client, LM90_REG_R_CONVRATE);
1392 if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0)
Jean Delvare8f2fa772009-12-09 20:35:53 +01001393 return -ENODEV;
1394
Jean Delvaref90be422011-07-24 20:37:05 +02001395 if (man_id == 0x01 || man_id == 0x5C || man_id == 0x41) {
Jean Delvareb2589ab2011-11-04 12:00:47 +01001396 config2 = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG2);
1397 if (config2 < 0)
Jean Delvare9b0e8522008-07-16 19:30:15 +02001398 return -ENODEV;
Jean Delvaref90be422011-07-24 20:37:05 +02001399 } else
Jean Delvareb2589ab2011-11-04 12:00:47 +01001400 config2 = 0; /* Make compiler happy */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jean Delvaref90be422011-07-24 20:37:05 +02001402 if ((address == 0x4C || address == 0x4D)
1403 && man_id == 0x01) { /* National Semiconductor */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001404 if ((config1 & 0x2A) == 0x00
1405 && (config2 & 0xF8) == 0x00
1406 && convrate <= 0x09) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001407 if (address == 0x4C
1408 && (chip_id & 0xF0) == 0x20) { /* LM90 */
1409 name = "lm90";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001411 if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
1412 name = "lm99";
1413 dev_info(&adapter->dev,
1414 "Assuming LM99 chip at 0x%02x\n",
1415 address);
1416 dev_info(&adapter->dev,
1417 "If it is an LM89, instantiate it "
1418 "with the new_device sysfs "
1419 "interface\n");
Rainer Birkenmaier32c82a92007-06-09 10:11:16 -04001420 } else
Jean Delvare8f2fa772009-12-09 20:35:53 +01001421 if (address == 0x4C
1422 && (chip_id & 0xF0) == 0x10) { /* LM86 */
1423 name = "lm86";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001426 } else
1427 if ((address == 0x4C || address == 0x4D)
1428 && man_id == 0x41) { /* Analog Devices */
1429 if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001430 && (config1 & 0x3F) == 0x00
1431 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001432 name = "adm1032";
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001433 /*
1434 * The ADM1032 supports PEC, but only if combined
1435 * transactions are not used.
1436 */
Jean Delvare8f2fa772009-12-09 20:35:53 +01001437 if (i2c_check_functionality(adapter,
1438 I2C_FUNC_SMBUS_BYTE))
1439 info->flags |= I2C_CLIENT_PEC;
1440 } else
1441 if (chip_id == 0x51 /* ADT7461 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001442 && (config1 & 0x1B) == 0x00
1443 && convrate <= 0x0A) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001444 name = "adt7461";
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001445 } else
1446 if (chip_id == 0x57 /* ADT7461A, NCT1008 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001447 && (config1 & 0x1B) == 0x00
1448 && convrate <= 0x0A) {
Guenter Roeck5a4e5e62011-04-29 16:33:35 +02001449 name = "adt7461a";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001450 }
1451 } else
1452 if (man_id == 0x4D) { /* Maxim */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001453 int emerg, emerg2, status2;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001454
1455 /*
1456 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
1457 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
1458 * exists, both readings will reflect the same value. Otherwise,
1459 * the readings will be different.
1460 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001461 emerg = i2c_smbus_read_byte_data(client,
1462 MAX6659_REG_R_REMOTE_EMERG);
1463 man_id = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001464 LM90_REG_R_MAN_ID);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001465 emerg2 = i2c_smbus_read_byte_data(client,
Jean Delvare8dc089d2011-11-04 12:00:46 +01001466 MAX6659_REG_R_REMOTE_EMERG);
Jean Delvareb2589ab2011-11-04 12:00:47 +01001467 status2 = i2c_smbus_read_byte_data(client,
1468 MAX6696_REG_R_STATUS2);
1469 if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001470 return -ENODEV;
1471
Jean Delvare8f2fa772009-12-09 20:35:53 +01001472 /*
1473 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
1474 * register. Reading from that address will return the last
1475 * read value, which in our case is those of the man_id
1476 * register. Likewise, the config1 register seems to lack a
1477 * low nibble, so the value will be those of the previous
1478 * read, so in our case those of the man_id register.
Guenter Roeck13c84952010-10-28 20:31:43 +02001479 * MAX6659 has a third set of upper temperature limit registers.
1480 * Those registers also return values on MAX6657 and MAX6658,
1481 * thus the only way to detect MAX6659 is by its address.
1482 * For this reason it will be mis-detected as MAX6657 if its
1483 * address is 0x4C.
Jean Delvare8f2fa772009-12-09 20:35:53 +01001484 */
1485 if (chip_id == man_id
Guenter Roeck13c84952010-10-28 20:31:43 +02001486 && (address == 0x4C || address == 0x4D || address == 0x4E)
Jean Delvareb2589ab2011-11-04 12:00:47 +01001487 && (config1 & 0x1F) == (man_id & 0x0F)
1488 && convrate <= 0x09) {
Guenter Roeck13c84952010-10-28 20:31:43 +02001489 if (address == 0x4C)
1490 name = "max6657";
1491 else
1492 name = "max6659";
Jean Delvare8f2fa772009-12-09 20:35:53 +01001493 } else
1494 /*
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001495 * Even though MAX6695 and MAX6696 do not have a chip ID
1496 * register, reading it returns 0x01. Bit 4 of the config1
1497 * register is unused and should return zero when read. Bit 0 of
1498 * the status2 register is unused and should return zero when
1499 * read.
1500 *
1501 * MAX6695 and MAX6696 have an additional set of temperature
1502 * limit registers. We can detect those chips by checking if
1503 * one of those registers exists.
1504 */
1505 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001506 && (config1 & 0x10) == 0x00
1507 && (status2 & 0x01) == 0x00
1508 && emerg == emerg2
1509 && convrate <= 0x07) {
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001510 name = "max6696";
1511 } else
1512 /*
Jean Delvare8f2fa772009-12-09 20:35:53 +01001513 * The chip_id register of the MAX6680 and MAX6681 holds the
1514 * revision of the chip. The lowest bit of the config1 register
1515 * is unused and should return zero when read, so should the
1516 * second to last bit of config1 (software reset).
1517 */
1518 if (chip_id == 0x01
Jean Delvareb2589ab2011-11-04 12:00:47 +01001519 && (config1 & 0x03) == 0x00
1520 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001521 name = "max6680";
1522 } else
1523 /*
1524 * The chip_id register of the MAX6646/6647/6649 holds the
1525 * revision of the chip. The lowest 6 bits of the config1
1526 * register are unused and should return zero when read.
1527 */
1528 if (chip_id == 0x59
Jean Delvareb2589ab2011-11-04 12:00:47 +01001529 && (config1 & 0x3f) == 0x00
1530 && convrate <= 0x07) {
Jean Delvare8f2fa772009-12-09 20:35:53 +01001531 name = "max6646";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001533 } else
1534 if (address == 0x4C
1535 && man_id == 0x5C) { /* Winbond/Nuvoton */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001536 if ((config1 & 0x2A) == 0x00
1537 && (config2 & 0xF8) == 0x00) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001538 if (chip_id == 0x01 /* W83L771W/G */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001539 && convrate <= 0x09) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001540 name = "w83l771";
1541 } else
1542 if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001543 && convrate <= 0x08) {
Jean Delvarec4f99a22010-10-28 20:31:44 +02001544 name = "w83l771";
1545 }
Jean Delvare6771ea12010-03-05 22:17:13 +01001546 }
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001547 } else
Jean Delvare6d101c52011-07-24 20:36:15 +02001548 if (address >= 0x48 && address <= 0x4F
1549 && man_id == 0xA1) { /* NXP Semiconductor/Philips */
Jean Delvare6d101c52011-07-24 20:36:15 +02001550 if (chip_id == 0x00
Jean Delvareb2589ab2011-11-04 12:00:47 +01001551 && (config1 & 0x2A) == 0x00
1552 && (config2 & 0xFE) == 0x00
1553 && convrate <= 0x09) {
Stijn Devriendt2ef01792011-06-06 10:40:45 +00001554 name = "sa56004";
1555 }
Guenter Roeckae544f62012-03-23 10:02:18 +01001556 } else
1557 if ((address == 0x4C || address == 0x4D)
1558 && man_id == 0x47) { /* GMT */
1559 if (chip_id == 0x01 /* G781 */
1560 && (config1 & 0x3F) == 0x00
1561 && convrate <= 0x08)
1562 name = "g781";
Wei Ni1daaceb2013-11-15 10:40:39 +01001563 } else
1564 if (address == 0x4C
1565 && man_id == 0x55) { /* Texas Instruments */
1566 int local_ext;
1567
1568 local_ext = i2c_smbus_read_byte_data(client,
1569 TMP451_REG_R_LOCAL_TEMPL);
1570
1571 if (chip_id == 0x00 /* TMP451 */
1572 && (config1 & 0x1B) == 0x00
1573 && convrate <= 0x09
1574 && (local_ext & 0x0F) == 0x00)
1575 name = "tmp451";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Jean Delvare8f2fa772009-12-09 20:35:53 +01001578 if (!name) { /* identification failed */
1579 dev_dbg(&adapter->dev,
1580 "Unsupported chip at 0x%02x (man_id=0x%02X, "
1581 "chip_id=0x%02X)\n", address, man_id, chip_id);
1582 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Jean Delvare8f2fa772009-12-09 20:35:53 +01001584
Jean Delvare9b0e8522008-07-16 19:30:15 +02001585 strlcpy(info->type, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jean Delvare9b0e8522008-07-16 19:30:15 +02001587 return 0;
1588}
1589
Guenter Roeck1f17a442016-06-13 06:19:11 -07001590static void lm90_restore_conf(void *_data)
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001591{
Guenter Roeck1f17a442016-06-13 06:19:11 -07001592 struct lm90_data *data = _data;
1593 struct i2c_client *client = data->client;
1594
Guenter Roeckf7001bb2012-03-23 10:02:18 +01001595 /* Restore initial configuration */
1596 i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
1597 data->convrate_orig);
1598 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
1599 data->config_orig);
1600}
1601
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001602static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001603{
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001604 int config, convrate;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001605
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001606 convrate = lm90_read_reg(client, LM90_REG_R_CONVRATE);
1607 if (convrate < 0)
1608 return convrate;
Guenter Roeck0c01b642010-10-28 20:31:44 +02001609 data->convrate_orig = convrate;
1610
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001611 /*
1612 * Start the conversions.
1613 */
Guenter Roeck0c01b642010-10-28 20:31:44 +02001614 lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001615 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1616 if (config < 0)
1617 return config;
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001618 data->config_orig = config;
1619
1620 /* Check Temperature Range Select */
Wei Ni1daaceb2013-11-15 10:40:39 +01001621 if (data->kind == adt7461 || data->kind == tmp451) {
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001622 if (config & 0x04)
1623 data->flags |= LM90_FLAG_ADT7461_EXT;
1624 }
1625
1626 /*
1627 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
1628 * 0.125 degree resolution) and range (0x08, extend range
1629 * to -64 degree) mode for the remote temperature sensor.
1630 */
1631 if (data->kind == max6680)
1632 config |= 0x18;
1633
1634 /*
1635 * Select external channel 0 for max6695/96
1636 */
1637 if (data->kind == max6696)
1638 config &= ~0x08;
1639
1640 config &= 0xBF; /* run */
1641 if (config != data->config_orig) /* Only write if changed */
1642 i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
Guenter Roeck1f17a442016-06-13 06:19:11 -07001643
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001644 return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001645}
1646
Wei Ni072de492013-11-15 10:40:38 +01001647static bool lm90_is_tripped(struct i2c_client *client, u16 *status)
1648{
1649 struct lm90_data *data = i2c_get_clientdata(client);
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001650 int st, st2 = 0;
Wei Ni072de492013-11-15 10:40:38 +01001651
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001652 st = lm90_read_reg(client, LM90_REG_R_STATUS);
1653 if (st < 0)
1654 return false;
Wei Ni072de492013-11-15 10:40:38 +01001655
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001656 if (data->kind == max6696) {
1657 st2 = lm90_read_reg(client, MAX6696_REG_R_STATUS2);
1658 if (st2 < 0)
1659 return false;
1660 }
Wei Ni072de492013-11-15 10:40:38 +01001661
1662 *status = st | (st2 << 8);
1663
1664 if ((st & 0x7f) == 0 && (st2 & 0xfe) == 0)
1665 return false;
1666
1667 if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
1668 (st2 & MAX6696_STATUS2_LOT2))
1669 dev_warn(&client->dev,
1670 "temp%d out of range, please check!\n", 1);
1671 if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
1672 (st2 & MAX6696_STATUS2_ROT2))
1673 dev_warn(&client->dev,
1674 "temp%d out of range, please check!\n", 2);
1675 if (st & LM90_STATUS_ROPEN)
1676 dev_warn(&client->dev,
1677 "temp%d diode open, please check!\n", 2);
1678 if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
1679 MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
1680 dev_warn(&client->dev,
1681 "temp%d out of range, please check!\n", 3);
1682 if (st2 & MAX6696_STATUS2_R2OPEN)
1683 dev_warn(&client->dev,
1684 "temp%d diode open, please check!\n", 3);
1685
1686 return true;
1687}
1688
Wei Ni109b1282013-11-15 10:40:39 +01001689static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
1690{
1691 struct i2c_client *client = dev_id;
1692 u16 status;
1693
1694 if (lm90_is_tripped(client, &status))
1695 return IRQ_HANDLED;
1696 else
1697 return IRQ_NONE;
1698}
1699
Guenter Roeck1f17a442016-06-13 06:19:11 -07001700static void lm90_remove_pec(void *dev)
1701{
1702 device_remove_file(dev, &dev_attr_pec);
1703}
1704
1705static void lm90_regulator_disable(void *regulator)
1706{
1707 regulator_disable(regulator);
1708}
1709
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001710
1711static const struct hwmon_ops lm90_ops = {
1712 .is_visible = lm90_is_visible,
1713 .read = lm90_read,
1714 .write = lm90_write,
1715};
1716
Jean Delvareb2589ab2011-11-04 12:00:47 +01001717static int lm90_probe(struct i2c_client *client,
Jean Delvare9b0e8522008-07-16 19:30:15 +02001718 const struct i2c_device_id *id)
1719{
Jean Delvareb2589ab2011-11-04 12:00:47 +01001720 struct device *dev = &client->dev;
1721 struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001722 struct hwmon_channel_info *info;
Wei Ni3e0f9642013-11-15 10:40:39 +01001723 struct regulator *regulator;
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001724 struct device *hwmon_dev;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001725 struct lm90_data *data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001726 int err;
1727
Wei Ni3e0f9642013-11-15 10:40:39 +01001728 regulator = devm_regulator_get(dev, "vcc");
1729 if (IS_ERR(regulator))
1730 return PTR_ERR(regulator);
1731
1732 err = regulator_enable(regulator);
1733 if (err < 0) {
Guenter Roeckd89fa682014-04-04 18:01:35 +02001734 dev_err(dev, "Failed to enable regulator: %d\n", err);
Wei Ni3e0f9642013-11-15 10:40:39 +01001735 return err;
1736 }
1737
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001738 err = devm_add_action_or_reset(dev, lm90_regulator_disable, regulator);
1739 if (err)
1740 return err;
Guenter Roeck1f17a442016-06-13 06:19:11 -07001741
Guenter Roeckd89fa682014-04-04 18:01:35 +02001742 data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
Guenter Roeck20f426f2012-06-02 09:58:10 -07001743 if (!data)
1744 return -ENOMEM;
1745
Guenter Roeck1de8b252014-04-04 18:01:35 +02001746 data->client = client;
Jean Delvareb2589ab2011-11-04 12:00:47 +01001747 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001748 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Jean Delvare9b0e8522008-07-16 19:30:15 +02001750 /* Set the device type */
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03001751 if (client->dev.of_node)
1752 data->kind = (enum chips)of_device_get_match_data(&client->dev);
1753 else
1754 data->kind = id->driver_data;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001755 if (data->kind == adm1032) {
1756 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
Jean Delvareb2589ab2011-11-04 12:00:47 +01001757 client->flags &= ~I2C_CLIENT_PEC;
Jean Delvare9b0e8522008-07-16 19:30:15 +02001758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001760 /*
1761 * Different devices have different alarm bits triggering the
1762 * ALERT# output
1763 */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001764 data->alert_alarms = lm90_params[data->kind].alert_alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001765
Guenter Roeck88073bb2010-10-28 20:31:43 +02001766 /* Set chip capabilities */
Guenter Roeck4667bcb2010-10-28 20:31:43 +02001767 data->flags = lm90_params[data->kind].flags;
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001768
1769 data->chip.ops = &lm90_ops;
1770 data->chip.info = data->info;
1771
Guenter Roecka4d41e62019-03-31 10:53:48 -07001772 data->info[0] = HWMON_CHANNEL_INFO(chip,
1773 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL | HWMON_C_ALARMS);
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001774 data->info[1] = &data->temp_info;
1775
1776 info = &data->temp_info;
1777 info->type = hwmon_temp;
1778 info->config = data->channel_config;
1779
1780 data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1781 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1782 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM;
1783 data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
1784 HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM |
1785 HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
1786
1787 if (data->flags & LM90_HAVE_OFFSET)
1788 data->channel_config[1] |= HWMON_T_OFFSET;
1789
1790 if (data->flags & LM90_HAVE_EMERGENCY) {
1791 data->channel_config[0] |= HWMON_T_EMERGENCY |
1792 HWMON_T_EMERGENCY_HYST;
1793 data->channel_config[1] |= HWMON_T_EMERGENCY |
1794 HWMON_T_EMERGENCY_HYST;
1795 }
1796
1797 if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
1798 data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
1799 data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
1800 }
1801
1802 if (data->flags & LM90_HAVE_TEMP3) {
1803 data->channel_config[2] = HWMON_T_INPUT |
1804 HWMON_T_MIN | HWMON_T_MAX |
1805 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
1806 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST |
1807 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
1808 HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY_ALARM |
1809 HWMON_T_FAULT;
1810 }
1811
Jean Delvarea095f682011-07-27 23:22:25 -07001812 data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
Guenter Roeck06e1c0a2010-10-28 20:31:43 +02001813
Guenter Roeck0c01b642010-10-28 20:31:44 +02001814 /* Set maximum conversion rate */
1815 data->max_convrate = lm90_params[data->kind].max_convrate;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* Initialize the LM90 chip */
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001818 err = lm90_init_client(client, data);
1819 if (err < 0) {
1820 dev_err(dev, "Failed to initialize device\n");
1821 return err;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001824 /*
1825 * The 'pec' attribute is attached to the i2c device and thus created
1826 * separately.
1827 */
Jean Delvareb2589ab2011-11-04 12:00:47 +01001828 if (client->flags & I2C_CLIENT_PEC) {
1829 err = device_create_file(dev, &dev_attr_pec);
Guenter Roeck11e57812010-10-28 20:31:42 +02001830 if (err)
Guenter Roeck1f17a442016-06-13 06:19:11 -07001831 return err;
Guenter Roeckc5fcf012016-07-25 14:53:23 -07001832 err = devm_add_action_or_reset(dev, lm90_remove_pec, dev);
1833 if (err)
1834 return err;
Jean Delvare0e39e012006-09-24 21:16:40 +02001835 }
Guenter Roeck084489e2014-04-04 18:01:35 +02001836
Guenter Roeckeb1c8f42016-06-18 19:56:08 -07001837 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
1838 data, &data->chip,
1839 NULL);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001840 if (IS_ERR(hwmon_dev))
1841 return PTR_ERR(hwmon_dev);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001842
Wei Ni109b1282013-11-15 10:40:39 +01001843 if (client->irq) {
1844 dev_dbg(dev, "IRQ: %d\n", client->irq);
1845 err = devm_request_threaded_irq(dev, client->irq,
1846 NULL, lm90_irq_thread,
1847 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1848 "lm90", client);
1849 if (err < 0) {
1850 dev_err(dev, "cannot request IRQ %d\n", client->irq);
Guenter Roeck6e5f62b2016-06-13 06:28:03 -07001851 return err;
Wei Ni109b1282013-11-15 10:40:39 +01001852 }
1853 }
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001858static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
1859 unsigned int flag)
Jean Delvare53de3342010-03-05 22:17:15 +01001860{
Wei Ni072de492013-11-15 10:40:38 +01001861 u16 alarms;
Jean Delvare53de3342010-03-05 22:17:15 +01001862
Benjamin Tissoiresb4f21052016-06-09 16:53:47 +02001863 if (type != I2C_PROTOCOL_SMBUS_ALERT)
1864 return;
1865
Wei Ni072de492013-11-15 10:40:38 +01001866 if (lm90_is_tripped(client, &alarms)) {
Guenter Roeckf36ffea2012-03-23 10:02:18 +01001867 /*
1868 * Disable ALERT# output, because these chips don't implement
1869 * SMBus alert correctly; they should only hold the alert line
1870 * low briefly.
1871 */
Wei Ni072de492013-11-15 10:40:38 +01001872 struct lm90_data *data = i2c_get_clientdata(client);
1873
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001874 if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
1875 (alarms & data->alert_alarms)) {
1876 int config;
1877
Jean Delvare53de3342010-03-05 22:17:15 +01001878 dev_dbg(&client->dev, "Disabling ALERT#\n");
Guenter Roeck37ad04d2016-06-13 06:57:37 -07001879 config = lm90_read_reg(client, LM90_REG_R_CONFIG1);
1880 if (config >= 0)
1881 i2c_smbus_write_byte_data(client,
1882 LM90_REG_W_CONFIG1,
1883 config | 0x80);
Jean Delvare53de3342010-03-05 22:17:15 +01001884 }
Wei Ni072de492013-11-15 10:40:38 +01001885 } else {
1886 dev_info(&client->dev, "Everything OK\n");
Jean Delvare53de3342010-03-05 22:17:15 +01001887 }
1888}
1889
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001890static struct i2c_driver lm90_driver = {
1891 .class = I2C_CLASS_HWMON,
1892 .driver = {
1893 .name = "lm90",
Javier Martinez Canillasdf8d57b2017-02-24 10:13:04 -03001894 .of_match_table = of_match_ptr(lm90_of_match),
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001895 },
1896 .probe = lm90_probe,
Guenter Roeck15b66ab2010-10-28 20:31:43 +02001897 .alert = lm90_alert,
1898 .id_table = lm90_id,
1899 .detect = lm90_detect,
1900 .address_list = normal_i2c,
1901};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Axel Linf0967ee2012-01-20 15:38:18 +08001903module_i2c_driver(lm90_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Jean Delvare7c81c60f2014-01-29 20:40:08 +01001905MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906MODULE_DESCRIPTION("LM90/ADM1032 driver");
1907MODULE_LICENSE("GPL");