blob: 24391b444b286deb848a6afd2b97b94d1e50119d [file] [log] [blame]
Kuninori Morimotoff4a7482018-12-28 00:31:49 -08001// SPDX-License-Identifier: GPL-2.0
Paul Mundt401e9092007-02-13 15:46:39 +09002/*
3 * Generic heartbeat driver for regular LED banks
4 *
Paul Mundt10ab92d2010-01-15 12:08:31 +09005 * Copyright (C) 2007 - 2010 Paul Mundt
Paul Mundt401e9092007-02-13 15:46:39 +09006 *
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.
Paul Mundt401e9092007-02-13 15:46:39 +090017 */
18#include <linux/init.h>
Paul Mundt401e9092007-02-13 15:46:39 +090019#include <linux/platform_device.h>
20#include <linux/sched.h>
Ingo Molnar4f177222017-02-08 08:45:17 +010021#include <linux/sched/loadavg.h>
Paul Mundt401e9092007-02-13 15:46:39 +090022#include <linux/timer.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Paul Mundt8786c952007-08-20 13:03:41 +090025#include <asm/heartbeat.h>
Paul Mundt401e9092007-02-13 15:46:39 +090026
27#define DRV_NAME "heartbeat"
Paul Mundt10ab92d2010-01-15 12:08:31 +090028#define DRV_VERSION "0.1.2"
Paul Mundt401e9092007-02-13 15:46:39 +090029
Paul Mundt8786c952007-08-20 13:03:41 +090030static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
31
32static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
33 unsigned bit, unsigned int inverted)
34{
35 unsigned int new;
36
37 new = (1 << hd->bit_pos[bit]);
38 if (inverted)
39 new = ~new;
40
Kuninori Morimotoe174d132009-08-18 07:00:20 +000041 new &= hd->mask;
42
Paul Mundt8786c952007-08-20 13:03:41 +090043 switch (hd->regsize) {
44 case 32:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000045 new |= ioread32(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090046 iowrite32(new, hd->base);
47 break;
48 case 16:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000049 new |= ioread16(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090050 iowrite16(new, hd->base);
51 break;
52 default:
Kuninori Morimotoe174d132009-08-18 07:00:20 +000053 new |= ioread8(hd->base) & ~hd->mask;
Paul Mundt8786c952007-08-20 13:03:41 +090054 iowrite8(new, hd->base);
55 break;
56 }
57}
Paul Mundt401e9092007-02-13 15:46:39 +090058
Kees Cooke99e88a2017-10-16 14:43:17 -070059static void heartbeat_timer(struct timer_list *t)
Paul Mundt401e9092007-02-13 15:46:39 +090060{
Kees Cooke99e88a2017-10-16 14:43:17 -070061 struct heartbeat_data *hd = from_timer(hd, t, timer);
Paul Mundt401e9092007-02-13 15:46:39 +090062 static unsigned bit = 0, up = 1;
63
Paul Mundt8786c952007-08-20 13:03:41 +090064 heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
65
Takashi YOSHIIf6072892007-03-12 15:33:22 +090066 bit += up;
Paul Mundt8786c952007-08-20 13:03:41 +090067 if ((bit == 0) || (bit == (hd->nr_bits)-1))
Takashi YOSHIIf6072892007-03-12 15:33:22 +090068 up = -up;
Paul Mundt401e9092007-02-13 15:46:39 +090069
70 mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
71 ((avenrun[0] / 5) + (3 << FSHIFT)))));
72}
73
74static int heartbeat_drv_probe(struct platform_device *pdev)
75{
76 struct resource *res;
77 struct heartbeat_data *hd;
Kuninori Morimotoe174d132009-08-18 07:00:20 +000078 int i;
Paul Mundt401e9092007-02-13 15:46:39 +090079
80 if (unlikely(pdev->num_resources != 1)) {
81 dev_err(&pdev->dev, "invalid number of resources\n");
82 return -EINVAL;
83 }
84
85 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86 if (unlikely(res == NULL)) {
87 dev_err(&pdev->dev, "invalid resource\n");
88 return -EINVAL;
89 }
90
Paul Mundt401e9092007-02-13 15:46:39 +090091 if (pdev->dev.platform_data) {
Paul Mundt8786c952007-08-20 13:03:41 +090092 hd = pdev->dev.platform_data;
Paul Mundt401e9092007-02-13 15:46:39 +090093 } else {
Paul Mundt8786c952007-08-20 13:03:41 +090094 hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
95 if (unlikely(!hd))
96 return -ENOMEM;
Paul Mundt401e9092007-02-13 15:46:39 +090097 }
98
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +010099 hd->base = ioremap(res->start, resource_size(res));
Roel Kluin1de83e92008-02-18 14:09:10 +0100100 if (unlikely(!hd->base)) {
Paul Mundt8786c952007-08-20 13:03:41 +0900101 dev_err(&pdev->dev, "ioremap failed\n");
102
103 if (!pdev->dev.platform_data)
104 kfree(hd);
105
106 return -ENXIO;
107 }
108
109 if (!hd->nr_bits) {
110 hd->bit_pos = default_bit_pos;
111 hd->nr_bits = ARRAY_SIZE(default_bit_pos);
112 }
113
Kuninori Morimotoe174d132009-08-18 07:00:20 +0000114 hd->mask = 0;
115 for (i = 0; i < hd->nr_bits; i++)
116 hd->mask |= (1 << hd->bit_pos[i]);
117
Paul Mundt10ab92d2010-01-15 12:08:31 +0900118 if (!hd->regsize) {
119 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
120 case IORESOURCE_MEM_32BIT:
121 hd->regsize = 32;
122 break;
123 case IORESOURCE_MEM_16BIT:
124 hd->regsize = 16;
125 break;
126 case IORESOURCE_MEM_8BIT:
127 default:
128 hd->regsize = 8;
129 break;
130 }
131 }
Paul Mundt401e9092007-02-13 15:46:39 +0900132
Kees Cooke99e88a2017-10-16 14:43:17 -0700133 timer_setup(&hd->timer, heartbeat_timer, 0);
Paul Mundt401e9092007-02-13 15:46:39 +0900134 platform_set_drvdata(pdev, hd);
135
136 return mod_timer(&hd->timer, jiffies + 1);
137}
138
Paul Mundt401e9092007-02-13 15:46:39 +0900139static struct platform_driver heartbeat_driver = {
140 .probe = heartbeat_drv_probe,
Paul Mundt401e9092007-02-13 15:46:39 +0900141 .driver = {
Paul Gortmakere75438e2016-04-22 14:07:04 -0400142 .name = DRV_NAME,
143 .suppress_bind_attrs = true,
Paul Mundt401e9092007-02-13 15:46:39 +0900144 },
145};
146
147static int __init heartbeat_init(void)
148{
149 printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
150 return platform_driver_register(&heartbeat_driver);
151}
Paul Gortmakere75438e2016-04-22 14:07:04 -0400152device_initcall(heartbeat_init);