1ff4a7481SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2401e9093SPaul Mundt /*
3401e9093SPaul Mundt * Generic heartbeat driver for regular LED banks
4401e9093SPaul Mundt *
510ab92d8SPaul Mundt * Copyright (C) 2007 - 2010 Paul Mundt
6401e9093SPaul Mundt *
7401e9093SPaul Mundt * Most SH reference boards include a number of individual LEDs that can
8401e9093SPaul Mundt * be independently controlled (either via a pre-defined hardware
9401e9093SPaul Mundt * function or via the LED class, if desired -- the hardware tends to
10401e9093SPaul Mundt * encapsulate some of the same "triggers" that the LED class supports,
11401e9093SPaul Mundt * so there's not too much value in it).
12401e9093SPaul Mundt *
13401e9093SPaul Mundt * Additionally, most of these boards also have a LED bank that we've
14401e9093SPaul Mundt * traditionally used for strobing the load average. This use case is
15401e9093SPaul Mundt * handled by this driver, rather than giving each LED bit position its
16401e9093SPaul Mundt * own struct device.
17401e9093SPaul Mundt */
18401e9093SPaul Mundt #include <linux/init.h>
19401e9093SPaul Mundt #include <linux/platform_device.h>
20401e9093SPaul Mundt #include <linux/sched.h>
214f17722cSIngo Molnar #include <linux/sched/loadavg.h>
22401e9093SPaul Mundt #include <linux/timer.h>
23401e9093SPaul Mundt #include <linux/io.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
258786c952SPaul Mundt #include <asm/heartbeat.h>
26401e9093SPaul Mundt
27401e9093SPaul Mundt #define DRV_NAME "heartbeat"
2810ab92d8SPaul Mundt #define DRV_VERSION "0.1.2"
29401e9093SPaul Mundt
308786c952SPaul Mundt static unsigned char default_bit_pos[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
318786c952SPaul Mundt
heartbeat_toggle_bit(struct heartbeat_data * hd,unsigned bit,unsigned int inverted)328786c952SPaul Mundt static inline void heartbeat_toggle_bit(struct heartbeat_data *hd,
338786c952SPaul Mundt unsigned bit, unsigned int inverted)
348786c952SPaul Mundt {
358786c952SPaul Mundt unsigned int new;
368786c952SPaul Mundt
378786c952SPaul Mundt new = (1 << hd->bit_pos[bit]);
388786c952SPaul Mundt if (inverted)
398786c952SPaul Mundt new = ~new;
408786c952SPaul Mundt
41e174d130SKuninori Morimoto new &= hd->mask;
42e174d130SKuninori Morimoto
438786c952SPaul Mundt switch (hd->regsize) {
448786c952SPaul Mundt case 32:
45e174d130SKuninori Morimoto new |= ioread32(hd->base) & ~hd->mask;
468786c952SPaul Mundt iowrite32(new, hd->base);
478786c952SPaul Mundt break;
488786c952SPaul Mundt case 16:
49e174d130SKuninori Morimoto new |= ioread16(hd->base) & ~hd->mask;
508786c952SPaul Mundt iowrite16(new, hd->base);
518786c952SPaul Mundt break;
528786c952SPaul Mundt default:
53e174d130SKuninori Morimoto new |= ioread8(hd->base) & ~hd->mask;
548786c952SPaul Mundt iowrite8(new, hd->base);
558786c952SPaul Mundt break;
568786c952SPaul Mundt }
578786c952SPaul Mundt }
58401e9093SPaul Mundt
heartbeat_timer(struct timer_list * t)59e99e88a9SKees Cook static void heartbeat_timer(struct timer_list *t)
60401e9093SPaul Mundt {
61e99e88a9SKees Cook struct heartbeat_data *hd = from_timer(hd, t, timer);
62401e9093SPaul Mundt static unsigned bit = 0, up = 1;
63401e9093SPaul Mundt
648786c952SPaul Mundt heartbeat_toggle_bit(hd, bit, hd->flags & HEARTBEAT_INVERTED);
658786c952SPaul Mundt
66f6072896STakashi YOSHII bit += up;
678786c952SPaul Mundt if ((bit == 0) || (bit == (hd->nr_bits)-1))
68f6072896STakashi YOSHII up = -up;
69401e9093SPaul Mundt
70401e9093SPaul Mundt mod_timer(&hd->timer, jiffies + (110 - ((300 << FSHIFT) /
71401e9093SPaul Mundt ((avenrun[0] / 5) + (3 << FSHIFT)))));
72401e9093SPaul Mundt }
73401e9093SPaul Mundt
heartbeat_drv_probe(struct platform_device * pdev)74401e9093SPaul Mundt static int heartbeat_drv_probe(struct platform_device *pdev)
75401e9093SPaul Mundt {
76401e9093SPaul Mundt struct resource *res;
77401e9093SPaul Mundt struct heartbeat_data *hd;
78e174d130SKuninori Morimoto int i;
79401e9093SPaul Mundt
80401e9093SPaul Mundt if (unlikely(pdev->num_resources != 1)) {
81401e9093SPaul Mundt dev_err(&pdev->dev, "invalid number of resources\n");
82401e9093SPaul Mundt return -EINVAL;
83401e9093SPaul Mundt }
84401e9093SPaul Mundt
85401e9093SPaul Mundt res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86401e9093SPaul Mundt if (unlikely(res == NULL)) {
87401e9093SPaul Mundt dev_err(&pdev->dev, "invalid resource\n");
88401e9093SPaul Mundt return -EINVAL;
89401e9093SPaul Mundt }
90401e9093SPaul Mundt
918786c952SPaul Mundt if (pdev->dev.platform_data) {
928786c952SPaul Mundt hd = pdev->dev.platform_data;
938786c952SPaul Mundt } else {
948786c952SPaul Mundt hd = kzalloc(sizeof(struct heartbeat_data), GFP_KERNEL);
95401e9093SPaul Mundt if (unlikely(!hd))
96401e9093SPaul Mundt return -ENOMEM;
97401e9093SPaul Mundt }
98401e9093SPaul Mundt
99*4bdc0d67SChristoph Hellwig hd->base = ioremap(res->start, resource_size(res));
1001de83e94SRoel Kluin if (unlikely(!hd->base)) {
1018786c952SPaul Mundt dev_err(&pdev->dev, "ioremap failed\n");
1028786c952SPaul Mundt
1038786c952SPaul Mundt if (!pdev->dev.platform_data)
1048786c952SPaul Mundt kfree(hd);
1058786c952SPaul Mundt
1068786c952SPaul Mundt return -ENXIO;
1078786c952SPaul Mundt }
1088786c952SPaul Mundt
1098786c952SPaul Mundt if (!hd->nr_bits) {
1108786c952SPaul Mundt hd->bit_pos = default_bit_pos;
1118786c952SPaul Mundt hd->nr_bits = ARRAY_SIZE(default_bit_pos);
1128786c952SPaul Mundt }
1138786c952SPaul Mundt
114e174d130SKuninori Morimoto hd->mask = 0;
115e174d130SKuninori Morimoto for (i = 0; i < hd->nr_bits; i++)
116e174d130SKuninori Morimoto hd->mask |= (1 << hd->bit_pos[i]);
117e174d130SKuninori Morimoto
11810ab92d8SPaul Mundt if (!hd->regsize) {
11910ab92d8SPaul Mundt switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
12010ab92d8SPaul Mundt case IORESOURCE_MEM_32BIT:
12110ab92d8SPaul Mundt hd->regsize = 32;
12210ab92d8SPaul Mundt break;
12310ab92d8SPaul Mundt case IORESOURCE_MEM_16BIT:
12410ab92d8SPaul Mundt hd->regsize = 16;
12510ab92d8SPaul Mundt break;
12610ab92d8SPaul Mundt case IORESOURCE_MEM_8BIT:
12710ab92d8SPaul Mundt default:
12810ab92d8SPaul Mundt hd->regsize = 8;
12910ab92d8SPaul Mundt break;
13010ab92d8SPaul Mundt }
13110ab92d8SPaul Mundt }
132401e9093SPaul Mundt
133e99e88a9SKees Cook timer_setup(&hd->timer, heartbeat_timer, 0);
134401e9093SPaul Mundt platform_set_drvdata(pdev, hd);
135401e9093SPaul Mundt
136401e9093SPaul Mundt return mod_timer(&hd->timer, jiffies + 1);
137401e9093SPaul Mundt }
138401e9093SPaul Mundt
139401e9093SPaul Mundt static struct platform_driver heartbeat_driver = {
140401e9093SPaul Mundt .probe = heartbeat_drv_probe,
141401e9093SPaul Mundt .driver = {
142401e9093SPaul Mundt .name = DRV_NAME,
143e75438e2SPaul Gortmaker .suppress_bind_attrs = true,
144401e9093SPaul Mundt },
145401e9093SPaul Mundt };
146401e9093SPaul Mundt
heartbeat_init(void)147401e9093SPaul Mundt static int __init heartbeat_init(void)
148401e9093SPaul Mundt {
149401e9093SPaul Mundt printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
150401e9093SPaul Mundt return platform_driver_register(&heartbeat_driver);
151401e9093SPaul Mundt }
152e75438e2SPaul Gortmaker device_initcall(heartbeat_init);
153