Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lm87.c |
| 3 | * |
| 4 | * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl> |
| 5 | * Philip Edelbrock <phil@netroedge.com> |
| 6 | * Stephen Rousset <stephen.rousset@rocketlogix.com> |
| 7 | * Dan Eaton <dan.eaton@rocketlogix.com> |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 8 | * Copyright (C) 2004-2008 Jean Delvare <jdelvare@suse.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Original port to Linux 2.6 by Jeff Oliver. |
| 11 | * |
| 12 | * The LM87 is a sensor chip made by National Semiconductor. It monitors up |
| 13 | * to 8 voltages (including its own power source), up to three temperatures |
| 14 | * (its own plus up to two external ones) and up to two fans. The default |
| 15 | * configuration is 6 voltages, two temperatures and two fans (see below). |
| 16 | * Voltages are scaled internally with ratios such that the nominal value of |
| 17 | * each voltage correspond to a register value of 192 (which means a |
| 18 | * resolution of about 0.5% of the nominal value). Temperature values are |
| 19 | * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete |
| 20 | * datasheet can be obtained from National's website at: |
| 21 | * http://www.national.com/pf/LM/LM87.html |
| 22 | * |
| 23 | * Some functions share pins, so not all functions are available at the same |
Ben Hutchings | 47064d6 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 24 | * time. Which are depends on the hardware setup. This driver normally |
| 25 | * assumes that firmware configured the chip correctly. Where this is not |
| 26 | * the case, platform code must set the I2C client's platform_data to point |
| 27 | * to a u8 value to be written to the channel register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * For reference, here is the list of exclusive functions: |
| 29 | * - in0+in5 (default) or temp3 |
| 30 | * - fan1 (default) or in6 |
| 31 | * - fan2 (default) or in7 |
| 32 | * - VID lines (default) or IRQ lines (not handled by this driver) |
| 33 | * |
| 34 | * The LM87 additionally features an analog output, supposedly usable to |
| 35 | * control the speed of a fan. All new chips use pulse width modulation |
| 36 | * instead. The LM87 is the only hardware monitoring chipset I know of |
| 37 | * which uses amplitude modulation. Be careful when using this feature. |
| 38 | * |
Jean Delvare | c7fa373 | 2007-10-09 15:22:22 +0200 | [diff] [blame] | 39 | * This driver also supports the ADM1024, a sensor chip made by Analog |
| 40 | * Devices. That chip is fully compatible with the LM87. Complete |
| 41 | * datasheet can be obtained from Analog's website at: |
| 42 | * http://www.analog.com/en/prod/0,2877,ADM1024,00.html |
| 43 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * This program is free software; you can redistribute it and/or modify |
| 45 | * it under the terms of the GNU General Public License as published by |
| 46 | * the Free Software Foundation; either version 2 of the License, or |
| 47 | * (at your option) any later version. |
| 48 | * |
| 49 | * This program is distributed in the hope that it will be useful, |
| 50 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 51 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 52 | * GNU General Public License for more details. |
| 53 | * |
| 54 | * You should have received a copy of the GNU General Public License |
| 55 | * along with this program; if not, write to the Free Software |
| 56 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 57 | */ |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/module.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/slab.h> |
| 62 | #include <linux/jiffies.h> |
| 63 | #include <linux/i2c.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 64 | #include <linux/hwmon.h> |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 65 | #include <linux/hwmon-sysfs.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 66 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 67 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 68 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Addresses to scan |
| 72 | * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e. |
| 73 | */ |
| 74 | |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 75 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame] | 77 | enum chips { lm87, adm1024 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * The LM87 registers |
| 81 | */ |
| 82 | |
| 83 | /* nr in 0..5 */ |
| 84 | #define LM87_REG_IN(nr) (0x20 + (nr)) |
| 85 | #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2) |
| 86 | #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2) |
| 87 | /* nr in 0..1 */ |
| 88 | #define LM87_REG_AIN(nr) (0x28 + (nr)) |
| 89 | #define LM87_REG_AIN_MIN(nr) (0x1A + (nr)) |
| 90 | #define LM87_REG_AIN_MAX(nr) (0x3B + (nr)) |
| 91 | |
| 92 | static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 }; |
| 93 | static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B }; |
| 94 | static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C }; |
| 95 | |
| 96 | #define LM87_REG_TEMP_HW_INT_LOCK 0x13 |
| 97 | #define LM87_REG_TEMP_HW_EXT_LOCK 0x14 |
| 98 | #define LM87_REG_TEMP_HW_INT 0x17 |
| 99 | #define LM87_REG_TEMP_HW_EXT 0x18 |
| 100 | |
| 101 | /* nr in 0..1 */ |
| 102 | #define LM87_REG_FAN(nr) (0x28 + (nr)) |
| 103 | #define LM87_REG_FAN_MIN(nr) (0x3B + (nr)) |
| 104 | #define LM87_REG_AOUT 0x19 |
| 105 | |
| 106 | #define LM87_REG_CONFIG 0x40 |
| 107 | #define LM87_REG_CHANNEL_MODE 0x16 |
| 108 | #define LM87_REG_VID_FAN_DIV 0x47 |
| 109 | #define LM87_REG_VID4 0x49 |
| 110 | |
| 111 | #define LM87_REG_ALARMS1 0x41 |
| 112 | #define LM87_REG_ALARMS2 0x42 |
| 113 | |
| 114 | #define LM87_REG_COMPANY_ID 0x3E |
| 115 | #define LM87_REG_REVISION 0x3F |
| 116 | |
| 117 | /* |
| 118 | * Conversions and various macros |
| 119 | * The LM87 uses signed 8-bit values for temperatures. |
| 120 | */ |
| 121 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 122 | #define IN_FROM_REG(reg, scale) (((reg) * (scale) + 96) / 192) |
| 123 | #define IN_TO_REG(val, scale) ((val) <= 0 ? 0 : \ |
Guenter Roeck | 12fa55c | 2016-12-04 18:16:02 -0800 | [diff] [blame] | 124 | (val) >= (scale) * 255 / 192 ? 255 : \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 125 | ((val) * 192 + (scale) / 2) / (scale)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | #define TEMP_FROM_REG(reg) ((reg) * 1000) |
| 128 | #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \ |
| 129 | (val) >= 126500 ? 127 : \ |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 130 | (((val) < 0 ? (val) - 500 : \ |
| 131 | (val) + 500) / 1000)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 133 | #define FAN_FROM_REG(reg, div) ((reg) == 255 || (reg) == 0 ? 0 : \ |
| 134 | (1350000 + (reg)*(div) / 2) / ((reg) * (div))) |
| 135 | #define FAN_TO_REG(val, div) ((val) * (div) * 255 <= 1350000 ? 255 : \ |
| 136 | (1350000 + (val)*(div) / 2) / ((val) * (div))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | #define FAN_DIV_FROM_REG(reg) (1 << (reg)) |
| 139 | |
| 140 | /* analog out is 9.80mV/LSB */ |
| 141 | #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10) |
| 142 | #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \ |
| 143 | (val) >= 2500 ? 255 : \ |
| 144 | ((val) * 10 + 49) / 98) |
| 145 | |
| 146 | /* nr in 0..1 */ |
| 147 | #define CHAN_NO_FAN(nr) (1 << (nr)) |
| 148 | #define CHAN_TEMP3 (1 << 2) |
| 149 | #define CHAN_VCC_5V (1 << 3) |
Jean Delvare | 889af3d | 2007-10-08 22:48:10 +0200 | [diff] [blame] | 150 | #define CHAN_NO_VID (1 << 7) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * Client data (each client gets its own) |
| 154 | */ |
| 155 | |
| 156 | struct lm87_data { |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 157 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | char valid; /* zero until following fields are valid */ |
| 159 | unsigned long last_updated; /* In jiffies */ |
| 160 | |
| 161 | u8 channel; /* register value */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 162 | u8 config; /* original register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | u8 in[8]; /* register value */ |
| 165 | u8 in_max[8]; /* register value */ |
| 166 | u8 in_min[8]; /* register value */ |
| 167 | u16 in_scale[8]; |
| 168 | |
| 169 | s8 temp[3]; /* register value */ |
| 170 | s8 temp_high[3]; /* register value */ |
| 171 | s8 temp_low[3]; /* register value */ |
| 172 | s8 temp_crit_int; /* min of two register values */ |
| 173 | s8 temp_crit_ext; /* min of two register values */ |
| 174 | |
| 175 | u8 fan[2]; /* register value */ |
| 176 | u8 fan_min[2]; /* register value */ |
| 177 | u8 fan_div[2]; /* register value, shifted right */ |
| 178 | u8 aout; /* register value */ |
| 179 | |
| 180 | u16 alarms; /* register values, combined */ |
| 181 | u8 vid; /* register values, combined */ |
| 182 | u8 vrm; |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 183 | |
| 184 | const struct attribute_group *attr_groups[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static inline int lm87_read_value(struct i2c_client *client, u8 reg) |
| 188 | { |
| 189 | return i2c_smbus_read_byte_data(client, reg); |
| 190 | } |
| 191 | |
| 192 | static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value) |
| 193 | { |
| 194 | return i2c_smbus_write_byte_data(client, reg, value); |
| 195 | } |
| 196 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 197 | static struct lm87_data *lm87_update_device(struct device *dev) |
| 198 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 199 | struct i2c_client *client = dev_get_drvdata(dev); |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 200 | struct lm87_data *data = i2c_get_clientdata(client); |
| 201 | |
| 202 | mutex_lock(&data->update_lock); |
| 203 | |
| 204 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 205 | int i, j; |
| 206 | |
| 207 | dev_dbg(&client->dev, "Updating data.\n"); |
| 208 | |
| 209 | i = (data->channel & CHAN_TEMP3) ? 1 : 0; |
| 210 | j = (data->channel & CHAN_TEMP3) ? 5 : 6; |
| 211 | for (; i < j; i++) { |
| 212 | data->in[i] = lm87_read_value(client, |
| 213 | LM87_REG_IN(i)); |
| 214 | data->in_min[i] = lm87_read_value(client, |
| 215 | LM87_REG_IN_MIN(i)); |
| 216 | data->in_max[i] = lm87_read_value(client, |
| 217 | LM87_REG_IN_MAX(i)); |
| 218 | } |
| 219 | |
| 220 | for (i = 0; i < 2; i++) { |
| 221 | if (data->channel & CHAN_NO_FAN(i)) { |
| 222 | data->in[6+i] = lm87_read_value(client, |
| 223 | LM87_REG_AIN(i)); |
| 224 | data->in_max[6+i] = lm87_read_value(client, |
| 225 | LM87_REG_AIN_MAX(i)); |
| 226 | data->in_min[6+i] = lm87_read_value(client, |
| 227 | LM87_REG_AIN_MIN(i)); |
| 228 | |
| 229 | } else { |
| 230 | data->fan[i] = lm87_read_value(client, |
| 231 | LM87_REG_FAN(i)); |
| 232 | data->fan_min[i] = lm87_read_value(client, |
| 233 | LM87_REG_FAN_MIN(i)); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | j = (data->channel & CHAN_TEMP3) ? 3 : 2; |
| 238 | for (i = 0 ; i < j; i++) { |
| 239 | data->temp[i] = lm87_read_value(client, |
| 240 | LM87_REG_TEMP[i]); |
| 241 | data->temp_high[i] = lm87_read_value(client, |
| 242 | LM87_REG_TEMP_HIGH[i]); |
| 243 | data->temp_low[i] = lm87_read_value(client, |
| 244 | LM87_REG_TEMP_LOW[i]); |
| 245 | } |
| 246 | |
| 247 | i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK); |
| 248 | j = lm87_read_value(client, LM87_REG_TEMP_HW_INT); |
| 249 | data->temp_crit_int = min(i, j); |
| 250 | |
| 251 | i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK); |
| 252 | j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT); |
| 253 | data->temp_crit_ext = min(i, j); |
| 254 | |
| 255 | i = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 256 | data->fan_div[0] = (i >> 4) & 0x03; |
| 257 | data->fan_div[1] = (i >> 6) & 0x03; |
| 258 | data->vid = (i & 0x0F) |
| 259 | | (lm87_read_value(client, LM87_REG_VID4) & 0x01) |
| 260 | << 4; |
| 261 | |
| 262 | data->alarms = lm87_read_value(client, LM87_REG_ALARMS1) |
| 263 | | (lm87_read_value(client, LM87_REG_ALARMS2) |
| 264 | << 8); |
| 265 | data->aout = lm87_read_value(client, LM87_REG_AOUT); |
| 266 | |
| 267 | data->last_updated = jiffies; |
| 268 | data->valid = 1; |
| 269 | } |
| 270 | |
| 271 | mutex_unlock(&data->update_lock); |
| 272 | |
| 273 | return data; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Sysfs stuff |
| 278 | */ |
| 279 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 280 | static ssize_t show_in_input(struct device *dev, struct device_attribute *attr, |
| 281 | char *buf) |
| 282 | { |
| 283 | struct lm87_data *data = lm87_update_device(dev); |
| 284 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 286 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in[nr], |
| 287 | data->in_scale[nr])); |
| 288 | } |
| 289 | |
| 290 | static ssize_t show_in_min(struct device *dev, |
| 291 | struct device_attribute *attr, char *buf) |
| 292 | { |
| 293 | struct lm87_data *data = lm87_update_device(dev); |
| 294 | int nr = to_sensor_dev_attr(attr)->index; |
| 295 | |
| 296 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[nr], |
| 297 | data->in_scale[nr])); |
| 298 | } |
| 299 | |
| 300 | static ssize_t show_in_max(struct device *dev, |
| 301 | struct device_attribute *attr, char *buf) |
| 302 | { |
| 303 | struct lm87_data *data = lm87_update_device(dev); |
| 304 | int nr = to_sensor_dev_attr(attr)->index; |
| 305 | |
| 306 | return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[nr], |
| 307 | data->in_scale[nr])); |
| 308 | } |
| 309 | |
| 310 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 311 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 313 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 315 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 316 | long val; |
| 317 | int err; |
| 318 | |
| 319 | err = kstrtol(buf, 10, &val); |
| 320 | if (err) |
| 321 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 323 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 325 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MIN(nr) : |
| 326 | LM87_REG_AIN_MIN(nr - 6), data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 327 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 328 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 331 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 332 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 334 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 336 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 337 | long val; |
| 338 | int err; |
| 339 | |
| 340 | err = kstrtol(buf, 10, &val); |
| 341 | if (err) |
| 342 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 344 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 346 | lm87_write_value(client, nr < 6 ? LM87_REG_IN_MAX(nr) : |
| 347 | LM87_REG_AIN_MAX(nr - 6), data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 348 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 349 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | #define set_in(offset) \ |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 353 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 354 | show_in_input, NULL, offset); \ |
| 355 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 356 | show_in_min, set_in_min, offset); \ |
| 357 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 358 | show_in_max, set_in_max, offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | set_in(0); |
| 360 | set_in(1); |
| 361 | set_in(2); |
| 362 | set_in(3); |
| 363 | set_in(4); |
| 364 | set_in(5); |
| 365 | set_in(6); |
| 366 | set_in(7); |
| 367 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 368 | static ssize_t show_temp_input(struct device *dev, |
| 369 | struct device_attribute *attr, char *buf) |
| 370 | { |
| 371 | struct lm87_data *data = lm87_update_device(dev); |
| 372 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 374 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
| 375 | } |
| 376 | |
| 377 | static ssize_t show_temp_low(struct device *dev, |
| 378 | struct device_attribute *attr, char *buf) |
| 379 | { |
| 380 | struct lm87_data *data = lm87_update_device(dev); |
| 381 | int nr = to_sensor_dev_attr(attr)->index; |
| 382 | |
| 383 | return sprintf(buf, "%d\n", |
| 384 | TEMP_FROM_REG(data->temp_low[nr])); |
| 385 | } |
| 386 | |
| 387 | static ssize_t show_temp_high(struct device *dev, |
| 388 | struct device_attribute *attr, char *buf) |
| 389 | { |
| 390 | struct lm87_data *data = lm87_update_device(dev); |
| 391 | int nr = to_sensor_dev_attr(attr)->index; |
| 392 | |
| 393 | return sprintf(buf, "%d\n", |
| 394 | TEMP_FROM_REG(data->temp_high[nr])); |
| 395 | } |
| 396 | |
| 397 | static ssize_t set_temp_low(struct device *dev, struct device_attribute *attr, |
| 398 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 400 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 402 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 403 | long val; |
| 404 | int err; |
| 405 | |
| 406 | err = kstrtol(buf, 10, &val); |
| 407 | if (err) |
| 408 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 410 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | data->temp_low[nr] = TEMP_TO_REG(val); |
| 412 | lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 413 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 414 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 417 | static ssize_t set_temp_high(struct device *dev, struct device_attribute *attr, |
| 418 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 420 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 422 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 423 | long val; |
| 424 | int err; |
| 425 | |
| 426 | err = kstrtol(buf, 10, &val); |
| 427 | if (err) |
| 428 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 430 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | data->temp_high[nr] = TEMP_TO_REG(val); |
| 432 | lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 433 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 434 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | #define set_temp(offset) \ |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 438 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 439 | show_temp_input, NULL, offset - 1); \ |
| 440 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 441 | show_temp_high, set_temp_high, offset - 1); \ |
| 442 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 443 | show_temp_low, set_temp_low, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | set_temp(1); |
| 445 | set_temp(2); |
| 446 | set_temp(3); |
| 447 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 448 | static ssize_t temp1_crit_show(struct device *dev, |
| 449 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | struct lm87_data *data = lm87_update_device(dev); |
| 452 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int)); |
| 453 | } |
| 454 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 455 | static ssize_t temp2_crit_show(struct device *dev, |
| 456 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | struct lm87_data *data = lm87_update_device(dev); |
| 459 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext)); |
| 460 | } |
| 461 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 462 | static DEVICE_ATTR_RO(temp1_crit); |
| 463 | static DEVICE_ATTR_RO(temp2_crit); |
| 464 | static DEVICE_ATTR(temp3_crit, S_IRUGO, temp2_crit_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 466 | static ssize_t show_fan_input(struct device *dev, |
| 467 | struct device_attribute *attr, char *buf) |
| 468 | { |
| 469 | struct lm87_data *data = lm87_update_device(dev); |
| 470 | int nr = to_sensor_dev_attr(attr)->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 472 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
| 473 | FAN_DIV_FROM_REG(data->fan_div[nr]))); |
| 474 | } |
| 475 | |
| 476 | static ssize_t show_fan_min(struct device *dev, |
| 477 | struct device_attribute *attr, char *buf) |
| 478 | { |
| 479 | struct lm87_data *data = lm87_update_device(dev); |
| 480 | int nr = to_sensor_dev_attr(attr)->index; |
| 481 | |
| 482 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 483 | FAN_DIV_FROM_REG(data->fan_div[nr]))); |
| 484 | } |
| 485 | |
| 486 | static ssize_t show_fan_div(struct device *dev, |
| 487 | struct device_attribute *attr, char *buf) |
| 488 | { |
| 489 | struct lm87_data *data = lm87_update_device(dev); |
| 490 | int nr = to_sensor_dev_attr(attr)->index; |
| 491 | |
| 492 | return sprintf(buf, "%d\n", |
| 493 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 494 | } |
| 495 | |
| 496 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 497 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 499 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 501 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 502 | long val; |
| 503 | int err; |
| 504 | |
| 505 | err = kstrtol(buf, 10, &val); |
| 506 | if (err) |
| 507 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 509 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | data->fan_min[nr] = FAN_TO_REG(val, |
| 511 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 512 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 513 | mutex_unlock(&data->update_lock); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 514 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 517 | /* |
| 518 | * Note: we save and restore the fan minimum here, because its value is |
| 519 | * determined in part by the fan clock divider. This follows the principle |
| 520 | * of least surprise; the user doesn't expect the fan minimum to change just |
| 521 | * because the divider changed. |
| 522 | */ |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 523 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 524 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 526 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | struct lm87_data *data = i2c_get_clientdata(client); |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 528 | int nr = to_sensor_dev_attr(attr)->index; |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 529 | long val; |
| 530 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | unsigned long min; |
| 532 | u8 reg; |
| 533 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 534 | err = kstrtol(buf, 10, &val); |
| 535 | if (err) |
| 536 | return err; |
| 537 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 538 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | min = FAN_FROM_REG(data->fan_min[nr], |
| 540 | FAN_DIV_FROM_REG(data->fan_div[nr])); |
| 541 | |
| 542 | switch (val) { |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 543 | case 1: |
| 544 | data->fan_div[nr] = 0; |
| 545 | break; |
| 546 | case 2: |
| 547 | data->fan_div[nr] = 1; |
| 548 | break; |
| 549 | case 4: |
| 550 | data->fan_div[nr] = 2; |
| 551 | break; |
| 552 | case 8: |
| 553 | data->fan_div[nr] = 3; |
| 554 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | default: |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 556 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV); |
| 561 | switch (nr) { |
| 562 | case 0: |
| 563 | reg = (reg & 0xCF) | (data->fan_div[0] << 4); |
| 564 | break; |
| 565 | case 1: |
| 566 | reg = (reg & 0x3F) | (data->fan_div[1] << 6); |
| 567 | break; |
| 568 | } |
| 569 | lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg); |
| 570 | |
| 571 | data->fan_min[nr] = FAN_TO_REG(min, val); |
| 572 | lm87_write_value(client, LM87_REG_FAN_MIN(nr), |
| 573 | data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 574 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | return count; |
| 577 | } |
| 578 | |
| 579 | #define set_fan(offset) \ |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 580 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 581 | show_fan_input, NULL, offset - 1); \ |
| 582 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 583 | show_fan_min, set_fan_min, offset - 1); \ |
| 584 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 585 | show_fan_div, set_fan_div, offset - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | set_fan(1); |
| 587 | set_fan(2); |
| 588 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 589 | static ssize_t alarms_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 590 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | struct lm87_data *data = lm87_update_device(dev); |
| 593 | return sprintf(buf, "%d\n", data->alarms); |
| 594 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 595 | static DEVICE_ATTR_RO(alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 597 | static ssize_t cpu0_vid_show(struct device *dev, |
| 598 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
| 600 | struct lm87_data *data = lm87_update_device(dev); |
| 601 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 602 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 603 | static DEVICE_ATTR_RO(cpu0_vid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 605 | static ssize_t vrm_show(struct device *dev, struct device_attribute *attr, |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 606 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 608 | struct lm87_data *data = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return sprintf(buf, "%d\n", data->vrm); |
| 610 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 611 | static ssize_t vrm_store(struct device *dev, struct device_attribute *attr, |
| 612 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 614 | struct lm87_data *data = dev_get_drvdata(dev); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 615 | unsigned long val; |
| 616 | int err; |
| 617 | |
| 618 | err = kstrtoul(buf, 10, &val); |
| 619 | if (err) |
| 620 | return err; |
Axel Lin | fa642d9 | 2014-08-06 08:24:00 +0800 | [diff] [blame] | 621 | |
| 622 | if (val > 255) |
| 623 | return -EINVAL; |
| 624 | |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 625 | data->vrm = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return count; |
| 627 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 628 | static DEVICE_ATTR_RW(vrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 630 | static ssize_t aout_output_show(struct device *dev, |
| 631 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
| 633 | struct lm87_data *data = lm87_update_device(dev); |
| 634 | return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout)); |
| 635 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 636 | static ssize_t aout_output_store(struct device *dev, |
| 637 | struct device_attribute *attr, |
| 638 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 640 | struct i2c_client *client = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | c6370db | 2012-01-14 20:58:08 -0800 | [diff] [blame] | 642 | long val; |
| 643 | int err; |
| 644 | |
| 645 | err = kstrtol(buf, 10, &val); |
| 646 | if (err) |
| 647 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 649 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | data->aout = AOUT_TO_REG(val); |
| 651 | lm87_write_value(client, LM87_REG_AOUT, data->aout); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 652 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return count; |
| 654 | } |
Julia Lawall | 07a366c | 2016-12-22 13:04:53 +0100 | [diff] [blame] | 655 | static DEVICE_ATTR_RW(aout_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 657 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 658 | char *buf) |
| 659 | { |
| 660 | struct lm87_data *data = lm87_update_device(dev); |
| 661 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 662 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 663 | } |
| 664 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 665 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 666 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 667 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 668 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 669 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9); |
| 670 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 671 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 672 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 673 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 674 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 675 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 676 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 677 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14); |
| 678 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15); |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* |
| 681 | * Real code |
| 682 | */ |
| 683 | |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 684 | static struct attribute *lm87_attributes[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 685 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 686 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 687 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 688 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 689 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 690 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 691 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 692 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 693 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 694 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 695 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 696 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 697 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 698 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 699 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 700 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 701 | |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 702 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 703 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 704 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 705 | &dev_attr_temp1_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 706 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 707 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 708 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 709 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 710 | &dev_attr_temp2_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 711 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
| 712 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 713 | |
| 714 | &dev_attr_alarms.attr, |
| 715 | &dev_attr_aout_output.attr, |
| 716 | |
| 717 | NULL |
| 718 | }; |
| 719 | |
| 720 | static const struct attribute_group lm87_group = { |
| 721 | .attrs = lm87_attributes, |
| 722 | }; |
| 723 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 724 | static struct attribute *lm87_attributes_in6[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 725 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 726 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 727 | &sensor_dev_attr_in6_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 728 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 729 | NULL |
| 730 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 731 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 732 | static const struct attribute_group lm87_group_in6 = { |
| 733 | .attrs = lm87_attributes_in6, |
| 734 | }; |
| 735 | |
| 736 | static struct attribute *lm87_attributes_fan1[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 737 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 738 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 739 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 740 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 741 | NULL |
| 742 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 743 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 744 | static const struct attribute_group lm87_group_fan1 = { |
| 745 | .attrs = lm87_attributes_fan1, |
| 746 | }; |
| 747 | |
| 748 | static struct attribute *lm87_attributes_in7[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 749 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 750 | &sensor_dev_attr_in7_min.dev_attr.attr, |
| 751 | &sensor_dev_attr_in7_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 752 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 753 | NULL |
| 754 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 755 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 756 | static const struct attribute_group lm87_group_in7 = { |
| 757 | .attrs = lm87_attributes_in7, |
| 758 | }; |
| 759 | |
| 760 | static struct attribute *lm87_attributes_fan2[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 761 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 762 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 763 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 764 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 765 | NULL |
| 766 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 767 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 768 | static const struct attribute_group lm87_group_fan2 = { |
| 769 | .attrs = lm87_attributes_fan2, |
| 770 | }; |
| 771 | |
| 772 | static struct attribute *lm87_attributes_temp3[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 773 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 774 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 775 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 776 | &dev_attr_temp3_crit.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 777 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
| 778 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 779 | NULL |
| 780 | }; |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 781 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 782 | static const struct attribute_group lm87_group_temp3 = { |
| 783 | .attrs = lm87_attributes_temp3, |
| 784 | }; |
| 785 | |
| 786 | static struct attribute *lm87_attributes_in0_5[] = { |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 787 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 788 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 789 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 790 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Jean Delvare | 0e190b7 | 2012-01-26 09:38:26 -0800 | [diff] [blame] | 791 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 792 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 793 | &sensor_dev_attr_in5_max.dev_attr.attr, |
Jean Delvare | c2803b9 | 2007-09-26 23:39:01 +0200 | [diff] [blame] | 794 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 795 | NULL |
| 796 | }; |
| 797 | |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 798 | static const struct attribute_group lm87_group_in0_5 = { |
| 799 | .attrs = lm87_attributes_in0_5, |
| 800 | }; |
| 801 | |
| 802 | static struct attribute *lm87_attributes_vid[] = { |
| 803 | &dev_attr_cpu0_vid.attr, |
| 804 | &dev_attr_vrm.attr, |
| 805 | NULL |
| 806 | }; |
| 807 | |
| 808 | static const struct attribute_group lm87_group_vid = { |
| 809 | .attrs = lm87_attributes_vid, |
Mark M. Hoffman | 0501a38 | 2006-09-24 21:14:35 +0200 | [diff] [blame] | 810 | }; |
| 811 | |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 812 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 813 | static int lm87_detect(struct i2c_client *client, struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 815 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 816 | const char *name; |
| 817 | u8 cid, rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 820 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 822 | if (lm87_read_value(client, LM87_REG_CONFIG) & 0x80) |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 823 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | /* Now, we do the remaining detection. */ |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 826 | cid = lm87_read_value(client, LM87_REG_COMPANY_ID); |
| 827 | rev = lm87_read_value(client, LM87_REG_REVISION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 829 | if (cid == 0x02 /* National Semiconductor */ |
| 830 | && (rev >= 0x01 && rev <= 0x08)) |
| 831 | name = "lm87"; |
| 832 | else if (cid == 0x41 /* Analog Devices */ |
| 833 | && (rev & 0xf0) == 0x10) |
| 834 | name = "adm1024"; |
| 835 | else { |
| 836 | dev_dbg(&adapter->dev, "LM87 detection failed at 0x%02x\n", |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 837 | client->addr); |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 838 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 841 | strlcpy(info->type, name, I2C_NAME_SIZE); |
Jean Delvare | a888420 | 2008-07-16 19:30:14 +0200 | [diff] [blame] | 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 846 | static void lm87_restore_config(void *arg) |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 847 | { |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 848 | struct i2c_client *client = arg; |
| 849 | struct lm87_data *data = i2c_get_clientdata(client); |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 850 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 851 | lm87_write_value(client, LM87_REG_CONFIG, data->config); |
Guenter Roeck | 073f1e6c | 2012-01-16 15:11:57 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 854 | static int lm87_init_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
| 856 | struct lm87_data *data = i2c_get_clientdata(client); |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 857 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Jingoo Han | a8b3a3a | 2013-07-30 17:13:06 +0900 | [diff] [blame] | 859 | if (dev_get_platdata(&client->dev)) { |
| 860 | data->channel = *(u8 *)dev_get_platdata(&client->dev); |
Ben Hutchings | 47064d6 | 2008-10-17 17:51:12 +0200 | [diff] [blame] | 861 | lm87_write_value(client, |
| 862 | LM87_REG_CHANNEL_MODE, data->channel); |
| 863 | } else { |
| 864 | data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE); |
| 865 | } |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 866 | data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 868 | rc = devm_add_action(&client->dev, lm87_restore_config, client); |
| 869 | if (rc) |
| 870 | return rc; |
| 871 | |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 872 | if (!(data->config & 0x01)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | int i; |
| 874 | |
| 875 | /* Limits are left uninitialized after power-up */ |
| 876 | for (i = 1; i < 6; i++) { |
| 877 | lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00); |
| 878 | lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF); |
| 879 | } |
| 880 | for (i = 0; i < 2; i++) { |
| 881 | lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F); |
| 882 | lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00); |
| 883 | lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00); |
| 884 | lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF); |
| 885 | } |
| 886 | if (data->channel & CHAN_TEMP3) { |
| 887 | lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F); |
| 888 | lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00); |
| 889 | } else { |
| 890 | lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00); |
| 891 | lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF); |
| 892 | } |
| 893 | } |
Ben Hutchings | 49ae6cc | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 894 | |
| 895 | /* Make sure Start is set and INT#_Clear is clear */ |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 896 | if ((data->config & 0x09) != 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | lm87_write_value(client, LM87_REG_CONFIG, |
Ben Hutchings | d2cac80 | 2008-10-17 17:51:11 +0200 | [diff] [blame] | 898 | (data->config & 0x77) | 0x01); |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 899 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 902 | static int lm87_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 903 | { |
| 904 | struct lm87_data *data; |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 905 | struct device *hwmon_dev; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 906 | int err; |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 907 | unsigned int group_tail = 0; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 908 | |
Guenter Roeck | 5ff512b | 2012-06-02 09:58:10 -0700 | [diff] [blame] | 909 | data = devm_kzalloc(&client->dev, sizeof(struct lm87_data), GFP_KERNEL); |
| 910 | if (!data) |
| 911 | return -ENOMEM; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 912 | |
| 913 | i2c_set_clientdata(client, data); |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 914 | mutex_init(&data->update_lock); |
| 915 | |
| 916 | /* Initialize the LM87 chip */ |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 917 | err = lm87_init_client(client); |
| 918 | if (err) |
| 919 | return err; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 920 | |
| 921 | data->in_scale[0] = 2500; |
| 922 | data->in_scale[1] = 2700; |
| 923 | data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300; |
| 924 | data->in_scale[3] = 5000; |
| 925 | data->in_scale[4] = 12000; |
| 926 | data->in_scale[5] = 2700; |
| 927 | data->in_scale[6] = 1875; |
| 928 | data->in_scale[7] = 1875; |
| 929 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 930 | /* |
| 931 | * Construct the list of attributes, the list depends on the |
| 932 | * configuration of the chip |
| 933 | */ |
| 934 | data->attr_groups[group_tail++] = &lm87_group; |
| 935 | if (data->channel & CHAN_NO_FAN(0)) |
| 936 | data->attr_groups[group_tail++] = &lm87_group_in6; |
| 937 | else |
| 938 | data->attr_groups[group_tail++] = &lm87_group_fan1; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 939 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 940 | if (data->channel & CHAN_NO_FAN(1)) |
| 941 | data->attr_groups[group_tail++] = &lm87_group_in7; |
| 942 | else |
| 943 | data->attr_groups[group_tail++] = &lm87_group_fan2; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 944 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 945 | if (data->channel & CHAN_TEMP3) |
| 946 | data->attr_groups[group_tail++] = &lm87_group_temp3; |
| 947 | else |
| 948 | data->attr_groups[group_tail++] = &lm87_group_in0_5; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 949 | |
| 950 | if (!(data->channel & CHAN_NO_VID)) { |
| 951 | data->vrm = vid_which_vrm(); |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 952 | data->attr_groups[group_tail++] = &lm87_group_vid; |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Jason Gunthorpe | 00a0c90 | 2016-10-26 10:56:59 -0600 | [diff] [blame] | 955 | hwmon_dev = devm_hwmon_device_register_with_groups( |
| 956 | &client->dev, client->name, client, data->attr_groups); |
| 957 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 960 | /* |
| 961 | * Driver data (common to all clients) |
| 962 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 964 | static const struct i2c_device_id lm87_id[] = { |
| 965 | { "lm87", lm87 }, |
| 966 | { "adm1024", adm1024 }, |
| 967 | { } |
| 968 | }; |
| 969 | MODULE_DEVICE_TABLE(i2c, lm87_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 971 | static struct i2c_driver lm87_driver = { |
| 972 | .class = I2C_CLASS_HWMON, |
| 973 | .driver = { |
| 974 | .name = "lm87", |
| 975 | }, |
| 976 | .probe = lm87_probe, |
Jean Delvare | 8652a26 | 2012-01-26 09:37:50 -0800 | [diff] [blame] | 977 | .id_table = lm87_id, |
| 978 | .detect = lm87_detect, |
| 979 | .address_list = normal_i2c, |
| 980 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 982 | module_i2c_driver(lm87_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Jean Delvare | 7c81c60 | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 984 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de> and others"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | MODULE_DESCRIPTION("LM87 driver"); |
| 986 | MODULE_LICENSE("GPL"); |