Merge tag 'nfs-for-3.14-4' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[sfrench/cifs-2.6.git] / drivers / staging / iio / adc / ad799x_core.c
1 /*
2  * iio/adc/ad799x.c
3  * Copyright (C) 2010-1011 Michael Hennerich, Analog Devices Inc.
4  *
5  * based on iio/adc/max1363
6  * Copyright (C) 2008-2010 Jonathan Cameron
7  *
8  * based on linux/drivers/i2c/chips/max123x
9  * Copyright (C) 2002-2004 Stefan Eletzhofer
10  *
11  * based on linux/drivers/acron/char/pcf8583.c
12  * Copyright (C) 2000 Russell King
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  *
18  * ad799x.c
19  *
20  * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21  * ad7998 and similar chips.
22  *
23  */
24
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/kernel.h>
28 #include <linux/sysfs.h>
29 #include <linux/i2c.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/err.h>
34 #include <linux/module.h>
35
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
38 #include <linux/iio/events.h>
39 #include <linux/iio/buffer.h>
40
41 #include "ad799x.h"
42
43 /*
44  * ad799x register access by I2C
45  */
46 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
47 {
48         struct i2c_client *client = st->client;
49         int ret = 0;
50
51         ret = i2c_smbus_read_word_swapped(client, reg);
52         if (ret < 0) {
53                 dev_err(&client->dev, "I2C read error\n");
54                 return ret;
55         }
56
57         *data = (u16)ret;
58
59         return 0;
60 }
61
62 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
63 {
64         struct i2c_client *client = st->client;
65         int ret = 0;
66
67         ret = i2c_smbus_read_byte_data(client, reg);
68         if (ret < 0) {
69                 dev_err(&client->dev, "I2C read error\n");
70                 return ret;
71         }
72
73         *data = (u8)ret;
74
75         return 0;
76 }
77
78 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
79 {
80         struct i2c_client *client = st->client;
81         int ret = 0;
82
83         ret = i2c_smbus_write_word_swapped(client, reg, data);
84         if (ret < 0)
85                 dev_err(&client->dev, "I2C write error\n");
86
87         return ret;
88 }
89
90 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
91 {
92         struct i2c_client *client = st->client;
93         int ret = 0;
94
95         ret = i2c_smbus_write_byte_data(client, reg, data);
96         if (ret < 0)
97                 dev_err(&client->dev, "I2C write error\n");
98
99         return ret;
100 }
101
102 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
103         const unsigned long *scan_mask)
104 {
105         struct ad799x_state *st = iio_priv(indio_dev);
106
107         kfree(st->rx_buf);
108         st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
109         if (!st->rx_buf)
110                 return -ENOMEM;
111
112         st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
113
114         switch (st->id) {
115         case ad7997:
116         case ad7998:
117                 return ad799x_i2c_write16(st, AD7998_CONF_REG,
118                         st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
119         default:
120                 break;
121         }
122
123         return 0;
124 }
125
126 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
127 {
128         u16 rxbuf;
129         u8 cmd;
130         int ret;
131
132         switch (st->id) {
133         case ad7991:
134         case ad7995:
135         case ad7999:
136                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
137                 break;
138         case ad7992:
139         case ad7993:
140         case ad7994:
141                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
142                 break;
143         case ad7997:
144         case ad7998:
145                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
146                 break;
147         default:
148                 return -EINVAL;
149         }
150
151         ret = ad799x_i2c_read16(st, cmd, &rxbuf);
152         if (ret < 0)
153                 return ret;
154
155         return rxbuf;
156 }
157
158 static int ad799x_read_raw(struct iio_dev *indio_dev,
159                            struct iio_chan_spec const *chan,
160                            int *val,
161                            int *val2,
162                            long m)
163 {
164         int ret;
165         struct ad799x_state *st = iio_priv(indio_dev);
166
167         switch (m) {
168         case IIO_CHAN_INFO_RAW:
169                 mutex_lock(&indio_dev->mlock);
170                 if (iio_buffer_enabled(indio_dev))
171                         ret = -EBUSY;
172                 else
173                         ret = ad799x_scan_direct(st, chan->scan_index);
174                 mutex_unlock(&indio_dev->mlock);
175
176                 if (ret < 0)
177                         return ret;
178                 *val = (ret >> chan->scan_type.shift) &
179                         RES_MASK(chan->scan_type.realbits);
180                 return IIO_VAL_INT;
181         case IIO_CHAN_INFO_SCALE:
182                 *val = st->int_vref_mv;
183                 *val2 = chan->scan_type.realbits;
184                 return IIO_VAL_FRACTIONAL_LOG2;
185         }
186         return -EINVAL;
187 }
188 static const unsigned int ad7998_frequencies[] = {
189         [AD7998_CYC_DIS]        = 0,
190         [AD7998_CYC_TCONF_32]   = 15625,
191         [AD7998_CYC_TCONF_64]   = 7812,
192         [AD7998_CYC_TCONF_128]  = 3906,
193         [AD7998_CYC_TCONF_512]  = 976,
194         [AD7998_CYC_TCONF_1024] = 488,
195         [AD7998_CYC_TCONF_2048] = 244,
196 };
197 static ssize_t ad799x_read_frequency(struct device *dev,
198                                         struct device_attribute *attr,
199                                         char *buf)
200 {
201         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
202         struct ad799x_state *st = iio_priv(indio_dev);
203
204         int ret;
205         u8 val;
206         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
207         if (ret)
208                 return ret;
209
210         val &= AD7998_CYC_MASK;
211
212         return sprintf(buf, "%u\n", ad7998_frequencies[val]);
213 }
214
215 static ssize_t ad799x_write_frequency(struct device *dev,
216                                          struct device_attribute *attr,
217                                          const char *buf,
218                                          size_t len)
219 {
220         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
221         struct ad799x_state *st = iio_priv(indio_dev);
222
223         long val;
224         int ret, i;
225         u8 t;
226
227         ret = kstrtol(buf, 10, &val);
228         if (ret)
229                 return ret;
230
231         mutex_lock(&indio_dev->mlock);
232         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
233         if (ret)
234                 goto error_ret_mutex;
235         /* Wipe the bits clean */
236         t &= ~AD7998_CYC_MASK;
237
238         for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
239                 if (val == ad7998_frequencies[i])
240                         break;
241         if (i == ARRAY_SIZE(ad7998_frequencies)) {
242                 ret = -EINVAL;
243                 goto error_ret_mutex;
244         }
245         t |= i;
246         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
247
248 error_ret_mutex:
249         mutex_unlock(&indio_dev->mlock);
250
251         return ret ? ret : len;
252 }
253
254 static int ad799x_read_event_config(struct iio_dev *indio_dev,
255                                     const struct iio_chan_spec *chan,
256                                     enum iio_event_type type,
257                                     enum iio_event_direction dir)
258 {
259         return 1;
260 }
261
262 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
263                                          enum iio_event_direction dir,
264                                          enum iio_event_info info)
265 {
266         switch (info) {
267         case IIO_EV_INFO_VALUE:
268                 if (dir == IIO_EV_DIR_FALLING)
269                         return AD7998_DATALOW_REG(chan->channel);
270                 else
271                         return AD7998_DATAHIGH_REG(chan->channel);
272         case IIO_EV_INFO_HYSTERESIS:
273                 return AD7998_HYST_REG(chan->channel);
274         default:
275                 return -EINVAL;
276         }
277
278         return 0;
279 }
280
281 static int ad799x_write_event_value(struct iio_dev *indio_dev,
282                                     const struct iio_chan_spec *chan,
283                                     enum iio_event_type type,
284                                     enum iio_event_direction dir,
285                                     enum iio_event_info info,
286                                     int val, int val2)
287 {
288         int ret;
289         struct ad799x_state *st = iio_priv(indio_dev);
290
291         mutex_lock(&indio_dev->mlock);
292         ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
293                 val);
294         mutex_unlock(&indio_dev->mlock);
295
296         return ret;
297 }
298
299 static int ad799x_read_event_value(struct iio_dev *indio_dev,
300                                     const struct iio_chan_spec *chan,
301                                     enum iio_event_type type,
302                                     enum iio_event_direction dir,
303                                     enum iio_event_info info,
304                                     int *val, int *val2)
305 {
306         int ret;
307         struct ad799x_state *st = iio_priv(indio_dev);
308         u16 valin;
309
310         mutex_lock(&indio_dev->mlock);
311         ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
312                 &valin);
313         mutex_unlock(&indio_dev->mlock);
314         if (ret < 0)
315                 return ret;
316         *val = valin;
317
318         return IIO_VAL_INT;
319 }
320
321 static irqreturn_t ad799x_event_handler(int irq, void *private)
322 {
323         struct iio_dev *indio_dev = private;
324         struct ad799x_state *st = iio_priv(private);
325         u8 status;
326         int i, ret;
327
328         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
329         if (ret)
330                 goto done;
331
332         if (!status)
333                 goto done;
334
335         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
336
337         for (i = 0; i < 8; i++) {
338                 if (status & (1 << i))
339                         iio_push_event(indio_dev,
340                                        i & 0x1 ?
341                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
342                                                             (i >> 1),
343                                                             IIO_EV_TYPE_THRESH,
344                                                             IIO_EV_DIR_RISING) :
345                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
346                                                             (i >> 1),
347                                                             IIO_EV_TYPE_THRESH,
348                                                             IIO_EV_DIR_FALLING),
349                                        iio_get_time_ns());
350         }
351
352 done:
353         return IRQ_HANDLED;
354 }
355
356 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
357                               ad799x_read_frequency,
358                               ad799x_write_frequency);
359 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
360
361 static struct attribute *ad799x_event_attributes[] = {
362         &iio_dev_attr_sampling_frequency.dev_attr.attr,
363         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
364         NULL,
365 };
366
367 static struct attribute_group ad799x_event_attrs_group = {
368         .attrs = ad799x_event_attributes,
369         .name = "events",
370 };
371
372 static const struct iio_info ad7991_info = {
373         .read_raw = &ad799x_read_raw,
374         .driver_module = THIS_MODULE,
375 };
376
377 static const struct iio_info ad7993_4_7_8_info = {
378         .read_raw = &ad799x_read_raw,
379         .event_attrs = &ad799x_event_attrs_group,
380         .read_event_config = &ad799x_read_event_config,
381         .read_event_value = &ad799x_read_event_value,
382         .write_event_value = &ad799x_write_event_value,
383         .driver_module = THIS_MODULE,
384         .update_scan_mode = ad7997_8_update_scan_mode,
385 };
386
387 static const struct iio_event_spec ad799x_events[] = {
388         {
389                 .type = IIO_EV_TYPE_THRESH,
390                 .dir = IIO_EV_DIR_RISING,
391                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
392                         BIT(IIO_EV_INFO_ENABLE),
393         }, {
394                 .type = IIO_EV_TYPE_THRESH,
395                 .dir = IIO_EV_DIR_FALLING,
396                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
397                         BIT(IIO_EV_INFO_ENABLE),
398         }, {
399                 .type = IIO_EV_TYPE_THRESH,
400                 .dir = IIO_EV_DIR_EITHER,
401                 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
402         },
403 };
404
405 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
406         .type = IIO_VOLTAGE, \
407         .indexed = 1, \
408         .channel = (_index), \
409         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
410         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
411         .scan_index = (_index), \
412         .scan_type = { \
413                 .sign = 'u', \
414                 .realbits = (_realbits), \
415                 .storagebits = 16, \
416                 .shift = 12 - (_realbits), \
417                 .endianness = IIO_BE, \
418         }, \
419         .event_spec = _ev_spec, \
420         .num_event_specs = _num_ev_spec, \
421 }
422
423 #define AD799X_CHANNEL(_index, _realbits) \
424         _AD799X_CHANNEL(_index, _realbits, NULL, 0)
425
426 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
427         _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
428                 ARRAY_SIZE(ad799x_events))
429
430 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
431         [ad7991] = {
432                 .channel = {
433                         AD799X_CHANNEL(0, 12),
434                         AD799X_CHANNEL(1, 12),
435                         AD799X_CHANNEL(2, 12),
436                         AD799X_CHANNEL(3, 12),
437                         IIO_CHAN_SOFT_TIMESTAMP(4),
438                 },
439                 .num_channels = 5,
440                 .info = &ad7991_info,
441         },
442         [ad7995] = {
443                 .channel = {
444                         AD799X_CHANNEL(0, 10),
445                         AD799X_CHANNEL(1, 10),
446                         AD799X_CHANNEL(2, 10),
447                         AD799X_CHANNEL(3, 10),
448                         IIO_CHAN_SOFT_TIMESTAMP(4),
449                 },
450                 .num_channels = 5,
451                 .info = &ad7991_info,
452         },
453         [ad7999] = {
454                 .channel = {
455                         AD799X_CHANNEL(0, 8),
456                         AD799X_CHANNEL(1, 8),
457                         AD799X_CHANNEL(2, 8),
458                         AD799X_CHANNEL(3, 8),
459                         IIO_CHAN_SOFT_TIMESTAMP(4),
460                 },
461                 .num_channels = 5,
462                 .info = &ad7991_info,
463         },
464         [ad7992] = {
465                 .channel = {
466                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
467                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
468                         IIO_CHAN_SOFT_TIMESTAMP(3),
469                 },
470                 .num_channels = 3,
471                 .default_config = AD7998_ALERT_EN,
472                 .info = &ad7993_4_7_8_info,
473         },
474         [ad7993] = {
475                 .channel = {
476                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
477                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
478                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
479                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
480                         IIO_CHAN_SOFT_TIMESTAMP(4),
481                 },
482                 .num_channels = 5,
483                 .default_config = AD7998_ALERT_EN,
484                 .info = &ad7993_4_7_8_info,
485         },
486         [ad7994] = {
487                 .channel = {
488                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
489                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
490                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
491                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
492                         IIO_CHAN_SOFT_TIMESTAMP(4),
493                 },
494                 .num_channels = 5,
495                 .default_config = AD7998_ALERT_EN,
496                 .info = &ad7993_4_7_8_info,
497         },
498         [ad7997] = {
499                 .channel = {
500                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
501                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
502                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
503                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
504                         AD799X_CHANNEL(4, 10),
505                         AD799X_CHANNEL(5, 10),
506                         AD799X_CHANNEL(6, 10),
507                         AD799X_CHANNEL(7, 10),
508                         IIO_CHAN_SOFT_TIMESTAMP(8),
509                 },
510                 .num_channels = 9,
511                 .default_config = AD7998_ALERT_EN,
512                 .info = &ad7993_4_7_8_info,
513         },
514         [ad7998] = {
515                 .channel = {
516                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
517                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
518                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
519                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
520                         AD799X_CHANNEL(4, 12),
521                         AD799X_CHANNEL(5, 12),
522                         AD799X_CHANNEL(6, 12),
523                         AD799X_CHANNEL(7, 12),
524                         IIO_CHAN_SOFT_TIMESTAMP(8),
525                 },
526                 .num_channels = 9,
527                 .default_config = AD7998_ALERT_EN,
528                 .info = &ad7993_4_7_8_info,
529         },
530 };
531
532 static int ad799x_probe(struct i2c_client *client,
533                                    const struct i2c_device_id *id)
534 {
535         int ret;
536         struct ad799x_platform_data *pdata = client->dev.platform_data;
537         struct ad799x_state *st;
538         struct iio_dev *indio_dev;
539
540         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
541         if (indio_dev == NULL)
542                 return -ENOMEM;
543
544         st = iio_priv(indio_dev);
545         /* this is only used for device removal purposes */
546         i2c_set_clientdata(client, indio_dev);
547
548         st->id = id->driver_data;
549         st->chip_info = &ad799x_chip_info_tbl[st->id];
550         st->config = st->chip_info->default_config;
551
552         /* TODO: Add pdata options for filtering and bit delay */
553
554         if (!pdata)
555                 return -EINVAL;
556
557         st->int_vref_mv = pdata->vref_mv;
558
559         st->reg = devm_regulator_get(&client->dev, "vcc");
560         if (!IS_ERR(st->reg)) {
561                 ret = regulator_enable(st->reg);
562                 if (ret)
563                         return ret;
564         }
565         st->client = client;
566
567         indio_dev->dev.parent = &client->dev;
568         indio_dev->name = id->name;
569         indio_dev->info = st->chip_info->info;
570
571         indio_dev->modes = INDIO_DIRECT_MODE;
572         indio_dev->channels = st->chip_info->channel;
573         indio_dev->num_channels = st->chip_info->num_channels;
574
575         ret = ad799x_register_ring_funcs_and_init(indio_dev);
576         if (ret)
577                 goto error_disable_reg;
578
579         if (client->irq > 0) {
580                 ret = request_threaded_irq(client->irq,
581                                            NULL,
582                                            ad799x_event_handler,
583                                            IRQF_TRIGGER_FALLING |
584                                            IRQF_ONESHOT,
585                                            client->name,
586                                            indio_dev);
587                 if (ret)
588                         goto error_cleanup_ring;
589         }
590         ret = iio_device_register(indio_dev);
591         if (ret)
592                 goto error_free_irq;
593
594         return 0;
595
596 error_free_irq:
597         if (client->irq > 0)
598                 free_irq(client->irq, indio_dev);
599 error_cleanup_ring:
600         ad799x_ring_cleanup(indio_dev);
601 error_disable_reg:
602         if (!IS_ERR(st->reg))
603                 regulator_disable(st->reg);
604
605         return ret;
606 }
607
608 static int ad799x_remove(struct i2c_client *client)
609 {
610         struct iio_dev *indio_dev = i2c_get_clientdata(client);
611         struct ad799x_state *st = iio_priv(indio_dev);
612
613         iio_device_unregister(indio_dev);
614         if (client->irq > 0)
615                 free_irq(client->irq, indio_dev);
616
617         ad799x_ring_cleanup(indio_dev);
618         if (!IS_ERR(st->reg))
619                 regulator_disable(st->reg);
620         kfree(st->rx_buf);
621
622         return 0;
623 }
624
625 static const struct i2c_device_id ad799x_id[] = {
626         { "ad7991", ad7991 },
627         { "ad7995", ad7995 },
628         { "ad7999", ad7999 },
629         { "ad7992", ad7992 },
630         { "ad7993", ad7993 },
631         { "ad7994", ad7994 },
632         { "ad7997", ad7997 },
633         { "ad7998", ad7998 },
634         {}
635 };
636
637 MODULE_DEVICE_TABLE(i2c, ad799x_id);
638
639 static struct i2c_driver ad799x_driver = {
640         .driver = {
641                 .name = "ad799x",
642         },
643         .probe = ad799x_probe,
644         .remove = ad799x_remove,
645         .id_table = ad799x_id,
646 };
647 module_i2c_driver(ad799x_driver);
648
649 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
650 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
651 MODULE_LICENSE("GPL v2");