1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Texas Instruments TMP108 SMBus temperature sensor driver 3 * 4 * Copyright (C) 2016 John Muir <john@jmuir.com> 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/hwmon.h> 11 #include <linux/hwmon-sysfs.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/of.h> 15 #include <linux/i2c.h> 16 #include <linux/i3c/device.h> 17 #include <linux/init.h> 18 #include <linux/jiffies.h> 19 #include <linux/regmap.h> 20 #include <linux/slab.h> 21 22 #define DRIVER_NAME "tmp108" 23 24 #define TMP108_REG_TEMP 0x00 25 #define TMP108_REG_CONF 0x01 26 #define TMP108_REG_TLOW 0x02 27 #define TMP108_REG_THIGH 0x03 28 29 #define TMP108_TEMP_MIN_MC -50000 /* Minimum millicelcius. */ 30 #define TMP108_TEMP_MAX_MC 127937 /* Maximum millicelcius. */ 31 32 /* Configuration register bits. 33 * Note: these bit definitions are byte swapped. 34 */ 35 #define TMP108_CONF_M0 0x0100 /* Sensor mode. */ 36 #define TMP108_CONF_M1 0x0200 37 #define TMP108_CONF_TM 0x0400 /* Thermostat mode. */ 38 #define TMP108_CONF_FL 0x0800 /* Watchdog flag - TLOW */ 39 #define TMP108_CONF_FH 0x1000 /* Watchdog flag - THIGH */ 40 #define TMP108_CONF_CR0 0x2000 /* Conversion rate. */ 41 #define TMP108_CONF_CR1 0x4000 42 #define TMP108_CONF_ID 0x8000 43 #define TMP108_CONF_HYS0 0x0010 /* Hysteresis. */ 44 #define TMP108_CONF_HYS1 0x0020 45 #define TMP108_CONF_POL 0x0080 /* Polarity of alert. */ 46 47 /* Defaults set by the hardware upon reset. */ 48 #define TMP108_CONF_DEFAULTS (TMP108_CONF_CR0 | TMP108_CONF_TM |\ 49 TMP108_CONF_HYS0 | TMP108_CONF_M1) 50 /* These bits are read-only. */ 51 #define TMP108_CONF_READ_ONLY (TMP108_CONF_FL | TMP108_CONF_FH |\ 52 TMP108_CONF_ID) 53 54 #define TMP108_CONF_MODE_MASK (TMP108_CONF_M0|TMP108_CONF_M1) 55 #define TMP108_MODE_SHUTDOWN 0x0000 56 #define TMP108_MODE_ONE_SHOT TMP108_CONF_M0 57 #define TMP108_MODE_CONTINUOUS TMP108_CONF_M1 /* Default */ 58 /* When M1 is set, M0 is ignored. */ 59 60 #define TMP108_CONF_CONVRATE_MASK (TMP108_CONF_CR0|TMP108_CONF_CR1) 61 #define TMP108_CONVRATE_0P25HZ 0x0000 62 #define TMP108_CONVRATE_1HZ TMP108_CONF_CR0 /* Default */ 63 #define TMP108_CONVRATE_4HZ TMP108_CONF_CR1 64 #define TMP108_CONVRATE_16HZ (TMP108_CONF_CR0|TMP108_CONF_CR1) 65 66 #define TMP108_CONF_HYSTERESIS_MASK (TMP108_CONF_HYS0|TMP108_CONF_HYS1) 67 #define TMP108_HYSTERESIS_0C 0x0000 68 #define TMP108_HYSTERESIS_1C TMP108_CONF_HYS0 /* Default */ 69 #define TMP108_HYSTERESIS_2C TMP108_CONF_HYS1 70 #define TMP108_HYSTERESIS_4C (TMP108_CONF_HYS0|TMP108_CONF_HYS1) 71 72 #define TMP108_CONVERSION_TIME_MS 30 /* in milli-seconds */ 73 74 struct tmp108 { 75 struct regmap *regmap; 76 u16 orig_config; 77 unsigned long ready_time; 78 }; 79 80 /* convert 12-bit TMP108 register value to milliCelsius */ 81 static inline int tmp108_temp_reg_to_mC(s16 val) 82 { 83 return (val & ~0x0f) * 1000 / 256; 84 } 85 86 /* convert milliCelsius to left adjusted 12-bit TMP108 register value */ 87 static inline u16 tmp108_mC_to_temp_reg(int val) 88 { 89 return (val * 256) / 1000; 90 } 91 92 static int tmp108_read(struct device *dev, enum hwmon_sensor_types type, 93 u32 attr, int channel, long *temp) 94 { 95 struct tmp108 *tmp108 = dev_get_drvdata(dev); 96 unsigned int regval; 97 int err, hyst; 98 99 if (type == hwmon_chip) { 100 if (attr == hwmon_chip_update_interval) { 101 err = regmap_read(tmp108->regmap, TMP108_REG_CONF, 102 ®val); 103 if (err < 0) 104 return err; 105 switch (regval & TMP108_CONF_CONVRATE_MASK) { 106 case TMP108_CONVRATE_0P25HZ: 107 default: 108 *temp = 4000; 109 break; 110 case TMP108_CONVRATE_1HZ: 111 *temp = 1000; 112 break; 113 case TMP108_CONVRATE_4HZ: 114 *temp = 250; 115 break; 116 case TMP108_CONVRATE_16HZ: 117 *temp = 63; 118 break; 119 } 120 return 0; 121 } 122 return -EOPNOTSUPP; 123 } 124 125 switch (attr) { 126 case hwmon_temp_input: 127 /* Is it too early to return a conversion ? */ 128 if (time_before(jiffies, tmp108->ready_time)) { 129 dev_dbg(dev, "%s: Conversion not ready yet..\n", 130 __func__); 131 return -EAGAIN; 132 } 133 err = regmap_read(tmp108->regmap, TMP108_REG_TEMP, ®val); 134 if (err < 0) 135 return err; 136 *temp = tmp108_temp_reg_to_mC(regval); 137 break; 138 case hwmon_temp_min: 139 case hwmon_temp_max: 140 err = regmap_read(tmp108->regmap, attr == hwmon_temp_min ? 141 TMP108_REG_TLOW : TMP108_REG_THIGH, ®val); 142 if (err < 0) 143 return err; 144 *temp = tmp108_temp_reg_to_mC(regval); 145 break; 146 case hwmon_temp_min_alarm: 147 case hwmon_temp_max_alarm: 148 err = regmap_read(tmp108->regmap, TMP108_REG_CONF, ®val); 149 if (err < 0) 150 return err; 151 *temp = !!(regval & (attr == hwmon_temp_min_alarm ? 152 TMP108_CONF_FL : TMP108_CONF_FH)); 153 break; 154 case hwmon_temp_min_hyst: 155 case hwmon_temp_max_hyst: 156 err = regmap_read(tmp108->regmap, TMP108_REG_CONF, ®val); 157 if (err < 0) 158 return err; 159 switch (regval & TMP108_CONF_HYSTERESIS_MASK) { 160 case TMP108_HYSTERESIS_0C: 161 default: 162 hyst = 0; 163 break; 164 case TMP108_HYSTERESIS_1C: 165 hyst = 1000; 166 break; 167 case TMP108_HYSTERESIS_2C: 168 hyst = 2000; 169 break; 170 case TMP108_HYSTERESIS_4C: 171 hyst = 4000; 172 break; 173 } 174 err = regmap_read(tmp108->regmap, attr == hwmon_temp_min_hyst ? 175 TMP108_REG_TLOW : TMP108_REG_THIGH, ®val); 176 if (err < 0) 177 return err; 178 *temp = tmp108_temp_reg_to_mC(regval); 179 if (attr == hwmon_temp_min_hyst) 180 *temp += hyst; 181 else 182 *temp -= hyst; 183 break; 184 default: 185 return -EOPNOTSUPP; 186 } 187 188 return 0; 189 } 190 191 static int tmp108_write(struct device *dev, enum hwmon_sensor_types type, 192 u32 attr, int channel, long temp) 193 { 194 struct tmp108 *tmp108 = dev_get_drvdata(dev); 195 u32 regval, mask; 196 int err; 197 198 if (type == hwmon_chip) { 199 if (attr == hwmon_chip_update_interval) { 200 if (temp < 156) 201 mask = TMP108_CONVRATE_16HZ; 202 else if (temp < 625) 203 mask = TMP108_CONVRATE_4HZ; 204 else if (temp < 2500) 205 mask = TMP108_CONVRATE_1HZ; 206 else 207 mask = TMP108_CONVRATE_0P25HZ; 208 return regmap_update_bits(tmp108->regmap, 209 TMP108_REG_CONF, 210 TMP108_CONF_CONVRATE_MASK, 211 mask); 212 } 213 return -EOPNOTSUPP; 214 } 215 216 switch (attr) { 217 case hwmon_temp_min: 218 case hwmon_temp_max: 219 temp = clamp_val(temp, TMP108_TEMP_MIN_MC, TMP108_TEMP_MAX_MC); 220 return regmap_write(tmp108->regmap, 221 attr == hwmon_temp_min ? 222 TMP108_REG_TLOW : TMP108_REG_THIGH, 223 tmp108_mC_to_temp_reg(temp)); 224 case hwmon_temp_min_hyst: 225 case hwmon_temp_max_hyst: 226 temp = clamp_val(temp, TMP108_TEMP_MIN_MC, TMP108_TEMP_MAX_MC); 227 err = regmap_read(tmp108->regmap, 228 attr == hwmon_temp_min_hyst ? 229 TMP108_REG_TLOW : TMP108_REG_THIGH, 230 ®val); 231 if (err < 0) 232 return err; 233 if (attr == hwmon_temp_min_hyst) 234 temp -= tmp108_temp_reg_to_mC(regval); 235 else 236 temp = tmp108_temp_reg_to_mC(regval) - temp; 237 if (temp < 500) 238 mask = TMP108_HYSTERESIS_0C; 239 else if (temp < 1500) 240 mask = TMP108_HYSTERESIS_1C; 241 else if (temp < 3000) 242 mask = TMP108_HYSTERESIS_2C; 243 else 244 mask = TMP108_HYSTERESIS_4C; 245 return regmap_update_bits(tmp108->regmap, TMP108_REG_CONF, 246 TMP108_CONF_HYSTERESIS_MASK, mask); 247 default: 248 return -EOPNOTSUPP; 249 } 250 } 251 252 static umode_t tmp108_is_visible(const void *data, enum hwmon_sensor_types type, 253 u32 attr, int channel) 254 { 255 if (type == hwmon_chip && attr == hwmon_chip_update_interval) 256 return 0644; 257 258 if (type != hwmon_temp) 259 return 0; 260 261 switch (attr) { 262 case hwmon_temp_input: 263 case hwmon_temp_min_alarm: 264 case hwmon_temp_max_alarm: 265 return 0444; 266 case hwmon_temp_min: 267 case hwmon_temp_max: 268 case hwmon_temp_min_hyst: 269 case hwmon_temp_max_hyst: 270 return 0644; 271 default: 272 return 0; 273 } 274 } 275 276 static const struct hwmon_channel_info * const tmp108_info[] = { 277 HWMON_CHANNEL_INFO(chip, 278 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL), 279 HWMON_CHANNEL_INFO(temp, 280 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN | 281 HWMON_T_MIN_HYST | HWMON_T_MAX_HYST | 282 HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM), 283 NULL 284 }; 285 286 static const struct hwmon_ops tmp108_hwmon_ops = { 287 .is_visible = tmp108_is_visible, 288 .read = tmp108_read, 289 .write = tmp108_write, 290 }; 291 292 static const struct hwmon_chip_info tmp108_chip_info = { 293 .ops = &tmp108_hwmon_ops, 294 .info = tmp108_info, 295 }; 296 297 static void tmp108_restore_config(void *data) 298 { 299 struct tmp108 *tmp108 = data; 300 301 regmap_write(tmp108->regmap, TMP108_REG_CONF, tmp108->orig_config); 302 } 303 304 static bool tmp108_is_writeable_reg(struct device *dev, unsigned int reg) 305 { 306 return reg != TMP108_REG_TEMP; 307 } 308 309 static bool tmp108_is_volatile_reg(struct device *dev, unsigned int reg) 310 { 311 /* Configuration register must be volatile to enable FL and FH. */ 312 return reg == TMP108_REG_TEMP || reg == TMP108_REG_CONF; 313 } 314 315 static const struct regmap_config tmp108_regmap_config = { 316 .reg_bits = 8, 317 .val_bits = 16, 318 .max_register = TMP108_REG_THIGH, 319 .writeable_reg = tmp108_is_writeable_reg, 320 .volatile_reg = tmp108_is_volatile_reg, 321 .val_format_endian = REGMAP_ENDIAN_BIG, 322 .cache_type = REGCACHE_MAPLE, 323 .use_single_read = true, 324 .use_single_write = true, 325 }; 326 327 static int tmp108_common_probe(struct device *dev, struct regmap *regmap, char *name) 328 { 329 struct device *hwmon_dev; 330 struct tmp108 *tmp108; 331 u32 config; 332 int err; 333 334 tmp108 = devm_kzalloc(dev, sizeof(*tmp108), GFP_KERNEL); 335 if (!tmp108) 336 return -ENOMEM; 337 338 dev_set_drvdata(dev, tmp108); 339 tmp108->regmap = regmap; 340 341 err = regmap_read(tmp108->regmap, TMP108_REG_CONF, &config); 342 if (err < 0) { 343 dev_err(dev, "error reading config register: %d", err); 344 return err; 345 } 346 tmp108->orig_config = config; 347 348 /* Only continuous mode is supported. */ 349 config &= ~TMP108_CONF_MODE_MASK; 350 config |= TMP108_MODE_CONTINUOUS; 351 352 /* Only comparator mode is supported. */ 353 config &= ~TMP108_CONF_TM; 354 355 err = regmap_write(tmp108->regmap, TMP108_REG_CONF, config); 356 if (err < 0) { 357 dev_err(dev, "error writing config register: %d", err); 358 return err; 359 } 360 361 tmp108->ready_time = jiffies; 362 if ((tmp108->orig_config & TMP108_CONF_MODE_MASK) == 363 TMP108_MODE_SHUTDOWN) 364 tmp108->ready_time += 365 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS); 366 367 err = devm_add_action_or_reset(dev, tmp108_restore_config, tmp108); 368 if (err) { 369 dev_err(dev, "add action or reset failed: %d", err); 370 return err; 371 } 372 373 hwmon_dev = devm_hwmon_device_register_with_info(dev, name, 374 tmp108, 375 &tmp108_chip_info, 376 NULL); 377 return PTR_ERR_OR_ZERO(hwmon_dev); 378 } 379 380 static int tmp108_probe(struct i2c_client *client) 381 { 382 struct device *dev = &client->dev; 383 struct regmap *regmap; 384 385 if (!i2c_check_functionality(client->adapter, 386 I2C_FUNC_SMBUS_WORD_DATA)) 387 return dev_err_probe(dev, -ENODEV, 388 "adapter doesn't support SMBus word transactions\n"); 389 390 regmap = devm_regmap_init_i2c(client, &tmp108_regmap_config); 391 if (IS_ERR(regmap)) 392 return dev_err_probe(dev, PTR_ERR(regmap), "regmap init failed"); 393 394 return tmp108_common_probe(dev, regmap, client->name); 395 } 396 397 static int tmp108_suspend(struct device *dev) 398 { 399 struct tmp108 *tmp108 = dev_get_drvdata(dev); 400 401 return regmap_update_bits(tmp108->regmap, TMP108_REG_CONF, 402 TMP108_CONF_MODE_MASK, TMP108_MODE_SHUTDOWN); 403 } 404 405 static int tmp108_resume(struct device *dev) 406 { 407 struct tmp108 *tmp108 = dev_get_drvdata(dev); 408 int err; 409 410 err = regmap_update_bits(tmp108->regmap, TMP108_REG_CONF, 411 TMP108_CONF_MODE_MASK, TMP108_MODE_CONTINUOUS); 412 tmp108->ready_time = jiffies + 413 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS); 414 return err; 415 } 416 417 static DEFINE_SIMPLE_DEV_PM_OPS(tmp108_dev_pm_ops, tmp108_suspend, tmp108_resume); 418 419 static const struct i2c_device_id tmp108_i2c_ids[] = { 420 { "tmp108" }, 421 { } 422 }; 423 MODULE_DEVICE_TABLE(i2c, tmp108_i2c_ids); 424 425 #ifdef CONFIG_OF 426 static const struct of_device_id tmp108_of_ids[] = { 427 { .compatible = "nxp,p3t1085", }, 428 { .compatible = "ti,tmp108", }, 429 {} 430 }; 431 MODULE_DEVICE_TABLE(of, tmp108_of_ids); 432 #endif 433 434 static struct i2c_driver tmp108_driver = { 435 .driver = { 436 .name = DRIVER_NAME, 437 .pm = pm_sleep_ptr(&tmp108_dev_pm_ops), 438 .of_match_table = of_match_ptr(tmp108_of_ids), 439 }, 440 .probe = tmp108_probe, 441 .id_table = tmp108_i2c_ids, 442 }; 443 444 static const struct i3c_device_id p3t1085_i3c_ids[] = { 445 I3C_DEVICE(0x011b, 0x1529, NULL), 446 {} 447 }; 448 MODULE_DEVICE_TABLE(i3c, p3t1085_i3c_ids); 449 450 static int p3t1085_i3c_probe(struct i3c_device *i3cdev) 451 { 452 struct device *dev = i3cdev_to_dev(i3cdev); 453 struct regmap *regmap; 454 455 regmap = devm_regmap_init_i3c(i3cdev, &tmp108_regmap_config); 456 if (IS_ERR(regmap)) 457 return dev_err_probe(dev, PTR_ERR(regmap), 458 "Failed to register i3c regmap\n"); 459 460 return tmp108_common_probe(dev, regmap, "p3t1085_i3c"); 461 } 462 463 static struct i3c_driver p3t1085_driver = { 464 .driver = { 465 .name = "p3t1085_i3c", 466 }, 467 .probe = p3t1085_i3c_probe, 468 .id_table = p3t1085_i3c_ids, 469 }; 470 471 module_i3c_i2c_driver(p3t1085_driver, &tmp108_driver) 472 473 MODULE_AUTHOR("John Muir <john@jmuir.com>"); 474 MODULE_DESCRIPTION("Texas Instruments TMP108 temperature sensor driver"); 475 MODULE_LICENSE("GPL"); 476