hwmon: (tmp102) Rework chip configuration
[linux-2.6-block.git] / drivers / hwmon / tmp102.c
CommitLineData
cff37c9e 1/* Texas Instruments TMP102 SMBus temperature sensor driver
beb1b6bb 2 *
cff37c9e 3 * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
beb1b6bb
SK
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.
beb1b6bb
SK
14 */
15
3d8f7a89 16#include <linux/delay.h>
beb1b6bb
SK
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>
cff37c9e 25#include <linux/device.h>
dcd8f392 26#include <linux/jiffies.h>
6a027523
EV
27#include <linux/thermal.h>
28#include <linux/of.h>
beb1b6bb
SK
29
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
a9f92ccf
GR
50#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
3d8f7a89
GR
61#define CONVERSION_TIME_MS 35 /* in milli-seconds */
62
beb1b6bb 63struct tmp102 {
ad9beea4 64 struct i2c_client *client;
beb1b6bb 65 struct mutex lock;
38806bda 66 u16 config_orig;
beb1b6bb 67 unsigned long last_update;
3d8f7a89
GR
68 unsigned long ready_time;
69 bool valid;
beb1b6bb
SK
70 int temp[3];
71};
72
cff37c9e
JD
73/* convert left adjusted 13-bit TMP102 register value to milliCelsius */
74static inline int tmp102_reg_to_mC(s16 val)
beb1b6bb 75{
cff37c9e 76 return ((val & ~0x01) * 1000) / 128;
beb1b6bb
SK
77}
78
cff37c9e
JD
79/* convert milliCelsius to left adjusted 13-bit TMP102 register value */
80static inline u16 tmp102_mC_to_reg(int val)
beb1b6bb
SK
81{
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
3d8f7a89 91static void tmp102_update_device(struct device *dev)
beb1b6bb 92{
ad9beea4
GR
93 struct tmp102 *tmp102 = dev_get_drvdata(dev);
94 struct i2c_client *client = tmp102->client;
beb1b6bb
SK
95
96 mutex_lock(&tmp102->lock);
3d8f7a89
GR
97 if (!tmp102->valid ||
98 time_after(jiffies, tmp102->last_update + HZ / 3)) {
beb1b6bb
SK
99 int i;
100 for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
90f4102c
JD
101 int status = i2c_smbus_read_word_swapped(client,
102 tmp102_reg[i]);
beb1b6bb
SK
103 if (status > -1)
104 tmp102->temp[i] = tmp102_reg_to_mC(status);
105 }
106 tmp102->last_update = jiffies;
3d8f7a89 107 tmp102->valid = true;
beb1b6bb
SK
108 }
109 mutex_unlock(&tmp102->lock);
beb1b6bb
SK
110}
111
17e8351a 112static int tmp102_read_temp(void *dev, int *temp)
6a027523 113{
3d8f7a89 114 struct tmp102 *tmp102 = dev_get_drvdata(dev);
6a027523 115
3d8f7a89 116 if (time_before(jiffies, tmp102->ready_time)) {
00917b5c
NM
117 dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
118 return -EAGAIN;
119 }
120
3d8f7a89
GR
121 tmp102_update_device(dev);
122
6a027523
EV
123 *temp = tmp102->temp[0];
124
125 return 0;
126}
127
beb1b6bb
SK
128static 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);
3d8f7a89 133 struct tmp102 *tmp102 = dev_get_drvdata(dev);
beb1b6bb 134
3d8f7a89 135 if (time_before(jiffies, tmp102->ready_time))
00917b5c
NM
136 return -EAGAIN;
137
3d8f7a89
GR
138 tmp102_update_device(dev);
139
beb1b6bb
SK
140 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);
ad9beea4
GR
148 struct tmp102 *tmp102 = dev_get_drvdata(dev);
149 struct i2c_client *client = tmp102->client;
beb1b6bb 150 long val;
cff37c9e 151 int status;
beb1b6bb 152
179c4fdb 153 if (kstrtol(buf, 10, &val) < 0)
beb1b6bb 154 return -EINVAL;
2a844c14 155 val = clamp_val(val, -256000, 255000);
cff37c9e 156
beb1b6bb 157 mutex_lock(&tmp102->lock);
cff37c9e 158 tmp102->temp[sda->index] = val;
90f4102c
JD
159 status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
160 tmp102_mC_to_reg(val));
beb1b6bb
SK
161 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
ad9beea4 173static struct attribute *tmp102_attrs[] = {
beb1b6bb
SK
174 &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};
ad9beea4 179ATTRIBUTE_GROUPS(tmp102);
beb1b6bb 180
2251aef6
EV
181static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
182 .get_temp = tmp102_read_temp,
183};
184
b17ea1ca
GR
185static 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
6c931ae1 194static int tmp102_probe(struct i2c_client *client,
beb1b6bb
SK
195 const struct i2c_device_id *id)
196{
fbd9af16 197 struct device *dev = &client->dev;
ad9beea4 198 struct device *hwmon_dev;
beb1b6bb
SK
199 struct tmp102 *tmp102;
200 int status;
201
cff37c9e 202 if (!i2c_check_functionality(client->adapter,
beb1b6bb 203 I2C_FUNC_SMBUS_WORD_DATA)) {
fbd9af16 204 dev_err(dev,
b55f3757 205 "adapter doesn't support SMBus word transactions\n");
beb1b6bb
SK
206 return -ENODEV;
207 }
208
fbd9af16 209 tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
f511a21f 210 if (!tmp102)
beb1b6bb 211 return -ENOMEM;
f511a21f 212
beb1b6bb 213 i2c_set_clientdata(client, tmp102);
ad9beea4 214 tmp102->client = client;
beb1b6bb 215
90f4102c 216 status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
38806bda 217 if (status < 0) {
fbd9af16 218 dev_err(dev, "error reading config register\n");
f511a21f 219 return status;
38806bda 220 }
a9f92ccf
GR
221
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
38806bda 228 tmp102->config_orig = status;
b17ea1ca
GR
229
230 devm_add_action(dev, tmp102_restore_config, tmp102);
231
a9f92ccf
GR
232 status &= ~TMP102_CONFIG_CLEAR;
233 status |= TMP102_CONFIG_SET;
234
235 status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, status);
cff37c9e 236 if (status < 0) {
fbd9af16 237 dev_err(dev, "error writing config register\n");
b17ea1ca 238 return status;
cff37c9e 239 }
3d8f7a89 240
beb1b6bb
SK
241 mutex_init(&tmp102->lock);
242
3d8f7a89
GR
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
b17ea1ca
GR
252 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
253 tmp102,
254 tmp102_groups);
ad9beea4 255 if (IS_ERR(hwmon_dev)) {
fbd9af16 256 dev_dbg(dev, "unable to register hwmon device\n");
b17ea1ca 257 return PTR_ERR(hwmon_dev);
beb1b6bb 258 }
51b77fd7
EV
259 devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
260 &tmp102_of_thermal_ops);
6a027523 261
fbd9af16 262 dev_info(dev, "initialized\n");
beb1b6bb 263
beb1b6bb
SK
264 return 0;
265}
266
dd378b1b 267#ifdef CONFIG_PM_SLEEP
beb1b6bb
SK
268static int tmp102_suspend(struct device *dev)
269{
270 struct i2c_client *client = to_i2c_client(dev);
8d4dee98 271 int config;
beb1b6bb 272
90f4102c 273 config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
8d4dee98
JD
274 if (config < 0)
275 return config;
beb1b6bb 276
8d4dee98 277 config |= TMP102_CONF_SD;
90f4102c 278 return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
beb1b6bb
SK
279}
280
281static int tmp102_resume(struct device *dev)
282{
283 struct i2c_client *client = to_i2c_client(dev);
3d8f7a89 284 struct tmp102 *tmp102 = i2c_get_clientdata(client);
8d4dee98 285 int config;
beb1b6bb 286
90f4102c 287 config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
8d4dee98
JD
288 if (config < 0)
289 return config;
beb1b6bb 290
3d8f7a89
GR
291 tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
292
8d4dee98 293 config &= ~TMP102_CONF_SD;
90f4102c 294 return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
beb1b6bb 295}
beb1b6bb
SK
296#endif /* CONFIG_PM */
297
dd378b1b
GS
298static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
299
beb1b6bb 300static const struct i2c_device_id tmp102_id[] = {
cff37c9e 301 { "tmp102", 0 },
beb1b6bb
SK
302 { }
303};
cff37c9e 304MODULE_DEVICE_TABLE(i2c, tmp102_id);
beb1b6bb
SK
305
306static struct i2c_driver tmp102_driver = {
307 .driver.name = DRIVER_NAME,
dd378b1b 308 .driver.pm = &tmp102_dev_pm_ops,
beb1b6bb 309 .probe = tmp102_probe,
beb1b6bb 310 .id_table = tmp102_id,
beb1b6bb
SK
311};
312
f0967eea 313module_i2c_driver(tmp102_driver);
beb1b6bb 314
beb1b6bb
SK
315MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
316MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
317MODULE_LICENSE("GPL");