Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm80.c - From lm_sensors, Linux kernel modules for hardware |
| 3 | * monitoring |
| 4 | * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl> |
| 5 | * and Philip Edelbrock <phil@netroedge.com> |
| 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 | |
| 63 | |
| 64 | /* Conversions. Rounding and limit checking is only done on the TO_REG |
| 65 | variants. Note that you should be a bit careful with which arguments |
| 66 | these macros are called: arguments may be evaluated more than once. |
| 67 | Fixing this is just not worth it. */ |
| 68 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 69 | #define IN_TO_REG(val) (SENSORS_LIMIT(((val) + 5) / 10, 0, 255)) |
| 70 | #define IN_FROM_REG(val) ((val) * 10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div) |
| 73 | { |
| 74 | if (rpm == 0) |
| 75 | return 255; |
| 76 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 77 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 80 | #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \ |
| 81 | (val) == 255 ? 0 : 1350000/((div) * (val))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | static inline long TEMP_FROM_REG(u16 temp) |
| 84 | { |
| 85 | long res; |
| 86 | |
| 87 | temp >>= 4; |
| 88 | if (temp < 0x0800) |
| 89 | res = 625 * (long) temp; |
| 90 | else |
| 91 | res = ((long) temp - 0x01000) * 625; |
| 92 | |
| 93 | return res / 10; |
| 94 | } |
| 95 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 96 | #define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \ |
| 97 | (val) - 0x100 : (val)) * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 99 | #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \ |
| 100 | ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | #define DIV_FROM_REG(val) (1 << (val)) |
| 103 | |
| 104 | /* |
| 105 | * Client data (each client gets its own) |
| 106 | */ |
| 107 | |
| 108 | struct lm80_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 109 | struct device *hwmon_dev; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 110 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | char valid; /* !=0 if following fields are valid */ |
| 112 | unsigned long last_updated; /* In jiffies */ |
| 113 | |
| 114 | u8 in[7]; /* Register value */ |
| 115 | u8 in_max[7]; /* Register value */ |
| 116 | u8 in_min[7]; /* Register value */ |
| 117 | u8 fan[2]; /* Register value */ |
| 118 | u8 fan_min[2]; /* Register value */ |
| 119 | u8 fan_div[2]; /* Register encoding, shifted right */ |
| 120 | u16 temp; /* Register values, shifted right */ |
| 121 | u8 temp_hot_max; /* Register value */ |
| 122 | u8 temp_hot_hyst; /* Register value */ |
| 123 | u8 temp_os_max; /* Register value */ |
| 124 | u8 temp_os_hyst; /* Register value */ |
| 125 | u16 alarms; /* Register encoding, combined */ |
| 126 | }; |
| 127 | |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 128 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * Functions declaration |
| 130 | */ |
| 131 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 132 | static int lm80_probe(struct i2c_client *client, |
| 133 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 134 | 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] | 135 | static void lm80_init_client(struct i2c_client *client); |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 136 | static int lm80_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static struct lm80_data *lm80_update_device(struct device *dev); |
| 138 | static int lm80_read_value(struct i2c_client *client, u8 reg); |
| 139 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); |
| 140 | |
| 141 | /* |
| 142 | * Driver data (common to all clients) |
| 143 | */ |
| 144 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 145 | static const struct i2c_device_id lm80_id[] = { |
Jean Delvare | 1f86df4 | 2009-12-14 21:17:26 +0100 | [diff] [blame] | 146 | { "lm80", 0 }, |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 147 | { } |
| 148 | }; |
| 149 | MODULE_DEVICE_TABLE(i2c, lm80_id); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | static struct i2c_driver lm80_driver = { |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 152 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 153 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 154 | .name = "lm80", |
| 155 | }, |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 156 | .probe = lm80_probe, |
| 157 | .remove = lm80_remove, |
| 158 | .id_table = lm80_id, |
| 159 | .detect = lm80_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 160 | .address_list = normal_i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /* |
| 164 | * Sysfs stuff |
| 165 | */ |
| 166 | |
| 167 | #define show_in(suffix, value) \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 168 | static ssize_t show_in_##suffix(struct device *dev, \ |
| 169 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { \ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 171 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | struct lm80_data *data = lm80_update_device(dev); \ |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 173 | if (IS_ERR(data)) \ |
| 174 | return PTR_ERR(data); \ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 175 | return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 177 | show_in(min, in_min) |
| 178 | show_in(max, in_max) |
| 179 | show_in(input, in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | #define set_in(suffix, value, reg) \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 182 | static ssize_t set_in_##suffix(struct device *dev, \ |
| 183 | struct device_attribute *attr, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { \ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 185 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | struct i2c_client *client = to_i2c_client(dev); \ |
| 187 | struct lm80_data *data = i2c_get_clientdata(client); \ |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame^] | 188 | long val; \ |
| 189 | int err = kstrtol(buf, 10, &val); \ |
| 190 | if (err < 0) \ |
| 191 | return err; \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 192 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 193 | mutex_lock(&data->update_lock);\ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 194 | data->value[nr] = IN_TO_REG(val); \ |
| 195 | lm80_write_value(client, reg(nr), data->value[nr]); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 196 | mutex_unlock(&data->update_lock);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return count; \ |
| 198 | } |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 199 | set_in(min, in_min, LM80_REG_IN_MIN) |
| 200 | set_in(max, in_max, LM80_REG_IN_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 202 | #define show_fan(suffix, value) \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 203 | static ssize_t show_fan_##suffix(struct device *dev, \ |
| 204 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { \ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 206 | int nr = to_sensor_dev_attr(attr)->index; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | struct lm80_data *data = lm80_update_device(dev); \ |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 208 | if (IS_ERR(data)) \ |
| 209 | return PTR_ERR(data); \ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 210 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \ |
| 211 | DIV_FROM_REG(data->fan_div[nr]))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 213 | show_fan(min, fan_min) |
| 214 | show_fan(input, fan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 216 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
| 217 | char *buf) |
| 218 | { |
| 219 | int nr = to_sensor_dev_attr(attr)->index; |
| 220 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 221 | if (IS_ERR(data)) |
| 222 | return PTR_ERR(data); |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 223 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 226 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 227 | const char *buf, size_t count) |
| 228 | { |
| 229 | int nr = to_sensor_dev_attr(attr)->index; |
| 230 | struct i2c_client *client = to_i2c_client(dev); |
| 231 | struct lm80_data *data = i2c_get_clientdata(client); |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame^] | 232 | unsigned long val; |
| 233 | int err = kstrtoul(buf, 10, &val); |
| 234 | if (err < 0) |
| 235 | return err; |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 236 | |
| 237 | mutex_lock(&data->update_lock); |
| 238 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 239 | lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); |
| 240 | mutex_unlock(&data->update_lock); |
| 241 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | /* Note: we save and restore the fan minimum here, because its value is |
| 245 | determined in part by the fan divisor. This follows the principle of |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 246 | least surprise; the user doesn't expect the fan minimum to change just |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | because the divisor changed. */ |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 248 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 249 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 251 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | struct i2c_client *client = to_i2c_client(dev); |
| 253 | struct lm80_data *data = i2c_get_clientdata(client); |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame^] | 254 | unsigned long min, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | u8 reg; |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame^] | 256 | int err = kstrtoul(buf, 10, &val); |
| 257 | if (err < 0) |
| 258 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | /* Save fan_min */ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 261 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | min = FAN_FROM_REG(data->fan_min[nr], |
| 263 | DIV_FROM_REG(data->fan_div[nr])); |
| 264 | |
| 265 | switch (val) { |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 266 | case 1: |
| 267 | data->fan_div[nr] = 0; |
| 268 | break; |
| 269 | case 2: |
| 270 | data->fan_div[nr] = 1; |
| 271 | break; |
| 272 | case 4: |
| 273 | data->fan_div[nr] = 2; |
| 274 | break; |
| 275 | case 8: |
| 276 | data->fan_div[nr] = 3; |
| 277 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | default: |
| 279 | dev_err(&client->dev, "fan_div value %ld not " |
| 280 | "supported. Choose one of 1, 2, 4 or 8!\n", val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 281 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return -EINVAL; |
| 283 | } |
| 284 | |
| 285 | reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1)))) |
| 286 | | (data->fan_div[nr] << (2 * (nr + 1))); |
| 287 | lm80_write_value(client, LM80_REG_FANDIV, reg); |
| 288 | |
| 289 | /* Restore fan_min */ |
| 290 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 291 | lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 292 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | return count; |
| 295 | } |
| 296 | |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 297 | static ssize_t show_temp_input1(struct device *dev, |
| 298 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 301 | if (IS_ERR(data)) |
| 302 | return PTR_ERR(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp)); |
| 304 | } |
| 305 | |
| 306 | #define show_temp(suffix, value) \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 307 | static ssize_t show_temp_##suffix(struct device *dev, \ |
| 308 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { \ |
| 310 | struct lm80_data *data = lm80_update_device(dev); \ |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 311 | if (IS_ERR(data)) \ |
| 312 | return PTR_ERR(data); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \ |
| 314 | } |
| 315 | show_temp(hot_max, temp_hot_max); |
| 316 | show_temp(hot_hyst, temp_hot_hyst); |
| 317 | show_temp(os_max, temp_os_max); |
| 318 | show_temp(os_hyst, temp_os_hyst); |
| 319 | |
| 320 | #define set_temp(suffix, value, reg) \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 321 | static ssize_t set_temp_##suffix(struct device *dev, \ |
| 322 | struct device_attribute *attr, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { \ |
| 324 | struct i2c_client *client = to_i2c_client(dev); \ |
| 325 | struct lm80_data *data = i2c_get_clientdata(client); \ |
Frans Meulenbroeks | 6a9e7c4c | 2012-01-10 15:49:35 +0100 | [diff] [blame^] | 326 | long val; \ |
| 327 | int err = kstrtol(buf, 10, &val); \ |
| 328 | if (err < 0) \ |
| 329 | return err; \ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 330 | \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 331 | mutex_lock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | data->value = TEMP_LIMIT_TO_REG(val); \ |
| 333 | lm80_write_value(client, reg, data->value); \ |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 334 | mutex_unlock(&data->update_lock); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return count; \ |
| 336 | } |
| 337 | set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX); |
| 338 | set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST); |
| 339 | set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX); |
| 340 | set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST); |
| 341 | |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 342 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 343 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 346 | if (IS_ERR(data)) |
| 347 | return PTR_ERR(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return sprintf(buf, "%u\n", data->alarms); |
| 349 | } |
| 350 | |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 351 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 352 | char *buf) |
| 353 | { |
| 354 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 355 | struct lm80_data *data = lm80_update_device(dev); |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 356 | if (IS_ERR(data)) |
| 357 | return PTR_ERR(data); |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 358 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 359 | } |
| 360 | |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 361 | static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, |
| 362 | show_in_min, set_in_min, 0); |
| 363 | static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, |
| 364 | show_in_min, set_in_min, 1); |
| 365 | static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, |
| 366 | show_in_min, set_in_min, 2); |
| 367 | static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, |
| 368 | show_in_min, set_in_min, 3); |
| 369 | static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, |
| 370 | show_in_min, set_in_min, 4); |
| 371 | static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, |
| 372 | show_in_min, set_in_min, 5); |
| 373 | static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, |
| 374 | show_in_min, set_in_min, 6); |
| 375 | static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, |
| 376 | show_in_max, set_in_max, 0); |
| 377 | static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, |
| 378 | show_in_max, set_in_max, 1); |
| 379 | static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, |
| 380 | show_in_max, set_in_max, 2); |
| 381 | static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, |
| 382 | show_in_max, set_in_max, 3); |
| 383 | static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, |
| 384 | show_in_max, set_in_max, 4); |
| 385 | static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, |
| 386 | show_in_max, set_in_max, 5); |
| 387 | static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, |
| 388 | show_in_max, set_in_max, 6); |
| 389 | static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0); |
| 390 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1); |
| 391 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2); |
| 392 | static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3); |
| 393 | static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4); |
| 394 | static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5); |
| 395 | static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6); |
| 396 | static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, |
| 397 | show_fan_min, set_fan_min, 0); |
| 398 | static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, |
| 399 | show_fan_min, set_fan_min, 1); |
| 400 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0); |
| 401 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1); |
| 402 | static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, |
| 403 | show_fan_div, set_fan_div, 0); |
| 404 | static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, |
| 405 | show_fan_div, set_fan_div, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL); |
| 407 | static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max, |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 408 | set_temp_hot_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst, |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 410 | set_temp_hot_hyst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max, |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 412 | set_temp_os_max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst, |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 414 | set_temp_os_hyst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 416 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 417 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 418 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 419 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 420 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 421 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 422 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 423 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10); |
| 424 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 425 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 426 | 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] | 427 | |
| 428 | /* |
| 429 | * Real code |
| 430 | */ |
| 431 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 432 | static struct attribute *lm80_attributes[] = { |
Jean Delvare | f818176 | 2008-01-05 15:37:05 +0100 | [diff] [blame] | 433 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 434 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 435 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 436 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 437 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 438 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 439 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 440 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 441 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 442 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 443 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 444 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 445 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 446 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 447 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 448 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 449 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 450 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 451 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 452 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 453 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 454 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 455 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 456 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 457 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 458 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 459 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 460 | &dev_attr_temp1_input.attr, |
| 461 | &dev_attr_temp1_max.attr, |
| 462 | &dev_attr_temp1_max_hyst.attr, |
| 463 | &dev_attr_temp1_crit.attr, |
| 464 | &dev_attr_temp1_crit_hyst.attr, |
| 465 | &dev_attr_alarms.attr, |
Jean Delvare | e84542f | 2008-01-05 15:40:38 +0100 | [diff] [blame] | 466 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 467 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 468 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 469 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 470 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 471 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 472 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 473 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 474 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
| 475 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 476 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 477 | NULL |
| 478 | }; |
| 479 | |
| 480 | static const struct attribute_group lm80_group = { |
| 481 | .attrs = lm80_attributes, |
| 482 | }; |
| 483 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 484 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 485 | 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] | 486 | { |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 487 | struct i2c_adapter *adapter = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | int i, cur; |
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 | |
| 493 | /* Now, we do the remaining detection. It is lousy. */ |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 494 | if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 495 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | for (i = 0x2a; i <= 0x3d; i++) { |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 497 | cur = i2c_smbus_read_byte_data(client, i); |
| 498 | if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) |
| 499 | || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) |
| 500 | || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 501 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 504 | strlcpy(info->type, "lm80", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int lm80_probe(struct i2c_client *client, |
| 510 | const struct i2c_device_id *id) |
| 511 | { |
| 512 | struct lm80_data *data; |
| 513 | int err; |
| 514 | |
| 515 | data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL); |
| 516 | if (!data) { |
| 517 | err = -ENOMEM; |
| 518 | goto exit; |
| 519 | } |
| 520 | |
| 521 | i2c_set_clientdata(client, data); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 522 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* Initialize the LM80 chip */ |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 525 | lm80_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | /* A few vars need to be filled upon startup */ |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 528 | data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
| 529 | data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | /* Register sysfs hooks */ |
Frans Meulenbroeks | 66b3b1f | 2012-01-04 20:58:53 +0100 | [diff] [blame] | 532 | err = sysfs_create_group(&client->dev.kobj, &lm80_group); |
| 533 | if (err) |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 534 | goto error_free; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 535 | |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 536 | data->hwmon_dev = hwmon_device_register(&client->dev); |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 537 | if (IS_ERR(data->hwmon_dev)) { |
| 538 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 539 | goto error_remove; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 544 | error_remove: |
Jean Delvare | 6cc37ee | 2008-01-05 15:35:09 +0100 | [diff] [blame] | 545 | sysfs_remove_group(&client->dev.kobj, &lm80_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | error_free: |
| 547 | kfree(data); |
| 548 | exit: |
| 549 | return err; |
| 550 | } |
| 551 | |
Jean Delvare | 8c8bacc | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 552 | static int lm80_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 554 | struct lm80_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 556 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 557 | sysfs_remove_group(&client->dev.kobj, &lm80_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 559 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int lm80_read_value(struct i2c_client *client, u8 reg) |
| 564 | { |
| 565 | return i2c_smbus_read_byte_data(client, reg); |
| 566 | } |
| 567 | |
| 568 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 569 | { |
| 570 | return i2c_smbus_write_byte_data(client, reg, value); |
| 571 | } |
| 572 | |
| 573 | /* Called when we have found a new LM80. */ |
| 574 | static void lm80_init_client(struct i2c_client *client) |
| 575 | { |
| 576 | /* Reset all except Watchdog values and last conversion values |
| 577 | This sets fan-divs to 2, among others. This makes most other |
| 578 | initializations unnecessary */ |
| 579 | lm80_write_value(client, LM80_REG_CONFIG, 0x80); |
| 580 | /* Set 11-bit temperature resolution */ |
| 581 | lm80_write_value(client, LM80_REG_RES, 0x08); |
| 582 | |
| 583 | /* Start monitoring */ |
| 584 | lm80_write_value(client, LM80_REG_CONFIG, 0x01); |
| 585 | } |
| 586 | |
| 587 | static struct lm80_data *lm80_update_device(struct device *dev) |
| 588 | { |
| 589 | struct i2c_client *client = to_i2c_client(dev); |
| 590 | struct lm80_data *data = i2c_get_clientdata(client); |
| 591 | int i; |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 592 | int rv; |
| 593 | int prev_rv; |
| 594 | struct lm80_data *ret = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 596 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { |
| 599 | dev_dbg(&client->dev, "Starting lm80 update\n"); |
| 600 | for (i = 0; i <= 6; i++) { |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 601 | rv = lm80_read_value(client, LM80_REG_IN(i)); |
| 602 | if (rv < 0) |
| 603 | goto abort; |
| 604 | data->in[i] = rv; |
| 605 | |
| 606 | rv = lm80_read_value(client, LM80_REG_IN_MIN(i)); |
| 607 | if (rv < 0) |
| 608 | goto abort; |
| 609 | data->in_min[i] = rv; |
| 610 | |
| 611 | rv = lm80_read_value(client, LM80_REG_IN_MAX(i)); |
| 612 | if (rv < 0) |
| 613 | goto abort; |
| 614 | data->in_max[i] = rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 617 | rv = lm80_read_value(client, LM80_REG_FAN1); |
| 618 | if (rv < 0) |
| 619 | goto abort; |
| 620 | data->fan[0] = rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 622 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(1)); |
| 623 | if (rv < 0) |
| 624 | goto abort; |
| 625 | data->fan_min[0] = rv; |
| 626 | |
| 627 | rv = lm80_read_value(client, LM80_REG_FAN2); |
| 628 | if (rv < 0) |
| 629 | goto abort; |
| 630 | data->fan[1] = rv; |
| 631 | |
| 632 | rv = lm80_read_value(client, LM80_REG_FAN_MIN(2)); |
| 633 | if (rv < 0) |
| 634 | goto abort; |
| 635 | data->fan_min[1] = rv; |
| 636 | |
| 637 | prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP); |
| 638 | if (rv < 0) |
| 639 | goto abort; |
| 640 | rv = lm80_read_value(client, LM80_REG_RES); |
| 641 | if (rv < 0) |
| 642 | goto abort; |
| 643 | data->temp = (prev_rv << 8) | (rv & 0xf0); |
| 644 | |
| 645 | rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX); |
| 646 | if (rv < 0) |
| 647 | goto abort; |
| 648 | data->temp_os_max = rv; |
| 649 | |
| 650 | rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST); |
| 651 | if (rv < 0) |
| 652 | goto abort; |
| 653 | data->temp_os_hyst = rv; |
| 654 | |
| 655 | rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX); |
| 656 | if (rv < 0) |
| 657 | goto abort; |
| 658 | data->temp_hot_max = rv; |
| 659 | |
| 660 | rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST); |
| 661 | if (rv < 0) |
| 662 | goto abort; |
| 663 | data->temp_hot_hyst = rv; |
| 664 | |
| 665 | rv = lm80_read_value(client, LM80_REG_FANDIV); |
| 666 | if (rv < 0) |
| 667 | goto abort; |
| 668 | data->fan_div[0] = (rv >> 2) & 0x03; |
| 669 | data->fan_div[1] = (rv >> 4) & 0x03; |
| 670 | |
| 671 | prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1); |
| 672 | if (rv < 0) |
| 673 | goto abort; |
| 674 | rv = lm80_read_value(client, LM80_REG_ALARM2); |
| 675 | if (rv < 0) |
| 676 | goto abort; |
| 677 | data->alarms = prev_rv + (rv << 8); |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | data->last_updated = jiffies; |
| 680 | data->valid = 1; |
| 681 | } |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 682 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 684 | abort: |
| 685 | ret = ERR_PTR(rv); |
| 686 | data->valid = 0; |
| 687 | |
| 688 | done: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 689 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Frans Meulenbroeks | 2faaa93 | 2012-01-05 14:41:53 +0100 | [diff] [blame] | 691 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static int __init sensors_lm80_init(void) |
| 695 | { |
| 696 | return i2c_add_driver(&lm80_driver); |
| 697 | } |
| 698 | |
| 699 | static void __exit sensors_lm80_exit(void) |
| 700 | { |
| 701 | i2c_del_driver(&lm80_driver); |
| 702 | } |
| 703 | |
| 704 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
| 705 | "Philip Edelbrock <phil@netroedge.com>"); |
| 706 | MODULE_DESCRIPTION("LM80 driver"); |
| 707 | MODULE_LICENSE("GPL"); |
| 708 | |
| 709 | module_init(sensors_lm80_init); |
| 710 | module_exit(sensors_lm80_exit); |