ti_dac7512.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * dac7512.c - Linux kernel module for
  3. * Texas Instruments DAC7512
  4. *
  5. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/spi/spi.h>
  24. #define DAC7512_DRV_NAME "dac7512"
  25. #define DRIVER_VERSION "1.0"
  26. static ssize_t dac7512_store_val(struct device *dev,
  27. struct device_attribute *attr,
  28. const char *buf, size_t count)
  29. {
  30. struct spi_device *spi = to_spi_device(dev);
  31. unsigned char tmp[2];
  32. unsigned long val;
  33. int ret;
  34. ret = kstrtoul(buf, 10, &val);
  35. if (ret)
  36. return ret;
  37. tmp[0] = val >> 8;
  38. tmp[1] = val & 0xff;
  39. spi_write(spi, tmp, sizeof(tmp));
  40. return count;
  41. }
  42. static DEVICE_ATTR(value, S_IWUSR, NULL, dac7512_store_val);
  43. static struct attribute *dac7512_attributes[] = {
  44. &dev_attr_value.attr,
  45. NULL
  46. };
  47. static const struct attribute_group dac7512_attr_group = {
  48. .attrs = dac7512_attributes,
  49. };
  50. static int dac7512_probe(struct spi_device *spi)
  51. {
  52. int ret;
  53. spi->bits_per_word = 8;
  54. spi->mode = SPI_MODE_0;
  55. ret = spi_setup(spi);
  56. if (ret < 0)
  57. return ret;
  58. return sysfs_create_group(&spi->dev.kobj, &dac7512_attr_group);
  59. }
  60. static int dac7512_remove(struct spi_device *spi)
  61. {
  62. sysfs_remove_group(&spi->dev.kobj, &dac7512_attr_group);
  63. return 0;
  64. }
  65. static struct spi_driver dac7512_driver = {
  66. .driver = {
  67. .name = DAC7512_DRV_NAME,
  68. .owner = THIS_MODULE,
  69. },
  70. .probe = dac7512_probe,
  71. .remove = dac7512_remove,
  72. };
  73. module_spi_driver(dac7512_driver);
  74. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  75. MODULE_DESCRIPTION("DAC7512 16-bit DAC");
  76. MODULE_LICENSE("GPL v2");
  77. MODULE_VERSION(DRIVER_VERSION);