heartbeat.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic heartbeat driver for regular LED banks
  4. *
  5. * Copyright (C) 2007 - 2010 Paul Mundt
  6. *
  7. * Most SH reference boards include a number of individual LEDs that can
  8. * be independently controlled (either via a pre-defined hardware
  9. * function or via the LED class, if desired -- the hardware tends to
  10. * encapsulate some of the same "triggers" that the LED class supports,
  11. * so there's not too much value in it).
  12. *
  13. * Additionally, most of these boards also have a LED bank that we've
  14. * traditionally used for strobing the load average. This use case is
  15. * handled by this driver, rather than giving each LED bit position its
  16. * own struct device.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched.h>
  21. #include <linux/sched/loadavg.h>
  22. #include <linux/timer.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <asm/heartbeat.h>
  26. #define DRV_NAME "heartbeat"
  27. #define DRV_VERSION "0.1.2"
  28. static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  29. static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
  30. unsigned bit, unsigned int inverted)
  31. {
  32. unsigned int new;
  33. new = (1 << hd->bit_pos[bit]);
  34. if (inverted)
  35. new = ~new;
  36. new &= hd->mask;
  37. switch (hd->regsize) {
  38. case 32:
  39. new |= ioread32(hd->base) & ~hd->mask;
  40. iowrite32(new, hd->base);
  41. break;
  42. case 16:
  43. new |= ioread16(hd->base) & ~hd->mask;
  44. iowrite16(new, hd->base);
  45. break;
  46. default:
  47. new |= ioread8(hd->base) & ~hd->mask;
  48. iowrite8(new, hd->base);
  49. break;
  50. }
  51. }
  52. static void heartbeat_timer(struct timer_list *t)
  53. {
  54. struct heartbeat_data *hd = from_timer(hd, t, timer);
  55. static unsigned bit = 0, up = 1;
  56. heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
  57. bit += up;
  58. if ((bit == 0) || (bit == (hd->nr_bits)-1))
  59. up = -up;
  60. mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
  61. ((avenrun[0] / 5) + (3 << FSHIFT)))));
  62. }
  63. static int heartbeat_drv_probe(struct platform_device *pdev)
  64. {
  65. struct resource *res;
  66. struct heartbeat_data *hd;
  67. int i;
  68. if (unlikely(pdev->num_resources != 1)) {
  69. dev_err(&pdev->dev, "invalid number of resources\n");
  70. return -EINVAL;
  71. }
  72. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  73. if (unlikely(res == NULL)) {
  74. dev_err(&pdev->dev, "invalid resource\n");
  75. return -EINVAL;
  76. }
  77. if (pdev->dev.platform_data) {
  78. hd = pdev->dev.platform_data;
  79. } else {
  80. hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
  81. if (unlikely(!hd))
  82. return -ENOMEM;
  83. }
  84. hd->base = ioremap(res->start, resource_size(res));
  85. if (unlikely(!hd->base)) {
  86. dev_err(&pdev->dev, "ioremap failed\n");
  87. if (!pdev->dev.platform_data)
  88. kfree(hd);
  89. return -ENXIO;
  90. }
  91. if (!hd->nr_bits) {
  92. hd->bit_pos = default_bit_pos;
  93. hd->nr_bits = ARRAY_SIZE(default_bit_pos);
  94. }
  95. hd->mask = 0;
  96. for (i = 0; i < hd->nr_bits; i++)
  97. hd->mask |= (1 << hd->bit_pos[i]);
  98. if (!hd->regsize) {
  99. switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
  100. case IORESOURCE_MEM_32BIT:
  101. hd->regsize = 32;
  102. break;
  103. case IORESOURCE_MEM_16BIT:
  104. hd->regsize = 16;
  105. break;
  106. case IORESOURCE_MEM_8BIT:
  107. default:
  108. hd->regsize = 8;
  109. break;
  110. }
  111. }
  112. timer_setup(&hd->timer, heartbeat_timer, 0);
  113. platform_set_drvdata(pdev, hd);
  114. return mod_timer(&hd->timer, jiffies + 1);
  115. }
  116. static struct platform_driver heartbeat_driver = {
  117. .probe = heartbeat_drv_probe,
  118. .driver = {
  119. .name = DRV_NAME,
  120. .suppress_bind_attrs = true,
  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. device_initcall(heartbeat_init);