leds-asic3.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/leds.h>
  12. #include <linux/slab.h>
  13. #include <linux/mfd/asic3.h>
  14. #include <linux/mfd/core.h>
  15. /*
  16. * The HTC ASIC3 LED GPIOs are inputs, not outputs.
  17. * Hence we turn the LEDs on/off via the TimeBase register.
  18. */
  19. /*
  20. * When TimeBase is 4 the clock resolution is about 32Hz.
  21. * This driver supports hardware blinking with an on+off
  22. * period from 62ms (2 clocks) to 125s (4000 clocks).
  23. */
  24. #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
  25. #define CLK_TO_MS(clk) (((clk)*32000)/1024)
  26. #define MAX_CLK 4000 /* Fits into 12-bit Time registers */
  27. #define MAX_MS CLK_TO_MS(MAX_CLK)
  28. static const unsigned int led_n_base[ASIC3_NUM_LEDS] = {
  29. [0] = ASIC3_LED_0_Base,
  30. [1] = ASIC3_LED_1_Base,
  31. [2] = ASIC3_LED_2_Base,
  32. };
  33. static void brightness_set(struct led_classdev *cdev,
  34. enum led_brightness value)
  35. {
  36. struct platform_device *pdev = to_platform_device(cdev->dev->parent);
  37. const struct mfd_cell *cell = mfd_get_cell(pdev);
  38. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  39. u32 timebase;
  40. unsigned int base;
  41. timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4);
  42. base = led_n_base[cell->id];
  43. asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32);
  44. asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32);
  45. asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
  46. asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase);
  47. }
  48. static int blink_set(struct led_classdev *cdev,
  49. unsigned long *delay_on,
  50. unsigned long *delay_off)
  51. {
  52. struct platform_device *pdev = to_platform_device(cdev->dev->parent);
  53. const struct mfd_cell *cell = mfd_get_cell(pdev);
  54. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  55. u32 on;
  56. u32 off;
  57. unsigned int base;
  58. if (*delay_on > MAX_MS || *delay_off > MAX_MS)
  59. return -EINVAL;
  60. if (*delay_on == 0 && *delay_off == 0) {
  61. /* If both are zero then a sensible default should be chosen */
  62. on = MS_TO_CLK(500);
  63. off = MS_TO_CLK(500);
  64. } else {
  65. on = MS_TO_CLK(*delay_on);
  66. off = MS_TO_CLK(*delay_off);
  67. if ((on + off) > MAX_CLK)
  68. return -EINVAL;
  69. }
  70. base = led_n_base[cell->id];
  71. asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off));
  72. asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on);
  73. asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
  74. asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4));
  75. *delay_on = CLK_TO_MS(on);
  76. *delay_off = CLK_TO_MS(off);
  77. return 0;
  78. }
  79. static int __devinit asic3_led_probe(struct platform_device *pdev)
  80. {
  81. struct asic3_led *led = pdev->dev.platform_data;
  82. int ret;
  83. ret = mfd_cell_enable(pdev);
  84. if (ret < 0)
  85. goto ret0;
  86. led->cdev = kzalloc(sizeof(struct led_classdev), GFP_KERNEL);
  87. if (!led->cdev) {
  88. ret = -ENOMEM;
  89. goto ret1;
  90. }
  91. led->cdev->name = led->name;
  92. led->cdev->default_trigger = led->default_trigger;
  93. led->cdev->brightness_set = brightness_set;
  94. led->cdev->blink_set = blink_set;
  95. ret = led_classdev_register(&pdev->dev, led->cdev);
  96. if (ret < 0)
  97. goto ret2;
  98. return 0;
  99. ret2:
  100. kfree(led->cdev);
  101. ret1:
  102. (void) mfd_cell_disable(pdev);
  103. ret0:
  104. return ret;
  105. }
  106. static int __devexit asic3_led_remove(struct platform_device *pdev)
  107. {
  108. struct asic3_led *led = pdev->dev.platform_data;
  109. led_classdev_unregister(led->cdev);
  110. kfree(led->cdev);
  111. return mfd_cell_disable(pdev);
  112. }
  113. static struct platform_driver asic3_led_driver = {
  114. .probe = asic3_led_probe,
  115. .remove = __devexit_p(asic3_led_remove),
  116. .driver = {
  117. .name = "leds-asic3",
  118. .owner = THIS_MODULE,
  119. },
  120. };
  121. MODULE_ALIAS("platform:leds-asic3");
  122. static int __init asic3_led_init(void)
  123. {
  124. return platform_driver_register(&asic3_led_driver);
  125. }
  126. static void __exit asic3_led_exit(void)
  127. {
  128. platform_driver_unregister(&asic3_led_driver);
  129. }
  130. module_init(asic3_led_init);
  131. module_exit(asic3_led_exit);
  132. MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
  133. MODULE_DESCRIPTION("HTC ASIC3 LED driver");
  134. MODULE_LICENSE("GPL");