/drivers/staging/iio/resolver/ad2s90.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 161 lines · 122 code · 28 blank · 11 comment · 6 complexity · a4c5d420f8706db6eb5715c6569d251d MD5 · raw file

  1. /*
  2. * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
  3. *
  4. * Copyright (c) 2010-2010 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/types.h>
  12. #include <linux/mutex.h>
  13. #include <linux/device.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include "../iio.h"
  18. #include "../sysfs.h"
  19. #define DRV_NAME "ad2s90"
  20. struct ad2s90_state {
  21. struct mutex lock;
  22. struct iio_dev *idev;
  23. struct spi_device *sdev;
  24. u8 rx[2];
  25. u8 tx[2];
  26. };
  27. static ssize_t ad2s90_show_angular(struct device *dev,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. struct spi_message msg;
  31. struct spi_transfer xfer;
  32. int ret;
  33. ssize_t len = 0;
  34. u16 val;
  35. struct iio_dev *idev = dev_get_drvdata(dev);
  36. struct ad2s90_state *st = idev->dev_data;
  37. xfer.len = 1;
  38. xfer.tx_buf = st->tx;
  39. xfer.rx_buf = st->rx;
  40. mutex_lock(&st->lock);
  41. spi_message_init(&msg);
  42. spi_message_add_tail(&xfer, &msg);
  43. ret = spi_sync(st->sdev, &msg);
  44. if (ret)
  45. goto error_ret;
  46. val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
  47. len = sprintf(buf, "%d\n", val);
  48. error_ret:
  49. mutex_unlock(&st->lock);
  50. return ret ? ret : len;
  51. }
  52. #define IIO_DEV_ATTR_SIMPLE_RESOLVER(_show) \
  53. IIO_DEVICE_ATTR(angular, S_IRUGO, _show, NULL, 0)
  54. static IIO_CONST_ATTR(description,
  55. "Low Cost, Complete 12-Bit Resolver-to-Digital Converter");
  56. static IIO_DEV_ATTR_SIMPLE_RESOLVER(ad2s90_show_angular);
  57. static struct attribute *ad2s90_attributes[] = {
  58. &iio_const_attr_description.dev_attr.attr,
  59. &iio_dev_attr_angular.dev_attr.attr,
  60. NULL,
  61. };
  62. static const struct attribute_group ad2s90_attribute_group = {
  63. .name = DRV_NAME,
  64. .attrs = ad2s90_attributes,
  65. };
  66. static const struct iio_info ad2s90_info = {
  67. .attrs = &ad2s90_attribute_group,
  68. .driver_module = THIS_MODULE,
  69. };
  70. static int __devinit ad2s90_probe(struct spi_device *spi)
  71. {
  72. struct ad2s90_state *st;
  73. int ret = 0;
  74. st = kzalloc(sizeof(*st), GFP_KERNEL);
  75. if (st == NULL) {
  76. ret = -ENOMEM;
  77. goto error_ret;
  78. }
  79. spi_set_drvdata(spi, st);
  80. mutex_init(&st->lock);
  81. st->sdev = spi;
  82. st->idev = iio_allocate_device(0);
  83. if (st->idev == NULL) {
  84. ret = -ENOMEM;
  85. goto error_free_st;
  86. }
  87. st->idev->dev.parent = &spi->dev;
  88. st->idev->info = &ad2s90_info;
  89. st->idev->dev_data = (void *)(st);
  90. st->idev->modes = INDIO_DIRECT_MODE;
  91. ret = iio_device_register(st->idev);
  92. if (ret)
  93. goto error_free_dev;
  94. /* need 600ns between CS and the first falling edge of SCLK */
  95. spi->max_speed_hz = 830000;
  96. spi->mode = SPI_MODE_3;
  97. spi_setup(spi);
  98. return 0;
  99. error_free_dev:
  100. iio_free_device(st->idev);
  101. error_free_st:
  102. kfree(st);
  103. error_ret:
  104. return ret;
  105. }
  106. static int __devexit ad2s90_remove(struct spi_device *spi)
  107. {
  108. struct ad2s90_state *st = spi_get_drvdata(spi);
  109. iio_device_unregister(st->idev);
  110. kfree(st);
  111. return 0;
  112. }
  113. static struct spi_driver ad2s90_driver = {
  114. .driver = {
  115. .name = DRV_NAME,
  116. .owner = THIS_MODULE,
  117. },
  118. .probe = ad2s90_probe,
  119. .remove = __devexit_p(ad2s90_remove),
  120. };
  121. static __init int ad2s90_spi_init(void)
  122. {
  123. return spi_register_driver(&ad2s90_driver);
  124. }
  125. module_init(ad2s90_spi_init);
  126. static __exit void ad2s90_spi_exit(void)
  127. {
  128. spi_unregister_driver(&ad2s90_driver);
  129. }
  130. module_exit(ad2s90_spi_exit);
  131. MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
  132. MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
  133. MODULE_LICENSE("GPL v2");