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