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