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