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