1 /* 2 * Copyright (c) 2012 Guenter Roeck <linux@roeck-us.net> 3 * 4 * based on max1668.c 5 * Copyright (c) 2011 David George <david.george@ska.ac.za> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/slab.h> 21 #include <linux/jiffies.h> 22 #include <linux/i2c.h> 23 #include <linux/hwmon.h> 24 #include <linux/hwmon-sysfs.h> 25 #include <linux/err.h> 26 #include <linux/mutex.h> 27 #include <linux/of.h> 28 29 #include <linux/platform_data/max6697.h> 30 31 enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694, 32 max6697, max6698, max6699 }; 33 34 /* Report local sensor as temp1 */ 35 36 static const u8 MAX6697_REG_TEMP[] = { 37 0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 }; 38 static const u8 MAX6697_REG_TEMP_EXT[] = { 39 0x57, 0x09, 0x52, 0x53, 0x54, 0x55, 0x56, 0 }; 40 static const u8 MAX6697_REG_MAX[] = { 41 0x17, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18 }; 42 static const u8 MAX6697_REG_CRIT[] = { 43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 }; 44 45 /* 46 * Map device tree / platform data register bit map to chip bit map. 47 * Applies to alert register and over-temperature register. 48 */ 49 #define MAX6697_MAP_BITS(reg) ((((reg) & 0x7e) >> 1) | \ 50 (((reg) & 0x01) << 6) | ((reg) & 0x80)) 51 52 #define MAX6697_REG_STAT(n) (0x44 + (n)) 53 54 #define MAX6697_REG_CONFIG 0x41 55 #define MAX6581_CONF_EXTENDED (1 << 1) 56 #define MAX6693_CONF_BETA (1 << 2) 57 #define MAX6697_CONF_RESISTANCE (1 << 3) 58 #define MAX6697_CONF_TIMEOUT (1 << 5) 59 #define MAX6697_REG_ALERT_MASK 0x42 60 #define MAX6697_REG_OVERT_MASK 0x43 61 62 #define MAX6581_REG_RESISTANCE 0x4a 63 #define MAX6581_REG_IDEALITY 0x4b 64 #define MAX6581_REG_IDEALITY_SELECT 0x4c 65 #define MAX6581_REG_OFFSET 0x4d 66 #define MAX6581_REG_OFFSET_SELECT 0x4e 67 68 #define MAX6697_CONV_TIME 156 /* ms per channel, worst case */ 69 70 struct max6697_chip_data { 71 int channels; 72 u32 have_ext; 73 u32 have_crit; 74 u32 have_fault; 75 u8 valid_conf; 76 const u8 *alarm_map; 77 }; 78 79 struct max6697_data { 80 struct device *hwmon_dev; 81 82 enum chips type; 83 const struct max6697_chip_data *chip; 84 85 int update_interval; /* in milli-seconds */ 86 int temp_offset; /* in degrees C */ 87 88 struct mutex update_lock; 89 unsigned long last_updated; /* In jiffies */ 90 bool valid; /* true if following fields are valid */ 91 92 /* 1x local and up to 7x remote */ 93 u8 temp[8][4]; /* [nr][0]=temp [1]=ext [2]=max [3]=crit */ 94 #define MAX6697_TEMP_INPUT 0 95 #define MAX6697_TEMP_EXT 1 96 #define MAX6697_TEMP_MAX 2 97 #define MAX6697_TEMP_CRIT 3 98 u32 alarms; 99 }; 100 101 /* Diode fault status bits on MAX6581 are right shifted by one bit */ 102 static const u8 max6581_alarm_map[] = { 103 0, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 104 16, 17, 18, 19, 20, 21, 22, 23 }; 105 106 static const struct max6697_chip_data max6697_chip_data[] = { 107 [max6581] = { 108 .channels = 8, 109 .have_crit = 0xff, 110 .have_ext = 0x7f, 111 .have_fault = 0xfe, 112 .valid_conf = MAX6581_CONF_EXTENDED | MAX6697_CONF_TIMEOUT, 113 .alarm_map = max6581_alarm_map, 114 }, 115 [max6602] = { 116 .channels = 5, 117 .have_crit = 0x12, 118 .have_ext = 0x02, 119 .have_fault = 0x1e, 120 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 121 }, 122 [max6622] = { 123 .channels = 5, 124 .have_crit = 0x12, 125 .have_ext = 0x02, 126 .have_fault = 0x1e, 127 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 128 }, 129 [max6636] = { 130 .channels = 7, 131 .have_crit = 0x72, 132 .have_ext = 0x02, 133 .have_fault = 0x7e, 134 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 135 }, 136 [max6689] = { 137 .channels = 7, 138 .have_crit = 0x72, 139 .have_ext = 0x02, 140 .have_fault = 0x7e, 141 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 142 }, 143 [max6693] = { 144 .channels = 7, 145 .have_crit = 0x72, 146 .have_ext = 0x02, 147 .have_fault = 0x7e, 148 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA | 149 MAX6697_CONF_TIMEOUT, 150 }, 151 [max6694] = { 152 .channels = 5, 153 .have_crit = 0x12, 154 .have_ext = 0x02, 155 .have_fault = 0x1e, 156 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA | 157 MAX6697_CONF_TIMEOUT, 158 }, 159 [max6697] = { 160 .channels = 7, 161 .have_crit = 0x72, 162 .have_ext = 0x02, 163 .have_fault = 0x7e, 164 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 165 }, 166 [max6698] = { 167 .channels = 7, 168 .have_crit = 0x72, 169 .have_ext = 0x02, 170 .have_fault = 0x0e, 171 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 172 }, 173 [max6699] = { 174 .channels = 5, 175 .have_crit = 0x12, 176 .have_ext = 0x02, 177 .have_fault = 0x1e, 178 .valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT, 179 }, 180 }; 181 182 static struct max6697_data *max6697_update_device(struct device *dev) 183 { 184 struct i2c_client *client = to_i2c_client(dev); 185 struct max6697_data *data = i2c_get_clientdata(client); 186 struct max6697_data *ret = data; 187 int val; 188 int i; 189 u32 alarms; 190 191 mutex_lock(&data->update_lock); 192 193 if (data->valid && 194 !time_after(jiffies, data->last_updated 195 + msecs_to_jiffies(data->update_interval))) 196 goto abort; 197 198 for (i = 0; i < data->chip->channels; i++) { 199 if (data->chip->have_ext & (1 << i)) { 200 val = i2c_smbus_read_byte_data(client, 201 MAX6697_REG_TEMP_EXT[i]); 202 if (unlikely(val < 0)) { 203 ret = ERR_PTR(val); 204 goto abort; 205 } 206 data->temp[i][MAX6697_TEMP_EXT] = val; 207 } 208 209 val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]); 210 if (unlikely(val < 0)) { 211 ret = ERR_PTR(val); 212 goto abort; 213 } 214 data->temp[i][MAX6697_TEMP_INPUT] = val; 215 216 val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]); 217 if (unlikely(val < 0)) { 218 ret = ERR_PTR(val); 219 goto abort; 220 } 221 data->temp[i][MAX6697_TEMP_MAX] = val; 222 223 if (data->chip->have_crit & (1 << i)) { 224 val = i2c_smbus_read_byte_data(client, 225 MAX6697_REG_CRIT[i]); 226 if (unlikely(val < 0)) { 227 ret = ERR_PTR(val); 228 goto abort; 229 } 230 data->temp[i][MAX6697_TEMP_CRIT] = val; 231 } 232 } 233 234 alarms = 0; 235 for (i = 0; i < 3; i++) { 236 val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i)); 237 if (unlikely(val < 0)) { 238 ret = ERR_PTR(val); 239 goto abort; 240 } 241 alarms = (alarms << 8) | val; 242 } 243 data->alarms = alarms; 244 data->last_updated = jiffies; 245 data->valid = true; 246 abort: 247 mutex_unlock(&data->update_lock); 248 249 return ret; 250 } 251 252 static ssize_t show_temp_input(struct device *dev, 253 struct device_attribute *devattr, char *buf) 254 { 255 int index = to_sensor_dev_attr(devattr)->index; 256 struct max6697_data *data = max6697_update_device(dev); 257 int temp; 258 259 if (IS_ERR(data)) 260 return PTR_ERR(data); 261 262 temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3; 263 temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5; 264 265 return sprintf(buf, "%d\n", temp * 125); 266 } 267 268 static ssize_t show_temp(struct device *dev, 269 struct device_attribute *devattr, char *buf) 270 { 271 int nr = to_sensor_dev_attr_2(devattr)->nr; 272 int index = to_sensor_dev_attr_2(devattr)->index; 273 struct max6697_data *data = max6697_update_device(dev); 274 int temp; 275 276 if (IS_ERR(data)) 277 return PTR_ERR(data); 278 279 temp = data->temp[nr][index]; 280 temp -= data->temp_offset; 281 282 return sprintf(buf, "%d\n", temp * 1000); 283 } 284 285 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 286 char *buf) 287 { 288 int index = to_sensor_dev_attr(attr)->index; 289 struct max6697_data *data = max6697_update_device(dev); 290 291 if (IS_ERR(data)) 292 return PTR_ERR(data); 293 294 if (data->chip->alarm_map) 295 index = data->chip->alarm_map[index]; 296 297 return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1); 298 } 299 300 static ssize_t set_temp(struct device *dev, 301 struct device_attribute *devattr, 302 const char *buf, size_t count) 303 { 304 int nr = to_sensor_dev_attr_2(devattr)->nr; 305 int index = to_sensor_dev_attr_2(devattr)->index; 306 struct i2c_client *client = to_i2c_client(dev); 307 struct max6697_data *data = i2c_get_clientdata(client); 308 long temp; 309 int ret; 310 311 ret = kstrtol(buf, 10, &temp); 312 if (ret < 0) 313 return ret; 314 315 mutex_lock(&data->update_lock); 316 temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset; 317 temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127); 318 data->temp[nr][index] = temp; 319 ret = i2c_smbus_write_byte_data(client, 320 index == 2 ? MAX6697_REG_MAX[nr] 321 : MAX6697_REG_CRIT[nr], 322 temp); 323 mutex_unlock(&data->update_lock); 324 325 return ret < 0 ? ret : count; 326 } 327 328 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0); 329 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 330 0, MAX6697_TEMP_MAX); 331 static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 332 0, MAX6697_TEMP_CRIT); 333 334 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1); 335 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 336 1, MAX6697_TEMP_MAX); 337 static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 338 1, MAX6697_TEMP_CRIT); 339 340 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2); 341 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 342 2, MAX6697_TEMP_MAX); 343 static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 344 2, MAX6697_TEMP_CRIT); 345 346 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3); 347 static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 348 3, MAX6697_TEMP_MAX); 349 static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 350 3, MAX6697_TEMP_CRIT); 351 352 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4); 353 static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 354 4, MAX6697_TEMP_MAX); 355 static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 356 4, MAX6697_TEMP_CRIT); 357 358 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5); 359 static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 360 5, MAX6697_TEMP_MAX); 361 static SENSOR_DEVICE_ATTR_2(temp6_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 362 5, MAX6697_TEMP_CRIT); 363 364 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp_input, NULL, 6); 365 static SENSOR_DEVICE_ATTR_2(temp7_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 366 6, MAX6697_TEMP_MAX); 367 static SENSOR_DEVICE_ATTR_2(temp7_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 368 6, MAX6697_TEMP_CRIT); 369 370 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp_input, NULL, 7); 371 static SENSOR_DEVICE_ATTR_2(temp8_max, S_IRUGO | S_IWUSR, show_temp, set_temp, 372 7, MAX6697_TEMP_MAX); 373 static SENSOR_DEVICE_ATTR_2(temp8_crit, S_IRUGO | S_IWUSR, show_temp, set_temp, 374 7, MAX6697_TEMP_CRIT); 375 376 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 22); 377 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 16); 378 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 17); 379 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 18); 380 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 19); 381 static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO, show_alarm, NULL, 20); 382 static SENSOR_DEVICE_ATTR(temp7_max_alarm, S_IRUGO, show_alarm, NULL, 21); 383 static SENSOR_DEVICE_ATTR(temp8_max_alarm, S_IRUGO, show_alarm, NULL, 23); 384 385 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 14); 386 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8); 387 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9); 388 static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 10); 389 static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO, show_alarm, NULL, 11); 390 static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO, show_alarm, NULL, 12); 391 static SENSOR_DEVICE_ATTR(temp7_crit_alarm, S_IRUGO, show_alarm, NULL, 13); 392 static SENSOR_DEVICE_ATTR(temp8_crit_alarm, S_IRUGO, show_alarm, NULL, 15); 393 394 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 1); 395 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); 396 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 3); 397 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL, 4); 398 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_alarm, NULL, 5); 399 static SENSOR_DEVICE_ATTR(temp7_fault, S_IRUGO, show_alarm, NULL, 6); 400 static SENSOR_DEVICE_ATTR(temp8_fault, S_IRUGO, show_alarm, NULL, 7); 401 402 static DEVICE_ATTR(dummy, 0, NULL, NULL); 403 404 static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr, 405 int index) 406 { 407 struct device *dev = container_of(kobj, struct device, kobj); 408 struct i2c_client *client = to_i2c_client(dev); 409 struct max6697_data *data = i2c_get_clientdata(client); 410 const struct max6697_chip_data *chip = data->chip; 411 int channel = index / 6; /* channel number */ 412 int nr = index % 6; /* attribute index within channel */ 413 414 if (channel >= chip->channels) 415 return 0; 416 417 if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel))) 418 return 0; 419 if (nr == 5 && !(chip->have_fault & (1 << channel))) 420 return 0; 421 422 return attr->mode; 423 } 424 425 /* 426 * max6697_is_visible uses the index into the following array to determine 427 * if attributes should be created or not. Any change in order or content 428 * must be matched in max6697_is_visible. 429 */ 430 static struct attribute *max6697_attributes[] = { 431 &sensor_dev_attr_temp1_input.dev_attr.attr, 432 &sensor_dev_attr_temp1_max.dev_attr.attr, 433 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 434 &sensor_dev_attr_temp1_crit.dev_attr.attr, 435 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 436 &dev_attr_dummy.attr, 437 438 &sensor_dev_attr_temp2_input.dev_attr.attr, 439 &sensor_dev_attr_temp2_max.dev_attr.attr, 440 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 441 &sensor_dev_attr_temp2_crit.dev_attr.attr, 442 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 443 &sensor_dev_attr_temp2_fault.dev_attr.attr, 444 445 &sensor_dev_attr_temp3_input.dev_attr.attr, 446 &sensor_dev_attr_temp3_max.dev_attr.attr, 447 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, 448 &sensor_dev_attr_temp3_crit.dev_attr.attr, 449 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, 450 &sensor_dev_attr_temp3_fault.dev_attr.attr, 451 452 &sensor_dev_attr_temp4_input.dev_attr.attr, 453 &sensor_dev_attr_temp4_max.dev_attr.attr, 454 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, 455 &sensor_dev_attr_temp4_crit.dev_attr.attr, 456 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr, 457 &sensor_dev_attr_temp4_fault.dev_attr.attr, 458 459 &sensor_dev_attr_temp5_input.dev_attr.attr, 460 &sensor_dev_attr_temp5_max.dev_attr.attr, 461 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, 462 &sensor_dev_attr_temp5_crit.dev_attr.attr, 463 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr, 464 &sensor_dev_attr_temp5_fault.dev_attr.attr, 465 466 &sensor_dev_attr_temp6_input.dev_attr.attr, 467 &sensor_dev_attr_temp6_max.dev_attr.attr, 468 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr, 469 &sensor_dev_attr_temp6_crit.dev_attr.attr, 470 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr, 471 &sensor_dev_attr_temp6_fault.dev_attr.attr, 472 473 &sensor_dev_attr_temp7_input.dev_attr.attr, 474 &sensor_dev_attr_temp7_max.dev_attr.attr, 475 &sensor_dev_attr_temp7_max_alarm.dev_attr.attr, 476 &sensor_dev_attr_temp7_crit.dev_attr.attr, 477 &sensor_dev_attr_temp7_crit_alarm.dev_attr.attr, 478 &sensor_dev_attr_temp7_fault.dev_attr.attr, 479 480 &sensor_dev_attr_temp8_input.dev_attr.attr, 481 &sensor_dev_attr_temp8_max.dev_attr.attr, 482 &sensor_dev_attr_temp8_max_alarm.dev_attr.attr, 483 &sensor_dev_attr_temp8_crit.dev_attr.attr, 484 &sensor_dev_attr_temp8_crit_alarm.dev_attr.attr, 485 &sensor_dev_attr_temp8_fault.dev_attr.attr, 486 NULL 487 }; 488 489 static const struct attribute_group max6697_group = { 490 .attrs = max6697_attributes, .is_visible = max6697_is_visible, 491 }; 492 493 static void max6697_get_config_of(struct device_node *node, 494 struct max6697_platform_data *pdata) 495 { 496 int len; 497 const __be32 *prop; 498 499 prop = of_get_property(node, "smbus-timeout-disable", &len); 500 if (prop) 501 pdata->smbus_timeout_disable = true; 502 prop = of_get_property(node, "extended-range-enable", &len); 503 if (prop) 504 pdata->extended_range_enable = true; 505 prop = of_get_property(node, "beta-compensation-enable", &len); 506 if (prop) 507 pdata->beta_compensation = true; 508 prop = of_get_property(node, "alert-mask", &len); 509 if (prop && len == sizeof(u32)) 510 pdata->alert_mask = be32_to_cpu(prop[0]); 511 prop = of_get_property(node, "over-temperature-mask", &len); 512 if (prop && len == sizeof(u32)) 513 pdata->over_temperature_mask = be32_to_cpu(prop[0]); 514 prop = of_get_property(node, "resistance-cancellation", &len); 515 if (prop) { 516 if (len == sizeof(u32)) 517 pdata->resistance_cancellation = be32_to_cpu(prop[0]); 518 else 519 pdata->resistance_cancellation = 0xfe; 520 } 521 prop = of_get_property(node, "transistor-ideality", &len); 522 if (prop && len == 2 * sizeof(u32)) { 523 pdata->ideality_mask = be32_to_cpu(prop[0]); 524 pdata->ideality_value = be32_to_cpu(prop[1]); 525 } 526 } 527 528 static int max6697_init_chip(struct i2c_client *client) 529 { 530 struct max6697_data *data = i2c_get_clientdata(client); 531 struct max6697_platform_data *pdata = dev_get_platdata(&client->dev); 532 struct max6697_platform_data p; 533 const struct max6697_chip_data *chip = data->chip; 534 int factor = chip->channels; 535 int ret, reg; 536 537 /* 538 * Don't touch configuration if neither platform data nor OF 539 * configuration was specified. If that is the case, use the 540 * current chip configuration. 541 */ 542 if (!pdata && !client->dev.of_node) { 543 reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG); 544 if (reg < 0) 545 return reg; 546 if (data->type == max6581) { 547 if (reg & MAX6581_CONF_EXTENDED) 548 data->temp_offset = 64; 549 reg = i2c_smbus_read_byte_data(client, 550 MAX6581_REG_RESISTANCE); 551 if (reg < 0) 552 return reg; 553 factor += hweight8(reg); 554 } else { 555 if (reg & MAX6697_CONF_RESISTANCE) 556 factor++; 557 } 558 goto done; 559 } 560 561 if (client->dev.of_node) { 562 memset(&p, 0, sizeof(p)); 563 max6697_get_config_of(client->dev.of_node, &p); 564 pdata = &p; 565 } 566 567 reg = 0; 568 if (pdata->smbus_timeout_disable && 569 (chip->valid_conf & MAX6697_CONF_TIMEOUT)) { 570 reg |= MAX6697_CONF_TIMEOUT; 571 } 572 if (pdata->extended_range_enable && 573 (chip->valid_conf & MAX6581_CONF_EXTENDED)) { 574 reg |= MAX6581_CONF_EXTENDED; 575 data->temp_offset = 64; 576 } 577 if (pdata->resistance_cancellation && 578 (chip->valid_conf & MAX6697_CONF_RESISTANCE)) { 579 reg |= MAX6697_CONF_RESISTANCE; 580 factor++; 581 } 582 if (pdata->beta_compensation && 583 (chip->valid_conf & MAX6693_CONF_BETA)) { 584 reg |= MAX6693_CONF_BETA; 585 } 586 587 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg); 588 if (ret < 0) 589 return ret; 590 591 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK, 592 MAX6697_MAP_BITS(pdata->alert_mask)); 593 if (ret < 0) 594 return ret; 595 596 ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK, 597 MAX6697_MAP_BITS(pdata->over_temperature_mask)); 598 if (ret < 0) 599 return ret; 600 601 if (data->type == max6581) { 602 factor += hweight8(pdata->resistance_cancellation >> 1); 603 ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE, 604 pdata->resistance_cancellation >> 1); 605 if (ret < 0) 606 return ret; 607 ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY, 608 pdata->ideality_value); 609 if (ret < 0) 610 return ret; 611 ret = i2c_smbus_write_byte_data(client, 612 MAX6581_REG_IDEALITY_SELECT, 613 pdata->ideality_mask >> 1); 614 if (ret < 0) 615 return ret; 616 } 617 done: 618 data->update_interval = factor * MAX6697_CONV_TIME; 619 return 0; 620 } 621 622 static int max6697_probe(struct i2c_client *client, 623 const struct i2c_device_id *id) 624 { 625 struct i2c_adapter *adapter = client->adapter; 626 struct device *dev = &client->dev; 627 struct max6697_data *data; 628 int err; 629 630 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 631 return -ENODEV; 632 633 data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL); 634 if (!data) 635 return -ENOMEM; 636 637 data->type = id->driver_data; 638 data->chip = &max6697_chip_data[data->type]; 639 640 i2c_set_clientdata(client, data); 641 mutex_init(&data->update_lock); 642 643 err = max6697_init_chip(client); 644 if (err) 645 return err; 646 647 err = sysfs_create_group(&client->dev.kobj, &max6697_group); 648 if (err) 649 return err; 650 651 data->hwmon_dev = hwmon_device_register(dev); 652 if (IS_ERR(data->hwmon_dev)) { 653 err = PTR_ERR(data->hwmon_dev); 654 goto error; 655 } 656 657 return 0; 658 659 error: 660 sysfs_remove_group(&client->dev.kobj, &max6697_group); 661 return err; 662 } 663 664 static int max6697_remove(struct i2c_client *client) 665 { 666 struct max6697_data *data = i2c_get_clientdata(client); 667 668 hwmon_device_unregister(data->hwmon_dev); 669 sysfs_remove_group(&client->dev.kobj, &max6697_group); 670 671 return 0; 672 } 673 674 static const struct i2c_device_id max6697_id[] = { 675 { "max6581", max6581 }, 676 { "max6602", max6602 }, 677 { "max6622", max6622 }, 678 { "max6636", max6636 }, 679 { "max6689", max6689 }, 680 { "max6693", max6693 }, 681 { "max6694", max6694 }, 682 { "max6697", max6697 }, 683 { "max6698", max6698 }, 684 { "max6699", max6699 }, 685 { } 686 }; 687 MODULE_DEVICE_TABLE(i2c, max6697_id); 688 689 static struct i2c_driver max6697_driver = { 690 .class = I2C_CLASS_HWMON, 691 .driver = { 692 .name = "max6697", 693 }, 694 .probe = max6697_probe, 695 .remove = max6697_remove, 696 .id_table = max6697_id, 697 }; 698 699 module_i2c_driver(max6697_driver); 700 701 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 702 MODULE_DESCRIPTION("MAX6697 temperature sensor driver"); 703 MODULE_LICENSE("GPL"); 704