heartbeat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #define DRV_NAME "heartbeat"
  28. #define DRV_VERSION "0.1.0"
  29. struct heartbeat_data {
  30. void __iomem *base;
  31. unsigned char bit_pos[8];
  32. struct timer_list timer;
  33. };
  34. static void heartbeat_timer(unsigned long data)
  35. {
  36. struct heartbeat_data *hd = (struct heartbeat_data *)data;
  37. static unsigned bit = 0, up = 1;
  38. ctrl_outw(1 << hd->bit_pos[bit], (unsigned long)hd->base);
  39. bit += up;
  40. if ((bit == 0) || (bit == ARRAY_SIZE(hd->bit_pos)-1))
  41. up = -up;
  42. mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
  43. ((avenrun[0] / 5) + (3 << FSHIFT)))));
  44. }
  45. static int heartbeat_drv_probe(struct platform_device *pdev)
  46. {
  47. struct resource *res;
  48. struct heartbeat_data *hd;
  49. if (unlikely(pdev->num_resources != 1)) {
  50. dev_err(&pdev->dev, "invalid number of resources\n");
  51. return -EINVAL;
  52. }
  53. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  54. if (unlikely(res == NULL)) {
  55. dev_err(&pdev->dev, "invalid resource\n");
  56. return -EINVAL;
  57. }
  58. hd = kmalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
  59. if (unlikely(!hd))
  60. return -ENOMEM;
  61. if (pdev->dev.platform_data) {
  62. memcpy(hd->bit_pos, pdev->dev.platform_data,
  63. ARRAY_SIZE(hd->bit_pos));
  64. } else {
  65. int i;
  66. for (i = 0; i < ARRAY_SIZE(hd->bit_pos); i++)
  67. hd->bit_pos[i] = i;
  68. }
  69. hd->base = (void __iomem *)res->start;
  70. setup_timer(&hd->timer, heartbeat_timer, (unsigned long)hd);
  71. platform_set_drvdata(pdev, hd);
  72. return mod_timer(&hd->timer, jiffies + 1);
  73. }
  74. static int heartbeat_drv_remove(struct platform_device *pdev)
  75. {
  76. struct heartbeat_data *hd = platform_get_drvdata(pdev);
  77. del_timer_sync(&hd->timer);
  78. platform_set_drvdata(pdev, NULL);
  79. kfree(hd);
  80. return 0;
  81. }
  82. static struct platform_driver heartbeat_driver = {
  83. .probe = heartbeat_drv_probe,
  84. .remove = heartbeat_drv_remove,
  85. .driver = {
  86. .name = DRV_NAME,
  87. },
  88. };
  89. static int __init heartbeat_init(void)
  90. {
  91. printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
  92. return platform_driver_register(&heartbeat_driver);
  93. }
  94. static void __exit heartbeat_exit(void)
  95. {
  96. platform_driver_unregister(&heartbeat_driver);
  97. }
  98. module_init(heartbeat_init);
  99. module_exit(heartbeat_exit);
  100. MODULE_VERSION(DRV_VERSION);
  101. MODULE_AUTHOR("Paul Mundt");
  102. MODULE_LICENSE("GPLv2");