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