]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/iio_simple_dummy.c
Merge remote-tracking branch 'usb-gadget/next'
[karo-tx-linux.git] / drivers / staging / iio / iio_simple_dummy.c
1 /**
2  * Copyright (c) 2011 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * A reference industrial I/O driver to illustrate the functionality available.
9  *
10  * There are numerous real drivers to illustrate the finer points.
11  * The purpose of this driver is to provide a driver with far more comments
12  * and explanatory notes than any 'real' driver would have.
13  * Anyone starting out writing an IIO driver should first make sure they
14  * understand all of this driver except those bits specifically marked
15  * as being present to allow us to 'fake' the presence of hardware.
16  */
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/events.h>
25 #include <linux/iio/buffer.h>
26 #include "iio_simple_dummy.h"
27
28 /*
29  * A few elements needed to fake a bus for this driver
30  * Note instances parameter controls how many of these
31  * dummy devices are registered.
32  */
33 static unsigned instances = 1;
34 module_param(instances, int, 0);
35
36 /* Pointer array used to fake bus elements */
37 static struct iio_dev **iio_dummy_devs;
38
39 /* Fake a name for the part number, usually obtained from the id table */
40 static const char *iio_dummy_part_number = "iio_dummy_part_no";
41
42 /**
43  * struct iio_dummy_accel_calibscale - realworld to register mapping
44  * @val: first value in read_raw - here integer part.
45  * @val2: second value in read_raw etc - here micro part.
46  * @regval: register value - magic device specific numbers.
47  */
48 struct iio_dummy_accel_calibscale {
49         int val;
50         int val2;
51         int regval; /* what would be written to hardware */
52 };
53
54 static const struct iio_dummy_accel_calibscale dummy_scales[] = {
55         { 0, 100, 0x8 }, /* 0.000100 */
56         { 0, 133, 0x7 }, /* 0.000133 */
57         { 733, 13, 0x9 }, /* 733.000013 */
58 };
59
60 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
61
62 /*
63  * simple event - triggered when value rises above
64  * a threshold
65  */
66 static const struct iio_event_spec iio_dummy_event = {
67         .type = IIO_EV_TYPE_THRESH,
68         .dir = IIO_EV_DIR_RISING,
69         .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
70 };
71
72 /*
73  * simple step detect event - triggered when a step is detected
74  */
75 static const struct iio_event_spec step_detect_event = {
76         .type = IIO_EV_TYPE_CHANGE,
77         .dir = IIO_EV_DIR_NONE,
78         .mask_separate = BIT(IIO_EV_INFO_ENABLE),
79 };
80
81 /*
82  * simple transition event - triggered when the reported running confidence
83  * value rises above a threshold value
84  */
85 static const struct iio_event_spec iio_running_event = {
86         .type = IIO_EV_TYPE_THRESH,
87         .dir = IIO_EV_DIR_RISING,
88         .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
89 };
90
91 /*
92  * simple transition event - triggered when the reported walking confidence
93  * value falls under a threshold value
94  */
95 static const struct iio_event_spec iio_walking_event = {
96         .type = IIO_EV_TYPE_THRESH,
97         .dir = IIO_EV_DIR_FALLING,
98         .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
99 };
100 #endif
101
102 /*
103  * iio_dummy_channels - Description of available channels
104  *
105  * This array of structures tells the IIO core about what the device
106  * actually provides for a given channel.
107  */
108 static const struct iio_chan_spec iio_dummy_channels[] = {
109         /* indexed ADC channel in_voltage0_raw etc */
110         {
111                 .type = IIO_VOLTAGE,
112                 /* Channel has a numeric index of 0 */
113                 .indexed = 1,
114                 .channel = 0,
115                 /* What other information is available? */
116                 .info_mask_separate =
117                 /*
118                  * in_voltage0_raw
119                  * Raw (unscaled no bias removal etc) measurement
120                  * from the device.
121                  */
122                 BIT(IIO_CHAN_INFO_RAW) |
123                 /*
124                  * in_voltage0_offset
125                  * Offset for userspace to apply prior to scale
126                  * when converting to standard units (microvolts)
127                  */
128                 BIT(IIO_CHAN_INFO_OFFSET) |
129                 /*
130                  * in_voltage0_scale
131                  * Multipler for userspace to apply post offset
132                  * when converting to standard units (microvolts)
133                  */
134                 BIT(IIO_CHAN_INFO_SCALE),
135                 /*
136                  * sampling_frequency
137                  * The frequency in Hz at which the channels are sampled
138                  */
139                 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
140                 /* The ordering of elements in the buffer via an enum */
141                 .scan_index = voltage0,
142                 .scan_type = { /* Description of storage in buffer */
143                         .sign = 'u', /* unsigned */
144                         .realbits = 13, /* 13 bits */
145                         .storagebits = 16, /* 16 bits used for storage */
146                         .shift = 0, /* zero shift */
147                 },
148 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
149                 .event_spec = &iio_dummy_event,
150                 .num_event_specs = 1,
151 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
152         },
153         /* Differential ADC channel in_voltage1-voltage2_raw etc*/
154         {
155                 .type = IIO_VOLTAGE,
156                 .differential = 1,
157                 /*
158                  * Indexing for differential channels uses channel
159                  * for the positive part, channel2 for the negative.
160                  */
161                 .indexed = 1,
162                 .channel = 1,
163                 .channel2 = 2,
164                 /*
165                  * in_voltage1-voltage2_raw
166                  * Raw (unscaled no bias removal etc) measurement
167                  * from the device.
168                  */
169                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
170                 /*
171                  * in_voltage-voltage_scale
172                  * Shared version of scale - shared by differential
173                  * input channels of type IIO_VOLTAGE.
174                  */
175                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
176                 /*
177                  * sampling_frequency
178                  * The frequency in Hz at which the channels are sampled
179                  */
180                 .scan_index = diffvoltage1m2,
181                 .scan_type = { /* Description of storage in buffer */
182                         .sign = 's', /* signed */
183                         .realbits = 12, /* 12 bits */
184                         .storagebits = 16, /* 16 bits used for storage */
185                         .shift = 0, /* zero shift */
186                 },
187         },
188         /* Differential ADC channel in_voltage3-voltage4_raw etc*/
189         {
190                 .type = IIO_VOLTAGE,
191                 .differential = 1,
192                 .indexed = 1,
193                 .channel = 3,
194                 .channel2 = 4,
195                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
196                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
197                 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
198                 .scan_index = diffvoltage3m4,
199                 .scan_type = {
200                         .sign = 's',
201                         .realbits = 11,
202                         .storagebits = 16,
203                         .shift = 0,
204                 },
205         },
206         /*
207          * 'modified' (i.e. axis specified) acceleration channel
208          * in_accel_z_raw
209          */
210         {
211                 .type = IIO_ACCEL,
212                 .modified = 1,
213                 /* Channel 2 is use for modifiers */
214                 .channel2 = IIO_MOD_X,
215                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
216                 /*
217                  * Internal bias and gain correction values. Applied
218                  * by the hardware or driver prior to userspace
219                  * seeing the readings. Typically part of hardware
220                  * calibration.
221                  */
222                 BIT(IIO_CHAN_INFO_CALIBSCALE) |
223                 BIT(IIO_CHAN_INFO_CALIBBIAS),
224                 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
225                 .scan_index = accelx,
226                 .scan_type = { /* Description of storage in buffer */
227                         .sign = 's', /* signed */
228                         .realbits = 16, /* 16 bits */
229                         .storagebits = 16, /* 16 bits used for storage */
230                         .shift = 0, /* zero shift */
231                 },
232         },
233         /*
234          * Convenience macro for timestamps. 4 is the index in
235          * the buffer.
236          */
237         IIO_CHAN_SOFT_TIMESTAMP(4),
238         /* DAC channel out_voltage0_raw */
239         {
240                 .type = IIO_VOLTAGE,
241                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
242                 .scan_index = -1, /* No buffer support */
243                 .output = 1,
244                 .indexed = 1,
245                 .channel = 0,
246         },
247         {
248                 .type = IIO_STEPS,
249                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
250                         BIT(IIO_CHAN_INFO_CALIBHEIGHT),
251                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
252                 .scan_index = -1, /* No buffer support */
253 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
254                 .event_spec = &step_detect_event,
255                 .num_event_specs = 1,
256 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
257         },
258         {
259                 .type = IIO_ACTIVITY,
260                 .modified = 1,
261                 .channel2 = IIO_MOD_RUNNING,
262                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
263                 .scan_index = -1, /* No buffer support */
264 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
265                 .event_spec = &iio_running_event,
266                 .num_event_specs = 1,
267 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
268         },
269         {
270                 .type = IIO_ACTIVITY,
271                 .modified = 1,
272                 .channel2 = IIO_MOD_WALKING,
273                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
274                 .scan_index = -1, /* No buffer support */
275 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
276                 .event_spec = &iio_walking_event,
277                 .num_event_specs = 1,
278 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
279         },
280 };
281
282 /**
283  * iio_dummy_read_raw() - data read function.
284  * @indio_dev:  the struct iio_dev associated with this device instance
285  * @chan:       the channel whose data is to be read
286  * @val:        first element of returned value (typically INT)
287  * @val2:       second element of returned value (typically MICRO)
288  * @mask:       what we actually want to read as per the info_mask_*
289  *              in iio_chan_spec.
290  */
291 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
292                               struct iio_chan_spec const *chan,
293                               int *val,
294                               int *val2,
295                               long mask)
296 {
297         struct iio_dummy_state *st = iio_priv(indio_dev);
298         int ret = -EINVAL;
299
300         mutex_lock(&st->lock);
301         switch (mask) {
302         case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
303                 switch (chan->type) {
304                 case IIO_VOLTAGE:
305                         if (chan->output) {
306                                 /* Set integer part to cached value */
307                                 *val = st->dac_val;
308                                 ret = IIO_VAL_INT;
309                         } else if (chan->differential) {
310                                 if (chan->channel == 1)
311                                         *val = st->differential_adc_val[0];
312                                 else
313                                         *val = st->differential_adc_val[1];
314                                 ret = IIO_VAL_INT;
315                         } else {
316                                 *val = st->single_ended_adc_val;
317                                 ret = IIO_VAL_INT;
318                         }
319                         break;
320                 case IIO_ACCEL:
321                         *val = st->accel_val;
322                         ret = IIO_VAL_INT;
323                         break;
324                 default:
325                         break;
326                 }
327                 break;
328         case IIO_CHAN_INFO_PROCESSED:
329                 switch (chan->type) {
330                 case IIO_STEPS:
331                         *val = st->steps;
332                         ret = IIO_VAL_INT;
333                         break;
334                 case IIO_ACTIVITY:
335                         switch (chan->channel2) {
336                         case IIO_MOD_RUNNING:
337                                 *val = st->activity_running;
338                                 ret = IIO_VAL_INT;
339                                 break;
340                         case IIO_MOD_WALKING:
341                                 *val = st->activity_walking;
342                                 ret = IIO_VAL_INT;
343                                 break;
344                         default:
345                                 break;
346                         }
347                         break;
348                 default:
349                         break;
350                 }
351                 break;
352         case IIO_CHAN_INFO_OFFSET:
353                 /* only single ended adc -> 7 */
354                 *val = 7;
355                 ret = IIO_VAL_INT;
356                 break;
357         case IIO_CHAN_INFO_SCALE:
358                 switch (chan->type) {
359                 case IIO_VOLTAGE:
360                         switch (chan->differential) {
361                         case 0:
362                                 /* only single ended adc -> 0.001333 */
363                                 *val = 0;
364                                 *val2 = 1333;
365                                 ret = IIO_VAL_INT_PLUS_MICRO;
366                                 break;
367                         case 1:
368                                 /* all differential adc channels ->
369                                  * 0.000001344 */
370                                 *val = 0;
371                                 *val2 = 1344;
372                                 ret = IIO_VAL_INT_PLUS_NANO;
373                         }
374                         break;
375                 default:
376                         break;
377                 }
378                 break;
379         case IIO_CHAN_INFO_CALIBBIAS:
380                 /* only the acceleration axis - read from cache */
381                 *val = st->accel_calibbias;
382                 ret = IIO_VAL_INT;
383                 break;
384         case IIO_CHAN_INFO_CALIBSCALE:
385                 *val = st->accel_calibscale->val;
386                 *val2 = st->accel_calibscale->val2;
387                 ret = IIO_VAL_INT_PLUS_MICRO;
388                 break;
389         case IIO_CHAN_INFO_SAMP_FREQ:
390                 *val = 3;
391                 *val2 = 33;
392                 ret = IIO_VAL_INT_PLUS_NANO;
393                 break;
394         case IIO_CHAN_INFO_ENABLE:
395                 switch (chan->type) {
396                 case IIO_STEPS:
397                         *val = st->steps_enabled;
398                         ret = IIO_VAL_INT;
399                         break;
400                 default:
401                         break;
402                 }
403                 break;
404         case IIO_CHAN_INFO_CALIBHEIGHT:
405                 switch (chan->type) {
406                 case IIO_STEPS:
407                         *val = st->height;
408                         ret = IIO_VAL_INT;
409                         break;
410                 default:
411                         break;
412                 }
413                 break;
414
415         default:
416                 break;
417         }
418         mutex_unlock(&st->lock);
419         return ret;
420 }
421
422 /**
423  * iio_dummy_write_raw() - data write function.
424  * @indio_dev:  the struct iio_dev associated with this device instance
425  * @chan:       the channel whose data is to be written
426  * @val:        first element of value to set (typically INT)
427  * @val2:       second element of value to set (typically MICRO)
428  * @mask:       what we actually want to write as per the info_mask_*
429  *              in iio_chan_spec.
430  *
431  * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
432  * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
433  * in struct iio_info is provided by the driver.
434  */
435 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
436                                struct iio_chan_spec const *chan,
437                                int val,
438                                int val2,
439                                long mask)
440 {
441         int i;
442         int ret = 0;
443         struct iio_dummy_state *st = iio_priv(indio_dev);
444
445         switch (mask) {
446         case IIO_CHAN_INFO_RAW:
447                 switch (chan->type) {
448                 case IIO_VOLTAGE:
449                         if (chan->output == 0)
450                                 return -EINVAL;
451
452                         /* Locking not required as writing single value */
453                         mutex_lock(&st->lock);
454                         st->dac_val = val;
455                         mutex_unlock(&st->lock);
456                         return 0;
457                 default:
458                         return -EINVAL;
459                 }
460         case IIO_CHAN_INFO_PROCESSED:
461                 switch (chan->type) {
462                 case IIO_STEPS:
463                         mutex_lock(&st->lock);
464                         st->steps = val;
465                         mutex_unlock(&st->lock);
466                         return 0;
467                 case IIO_ACTIVITY:
468                         if (val < 0)
469                                 val = 0;
470                         if (val > 100)
471                                 val = 100;
472                         switch (chan->channel2) {
473                         case IIO_MOD_RUNNING:
474                                 st->activity_running = val;
475                                 return 0;
476                         case IIO_MOD_WALKING:
477                                 st->activity_walking = val;
478                                 return 0;
479                         default:
480                                 return -EINVAL;
481                         }
482                         break;
483                 default:
484                         return -EINVAL;
485                 }
486         case IIO_CHAN_INFO_CALIBSCALE:
487                 mutex_lock(&st->lock);
488                 /* Compare against table - hard matching here */
489                 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
490                         if (val == dummy_scales[i].val &&
491                             val2 == dummy_scales[i].val2)
492                                 break;
493                 if (i == ARRAY_SIZE(dummy_scales))
494                         ret = -EINVAL;
495                 else
496                         st->accel_calibscale = &dummy_scales[i];
497                 mutex_unlock(&st->lock);
498                 return ret;
499         case IIO_CHAN_INFO_CALIBBIAS:
500                 mutex_lock(&st->lock);
501                 st->accel_calibbias = val;
502                 mutex_unlock(&st->lock);
503                 return 0;
504         case IIO_CHAN_INFO_ENABLE:
505                 switch (chan->type) {
506                 case IIO_STEPS:
507                         mutex_lock(&st->lock);
508                         st->steps_enabled = val;
509                         mutex_unlock(&st->lock);
510                         return 0;
511                 default:
512                         return -EINVAL;
513                 }
514         case IIO_CHAN_INFO_CALIBHEIGHT:
515                 switch (chan->type) {
516                 case IIO_STEPS:
517                         st->height = val;
518                         return 0;
519                 default:
520                         return -EINVAL;
521                 }
522
523         default:
524                 return -EINVAL;
525         }
526 }
527
528 /*
529  * Device type specific information.
530  */
531 static const struct iio_info iio_dummy_info = {
532         .driver_module = THIS_MODULE,
533         .read_raw = &iio_dummy_read_raw,
534         .write_raw = &iio_dummy_write_raw,
535 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
536         .read_event_config = &iio_simple_dummy_read_event_config,
537         .write_event_config = &iio_simple_dummy_write_event_config,
538         .read_event_value = &iio_simple_dummy_read_event_value,
539         .write_event_value = &iio_simple_dummy_write_event_value,
540 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
541 };
542
543 /**
544  * iio_dummy_init_device() - device instance specific init
545  * @indio_dev: the iio device structure
546  *
547  * Most drivers have one of these to set up default values,
548  * reset the device to known state etc.
549  */
550 static int iio_dummy_init_device(struct iio_dev *indio_dev)
551 {
552         struct iio_dummy_state *st = iio_priv(indio_dev);
553
554         st->dac_val = 0;
555         st->single_ended_adc_val = 73;
556         st->differential_adc_val[0] = 33;
557         st->differential_adc_val[1] = -34;
558         st->accel_val = 34;
559         st->accel_calibbias = -7;
560         st->accel_calibscale = &dummy_scales[0];
561         st->steps = 47;
562         st->activity_running = 98;
563         st->activity_walking = 4;
564
565         return 0;
566 }
567
568 /**
569  * iio_dummy_probe() - device instance probe
570  * @index: an id number for this instance.
571  *
572  * Arguments are bus type specific.
573  * I2C: iio_dummy_probe(struct i2c_client *client,
574  *                      const struct i2c_device_id *id)
575  * SPI: iio_dummy_probe(struct spi_device *spi)
576  */
577 static int iio_dummy_probe(int index)
578 {
579         int ret;
580         struct iio_dev *indio_dev;
581         struct iio_dummy_state *st;
582
583         /*
584          * Allocate an IIO device.
585          *
586          * This structure contains all generic state
587          * information about the device instance.
588          * It also has a region (accessed by iio_priv()
589          * for chip specific state information.
590          */
591         indio_dev = iio_device_alloc(sizeof(*st));
592         if (indio_dev == NULL) {
593                 ret = -ENOMEM;
594                 goto error_ret;
595         }
596
597         st = iio_priv(indio_dev);
598         mutex_init(&st->lock);
599
600         iio_dummy_init_device(indio_dev);
601         /*
602          * With hardware: Set the parent device.
603          * indio_dev->dev.parent = &spi->dev;
604          * indio_dev->dev.parent = &client->dev;
605          */
606
607          /*
608          * Make the iio_dev struct available to remove function.
609          * Bus equivalents
610          * i2c_set_clientdata(client, indio_dev);
611          * spi_set_drvdata(spi, indio_dev);
612          */
613         iio_dummy_devs[index] = indio_dev;
614
615
616         /*
617          * Set the device name.
618          *
619          * This is typically a part number and obtained from the module
620          * id table.
621          * e.g. for i2c and spi:
622          *    indio_dev->name = id->name;
623          *    indio_dev->name = spi_get_device_id(spi)->name;
624          */
625         indio_dev->name = iio_dummy_part_number;
626
627         /* Provide description of available channels */
628         indio_dev->channels = iio_dummy_channels;
629         indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
630
631         /*
632          * Provide device type specific interface functions and
633          * constant data.
634          */
635         indio_dev->info = &iio_dummy_info;
636
637         /* Specify that device provides sysfs type interfaces */
638         indio_dev->modes = INDIO_DIRECT_MODE;
639
640         ret = iio_simple_dummy_events_register(indio_dev);
641         if (ret < 0)
642                 goto error_free_device;
643
644         ret = iio_simple_dummy_configure_buffer(indio_dev);
645         if (ret < 0)
646                 goto error_unregister_events;
647
648         ret = iio_device_register(indio_dev);
649         if (ret < 0)
650                 goto error_unconfigure_buffer;
651
652         return 0;
653 error_unconfigure_buffer:
654         iio_simple_dummy_unconfigure_buffer(indio_dev);
655 error_unregister_events:
656         iio_simple_dummy_events_unregister(indio_dev);
657 error_free_device:
658         iio_device_free(indio_dev);
659 error_ret:
660         return ret;
661 }
662
663 /**
664  * iio_dummy_remove() - device instance removal function
665  * @index: device index.
666  *
667  * Parameters follow those of iio_dummy_probe for buses.
668  */
669 static int iio_dummy_remove(int index)
670 {
671         int ret;
672         /*
673          * Get a pointer to the device instance iio_dev structure
674          * from the bus subsystem. E.g.
675          * struct iio_dev *indio_dev = i2c_get_clientdata(client);
676          * struct iio_dev *indio_dev = spi_get_drvdata(spi);
677          */
678         struct iio_dev *indio_dev = iio_dummy_devs[index];
679
680
681         /* Unregister the device */
682         iio_device_unregister(indio_dev);
683
684         /* Device specific code to power down etc */
685
686         /* Buffered capture related cleanup */
687         iio_simple_dummy_unconfigure_buffer(indio_dev);
688
689         ret = iio_simple_dummy_events_unregister(indio_dev);
690         if (ret)
691                 goto error_ret;
692
693         /* Free all structures */
694         iio_device_free(indio_dev);
695
696 error_ret:
697         return ret;
698 }
699
700 /**
701  * iio_dummy_init() -  device driver registration
702  *
703  * Varies depending on bus type of the device. As there is no device
704  * here, call probe directly. For information on device registration
705  * i2c:
706  * Documentation/i2c/writing-clients
707  * spi:
708  * Documentation/spi/spi-summary
709  */
710 static __init int iio_dummy_init(void)
711 {
712         int i, ret;
713
714         if (instances > 10) {
715                 instances = 1;
716                 return -EINVAL;
717         }
718
719         /* Fake a bus */
720         iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
721                                  GFP_KERNEL);
722         /* Here we have no actual device so call probe */
723         for (i = 0; i < instances; i++) {
724                 ret = iio_dummy_probe(i);
725                 if (ret < 0)
726                         return ret;
727         }
728         return 0;
729 }
730 module_init(iio_dummy_init);
731
732 /**
733  * iio_dummy_exit() - device driver removal
734  *
735  * Varies depending on bus type of the device.
736  * As there is no device here, call remove directly.
737  */
738 static __exit void iio_dummy_exit(void)
739 {
740         int i;
741
742         for (i = 0; i < instances; i++)
743                 iio_dummy_remove(i);
744         kfree(iio_dummy_devs);
745 }
746 module_exit(iio_dummy_exit);
747
748 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
749 MODULE_DESCRIPTION("IIO dummy driver");
750 MODULE_LICENSE("GPL v2");