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