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