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