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