leds-dac124s085.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/spinlock.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/spi/spi.h>
  18. struct dac124s085_led {
  19. struct led_classdev ldev;
  20. struct spi_device *spi;
  21. int id;
  22. int brightness;
  23. char name[sizeof("dac124s085-3")];
  24. struct mutex mutex;
  25. struct work_struct work;
  26. spinlock_t lock;
  27. };
  28. struct dac124s085 {
  29. struct dac124s085_led leds[4];
  30. };
  31. #define REG_WRITE (0 << 12)
  32. #define REG_WRITE_UPDATE (1 << 12)
  33. #define ALL_WRITE_UPDATE (2 << 12)
  34. #define POWER_DOWN_OUTPUT (3 << 12)
  35. static void dac124s085_led_work(struct work_struct *work)
  36. {
  37. struct dac124s085_led *led = container_of(work, struct dac124s085_led,
  38. work);
  39. u16 word;
  40. mutex_lock(&led->mutex);
  41. word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE |
  42. (led->brightness & 0xfff));
  43. spi_write(led->spi, (const u8 *)&word, sizeof(word));
  44. mutex_unlock(&led->mutex);
  45. }
  46. static void dac124s085_set_brightness(struct led_classdev *ldev,
  47. enum led_brightness brightness)
  48. {
  49. struct dac124s085_led *led = container_of(ldev, struct dac124s085_led,
  50. ldev);
  51. spin_lock(&led->lock);
  52. led->brightness = brightness;
  53. schedule_work(&led->work);
  54. spin_unlock(&led->lock);
  55. }
  56. static int dac124s085_probe(struct spi_device *spi)
  57. {
  58. struct dac124s085 *dac;
  59. struct dac124s085_led *led;
  60. int i, ret;
  61. dac = kzalloc(sizeof(*dac), GFP_KERNEL);
  62. if (!dac)
  63. return -ENOMEM;
  64. spi->bits_per_word = 16;
  65. for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
  66. led = dac->leds + i;
  67. led->id = i;
  68. led->brightness = LED_OFF;
  69. led->spi = spi;
  70. snprintf(led->name, sizeof(led->name), "dac124s085-%d", i);
  71. spin_lock_init(&led->lock);
  72. INIT_WORK(&led->work, dac124s085_led_work);
  73. mutex_init(&led->mutex);
  74. led->ldev.name = led->name;
  75. led->ldev.brightness = LED_OFF;
  76. led->ldev.max_brightness = 0xfff;
  77. led->ldev.brightness_set = dac124s085_set_brightness;
  78. ret = led_classdev_register(&spi->dev, &led->ldev);
  79. if (ret < 0)
  80. goto eledcr;
  81. }
  82. spi_set_drvdata(spi, dac);
  83. return 0;
  84. eledcr:
  85. while (i--)
  86. led_classdev_unregister(&dac->leds[i].ldev);
  87. spi_set_drvdata(spi, NULL);
  88. kfree(dac);
  89. return ret;
  90. }
  91. static int dac124s085_remove(struct spi_device *spi)
  92. {
  93. struct dac124s085 *dac = spi_get_drvdata(spi);
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(dac->leds); i++) {
  96. led_classdev_unregister(&dac->leds[i].ldev);
  97. cancel_work_sync(&dac->leds[i].work);
  98. }
  99. spi_set_drvdata(spi, NULL);
  100. kfree(dac);
  101. return 0;
  102. }
  103. static struct spi_driver dac124s085_driver = {
  104. .probe = dac124s085_probe,
  105. .remove = dac124s085_remove,
  106. .driver = {
  107. .name = "dac124s085",
  108. .owner = THIS_MODULE,
  109. },
  110. };
  111. static int __init dac124s085_leds_init(void)
  112. {
  113. return spi_register_driver(&dac124s085_driver);
  114. }
  115. static void __exit dac124s085_leds_exit(void)
  116. {
  117. spi_unregister_driver(&dac124s085_driver);
  118. }
  119. module_init(dac124s085_leds_init);
  120. module_exit(dac124s085_leds_exit);
  121. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  122. MODULE_DESCRIPTION("DAC124S085 LED driver");
  123. MODULE_LICENSE("GPL v2");
  124. MODULE_ALIAS("spi:dac124s085");