]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/tmp102.c
Merge branch 'for-4.8/core' of git://git.kernel.dk/linux-block
[karo-tx-linux.git] / drivers / hwmon / tmp102.c
1 /* Texas Instruments TMP102 SMBus temperature sensor driver
2  *
3  * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
4  *
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.
14  */
15
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>
29 #include <linux/of.h>
30
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
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)
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
62 #define CONVERSION_TIME_MS              35      /* in milli-seconds */
63
64 struct tmp102 {
65         struct regmap *regmap;
66         u16 config_orig;
67         unsigned long ready_time;
68 };
69
70 /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
71 static inline int tmp102_reg_to_mC(s16 val)
72 {
73         return ((val & ~0x01) * 1000) / 128;
74 }
75
76 /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
77 static inline u16 tmp102_mC_to_reg(int val)
78 {
79         return (val * 128) / 1000;
80 }
81
82 static int tmp102_read_temp(void *dev, int *temp)
83 {
84         struct tmp102 *tmp102 = dev_get_drvdata(dev);
85         unsigned int reg;
86         int ret;
87
88         if (time_before(jiffies, tmp102->ready_time)) {
89                 dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
90                 return -EAGAIN;
91         }
92
93         ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, &reg);
94         if (ret < 0)
95                 return ret;
96
97         *temp = tmp102_reg_to_mC(reg);
98
99         return 0;
100 }
101
102 static 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);
107         struct tmp102 *tmp102 = dev_get_drvdata(dev);
108         int regaddr = sda->index;
109         unsigned int reg;
110         int err;
111
112         if (regaddr == TMP102_TEMP_REG &&
113             time_before(jiffies, tmp102->ready_time))
114                 return -EAGAIN;
115
116         err = regmap_read(tmp102->regmap, regaddr, &reg);
117         if (err < 0)
118                 return err;
119
120         return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg));
121 }
122
123 static 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);
128         struct tmp102 *tmp102 = dev_get_drvdata(dev);
129         int reg = sda->index;
130         long val;
131         int err;
132
133         if (kstrtol(buf, 10, &val) < 0)
134                 return -EINVAL;
135         val = clamp_val(val, -256000, 255000);
136
137         err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val));
138         return err ? : count;
139 }
140
141 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL,
142                           TMP102_TEMP_REG);
143
144 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
145                           tmp102_set_temp, TMP102_TLOW_REG);
146
147 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
148                           tmp102_set_temp, TMP102_THIGH_REG);
149
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,
154         NULL
155 };
156 ATTRIBUTE_GROUPS(tmp102);
157
158 static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
159         .get_temp = tmp102_read_temp,
160 };
161
162 static void tmp102_restore_config(void *data)
163 {
164         struct tmp102 *tmp102 = data;
165
166         regmap_write(tmp102->regmap, TMP102_CONF_REG, tmp102->config_orig);
167 }
168
169 static bool tmp102_is_writeable_reg(struct device *dev, unsigned int reg)
170 {
171         return reg != TMP102_TEMP_REG;
172 }
173
174 static bool tmp102_is_volatile_reg(struct device *dev, unsigned int reg)
175 {
176         return reg == TMP102_TEMP_REG;
177 }
178
179 static 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
190 static int tmp102_probe(struct i2c_client *client,
191                                   const struct i2c_device_id *id)
192 {
193         struct device *dev = &client->dev;
194         struct device *hwmon_dev;
195         struct tmp102 *tmp102;
196         unsigned int regval;
197         int err;
198
199         if (!i2c_check_functionality(client->adapter,
200                                      I2C_FUNC_SMBUS_WORD_DATA)) {
201                 dev_err(dev,
202                         "adapter doesn't support SMBus word transactions\n");
203                 return -ENODEV;
204         }
205
206         tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
207         if (!tmp102)
208                 return -ENOMEM;
209
210         i2c_set_clientdata(client, tmp102);
211
212         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) {
218                 dev_err(dev, "error reading config register\n");
219                 return err;
220         }
221
222         if ((regval & ~TMP102_CONFREG_MASK) !=
223             (TMP102_CONF_R0 | TMP102_CONF_R1)) {
224                 dev_err(dev, "unexpected config register value\n");
225                 return -ENODEV;
226         }
227
228         tmp102->config_orig = regval;
229
230         devm_add_action(dev, tmp102_restore_config, tmp102);
231
232         regval &= ~TMP102_CONFIG_CLEAR;
233         regval |= TMP102_CONFIG_SET;
234
235         err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
236         if (err < 0) {
237                 dev_err(dev, "error writing config register\n");
238                 return err;
239         }
240
241         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
250         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
251                                                            tmp102,
252                                                            tmp102_groups);
253         if (IS_ERR(hwmon_dev)) {
254                 dev_dbg(dev, "unable to register hwmon device\n");
255                 return PTR_ERR(hwmon_dev);
256         }
257         devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
258                                              &tmp102_of_thermal_ops);
259
260         dev_info(dev, "initialized\n");
261
262         return 0;
263 }
264
265 #ifdef CONFIG_PM_SLEEP
266 static int tmp102_suspend(struct device *dev)
267 {
268         struct i2c_client *client = to_i2c_client(dev);
269         struct tmp102 *tmp102 = i2c_get_clientdata(client);
270
271         return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
272                                   TMP102_CONF_SD, TMP102_CONF_SD);
273 }
274
275 static int tmp102_resume(struct device *dev)
276 {
277         struct i2c_client *client = to_i2c_client(dev);
278         struct tmp102 *tmp102 = i2c_get_clientdata(client);
279         int err;
280
281         err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
282                                  TMP102_CONF_SD, 0);
283
284         tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
285
286         return err;
287 }
288 #endif /* CONFIG_PM */
289
290 static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
291
292 static const struct i2c_device_id tmp102_id[] = {
293         { "tmp102", 0 },
294         { }
295 };
296 MODULE_DEVICE_TABLE(i2c, tmp102_id);
297
298 static struct i2c_driver tmp102_driver = {
299         .driver.name    = DRIVER_NAME,
300         .driver.pm      = &tmp102_dev_pm_ops,
301         .probe          = tmp102_probe,
302         .id_table       = tmp102_id,
303 };
304
305 module_i2c_driver(tmp102_driver);
306
307 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
308 MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
309 MODULE_LICENSE("GPL");