123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include <linux/kernel.h>
- #include <linux/slab.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/iio/iio.h>
- #include <linux/iio/sysfs.h>
- #include <linux/iio/sw_device.h>
- static struct config_item_type iio_dummy_dac_type = {
- .ct_owner = THIS_MODULE,
- };
- struct iio_dummy_dac_state {
- int dac_val;
- struct mutex lock;
- };
- static const struct iio_chan_spec iio_dummy_dac_channels[] = {
- {
- .type = IIO_VOLTAGE,
- .output = 1,
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
- },
- };
- static int dummy_dac_read_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int *val, int *val2, long mask)
- {
- struct iio_dummy_dac_state *st = iio_priv(indio_dev);
- mutex_lock(&st->lock);
- *val = st->dac_val;
- mutex_unlock(&st->lock);
- return IIO_VAL_INT;
- }
- static int dummy_dac_write_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int val, int val2, long mask)
- {
-
- struct iio_dummy_dac_state *st = iio_priv(indio_dev);
-
- switch(mask) {
- case IIO_CHAN_INFO_RAW:
- if(val < 0 || val > 255)
- return -EINVAL;
- mutex_lock(&st->lock);
- st->dac_val = val;
- printk(KERN_INFO "dummy_dac: %i\n",val);
- mutex_unlock(&st->lock);
- return 0;
- }
- return -EINVAL;
- }
- static const struct iio_info iio_dummy_dac_info = {
- .read_raw = &dummy_dac_read_raw,
- .write_raw = &dummy_dac_write_raw,
- .driver_module = THIS_MODULE,
- };
- static struct iio_sw_device *iio_dummy_dac_probe(const char *name)
- {
- int ret;
- struct iio_dev *indio_dev;
- struct iio_dummy_dac_state *st;
- struct iio_sw_device *swd;
- printk(KERN_INFO "HIMODA\n");
- swd = kzalloc(sizeof(*swd), GFP_KERNEL);
- if(!swd) {
- ret = -ENOMEM;
- goto error_kzalloc;
- }
- indio_dev = iio_device_alloc(sizeof(*st));
- if(!indio_dev) {
- ret = -ENOMEM;
- goto error_ret;
- }
- st = iio_priv(indio_dev);
- mutex_init(&st->lock);
- st->dac_val = 0;
- swd->device = indio_dev;
- indio_dev->name = kstrdup(name, GFP_KERNEL);
- indio_dev->channels = iio_dummy_dac_channels;
- indio_dev->num_channels = ARRAY_SIZE(iio_dummy_dac_channels);
- indio_dev->info = &iio_dummy_dac_info;
- indio_dev->modes = INDIO_DIRECT_MODE;
-
- ret = iio_device_register(indio_dev);
-
- if(ret < 0)
- goto error_free_device;
- iio_swd_group_init_type_name(swd, name, &iio_dummy_dac_type);
- return swd;
- error_free_device:
- iio_device_free(indio_dev);
- error_ret:
- kfree(swd);
- error_kzalloc:
- return ERR_PTR(ret);
- }
- static int iio_dummy_dac_remove(struct iio_sw_device *swd)
- {
- struct iio_dev *indio_dev = swd->device;
- iio_device_unregister(indio_dev);
- kfree(indio_dev->name);
- iio_device_free(indio_dev);
- return 0;
- }
- static const struct iio_sw_device_ops iio_dummy_dac_ops = {
- .probe = iio_dummy_dac_probe,
- .remove = iio_dummy_dac_remove,
- };
- static struct iio_sw_device_type iio_dummy_dac = {
- .name = "dummy_dac",
- .owner = THIS_MODULE,
- .ops = &iio_dummy_dac_ops,
- };
- module_iio_sw_device_driver(iio_dummy_dac);
- MODULE_AUTHOR("encrypt <encrypt@labr.xyz>");
- MODULE_DESCRIPTION("IIO DAC dummy driver");
- MODULE_LICENSE("GPL v3");
|