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