blob: 42914fc1436d9b91894d42daf3d722d147e6c0c1 [file] [log] [blame]
Steve Hardy5812f922008-01-22 23:00:02 +00001/*
Guenter Roeckd13d6232012-01-19 11:02:14 -08002 * 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 *
Vivien Didelot46d78462012-10-03 16:54:07 -04009 * For further information, see the Documentation/hwmon/ads7828 file.
Guenter Roeckd13d6232012-01-19 11:02:14 -080010 *
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 */
Steve Hardy5812f922008-01-22 23:00:02 +000025
Vivien Didelot46d78462012-10-03 16:54:07 -040026#include <linux/err.h>
Steve Hardy5812f922008-01-22 23:00:02 +000027#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
Vivien Didelot46d78462012-10-03 16:54:07 -040029#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/jiffies.h>
32#include <linux/module.h>
Steve Hardy5812f922008-01-22 23:00:02 +000033#include <linux/mutex.h>
Vivien Didelot46d78462012-10-03 16:54:07 -040034#include <linux/platform_data/ads7828.h>
35#include <linux/slab.h>
Steve Hardy5812f922008-01-22 23:00:02 +000036
37/* The ADS7828 registers */
Vivien Didelot46d78462012-10-03 16:54:07 -040038#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 */
Steve Hardy5812f922008-01-22 23:00:02 +000045
Vivien Didelot46d78462012-10-03 16:54:07 -040046/* Client specific data */
Steve Hardy5812f922008-01-22 23:00:02 +000047struct ads7828_data {
Steve Hardy5812f922008-01-22 23:00:02 +000048 struct device *hwmon_dev;
Vivien Didelot46d78462012-10-03 16:54:07 -040049 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 */
Steve Hardy5812f922008-01-22 23:00:02 +000058};
59
Vivien Didelot46d78462012-10-03 16:54:07 -040060/* Command byte C2,C1,C0 - see datasheet */
61static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
Steve Hardy5812f922008-01-22 23:00:02 +000062{
Vivien Didelot46d78462012-10-03 16:54:07 -040063 return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
Steve Hardy5812f922008-01-22 23:00:02 +000064}
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++) {
Vivien Didelot46d78462012-10-03 16:54:07 -040080 u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch);
Jean Delvare90f41022011-11-04 12:00:47 +010081 data->adc_input[ch] =
82 i2c_smbus_read_word_swapped(client, cmd);
Steve Hardy5812f922008-01-22 23:00:02 +000083 }
84 data->last_updated = jiffies;
Vivien Didelot46d78462012-10-03 16:54:07 -040085 data->valid = true;
Steve Hardy5812f922008-01-22 23:00:02 +000086 }
87
88 mutex_unlock(&data->update_lock);
89
90 return data;
91}
92
93/* sysfs callback function */
Vivien Didelot46d78462012-10-03 16:54:07 -040094static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
95 char *buf)
Steve Hardy5812f922008-01-22 23:00:02 +000096{
97 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
98 struct ads7828_data *data = ads7828_update_device(dev);
Vivien Didelot46d78462012-10-03 16:54:07 -040099 unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] *
100 data->lsb_resol, 1000);
101
102 return sprintf(buf, "%d\n", value);
Steve Hardy5812f922008-01-22 23:00:02 +0000103}
104
Vivien Didelot46d78462012-10-03 16:54:07 -0400105static 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);
Steve Hardy5812f922008-01-22 23:00:02 +0000113
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
Jean Delvare7347cb32008-07-16 19:30:10 +0200130static int ads7828_remove(struct i2c_client *client)
Steve Hardy5812f922008-01-22 23:00:02 +0000131{
132 struct ads7828_data *data = i2c_get_clientdata(client);
Vivien Didelot46d78462012-10-03 16:54:07 -0400133
Steve Hardy5812f922008-01-22 23:00:02 +0000134 hwmon_device_unregister(data->hwmon_dev);
135 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
Steve Hardy5812f922008-01-22 23:00:02 +0000136
Jean Delvare7347cb32008-07-16 19:30:10 +0200137 return 0;
138}
Steve Hardy5812f922008-01-22 23:00:02 +0000139
Jean Delvare7347cb32008-07-16 19:30:10 +0200140static int ads7828_probe(struct i2c_client *client,
141 const struct i2c_device_id *id)
142{
Vivien Didelot46d78462012-10-03 16:54:07 -0400143 struct ads7828_platform_data *pdata = client->dev.platform_data;
Jean Delvare7347cb32008-07-16 19:30:10 +0200144 struct ads7828_data *data;
145 int err;
Steve Hardy5812f922008-01-22 23:00:02 +0000146
Guenter Roeck34e3f7f2012-06-02 09:58:00 -0700147 data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data),
148 GFP_KERNEL);
149 if (!data)
150 return -ENOMEM;
Steve Hardy5812f922008-01-22 23:00:02 +0000151
Vivien Didelot46d78462012-10-03 16:54:07 -0400152 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
Jean Delvare7347cb32008-07-16 19:30:10 +0200173 i2c_set_clientdata(client, data);
Steve Hardy5812f922008-01-22 23:00:02 +0000174 mutex_init(&data->update_lock);
175
Steve Hardy5812f922008-01-22 23:00:02 +0000176 err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
177 if (err)
Guenter Roeck34e3f7f2012-06-02 09:58:00 -0700178 return err;
Steve Hardy5812f922008-01-22 23:00:02 +0000179
180 data->hwmon_dev = hwmon_device_register(&client->dev);
181 if (IS_ERR(data->hwmon_dev)) {
182 err = PTR_ERR(data->hwmon_dev);
Vivien Didelot46d78462012-10-03 16:54:07 -0400183 goto error;
Steve Hardy5812f922008-01-22 23:00:02 +0000184 }
185
186 return 0;
187
Vivien Didelot46d78462012-10-03 16:54:07 -0400188error:
Steve Hardy5812f922008-01-22 23:00:02 +0000189 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
Steve Hardy5812f922008-01-22 23:00:02 +0000190 return err;
191}
192
Vivien Didelot46d78462012-10-03 16:54:07 -0400193static const struct i2c_device_id ads7828_device_ids[] = {
194 { "ads7828", 0 },
195 { }
196};
197MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
Steve Hardy5812f922008-01-22 23:00:02 +0000198
Vivien Didelot46d78462012-10-03 16:54:07 -0400199static struct i2c_driver ads7828_driver = {
200 .driver = {
201 .name = "ads7828",
202 },
Steve Hardy5812f922008-01-22 23:00:02 +0000203
Vivien Didelot46d78462012-10-03 16:54:07 -0400204 .id_table = ads7828_device_ids,
205 .probe = ads7828_probe,
206 .remove = ads7828_remove,
207};
Steve Hardy5812f922008-01-22 23:00:02 +0000208
Vivien Didelot46d78462012-10-03 16:54:07 -0400209module_i2c_driver(ads7828_driver);
Steve Hardy5812f922008-01-22 23:00:02 +0000210
Steve Hardy5812f922008-01-22 23:00:02 +0000211MODULE_LICENSE("GPL");
Vivien Didelot46d78462012-10-03 16:54:07 -0400212MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
213MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter");