Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 1 | /* |
Guenter Roeck | bf0f3a0 | 2012-01-19 11:02:17 -0800 | [diff] [blame] | 2 | * emc2103.c - Support for SMSC EMC2103 |
| 3 | * Copyright (c) 2010 SMSC |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/hwmon.h> |
| 26 | #include <linux/hwmon-sysfs.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/mutex.h> |
| 29 | |
| 30 | /* Addresses scanned */ |
| 31 | static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END }; |
| 32 | |
| 33 | static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 }; |
| 34 | static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a }; |
| 35 | static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 }; |
| 36 | |
| 37 | #define REG_CONF1 0x20 |
| 38 | #define REG_TEMP_MAX_ALARM 0x24 |
| 39 | #define REG_TEMP_MIN_ALARM 0x25 |
| 40 | #define REG_FAN_CONF1 0x42 |
| 41 | #define REG_FAN_TARGET_LO 0x4c |
| 42 | #define REG_FAN_TARGET_HI 0x4d |
| 43 | #define REG_FAN_TACH_HI 0x4e |
| 44 | #define REG_FAN_TACH_LO 0x4f |
| 45 | #define REG_PRODUCT_ID 0xfd |
| 46 | #define REG_MFG_ID 0xfe |
| 47 | |
| 48 | /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */ |
| 49 | #define FAN_RPM_FACTOR 3932160 |
| 50 | |
Guenter Roeck | bf0f3a0 | 2012-01-19 11:02:17 -0800 | [diff] [blame] | 51 | /* |
| 52 | * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 53 | * in anti-parallel mode, and in this configuration both can be read |
| 54 | * independently (so we have 4 temperature inputs). The device can't |
| 55 | * detect if it's connected in this mode, so we have to manually enable |
| 56 | * it. Default is to leave the device in the state it's already in (-1). |
Guenter Roeck | bf0f3a0 | 2012-01-19 11:02:17 -0800 | [diff] [blame] | 57 | * This parameter allows APD mode to be optionally forced on or off |
| 58 | */ |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 59 | static int apd = -1; |
Rusty Russell | 69116f2 | 2012-01-13 09:32:17 +1030 | [diff] [blame] | 60 | module_param(apd, bint, 0); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 61 | MODULE_PARM_DESC(init, "Set to zero to disable anti-parallel diode mode"); |
| 62 | |
| 63 | struct temperature { |
| 64 | s8 degrees; |
| 65 | u8 fraction; /* 0-7 multiples of 0.125 */ |
| 66 | }; |
| 67 | |
| 68 | struct emc2103_data { |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 69 | struct i2c_client *client; |
| 70 | const struct attribute_group *groups[4]; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 71 | struct mutex update_lock; |
| 72 | bool valid; /* registers are valid */ |
| 73 | bool fan_rpm_control; |
| 74 | int temp_count; /* num of temp sensors */ |
| 75 | unsigned long last_updated; /* in jiffies */ |
| 76 | struct temperature temp[4]; /* internal + 3 external */ |
| 77 | s8 temp_min[4]; /* no fractional part */ |
| 78 | s8 temp_max[4]; /* no fractional part */ |
| 79 | u8 temp_min_alarm; |
| 80 | u8 temp_max_alarm; |
| 81 | u8 fan_multiplier; |
| 82 | u16 fan_tach; |
| 83 | u16 fan_target; |
| 84 | }; |
| 85 | |
| 86 | static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output) |
| 87 | { |
| 88 | int status = i2c_smbus_read_byte_data(client, i2c_reg); |
| 89 | if (status < 0) { |
| 90 | dev_warn(&client->dev, "reg 0x%02x, err %d\n", |
| 91 | i2c_reg, status); |
| 92 | } else { |
| 93 | *output = status; |
| 94 | } |
| 95 | return status; |
| 96 | } |
| 97 | |
| 98 | static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg, |
| 99 | struct temperature *temp) |
| 100 | { |
| 101 | u8 degrees, fractional; |
| 102 | |
| 103 | if (read_u8_from_i2c(client, i2c_reg, °rees) < 0) |
| 104 | return; |
| 105 | |
| 106 | if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0) |
| 107 | return; |
| 108 | |
| 109 | temp->degrees = degrees; |
| 110 | temp->fraction = (fractional & 0xe0) >> 5; |
| 111 | } |
| 112 | |
| 113 | static void read_fan_from_i2c(struct i2c_client *client, u16 *output, |
| 114 | u8 hi_addr, u8 lo_addr) |
| 115 | { |
| 116 | u8 high_byte, lo_byte; |
| 117 | |
| 118 | if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0) |
| 119 | return; |
| 120 | |
| 121 | if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0) |
| 122 | return; |
| 123 | |
| 124 | *output = ((u16)high_byte << 5) | (lo_byte >> 3); |
| 125 | } |
| 126 | |
| 127 | static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target) |
| 128 | { |
| 129 | u8 high_byte = (new_target & 0x1fe0) >> 5; |
| 130 | u8 low_byte = (new_target & 0x001f) << 3; |
| 131 | i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte); |
| 132 | i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte); |
| 133 | } |
| 134 | |
| 135 | static void read_fan_config_from_i2c(struct i2c_client *client) |
| 136 | |
| 137 | { |
| 138 | struct emc2103_data *data = i2c_get_clientdata(client); |
| 139 | u8 conf1; |
| 140 | |
| 141 | if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0) |
| 142 | return; |
| 143 | |
| 144 | data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5); |
| 145 | data->fan_rpm_control = (conf1 & 0x80) != 0; |
| 146 | } |
| 147 | |
| 148 | static struct emc2103_data *emc2103_update_device(struct device *dev) |
| 149 | { |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 150 | struct emc2103_data *data = dev_get_drvdata(dev); |
| 151 | struct i2c_client *client = data->client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 152 | |
| 153 | mutex_lock(&data->update_lock); |
| 154 | |
| 155 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 156 | || !data->valid) { |
| 157 | int i; |
| 158 | |
| 159 | for (i = 0; i < data->temp_count; i++) { |
| 160 | read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]); |
| 161 | read_u8_from_i2c(client, REG_TEMP_MIN[i], |
| 162 | &data->temp_min[i]); |
| 163 | read_u8_from_i2c(client, REG_TEMP_MAX[i], |
| 164 | &data->temp_max[i]); |
| 165 | } |
| 166 | |
| 167 | read_u8_from_i2c(client, REG_TEMP_MIN_ALARM, |
| 168 | &data->temp_min_alarm); |
| 169 | read_u8_from_i2c(client, REG_TEMP_MAX_ALARM, |
| 170 | &data->temp_max_alarm); |
| 171 | |
| 172 | read_fan_from_i2c(client, &data->fan_tach, |
| 173 | REG_FAN_TACH_HI, REG_FAN_TACH_LO); |
| 174 | read_fan_from_i2c(client, &data->fan_target, |
| 175 | REG_FAN_TARGET_HI, REG_FAN_TARGET_LO); |
| 176 | read_fan_config_from_i2c(client); |
| 177 | |
| 178 | data->last_updated = jiffies; |
| 179 | data->valid = true; |
| 180 | } |
| 181 | |
| 182 | mutex_unlock(&data->update_lock); |
| 183 | |
| 184 | return data; |
| 185 | } |
| 186 | |
| 187 | static ssize_t |
| 188 | show_temp(struct device *dev, struct device_attribute *da, char *buf) |
| 189 | { |
| 190 | int nr = to_sensor_dev_attr(da)->index; |
| 191 | struct emc2103_data *data = emc2103_update_device(dev); |
| 192 | int millidegrees = data->temp[nr].degrees * 1000 |
| 193 | + data->temp[nr].fraction * 125; |
| 194 | return sprintf(buf, "%d\n", millidegrees); |
| 195 | } |
| 196 | |
| 197 | static ssize_t |
| 198 | show_temp_min(struct device *dev, struct device_attribute *da, char *buf) |
| 199 | { |
| 200 | int nr = to_sensor_dev_attr(da)->index; |
| 201 | struct emc2103_data *data = emc2103_update_device(dev); |
| 202 | int millidegrees = data->temp_min[nr] * 1000; |
| 203 | return sprintf(buf, "%d\n", millidegrees); |
| 204 | } |
| 205 | |
| 206 | static ssize_t |
| 207 | show_temp_max(struct device *dev, struct device_attribute *da, char *buf) |
| 208 | { |
| 209 | int nr = to_sensor_dev_attr(da)->index; |
| 210 | struct emc2103_data *data = emc2103_update_device(dev); |
| 211 | int millidegrees = data->temp_max[nr] * 1000; |
| 212 | return sprintf(buf, "%d\n", millidegrees); |
| 213 | } |
| 214 | |
| 215 | static ssize_t |
| 216 | show_temp_fault(struct device *dev, struct device_attribute *da, char *buf) |
| 217 | { |
| 218 | int nr = to_sensor_dev_attr(da)->index; |
| 219 | struct emc2103_data *data = emc2103_update_device(dev); |
| 220 | bool fault = (data->temp[nr].degrees == -128); |
| 221 | return sprintf(buf, "%d\n", fault ? 1 : 0); |
| 222 | } |
| 223 | |
| 224 | static ssize_t |
| 225 | show_temp_min_alarm(struct device *dev, struct device_attribute *da, char *buf) |
| 226 | { |
| 227 | int nr = to_sensor_dev_attr(da)->index; |
| 228 | struct emc2103_data *data = emc2103_update_device(dev); |
| 229 | bool alarm = data->temp_min_alarm & (1 << nr); |
| 230 | return sprintf(buf, "%d\n", alarm ? 1 : 0); |
| 231 | } |
| 232 | |
| 233 | static ssize_t |
| 234 | show_temp_max_alarm(struct device *dev, struct device_attribute *da, char *buf) |
| 235 | { |
| 236 | int nr = to_sensor_dev_attr(da)->index; |
| 237 | struct emc2103_data *data = emc2103_update_device(dev); |
| 238 | bool alarm = data->temp_max_alarm & (1 << nr); |
| 239 | return sprintf(buf, "%d\n", alarm ? 1 : 0); |
| 240 | } |
| 241 | |
| 242 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *da, |
| 243 | const char *buf, size_t count) |
| 244 | { |
| 245 | int nr = to_sensor_dev_attr(da)->index; |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 246 | struct emc2103_data *data = dev_get_drvdata(dev); |
| 247 | struct i2c_client *client = data->client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 248 | long val; |
| 249 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 250 | int result = kstrtol(buf, 10, &val); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 251 | if (result < 0) |
Sachin Kamat | 1a3abbd | 2013-09-11 09:49:48 +0530 | [diff] [blame] | 252 | return result; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 253 | |
Guenter Roeck | f6c2dd2 | 2014-07-06 11:39:24 -0700 | [diff] [blame] | 254 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 255 | |
| 256 | mutex_lock(&data->update_lock); |
| 257 | data->temp_min[nr] = val; |
| 258 | i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val); |
| 259 | mutex_unlock(&data->update_lock); |
| 260 | |
| 261 | return count; |
| 262 | } |
| 263 | |
| 264 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *da, |
| 265 | const char *buf, size_t count) |
| 266 | { |
| 267 | int nr = to_sensor_dev_attr(da)->index; |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 268 | struct emc2103_data *data = dev_get_drvdata(dev); |
| 269 | struct i2c_client *client = data->client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 270 | long val; |
| 271 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 272 | int result = kstrtol(buf, 10, &val); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 273 | if (result < 0) |
Sachin Kamat | 1a3abbd | 2013-09-11 09:49:48 +0530 | [diff] [blame] | 274 | return result; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 275 | |
Guenter Roeck | f6c2dd2 | 2014-07-06 11:39:24 -0700 | [diff] [blame] | 276 | val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 277 | |
| 278 | mutex_lock(&data->update_lock); |
| 279 | data->temp_max[nr] = val; |
| 280 | i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val); |
| 281 | mutex_unlock(&data->update_lock); |
| 282 | |
| 283 | return count; |
| 284 | } |
| 285 | |
| 286 | static ssize_t |
| 287 | show_fan(struct device *dev, struct device_attribute *da, char *buf) |
| 288 | { |
| 289 | struct emc2103_data *data = emc2103_update_device(dev); |
| 290 | int rpm = 0; |
| 291 | if (data->fan_tach != 0) |
| 292 | rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach; |
| 293 | return sprintf(buf, "%d\n", rpm); |
| 294 | } |
| 295 | |
| 296 | static ssize_t |
| 297 | show_fan_div(struct device *dev, struct device_attribute *da, char *buf) |
| 298 | { |
| 299 | struct emc2103_data *data = emc2103_update_device(dev); |
| 300 | int fan_div = 8 / data->fan_multiplier; |
| 301 | return sprintf(buf, "%d\n", fan_div); |
| 302 | } |
| 303 | |
Guenter Roeck | bf0f3a0 | 2012-01-19 11:02:17 -0800 | [diff] [blame] | 304 | /* |
| 305 | * Note: we also update the fan target here, because its value is |
| 306 | * determined in part by the fan clock divider. This follows the principle |
| 307 | * of least surprise; the user doesn't expect the fan target to change just |
| 308 | * because the divider changed. |
| 309 | */ |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 310 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *da, |
| 311 | const char *buf, size_t count) |
| 312 | { |
| 313 | struct emc2103_data *data = emc2103_update_device(dev); |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 314 | struct i2c_client *client = data->client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 315 | int new_range_bits, old_div = 8 / data->fan_multiplier; |
| 316 | long new_div; |
| 317 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 318 | int status = kstrtol(buf, 10, &new_div); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 319 | if (status < 0) |
Sachin Kamat | 1a3abbd | 2013-09-11 09:49:48 +0530 | [diff] [blame] | 320 | return status; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 321 | |
| 322 | if (new_div == old_div) /* No change */ |
| 323 | return count; |
| 324 | |
| 325 | switch (new_div) { |
| 326 | case 1: |
| 327 | new_range_bits = 3; |
| 328 | break; |
| 329 | case 2: |
| 330 | new_range_bits = 2; |
| 331 | break; |
| 332 | case 4: |
| 333 | new_range_bits = 1; |
| 334 | break; |
| 335 | case 8: |
| 336 | new_range_bits = 0; |
| 337 | break; |
| 338 | default: |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | mutex_lock(&data->update_lock); |
| 343 | |
| 344 | status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1); |
| 345 | if (status < 0) { |
| 346 | dev_dbg(&client->dev, "reg 0x%02x, err %d\n", |
| 347 | REG_FAN_CONF1, status); |
| 348 | mutex_unlock(&data->update_lock); |
Guenter Roeck | 9bf3bab | 2014-02-11 21:34:21 -0800 | [diff] [blame] | 349 | return status; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 350 | } |
| 351 | status &= 0x9F; |
| 352 | status |= (new_range_bits << 5); |
| 353 | i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status); |
| 354 | |
| 355 | data->fan_multiplier = 8 / new_div; |
| 356 | |
| 357 | /* update fan target if high byte is not disabled */ |
| 358 | if ((data->fan_target & 0x1fe0) != 0x1fe0) { |
| 359 | u16 new_target = (data->fan_target * old_div) / new_div; |
| 360 | data->fan_target = min(new_target, (u16)0x1fff); |
| 361 | write_fan_target_to_i2c(client, data->fan_target); |
| 362 | } |
| 363 | |
| 364 | /* invalidate data to force re-read from hardware */ |
| 365 | data->valid = false; |
| 366 | |
| 367 | mutex_unlock(&data->update_lock); |
| 368 | return count; |
| 369 | } |
| 370 | |
| 371 | static ssize_t |
| 372 | show_fan_target(struct device *dev, struct device_attribute *da, char *buf) |
| 373 | { |
| 374 | struct emc2103_data *data = emc2103_update_device(dev); |
| 375 | int rpm = 0; |
| 376 | |
| 377 | /* high byte of 0xff indicates disabled so return 0 */ |
| 378 | if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0)) |
| 379 | rpm = (FAN_RPM_FACTOR * data->fan_multiplier) |
| 380 | / data->fan_target; |
| 381 | |
| 382 | return sprintf(buf, "%d\n", rpm); |
| 383 | } |
| 384 | |
| 385 | static ssize_t set_fan_target(struct device *dev, struct device_attribute *da, |
| 386 | const char *buf, size_t count) |
| 387 | { |
| 388 | struct emc2103_data *data = emc2103_update_device(dev); |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 389 | struct i2c_client *client = data->client; |
Guenter Roeck | f6c2dd2 | 2014-07-06 11:39:24 -0700 | [diff] [blame] | 390 | unsigned long rpm_target; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 391 | |
Guenter Roeck | f6c2dd2 | 2014-07-06 11:39:24 -0700 | [diff] [blame] | 392 | int result = kstrtoul(buf, 10, &rpm_target); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 393 | if (result < 0) |
Sachin Kamat | 1a3abbd | 2013-09-11 09:49:48 +0530 | [diff] [blame] | 394 | return result; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 395 | |
| 396 | /* Datasheet states 16384 as maximum RPM target (table 3.2) */ |
Guenter Roeck | f6c2dd2 | 2014-07-06 11:39:24 -0700 | [diff] [blame] | 397 | rpm_target = clamp_val(rpm_target, 0, 16384); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 398 | |
| 399 | mutex_lock(&data->update_lock); |
| 400 | |
| 401 | if (rpm_target == 0) |
| 402 | data->fan_target = 0x1fff; |
| 403 | else |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 404 | data->fan_target = clamp_val( |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 405 | (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target, |
| 406 | 0, 0x1fff); |
| 407 | |
| 408 | write_fan_target_to_i2c(client, data->fan_target); |
| 409 | |
| 410 | mutex_unlock(&data->update_lock); |
| 411 | return count; |
| 412 | } |
| 413 | |
| 414 | static ssize_t |
| 415 | show_fan_fault(struct device *dev, struct device_attribute *da, char *buf) |
| 416 | { |
| 417 | struct emc2103_data *data = emc2103_update_device(dev); |
| 418 | bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0); |
| 419 | return sprintf(buf, "%d\n", fault ? 1 : 0); |
| 420 | } |
| 421 | |
| 422 | static ssize_t |
| 423 | show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf) |
| 424 | { |
| 425 | struct emc2103_data *data = emc2103_update_device(dev); |
| 426 | return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0); |
| 427 | } |
| 428 | |
| 429 | static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da, |
| 430 | const char *buf, size_t count) |
| 431 | { |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 432 | struct emc2103_data *data = dev_get_drvdata(dev); |
| 433 | struct i2c_client *client = data->client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 434 | long new_value; |
| 435 | u8 conf_reg; |
| 436 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 437 | int result = kstrtol(buf, 10, &new_value); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 438 | if (result < 0) |
Sachin Kamat | 1a3abbd | 2013-09-11 09:49:48 +0530 | [diff] [blame] | 439 | return result; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 440 | |
| 441 | mutex_lock(&data->update_lock); |
| 442 | switch (new_value) { |
| 443 | case 0: |
| 444 | data->fan_rpm_control = false; |
| 445 | break; |
| 446 | case 3: |
| 447 | data->fan_rpm_control = true; |
| 448 | break; |
| 449 | default: |
Guenter Roeck | 2355375 | 2012-06-02 10:16:32 -0700 | [diff] [blame] | 450 | count = -EINVAL; |
| 451 | goto err; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Guenter Roeck | 2355375 | 2012-06-02 10:16:32 -0700 | [diff] [blame] | 454 | result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg); |
| 455 | if (result) { |
| 456 | count = result; |
| 457 | goto err; |
| 458 | } |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 459 | |
| 460 | if (data->fan_rpm_control) |
| 461 | conf_reg |= 0x80; |
| 462 | else |
| 463 | conf_reg &= ~0x80; |
| 464 | |
| 465 | i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg); |
Guenter Roeck | 2355375 | 2012-06-02 10:16:32 -0700 | [diff] [blame] | 466 | err: |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 467 | mutex_unlock(&data->update_lock); |
| 468 | return count; |
| 469 | } |
| 470 | |
| 471 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 472 | static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min, |
| 473 | set_temp_min, 0); |
| 474 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max, |
| 475 | set_temp_max, 0); |
| 476 | static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0); |
| 477 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm, |
| 478 | NULL, 0); |
| 479 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm, |
| 480 | NULL, 0); |
| 481 | |
| 482 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 483 | static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min, |
| 484 | set_temp_min, 1); |
| 485 | static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max, |
| 486 | set_temp_max, 1); |
| 487 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1); |
| 488 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm, |
| 489 | NULL, 1); |
| 490 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm, |
| 491 | NULL, 1); |
| 492 | |
| 493 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); |
| 494 | static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min, |
| 495 | set_temp_min, 2); |
| 496 | static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max, |
| 497 | set_temp_max, 2); |
| 498 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2); |
| 499 | static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm, |
| 500 | NULL, 2); |
| 501 | static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm, |
| 502 | NULL, 2); |
| 503 | |
| 504 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); |
| 505 | static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min, |
| 506 | set_temp_min, 3); |
| 507 | static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max, |
| 508 | set_temp_max, 3); |
| 509 | static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3); |
| 510 | static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm, |
| 511 | NULL, 3); |
| 512 | static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm, |
| 513 | NULL, 3); |
| 514 | |
| 515 | static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL); |
| 516 | static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div); |
| 517 | static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target, |
| 518 | set_fan_target); |
| 519 | static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL); |
| 520 | |
| 521 | static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable, |
| 522 | set_pwm_enable); |
| 523 | |
| 524 | /* sensors present on all models */ |
| 525 | static struct attribute *emc2103_attributes[] = { |
| 526 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 527 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 528 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 529 | &sensor_dev_attr_temp1_fault.dev_attr.attr, |
| 530 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 531 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 532 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 533 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 534 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 535 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 536 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 537 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 538 | &dev_attr_fan1_input.attr, |
| 539 | &dev_attr_fan1_div.attr, |
| 540 | &dev_attr_fan1_target.attr, |
| 541 | &dev_attr_fan1_fault.attr, |
| 542 | &dev_attr_pwm1_enable.attr, |
| 543 | NULL |
| 544 | }; |
| 545 | |
| 546 | /* extra temperature sensors only present on 2103-2 and 2103-4 */ |
| 547 | static struct attribute *emc2103_attributes_temp3[] = { |
| 548 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 549 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 550 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 551 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 552 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 553 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 554 | NULL |
| 555 | }; |
| 556 | |
| 557 | /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */ |
| 558 | static struct attribute *emc2103_attributes_temp4[] = { |
| 559 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 560 | &sensor_dev_attr_temp4_min.dev_attr.attr, |
| 561 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
| 562 | &sensor_dev_attr_temp4_fault.dev_attr.attr, |
| 563 | &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, |
| 564 | &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, |
| 565 | NULL |
| 566 | }; |
| 567 | |
| 568 | static const struct attribute_group emc2103_group = { |
| 569 | .attrs = emc2103_attributes, |
| 570 | }; |
| 571 | |
| 572 | static const struct attribute_group emc2103_temp3_group = { |
| 573 | .attrs = emc2103_attributes_temp3, |
| 574 | }; |
| 575 | |
| 576 | static const struct attribute_group emc2103_temp4_group = { |
| 577 | .attrs = emc2103_attributes_temp4, |
| 578 | }; |
| 579 | |
| 580 | static int |
| 581 | emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 582 | { |
| 583 | struct emc2103_data *data; |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 584 | struct device *hwmon_dev; |
| 585 | int status, idx = 0; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 586 | |
| 587 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 588 | return -EIO; |
| 589 | |
Guenter Roeck | 59da32d | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 590 | data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data), |
| 591 | GFP_KERNEL); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 592 | if (!data) |
| 593 | return -ENOMEM; |
| 594 | |
| 595 | i2c_set_clientdata(client, data); |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 596 | data->client = client; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 597 | mutex_init(&data->update_lock); |
| 598 | |
| 599 | /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */ |
| 600 | status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID); |
| 601 | if (status == 0x24) { |
| 602 | /* 2103-1 only has 1 external diode */ |
| 603 | data->temp_count = 2; |
| 604 | } else { |
| 605 | /* 2103-2 and 2103-4 have 3 or 4 external diodes */ |
| 606 | status = i2c_smbus_read_byte_data(client, REG_CONF1); |
| 607 | if (status < 0) { |
| 608 | dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1, |
| 609 | status); |
Guenter Roeck | 59da32d | 2012-06-02 09:58:04 -0700 | [diff] [blame] | 610 | return status; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* detect current state of hardware */ |
| 614 | data->temp_count = (status & 0x01) ? 4 : 3; |
| 615 | |
| 616 | /* force APD state if module parameter is set */ |
| 617 | if (apd == 0) { |
| 618 | /* force APD mode off */ |
| 619 | data->temp_count = 3; |
| 620 | status &= ~(0x01); |
| 621 | i2c_smbus_write_byte_data(client, REG_CONF1, status); |
| 622 | } else if (apd == 1) { |
| 623 | /* force APD mode on */ |
| 624 | data->temp_count = 4; |
| 625 | status |= 0x01; |
| 626 | i2c_smbus_write_byte_data(client, REG_CONF1, status); |
| 627 | } |
| 628 | } |
| 629 | |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 630 | /* sysfs hooks */ |
| 631 | data->groups[idx++] = &emc2103_group; |
| 632 | if (data->temp_count >= 3) |
| 633 | data->groups[idx++] = &emc2103_temp3_group; |
| 634 | if (data->temp_count == 4) |
| 635 | data->groups[idx++] = &emc2103_temp4_group; |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 636 | |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 637 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 638 | client->name, data, |
| 639 | data->groups); |
| 640 | if (IS_ERR(hwmon_dev)) |
| 641 | return PTR_ERR(hwmon_dev); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 642 | |
| 643 | dev_info(&client->dev, "%s: sensor '%s'\n", |
Axel Lin | 9dd304f | 2014-07-19 22:52:40 +0800 | [diff] [blame] | 644 | dev_name(hwmon_dev), client->name); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 645 | |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static const struct i2c_device_id emc2103_ids[] = { |
| 650 | { "emc2103", 0, }, |
| 651 | { /* LIST END */ } |
| 652 | }; |
| 653 | MODULE_DEVICE_TABLE(i2c, emc2103_ids); |
| 654 | |
| 655 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 656 | static int |
| 657 | emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info) |
| 658 | { |
| 659 | struct i2c_adapter *adapter = new_client->adapter; |
| 660 | int manufacturer, product; |
| 661 | |
| 662 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 663 | return -ENODEV; |
| 664 | |
| 665 | manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID); |
| 666 | if (manufacturer != 0x5D) |
| 667 | return -ENODEV; |
| 668 | |
| 669 | product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID); |
| 670 | if ((product != 0x24) && (product != 0x26)) |
| 671 | return -ENODEV; |
| 672 | |
| 673 | strlcpy(info->type, "emc2103", I2C_NAME_SIZE); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static struct i2c_driver emc2103_driver = { |
| 679 | .class = I2C_CLASS_HWMON, |
| 680 | .driver = { |
| 681 | .name = "emc2103", |
| 682 | }, |
| 683 | .probe = emc2103_probe, |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 684 | .id_table = emc2103_ids, |
| 685 | .detect = emc2103_detect, |
| 686 | .address_list = normal_i2c, |
| 687 | }; |
| 688 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 689 | module_i2c_driver(emc2103_driver); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 690 | |
Steve Glendinning | 90b24cf | 2012-04-16 12:13:29 +0100 | [diff] [blame] | 691 | MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>"); |
Steve Glendinning | 9df7305 | 2010-08-14 21:08:54 +0200 | [diff] [blame] | 692 | MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver"); |
| 693 | MODULE_LICENSE("GPL"); |