xref: /linux/drivers/hwmon/tmp102.c (revision 90f4102ce59226954edbe960b2434d8b3da5f086)
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>
29beb1b6bbSSteven King 
30beb1b6bbSSteven King #define	DRIVER_NAME "tmp102"
31beb1b6bbSSteven King 
32beb1b6bbSSteven King #define	TMP102_TEMP_REG			0x00
33beb1b6bbSSteven King #define	TMP102_CONF_REG			0x01
34beb1b6bbSSteven King /* note: these bit definitions are byte swapped */
35beb1b6bbSSteven King #define		TMP102_CONF_SD		0x0100
36beb1b6bbSSteven King #define		TMP102_CONF_TM		0x0200
37beb1b6bbSSteven King #define		TMP102_CONF_POL		0x0400
38beb1b6bbSSteven King #define		TMP102_CONF_F0		0x0800
39beb1b6bbSSteven King #define		TMP102_CONF_F1		0x1000
40beb1b6bbSSteven King #define		TMP102_CONF_R0		0x2000
41beb1b6bbSSteven King #define		TMP102_CONF_R1		0x4000
42beb1b6bbSSteven King #define		TMP102_CONF_OS		0x8000
43beb1b6bbSSteven King #define		TMP102_CONF_EM		0x0010
44beb1b6bbSSteven King #define		TMP102_CONF_AL		0x0020
45beb1b6bbSSteven King #define		TMP102_CONF_CR0		0x0040
46beb1b6bbSSteven King #define		TMP102_CONF_CR1		0x0080
47beb1b6bbSSteven King #define	TMP102_TLOW_REG			0x02
48beb1b6bbSSteven King #define	TMP102_THIGH_REG		0x03
49beb1b6bbSSteven King 
50beb1b6bbSSteven King struct tmp102 {
51beb1b6bbSSteven King 	struct device *hwmon_dev;
52beb1b6bbSSteven King 	struct mutex lock;
5338806bdaSJean Delvare 	u16 config_orig;
54beb1b6bbSSteven King 	unsigned long last_update;
55beb1b6bbSSteven King 	int temp[3];
56beb1b6bbSSteven King };
57beb1b6bbSSteven King 
58cff37c9eSJean Delvare /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
59cff37c9eSJean Delvare static inline int tmp102_reg_to_mC(s16 val)
60beb1b6bbSSteven King {
61cff37c9eSJean Delvare 	return ((val & ~0x01) * 1000) / 128;
62beb1b6bbSSteven King }
63beb1b6bbSSteven King 
64cff37c9eSJean Delvare /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
65cff37c9eSJean Delvare static inline u16 tmp102_mC_to_reg(int val)
66beb1b6bbSSteven King {
67beb1b6bbSSteven King 	return (val * 128) / 1000;
68beb1b6bbSSteven King }
69beb1b6bbSSteven King 
70beb1b6bbSSteven King static const u8 tmp102_reg[] = {
71beb1b6bbSSteven King 	TMP102_TEMP_REG,
72beb1b6bbSSteven King 	TMP102_TLOW_REG,
73beb1b6bbSSteven King 	TMP102_THIGH_REG,
74beb1b6bbSSteven King };
75beb1b6bbSSteven King 
76beb1b6bbSSteven King static struct tmp102 *tmp102_update_device(struct i2c_client *client)
77beb1b6bbSSteven King {
78beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
79beb1b6bbSSteven King 
80beb1b6bbSSteven King 	mutex_lock(&tmp102->lock);
81cff37c9eSJean Delvare 	if (time_after(jiffies, tmp102->last_update + HZ / 3)) {
82beb1b6bbSSteven King 		int i;
83beb1b6bbSSteven King 		for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
84*90f4102cSJean Delvare 			int status = i2c_smbus_read_word_swapped(client,
85*90f4102cSJean Delvare 								 tmp102_reg[i]);
86beb1b6bbSSteven King 			if (status > -1)
87beb1b6bbSSteven King 				tmp102->temp[i] = tmp102_reg_to_mC(status);
88beb1b6bbSSteven King 		}
89beb1b6bbSSteven King 		tmp102->last_update = jiffies;
90beb1b6bbSSteven King 	}
91beb1b6bbSSteven King 	mutex_unlock(&tmp102->lock);
92beb1b6bbSSteven King 	return tmp102;
93beb1b6bbSSteven King }
94beb1b6bbSSteven King 
95beb1b6bbSSteven King static ssize_t tmp102_show_temp(struct device *dev,
96beb1b6bbSSteven King 				struct device_attribute *attr,
97beb1b6bbSSteven King 				char *buf)
98beb1b6bbSSteven King {
99beb1b6bbSSteven King 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
100beb1b6bbSSteven King 	struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev));
101beb1b6bbSSteven King 
102beb1b6bbSSteven King 	return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
103beb1b6bbSSteven King }
104beb1b6bbSSteven King 
105beb1b6bbSSteven King static ssize_t tmp102_set_temp(struct device *dev,
106beb1b6bbSSteven King 			       struct device_attribute *attr,
107beb1b6bbSSteven King 			       const char *buf, size_t count)
108beb1b6bbSSteven King {
109beb1b6bbSSteven King 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
110beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
111beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
112beb1b6bbSSteven King 	long val;
113cff37c9eSJean Delvare 	int status;
114beb1b6bbSSteven King 
115cff37c9eSJean Delvare 	if (strict_strtol(buf, 10, &val) < 0)
116beb1b6bbSSteven King 		return -EINVAL;
117cff37c9eSJean Delvare 	val = SENSORS_LIMIT(val, -256000, 255000);
118cff37c9eSJean Delvare 
119beb1b6bbSSteven King 	mutex_lock(&tmp102->lock);
120beb1b6bbSSteven King 	tmp102->temp[sda->index] = val;
121*90f4102cSJean Delvare 	status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
122beb1b6bbSSteven King 					      tmp102_mC_to_reg(val));
123beb1b6bbSSteven King 	mutex_unlock(&tmp102->lock);
124beb1b6bbSSteven King 	return status ? : count;
125beb1b6bbSSteven King }
126beb1b6bbSSteven King 
127beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
128beb1b6bbSSteven King 
129beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
130beb1b6bbSSteven King 			  tmp102_set_temp, 1);
131beb1b6bbSSteven King 
132beb1b6bbSSteven King static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
133beb1b6bbSSteven King 			  tmp102_set_temp, 2);
134beb1b6bbSSteven King 
135beb1b6bbSSteven King static struct attribute *tmp102_attributes[] = {
136beb1b6bbSSteven King 	&sensor_dev_attr_temp1_input.dev_attr.attr,
137beb1b6bbSSteven King 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
138beb1b6bbSSteven King 	&sensor_dev_attr_temp1_max.dev_attr.attr,
139beb1b6bbSSteven King 	NULL
140beb1b6bbSSteven King };
141beb1b6bbSSteven King 
142beb1b6bbSSteven King static const struct attribute_group tmp102_attr_group = {
143beb1b6bbSSteven King 	.attrs = tmp102_attributes,
144beb1b6bbSSteven King };
145beb1b6bbSSteven King 
146beb1b6bbSSteven King #define TMP102_CONFIG  (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
147beb1b6bbSSteven King #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
148beb1b6bbSSteven King 
149beb1b6bbSSteven King static int __devinit tmp102_probe(struct i2c_client *client,
150beb1b6bbSSteven King 				  const struct i2c_device_id *id)
151beb1b6bbSSteven King {
152beb1b6bbSSteven King 	struct tmp102 *tmp102;
153beb1b6bbSSteven King 	int status;
154beb1b6bbSSteven King 
155cff37c9eSJean Delvare 	if (!i2c_check_functionality(client->adapter,
156beb1b6bbSSteven King 				     I2C_FUNC_SMBUS_WORD_DATA)) {
15725985edcSLucas De Marchi 		dev_err(&client->dev, "adapter doesn't support SMBus word "
158cff37c9eSJean Delvare 			"transactions\n");
159beb1b6bbSSteven King 		return -ENODEV;
160beb1b6bbSSteven King 	}
161beb1b6bbSSteven King 
162beb1b6bbSSteven King 	tmp102 = kzalloc(sizeof(*tmp102), GFP_KERNEL);
163beb1b6bbSSteven King 	if (!tmp102) {
164beb1b6bbSSteven King 		dev_dbg(&client->dev, "kzalloc failed\n");
165beb1b6bbSSteven King 		return -ENOMEM;
166beb1b6bbSSteven King 	}
167beb1b6bbSSteven King 	i2c_set_clientdata(client, tmp102);
168beb1b6bbSSteven King 
169*90f4102cSJean 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");
17238806bdaSJean Delvare 		goto fail_free;
17338806bdaSJean Delvare 	}
17438806bdaSJean Delvare 	tmp102->config_orig = status;
175*90f4102cSJean Delvare 	status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
176*90f4102cSJean 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 	}
181*90f4102cSJean 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:
214*90f4102cSJean Delvare 	i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
215*90f4102cSJean Delvare 				     tmp102->config_orig);
21638806bdaSJean Delvare fail_free:
217beb1b6bbSSteven King 	kfree(tmp102);
218beb1b6bbSSteven King 
219cff37c9eSJean Delvare 	return status;
220beb1b6bbSSteven King }
221beb1b6bbSSteven King 
222beb1b6bbSSteven King static int __devexit tmp102_remove(struct i2c_client *client)
223beb1b6bbSSteven King {
224beb1b6bbSSteven King 	struct tmp102 *tmp102 = i2c_get_clientdata(client);
225beb1b6bbSSteven King 
226beb1b6bbSSteven King 	hwmon_device_unregister(tmp102->hwmon_dev);
227beb1b6bbSSteven King 	sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
22838806bdaSJean Delvare 
22938806bdaSJean Delvare 	/* Stop monitoring if device was stopped originally */
23038806bdaSJean Delvare 	if (tmp102->config_orig & TMP102_CONF_SD) {
23138806bdaSJean Delvare 		int config;
23238806bdaSJean Delvare 
233*90f4102cSJean Delvare 		config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
23438806bdaSJean Delvare 		if (config >= 0)
235*90f4102cSJean Delvare 			i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
23638806bdaSJean Delvare 						     config | TMP102_CONF_SD);
23738806bdaSJean Delvare 	}
23838806bdaSJean Delvare 
239beb1b6bbSSteven King 	kfree(tmp102);
240beb1b6bbSSteven King 
241beb1b6bbSSteven King 	return 0;
242beb1b6bbSSteven King }
243beb1b6bbSSteven King 
244beb1b6bbSSteven King #ifdef CONFIG_PM
245beb1b6bbSSteven King static int tmp102_suspend(struct device *dev)
246beb1b6bbSSteven King {
247beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
2488d4dee98SJean Delvare 	int config;
249beb1b6bbSSteven King 
250*90f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
2518d4dee98SJean Delvare 	if (config < 0)
2528d4dee98SJean Delvare 		return config;
253beb1b6bbSSteven King 
2548d4dee98SJean Delvare 	config |= TMP102_CONF_SD;
255*90f4102cSJean Delvare 	return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
256beb1b6bbSSteven King }
257beb1b6bbSSteven King 
258beb1b6bbSSteven King static int tmp102_resume(struct device *dev)
259beb1b6bbSSteven King {
260beb1b6bbSSteven King 	struct i2c_client *client = to_i2c_client(dev);
2618d4dee98SJean Delvare 	int config;
262beb1b6bbSSteven King 
263*90f4102cSJean Delvare 	config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
2648d4dee98SJean Delvare 	if (config < 0)
2658d4dee98SJean Delvare 		return config;
266beb1b6bbSSteven King 
2678d4dee98SJean Delvare 	config &= ~TMP102_CONF_SD;
268*90f4102cSJean Delvare 	return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
269beb1b6bbSSteven King }
270beb1b6bbSSteven King 
271beb1b6bbSSteven King static const struct dev_pm_ops tmp102_dev_pm_ops = {
272beb1b6bbSSteven King 	.suspend	= tmp102_suspend,
273beb1b6bbSSteven King 	.resume		= tmp102_resume,
274beb1b6bbSSteven King };
275beb1b6bbSSteven King 
276beb1b6bbSSteven King #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops)
277beb1b6bbSSteven King #else
278beb1b6bbSSteven King #define	TMP102_DEV_PM_OPS NULL
279beb1b6bbSSteven King #endif /* CONFIG_PM */
280beb1b6bbSSteven King 
281beb1b6bbSSteven King static const struct i2c_device_id tmp102_id[] = {
282cff37c9eSJean Delvare 	{ "tmp102", 0 },
283beb1b6bbSSteven King 	{ }
284beb1b6bbSSteven King };
285cff37c9eSJean Delvare MODULE_DEVICE_TABLE(i2c, tmp102_id);
286beb1b6bbSSteven King 
287beb1b6bbSSteven King static struct i2c_driver tmp102_driver = {
288beb1b6bbSSteven King 	.driver.name	= DRIVER_NAME,
289beb1b6bbSSteven King 	.driver.pm	= TMP102_DEV_PM_OPS,
290beb1b6bbSSteven King 	.probe		= tmp102_probe,
291beb1b6bbSSteven King 	.remove		= __devexit_p(tmp102_remove),
292beb1b6bbSSteven King 	.id_table	= tmp102_id,
293beb1b6bbSSteven King };
294beb1b6bbSSteven King 
295beb1b6bbSSteven King static int __init tmp102_init(void)
296beb1b6bbSSteven King {
297beb1b6bbSSteven King 	return i2c_add_driver(&tmp102_driver);
298beb1b6bbSSteven King }
299beb1b6bbSSteven King module_init(tmp102_init);
300beb1b6bbSSteven King 
301beb1b6bbSSteven King static void __exit tmp102_exit(void)
302beb1b6bbSSteven King {
303beb1b6bbSSteven King 	i2c_del_driver(&tmp102_driver);
304beb1b6bbSSteven King }
305beb1b6bbSSteven King module_exit(tmp102_exit);
306beb1b6bbSSteven King 
307beb1b6bbSSteven King MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
308beb1b6bbSSteven King MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
309beb1b6bbSSteven King MODULE_LICENSE("GPL");
310