1 /* 2 * thmc50.c - Part of lm_sensors, Linux kernel modules for hardware 3 * monitoring 4 * Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl> 5 * Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and 6 * Philip Edelbrock <phil@netroedge.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/init.h> 25 #include <linux/slab.h> 26 #include <linux/i2c.h> 27 #include <linux/hwmon.h> 28 #include <linux/hwmon-sysfs.h> 29 #include <linux/err.h> 30 #include <linux/mutex.h> 31 #include <linux/jiffies.h> 32 33 MODULE_LICENSE("GPL"); 34 35 /* Addresses to scan */ 36 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; 37 38 /* Insmod parameters */ 39 enum chips { thmc50, adm1022 }; 40 41 static unsigned short adm1022_temp3[16]; 42 static unsigned int adm1022_temp3_num; 43 module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0); 44 MODULE_PARM_DESC(adm1022_temp3, 45 "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)"); 46 47 /* Many THMC50 constants specified below */ 48 49 /* The THMC50 registers */ 50 #define THMC50_REG_CONF 0x40 51 #define THMC50_REG_COMPANY_ID 0x3E 52 #define THMC50_REG_DIE_CODE 0x3F 53 #define THMC50_REG_ANALOG_OUT 0x19 54 /* 55 * The mirror status register cannot be used as 56 * reading it does not clear alarms. 57 */ 58 #define THMC50_REG_INTR 0x41 59 60 static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 }; 61 static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C }; 62 static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B }; 63 static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 }; 64 static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 }; 65 66 #define THMC50_REG_CONF_nFANOFF 0x20 67 #define THMC50_REG_CONF_PROGRAMMED 0x08 68 69 /* Each client has this additional data */ 70 struct thmc50_data { 71 struct device *hwmon_dev; 72 73 struct mutex update_lock; 74 enum chips type; 75 unsigned long last_updated; /* In jiffies */ 76 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */ 77 char valid; /* !=0 if following fields are valid */ 78 79 /* Register values */ 80 s8 temp_input[3]; 81 s8 temp_max[3]; 82 s8 temp_min[3]; 83 s8 temp_critical[3]; 84 u8 analog_out; 85 u8 alarms; 86 }; 87 88 static struct thmc50_data *thmc50_update_device(struct device *dev) 89 { 90 struct i2c_client *client = to_i2c_client(dev); 91 struct thmc50_data *data = i2c_get_clientdata(client); 92 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0); 93 94 mutex_lock(&data->update_lock); 95 96 if (time_after(jiffies, data->last_updated + timeout) 97 || !data->valid) { 98 99 int temps = data->has_temp3 ? 3 : 2; 100 int i; 101 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); 102 103 prog &= THMC50_REG_CONF_PROGRAMMED; 104 105 for (i = 0; i < temps; i++) { 106 data->temp_input[i] = i2c_smbus_read_byte_data(client, 107 THMC50_REG_TEMP[i]); 108 data->temp_max[i] = i2c_smbus_read_byte_data(client, 109 THMC50_REG_TEMP_MAX[i]); 110 data->temp_min[i] = i2c_smbus_read_byte_data(client, 111 THMC50_REG_TEMP_MIN[i]); 112 data->temp_critical[i] = 113 i2c_smbus_read_byte_data(client, 114 prog ? THMC50_REG_TEMP_CRITICAL[i] 115 : THMC50_REG_TEMP_DEFAULT[i]); 116 } 117 data->analog_out = 118 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT); 119 data->alarms = 120 i2c_smbus_read_byte_data(client, THMC50_REG_INTR); 121 data->last_updated = jiffies; 122 data->valid = 1; 123 } 124 125 mutex_unlock(&data->update_lock); 126 127 return data; 128 } 129 130 static ssize_t show_analog_out(struct device *dev, 131 struct device_attribute *attr, char *buf) 132 { 133 struct thmc50_data *data = thmc50_update_device(dev); 134 return sprintf(buf, "%d\n", data->analog_out); 135 } 136 137 static ssize_t set_analog_out(struct device *dev, 138 struct device_attribute *attr, 139 const char *buf, size_t count) 140 { 141 struct i2c_client *client = to_i2c_client(dev); 142 struct thmc50_data *data = i2c_get_clientdata(client); 143 int config; 144 unsigned long tmp; 145 int err; 146 147 err = kstrtoul(buf, 10, &tmp); 148 if (err) 149 return err; 150 151 mutex_lock(&data->update_lock); 152 data->analog_out = clamp_val(tmp, 0, 255); 153 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, 154 data->analog_out); 155 156 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); 157 if (data->analog_out == 0) 158 config &= ~THMC50_REG_CONF_nFANOFF; 159 else 160 config |= THMC50_REG_CONF_nFANOFF; 161 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); 162 163 mutex_unlock(&data->update_lock); 164 return count; 165 } 166 167 /* There is only one PWM mode = DC */ 168 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr, 169 char *buf) 170 { 171 return sprintf(buf, "0\n"); 172 } 173 174 /* Temperatures */ 175 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, 176 char *buf) 177 { 178 int nr = to_sensor_dev_attr(attr)->index; 179 struct thmc50_data *data = thmc50_update_device(dev); 180 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000); 181 } 182 183 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, 184 char *buf) 185 { 186 int nr = to_sensor_dev_attr(attr)->index; 187 struct thmc50_data *data = thmc50_update_device(dev); 188 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000); 189 } 190 191 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, 192 const char *buf, size_t count) 193 { 194 int nr = to_sensor_dev_attr(attr)->index; 195 struct i2c_client *client = to_i2c_client(dev); 196 struct thmc50_data *data = i2c_get_clientdata(client); 197 long val; 198 int err; 199 200 err = kstrtol(buf, 10, &val); 201 if (err) 202 return err; 203 204 mutex_lock(&data->update_lock); 205 data->temp_min[nr] = clamp_val(val / 1000, -128, 127); 206 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr], 207 data->temp_min[nr]); 208 mutex_unlock(&data->update_lock); 209 return count; 210 } 211 212 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, 213 char *buf) 214 { 215 int nr = to_sensor_dev_attr(attr)->index; 216 struct thmc50_data *data = thmc50_update_device(dev); 217 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000); 218 } 219 220 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, 221 const char *buf, size_t count) 222 { 223 int nr = to_sensor_dev_attr(attr)->index; 224 struct i2c_client *client = to_i2c_client(dev); 225 struct thmc50_data *data = i2c_get_clientdata(client); 226 long val; 227 int err; 228 229 err = kstrtol(buf, 10, &val); 230 if (err) 231 return err; 232 233 mutex_lock(&data->update_lock); 234 data->temp_max[nr] = clamp_val(val / 1000, -128, 127); 235 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr], 236 data->temp_max[nr]); 237 mutex_unlock(&data->update_lock); 238 return count; 239 } 240 241 static ssize_t show_temp_critical(struct device *dev, 242 struct device_attribute *attr, 243 char *buf) 244 { 245 int nr = to_sensor_dev_attr(attr)->index; 246 struct thmc50_data *data = thmc50_update_device(dev); 247 return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000); 248 } 249 250 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, 251 char *buf) 252 { 253 int index = to_sensor_dev_attr(attr)->index; 254 struct thmc50_data *data = thmc50_update_device(dev); 255 256 return sprintf(buf, "%u\n", (data->alarms >> index) & 1); 257 } 258 259 #define temp_reg(offset) \ 260 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \ 261 NULL, offset - 1); \ 262 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ 263 show_temp_min, set_temp_min, offset - 1); \ 264 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ 265 show_temp_max, set_temp_max, offset - 1); \ 266 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \ 267 show_temp_critical, NULL, offset - 1); 268 269 temp_reg(1); 270 temp_reg(2); 271 temp_reg(3); 272 273 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0); 274 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5); 275 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1); 276 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7); 277 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2); 278 279 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out, 280 set_analog_out, 0); 281 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0); 282 283 static struct attribute *thmc50_attributes[] = { 284 &sensor_dev_attr_temp1_max.dev_attr.attr, 285 &sensor_dev_attr_temp1_min.dev_attr.attr, 286 &sensor_dev_attr_temp1_input.dev_attr.attr, 287 &sensor_dev_attr_temp1_crit.dev_attr.attr, 288 &sensor_dev_attr_temp1_alarm.dev_attr.attr, 289 &sensor_dev_attr_temp2_max.dev_attr.attr, 290 &sensor_dev_attr_temp2_min.dev_attr.attr, 291 &sensor_dev_attr_temp2_input.dev_attr.attr, 292 &sensor_dev_attr_temp2_crit.dev_attr.attr, 293 &sensor_dev_attr_temp2_alarm.dev_attr.attr, 294 &sensor_dev_attr_temp2_fault.dev_attr.attr, 295 &sensor_dev_attr_pwm1.dev_attr.attr, 296 &sensor_dev_attr_pwm1_mode.dev_attr.attr, 297 NULL 298 }; 299 300 static const struct attribute_group thmc50_group = { 301 .attrs = thmc50_attributes, 302 }; 303 304 /* for ADM1022 3rd temperature mode */ 305 static struct attribute *temp3_attributes[] = { 306 &sensor_dev_attr_temp3_max.dev_attr.attr, 307 &sensor_dev_attr_temp3_min.dev_attr.attr, 308 &sensor_dev_attr_temp3_input.dev_attr.attr, 309 &sensor_dev_attr_temp3_crit.dev_attr.attr, 310 &sensor_dev_attr_temp3_alarm.dev_attr.attr, 311 &sensor_dev_attr_temp3_fault.dev_attr.attr, 312 NULL 313 }; 314 315 static const struct attribute_group temp3_group = { 316 .attrs = temp3_attributes, 317 }; 318 319 /* Return 0 if detection is successful, -ENODEV otherwise */ 320 static int thmc50_detect(struct i2c_client *client, 321 struct i2c_board_info *info) 322 { 323 unsigned company; 324 unsigned revision; 325 unsigned config; 326 struct i2c_adapter *adapter = client->adapter; 327 const char *type_name; 328 329 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 330 pr_debug("thmc50: detect failed, smbus byte data not supported!\n"); 331 return -ENODEV; 332 } 333 334 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", 335 client->addr, i2c_adapter_id(client->adapter)); 336 337 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID); 338 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE); 339 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); 340 if (revision < 0xc0 || (config & 0x10)) 341 return -ENODEV; 342 343 if (company == 0x41) { 344 int id = i2c_adapter_id(client->adapter); 345 int i; 346 347 type_name = "adm1022"; 348 for (i = 0; i + 1 < adm1022_temp3_num; i += 2) 349 if (adm1022_temp3[i] == id && 350 adm1022_temp3[i + 1] == client->addr) { 351 /* enable 2nd remote temp */ 352 config |= (1 << 7); 353 i2c_smbus_write_byte_data(client, 354 THMC50_REG_CONF, 355 config); 356 break; 357 } 358 } else if (company == 0x49) { 359 type_name = "thmc50"; 360 } else { 361 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); 362 return -ENODEV; 363 } 364 365 pr_debug("thmc50: Detected %s (version %x, revision %x)\n", 366 type_name, (revision >> 4) - 0xc, revision & 0xf); 367 368 strlcpy(info->type, type_name, I2C_NAME_SIZE); 369 370 return 0; 371 } 372 373 static void thmc50_init_client(struct i2c_client *client) 374 { 375 struct thmc50_data *data = i2c_get_clientdata(client); 376 int config; 377 378 data->analog_out = i2c_smbus_read_byte_data(client, 379 THMC50_REG_ANALOG_OUT); 380 /* set up to at least 1 */ 381 if (data->analog_out == 0) { 382 data->analog_out = 1; 383 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT, 384 data->analog_out); 385 } 386 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); 387 config |= 0x1; /* start the chip if it is in standby mode */ 388 if (data->type == adm1022 && (config & (1 << 7))) 389 data->has_temp3 = 1; 390 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); 391 } 392 393 static int thmc50_probe(struct i2c_client *client, 394 const struct i2c_device_id *id) 395 { 396 struct thmc50_data *data; 397 int err; 398 399 data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data), 400 GFP_KERNEL); 401 if (!data) 402 return -ENOMEM; 403 404 i2c_set_clientdata(client, data); 405 data->type = id->driver_data; 406 mutex_init(&data->update_lock); 407 408 thmc50_init_client(client); 409 410 /* Register sysfs hooks */ 411 err = sysfs_create_group(&client->dev.kobj, &thmc50_group); 412 if (err) 413 return err; 414 415 /* Register ADM1022 sysfs hooks */ 416 if (data->has_temp3) { 417 err = sysfs_create_group(&client->dev.kobj, &temp3_group); 418 if (err) 419 goto exit_remove_sysfs_thmc50; 420 } 421 422 /* Register a new directory entry with module sensors */ 423 data->hwmon_dev = hwmon_device_register(&client->dev); 424 if (IS_ERR(data->hwmon_dev)) { 425 err = PTR_ERR(data->hwmon_dev); 426 goto exit_remove_sysfs; 427 } 428 429 return 0; 430 431 exit_remove_sysfs: 432 if (data->has_temp3) 433 sysfs_remove_group(&client->dev.kobj, &temp3_group); 434 exit_remove_sysfs_thmc50: 435 sysfs_remove_group(&client->dev.kobj, &thmc50_group); 436 return err; 437 } 438 439 static int thmc50_remove(struct i2c_client *client) 440 { 441 struct thmc50_data *data = i2c_get_clientdata(client); 442 443 hwmon_device_unregister(data->hwmon_dev); 444 sysfs_remove_group(&client->dev.kobj, &thmc50_group); 445 if (data->has_temp3) 446 sysfs_remove_group(&client->dev.kobj, &temp3_group); 447 448 return 0; 449 } 450 451 static const struct i2c_device_id thmc50_id[] = { 452 { "adm1022", adm1022 }, 453 { "thmc50", thmc50 }, 454 { } 455 }; 456 MODULE_DEVICE_TABLE(i2c, thmc50_id); 457 458 static struct i2c_driver thmc50_driver = { 459 .class = I2C_CLASS_HWMON, 460 .driver = { 461 .name = "thmc50", 462 }, 463 .probe = thmc50_probe, 464 .remove = thmc50_remove, 465 .id_table = thmc50_id, 466 .detect = thmc50_detect, 467 .address_list = normal_i2c, 468 }; 469 470 module_i2c_driver(thmc50_driver); 471 472 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>"); 473 MODULE_DESCRIPTION("THMC50 driver"); 474