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 int tmp103_read(struct device *dev, enum hwmon_sensor_types type,
55 u32 attr, int channel, long *temp)
57 struct regmap *regmap = dev_get_drvdata(dev);
62 case hwmon_temp_input:
63 reg = TMP103_TEMP_REG;
66 reg = TMP103_TLOW_REG;
69 reg = TMP103_THIGH_REG;
75 err = regmap_read(regmap, reg, ®val);
79 *temp = tmp103_reg_to_mc(regval);
84 static int tmp103_write(struct device *dev, enum hwmon_sensor_types type,
85 u32 attr, int channel, long temp)
87 struct regmap *regmap = dev_get_drvdata(dev);
92 reg = TMP103_TLOW_REG;
95 reg = TMP103_THIGH_REG;
101 temp = clamp_val(temp, -55000, 127000);
102 return regmap_write(regmap, reg, tmp103_mc_to_reg(temp));
105 static umode_t tmp103_is_visible(const void *data, enum hwmon_sensor_types type,
106 u32 attr, int channel)
108 if (type != hwmon_temp)
112 case hwmon_temp_input:
122 static const struct hwmon_channel_info *tmp103_info[] = {
123 HWMON_CHANNEL_INFO(chip,
124 HWMON_C_REGISTER_TZ),
125 HWMON_CHANNEL_INFO(temp,
126 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN),
130 static const struct hwmon_ops tmp103_hwmon_ops = {
131 .is_visible = tmp103_is_visible,
133 .write = tmp103_write,
136 static const struct hwmon_chip_info tmp103_chip_info = {
137 .ops = &tmp103_hwmon_ops,
141 static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg)
143 return reg == TMP103_TEMP_REG;
146 static const struct regmap_config tmp103_regmap_config = {
149 .max_register = TMP103_THIGH_REG,
150 .volatile_reg = tmp103_regmap_is_volatile,
153 static int tmp103_probe(struct i2c_client *client)
155 struct device *dev = &client->dev;
156 struct device *hwmon_dev;
157 struct regmap *regmap;
160 regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config);
161 if (IS_ERR(regmap)) {
162 dev_err(dev, "failed to allocate register map\n");
163 return PTR_ERR(regmap);
166 ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK,
169 dev_err(&client->dev, "error writing config register\n");
173 i2c_set_clientdata(client, regmap);
174 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
178 return PTR_ERR_OR_ZERO(hwmon_dev);
181 static int tmp103_suspend(struct device *dev)
183 struct regmap *regmap = dev_get_drvdata(dev);
185 return regmap_update_bits(regmap, TMP103_CONF_REG,
186 TMP103_CONF_SD_MASK, 0);
189 static int tmp103_resume(struct device *dev)
191 struct regmap *regmap = dev_get_drvdata(dev);
193 return regmap_update_bits(regmap, TMP103_CONF_REG,
194 TMP103_CONF_SD_MASK, TMP103_CONF_SD);
197 static DEFINE_SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops, tmp103_suspend, tmp103_resume);
199 static const struct i2c_device_id tmp103_id[] = {
203 MODULE_DEVICE_TABLE(i2c, tmp103_id);
205 static const struct of_device_id __maybe_unused tmp103_of_match[] = {
206 { .compatible = "ti,tmp103" },
209 MODULE_DEVICE_TABLE(of, tmp103_of_match);
211 static struct i2c_driver tmp103_driver = {
214 .of_match_table = of_match_ptr(tmp103_of_match),
215 .pm = pm_sleep_ptr(&tmp103_dev_pm_ops),
217 .probe_new = tmp103_probe,
218 .id_table = tmp103_id,
221 module_i2c_driver(tmp103_driver);
223 MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
224 MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
225 MODULE_LICENSE("GPL");