1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/err.h>
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31#include <linux/i2c.h>
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/module.h>
35#include <linux/mutex.h>
36#include <linux/platform_data/ads7828.h>
37#include <linux/slab.h>
38
39
40#define ADS7828_NCH 8
41#define ADS7828_CMD_SD_SE 0x80
42#define ADS7828_CMD_PD1 0x04
43#define ADS7828_CMD_PD3 0x0C
44#define ADS7828_INT_VREF_MV 2500
45#define ADS7828_EXT_VREF_MV_MIN 50
46#define ADS7828_EXT_VREF_MV_MAX 5250
47
48
49enum ads7828_chips { ads7828, ads7830 };
50
51
52struct ads7828_data {
53 struct device *hwmon_dev;
54 struct mutex update_lock;
55 unsigned long last_updated;
56 u16 adc_input[ADS7828_NCH];
57 bool valid;
58 bool diff_input;
59 bool ext_vref;
60 unsigned int vref_mv;
61 u8 cmd_byte;
62 unsigned int lsb_resol;
63 s32 (*read_channel)(const struct i2c_client *client, u8 command);
64};
65
66
67static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
68{
69 return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
70}
71
72
73static struct ads7828_data *ads7828_update_device(struct device *dev)
74{
75 struct i2c_client *client = to_i2c_client(dev);
76 struct ads7828_data *data = i2c_get_clientdata(client);
77
78 mutex_lock(&data->update_lock);
79
80 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
81 || !data->valid) {
82 unsigned int ch;
83 dev_dbg(&client->dev, "Starting ads7828 update\n");
84
85 for (ch = 0; ch < ADS7828_NCH; ch++) {
86 u8 cmd = ads7828_cmd_byte(data->cmd_byte, ch);
87 data->adc_input[ch] = data->read_channel(client, cmd);
88 }
89 data->last_updated = jiffies;
90 data->valid = true;
91 }
92
93 mutex_unlock(&data->update_lock);
94
95 return data;
96}
97
98
99static ssize_t ads7828_show_in(struct device *dev, struct device_attribute *da,
100 char *buf)
101{
102 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
103 struct ads7828_data *data = ads7828_update_device(dev);
104 unsigned int value = DIV_ROUND_CLOSEST(data->adc_input[attr->index] *
105 data->lsb_resol, 1000);
106
107 return sprintf(buf, "%d\n", value);
108}
109
110static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ads7828_show_in, NULL, 0);
111static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ads7828_show_in, NULL, 1);
112static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ads7828_show_in, NULL, 2);
113static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ads7828_show_in, NULL, 3);
114static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ads7828_show_in, NULL, 4);
115static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ads7828_show_in, NULL, 5);
116static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ads7828_show_in, NULL, 6);
117static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ads7828_show_in, NULL, 7);
118
119static struct attribute *ads7828_attributes[] = {
120 &sensor_dev_attr_in0_input.dev_attr.attr,
121 &sensor_dev_attr_in1_input.dev_attr.attr,
122 &sensor_dev_attr_in2_input.dev_attr.attr,
123 &sensor_dev_attr_in3_input.dev_attr.attr,
124 &sensor_dev_attr_in4_input.dev_attr.attr,
125 &sensor_dev_attr_in5_input.dev_attr.attr,
126 &sensor_dev_attr_in6_input.dev_attr.attr,
127 &sensor_dev_attr_in7_input.dev_attr.attr,
128 NULL
129};
130
131static const struct attribute_group ads7828_group = {
132 .attrs = ads7828_attributes,
133};
134
135static int ads7828_remove(struct i2c_client *client)
136{
137 struct ads7828_data *data = i2c_get_clientdata(client);
138
139 hwmon_device_unregister(data->hwmon_dev);
140 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
141
142 return 0;
143}
144
145static int ads7828_probe(struct i2c_client *client,
146 const struct i2c_device_id *id)
147{
148 struct ads7828_platform_data *pdata = dev_get_platdata(&client->dev);
149 struct ads7828_data *data;
150 int err;
151
152 data = devm_kzalloc(&client->dev, sizeof(struct ads7828_data),
153 GFP_KERNEL);
154 if (!data)
155 return -ENOMEM;
156
157 if (pdata) {
158 data->diff_input = pdata->diff_input;
159 data->ext_vref = pdata->ext_vref;
160 if (data->ext_vref)
161 data->vref_mv = pdata->vref_mv;
162 }
163
164
165 if (data->vref_mv)
166 data->vref_mv = clamp_val(data->vref_mv,
167 ADS7828_EXT_VREF_MV_MIN,
168 ADS7828_EXT_VREF_MV_MAX);
169 else
170 data->vref_mv = ADS7828_INT_VREF_MV;
171
172
173 if (id->driver_data == ads7828) {
174 data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 4096);
175 data->read_channel = i2c_smbus_read_word_swapped;
176 } else {
177 data->lsb_resol = DIV_ROUND_CLOSEST(data->vref_mv * 1000, 256);
178 data->read_channel = i2c_smbus_read_byte_data;
179 }
180
181 data->cmd_byte = data->ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
182 if (!data->diff_input)
183 data->cmd_byte |= ADS7828_CMD_SD_SE;
184
185 i2c_set_clientdata(client, data);
186 mutex_init(&data->update_lock);
187
188 err = sysfs_create_group(&client->dev.kobj, &ads7828_group);
189 if (err)
190 return err;
191
192 data->hwmon_dev = hwmon_device_register(&client->dev);
193 if (IS_ERR(data->hwmon_dev)) {
194 err = PTR_ERR(data->hwmon_dev);
195 goto error;
196 }
197
198 return 0;
199
200error:
201 sysfs_remove_group(&client->dev.kobj, &ads7828_group);
202 return err;
203}
204
205static const struct i2c_device_id ads7828_device_ids[] = {
206 { "ads7828", ads7828 },
207 { "ads7830", ads7830 },
208 { }
209};
210MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
211
212static struct i2c_driver ads7828_driver = {
213 .driver = {
214 .name = "ads7828",
215 },
216
217 .id_table = ads7828_device_ids,
218 .probe = ads7828_probe,
219 .remove = ads7828_remove,
220};
221
222module_i2c_driver(ads7828_driver);
223
224MODULE_LICENSE("GPL");
225MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
226MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter and compatibles");
227