Merge branch 'sh/dmaengine'
[deliverable/linux.git] / arch / sh / drivers / heartbeat.c
CommitLineData
401e9093
PM
1/*
2 * Generic heartbeat driver for regular LED banks
3 *
10ab92d8 4 * Copyright (C) 2007 - 2010 Paul Mundt
401e9093
PM
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>
8786c952 27#include <asm/heartbeat.h>
401e9093
PM
28
29#define DRV_NAME "heartbeat"
10ab92d8 30#define DRV_VERSION "0.1.2"
401e9093 31
8786c952
PM
32static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
33
34static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
35 unsigned bit, unsigned int inverted)
36{
37 unsigned int new;
38
39 new = (1 << hd->bit_pos[bit]);
40 if (inverted)
41 new = ~new;
42
e174d130
KM
43 new &= hd->mask;
44
8786c952
PM
45 switch (hd->regsize) {
46 case 32:
e174d130 47 new |= ioread32(hd->base) & ~hd->mask;
8786c952
PM
48 iowrite32(new, hd->base);
49 break;
50 case 16:
e174d130 51 new |= ioread16(hd->base) & ~hd->mask;
8786c952
PM
52 iowrite16(new, hd->base);
53 break;
54 default:
e174d130 55 new |= ioread8(hd->base) & ~hd->mask;
8786c952
PM
56 iowrite8(new, hd->base);
57 break;
58 }
59}
401e9093
PM
60
61static void heartbeat_timer(unsigned long data)
62{
63 struct heartbeat_data *hd = (struct heartbeat_data *)data;
64 static unsigned bit = 0, up = 1;
65
8786c952
PM
66 heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
67
f6072896 68 bit += up;
8786c952 69 if ((bit == 0) || (bit == (hd->nr_bits)-1))
f6072896 70 up = -up;
401e9093
PM
71
72 mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
73 ((avenrun[0] / 5) + (3 << FSHIFT)))));
74}
75
76static int heartbeat_drv_probe(struct platform_device *pdev)
77{
78 struct resource *res;
79 struct heartbeat_data *hd;
e174d130 80 int i;
401e9093
PM
81
82 if (unlikely(pdev->num_resources != 1)) {
83 dev_err(&pdev->dev, "invalid number of resources\n");
84 return -EINVAL;
85 }
86
87 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
88 if (unlikely(res == NULL)) {
89 dev_err(&pdev->dev, "invalid resource\n");
90 return -EINVAL;
91 }
92
401e9093 93 if (pdev->dev.platform_data) {
8786c952 94 hd = pdev->dev.platform_data;
401e9093 95 } else {
8786c952
PM
96 hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
97 if (unlikely(!hd))
98 return -ENOMEM;
99 }
100
10ab92d8 101 hd->base = ioremap_nocache(res->start, resource_size(res));
1de83e94 102 if (unlikely(!hd->base)) {
8786c952
PM
103 dev_err(&pdev->dev, "ioremap failed\n");
104
105 if (!pdev->dev.platform_data)
106 kfree(hd);
107
108 return -ENXIO;
109 }
401e9093 110
8786c952
PM
111 if (!hd->nr_bits) {
112 hd->bit_pos = default_bit_pos;
113 hd->nr_bits = ARRAY_SIZE(default_bit_pos);
401e9093
PM
114 }
115
e174d130
KM
116 hd->mask = 0;
117 for (i = 0; i < hd->nr_bits; i++)
118 hd->mask |= (1 << hd->bit_pos[i]);
119
10ab92d8
PM
120 if (!hd->regsize) {
121 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
122 case IORESOURCE_MEM_32BIT:
123 hd->regsize = 32;
124 break;
125 case IORESOURCE_MEM_16BIT:
126 hd->regsize = 16;
127 break;
128 case IORESOURCE_MEM_8BIT:
129 default:
130 hd->regsize = 8;
131 break;
132 }
133 }
401e9093
PM
134
135 setup_timer(&hd->timer, heartbeat_timer, (unsigned long)hd);
136 platform_set_drvdata(pdev, hd);
137
138 return mod_timer(&hd->timer, jiffies + 1);
139}
140
141static int heartbeat_drv_remove(struct platform_device *pdev)
142{
143 struct heartbeat_data *hd = platform_get_drvdata(pdev);
144
145 del_timer_sync(&hd->timer);
8786c952 146 iounmap(hd->base);
401e9093
PM
147
148 platform_set_drvdata(pdev, NULL);
149
8786c952
PM
150 if (!pdev->dev.platform_data)
151 kfree(hd);
401e9093
PM
152
153 return 0;
154}
155
156static struct platform_driver heartbeat_driver = {
157 .probe = heartbeat_drv_probe,
158 .remove = heartbeat_drv_remove,
159 .driver = {
160 .name = DRV_NAME,
161 },
162};
163
164static int __init heartbeat_init(void)
165{
166 printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
167 return platform_driver_register(&heartbeat_driver);
168}
169
170static void __exit heartbeat_exit(void)
171{
172 platform_driver_unregister(&heartbeat_driver);
173}
174module_init(heartbeat_init);
175module_exit(heartbeat_exit);
176
177MODULE_VERSION(DRV_VERSION);
178MODULE_AUTHOR("Paul Mundt");
839cd310 179MODULE_LICENSE("GPL v2");
This page took 0.462821 seconds and 5 git commands to generate.