leds-ipaq-micro.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * h3xxx atmel micro companion support, notification LED subdevice
  5. *
  6. * Author : Linus Walleij <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/mfd/ipaq-micro.h>
  11. #include <linux/leds.h>
  12. #define LED_YELLOW 0x00
  13. #define LED_GREEN 0x01
  14. #define LED_EN (1 << 4) /* LED ON/OFF 0:off, 1:on */
  15. #define LED_AUTOSTOP (1 << 5) /* LED ON/OFF auto stop set 0:disable, 1:enable */
  16. #define LED_ALWAYS (1 << 6) /* LED Interrupt Mask 0:No mask, 1:mask */
  17. static int micro_leds_brightness_set(struct led_classdev *led_cdev,
  18. enum led_brightness value)
  19. {
  20. struct ipaq_micro *micro = dev_get_drvdata(led_cdev->dev->parent->parent);
  21. /*
  22. * In this message:
  23. * Byte 0 = LED color: 0 = yellow, 1 = green
  24. * yellow LED is always ~30 blinks per minute
  25. * Byte 1 = duration (flags?) appears to be ignored
  26. * Byte 2 = green ontime in 1/10 sec (deciseconds)
  27. * 1 = 1/10 second
  28. * 0 = 256/10 second
  29. * Byte 3 = green offtime in 1/10 sec (deciseconds)
  30. * 1 = 1/10 second
  31. * 0 = 256/10 seconds
  32. */
  33. struct ipaq_micro_msg msg = {
  34. .id = MSG_NOTIFY_LED,
  35. .tx_len = 4,
  36. };
  37. msg.tx_data[0] = LED_GREEN;
  38. msg.tx_data[1] = 0;
  39. if (value) {
  40. msg.tx_data[2] = 0; /* Duty cycle 256 */
  41. msg.tx_data[3] = 1;
  42. } else {
  43. msg.tx_data[2] = 1;
  44. msg.tx_data[3] = 0; /* Duty cycle 256 */
  45. }
  46. return ipaq_micro_tx_msg_sync(micro, &msg);
  47. }
  48. /* Maximum duty cycle in ms 256/10 sec = 25600 ms */
  49. #define IPAQ_LED_MAX_DUTY 25600
  50. static int micro_leds_blink_set(struct led_classdev *led_cdev,
  51. unsigned long *delay_on,
  52. unsigned long *delay_off)
  53. {
  54. struct ipaq_micro *micro = dev_get_drvdata(led_cdev->dev->parent->parent);
  55. /*
  56. * In this message:
  57. * Byte 0 = LED color: 0 = yellow, 1 = green
  58. * yellow LED is always ~30 blinks per minute
  59. * Byte 1 = duration (flags?) appears to be ignored
  60. * Byte 2 = green ontime in 1/10 sec (deciseconds)
  61. * 1 = 1/10 second
  62. * 0 = 256/10 second
  63. * Byte 3 = green offtime in 1/10 sec (deciseconds)
  64. * 1 = 1/10 second
  65. * 0 = 256/10 seconds
  66. */
  67. struct ipaq_micro_msg msg = {
  68. .id = MSG_NOTIFY_LED,
  69. .tx_len = 4,
  70. };
  71. msg.tx_data[0] = LED_GREEN;
  72. if (*delay_on > IPAQ_LED_MAX_DUTY ||
  73. *delay_off > IPAQ_LED_MAX_DUTY)
  74. return -EINVAL;
  75. if (*delay_on == 0 && *delay_off == 0) {
  76. *delay_on = 100;
  77. *delay_off = 100;
  78. }
  79. msg.tx_data[1] = 0;
  80. if (*delay_on >= IPAQ_LED_MAX_DUTY)
  81. msg.tx_data[2] = 0;
  82. else
  83. msg.tx_data[2] = (u8) DIV_ROUND_CLOSEST(*delay_on, 100);
  84. if (*delay_off >= IPAQ_LED_MAX_DUTY)
  85. msg.tx_data[3] = 0;
  86. else
  87. msg.tx_data[3] = (u8) DIV_ROUND_CLOSEST(*delay_off, 100);
  88. return ipaq_micro_tx_msg_sync(micro, &msg);
  89. }
  90. static struct led_classdev micro_led = {
  91. .name = "led-ipaq-micro",
  92. .brightness_set_blocking = micro_leds_brightness_set,
  93. .blink_set = micro_leds_blink_set,
  94. .flags = LED_CORE_SUSPENDRESUME,
  95. };
  96. static int micro_leds_probe(struct platform_device *pdev)
  97. {
  98. int ret;
  99. ret = devm_led_classdev_register(&pdev->dev, &micro_led);
  100. if (ret) {
  101. dev_err(&pdev->dev, "registering led failed: %d\n", ret);
  102. return ret;
  103. }
  104. dev_info(&pdev->dev, "iPAQ micro notification LED driver\n");
  105. return 0;
  106. }
  107. static struct platform_driver micro_leds_device_driver = {
  108. .driver = {
  109. .name = "ipaq-micro-leds",
  110. },
  111. .probe = micro_leds_probe,
  112. };
  113. module_platform_driver(micro_leds_device_driver);
  114. MODULE_LICENSE("GPL");
  115. MODULE_DESCRIPTION("driver for iPAQ Atmel micro leds");
  116. MODULE_ALIAS("platform:ipaq-micro-leds");