Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm80.c - From lm_sensors, Linux kernel modules for hardware |
Guenter Roeck | 1160631 | 2012-01-19 11:02:20 -0800 | [diff] [blame] | 3 | * monitoring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
Guenter Roeck | 1160631 | 2012-01-19 11:02:20 -0800 | [diff] [blame] | 5 | * and Philip Edelbrock <phil@netroedge.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/hwmon.h> |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 30 | #include <linux/hwmon-sysfs.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 31 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 35 | static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, |
| 36 | 0x2e, 0x2f, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* Many LM80 constants specified below */ |
| 39 | |
| 40 | /* The LM80 registers */ |
| 41 | #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2) |
| 42 | #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2) |
| 43 | #define LM80_REG_IN(nr) (0x20 + (nr)) |
| 44 | |
| 45 | #define LM80_REG_FAN1 0x28 |
| 46 | #define LM80_REG_FAN2 0x29 |
| 47 | #define LM80_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 48 | |
| 49 | #define LM80_REG_TEMP 0x27 |
| 50 | #define LM80_REG_TEMP_HOT_MAX 0x38 |
| 51 | #define LM80_REG_TEMP_HOT_HYST 0x39 |
| 52 | #define LM80_REG_TEMP_OS_MAX 0x3a |
| 53 | #define LM80_REG_TEMP_OS_HYST 0x3b |
| 54 | |
| 55 | #define LM80_REG_CONFIG 0x00 |
| 56 | #define LM80_REG_ALARM1 0x01 |
| 57 | #define LM80_REG_ALARM2 0x02 |
| 58 | #define LM80_REG_MASK1 0x03 |
| 59 | #define LM80_REG_MASK2 0x04 |
| 60 | #define LM80_REG_FANDIV 0x05 |
| 61 | #define LM80_REG_RES 0x06 |
| 62 | |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 63 | #define LM96080_REG_CONV_RATE 0x07 |
| 64 | #define LM96080_REG_MAN_ID 0x3e |
| 65 | #define LM96080_REG_DEV_ID 0x3f |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Guenter Roeck | 1160631 | 2012-01-19 11:02:20 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Conversions. Rounding and limit checking is only done on the TO_REG |
| 70 | * variants. Note that you should be a bit careful with which arguments |
| 71 | * these macros are called: arguments may be evaluated more than once. |
| 72 | * Fixing this is just not worth it. |
| 73 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 75 | #define IN_TO_REG(val) (clamp_val(((val) + 5) / 10, 0, 255)) |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 76 | #define IN_FROM_REG(val) ((val) * 10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div) |
| 79 | { |
| 80 | if (rpm == 0) |
| 81 | return 255; |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 82 | rpm = clamp_val(rpm, 1, 1000000); |
| 83 | return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 86 | #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \ |
| 87 | (val) == 255 ? 0 : 1350000/((div) * (val))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Guenter Roeck | 688a317 | 2014-04-13 09:30:09 -0700 | [diff] [blame] | 89 | #define TEMP_FROM_REG(reg) ((reg) * 125 / 32) |
| 90 | #define TEMP_TO_REG(temp) (DIV_ROUND_CLOSEST(clamp_val((temp), \ |
| 91 | -128000, 127000), 1000) << 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | #define DIV_FROM_REG(val) (1 << (val)) |
| 94 | |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 95 | enum temp_index { |
| 96 | t_input = 0, |
| 97 | t_hot_max, |
| 98 | t_hot_hyst, |
| 99 | t_os_max, |
| 100 | t_os_hyst, |
| 101 | t_num_temp |
| 102 | }; |
| 103 | |
| 104 | static const u8 temp_regs[t_num_temp] = { |
| 105 | [t_input] = LM80_REG_TEMP, |
| 106 | [t_hot_max] = LM80_REG_TEMP_HOT_MAX, |
| 107 | [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST, |
| 108 | [t_os_max] = LM80_REG_TEMP_OS_MAX, |
| 109 | [t_os_hyst] = LM80_REG_TEMP_OS_HYST, |
| 110 | }; |
| 111 | |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 112 | enum in_index { |
| 113 | i_input = 0, |
| 114 | i_max, |
| 115 | i_min, |
| 116 | i_num_in |
| 117 | }; |
| 118 | |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 119 | enum fan_index { |
| 120 | f_input, |
| 121 | f_min, |
| 122 | f_num_fan |
| 123 | }; |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Client data (each client gets its own) |
| 127 | */ |
| 128 | |
| 129 | struct lm80_data { |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 130 | struct i2c_client *client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 131 | struct mutex update_lock; |
Frans Meulenbroeks | 96585f1 | 2012-01-10 15:49:36 +0100 | [diff] [blame] | 132 | char error; /* !=0 if error occurred during last update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | char valid; /* !=0 if following fields are valid */ |
| 134 | unsigned long last_updated; /* In jiffies */ |
| 135 | |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 136 | u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */ |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 137 | u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | u8 fan_div[2]; /* Register encoding, shifted right */ |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 139 | s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | u16 alarms; /* Register encoding, combined */ |
| 141 | }; |
| 142 | |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 143 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | * Functions declaration |
| 145 | */ |
| 146 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 147 | static int lm80_probe(struct i2c_client *client, |
| 148 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 149 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | static void lm80_init_client(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | static struct lm80_data *lm80_update_device(struct device *dev); |
| 152 | static int lm80_read_value(struct i2c_client *client, u8 reg); |
| 153 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); |
| 154 | |
| 155 | /* |
| 156 | * Driver data (common to all clients) |
| 157 | */ |
| 158 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 159 | static const struct i2c_device_id lm80_id[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 160 | { "lm80", 0 }, |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 161 | { "lm96080", 1 }, |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 162 | { } |
| 163 | }; |
| 164 | MODULE_DEVICE_TABLE(i2c, lm80_id); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static struct i2c_driver lm80_driver = { |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 167 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 168 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 169 | .name = "lm80", |
| 170 | }, |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 171 | .probe = lm80_probe, |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 172 | .id_table = lm80_id, |
| 173 | .detect = lm80_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 174 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /* |
| 178 | * Sysfs stuff |
| 179 | */ |
| 180 | |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 181 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 182 | char *buf) |
| 183 | { |
| 184 | struct lm80_data *data = lm80_update_device(dev); |
| 185 | int index = to_sensor_dev_attr_2(attr)->index; |
| 186 | int nr = to_sensor_dev_attr_2(attr)->nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 188 | if (IS_ERR(data)) |
| 189 | return PTR_ERR(data); |
| 190 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 192 | |
| 193 | static ssize_t set_in(struct device *dev, struct device_attribute *attr, |
| 194 | const char *buf, size_t count) |
| 195 | { |
| 196 | struct lm80_data *data = dev_get_drvdata(dev); |
| 197 | struct i2c_client *client = data->client; |
| 198 | int index = to_sensor_dev_attr_2(attr)->index; |
| 199 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 200 | long val; |
| 201 | u8 reg; |
| 202 | int err = kstrtol(buf, 10, &val); |
| 203 | if (err < 0) |
| 204 | return err; |
| 205 | |
| 206 | reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index); |
| 207 | |
| 208 | mutex_lock(&data->update_lock); |
| 209 | data->in[nr][index] = IN_TO_REG(val); |
| 210 | lm80_write_value(client, reg, data->in[nr][index]); |
| 211 | mutex_unlock(&data->update_lock); |
| 212 | return count; |
| 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 215 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 216 | char *buf) |
| 217 | { |
| 218 | int index = to_sensor_dev_attr_2(attr)->index; |
| 219 | int nr = to_sensor_dev_attr_2(attr)->nr; |
| 220 | struct lm80_data *data = lm80_update_device(dev); |
| 221 | if (IS_ERR(data)) |
| 222 | return PTR_ERR(data); |
| 223 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index], |
| 224 | DIV_FROM_REG(data->fan_div[index]))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 227 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
| 228 | char *buf) |
| 229 | { |
| 230 | int nr = to_sensor_dev_attr(attr)->index; |
| 231 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 232 | if (IS_ERR(data)) |
| 233 | return PTR_ERR(data); |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 234 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 237 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 238 | const char *buf, size_t count) |
| 239 | { |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 240 | int index = to_sensor_dev_attr_2(attr)->index; |
| 241 | int nr = to_sensor_dev_attr_2(attr)->nr; |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 242 | struct lm80_data *data = dev_get_drvdata(dev); |
| 243 | struct i2c_client *client = data->client; |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame] | 244 | unsigned long val; |
| 245 | int err = kstrtoul(buf, 10, &val); |
| 246 | if (err < 0) |
| 247 | return err; |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 248 | |
| 249 | mutex_lock(&data->update_lock); |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 250 | data->fan[nr][index] = FAN_TO_REG(val, |
| 251 | DIV_FROM_REG(data->fan_div[index])); |
| 252 | lm80_write_value(client, LM80_REG_FAN_MIN(index + 1), |
| 253 | data->fan[nr][index]); |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 254 | mutex_unlock(&data->update_lock); |
| 255 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Guenter Roeck | 1160631 | 2012-01-19 11:02:20 -0800 | [diff] [blame] | 258 | /* |
| 259 | * Note: we save and restore the fan minimum here, because its value is |
| 260 | * determined in part by the fan divisor. This follows the principle of |
| 261 | * least surprise; the user doesn't expect the fan minimum to change just |
| 262 | * because the divisor changed. |
| 263 | */ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 264 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 265 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 267 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 268 | struct lm80_data *data = dev_get_drvdata(dev); |
| 269 | struct i2c_client *client = data->client; |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame] | 270 | unsigned long min, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | u8 reg; |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame] | 272 | int err = kstrtoul(buf, 10, &val); |
| 273 | if (err < 0) |
| 274 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | /* Save fan_min */ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 277 | mutex_lock(&data->update_lock); |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 278 | min = FAN_FROM_REG(data->fan[f_min][nr], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | DIV_FROM_REG(data->fan_div[nr])); |
| 280 | |
| 281 | switch (val) { |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 282 | case 1: |
| 283 | data->fan_div[nr] = 0; |
| 284 | break; |
| 285 | case 2: |
| 286 | data->fan_div[nr] = 1; |
| 287 | break; |
| 288 | case 4: |
| 289 | data->fan_div[nr] = 2; |
| 290 | break; |
| 291 | case 8: |
| 292 | data->fan_div[nr] = 3; |
| 293 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | default: |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 295 | dev_err(dev, |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 296 | "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", |
| 297 | val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 298 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | } |
| 301 | |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 302 | reg = (lm80_read_value(client, LM80_REG_FANDIV) & |
| 303 | ~(3 << (2 * (nr + 1)))) | (data->fan_div[nr] << (2 * (nr + 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | lm80_write_value(client, LM80_REG_FANDIV, reg); |
| 305 | |
| 306 | /* Restore fan_min */ |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 307 | data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 308 | lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), |
| 309 | data->fan[f_min][nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 310 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | return count; |
| 313 | } |
| 314 | |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 315 | static ssize_t show_temp(struct device *dev, struct device_attribute *devattr, |
| 316 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 318 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 320 | if (IS_ERR(data)) |
| 321 | return PTR_ERR(data); |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 322 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 325 | static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, |
| 326 | const char *buf, size_t count) |
| 327 | { |
| 328 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 329 | struct lm80_data *data = dev_get_drvdata(dev); |
| 330 | struct i2c_client *client = data->client; |
| 331 | int nr = attr->index; |
| 332 | long val; |
| 333 | int err = kstrtol(buf, 10, &val); |
| 334 | if (err < 0) |
| 335 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 337 | mutex_lock(&data->update_lock); |
| 338 | data->temp[nr] = TEMP_TO_REG(val); |
| 339 | lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8); |
| 340 | mutex_unlock(&data->update_lock); |
| 341 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 344 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 345 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 348 | if (IS_ERR(data)) |
| 349 | return PTR_ERR(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return sprintf(buf, "%u\n", data->alarms); |
| 351 | } |
| 352 | |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 353 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 354 | char *buf) |
| 355 | { |
| 356 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 357 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 358 | if (IS_ERR(data)) |
| 359 | return PTR_ERR(data); |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 360 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 361 | } |
| 362 | |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 363 | static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, |
| 364 | show_in, set_in, i_min, 0); |
| 365 | static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, |
| 366 | show_in, set_in, i_min, 1); |
| 367 | static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, |
| 368 | show_in, set_in, i_min, 2); |
| 369 | static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, |
| 370 | show_in, set_in, i_min, 3); |
| 371 | static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, |
| 372 | show_in, set_in, i_min, 4); |
| 373 | static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, |
| 374 | show_in, set_in, i_min, 5); |
| 375 | static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, |
| 376 | show_in, set_in, i_min, 6); |
| 377 | static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, |
| 378 | show_in, set_in, i_max, 0); |
| 379 | static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, |
| 380 | show_in, set_in, i_max, 1); |
| 381 | static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, |
| 382 | show_in, set_in, i_max, 2); |
| 383 | static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, |
| 384 | show_in, set_in, i_max, 3); |
| 385 | static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, |
| 386 | show_in, set_in, i_max, 4); |
| 387 | static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, |
| 388 | show_in, set_in, i_max, 5); |
| 389 | static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, |
| 390 | show_in, set_in, i_max, 6); |
| 391 | static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, i_input, 0); |
| 392 | static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, i_input, 1); |
| 393 | static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, i_input, 2); |
| 394 | static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, i_input, 3); |
| 395 | static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4); |
| 396 | static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5); |
| 397 | static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6); |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 398 | static SENSOR_DEVICE_ATTR_2(fan1_min, S_IWUSR | S_IRUGO, |
| 399 | show_fan, set_fan_min, f_min, 0); |
| 400 | static SENSOR_DEVICE_ATTR_2(fan2_min, S_IWUSR | S_IRUGO, |
| 401 | show_fan, set_fan_min, f_min, 1); |
| 402 | static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, f_input, 0); |
| 403 | static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, f_input, 1); |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 404 | static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, |
| 405 | show_fan_div, set_fan_div, 0); |
| 406 | static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, |
| 407 | show_fan_div, set_fan_div, 1); |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 408 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input); |
| 409 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, |
| 410 | set_temp, t_hot_max); |
| 411 | static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp, |
| 412 | set_temp, t_hot_hyst); |
| 413 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp, |
| 414 | set_temp, t_os_max); |
| 415 | static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp, |
| 416 | set_temp, t_os_hyst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 418 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 419 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 420 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 421 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 422 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 423 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 424 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 425 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); |
| 426 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 427 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 428 | static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * Real code |
| 432 | */ |
| 433 | |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 434 | static struct attribute *lm80_attrs[] = { |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 435 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 436 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 437 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 438 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 439 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 440 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 441 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 442 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 443 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 444 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 445 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 446 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 447 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 448 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 449 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 450 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 451 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 452 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 453 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 454 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 455 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 456 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 457 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 458 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 459 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 460 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 461 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 462 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 463 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 464 | &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, |
| 465 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 466 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 467 | &dev_attr_alarms.attr, |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 468 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 469 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 470 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 471 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 472 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 473 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 474 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 475 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 476 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 477 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 478 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 479 | NULL |
| 480 | }; |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 481 | ATTRIBUTE_GROUPS(lm80); |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 482 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 483 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 484 | static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 486 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 487 | int i, cur, man_id, dev_id; |
| 488 | const char *name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 491 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 493 | /* First check for unused bits, common to both chip types */ |
| 494 | if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) |
| 495 | || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80)) |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 496 | return -ENODEV; |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * The LM96080 has manufacturer and stepping/die rev registers so we |
| 500 | * can just check that. The LM80 does not have such registers so we |
| 501 | * have to use a more expensive trick. |
| 502 | */ |
| 503 | man_id = lm80_read_value(client, LM96080_REG_MAN_ID); |
| 504 | dev_id = lm80_read_value(client, LM96080_REG_DEV_ID); |
| 505 | if (man_id == 0x01 && dev_id == 0x08) { |
| 506 | /* Check more unused bits for confirmation */ |
| 507 | if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe) |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 508 | return -ENODEV; |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 509 | |
| 510 | name = "lm96080"; |
| 511 | } else { |
| 512 | /* Check 6-bit addressing */ |
| 513 | for (i = 0x2a; i <= 0x3d; i++) { |
| 514 | cur = i2c_smbus_read_byte_data(client, i); |
| 515 | if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) |
| 516 | || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) |
| 517 | || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) |
| 518 | return -ENODEV; |
| 519 | } |
| 520 | |
| 521 | name = "lm80"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jean Delvare | 9908ad4 | 2012-01-31 09:27:11 -0500 | [diff] [blame] | 524 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int lm80_probe(struct i2c_client *client, |
| 530 | const struct i2c_device_id *id) |
| 531 | { |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 532 | struct device *dev = &client->dev; |
| 533 | struct device *hwmon_dev; |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 534 | struct lm80_data *data; |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 535 | |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 536 | data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL); |
Guenter Roeck | c38b282 | 2012-06-02 09:58:09 -0700 | [diff] [blame] | 537 | if (!data) |
| 538 | return -ENOMEM; |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 539 | |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 540 | data->client = client; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 541 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /* Initialize the LM80 chip */ |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 544 | lm80_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | /* A few vars need to be filled upon startup */ |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 547 | data->fan[f_min][0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
| 548 | data->fan[f_min][1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 550 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 551 | data, lm80_groups); |
Mark M. Hoffman | 0501a381 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 552 | |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 553 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static int lm80_read_value(struct i2c_client *client, u8 reg) |
| 557 | { |
| 558 | return i2c_smbus_read_byte_data(client, reg); |
| 559 | } |
| 560 | |
| 561 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 562 | { |
| 563 | return i2c_smbus_write_byte_data(client, reg, value); |
| 564 | } |
| 565 | |
| 566 | /* Called when we have found a new LM80. */ |
| 567 | static void lm80_init_client(struct i2c_client *client) |
| 568 | { |
Guenter Roeck | 1160631 | 2012-01-19 11:02:20 -0800 | [diff] [blame] | 569 | /* |
| 570 | * Reset all except Watchdog values and last conversion values |
| 571 | * This sets fan-divs to 2, among others. This makes most other |
| 572 | * initializations unnecessary |
| 573 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | lm80_write_value(client, LM80_REG_CONFIG, 0x80); |
| 575 | /* Set 11-bit temperature resolution */ |
| 576 | lm80_write_value(client, LM80_REG_RES, 0x08); |
| 577 | |
| 578 | /* Start monitoring */ |
| 579 | lm80_write_value(client, LM80_REG_CONFIG, 0x01); |
| 580 | } |
| 581 | |
| 582 | static struct lm80_data *lm80_update_device(struct device *dev) |
| 583 | { |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 584 | struct lm80_data *data = dev_get_drvdata(dev); |
| 585 | struct i2c_client *client = data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | int i; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 587 | int rv; |
| 588 | int prev_rv; |
| 589 | struct lm80_data *ret = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 591 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Frans Meulenbroeks | 96585f1 | 2012-01-10 15:49:36 +0100 | [diff] [blame] | 593 | if (data->error) |
| 594 | lm80_init_client(client); |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
Guenter Roeck | 118c9a6 | 2014-04-04 18:01:34 +0200 | [diff] [blame] | 597 | dev_dbg(dev, "Starting lm80 update\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | for (i = 0; i <= 6; i++) { |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 599 | rv = lm80_read_value(client, LM80_REG_IN(i)); |
| 600 | if (rv < 0) |
| 601 | goto abort; |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 602 | data->in[i_input][i] = rv; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 603 | |
| 604 | rv = lm80_read_value(client, LM80_REG_IN_MIN(i)); |
| 605 | if (rv < 0) |
| 606 | goto abort; |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 607 | data->in[i_min][i] = rv; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 608 | |
| 609 | rv = lm80_read_value(client, LM80_REG_IN_MAX(i)); |
| 610 | if (rv < 0) |
| 611 | goto abort; |
Guenter Roeck | 1adf3a3 | 2014-04-13 10:53:50 -0700 | [diff] [blame] | 612 | data->in[i_max][i] = rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 615 | rv = lm80_read_value(client, LM80_REG_FAN1); |
| 616 | if (rv < 0) |
| 617 | goto abort; |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 618 | data->fan[f_input][0] = rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 620 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
| 621 | if (rv < 0) |
| 622 | goto abort; |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 623 | data->fan[f_min][0] = rv; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 624 | |
| 625 | rv = lm80_read_value(client, LM80_REG_FAN2); |
| 626 | if (rv < 0) |
| 627 | goto abort; |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 628 | data->fan[f_input][1] = rv; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 629 | |
| 630 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
| 631 | if (rv < 0) |
| 632 | goto abort; |
Guenter Roeck | 85b3ee0 | 2014-04-13 11:28:29 -0700 | [diff] [blame^] | 633 | data->fan[f_min][1] = rv; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 634 | |
| 635 | prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP); |
| 636 | if (rv < 0) |
| 637 | goto abort; |
| 638 | rv = lm80_read_value(client, LM80_REG_RES); |
| 639 | if (rv < 0) |
| 640 | goto abort; |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 641 | data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 642 | |
Guenter Roeck | 9711bcd | 2014-04-13 09:45:15 -0700 | [diff] [blame] | 643 | for (i = t_input + 1; i < t_num_temp; i++) { |
| 644 | rv = lm80_read_value(client, temp_regs[i]); |
| 645 | if (rv < 0) |
| 646 | goto abort; |
| 647 | data->temp[i] = rv << 8; |
| 648 | } |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 649 | |
| 650 | rv = lm80_read_value(client, LM80_REG_FANDIV); |
| 651 | if (rv < 0) |
| 652 | goto abort; |
| 653 | data->fan_div[0] = (rv >> 2) & 0x03; |
| 654 | data->fan_div[1] = (rv >> 4) & 0x03; |
| 655 | |
| 656 | prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1); |
| 657 | if (rv < 0) |
| 658 | goto abort; |
| 659 | rv = lm80_read_value(client, LM80_REG_ALARM2); |
| 660 | if (rv < 0) |
| 661 | goto abort; |
| 662 | data->alarms = prev_rv + (rv << 8); |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | data->last_updated = jiffies; |
| 665 | data->valid = 1; |
Frans Meulenbroeks | 96585f1 | 2012-01-10 15:49:36 +0100 | [diff] [blame] | 666 | data->error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 668 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 670 | abort: |
| 671 | ret = ERR_PTR(rv); |
| 672 | data->valid = 0; |
Frans Meulenbroeks | 96585f1 | 2012-01-10 15:49:36 +0100 | [diff] [blame] | 673 | data->error = 1; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 674 | |
| 675 | done: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 676 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 678 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 681 | module_i2c_driver(lm80_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
| 684 | "Philip Edelbrock <phil@netroedge.com>"); |
| 685 | MODULE_DESCRIPTION("LM80 driver"); |
| 686 | MODULE_LICENSE("GPL"); |