leds-asic3.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include <linux/module.h>
  16. /*
  17. * The HTC ASIC3 LED GPIOs are inputs, not outputs.
  18. * Hence we turn the LEDs on/off via the TimeBase register.
  19. */
  20. /*
  21. * When TimeBase is 4 the clock resolution is about 32Hz.
  22. * This driver supports hardware blinking with an on+off
  23. * period from 62ms (2 clocks) to 125s (4000 clocks).
  24. */
  25. #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
  26. #define CLK_TO_MS(clk) (((clk)*32000)/1024)
  27. #define MAX_CLK 4000 /* Fits into 12-bit Time registers */
  28. #define MAX_MS CLK_TO_MS(MAX_CLK)
  29. static const unsigned int led_n_base[ASIC3_NUM_LEDS] = {
  30. [0] = ASIC3_LED_0_Base,
  31. [1] = ASIC3_LED_1_Base,
  32. [2] = ASIC3_LED_2_Base,
  33. };
  34. static void brightness_set(struct led_classdev *cdev,
  35. enum led_brightness value)
  36. {
  37. struct platform_device *pdev = to_platform_device(cdev->dev->parent);
  38. const struct mfd_cell *cell = mfd_get_cell(pdev);
  39. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  40. u32 timebase;
  41. unsigned int base;
  42. timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4);
  43. base = led_n_base[cell->id];
  44. asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32);
  45. asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32);
  46. asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
  47. asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase);
  48. }
  49. static int blink_set(struct led_classdev *cdev,
  50. unsigned long *delay_on,
  51. unsigned long *delay_off)
  52. {
  53. struct platform_device *pdev = to_platform_device(cdev->dev->parent);
  54. const struct mfd_cell *cell = mfd_get_cell(pdev);
  55. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  56. u32 on;
  57. u32 off;
  58. unsigned int base;
  59. if (*delay_on > MAX_MS || *delay_off > MAX_MS)
  60. return -EINVAL;
  61. if (*delay_on == 0 && *delay_off == 0) {
  62. /* If both are zero then a sensible default should be chosen */
  63. on = MS_TO_CLK(500);
  64. off = MS_TO_CLK(500);
  65. } else {
  66. on = MS_TO_CLK(*delay_on);
  67. off = MS_TO_CLK(*delay_off);
  68. if ((on + off) > MAX_CLK)
  69. return -EINVAL;
  70. }
  71. base = led_n_base[cell->id];
  72. asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off));
  73. asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on);
  74. asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
  75. asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4));
  76. *delay_on = CLK_TO_MS(on);
  77. *delay_off = CLK_TO_MS(off);
  78. return 0;
  79. }
  80. static int __devinit asic3_led_probe(struct platform_device *pdev)
  81. {
  82. struct asic3_led *led = pdev->dev.platform_data;
  83. int ret;
  84. ret = mfd_cell_enable(pdev);
  85. if (ret < 0)
  86. goto ret0;
  87. led->cdev = kzalloc(sizeof(struct led_classdev), GFP_KERNEL);
  88. if (!led->cdev) {
  89. ret = -ENOMEM;
  90. goto ret1;
  91. }
  92. led->cdev->name = led->name;
  93. led->cdev->flags = LED_CORE_SUSPENDRESUME;
  94. led->cdev->brightness_set = brightness_set;
  95. led->cdev->blink_set = blink_set;
  96. led->cdev->default_trigger = led->default_trigger;
  97. ret = led_classdev_register(&pdev->dev, led->cdev);
  98. if (ret < 0)
  99. goto ret2;
  100. return 0;
  101. ret2:
  102. kfree(led->cdev);
  103. ret1:
  104. (void) mfd_cell_disable(pdev);
  105. ret0:
  106. return ret;
  107. }
  108. static int __devexit asic3_led_remove(struct platform_device *pdev)
  109. {
  110. struct asic3_led *led = pdev->dev.platform_data;
  111. led_classdev_unregister(led->cdev);
  112. kfree(led->cdev);
  113. return mfd_cell_disable(pdev);
  114. }
  115. static int asic3_led_suspend(struct device *dev)
  116. {
  117. struct platform_device *pdev = to_platform_device(dev);
  118. const struct mfd_cell *cell = mfd_get_cell(pdev);
  119. int ret;
  120. ret = 0;
  121. if (cell->suspend)
  122. ret = (*cell->suspend)(pdev);
  123. return ret;
  124. }
  125. static int asic3_led_resume(struct device *dev)
  126. {
  127. struct platform_device *pdev = to_platform_device(dev);
  128. const struct mfd_cell *cell = mfd_get_cell(pdev);
  129. int ret;
  130. ret = 0;
  131. if (cell->resume)
  132. ret = (*cell->resume)(pdev);
  133. return ret;
  134. }
  135. static const struct dev_pm_ops asic3_led_pm_ops = {
  136. .suspend = asic3_led_suspend,
  137. .resume = asic3_led_resume,
  138. };
  139. static struct platform_driver asic3_led_driver = {
  140. .probe = asic3_led_probe,
  141. .remove = __devexit_p(asic3_led_remove),
  142. .driver = {
  143. .name = "leds-asic3",
  144. .owner = THIS_MODULE,
  145. .pm = &asic3_led_pm_ops,
  146. },
  147. };
  148. module_platform_driver(asic3_led_driver);
  149. MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
  150. MODULE_DESCRIPTION("HTC ASIC3 LED driver");
  151. MODULE_LICENSE("GPL");
  152. MODULE_ALIAS("platform:leds-asic3");