blob: 4aeba29b4629c552164146cfc0bf87812440a1f7 [file] [log] [blame]
Alessandro Zummo2d8dd652007-05-08 17:22:02 +02001/*
2 * An hwmon driver for the Analog Devices AD7416/17/18
3 * Copyright (C) 2006-07 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * Based on lm75.c
8 * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License,
12 * as published by the Free Software Foundation - version 2.
13 */
14
15#include <linux/module.h>
16#include <linux/jiffies.h>
17#include <linux/i2c.h>
18#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
20#include <linux/err.h>
21#include <linux/mutex.h>
Linus Walleijf4c29652019-01-28 22:06:37 +010022#include <linux/of_device.h>
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020023#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020025
26#include "lm75.h"
27
Jean Delvare369932f2008-07-16 19:30:08 +020028#define DRV_VERSION "0.4"
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020029
Jean Delvare369932f2008-07-16 19:30:08 +020030enum chips { ad7416, ad7417, ad7418 };
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020031
32/* AD7418 registers */
33#define AD7418_REG_TEMP_IN 0x00
34#define AD7418_REG_CONF 0x01
35#define AD7418_REG_TEMP_HYST 0x02
36#define AD7418_REG_TEMP_OS 0x03
37#define AD7418_REG_ADC 0x04
38#define AD7418_REG_CONF2 0x05
39
40#define AD7418_REG_ADC_CH(x) ((x) << 5)
41#define AD7418_CH_TEMP AD7418_REG_ADC_CH(0)
42
43static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
44 AD7418_REG_TEMP_HYST,
45 AD7418_REG_TEMP_OS };
46
47struct ad7418_data {
Axel Lin337076f2014-07-01 22:40:29 +080048 struct i2c_client *client;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020049 enum chips type;
50 struct mutex lock;
51 int adc_max; /* number of ADC channels */
52 char valid;
53 unsigned long last_updated; /* In jiffies */
54 s16 temp[3]; /* Register values */
55 u16 in[4];
56};
57
Brandon Maier45034e42019-02-07 15:50:36 -060058static int ad7418_update_device(struct device *dev)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020059{
Axel Lin337076f2014-07-01 22:40:29 +080060 struct ad7418_data *data = dev_get_drvdata(dev);
61 struct i2c_client *client = data->client;
Brandon Maier45034e42019-02-07 15:50:36 -060062 s32 val;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020063
64 mutex_lock(&data->lock);
65
66 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
67 || !data->valid) {
68 u8 cfg;
69 int i, ch;
70
71 /* read config register and clear channel bits */
Brandon Maier45034e42019-02-07 15:50:36 -060072 val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
73 if (val < 0)
74 goto abort;
75
76 cfg = val;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020077 cfg &= 0x1F;
78
Brandon Maier45034e42019-02-07 15:50:36 -060079 val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020080 cfg | AD7418_CH_TEMP);
Brandon Maier45034e42019-02-07 15:50:36 -060081 if (val < 0)
82 goto abort;
83
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020084 udelay(30);
85
86 for (i = 0; i < 3; i++) {
Brandon Maier45034e42019-02-07 15:50:36 -060087 val = i2c_smbus_read_word_swapped(client,
88 AD7418_REG_TEMP[i]);
89 if (val < 0)
90 goto abort;
91
92 data->temp[i] = val;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020093 }
94
95 for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
Brandon Maier45034e42019-02-07 15:50:36 -060096 val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
Alessandro Zummo2d8dd652007-05-08 17:22:02 +020097 cfg | AD7418_REG_ADC_CH(ch));
Brandon Maier45034e42019-02-07 15:50:36 -060098 if (val < 0)
99 goto abort;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200100
101 udelay(15);
Brandon Maier45034e42019-02-07 15:50:36 -0600102 val = i2c_smbus_read_word_swapped(client,
103 AD7418_REG_ADC);
104 if (val < 0)
105 goto abort;
106
107 data->in[data->adc_max - 1 - i] = val;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200108 }
109
110 /* restore old configuration value */
Brandon Maier45034e42019-02-07 15:50:36 -0600111 val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
112 cfg);
113 if (val < 0)
114 goto abort;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200115
116 data->last_updated = jiffies;
117 data->valid = 1;
118 }
119
120 mutex_unlock(&data->lock);
Brandon Maier45034e42019-02-07 15:50:36 -0600121 return 0;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200122
Brandon Maier45034e42019-02-07 15:50:36 -0600123abort:
124 data->valid = 0;
125 mutex_unlock(&data->lock);
126 return val;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200127}
128
Guenter Roeck6fdc5d72018-12-10 14:01:59 -0800129static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
130 char *buf)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200131{
132 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Brandon Maier45034e42019-02-07 15:50:36 -0600133 struct ad7418_data *data = dev_get_drvdata(dev);
134 int ret;
135
136 ret = ad7418_update_device(dev);
137 if (ret < 0)
138 return ret;
139
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200140 return sprintf(buf, "%d\n",
141 LM75_TEMP_FROM_REG(data->temp[attr->index]));
142}
143
Guenter Roeck6fdc5d72018-12-10 14:01:59 -0800144static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200145 char *buf)
146{
147 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Brandon Maier45034e42019-02-07 15:50:36 -0600148 struct ad7418_data *data = dev_get_drvdata(dev);
149 int ret;
150
151 ret = ad7418_update_device(dev);
152 if (ret < 0)
153 return ret;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200154
155 return sprintf(buf, "%d\n",
156 ((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
157}
158
Guenter Roeck6fdc5d72018-12-10 14:01:59 -0800159static ssize_t temp_store(struct device *dev,
160 struct device_attribute *devattr, const char *buf,
161 size_t count)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200162{
163 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Axel Lin337076f2014-07-01 22:40:29 +0800164 struct ad7418_data *data = dev_get_drvdata(dev);
165 struct i2c_client *client = data->client;
Frans Meulenbroekse91aef222012-01-08 19:34:17 +0100166 long temp;
167 int ret = kstrtol(buf, 10, &temp);
168
169 if (ret < 0)
170 return ret;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200171
172 mutex_lock(&data->lock);
173 data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
Jean Delvare90f41022011-11-04 12:00:47 +0100174 i2c_smbus_write_word_swapped(client,
175 AD7418_REG_TEMP[attr->index],
176 data->temp[attr->index]);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200177 mutex_unlock(&data->lock);
178 return count;
179}
180
Guenter Roeck6fdc5d72018-12-10 14:01:59 -0800181static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
182static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
183static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200184
Guenter Roeck6fdc5d72018-12-10 14:01:59 -0800185static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
186static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
187static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
188static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200189
Axel Lin337076f2014-07-01 22:40:29 +0800190static struct attribute *ad7416_attrs[] = {
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200191 &sensor_dev_attr_temp1_max.dev_attr.attr,
192 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
193 &sensor_dev_attr_temp1_input.dev_attr.attr,
194 NULL
195};
Axel Lin337076f2014-07-01 22:40:29 +0800196ATTRIBUTE_GROUPS(ad7416);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200197
Axel Lin337076f2014-07-01 22:40:29 +0800198static struct attribute *ad7417_attrs[] = {
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200199 &sensor_dev_attr_temp1_max.dev_attr.attr,
200 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
201 &sensor_dev_attr_temp1_input.dev_attr.attr,
202 &sensor_dev_attr_in1_input.dev_attr.attr,
203 &sensor_dev_attr_in2_input.dev_attr.attr,
204 &sensor_dev_attr_in3_input.dev_attr.attr,
205 &sensor_dev_attr_in4_input.dev_attr.attr,
206 NULL
207};
Axel Lin337076f2014-07-01 22:40:29 +0800208ATTRIBUTE_GROUPS(ad7417);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200209
Axel Lin337076f2014-07-01 22:40:29 +0800210static struct attribute *ad7418_attrs[] = {
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200211 &sensor_dev_attr_temp1_max.dev_attr.attr,
212 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
213 &sensor_dev_attr_temp1_input.dev_attr.attr,
214 &sensor_dev_attr_in1_input.dev_attr.attr,
215 NULL
216};
Axel Lin337076f2014-07-01 22:40:29 +0800217ATTRIBUTE_GROUPS(ad7418);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200218
Axel Lin991763a2014-07-01 22:39:08 +0800219static void ad7418_init_client(struct i2c_client *client)
220{
221 struct ad7418_data *data = i2c_get_clientdata(client);
222
223 int reg = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
224 if (reg < 0) {
225 dev_err(&client->dev, "cannot read configuration register\n");
226 } else {
227 dev_info(&client->dev, "configuring for mode 1\n");
228 i2c_smbus_write_byte_data(client, AD7418_REG_CONF, reg & 0xfe);
229
230 if (data->type == ad7417 || data->type == ad7418)
231 i2c_smbus_write_byte_data(client,
232 AD7418_REG_CONF2, 0x00);
233 }
234}
235
Jean Delvare369932f2008-07-16 19:30:08 +0200236static int ad7418_probe(struct i2c_client *client,
237 const struct i2c_device_id *id)
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200238{
Axel Lin337076f2014-07-01 22:40:29 +0800239 struct device *dev = &client->dev;
Jean Delvare369932f2008-07-16 19:30:08 +0200240 struct i2c_adapter *adapter = client->adapter;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200241 struct ad7418_data *data;
Axel Lin337076f2014-07-01 22:40:29 +0800242 struct device *hwmon_dev;
243 const struct attribute_group **attr_groups = NULL;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200244
245 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
Guenter Roeckd5282922012-06-02 09:57:58 -0700246 I2C_FUNC_SMBUS_WORD_DATA))
247 return -EOPNOTSUPP;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200248
Axel Lin337076f2014-07-01 22:40:29 +0800249 data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
Guenter Roeckd5282922012-06-02 09:57:58 -0700250 if (!data)
251 return -ENOMEM;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200252
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200253 i2c_set_clientdata(client, data);
254
255 mutex_init(&data->lock);
Axel Lin337076f2014-07-01 22:40:29 +0800256 data->client = client;
Linus Walleijf4c29652019-01-28 22:06:37 +0100257 if (dev->of_node)
258 data->type = (enum chips)of_device_get_match_data(dev);
259 else
260 data->type = id->driver_data;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200261
262 switch (data->type) {
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200263 case ad7416:
264 data->adc_max = 0;
Axel Lin337076f2014-07-01 22:40:29 +0800265 attr_groups = ad7416_groups;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200266 break;
267
268 case ad7417:
269 data->adc_max = 4;
Axel Lin337076f2014-07-01 22:40:29 +0800270 attr_groups = ad7417_groups;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200271 break;
272
273 case ad7418:
274 data->adc_max = 1;
Axel Lin337076f2014-07-01 22:40:29 +0800275 attr_groups = ad7418_groups;
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200276 break;
277 }
278
Axel Lin337076f2014-07-01 22:40:29 +0800279 dev_info(dev, "%s chip found\n", client->name);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200280
281 /* Initialize the AD7418 chip */
282 ad7418_init_client(client);
283
Axel Lin337076f2014-07-01 22:40:29 +0800284 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
285 client->name,
286 data, attr_groups);
287 return PTR_ERR_OR_ZERO(hwmon_dev);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200288}
289
Axel Lin991763a2014-07-01 22:39:08 +0800290static const struct i2c_device_id ad7418_id[] = {
291 { "ad7416", ad7416 },
292 { "ad7417", ad7417 },
293 { "ad7418", ad7418 },
294 { }
295};
296MODULE_DEVICE_TABLE(i2c, ad7418_id);
297
Linus Walleijf4c29652019-01-28 22:06:37 +0100298static const struct of_device_id ad7418_dt_ids[] = {
299 { .compatible = "adi,ad7416", .data = (void *)ad7416, },
300 { .compatible = "adi,ad7417", .data = (void *)ad7417, },
301 { .compatible = "adi,ad7418", .data = (void *)ad7418, },
302 { }
303};
304MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
305
Axel Lin991763a2014-07-01 22:39:08 +0800306static struct i2c_driver ad7418_driver = {
307 .driver = {
308 .name = "ad7418",
Linus Walleijf4c29652019-01-28 22:06:37 +0100309 .of_match_table = ad7418_dt_ids,
Axel Lin991763a2014-07-01 22:39:08 +0800310 },
311 .probe = ad7418_probe,
Axel Lin991763a2014-07-01 22:39:08 +0800312 .id_table = ad7418_id,
313};
314
Axel Linf0967ee2012-01-20 15:38:18 +0800315module_i2c_driver(ad7418_driver);
Alessandro Zummo2d8dd652007-05-08 17:22:02 +0200316
317MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
318MODULE_DESCRIPTION("AD7416/17/18 driver");
319MODULE_LICENSE("GPL");
320MODULE_VERSION(DRV_VERSION);