viperboard_adc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Nano River Technologies viperboard IIO ADC driver
  3. *
  4. * (C) 2012 by Lemonage GmbH
  5. * Author: Lars Poeschel <poeschel@lemonage.de>
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/mutex.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/mfd/viperboard.h>
  24. #define VPRBRD_ADC_CMD_GET 0x00
  25. struct vprbrd_adc_msg {
  26. u8 cmd;
  27. u8 chan;
  28. u8 val;
  29. } __packed;
  30. struct vprbrd_adc {
  31. struct vprbrd *vb;
  32. };
  33. #define VPRBRD_ADC_CHANNEL(_index) { \
  34. .type = IIO_VOLTAGE, \
  35. .indexed = 1, \
  36. .channel = _index, \
  37. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  38. }
  39. static struct iio_chan_spec const vprbrd_adc_iio_channels[] = {
  40. VPRBRD_ADC_CHANNEL(0),
  41. VPRBRD_ADC_CHANNEL(1),
  42. VPRBRD_ADC_CHANNEL(2),
  43. VPRBRD_ADC_CHANNEL(3),
  44. };
  45. static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
  46. struct iio_chan_spec const *chan,
  47. int *val,
  48. int *val2,
  49. long info)
  50. {
  51. int ret, error = 0;
  52. struct vprbrd_adc *adc = iio_priv(iio_dev);
  53. struct vprbrd *vb = adc->vb;
  54. struct vprbrd_adc_msg *admsg = (struct vprbrd_adc_msg *)vb->buf;
  55. switch (info) {
  56. case IIO_CHAN_INFO_RAW:
  57. mutex_lock(&vb->lock);
  58. admsg->cmd = VPRBRD_ADC_CMD_GET;
  59. admsg->chan = chan->channel;
  60. admsg->val = 0x00;
  61. ret = usb_control_msg(vb->usb_dev,
  62. usb_sndctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
  63. VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
  64. sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
  65. if (ret != sizeof(struct vprbrd_adc_msg)) {
  66. dev_err(&iio_dev->dev, "usb send error on adc read\n");
  67. error = -EREMOTEIO;
  68. }
  69. ret = usb_control_msg(vb->usb_dev,
  70. usb_rcvctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
  71. VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, admsg,
  72. sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
  73. *val = admsg->val;
  74. mutex_unlock(&vb->lock);
  75. if (ret != sizeof(struct vprbrd_adc_msg)) {
  76. dev_err(&iio_dev->dev, "usb recv error on adc read\n");
  77. error = -EREMOTEIO;
  78. }
  79. if (error)
  80. goto error;
  81. return IIO_VAL_INT;
  82. default:
  83. error = -EINVAL;
  84. break;
  85. }
  86. error:
  87. return error;
  88. }
  89. static const struct iio_info vprbrd_adc_iio_info = {
  90. .read_raw = &vprbrd_iio_read_raw,
  91. .driver_module = THIS_MODULE,
  92. };
  93. static int vprbrd_adc_probe(struct platform_device *pdev)
  94. {
  95. struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
  96. struct vprbrd_adc *adc;
  97. struct iio_dev *indio_dev;
  98. int ret;
  99. /* registering iio */
  100. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  101. if (!indio_dev) {
  102. dev_err(&pdev->dev, "failed allocating iio device\n");
  103. return -ENOMEM;
  104. }
  105. adc = iio_priv(indio_dev);
  106. adc->vb = vb;
  107. indio_dev->name = "viperboard adc";
  108. indio_dev->dev.parent = &pdev->dev;
  109. indio_dev->info = &vprbrd_adc_iio_info;
  110. indio_dev->modes = INDIO_DIRECT_MODE;
  111. indio_dev->channels = vprbrd_adc_iio_channels;
  112. indio_dev->num_channels = ARRAY_SIZE(vprbrd_adc_iio_channels);
  113. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  114. if (ret) {
  115. dev_err(&pdev->dev, "could not register iio (adc)");
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. static struct platform_driver vprbrd_adc_driver = {
  121. .driver = {
  122. .name = "viperboard-adc",
  123. },
  124. .probe = vprbrd_adc_probe,
  125. };
  126. module_platform_driver(vprbrd_adc_driver);
  127. MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
  128. MODULE_DESCRIPTION("IIO ADC driver for Nano River Techs Viperboard");
  129. MODULE_LICENSE("GPL");
  130. MODULE_ALIAS("platform:viperboard-adc");