1 /* Texas Instruments TMP108 SMBus temperature sensor driver
3 * Copyright (C) 2016 John Muir <john@jmuir.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/jiffies.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
30 #define DRIVER_NAME "tmp108"
32 #define TMP108_REG_TEMP 0x00
33 #define TMP108_REG_CONF 0x01
34 #define TMP108_REG_TLOW 0x02
35 #define TMP108_REG_THIGH 0x03
37 #define TMP108_TEMP_MIN_MC -50000 /* Minimum millicelcius. */
38 #define TMP108_TEMP_MAX_MC 127937 /* Maximum millicelcius. */
40 /* Configuration register bits.
41 * Note: these bit definitions are byte swapped.
43 #define TMP108_CONF_M0 0x0100 /* Sensor mode. */
44 #define TMP108_CONF_M1 0x0200
45 #define TMP108_CONF_TM 0x0400 /* Thermostat mode. */
46 #define TMP108_CONF_FL 0x0800 /* Watchdog flag - TLOW */
47 #define TMP108_CONF_FH 0x1000 /* Watchdog flag - THIGH */
48 #define TMP108_CONF_CR0 0x2000 /* Conversion rate. */
49 #define TMP108_CONF_CR1 0x4000
50 #define TMP108_CONF_ID 0x8000
51 #define TMP108_CONF_HYS0 0x0010 /* Hysteresis. */
52 #define TMP108_CONF_HYS1 0x0020
53 #define TMP108_CONF_POL 0x0080 /* Polarity of alert. */
55 /* Defaults set by the hardware upon reset. */
56 #define TMP108_CONF_DEFAULTS (TMP108_CONF_CR0 | TMP108_CONF_TM |\
57 TMP108_CONF_HYS0 | TMP108_CONF_M1)
58 /* These bits are read-only. */
59 #define TMP108_CONF_READ_ONLY (TMP108_CONF_FL | TMP108_CONF_FH |\
62 #define TMP108_CONF_MODE_MASK (TMP108_CONF_M0|TMP108_CONF_M1)
63 #define TMP108_MODE_SHUTDOWN 0x0000
64 #define TMP108_MODE_ONE_SHOT TMP108_CONF_M0
65 #define TMP108_MODE_CONTINUOUS TMP108_CONF_M1 /* Default */
66 /* When M1 is set, M0 is ignored. */
68 #define TMP108_CONF_CONVRATE_MASK (TMP108_CONF_CR0|TMP108_CONF_CR1)
69 #define TMP108_CONVRATE_0P25HZ 0x0000
70 #define TMP108_CONVRATE_1HZ TMP108_CONF_CR0 /* Default */
71 #define TMP108_CONVRATE_4HZ TMP108_CONF_CR1
72 #define TMP108_CONVRATE_16HZ (TMP108_CONF_CR0|TMP108_CONF_CR1)
74 #define TMP108_CONF_HYSTERESIS_MASK (TMP108_CONF_HYS0|TMP108_CONF_HYS1)
75 #define TMP108_HYSTERESIS_0C 0x0000
76 #define TMP108_HYSTERESIS_1C TMP108_CONF_HYS0 /* Default */
77 #define TMP108_HYSTERESIS_2C TMP108_CONF_HYS1
78 #define TMP108_HYSTERESIS_4C (TMP108_CONF_HYS0|TMP108_CONF_HYS1)
80 #define TMP108_CONVERSION_TIME_MS 30 /* in milli-seconds */
83 struct regmap
*regmap
;
85 unsigned long ready_time
;
88 /* convert 12-bit TMP108 register value to milliCelsius */
89 static inline int tmp108_temp_reg_to_mC(s16 val
)
91 return (val
& ~0x0f) * 1000 / 256;
94 /* convert milliCelsius to left adjusted 12-bit TMP108 register value */
95 static inline u16
tmp108_mC_to_temp_reg(int val
)
97 return (val
* 256) / 1000;
100 static int tmp108_read(struct device
*dev
, enum hwmon_sensor_types type
,
101 u32 attr
, int channel
, long *temp
)
103 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
107 if (type
== hwmon_chip
) {
108 if (attr
== hwmon_chip_update_interval
) {
109 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
,
113 switch (regval
& TMP108_CONF_CONVRATE_MASK
) {
114 case TMP108_CONVRATE_0P25HZ
:
118 case TMP108_CONVRATE_1HZ
:
121 case TMP108_CONVRATE_4HZ
:
124 case TMP108_CONVRATE_16HZ
:
134 case hwmon_temp_input
:
135 /* Is it too early to return a conversion ? */
136 if (time_before(jiffies
, tmp108
->ready_time
)) {
137 dev_dbg(dev
, "%s: Conversion not ready yet..\n",
141 err
= regmap_read(tmp108
->regmap
, TMP108_REG_TEMP
, ®val
);
144 *temp
= tmp108_temp_reg_to_mC(regval
);
148 err
= regmap_read(tmp108
->regmap
, attr
== hwmon_temp_min
?
149 TMP108_REG_TLOW
: TMP108_REG_THIGH
, ®val
);
152 *temp
= tmp108_temp_reg_to_mC(regval
);
154 case hwmon_temp_min_alarm
:
155 case hwmon_temp_max_alarm
:
156 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, ®val
);
159 *temp
= !!(regval
& (attr
== hwmon_temp_min_alarm
?
160 TMP108_CONF_FL
: TMP108_CONF_FH
));
162 case hwmon_temp_min_hyst
:
163 case hwmon_temp_max_hyst
:
164 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, ®val
);
167 switch (regval
& TMP108_CONF_HYSTERESIS_MASK
) {
168 case TMP108_HYSTERESIS_0C
:
172 case TMP108_HYSTERESIS_1C
:
175 case TMP108_HYSTERESIS_2C
:
178 case TMP108_HYSTERESIS_4C
:
182 err
= regmap_read(tmp108
->regmap
, attr
== hwmon_temp_min_hyst
?
183 TMP108_REG_TLOW
: TMP108_REG_THIGH
, ®val
);
186 *temp
= tmp108_temp_reg_to_mC(regval
);
187 if (attr
== hwmon_temp_min_hyst
)
199 static int tmp108_write(struct device
*dev
, enum hwmon_sensor_types type
,
200 u32 attr
, int channel
, long temp
)
202 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
206 if (type
== hwmon_chip
) {
207 if (attr
== hwmon_chip_update_interval
) {
209 mask
= TMP108_CONVRATE_16HZ
;
211 mask
= TMP108_CONVRATE_4HZ
;
212 else if (temp
< 2500)
213 mask
= TMP108_CONVRATE_1HZ
;
215 mask
= TMP108_CONVRATE_0P25HZ
;
216 return regmap_update_bits(tmp108
->regmap
,
218 TMP108_CONF_CONVRATE_MASK
,
227 temp
= clamp_val(temp
, TMP108_TEMP_MIN_MC
, TMP108_TEMP_MAX_MC
);
228 return regmap_write(tmp108
->regmap
,
229 attr
== hwmon_temp_min
?
230 TMP108_REG_TLOW
: TMP108_REG_THIGH
,
231 tmp108_mC_to_temp_reg(temp
));
232 case hwmon_temp_min_hyst
:
233 case hwmon_temp_max_hyst
:
234 temp
= clamp_val(temp
, TMP108_TEMP_MIN_MC
, TMP108_TEMP_MAX_MC
);
235 err
= regmap_read(tmp108
->regmap
,
236 attr
== hwmon_temp_min_hyst
?
237 TMP108_REG_TLOW
: TMP108_REG_THIGH
,
241 if (attr
== hwmon_temp_min_hyst
)
242 temp
-= tmp108_temp_reg_to_mC(regval
);
244 temp
= tmp108_temp_reg_to_mC(regval
) - temp
;
246 mask
= TMP108_HYSTERESIS_0C
;
247 else if (temp
< 1500)
248 mask
= TMP108_HYSTERESIS_1C
;
249 else if (temp
< 3000)
250 mask
= TMP108_HYSTERESIS_2C
;
252 mask
= TMP108_HYSTERESIS_4C
;
253 return regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
254 TMP108_CONF_HYSTERESIS_MASK
, mask
);
260 static umode_t
tmp108_is_visible(const void *data
, enum hwmon_sensor_types type
,
261 u32 attr
, int channel
)
263 if (type
== hwmon_chip
&& attr
== hwmon_chip_update_interval
)
266 if (type
!= hwmon_temp
)
270 case hwmon_temp_input
:
271 case hwmon_temp_min_alarm
:
272 case hwmon_temp_max_alarm
:
276 case hwmon_temp_min_hyst
:
277 case hwmon_temp_max_hyst
:
284 static u32 tmp108_chip_config
[] = {
285 HWMON_C_REGISTER_TZ
| HWMON_C_UPDATE_INTERVAL
,
289 static const struct hwmon_channel_info tmp108_chip
= {
291 .config
= tmp108_chip_config
,
294 static u32 tmp108_temp_config
[] = {
295 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MIN
| HWMON_T_MIN_HYST
296 | HWMON_T_MAX_HYST
| HWMON_T_MIN_ALARM
| HWMON_T_MAX_ALARM
,
300 static const struct hwmon_channel_info tmp108_temp
= {
302 .config
= tmp108_temp_config
,
305 static const struct hwmon_channel_info
*tmp108_info
[] = {
311 static const struct hwmon_ops tmp108_hwmon_ops
= {
312 .is_visible
= tmp108_is_visible
,
314 .write
= tmp108_write
,
317 static const struct hwmon_chip_info tmp108_chip_info
= {
318 .ops
= &tmp108_hwmon_ops
,
322 static void tmp108_restore_config(void *data
)
324 struct tmp108
*tmp108
= data
;
326 regmap_write(tmp108
->regmap
, TMP108_REG_CONF
, tmp108
->orig_config
);
329 static bool tmp108_is_writeable_reg(struct device
*dev
, unsigned int reg
)
331 return reg
!= TMP108_REG_TEMP
;
334 static bool tmp108_is_volatile_reg(struct device
*dev
, unsigned int reg
)
336 /* Configuration register must be volatile to enable FL and FH. */
337 return reg
== TMP108_REG_TEMP
|| reg
== TMP108_REG_CONF
;
340 static const struct regmap_config tmp108_regmap_config
= {
343 .max_register
= TMP108_REG_THIGH
,
344 .writeable_reg
= tmp108_is_writeable_reg
,
345 .volatile_reg
= tmp108_is_volatile_reg
,
346 .val_format_endian
= REGMAP_ENDIAN_BIG
,
347 .cache_type
= REGCACHE_RBTREE
,
348 .use_single_rw
= true,
351 static int tmp108_probe(struct i2c_client
*client
,
352 const struct i2c_device_id
*id
)
354 struct device
*dev
= &client
->dev
;
355 struct device
*hwmon_dev
;
356 struct tmp108
*tmp108
;
360 if (!i2c_check_functionality(client
->adapter
,
361 I2C_FUNC_SMBUS_WORD_DATA
)) {
363 "adapter doesn't support SMBus word transactions\n");
367 tmp108
= devm_kzalloc(dev
, sizeof(*tmp108
), GFP_KERNEL
);
371 dev_set_drvdata(dev
, tmp108
);
373 tmp108
->regmap
= devm_regmap_init_i2c(client
, &tmp108_regmap_config
);
374 if (IS_ERR(tmp108
->regmap
)) {
375 err
= PTR_ERR(tmp108
->regmap
);
376 dev_err(dev
, "regmap init failed: %d", err
);
380 err
= regmap_read(tmp108
->regmap
, TMP108_REG_CONF
, &config
);
382 dev_err(dev
, "error reading config register: %d", err
);
385 tmp108
->orig_config
= config
;
387 /* Only continuous mode is supported. */
388 config
&= ~TMP108_CONF_MODE_MASK
;
389 config
|= TMP108_MODE_CONTINUOUS
;
391 /* Only comparator mode is supported. */
392 config
&= ~TMP108_CONF_TM
;
394 err
= regmap_write(tmp108
->regmap
, TMP108_REG_CONF
, config
);
396 dev_err(dev
, "error writing config register: %d", err
);
400 tmp108
->ready_time
= jiffies
;
401 if ((tmp108
->orig_config
& TMP108_CONF_MODE_MASK
) ==
402 TMP108_MODE_SHUTDOWN
)
403 tmp108
->ready_time
+=
404 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS
);
406 err
= devm_add_action_or_reset(dev
, tmp108_restore_config
, tmp108
);
408 dev_err(dev
, "add action or reset failed: %d", err
);
412 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
416 return PTR_ERR_OR_ZERO(hwmon_dev
);
419 static int __maybe_unused
tmp108_suspend(struct device
*dev
)
421 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
423 return regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
424 TMP108_CONF_MODE_MASK
, TMP108_MODE_SHUTDOWN
);
427 static int __maybe_unused
tmp108_resume(struct device
*dev
)
429 struct tmp108
*tmp108
= dev_get_drvdata(dev
);
432 err
= regmap_update_bits(tmp108
->regmap
, TMP108_REG_CONF
,
433 TMP108_CONF_MODE_MASK
, TMP108_MODE_CONTINUOUS
);
434 tmp108
->ready_time
= jiffies
+
435 msecs_to_jiffies(TMP108_CONVERSION_TIME_MS
);
439 static SIMPLE_DEV_PM_OPS(tmp108_dev_pm_ops
, tmp108_suspend
, tmp108_resume
);
441 static const struct i2c_device_id tmp108_i2c_ids
[] = {
445 MODULE_DEVICE_TABLE(i2c
, tmp108_i2c_ids
);
448 static const struct of_device_id tmp108_of_ids
[] = {
449 { .compatible
= "ti,tmp108", },
452 MODULE_DEVICE_TABLE(of
, tmp108_of_ids
);
455 static struct i2c_driver tmp108_driver
= {
458 .pm
= &tmp108_dev_pm_ops
,
459 .of_match_table
= of_match_ptr(tmp108_of_ids
),
461 .probe
= tmp108_probe
,
462 .id_table
= tmp108_i2c_ids
,
465 module_i2c_driver(tmp108_driver
);
467 MODULE_AUTHOR("John Muir <john@jmuir.com>");
468 MODULE_DESCRIPTION("Texas Instruments TMP108 temperature sensor driver");
469 MODULE_LICENSE("GPL");