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