blob: 82a8a29af2e4c65b7d120b3dec662a113e69b187 [file] [log] [blame]
Jean Delvarecff37c92010-05-27 19:58:57 +02001/* Texas Instruments TMP102 SMBus temperature sensor driver
Steven Kingbeb1b6b2010-05-27 19:58:56 +02002 *
Jean Delvarecff37c92010-05-27 19:58:57 +02003 * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
Steven Kingbeb1b6b2010-05-27 19:58:56 +02004 *
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.
9 *
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.
Steven Kingbeb1b6b2010-05-27 19:58:56 +020014 */
15
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070016#include <linux/delay.h>
Steven Kingbeb1b6b2010-05-27 19:58:56 +020017#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>
Jean Delvarecff37c92010-05-27 19:58:57 +020025#include <linux/device.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020026#include <linux/jiffies.h>
Eduardo Valentin6a027522013-07-16 14:57:51 -040027#include <linux/thermal.h>
28#include <linux/of.h>
Steven Kingbeb1b6b2010-05-27 19:58:56 +020029
30#define DRIVER_NAME "tmp102"
31
32#define TMP102_TEMP_REG 0x00
33#define TMP102_CONF_REG 0x01
34/* note: these bit definitions are byte swapped */
35#define TMP102_CONF_SD 0x0100
36#define TMP102_CONF_TM 0x0200
37#define TMP102_CONF_POL 0x0400
38#define TMP102_CONF_F0 0x0800
39#define TMP102_CONF_F1 0x1000
40#define TMP102_CONF_R0 0x2000
41#define TMP102_CONF_R1 0x4000
42#define TMP102_CONF_OS 0x8000
43#define TMP102_CONF_EM 0x0010
44#define TMP102_CONF_AL 0x0020
45#define TMP102_CONF_CR0 0x0040
46#define TMP102_CONF_CR1 0x0080
47#define TMP102_TLOW_REG 0x02
48#define TMP102_THIGH_REG 0x03
49
Guenter Roecka9f92ccf32016-06-22 10:01:57 -070050#define TMP102_CONFREG_MASK (TMP102_CONF_SD | TMP102_CONF_TM | \
51 TMP102_CONF_POL | TMP102_CONF_F0 | \
52 TMP102_CONF_F1 | TMP102_CONF_OS | \
53 TMP102_CONF_EM | TMP102_CONF_AL | \
54 TMP102_CONF_CR0 | TMP102_CONF_CR1)
55
56#define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
57 TMP102_CONF_CR0)
58#define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
59 TMP102_CONF_CR1)
60
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070061#define CONVERSION_TIME_MS 35 /* in milli-seconds */
62
Steven Kingbeb1b6b2010-05-27 19:58:56 +020063struct tmp102 {
Guenter Roeckad9beea2014-02-02 10:08:09 -080064 struct i2c_client *client;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020065 struct mutex lock;
Jean Delvare38806bd2010-05-27 19:58:59 +020066 u16 config_orig;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020067 unsigned long last_update;
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070068 unsigned long ready_time;
69 bool valid;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020070 int temp[3];
71};
72
Jean Delvarecff37c92010-05-27 19:58:57 +020073/* convert left adjusted 13-bit TMP102 register value to milliCelsius */
74static inline int tmp102_reg_to_mC(s16 val)
Steven Kingbeb1b6b2010-05-27 19:58:56 +020075{
Jean Delvarecff37c92010-05-27 19:58:57 +020076 return ((val & ~0x01) * 1000) / 128;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020077}
78
Jean Delvarecff37c92010-05-27 19:58:57 +020079/* convert milliCelsius to left adjusted 13-bit TMP102 register value */
80static inline u16 tmp102_mC_to_reg(int val)
Steven Kingbeb1b6b2010-05-27 19:58:56 +020081{
82 return (val * 128) / 1000;
83}
84
85static const u8 tmp102_reg[] = {
86 TMP102_TEMP_REG,
87 TMP102_TLOW_REG,
88 TMP102_THIGH_REG,
89};
90
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070091static void tmp102_update_device(struct device *dev)
Steven Kingbeb1b6b2010-05-27 19:58:56 +020092{
Guenter Roeckad9beea2014-02-02 10:08:09 -080093 struct tmp102 *tmp102 = dev_get_drvdata(dev);
94 struct i2c_client *client = tmp102->client;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020095
96 mutex_lock(&tmp102->lock);
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070097 if (!tmp102->valid ||
98 time_after(jiffies, tmp102->last_update + HZ / 3)) {
Steven Kingbeb1b6b2010-05-27 19:58:56 +020099 int i;
100 for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
Jean Delvare90f41022011-11-04 12:00:47 +0100101 int status = i2c_smbus_read_word_swapped(client,
102 tmp102_reg[i]);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200103 if (status > -1)
104 tmp102->temp[i] = tmp102_reg_to_mC(status);
105 }
106 tmp102->last_update = jiffies;
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700107 tmp102->valid = true;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200108 }
109 mutex_unlock(&tmp102->lock);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200110}
111
Sascha Hauer17e83512015-07-24 08:12:54 +0200112static int tmp102_read_temp(void *dev, int *temp)
Eduardo Valentin6a027522013-07-16 14:57:51 -0400113{
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700114 struct tmp102 *tmp102 = dev_get_drvdata(dev);
Eduardo Valentin6a027522013-07-16 14:57:51 -0400115
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700116 if (time_before(jiffies, tmp102->ready_time)) {
Nishanth Menon00917b52015-12-01 10:10:21 -0600117 dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
118 return -EAGAIN;
119 }
120
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700121 tmp102_update_device(dev);
122
Eduardo Valentin6a027522013-07-16 14:57:51 -0400123 *temp = tmp102->temp[0];
124
125 return 0;
126}
127
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200128static ssize_t tmp102_show_temp(struct device *dev,
129 struct device_attribute *attr,
130 char *buf)
131{
132 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700133 struct tmp102 *tmp102 = dev_get_drvdata(dev);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200134
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700135 if (time_before(jiffies, tmp102->ready_time))
Nishanth Menon00917b52015-12-01 10:10:21 -0600136 return -EAGAIN;
137
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700138 tmp102_update_device(dev);
139
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200140 return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
141}
142
143static ssize_t tmp102_set_temp(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf, size_t count)
146{
147 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeckad9beea2014-02-02 10:08:09 -0800148 struct tmp102 *tmp102 = dev_get_drvdata(dev);
149 struct i2c_client *client = tmp102->client;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200150 long val;
Jean Delvarecff37c92010-05-27 19:58:57 +0200151 int status;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200152
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100153 if (kstrtol(buf, 10, &val) < 0)
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200154 return -EINVAL;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800155 val = clamp_val(val, -256000, 255000);
Jean Delvarecff37c92010-05-27 19:58:57 +0200156
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200157 mutex_lock(&tmp102->lock);
Jean Delvarecff37c92010-05-27 19:58:57 +0200158 tmp102->temp[sda->index] = val;
Jean Delvare90f41022011-11-04 12:00:47 +0100159 status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
160 tmp102_mC_to_reg(val));
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200161 mutex_unlock(&tmp102->lock);
162 return status ? : count;
163}
164
165static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
166
167static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
168 tmp102_set_temp, 1);
169
170static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
171 tmp102_set_temp, 2);
172
Guenter Roeckad9beea2014-02-02 10:08:09 -0800173static struct attribute *tmp102_attrs[] = {
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200174 &sensor_dev_attr_temp1_input.dev_attr.attr,
175 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
176 &sensor_dev_attr_temp1_max.dev_attr.attr,
177 NULL
178};
Guenter Roeckad9beea2014-02-02 10:08:09 -0800179ATTRIBUTE_GROUPS(tmp102);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200180
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400181static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
182 .get_temp = tmp102_read_temp,
183};
184
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700185static void tmp102_restore_config(void *data)
186{
187 struct tmp102 *tmp102 = data;
188 struct i2c_client *client = tmp102->client;
189
190 i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
191 tmp102->config_orig);
192}
193
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500194static int tmp102_probe(struct i2c_client *client,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200195 const struct i2c_device_id *id)
196{
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800197 struct device *dev = &client->dev;
Guenter Roeckad9beea2014-02-02 10:08:09 -0800198 struct device *hwmon_dev;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200199 struct tmp102 *tmp102;
200 int status;
201
Jean Delvarecff37c92010-05-27 19:58:57 +0200202 if (!i2c_check_functionality(client->adapter,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200203 I2C_FUNC_SMBUS_WORD_DATA)) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800204 dev_err(dev,
Guenter Roeckb55f3752013-01-10 10:01:24 -0800205 "adapter doesn't support SMBus word transactions\n");
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200206 return -ENODEV;
207 }
208
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800209 tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
Guenter Roeckf511a212012-06-02 11:35:52 -0700210 if (!tmp102)
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200211 return -ENOMEM;
Guenter Roeckf511a212012-06-02 11:35:52 -0700212
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200213 i2c_set_clientdata(client, tmp102);
Guenter Roeckad9beea2014-02-02 10:08:09 -0800214 tmp102->client = client;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200215
Jean Delvare90f41022011-11-04 12:00:47 +0100216 status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
Jean Delvare38806bd2010-05-27 19:58:59 +0200217 if (status < 0) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800218 dev_err(dev, "error reading config register\n");
Guenter Roeckf511a212012-06-02 11:35:52 -0700219 return status;
Jean Delvare38806bd2010-05-27 19:58:59 +0200220 }
Guenter Roecka9f92ccf32016-06-22 10:01:57 -0700221
222 if ((status & ~TMP102_CONFREG_MASK) !=
223 (TMP102_CONF_R0 | TMP102_CONF_R1)) {
224 dev_err(dev, "unexpected config register value\n");
225 return -ENODEV;
226 }
227
Jean Delvare38806bd2010-05-27 19:58:59 +0200228 tmp102->config_orig = status;
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700229
230 devm_add_action(dev, tmp102_restore_config, tmp102);
231
Guenter Roecka9f92ccf32016-06-22 10:01:57 -0700232 status &= ~TMP102_CONFIG_CLEAR;
233 status |= TMP102_CONFIG_SET;
234
235 status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, status);
Jean Delvarecff37c92010-05-27 19:58:57 +0200236 if (status < 0) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800237 dev_err(dev, "error writing config register\n");
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700238 return status;
Jean Delvarecff37c92010-05-27 19:58:57 +0200239 }
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700240
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200241 mutex_init(&tmp102->lock);
242
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700243 tmp102->ready_time = jiffies;
244 if (tmp102->config_orig & TMP102_CONF_SD) {
245 /*
246 * Mark that we are not ready with data until the first
247 * conversion is complete
248 */
249 tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
250 }
251
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700252 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
253 tmp102,
254 tmp102_groups);
Guenter Roeckad9beea2014-02-02 10:08:09 -0800255 if (IS_ERR(hwmon_dev)) {
256 dev_dbg(dev, "unable to register hwmon device\n");
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700257 return PTR_ERR(hwmon_dev);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200258 }
Eduardo Valentin51b77fd2016-03-09 13:02:54 -0800259 devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
260 &tmp102_of_thermal_ops);
Eduardo Valentin6a027522013-07-16 14:57:51 -0400261
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800262 dev_info(dev, "initialized\n");
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200263
264 return 0;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200265}
266
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200267#ifdef CONFIG_PM_SLEEP
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200268static int tmp102_suspend(struct device *dev)
269{
270 struct i2c_client *client = to_i2c_client(dev);
Jean Delvare8d4dee92010-05-27 19:58:58 +0200271 int config;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200272
Jean Delvare90f41022011-11-04 12:00:47 +0100273 config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
Jean Delvare8d4dee92010-05-27 19:58:58 +0200274 if (config < 0)
275 return config;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200276
Jean Delvare8d4dee92010-05-27 19:58:58 +0200277 config |= TMP102_CONF_SD;
Jean Delvare90f41022011-11-04 12:00:47 +0100278 return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200279}
280
281static int tmp102_resume(struct device *dev)
282{
283 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700284 struct tmp102 *tmp102 = i2c_get_clientdata(client);
Jean Delvare8d4dee92010-05-27 19:58:58 +0200285 int config;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200286
Jean Delvare90f41022011-11-04 12:00:47 +0100287 config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
Jean Delvare8d4dee92010-05-27 19:58:58 +0200288 if (config < 0)
289 return config;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200290
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700291 tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
292
Jean Delvare8d4dee92010-05-27 19:58:58 +0200293 config &= ~TMP102_CONF_SD;
Jean Delvare90f41022011-11-04 12:00:47 +0100294 return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200295}
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200296#endif /* CONFIG_PM */
297
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200298static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
299
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200300static const struct i2c_device_id tmp102_id[] = {
Jean Delvarecff37c92010-05-27 19:58:57 +0200301 { "tmp102", 0 },
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200302 { }
303};
Jean Delvarecff37c92010-05-27 19:58:57 +0200304MODULE_DEVICE_TABLE(i2c, tmp102_id);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200305
306static struct i2c_driver tmp102_driver = {
307 .driver.name = DRIVER_NAME,
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200308 .driver.pm = &tmp102_dev_pm_ops,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200309 .probe = tmp102_probe,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200310 .id_table = tmp102_id,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200311};
312
Axel Linf0967ee2012-01-20 15:38:18 +0800313module_i2c_driver(tmp102_driver);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200314
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200315MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
316MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
317MODULE_LICENSE("GPL");