libceph: move r_reply_op_{len,result} into struct ceph_osd_req_op
[linux-2.6-block.git] / drivers / hwmon / ads1015.c
CommitLineData
8c22a8f5
DE
1/*
2 * ads1015.c - lm_sensors driver for ads1015 12-bit 4-input ADC
3 * (C) Copyright 2010
4 * Dirk Eibach, Guntermann & Drunck GmbH <eibach@gdsys.de>
5 *
6 * Based on the ads7828 driver by Steve Hardy.
7 *
8 * Datasheet available at: http://focus.ti.com/lit/ds/symlink/ads1015.pdf
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 as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
29#include <linux/i2c.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32#include <linux/err.h>
33#include <linux/mutex.h>
34#include <linux/of.h>
35
36#include <linux/i2c/ads1015.h>
37
38/* ADS1015 registers */
39enum {
40 ADS1015_CONVERSION = 0,
41 ADS1015_CONFIG = 1,
42};
43
44/* PGA fullscale voltages in mV */
45static const unsigned int fullscale_table[8] = {
46 6144, 4096, 2048, 1024, 512, 256, 256, 256 };
47
c0046867 48/* Data rates in samples per second */
60c1f31f
ED
49static const unsigned int data_rate_table_1015[8] = {
50 128, 250, 490, 920, 1600, 2400, 3300, 3300
51};
52
53static const unsigned int data_rate_table_1115[8] = {
54 8, 16, 32, 64, 128, 250, 475, 860
55};
c0046867 56
8c22a8f5 57#define ADS1015_DEFAULT_CHANNELS 0xff
c0046867
DE
58#define ADS1015_DEFAULT_PGA 2
59#define ADS1015_DEFAULT_DATA_RATE 4
8c22a8f5 60
60c1f31f
ED
61enum ads1015_chips {
62 ads1015,
63 ads1115,
64};
65
8c22a8f5
DE
66struct ads1015_data {
67 struct device *hwmon_dev;
68 struct mutex update_lock; /* mutex protect updates */
c0046867 69 struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
60c1f31f 70 enum ads1015_chips id;
8c22a8f5
DE
71};
72
1196573f 73static int ads1015_read_adc(struct i2c_client *client, unsigned int channel)
8c22a8f5
DE
74{
75 u16 config;
8c22a8f5 76 struct ads1015_data *data = i2c_get_clientdata(client);
c0046867 77 unsigned int pga = data->channel_data[channel].pga;
c0046867
DE
78 unsigned int data_rate = data->channel_data[channel].data_rate;
79 unsigned int conversion_time_ms;
60c1f31f
ED
80 const unsigned int * const rate_table = data->id == ads1115 ?
81 data_rate_table_1115 : data_rate_table_1015;
8c22a8f5
DE
82 int res;
83
84 mutex_lock(&data->update_lock);
85
c0046867 86 /* get channel parameters */
90f4102c 87 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
8c22a8f5
DE
88 if (res < 0)
89 goto err_unlock;
90 config = res;
60c1f31f 91 conversion_time_ms = DIV_ROUND_UP(1000, rate_table[data_rate]);
8c22a8f5 92
c0046867
DE
93 /* setup and start single conversion */
94 config &= 0x001f;
95 config |= (1 << 15) | (1 << 8);
96 config |= (channel & 0x0007) << 12;
97 config |= (pga & 0x0007) << 9;
98 config |= (data_rate & 0x0007) << 5;
8c22a8f5 99
90f4102c 100 res = i2c_smbus_write_word_swapped(client, ADS1015_CONFIG, config);
8c22a8f5
DE
101 if (res < 0)
102 goto err_unlock;
c0046867
DE
103
104 /* wait until conversion finished */
105 msleep(conversion_time_ms);
90f4102c 106 res = i2c_smbus_read_word_swapped(client, ADS1015_CONFIG);
c0046867
DE
107 if (res < 0)
108 goto err_unlock;
109 config = res;
110 if (!(config & (1 << 15))) {
111 /* conversion not finished in time */
8c22a8f5
DE
112 res = -EIO;
113 goto err_unlock;
114 }
115
90f4102c 116 res = i2c_smbus_read_word_swapped(client, ADS1015_CONVERSION);
8c22a8f5
DE
117
118err_unlock:
119 mutex_unlock(&data->update_lock);
120 return res;
121}
122
1196573f
GR
123static int ads1015_reg_to_mv(struct i2c_client *client, unsigned int channel,
124 s16 reg)
125{
126 struct ads1015_data *data = i2c_get_clientdata(client);
127 unsigned int pga = data->channel_data[channel].pga;
128 int fullscale = fullscale_table[pga];
acc14694 129 const int mask = data->id == ads1115 ? 0x7fff : 0x7ff0;
1196573f 130
60c1f31f 131 return DIV_ROUND_CLOSEST(reg * fullscale, mask);
1196573f
GR
132}
133
8c22a8f5
DE
134/* sysfs callback function */
135static ssize_t show_in(struct device *dev, struct device_attribute *da,
136 char *buf)
137{
138 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
139 struct i2c_client *client = to_i2c_client(dev);
8c22a8f5 140 int res;
1196573f 141 int index = attr->index;
8c22a8f5 142
1196573f
GR
143 res = ads1015_read_adc(client, index);
144 if (res < 0)
145 return res;
8c22a8f5 146
1196573f 147 return sprintf(buf, "%d\n", ads1015_reg_to_mv(client, index, res));
8c22a8f5
DE
148}
149
fdf241a8
JD
150static const struct sensor_device_attribute ads1015_in[] = {
151 SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
152 SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
153 SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
154 SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
155 SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
156 SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
157 SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
158 SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
8c22a8f5
DE
159};
160
161/*
162 * Driver interface
163 */
164
165static int ads1015_remove(struct i2c_client *client)
166{
167 struct ads1015_data *data = i2c_get_clientdata(client);
fdf241a8
JD
168 int k;
169
8c22a8f5 170 hwmon_device_unregister(data->hwmon_dev);
c0046867 171 for (k = 0; k < ADS1015_CHANNELS; ++k)
fdf241a8 172 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
8c22a8f5
DE
173 return 0;
174}
175
8c22a8f5 176#ifdef CONFIG_OF
c0046867
DE
177static int ads1015_get_channels_config_of(struct i2c_client *client)
178{
179 struct ads1015_data *data = i2c_get_clientdata(client);
180 struct device_node *node;
181
182 if (!client->dev.of_node
183 || !of_get_next_child(client->dev.of_node, NULL))
184 return -EINVAL;
185
186 for_each_child_of_node(client->dev.of_node, node) {
8e35762f 187 u32 pval;
c0046867
DE
188 unsigned int channel;
189 unsigned int pga = ADS1015_DEFAULT_PGA;
190 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
191
8e35762f 192 if (of_property_read_u32(node, "reg", &pval)) {
c0046867
DE
193 dev_err(&client->dev, "invalid reg on %s\n",
194 node->full_name);
195 continue;
196 }
197
8e35762f 198 channel = pval;
56de1377 199 if (channel >= ADS1015_CHANNELS) {
c0046867
DE
200 dev_err(&client->dev,
201 "invalid channel index %d on %s\n",
202 channel, node->full_name);
203 continue;
204 }
205
8e35762f
AL
206 if (!of_property_read_u32(node, "ti,gain", &pval)) {
207 pga = pval;
c0046867 208 if (pga > 6) {
8e35762f 209 dev_err(&client->dev, "invalid gain on %s\n",
c0046867 210 node->full_name);
e9814295 211 return -EINVAL;
c0046867
DE
212 }
213 }
214
8e35762f
AL
215 if (!of_property_read_u32(node, "ti,datarate", &pval)) {
216 data_rate = pval;
c0046867
DE
217 if (data_rate > 7) {
218 dev_err(&client->dev,
219 "invalid data_rate on %s\n",
220 node->full_name);
e9814295 221 return -EINVAL;
c0046867
DE
222 }
223 }
224
225 data->channel_data[channel].enabled = true;
226 data->channel_data[channel].pga = pga;
227 data->channel_data[channel].data_rate = data_rate;
228 }
229
230 return 0;
231}
8c22a8f5
DE
232#endif
233
c0046867
DE
234static void ads1015_get_channels_config(struct i2c_client *client)
235{
236 unsigned int k;
237 struct ads1015_data *data = i2c_get_clientdata(client);
238 struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
239
8c22a8f5 240 /* prefer platform data */
c0046867
DE
241 if (pdata) {
242 memcpy(data->channel_data, pdata->channel_data,
243 sizeof(data->channel_data));
244 return;
245 }
8c22a8f5
DE
246
247#ifdef CONFIG_OF
c0046867
DE
248 if (!ads1015_get_channels_config_of(client))
249 return;
8c22a8f5
DE
250#endif
251
252 /* fallback on default configuration */
c0046867
DE
253 for (k = 0; k < ADS1015_CHANNELS; ++k) {
254 data->channel_data[k].enabled = true;
255 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
256 data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
257 }
8c22a8f5
DE
258}
259
260static int ads1015_probe(struct i2c_client *client,
261 const struct i2c_device_id *id)
262{
263 struct ads1015_data *data;
264 int err;
8c22a8f5 265 unsigned int k;
8c22a8f5 266
57457e31
GR
267 data = devm_kzalloc(&client->dev, sizeof(struct ads1015_data),
268 GFP_KERNEL);
269 if (!data)
270 return -ENOMEM;
60c1f31f 271 data->id = id->driver_data;
8c22a8f5
DE
272 i2c_set_clientdata(client, data);
273 mutex_init(&data->update_lock);
274
275 /* build sysfs attribute group */
c0046867
DE
276 ads1015_get_channels_config(client);
277 for (k = 0; k < ADS1015_CHANNELS; ++k) {
278 if (!data->channel_data[k].enabled)
8c22a8f5 279 continue;
fdf241a8
JD
280 err = device_create_file(&client->dev, &ads1015_in[k].dev_attr);
281 if (err)
363434b5 282 goto exit_remove;
8c22a8f5 283 }
8c22a8f5
DE
284
285 data->hwmon_dev = hwmon_device_register(&client->dev);
286 if (IS_ERR(data->hwmon_dev)) {
287 err = PTR_ERR(data->hwmon_dev);
288 goto exit_remove;
289 }
290
291 return 0;
292
293exit_remove:
c0046867 294 for (k = 0; k < ADS1015_CHANNELS; ++k)
fdf241a8 295 device_remove_file(&client->dev, &ads1015_in[k].dev_attr);
8c22a8f5
DE
296 return err;
297}
298
299static const struct i2c_device_id ads1015_id[] = {
60c1f31f
ED
300 { "ads1015", ads1015},
301 { "ads1115", ads1115},
8c22a8f5
DE
302 { }
303};
304MODULE_DEVICE_TABLE(i2c, ads1015_id);
305
306static struct i2c_driver ads1015_driver = {
307 .driver = {
308 .name = "ads1015",
309 },
310 .probe = ads1015_probe,
311 .remove = ads1015_remove,
312 .id_table = ads1015_id,
313};
314
f0967eea 315module_i2c_driver(ads1015_driver);
8c22a8f5
DE
316
317MODULE_AUTHOR("Dirk Eibach <eibach@gdsys.de>");
318MODULE_DESCRIPTION("ADS1015 driver");
319MODULE_LICENSE("GPL");