blob: 49bace446a1ab885be104c03946226e9ea44c4bc [file] [log] [blame]
Paul Mundt401e9092007-02-13 15:46:39 +09001/*
2 * Generic heartbeat driver for regular LED banks
3 *
Paul Mundt10ab92d2010-01-15 12:08:31 +09004 * Copyright (C) 2007 - 2010 Paul Mundt
Paul Mundt401e9092007-02-13 15:46:39 +09005 *
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>
Paul Mundt401e9092007-02-13 15:46:39 +090022#include <linux/platform_device.h>
23#include <linux/sched.h>
24#include <linux/timer.h>
25#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Mundt8786c952007-08-20 13:03:41 +090027#include <asm/heartbeat.h>
Paul Mundt401e9092007-02-13 15:46:39 +090028
29#define DRV_NAME "heartbeat"
Paul Mundt10ab92d2010-01-15 12:08:31 +090030#define DRV_VERSION "0.1.2"
Paul Mundt401e9092007-02-13 15:46:39 +090031
Paul Mundt8786c952007-08-20 13:03:41 +090032static 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
Kuninori Morimotoe174d132009-08-18 07:00:20 +000043 new &= hd->mask;
44
Paul Mundt8786c952007-08-20 13:03:41 +090045 switch (hd->regsize) {
46 case 32:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000047 new |= ioread32(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090048 iowrite32(new, hd->base);
49 break;
50 case 16:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000051 new |= ioread16(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090052 iowrite16(new, hd->base);
53 break;
54 default:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000055 new |= ioread8(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090056 iowrite8(new, hd->base);
57 break;
58 }
59}
Paul Mundt401e9092007-02-13 15:46:39 +090060
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
Paul Mundt8786c952007-08-20 13:03:41 +090066 heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
67
Takashi YOSHIIf6072892007-03-12 15:33:22 +090068 bit += up;
Paul Mundt8786c952007-08-20 13:03:41 +090069 if ((bit == 0) || (bit == (hd->nr_bits)-1))
Takashi YOSHIIf6072892007-03-12 15:33:22 +090070 up = -up;
Paul Mundt401e9092007-02-13 15:46:39 +090071
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;
Kuninori Morimotoe174d132009-08-18 07:00:20 +000080 int i;
Paul Mundt401e9092007-02-13 15:46:39 +090081
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
Paul Mundt401e9092007-02-13 15:46:39 +090093 if (pdev->dev.platform_data) {
Paul Mundt8786c952007-08-20 13:03:41 +090094 hd = pdev->dev.platform_data;
Paul Mundt401e9092007-02-13 15:46:39 +090095 } else {
Paul Mundt8786c952007-08-20 13:03:41 +090096 hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
97 if (unlikely(!hd))
98 return -ENOMEM;
Paul Mundt401e9092007-02-13 15:46:39 +090099 }
100
Paul Mundt10ab92d2010-01-15 12:08:31 +0900101 hd->base = ioremap_nocache(res->start, resource_size(res));
Roel Kluin1de83e92008-02-18 14:09:10 +0100102 if (unlikely(!hd->base)) {
Paul Mundt8786c952007-08-20 13:03:41 +0900103 dev_err(&pdev->dev, "ioremap failed\n");
104
105 if (!pdev->dev.platform_data)
106 kfree(hd);
107
108 return -ENXIO;
109 }
110
111 if (!hd->nr_bits) {
112 hd->bit_pos = default_bit_pos;
113 hd->nr_bits = ARRAY_SIZE(default_bit_pos);
114 }
115
Kuninori Morimotoe174d132009-08-18 07:00:20 +0000116 hd->mask = 0;
117 for (i = 0; i < hd->nr_bits; i++)
118 hd->mask |= (1 << hd->bit_pos[i]);
119
Paul Mundt10ab92d2010-01-15 12:08:31 +0900120 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 }
Paul Mundt401e9092007-02-13 15:46:39 +0900134
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
Paul Mundt401e9092007-02-13 15:46:39 +0900141static struct platform_driver heartbeat_driver = {
142 .probe = heartbeat_drv_probe,
Paul Mundt401e9092007-02-13 15:46:39 +0900143 .driver = {
Paul Gortmakere75438e2016-04-22 14:07:04 -0400144 .name = DRV_NAME,
145 .suppress_bind_attrs = true,
Paul Mundt401e9092007-02-13 15:46:39 +0900146 },
147};
148
149static int __init heartbeat_init(void)
150{
151 printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
152 return platform_driver_register(&heartbeat_driver);
153}
Paul Gortmakere75438e2016-04-22 14:07:04 -0400154device_initcall(heartbeat_init);