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