1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2011 David George <david.george@ska.ac.za> 4 * 5 * based on adm1021.c 6 * some credit to Christoph Scheurer, but largely a rewrite 7 */ 8 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/slab.h> 12 #include <linux/jiffies.h> 13 #include <linux/i2c.h> 14 #include <linux/hwmon.h> 15 #include <linux/hwmon-sysfs.h> 16 #include <linux/err.h> 17 #include <linux/mutex.h> 18 19 /* Addresses to scan */ 20 static const unsigned short max1668_addr_list[] = { 21 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; 22 23 /* max1668 registers */ 24 25 #define MAX1668_REG_TEMP(nr) (nr) 26 #define MAX1668_REG_STAT1 0x05 27 #define MAX1668_REG_STAT2 0x06 28 #define MAX1668_REG_MAN_ID 0xfe 29 #define MAX1668_REG_DEV_ID 0xff 30 31 /* limits */ 32 33 /* write high limits */ 34 #define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr)) 35 /* write low limits */ 36 #define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr)) 37 /* read high limits */ 38 #define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr)) 39 /* read low limits */ 40 #define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr)) 41 42 /* manufacturer and device ID Constants */ 43 #define MAN_ID_MAXIM 0x4d 44 #define DEV_ID_MAX1668 0x3 45 #define DEV_ID_MAX1805 0x5 46 #define DEV_ID_MAX1989 0xb 47 48 /* read only mode module parameter */ 49 static bool read_only; 50 module_param(read_only, bool, 0); 51 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); 52 53 enum chips { max1668, max1805, max1989 }; 54 55 struct max1668_data { 56 struct i2c_client *client; 57 const struct attribute_group *groups[3]; 58 enum chips type; 59 60 struct mutex update_lock; 61 bool valid; /* true if following fields are valid */ 62 unsigned long last_updated; /* In jiffies */ 63 64 /* 1x local and 4x remote */ 65 s8 temp_max[5]; 66 s8 temp_min[5]; 67 s8 temp[5]; 68 u16 alarms; 69 }; 70 71 static struct max1668_data *max1668_update_device(struct device *dev) 72 { 73 struct max1668_data *data = dev_get_drvdata(dev); 74 struct i2c_client *client = data->client; 75 struct max1668_data *ret = data; 76 s32 val; 77 int i; 78 79 mutex_lock(&data->update_lock); 80 81 if (data->valid && !time_after(jiffies, 82 data->last_updated + HZ + HZ / 2)) 83 goto abort; 84 85 for (i = 0; i < 5; i++) { 86 val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i)); 87 if (unlikely(val < 0)) { 88 ret = ERR_PTR(val); 89 goto abort; 90 } 91 data->temp[i] = (s8) val; 92 93 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i)); 94 if (unlikely(val < 0)) { 95 ret = ERR_PTR(val); 96 goto abort; 97 } 98 data->temp_max[i] = (s8) val; 99 100 val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i)); 101 if (unlikely(val < 0)) { 102 ret = ERR_PTR(val); 103 goto abort; 104 } 105 data->temp_min[i] = (s8) val; 106 } 107 108 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1); 109 if (unlikely(val < 0)) { 110 ret = ERR_PTR(val); 111 goto abort; 112 } 113 data->alarms = val << 8; 114 115 val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2); 116 if (unlikely(val < 0)) { 117 ret = ERR_PTR(val); 118 goto abort; 119 } 120 data->alarms |= val; 121 122 data->last_updated = jiffies; 123 data->valid = true; 124 abort: 125 mutex_unlock(&data->update_lock); 126 127 return ret; 128 } 129 130 static ssize_t show_temp(struct device *dev, 131 struct device_attribute *devattr, char *buf) 132 { 133 int index = to_sensor_dev_attr(devattr)->index; 134 struct max1668_data *data = max1668_update_device(dev); 135 136 if (IS_ERR(data)) 137 return PTR_ERR(data); 138 139 return sprintf(buf, "%d\n", data->temp[index] * 1000); 140 } 141 142 static ssize_t show_temp_max(struct device *dev, 143 struct device_attribute *devattr, char *buf) 144 { 145 int index = to_sensor_dev_attr(devattr)->index; 146 struct max1668_data *data = max1668_update_device(dev); 147 148 if (IS_ERR(data)) 149 return PTR_ERR(data); 150 151 return sprintf(buf, "%d\n", data->temp_max[index] * 1000); 152 } 153 154 static ssize_t show_temp_min(struct device *dev, 155 struct device_attribute *devattr, char *buf) 156 { 157 int index = to_sensor_dev_attr(devattr)->index; 158 struct max1668_data *data = max1668_update_device(dev); 159 160 if (IS_ERR(data)) 161 return PTR_ERR(data); 162 163 return sprintf(buf, "%d\n", data->temp_min[index] * 1000); 164 } 165 166 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 167 char *buf) 168 { 169 int index = to_sensor_dev_attr(attr)->index; 170 struct max1668_data *data = max1668_update_device(dev); 171 172 if (IS_ERR(data)) 173 return PTR_ERR(data); 174 175 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1); 176 } 177 178 static ssize_t show_fault(struct device *dev, 179 struct device_attribute *devattr, char *buf) 180 { 181 int index = to_sensor_dev_attr(devattr)->index; 182 struct max1668_data *data = max1668_update_device(dev); 183 184 if (IS_ERR(data)) 185 return PTR_ERR(data); 186 187 return sprintf(buf, "%u\n", 188 (data->alarms & (1 << 12)) && data->temp[index] == 127); 189 } 190 191 static ssize_t set_temp_max(struct device *dev, 192 struct device_attribute *devattr, 193 const char *buf, size_t count) 194 { 195 int index = to_sensor_dev_attr(devattr)->index; 196 struct max1668_data *data = dev_get_drvdata(dev); 197 struct i2c_client *client = data->client; 198 long temp; 199 int ret; 200 201 ret = kstrtol(buf, 10, &temp); 202 if (ret < 0) 203 return ret; 204 205 mutex_lock(&data->update_lock); 206 data->temp_max[index] = clamp_val(temp/1000, -128, 127); 207 ret = i2c_smbus_write_byte_data(client, 208 MAX1668_REG_LIMH_WR(index), 209 data->temp_max[index]); 210 if (ret < 0) 211 count = ret; 212 mutex_unlock(&data->update_lock); 213 214 return count; 215 } 216 217 static ssize_t set_temp_min(struct device *dev, 218 struct device_attribute *devattr, 219 const char *buf, size_t count) 220 { 221 int index = to_sensor_dev_attr(devattr)->index; 222 struct max1668_data *data = dev_get_drvdata(dev); 223 struct i2c_client *client = data->client; 224 long temp; 225 int ret; 226 227 ret = kstrtol(buf, 10, &temp); 228 if (ret < 0) 229 return ret; 230 231 mutex_lock(&data->update_lock); 232 data->temp_min[index] = clamp_val(temp/1000, -128, 127); 233 ret = i2c_smbus_write_byte_data(client, 234 MAX1668_REG_LIML_WR(index), 235 data->temp_min[index]); 236 if (ret < 0) 237 count = ret; 238 mutex_unlock(&data->update_lock); 239 240 return count; 241 } 242 243 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); 244 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, 245 set_temp_max, 0); 246 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, 247 set_temp_min, 0); 248 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); 249 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, 250 set_temp_max, 1); 251 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, 252 set_temp_min, 1); 253 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); 254 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, 255 set_temp_max, 2); 256 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, 257 set_temp_min, 2); 258 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); 259 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, 260 set_temp_max, 3); 261 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, 262 set_temp_min, 3); 263 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); 264 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, 265 set_temp_max, 4); 266 static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, 267 set_temp_min, 4); 268 269 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14); 270 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13); 271 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7); 272 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6); 273 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5); 274 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); 275 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3); 276 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2); 277 static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1); 278 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0); 279 280 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_fault, NULL, 1); 281 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_fault, NULL, 2); 282 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_fault, NULL, 3); 283 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_fault, NULL, 4); 284 285 /* Attributes common to MAX1668, MAX1989 and MAX1805 */ 286 static struct attribute *max1668_attribute_common[] = { 287 &sensor_dev_attr_temp1_max.dev_attr.attr, 288 &sensor_dev_attr_temp1_min.dev_attr.attr, 289 &sensor_dev_attr_temp1_input.dev_attr.attr, 290 &sensor_dev_attr_temp2_max.dev_attr.attr, 291 &sensor_dev_attr_temp2_min.dev_attr.attr, 292 &sensor_dev_attr_temp2_input.dev_attr.attr, 293 &sensor_dev_attr_temp3_max.dev_attr.attr, 294 &sensor_dev_attr_temp3_min.dev_attr.attr, 295 &sensor_dev_attr_temp3_input.dev_attr.attr, 296 297 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 298 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, 299 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 300 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, 301 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 302 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, 303 304 &sensor_dev_attr_temp2_fault.dev_attr.attr, 305 &sensor_dev_attr_temp3_fault.dev_attr.attr, 306 NULL 307 }; 308 309 /* Attributes not present on MAX1805 */ 310 static struct attribute *max1668_attribute_unique[] = { 311 &sensor_dev_attr_temp4_max.dev_attr.attr, 312 &sensor_dev_attr_temp4_min.dev_attr.attr, 313 &sensor_dev_attr_temp4_input.dev_attr.attr, 314 &sensor_dev_attr_temp5_max.dev_attr.attr, 315 &sensor_dev_attr_temp5_min.dev_attr.attr, 316 &sensor_dev_attr_temp5_input.dev_attr.attr, 317 318 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, 319 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, 320 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, 321 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, 322 323 &sensor_dev_attr_temp4_fault.dev_attr.attr, 324 &sensor_dev_attr_temp5_fault.dev_attr.attr, 325 NULL 326 }; 327 328 static umode_t max1668_attribute_mode(struct kobject *kobj, 329 struct attribute *attr, int index) 330 { 331 umode_t ret = S_IRUGO; 332 if (read_only) 333 return ret; 334 if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr || 335 attr == &sensor_dev_attr_temp2_max.dev_attr.attr || 336 attr == &sensor_dev_attr_temp3_max.dev_attr.attr || 337 attr == &sensor_dev_attr_temp4_max.dev_attr.attr || 338 attr == &sensor_dev_attr_temp5_max.dev_attr.attr || 339 attr == &sensor_dev_attr_temp1_min.dev_attr.attr || 340 attr == &sensor_dev_attr_temp2_min.dev_attr.attr || 341 attr == &sensor_dev_attr_temp3_min.dev_attr.attr || 342 attr == &sensor_dev_attr_temp4_min.dev_attr.attr || 343 attr == &sensor_dev_attr_temp5_min.dev_attr.attr) 344 ret |= S_IWUSR; 345 return ret; 346 } 347 348 static const struct attribute_group max1668_group_common = { 349 .attrs = max1668_attribute_common, 350 .is_visible = max1668_attribute_mode 351 }; 352 353 static const struct attribute_group max1668_group_unique = { 354 .attrs = max1668_attribute_unique, 355 .is_visible = max1668_attribute_mode 356 }; 357 358 /* Return 0 if detection is successful, -ENODEV otherwise */ 359 static int max1668_detect(struct i2c_client *client, 360 struct i2c_board_info *info) 361 { 362 struct i2c_adapter *adapter = client->adapter; 363 const char *type_name; 364 int man_id, dev_id; 365 366 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 367 return -ENODEV; 368 369 /* Check for unsupported part */ 370 man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID); 371 if (man_id != MAN_ID_MAXIM) 372 return -ENODEV; 373 374 dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID); 375 if (dev_id < 0) 376 return -ENODEV; 377 378 type_name = NULL; 379 if (dev_id == DEV_ID_MAX1668) 380 type_name = "max1668"; 381 else if (dev_id == DEV_ID_MAX1805) 382 type_name = "max1805"; 383 else if (dev_id == DEV_ID_MAX1989) 384 type_name = "max1989"; 385 386 if (!type_name) 387 return -ENODEV; 388 389 strscpy(info->type, type_name, I2C_NAME_SIZE); 390 391 return 0; 392 } 393 394 static const struct i2c_device_id max1668_id[]; 395 396 static int max1668_probe(struct i2c_client *client) 397 { 398 struct i2c_adapter *adapter = client->adapter; 399 struct device *dev = &client->dev; 400 struct device *hwmon_dev; 401 struct max1668_data *data; 402 403 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 404 return -ENODEV; 405 406 data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL); 407 if (!data) 408 return -ENOMEM; 409 410 data->client = client; 411 data->type = i2c_match_id(max1668_id, client)->driver_data; 412 mutex_init(&data->update_lock); 413 414 /* sysfs hooks */ 415 data->groups[0] = &max1668_group_common; 416 if (data->type == max1668 || data->type == max1989) 417 data->groups[1] = &max1668_group_unique; 418 419 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 420 data, data->groups); 421 return PTR_ERR_OR_ZERO(hwmon_dev); 422 } 423 424 static const struct i2c_device_id max1668_id[] = { 425 { "max1668", max1668 }, 426 { "max1805", max1805 }, 427 { "max1989", max1989 }, 428 { } 429 }; 430 MODULE_DEVICE_TABLE(i2c, max1668_id); 431 432 /* This is the driver that will be inserted */ 433 static struct i2c_driver max1668_driver = { 434 .class = I2C_CLASS_HWMON, 435 .driver = { 436 .name = "max1668", 437 }, 438 .probe = max1668_probe, 439 .id_table = max1668_id, 440 .detect = max1668_detect, 441 .address_list = max1668_addr_list, 442 }; 443 444 module_i2c_driver(max1668_driver); 445 446 MODULE_AUTHOR("David George <david.george@ska.ac.za>"); 447 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver"); 448 MODULE_LICENSE("GPL"); 449