xref: /linux/drivers/hwmon/ad7418.c (revision 3213486f2e442831e324cc6201a2f9e924ecc235)
1 /*
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>
22 #include <linux/of_device.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 
26 #include "lm75.h"
27 
28 #define DRV_VERSION "0.4"
29 
30 enum chips { ad7416, ad7417, ad7418 };
31 
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 
43 static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
44 					AD7418_REG_TEMP_HYST,
45 					AD7418_REG_TEMP_OS };
46 
47 struct ad7418_data {
48 	struct i2c_client	*client;
49 	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 
58 static int ad7418_update_device(struct device *dev)
59 {
60 	struct ad7418_data *data = dev_get_drvdata(dev);
61 	struct i2c_client *client = data->client;
62 	s32 val;
63 
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 */
72 		val = i2c_smbus_read_byte_data(client, AD7418_REG_CONF);
73 		if (val < 0)
74 			goto abort;
75 
76 		cfg = val;
77 		cfg &= 0x1F;
78 
79 		val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
80 						cfg | AD7418_CH_TEMP);
81 		if (val < 0)
82 			goto abort;
83 
84 		udelay(30);
85 
86 		for (i = 0; i < 3; i++) {
87 			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;
93 		}
94 
95 		for (i = 0, ch = 4; i < data->adc_max; i++, ch--) {
96 			val = i2c_smbus_write_byte_data(client, AD7418_REG_CONF,
97 					cfg | AD7418_REG_ADC_CH(ch));
98 			if (val < 0)
99 				goto abort;
100 
101 			udelay(15);
102 			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;
108 		}
109 
110 		/* restore old configuration value */
111 		val = i2c_smbus_write_word_swapped(client, AD7418_REG_CONF,
112 						   cfg);
113 		if (val < 0)
114 			goto abort;
115 
116 		data->last_updated = jiffies;
117 		data->valid = 1;
118 	}
119 
120 	mutex_unlock(&data->lock);
121 	return 0;
122 
123 abort:
124 	data->valid = 0;
125 	mutex_unlock(&data->lock);
126 	return val;
127 }
128 
129 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
130 			 char *buf)
131 {
132 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
133 	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 
140 	return sprintf(buf, "%d\n",
141 		LM75_TEMP_FROM_REG(data->temp[attr->index]));
142 }
143 
144 static ssize_t adc_show(struct device *dev, struct device_attribute *devattr,
145 			char *buf)
146 {
147 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
148 	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;
154 
155 	return sprintf(buf, "%d\n",
156 		((data->in[attr->index] >> 6) * 2500 + 512) / 1024);
157 }
158 
159 static ssize_t temp_store(struct device *dev,
160 			  struct device_attribute *devattr, const char *buf,
161 			  size_t count)
162 {
163 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
164 	struct ad7418_data *data = dev_get_drvdata(dev);
165 	struct i2c_client *client = data->client;
166 	long temp;
167 	int ret = kstrtol(buf, 10, &temp);
168 
169 	if (ret < 0)
170 		return ret;
171 
172 	mutex_lock(&data->lock);
173 	data->temp[attr->index] = LM75_TEMP_TO_REG(temp);
174 	i2c_smbus_write_word_swapped(client,
175 				     AD7418_REG_TEMP[attr->index],
176 				     data->temp[attr->index]);
177 	mutex_unlock(&data->lock);
178 	return count;
179 }
180 
181 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
182 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, 1);
183 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 2);
184 
185 static SENSOR_DEVICE_ATTR_RO(in1_input, adc, 0);
186 static SENSOR_DEVICE_ATTR_RO(in2_input, adc, 1);
187 static SENSOR_DEVICE_ATTR_RO(in3_input, adc, 2);
188 static SENSOR_DEVICE_ATTR_RO(in4_input, adc, 3);
189 
190 static struct attribute *ad7416_attrs[] = {
191 	&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 };
196 ATTRIBUTE_GROUPS(ad7416);
197 
198 static struct attribute *ad7417_attrs[] = {
199 	&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 };
208 ATTRIBUTE_GROUPS(ad7417);
209 
210 static struct attribute *ad7418_attrs[] = {
211 	&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 };
217 ATTRIBUTE_GROUPS(ad7418);
218 
219 static 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 
236 static int ad7418_probe(struct i2c_client *client,
237 			 const struct i2c_device_id *id)
238 {
239 	struct device *dev = &client->dev;
240 	struct i2c_adapter *adapter = client->adapter;
241 	struct ad7418_data *data;
242 	struct device *hwmon_dev;
243 	const struct attribute_group **attr_groups = NULL;
244 
245 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
246 					I2C_FUNC_SMBUS_WORD_DATA))
247 		return -EOPNOTSUPP;
248 
249 	data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL);
250 	if (!data)
251 		return -ENOMEM;
252 
253 	i2c_set_clientdata(client, data);
254 
255 	mutex_init(&data->lock);
256 	data->client = client;
257 	if (dev->of_node)
258 		data->type = (enum chips)of_device_get_match_data(dev);
259 	else
260 		data->type = id->driver_data;
261 
262 	switch (data->type) {
263 	case ad7416:
264 		data->adc_max = 0;
265 		attr_groups = ad7416_groups;
266 		break;
267 
268 	case ad7417:
269 		data->adc_max = 4;
270 		attr_groups = ad7417_groups;
271 		break;
272 
273 	case ad7418:
274 		data->adc_max = 1;
275 		attr_groups = ad7418_groups;
276 		break;
277 	}
278 
279 	dev_info(dev, "%s chip found\n", client->name);
280 
281 	/* Initialize the AD7418 chip */
282 	ad7418_init_client(client);
283 
284 	hwmon_dev = devm_hwmon_device_register_with_groups(dev,
285 							   client->name,
286 							   data, attr_groups);
287 	return PTR_ERR_OR_ZERO(hwmon_dev);
288 }
289 
290 static const struct i2c_device_id ad7418_id[] = {
291 	{ "ad7416", ad7416 },
292 	{ "ad7417", ad7417 },
293 	{ "ad7418", ad7418 },
294 	{ }
295 };
296 MODULE_DEVICE_TABLE(i2c, ad7418_id);
297 
298 static 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 };
304 MODULE_DEVICE_TABLE(of, ad7418_dt_ids);
305 
306 static struct i2c_driver ad7418_driver = {
307 	.driver = {
308 		.name	= "ad7418",
309 		.of_match_table = ad7418_dt_ids,
310 	},
311 	.probe		= ad7418_probe,
312 	.id_table	= ad7418_id,
313 };
314 
315 module_i2c_driver(ad7418_driver);
316 
317 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
318 MODULE_DESCRIPTION("AD7416/17/18 driver");
319 MODULE_LICENSE("GPL");
320 MODULE_VERSION(DRV_VERSION);
321