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