heartbeat.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Generic heartbeat driver for regular LED banks
  3. *
  4. * Copyright (C) 2007 - 2010 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/platform_device.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/loadavg.h>
  25. #include <linux/timer.h>
  26. #include <linux/io.h>
  27. #include <linux/slab.h>
  28. #include <asm/heartbeat.h>
  29. #define DRV_NAME "heartbeat"
  30. #define DRV_VERSION "0.1.2"
  31. static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  32. static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
  33. unsigned bit, unsigned int inverted)
  34. {
  35. unsigned int new;
  36. new = (1 << hd->bit_pos[bit]);
  37. if (inverted)
  38. new = ~new;
  39. new &= hd->mask;
  40. switch (hd->regsize) {
  41. case 32:
  42. new |= ioread32(hd->base) & ~hd->mask;
  43. iowrite32(new, hd->base);
  44. break;
  45. case 16:
  46. new |= ioread16(hd->base) & ~hd->mask;
  47. iowrite16(new, hd->base);
  48. break;
  49. default:
  50. new |= ioread8(hd->base) & ~hd->mask;
  51. iowrite8(new, hd->base);
  52. break;
  53. }
  54. }
  55. static void heartbeat_timer(struct timer_list *t)
  56. {
  57. struct heartbeat_data *hd = from_timer(hd, t, timer);
  58. static unsigned bit = 0, up = 1;
  59. heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
  60. bit += up;
  61. if ((bit == 0) || (bit == (hd->nr_bits)-1))
  62. up = -up;
  63. mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
  64. ((avenrun[0] / 5) + (3 << FSHIFT)))));
  65. }
  66. static int heartbeat_drv_probe(struct platform_device *pdev)
  67. {
  68. struct resource *res;
  69. struct heartbeat_data *hd;
  70. int i;
  71. if (unlikely(pdev->num_resources != 1)) {
  72. dev_err(&pdev->dev, "invalid number of resources\n");
  73. return -EINVAL;
  74. }
  75. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  76. if (unlikely(res == NULL)) {
  77. dev_err(&pdev->dev, "invalid resource\n");
  78. return -EINVAL;
  79. }
  80. if (pdev->dev.platform_data) {
  81. hd = pdev->dev.platform_data;
  82. } else {
  83. hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
  84. if (unlikely(!hd))
  85. return -ENOMEM;
  86. }
  87. hd->base = ioremap_nocache(res->start, resource_size(res));
  88. if (unlikely(!hd->base)) {
  89. dev_err(&pdev->dev, "ioremap failed\n");
  90. if (!pdev->dev.platform_data)
  91. kfree(hd);
  92. return -ENXIO;
  93. }
  94. if (!hd->nr_bits) {
  95. hd->bit_pos = default_bit_pos;
  96. hd->nr_bits = ARRAY_SIZE(default_bit_pos);
  97. }
  98. hd->mask = 0;
  99. for (i = 0; i < hd->nr_bits; i++)
  100. hd->mask |= (1 << hd->bit_pos[i]);
  101. if (!hd->regsize) {
  102. switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
  103. case IORESOURCE_MEM_32BIT:
  104. hd->regsize = 32;
  105. break;
  106. case IORESOURCE_MEM_16BIT:
  107. hd->regsize = 16;
  108. break;
  109. case IORESOURCE_MEM_8BIT:
  110. default:
  111. hd->regsize = 8;
  112. break;
  113. }
  114. }
  115. timer_setup(&hd->timer, heartbeat_timer, 0);
  116. platform_set_drvdata(pdev, hd);
  117. return mod_timer(&hd->timer, jiffies + 1);
  118. }
  119. static struct platform_driver heartbeat_driver = {
  120. .probe = heartbeat_drv_probe,
  121. .driver = {
  122. .name = DRV_NAME,
  123. .suppress_bind_attrs = true,
  124. },
  125. };
  126. static int __init heartbeat_init(void)
  127. {
  128. printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
  129. return platform_driver_register(&heartbeat_driver);
  130. }
  131. device_initcall(heartbeat_init);