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