dummy-dac.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/iio/iio.h>
  6. #include <linux/iio/sysfs.h>
  7. #include <linux/iio/sw_device.h>
  8. static struct config_item_type iio_dummy_dac_type = {
  9. .ct_owner = THIS_MODULE,
  10. };
  11. struct iio_dummy_dac_state {
  12. int dac_val;
  13. struct mutex lock;
  14. };
  15. static const struct iio_chan_spec iio_dummy_dac_channels[] = {
  16. {
  17. .type = IIO_VOLTAGE,
  18. .output = 1,
  19. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  20. },
  21. };
  22. static int dummy_dac_read_raw(struct iio_dev *indio_dev,
  23. struct iio_chan_spec const *chan,
  24. int *val, int *val2, long mask)
  25. {
  26. struct iio_dummy_dac_state *st = iio_priv(indio_dev);
  27. mutex_lock(&st->lock);
  28. *val = st->dac_val;
  29. mutex_unlock(&st->lock);
  30. return IIO_VAL_INT;
  31. }
  32. static int dummy_dac_write_raw(struct iio_dev *indio_dev,
  33. struct iio_chan_spec const *chan,
  34. int val, int val2, long mask)
  35. {
  36. struct iio_dummy_dac_state *st = iio_priv(indio_dev);
  37. switch(mask) {
  38. case IIO_CHAN_INFO_RAW:
  39. if(val < 0 || val > 255)
  40. return -EINVAL;
  41. mutex_lock(&st->lock);
  42. st->dac_val = val;
  43. printk(KERN_INFO "dummy_dac: %i\n",val);
  44. mutex_unlock(&st->lock);
  45. return 0;
  46. }
  47. return -EINVAL;
  48. }
  49. static const struct iio_info iio_dummy_dac_info = {
  50. .read_raw = &dummy_dac_read_raw,
  51. .write_raw = &dummy_dac_write_raw,
  52. .driver_module = THIS_MODULE,
  53. };
  54. static struct iio_sw_device *iio_dummy_dac_probe(const char *name)
  55. {
  56. int ret;
  57. struct iio_dev *indio_dev;
  58. struct iio_dummy_dac_state *st;
  59. struct iio_sw_device *swd;
  60. printk(KERN_INFO "HIMODA\n");
  61. swd = kzalloc(sizeof(*swd), GFP_KERNEL);
  62. if(!swd) {
  63. ret = -ENOMEM;
  64. goto error_kzalloc;
  65. }
  66. indio_dev = iio_device_alloc(sizeof(*st));
  67. if(!indio_dev) {
  68. ret = -ENOMEM;
  69. goto error_ret;
  70. }
  71. st = iio_priv(indio_dev);
  72. mutex_init(&st->lock);
  73. st->dac_val = 0;
  74. swd->device = indio_dev;
  75. indio_dev->name = kstrdup(name, GFP_KERNEL);
  76. indio_dev->channels = iio_dummy_dac_channels;
  77. indio_dev->num_channels = ARRAY_SIZE(iio_dummy_dac_channels);
  78. indio_dev->info = &iio_dummy_dac_info;
  79. indio_dev->modes = INDIO_DIRECT_MODE;
  80. ret = iio_device_register(indio_dev);
  81. if(ret < 0)
  82. goto error_free_device;
  83. iio_swd_group_init_type_name(swd, name, &iio_dummy_dac_type);
  84. return swd;
  85. error_free_device:
  86. iio_device_free(indio_dev);
  87. error_ret:
  88. kfree(swd);
  89. error_kzalloc:
  90. return ERR_PTR(ret);
  91. }
  92. static int iio_dummy_dac_remove(struct iio_sw_device *swd)
  93. {
  94. struct iio_dev *indio_dev = swd->device;
  95. iio_device_unregister(indio_dev);
  96. kfree(indio_dev->name);
  97. iio_device_free(indio_dev);
  98. return 0;
  99. }
  100. static const struct iio_sw_device_ops iio_dummy_dac_ops = {
  101. .probe = iio_dummy_dac_probe,
  102. .remove = iio_dummy_dac_remove,
  103. };
  104. static struct iio_sw_device_type iio_dummy_dac = {
  105. .name = "dummy_dac",
  106. .owner = THIS_MODULE,
  107. .ops = &iio_dummy_dac_ops,
  108. };
  109. module_iio_sw_device_driver(iio_dummy_dac);
  110. MODULE_AUTHOR("encrypt <encrypt@labr.xyz>");
  111. MODULE_DESCRIPTION("IIO DAC dummy driver");
  112. MODULE_LICENSE("GPL v3");