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 | |
Matthew Garrett | 10f2ed3 | 2010-05-27 19:58:38 +0200 | [diff] [blame] | 56 | static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * The LM63 registers |
| 60 | */ |
| 61 | |
| 62 | #define LM63_REG_CONFIG1 0x03 |
| 63 | #define LM63_REG_CONFIG2 0xBF |
| 64 | #define LM63_REG_CONFIG_FAN 0x4A |
| 65 | |
| 66 | #define LM63_REG_TACH_COUNT_MSB 0x47 |
| 67 | #define LM63_REG_TACH_COUNT_LSB 0x46 |
| 68 | #define LM63_REG_TACH_LIMIT_MSB 0x49 |
| 69 | #define LM63_REG_TACH_LIMIT_LSB 0x48 |
| 70 | |
| 71 | #define LM63_REG_PWM_VALUE 0x4C |
| 72 | #define LM63_REG_PWM_FREQ 0x4D |
| 73 | |
| 74 | #define LM63_REG_LOCAL_TEMP 0x00 |
| 75 | #define LM63_REG_LOCAL_HIGH 0x05 |
| 76 | |
| 77 | #define LM63_REG_REMOTE_TEMP_MSB 0x01 |
| 78 | #define LM63_REG_REMOTE_TEMP_LSB 0x10 |
| 79 | #define LM63_REG_REMOTE_OFFSET_MSB 0x11 |
| 80 | #define LM63_REG_REMOTE_OFFSET_LSB 0x12 |
| 81 | #define LM63_REG_REMOTE_HIGH_MSB 0x07 |
| 82 | #define LM63_REG_REMOTE_HIGH_LSB 0x13 |
| 83 | #define LM63_REG_REMOTE_LOW_MSB 0x08 |
| 84 | #define LM63_REG_REMOTE_LOW_LSB 0x14 |
| 85 | #define LM63_REG_REMOTE_TCRIT 0x19 |
| 86 | #define LM63_REG_REMOTE_TCRIT_HYST 0x21 |
| 87 | |
| 88 | #define LM63_REG_ALERT_STATUS 0x02 |
| 89 | #define LM63_REG_ALERT_MASK 0x16 |
| 90 | |
| 91 | #define LM63_REG_MAN_ID 0xFE |
| 92 | #define LM63_REG_CHIP_ID 0xFF |
| 93 | |
| 94 | /* |
| 95 | * Conversions and various macros |
| 96 | * For tachometer counts, the LM63 uses 16-bit values. |
| 97 | * For local temperature and high limit, remote critical limit and hysteresis |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 98 | * value, it uses signed 8-bit values with LSB = 1 degree Celsius. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * 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] | 100 | * with LSB = 0.125 degree Celsius, left-justified in 16-bit registers. |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 101 | * For LM64 the actual remote diode temperature is 16 degree Celsius higher |
| 102 | * than the register reading. Remote temperature setpoints have to be |
| 103 | * adapted accordingly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | #define FAN_FROM_REG(reg) ((reg) == 0xFFFC || (reg) == 0 ? 0 : \ |
| 107 | 5400000 / (reg)) |
| 108 | #define FAN_TO_REG(val) ((val) <= 82 ? 0xFFFC : \ |
| 109 | (5400000 / (val)) & 0xFFFC) |
| 110 | #define TEMP8_FROM_REG(reg) ((reg) * 1000) |
| 111 | #define TEMP8_TO_REG(val) ((val) <= -128000 ? -128 : \ |
| 112 | (val) >= 127000 ? 127 : \ |
| 113 | (val) < 0 ? ((val) - 500) / 1000 : \ |
| 114 | ((val) + 500) / 1000) |
| 115 | #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) |
| 116 | #define TEMP11_TO_REG(val) ((val) <= -128000 ? 0x8000 : \ |
| 117 | (val) >= 127875 ? 0x7FE0 : \ |
| 118 | (val) < 0 ? ((val) - 62) / 125 * 32 : \ |
| 119 | ((val) + 62) / 125 * 32) |
| 120 | #define HYST_TO_REG(val) ((val) <= 0 ? 0 : \ |
| 121 | (val) >= 127000 ? 127 : \ |
| 122 | ((val) + 500) / 1000) |
| 123 | |
| 124 | /* |
| 125 | * Functions declaration |
| 126 | */ |
| 127 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 128 | static int lm63_probe(struct i2c_client *client, |
| 129 | const struct i2c_device_id *id); |
| 130 | static int lm63_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | static struct lm63_data *lm63_update_device(struct device *dev); |
| 133 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 134 | static int lm63_detect(struct i2c_client *client, struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | static void lm63_init_client(struct i2c_client *client); |
| 136 | |
Matthew Garrett | 10f2ed3 | 2010-05-27 19:58:38 +0200 | [diff] [blame] | 137 | enum chips { lm63, lm64 }; |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Driver data (common to all clients) |
| 141 | */ |
| 142 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 143 | static const struct i2c_device_id lm63_id[] = { |
Matthew Garrett | 10f2ed3 | 2010-05-27 19:58:38 +0200 | [diff] [blame] | 144 | { "lm63", lm63 }, |
| 145 | { "lm64", lm64 }, |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 146 | { } |
| 147 | }; |
| 148 | MODULE_DEVICE_TABLE(i2c, lm63_id); |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static struct i2c_driver lm63_driver = { |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 151 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 152 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 153 | .name = "lm63", |
| 154 | }, |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 155 | .probe = lm63_probe, |
| 156 | .remove = lm63_remove, |
| 157 | .id_table = lm63_id, |
| 158 | .detect = lm63_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 159 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * Client data (each client gets its own) |
| 164 | */ |
| 165 | |
| 166 | struct lm63_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 167 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 168 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | char valid; /* zero until following fields are valid */ |
| 170 | unsigned long last_updated; /* in jiffies */ |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 171 | int kind; |
| 172 | int temp2_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* registers values */ |
| 175 | u8 config, config_fan; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 176 | u16 fan[2]; /* 0: input |
| 177 | 1: low limit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | u8 pwm1_freq; |
| 179 | u8 pwm1_value; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 180 | s8 temp8[3]; /* 0: local input |
| 181 | 1: local high limit |
| 182 | 2: remote critical limit */ |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 183 | s16 temp11[4]; /* 0: remote input |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 184 | 1: remote low limit |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 185 | 2: remote high limit |
| 186 | 3: remote offset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | u8 temp2_crit_hyst; |
| 188 | u8 alarms; |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * Sysfs callback functions and files |
| 193 | */ |
| 194 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 195 | static ssize_t show_fan(struct device *dev, struct device_attribute *devattr, |
| 196 | char *buf) |
| 197 | { |
| 198 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 199 | struct lm63_data *data = lm63_update_device(dev); |
| 200 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 203 | static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, |
| 204 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | struct i2c_client *client = to_i2c_client(dev); |
| 207 | struct lm63_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 208 | unsigned long val; |
| 209 | int err; |
| 210 | |
| 211 | err = kstrtoul(buf, 10, &val); |
| 212 | if (err) |
| 213 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 215 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 216 | data->fan[1] = FAN_TO_REG(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_LSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 218 | data->fan[1] & 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | i2c_smbus_write_byte_data(client, LM63_REG_TACH_LIMIT_MSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 220 | data->fan[1] >> 8); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 221 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return count; |
| 223 | } |
| 224 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 225 | static ssize_t show_pwm1(struct device *dev, struct device_attribute *dummy, |
| 226 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | struct lm63_data *data = lm63_update_device(dev); |
| 229 | return sprintf(buf, "%d\n", data->pwm1_value >= 2 * data->pwm1_freq ? |
| 230 | 255 : (data->pwm1_value * 255 + data->pwm1_freq) / |
| 231 | (2 * data->pwm1_freq)); |
| 232 | } |
| 233 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 234 | static ssize_t set_pwm1(struct device *dev, struct device_attribute *dummy, |
| 235 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | struct i2c_client *client = to_i2c_client(dev); |
| 238 | struct lm63_data *data = i2c_get_clientdata(client); |
| 239 | unsigned long val; |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 240 | int err; |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (!(data->config_fan & 0x20)) /* register is read-only */ |
| 243 | return -EPERM; |
| 244 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 245 | err = kstrtoul(buf, 10, &val); |
| 246 | if (err) |
| 247 | return err; |
| 248 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 249 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | data->pwm1_value = val <= 0 ? 0 : |
| 251 | val >= 255 ? 2 * data->pwm1_freq : |
| 252 | (val * data->pwm1_freq * 2 + 127) / 255; |
| 253 | 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] | 254 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return count; |
| 256 | } |
| 257 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 258 | static ssize_t show_pwm1_enable(struct device *dev, |
| 259 | struct device_attribute *dummy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
| 261 | struct lm63_data *data = lm63_update_device(dev); |
| 262 | return sprintf(buf, "%d\n", data->config_fan & 0x20 ? 1 : 2); |
| 263 | } |
| 264 | |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 265 | /* |
| 266 | * There are 8bit registers for both local(temp1) and remote(temp2) sensor. |
| 267 | * For remote sensor registers temp2_offset has to be considered, |
| 268 | * for local sensor it must not. |
| 269 | * So we need separate 8bit accessors for local and remote sensor. |
| 270 | */ |
| 271 | static ssize_t show_local_temp8(struct device *dev, |
| 272 | struct device_attribute *devattr, |
| 273 | char *buf) |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 274 | { |
| 275 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 276 | struct lm63_data *data = lm63_update_device(dev); |
| 277 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 280 | static ssize_t show_remote_temp8(struct device *dev, |
| 281 | struct device_attribute *devattr, |
| 282 | char *buf) |
| 283 | { |
| 284 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 285 | struct lm63_data *data = lm63_update_device(dev); |
| 286 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index]) |
| 287 | + data->temp2_offset); |
| 288 | } |
| 289 | |
| 290 | static ssize_t set_local_temp8(struct device *dev, |
| 291 | struct device_attribute *dummy, |
| 292 | const char *buf, size_t count) |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 293 | { |
| 294 | struct i2c_client *client = to_i2c_client(dev); |
| 295 | struct lm63_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 296 | long val; |
| 297 | int err; |
| 298 | |
| 299 | err = kstrtol(buf, 10, &val); |
| 300 | if (err) |
| 301 | return err; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 302 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 303 | mutex_lock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 304 | data->temp8[1] = TEMP8_TO_REG(val); |
| 305 | 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] | 306 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 307 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 309 | |
| 310 | static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, |
| 311 | char *buf) |
| 312 | { |
| 313 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 314 | struct lm63_data *data = lm63_update_device(dev); |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 315 | return sprintf(buf, "%d\n", TEMP11_FROM_REG(data->temp11[attr->index]) |
| 316 | + data->temp2_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 318 | |
| 319 | static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr, |
| 320 | const char *buf, size_t count) |
| 321 | { |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 322 | static const u8 reg[6] = { |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 323 | LM63_REG_REMOTE_LOW_MSB, |
| 324 | LM63_REG_REMOTE_LOW_LSB, |
| 325 | LM63_REG_REMOTE_HIGH_MSB, |
| 326 | LM63_REG_REMOTE_HIGH_LSB, |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 327 | LM63_REG_REMOTE_OFFSET_MSB, |
| 328 | LM63_REG_REMOTE_OFFSET_LSB, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 332 | struct i2c_client *client = to_i2c_client(dev); |
| 333 | struct lm63_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 334 | long val; |
| 335 | int err; |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 336 | int nr = attr->index; |
| 337 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 338 | err = kstrtol(buf, 10, &val); |
| 339 | if (err) |
| 340 | return err; |
| 341 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 342 | mutex_lock(&data->update_lock); |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 343 | data->temp11[nr] = TEMP11_TO_REG(val - data->temp2_offset); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 344 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2], |
| 345 | data->temp11[nr] >> 8); |
| 346 | i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1], |
| 347 | data->temp11[nr] & 0xff); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 348 | mutex_unlock(&data->update_lock); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 349 | return count; |
| 350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 352 | /* |
| 353 | * Hysteresis register holds a relative value, while we want to present |
| 354 | * an absolute to user-space |
| 355 | */ |
| 356 | static ssize_t show_temp2_crit_hyst(struct device *dev, |
| 357 | struct device_attribute *dummy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | struct lm63_data *data = lm63_update_device(dev); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 360 | return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[2]) |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 361 | + data->temp2_offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | - TEMP8_FROM_REG(data->temp2_crit_hyst)); |
| 363 | } |
| 364 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 365 | /* |
| 366 | * And now the other way around, user-space provides an absolute |
| 367 | * hysteresis value and we have to store a relative one |
| 368 | */ |
| 369 | static ssize_t set_temp2_crit_hyst(struct device *dev, |
| 370 | struct device_attribute *dummy, |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 371 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct i2c_client *client = to_i2c_client(dev); |
| 374 | struct lm63_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 375 | long val; |
| 376 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | long hyst; |
| 378 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 379 | err = kstrtol(buf, 10, &val); |
| 380 | if (err) |
| 381 | return err; |
| 382 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 383 | mutex_lock(&data->update_lock); |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 384 | hyst = TEMP8_FROM_REG(data->temp8[2]) + data->temp2_offset - val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | i2c_smbus_write_byte_data(client, LM63_REG_REMOTE_TCRIT_HYST, |
| 386 | HYST_TO_REG(hyst)); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 387 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return count; |
| 389 | } |
| 390 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 391 | static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy, |
| 392 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | struct lm63_data *data = lm63_update_device(dev); |
| 395 | return sprintf(buf, "%u\n", data->alarms); |
| 396 | } |
| 397 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 398 | static ssize_t show_alarm(struct device *dev, struct device_attribute *devattr, |
| 399 | char *buf) |
| 400 | { |
| 401 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 402 | struct lm63_data *data = lm63_update_device(dev); |
| 403 | int bitnr = attr->index; |
| 404 | |
| 405 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 406 | } |
| 407 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 408 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); |
| 409 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan, |
| 410 | set_fan, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm1, set_pwm1); |
| 413 | static DEVICE_ATTR(pwm1_enable, S_IRUGO, show_pwm1_enable, NULL); |
| 414 | |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 415 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_local_temp8, NULL, 0); |
| 416 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_local_temp8, |
| 417 | set_local_temp8, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 419 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0); |
| 420 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11, |
| 421 | set_temp11, 1); |
| 422 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11, |
| 423 | set_temp11, 2); |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 424 | static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11, |
| 425 | set_temp11, 3); |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 426 | /* |
| 427 | * On LM63, temp2_crit can be set only once, which should be job |
| 428 | * of the bootloader. |
| 429 | */ |
| 430 | static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_remote_temp8, |
| 431 | NULL, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | static DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_temp2_crit_hyst, |
| 433 | set_temp2_crit_hyst); |
| 434 | |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 435 | /* Individual alarm files */ |
| 436 | static SENSOR_DEVICE_ATTR(fan1_min_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 437 | 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] | 438 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
Jean Delvare | 2d45771 | 2006-09-24 20:52:15 +0200 | [diff] [blame] | 439 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 440 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 441 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 442 | /* Raw alarm file for compatibility */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 444 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 445 | static struct attribute *lm63_attributes[] = { |
| 446 | &dev_attr_pwm1.attr, |
| 447 | &dev_attr_pwm1_enable.attr, |
| 448 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 449 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 450 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 451 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 452 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 453 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 454 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 455 | &dev_attr_temp2_crit_hyst.attr, |
| 456 | |
| 457 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 458 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 459 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 460 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 461 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 462 | &dev_attr_alarms.attr, |
| 463 | NULL |
| 464 | }; |
| 465 | |
| 466 | static const struct attribute_group lm63_group = { |
| 467 | .attrs = lm63_attributes, |
| 468 | }; |
| 469 | |
| 470 | static struct attribute *lm63_attributes_fan1[] = { |
| 471 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 472 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 473 | |
| 474 | &sensor_dev_attr_fan1_min_alarm.dev_attr.attr, |
| 475 | NULL |
| 476 | }; |
| 477 | |
| 478 | static const struct attribute_group lm63_group_fan1 = { |
| 479 | .attrs = lm63_attributes_fan1, |
| 480 | }; |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* |
| 483 | * Real code |
| 484 | */ |
| 485 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 486 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 487 | static int lm63_detect(struct i2c_client *new_client, |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 488 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 490 | struct i2c_adapter *adapter = new_client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 491 | u8 man_id, chip_id, reg_config1, reg_config2; |
| 492 | u8 reg_alert_status, reg_alert_mask; |
Matthew Garrett | 10f2ed3 | 2010-05-27 19:58:38 +0200 | [diff] [blame] | 493 | int address = new_client->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 496 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 498 | man_id = i2c_smbus_read_byte_data(new_client, LM63_REG_MAN_ID); |
| 499 | chip_id = i2c_smbus_read_byte_data(new_client, LM63_REG_CHIP_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 501 | reg_config1 = i2c_smbus_read_byte_data(new_client, |
| 502 | LM63_REG_CONFIG1); |
| 503 | reg_config2 = i2c_smbus_read_byte_data(new_client, |
| 504 | LM63_REG_CONFIG2); |
| 505 | reg_alert_status = i2c_smbus_read_byte_data(new_client, |
| 506 | LM63_REG_ALERT_STATUS); |
| 507 | reg_alert_mask = i2c_smbus_read_byte_data(new_client, |
| 508 | LM63_REG_ALERT_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 510 | if (man_id != 0x01 /* National Semiconductor */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 511 | || (reg_config1 & 0x18) != 0x00 |
| 512 | || (reg_config2 & 0xF8) != 0x00 |
| 513 | || (reg_alert_status & 0x20) != 0x00 |
| 514 | || (reg_alert_mask & 0xA4) != 0xA4) { |
| 515 | dev_dbg(&adapter->dev, |
| 516 | "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n", |
| 517 | man_id, chip_id); |
| 518 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Matthew Garrett | 10f2ed3 | 2010-05-27 19:58:38 +0200 | [diff] [blame] | 521 | if (chip_id == 0x41 && address == 0x4c) |
| 522 | strlcpy(info->type, "lm63", I2C_NAME_SIZE); |
| 523 | else if (chip_id == 0x51 && (address == 0x18 || address == 0x4e)) |
| 524 | strlcpy(info->type, "lm64", I2C_NAME_SIZE); |
| 525 | else |
| 526 | return -ENODEV; |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int lm63_probe(struct i2c_client *new_client, |
| 532 | const struct i2c_device_id *id) |
| 533 | { |
| 534 | struct lm63_data *data; |
| 535 | int err; |
| 536 | |
| 537 | data = kzalloc(sizeof(struct lm63_data), GFP_KERNEL); |
| 538 | if (!data) { |
| 539 | err = -ENOMEM; |
| 540 | goto exit; |
| 541 | } |
| 542 | |
| 543 | i2c_set_clientdata(new_client, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | data->valid = 0; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 545 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Dirk Eibach | 2778fb1 | 2011-02-09 04:51:34 -0500 | [diff] [blame] | 547 | /* Set the device type */ |
| 548 | data->kind = id->driver_data; |
| 549 | if (data->kind == lm64) |
| 550 | data->temp2_offset = 16000; |
| 551 | |
| 552 | /* Initialize chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | lm63_init_client(new_client); |
| 554 | |
| 555 | /* Register sysfs hooks */ |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 556 | err = sysfs_create_group(&new_client->dev.kobj, &lm63_group); |
| 557 | if (err) |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 558 | goto exit_free; |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 559 | if (data->config & 0x04) { /* tachometer enabled */ |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 560 | err = sysfs_create_group(&new_client->dev.kobj, |
| 561 | &lm63_group_fan1); |
| 562 | if (err) |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 563 | goto exit_remove_files; |
| 564 | } |
| 565 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 566 | data->hwmon_dev = hwmon_device_register(&new_client->dev); |
| 567 | if (IS_ERR(data->hwmon_dev)) { |
| 568 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 569 | goto exit_remove_files; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | return 0; |
| 573 | |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 574 | exit_remove_files: |
| 575 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group); |
| 576 | sysfs_remove_group(&new_client->dev.kobj, &lm63_group_fan1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | exit_free: |
| 578 | kfree(data); |
| 579 | exit: |
| 580 | return err; |
| 581 | } |
| 582 | |
Guenter Roeck | 662bda2 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 583 | /* |
| 584 | * Ideally we shouldn't have to initialize anything, since the BIOS |
| 585 | * should have taken care of everything |
| 586 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | static void lm63_init_client(struct i2c_client *client) |
| 588 | { |
| 589 | struct lm63_data *data = i2c_get_clientdata(client); |
| 590 | |
| 591 | data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); |
| 592 | data->config_fan = i2c_smbus_read_byte_data(client, |
| 593 | LM63_REG_CONFIG_FAN); |
| 594 | |
| 595 | /* Start converting if needed */ |
| 596 | if (data->config & 0x40) { /* standby */ |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 597 | dev_dbg(&client->dev, "Switching to operational mode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | data->config &= 0xA7; |
| 599 | i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, |
| 600 | data->config); |
| 601 | } |
| 602 | |
| 603 | /* We may need pwm1_freq before ever updating the client data */ |
| 604 | data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); |
| 605 | if (data->pwm1_freq == 0) |
| 606 | data->pwm1_freq = 1; |
| 607 | |
| 608 | /* Show some debug info about the LM63 configuration */ |
| 609 | dev_dbg(&client->dev, "Alert/tach pin configured for %s\n", |
| 610 | (data->config & 0x04) ? "tachometer input" : |
| 611 | "alert output"); |
| 612 | dev_dbg(&client->dev, "PWM clock %s kHz, output frequency %u Hz\n", |
| 613 | (data->config_fan & 0x08) ? "1.4" : "360", |
| 614 | ((data->config_fan & 0x08) ? 700 : 180000) / data->pwm1_freq); |
| 615 | dev_dbg(&client->dev, "PWM output active %s, %s mode\n", |
| 616 | (data->config_fan & 0x10) ? "low" : "high", |
| 617 | (data->config_fan & 0x20) ? "manual" : "auto"); |
| 618 | } |
| 619 | |
Jean Delvare | d5957be | 2008-07-16 19:30:13 +0200 | [diff] [blame] | 620 | static int lm63_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 622 | struct lm63_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 624 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | 0e39e01 | 2006-09-24 21:16:40 +0200 | [diff] [blame] | 625 | sysfs_remove_group(&client->dev.kobj, &lm63_group); |
| 626 | sysfs_remove_group(&client->dev.kobj, &lm63_group_fan1); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 627 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 628 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static struct lm63_data *lm63_update_device(struct device *dev) |
| 633 | { |
| 634 | struct i2c_client *client = to_i2c_client(dev); |
| 635 | struct lm63_data *data = i2c_get_clientdata(client); |
| 636 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 637 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 640 | if (data->config & 0x04) { /* tachometer enabled */ |
| 641 | /* order matters for fan1_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 642 | data->fan[0] = i2c_smbus_read_byte_data(client, |
| 643 | LM63_REG_TACH_COUNT_LSB) & 0xFC; |
| 644 | data->fan[0] |= i2c_smbus_read_byte_data(client, |
| 645 | LM63_REG_TACH_COUNT_MSB) << 8; |
| 646 | data->fan[1] = (i2c_smbus_read_byte_data(client, |
| 647 | LM63_REG_TACH_LIMIT_LSB) & 0xFC) |
| 648 | | (i2c_smbus_read_byte_data(client, |
| 649 | LM63_REG_TACH_LIMIT_MSB) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | data->pwm1_freq = i2c_smbus_read_byte_data(client, |
| 653 | LM63_REG_PWM_FREQ); |
| 654 | if (data->pwm1_freq == 0) |
| 655 | data->pwm1_freq = 1; |
| 656 | data->pwm1_value = i2c_smbus_read_byte_data(client, |
| 657 | LM63_REG_PWM_VALUE); |
| 658 | |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 659 | data->temp8[0] = i2c_smbus_read_byte_data(client, |
| 660 | LM63_REG_LOCAL_TEMP); |
| 661 | data->temp8[1] = i2c_smbus_read_byte_data(client, |
| 662 | LM63_REG_LOCAL_HIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* order matters for temp2_input */ |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 665 | data->temp11[0] = i2c_smbus_read_byte_data(client, |
| 666 | LM63_REG_REMOTE_TEMP_MSB) << 8; |
| 667 | data->temp11[0] |= i2c_smbus_read_byte_data(client, |
| 668 | LM63_REG_REMOTE_TEMP_LSB); |
| 669 | data->temp11[1] = (i2c_smbus_read_byte_data(client, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | LM63_REG_REMOTE_LOW_MSB) << 8) |
| 671 | | i2c_smbus_read_byte_data(client, |
| 672 | LM63_REG_REMOTE_LOW_LSB); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 673 | data->temp11[2] = (i2c_smbus_read_byte_data(client, |
| 674 | LM63_REG_REMOTE_HIGH_MSB) << 8) |
| 675 | | i2c_smbus_read_byte_data(client, |
| 676 | LM63_REG_REMOTE_HIGH_LSB); |
Guenter Roeck | 786375f | 2012-01-16 22:51:45 +0100 | [diff] [blame^] | 677 | data->temp11[3] = (i2c_smbus_read_byte_data(client, |
| 678 | LM63_REG_REMOTE_OFFSET_MSB) << 8) |
| 679 | | i2c_smbus_read_byte_data(client, |
| 680 | LM63_REG_REMOTE_OFFSET_LSB); |
Jean Delvare | bc51ae1 | 2005-06-05 20:32:27 +0200 | [diff] [blame] | 681 | data->temp8[2] = i2c_smbus_read_byte_data(client, |
| 682 | LM63_REG_REMOTE_TCRIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | data->temp2_crit_hyst = i2c_smbus_read_byte_data(client, |
| 684 | LM63_REG_REMOTE_TCRIT_HYST); |
| 685 | |
| 686 | data->alarms = i2c_smbus_read_byte_data(client, |
| 687 | LM63_REG_ALERT_STATUS) & 0x7F; |
| 688 | |
| 689 | data->last_updated = jiffies; |
| 690 | data->valid = 1; |
| 691 | } |
| 692 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 693 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | return data; |
| 696 | } |
| 697 | |
| 698 | static int __init sensors_lm63_init(void) |
| 699 | { |
| 700 | return i2c_add_driver(&lm63_driver); |
| 701 | } |
| 702 | |
| 703 | static void __exit sensors_lm63_exit(void) |
| 704 | { |
| 705 | i2c_del_driver(&lm63_driver); |
| 706 | } |
| 707 | |
| 708 | MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); |
| 709 | MODULE_DESCRIPTION("LM63 driver"); |
| 710 | MODULE_LICENSE("GPL"); |
| 711 | |
| 712 | module_init(sensors_lm63_init); |
| 713 | module_exit(sensors_lm63_exit); |