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