| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware | 
 | 3 |  *          monitoring | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 4 |  * Copyright (C) 2003-2010  Jean Delvare <khali@linux-fr.org> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 |  * | 
 | 6 |  * Based on the lm83 driver. The LM90 is a sensor chip made by National | 
 | 7 |  * Semiconductor. It reports up to two temperatures (its own plus up to | 
 | 8 |  * one external one) with a 0.125 deg resolution (1 deg for local | 
| Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 9 |  * temperature) and a 3-4 deg accuracy. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 |  * | 
 | 11 |  * This driver also supports the LM89 and LM99, two other sensor chips | 
 | 12 |  * made by National Semiconductor. Both have an increased remote | 
 | 13 |  * temperature measurement accuracy (1 degree), and the LM99 | 
 | 14 |  * additionally shifts remote temperatures (measured and limits) by 16 | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 15 |  * degrees, which allows for higher temperatures measurement. | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 16 |  * Note that there is no way to differentiate between both chips. | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 17 |  * When device is auto-detected, the driver will assume an LM99. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  * | 
 | 19 |  * This driver also supports the LM86, another sensor chip made by | 
 | 20 |  * National Semiconductor. It is exactly similar to the LM90 except it | 
 | 21 |  * has a higher accuracy. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  * | 
 | 23 |  * This driver also supports the ADM1032, a sensor chip made by Analog | 
 | 24 |  * Devices. That chip is similar to the LM90, with a few differences | 
| Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 25 |  * that are not handled by this driver. Among others, it has a higher | 
 | 26 |  * accuracy than the LM90, much like the LM86 does. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  * | 
 | 28 |  * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor | 
| Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 29 |  * chips made by Maxim. These chips are similar to the LM86. | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 30 |  * Note that there is no easy way to differentiate between the three | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 31 |  * variants. We use the device address to detect MAX6659, which will result | 
 | 32 |  * in a detection as max6657 if it is on address 0x4c. The extra address | 
 | 33 |  * and features of the MAX6659 are only supported if the chip is configured | 
 | 34 |  * explicitly as max6659, or if its address is not 0x4c. | 
 | 35 |  * These chips lack the remote temperature offset feature. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  * | 
| Darrick J. Wong | 1a51e06 | 2009-03-12 13:36:38 +0100 | [diff] [blame] | 37 |  * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and | 
 | 38 |  * MAX6692 chips made by Maxim.  These are again similar to the LM86, | 
 | 39 |  * but they use unsigned temperature values and can report temperatures | 
 | 40 |  * from 0 to 145 degrees. | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 41 |  * | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 42 |  * This driver also supports the MAX6680 and MAX6681, two other sensor | 
 | 43 |  * chips made by Maxim. These are quite similar to the other Maxim | 
| Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 44 |  * chips. The MAX6680 and MAX6681 only differ in the pinout so they can | 
 | 45 |  * be treated identically. | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 46 |  * | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 47 |  * This driver also supports the MAX6695 and MAX6696, two other sensor | 
 | 48 |  * chips made by Maxim. These are also quite similar to other Maxim | 
 | 49 |  * chips, but support three temperature sensors instead of two. MAX6695 | 
 | 50 |  * and MAX6696 only differ in the pinout so they can be treated identically. | 
 | 51 |  * | 
| Guenter Roeck | 5a4e5e6 | 2011-04-29 16:33:35 +0200 | [diff] [blame] | 52 |  * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as | 
 | 53 |  * NCT1008 from ON Semiconductor. The chips are supported in both compatibility | 
 | 54 |  * and extended mode. They are mostly compatible with LM90 except for a data | 
 | 55 |  * format difference for the temperature value registers. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  * | 
 | 57 |  * Since the LM90 was the first chipset supported by this driver, most | 
 | 58 |  * comments will refer to this chipset, but are actually general and | 
 | 59 |  * concern all supported chipsets, unless mentioned otherwise. | 
 | 60 |  * | 
 | 61 |  * This program is free software; you can redistribute it and/or modify | 
 | 62 |  * it under the terms of the GNU General Public License as published by | 
 | 63 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 64 |  * (at your option) any later version. | 
 | 65 |  * | 
 | 66 |  * This program is distributed in the hope that it will be useful, | 
 | 67 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 68 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 69 |  * GNU General Public License for more details. | 
 | 70 |  * | 
 | 71 |  * You should have received a copy of the GNU General Public License | 
 | 72 |  * along with this program; if not, write to the Free Software | 
 | 73 |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 74 |  */ | 
 | 75 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/module.h> | 
 | 77 | #include <linux/init.h> | 
 | 78 | #include <linux/slab.h> | 
 | 79 | #include <linux/jiffies.h> | 
 | 80 | #include <linux/i2c.h> | 
| Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 81 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 82 | #include <linux/hwmon.h> | 
 | 83 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 84 | #include <linux/mutex.h> | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 85 | #include <linux/sysfs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
 | 87 | /* | 
 | 88 |  * Addresses to scan | 
 | 89 |  * Address is fully defined internally and cannot be changed except for | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 90 |  * MAX6659, MAX6680 and MAX6681. | 
| Guenter Roeck | 5a4e5e6 | 2011-04-29 16:33:35 +0200 | [diff] [blame] | 91 |  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649, | 
 | 92 |  * MAX6657, MAX6658, NCT1008 and W83L771 have address 0x4c. | 
 | 93 |  * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D | 
 | 94 |  * have address 0x4d. | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 95 |  * MAX6647 has address 0x4e. | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 96 |  * MAX6659 can have address 0x4c, 0x4d or 0x4e. | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 97 |  * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, | 
 | 98 |  * 0x4c, 0x4d or 0x4e. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 |  */ | 
 | 100 |  | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 101 | static const unsigned short normal_i2c[] = { | 
 | 102 | 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 |  | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 104 | enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680, | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 105 | 	max6646, w83l771, max6696 }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
 | 107 | /* | 
 | 108 |  * The LM90 registers | 
 | 109 |  */ | 
 | 110 |  | 
 | 111 | #define LM90_REG_R_MAN_ID		0xFE | 
 | 112 | #define LM90_REG_R_CHIP_ID		0xFF | 
 | 113 | #define LM90_REG_R_CONFIG1		0x03 | 
 | 114 | #define LM90_REG_W_CONFIG1		0x09 | 
 | 115 | #define LM90_REG_R_CONFIG2		0xBF | 
 | 116 | #define LM90_REG_W_CONFIG2		0xBF | 
 | 117 | #define LM90_REG_R_CONVRATE		0x04 | 
 | 118 | #define LM90_REG_W_CONVRATE		0x0A | 
 | 119 | #define LM90_REG_R_STATUS		0x02 | 
 | 120 | #define LM90_REG_R_LOCAL_TEMP		0x00 | 
 | 121 | #define LM90_REG_R_LOCAL_HIGH		0x05 | 
 | 122 | #define LM90_REG_W_LOCAL_HIGH		0x0B | 
 | 123 | #define LM90_REG_R_LOCAL_LOW		0x06 | 
 | 124 | #define LM90_REG_W_LOCAL_LOW		0x0C | 
 | 125 | #define LM90_REG_R_LOCAL_CRIT		0x20 | 
 | 126 | #define LM90_REG_W_LOCAL_CRIT		0x20 | 
 | 127 | #define LM90_REG_R_REMOTE_TEMPH		0x01 | 
 | 128 | #define LM90_REG_R_REMOTE_TEMPL		0x10 | 
 | 129 | #define LM90_REG_R_REMOTE_OFFSH		0x11 | 
 | 130 | #define LM90_REG_W_REMOTE_OFFSH		0x11 | 
 | 131 | #define LM90_REG_R_REMOTE_OFFSL		0x12 | 
 | 132 | #define LM90_REG_W_REMOTE_OFFSL		0x12 | 
 | 133 | #define LM90_REG_R_REMOTE_HIGHH		0x07 | 
 | 134 | #define LM90_REG_W_REMOTE_HIGHH		0x0D | 
 | 135 | #define LM90_REG_R_REMOTE_HIGHL		0x13 | 
 | 136 | #define LM90_REG_W_REMOTE_HIGHL		0x13 | 
 | 137 | #define LM90_REG_R_REMOTE_LOWH		0x08 | 
 | 138 | #define LM90_REG_W_REMOTE_LOWH		0x0E | 
 | 139 | #define LM90_REG_R_REMOTE_LOWL		0x14 | 
 | 140 | #define LM90_REG_W_REMOTE_LOWL		0x14 | 
 | 141 | #define LM90_REG_R_REMOTE_CRIT		0x19 | 
 | 142 | #define LM90_REG_W_REMOTE_CRIT		0x19 | 
 | 143 | #define LM90_REG_R_TCRIT_HYST		0x21 | 
 | 144 | #define LM90_REG_W_TCRIT_HYST		0x21 | 
 | 145 |  | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 146 | /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */ | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 147 |  | 
 | 148 | #define MAX6657_REG_R_LOCAL_TEMPL	0x11 | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 149 | #define MAX6696_REG_R_STATUS2		0x12 | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 150 | #define MAX6659_REG_R_REMOTE_EMERG	0x16 | 
 | 151 | #define MAX6659_REG_W_REMOTE_EMERG	0x16 | 
 | 152 | #define MAX6659_REG_R_LOCAL_EMERG	0x17 | 
 | 153 | #define MAX6659_REG_W_LOCAL_EMERG	0x17 | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 154 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 155 | #define LM90_DEF_CONVRATE_RVAL	6	/* Def conversion rate register value */ | 
 | 156 | #define LM90_MAX_CONVRATE_MS	16000	/* Maximum conversion rate in ms */ | 
 | 157 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 159 |  * Device flags | 
 | 160 |  */ | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 161 | #define LM90_FLAG_ADT7461_EXT	(1 << 0) /* ADT7461 extended mode	*/ | 
 | 162 | /* Device features */ | 
 | 163 | #define LM90_HAVE_OFFSET	(1 << 1) /* temperature offset register	*/ | 
 | 164 | #define LM90_HAVE_LOCAL_EXT	(1 << 2) /* extended local temperature	*/ | 
 | 165 | #define LM90_HAVE_REM_LIMIT_EXT	(1 << 3) /* extended remote limit	*/ | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 166 | #define LM90_HAVE_EMERGENCY	(1 << 4) /* 3rd upper (emergency) limit	*/ | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 167 | #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm		*/ | 
 | 168 | #define LM90_HAVE_TEMP3		(1 << 6) /* 3rd temperature sensor	*/ | 
| Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 169 | #define LM90_HAVE_BROKEN_ALERT	(1 << 7) /* Broken alert		*/ | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 170 |  | 
 | 171 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 |  * Driver data (common to all clients) | 
 | 173 |  */ | 
 | 174 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 175 | static const struct i2c_device_id lm90_id[] = { | 
 | 176 | 	{ "adm1032", adm1032 }, | 
 | 177 | 	{ "adt7461", adt7461 }, | 
| Guenter Roeck | 5a4e5e6 | 2011-04-29 16:33:35 +0200 | [diff] [blame] | 178 | 	{ "adt7461a", adt7461 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 179 | 	{ "lm90", lm90 }, | 
 | 180 | 	{ "lm86", lm86 }, | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 181 | 	{ "lm89", lm86 }, | 
 | 182 | 	{ "lm99", lm99 }, | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 183 | 	{ "max6646", max6646 }, | 
 | 184 | 	{ "max6647", max6646 }, | 
 | 185 | 	{ "max6649", max6646 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 186 | 	{ "max6657", max6657 }, | 
 | 187 | 	{ "max6658", max6657 }, | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 188 | 	{ "max6659", max6659 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 189 | 	{ "max6680", max6680 }, | 
 | 190 | 	{ "max6681", max6680 }, | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 191 | 	{ "max6695", max6696 }, | 
 | 192 | 	{ "max6696", max6696 }, | 
| Guenter Roeck | 5a4e5e6 | 2011-04-29 16:33:35 +0200 | [diff] [blame] | 193 | 	{ "nct1008", adt7461 }, | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 194 | 	{ "w83l771", w83l771 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 195 | 	{ } | 
 | 196 | }; | 
 | 197 | MODULE_DEVICE_TABLE(i2c, lm90_id); | 
 | 198 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 200 |  * chip type specific parameters | 
 | 201 |  */ | 
 | 202 | struct lm90_params { | 
 | 203 | 	u32 flags;		/* Capabilities */ | 
 | 204 | 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */ | 
 | 205 | 				/* Upper 8 bits for max6695/96 */ | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 206 | 	u8 max_convrate;	/* Maximum conversion rate register value */ | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 207 | }; | 
 | 208 |  | 
 | 209 | static const struct lm90_params lm90_params[] = { | 
 | 210 | 	[adm1032] = { | 
| Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 211 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | 
 | 212 | 		  | LM90_HAVE_BROKEN_ALERT, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 213 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 214 | 		.max_convrate = 10, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 215 | 	}, | 
 | 216 | 	[adt7461] = { | 
| Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 217 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | 
 | 218 | 		  | LM90_HAVE_BROKEN_ALERT, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 219 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 220 | 		.max_convrate = 10, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 221 | 	}, | 
 | 222 | 	[lm86] = { | 
 | 223 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, | 
 | 224 | 		.alert_alarms = 0x7b, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 225 | 		.max_convrate = 9, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 226 | 	}, | 
 | 227 | 	[lm90] = { | 
 | 228 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, | 
 | 229 | 		.alert_alarms = 0x7b, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 230 | 		.max_convrate = 9, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 231 | 	}, | 
 | 232 | 	[lm99] = { | 
 | 233 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, | 
 | 234 | 		.alert_alarms = 0x7b, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 235 | 		.max_convrate = 9, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 236 | 	}, | 
 | 237 | 	[max6646] = { | 
 | 238 | 		.flags = LM90_HAVE_LOCAL_EXT, | 
 | 239 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 240 | 		.max_convrate = 6, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 241 | 	}, | 
 | 242 | 	[max6657] = { | 
 | 243 | 		.flags = LM90_HAVE_LOCAL_EXT, | 
 | 244 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 245 | 		.max_convrate = 8, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 246 | 	}, | 
 | 247 | 	[max6659] = { | 
 | 248 | 		.flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY, | 
 | 249 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 250 | 		.max_convrate = 8, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 251 | 	}, | 
 | 252 | 	[max6680] = { | 
 | 253 | 		.flags = LM90_HAVE_OFFSET, | 
 | 254 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 255 | 		.max_convrate = 7, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 256 | 	}, | 
 | 257 | 	[max6696] = { | 
 | 258 | 		.flags = LM90_HAVE_LOCAL_EXT | LM90_HAVE_EMERGENCY | 
 | 259 | 		  | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3, | 
 | 260 | 		.alert_alarms = 0x187c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 261 | 		.max_convrate = 6, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 262 | 	}, | 
 | 263 | 	[w83l771] = { | 
 | 264 | 		.flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT, | 
 | 265 | 		.alert_alarms = 0x7c, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 266 | 		.max_convrate = 8, | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 267 | 	}, | 
 | 268 | }; | 
 | 269 |  | 
 | 270 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 |  * Client data (each client gets its own) | 
 | 272 |  */ | 
 | 273 |  | 
 | 274 | struct lm90_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 275 | 	struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 276 | 	struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 	char valid; /* zero until following fields are valid */ | 
 | 278 | 	unsigned long last_updated; /* in jiffies */ | 
 | 279 | 	int kind; | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 280 | 	u32 flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 282 | 	int update_interval;	/* in milliseconds */ | 
 | 283 |  | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 284 | 	u8 config_orig;		/* Original configuration register value */ | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 285 | 	u8 convrate_orig;	/* Original conversion rate register value */ | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 286 | 	u16 alert_alarms;	/* Which alarm bits trigger ALERT# */ | 
 | 287 | 				/* Upper 8 bits for max6695/96 */ | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 288 | 	u8 max_convrate;	/* Maximum conversion rate */ | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 289 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 	/* registers values */ | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 291 | 	s8 temp8[8];	/* 0: local low limit | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 292 | 			   1: local high limit | 
 | 293 | 			   2: local critical limit | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 294 | 			   3: remote critical limit | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 295 | 			   4: local emergency limit (max6659 and max6695/96) | 
 | 296 | 			   5: remote emergency limit (max6659 and max6695/96) | 
 | 297 | 			   6: remote 2 critical limit (max6695/96 only) | 
 | 298 | 			   7: remote 2 emergency limit (max6695/96 only) */ | 
 | 299 | 	s16 temp11[8];	/* 0: remote input | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 300 | 			   1: remote low limit | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 301 | 			   2: remote high limit | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 302 | 			   3: remote offset (except max6646, max6657/58/59, | 
 | 303 | 					     and max6695/96) | 
 | 304 | 			   4: local input | 
 | 305 | 			   5: remote 2 input (max6695/96 only) | 
 | 306 | 			   6: remote 2 low limit (max6695/96 only) | 
 | 307 | 			   7: remote 2 high limit (ma6695/96 only) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | 	u8 temp_hyst; | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 309 | 	u16 alarms; /* bitvector (upper 8 bits for max6695/96) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | }; | 
 | 311 |  | 
 | 312 | /* | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 313 |  * Support functions | 
 | 314 |  */ | 
 | 315 |  | 
 | 316 | /* | 
 | 317 |  * The ADM1032 supports PEC but not on write byte transactions, so we need | 
 | 318 |  * to explicitly ask for a transaction without PEC. | 
 | 319 |  */ | 
 | 320 | static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value) | 
 | 321 | { | 
 | 322 | 	return i2c_smbus_xfer(client->adapter, client->addr, | 
 | 323 | 			      client->flags & ~I2C_CLIENT_PEC, | 
 | 324 | 			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); | 
 | 325 | } | 
 | 326 |  | 
 | 327 | /* | 
 | 328 |  * It is assumed that client->update_lock is held (unless we are in | 
 | 329 |  * detection or initialization steps). This matters when PEC is enabled, | 
 | 330 |  * because we don't want the address pointer to change between the write | 
 | 331 |  * byte and the read byte transactions. | 
 | 332 |  */ | 
 | 333 | static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value) | 
 | 334 | { | 
 | 335 | 	int err; | 
 | 336 |  | 
 | 337 | 	if (client->flags & I2C_CLIENT_PEC) { | 
 | 338 | 		err = adm1032_write_byte(client, reg); | 
 | 339 | 		if (err >= 0) | 
 | 340 | 			err = i2c_smbus_read_byte(client); | 
 | 341 | 	} else | 
 | 342 | 		err = i2c_smbus_read_byte_data(client, reg); | 
 | 343 |  | 
 | 344 | 	if (err < 0) { | 
 | 345 | 		dev_warn(&client->dev, "Register %#02x read failed (%d)\n", | 
 | 346 | 			 reg, err); | 
 | 347 | 		return err; | 
 | 348 | 	} | 
 | 349 | 	*value = err; | 
 | 350 |  | 
 | 351 | 	return 0; | 
 | 352 | } | 
 | 353 |  | 
 | 354 | static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) | 
 | 355 | { | 
 | 356 | 	int err; | 
 | 357 | 	u8 oldh, newh, l; | 
 | 358 |  | 
 | 359 | 	/* | 
 | 360 | 	 * There is a trick here. We have to read two registers to have the | 
 | 361 | 	 * sensor temperature, but we have to beware a conversion could occur | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 362 | 	 * between the readings. The datasheet says we should either use | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 363 | 	 * the one-shot conversion register, which we don't want to do | 
 | 364 | 	 * (disables hardware monitoring) or monitor the busy bit, which is | 
 | 365 | 	 * impossible (we can't read the values and monitor that bit at the | 
 | 366 | 	 * exact same time). So the solution used here is to read the high | 
 | 367 | 	 * byte once, then the low byte, then the high byte again. If the new | 
 | 368 | 	 * high byte matches the old one, then we have a valid reading. Else | 
 | 369 | 	 * we have to read the low byte again, and now we believe we have a | 
 | 370 | 	 * correct reading. | 
 | 371 | 	 */ | 
 | 372 | 	if ((err = lm90_read_reg(client, regh, &oldh)) | 
 | 373 | 	 || (err = lm90_read_reg(client, regl, &l)) | 
 | 374 | 	 || (err = lm90_read_reg(client, regh, &newh))) | 
 | 375 | 		return err; | 
 | 376 | 	if (oldh != newh) { | 
 | 377 | 		err = lm90_read_reg(client, regl, &l); | 
 | 378 | 		if (err) | 
 | 379 | 			return err; | 
 | 380 | 	} | 
 | 381 | 	*value = (newh << 8) | l; | 
 | 382 |  | 
 | 383 | 	return 0; | 
 | 384 | } | 
 | 385 |  | 
 | 386 | /* | 
 | 387 |  * client->update_lock must be held when calling this function (unless we are | 
 | 388 |  * in detection or initialization steps), and while a remote channel other | 
 | 389 |  * than channel 0 is selected. Also, calling code must make sure to re-select | 
 | 390 |  * external channel 0 before releasing the lock. This is necessary because | 
 | 391 |  * various registers have different meanings as a result of selecting a | 
 | 392 |  * non-default remote channel. | 
 | 393 |  */ | 
 | 394 | static inline void lm90_select_remote_channel(struct i2c_client *client, | 
 | 395 | 					      struct lm90_data *data, | 
 | 396 | 					      int channel) | 
 | 397 | { | 
 | 398 | 	u8 config; | 
 | 399 |  | 
 | 400 | 	if (data->kind == max6696) { | 
 | 401 | 		lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | 
 | 402 | 		config &= ~0x08; | 
 | 403 | 		if (channel) | 
 | 404 | 			config |= 0x08; | 
 | 405 | 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | 
 | 406 | 					  config); | 
 | 407 | 	} | 
 | 408 | } | 
 | 409 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 410 | /* | 
 | 411 |  * Set conversion rate. | 
 | 412 |  * client->update_lock must be held when calling this function (unless we are | 
 | 413 |  * in detection or initialization steps). | 
 | 414 |  */ | 
 | 415 | static void lm90_set_convrate(struct i2c_client *client, struct lm90_data *data, | 
 | 416 | 			      unsigned int interval) | 
 | 417 | { | 
 | 418 | 	int i; | 
 | 419 | 	unsigned int update_interval; | 
 | 420 |  | 
 | 421 | 	/* Shift calculations to avoid rounding errors */ | 
 | 422 | 	interval <<= 6; | 
 | 423 |  | 
 | 424 | 	/* find the nearest update rate */ | 
 | 425 | 	for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6; | 
 | 426 | 	     i < data->max_convrate; i++, update_interval >>= 1) | 
 | 427 | 		if (interval >= update_interval * 3 / 4) | 
 | 428 | 			break; | 
 | 429 |  | 
 | 430 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, i); | 
 | 431 | 	data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64); | 
 | 432 | } | 
 | 433 |  | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 434 | static struct lm90_data *lm90_update_device(struct device *dev) | 
 | 435 | { | 
 | 436 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 437 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 438 | 	unsigned long next_update; | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 439 |  | 
 | 440 | 	mutex_lock(&data->update_lock); | 
 | 441 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 442 | 	next_update = data->last_updated | 
 | 443 | 	  + msecs_to_jiffies(data->update_interval) + 1; | 
 | 444 | 	if (time_after(jiffies, next_update) || !data->valid) { | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 445 | 		u8 h, l; | 
 | 446 | 		u8 alarms; | 
 | 447 |  | 
 | 448 | 		dev_dbg(&client->dev, "Updating lm90 data.\n"); | 
 | 449 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]); | 
 | 450 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]); | 
 | 451 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]); | 
 | 452 | 		lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]); | 
 | 453 | 		lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst); | 
 | 454 |  | 
 | 455 | 		if (data->flags & LM90_HAVE_LOCAL_EXT) { | 
 | 456 | 			lm90_read16(client, LM90_REG_R_LOCAL_TEMP, | 
 | 457 | 				    MAX6657_REG_R_LOCAL_TEMPL, | 
 | 458 | 				    &data->temp11[4]); | 
 | 459 | 		} else { | 
 | 460 | 			if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, | 
 | 461 | 					  &h) == 0) | 
 | 462 | 				data->temp11[4] = h << 8; | 
 | 463 | 		} | 
 | 464 | 		lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, | 
 | 465 | 			    LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]); | 
 | 466 |  | 
 | 467 | 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) { | 
 | 468 | 			data->temp11[1] = h << 8; | 
 | 469 | 			if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) | 
 | 470 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, | 
 | 471 | 					  &l) == 0) | 
 | 472 | 				data->temp11[1] |= l; | 
 | 473 | 		} | 
 | 474 | 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) { | 
 | 475 | 			data->temp11[2] = h << 8; | 
 | 476 | 			if ((data->flags & LM90_HAVE_REM_LIMIT_EXT) | 
 | 477 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, | 
 | 478 | 					  &l) == 0) | 
 | 479 | 				data->temp11[2] |= l; | 
 | 480 | 		} | 
 | 481 |  | 
 | 482 | 		if (data->flags & LM90_HAVE_OFFSET) { | 
 | 483 | 			if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH, | 
 | 484 | 					  &h) == 0 | 
 | 485 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL, | 
 | 486 | 					  &l) == 0) | 
 | 487 | 				data->temp11[3] = (h << 8) | l; | 
 | 488 | 		} | 
 | 489 | 		if (data->flags & LM90_HAVE_EMERGENCY) { | 
 | 490 | 			lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG, | 
 | 491 | 				      &data->temp8[4]); | 
 | 492 | 			lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG, | 
 | 493 | 				      &data->temp8[5]); | 
 | 494 | 		} | 
 | 495 | 		lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); | 
 | 496 | 		data->alarms = alarms;	/* save as 16 bit value */ | 
 | 497 |  | 
 | 498 | 		if (data->kind == max6696) { | 
 | 499 | 			lm90_select_remote_channel(client, data, 1); | 
 | 500 | 			lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, | 
 | 501 | 				      &data->temp8[6]); | 
 | 502 | 			lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG, | 
 | 503 | 				      &data->temp8[7]); | 
 | 504 | 			lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, | 
 | 505 | 				    LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]); | 
 | 506 | 			if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h)) | 
 | 507 | 				data->temp11[6] = h << 8; | 
 | 508 | 			if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h)) | 
 | 509 | 				data->temp11[7] = h << 8; | 
 | 510 | 			lm90_select_remote_channel(client, data, 0); | 
 | 511 |  | 
 | 512 | 			if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2, | 
 | 513 | 					   &alarms)) | 
 | 514 | 				data->alarms |= alarms << 8; | 
 | 515 | 		} | 
 | 516 |  | 
 | 517 | 		/* Re-enable ALERT# output if it was originally enabled and | 
 | 518 | 		 * relevant alarms are all clear */ | 
 | 519 | 		if ((data->config_orig & 0x80) == 0 | 
 | 520 | 		 && (data->alarms & data->alert_alarms) == 0) { | 
 | 521 | 			u8 config; | 
 | 522 |  | 
 | 523 | 			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | 
 | 524 | 			if (config & 0x80) { | 
 | 525 | 				dev_dbg(&client->dev, "Re-enabling ALERT#\n"); | 
 | 526 | 				i2c_smbus_write_byte_data(client, | 
 | 527 | 							  LM90_REG_W_CONFIG1, | 
 | 528 | 							  config & ~0x80); | 
 | 529 | 			} | 
 | 530 | 		} | 
 | 531 |  | 
 | 532 | 		data->last_updated = jiffies; | 
 | 533 | 		data->valid = 1; | 
 | 534 | 	} | 
 | 535 |  | 
 | 536 | 	mutex_unlock(&data->update_lock); | 
 | 537 |  | 
 | 538 | 	return data; | 
 | 539 | } | 
 | 540 |  | 
 | 541 | /* | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 542 |  * Conversions | 
 | 543 |  * For local temperatures and limits, critical limits and the hysteresis | 
 | 544 |  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. | 
 | 545 |  * For remote temperatures and limits, it uses signed 11-bit values with | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 546 |  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some | 
 | 547 |  * Maxim chips use unsigned values. | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 548 |  */ | 
 | 549 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 550 | static inline int temp_from_s8(s8 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 551 | { | 
 | 552 | 	return val * 1000; | 
 | 553 | } | 
 | 554 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 555 | static inline int temp_from_u8(u8 val) | 
 | 556 | { | 
 | 557 | 	return val * 1000; | 
 | 558 | } | 
 | 559 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 560 | static inline int temp_from_s16(s16 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 561 | { | 
 | 562 | 	return val / 32 * 125; | 
 | 563 | } | 
 | 564 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 565 | static inline int temp_from_u16(u16 val) | 
 | 566 | { | 
 | 567 | 	return val / 32 * 125; | 
 | 568 | } | 
 | 569 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 570 | static s8 temp_to_s8(long val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 571 | { | 
 | 572 | 	if (val <= -128000) | 
 | 573 | 		return -128; | 
 | 574 | 	if (val >= 127000) | 
 | 575 | 		return 127; | 
 | 576 | 	if (val < 0) | 
 | 577 | 		return (val - 500) / 1000; | 
 | 578 | 	return (val + 500) / 1000; | 
 | 579 | } | 
 | 580 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 581 | static u8 temp_to_u8(long val) | 
 | 582 | { | 
 | 583 | 	if (val <= 0) | 
 | 584 | 		return 0; | 
 | 585 | 	if (val >= 255000) | 
 | 586 | 		return 255; | 
 | 587 | 	return (val + 500) / 1000; | 
 | 588 | } | 
 | 589 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 590 | static s16 temp_to_s16(long val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 591 | { | 
 | 592 | 	if (val <= -128000) | 
 | 593 | 		return 0x8000; | 
 | 594 | 	if (val >= 127875) | 
 | 595 | 		return 0x7FE0; | 
 | 596 | 	if (val < 0) | 
 | 597 | 		return (val - 62) / 125 * 32; | 
 | 598 | 	return (val + 62) / 125 * 32; | 
 | 599 | } | 
 | 600 |  | 
 | 601 | static u8 hyst_to_reg(long val) | 
 | 602 | { | 
 | 603 | 	if (val <= 0) | 
 | 604 | 		return 0; | 
 | 605 | 	if (val >= 30500) | 
 | 606 | 		return 31; | 
 | 607 | 	return (val + 500) / 1000; | 
 | 608 | } | 
 | 609 |  | 
 | 610 | /* | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 611 |  * ADT7461 in compatibility mode is almost identical to LM90 except that | 
 | 612 |  * attempts to write values that are outside the range 0 < temp < 127 are | 
 | 613 |  * treated as the boundary value. | 
 | 614 |  * | 
 | 615 |  * ADT7461 in "extended mode" operation uses unsigned integers offset by | 
 | 616 |  * 64 (e.g., 0 -> -64 degC).  The range is restricted to -64..191 degC. | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 617 |  */ | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 618 | static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 619 | { | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 620 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) | 
 | 621 | 		return (val - 64) * 1000; | 
 | 622 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 623 | 		return temp_from_s8(val); | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 624 | } | 
 | 625 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 626 | static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 627 | { | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 628 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) | 
 | 629 | 		return (val - 0x4000) / 64 * 250; | 
 | 630 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 631 | 		return temp_from_s16(val); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 632 | } | 
 | 633 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 634 | static u8 temp_to_u8_adt7461(struct lm90_data *data, long val) | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 635 | { | 
 | 636 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) { | 
 | 637 | 		if (val <= -64000) | 
 | 638 | 			return 0; | 
 | 639 | 		if (val >= 191000) | 
 | 640 | 			return 0xFF; | 
 | 641 | 		return (val + 500 + 64000) / 1000; | 
 | 642 | 	} else { | 
 | 643 | 		if (val <= 0) | 
 | 644 | 			return 0; | 
 | 645 | 		if (val >= 127000) | 
 | 646 | 			return 127; | 
 | 647 | 		return (val + 500) / 1000; | 
 | 648 | 	} | 
 | 649 | } | 
 | 650 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 651 | static u16 temp_to_u16_adt7461(struct lm90_data *data, long val) | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 652 | { | 
 | 653 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) { | 
 | 654 | 		if (val <= -64000) | 
 | 655 | 			return 0; | 
 | 656 | 		if (val >= 191750) | 
 | 657 | 			return 0xFFC0; | 
 | 658 | 		return (val + 64000 + 125) / 250 * 64; | 
 | 659 | 	} else { | 
 | 660 | 		if (val <= 0) | 
 | 661 | 			return 0; | 
 | 662 | 		if (val >= 127750) | 
 | 663 | 			return 0x7FC0; | 
 | 664 | 		return (val + 125) / 250 * 64; | 
 | 665 | 	} | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 666 | } | 
 | 667 |  | 
 | 668 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 |  * Sysfs stuff | 
 | 670 |  */ | 
 | 671 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 672 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, | 
 | 673 | 			  char *buf) | 
 | 674 | { | 
 | 675 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 676 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 677 | 	int temp; | 
 | 678 |  | 
 | 679 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 680 | 		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 681 | 	else if (data->kind == max6646) | 
 | 682 | 		temp = temp_from_u8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 683 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 684 | 		temp = temp_from_s8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 685 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 686 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 687 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 688 | 		temp += 16000; | 
 | 689 |  | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 690 | 	return sprintf(buf, "%d\n", temp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 693 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, | 
 | 694 | 			 const char *buf, size_t count) | 
 | 695 | { | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 696 | 	static const u8 reg[8] = { | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 697 | 		LM90_REG_W_LOCAL_LOW, | 
 | 698 | 		LM90_REG_W_LOCAL_HIGH, | 
 | 699 | 		LM90_REG_W_LOCAL_CRIT, | 
 | 700 | 		LM90_REG_W_REMOTE_CRIT, | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 701 | 		MAX6659_REG_W_LOCAL_EMERG, | 
 | 702 | 		MAX6659_REG_W_REMOTE_EMERG, | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 703 | 		LM90_REG_W_REMOTE_CRIT, | 
 | 704 | 		MAX6659_REG_W_REMOTE_EMERG, | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 705 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 707 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 708 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 709 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 710 | 	int nr = attr->index; | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 711 | 	long val; | 
 | 712 | 	int err; | 
 | 713 |  | 
 | 714 | 	err = strict_strtol(buf, 10, &val); | 
 | 715 | 	if (err < 0) | 
 | 716 | 		return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 718 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 719 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 720 | 		val -= 16000; | 
 | 721 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 722 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 723 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 724 | 		data->temp8[nr] = temp_to_u8_adt7461(data, val); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 725 | 	else if (data->kind == max6646) | 
 | 726 | 		data->temp8[nr] = temp_to_u8(val); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 727 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 728 | 		data->temp8[nr] = temp_to_s8(val); | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 729 |  | 
 | 730 | 	lm90_select_remote_channel(client, data, nr >= 6); | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 731 | 	i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 732 | 	lm90_select_remote_channel(client, data, 0); | 
 | 733 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 734 | 	mutex_unlock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 735 | 	return count; | 
 | 736 | } | 
 | 737 |  | 
 | 738 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | 
 | 739 | 			   char *buf) | 
 | 740 | { | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 741 | 	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 742 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 743 | 	int temp; | 
 | 744 |  | 
 | 745 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 746 | 		temp = temp_from_u16_adt7461(data, data->temp11[attr->index]); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 747 | 	else if (data->kind == max6646) | 
 | 748 | 		temp = temp_from_u16(data->temp11[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 749 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 750 | 		temp = temp_from_s16(data->temp11[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 751 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 752 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 753 | 	if (data->kind == lm99 &&  attr->index <= 2) | 
 | 754 | 		temp += 16000; | 
 | 755 |  | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 756 | 	return sprintf(buf, "%d\n", temp); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 757 | } | 
 | 758 |  | 
 | 759 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | 
 | 760 | 			  const char *buf, size_t count) | 
 | 761 | { | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 762 | 	struct { | 
 | 763 | 		u8 high; | 
 | 764 | 		u8 low; | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 765 | 		int channel; | 
 | 766 | 	} reg[5] = { | 
 | 767 | 		{ LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 }, | 
 | 768 | 		{ LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 }, | 
 | 769 | 		{ LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 }, | 
 | 770 | 		{ LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 }, | 
 | 771 | 		{ LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 } | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 772 | 	}; | 
 | 773 |  | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 774 | 	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 775 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 776 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 777 | 	int nr = attr->nr; | 
 | 778 | 	int index = attr->index; | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 779 | 	long val; | 
 | 780 | 	int err; | 
 | 781 |  | 
 | 782 | 	err = strict_strtol(buf, 10, &val); | 
 | 783 | 	if (err < 0) | 
 | 784 | 		return err; | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 785 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 786 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 787 | 	if (data->kind == lm99 && index <= 2) | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 788 | 		val -= 16000; | 
 | 789 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 790 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 791 | 	if (data->kind == adt7461) | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 792 | 		data->temp11[index] = temp_to_u16_adt7461(data, val); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 793 | 	else if (data->kind == max6646) | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 794 | 		data->temp11[index] = temp_to_u8(val) << 8; | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 795 | 	else if (data->flags & LM90_HAVE_REM_LIMIT_EXT) | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 796 | 		data->temp11[index] = temp_to_s16(val); | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 797 | 	else | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 798 | 		data->temp11[index] = temp_to_s8(val) << 8; | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 799 |  | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 800 | 	lm90_select_remote_channel(client, data, reg[nr].channel); | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 801 | 	i2c_smbus_write_byte_data(client, reg[nr].high, | 
 | 802 | 				  data->temp11[index] >> 8); | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 803 | 	if (data->flags & LM90_HAVE_REM_LIMIT_EXT) | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 804 | 		i2c_smbus_write_byte_data(client, reg[nr].low, | 
 | 805 | 					  data->temp11[index] & 0xff); | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 806 | 	lm90_select_remote_channel(client, data, 0); | 
 | 807 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 808 | 	mutex_unlock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 809 | 	return count; | 
 | 810 | } | 
 | 811 |  | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 812 | static ssize_t show_temphyst(struct device *dev, | 
 | 813 | 			     struct device_attribute *devattr, | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 814 | 			     char *buf) | 
 | 815 | { | 
 | 816 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 817 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 818 | 	int temp; | 
 | 819 |  | 
 | 820 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 821 | 		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 822 | 	else if (data->kind == max6646) | 
 | 823 | 		temp = temp_from_u8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 824 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 825 | 		temp = temp_from_s8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 826 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 827 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 828 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 829 | 		temp += 16000; | 
 | 830 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 831 | 	return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst)); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 832 | } | 
 | 833 |  | 
 | 834 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, | 
 | 835 | 			    const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { | 
 | 837 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 838 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 839 | 	long val; | 
 | 840 | 	int err; | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 841 | 	int temp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 |  | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 843 | 	err = strict_strtol(buf, 10, &val); | 
 | 844 | 	if (err < 0) | 
 | 845 | 		return err; | 
 | 846 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 847 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 848 | 	if (data->kind == adt7461) | 
 | 849 | 		temp = temp_from_u8_adt7461(data, data->temp8[2]); | 
 | 850 | 	else if (data->kind == max6646) | 
 | 851 | 		temp = temp_from_u8(data->temp8[2]); | 
 | 852 | 	else | 
 | 853 | 		temp = temp_from_s8(data->temp8[2]); | 
 | 854 |  | 
 | 855 | 	data->temp_hyst = hyst_to_reg(temp - val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 857 | 				  data->temp_hyst); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 858 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | 	return count; | 
 | 860 | } | 
 | 861 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 862 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | 
 | 863 | 			   char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { | 
 | 865 | 	struct lm90_data *data = lm90_update_device(dev); | 
 | 866 | 	return sprintf(buf, "%d\n", data->alarms); | 
 | 867 | } | 
 | 868 |  | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 869 | static ssize_t show_alarm(struct device *dev, struct device_attribute | 
 | 870 | 			  *devattr, char *buf) | 
 | 871 | { | 
 | 872 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 873 | 	struct lm90_data *data = lm90_update_device(dev); | 
 | 874 | 	int bitnr = attr->index; | 
 | 875 |  | 
 | 876 | 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); | 
 | 877 | } | 
 | 878 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 879 | static ssize_t show_update_interval(struct device *dev, | 
 | 880 | 				    struct device_attribute *attr, char *buf) | 
 | 881 | { | 
 | 882 | 	struct lm90_data *data = dev_get_drvdata(dev); | 
 | 883 |  | 
 | 884 | 	return sprintf(buf, "%u\n", data->update_interval); | 
 | 885 | } | 
 | 886 |  | 
 | 887 | static ssize_t set_update_interval(struct device *dev, | 
 | 888 | 				   struct device_attribute *attr, | 
 | 889 | 				   const char *buf, size_t count) | 
 | 890 | { | 
 | 891 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 892 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 893 | 	unsigned long val; | 
 | 894 | 	int err; | 
 | 895 |  | 
 | 896 | 	err = strict_strtoul(buf, 10, &val); | 
 | 897 | 	if (err) | 
 | 898 | 		return err; | 
 | 899 |  | 
 | 900 | 	mutex_lock(&data->update_lock); | 
 | 901 | 	lm90_set_convrate(client, data, val); | 
 | 902 | 	mutex_unlock(&data->update_lock); | 
 | 903 |  | 
 | 904 | 	return count; | 
 | 905 | } | 
 | 906 |  | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 907 | static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4); | 
 | 908 | static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 909 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 910 | 	set_temp8, 0); | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 911 | static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | 
 | 912 | 	set_temp11, 0, 1); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 913 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 914 | 	set_temp8, 1); | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 915 | static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | 
 | 916 | 	set_temp11, 1, 2); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 917 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 918 | 	set_temp8, 2); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 919 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 920 | 	set_temp8, 3); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 921 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 922 | 	set_temphyst, 2); | 
 | 923 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); | 
| Guenter Roeck | 9651286 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 924 | static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, | 
 | 925 | 	set_temp11, 2, 3); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 926 |  | 
 | 927 | /* Individual alarm files */ | 
 | 928 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); | 
 | 929 | static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1); | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 930 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 931 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | 
 | 932 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | 
 | 933 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | 
 | 934 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 
 | 935 | /* Raw alarm file for compatibility */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
 | 937 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 938 | static DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR, show_update_interval, | 
 | 939 | 		   set_update_interval); | 
 | 940 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 941 | static struct attribute *lm90_attributes[] = { | 
 | 942 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 943 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
 | 944 | 	&sensor_dev_attr_temp1_min.dev_attr.attr, | 
 | 945 | 	&sensor_dev_attr_temp2_min.dev_attr.attr, | 
 | 946 | 	&sensor_dev_attr_temp1_max.dev_attr.attr, | 
 | 947 | 	&sensor_dev_attr_temp2_max.dev_attr.attr, | 
 | 948 | 	&sensor_dev_attr_temp1_crit.dev_attr.attr, | 
 | 949 | 	&sensor_dev_attr_temp2_crit.dev_attr.attr, | 
 | 950 | 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | 
 | 951 | 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | 
 | 952 |  | 
 | 953 | 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
 | 954 | 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 955 | 	&sensor_dev_attr_temp2_fault.dev_attr.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 956 | 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | 
 | 957 | 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
 | 958 | 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | 
 | 959 | 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
 | 960 | 	&dev_attr_alarms.attr, | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 961 | 	&dev_attr_update_interval.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 962 | 	NULL | 
 | 963 | }; | 
 | 964 |  | 
 | 965 | static const struct attribute_group lm90_group = { | 
 | 966 | 	.attrs = lm90_attributes, | 
 | 967 | }; | 
 | 968 |  | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 969 | /* | 
 | 970 |  * Additional attributes for devices with emergency sensors | 
 | 971 |  */ | 
 | 972 | static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8, | 
 | 973 | 	set_temp8, 4); | 
 | 974 | static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8, | 
 | 975 | 	set_temp8, 5); | 
 | 976 | static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst, | 
 | 977 | 			  NULL, 4); | 
 | 978 | static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst, | 
 | 979 | 			  NULL, 5); | 
 | 980 |  | 
 | 981 | static struct attribute *lm90_emergency_attributes[] = { | 
 | 982 | 	&sensor_dev_attr_temp1_emergency.dev_attr.attr, | 
 | 983 | 	&sensor_dev_attr_temp2_emergency.dev_attr.attr, | 
 | 984 | 	&sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr, | 
 | 985 | 	&sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr, | 
 | 986 | 	NULL | 
 | 987 | }; | 
 | 988 |  | 
 | 989 | static const struct attribute_group lm90_emergency_group = { | 
 | 990 | 	.attrs = lm90_emergency_attributes, | 
 | 991 | }; | 
 | 992 |  | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 993 | static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15); | 
 | 994 | static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13); | 
 | 995 |  | 
 | 996 | static struct attribute *lm90_emergency_alarm_attributes[] = { | 
 | 997 | 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, | 
 | 998 | 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, | 
 | 999 | 	NULL | 
 | 1000 | }; | 
 | 1001 |  | 
 | 1002 | static const struct attribute_group lm90_emergency_alarm_group = { | 
 | 1003 | 	.attrs = lm90_emergency_alarm_attributes, | 
 | 1004 | }; | 
 | 1005 |  | 
 | 1006 | /* | 
 | 1007 |  * Additional attributes for devices with 3 temperature sensors | 
 | 1008 |  */ | 
 | 1009 | static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5); | 
 | 1010 | static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11, | 
 | 1011 | 	set_temp11, 3, 6); | 
 | 1012 | static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11, | 
 | 1013 | 	set_temp11, 4, 7); | 
 | 1014 | static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8, | 
 | 1015 | 	set_temp8, 6); | 
 | 1016 | static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6); | 
 | 1017 | static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8, | 
 | 1018 | 	set_temp8, 7); | 
 | 1019 | static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst, | 
 | 1020 | 			  NULL, 7); | 
 | 1021 |  | 
 | 1022 | static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9); | 
 | 1023 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10); | 
 | 1024 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11); | 
 | 1025 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12); | 
 | 1026 | static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14); | 
 | 1027 |  | 
 | 1028 | static struct attribute *lm90_temp3_attributes[] = { | 
 | 1029 | 	&sensor_dev_attr_temp3_input.dev_attr.attr, | 
 | 1030 | 	&sensor_dev_attr_temp3_min.dev_attr.attr, | 
 | 1031 | 	&sensor_dev_attr_temp3_max.dev_attr.attr, | 
 | 1032 | 	&sensor_dev_attr_temp3_crit.dev_attr.attr, | 
 | 1033 | 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, | 
 | 1034 | 	&sensor_dev_attr_temp3_emergency.dev_attr.attr, | 
 | 1035 | 	&sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr, | 
 | 1036 |  | 
 | 1037 | 	&sensor_dev_attr_temp3_fault.dev_attr.attr, | 
 | 1038 | 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | 
 | 1039 | 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | 
 | 1040 | 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, | 
 | 1041 | 	&sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr, | 
 | 1042 | 	NULL | 
 | 1043 | }; | 
 | 1044 |  | 
 | 1045 | static const struct attribute_group lm90_temp3_group = { | 
 | 1046 | 	.attrs = lm90_temp3_attributes, | 
 | 1047 | }; | 
 | 1048 |  | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 1049 | /* pec used for ADM1032 only */ | 
 | 1050 | static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, | 
 | 1051 | 			char *buf) | 
 | 1052 | { | 
 | 1053 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 1054 | 	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); | 
 | 1055 | } | 
 | 1056 |  | 
 | 1057 | static ssize_t set_pec(struct device *dev, struct device_attribute *dummy, | 
 | 1058 | 		       const char *buf, size_t count) | 
 | 1059 | { | 
 | 1060 | 	struct i2c_client *client = to_i2c_client(dev); | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1061 | 	long val; | 
 | 1062 | 	int err; | 
 | 1063 |  | 
 | 1064 | 	err = strict_strtol(buf, 10, &val); | 
 | 1065 | 	if (err < 0) | 
 | 1066 | 		return err; | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 1067 |  | 
 | 1068 | 	switch (val) { | 
 | 1069 | 	case 0: | 
 | 1070 | 		client->flags &= ~I2C_CLIENT_PEC; | 
 | 1071 | 		break; | 
 | 1072 | 	case 1: | 
 | 1073 | 		client->flags |= I2C_CLIENT_PEC; | 
 | 1074 | 		break; | 
 | 1075 | 	default: | 
 | 1076 | 		return -EINVAL; | 
 | 1077 | 	} | 
 | 1078 |  | 
 | 1079 | 	return count; | 
 | 1080 | } | 
 | 1081 |  | 
 | 1082 | static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec); | 
 | 1083 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | /* | 
 | 1085 |  * Real code | 
 | 1086 |  */ | 
 | 1087 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1088 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 1089 | static int lm90_detect(struct i2c_client *new_client, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1090 | 		       struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1092 | 	struct i2c_adapter *adapter = new_client->adapter; | 
 | 1093 | 	int address = new_client->addr; | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1094 | 	const char *name = NULL; | 
 | 1095 | 	int man_id, chip_id, reg_config1, reg_convrate; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 |  | 
 | 1097 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1098 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1100 | 	/* detection and identification */ | 
 | 1101 | 	if ((man_id = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1102 | 						LM90_REG_R_MAN_ID)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1103 | 	 || (chip_id = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1104 | 						LM90_REG_R_CHIP_ID)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1105 | 	 || (reg_config1 = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1106 | 						LM90_REG_R_CONFIG1)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1107 | 	 || (reg_convrate = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 1108 | 						LM90_REG_R_CONVRATE)) < 0) | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1109 | 		return -ENODEV; | 
 | 1110 |  | 
 | 1111 | 	if ((address == 0x4C || address == 0x4D) | 
 | 1112 | 	 && man_id == 0x01) { /* National Semiconductor */ | 
 | 1113 | 		int reg_config2; | 
 | 1114 |  | 
 | 1115 | 		reg_config2 = i2c_smbus_read_byte_data(new_client, | 
 | 1116 | 						LM90_REG_R_CONFIG2); | 
 | 1117 | 		if (reg_config2 < 0) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1118 | 			return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1120 | 		if ((reg_config1 & 0x2A) == 0x00 | 
 | 1121 | 		 && (reg_config2 & 0xF8) == 0x00 | 
 | 1122 | 		 && reg_convrate <= 0x09) { | 
 | 1123 | 			if (address == 0x4C | 
 | 1124 | 			 && (chip_id & 0xF0) == 0x20) { /* LM90 */ | 
 | 1125 | 				name = "lm90"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | 			} else | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1127 | 			if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */ | 
 | 1128 | 				name = "lm99"; | 
 | 1129 | 				dev_info(&adapter->dev, | 
 | 1130 | 					 "Assuming LM99 chip at 0x%02x\n", | 
 | 1131 | 					 address); | 
 | 1132 | 				dev_info(&adapter->dev, | 
 | 1133 | 					 "If it is an LM89, instantiate it " | 
 | 1134 | 					 "with the new_device sysfs " | 
 | 1135 | 					 "interface\n"); | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 1136 | 			} else | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1137 | 			if (address == 0x4C | 
 | 1138 | 			 && (chip_id & 0xF0) == 0x10) { /* LM86 */ | 
 | 1139 | 				name = "lm86"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | 			} | 
 | 1141 | 		} | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1142 | 	} else | 
 | 1143 | 	if ((address == 0x4C || address == 0x4D) | 
 | 1144 | 	 && man_id == 0x41) { /* Analog Devices */ | 
 | 1145 | 		if ((chip_id & 0xF0) == 0x40 /* ADM1032 */ | 
 | 1146 | 		 && (reg_config1 & 0x3F) == 0x00 | 
 | 1147 | 		 && reg_convrate <= 0x0A) { | 
 | 1148 | 			name = "adm1032"; | 
 | 1149 | 			/* The ADM1032 supports PEC, but only if combined | 
 | 1150 | 			   transactions are not used. */ | 
 | 1151 | 			if (i2c_check_functionality(adapter, | 
 | 1152 | 						    I2C_FUNC_SMBUS_BYTE)) | 
 | 1153 | 				info->flags |= I2C_CLIENT_PEC; | 
 | 1154 | 		} else | 
 | 1155 | 		if (chip_id == 0x51 /* ADT7461 */ | 
 | 1156 | 		 && (reg_config1 & 0x1B) == 0x00 | 
 | 1157 | 		 && reg_convrate <= 0x0A) { | 
 | 1158 | 			name = "adt7461"; | 
| Guenter Roeck | 5a4e5e6 | 2011-04-29 16:33:35 +0200 | [diff] [blame] | 1159 | 		} else | 
 | 1160 | 		if (chip_id == 0x57 /* ADT7461A, NCT1008 */ | 
 | 1161 | 		 && (reg_config1 & 0x1B) == 0x00 | 
 | 1162 | 		 && reg_convrate <= 0x0A) { | 
 | 1163 | 			name = "adt7461a"; | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1164 | 		} | 
 | 1165 | 	} else | 
 | 1166 | 	if (man_id == 0x4D) { /* Maxim */ | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1167 | 		int reg_emerg, reg_emerg2, reg_status2; | 
 | 1168 |  | 
 | 1169 | 		/* | 
 | 1170 | 		 * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read | 
 | 1171 | 		 * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG | 
 | 1172 | 		 * exists, both readings will reflect the same value. Otherwise, | 
 | 1173 | 		 * the readings will be different. | 
 | 1174 | 		 */ | 
 | 1175 | 		if ((reg_emerg = i2c_smbus_read_byte_data(new_client, | 
 | 1176 | 						MAX6659_REG_R_REMOTE_EMERG)) < 0 | 
 | 1177 | 		 || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0 | 
 | 1178 | 		 || (reg_emerg2 = i2c_smbus_read_byte_data(new_client, | 
 | 1179 | 						MAX6659_REG_R_REMOTE_EMERG)) < 0 | 
 | 1180 | 		 || (reg_status2 = i2c_smbus_read_byte_data(new_client, | 
 | 1181 | 						MAX6696_REG_R_STATUS2)) < 0) | 
 | 1182 | 			return -ENODEV; | 
 | 1183 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1184 | 		/* | 
 | 1185 | 		 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id | 
 | 1186 | 		 * register. Reading from that address will return the last | 
 | 1187 | 		 * read value, which in our case is those of the man_id | 
 | 1188 | 		 * register. Likewise, the config1 register seems to lack a | 
 | 1189 | 		 * low nibble, so the value will be those of the previous | 
 | 1190 | 		 * read, so in our case those of the man_id register. | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1191 | 		 * MAX6659 has a third set of upper temperature limit registers. | 
 | 1192 | 		 * Those registers also return values on MAX6657 and MAX6658, | 
 | 1193 | 		 * thus the only way to detect MAX6659 is by its address. | 
 | 1194 | 		 * For this reason it will be mis-detected as MAX6657 if its | 
 | 1195 | 		 * address is 0x4C. | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1196 | 		 */ | 
 | 1197 | 		if (chip_id == man_id | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1198 | 		 && (address == 0x4C || address == 0x4D || address == 0x4E) | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1199 | 		 && (reg_config1 & 0x1F) == (man_id & 0x0F) | 
 | 1200 | 		 && reg_convrate <= 0x09) { | 
| Guenter Roeck | 13c8495 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1201 | 			if (address == 0x4C) | 
 | 1202 | 				name = "max6657"; | 
 | 1203 | 			else | 
 | 1204 | 				name = "max6659"; | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1205 | 		} else | 
 | 1206 | 		/* | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1207 | 		 * Even though MAX6695 and MAX6696 do not have a chip ID | 
 | 1208 | 		 * register, reading it returns 0x01. Bit 4 of the config1 | 
 | 1209 | 		 * register is unused and should return zero when read. Bit 0 of | 
 | 1210 | 		 * the status2 register is unused and should return zero when | 
 | 1211 | 		 * read. | 
 | 1212 | 		 * | 
 | 1213 | 		 * MAX6695 and MAX6696 have an additional set of temperature | 
 | 1214 | 		 * limit registers. We can detect those chips by checking if | 
 | 1215 | 		 * one of those registers exists. | 
 | 1216 | 		 */ | 
 | 1217 | 		if (chip_id == 0x01 | 
 | 1218 | 		 && (reg_config1 & 0x10) == 0x00 | 
 | 1219 | 		 && (reg_status2 & 0x01) == 0x00 | 
 | 1220 | 		 && reg_emerg == reg_emerg2 | 
 | 1221 | 		 && reg_convrate <= 0x07) { | 
 | 1222 | 			name = "max6696"; | 
 | 1223 | 		} else | 
 | 1224 | 		/* | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1225 | 		 * The chip_id register of the MAX6680 and MAX6681 holds the | 
 | 1226 | 		 * revision of the chip. The lowest bit of the config1 register | 
 | 1227 | 		 * is unused and should return zero when read, so should the | 
 | 1228 | 		 * second to last bit of config1 (software reset). | 
 | 1229 | 		 */ | 
 | 1230 | 		if (chip_id == 0x01 | 
 | 1231 | 		 && (reg_config1 & 0x03) == 0x00 | 
 | 1232 | 		 && reg_convrate <= 0x07) { | 
 | 1233 | 			name = "max6680"; | 
 | 1234 | 		} else | 
 | 1235 | 		/* | 
 | 1236 | 		 * The chip_id register of the MAX6646/6647/6649 holds the | 
 | 1237 | 		 * revision of the chip. The lowest 6 bits of the config1 | 
 | 1238 | 		 * register are unused and should return zero when read. | 
 | 1239 | 		 */ | 
 | 1240 | 		if (chip_id == 0x59 | 
 | 1241 | 		 && (reg_config1 & 0x3f) == 0x00 | 
 | 1242 | 		 && reg_convrate <= 0x07) { | 
 | 1243 | 			name = "max6646"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | 		} | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 1245 | 	} else | 
 | 1246 | 	if (address == 0x4C | 
 | 1247 | 	 && man_id == 0x5C) { /* Winbond/Nuvoton */ | 
| Jean Delvare | c4f99a2 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1248 | 		int reg_config2; | 
 | 1249 |  | 
 | 1250 | 		reg_config2 = i2c_smbus_read_byte_data(new_client, | 
 | 1251 | 						LM90_REG_R_CONFIG2); | 
 | 1252 | 		if (reg_config2 < 0) | 
 | 1253 | 			return -ENODEV; | 
 | 1254 |  | 
 | 1255 | 		if ((reg_config1 & 0x2A) == 0x00 | 
 | 1256 | 		 && (reg_config2 & 0xF8) == 0x00) { | 
 | 1257 | 			if (chip_id == 0x01 /* W83L771W/G */ | 
 | 1258 | 			 && reg_convrate <= 0x09) { | 
 | 1259 | 				name = "w83l771"; | 
 | 1260 | 			} else | 
 | 1261 | 			if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */ | 
 | 1262 | 			 && reg_convrate <= 0x08) { | 
 | 1263 | 				name = "w83l771"; | 
 | 1264 | 			} | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 1265 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | 	} | 
 | 1267 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1268 | 	if (!name) { /* identification failed */ | 
 | 1269 | 		dev_dbg(&adapter->dev, | 
 | 1270 | 			"Unsupported chip at 0x%02x (man_id=0x%02X, " | 
 | 1271 | 			"chip_id=0x%02X)\n", address, man_id, chip_id); | 
 | 1272 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | 	} | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 1274 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1275 | 	strlcpy(info->type, name, I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1277 | 	return 0; | 
 | 1278 | } | 
 | 1279 |  | 
| Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1280 | static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data) | 
 | 1281 | { | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1282 | 	if (data->flags & LM90_HAVE_TEMP3) | 
 | 1283 | 		sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group); | 
 | 1284 | 	if (data->flags & LM90_HAVE_EMERGENCY_ALARM) | 
 | 1285 | 		sysfs_remove_group(&client->dev.kobj, | 
 | 1286 | 				   &lm90_emergency_alarm_group); | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1287 | 	if (data->flags & LM90_HAVE_EMERGENCY) | 
 | 1288 | 		sysfs_remove_group(&client->dev.kobj, | 
 | 1289 | 				   &lm90_emergency_group); | 
| Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1290 | 	if (data->flags & LM90_HAVE_OFFSET) | 
 | 1291 | 		device_remove_file(&client->dev, | 
 | 1292 | 				   &sensor_dev_attr_temp2_offset.dev_attr); | 
 | 1293 | 	device_remove_file(&client->dev, &dev_attr_pec); | 
 | 1294 | 	sysfs_remove_group(&client->dev.kobj, &lm90_group); | 
 | 1295 | } | 
 | 1296 |  | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1297 | static void lm90_init_client(struct i2c_client *client) | 
 | 1298 | { | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1299 | 	u8 config, convrate; | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1300 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 1301 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1302 | 	if (lm90_read_reg(client, LM90_REG_R_CONVRATE, &convrate) < 0) { | 
 | 1303 | 		dev_warn(&client->dev, "Failed to read convrate register!\n"); | 
 | 1304 | 		convrate = LM90_DEF_CONVRATE_RVAL; | 
 | 1305 | 	} | 
 | 1306 | 	data->convrate_orig = convrate; | 
 | 1307 |  | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1308 | 	/* | 
 | 1309 | 	 * Start the conversions. | 
 | 1310 | 	 */ | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1311 | 	lm90_set_convrate(client, data, 500);	/* 500ms; 2Hz conversion rate */ | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1312 | 	if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) { | 
 | 1313 | 		dev_warn(&client->dev, "Initialization failed!\n"); | 
 | 1314 | 		return; | 
 | 1315 | 	} | 
 | 1316 | 	data->config_orig = config; | 
 | 1317 |  | 
 | 1318 | 	/* Check Temperature Range Select */ | 
 | 1319 | 	if (data->kind == adt7461) { | 
 | 1320 | 		if (config & 0x04) | 
 | 1321 | 			data->flags |= LM90_FLAG_ADT7461_EXT; | 
 | 1322 | 	} | 
 | 1323 |  | 
 | 1324 | 	/* | 
 | 1325 | 	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10, | 
 | 1326 | 	 * 0.125 degree resolution) and range (0x08, extend range | 
 | 1327 | 	 * to -64 degree) mode for the remote temperature sensor. | 
 | 1328 | 	 */ | 
 | 1329 | 	if (data->kind == max6680) | 
 | 1330 | 		config |= 0x18; | 
 | 1331 |  | 
 | 1332 | 	/* | 
 | 1333 | 	 * Select external channel 0 for max6695/96 | 
 | 1334 | 	 */ | 
 | 1335 | 	if (data->kind == max6696) | 
 | 1336 | 		config &= ~0x08; | 
 | 1337 |  | 
 | 1338 | 	config &= 0xBF;	/* run */ | 
 | 1339 | 	if (config != data->config_orig) /* Only write if changed */ | 
 | 1340 | 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); | 
 | 1341 | } | 
 | 1342 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1343 | static int lm90_probe(struct i2c_client *new_client, | 
 | 1344 | 		      const struct i2c_device_id *id) | 
 | 1345 | { | 
 | 1346 | 	struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent); | 
 | 1347 | 	struct lm90_data *data; | 
 | 1348 | 	int err; | 
 | 1349 |  | 
 | 1350 | 	data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL); | 
 | 1351 | 	if (!data) { | 
 | 1352 | 		err = -ENOMEM; | 
 | 1353 | 		goto exit; | 
 | 1354 | 	} | 
 | 1355 | 	i2c_set_clientdata(new_client, data); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1356 | 	mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1358 | 	/* Set the device type */ | 
 | 1359 | 	data->kind = id->driver_data; | 
 | 1360 | 	if (data->kind == adm1032) { | 
 | 1361 | 		if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | 
 | 1362 | 			new_client->flags &= ~I2C_CLIENT_PEC; | 
 | 1363 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1365 | 	/* Different devices have different alarm bits triggering the | 
 | 1366 | 	 * ALERT# output */ | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1367 | 	data->alert_alarms = lm90_params[data->kind].alert_alarms; | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1368 |  | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1369 | 	/* Set chip capabilities */ | 
| Guenter Roeck | 4667bcb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1370 | 	data->flags = lm90_params[data->kind].flags; | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1371 |  | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1372 | 	/* Set maximum conversion rate */ | 
 | 1373 | 	data->max_convrate = lm90_params[data->kind].max_convrate; | 
 | 1374 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | 	/* Initialize the LM90 chip */ | 
 | 1376 | 	lm90_init_client(new_client); | 
 | 1377 |  | 
 | 1378 | 	/* Register sysfs hooks */ | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1379 | 	err = sysfs_create_group(&new_client->dev.kobj, &lm90_group); | 
 | 1380 | 	if (err) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1381 | 		goto exit_free; | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1382 | 	if (new_client->flags & I2C_CLIENT_PEC) { | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1383 | 		err = device_create_file(&new_client->dev, &dev_attr_pec); | 
 | 1384 | 		if (err) | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1385 | 			goto exit_remove_files; | 
 | 1386 | 	} | 
| Guenter Roeck | 88073bb | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1387 | 	if (data->flags & LM90_HAVE_OFFSET) { | 
| Guenter Roeck | 11e5781 | 2010-10-28 20:31:42 +0200 | [diff] [blame] | 1388 | 		err = device_create_file(&new_client->dev, | 
 | 1389 | 					&sensor_dev_attr_temp2_offset.dev_attr); | 
 | 1390 | 		if (err) | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 1391 | 			goto exit_remove_files; | 
 | 1392 | 	} | 
| Guenter Roeck | 6948708 | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1393 | 	if (data->flags & LM90_HAVE_EMERGENCY) { | 
 | 1394 | 		err = sysfs_create_group(&new_client->dev.kobj, | 
 | 1395 | 					 &lm90_emergency_group); | 
 | 1396 | 		if (err) | 
 | 1397 | 			goto exit_remove_files; | 
 | 1398 | 	} | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1399 | 	if (data->flags & LM90_HAVE_EMERGENCY_ALARM) { | 
 | 1400 | 		err = sysfs_create_group(&new_client->dev.kobj, | 
 | 1401 | 					 &lm90_emergency_alarm_group); | 
 | 1402 | 		if (err) | 
 | 1403 | 			goto exit_remove_files; | 
 | 1404 | 	} | 
 | 1405 | 	if (data->flags & LM90_HAVE_TEMP3) { | 
 | 1406 | 		err = sysfs_create_group(&new_client->dev.kobj, | 
 | 1407 | 					 &lm90_temp3_group); | 
 | 1408 | 		if (err) | 
 | 1409 | 			goto exit_remove_files; | 
 | 1410 | 	} | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1411 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1412 | 	data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
 | 1413 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 1414 | 		err = PTR_ERR(data->hwmon_dev); | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1415 | 		goto exit_remove_files; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1416 | 	} | 
 | 1417 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | 	return 0; | 
 | 1419 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 1420 | exit_remove_files: | 
| Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1421 | 	lm90_remove_files(new_client, data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | exit_free: | 
 | 1423 | 	kfree(data); | 
 | 1424 | exit: | 
 | 1425 | 	return err; | 
 | 1426 | } | 
 | 1427 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 1428 | static int lm90_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1430 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1432 | 	hwmon_device_unregister(data->hwmon_dev); | 
| Guenter Roeck | b6fc1ba | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1433 | 	lm90_remove_files(client, data); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1434 |  | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 1435 | 	/* Restore initial configuration */ | 
| Guenter Roeck | 0c01b64 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1436 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, | 
 | 1437 | 				  data->convrate_orig); | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 1438 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | 
 | 1439 | 				  data->config_orig); | 
 | 1440 |  | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1441 | 	kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | 	return 0; | 
 | 1443 | } | 
 | 1444 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1445 | static void lm90_alert(struct i2c_client *client, unsigned int flag) | 
 | 1446 | { | 
 | 1447 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1448 | 	u8 config, alarms, alarms2 = 0; | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1449 |  | 
 | 1450 | 	lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1451 |  | 
 | 1452 | 	if (data->kind == max6696) | 
 | 1453 | 		lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2); | 
 | 1454 |  | 
 | 1455 | 	if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) { | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1456 | 		dev_info(&client->dev, "Everything OK\n"); | 
 | 1457 | 	} else { | 
 | 1458 | 		if (alarms & 0x61) | 
 | 1459 | 			dev_warn(&client->dev, | 
 | 1460 | 				 "temp%d out of range, please check!\n", 1); | 
 | 1461 | 		if (alarms & 0x1a) | 
 | 1462 | 			dev_warn(&client->dev, | 
 | 1463 | 				 "temp%d out of range, please check!\n", 2); | 
 | 1464 | 		if (alarms & 0x04) | 
 | 1465 | 			dev_warn(&client->dev, | 
 | 1466 | 				 "temp%d diode open, please check!\n", 2); | 
 | 1467 |  | 
| Guenter Roeck | 06e1c0a | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1468 | 		if (alarms2 & 0x18) | 
 | 1469 | 			dev_warn(&client->dev, | 
 | 1470 | 				 "temp%d out of range, please check!\n", 3); | 
 | 1471 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1472 | 		/* Disable ALERT# output, because these chips don't implement | 
 | 1473 | 		  SMBus alert correctly; they should only hold the alert line | 
 | 1474 | 		  low briefly. */ | 
| Guenter Roeck | 1179324 | 2010-10-28 20:31:44 +0200 | [diff] [blame] | 1475 | 		if ((data->flags & LM90_HAVE_BROKEN_ALERT) | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1476 | 		 && (alarms & data->alert_alarms)) { | 
 | 1477 | 			dev_dbg(&client->dev, "Disabling ALERT#\n"); | 
 | 1478 | 			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | 
 | 1479 | 			i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | 
 | 1480 | 						  config | 0x80); | 
 | 1481 | 		} | 
 | 1482 | 	} | 
 | 1483 | } | 
 | 1484 |  | 
| Guenter Roeck | 15b66ab | 2010-10-28 20:31:43 +0200 | [diff] [blame] | 1485 | static struct i2c_driver lm90_driver = { | 
 | 1486 | 	.class		= I2C_CLASS_HWMON, | 
 | 1487 | 	.driver = { | 
 | 1488 | 		.name	= "lm90", | 
 | 1489 | 	}, | 
 | 1490 | 	.probe		= lm90_probe, | 
 | 1491 | 	.remove		= lm90_remove, | 
 | 1492 | 	.alert		= lm90_alert, | 
 | 1493 | 	.id_table	= lm90_id, | 
 | 1494 | 	.detect		= lm90_detect, | 
 | 1495 | 	.address_list	= normal_i2c, | 
 | 1496 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 |  | 
 | 1498 | static int __init sensors_lm90_init(void) | 
 | 1499 | { | 
 | 1500 | 	return i2c_add_driver(&lm90_driver); | 
 | 1501 | } | 
 | 1502 |  | 
 | 1503 | static void __exit sensors_lm90_exit(void) | 
 | 1504 | { | 
 | 1505 | 	i2c_del_driver(&lm90_driver); | 
 | 1506 | } | 
 | 1507 |  | 
 | 1508 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | 
 | 1509 | MODULE_DESCRIPTION("LM90/ADM1032 driver"); | 
 | 1510 | MODULE_LICENSE("GPL"); | 
 | 1511 |  | 
 | 1512 | module_init(sensors_lm90_init); | 
 | 1513 | module_exit(sensors_lm90_exit); |