Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 2 | /* |
| 3 | * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201 |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 4 | * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de> |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/jiffies.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/hwmon.h> |
| 13 | #include <linux/hwmon-sysfs.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/mutex.h> |
| 16 | |
| 17 | /* |
| 18 | * Addresses to scan |
| 19 | */ |
| 20 | |
| 21 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; |
| 22 | |
| 23 | /* |
| 24 | * The EMC6W201 registers |
| 25 | */ |
| 26 | |
| 27 | #define EMC6W201_REG_IN(nr) (0x20 + (nr)) |
| 28 | #define EMC6W201_REG_TEMP(nr) (0x26 + (nr)) |
| 29 | #define EMC6W201_REG_FAN(nr) (0x2C + (nr) * 2) |
| 30 | #define EMC6W201_REG_COMPANY 0x3E |
| 31 | #define EMC6W201_REG_VERSTEP 0x3F |
| 32 | #define EMC6W201_REG_CONFIG 0x40 |
| 33 | #define EMC6W201_REG_IN_LOW(nr) (0x4A + (nr) * 2) |
| 34 | #define EMC6W201_REG_IN_HIGH(nr) (0x4B + (nr) * 2) |
| 35 | #define EMC6W201_REG_TEMP_LOW(nr) (0x56 + (nr) * 2) |
| 36 | #define EMC6W201_REG_TEMP_HIGH(nr) (0x57 + (nr) * 2) |
| 37 | #define EMC6W201_REG_FAN_MIN(nr) (0x62 + (nr) * 2) |
| 38 | |
Guenter Roeck | 94e4441 | 2013-09-06 14:05:43 +0200 | [diff] [blame] | 39 | enum subfeature { input, min, max }; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Per-device data |
| 43 | */ |
| 44 | |
| 45 | struct emc6w201_data { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 46 | struct i2c_client *client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 47 | struct mutex update_lock; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 48 | bool valid; /* false until following fields are valid */ |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 49 | unsigned long last_updated; /* in jiffies */ |
| 50 | |
| 51 | /* registers values */ |
| 52 | u8 in[3][6]; |
| 53 | s8 temp[3][6]; |
| 54 | u16 fan[2][5]; |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * Combine LSB and MSB registers in a single value |
| 59 | * Locking: must be called with data->update_lock held |
| 60 | */ |
| 61 | static u16 emc6w201_read16(struct i2c_client *client, u8 reg) |
| 62 | { |
| 63 | int lsb, msb; |
| 64 | |
| 65 | lsb = i2c_smbus_read_byte_data(client, reg); |
| 66 | msb = i2c_smbus_read_byte_data(client, reg + 1); |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 67 | if (unlikely(lsb < 0 || msb < 0)) { |
| 68 | dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n", |
| 69 | 16, "read", reg); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 70 | return 0xFFFF; /* Arbitrary value */ |
| 71 | } |
| 72 | |
| 73 | return (msb << 8) | lsb; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Write 16-bit value to LSB and MSB registers |
| 78 | * Locking: must be called with data->update_lock held |
| 79 | */ |
| 80 | static int emc6w201_write16(struct i2c_client *client, u8 reg, u16 val) |
| 81 | { |
| 82 | int err; |
| 83 | |
| 84 | err = i2c_smbus_write_byte_data(client, reg, val & 0xff); |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 85 | if (likely(!err)) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 86 | err = i2c_smbus_write_byte_data(client, reg + 1, val >> 8); |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 87 | if (unlikely(err < 0)) |
| 88 | dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n", |
| 89 | 16, "write", reg); |
| 90 | |
| 91 | return err; |
| 92 | } |
| 93 | |
| 94 | /* Read 8-bit value from register */ |
| 95 | static u8 emc6w201_read8(struct i2c_client *client, u8 reg) |
| 96 | { |
| 97 | int val; |
| 98 | |
| 99 | val = i2c_smbus_read_byte_data(client, reg); |
| 100 | if (unlikely(val < 0)) { |
| 101 | dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n", |
| 102 | 8, "read", reg); |
| 103 | return 0x00; /* Arbitrary value */ |
| 104 | } |
| 105 | |
| 106 | return val; |
| 107 | } |
| 108 | |
| 109 | /* Write 8-bit value to register */ |
| 110 | static int emc6w201_write8(struct i2c_client *client, u8 reg, u8 val) |
| 111 | { |
| 112 | int err; |
| 113 | |
| 114 | err = i2c_smbus_write_byte_data(client, reg, val); |
| 115 | if (unlikely(err < 0)) |
| 116 | dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n", |
| 117 | 8, "write", reg); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 118 | |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | static struct emc6w201_data *emc6w201_update_device(struct device *dev) |
| 123 | { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 124 | struct emc6w201_data *data = dev_get_drvdata(dev); |
| 125 | struct i2c_client *client = data->client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 126 | int nr; |
| 127 | |
| 128 | mutex_lock(&data->update_lock); |
| 129 | |
| 130 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 131 | for (nr = 0; nr < 6; nr++) { |
| 132 | data->in[input][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 133 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 134 | EMC6W201_REG_IN(nr)); |
| 135 | data->in[min][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 136 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 137 | EMC6W201_REG_IN_LOW(nr)); |
| 138 | data->in[max][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 139 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 140 | EMC6W201_REG_IN_HIGH(nr)); |
| 141 | } |
| 142 | |
| 143 | for (nr = 0; nr < 6; nr++) { |
| 144 | data->temp[input][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 145 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 146 | EMC6W201_REG_TEMP(nr)); |
| 147 | data->temp[min][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 148 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 149 | EMC6W201_REG_TEMP_LOW(nr)); |
| 150 | data->temp[max][nr] = |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 151 | emc6w201_read8(client, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 152 | EMC6W201_REG_TEMP_HIGH(nr)); |
| 153 | } |
| 154 | |
| 155 | for (nr = 0; nr < 5; nr++) { |
| 156 | data->fan[input][nr] = |
| 157 | emc6w201_read16(client, |
| 158 | EMC6W201_REG_FAN(nr)); |
| 159 | data->fan[min][nr] = |
| 160 | emc6w201_read16(client, |
| 161 | EMC6W201_REG_FAN_MIN(nr)); |
| 162 | } |
| 163 | |
| 164 | data->last_updated = jiffies; |
Paul Fertser | 952a11c | 2021-09-24 22:52:02 +0300 | [diff] [blame] | 165 | data->valid = true; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | mutex_unlock(&data->update_lock); |
| 169 | |
| 170 | return data; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Sysfs callback functions |
| 175 | */ |
| 176 | |
Guenter Roeck | 86266ca | 2012-12-18 18:16:08 -0800 | [diff] [blame] | 177 | static const s16 nominal_mv[6] = { 2500, 1500, 3300, 5000, 1500, 1500 }; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 178 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 179 | static ssize_t in_show(struct device *dev, struct device_attribute *devattr, |
| 180 | char *buf) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 181 | { |
| 182 | struct emc6w201_data *data = emc6w201_update_device(dev); |
| 183 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 184 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 185 | |
| 186 | return sprintf(buf, "%u\n", |
| 187 | (unsigned)data->in[sf][nr] * nominal_mv[nr] / 0xC0); |
| 188 | } |
| 189 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 190 | static ssize_t in_store(struct device *dev, struct device_attribute *devattr, |
| 191 | const char *buf, size_t count) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 192 | { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 193 | struct emc6w201_data *data = dev_get_drvdata(dev); |
| 194 | struct i2c_client *client = data->client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 195 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 196 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 197 | int err; |
| 198 | long val; |
| 199 | u8 reg; |
| 200 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 201 | err = kstrtol(buf, 10, &val); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 202 | if (err < 0) |
| 203 | return err; |
| 204 | |
Guenter Roeck | 59715f4 | 2016-12-04 18:20:52 -0800 | [diff] [blame] | 205 | val = clamp_val(val, 0, 255 * nominal_mv[nr] / 192); |
| 206 | val = DIV_ROUND_CLOSEST(val * 192, nominal_mv[nr]); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 207 | reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr) |
| 208 | : EMC6W201_REG_IN_HIGH(nr); |
| 209 | |
| 210 | mutex_lock(&data->update_lock); |
Guenter Roeck | 59715f4 | 2016-12-04 18:20:52 -0800 | [diff] [blame] | 211 | data->in[sf][nr] = val; |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 212 | err = emc6w201_write8(client, reg, data->in[sf][nr]); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 213 | mutex_unlock(&data->update_lock); |
| 214 | |
| 215 | return err < 0 ? err : count; |
| 216 | } |
| 217 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 218 | static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, |
| 219 | char *buf) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 220 | { |
| 221 | struct emc6w201_data *data = emc6w201_update_device(dev); |
| 222 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 223 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 224 | |
| 225 | return sprintf(buf, "%d\n", (int)data->temp[sf][nr] * 1000); |
| 226 | } |
| 227 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 228 | static ssize_t temp_store(struct device *dev, |
| 229 | struct device_attribute *devattr, const char *buf, |
| 230 | size_t count) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 231 | { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 232 | struct emc6w201_data *data = dev_get_drvdata(dev); |
| 233 | struct i2c_client *client = data->client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 234 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 235 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 236 | int err; |
| 237 | long val; |
| 238 | u8 reg; |
| 239 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 240 | err = kstrtol(buf, 10, &val); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 241 | if (err < 0) |
| 242 | return err; |
| 243 | |
Guenter Roeck | 59715f4 | 2016-12-04 18:20:52 -0800 | [diff] [blame] | 244 | val = clamp_val(val, -127000, 127000); |
Guenter Roeck | 539a719 | 2014-08-05 09:54:04 -0700 | [diff] [blame] | 245 | val = DIV_ROUND_CLOSEST(val, 1000); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 246 | reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr) |
| 247 | : EMC6W201_REG_TEMP_HIGH(nr); |
| 248 | |
| 249 | mutex_lock(&data->update_lock); |
Guenter Roeck | 59715f4 | 2016-12-04 18:20:52 -0800 | [diff] [blame] | 250 | data->temp[sf][nr] = val; |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 251 | err = emc6w201_write8(client, reg, data->temp[sf][nr]); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 252 | mutex_unlock(&data->update_lock); |
| 253 | |
| 254 | return err < 0 ? err : count; |
| 255 | } |
| 256 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 257 | static ssize_t fan_show(struct device *dev, struct device_attribute *devattr, |
| 258 | char *buf) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 259 | { |
| 260 | struct emc6w201_data *data = emc6w201_update_device(dev); |
| 261 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 262 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 263 | unsigned rpm; |
| 264 | |
| 265 | if (data->fan[sf][nr] == 0 || data->fan[sf][nr] == 0xFFFF) |
| 266 | rpm = 0; |
| 267 | else |
| 268 | rpm = 5400000U / data->fan[sf][nr]; |
| 269 | |
| 270 | return sprintf(buf, "%u\n", rpm); |
| 271 | } |
| 272 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 273 | static ssize_t fan_store(struct device *dev, struct device_attribute *devattr, |
| 274 | const char *buf, size_t count) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 275 | { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 276 | struct emc6w201_data *data = dev_get_drvdata(dev); |
| 277 | struct i2c_client *client = data->client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 278 | int sf = to_sensor_dev_attr_2(devattr)->index; |
| 279 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 280 | int err; |
| 281 | unsigned long val; |
| 282 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 283 | err = kstrtoul(buf, 10, &val); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 284 | if (err < 0) |
| 285 | return err; |
| 286 | |
| 287 | if (val == 0) { |
| 288 | val = 0xFFFF; |
| 289 | } else { |
| 290 | val = DIV_ROUND_CLOSEST(5400000U, val); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 291 | val = clamp_val(val, 0, 0xFFFE); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | mutex_lock(&data->update_lock); |
| 295 | data->fan[sf][nr] = val; |
| 296 | err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr), |
| 297 | data->fan[sf][nr]); |
| 298 | mutex_unlock(&data->update_lock); |
| 299 | |
| 300 | return err < 0 ? err : count; |
| 301 | } |
| 302 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 303 | static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, 0, input); |
| 304 | static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, 0, min); |
| 305 | static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, 0, max); |
| 306 | static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, 1, input); |
| 307 | static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, 1, min); |
| 308 | static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, 1, max); |
| 309 | static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, 2, input); |
| 310 | static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, 2, min); |
| 311 | static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, 2, max); |
| 312 | static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, 3, input); |
| 313 | static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, 3, min); |
| 314 | static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, 3, max); |
| 315 | static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, 4, input); |
| 316 | static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, 4, min); |
| 317 | static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, 4, max); |
| 318 | static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, 5, input); |
| 319 | static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, 5, min); |
| 320 | static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, 5, max); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 321 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 322 | static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, input); |
| 323 | static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 0, min); |
| 324 | static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, max); |
| 325 | static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 1, input); |
| 326 | static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, min); |
| 327 | static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, max); |
| 328 | static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 2, input); |
| 329 | static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 2, min); |
| 330 | static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, max); |
| 331 | static SENSOR_DEVICE_ATTR_2_RO(temp4_input, temp, 3, input); |
| 332 | static SENSOR_DEVICE_ATTR_2_RW(temp4_min, temp, 3, min); |
| 333 | static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, max); |
| 334 | static SENSOR_DEVICE_ATTR_2_RO(temp5_input, temp, 4, input); |
| 335 | static SENSOR_DEVICE_ATTR_2_RW(temp5_min, temp, 4, min); |
| 336 | static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, max); |
| 337 | static SENSOR_DEVICE_ATTR_2_RO(temp6_input, temp, 5, input); |
| 338 | static SENSOR_DEVICE_ATTR_2_RW(temp6_min, temp, 5, min); |
| 339 | static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, max); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 340 | |
Guenter Roeck | 7a61d71 | 2018-12-10 14:02:06 -0800 | [diff] [blame] | 341 | static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, 0, input); |
| 342 | static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, 0, min); |
| 343 | static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, 1, input); |
| 344 | static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, 1, min); |
| 345 | static SENSOR_DEVICE_ATTR_2_RO(fan3_input, fan, 2, input); |
| 346 | static SENSOR_DEVICE_ATTR_2_RW(fan3_min, fan, 2, min); |
| 347 | static SENSOR_DEVICE_ATTR_2_RO(fan4_input, fan, 3, input); |
| 348 | static SENSOR_DEVICE_ATTR_2_RW(fan4_min, fan, 3, min); |
| 349 | static SENSOR_DEVICE_ATTR_2_RO(fan5_input, fan, 4, input); |
| 350 | static SENSOR_DEVICE_ATTR_2_RW(fan5_min, fan, 4, min); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 351 | |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 352 | static struct attribute *emc6w201_attrs[] = { |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 353 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 354 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 355 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 356 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 357 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 358 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 359 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 360 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 361 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 362 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 363 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 364 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 365 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 366 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 367 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 368 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 369 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 370 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 371 | |
| 372 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 373 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 374 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 375 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 376 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 377 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 378 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 379 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 380 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 381 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 382 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 383 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 384 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 385 | &sensor_dev_attr_temp5_min.dev_attr.attr, |
| 386 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
| 387 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 388 | &sensor_dev_attr_temp6_min.dev_attr.attr, |
| 389 | &sensor_dev_attr_temp6_max.dev_attr.attr, |
| 390 | |
| 391 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 392 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 393 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 394 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 395 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 396 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 397 | &sensor_dev_attr_fan4_input.dev_attr.attr, |
| 398 | &sensor_dev_attr_fan4_min.dev_attr.attr, |
| 399 | &sensor_dev_attr_fan5_input.dev_attr.attr, |
| 400 | &sensor_dev_attr_fan5_min.dev_attr.attr, |
| 401 | NULL |
| 402 | }; |
| 403 | |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 404 | ATTRIBUTE_GROUPS(emc6w201); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Driver interface |
| 408 | */ |
| 409 | |
| 410 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 411 | static int emc6w201_detect(struct i2c_client *client, |
| 412 | struct i2c_board_info *info) |
| 413 | { |
| 414 | struct i2c_adapter *adapter = client->adapter; |
| 415 | int company, verstep, config; |
| 416 | |
| 417 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 418 | return -ENODEV; |
| 419 | |
| 420 | /* Identification */ |
| 421 | company = i2c_smbus_read_byte_data(client, EMC6W201_REG_COMPANY); |
| 422 | if (company != 0x5C) |
| 423 | return -ENODEV; |
| 424 | verstep = i2c_smbus_read_byte_data(client, EMC6W201_REG_VERSTEP); |
| 425 | if (verstep < 0 || (verstep & 0xF0) != 0xB0) |
| 426 | return -ENODEV; |
| 427 | if ((verstep & 0x0F) > 2) { |
Colin Ian King | 51b8c2c | 2016-06-24 18:31:32 +0100 | [diff] [blame] | 428 | dev_dbg(&client->dev, "Unknown EMC6W201 stepping %d\n", |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 429 | verstep & 0x0F); |
| 430 | return -ENODEV; |
| 431 | } |
| 432 | |
| 433 | /* Check configuration */ |
| 434 | config = i2c_smbus_read_byte_data(client, EMC6W201_REG_CONFIG); |
Jean Delvare | b6b2a1e | 2011-07-03 13:32:53 +0200 | [diff] [blame] | 435 | if (config < 0 || (config & 0xF4) != 0x04) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | if (!(config & 0x01)) { |
| 438 | dev_err(&client->dev, "Monitoring not enabled\n"); |
| 439 | return -ENODEV; |
| 440 | } |
| 441 | |
| 442 | strlcpy(info->type, "emc6w201", I2C_NAME_SIZE); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 447 | static int emc6w201_probe(struct i2c_client *client) |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 448 | { |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 449 | struct device *dev = &client->dev; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 450 | struct emc6w201_data *data; |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 451 | struct device *hwmon_dev; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 452 | |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 453 | data = devm_kzalloc(dev, sizeof(struct emc6w201_data), GFP_KERNEL); |
Guenter Roeck | 6ecffe1 | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 454 | if (!data) |
| 455 | return -ENOMEM; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 456 | |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 457 | data->client = client; |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 458 | mutex_init(&data->update_lock); |
| 459 | |
Axel Lin | 7b3dadc | 2014-06-29 11:32:01 +0800 | [diff] [blame] | 460 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 461 | data, |
| 462 | emc6w201_groups); |
| 463 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static const struct i2c_device_id emc6w201_id[] = { |
| 467 | { "emc6w201", 0 }, |
| 468 | { } |
| 469 | }; |
| 470 | MODULE_DEVICE_TABLE(i2c, emc6w201_id); |
| 471 | |
| 472 | static struct i2c_driver emc6w201_driver = { |
| 473 | .class = I2C_CLASS_HWMON, |
| 474 | .driver = { |
| 475 | .name = "emc6w201", |
| 476 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 477 | .probe_new = emc6w201_probe, |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 478 | .id_table = emc6w201_id, |
| 479 | .detect = emc6w201_detect, |
| 480 | .address_list = normal_i2c, |
| 481 | }; |
| 482 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 483 | module_i2c_driver(emc6w201_driver); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 484 | |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 485 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
Jean Delvare | b0b349a | 2011-05-25 20:43:33 +0200 | [diff] [blame] | 486 | MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver"); |
| 487 | MODULE_LICENSE("GPL"); |