1
0

leds-rb750.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * LED driver for the RouterBOARD 750
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  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/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/leds.h>
  16. #include <linux/slab.h>
  17. #include <asm/mach-ath79/mach-rb750.h>
  18. #define DRV_NAME "leds-rb750"
  19. struct rb750_led_dev {
  20. struct led_classdev cdev;
  21. u32 mask;
  22. int active_low;
  23. void (*latch_change)(u32 clear, u32 set);
  24. };
  25. struct rb750_led_drvdata {
  26. struct rb750_led_dev *led_devs;
  27. int num_leds;
  28. };
  29. static inline struct rb750_led_dev *to_rbled(struct led_classdev *led_cdev)
  30. {
  31. return (struct rb750_led_dev *)container_of(led_cdev,
  32. struct rb750_led_dev, cdev);
  33. }
  34. static void rb750_led_brightness_set(struct led_classdev *led_cdev,
  35. enum led_brightness value)
  36. {
  37. struct rb750_led_dev *rbled = to_rbled(led_cdev);
  38. int level;
  39. level = (value == LED_OFF) ? 0 : 1;
  40. level ^= rbled->active_low;
  41. if (level)
  42. rbled->latch_change(0, rbled->mask);
  43. else
  44. rbled->latch_change(rbled->mask, 0);
  45. }
  46. static int rb750_led_probe(struct platform_device *pdev)
  47. {
  48. struct rb750_led_platform_data *pdata;
  49. struct rb750_led_drvdata *drvdata;
  50. int ret = 0;
  51. int i;
  52. pdata = pdev->dev.platform_data;
  53. if (!pdata)
  54. return -EINVAL;
  55. drvdata = kzalloc(sizeof(struct rb750_led_drvdata) +
  56. sizeof(struct rb750_led_dev) * pdata->num_leds,
  57. GFP_KERNEL);
  58. if (!drvdata)
  59. return -ENOMEM;
  60. drvdata->num_leds = pdata->num_leds;
  61. drvdata->led_devs = (struct rb750_led_dev *) &drvdata[1];
  62. for (i = 0; i < drvdata->num_leds; i++) {
  63. struct rb750_led_dev *rbled = &drvdata->led_devs[i];
  64. struct rb750_led_data *led_data = &pdata->leds[i];
  65. rbled->cdev.name = led_data->name;
  66. rbled->cdev.default_trigger = led_data->default_trigger;
  67. rbled->cdev.brightness_set = rb750_led_brightness_set;
  68. rbled->cdev.brightness = LED_OFF;
  69. rbled->mask = led_data->mask;
  70. rbled->active_low = !!led_data->active_low;
  71. rbled->latch_change = pdata->latch_change;
  72. ret = led_classdev_register(&pdev->dev, &rbled->cdev);
  73. if (ret)
  74. goto err;
  75. }
  76. platform_set_drvdata(pdev, drvdata);
  77. return 0;
  78. err:
  79. for (i = i - 1; i >= 0; i--)
  80. led_classdev_unregister(&drvdata->led_devs[i].cdev);
  81. kfree(drvdata);
  82. return ret;
  83. }
  84. static int rb750_led_remove(struct platform_device *pdev)
  85. {
  86. struct rb750_led_drvdata *drvdata;
  87. int i;
  88. drvdata = platform_get_drvdata(pdev);
  89. for (i = 0; i < drvdata->num_leds; i++)
  90. led_classdev_unregister(&drvdata->led_devs[i].cdev);
  91. kfree(drvdata);
  92. return 0;
  93. }
  94. static struct platform_driver rb750_led_driver = {
  95. .probe = rb750_led_probe,
  96. .remove = rb750_led_remove,
  97. .driver = {
  98. .name = DRV_NAME,
  99. .owner = THIS_MODULE,
  100. },
  101. };
  102. MODULE_ALIAS("platform:leds-rb750");
  103. static int __init rb750_led_init(void)
  104. {
  105. return platform_driver_register(&rb750_led_driver);
  106. }
  107. static void __exit rb750_led_exit(void)
  108. {
  109. platform_driver_unregister(&rb750_led_driver);
  110. }
  111. module_init(rb750_led_init);
  112. module_exit(rb750_led_exit);
  113. MODULE_DESCRIPTION(DRV_NAME);
  114. MODULE_DESCRIPTION("LED driver for the RouterBOARD 750");
  115. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  116. MODULE_LICENSE("GPL v2");