staging:iio: Add missing MODULE_DEVICE_TABLE and MODULE_ALIAS
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / dac / ad5446.c
1 /*
2  * AD5446 SPI DAC driver
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "dac.h"
24
25 #include "ad5446.h"
26
27 static void ad5446_store_sample(struct ad5446_state *st, unsigned val)
28 {
29         st->data.d16 = cpu_to_be16(AD5446_LOAD | val);
30 }
31
32 static void ad5542_store_sample(struct ad5446_state *st, unsigned val)
33 {
34         st->data.d16 = cpu_to_be16(val);
35 }
36
37 static void ad5620_store_sample(struct ad5446_state *st, unsigned val)
38 {
39         st->data.d16 = cpu_to_be16(AD5620_LOAD | val);
40 }
41
42 static void ad5660_store_sample(struct ad5446_state *st, unsigned val)
43 {
44         val |= AD5660_LOAD;
45         st->data.d24[0] = (val >> 16) & 0xFF;
46         st->data.d24[1] = (val >> 8) & 0xFF;
47         st->data.d24[2] = val & 0xFF;
48 }
49
50 static void ad5620_store_pwr_down(struct ad5446_state *st, unsigned mode)
51 {
52         st->data.d16 = cpu_to_be16(mode << 14);
53 }
54
55 static void ad5660_store_pwr_down(struct ad5446_state *st, unsigned mode)
56 {
57         unsigned val = mode << 16;
58
59         st->data.d24[0] = (val >> 16) & 0xFF;
60         st->data.d24[1] = (val >> 8) & 0xFF;
61         st->data.d24[2] = val & 0xFF;
62 }
63
64 static ssize_t ad5446_write_powerdown_mode(struct device *dev,
65                                        struct device_attribute *attr,
66                                        const char *buf, size_t len)
67 {
68         struct iio_dev *indio_dev = dev_get_drvdata(dev);
69         struct ad5446_state *st = iio_priv(indio_dev);
70
71         if (sysfs_streq(buf, "1kohm_to_gnd"))
72                 st->pwr_down_mode = MODE_PWRDWN_1k;
73         else if (sysfs_streq(buf, "100kohm_to_gnd"))
74                 st->pwr_down_mode = MODE_PWRDWN_100k;
75         else if (sysfs_streq(buf, "three_state"))
76                 st->pwr_down_mode = MODE_PWRDWN_TRISTATE;
77         else
78                 return -EINVAL;
79
80         return len;
81 }
82
83 static ssize_t ad5446_read_powerdown_mode(struct device *dev,
84                                       struct device_attribute *attr, char *buf)
85 {
86         struct iio_dev *indio_dev = dev_get_drvdata(dev);
87         struct ad5446_state *st = iio_priv(indio_dev);
88
89         char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
90
91         return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
92 }
93
94 static ssize_t ad5446_read_dac_powerdown(struct device *dev,
95                                            struct device_attribute *attr,
96                                            char *buf)
97 {
98         struct iio_dev *indio_dev = dev_get_drvdata(dev);
99         struct ad5446_state *st = iio_priv(indio_dev);
100
101         return sprintf(buf, "%d\n", st->pwr_down);
102 }
103
104 static ssize_t ad5446_write_dac_powerdown(struct device *dev,
105                                             struct device_attribute *attr,
106                                             const char *buf, size_t len)
107 {
108         struct iio_dev *indio_dev = dev_get_drvdata(dev);
109         struct ad5446_state *st = iio_priv(indio_dev);
110         unsigned long readin;
111         int ret;
112
113         ret = strict_strtol(buf, 10, &readin);
114         if (ret)
115                 return ret;
116
117         if (readin > 1)
118                 ret = -EINVAL;
119
120         mutex_lock(&indio_dev->mlock);
121         st->pwr_down = readin;
122
123         if (st->pwr_down)
124                 st->chip_info->store_pwr_down(st, st->pwr_down_mode);
125         else
126                 st->chip_info->store_sample(st, st->cached_val);
127
128         ret = spi_sync(st->spi, &st->msg);
129         mutex_unlock(&indio_dev->mlock);
130
131         return ret ? ret : len;
132 }
133
134 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO | S_IWUSR,
135                         ad5446_read_powerdown_mode,
136                         ad5446_write_powerdown_mode, 0);
137
138 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
139                         "1kohm_to_gnd 100kohm_to_gnd three_state");
140
141 static IIO_DEVICE_ATTR(out_voltage0_powerdown, S_IRUGO | S_IWUSR,
142                         ad5446_read_dac_powerdown,
143                         ad5446_write_dac_powerdown, 0);
144
145 static struct attribute *ad5446_attributes[] = {
146         &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
147         &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
148         &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
149         NULL,
150 };
151
152 static mode_t ad5446_attr_is_visible(struct kobject *kobj,
153                                      struct attribute *attr, int n)
154 {
155         struct device *dev = container_of(kobj, struct device, kobj);
156         struct iio_dev *indio_dev = dev_get_drvdata(dev);
157         struct ad5446_state *st = iio_priv(indio_dev);
158
159         mode_t mode = attr->mode;
160
161         if (!st->chip_info->store_pwr_down &&
162                 (attr == &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr ||
163                 attr == &iio_dev_attr_out_voltage_powerdown_mode.
164                  dev_attr.attr ||
165                 attr ==
166                 &iio_const_attr_out_voltage_powerdown_mode_available.
167                  dev_attr.attr))
168                 mode = 0;
169
170         return mode;
171 }
172
173 static const struct attribute_group ad5446_attribute_group = {
174         .attrs = ad5446_attributes,
175         .is_visible = ad5446_attr_is_visible,
176 };
177
178 #define AD5446_CHANNEL(bits, storage, shift) { \
179         .type = IIO_VOLTAGE, \
180         .indexed = 1, \
181         .output = 1, \
182         .channel = 0, \
183         .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
184         .scan_type = IIO_ST('u', (bits), (storage), (shift)) \
185 }
186
187 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
188         [ID_AD5444] = {
189                 .channel = AD5446_CHANNEL(12, 16, 2),
190                 .store_sample = ad5446_store_sample,
191         },
192         [ID_AD5446] = {
193                 .channel = AD5446_CHANNEL(14, 16, 0),
194                 .store_sample = ad5446_store_sample,
195         },
196         [ID_AD5541A] = {
197                 .channel = AD5446_CHANNEL(16, 16, 0),
198                 .store_sample = ad5542_store_sample,
199         },
200         [ID_AD5542A] = {
201                 .channel = AD5446_CHANNEL(16, 16, 0),
202                 .store_sample = ad5542_store_sample,
203         },
204         [ID_AD5543] = {
205                 .channel = AD5446_CHANNEL(16, 16, 0),
206                 .store_sample = ad5542_store_sample,
207         },
208         [ID_AD5512A] = {
209                 .channel = AD5446_CHANNEL(12, 16, 4),
210                 .store_sample = ad5542_store_sample,
211         },
212         [ID_AD5553] = {
213                 .channel = AD5446_CHANNEL(14, 16, 0),
214                 .store_sample = ad5542_store_sample,
215         },
216         [ID_AD5601] = {
217                 .channel = AD5446_CHANNEL(8, 16, 6),
218                 .store_sample = ad5542_store_sample,
219                 .store_pwr_down = ad5620_store_pwr_down,
220         },
221         [ID_AD5611] = {
222                 .channel = AD5446_CHANNEL(10, 16, 4),
223                 .store_sample = ad5542_store_sample,
224                 .store_pwr_down = ad5620_store_pwr_down,
225         },
226         [ID_AD5621] = {
227                 .channel = AD5446_CHANNEL(12, 16, 2),
228                 .store_sample = ad5542_store_sample,
229                 .store_pwr_down = ad5620_store_pwr_down,
230         },
231         [ID_AD5620_2500] = {
232                 .channel = AD5446_CHANNEL(12, 16, 2),
233                 .int_vref_mv = 2500,
234                 .store_sample = ad5620_store_sample,
235                 .store_pwr_down = ad5620_store_pwr_down,
236         },
237         [ID_AD5620_1250] = {
238                 .channel = AD5446_CHANNEL(12, 16, 2),
239                 .int_vref_mv = 1250,
240                 .store_sample = ad5620_store_sample,
241                 .store_pwr_down = ad5620_store_pwr_down,
242         },
243         [ID_AD5640_2500] = {
244                 .channel = AD5446_CHANNEL(14, 16, 0),
245                 .int_vref_mv = 2500,
246                 .store_sample = ad5620_store_sample,
247                 .store_pwr_down = ad5620_store_pwr_down,
248         },
249         [ID_AD5640_1250] = {
250                 .channel = AD5446_CHANNEL(14, 16, 0),
251                 .int_vref_mv = 1250,
252                 .store_sample = ad5620_store_sample,
253                 .store_pwr_down = ad5620_store_pwr_down,
254         },
255         [ID_AD5660_2500] = {
256                 .channel = AD5446_CHANNEL(16, 16, 0),
257                 .int_vref_mv = 2500,
258                 .store_sample = ad5660_store_sample,
259                 .store_pwr_down = ad5660_store_pwr_down,
260         },
261         [ID_AD5660_1250] = {
262                 .channel = AD5446_CHANNEL(16, 16, 0),
263                 .int_vref_mv = 1250,
264                 .store_sample = ad5660_store_sample,
265                 .store_pwr_down = ad5660_store_pwr_down,
266         },
267 };
268
269 static int ad5446_read_raw(struct iio_dev *indio_dev,
270                            struct iio_chan_spec const *chan,
271                            int *val,
272                            int *val2,
273                            long m)
274 {
275         struct ad5446_state *st = iio_priv(indio_dev);
276         unsigned long scale_uv;
277
278         switch (m) {
279         case IIO_CHAN_INFO_SCALE:
280                 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
281                 *val =  scale_uv / 1000;
282                 *val2 = (scale_uv % 1000) * 1000;
283                 return IIO_VAL_INT_PLUS_MICRO;
284
285         }
286         return -EINVAL;
287 }
288
289 static int ad5446_write_raw(struct iio_dev *indio_dev,
290                                struct iio_chan_spec const *chan,
291                                int val,
292                                int val2,
293                                long mask)
294 {
295         struct ad5446_state *st = iio_priv(indio_dev);
296         int ret;
297
298         switch (mask) {
299         case 0:
300                 if (val >= (1 << chan->scan_type.realbits) || val < 0)
301                         return -EINVAL;
302
303                 val <<= chan->scan_type.shift;
304                 mutex_lock(&indio_dev->mlock);
305                 st->cached_val = val;
306                 st->chip_info->store_sample(st, val);
307                 ret = spi_sync(st->spi, &st->msg);
308                 mutex_unlock(&indio_dev->mlock);
309                 break;
310         default:
311                 ret = -EINVAL;
312         }
313
314         return ret;
315 }
316
317 static const struct iio_info ad5446_info = {
318         .read_raw = ad5446_read_raw,
319         .write_raw = ad5446_write_raw,
320         .attrs = &ad5446_attribute_group,
321         .driver_module = THIS_MODULE,
322 };
323
324 static int __devinit ad5446_probe(struct spi_device *spi)
325 {
326         struct ad5446_state *st;
327         struct iio_dev *indio_dev;
328         struct regulator *reg;
329         int ret, voltage_uv = 0;
330
331         reg = regulator_get(&spi->dev, "vcc");
332         if (!IS_ERR(reg)) {
333                 ret = regulator_enable(reg);
334                 if (ret)
335                         goto error_put_reg;
336
337                 voltage_uv = regulator_get_voltage(reg);
338         }
339
340         indio_dev = iio_allocate_device(sizeof(*st));
341         if (indio_dev == NULL) {
342                 ret = -ENOMEM;
343                 goto error_disable_reg;
344         }
345         st = iio_priv(indio_dev);
346         st->chip_info =
347                 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
348
349         spi_set_drvdata(spi, indio_dev);
350         st->reg = reg;
351         st->spi = spi;
352
353         /* Estabilish that the iio_dev is a child of the spi device */
354         indio_dev->dev.parent = &spi->dev;
355         indio_dev->name = spi_get_device_id(spi)->name;
356         indio_dev->info = &ad5446_info;
357         indio_dev->modes = INDIO_DIRECT_MODE;
358         indio_dev->channels = &st->chip_info->channel;
359         indio_dev->num_channels = 1;
360
361         /* Setup default message */
362
363         st->xfer.tx_buf = &st->data;
364         st->xfer.len = st->chip_info->channel.scan_type.storagebits / 8;
365
366         spi_message_init(&st->msg);
367         spi_message_add_tail(&st->xfer, &st->msg);
368
369         switch (spi_get_device_id(spi)->driver_data) {
370         case ID_AD5620_2500:
371         case ID_AD5620_1250:
372         case ID_AD5640_2500:
373         case ID_AD5640_1250:
374         case ID_AD5660_2500:
375         case ID_AD5660_1250:
376                 st->vref_mv = st->chip_info->int_vref_mv;
377                 break;
378         default:
379                 if (voltage_uv)
380                         st->vref_mv = voltage_uv / 1000;
381                 else
382                         dev_warn(&spi->dev,
383                                  "reference voltage unspecified\n");
384         }
385
386         ret = iio_device_register(indio_dev);
387         if (ret)
388                 goto error_free_device;
389
390         return 0;
391
392 error_free_device:
393         iio_free_device(indio_dev);
394 error_disable_reg:
395         if (!IS_ERR(reg))
396                 regulator_disable(reg);
397 error_put_reg:
398         if (!IS_ERR(reg))
399                 regulator_put(reg);
400
401         return ret;
402 }
403
404 static int ad5446_remove(struct spi_device *spi)
405 {
406         struct iio_dev *indio_dev = spi_get_drvdata(spi);
407         struct ad5446_state *st = iio_priv(indio_dev);
408
409         iio_device_unregister(indio_dev);
410         if (!IS_ERR(st->reg)) {
411                 regulator_disable(st->reg);
412                 regulator_put(st->reg);
413         }
414         iio_free_device(indio_dev);
415
416         return 0;
417 }
418
419 static const struct spi_device_id ad5446_id[] = {
420         {"ad5444", ID_AD5444},
421         {"ad5446", ID_AD5446},
422         {"ad5512a", ID_AD5512A},
423         {"ad5541a", ID_AD5541A},
424         {"ad5542a", ID_AD5542A},
425         {"ad5543", ID_AD5543},
426         {"ad5553", ID_AD5553},
427         {"ad5601", ID_AD5601},
428         {"ad5611", ID_AD5611},
429         {"ad5621", ID_AD5621},
430         {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
431         {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
432         {"ad5640-2500", ID_AD5640_2500},
433         {"ad5640-1250", ID_AD5640_1250},
434         {"ad5660-2500", ID_AD5660_2500},
435         {"ad5660-1250", ID_AD5660_1250},
436         {}
437 };
438 MODULE_DEVICE_TABLE(spi, ad5446_id);
439
440 static struct spi_driver ad5446_driver = {
441         .driver = {
442                 .name   = "ad5446",
443                 .bus    = &spi_bus_type,
444                 .owner  = THIS_MODULE,
445         },
446         .probe          = ad5446_probe,
447         .remove         = __devexit_p(ad5446_remove),
448         .id_table       = ad5446_id,
449 };
450
451 static int __init ad5446_init(void)
452 {
453         return spi_register_driver(&ad5446_driver);
454 }
455 module_init(ad5446_init);
456
457 static void __exit ad5446_exit(void)
458 {
459         spi_unregister_driver(&ad5446_driver);
460 }
461 module_exit(ad5446_exit);
462
463 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
464 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
465 MODULE_LICENSE("GPL v2");