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