leds-ipaq-micro.c 3.7 KB

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