| 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  * variants. The extra address and features of the MAX6659 are not | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 32 |  * supported by this driver. These chips lack the remote temperature | 
 | 33 |  * offset feature. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  * | 
| Darrick J. Wong | 1a51e06 | 2009-03-12 13:36:38 +0100 | [diff] [blame] | 35 |  * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and | 
 | 36 |  * MAX6692 chips made by Maxim.  These are again similar to the LM86, | 
 | 37 |  * but they use unsigned temperature values and can report temperatures | 
 | 38 |  * from 0 to 145 degrees. | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 39 |  * | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 40 |  * This driver also supports the MAX6680 and MAX6681, two other sensor | 
 | 41 |  * chips made by Maxim. These are quite similar to the other Maxim | 
| Jean Delvare | a874a10 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 42 |  * chips. The MAX6680 and MAX6681 only differ in the pinout so they can | 
 | 43 |  * be treated identically. | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 44 |  * | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 45 |  * This driver also supports the ADT7461 chip from Analog Devices. | 
 | 46 |  * It's supported in both compatibility and extended mode. It is mostly | 
 | 47 |  * compatible with LM90 except for a data format difference for the | 
 | 48 |  * temperature value registers. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 |  * | 
 | 50 |  * Since the LM90 was the first chipset supported by this driver, most | 
 | 51 |  * comments will refer to this chipset, but are actually general and | 
 | 52 |  * concern all supported chipsets, unless mentioned otherwise. | 
 | 53 |  * | 
 | 54 |  * This program is free software; you can redistribute it and/or modify | 
 | 55 |  * it under the terms of the GNU General Public License as published by | 
 | 56 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 57 |  * (at your option) any later version. | 
 | 58 |  * | 
 | 59 |  * This program is distributed in the hope that it will be useful, | 
 | 60 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 61 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 62 |  * GNU General Public License for more details. | 
 | 63 |  * | 
 | 64 |  * You should have received a copy of the GNU General Public License | 
 | 65 |  * along with this program; if not, write to the Free Software | 
 | 66 |  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 67 |  */ | 
 | 68 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <linux/module.h> | 
 | 70 | #include <linux/init.h> | 
 | 71 | #include <linux/slab.h> | 
 | 72 | #include <linux/jiffies.h> | 
 | 73 | #include <linux/i2c.h> | 
| Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 74 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 75 | #include <linux/hwmon.h> | 
 | 76 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 77 | #include <linux/mutex.h> | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 78 | #include <linux/sysfs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 |  | 
 | 80 | /* | 
 | 81 |  * Addresses to scan | 
 | 82 |  * Address is fully defined internally and cannot be changed except for | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 83 |  * MAX6659, MAX6680 and MAX6681. | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 84 |  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657 | 
 | 85 |  * and MAX6658 have address 0x4c. | 
 | 86 |  * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d. | 
 | 87 |  * MAX6647 has address 0x4e. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported). | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 89 |  * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, | 
 | 90 |  * 0x4c, 0x4d or 0x4e. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  */ | 
 | 92 |  | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 93 | static const unsigned short normal_i2c[] = { | 
 | 94 | 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 96 | enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646, | 
 | 97 | 	w83l771 }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
 | 99 | /* | 
 | 100 |  * The LM90 registers | 
 | 101 |  */ | 
 | 102 |  | 
 | 103 | #define LM90_REG_R_MAN_ID		0xFE | 
 | 104 | #define LM90_REG_R_CHIP_ID		0xFF | 
 | 105 | #define LM90_REG_R_CONFIG1		0x03 | 
 | 106 | #define LM90_REG_W_CONFIG1		0x09 | 
 | 107 | #define LM90_REG_R_CONFIG2		0xBF | 
 | 108 | #define LM90_REG_W_CONFIG2		0xBF | 
 | 109 | #define LM90_REG_R_CONVRATE		0x04 | 
 | 110 | #define LM90_REG_W_CONVRATE		0x0A | 
 | 111 | #define LM90_REG_R_STATUS		0x02 | 
 | 112 | #define LM90_REG_R_LOCAL_TEMP		0x00 | 
 | 113 | #define LM90_REG_R_LOCAL_HIGH		0x05 | 
 | 114 | #define LM90_REG_W_LOCAL_HIGH		0x0B | 
 | 115 | #define LM90_REG_R_LOCAL_LOW		0x06 | 
 | 116 | #define LM90_REG_W_LOCAL_LOW		0x0C | 
 | 117 | #define LM90_REG_R_LOCAL_CRIT		0x20 | 
 | 118 | #define LM90_REG_W_LOCAL_CRIT		0x20 | 
 | 119 | #define LM90_REG_R_REMOTE_TEMPH		0x01 | 
 | 120 | #define LM90_REG_R_REMOTE_TEMPL		0x10 | 
 | 121 | #define LM90_REG_R_REMOTE_OFFSH		0x11 | 
 | 122 | #define LM90_REG_W_REMOTE_OFFSH		0x11 | 
 | 123 | #define LM90_REG_R_REMOTE_OFFSL		0x12 | 
 | 124 | #define LM90_REG_W_REMOTE_OFFSL		0x12 | 
 | 125 | #define LM90_REG_R_REMOTE_HIGHH		0x07 | 
 | 126 | #define LM90_REG_W_REMOTE_HIGHH		0x0D | 
 | 127 | #define LM90_REG_R_REMOTE_HIGHL		0x13 | 
 | 128 | #define LM90_REG_W_REMOTE_HIGHL		0x13 | 
 | 129 | #define LM90_REG_R_REMOTE_LOWH		0x08 | 
 | 130 | #define LM90_REG_W_REMOTE_LOWH		0x0E | 
 | 131 | #define LM90_REG_R_REMOTE_LOWL		0x14 | 
 | 132 | #define LM90_REG_W_REMOTE_LOWL		0x14 | 
 | 133 | #define LM90_REG_R_REMOTE_CRIT		0x19 | 
 | 134 | #define LM90_REG_W_REMOTE_CRIT		0x19 | 
 | 135 | #define LM90_REG_R_TCRIT_HYST		0x21 | 
 | 136 | #define LM90_REG_W_TCRIT_HYST		0x21 | 
 | 137 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 138 | /* MAX6646/6647/6649/6657/6658/6659 registers */ | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 139 |  | 
 | 140 | #define MAX6657_REG_R_LOCAL_TEMPL	0x11 | 
 | 141 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 143 |  * Device flags | 
 | 144 |  */ | 
 | 145 | #define LM90_FLAG_ADT7461_EXT		0x01	/* ADT7461 extended mode */ | 
 | 146 |  | 
 | 147 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 |  * Functions declaration | 
 | 149 |  */ | 
 | 150 |  | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 151 | static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info); | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 152 | static int lm90_probe(struct i2c_client *client, | 
 | 153 | 		      const struct i2c_device_id *id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | static void lm90_init_client(struct i2c_client *client); | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 155 | static void lm90_alert(struct i2c_client *client, unsigned int flag); | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 156 | static int lm90_remove(struct i2c_client *client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static struct lm90_data *lm90_update_device(struct device *dev); | 
 | 158 |  | 
 | 159 | /* | 
 | 160 |  * Driver data (common to all clients) | 
 | 161 |  */ | 
 | 162 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 163 | static const struct i2c_device_id lm90_id[] = { | 
 | 164 | 	{ "adm1032", adm1032 }, | 
 | 165 | 	{ "adt7461", adt7461 }, | 
 | 166 | 	{ "lm90", lm90 }, | 
 | 167 | 	{ "lm86", lm86 }, | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 168 | 	{ "lm89", lm86 }, | 
 | 169 | 	{ "lm99", lm99 }, | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 170 | 	{ "max6646", max6646 }, | 
 | 171 | 	{ "max6647", max6646 }, | 
 | 172 | 	{ "max6649", max6646 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 173 | 	{ "max6657", max6657 }, | 
 | 174 | 	{ "max6658", max6657 }, | 
 | 175 | 	{ "max6659", max6657 }, | 
 | 176 | 	{ "max6680", max6680 }, | 
 | 177 | 	{ "max6681", max6680 }, | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 178 | 	{ "w83l771", w83l771 }, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 179 | 	{ } | 
 | 180 | }; | 
 | 181 | MODULE_DEVICE_TABLE(i2c, lm90_id); | 
 | 182 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static struct i2c_driver lm90_driver = { | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 184 | 	.class		= I2C_CLASS_HWMON, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 185 | 	.driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 186 | 		.name	= "lm90", | 
 | 187 | 	}, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 188 | 	.probe		= lm90_probe, | 
 | 189 | 	.remove		= lm90_remove, | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 190 | 	.alert		= lm90_alert, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 191 | 	.id_table	= lm90_id, | 
 | 192 | 	.detect		= lm90_detect, | 
| Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 193 | 	.address_list	= normal_i2c, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | }; | 
 | 195 |  | 
 | 196 | /* | 
 | 197 |  * Client data (each client gets its own) | 
 | 198 |  */ | 
 | 199 |  | 
 | 200 | struct lm90_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 201 | 	struct device *hwmon_dev; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 202 | 	struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 	char valid; /* zero until following fields are valid */ | 
 | 204 | 	unsigned long last_updated; /* in jiffies */ | 
 | 205 | 	int kind; | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 206 | 	int flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 |  | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 208 | 	u8 config_orig;		/* Original configuration register value */ | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 209 | 	u8 alert_alarms;	/* Which alarm bits trigger ALERT# */ | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 210 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | 	/* registers values */ | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 212 | 	s8 temp8[4];	/* 0: local low limit | 
 | 213 | 			   1: local high limit | 
 | 214 | 			   2: local critical limit | 
 | 215 | 			   3: remote critical limit */ | 
 | 216 | 	s16 temp11[5];	/* 0: remote input | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 217 | 			   1: remote low limit | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 218 | 			   2: remote high limit | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 219 | 			   3: remote offset (except max6646 and max6657) | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 220 | 			   4: local input */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	u8 temp_hyst; | 
 | 222 | 	u8 alarms; /* bitvector */ | 
 | 223 | }; | 
 | 224 |  | 
 | 225 | /* | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 226 |  * Conversions | 
 | 227 |  * For local temperatures and limits, critical limits and the hysteresis | 
 | 228 |  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius. | 
 | 229 |  * For remote temperatures and limits, it uses signed 11-bit values with | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 230 |  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.  Some | 
 | 231 |  * Maxim chips use unsigned values. | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 232 |  */ | 
 | 233 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 234 | static inline int temp_from_s8(s8 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 235 | { | 
 | 236 | 	return val * 1000; | 
 | 237 | } | 
 | 238 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 239 | static inline int temp_from_u8(u8 val) | 
 | 240 | { | 
 | 241 | 	return val * 1000; | 
 | 242 | } | 
 | 243 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 244 | static inline int temp_from_s16(s16 val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 245 | { | 
 | 246 | 	return val / 32 * 125; | 
 | 247 | } | 
 | 248 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 249 | static inline int temp_from_u16(u16 val) | 
 | 250 | { | 
 | 251 | 	return val / 32 * 125; | 
 | 252 | } | 
 | 253 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 254 | static s8 temp_to_s8(long val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 255 | { | 
 | 256 | 	if (val <= -128000) | 
 | 257 | 		return -128; | 
 | 258 | 	if (val >= 127000) | 
 | 259 | 		return 127; | 
 | 260 | 	if (val < 0) | 
 | 261 | 		return (val - 500) / 1000; | 
 | 262 | 	return (val + 500) / 1000; | 
 | 263 | } | 
 | 264 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 265 | static u8 temp_to_u8(long val) | 
 | 266 | { | 
 | 267 | 	if (val <= 0) | 
 | 268 | 		return 0; | 
 | 269 | 	if (val >= 255000) | 
 | 270 | 		return 255; | 
 | 271 | 	return (val + 500) / 1000; | 
 | 272 | } | 
 | 273 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 274 | static s16 temp_to_s16(long val) | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 275 | { | 
 | 276 | 	if (val <= -128000) | 
 | 277 | 		return 0x8000; | 
 | 278 | 	if (val >= 127875) | 
 | 279 | 		return 0x7FE0; | 
 | 280 | 	if (val < 0) | 
 | 281 | 		return (val - 62) / 125 * 32; | 
 | 282 | 	return (val + 62) / 125 * 32; | 
 | 283 | } | 
 | 284 |  | 
 | 285 | static u8 hyst_to_reg(long val) | 
 | 286 | { | 
 | 287 | 	if (val <= 0) | 
 | 288 | 		return 0; | 
 | 289 | 	if (val >= 30500) | 
 | 290 | 		return 31; | 
 | 291 | 	return (val + 500) / 1000; | 
 | 292 | } | 
 | 293 |  | 
 | 294 | /* | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 295 |  * ADT7461 in compatibility mode is almost identical to LM90 except that | 
 | 296 |  * attempts to write values that are outside the range 0 < temp < 127 are | 
 | 297 |  * treated as the boundary value. | 
 | 298 |  * | 
 | 299 |  * ADT7461 in "extended mode" operation uses unsigned integers offset by | 
 | 300 |  * 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] | 301 |  */ | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 302 | 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] | 303 | { | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 304 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) | 
 | 305 | 		return (val - 64) * 1000; | 
 | 306 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 307 | 		return temp_from_s8(val); | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 308 | } | 
 | 309 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 310 | 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] | 311 | { | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 312 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) | 
 | 313 | 		return (val - 0x4000) / 64 * 250; | 
 | 314 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 315 | 		return temp_from_s16(val); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 316 | } | 
 | 317 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 318 | static u8 temp_to_u8_adt7461(struct lm90_data *data, long val) | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 319 | { | 
 | 320 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) { | 
 | 321 | 		if (val <= -64000) | 
 | 322 | 			return 0; | 
 | 323 | 		if (val >= 191000) | 
 | 324 | 			return 0xFF; | 
 | 325 | 		return (val + 500 + 64000) / 1000; | 
 | 326 | 	} else { | 
 | 327 | 		if (val <= 0) | 
 | 328 | 			return 0; | 
 | 329 | 		if (val >= 127000) | 
 | 330 | 			return 127; | 
 | 331 | 		return (val + 500) / 1000; | 
 | 332 | 	} | 
 | 333 | } | 
 | 334 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 335 | static u16 temp_to_u16_adt7461(struct lm90_data *data, long val) | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 336 | { | 
 | 337 | 	if (data->flags & LM90_FLAG_ADT7461_EXT) { | 
 | 338 | 		if (val <= -64000) | 
 | 339 | 			return 0; | 
 | 340 | 		if (val >= 191750) | 
 | 341 | 			return 0xFFC0; | 
 | 342 | 		return (val + 64000 + 125) / 250 * 64; | 
 | 343 | 	} else { | 
 | 344 | 		if (val <= 0) | 
 | 345 | 			return 0; | 
 | 346 | 		if (val >= 127750) | 
 | 347 | 			return 0x7FC0; | 
 | 348 | 		return (val + 125) / 250 * 64; | 
 | 349 | 	} | 
| Nate Case | cea50fe | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 350 | } | 
 | 351 |  | 
 | 352 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  * Sysfs stuff | 
 | 354 |  */ | 
 | 355 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 356 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, | 
 | 357 | 			  char *buf) | 
 | 358 | { | 
 | 359 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 360 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 361 | 	int temp; | 
 | 362 |  | 
 | 363 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 364 | 		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 365 | 	else if (data->kind == max6646) | 
 | 366 | 		temp = temp_from_u8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 367 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 368 | 		temp = temp_from_s8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 369 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 370 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 371 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 372 | 		temp += 16000; | 
 | 373 |  | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 374 | 	return sprintf(buf, "%d\n", temp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 377 | static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, | 
 | 378 | 			 const char *buf, size_t count) | 
 | 379 | { | 
 | 380 | 	static const u8 reg[4] = { | 
 | 381 | 		LM90_REG_W_LOCAL_LOW, | 
 | 382 | 		LM90_REG_W_LOCAL_HIGH, | 
 | 383 | 		LM90_REG_W_LOCAL_CRIT, | 
 | 384 | 		LM90_REG_W_REMOTE_CRIT, | 
 | 385 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 387 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 388 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 389 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 390 | 	long val = simple_strtol(buf, NULL, 10); | 
 | 391 | 	int nr = attr->index; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 393 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 394 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 395 | 		val -= 16000; | 
 | 396 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 397 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 398 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 399 | 		data->temp8[nr] = temp_to_u8_adt7461(data, val); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 400 | 	else if (data->kind == max6646) | 
 | 401 | 		data->temp8[nr] = temp_to_u8(val); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 402 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 403 | 		data->temp8[nr] = temp_to_s8(val); | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 404 | 	i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 405 | 	mutex_unlock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 406 | 	return count; | 
 | 407 | } | 
 | 408 |  | 
 | 409 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | 
 | 410 | 			   char *buf) | 
 | 411 | { | 
 | 412 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 413 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 414 | 	int temp; | 
 | 415 |  | 
 | 416 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 417 | 		temp = temp_from_u16_adt7461(data, data->temp11[attr->index]); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 418 | 	else if (data->kind == max6646) | 
 | 419 | 		temp = temp_from_u16(data->temp11[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 420 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 421 | 		temp = temp_from_s16(data->temp11[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 422 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 423 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 424 | 	if (data->kind == lm99 &&  attr->index <= 2) | 
 | 425 | 		temp += 16000; | 
 | 426 |  | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 427 | 	return sprintf(buf, "%d\n", temp); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 428 | } | 
 | 429 |  | 
 | 430 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | 
 | 431 | 			  const char *buf, size_t count) | 
 | 432 | { | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 433 | 	static const u8 reg[6] = { | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 434 | 		LM90_REG_W_REMOTE_LOWH, | 
 | 435 | 		LM90_REG_W_REMOTE_LOWL, | 
 | 436 | 		LM90_REG_W_REMOTE_HIGHH, | 
 | 437 | 		LM90_REG_W_REMOTE_HIGHL, | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 438 | 		LM90_REG_W_REMOTE_OFFSH, | 
 | 439 | 		LM90_REG_W_REMOTE_OFFSL, | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 440 | 	}; | 
 | 441 |  | 
 | 442 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 443 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 444 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 445 | 	long val = simple_strtol(buf, NULL, 10); | 
 | 446 | 	int nr = attr->index; | 
 | 447 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 448 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 449 | 	if (data->kind == lm99 && attr->index <= 2) | 
 | 450 | 		val -= 16000; | 
 | 451 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 452 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 453 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 454 | 		data->temp11[nr] = temp_to_u16_adt7461(data, val); | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 455 | 	else if (data->kind == max6657 || data->kind == max6680) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 456 | 		data->temp11[nr] = temp_to_s8(val) << 8; | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 457 | 	else if (data->kind == max6646) | 
 | 458 | 		data->temp11[nr] = temp_to_u8(val) << 8; | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 459 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 460 | 		data->temp11[nr] = temp_to_s16(val); | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 461 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 462 | 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], | 
 | 463 | 				  data->temp11[nr] >> 8); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 464 | 	if (data->kind != max6657 && data->kind != max6680 | 
 | 465 | 	    && data->kind != max6646) | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 466 | 		i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | 
 | 467 | 					  data->temp11[nr] & 0xff); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 468 | 	mutex_unlock(&data->update_lock); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 469 | 	return count; | 
 | 470 | } | 
 | 471 |  | 
 | 472 | static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr, | 
 | 473 | 			     char *buf) | 
 | 474 | { | 
 | 475 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 476 | 	struct lm90_data *data = lm90_update_device(dev); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 477 | 	int temp; | 
 | 478 |  | 
 | 479 | 	if (data->kind == adt7461) | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 480 | 		temp = temp_from_u8_adt7461(data, data->temp8[attr->index]); | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 481 | 	else if (data->kind == max6646) | 
 | 482 | 		temp = temp_from_u8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 483 | 	else | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 484 | 		temp = temp_from_s8(data->temp8[attr->index]); | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 485 |  | 
| Jean Delvare | 97ae60b | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 486 | 	/* +16 degrees offset for temp2 for the LM99 */ | 
 | 487 | 	if (data->kind == lm99 && attr->index == 3) | 
 | 488 | 		temp += 16000; | 
 | 489 |  | 
| Ben Hutchings | 9d4d383 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 490 | 	return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst)); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 491 | } | 
 | 492 |  | 
 | 493 | static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy, | 
 | 494 | 			    const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { | 
 | 496 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 497 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 498 | 	long val = simple_strtol(buf, NULL, 10); | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 499 | 	int temp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 501 | 	mutex_lock(&data->update_lock); | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 502 | 	if (data->kind == adt7461) | 
 | 503 | 		temp = temp_from_u8_adt7461(data, data->temp8[2]); | 
 | 504 | 	else if (data->kind == max6646) | 
 | 505 | 		temp = temp_from_u8(data->temp8[2]); | 
 | 506 | 	else | 
 | 507 | 		temp = temp_from_s8(data->temp8[2]); | 
 | 508 |  | 
 | 509 | 	data->temp_hyst = hyst_to_reg(temp - val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST, | 
| Jean Delvare | ec38fa2 | 2008-10-26 17:04:39 +0100 | [diff] [blame] | 511 | 				  data->temp_hyst); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 512 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | 	return count; | 
 | 514 | } | 
 | 515 |  | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 516 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | 
 | 517 | 			   char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { | 
 | 519 | 	struct lm90_data *data = lm90_update_device(dev); | 
 | 520 | 	return sprintf(buf, "%d\n", data->alarms); | 
 | 521 | } | 
 | 522 |  | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 523 | static ssize_t show_alarm(struct device *dev, struct device_attribute | 
 | 524 | 			  *devattr, char *buf) | 
 | 525 | { | 
 | 526 | 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
 | 527 | 	struct lm90_data *data = lm90_update_device(dev); | 
 | 528 | 	int bitnr = attr->index; | 
 | 529 |  | 
 | 530 | 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1); | 
 | 531 | } | 
 | 532 |  | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 533 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 534 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); | 
 | 535 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 536 | 	set_temp8, 0); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 537 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | 
 | 538 | 	set_temp11, 1); | 
 | 539 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 540 | 	set_temp8, 1); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 541 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | 
 | 542 | 	set_temp11, 2); | 
 | 543 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 544 | 	set_temp8, 2); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 545 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8, | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 546 | 	set_temp8, 3); | 
| Jean Delvare | 30d7394 | 2005-06-05 21:27:28 +0200 | [diff] [blame] | 547 | 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] | 548 | 	set_temphyst, 2); | 
 | 549 | static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3); | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 550 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, | 
 | 551 | 	set_temp11, 3); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 552 |  | 
 | 553 | /* Individual alarm files */ | 
 | 554 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0); | 
 | 555 | 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] | 556 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | 
| Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 557 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | 
 | 558 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | 
 | 559 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | 
 | 560 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 
 | 561 | /* Raw alarm file for compatibility */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
 | 563 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 564 | static struct attribute *lm90_attributes[] = { | 
 | 565 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 566 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
 | 567 | 	&sensor_dev_attr_temp1_min.dev_attr.attr, | 
 | 568 | 	&sensor_dev_attr_temp2_min.dev_attr.attr, | 
 | 569 | 	&sensor_dev_attr_temp1_max.dev_attr.attr, | 
 | 570 | 	&sensor_dev_attr_temp2_max.dev_attr.attr, | 
 | 571 | 	&sensor_dev_attr_temp1_crit.dev_attr.attr, | 
 | 572 | 	&sensor_dev_attr_temp2_crit.dev_attr.attr, | 
 | 573 | 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, | 
 | 574 | 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, | 
 | 575 |  | 
 | 576 | 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
 | 577 | 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, | 
| Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 578 | 	&sensor_dev_attr_temp2_fault.dev_attr.attr, | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 579 | 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | 
 | 580 | 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
 | 581 | 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | 
 | 582 | 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
 | 583 | 	&dev_attr_alarms.attr, | 
 | 584 | 	NULL | 
 | 585 | }; | 
 | 586 |  | 
 | 587 | static const struct attribute_group lm90_group = { | 
 | 588 | 	.attrs = lm90_attributes, | 
 | 589 | }; | 
 | 590 |  | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 591 | /* pec used for ADM1032 only */ | 
 | 592 | static ssize_t show_pec(struct device *dev, struct device_attribute *dummy, | 
 | 593 | 			char *buf) | 
 | 594 | { | 
 | 595 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 596 | 	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); | 
 | 597 | } | 
 | 598 |  | 
 | 599 | static ssize_t set_pec(struct device *dev, struct device_attribute *dummy, | 
 | 600 | 		       const char *buf, size_t count) | 
 | 601 | { | 
 | 602 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 603 | 	long val = simple_strtol(buf, NULL, 10); | 
 | 604 |  | 
 | 605 | 	switch (val) { | 
 | 606 | 	case 0: | 
 | 607 | 		client->flags &= ~I2C_CLIENT_PEC; | 
 | 608 | 		break; | 
 | 609 | 	case 1: | 
 | 610 | 		client->flags |= I2C_CLIENT_PEC; | 
 | 611 | 		break; | 
 | 612 | 	default: | 
 | 613 | 		return -EINVAL; | 
 | 614 | 	} | 
 | 615 |  | 
 | 616 | 	return count; | 
 | 617 | } | 
 | 618 |  | 
 | 619 | static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec); | 
 | 620 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | /* | 
 | 622 |  * Real code | 
 | 623 |  */ | 
 | 624 |  | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 625 | /* The ADM1032 supports PEC but not on write byte transactions, so we need | 
| Jean Delvare | 0966415 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 626 |    to explicitly ask for a transaction without PEC. */ | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 627 | static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value) | 
 | 628 | { | 
 | 629 | 	return i2c_smbus_xfer(client->adapter, client->addr, | 
 | 630 | 			      client->flags & ~I2C_CLIENT_PEC, | 
 | 631 | 			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); | 
 | 632 | } | 
 | 633 |  | 
 | 634 | /* It is assumed that client->update_lock is held (unless we are in | 
 | 635 |    detection or initialization steps). This matters when PEC is enabled, | 
 | 636 |    because we don't want the address pointer to change between the write | 
 | 637 |    byte and the read byte transactions. */ | 
| Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 638 | static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value) | 
 | 639 | { | 
 | 640 | 	int err; | 
 | 641 |  | 
| Jean Delvare | c3df580 | 2005-10-26 21:39:40 +0200 | [diff] [blame] | 642 |  	if (client->flags & I2C_CLIENT_PEC) { | 
 | 643 |  		err = adm1032_write_byte(client, reg); | 
 | 644 |  		if (err >= 0) | 
 | 645 |  			err = i2c_smbus_read_byte(client); | 
 | 646 |  	} else | 
 | 647 |  		err = i2c_smbus_read_byte_data(client, reg); | 
| Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 648 |  | 
 | 649 | 	if (err < 0) { | 
 | 650 | 		dev_warn(&client->dev, "Register %#02x read failed (%d)\n", | 
 | 651 | 			 reg, err); | 
 | 652 | 		return err; | 
 | 653 | 	} | 
 | 654 | 	*value = err; | 
 | 655 |  | 
 | 656 | 	return 0; | 
 | 657 | } | 
 | 658 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 659 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
| Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 660 | static int lm90_detect(struct i2c_client *new_client, | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 661 | 		       struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 663 | 	struct i2c_adapter *adapter = new_client->adapter; | 
 | 664 | 	int address = new_client->addr; | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 665 | 	const char *name = NULL; | 
 | 666 | 	int man_id, chip_id, reg_config1, reg_convrate; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 |  | 
 | 668 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 669 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 671 | 	/* detection and identification */ | 
 | 672 | 	if ((man_id = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 673 | 						LM90_REG_R_MAN_ID)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 674 | 	 || (chip_id = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 675 | 						LM90_REG_R_CHIP_ID)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 676 | 	 || (reg_config1 = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 677 | 						LM90_REG_R_CONFIG1)) < 0 | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 678 | 	 || (reg_convrate = i2c_smbus_read_byte_data(new_client, | 
| Jean Delvare | e0ae87a | 2007-11-25 21:58:21 +0100 | [diff] [blame] | 679 | 						LM90_REG_R_CONVRATE)) < 0) | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 680 | 		return -ENODEV; | 
 | 681 |  | 
 | 682 | 	if ((address == 0x4C || address == 0x4D) | 
 | 683 | 	 && man_id == 0x01) { /* National Semiconductor */ | 
 | 684 | 		int reg_config2; | 
 | 685 |  | 
 | 686 | 		reg_config2 = i2c_smbus_read_byte_data(new_client, | 
 | 687 | 						LM90_REG_R_CONFIG2); | 
 | 688 | 		if (reg_config2 < 0) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 689 | 			return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 691 | 		if ((reg_config1 & 0x2A) == 0x00 | 
 | 692 | 		 && (reg_config2 & 0xF8) == 0x00 | 
 | 693 | 		 && reg_convrate <= 0x09) { | 
 | 694 | 			if (address == 0x4C | 
 | 695 | 			 && (chip_id & 0xF0) == 0x20) { /* LM90 */ | 
 | 696 | 				name = "lm90"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | 			} else | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 698 | 			if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */ | 
 | 699 | 				name = "lm99"; | 
 | 700 | 				dev_info(&adapter->dev, | 
 | 701 | 					 "Assuming LM99 chip at 0x%02x\n", | 
 | 702 | 					 address); | 
 | 703 | 				dev_info(&adapter->dev, | 
 | 704 | 					 "If it is an LM89, instantiate it " | 
 | 705 | 					 "with the new_device sysfs " | 
 | 706 | 					 "interface\n"); | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 707 | 			} else | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 708 | 			if (address == 0x4C | 
 | 709 | 			 && (chip_id & 0xF0) == 0x10) { /* LM86 */ | 
 | 710 | 				name = "lm86"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | 			} | 
 | 712 | 		} | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 713 | 	} else | 
 | 714 | 	if ((address == 0x4C || address == 0x4D) | 
 | 715 | 	 && man_id == 0x41) { /* Analog Devices */ | 
 | 716 | 		if ((chip_id & 0xF0) == 0x40 /* ADM1032 */ | 
 | 717 | 		 && (reg_config1 & 0x3F) == 0x00 | 
 | 718 | 		 && reg_convrate <= 0x0A) { | 
 | 719 | 			name = "adm1032"; | 
 | 720 | 			/* The ADM1032 supports PEC, but only if combined | 
 | 721 | 			   transactions are not used. */ | 
 | 722 | 			if (i2c_check_functionality(adapter, | 
 | 723 | 						    I2C_FUNC_SMBUS_BYTE)) | 
 | 724 | 				info->flags |= I2C_CLIENT_PEC; | 
 | 725 | 		} else | 
 | 726 | 		if (chip_id == 0x51 /* ADT7461 */ | 
 | 727 | 		 && (reg_config1 & 0x1B) == 0x00 | 
 | 728 | 		 && reg_convrate <= 0x0A) { | 
 | 729 | 			name = "adt7461"; | 
 | 730 | 		} | 
 | 731 | 	} else | 
 | 732 | 	if (man_id == 0x4D) { /* Maxim */ | 
 | 733 | 		/* | 
 | 734 | 		 * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id | 
 | 735 | 		 * register. Reading from that address will return the last | 
 | 736 | 		 * read value, which in our case is those of the man_id | 
 | 737 | 		 * register. Likewise, the config1 register seems to lack a | 
 | 738 | 		 * low nibble, so the value will be those of the previous | 
 | 739 | 		 * read, so in our case those of the man_id register. | 
 | 740 | 		 */ | 
 | 741 | 		if (chip_id == man_id | 
 | 742 | 		 && (address == 0x4C || address == 0x4D) | 
 | 743 | 		 && (reg_config1 & 0x1F) == (man_id & 0x0F) | 
 | 744 | 		 && reg_convrate <= 0x09) { | 
 | 745 | 			name = "max6657"; | 
 | 746 | 		} else | 
 | 747 | 		/* | 
 | 748 | 		 * The chip_id register of the MAX6680 and MAX6681 holds the | 
 | 749 | 		 * revision of the chip. The lowest bit of the config1 register | 
 | 750 | 		 * is unused and should return zero when read, so should the | 
 | 751 | 		 * second to last bit of config1 (software reset). | 
 | 752 | 		 */ | 
 | 753 | 		if (chip_id == 0x01 | 
 | 754 | 		 && (reg_config1 & 0x03) == 0x00 | 
 | 755 | 		 && reg_convrate <= 0x07) { | 
 | 756 | 			name = "max6680"; | 
 | 757 | 		} else | 
 | 758 | 		/* | 
 | 759 | 		 * The chip_id register of the MAX6646/6647/6649 holds the | 
 | 760 | 		 * revision of the chip. The lowest 6 bits of the config1 | 
 | 761 | 		 * register are unused and should return zero when read. | 
 | 762 | 		 */ | 
 | 763 | 		if (chip_id == 0x59 | 
 | 764 | 		 && (reg_config1 & 0x3f) == 0x00 | 
 | 765 | 		 && reg_convrate <= 0x07) { | 
 | 766 | 			name = "max6646"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | 		} | 
| Jean Delvare | 6771ea1 | 2010-03-05 22:17:13 +0100 | [diff] [blame] | 768 | 	} else | 
 | 769 | 	if (address == 0x4C | 
 | 770 | 	 && man_id == 0x5C) { /* Winbond/Nuvoton */ | 
 | 771 | 		if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */ | 
 | 772 | 		 && (reg_config1 & 0x2A) == 0x00 | 
 | 773 | 		 && reg_convrate <= 0x08) { | 
 | 774 | 			name = "w83l771"; | 
 | 775 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | 	} | 
 | 777 |  | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 778 | 	if (!name) { /* identification failed */ | 
 | 779 | 		dev_dbg(&adapter->dev, | 
 | 780 | 			"Unsupported chip at 0x%02x (man_id=0x%02X, " | 
 | 781 | 			"chip_id=0x%02X)\n", address, man_id, chip_id); | 
 | 782 | 		return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 	} | 
| Jean Delvare | 8f2fa77 | 2009-12-09 20:35:53 +0100 | [diff] [blame] | 784 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 785 | 	strlcpy(info->type, name, I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 787 | 	return 0; | 
 | 788 | } | 
 | 789 |  | 
 | 790 | static int lm90_probe(struct i2c_client *new_client, | 
 | 791 | 		      const struct i2c_device_id *id) | 
 | 792 | { | 
 | 793 | 	struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent); | 
 | 794 | 	struct lm90_data *data; | 
 | 795 | 	int err; | 
 | 796 |  | 
 | 797 | 	data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL); | 
 | 798 | 	if (!data) { | 
 | 799 | 		err = -ENOMEM; | 
 | 800 | 		goto exit; | 
 | 801 | 	} | 
 | 802 | 	i2c_set_clientdata(new_client, data); | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 803 | 	mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 805 | 	/* Set the device type */ | 
 | 806 | 	data->kind = id->driver_data; | 
 | 807 | 	if (data->kind == adm1032) { | 
 | 808 | 		if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | 
 | 809 | 			new_client->flags &= ~I2C_CLIENT_PEC; | 
 | 810 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 812 | 	/* Different devices have different alarm bits triggering the | 
 | 813 | 	 * ALERT# output */ | 
 | 814 | 	switch (data->kind) { | 
 | 815 | 	case lm90: | 
 | 816 | 	case lm99: | 
 | 817 | 	case lm86: | 
 | 818 | 		data->alert_alarms = 0x7b; | 
 | 819 | 		break; | 
 | 820 | 	default: | 
 | 821 | 		data->alert_alarms = 0x7c; | 
 | 822 | 		break; | 
 | 823 | 	} | 
 | 824 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | 	/* Initialize the LM90 chip */ | 
 | 826 | 	lm90_init_client(new_client); | 
 | 827 |  | 
 | 828 | 	/* Register sysfs hooks */ | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 829 | 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group))) | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 830 | 		goto exit_free; | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 831 | 	if (new_client->flags & I2C_CLIENT_PEC) { | 
 | 832 | 		if ((err = device_create_file(&new_client->dev, | 
 | 833 | 					      &dev_attr_pec))) | 
 | 834 | 			goto exit_remove_files; | 
 | 835 | 	} | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 836 | 	if (data->kind != max6657 && data->kind != max6646) { | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 837 | 		if ((err = device_create_file(&new_client->dev, | 
 | 838 | 				&sensor_dev_attr_temp2_offset.dev_attr))) | 
 | 839 | 			goto exit_remove_files; | 
 | 840 | 	} | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 841 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 842 | 	data->hwmon_dev = hwmon_device_register(&new_client->dev); | 
 | 843 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 844 | 		err = PTR_ERR(data->hwmon_dev); | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 845 | 		goto exit_remove_files; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 846 | 	} | 
 | 847 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | 	return 0; | 
 | 849 |  | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 850 | exit_remove_files: | 
 | 851 | 	sysfs_remove_group(&new_client->dev.kobj, &lm90_group); | 
 | 852 | 	device_remove_file(&new_client->dev, &dev_attr_pec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | exit_free: | 
 | 854 | 	kfree(data); | 
 | 855 | exit: | 
 | 856 | 	return err; | 
 | 857 | } | 
 | 858 |  | 
 | 859 | static void lm90_init_client(struct i2c_client *client) | 
 | 860 | { | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 861 | 	u8 config; | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 862 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 |  | 
 | 864 | 	/* | 
 | 865 | 	 * Start the conversions. | 
 | 866 | 	 */ | 
 | 867 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE, | 
 | 868 | 				  5); /* 2 Hz */ | 
| Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 869 | 	if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) { | 
 | 870 | 		dev_warn(&client->dev, "Initialization failed!\n"); | 
 | 871 | 		return; | 
 | 872 | 	} | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 873 | 	data->config_orig = config; | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 874 |  | 
| Nate Case | 23b2d47 | 2008-10-17 17:51:10 +0200 | [diff] [blame] | 875 | 	/* Check Temperature Range Select */ | 
 | 876 | 	if (data->kind == adt7461) { | 
 | 877 | 		if (config & 0x04) | 
 | 878 | 			data->flags |= LM90_FLAG_ADT7461_EXT; | 
 | 879 | 	} | 
 | 880 |  | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 881 | 	/* | 
 | 882 | 	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10, | 
 | 883 | 	 * 0.125 degree resolution) and range (0x08, extend range | 
 | 884 | 	 * to -64 degree) mode for the remote temperature sensor. | 
 | 885 | 	 */ | 
 | 886 | 	if (data->kind == max6680) { | 
 | 887 | 		config |= 0x18; | 
 | 888 | 	} | 
 | 889 |  | 
 | 890 | 	config &= 0xBF;	/* run */ | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 891 | 	if (config != data->config_orig) /* Only write if changed */ | 
| Rainer Birkenmaier | 32c82a9 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 892 | 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } | 
 | 894 |  | 
| Jean Delvare | 9b0e852 | 2008-07-16 19:30:15 +0200 | [diff] [blame] | 895 | static int lm90_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 897 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 899 | 	hwmon_device_unregister(data->hwmon_dev); | 
| Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 900 | 	sysfs_remove_group(&client->dev.kobj, &lm90_group); | 
 | 901 | 	device_remove_file(&client->dev, &dev_attr_pec); | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 902 | 	if (data->kind != max6657 && data->kind != max6646) | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 903 | 		device_remove_file(&client->dev, | 
 | 904 | 				   &sensor_dev_attr_temp2_offset.dev_attr); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 905 |  | 
| Jean Delvare | 9523836 | 2010-03-05 22:17:14 +0100 | [diff] [blame] | 906 | 	/* Restore initial configuration */ | 
 | 907 | 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | 
 | 908 | 				  data->config_orig); | 
 | 909 |  | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 910 | 	kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | 	return 0; | 
 | 912 | } | 
 | 913 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 914 | static void lm90_alert(struct i2c_client *client, unsigned int flag) | 
 | 915 | { | 
 | 916 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 917 | 	u8 config, alarms; | 
 | 918 |  | 
 | 919 | 	lm90_read_reg(client, LM90_REG_R_STATUS, &alarms); | 
 | 920 | 	if ((alarms & 0x7f) == 0) { | 
 | 921 | 		dev_info(&client->dev, "Everything OK\n"); | 
 | 922 | 	} else { | 
 | 923 | 		if (alarms & 0x61) | 
 | 924 | 			dev_warn(&client->dev, | 
 | 925 | 				 "temp%d out of range, please check!\n", 1); | 
 | 926 | 		if (alarms & 0x1a) | 
 | 927 | 			dev_warn(&client->dev, | 
 | 928 | 				 "temp%d out of range, please check!\n", 2); | 
 | 929 | 		if (alarms & 0x04) | 
 | 930 | 			dev_warn(&client->dev, | 
 | 931 | 				 "temp%d diode open, please check!\n", 2); | 
 | 932 |  | 
 | 933 | 		/* Disable ALERT# output, because these chips don't implement | 
 | 934 | 		  SMBus alert correctly; they should only hold the alert line | 
 | 935 | 		  low briefly. */ | 
 | 936 | 		if ((data->kind == adm1032 || data->kind == adt7461) | 
 | 937 | 		 && (alarms & data->alert_alarms)) { | 
 | 938 | 			dev_dbg(&client->dev, "Disabling ALERT#\n"); | 
 | 939 | 			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | 
 | 940 | 			i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, | 
 | 941 | 						  config | 0x80); | 
 | 942 | 		} | 
 | 943 | 	} | 
 | 944 | } | 
 | 945 |  | 
| Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 946 | static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value) | 
 | 947 | { | 
 | 948 | 	int err; | 
 | 949 | 	u8 oldh, newh, l; | 
 | 950 |  | 
 | 951 | 	/* | 
 | 952 | 	 * There is a trick here. We have to read two registers to have the | 
 | 953 | 	 * sensor temperature, but we have to beware a conversion could occur | 
 | 954 | 	 * inbetween the readings. The datasheet says we should either use | 
 | 955 | 	 * the one-shot conversion register, which we don't want to do | 
 | 956 | 	 * (disables hardware monitoring) or monitor the busy bit, which is | 
 | 957 | 	 * impossible (we can't read the values and monitor that bit at the | 
 | 958 | 	 * exact same time). So the solution used here is to read the high | 
 | 959 | 	 * byte once, then the low byte, then the high byte again. If the new | 
 | 960 | 	 * high byte matches the old one, then we have a valid reading. Else | 
 | 961 | 	 * we have to read the low byte again, and now we believe we have a | 
 | 962 | 	 * correct reading. | 
 | 963 | 	 */ | 
 | 964 | 	if ((err = lm90_read_reg(client, regh, &oldh)) | 
 | 965 | 	 || (err = lm90_read_reg(client, regl, &l)) | 
 | 966 | 	 || (err = lm90_read_reg(client, regh, &newh))) | 
 | 967 | 		return err; | 
 | 968 | 	if (oldh != newh) { | 
 | 969 | 		err = lm90_read_reg(client, regl, &l); | 
 | 970 | 		if (err) | 
 | 971 | 			return err; | 
 | 972 | 	} | 
 | 973 | 	*value = (newh << 8) | l; | 
 | 974 |  | 
 | 975 | 	return 0; | 
 | 976 | } | 
 | 977 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | static struct lm90_data *lm90_update_device(struct device *dev) | 
 | 979 | { | 
 | 980 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 981 | 	struct lm90_data *data = i2c_get_clientdata(client); | 
 | 982 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 983 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 |  | 
| Ira W. Snyder | 8c3c7a2 | 2010-05-27 19:58:44 +0200 | [diff] [blame] | 985 | 	if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10) | 
 | 986 | 	 || !data->valid) { | 
| Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 987 | 		u8 h, l; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 |  | 
 | 989 | 		dev_dbg(&client->dev, "Updating lm90 data.\n"); | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 990 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]); | 
 | 991 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]); | 
 | 992 | 		lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]); | 
 | 993 | 		lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]); | 
| Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 994 | 		lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 996 | 		if (data->kind == max6657 || data->kind == max6646) { | 
| Jean Delvare | f65e170 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 997 | 			lm90_read16(client, LM90_REG_R_LOCAL_TEMP, | 
 | 998 | 				    MAX6657_REG_R_LOCAL_TEMPL, | 
 | 999 | 				    &data->temp11[4]); | 
 | 1000 | 		} else { | 
 | 1001 | 			if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, | 
 | 1002 | 					  &h) == 0) | 
 | 1003 | 				data->temp11[4] = h << 8; | 
 | 1004 | 		} | 
| Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1005 | 		lm90_read16(client, LM90_REG_R_REMOTE_TEMPH, | 
 | 1006 | 			    LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 |  | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1008 | 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) { | 
 | 1009 | 			data->temp11[1] = h << 8; | 
 | 1010 | 			if (data->kind != max6657 && data->kind != max6680 | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 1011 | 			 && data->kind != max6646 | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1012 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, | 
 | 1013 | 					  &l) == 0) | 
 | 1014 | 				data->temp11[1] |= l; | 
 | 1015 | 		} | 
 | 1016 | 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) { | 
 | 1017 | 			data->temp11[2] = h << 8; | 
 | 1018 | 			if (data->kind != max6657 && data->kind != max6680 | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 1019 | 			 && data->kind != max6646 | 
| Jean Delvare | 5f502a8 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1020 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, | 
 | 1021 | 					  &l) == 0) | 
 | 1022 | 				data->temp11[2] |= l; | 
 | 1023 | 		} | 
 | 1024 |  | 
| Ben Hutchings | 271dabf | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 1025 | 		if (data->kind != max6657 && data->kind != max6646) { | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 1026 | 			if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH, | 
| Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1027 | 					  &h) == 0 | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 1028 | 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL, | 
 | 1029 | 					  &l) == 0) | 
| Jean Delvare | 6388a38 | 2008-10-17 17:51:09 +0200 | [diff] [blame] | 1030 | 				data->temp11[3] = (h << 8) | l; | 
| Jean Delvare | 69f2f96 | 2007-09-05 14:15:37 +0200 | [diff] [blame] | 1031 | 		} | 
| Jean Delvare | 8256fe0 | 2005-10-26 21:37:52 +0200 | [diff] [blame] | 1032 | 		lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 |  | 
| Jean Delvare | 53de334 | 2010-03-05 22:17:15 +0100 | [diff] [blame] | 1034 | 		/* Re-enable ALERT# output if it was originally enabled and | 
 | 1035 | 		 * relevant alarms are all clear */ | 
 | 1036 | 		if ((data->config_orig & 0x80) == 0 | 
 | 1037 | 		 && (data->alarms & data->alert_alarms) == 0) { | 
 | 1038 | 			u8 config; | 
 | 1039 |  | 
 | 1040 | 			lm90_read_reg(client, LM90_REG_R_CONFIG1, &config); | 
 | 1041 | 			if (config & 0x80) { | 
 | 1042 | 				dev_dbg(&client->dev, "Re-enabling ALERT#\n"); | 
 | 1043 | 				i2c_smbus_write_byte_data(client, | 
 | 1044 | 							  LM90_REG_W_CONFIG1, | 
 | 1045 | 							  config & ~0x80); | 
 | 1046 | 			} | 
 | 1047 | 		} | 
 | 1048 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | 		data->last_updated = jiffies; | 
 | 1050 | 		data->valid = 1; | 
 | 1051 | 	} | 
 | 1052 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1053 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 |  | 
 | 1055 | 	return data; | 
 | 1056 | } | 
 | 1057 |  | 
 | 1058 | static int __init sensors_lm90_init(void) | 
 | 1059 | { | 
 | 1060 | 	return i2c_add_driver(&lm90_driver); | 
 | 1061 | } | 
 | 1062 |  | 
 | 1063 | static void __exit sensors_lm90_exit(void) | 
 | 1064 | { | 
 | 1065 | 	i2c_del_driver(&lm90_driver); | 
 | 1066 | } | 
 | 1067 |  | 
 | 1068 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | 
 | 1069 | MODULE_DESCRIPTION("LM90/ADM1032 driver"); | 
 | 1070 | MODULE_LICENSE("GPL"); | 
 | 1071 |  | 
 | 1072 | module_init(sensors_lm90_init); | 
 | 1073 | module_exit(sensors_lm90_exit); |