xref: /linux/drivers/hwmon/tmp102.c (revision b55f375725ff85aada394da488802b0a3cc99e88)
1cff37c9eSJean Delvare /* Texas Instruments TMP102 SMBus temperature sensor driver
2beb1b6bbSSteven King  *
3cff37c9eSJean Delvare  * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
4beb1b6bbSSteven King  *
5beb1b6bbSSteven King  * This program is free software; you can redistribute it and/or modify
6beb1b6bbSSteven King  * it under the terms of the GNU General Public License as published by
7beb1b6bbSSteven King  * the Free Software Foundation; either version 2 of the License, or
8beb1b6bbSSteven King  * (at your option) any later version.
9beb1b6bbSSteven King  *
10beb1b6bbSSteven King  * This program is distributed in the hope that it will be useful,
11beb1b6bbSSteven King  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12beb1b6bbSSteven King  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13beb1b6bbSSteven King  * GNU General Public License for more details.
14beb1b6bbSSteven King  *
15beb1b6bbSSteven King  * You should have received a copy of the GNU General Public License
16beb1b6bbSSteven King  * along with this program; if not, write to the Free Software
17beb1b6bbSSteven King  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
18beb1b6bbSSteven King  */
19beb1b6bbSSteven King 
20beb1b6bbSSteven King #include <linux/module.h>
21beb1b6bbSSteven King #include <linux/init.h>
22beb1b6bbSSteven King #include <linux/slab.h>
23beb1b6bbSSteven King #include <linux/i2c.h>
24beb1b6bbSSteven King #include <linux/hwmon.h>
25beb1b6bbSSteven King #include <linux/hwmon-sysfs.h>
26beb1b6bbSSteven King #include <linux/err.h>
27beb1b6bbSSteven King #include <linux/mutex.h>
28cff37c9eSJean Delvare #include <linux/device.h>
29dcd8f392SJean Delvare #include <linux/jiffies.h>
30beb1b6bbSSteven King 
31beb1b6bbSSteven King #define	DRIVER_NAME "tmp102"
32beb1b6bbSSteven King 
33beb1b6bbSSteven King #define	TMP102_TEMP_REG			0x00
34beb1b6bbSSteven King #define	TMP102_CONF_REG			0x01
35beb1b6bbSSteven King /* note: these bit definitions are byte swapped */
36beb1b6bbSSteven King #define		TMP102_CONF_SD		0x0100
37beb1b6bbSSteven King #define		TMP102_CONF_TM		0x0200
38beb1b6bbSSteven King #define		TMP102_CONF_POL		0x0400
39beb1b6bbSSteven King #define		TMP102_CONF_F0		0x0800
40beb1b6bbSSteven King #define		TMP102_CONF_F1		0x1000
41beb1b6bbSSteven King #define		TMP102_CONF_R0		0x2000
42beb1b6bbSSteven King #define		TMP102_CONF_R1		0x4000
43beb1b6bbSSteven King #define		TMP102_CONF_OS		0x8000
44beb1b6bbSSteven King #define		TMP102_CONF_EM		0x0010
45beb1b6bbSSteven King #define		TMP102_CONF_AL		0x0020
46beb1b6bbSSteven King #define		TMP102_CONF_CR0		0x0040
47beb1b6bbSSteven King #define		TMP102_CONF_CR1		0x0080
48beb1b6bbSSteven King #define	TMP102_TLOW_REG			0x02
49beb1b6bbSSteven King #define	TMP102_THIGH_REG		0x03
50beb1b6bbSSteven King 
51beb1b6bbSSteven King struct tmp102 {
52beb1b6bbSSteven King 	struct device *hwmon_dev;
53beb1b6bbSSteven King 	struct mutex lock;
5438806bdaSJean Delvare 	u16 config_orig;
55beb1b6bbSSteven King 	unsigned long last_update;
56beb1b6bbSSteven King 	int temp[3];
57beb1b6bbSSteven King };
58beb1b6bbSSteven King 
59cff37c9eSJean Delvare /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
60cff37c9eSJean Delvare static inline int tmp102_reg_to_mC(s16 val)
61beb1b6bbSSteven King {
62cff37c9eSJean Delvare 	return ((val & ~0x01) * 1000) / 128;
63beb1b6bbSSteven King }
64beb1b6bbSSteven King 
65cff37c9eSJean Delvare /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
66cff37c9eSJean Delvare static inline u16 tmp102_mC_to_reg(int val)
67beb1b6bbSSteven King {
68beb1b6bbSSteven King 	return (val * 128) / 1000;
69beb1b6bbSSteven King }
70beb1b6bbSSteven King 
71beb1b6bbSSteven King static const u8 tmp102_reg[] = {
72beb1b6bbSSteven King 	TMP102_TEMP_REG,
73beb1b6bbSSteven King 	TMP102_TLOW_REG,
74beb1b6bbSSteven King 	TMP102_THIGH_REG,
75beb1b6bbSSteven King };
76beb1b6bbSSteven King 
77beb1b6bbSSteven King static struct tmp102 *tmp102_update_device(struct i2c_client *client)
78beb1b6bbSSteven King {
79beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
80beb1b6bbSSteven King 
81beb1b6bbSSteven King 	mutex_lock(&tmp102->lock);
82cff37c9eSJean Delvare 	if (time_after(jiffies, tmp102->last_update + HZ / 3)) {
83beb1b6bbSSteven King 		int i;
84beb1b6bbSSteven King 		for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
8590f4102cSJean Delvare 			int status = i2c_smbus_read_word_swapped(client,
8690f4102cSJean Delvare 								 tmp102_reg[i]);
87beb1b6bbSSteven King 			if (status > -1)
88beb1b6bbSSteven King 				tmp102->temp[i] = tmp102_reg_to_mC(status);
89beb1b6bbSSteven King 		}
90beb1b6bbSSteven King 		tmp102->last_update = jiffies;
91beb1b6bbSSteven King 	}
92beb1b6bbSSteven King 	mutex_unlock(&tmp102->lock);
93beb1b6bbSSteven King 	return tmp102;
94beb1b6bbSSteven King }
95beb1b6bbSSteven King 
96beb1b6bbSSteven King static ssize_t tmp102_show_temp(struct device *dev,
97beb1b6bbSSteven King 				struct device_attribute *attr,
98beb1b6bbSSteven King 				char *buf)
99beb1b6bbSSteven King {
100beb1b6bbSSteven King 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
101beb1b6bbSSteven King 	struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev));
102beb1b6bbSSteven King 
103beb1b6bbSSteven King 	return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
104beb1b6bbSSteven King }
105beb1b6bbSSteven King 
106beb1b6bbSSteven King static ssize_t tmp102_set_temp(struct device *dev,
107beb1b6bbSSteven King 			       struct device_attribute *attr,
108beb1b6bbSSteven King 			       const char *buf, size_t count)
109beb1b6bbSSteven King {
110beb1b6bbSSteven King 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
111beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
112beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
113beb1b6bbSSteven King 	long val;
114cff37c9eSJean Delvare 	int status;
115beb1b6bbSSteven King 
116179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val) < 0)
117beb1b6bbSSteven King 		return -EINVAL;
1182a844c14SGuenter Roeck 	val = clamp_val(val, -256000, 255000);
119cff37c9eSJean Delvare 
120beb1b6bbSSteven King 	mutex_lock(&tmp102->lock);
121beb1b6bbSSteven King 	tmp102->temp[sda->index] = val;
12290f4102cSJean Delvare 	status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
123beb1b6bbSSteven King 					      tmp102_mC_to_reg(val));
124beb1b6bbSSteven King 	mutex_unlock(&tmp102->lock);
125beb1b6bbSSteven King 	return status ? : count;
126beb1b6bbSSteven King }
127beb1b6bbSSteven King 
128beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
129beb1b6bbSSteven King 
130beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
131beb1b6bbSSteven King 			  tmp102_set_temp, 1);
132beb1b6bbSSteven King 
133beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
134beb1b6bbSSteven King 			  tmp102_set_temp, 2);
135beb1b6bbSSteven King 
136beb1b6bbSSteven King static struct attribute *tmp102_attributes[] = {
137beb1b6bbSSteven King 	&sensor_dev_attr_temp1_input.dev_attr.attr,
138beb1b6bbSSteven King 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
139beb1b6bbSSteven King 	&sensor_dev_attr_temp1_max.dev_attr.attr,
140beb1b6bbSSteven King 	NULL
141beb1b6bbSSteven King };
142beb1b6bbSSteven King 
143beb1b6bbSSteven King static const struct attribute_group tmp102_attr_group = {
144beb1b6bbSSteven King 	.attrs = tmp102_attributes,
145beb1b6bbSSteven King };
146beb1b6bbSSteven King 
147beb1b6bbSSteven King #define TMP102_CONFIG  (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
148beb1b6bbSSteven King #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
149beb1b6bbSSteven King 
1506c931ae1SBill Pemberton static int tmp102_probe(struct i2c_client *client,
151beb1b6bbSSteven King 				  const struct i2c_device_id *id)
152beb1b6bbSSteven King {
153beb1b6bbSSteven King 	struct tmp102 *tmp102;
154beb1b6bbSSteven King 	int status;
155beb1b6bbSSteven King 
156cff37c9eSJean Delvare 	if (!i2c_check_functionality(client->adapter,
157beb1b6bbSSteven King 				     I2C_FUNC_SMBUS_WORD_DATA)) {
158*b55f3757SGuenter Roeck 		dev_err(&client->dev,
159*b55f3757SGuenter Roeck 			"adapter doesn't support SMBus word transactions\n");
160beb1b6bbSSteven King 		return -ENODEV;
161beb1b6bbSSteven King 	}
162beb1b6bbSSteven King 
163f511a21fSGuenter Roeck 	tmp102 = devm_kzalloc(&client->dev, sizeof(*tmp102), GFP_KERNEL);
164f511a21fSGuenter Roeck 	if (!tmp102)
165beb1b6bbSSteven King 		return -ENOMEM;
166f511a21fSGuenter Roeck 
167beb1b6bbSSteven King 	i2c_set_clientdata(client, tmp102);
168beb1b6bbSSteven King 
16990f4102cSJean Delvare 	status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
17038806bdaSJean Delvare 	if (status < 0) {
17138806bdaSJean Delvare 		dev_err(&client->dev, "error reading config register\n");
172f511a21fSGuenter Roeck 		return status;
17338806bdaSJean Delvare 	}
17438806bdaSJean Delvare 	tmp102->config_orig = status;
17590f4102cSJean Delvare 	status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
17690f4102cSJean Delvare 					      TMP102_CONFIG);
177cff37c9eSJean Delvare 	if (status < 0) {
178cff37c9eSJean Delvare 		dev_err(&client->dev, "error writing config register\n");
17938806bdaSJean Delvare 		goto fail_restore_config;
180cff37c9eSJean Delvare 	}
18190f4102cSJean Delvare 	status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
182beb1b6bbSSteven King 	if (status < 0) {
183cff37c9eSJean Delvare 		dev_err(&client->dev, "error reading config register\n");
18438806bdaSJean Delvare 		goto fail_restore_config;
185beb1b6bbSSteven King 	}
186beb1b6bbSSteven King 	status &= ~TMP102_CONFIG_RD_ONLY;
187beb1b6bbSSteven King 	if (status != TMP102_CONFIG) {
188cff37c9eSJean Delvare 		dev_err(&client->dev, "config settings did not stick\n");
189cff37c9eSJean Delvare 		status = -ENODEV;
19038806bdaSJean Delvare 		goto fail_restore_config;
191beb1b6bbSSteven King 	}
192beb1b6bbSSteven King 	tmp102->last_update = jiffies - HZ;
193beb1b6bbSSteven King 	mutex_init(&tmp102->lock);
194beb1b6bbSSteven King 
195beb1b6bbSSteven King 	status = sysfs_create_group(&client->dev.kobj, &tmp102_attr_group);
196beb1b6bbSSteven King 	if (status) {
197beb1b6bbSSteven King 		dev_dbg(&client->dev, "could not create sysfs files\n");
19838806bdaSJean Delvare 		goto fail_restore_config;
199beb1b6bbSSteven King 	}
200beb1b6bbSSteven King 	tmp102->hwmon_dev = hwmon_device_register(&client->dev);
201beb1b6bbSSteven King 	if (IS_ERR(tmp102->hwmon_dev)) {
202beb1b6bbSSteven King 		dev_dbg(&client->dev, "unable to register hwmon device\n");
203beb1b6bbSSteven King 		status = PTR_ERR(tmp102->hwmon_dev);
20438806bdaSJean Delvare 		goto fail_remove_sysfs;
205beb1b6bbSSteven King 	}
206beb1b6bbSSteven King 
207beb1b6bbSSteven King 	dev_info(&client->dev, "initialized\n");
208beb1b6bbSSteven King 
209beb1b6bbSSteven King 	return 0;
21038806bdaSJean Delvare 
21138806bdaSJean Delvare fail_remove_sysfs:
212beb1b6bbSSteven King 	sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
21338806bdaSJean Delvare fail_restore_config:
21490f4102cSJean Delvare 	i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
21590f4102cSJean Delvare 				     tmp102->config_orig);
216cff37c9eSJean Delvare 	return status;
217beb1b6bbSSteven King }
218beb1b6bbSSteven King 
219281dfd0bSBill Pemberton static int tmp102_remove(struct i2c_client *client)
220beb1b6bbSSteven King {
221beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
222beb1b6bbSSteven King 
223beb1b6bbSSteven King 	hwmon_device_unregister(tmp102->hwmon_dev);
224beb1b6bbSSteven King 	sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
22538806bdaSJean Delvare 
22638806bdaSJean Delvare 	/* Stop monitoring if device was stopped originally */
22738806bdaSJean Delvare 	if (tmp102->config_orig & TMP102_CONF_SD) {
22838806bdaSJean Delvare 		int config;
22938806bdaSJean Delvare 
23090f4102cSJean Delvare 		config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
23138806bdaSJean Delvare 		if (config >= 0)
23290f4102cSJean Delvare 			i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
23338806bdaSJean Delvare 						     config | TMP102_CONF_SD);
23438806bdaSJean Delvare 	}
23538806bdaSJean Delvare 
236beb1b6bbSSteven King 	return 0;
237beb1b6bbSSteven King }
238beb1b6bbSSteven King 
239beb1b6bbSSteven King #ifdef CONFIG_PM
240beb1b6bbSSteven King static int tmp102_suspend(struct device *dev)
241beb1b6bbSSteven King {
242beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
2438d4dee98SJean Delvare 	int config;
244beb1b6bbSSteven King 
24590f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
2468d4dee98SJean Delvare 	if (config < 0)
2478d4dee98SJean Delvare 		return config;
248beb1b6bbSSteven King 
2498d4dee98SJean Delvare 	config |= TMP102_CONF_SD;
25090f4102cSJean Delvare 	return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
251beb1b6bbSSteven King }
252beb1b6bbSSteven King 
253beb1b6bbSSteven King static int tmp102_resume(struct device *dev)
254beb1b6bbSSteven King {
255beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
2568d4dee98SJean Delvare 	int config;
257beb1b6bbSSteven King 
25890f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
2598d4dee98SJean Delvare 	if (config < 0)
2608d4dee98SJean Delvare 		return config;
261beb1b6bbSSteven King 
2628d4dee98SJean Delvare 	config &= ~TMP102_CONF_SD;
26390f4102cSJean Delvare 	return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
264beb1b6bbSSteven King }
265beb1b6bbSSteven King 
266beb1b6bbSSteven King static const struct dev_pm_ops tmp102_dev_pm_ops = {
267beb1b6bbSSteven King 	.suspend	= tmp102_suspend,
268beb1b6bbSSteven King 	.resume		= tmp102_resume,
269beb1b6bbSSteven King };
270beb1b6bbSSteven King 
271beb1b6bbSSteven King #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops)
272beb1b6bbSSteven King #else
273beb1b6bbSSteven King #define	TMP102_DEV_PM_OPS NULL
274beb1b6bbSSteven King #endif /* CONFIG_PM */
275beb1b6bbSSteven King 
276beb1b6bbSSteven King static const struct i2c_device_id tmp102_id[] = {
277cff37c9eSJean Delvare 	{ "tmp102", 0 },
278beb1b6bbSSteven King 	{ }
279beb1b6bbSSteven King };
280cff37c9eSJean Delvare MODULE_DEVICE_TABLE(i2c, tmp102_id);
281beb1b6bbSSteven King 
282beb1b6bbSSteven King static struct i2c_driver tmp102_driver = {
283beb1b6bbSSteven King 	.driver.name	= DRIVER_NAME,
284beb1b6bbSSteven King 	.driver.pm	= TMP102_DEV_PM_OPS,
285beb1b6bbSSteven King 	.probe		= tmp102_probe,
2869e5e9b7aSBill Pemberton 	.remove		= tmp102_remove,
287beb1b6bbSSteven King 	.id_table	= tmp102_id,
288beb1b6bbSSteven King };
289beb1b6bbSSteven King 
290f0967eeaSAxel Lin module_i2c_driver(tmp102_driver);
291beb1b6bbSSteven King 
292beb1b6bbSSteven King MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
293beb1b6bbSSteven King MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
294beb1b6bbSSteven King MODULE_LICENSE("GPL");
295