1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Texas Instruments TMP108 SMBus temperature sensor driver
4 * Copyright (C) 2016 John Muir <john@jmuir.com>
7 #include <linux/delay.h>
8 #include <linux/device.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/module.h>
13 #include <linux/mutex.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>
21 #define DRIVER_NAME "tmp108"
23 #define TMP108_REG_TEMP 0x00
24 #define TMP108_REG_CONF 0x01
25 #define TMP108_REG_TLOW 0x02
26 #define TMP108_REG_THIGH 0x03
28 #define TMP108_TEMP_MIN_MC -50000 /* Minimum millicelcius. */
29 #define TMP108_TEMP_MAX_MC 127937 /* Maximum millicelcius. */
31 /* Configuration register bits.
32 * Note: these bit definitions are byte swapped.
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. */
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 |\
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. */
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)
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)
71 #define TMP108_CONVERSION_TIME_MS 30 /* in milli-seconds */
74 struct regmap
*regmap
;
76 unsigned long ready_time
;
79 /* convert 12-bit TMP108 register value to milliCelsius */
80 static inline int tmp108_temp_reg_to_mC(s16 val
)
82 return (val
& ~0x0f) * 1000 / 256;
85 /* convert milliCelsius to left adjusted 12-bit TMP108 register value */
86 static inline u16
tmp108_mC_to_temp_reg(int val
)
88 return (val
* 256) / 1000;
91 static int tmp108_read(struct device
*dev
, enum hwmon_sensor_types type
,
92 u32 attr
, int channel
, long *temp
)
94 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
98 if (type
== hwmon_chip
) {
99 if (attr
== hwmon_chip_update_interval
) {
100 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
,
104 switch (regval
& TMP108_CONF_CONVRATE_MASK
) {
105 case TMP108_CONVRATE_0P25HZ
:
109 case TMP108_CONVRATE_1HZ
:
112 case TMP108_CONVRATE_4HZ
:
115 case TMP108_CONVRATE_16HZ
:
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",
132 err
= regmap_read(tmp108
->regmap
, TMP108_REG_TEMP
, ®val
);
135 *temp
= tmp108_temp_reg_to_mC(regval
);
139 err
= regmap_read(tmp108
->regmap
, attr
== hwmon_temp_min
?
140 TMP108_REG_TLOW
: TMP108_REG_THIGH
, ®val
);
143 *temp
= tmp108_temp_reg_to_mC(regval
);
145 case hwmon_temp_min_alarm
:
146 case hwmon_temp_max_alarm
:
147 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, ®val
);
150 *temp
= !!(regval
& (attr
== hwmon_temp_min_alarm
?
151 TMP108_CONF_FL
: TMP108_CONF_FH
));
153 case hwmon_temp_min_hyst
:
154 case hwmon_temp_max_hyst
:
155 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, ®val
);
158 switch (regval
& TMP108_CONF_HYSTERESIS_MASK
) {
159 case TMP108_HYSTERESIS_0C
:
163 case TMP108_HYSTERESIS_1C
:
166 case TMP108_HYSTERESIS_2C
:
169 case TMP108_HYSTERESIS_4C
:
173 err
= regmap_read(tmp108
->regmap
, attr
== hwmon_temp_min_hyst
?
174 TMP108_REG_TLOW
: TMP108_REG_THIGH
, ®val
);
177 *temp
= tmp108_temp_reg_to_mC(regval
);
178 if (attr
== hwmon_temp_min_hyst
)
190 static int tmp108_write(struct device
*dev
, enum hwmon_sensor_types type
,
191 u32 attr
, int channel
, long temp
)
193 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
197 if (type
== hwmon_chip
) {
198 if (attr
== hwmon_chip_update_interval
) {
200 mask
= TMP108_CONVRATE_16HZ
;
202 mask
= TMP108_CONVRATE_4HZ
;
203 else if (temp
< 2500)
204 mask
= TMP108_CONVRATE_1HZ
;
206 mask
= TMP108_CONVRATE_0P25HZ
;
207 return regmap_update_bits(tmp108
->regmap
,
209 TMP108_CONF_CONVRATE_MASK
,
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
,
232 if (attr
== hwmon_temp_min_hyst
)
233 temp
-= tmp108_temp_reg_to_mC(regval
);
235 temp
= tmp108_temp_reg_to_mC(regval
) - temp
;
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
;
243 mask
= TMP108_HYSTERESIS_4C
;
244 return regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
245 TMP108_CONF_HYSTERESIS_MASK
, mask
);
251 static umode_t
tmp108_is_visible(const void *data
, enum hwmon_sensor_types type
,
252 u32 attr
, int channel
)
254 if (type
== hwmon_chip
&& attr
== hwmon_chip_update_interval
)
257 if (type
!= hwmon_temp
)
261 case hwmon_temp_input
:
262 case hwmon_temp_min_alarm
:
263 case hwmon_temp_max_alarm
:
267 case hwmon_temp_min_hyst
:
268 case hwmon_temp_max_hyst
:
275 static 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
),
285 static const struct hwmon_ops tmp108_hwmon_ops
= {
286 .is_visible
= tmp108_is_visible
,
288 .write
= tmp108_write
,
291 static const struct hwmon_chip_info tmp108_chip_info
= {
292 .ops
= &tmp108_hwmon_ops
,
296 static void tmp108_restore_config(void *data
)
298 struct tmp108
*tmp108
= data
;
300 regmap_write(tmp108
->regmap
, TMP108_REG_CONF
, tmp108
->orig_config
);
303 static bool tmp108_is_writeable_reg(struct device
*dev
, unsigned int reg
)
305 return reg
!= TMP108_REG_TEMP
;
308 static bool tmp108_is_volatile_reg(struct device
*dev
, unsigned int reg
)
310 /* Configuration register must be volatile to enable FL and FH. */
311 return reg
== TMP108_REG_TEMP
|| reg
== TMP108_REG_CONF
;
314 static const struct regmap_config tmp108_regmap_config
= {
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,
326 static int tmp108_probe(struct i2c_client
*client
)
328 struct device
*dev
= &client
->dev
;
329 struct device
*hwmon_dev
;
330 struct tmp108
*tmp108
;
334 if (!i2c_check_functionality(client
->adapter
,
335 I2C_FUNC_SMBUS_WORD_DATA
)) {
337 "adapter doesn't support SMBus word transactions\n");
341 tmp108
= devm_kzalloc(dev
, sizeof(*tmp108
), GFP_KERNEL
);
345 dev_set_drvdata(dev
, tmp108
);
347 tmp108
->regmap
= devm_regmap_init_i2c(client
, &tmp108_regmap_config
);
348 if (IS_ERR(tmp108
->regmap
)) {
349 err
= PTR_ERR(tmp108
->regmap
);
350 dev_err(dev
, "regmap init failed: %d", err
);
354 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, &config
);
356 dev_err(dev
, "error reading config register: %d", err
);
359 tmp108
->orig_config
= config
;
361 /* Only continuous mode is supported. */
362 config
&= ~TMP108_CONF_MODE_MASK
;
363 config
|= TMP108_MODE_CONTINUOUS
;
365 /* Only comparator mode is supported. */
366 config
&= ~TMP108_CONF_TM
;
368 err
= regmap_write(tmp108
->regmap
, TMP108_REG_CONF
, config
);
370 dev_err(dev
, "error writing config register: %d", err
);
374 tmp108
->ready_time
= jiffies
;
375 if ((tmp108
->orig_config
& TMP108_CONF_MODE_MASK
) ==
376 TMP108_MODE_SHUTDOWN
)
377 tmp108
->ready_time
+=
378 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS
);
380 err
= devm_add_action_or_reset(dev
, tmp108_restore_config
, tmp108
);
382 dev_err(dev
, "add action or reset failed: %d", err
);
386 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
390 return PTR_ERR_OR_ZERO(hwmon_dev
);
393 static int __maybe_unused
tmp108_suspend(struct device
*dev
)
395 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
397 return regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
398 TMP108_CONF_MODE_MASK
, TMP108_MODE_SHUTDOWN
);
401 static int __maybe_unused
tmp108_resume(struct device
*dev
)
403 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
406 err
= regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
407 TMP108_CONF_MODE_MASK
, TMP108_MODE_CONTINUOUS
);
408 tmp108
->ready_time
= jiffies
+
409 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS
);
413 static SIMPLE_DEV_PM_OPS(tmp108_dev_pm_ops
, tmp108_suspend
, tmp108_resume
);
415 static const struct i2c_device_id tmp108_i2c_ids
[] = {
419 MODULE_DEVICE_TABLE(i2c
, tmp108_i2c_ids
);
422 static const struct of_device_id tmp108_of_ids
[] = {
423 { .compatible
= "ti,tmp108", },
426 MODULE_DEVICE_TABLE(of
, tmp108_of_ids
);
429 static struct i2c_driver tmp108_driver
= {
432 .pm
= &tmp108_dev_pm_ops
,
433 .of_match_table
= of_match_ptr(tmp108_of_ids
),
435 .probe_new
= tmp108_probe
,
436 .id_table
= tmp108_i2c_ids
,
439 module_i2c_driver(tmp108_driver
);
441 MODULE_AUTHOR("John Muir <john@jmuir.com>");
442 MODULE_DESCRIPTION("Texas Instruments TMP108 temperature sensor driver");
443 MODULE_LICENSE("GPL");