leds-dac124s085.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2008
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * This file is subject to the terms and conditions of version 2 of
  6. * the GNU General Public License. See the file COPYING in the main
  7. * directory of this archive for more details.
  8. *
  9. * LED driver for the DAC124S085 SPI DAC
  10. */
  11. #include <linux/leds.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. struct dac124s085_led {
  17. struct led_classdev ldev;
  18. struct spi_device *spi;
  19. int id;
  20. char name[sizeof("dac124s085-3")];
  21. struct mutex mutex;
  22. };
  23. struct dac124s085 {
  24. struct dac124s085_led leds[4];
  25. };
  26. #define REG_WRITE (0 << 12)
  27. #define REG_WRITE_UPDATE (1 << 12)
  28. #define ALL_WRITE_UPDATE (2 << 12)
  29. #define POWER_DOWN_OUTPUT (3 << 12)
  30. static int dac124s085_set_brightness(struct led_classdev *ldev,
  31. enum led_brightness brightness)
  32. {
  33. struct dac124s085_led *led = container_of(ldev, struct dac124s085_led,
  34. ldev);
  35. u16 word;
  36. int ret;
  37. mutex_lock(&led->mutex);
  38. word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE |
  39. (brightness & 0xfff));
  40. ret = spi_write(led->spi, (const u8 *)&word, sizeof(word));
  41. mutex_unlock(&led->mutex);
  42. return ret;
  43. }
  44. static int dac124s085_probe(struct spi_device *spi)
  45. {
  46. struct dac124s085 *dac;
  47. struct dac124s085_led *led;
  48. int i, ret;
  49. dac = devm_kzalloc(&spi->dev, sizeof(*dac), GFP_KERNEL);
  50. if (!dac)
  51. return -ENOMEM;
  52. spi->bits_per_word = 16;
  53. for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
  54. led = dac->leds + i;
  55. led->id = i;
  56. led->spi = spi;
  57. snprintf(led->name, sizeof(led->name), "dac124s085-%d", i);
  58. mutex_init(&led->mutex);
  59. led->ldev.name = led->name;
  60. led->ldev.brightness = LED_OFF;
  61. led->ldev.max_brightness = 0xfff;
  62. led->ldev.brightness_set_blocking = dac124s085_set_brightness;
  63. ret = led_classdev_register(&spi->dev, &led->ldev);
  64. if (ret < 0)
  65. goto eledcr;
  66. }
  67. spi_set_drvdata(spi, dac);
  68. return 0;
  69. eledcr:
  70. while (i--)
  71. led_classdev_unregister(&dac->leds[i].ldev);
  72. return ret;
  73. }
  74. static int dac124s085_remove(struct spi_device *spi)
  75. {
  76. struct dac124s085 *dac = spi_get_drvdata(spi);
  77. int i;
  78. for (i = 0; i < ARRAY_SIZE(dac->leds); i++)
  79. led_classdev_unregister(&dac->leds[i].ldev);
  80. return 0;
  81. }
  82. static struct spi_driver dac124s085_driver = {
  83. .probe = dac124s085_probe,
  84. .remove = dac124s085_remove,
  85. .driver = {
  86. .name = "dac124s085",
  87. },
  88. };
  89. module_spi_driver(dac124s085_driver);
  90. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  91. MODULE_DESCRIPTION("DAC124S085 LED driver");
  92. MODULE_LICENSE("GPL v2");
  93. MODULE_ALIAS("spi:dac124s085");