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 | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2008 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 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 56 | static const 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 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 131 | static int lm63_probe(struct i2c_client *client, |
| 132 | const struct i2c_device_id *id); |
| 133 | static int lm63_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | static struct lm63_data *lm63_update_device(struct device *dev); |
| 136 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 137 | static int lm63_detect(struct i2c_client *client, int kind, |
| 138 | struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static void lm63_init_client(struct i2c_client *client); |
| 140 | |
| 141 | /* |
| 142 | * Driver data (common to all clients) |
| 143 | */ |
| 144 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 145 | static const struct i2c_device_id lm63_id[] = { |
| 146 | { "lm63", lm63 }, |
| 147 | { } |
| 148 | }; |
| 149 | MODULE_DEVICE_TABLE(i2c, lm63_id); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | static struct i2c_driver lm63_driver = { |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 152 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 153 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 154 | .name = "lm63", |
| 155 | }, |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 156 | .probe = lm63_probe, |
| 157 | .remove = lm63_remove, |
| 158 | .id_table = lm63_id, |
| 159 | .detect = lm63_detect, |
| 160 | .address_data = &addr_data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * Client data (each client gets its own) |
| 165 | */ |
| 166 | |
| 167 | struct lm63_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 168 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 169 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | char valid; /* zero until following fields are valid */ |
| 171 | unsigned long last_updated; /* in jiffies */ |
| 172 | |
| 173 | /* registers values */ |
| 174 | u8 config, config_fan; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 175 | u16 fan[2]; /* 0: input |
| 176 | 1: low limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | u8 pwm1_freq; |
| 178 | u8 pwm1_value; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 179 | s8 temp8[3]; /* 0: local input |
| 180 | 1: local high limit |
| 181 | 2: remote critical limit */ |
| 182 | s16 temp11[3]; /* 0: remote input |
| 183 | 1: remote low limit |
| 184 | 2: remote high limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | u8 temp2_crit_hyst; |
| 186 | u8 alarms; |
| 187 | }; |
| 188 | |
| 189 | /* |
| 190 | * Sysfs callback functions and files |
| 191 | */ |
| 192 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 193 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
| 194 | char *buf) |
| 195 | { |
| 196 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 197 | struct lm63_data *data = lm63_update_device(dev); |
| 198 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 201 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
| 202 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | struct i2c_client *client = to_i2c_client(dev); |
| 205 | struct lm63_data *data = i2c_get_clientdata(client); |
| 206 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 207 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 208 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 209 | data->fan[1] = FAN_TO_REG(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 211 | data->fan[1] & 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 213 | data->fan[1] >> 8); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 214 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return count; |
| 216 | } |
| 217 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 218 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, |
| 219 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | struct lm63_data *data = lm63_update_device(dev); |
| 222 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? |
| 223 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / |
| 224 | (2 * data->pwm1_freq)); |
| 225 | } |
| 226 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 227 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
| 228 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
| 230 | struct i2c_client *client = to_i2c_client(dev); |
| 231 | struct lm63_data *data = i2c_get_clientdata(client); |
| 232 | unsigned long val; |
| 233 | |
| 234 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
| 235 | return -EPERM; |
| 236 | |
| 237 | val = simple_strtoul(buf, NULL, 10); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 238 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | data->pwm1_value = val <= 0 ? 0 : |
| 240 | val >= 255 ? 2 * data->pwm1_freq : |
| 241 | (val * data->pwm1_freq * 2 + 127) / 255; |
| 242 | 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] | 243 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return count; |
| 245 | } |
| 246 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 247 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy, |
| 248 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | struct lm63_data *data = lm63_update_device(dev); |
| 251 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); |
| 252 | } |
| 253 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 254 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, |
| 255 | char *buf) |
| 256 | { |
| 257 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 258 | struct lm63_data *data = lm63_update_device(dev); |
| 259 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 262 | static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy, |
| 263 | const char *buf, size_t count) |
| 264 | { |
| 265 | struct i2c_client *client = to_i2c_client(dev); |
| 266 | struct lm63_data *data = i2c_get_clientdata(client); |
| 267 | long val = simple_strtol(buf, NULL, 10); |
| 268 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 269 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 270 | data->temp8[1] = TEMP8_TO_REG(val); |
| 271 | 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] | 272 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 273 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 275 | |
| 276 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, |
| 277 | char *buf) |
| 278 | { |
| 279 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 280 | struct lm63_data *data = lm63_update_device(dev); |
| 281 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 283 | |
| 284 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, |
| 285 | const char *buf, size_t count) |
| 286 | { |
| 287 | static const u8 reg[4] = { |
| 288 | LM63_REG_REMOTE_LOW_MSB, |
| 289 | LM63_REG_REMOTE_LOW_LSB, |
| 290 | LM63_REG_REMOTE_HIGH_MSB, |
| 291 | LM63_REG_REMOTE_HIGH_LSB, |
| 292 | }; |
| 293 | |
| 294 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 295 | struct i2c_client *client = to_i2c_client(dev); |
| 296 | struct lm63_data *data = i2c_get_clientdata(client); |
| 297 | long val = simple_strtol(buf, NULL, 10); |
| 298 | int nr = attr->index; |
| 299 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 300 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 301 | data->temp11[nr] = TEMP11_TO_REG(val); |
| 302 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
| 303 | data->temp11[nr] >> 8); |
| 304 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], |
| 305 | data->temp11[nr] & 0xff); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 306 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 307 | return count; |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | /* Hysteresis register holds a relative value, while we want to present |
| 311 | an absolute to user-space */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 312 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
| 313 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct lm63_data *data = lm63_update_device(dev); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 316 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
| 318 | } |
| 319 | |
| 320 | /* And now the other way around, user-space provides an absolute |
| 321 | hysteresis value and we have to store a relative one */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 322 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, |
| 323 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | struct i2c_client *client = to_i2c_client(dev); |
| 326 | struct lm63_data *data = i2c_get_clientdata(client); |
| 327 | long val = simple_strtol(buf, NULL, 10); |
| 328 | long hyst; |
| 329 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 330 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 331 | hyst = TEMP8_FROM_REG(data->temp8[2]) - val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
| 333 | HYST_TO_REG(hyst)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 334 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return count; |
| 336 | } |
| 337 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 338 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 339 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
| 341 | struct lm63_data *data = lm63_update_device(dev); |
| 342 | return sprintf(buf, "%u\n", data->alarms); |
| 343 | } |
| 344 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 345 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
| 346 | char *buf) |
| 347 | { |
| 348 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 349 | struct lm63_data *data = lm63_update_device(dev); |
| 350 | int bitnr = attr->index; |
| 351 | |
| 352 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 353 | } |
| 354 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 355 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 356 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, |
| 357 | set_fan, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); |
| 360 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); |
| 361 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 362 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); |
| 363 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, |
| 364 | set_temp8, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 366 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
| 367 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
| 368 | set_temp11, 1); |
| 369 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
| 370 | set_temp11, 2); |
| 371 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
| 373 | set_temp2_crit_hyst); |
| 374 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 375 | /* Individual alarm files */ |
| 376 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 377 | 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] | 378 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 379 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 380 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 381 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 382 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 384 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 385 | static struct attribute *lm63_attributes[] = { |
| 386 | &dev_attr_pwm1.attr, |
| 387 | &dev_attr_pwm1_enable.attr, |
| 388 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 389 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 390 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 391 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 392 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 393 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 394 | &dev_attr_temp2_crit_hyst.attr, |
| 395 | |
| 396 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 397 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 398 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 399 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 400 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 401 | &dev_attr_alarms.attr, |
| 402 | NULL |
| 403 | }; |
| 404 | |
| 405 | static const struct attribute_group lm63_group = { |
| 406 | .attrs = lm63_attributes, |
| 407 | }; |
| 408 | |
| 409 | static struct attribute *lm63_attributes_fan1[] = { |
| 410 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 411 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 412 | |
| 413 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, |
| 414 | NULL |
| 415 | }; |
| 416 | |
| 417 | static const struct attribute_group lm63_group_fan1 = { |
| 418 | .attrs = lm63_attributes_fan1, |
| 419 | }; |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | /* |
| 422 | * Real code |
| 423 | */ |
| 424 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 425 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 426 | static int lm63_detect(struct i2c_client *new_client, int kind, |
| 427 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 429 | struct i2c_adapter *adapter = new_client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 432 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | if (kind < 0) { /* must identify */ |
| 435 | u8 man_id, chip_id, reg_config1, reg_config2; |
| 436 | u8 reg_alert_status, reg_alert_mask; |
| 437 | |
| 438 | man_id = i2c_smbus_read_byte_data(new_client, |
| 439 | LM63_REG_MAN_ID); |
| 440 | chip_id = i2c_smbus_read_byte_data(new_client, |
| 441 | LM63_REG_CHIP_ID); |
| 442 | reg_config1 = i2c_smbus_read_byte_data(new_client, |
| 443 | LM63_REG_CONFIG1); |
| 444 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 445 | LM63_REG_CONFIG2); |
| 446 | reg_alert_status = i2c_smbus_read_byte_data(new_client, |
| 447 | LM63_REG_ALERT_STATUS); |
| 448 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, |
| 449 | LM63_REG_ALERT_MASK); |
| 450 | |
| 451 | if (man_id == 0x01 /* National Semiconductor */ |
| 452 | && chip_id == 0x41 /* LM63 */ |
| 453 | && (reg_config1 & 0x18) == 0x00 |
| 454 | && (reg_config2 & 0xF8) == 0x00 |
| 455 | && (reg_alert_status & 0x20) == 0x00 |
| 456 | && (reg_alert_mask & 0xA4) == 0xA4) { |
| 457 | kind = lm63; |
| 458 | } else { /* failed */ |
| 459 | dev_dbg(&adapter->dev, "Unsupported chip " |
| 460 | "(man_id=0x%02X, chip_id=0x%02X).\n", |
| 461 | man_id, chip_id); |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 462 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 466 | strlcpy(info->type, "lm63", I2C_NAME_SIZE); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int lm63_probe(struct i2c_client *new_client, |
| 472 | const struct i2c_device_id *id) |
| 473 | { |
| 474 | struct lm63_data *data; |
| 475 | int err; |
| 476 | |
| 477 | data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); |
| 478 | if (!data) { |
| 479 | err = -ENOMEM; |
| 480 | goto exit; |
| 481 | } |
| 482 | |
| 483 | i2c_set_clientdata(new_client, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 485 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | /* Initialize the LM63 chip */ |
| 488 | lm63_init_client(new_client); |
| 489 | |
| 490 | /* Register sysfs hooks */ |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 491 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 492 | &lm63_group))) |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 493 | goto exit_free; |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 494 | if (data->config & 0x04) { /* tachometer enabled */ |
| 495 | if ((err = sysfs_create_group(&new_client->dev.kobj, |
| 496 | &lm63_group_fan1))) |
| 497 | goto exit_remove_files; |
| 498 | } |
| 499 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 500 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 501 | if (IS_ERR(data->hwmon_dev)) { |
| 502 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 503 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 508 | exit_remove_files: |
| 509 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group); |
| 510 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | exit_free: |
| 512 | kfree(data); |
| 513 | exit: |
| 514 | return err; |
| 515 | } |
| 516 | |
| 517 | /* Idealy we shouldn't have to initialize anything, since the BIOS |
| 518 | should have taken care of everything */ |
| 519 | static void lm63_init_client(struct i2c_client *client) |
| 520 | { |
| 521 | struct lm63_data *data = i2c_get_clientdata(client); |
| 522 | |
| 523 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); |
| 524 | data->config_fan = i2c_smbus_read_byte_data(client, |
| 525 | LM63_REG_CONFIG_FAN); |
| 526 | |
| 527 | /* Start converting if needed */ |
| 528 | if (data->config & 0x40) { /* standby */ |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 529 | dev_dbg(&client->dev, "Switching to operational mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | data->config &= 0xA7; |
| 531 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, |
| 532 | data->config); |
| 533 | } |
| 534 | |
| 535 | /* We may need pwm1_freq before ever updating the client data */ |
| 536 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); |
| 537 | if (data->pwm1_freq == 0) |
| 538 | data->pwm1_freq = 1; |
| 539 | |
| 540 | /* Show some debug info about the LM63 configuration */ |
| 541 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", |
| 542 | (data->config & 0x04) ? "tachometer input" : |
| 543 | "alert output"); |
| 544 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", |
| 545 | (data->config_fan & 0x08) ? "1.4" : "360", |
| 546 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); |
| 547 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", |
| 548 | (data->config_fan & 0x10) ? "low" : "high", |
| 549 | (data->config_fan & 0x20) ? "manual" : "auto"); |
| 550 | } |
| 551 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 552 | static int lm63_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 554 | struct lm63_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 556 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 557 | sysfs_remove_group(&client->dev.kobj, &lm63_group); |
| 558 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 559 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 560 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static struct lm63_data *lm63_update_device(struct device *dev) |
| 565 | { |
| 566 | struct i2c_client *client = to_i2c_client(dev); |
| 567 | struct lm63_data *data = i2c_get_clientdata(client); |
| 568 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 569 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 572 | if (data->config & 0x04) { /* tachometer enabled */ |
| 573 | /* order matters for fan1_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 574 | data->fan[0] = i2c_smbus_read_byte_data(client, |
| 575 | LM63_REG_TACH_COUNT_LSB) & 0xFC; |
| 576 | data->fan[0] |= i2c_smbus_read_byte_data(client, |
| 577 | LM63_REG_TACH_COUNT_MSB) << 8; |
| 578 | data->fan[1] = (i2c_smbus_read_byte_data(client, |
| 579 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) |
| 580 | | (i2c_smbus_read_byte_data(client, |
| 581 | LM63_REG_TACH_LIMIT_MSB) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | data->pwm1_freq = i2c_smbus_read_byte_data(client, |
| 585 | LM63_REG_PWM_FREQ); |
| 586 | if (data->pwm1_freq == 0) |
| 587 | data->pwm1_freq = 1; |
| 588 | data->pwm1_value = i2c_smbus_read_byte_data(client, |
| 589 | LM63_REG_PWM_VALUE); |
| 590 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 591 | data->temp8[0] = i2c_smbus_read_byte_data(client, |
| 592 | LM63_REG_LOCAL_TEMP); |
| 593 | data->temp8[1] = i2c_smbus_read_byte_data(client, |
| 594 | LM63_REG_LOCAL_HIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* order matters for temp2_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 597 | data->temp11[0] = i2c_smbus_read_byte_data(client, |
| 598 | LM63_REG_REMOTE_TEMP_MSB) << 8; |
| 599 | data->temp11[0] |= i2c_smbus_read_byte_data(client, |
| 600 | LM63_REG_REMOTE_TEMP_LSB); |
| 601 | data->temp11[1] = (i2c_smbus_read_byte_data(client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | LM63_REG_REMOTE_LOW_MSB) << 8) |
| 603 | | i2c_smbus_read_byte_data(client, |
| 604 | LM63_REG_REMOTE_LOW_LSB); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 605 | data->temp11[2] = (i2c_smbus_read_byte_data(client, |
| 606 | LM63_REG_REMOTE_HIGH_MSB) << 8) |
| 607 | | i2c_smbus_read_byte_data(client, |
| 608 | LM63_REG_REMOTE_HIGH_LSB); |
| 609 | data->temp8[2] = i2c_smbus_read_byte_data(client, |
| 610 | LM63_REG_REMOTE_TCRIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
| 612 | LM63_REG_REMOTE_TCRIT_HYST); |
| 613 | |
| 614 | data->alarms = i2c_smbus_read_byte_data(client, |
| 615 | LM63_REG_ALERT_STATUS) & 0x7F; |
| 616 | |
| 617 | data->last_updated = jiffies; |
| 618 | data->valid = 1; |
| 619 | } |
| 620 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 621 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | return data; |
| 624 | } |
| 625 | |
| 626 | static int __init sensors_lm63_init(void) |
| 627 | { |
| 628 | return i2c_add_driver(&lm63_driver); |
| 629 | } |
| 630 | |
| 631 | static void __exit sensors_lm63_exit(void) |
| 632 | { |
| 633 | i2c_del_driver(&lm63_driver); |
| 634 | } |
| 635 | |
| 636 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 637 | MODULE_DESCRIPTION("LM63 driver"); |
| 638 | MODULE_LICENSE("GPL"); |
| 639 | |
| 640 | module_init(sensors_lm63_init); |
| 641 | module_exit(sensors_lm63_exit); |