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