Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm63.c - driver for the National Semiconductor LM63 temperature sensor |
| 3 | * with integrated fan control |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2006 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Based on the lm90 driver. |
| 6 | * |
| 7 | * The LM63 is a sensor chip made by National Semiconductor. It measures |
| 8 | * two temperatures (its own and one external one) and the speed of one |
| 9 | * fan, those speed it can additionally control. Complete datasheet can be |
| 10 | * obtained from National's website at: |
| 11 | * http://www.national.com/pf/LM/LM63.html |
| 12 | * |
| 13 | * The LM63 is basically an LM86 with fan speed monitoring and control |
| 14 | * capabilities added. It misses some of the LM86 features though: |
| 15 | * - No low limit for local temperature. |
| 16 | * - No critical limit for local temperature. |
| 17 | * - Critical limit for remote temperature can be changed only once. We |
| 18 | * will consider that the critical limit is read-only. |
| 19 | * |
| 20 | * The datasheet isn't very clear about what the tachometer reading is. |
| 21 | * I had a explanation from National Semiconductor though. The two lower |
| 22 | * bits of the read value have to be masked out. The value is still 16 bit |
| 23 | * in width. |
| 24 | * |
| 25 | * This program is free software; you can redistribute it and/or modify |
| 26 | * it under the terms of the GNU General Public License as published by |
| 27 | * the Free Software Foundation; either version 2 of the License, or |
| 28 | * (at your option) any later version. |
| 29 | * |
| 30 | * This program is distributed in the hope that it will be useful, |
| 31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 | * GNU General Public License for more details. |
| 34 | * |
| 35 | * You should have received a copy of the GNU General Public License |
| 36 | * along with this program; if not, write to the Free Software |
| 37 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 38 | */ |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/module.h> |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/jiffies.h> |
| 44 | #include <linux/i2c.h> |
Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 45 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 46 | #include <linux/hwmon.h> |
| 47 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 48 | #include <linux/mutex.h> |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 49 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Addresses to scan |
| 53 | * Address is fully defined internally and cannot be changed. |
| 54 | */ |
| 55 | |
| 56 | static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Insmod parameters |
| 60 | */ |
| 61 | |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 62 | I2C_CLIENT_INSMOD_1(lm63); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * The LM63 registers |
| 66 | */ |
| 67 | |
| 68 | #define LM63_REG_CONFIG1 0x03 |
| 69 | #define LM63_REG_CONFIG2 0xBF |
| 70 | #define LM63_REG_CONFIG_FAN 0x4A |
| 71 | |
| 72 | #define LM63_REG_TACH_COUNT_MSB 0x47 |
| 73 | #define LM63_REG_TACH_COUNT_LSB 0x46 |
| 74 | #define LM63_REG_TACH_LIMIT_MSB 0x49 |
| 75 | #define LM63_REG_TACH_LIMIT_LSB 0x48 |
| 76 | |
| 77 | #define LM63_REG_PWM_VALUE 0x4C |
| 78 | #define LM63_REG_PWM_FREQ 0x4D |
| 79 | |
| 80 | #define LM63_REG_LOCAL_TEMP 0x00 |
| 81 | #define LM63_REG_LOCAL_HIGH 0x05 |
| 82 | |
| 83 | #define LM63_REG_REMOTE_TEMP_MSB 0x01 |
| 84 | #define LM63_REG_REMOTE_TEMP_LSB 0x10 |
| 85 | #define LM63_REG_REMOTE_OFFSET_MSB 0x11 |
| 86 | #define LM63_REG_REMOTE_OFFSET_LSB 0x12 |
| 87 | #define LM63_REG_REMOTE_HIGH_MSB 0x07 |
| 88 | #define LM63_REG_REMOTE_HIGH_LSB 0x13 |
| 89 | #define LM63_REG_REMOTE_LOW_MSB 0x08 |
| 90 | #define LM63_REG_REMOTE_LOW_LSB 0x14 |
| 91 | #define LM63_REG_REMOTE_TCRIT 0x19 |
| 92 | #define LM63_REG_REMOTE_TCRIT_HYST 0x21 |
| 93 | |
| 94 | #define LM63_REG_ALERT_STATUS 0x02 |
| 95 | #define LM63_REG_ALERT_MASK 0x16 |
| 96 | |
| 97 | #define LM63_REG_MAN_ID 0xFE |
| 98 | #define LM63_REG_CHIP_ID 0xFF |
| 99 | |
| 100 | /* |
| 101 | * Conversions and various macros |
| 102 | * For tachometer counts, the LM63 uses 16-bit values. |
| 103 | * For local temperature and high limit, remote critical limit and hysteresis |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 104 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * For remote temperature, low and high limits, it uses signed 11-bit values |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 106 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
| 108 | |
| 109 | #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ |
| 110 | 5400000 / (reg)) |
| 111 | #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ |
| 112 | (5400000 / (val)) & 0xFFFC) |
| 113 | #define TEMP8_FROM_REG(reg) ((reg) * 1000) |
| 114 | #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 115 | (val) >= 127000 ? 127 : \ |
| 116 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 117 | ((val) + 500) / 1000) |
| 118 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) |
| 119 | #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ |
| 120 | (val) >= 127875 ? 0x7FE0 : \ |
| 121 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ |
| 122 | ((val) + 62) / 125 * 32) |
| 123 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ |
| 124 | (val) >= 127000 ? 127 : \ |
| 125 | ((val) + 500) / 1000) |
| 126 | |
| 127 | /* |
| 128 | * Functions declaration |
| 129 | */ |
| 130 | |
| 131 | static int lm63_attach_adapter(struct i2c_adapter *adapter); |
| 132 | static int lm63_detach_client(struct i2c_client *client); |
| 133 | |
| 134 | static struct lm63_data *lm63_update_device(struct device *dev); |
| 135 | |
| 136 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind); |
| 137 | static void lm63_init_client(struct i2c_client *client); |
| 138 | |
| 139 | /* |
| 140 | * Driver data (common to all clients) |
| 141 | */ |
| 142 | |
| 143 | static struct i2c_driver lm63_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 144 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 145 | .name = "lm63", |
| 146 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | .attach_adapter = lm63_attach_adapter, |
| 148 | .detach_client = lm63_detach_client, |
| 149 | }; |
| 150 | |
| 151 | /* |
| 152 | * Client data (each client gets its own) |
| 153 | */ |
| 154 | |
| 155 | struct lm63_data { |
| 156 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 157 | struct class_device *class_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 158 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | char valid; /* zero until following fields are valid */ |
| 160 | unsigned long last_updated; /* in jiffies */ |
| 161 | |
| 162 | /* registers values */ |
| 163 | u8 config, config_fan; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 164 | u16 fan[2]; /* 0: input |
| 165 | 1: low limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | u8 pwm1_freq; |
| 167 | u8 pwm1_value; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 168 | s8 temp8[3]; /* 0: local input |
| 169 | 1: local high limit |
| 170 | 2: remote critical limit */ |
| 171 | s16 temp11[3]; /* 0: remote input |
| 172 | 1: remote low limit |
| 173 | 2: remote high limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | u8 temp2_crit_hyst; |
| 175 | u8 alarms; |
| 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * Sysfs callback functions and files |
| 180 | */ |
| 181 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 182 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
| 183 | char *buf) |
| 184 | { |
| 185 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 186 | struct lm63_data *data = lm63_update_device(dev); |
| 187 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 190 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
| 191 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct i2c_client *client = to_i2c_client(dev); |
| 194 | struct lm63_data *data = i2c_get_clientdata(client); |
| 195 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 196 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 197 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 198 | data->fan[1] = FAN_TO_REG(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 200 | data->fan[1] & 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 202 | data->fan[1] >> 8); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 203 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return count; |
| 205 | } |
| 206 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 207 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, |
| 208 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct lm63_data *data = lm63_update_device(dev); |
| 211 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? |
| 212 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / |
| 213 | (2 * data->pwm1_freq)); |
| 214 | } |
| 215 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 216 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
| 217 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct i2c_client *client = to_i2c_client(dev); |
| 220 | struct lm63_data *data = i2c_get_clientdata(client); |
| 221 | unsigned long val; |
| 222 | |
| 223 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
| 224 | return -EPERM; |
| 225 | |
| 226 | val = simple_strtoul(buf, NULL, 10); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 227 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | data->pwm1_value = val <= 0 ? 0 : |
| 229 | val >= 255 ? 2 * data->pwm1_freq : |
| 230 | (val * data->pwm1_freq * 2 + 127) / 255; |
| 231 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 232 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return count; |
| 234 | } |
| 235 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 236 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy, |
| 237 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct lm63_data *data = lm63_update_device(dev); |
| 240 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); |
| 241 | } |
| 242 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 243 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, |
| 244 | char *buf) |
| 245 | { |
| 246 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 247 | struct lm63_data *data = lm63_update_device(dev); |
| 248 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 251 | static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy, |
| 252 | const char *buf, size_t count) |
| 253 | { |
| 254 | struct i2c_client *client = to_i2c_client(dev); |
| 255 | struct lm63_data *data = i2c_get_clientdata(client); |
| 256 | long val = simple_strtol(buf, NULL, 10); |
| 257 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 258 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 259 | data->temp8[1] = TEMP8_TO_REG(val); |
| 260 | i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 261 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 262 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 264 | |
| 265 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, |
| 266 | char *buf) |
| 267 | { |
| 268 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 269 | struct lm63_data *data = lm63_update_device(dev); |
| 270 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 272 | |
| 273 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, |
| 274 | const char *buf, size_t count) |
| 275 | { |
| 276 | static const u8 reg[4] = { |
| 277 | LM63_REG_REMOTE_LOW_MSB, |
| 278 | LM63_REG_REMOTE_LOW_LSB, |
| 279 | LM63_REG_REMOTE_HIGH_MSB, |
| 280 | LM63_REG_REMOTE_HIGH_LSB, |
| 281 | }; |
| 282 | |
| 283 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 284 | struct i2c_client *client = to_i2c_client(dev); |
| 285 | struct lm63_data *data = i2c_get_clientdata(client); |
| 286 | long val = simple_strtol(buf, NULL, 10); |
| 287 | int nr = attr->index; |
| 288 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 289 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 290 | data->temp11[nr] = TEMP11_TO_REG(val); |
| 291 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
| 292 | data->temp11[nr] >> 8); |
| 293 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], |
| 294 | data->temp11[nr] & 0xff); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 295 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 296 | return count; |
| 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | /* Hysteresis register holds a relative value, while we want to present |
| 300 | an absolute to user-space */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 301 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
| 302 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | struct lm63_data *data = lm63_update_device(dev); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 305 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
| 307 | } |
| 308 | |
| 309 | /* And now the other way around, user-space provides an absolute |
| 310 | hysteresis value and we have to store a relative one */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 311 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
| 312 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct i2c_client *client = to_i2c_client(dev); |
| 315 | struct lm63_data *data = i2c_get_clientdata(client); |
| 316 | long val = simple_strtol(buf, NULL, 10); |
| 317 | long hyst; |
| 318 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 319 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 320 | hyst = TEMP8_FROM_REG(data->temp8[2]) - val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
| 322 | HYST_TO_REG(hyst)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 323 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return count; |
| 325 | } |
| 326 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 327 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 328 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | struct lm63_data *data = lm63_update_device(dev); |
| 331 | return sprintf(buf, "%u\n", data->alarms); |
| 332 | } |
| 333 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 334 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
| 335 | char *buf) |
| 336 | { |
| 337 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 338 | struct lm63_data *data = lm63_update_device(dev); |
| 339 | int bitnr = attr->index; |
| 340 | |
| 341 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 342 | } |
| 343 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 344 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 345 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, |
| 346 | set_fan, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); |
| 349 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); |
| 350 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 351 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); |
| 352 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, |
| 353 | set_temp8, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 355 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
| 356 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
| 357 | set_temp11, 1); |
| 358 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
| 359 | set_temp11, 2); |
| 360 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
| 362 | set_temp2_crit_hyst); |
| 363 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 364 | /* Individual alarm files */ |
| 365 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 366 | 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] | 367 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 368 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 369 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 370 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 371 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 373 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 374 | static struct attribute *lm63_attributes[] = { |
| 375 | &dev_attr_pwm1.attr, |
| 376 | &dev_attr_pwm1_enable.attr, |
| 377 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 378 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 379 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 380 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 381 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 382 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 383 | &dev_attr_temp2_crit_hyst.attr, |
| 384 | |
| 385 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 386 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 387 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 388 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 389 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 390 | &dev_attr_alarms.attr, |
| 391 | NULL |
| 392 | }; |
| 393 | |
| 394 | static const struct attribute_group lm63_group = { |
| 395 | .attrs = lm63_attributes, |
| 396 | }; |
| 397 | |
| 398 | static struct attribute *lm63_attributes_fan1[] = { |
| 399 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 400 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 401 | |
| 402 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, |
| 403 | NULL |
| 404 | }; |
| 405 | |
| 406 | static const struct attribute_group lm63_group_fan1 = { |
| 407 | .attrs = lm63_attributes_fan1, |
| 408 | }; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Real code |
| 412 | */ |
| 413 | |
| 414 | static int lm63_attach_adapter(struct i2c_adapter *adapter) |
| 415 | { |
| 416 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 417 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 418 | return i2c_probe(adapter, &addr_data, lm63_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* |
| 422 | * The following function does more than just detection. If detection |
| 423 | * succeeds, it also registers the new chip. |
| 424 | */ |
| 425 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind) |
| 426 | { |
| 427 | struct i2c_client *new_client; |
| 428 | struct lm63_data *data; |
| 429 | int err = 0; |
| 430 | |
| 431 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 432 | goto exit; |
| 433 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 434 | if (!(data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | err = -ENOMEM; |
| 436 | goto exit; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | /* The common I2C client data is placed right before the |
| 440 | LM63-specific data. */ |
| 441 | new_client = &data->client; |
| 442 | i2c_set_clientdata(new_client, data); |
| 443 | new_client->addr = address; |
| 444 | new_client->adapter = adapter; |
| 445 | new_client->driver = &lm63_driver; |
| 446 | new_client->flags = 0; |
| 447 | |
| 448 | /* Default to an LM63 if forced */ |
| 449 | if (kind == 0) |
| 450 | kind = lm63; |
| 451 | |
| 452 | if (kind < 0) { /* must identify */ |
| 453 | u8 man_id, chip_id, reg_config1, reg_config2; |
| 454 | u8 reg_alert_status, reg_alert_mask; |
| 455 | |
| 456 | man_id = i2c_smbus_read_byte_data(new_client, |
| 457 | LM63_REG_MAN_ID); |
| 458 | chip_id = i2c_smbus_read_byte_data(new_client, |
| 459 | LM63_REG_CHIP_ID); |
| 460 | reg_config1 = i2c_smbus_read_byte_data(new_client, |
| 461 | LM63_REG_CONFIG1); |
| 462 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 463 | LM63_REG_CONFIG2); |
| 464 | reg_alert_status = i2c_smbus_read_byte_data(new_client, |
| 465 | LM63_REG_ALERT_STATUS); |
| 466 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, |
| 467 | LM63_REG_ALERT_MASK); |
| 468 | |
| 469 | if (man_id == 0x01 /* National Semiconductor */ |
| 470 | && chip_id == 0x41 /* LM63 */ |
| 471 | && (reg_config1 & 0x18) == 0x00 |
| 472 | && (reg_config2 & 0xF8) == 0x00 |
| 473 | && (reg_alert_status & 0x20) == 0x00 |
| 474 | && (reg_alert_mask & 0xA4) == 0xA4) { |
| 475 | kind = lm63; |
| 476 | } else { /* failed */ |
| 477 | dev_dbg(&adapter->dev, "Unsupported chip " |
| 478 | "(man_id=0x%02X, chip_id=0x%02X).\n", |
| 479 | man_id, chip_id); |
| 480 | goto exit_free; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | strlcpy(new_client->name, "lm63", I2C_NAME_SIZE); |
| 485 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 486 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* Tell the I2C layer a new client has arrived */ |
| 489 | if ((err = i2c_attach_client(new_client))) |
| 490 | goto exit_free; |
| 491 | |
| 492 | /* Initialize the LM63 chip */ |
| 493 | lm63_init_client(new_client); |
| 494 | |
| 495 | /* Register sysfs hooks */ |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 496 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 497 | &lm63_group))) |
| 498 | goto exit_detach; |
| 499 | if (data->config & 0x04) { /* tachometer enabled */ |
| 500 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 501 | &lm63_group_fan1))) |
| 502 | goto exit_remove_files; |
| 503 | } |
| 504 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 505 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 506 | if (IS_ERR(data->class_dev)) { |
| 507 | err = PTR_ERR(data->class_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 508 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return 0; |
| 512 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 513 | exit_remove_files: |
| 514 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group); |
| 515 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 516 | exit_detach: |
| 517 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | exit_free: |
| 519 | kfree(data); |
| 520 | exit: |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | /* Idealy we shouldn't have to initialize anything, since the BIOS |
| 525 | should have taken care of everything */ |
| 526 | static void lm63_init_client(struct i2c_client *client) |
| 527 | { |
| 528 | struct lm63_data *data = i2c_get_clientdata(client); |
| 529 | |
| 530 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); |
| 531 | data->config_fan = i2c_smbus_read_byte_data(client, |
| 532 | LM63_REG_CONFIG_FAN); |
| 533 | |
| 534 | /* Start converting if needed */ |
| 535 | if (data->config & 0x40) { /* standby */ |
| 536 | dev_dbg(&client->dev, "Switching to operational mode"); |
| 537 | data->config &= 0xA7; |
| 538 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, |
| 539 | data->config); |
| 540 | } |
| 541 | |
| 542 | /* We may need pwm1_freq before ever updating the client data */ |
| 543 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); |
| 544 | if (data->pwm1_freq == 0) |
| 545 | data->pwm1_freq = 1; |
| 546 | |
| 547 | /* Show some debug info about the LM63 configuration */ |
| 548 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", |
| 549 | (data->config & 0x04) ? "tachometer input" : |
| 550 | "alert output"); |
| 551 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", |
| 552 | (data->config_fan & 0x08) ? "1.4" : "360", |
| 553 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); |
| 554 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", |
| 555 | (data->config_fan & 0x10) ? "low" : "high", |
| 556 | (data->config_fan & 0x20) ? "manual" : "auto"); |
| 557 | } |
| 558 | |
| 559 | static int lm63_detach_client(struct i2c_client *client) |
| 560 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 561 | struct lm63_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | int err; |
| 563 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 564 | hwmon_device_unregister(data->class_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 565 | sysfs_remove_group(&client->dev.kobj, &lm63_group); |
| 566 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 567 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 568 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 571 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static struct lm63_data *lm63_update_device(struct device *dev) |
| 576 | { |
| 577 | struct i2c_client *client = to_i2c_client(dev); |
| 578 | struct lm63_data *data = i2c_get_clientdata(client); |
| 579 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 580 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 583 | if (data->config & 0x04) { /* tachometer enabled */ |
| 584 | /* order matters for fan1_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 585 | data->fan[0] = i2c_smbus_read_byte_data(client, |
| 586 | LM63_REG_TACH_COUNT_LSB) & 0xFC; |
| 587 | data->fan[0] |= i2c_smbus_read_byte_data(client, |
| 588 | LM63_REG_TACH_COUNT_MSB) << 8; |
| 589 | data->fan[1] = (i2c_smbus_read_byte_data(client, |
| 590 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) |
| 591 | | (i2c_smbus_read_byte_data(client, |
| 592 | LM63_REG_TACH_LIMIT_MSB) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | data->pwm1_freq = i2c_smbus_read_byte_data(client, |
| 596 | LM63_REG_PWM_FREQ); |
| 597 | if (data->pwm1_freq == 0) |
| 598 | data->pwm1_freq = 1; |
| 599 | data->pwm1_value = i2c_smbus_read_byte_data(client, |
| 600 | LM63_REG_PWM_VALUE); |
| 601 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 602 | data->temp8[0] = i2c_smbus_read_byte_data(client, |
| 603 | LM63_REG_LOCAL_TEMP); |
| 604 | data->temp8[1] = i2c_smbus_read_byte_data(client, |
| 605 | LM63_REG_LOCAL_HIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | /* order matters for temp2_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 608 | data->temp11[0] = i2c_smbus_read_byte_data(client, |
| 609 | LM63_REG_REMOTE_TEMP_MSB) << 8; |
| 610 | data->temp11[0] |= i2c_smbus_read_byte_data(client, |
| 611 | LM63_REG_REMOTE_TEMP_LSB); |
| 612 | data->temp11[1] = (i2c_smbus_read_byte_data(client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | LM63_REG_REMOTE_LOW_MSB) << 8) |
| 614 | | i2c_smbus_read_byte_data(client, |
| 615 | LM63_REG_REMOTE_LOW_LSB); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 616 | data->temp11[2] = (i2c_smbus_read_byte_data(client, |
| 617 | LM63_REG_REMOTE_HIGH_MSB) << 8) |
| 618 | | i2c_smbus_read_byte_data(client, |
| 619 | LM63_REG_REMOTE_HIGH_LSB); |
| 620 | data->temp8[2] = i2c_smbus_read_byte_data(client, |
| 621 | LM63_REG_REMOTE_TCRIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
| 623 | LM63_REG_REMOTE_TCRIT_HYST); |
| 624 | |
| 625 | data->alarms = i2c_smbus_read_byte_data(client, |
| 626 | LM63_REG_ALERT_STATUS) & 0x7F; |
| 627 | |
| 628 | data->last_updated = jiffies; |
| 629 | data->valid = 1; |
| 630 | } |
| 631 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 632 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | return data; |
| 635 | } |
| 636 | |
| 637 | static int __init sensors_lm63_init(void) |
| 638 | { |
| 639 | return i2c_add_driver(&lm63_driver); |
| 640 | } |
| 641 | |
| 642 | static void __exit sensors_lm63_exit(void) |
| 643 | { |
| 644 | i2c_del_driver(&lm63_driver); |
| 645 | } |
| 646 | |
| 647 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 648 | MODULE_DESCRIPTION("LM63 driver"); |
| 649 | MODULE_LICENSE("GPL"); |
| 650 | |
| 651 | module_init(sensors_lm63_init); |
| 652 | module_exit(sensors_lm63_exit); |