hwmon: (ads7828) driver cleanup
[linux-2.6-block.git] / drivers / hwmon / ads7828.c
CommitLineData
5812f928 1/*
d13d6232
GR
2 * ads7828.c - lm_sensors driver for ads7828 12-bit 8-channel ADC
3 * (C) 2007 EADS Astrium
4 *
5 * This driver is based on the lm75 and other lm_sensors/hwmon drivers
6 *
7 * Written by Steve Hardy <shardy@redhat.com>
8 *
46d78462 9 * For further information, see the Documentation/hwmon/ads7828 file.
d13d6232
GR
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
5812f928 25
46d78462 26#include <linux/err.h>
5812f928
SH
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
46d78462
VD
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/jiffies.h>
32#include <linux/module.h>
5812f928 33#include <linux/mutex.h>
46d78462
VD
34#include <linux/platform_data/ads7828.h>
35#include <linux/slab.h>
5812f928
SH
36
37/* The ADS7828 registers */
46d78462
VD
38#define ADS7828_NCH 8 /* 8 channels supported */
39#define ADS7828_CMD_SD_SE 0x80 /* Single ended inputs */
40#define ADS7828_CMD_PD1 0x04 /* Internal vref OFF && A/D ON */
41#define ADS7828_CMD_PD3 0x0C /* Internal vref ON && A/D ON */
42#define ADS7828_INT_VREF_MV 2500 /* Internal vref is 2.5V, 2500mV */
43#define ADS7828_EXT_VREF_MV_MIN 50 /* External vref min value 0.05V */
44#define ADS7828_EXT_VREF_MV_MAX 5250 /* External vref max value 5.25V */
45
46/* Client specific data */
5812f928 47struct ads7828_data {
5812f928 48 struct device *hwmon_dev;
46d78462
VD
49 struct mutex update_lock; /* Mutex protecting updates */
50 unsigned long last_updated; /* Last updated time (in jiffies) */
51 u16 adc_input[ADS7828_NCH]; /* ADS7828_NCH samples */
52 bool valid; /* Validity flag */
53 bool diff_input; /* Differential input */
54 bool ext_vref; /* External voltage reference */
55 unsigned int vref_mv; /* voltage reference value */
56 u8 cmd_byte; /* Command byte without channel bits */
57 unsigned int lsb_resol; /* Resolution of the ADC sample LSB */
5812f928
SH
58};
59
46d78462
VD
60/* Command byte C2,C1,C0 - see datasheet */
61static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
5812f928 62{
46d78462 63 return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
5812f928
SH
64}
65
66/* Update data for the device (all 8 channels) */
67static struct ads7828_data *ads7828_update_device(struct device *dev)
68{
69 struct i2c_client *client = to_i2c_client(dev);
70 struct ads7828_data *data = i2c_get_clientdata(client);
71
72 mutex_lock(&data->update_lock);
73
74 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
75 || !data->valid) {
76 unsigned int ch;
77 dev_dbg(&client->dev, "Starting ads7828 update\n");
78
79 for (ch = 0; ch < ADS7828_NCH; ch++) {
46d78462 80 u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch);
90f4102c
JD
81 data->adc_input[ch] =
82 i2c_smbus_read_word_swapped(client, cmd);
5812f928
SH
83 }
84 data->last_updated = jiffies;
46d78462 85 data->valid = true;
5812f928
SH
86 }
87
88 mutex_unlock(&data->update_lock);
89
90 return data;
91}
92
93/* sysfs callback function */
46d78462
VD
94static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
95 char *buf)
5812f928
SH
96{
97 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
98 struct ads7828_data *data = ads7828_update_device(dev);
46d78462
VD
99 unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] *
100 data->lsb_resol, 1000);
5812f928 101
46d78462
VD
102 return sprintf(buf, "%d\n", value);
103}
5812f928 104
46d78462
VD
105static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
106static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1);
107static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2);
108static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3);
109static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4);
110static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5);
111static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6);
112static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7);
5812f928
SH
113
114static struct attribute *ads7828_attributes[] = {
115 &sensor_dev_attr_in0_input.dev_attr.attr,
116 &sensor_dev_attr_in1_input.dev_attr.attr,
117 &sensor_dev_attr_in2_input.dev_attr.attr,
118 &sensor_dev_attr_in3_input.dev_attr.attr,
119 &sensor_dev_attr_in4_input.dev_attr.attr,
120 &sensor_dev_attr_in5_input.dev_attr.attr,
121 &sensor_dev_attr_in6_input.dev_attr.attr,
122 &sensor_dev_attr_in7_input.dev_attr.attr,
123 NULL
124};
125
126static const struct attribute_group ads7828_group = {
127 .attrs = ads7828_attributes,
128};
129
7347cb38 130static int ads7828_remove(struct i2c_client *client)
5812f928
SH
131{
132 struct ads7828_data *data = i2c_get_clientdata(client);
46d78462 133
5812f928
SH
134 hwmon_device_unregister(data->hwmon_dev);
135 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
5812f928 136
7347cb38
JD
137 return 0;
138}
5812f928 139
7347cb38
JD
140static int ads7828_probe(struct i2c_client *client,
141 const struct i2c_device_id *id)
142{
46d78462 143 struct ads7828_platform_data *pdata = client->dev.platform_data;
7347cb38
JD
144 struct ads7828_data *data;
145 int err;
5812f928 146
34e3f7f5
GR
147 data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data),
148 GFP_KERNEL);
149 if (!data)
150 return -ENOMEM;
5812f928 151
46d78462
VD
152 if (pdata) {
153 data->diff_input = pdata->diff_input;
154 data->ext_vref = pdata->ext_vref;
155 if (data->ext_vref)
156 data->vref_mv = pdata->vref_mv;
157 }
158
159 /* Bound Vref with min/max values if it was provided */
160 if (data->vref_mv)
161 data->vref_mv = SENSORS_LIMIT(data->vref_mv,
162 ADS7828_EXT_VREF_MV_MIN,
163 ADS7828_EXT_VREF_MV_MAX);
164 else
165 data->vref_mv = ADS7828_INT_VREF_MV;
166
167 data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096);
168
169 data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
170 if (!data->diff_input)
171 data->cmd_byte |= ADS7828_CMD_SD_SE;
172
7347cb38 173 i2c_set_clientdata(client, data);
5812f928
SH
174 mutex_init(&data->update_lock);
175
5812f928
SH
176 err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
177 if (err)
34e3f7f5 178 return err;
5812f928
SH
179
180 data->hwmon_dev = hwmon_device_register(&client->dev);
181 if (IS_ERR(data->hwmon_dev)) {
182 err = PTR_ERR(data->hwmon_dev);
46d78462 183 goto error;
5812f928
SH
184 }
185
186 return 0;
187
46d78462 188error:
5812f928 189 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
5812f928
SH
190 return err;
191}
192
46d78462
VD
193static const struct i2c_device_id ads7828_device_ids[] = {
194 { "ads7828", 0 },
195 { }
196};
197MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
5812f928 198
46d78462
VD
199static struct i2c_driver ads7828_driver = {
200 .driver = {
201 .name = "ads7828",
202 },
5812f928 203
46d78462
VD
204 .id_table = ads7828_device_ids,
205 .probe = ads7828_probe,
206 .remove = ads7828_remove,
207};
5812f928 208
46d78462 209module_i2c_driver(ads7828_driver);
5812f928 210
5812f928 211MODULE_LICENSE("GPL");
46d78462
VD
212MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
213MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter");