1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Texas Instruments TMP103 SMBus temperature sensor driver
4 * Copyright (C) 2014 Heiko Schocher <hs@denx.de>
7 * Texas Instruments TMP102 SMBus temperature sensor driver
9 * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/device.h>
21 #include <linux/jiffies.h>
22 #include <linux/regmap.h>
24 #define TMP103_TEMP_REG 0x00
25 #define TMP103_CONF_REG 0x01
26 #define TMP103_TLOW_REG 0x02
27 #define TMP103_THIGH_REG 0x03
29 #define TMP103_CONF_M0 0x01
30 #define TMP103_CONF_M1 0x02
31 #define TMP103_CONF_LC 0x04
32 #define TMP103_CONF_FL 0x08
33 #define TMP103_CONF_FH 0x10
34 #define TMP103_CONF_CR0 0x20
35 #define TMP103_CONF_CR1 0x40
36 #define TMP103_CONF_ID 0x80
37 #define TMP103_CONF_SD (TMP103_CONF_M1)
38 #define TMP103_CONF_SD_MASK (TMP103_CONF_M0 | TMP103_CONF_M1)
40 #define TMP103_CONFIG (TMP103_CONF_CR1 | TMP103_CONF_M1)
41 #define TMP103_CONFIG_MASK (TMP103_CONF_CR0 | TMP103_CONF_CR1 | \
42 TMP103_CONF_M0 | TMP103_CONF_M1)
44 static inline int tmp103_reg_to_mc(s8 val
)
49 static inline u8
tmp103_mc_to_reg(int val
)
51 return DIV_ROUND_CLOSEST(val
, 1000);
54 static ssize_t
tmp103_temp_show(struct device
*dev
,
55 struct device_attribute
*attr
, char *buf
)
57 struct sensor_device_attribute
*sda
= to_sensor_dev_attr(attr
);
58 struct regmap
*regmap
= dev_get_drvdata(dev
);
62 ret
= regmap_read(regmap
, sda
->index
, ®val
);
66 return sprintf(buf
, "%d\n", tmp103_reg_to_mc(regval
));
69 static ssize_t
tmp103_temp_store(struct device
*dev
,
70 struct device_attribute
*attr
,
71 const char *buf
, size_t count
)
73 struct sensor_device_attribute
*sda
= to_sensor_dev_attr(attr
);
74 struct regmap
*regmap
= dev_get_drvdata(dev
);
78 if (kstrtol(buf
, 10, &val
) < 0)
81 val
= clamp_val(val
, -55000, 127000);
82 ret
= regmap_write(regmap
, sda
->index
, tmp103_mc_to_reg(val
));
83 return ret
? ret
: count
;
86 static SENSOR_DEVICE_ATTR_RO(temp1_input
, tmp103_temp
, TMP103_TEMP_REG
);
88 static SENSOR_DEVICE_ATTR_RW(temp1_min
, tmp103_temp
, TMP103_TLOW_REG
);
90 static SENSOR_DEVICE_ATTR_RW(temp1_max
, tmp103_temp
, TMP103_THIGH_REG
);
92 static struct attribute
*tmp103_attrs
[] = {
93 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
94 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
95 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
98 ATTRIBUTE_GROUPS(tmp103
);
100 static bool tmp103_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
102 return reg
== TMP103_TEMP_REG
;
105 static const struct regmap_config tmp103_regmap_config
= {
108 .max_register
= TMP103_THIGH_REG
,
109 .volatile_reg
= tmp103_regmap_is_volatile
,
112 static int tmp103_probe(struct i2c_client
*client
)
114 struct device
*dev
= &client
->dev
;
115 struct device
*hwmon_dev
;
116 struct regmap
*regmap
;
119 regmap
= devm_regmap_init_i2c(client
, &tmp103_regmap_config
);
120 if (IS_ERR(regmap
)) {
121 dev_err(dev
, "failed to allocate register map\n");
122 return PTR_ERR(regmap
);
125 ret
= regmap_update_bits(regmap
, TMP103_CONF_REG
, TMP103_CONFIG_MASK
,
128 dev_err(&client
->dev
, "error writing config register\n");
132 i2c_set_clientdata(client
, regmap
);
133 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
134 regmap
, tmp103_groups
);
135 return PTR_ERR_OR_ZERO(hwmon_dev
);
138 static int __maybe_unused
tmp103_suspend(struct device
*dev
)
140 struct regmap
*regmap
= dev_get_drvdata(dev
);
142 return regmap_update_bits(regmap
, TMP103_CONF_REG
,
143 TMP103_CONF_SD_MASK
, 0);
146 static int __maybe_unused
tmp103_resume(struct device
*dev
)
148 struct regmap
*regmap
= dev_get_drvdata(dev
);
150 return regmap_update_bits(regmap
, TMP103_CONF_REG
,
151 TMP103_CONF_SD_MASK
, TMP103_CONF_SD
);
154 static SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops
, tmp103_suspend
, tmp103_resume
);
156 static const struct i2c_device_id tmp103_id
[] = {
160 MODULE_DEVICE_TABLE(i2c
, tmp103_id
);
162 static const struct of_device_id __maybe_unused tmp103_of_match
[] = {
163 { .compatible
= "ti,tmp103" },
166 MODULE_DEVICE_TABLE(of
, tmp103_of_match
);
168 static struct i2c_driver tmp103_driver
= {
171 .of_match_table
= of_match_ptr(tmp103_of_match
),
172 .pm
= &tmp103_dev_pm_ops
,
174 .probe_new
= tmp103_probe
,
175 .id_table
= tmp103_id
,
178 module_i2c_driver(tmp103_driver
);
180 MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
181 MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
182 MODULE_LICENSE("GPL");