blob: 812fbc00f69331b20f048150d4f9719438272970 [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 *
Michael Wallea95da112016-07-25 11:12:06 +020010 * TODO: SPI, use power-down mode for suspend?, interrupt handling?
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010011 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/err.h>
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010017#include <linux/mutex.h>
18#include <linux/jiffies.h>
19#include <linux/i2c.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010023
24#define ADT7411_REG_INT_TEMP_VDD_LSB 0x03
25#define ADT7411_REG_EXT_TEMP_AIN14_LSB 0x04
26#define ADT7411_REG_VDD_MSB 0x06
27#define ADT7411_REG_INT_TEMP_MSB 0x07
28#define ADT7411_REG_EXT_TEMP_AIN1_MSB 0x08
29
30#define ADT7411_REG_CFG1 0x18
31#define ADT7411_CFG1_START_MONITOR (1 << 0)
Michael Walle601807b2016-07-25 11:12:05 +020032#define ADT7411_CFG1_RESERVED_BIT1 (1 << 1)
Michael Wallea95da112016-07-25 11:12:06 +020033#define ADT7411_CFG1_EXT_TDM (1 << 2)
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;
Michael Wallea95da112016-07-25 11:12:06 +020060 bool use_ext_temp;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +010061};
62
63/*
64 * When reading a register containing (up to 4) lsb, all associated
65 * msb-registers get locked by the hardware. After _one_ of those msb is read,
66 * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
67 * is protected here with a mutex, too.
68 */
69static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
70 u8 msb_reg, u8 lsb_shift)
71{
72 struct adt7411_data *data = i2c_get_clientdata(client);
73 int val, tmp;
74
75 mutex_lock(&data->device_lock);
76
77 val = i2c_smbus_read_byte_data(client, lsb_reg);
78 if (val < 0)
79 goto exit_unlock;
80
81 tmp = (val >> lsb_shift) & 3;
82 val = i2c_smbus_read_byte_data(client, msb_reg);
83
84 if (val >= 0)
85 val = (val << 2) | tmp;
86
87 exit_unlock:
88 mutex_unlock(&data->device_lock);
89
90 return val;
91}
92
93static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
94 bool flag)
95{
96 struct adt7411_data *data = i2c_get_clientdata(client);
97 int ret, val;
98
99 mutex_lock(&data->device_lock);
100
101 ret = i2c_smbus_read_byte_data(client, reg);
102 if (ret < 0)
103 goto exit_unlock;
104
105 if (flag)
106 val = ret | bit;
107 else
108 val = ret & ~bit;
109
110 ret = i2c_smbus_write_byte_data(client, reg, val);
111
112 exit_unlock:
113 mutex_unlock(&data->device_lock);
114 return ret;
115}
116
117static ssize_t adt7411_show_vdd(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
Axel Lineac83cd2014-07-19 22:42:33 +0800120 struct adt7411_data *data = dev_get_drvdata(dev);
121 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100122 int ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
123 ADT7411_REG_VDD_MSB, 2);
124
125 return ret < 0 ? ret : sprintf(buf, "%u\n", ret * 7000 / 1024);
126}
127
128static ssize_t adt7411_show_temp(struct device *dev,
129 struct device_attribute *attr, char *buf)
130{
Michael Wallea95da112016-07-25 11:12:06 +0200131 int nr = to_sensor_dev_attr(attr)->index;
Axel Lineac83cd2014-07-19 22:42:33 +0800132 struct adt7411_data *data = dev_get_drvdata(dev);
133 struct i2c_client *client = data->client;
Michael Wallea95da112016-07-25 11:12:06 +0200134 int val;
135 struct {
136 u8 low;
137 u8 high;
138 } reg[2] = {
139 { ADT7411_REG_INT_TEMP_VDD_LSB, ADT7411_REG_INT_TEMP_MSB },
140 { ADT7411_REG_EXT_TEMP_AIN14_LSB,
141 ADT7411_REG_EXT_TEMP_AIN1_MSB },
142 };
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100143
Michael Wallea95da112016-07-25 11:12:06 +0200144 val = adt7411_read_10_bit(client, reg[nr].low, reg[nr].high, 0);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100145 if (val < 0)
146 return val;
147
148 val = val & 0x200 ? val - 0x400 : val; /* 10 bit signed */
149
150 return sprintf(buf, "%d\n", val * 250);
151}
152
153static ssize_t adt7411_show_input(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
156 int nr = to_sensor_dev_attr(attr)->index;
Axel Lineac83cd2014-07-19 22:42:33 +0800157 struct adt7411_data *data = dev_get_drvdata(dev);
158 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100159 int val;
160 u8 lsb_reg, lsb_shift;
161
Wolfram Sang23244982010-03-05 22:17:23 +0100162 mutex_lock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100163 if (time_after_eq(jiffies, data->next_update)) {
164 val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
165 if (val < 0)
Wolfram Sang23244982010-03-05 22:17:23 +0100166 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100167
Wolfram Sang23244982010-03-05 22:17:23 +0100168 if (val & ADT7411_CFG3_REF_VDD) {
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100169 val = adt7411_read_10_bit(client,
170 ADT7411_REG_INT_TEMP_VDD_LSB,
171 ADT7411_REG_VDD_MSB, 2);
172 if (val < 0)
Wolfram Sang23244982010-03-05 22:17:23 +0100173 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100174
175 data->vref_cached = val * 7000 / 1024;
176 } else {
177 data->vref_cached = 2250;
178 }
179
180 data->next_update = jiffies + HZ;
181 }
182
183 lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
184 lsb_shift = 2 * (nr & 0x03);
185 val = adt7411_read_10_bit(client, lsb_reg,
186 ADT7411_REG_EXT_TEMP_AIN1_MSB + nr, lsb_shift);
Wolfram Sang23244982010-03-05 22:17:23 +0100187 if (val < 0)
188 goto exit_unlock;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100189
Wolfram Sang23244982010-03-05 22:17:23 +0100190 val = sprintf(buf, "%u\n", val * data->vref_cached / 1024);
191 exit_unlock:
192 mutex_unlock(&data->update_lock);
193 return val;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100194}
195
196static ssize_t adt7411_show_bit(struct device *dev,
197 struct device_attribute *attr, char *buf)
198{
199 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
Axel Lineac83cd2014-07-19 22:42:33 +0800200 struct adt7411_data *data = dev_get_drvdata(dev);
201 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100202 int ret = i2c_smbus_read_byte_data(client, attr2->index);
203
204 return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
205}
206
207static ssize_t adt7411_set_bit(struct device *dev,
208 struct device_attribute *attr, const char *buf,
209 size_t count)
210{
211 struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
Axel Lineac83cd2014-07-19 22:42:33 +0800212 struct adt7411_data *data = dev_get_drvdata(dev);
213 struct i2c_client *client = data->client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100214 int ret;
215 unsigned long flag;
216
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100217 ret = kstrtoul(buf, 0, &flag);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100218 if (ret || flag > 1)
219 return -EINVAL;
220
221 ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
222
223 /* force update */
Wolfram Sang23244982010-03-05 22:17:23 +0100224 mutex_lock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100225 data->next_update = jiffies;
Wolfram Sang23244982010-03-05 22:17:23 +0100226 mutex_unlock(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100227
228 return ret < 0 ? ret : count;
229}
230
Michael Wallea95da112016-07-25 11:12:06 +0200231
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100232#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
233 SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
234 adt7411_set_bit, __bit, __reg)
235
Michael Wallea95da112016-07-25 11:12:06 +0200236static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7411_show_temp, NULL, 0);
237static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, adt7411_show_temp, NULL, 1);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100238static DEVICE_ATTR(in0_input, S_IRUGO, adt7411_show_vdd, NULL);
239static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, adt7411_show_input, NULL, 0);
240static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, adt7411_show_input, NULL, 1);
241static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, adt7411_show_input, NULL, 2);
242static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, adt7411_show_input, NULL, 3);
243static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, adt7411_show_input, NULL, 4);
244static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, adt7411_show_input, NULL, 5);
245static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, adt7411_show_input, NULL, 6);
246static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, adt7411_show_input, NULL, 7);
247static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
248static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
249static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
250
251static struct attribute *adt7411_attrs[] = {
Michael Wallea95da112016-07-25 11:12:06 +0200252 &sensor_dev_attr_temp1_input.dev_attr.attr,
253 &sensor_dev_attr_temp2_input.dev_attr.attr,
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100254 &dev_attr_in0_input.attr,
255 &sensor_dev_attr_in1_input.dev_attr.attr,
256 &sensor_dev_attr_in2_input.dev_attr.attr,
257 &sensor_dev_attr_in3_input.dev_attr.attr,
258 &sensor_dev_attr_in4_input.dev_attr.attr,
259 &sensor_dev_attr_in5_input.dev_attr.attr,
260 &sensor_dev_attr_in6_input.dev_attr.attr,
261 &sensor_dev_attr_in7_input.dev_attr.attr,
262 &sensor_dev_attr_in8_input.dev_attr.attr,
263 &sensor_dev_attr_no_average.dev_attr.attr,
264 &sensor_dev_attr_fast_sampling.dev_attr.attr,
265 &sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
266 NULL
267};
268
Michael Wallea95da112016-07-25 11:12:06 +0200269static umode_t adt7411_attrs_visible(struct kobject *kobj,
270 struct attribute *attr, int index)
271{
272 struct device *dev = container_of(kobj, struct device, kobj);
273 struct adt7411_data *data = dev_get_drvdata(dev);
274 bool visible = true;
275
276 if (attr == &sensor_dev_attr_temp2_input.dev_attr.attr)
277 visible = data->use_ext_temp;
278 else if (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
279 attr == &sensor_dev_attr_in2_input.dev_attr.attr)
280 visible = !data->use_ext_temp;
281
282 return visible ? attr->mode : 0;
283}
284
285static const struct attribute_group adt7411_group = {
286 .attrs = adt7411_attrs,
287 .is_visible = adt7411_attrs_visible,
288};
289__ATTRIBUTE_GROUPS(adt7411);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100290
291static int adt7411_detect(struct i2c_client *client,
292 struct i2c_board_info *info)
293{
294 int val;
295
296 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
297 return -ENODEV;
298
299 val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
300 if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800301 dev_dbg(&client->dev,
302 "Wrong manufacturer ID. Got %d, expected %d\n",
303 val, ADT7411_MANUFACTURER_ID);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100304 return -ENODEV;
305 }
306
307 val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
308 if (val < 0 || val != ADT7411_DEVICE_ID) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800309 dev_dbg(&client->dev,
310 "Wrong device ID. Got %d, expected %d\n",
311 val, ADT7411_DEVICE_ID);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100312 return -ENODEV;
313 }
314
315 strlcpy(info->type, "adt7411", I2C_NAME_SIZE);
316
317 return 0;
318}
319
Michael Walle601807b2016-07-25 11:12:05 +0200320static int adt7411_init_device(struct adt7411_data *data)
321{
322 int ret;
323 u8 val;
324
325 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
326 if (ret < 0)
327 return ret;
328
329 /*
330 * We must only write zero to bit 1 and bit 2 and only one to bit 3
331 * according to the datasheet.
332 */
333 val = ret;
334 val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
335 val |= ADT7411_CFG3_RESERVED_BIT3;
336
337 ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
338 if (ret < 0)
339 return ret;
340
341 ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
342 if (ret < 0)
343 return ret;
344
Michael Wallea95da112016-07-25 11:12:06 +0200345 data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
346
Michael Walle601807b2016-07-25 11:12:05 +0200347 /*
348 * We must only write zero to bit 1 and only one to bit 3 according to
349 * the datasheet.
350 */
351 val = ret;
352 val &= ~ADT7411_CFG1_RESERVED_BIT1;
353 val |= ADT7411_CFG1_RESERVED_BIT3;
354
355 /* enable monitoring */
356 val |= ADT7411_CFG1_START_MONITOR;
357
358 return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
359}
360
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500361static int adt7411_probe(struct i2c_client *client,
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100362 const struct i2c_device_id *id)
363{
Axel Lineac83cd2014-07-19 22:42:33 +0800364 struct device *dev = &client->dev;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100365 struct adt7411_data *data;
Axel Lineac83cd2014-07-19 22:42:33 +0800366 struct device *hwmon_dev;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100367 int ret;
368
Axel Lineac83cd2014-07-19 22:42:33 +0800369 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100370 if (!data)
371 return -ENOMEM;
372
373 i2c_set_clientdata(client, data);
Axel Lineac83cd2014-07-19 22:42:33 +0800374 data->client = client;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100375 mutex_init(&data->device_lock);
Wolfram Sang23244982010-03-05 22:17:23 +0100376 mutex_init(&data->update_lock);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100377
Michael Walle601807b2016-07-25 11:12:05 +0200378 ret = adt7411_init_device(data);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100379 if (ret < 0)
Guenter Roeck65ec17b2012-06-02 09:58:01 -0700380 return ret;
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100381
382 /* force update on first occasion */
383 data->next_update = jiffies;
384
Axel Lineac83cd2014-07-19 22:42:33 +0800385 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
386 data,
387 adt7411_groups);
388 return PTR_ERR_OR_ZERO(hwmon_dev);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100389}
390
391static const struct i2c_device_id adt7411_id[] = {
392 { "adt7411", 0 },
393 { }
394};
axel lina7254d62011-02-24 02:22:01 +0000395MODULE_DEVICE_TABLE(i2c, adt7411_id);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100396
397static struct i2c_driver adt7411_driver = {
398 .driver = {
399 .name = "adt7411",
400 },
401 .probe = adt7411_probe,
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100402 .id_table = adt7411_id,
403 .detect = adt7411_detect,
404 .address_list = normal_i2c,
405 .class = I2C_CLASS_HWMON,
406};
407
Axel Linf0967ee2012-01-20 15:38:18 +0800408module_i2c_driver(adt7411_driver);
Wolfram Sangd84ca5b2010-03-05 22:17:23 +0100409
410MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de> and "
411 "Wolfram Sang <w.sang@pengutronix.de>");
412MODULE_DESCRIPTION("ADT7411 driver");
413MODULE_LICENSE("GPL v2");