u64_stats: Introduce IRQs disabled helpers
[cascardo/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         err = devm_add_action_or_reset(dev, tmp102_restore_config, tmp102);
231         if (err)
232                 return err;
233
234         regval &= ~TMP102_CONFIG_CLEAR;
235         regval |= TMP102_CONFIG_SET;
236
237         err = regmap_write(tmp102->regmap, TMP102_CONF_REG, regval);
238         if (err < 0) {
239                 dev_err(dev, "error writing config register\n");
240                 return err;
241         }
242
243         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
252         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
253                                                            tmp102,
254                                                            tmp102_groups);
255         if (IS_ERR(hwmon_dev)) {
256                 dev_dbg(dev, "unable to register hwmon device\n");
257                 return PTR_ERR(hwmon_dev);
258         }
259         devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
260                                              &tmp102_of_thermal_ops);
261
262         dev_info(dev, "initialized\n");
263
264         return 0;
265 }
266
267 #ifdef CONFIG_PM_SLEEP
268 static int tmp102_suspend(struct device *dev)
269 {
270         struct i2c_client *client = to_i2c_client(dev);
271         struct tmp102 *tmp102 = i2c_get_clientdata(client);
272
273         return regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
274                                   TMP102_CONF_SD, TMP102_CONF_SD);
275 }
276
277 static int tmp102_resume(struct device *dev)
278 {
279         struct i2c_client *client = to_i2c_client(dev);
280         struct tmp102 *tmp102 = i2c_get_clientdata(client);
281         int err;
282
283         err = regmap_update_bits(tmp102->regmap, TMP102_CONF_REG,
284                                  TMP102_CONF_SD, 0);
285
286         tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
287
288         return err;
289 }
290 #endif /* CONFIG_PM */
291
292 static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
293
294 static const struct i2c_device_id tmp102_id[] = {
295         { "tmp102", 0 },
296         { }
297 };
298 MODULE_DEVICE_TABLE(i2c, tmp102_id);
299
300 static struct i2c_driver tmp102_driver = {
301         .driver.name    = DRIVER_NAME,
302         .driver.pm      = &tmp102_dev_pm_ops,
303         .probe          = tmp102_probe,
304         .id_table       = tmp102_id,
305 };
306
307 module_i2c_driver(tmp102_driver);
308
309 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
310 MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
311 MODULE_LICENSE("GPL");