ti_dac7512.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <linux/of.h>
  25. static ssize_t dac7512_store_val(struct device *dev,
  26. struct device_attribute *attr,
  27. const char *buf, size_t count)
  28. {
  29. struct spi_device *spi = to_spi_device(dev);
  30. unsigned char tmp[2];
  31. unsigned long val;
  32. int ret;
  33. ret = kstrtoul(buf, 10, &val);
  34. if (ret)
  35. return ret;
  36. tmp[0] = val >> 8;
  37. tmp[1] = val & 0xff;
  38. spi_write(spi, tmp, sizeof(tmp));
  39. return count;
  40. }
  41. static DEVICE_ATTR(value, S_IWUSR, NULL, dac7512_store_val);
  42. static struct attribute *dac7512_attributes[] = {
  43. &dev_attr_value.attr,
  44. NULL
  45. };
  46. static const struct attribute_group dac7512_attr_group = {
  47. .attrs = dac7512_attributes,
  48. };
  49. static int dac7512_probe(struct spi_device *spi)
  50. {
  51. int ret;
  52. spi->bits_per_word = 8;
  53. spi->mode = SPI_MODE_0;
  54. ret = spi_setup(spi);
  55. if (ret < 0)
  56. return ret;
  57. return sysfs_create_group(&spi->dev.kobj, &dac7512_attr_group);
  58. }
  59. static int dac7512_remove(struct spi_device *spi)
  60. {
  61. sysfs_remove_group(&spi->dev.kobj, &dac7512_attr_group);
  62. return 0;
  63. }
  64. static const struct spi_device_id dac7512_id_table[] = {
  65. { "dac7512", 0 },
  66. { }
  67. };
  68. MODULE_DEVICE_TABLE(spi, dac7512_id_table);
  69. #ifdef CONFIG_OF
  70. static const struct of_device_id dac7512_of_match[] = {
  71. { .compatible = "ti,dac7512", },
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(of, dac7512_of_match);
  75. #endif
  76. static struct spi_driver dac7512_driver = {
  77. .driver = {
  78. .name = "dac7512",
  79. .owner = THIS_MODULE,
  80. .of_match_table = of_match_ptr(dac7512_of_match),
  81. },
  82. .probe = dac7512_probe,
  83. .remove = dac7512_remove,
  84. .id_table = dac7512_id_table,
  85. };
  86. module_spi_driver(dac7512_driver);
  87. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  88. MODULE_DESCRIPTION("DAC7512 16-bit DAC");
  89. MODULE_LICENSE("GPL v2");