heartbeat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Generic heartbeat driver for regular LED banks
  3. *
  4. * Copyright (C) 2007 Paul Mundt
  5. *
  6. * Most SH reference boards include a number of individual LEDs that can
  7. * be independently controlled (either via a pre-defined hardware
  8. * function or via the LED class, if desired -- the hardware tends to
  9. * encapsulate some of the same "triggers" that the LED class supports,
  10. * so there's not too much value in it).
  11. *
  12. * Additionally, most of these boards also have a LED bank that we've
  13. * traditionally used for strobing the load average. This use case is
  14. * handled by this driver, rather than giving each LED bit position its
  15. * own struct device.
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file "COPYING" in the main directory of this archive
  19. * for more details.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/sched.h>
  25. #include <linux/timer.h>
  26. #include <linux/io.h>
  27. #include <asm/heartbeat.h>
  28. #define DRV_NAME "heartbeat"
  29. #define DRV_VERSION "0.1.1"
  30. static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  31. static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
  32. unsigned bit, unsigned int inverted)
  33. {
  34. unsigned int new;
  35. new = (1 << hd->bit_pos[bit]);
  36. if (inverted)
  37. new = ~new;
  38. new &= hd->mask;
  39. switch (hd->regsize) {
  40. case 32:
  41. new |= ioread32(hd->base) & ~hd->mask;
  42. iowrite32(new, hd->base);
  43. break;
  44. case 16:
  45. new |= ioread16(hd->base) & ~hd->mask;
  46. iowrite16(new, hd->base);
  47. break;
  48. default:
  49. new |= ioread8(hd->base) & ~hd->mask;
  50. iowrite8(new, hd->base);
  51. break;
  52. }
  53. }
  54. static void heartbeat_timer(unsigned long data)
  55. {
  56. struct heartbeat_data *hd = (struct heartbeat_data *)data;
  57. static unsigned bit = 0, up = 1;
  58. heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
  59. bit += up;
  60. if ((bit == 0) || (bit == (hd->nr_bits)-1))
  61. up = -up;
  62. mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
  63. ((avenrun[0] / 5) + (3 << FSHIFT)))));
  64. }
  65. static int heartbeat_drv_probe(struct platform_device *pdev)
  66. {
  67. struct resource *res;
  68. struct heartbeat_data *hd;
  69. int i;
  70. if (unlikely(pdev->num_resources != 1)) {
  71. dev_err(&pdev->dev, "invalid number of resources\n");
  72. return -EINVAL;
  73. }
  74. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  75. if (unlikely(res == NULL)) {
  76. dev_err(&pdev->dev, "invalid resource\n");
  77. return -EINVAL;
  78. }
  79. if (pdev->dev.platform_data) {
  80. hd = pdev->dev.platform_data;
  81. } else {
  82. hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
  83. if (unlikely(!hd))
  84. return -ENOMEM;
  85. }
  86. hd->base = ioremap_nocache(res->start, res->end - res->start + 1);
  87. if (unlikely(!hd->base)) {
  88. dev_err(&pdev->dev, "ioremap failed\n");
  89. if (!pdev->dev.platform_data)
  90. kfree(hd);
  91. return -ENXIO;
  92. }
  93. if (!hd->nr_bits) {
  94. hd->bit_pos = default_bit_pos;
  95. hd->nr_bits = ARRAY_SIZE(default_bit_pos);
  96. }
  97. hd->mask = 0;
  98. for (i = 0; i < hd->nr_bits; i++)
  99. hd->mask |= (1 << hd->bit_pos[i]);
  100. if (!hd->regsize)
  101. hd->regsize = 8; /* default access size */
  102. setup_timer(&hd->timer, heartbeat_timer, (unsigned long)hd);
  103. platform_set_drvdata(pdev, hd);
  104. return mod_timer(&hd->timer, jiffies + 1);
  105. }
  106. static int heartbeat_drv_remove(struct platform_device *pdev)
  107. {
  108. struct heartbeat_data *hd = platform_get_drvdata(pdev);
  109. del_timer_sync(&hd->timer);
  110. iounmap(hd->base);
  111. platform_set_drvdata(pdev, NULL);
  112. if (!pdev->dev.platform_data)
  113. kfree(hd);
  114. return 0;
  115. }
  116. static struct platform_driver heartbeat_driver = {
  117. .probe = heartbeat_drv_probe,
  118. .remove = heartbeat_drv_remove,
  119. .driver = {
  120. .name = DRV_NAME,
  121. },
  122. };
  123. static int __init heartbeat_init(void)
  124. {
  125. printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
  126. return platform_driver_register(&heartbeat_driver);
  127. }
  128. static void __exit heartbeat_exit(void)
  129. {
  130. platform_driver_unregister(&heartbeat_driver);
  131. }
  132. module_init(heartbeat_init);
  133. module_exit(heartbeat_exit);
  134. MODULE_VERSION(DRV_VERSION);
  135. MODULE_AUTHOR("Paul Mundt");
  136. MODULE_LICENSE("GPL v2");