blob: fc1e65a263a47a902e8dc87f416ca68c08f4bed2 [file] [log] [blame]
Wolfram Sangd84ca5b2010-03-05 22:17:23 +01001/*
2 * Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
3 *
4 * Copyright (C) 2008, 2010 Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO: SPI, support for external temperature sensor
Frans Meulenbroekse6a83db2012-01-08 19:34:12 +010011 * use power-down mode for suspend?, interrupt handling?
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010012 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/err.h>
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010018#include <linux/mutex.h>
19#include <linux/jiffies.h>
20#include <linux/i2c.h>
21#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010024
25#define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
26#define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
27#define ADT7411_REG_VDD_MSB 0x06
28#define ADT7411_REG_INT_TEMP_MSB 0x07
29#define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
30
31#define ADT7411_REG_CFG1 0x18
32#define ADT7411_CFG1_START_MONITOR (1 << 0)
Michael Walle601807b2016-07-25 11:12:05 +020033#define ADT7411_CFG1_RESERVED_BIT1 (1 << 1)
Michael Walleb53893a2016-07-19 16:43:26 +020034#define ADT7411_CFG1_RESERVED_BIT3 (1 << 3)
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010035
36#define ADT7411_REG_CFG2 0x19
37#define ADT7411_CFG2_DISABLE_AVG (1 << 5)
38
39#define ADT7411_REG_CFG3 0x1a
40#define ADT7411_CFG3_ADC_CLK_225 (1 << 0)
Michael Walle601807b2016-07-25 11:12:05 +020041#define ADT7411_CFG3_RESERVED_BIT1 (1 << 1)
42#define ADT7411_CFG3_RESERVED_BIT2 (1 << 2)
43#define ADT7411_CFG3_RESERVED_BIT3 (1 << 3)
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010044#define ADT7411_CFG3_REF_VDD (1 << 4)
45
46#define ADT7411_REG_DEVICE_ID 0x4d
47#define ADT7411_REG_MANUFACTURER_ID 0x4e
48
49#define ADT7411_DEVICE_ID 0x2
50#define ADT7411_MANUFACTURER_ID 0x41
51
52static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
53
54struct adt7411_data {
55 struct mutex device_lock; /* for "atomic" device accesses */
Wolfram Sang23244982010-03-05 22:17:23 +010056 struct mutex update_lock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010057 unsigned long next_update;
58 int vref_cached;
Axel Lineac83cd2014-07-19 22:42:33 +080059 struct i2c_client *client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010060};
61
62/*
63 * When reading a register containing (up to 4) lsb, all associated
64 * msb-registers get locked by the hardware. After _one_ of those msb is read,
65 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
66 * is protected here with a mutex, too.
67 */
68static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
69 u8 msb_reg, u8 lsb_shift)
70{
71 struct adt7411_data *data = i2c_get_clientdata(client);
72 int val, tmp;
73
74 mutex_lock(&data->device_lock);
75
76 val = i2c_smbus_read_byte_data(client, lsb_reg);
77 if (val < 0)
78 goto exit_unlock;
79
80 tmp = (val >> lsb_shift) & 3;
81 val = i2c_smbus_read_byte_data(client, msb_reg);
82
83 if (val >= 0)
84 val = (val << 2) | tmp;
85
86 exit_unlock:
87 mutex_unlock(&data->device_lock);
88
89 return val;
90}
91
92static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
93 bool flag)
94{
95 struct adt7411_data *data = i2c_get_clientdata(client);
96 int ret, val;
97
98 mutex_lock(&data->device_lock);
99
100 ret = i2c_smbus_read_byte_data(client, reg);
101 if (ret < 0)
102 goto exit_unlock;
103
104 if (flag)
105 val = ret | bit;
106 else
107 val = ret & ~bit;
108
109 ret = i2c_smbus_write_byte_data(client, reg, val);
110
111 exit_unlock:
112 mutex_unlock(&data->device_lock);
113 return ret;
114}
115
116static ssize_t adt7411_show_vdd(struct device *dev,
117 struct device_attribute *attr, char *buf)
118{
Axel Lineac83cd2014-07-19 22:42:33 +0800119 struct adt7411_data *data = dev_get_drvdata(dev);
120 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100121 int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
122 ADT7411_REG_VDD_MSB, 2);
123
124 return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
125}
126
127static ssize_t adt7411_show_temp(struct device *dev,
128 struct device_attribute *attr, char *buf)
129{
Axel Lineac83cd2014-07-19 22:42:33 +0800130 struct adt7411_data *data = dev_get_drvdata(dev);
131 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100132 int val = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
133 ADT7411_REG_INT_TEMP_MSB, 0);
134
135 if (val < 0)
136 return val;
137
138 val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
139
140 return sprintf(buf, "%d\n", val * 250);
141}
142
143static ssize_t adt7411_show_input(struct device *dev,
144 struct device_attribute *attr, char *buf)
145{
146 int nr = to_sensor_dev_attr(attr)->index;
Axel Lineac83cd2014-07-19 22:42:33 +0800147 struct adt7411_data *data = dev_get_drvdata(dev);
148 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100149 int val;
150 u8 lsb_reg, lsb_shift;
151
Wolfram Sang23244982010-03-05 22:17:23 +0100152 mutex_lock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100153 if (time_after_eq(jiffies, data->next_update)) {
154 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
155 if (val < 0)
Wolfram Sang23244982010-03-05 22:17:23 +0100156 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100157
Wolfram Sang23244982010-03-05 22:17:23 +0100158 if (val & ADT7411_CFG3_REF_VDD) {
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100159 val = adt7411_read_10_bit(client,
160 ADT7411_REG_INT_TEMP_VDD_LSB,
161 ADT7411_REG_VDD_MSB, 2);
162 if (val < 0)
Wolfram Sang23244982010-03-05 22:17:23 +0100163 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100164
165 data->vref_cached = val * 7000 / 1024;
166 } else {
167 data->vref_cached = 2250;
168 }
169
170 data->next_update = jiffies + HZ;
171 }
172
173 lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
174 lsb_shift = 2 * (nr & 0x03);
175 val = adt7411_read_10_bit(client, lsb_reg,
176 ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
Wolfram Sang23244982010-03-05 22:17:23 +0100177 if (val < 0)
178 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100179
Wolfram Sang23244982010-03-05 22:17:23 +0100180 val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
181 exit_unlock:
182 mutex_unlock(&data->update_lock);
183 return val;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100184}
185
186static ssize_t adt7411_show_bit(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
189 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
Axel Lineac83cd2014-07-19 22:42:33 +0800190 struct adt7411_data *data = dev_get_drvdata(dev);
191 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100192 int ret = i2c_smbus_read_byte_data(client, attr2->index);
193
194 return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
195}
196
197static ssize_t adt7411_set_bit(struct device *dev,
198 struct device_attribute *attr, const char *buf,
199 size_t count)
200{
201 struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
Axel Lineac83cd2014-07-19 22:42:33 +0800202 struct adt7411_data *data = dev_get_drvdata(dev);
203 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100204 int ret;
205 unsigned long flag;
206
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100207 ret = kstrtoul(buf, 0, &flag);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100208 if (ret || flag > 1)
209 return -EINVAL;
210
211 ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
212
213 /* force update */
Wolfram Sang23244982010-03-05 22:17:23 +0100214 mutex_lock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100215 data->next_update = jiffies;
Wolfram Sang23244982010-03-05 22:17:23 +0100216 mutex_unlock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100217
218 return ret < 0 ? ret : count;
219}
220
221#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
222 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
223 adt7411_set_bit, __bit, __reg)
224
225static DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL);
226static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
227static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
228static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
229static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
230static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
231static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
232static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
233static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
234static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
235static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
236static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
237static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
238
239static struct attribute *adt7411_attrs[] = {
240 &dev_attr_temp1_input.attr,
241 &dev_attr_in0_input.attr,
242 &sensor_dev_attr_in1_input.dev_attr.attr,
243 &sensor_dev_attr_in2_input.dev_attr.attr,
244 &sensor_dev_attr_in3_input.dev_attr.attr,
245 &sensor_dev_attr_in4_input.dev_attr.attr,
246 &sensor_dev_attr_in5_input.dev_attr.attr,
247 &sensor_dev_attr_in6_input.dev_attr.attr,
248 &sensor_dev_attr_in7_input.dev_attr.attr,
249 &sensor_dev_attr_in8_input.dev_attr.attr,
250 &sensor_dev_attr_no_average.dev_attr.attr,
251 &sensor_dev_attr_fast_sampling.dev_attr.attr,
252 &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
253 NULL
254};
255
Axel Lineac83cd2014-07-19 22:42:33 +0800256ATTRIBUTE_GROUPS(adt7411);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100257
258static int adt7411_detect(struct i2c_client *client,
259 struct i2c_board_info *info)
260{
261 int val;
262
263 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
264 return -ENODEV;
265
266 val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
267 if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800268 dev_dbg(&client->dev,
269 "Wrong manufacturer ID. Got %d, expected %d\n",
270 val, ADT7411_MANUFACTURER_ID);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100271 return -ENODEV;
272 }
273
274 val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
275 if (val < 0 || val != ADT7411_DEVICE_ID) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800276 dev_dbg(&client->dev,
277 "Wrong device ID. Got %d, expected %d\n",
278 val, ADT7411_DEVICE_ID);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100279 return -ENODEV;
280 }
281
282 strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
283
284 return 0;
285}
286
Michael Walle601807b2016-07-25 11:12:05 +0200287static int adt7411_init_device(struct adt7411_data *data)
288{
289 int ret;
290 u8 val;
291
292 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
293 if (ret < 0)
294 return ret;
295
296 /*
297 * We must only write zero to bit 1 and bit 2 and only one to bit 3
298 * according to the datasheet.
299 */
300 val = ret;
301 val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
302 val |= ADT7411_CFG3_RESERVED_BIT3;
303
304 ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
305 if (ret < 0)
306 return ret;
307
308 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
309 if (ret < 0)
310 return ret;
311
312 /*
313 * We must only write zero to bit 1 and only one to bit 3 according to
314 * the datasheet.
315 */
316 val = ret;
317 val &= ~ADT7411_CFG1_RESERVED_BIT1;
318 val |= ADT7411_CFG1_RESERVED_BIT3;
319
320 /* enable monitoring */
321 val |= ADT7411_CFG1_START_MONITOR;
322
323 return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
324}
325
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500326static int adt7411_probe(struct i2c_client *client,
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100327 const struct i2c_device_id *id)
328{
Axel Lineac83cd2014-07-19 22:42:33 +0800329 struct device *dev = &client->dev;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100330 struct adt7411_data *data;
Axel Lineac83cd2014-07-19 22:42:33 +0800331 struct device *hwmon_dev;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100332 int ret;
333
Axel Lineac83cd2014-07-19 22:42:33 +0800334 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100335 if (!data)
336 return -ENOMEM;
337
338 i2c_set_clientdata(client, data);
Axel Lineac83cd2014-07-19 22:42:33 +0800339 data->client = client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100340 mutex_init(&data->device_lock);
Wolfram Sang23244982010-03-05 22:17:23 +0100341 mutex_init(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100342
Michael Walle601807b2016-07-25 11:12:05 +0200343 ret = adt7411_init_device(data);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100344 if (ret < 0)
Guenter Roeck65ec17b2012-06-02 09:58:01 -0700345 return ret;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100346
347 /* force update on first occasion */
348 data->next_update = jiffies;
349
Axel Lineac83cd2014-07-19 22:42:33 +0800350 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
351 data,
352 adt7411_groups);
353 return PTR_ERR_OR_ZERO(hwmon_dev);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100354}
355
356static const struct i2c_device_id adt7411_id[] = {
357 { "adt7411", 0 },
358 { }
359};
axel lina7254d62011-02-24 02:22:01 +0000360MODULE_DEVICE_TABLE(i2c, adt7411_id);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100361
362static struct i2c_driver adt7411_driver = {
363 .driver = {
364 .name = "adt7411",
365 },
366 .probe = adt7411_probe,
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100367 .id_table = adt7411_id,
368 .detect = adt7411_detect,
369 .address_list = normal_i2c,
370 .class = I2C_CLASS_HWMON,
371};
372
Axel Linf0967ee2012-01-20 15:38:18 +0800373module_i2c_driver(adt7411_driver);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100374
375MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
376 "Wolfram Sang <w.sang@pengutronix.de>");
377MODULE_DESCRIPTION("ADT7411 driver");
378MODULE_LICENSE("GPL v2");