1
2
3
4
5
6
7
8
9
10
11
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>
23
24#define TMP103_TEMP_REG 0x00
25#define TMP103_CONF_REG 0x01
26#define TMP103_TLOW_REG 0x02
27#define TMP103_THIGH_REG 0x03
28
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)
39
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)
43
44static inline int tmp103_reg_to_mc(s8 val)
45{
46 return val * 1000;
47}
48
49static inline u8 tmp103_mc_to_reg(int val)
50{
51 return DIV_ROUND_CLOSEST(val, 1000);
52}
53
54static ssize_t tmp103_temp_show(struct device *dev,
55 struct device_attribute *attr, char *buf)
56{
57 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
58 struct regmap *regmap = dev_get_drvdata(dev);
59 unsigned int regval;
60 int ret;
61
62 ret = regmap_read(regmap, sda->index, ®val);
63 if (ret < 0)
64 return ret;
65
66 return sprintf(buf, "%d\n", tmp103_reg_to_mc(regval));
67}
68
69static ssize_t tmp103_temp_store(struct device *dev,
70 struct device_attribute *attr,
71 const char *buf, size_t count)
72{
73 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
74 struct regmap *regmap = dev_get_drvdata(dev);
75 long val;
76 int ret;
77
78 if (kstrtol(buf, 10, &val) < 0)
79 return -EINVAL;
80
81 val = clamp_val(val, -55000, 127000);
82 ret = regmap_write(regmap, sda->index, tmp103_mc_to_reg(val));
83 return ret ? ret : count;
84}
85
86static SENSOR_DEVICE_ATTR_RO(temp1_input, tmp103_temp, TMP103_TEMP_REG);
87
88static SENSOR_DEVICE_ATTR_RW(temp1_min, tmp103_temp, TMP103_TLOW_REG);
89
90static SENSOR_DEVICE_ATTR_RW(temp1_max, tmp103_temp, TMP103_THIGH_REG);
91
92static 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,
96 NULL
97};
98ATTRIBUTE_GROUPS(tmp103);
99
100static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg)
101{
102 return reg == TMP103_TEMP_REG;
103}
104
105static const struct regmap_config tmp103_regmap_config = {
106 .reg_bits = 8,
107 .val_bits = 8,
108 .max_register = TMP103_THIGH_REG,
109 .volatile_reg = tmp103_regmap_is_volatile,
110};
111
112static int tmp103_probe(struct i2c_client *client,
113 const struct i2c_device_id *id)
114{
115 struct device *dev = &client->dev;
116 struct device *hwmon_dev;
117 struct regmap *regmap;
118 int ret;
119
120 regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config);
121 if (IS_ERR(regmap)) {
122 dev_err(dev, "failed to allocate register map\n");
123 return PTR_ERR(regmap);
124 }
125
126 ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK,
127 TMP103_CONFIG);
128 if (ret < 0) {
129 dev_err(&client->dev, "error writing config register\n");
130 return ret;
131 }
132
133 i2c_set_clientdata(client, regmap);
134 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
135 regmap, tmp103_groups);
136 return PTR_ERR_OR_ZERO(hwmon_dev);
137}
138
139static int __maybe_unused tmp103_suspend(struct device *dev)
140{
141 struct regmap *regmap = dev_get_drvdata(dev);
142
143 return regmap_update_bits(regmap, TMP103_CONF_REG,
144 TMP103_CONF_SD_MASK, 0);
145}
146
147static int __maybe_unused tmp103_resume(struct device *dev)
148{
149 struct regmap *regmap = dev_get_drvdata(dev);
150
151 return regmap_update_bits(regmap, TMP103_CONF_REG,
152 TMP103_CONF_SD_MASK, TMP103_CONF_SD);
153}
154
155static SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops, tmp103_suspend, tmp103_resume);
156
157static const struct i2c_device_id tmp103_id[] = {
158 { "tmp103", 0 },
159 { }
160};
161MODULE_DEVICE_TABLE(i2c, tmp103_id);
162
163static const struct of_device_id __maybe_unused tmp103_of_match[] = {
164 { .compatible = "ti,tmp103" },
165 { },
166};
167MODULE_DEVICE_TABLE(of, tmp103_of_match);
168
169static struct i2c_driver tmp103_driver = {
170 .driver = {
171 .name = "tmp103",
172 .of_match_table = of_match_ptr(tmp103_of_match),
173 .pm = &tmp103_dev_pm_ops,
174 },
175 .probe = tmp103_probe,
176 .id_table = tmp103_id,
177};
178
179module_i2c_driver(tmp103_driver);
180
181MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
182MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
183MODULE_LICENSE("GPL");
184