aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorDaniel Baluta <daniel.baluta@intel.com>2016-04-25 09:15:52 -0400
committerJonathan Cameron <jic23@kernel.org>2016-05-04 06:44:06 -0400
commit3d85fb6f81046b51e4428e14fb9643ea75648630 (patch)
tree0f789377dc6077134085de3eec585e1790d6613b /drivers/iio
parent0f3a8c3f34f728e7c96651bb7271e1c388c9aac2 (diff)
iio: dummy: Convert IIO dummy to configfs
We register a new device type named "dummy", this will create a configfs entry under: * /config/iio/devices/dummy. Creating dummy devices is now as simple as: $ mkdir /config/iio/devices/dummy/my_dummy_device Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/dummy/Kconfig1
-rw-r--r--drivers/iio/dummy/iio_simple_dummy.c102
2 files changed, 38 insertions, 65 deletions
diff --git a/drivers/iio/dummy/Kconfig b/drivers/iio/dummy/Kconfig
index 71805ced1aae..aa5824d96a43 100644
--- a/drivers/iio/dummy/Kconfig
+++ b/drivers/iio/dummy/Kconfig
@@ -10,6 +10,7 @@ config IIO_DUMMY_EVGEN
10 10
11config IIO_SIMPLE_DUMMY 11config IIO_SIMPLE_DUMMY
12 tristate "An example driver with no hardware requirements" 12 tristate "An example driver with no hardware requirements"
13 depends on IIO_SW_DEVICE
13 help 14 help
14 Driver intended mainly as documentation for how to write 15 Driver intended mainly as documentation for how to write
15 a driver. May also be useful for testing userspace code 16 a driver. May also be useful for testing userspace code
diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c
index 43fe4ba7d0dc..ad3410e528b6 100644
--- a/drivers/iio/dummy/iio_simple_dummy.c
+++ b/drivers/iio/dummy/iio_simple_dummy.c
@@ -17,26 +17,18 @@
17#include <linux/kernel.h> 17#include <linux/kernel.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/string.h>
20 21
21#include <linux/iio/iio.h> 22#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h> 23#include <linux/iio/sysfs.h>
23#include <linux/iio/events.h> 24#include <linux/iio/events.h>
24#include <linux/iio/buffer.h> 25#include <linux/iio/buffer.h>
26#include <linux/iio/sw_device.h>
25#include "iio_simple_dummy.h" 27#include "iio_simple_dummy.h"
26 28
27/* 29static struct config_item_type iio_dummy_type = {
28 * A few elements needed to fake a bus for this driver 30 .ct_owner = THIS_MODULE,
29 * Note instances parameter controls how many of these 31};
30 * dummy devices are registered.
31 */
32static unsigned instances = 1;
33module_param(instances, uint, 0);
34
35/* Pointer array used to fake bus elements */
36static struct iio_dev **iio_dummy_devs;
37
38/* Fake a name for the part number, usually obtained from the id table */
39static const char *iio_dummy_part_number = "iio_dummy_part_no";
40 32
41/** 33/**
42 * struct iio_dummy_accel_calibscale - realworld to register mapping 34 * struct iio_dummy_accel_calibscale - realworld to register mapping
@@ -572,12 +564,18 @@ static int iio_dummy_init_device(struct iio_dev *indio_dev)
572 * const struct i2c_device_id *id) 564 * const struct i2c_device_id *id)
573 * SPI: iio_dummy_probe(struct spi_device *spi) 565 * SPI: iio_dummy_probe(struct spi_device *spi)
574 */ 566 */
575static int iio_dummy_probe(int index) 567static struct iio_sw_device *iio_dummy_probe(const char *name)
576{ 568{
577 int ret; 569 int ret;
578 struct iio_dev *indio_dev; 570 struct iio_dev *indio_dev;
579 struct iio_dummy_state *st; 571 struct iio_dummy_state *st;
572 struct iio_sw_device *swd;
580 573
574 swd = kzalloc(sizeof(*swd), GFP_KERNEL);
575 if (!swd) {
576 ret = -ENOMEM;
577 goto error_kzalloc;
578 }
581 /* 579 /*
582 * Allocate an IIO device. 580 * Allocate an IIO device.
583 * 581 *
@@ -608,7 +606,7 @@ static int iio_dummy_probe(int index)
608 * i2c_set_clientdata(client, indio_dev); 606 * i2c_set_clientdata(client, indio_dev);
609 * spi_set_drvdata(spi, indio_dev); 607 * spi_set_drvdata(spi, indio_dev);
610 */ 608 */
611 iio_dummy_devs[index] = indio_dev; 609 swd->device = indio_dev;
612 610
613 /* 611 /*
614 * Set the device name. 612 * Set the device name.
@@ -619,7 +617,7 @@ static int iio_dummy_probe(int index)
619 * indio_dev->name = id->name; 617 * indio_dev->name = id->name;
620 * indio_dev->name = spi_get_device_id(spi)->name; 618 * indio_dev->name = spi_get_device_id(spi)->name;
621 */ 619 */
622 indio_dev->name = iio_dummy_part_number; 620 indio_dev->name = kstrdup(name, GFP_KERNEL);
623 621
624 /* Provide description of available channels */ 622 /* Provide description of available channels */
625 indio_dev->channels = iio_dummy_channels; 623 indio_dev->channels = iio_dummy_channels;
@@ -646,7 +644,9 @@ static int iio_dummy_probe(int index)
646 if (ret < 0) 644 if (ret < 0)
647 goto error_unconfigure_buffer; 645 goto error_unconfigure_buffer;
648 646
649 return 0; 647 iio_swd_group_init_type_name(swd, name, &iio_dummy_type);
648
649 return swd;
650error_unconfigure_buffer: 650error_unconfigure_buffer:
651 iio_simple_dummy_unconfigure_buffer(indio_dev); 651 iio_simple_dummy_unconfigure_buffer(indio_dev);
652error_unregister_events: 652error_unregister_events:
@@ -654,16 +654,18 @@ error_unregister_events:
654error_free_device: 654error_free_device:
655 iio_device_free(indio_dev); 655 iio_device_free(indio_dev);
656error_ret: 656error_ret:
657 return ret; 657 kfree(swd);
658error_kzalloc:
659 return ERR_PTR(ret);
658} 660}
659 661
660/** 662/**
661 * iio_dummy_remove() - device instance removal function 663 * iio_dummy_remove() - device instance removal function
662 * @index: device index. 664 * @swd: pointer to software IIO device abstraction
663 * 665 *
664 * Parameters follow those of iio_dummy_probe for buses. 666 * Parameters follow those of iio_dummy_probe for buses.
665 */ 667 */
666static void iio_dummy_remove(int index) 668static int iio_dummy_remove(struct iio_sw_device *swd)
667{ 669{
668 /* 670 /*
669 * Get a pointer to the device instance iio_dev structure 671 * Get a pointer to the device instance iio_dev structure
@@ -671,7 +673,7 @@ static void iio_dummy_remove(int index)
671 * struct iio_dev *indio_dev = i2c_get_clientdata(client); 673 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
672 * struct iio_dev *indio_dev = spi_get_drvdata(spi); 674 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
673 */ 675 */
674 struct iio_dev *indio_dev = iio_dummy_devs[index]; 676 struct iio_dev *indio_dev = swd->device;
675 677
676 /* Unregister the device */ 678 /* Unregister the device */
677 iio_device_unregister(indio_dev); 679 iio_device_unregister(indio_dev);
@@ -684,11 +686,13 @@ static void iio_dummy_remove(int index)
684 iio_simple_dummy_events_unregister(indio_dev); 686 iio_simple_dummy_events_unregister(indio_dev);
685 687
686 /* Free all structures */ 688 /* Free all structures */
689 kfree(indio_dev->name);
687 iio_device_free(indio_dev); 690 iio_device_free(indio_dev);
688}
689 691
692 return 0;
693}
690/** 694/**
691 * iio_dummy_init() - device driver registration 695 * module_iio_sw_device_driver() - device driver registration
692 * 696 *
693 * Varies depending on bus type of the device. As there is no device 697 * Varies depending on bus type of the device. As there is no device
694 * here, call probe directly. For information on device registration 698 * here, call probe directly. For information on device registration
@@ -697,50 +701,18 @@ static void iio_dummy_remove(int index)
697 * spi: 701 * spi:
698 * Documentation/spi/spi-summary 702 * Documentation/spi/spi-summary
699 */ 703 */
700static __init int iio_dummy_init(void) 704static const struct iio_sw_device_ops iio_dummy_device_ops = {
701{ 705 .probe = iio_dummy_probe,
702 int i, ret; 706 .remove = iio_dummy_remove,
703 707};
704 if (instances > 10) {
705 instances = 1;
706 return -EINVAL;
707 }
708
709 /* Fake a bus */
710 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
711 GFP_KERNEL);
712 /* Here we have no actual device so call probe */
713 for (i = 0; i < instances; i++) {
714 ret = iio_dummy_probe(i);
715 if (ret < 0)
716 goto error_remove_devs;
717 }
718 return 0;
719
720error_remove_devs:
721 while (i--)
722 iio_dummy_remove(i);
723
724 kfree(iio_dummy_devs);
725 return ret;
726}
727module_init(iio_dummy_init);
728 708
729/** 709static struct iio_sw_device_type iio_dummy_device = {
730 * iio_dummy_exit() - device driver removal 710 .name = "dummy",
731 * 711 .owner = THIS_MODULE,
732 * Varies depending on bus type of the device. 712 .ops = &iio_dummy_device_ops,
733 * As there is no device here, call remove directly. 713};
734 */
735static __exit void iio_dummy_exit(void)
736{
737 int i;
738 714
739 for (i = 0; i < instances; i++) 715module_iio_sw_device_driver(iio_dummy_device);
740 iio_dummy_remove(i);
741 kfree(iio_dummy_devs);
742}
743module_exit(iio_dummy_exit);
744 716
745MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 717MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
746MODULE_DESCRIPTION("IIO dummy driver"); 718MODULE_DESCRIPTION("IIO dummy driver");