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