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