blob: a942a2574a4d7dfaa4229f61a48d37905db2b5c3 [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>
Guenter Roeck28a340d2016-06-20 09:55:46 -070027#include <linux/regmap.h>
Eduardo Valentin6a027522013-07-16 14:57:51 -040028#include <linux/thermal.h>
29#include <linux/of.h>
Steven Kingbeb1b6b2010-05-27 19:58:56 +020030
31#define DRIVER_NAME "tmp102"
32
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
50
Guenter Roecka9f92ccf32016-06-22 10:01:57 -070051#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)
56
57#define TMP102_CONFIG_CLEAR (TMP102_CONF_SD | TMP102_CONF_OS | \
58 TMP102_CONF_CR0)
59#define TMP102_CONFIG_SET (TMP102_CONF_TM | TMP102_CONF_EM | \
60 TMP102_CONF_CR1)
61
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070062#define CONVERSION_TIME_MS 35 /* in milli-seconds */
63
Steven Kingbeb1b6b2010-05-27 19:58:56 +020064struct tmp102 {
Guenter Roeck28a340d2016-06-20 09:55:46 -070065 struct regmap *regmap;
Jean Delvare38806bd2010-05-27 19:58:59 +020066 u16 config_orig;
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070067 unsigned long ready_time;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020068};
69
Jean Delvarecff37c92010-05-27 19:58:57 +020070/* convert left adjusted 13-bit TMP102 register value to milliCelsius */
71static inline int tmp102_reg_to_mC(s16 val)
Steven Kingbeb1b6b2010-05-27 19:58:56 +020072{
Jean Delvarecff37c92010-05-27 19:58:57 +020073 return ((val & ~0x01) * 1000) / 128;
Steven Kingbeb1b6b2010-05-27 19:58:56 +020074}
75
Jean Delvarecff37c92010-05-27 19:58:57 +020076/* convert milliCelsius to left adjusted 13-bit TMP102 register value */
77static inline u16 tmp102_mC_to_reg(int val)
Steven Kingbeb1b6b2010-05-27 19:58:56 +020078{
79 return (val * 128) / 1000;
80}
81
Sascha Hauer17e83512015-07-24 08:12:54 +020082static int tmp102_read_temp(void *dev, int *temp)
Eduardo Valentin6a027522013-07-16 14:57:51 -040083{
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070084 struct tmp102 *tmp102 = dev_get_drvdata(dev);
Guenter Roeck28a340d2016-06-20 09:55:46 -070085 unsigned int reg;
86 int ret;
Eduardo Valentin6a027522013-07-16 14:57:51 -040087
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070088 if (time_before(jiffies, tmp102->ready_time)) {
Nishanth Menon00917b52015-12-01 10:10:21 -060089 dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
90 return -EAGAIN;
91 }
92
Guenter Roeck28a340d2016-06-20 09:55:46 -070093 ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, &reg);
94 if (ret < 0)
95 return ret;
Guenter Roeck3d8f7a82016-06-19 20:34:57 -070096
Guenter Roeck28a340d2016-06-20 09:55:46 -070097 *temp = tmp102_reg_to_mC(reg);
Eduardo Valentin6a027522013-07-16 14:57:51 -040098
99 return 0;
100}
101
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200102static ssize_t tmp102_show_temp(struct device *dev,
103 struct device_attribute *attr,
104 char *buf)
105{
106 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700107 struct tmp102 *tmp102 = dev_get_drvdata(dev);
Guenter Roeck28a340d2016-06-20 09:55:46 -0700108 int regaddr = sda->index;
109 unsigned int reg;
110 int err;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200111
Guenter Roeck28a340d2016-06-20 09:55:46 -0700112 if (regaddr == TMP102_TEMP_REG &&
113 time_before(jiffies, tmp102->ready_time))
Nishanth Menon00917b52015-12-01 10:10:21 -0600114 return -EAGAIN;
115
Guenter Roeck28a340d2016-06-20 09:55:46 -0700116 err = regmap_read(tmp102->regmap, regaddr, &reg);
117 if (err < 0)
118 return err;
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700119
Guenter Roeck28a340d2016-06-20 09:55:46 -0700120 return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg));
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200121}
122
123static ssize_t tmp102_set_temp(struct device *dev,
124 struct device_attribute *attr,
125 const char *buf, size_t count)
126{
127 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
Guenter Roeckad9beea2014-02-02 10:08:09 -0800128 struct tmp102 *tmp102 = dev_get_drvdata(dev);
Guenter Roeck28a340d2016-06-20 09:55:46 -0700129 int reg = sda->index;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200130 long val;
Guenter Roeck28a340d2016-06-20 09:55:46 -0700131 int err;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200132
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100133 if (kstrtol(buf, 10, &val) < 0)
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200134 return -EINVAL;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800135 val = clamp_val(val, -256000, 255000);
Jean Delvarecff37c92010-05-27 19:58:57 +0200136
Guenter Roeck28a340d2016-06-20 09:55:46 -0700137 err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val));
138 return err ? : count;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200139}
140
Guenter Roeck28a340d2016-06-20 09:55:46 -0700141static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL,
142 TMP102_TEMP_REG);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200143
144static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
Guenter Roeck28a340d2016-06-20 09:55:46 -0700145 tmp102_set_temp, TMP102_TLOW_REG);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200146
147static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
Guenter Roeck28a340d2016-06-20 09:55:46 -0700148 tmp102_set_temp, TMP102_THIGH_REG);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200149
Guenter Roeckad9beea2014-02-02 10:08:09 -0800150static struct attribute *tmp102_attrs[] = {
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200151 &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,
154 NULL
155};
Guenter Roeckad9beea2014-02-02 10:08:09 -0800156ATTRIBUTE_GROUPS(tmp102);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200157
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400158static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
159 .get_temp = tmp102_read_temp,
160};
161
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700162static void tmp102_restore_config(void *data)
163{
164 struct tmp102 *tmp102 = data;
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700165
Guenter Roeck28a340d2016-06-20 09:55:46 -0700166 regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700167}
168
Guenter Roeck28a340d2016-06-20 09:55:46 -0700169static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
170{
171 return reg != TMP102_TEMP_REG;
172}
173
174static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
175{
176 return reg == TMP102_TEMP_REG;
177}
178
179static const struct regmap_config tmp102_regmap_config = {
180 .reg_bits = 8,
181 .val_bits = 16,
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,
188};
189
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500190static int tmp102_probe(struct i2c_client *client,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200191 const struct i2c_device_id *id)
192{
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800193 struct device *dev = &client->dev;
Guenter Roeckad9beea2014-02-02 10:08:09 -0800194 struct device *hwmon_dev;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200195 struct tmp102 *tmp102;
Guenter Roeck28a340d2016-06-20 09:55:46 -0700196 unsigned int regval;
197 int err;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200198
Jean Delvarecff37c92010-05-27 19:58:57 +0200199 if (!i2c_check_functionality(client->adapter,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200200 I2C_FUNC_SMBUS_WORD_DATA)) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800201 dev_err(dev,
Guenter Roeckb55f3752013-01-10 10:01:24 -0800202 "adapter doesn't support SMBus word transactions\n");
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200203 return -ENODEV;
204 }
205
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800206 tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
Guenter Roeckf511a212012-06-02 11:35:52 -0700207 if (!tmp102)
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200208 return -ENOMEM;
Guenter Roeckf511a212012-06-02 11:35:52 -0700209
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200210 i2c_set_clientdata(client, tmp102);
211
Guenter Roeck28a340d2016-06-20 09:55:46 -0700212 tmp102->regmap = devm_regmap_init_i2c(client, &tmp102_regmap_config);
213 if (IS_ERR(tmp102->regmap))
214 return PTR_ERR(tmp102->regmap);
215
216 err = regmap_read(tmp102->regmap, TMP102_CONF_REG, &regval);
217 if (err < 0) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800218 dev_err(dev, "error reading config register\n");
Guenter Roeck28a340d2016-06-20 09:55:46 -0700219 return err;
Jean Delvare38806bd2010-05-27 19:58:59 +0200220 }
Guenter Roecka9f92ccf32016-06-22 10:01:57 -0700221
Guenter Roeck28a340d2016-06-20 09:55:46 -0700222 if ((regval & ~TMP102_CONFREG_MASK) !=
Guenter Roecka9f92ccf32016-06-22 10:01:57 -0700223 (TMP102_CONF_R0 | TMP102_CONF_R1)) {
224 dev_err(dev, "unexpected config register value\n");
225 return -ENODEV;
226 }
227
Guenter Roeck28a340d2016-06-20 09:55:46 -0700228 tmp102->config_orig = regval;
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700229
230 devm_add_action(dev, tmp102_restore_config, tmp102);
231
Guenter Roeck28a340d2016-06-20 09:55:46 -0700232 regval &= ~TMP102_CONFIG_CLEAR;
233 regval |= TMP102_CONFIG_SET;
Guenter Roecka9f92ccf32016-06-22 10:01:57 -0700234
Guenter Roeck28a340d2016-06-20 09:55:46 -0700235 err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
236 if (err < 0) {
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800237 dev_err(dev, "error writing config register\n");
Guenter Roeck28a340d2016-06-20 09:55:46 -0700238 return err;
Jean Delvarecff37c92010-05-27 19:58:57 +0200239 }
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700240
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700241 tmp102->ready_time = jiffies;
242 if (tmp102->config_orig & TMP102_CONF_SD) {
243 /*
244 * Mark that we are not ready with data until the first
245 * conversion is complete
246 */
247 tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
248 }
249
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700250 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
251 tmp102,
252 tmp102_groups);
Guenter Roeckad9beea2014-02-02 10:08:09 -0800253 if (IS_ERR(hwmon_dev)) {
254 dev_dbg(dev, "unable to register hwmon device\n");
Guenter Roeckb17ea1c2016-06-19 20:09:54 -0700255 return PTR_ERR(hwmon_dev);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200256 }
Eduardo Valentin51b77fd2016-03-09 13:02:54 -0800257 devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
258 &tmp102_of_thermal_ops);
Eduardo Valentin6a027522013-07-16 14:57:51 -0400259
Guenter Roeckfbd9af12014-02-02 14:22:30 -0800260 dev_info(dev, "initialized\n");
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200261
262 return 0;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200263}
264
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200265#ifdef CONFIG_PM_SLEEP
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200266static int tmp102_suspend(struct device *dev)
267{
268 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck28a340d2016-06-20 09:55:46 -0700269 struct tmp102 *tmp102 = i2c_get_clientdata(client);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200270
Guenter Roeck28a340d2016-06-20 09:55:46 -0700271 return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
272 TMP102_CONF_SD, TMP102_CONF_SD);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200273}
274
275static int tmp102_resume(struct device *dev)
276{
277 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700278 struct tmp102 *tmp102 = i2c_get_clientdata(client);
Guenter Roeck28a340d2016-06-20 09:55:46 -0700279 int err;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200280
Guenter Roeck28a340d2016-06-20 09:55:46 -0700281 err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
282 TMP102_CONF_SD, 0);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200283
Guenter Roeck3d8f7a82016-06-19 20:34:57 -0700284 tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
285
Guenter Roeck28a340d2016-06-20 09:55:46 -0700286 return err;
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200287}
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200288#endif /* CONFIG_PM */
289
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200290static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
291
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200292static const struct i2c_device_id tmp102_id[] = {
Jean Delvarecff37c92010-05-27 19:58:57 +0200293 { "tmp102", 0 },
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200294 { }
295};
Jean Delvarecff37c92010-05-27 19:58:57 +0200296MODULE_DEVICE_TABLE(i2c, tmp102_id);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200297
298static struct i2c_driver tmp102_driver = {
299 .driver.name = DRIVER_NAME,
Grygorii Strashkodd378b12015-02-03 17:01:58 +0200300 .driver.pm = &tmp102_dev_pm_ops,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200301 .probe = tmp102_probe,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200302 .id_table = tmp102_id,
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200303};
304
Axel Linf0967ee2012-01-20 15:38:18 +0800305module_i2c_driver(tmp102_driver);
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200306
Steven Kingbeb1b6b2010-05-27 19:58:56 +0200307MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
308MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
309MODULE_LICENSE("GPL");