| 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 | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2005  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> | 
|  | 45 | #include <linux/i2c-sensor.h> | 
| Jean Delvare | 10c08f8 | 2005-06-06 19:34:45 +0200 | [diff] [blame] | 46 | #include <linux/hwmon-sysfs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | /* | 
|  | 49 | * Addresses to scan | 
|  | 50 | * Address is fully defined internally and cannot be changed. | 
|  | 51 | */ | 
|  | 52 |  | 
|  | 53 | static unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; | 
|  | 54 | static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END }; | 
|  | 55 |  | 
|  | 56 | /* | 
|  | 57 | * Insmod parameters | 
|  | 58 | */ | 
|  | 59 |  | 
|  | 60 | SENSORS_INSMOD_1(lm63); | 
|  | 61 |  | 
|  | 62 | /* | 
|  | 63 | * The LM63 registers | 
|  | 64 | */ | 
|  | 65 |  | 
|  | 66 | #define LM63_REG_CONFIG1		0x03 | 
|  | 67 | #define LM63_REG_CONFIG2		0xBF | 
|  | 68 | #define LM63_REG_CONFIG_FAN		0x4A | 
|  | 69 |  | 
|  | 70 | #define LM63_REG_TACH_COUNT_MSB		0x47 | 
|  | 71 | #define LM63_REG_TACH_COUNT_LSB		0x46 | 
|  | 72 | #define LM63_REG_TACH_LIMIT_MSB		0x49 | 
|  | 73 | #define LM63_REG_TACH_LIMIT_LSB		0x48 | 
|  | 74 |  | 
|  | 75 | #define LM63_REG_PWM_VALUE		0x4C | 
|  | 76 | #define LM63_REG_PWM_FREQ		0x4D | 
|  | 77 |  | 
|  | 78 | #define LM63_REG_LOCAL_TEMP		0x00 | 
|  | 79 | #define LM63_REG_LOCAL_HIGH		0x05 | 
|  | 80 |  | 
|  | 81 | #define LM63_REG_REMOTE_TEMP_MSB	0x01 | 
|  | 82 | #define LM63_REG_REMOTE_TEMP_LSB	0x10 | 
|  | 83 | #define LM63_REG_REMOTE_OFFSET_MSB	0x11 | 
|  | 84 | #define LM63_REG_REMOTE_OFFSET_LSB	0x12 | 
|  | 85 | #define LM63_REG_REMOTE_HIGH_MSB	0x07 | 
|  | 86 | #define LM63_REG_REMOTE_HIGH_LSB	0x13 | 
|  | 87 | #define LM63_REG_REMOTE_LOW_MSB		0x08 | 
|  | 88 | #define LM63_REG_REMOTE_LOW_LSB		0x14 | 
|  | 89 | #define LM63_REG_REMOTE_TCRIT		0x19 | 
|  | 90 | #define LM63_REG_REMOTE_TCRIT_HYST	0x21 | 
|  | 91 |  | 
|  | 92 | #define LM63_REG_ALERT_STATUS		0x02 | 
|  | 93 | #define LM63_REG_ALERT_MASK		0x16 | 
|  | 94 |  | 
|  | 95 | #define LM63_REG_MAN_ID			0xFE | 
|  | 96 | #define LM63_REG_CHIP_ID		0xFF | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * Conversions and various macros | 
|  | 100 | * For tachometer counts, the LM63 uses 16-bit values. | 
|  | 101 | * For local temperature and high limit, remote critical limit and hysteresis | 
| Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 102 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | * 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] | 104 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ | 
|  | 106 |  | 
|  | 107 | #define FAN_FROM_REG(reg)	((reg) == 0xFFFC || (reg) == 0 ? 0 : \ | 
|  | 108 | 5400000 / (reg)) | 
|  | 109 | #define FAN_TO_REG(val)		((val) <= 82 ? 0xFFFC : \ | 
|  | 110 | (5400000 / (val)) & 0xFFFC) | 
|  | 111 | #define TEMP8_FROM_REG(reg)	((reg) * 1000) | 
|  | 112 | #define TEMP8_TO_REG(val)	((val) <= -128000 ? -128 : \ | 
|  | 113 | (val) >= 127000 ? 127 : \ | 
|  | 114 | (val) < 0 ? ((val) - 500) / 1000 : \ | 
|  | 115 | ((val) + 500) / 1000) | 
|  | 116 | #define TEMP11_FROM_REG(reg)	((reg) / 32 * 125) | 
|  | 117 | #define TEMP11_TO_REG(val)	((val) <= -128000 ? 0x8000 : \ | 
|  | 118 | (val) >= 127875 ? 0x7FE0 : \ | 
|  | 119 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ | 
|  | 120 | ((val) + 62) / 125 * 32) | 
|  | 121 | #define HYST_TO_REG(val)	((val) <= 0 ? 0 : \ | 
|  | 122 | (val) >= 127000 ? 127 : \ | 
|  | 123 | ((val) + 500) / 1000) | 
|  | 124 |  | 
|  | 125 | /* | 
|  | 126 | * Functions declaration | 
|  | 127 | */ | 
|  | 128 |  | 
|  | 129 | static int lm63_attach_adapter(struct i2c_adapter *adapter); | 
|  | 130 | static int lm63_detach_client(struct i2c_client *client); | 
|  | 131 |  | 
|  | 132 | static struct lm63_data *lm63_update_device(struct device *dev); | 
|  | 133 |  | 
|  | 134 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind); | 
|  | 135 | static void lm63_init_client(struct i2c_client *client); | 
|  | 136 |  | 
|  | 137 | /* | 
|  | 138 | * Driver data (common to all clients) | 
|  | 139 | */ | 
|  | 140 |  | 
|  | 141 | static struct i2c_driver lm63_driver = { | 
|  | 142 | .owner		= THIS_MODULE, | 
|  | 143 | .name		= "lm63", | 
|  | 144 | .flags		= I2C_DF_NOTIFY, | 
|  | 145 | .attach_adapter	= lm63_attach_adapter, | 
|  | 146 | .detach_client	= lm63_detach_client, | 
|  | 147 | }; | 
|  | 148 |  | 
|  | 149 | /* | 
|  | 150 | * Client data (each client gets its own) | 
|  | 151 | */ | 
|  | 152 |  | 
|  | 153 | struct lm63_data { | 
|  | 154 | struct i2c_client client; | 
|  | 155 | struct semaphore update_lock; | 
|  | 156 | char valid; /* zero until following fields are valid */ | 
|  | 157 | unsigned long last_updated; /* in jiffies */ | 
|  | 158 |  | 
|  | 159 | /* registers values */ | 
|  | 160 | u8 config, config_fan; | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 161 | u16 fan[2];	/* 0: input | 
|  | 162 | 1: low limit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | u8 pwm1_freq; | 
|  | 164 | u8 pwm1_value; | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 165 | s8 temp8[3];	/* 0: local input | 
|  | 166 | 1: local high limit | 
|  | 167 | 2: remote critical limit */ | 
|  | 168 | s16 temp11[3];	/* 0: remote input | 
|  | 169 | 1: remote low limit | 
|  | 170 | 2: remote high limit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | u8 temp2_crit_hyst; | 
|  | 172 | u8 alarms; | 
|  | 173 | }; | 
|  | 174 |  | 
|  | 175 | /* | 
|  | 176 | * Sysfs callback functions and files | 
|  | 177 | */ | 
|  | 178 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 179 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, | 
|  | 180 | char *buf) | 
|  | 181 | { | 
|  | 182 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 183 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 184 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 187 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, | 
|  | 188 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { | 
|  | 190 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 191 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 192 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 193 |  | 
|  | 194 | down(&data->update_lock); | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 195 | data->fan[1] = FAN_TO_REG(val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 197 | data->fan[1] & 0xFF); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 199 | data->fan[1] >> 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | up(&data->update_lock); | 
|  | 201 | return count; | 
|  | 202 | } | 
|  | 203 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 204 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, | 
|  | 205 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { | 
|  | 207 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 208 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? | 
|  | 209 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / | 
|  | 210 | (2 * data->pwm1_freq)); | 
|  | 211 | } | 
|  | 212 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 213 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, | 
|  | 214 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { | 
|  | 216 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 217 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 218 | unsigned long val; | 
|  | 219 |  | 
|  | 220 | if (!(data->config_fan & 0x20)) /* register is read-only */ | 
|  | 221 | return -EPERM; | 
|  | 222 |  | 
|  | 223 | val = simple_strtoul(buf, NULL, 10); | 
|  | 224 | down(&data->update_lock); | 
|  | 225 | data->pwm1_value = val <= 0 ? 0 : | 
|  | 226 | val >= 255 ? 2 * data->pwm1_freq : | 
|  | 227 | (val * data->pwm1_freq * 2 + 127) / 255; | 
|  | 228 | i2c_smbus_write_byte_data(client, LM63_REG_PWM_VALUE, data->pwm1_value); | 
|  | 229 | up(&data->update_lock); | 
|  | 230 | return count; | 
|  | 231 | } | 
|  | 232 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 233 | static ssize_t show_pwm1_enable(struct device *dev, struct device_attribute *dummy, | 
|  | 234 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { | 
|  | 236 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 237 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); | 
|  | 238 | } | 
|  | 239 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 240 | static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr, | 
|  | 241 | char *buf) | 
|  | 242 | { | 
|  | 243 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 244 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 245 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 248 | static ssize_t set_temp8(struct device *dev, struct device_attribute *dummy, | 
|  | 249 | const char *buf, size_t count) | 
|  | 250 | { | 
|  | 251 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 252 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 253 | long val = simple_strtol(buf, NULL, 10); | 
|  | 254 |  | 
|  | 255 | down(&data->update_lock); | 
|  | 256 | data->temp8[1] = TEMP8_TO_REG(val); | 
|  | 257 | i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, data->temp8[1]); | 
|  | 258 | up(&data->update_lock); | 
|  | 259 | return count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, | 
|  | 263 | char *buf) | 
|  | 264 | { | 
|  | 265 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 266 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 267 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index])); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 269 |  | 
|  | 270 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, | 
|  | 271 | const char *buf, size_t count) | 
|  | 272 | { | 
|  | 273 | static const u8 reg[4] = { | 
|  | 274 | LM63_REG_REMOTE_LOW_MSB, | 
|  | 275 | LM63_REG_REMOTE_LOW_LSB, | 
|  | 276 | LM63_REG_REMOTE_HIGH_MSB, | 
|  | 277 | LM63_REG_REMOTE_HIGH_LSB, | 
|  | 278 | }; | 
|  | 279 |  | 
|  | 280 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); | 
|  | 281 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 282 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 283 | long val = simple_strtol(buf, NULL, 10); | 
|  | 284 | int nr = attr->index; | 
|  | 285 |  | 
|  | 286 | down(&data->update_lock); | 
|  | 287 | data->temp11[nr] = TEMP11_TO_REG(val); | 
|  | 288 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], | 
|  | 289 | data->temp11[nr] >> 8); | 
|  | 290 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], | 
|  | 291 | data->temp11[nr] & 0xff); | 
|  | 292 | up(&data->update_lock); | 
|  | 293 | return count; | 
|  | 294 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 |  | 
|  | 296 | /* Hysteresis register holds a relative value, while we want to present | 
|  | 297 | an absolute to user-space */ | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 298 | static ssize_t show_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, | 
|  | 299 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { | 
|  | 301 | struct lm63_data *data = lm63_update_device(dev); | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 302 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | /* And now the other way around, user-space provides an absolute | 
|  | 307 | hysteresis value and we have to store a relative one */ | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 308 | static ssize_t set_temp2_crit_hyst(struct device *dev, struct device_attribute *dummy, | 
|  | 309 | const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { | 
|  | 311 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 312 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 313 | long val = simple_strtol(buf, NULL, 10); | 
|  | 314 | long hyst; | 
|  | 315 |  | 
|  | 316 | down(&data->update_lock); | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 317 | hyst = TEMP8_FROM_REG(data->temp8[2]) - val; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, | 
|  | 319 | HYST_TO_REG(hyst)); | 
|  | 320 | up(&data->update_lock); | 
|  | 321 | return count; | 
|  | 322 | } | 
|  | 323 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 324 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, | 
|  | 325 | char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { | 
|  | 327 | struct lm63_data *data = lm63_update_device(dev); | 
|  | 328 | return sprintf(buf, "%u\n", data->alarms); | 
|  | 329 | } | 
|  | 330 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 331 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); | 
|  | 332 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, | 
|  | 333 | set_fan, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 |  | 
|  | 335 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); | 
|  | 336 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); | 
|  | 337 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 338 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0); | 
|  | 339 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8, | 
|  | 340 | set_temp8, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 342 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); | 
|  | 343 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, | 
|  | 344 | set_temp11, 1); | 
|  | 345 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, | 
|  | 346 | set_temp11, 2); | 
|  | 347 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp8, NULL, 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, | 
|  | 349 | set_temp2_crit_hyst); | 
|  | 350 |  | 
|  | 351 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
|  | 352 |  | 
|  | 353 | /* | 
|  | 354 | * Real code | 
|  | 355 | */ | 
|  | 356 |  | 
|  | 357 | static int lm63_attach_adapter(struct i2c_adapter *adapter) | 
|  | 358 | { | 
|  | 359 | if (!(adapter->class & I2C_CLASS_HWMON)) | 
|  | 360 | return 0; | 
|  | 361 | return i2c_detect(adapter, &addr_data, lm63_detect); | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | /* | 
|  | 365 | * The following function does more than just detection. If detection | 
|  | 366 | * succeeds, it also registers the new chip. | 
|  | 367 | */ | 
|  | 368 | static int lm63_detect(struct i2c_adapter *adapter, int address, int kind) | 
|  | 369 | { | 
|  | 370 | struct i2c_client *new_client; | 
|  | 371 | struct lm63_data *data; | 
|  | 372 | int err = 0; | 
|  | 373 |  | 
|  | 374 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 
|  | 375 | goto exit; | 
|  | 376 |  | 
|  | 377 | if (!(data = kmalloc(sizeof(struct lm63_data), GFP_KERNEL))) { | 
|  | 378 | err = -ENOMEM; | 
|  | 379 | goto exit; | 
|  | 380 | } | 
|  | 381 | memset(data, 0, sizeof(struct lm63_data)); | 
|  | 382 |  | 
|  | 383 | /* The common I2C client data is placed right before the | 
|  | 384 | LM63-specific data. */ | 
|  | 385 | new_client = &data->client; | 
|  | 386 | i2c_set_clientdata(new_client, data); | 
|  | 387 | new_client->addr = address; | 
|  | 388 | new_client->adapter = adapter; | 
|  | 389 | new_client->driver = &lm63_driver; | 
|  | 390 | new_client->flags = 0; | 
|  | 391 |  | 
|  | 392 | /* Default to an LM63 if forced */ | 
|  | 393 | if (kind == 0) | 
|  | 394 | kind = lm63; | 
|  | 395 |  | 
|  | 396 | if (kind < 0) { /* must identify */ | 
|  | 397 | u8 man_id, chip_id, reg_config1, reg_config2; | 
|  | 398 | u8 reg_alert_status, reg_alert_mask; | 
|  | 399 |  | 
|  | 400 | man_id = i2c_smbus_read_byte_data(new_client, | 
|  | 401 | LM63_REG_MAN_ID); | 
|  | 402 | chip_id = i2c_smbus_read_byte_data(new_client, | 
|  | 403 | LM63_REG_CHIP_ID); | 
|  | 404 | reg_config1 = i2c_smbus_read_byte_data(new_client, | 
|  | 405 | LM63_REG_CONFIG1); | 
|  | 406 | reg_config2 = i2c_smbus_read_byte_data(new_client, | 
|  | 407 | LM63_REG_CONFIG2); | 
|  | 408 | reg_alert_status = i2c_smbus_read_byte_data(new_client, | 
|  | 409 | LM63_REG_ALERT_STATUS); | 
|  | 410 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, | 
|  | 411 | LM63_REG_ALERT_MASK); | 
|  | 412 |  | 
|  | 413 | if (man_id == 0x01 /* National Semiconductor */ | 
|  | 414 | && chip_id == 0x41 /* LM63 */ | 
|  | 415 | && (reg_config1 & 0x18) == 0x00 | 
|  | 416 | && (reg_config2 & 0xF8) == 0x00 | 
|  | 417 | && (reg_alert_status & 0x20) == 0x00 | 
|  | 418 | && (reg_alert_mask & 0xA4) == 0xA4) { | 
|  | 419 | kind = lm63; | 
|  | 420 | } else { /* failed */ | 
|  | 421 | dev_dbg(&adapter->dev, "Unsupported chip " | 
|  | 422 | "(man_id=0x%02X, chip_id=0x%02X).\n", | 
|  | 423 | man_id, chip_id); | 
|  | 424 | goto exit_free; | 
|  | 425 | } | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | strlcpy(new_client->name, "lm63", I2C_NAME_SIZE); | 
|  | 429 | data->valid = 0; | 
|  | 430 | init_MUTEX(&data->update_lock); | 
|  | 431 |  | 
|  | 432 | /* Tell the I2C layer a new client has arrived */ | 
|  | 433 | if ((err = i2c_attach_client(new_client))) | 
|  | 434 | goto exit_free; | 
|  | 435 |  | 
|  | 436 | /* Initialize the LM63 chip */ | 
|  | 437 | lm63_init_client(new_client); | 
|  | 438 |  | 
|  | 439 | /* Register sysfs hooks */ | 
|  | 440 | if (data->config & 0x04) { /* tachometer enabled */ | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 441 | device_create_file(&new_client->dev, | 
|  | 442 | &sensor_dev_attr_fan1_input.dev_attr); | 
|  | 443 | device_create_file(&new_client->dev, | 
|  | 444 | &sensor_dev_attr_fan1_min.dev_attr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } | 
|  | 446 | device_create_file(&new_client->dev, &dev_attr_pwm1); | 
|  | 447 | device_create_file(&new_client->dev, &dev_attr_pwm1_enable); | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 448 | device_create_file(&new_client->dev, | 
|  | 449 | &sensor_dev_attr_temp1_input.dev_attr); | 
|  | 450 | device_create_file(&new_client->dev, | 
|  | 451 | &sensor_dev_attr_temp2_input.dev_attr); | 
|  | 452 | device_create_file(&new_client->dev, | 
|  | 453 | &sensor_dev_attr_temp2_min.dev_attr); | 
|  | 454 | device_create_file(&new_client->dev, | 
|  | 455 | &sensor_dev_attr_temp1_max.dev_attr); | 
|  | 456 | device_create_file(&new_client->dev, | 
|  | 457 | &sensor_dev_attr_temp2_max.dev_attr); | 
|  | 458 | device_create_file(&new_client->dev, | 
|  | 459 | &sensor_dev_attr_temp2_crit.dev_attr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst); | 
|  | 461 | device_create_file(&new_client->dev, &dev_attr_alarms); | 
|  | 462 |  | 
|  | 463 | return 0; | 
|  | 464 |  | 
|  | 465 | exit_free: | 
|  | 466 | kfree(data); | 
|  | 467 | exit: | 
|  | 468 | return err; | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | /* Idealy we shouldn't have to initialize anything, since the BIOS | 
|  | 472 | should have taken care of everything */ | 
|  | 473 | static void lm63_init_client(struct i2c_client *client) | 
|  | 474 | { | 
|  | 475 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 476 |  | 
|  | 477 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); | 
|  | 478 | data->config_fan = i2c_smbus_read_byte_data(client, | 
|  | 479 | LM63_REG_CONFIG_FAN); | 
|  | 480 |  | 
|  | 481 | /* Start converting if needed */ | 
|  | 482 | if (data->config & 0x40) { /* standby */ | 
|  | 483 | dev_dbg(&client->dev, "Switching to operational mode"); | 
|  | 484 | data->config &= 0xA7; | 
|  | 485 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, | 
|  | 486 | data->config); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | /* We may need pwm1_freq before ever updating the client data */ | 
|  | 490 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); | 
|  | 491 | if (data->pwm1_freq == 0) | 
|  | 492 | data->pwm1_freq = 1; | 
|  | 493 |  | 
|  | 494 | /* Show some debug info about the LM63 configuration */ | 
|  | 495 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", | 
|  | 496 | (data->config & 0x04) ? "tachometer input" : | 
|  | 497 | "alert output"); | 
|  | 498 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", | 
|  | 499 | (data->config_fan & 0x08) ? "1.4" : "360", | 
|  | 500 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); | 
|  | 501 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", | 
|  | 502 | (data->config_fan & 0x10) ? "low" : "high", | 
|  | 503 | (data->config_fan & 0x20) ? "manual" : "auto"); | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | static int lm63_detach_client(struct i2c_client *client) | 
|  | 507 | { | 
|  | 508 | int err; | 
|  | 509 |  | 
|  | 510 | if ((err = i2c_detach_client(client))) { | 
|  | 511 | dev_err(&client->dev, "Client deregistration failed, " | 
|  | 512 | "client not detached\n"); | 
|  | 513 | return err; | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | kfree(i2c_get_clientdata(client)); | 
|  | 517 | return 0; | 
|  | 518 | } | 
|  | 519 |  | 
|  | 520 | static struct lm63_data *lm63_update_device(struct device *dev) | 
|  | 521 | { | 
|  | 522 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 523 | struct lm63_data *data = i2c_get_clientdata(client); | 
|  | 524 |  | 
|  | 525 | down(&data->update_lock); | 
|  | 526 |  | 
|  | 527 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | 
|  | 528 | if (data->config & 0x04) { /* tachometer enabled  */ | 
|  | 529 | /* order matters for fan1_input */ | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 530 | data->fan[0] = i2c_smbus_read_byte_data(client, | 
|  | 531 | LM63_REG_TACH_COUNT_LSB) & 0xFC; | 
|  | 532 | data->fan[0] |= i2c_smbus_read_byte_data(client, | 
|  | 533 | LM63_REG_TACH_COUNT_MSB) << 8; | 
|  | 534 | data->fan[1] = (i2c_smbus_read_byte_data(client, | 
|  | 535 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) | 
|  | 536 | | (i2c_smbus_read_byte_data(client, | 
|  | 537 | LM63_REG_TACH_LIMIT_MSB) << 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } | 
|  | 539 |  | 
|  | 540 | data->pwm1_freq = i2c_smbus_read_byte_data(client, | 
|  | 541 | LM63_REG_PWM_FREQ); | 
|  | 542 | if (data->pwm1_freq == 0) | 
|  | 543 | data->pwm1_freq = 1; | 
|  | 544 | data->pwm1_value = i2c_smbus_read_byte_data(client, | 
|  | 545 | LM63_REG_PWM_VALUE); | 
|  | 546 |  | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 547 | data->temp8[0] = i2c_smbus_read_byte_data(client, | 
|  | 548 | LM63_REG_LOCAL_TEMP); | 
|  | 549 | data->temp8[1] = i2c_smbus_read_byte_data(client, | 
|  | 550 | LM63_REG_LOCAL_HIGH); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 |  | 
|  | 552 | /* order matters for temp2_input */ | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 553 | data->temp11[0] = i2c_smbus_read_byte_data(client, | 
|  | 554 | LM63_REG_REMOTE_TEMP_MSB) << 8; | 
|  | 555 | data->temp11[0] |= i2c_smbus_read_byte_data(client, | 
|  | 556 | LM63_REG_REMOTE_TEMP_LSB); | 
|  | 557 | data->temp11[1] = (i2c_smbus_read_byte_data(client, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | LM63_REG_REMOTE_LOW_MSB) << 8) | 
|  | 559 | | i2c_smbus_read_byte_data(client, | 
|  | 560 | LM63_REG_REMOTE_LOW_LSB); | 
| Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 561 | data->temp11[2] = (i2c_smbus_read_byte_data(client, | 
|  | 562 | LM63_REG_REMOTE_HIGH_MSB) << 8) | 
|  | 563 | | i2c_smbus_read_byte_data(client, | 
|  | 564 | LM63_REG_REMOTE_HIGH_LSB); | 
|  | 565 | data->temp8[2] = i2c_smbus_read_byte_data(client, | 
|  | 566 | LM63_REG_REMOTE_TCRIT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, | 
|  | 568 | LM63_REG_REMOTE_TCRIT_HYST); | 
|  | 569 |  | 
|  | 570 | data->alarms = i2c_smbus_read_byte_data(client, | 
|  | 571 | LM63_REG_ALERT_STATUS) & 0x7F; | 
|  | 572 |  | 
|  | 573 | data->last_updated = jiffies; | 
|  | 574 | data->valid = 1; | 
|  | 575 | } | 
|  | 576 |  | 
|  | 577 | up(&data->update_lock); | 
|  | 578 |  | 
|  | 579 | return data; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | static int __init sensors_lm63_init(void) | 
|  | 583 | { | 
|  | 584 | return i2c_add_driver(&lm63_driver); | 
|  | 585 | } | 
|  | 586 |  | 
|  | 587 | static void __exit sensors_lm63_exit(void) | 
|  | 588 | { | 
|  | 589 | i2c_del_driver(&lm63_driver); | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); | 
|  | 593 | MODULE_DESCRIPTION("LM63 driver"); | 
|  | 594 | MODULE_LICENSE("GPL"); | 
|  | 595 |  | 
|  | 596 | module_init(sensors_lm63_init); | 
|  | 597 | module_exit(sensors_lm63_exit); |