1 /* Texas Instruments TMP102 SMBus temperature sensor driver
3 * Copyright (C) 2010 Steven King <sfking@fdwdc.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/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/device.h>
26 #include <linux/jiffies.h>
27 #include <linux/regmap.h>
28 #include <linux/thermal.h>
31 #define DRIVER_NAME "tmp102"
33 #define TMP102_TEMP_REG 0x00
34 #define TMP102_CONF_REG 0x01
35 /* note: these bit definitions are byte swapped */
36 #define TMP102_CONF_SD 0x0100
37 #define TMP102_CONF_TM 0x0200
38 #define TMP102_CONF_POL 0x0400
39 #define TMP102_CONF_F0 0x0800
40 #define TMP102_CONF_F1 0x1000
41 #define TMP102_CONF_R0 0x2000
42 #define TMP102_CONF_R1 0x4000
43 #define TMP102_CONF_OS 0x8000
44 #define TMP102_CONF_EM 0x0010
45 #define TMP102_CONF_AL 0x0020
46 #define TMP102_CONF_CR0 0x0040
47 #define TMP102_CONF_CR1 0x0080
48 #define TMP102_TLOW_REG 0x02
49 #define TMP102_THIGH_REG 0x03
51 #define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \
52 TMP102_CONF_POL | TMP102_CONF_F0 | \
53 TMP102_CONF_F1 | TMP102_CONF_OS | \
54 TMP102_CONF_EM | TMP102_CONF_AL | \
55 TMP102_CONF_CR0 | TMP102_CONF_CR1)
57 #define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
59 #define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
62 #define CONVERSION_TIME_MS 35 /* in milli-seconds */
65 struct regmap
*regmap
;
67 unsigned long ready_time
;
70 /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
71 static inline int tmp102_reg_to_mC(s16 val
)
73 return ((val
& ~0x01) * 1000) / 128;
76 /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
77 static inline u16
tmp102_mC_to_reg(int val
)
79 return (val
* 128) / 1000;
82 static int tmp102_read_temp(void *dev
, int *temp
)
84 struct tmp102
*tmp102
= dev_get_drvdata(dev
);
88 if (time_before(jiffies
, tmp102
->ready_time
)) {
89 dev_dbg(dev
, "%s: Conversion not ready yet..\n", __func__
);
93 ret
= regmap_read(tmp102
->regmap
, TMP102_TEMP_REG
, ®
);
97 *temp
= tmp102_reg_to_mC(reg
);
102 static ssize_t
tmp102_show_temp(struct device
*dev
,
103 struct device_attribute
*attr
,
106 struct sensor_device_attribute
*sda
= to_sensor_dev_attr(attr
);
107 struct tmp102
*tmp102
= dev_get_drvdata(dev
);
108 int regaddr
= sda
->index
;
112 if (regaddr
== TMP102_TEMP_REG
&&
113 time_before(jiffies
, tmp102
->ready_time
))
116 err
= regmap_read(tmp102
->regmap
, regaddr
, ®
);
120 return sprintf(buf
, "%d\n", tmp102_reg_to_mC(reg
));
123 static ssize_t
tmp102_set_temp(struct device
*dev
,
124 struct device_attribute
*attr
,
125 const char *buf
, size_t count
)
127 struct sensor_device_attribute
*sda
= to_sensor_dev_attr(attr
);
128 struct tmp102
*tmp102
= dev_get_drvdata(dev
);
129 int reg
= sda
->index
;
133 if (kstrtol(buf
, 10, &val
) < 0)
135 val
= clamp_val(val
, -256000, 255000);
137 err
= regmap_write(tmp102
->regmap
, reg
, tmp102_mC_to_reg(val
));
138 return err
? : count
;
141 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, tmp102_show_temp
, NULL
,
144 static SENSOR_DEVICE_ATTR(temp1_max_hyst
, S_IWUSR
| S_IRUGO
, tmp102_show_temp
,
145 tmp102_set_temp
, TMP102_TLOW_REG
);
147 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, tmp102_show_temp
,
148 tmp102_set_temp
, TMP102_THIGH_REG
);
150 static struct attribute
*tmp102_attrs
[] = {
151 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
152 &sensor_dev_attr_temp1_max_hyst
.dev_attr
.attr
,
153 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
156 ATTRIBUTE_GROUPS(tmp102
);
158 static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops
= {
159 .get_temp
= tmp102_read_temp
,
162 static void tmp102_restore_config(void *data
)
164 struct tmp102
*tmp102
= data
;
166 regmap_write(tmp102
->regmap
, TMP102_CONF_REG
, tmp102
->config_orig
);
169 static bool tmp102_is_writeable_reg(struct device
*dev
, unsigned int reg
)
171 return reg
!= TMP102_TEMP_REG
;
174 static bool tmp102_is_volatile_reg(struct device
*dev
, unsigned int reg
)
176 return reg
== TMP102_TEMP_REG
;
179 static const struct regmap_config tmp102_regmap_config
= {
182 .max_register
= TMP102_THIGH_REG
,
183 .writeable_reg
= tmp102_is_writeable_reg
,
184 .volatile_reg
= tmp102_is_volatile_reg
,
185 .val_format_endian
= REGMAP_ENDIAN_BIG
,
186 .cache_type
= REGCACHE_RBTREE
,
187 .use_single_rw
= true,
190 static int tmp102_probe(struct i2c_client
*client
,
191 const struct i2c_device_id
*id
)
193 struct device
*dev
= &client
->dev
;
194 struct device
*hwmon_dev
;
195 struct tmp102
*tmp102
;
199 if (!i2c_check_functionality(client
->adapter
,
200 I2C_FUNC_SMBUS_WORD_DATA
)) {
202 "adapter doesn't support SMBus word transactions\n");
206 tmp102
= devm_kzalloc(dev
, sizeof(*tmp102
), GFP_KERNEL
);
210 i2c_set_clientdata(client
, tmp102
);
212 tmp102
->regmap
= devm_regmap_init_i2c(client
, &tmp102_regmap_config
);
213 if (IS_ERR(tmp102
->regmap
))
214 return PTR_ERR(tmp102
->regmap
);
216 err
= regmap_read(tmp102
->regmap
, TMP102_CONF_REG
, ®val
);
218 dev_err(dev
, "error reading config register\n");
222 if ((regval
& ~TMP102_CONFREG_MASK
) !=
223 (TMP102_CONF_R0
| TMP102_CONF_R1
)) {
224 dev_err(dev
, "unexpected config register value\n");
228 tmp102
->config_orig
= regval
;
230 err
= devm_add_action_or_reset(dev
, tmp102_restore_config
, tmp102
);
234 regval
&= ~TMP102_CONFIG_CLEAR
;
235 regval
|= TMP102_CONFIG_SET
;
237 err
= regmap_write(tmp102
->regmap
, TMP102_CONF_REG
, regval
);
239 dev_err(dev
, "error writing config register\n");
243 tmp102
->ready_time
= jiffies
;
244 if (tmp102
->config_orig
& TMP102_CONF_SD
) {
246 * Mark that we are not ready with data until the first
247 * conversion is complete
249 tmp102
->ready_time
+= msecs_to_jiffies(CONVERSION_TIME_MS
);
252 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
255 if (IS_ERR(hwmon_dev
)) {
256 dev_dbg(dev
, "unable to register hwmon device\n");
257 return PTR_ERR(hwmon_dev
);
259 devm_thermal_zone_of_sensor_register(hwmon_dev
, 0, hwmon_dev
,
260 &tmp102_of_thermal_ops
);
262 dev_info(dev
, "initialized\n");
267 #ifdef CONFIG_PM_SLEEP
268 static int tmp102_suspend(struct device
*dev
)
270 struct i2c_client
*client
= to_i2c_client(dev
);
271 struct tmp102
*tmp102
= i2c_get_clientdata(client
);
273 return regmap_update_bits(tmp102
->regmap
, TMP102_CONF_REG
,
274 TMP102_CONF_SD
, TMP102_CONF_SD
);
277 static int tmp102_resume(struct device
*dev
)
279 struct i2c_client
*client
= to_i2c_client(dev
);
280 struct tmp102
*tmp102
= i2c_get_clientdata(client
);
283 err
= regmap_update_bits(tmp102
->regmap
, TMP102_CONF_REG
,
286 tmp102
->ready_time
= jiffies
+ msecs_to_jiffies(CONVERSION_TIME_MS
);
290 #endif /* CONFIG_PM */
292 static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops
, tmp102_suspend
, tmp102_resume
);
294 static const struct i2c_device_id tmp102_id
[] = {
298 MODULE_DEVICE_TABLE(i2c
, tmp102_id
);
300 static struct i2c_driver tmp102_driver
= {
301 .driver
.name
= DRIVER_NAME
,
302 .driver
.pm
= &tmp102_dev_pm_ops
,
303 .probe
= tmp102_probe
,
304 .id_table
= tmp102_id
,
307 module_i2c_driver(tmp102_driver
);
309 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
310 MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
311 MODULE_LICENSE("GPL");