1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  LEDs driver for the Cobalt Raq series.
4  *
5  *  Copyright (C) 2007  Yoichi Yuasa <yuasa@linux-mips.org>
6  */
7 #include <linux/init.h>
8 #include <linux/io.h>
9 #include <linux/ioport.h>
10 #include <linux/leds.h>
11 #include <linux/platform_device.h>
12 #include <linux/spinlock.h>
13 #include <linux/types.h>
14 #include <linux/export.h>
15 
16 #define LED_WEB		0x04
17 #define LED_POWER_OFF	0x08
18 
19 static void __iomem *led_port;
20 static u8 led_value;
21 static DEFINE_SPINLOCK(led_value_lock);
22 
23 static void raq_web_led_set(struct led_classdev *led_cdev,
24 			    enum led_brightness brightness)
25 {
26 	unsigned long flags;
27 
28 	spin_lock_irqsave(&led_value_lock, flags);
29 
30 	if (brightness)
31 		led_value |= LED_WEB;
32 	else
33 		led_value &= ~LED_WEB;
34 	writeb(led_value, led_port);
35 
36 	spin_unlock_irqrestore(&led_value_lock, flags);
37 }
38 
39 static struct led_classdev raq_web_led = {
40 	.name		= "raq::web",
41 	.brightness_set	= raq_web_led_set,
42 };
43 
44 static void raq_power_off_led_set(struct led_classdev *led_cdev,
45 				  enum led_brightness brightness)
46 {
47 	unsigned long flags;
48 
49 	spin_lock_irqsave(&led_value_lock, flags);
50 
51 	if (brightness)
52 		led_value |= LED_POWER_OFF;
53 	else
54 		led_value &= ~LED_POWER_OFF;
55 	writeb(led_value, led_port);
56 
57 	spin_unlock_irqrestore(&led_value_lock, flags);
58 }
59 
60 static struct led_classdev raq_power_off_led = {
61 	.name			= "raq::power-off",
62 	.brightness_set		= raq_power_off_led_set,
63 	.default_trigger	= "power-off",
64 };
65 
66 static int cobalt_raq_led_probe(struct platform_device *pdev)
67 {
68 	struct resource *res;
69 	int retval;
70 
71 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72 	if (!res)
73 		return -EBUSY;
74 
75 	led_port = devm_ioremap(&pdev->dev, res->start, resource_size(res));
76 	if (!led_port)
77 		return -ENOMEM;
78 
79 	retval = led_classdev_register(&pdev->dev, &raq_power_off_led);
80 	if (retval)
81 		goto err_null;
82 
83 	retval = led_classdev_register(&pdev->dev, &raq_web_led);
84 	if (retval)
85 		goto err_unregister;
86 
87 	return 0;
88 
89 err_unregister:
90 	led_classdev_unregister(&raq_power_off_led);
91 
92 err_null:
93 	led_port = NULL;
94 
95 	return retval;
96 }
97 
98 static struct platform_driver cobalt_raq_led_driver = {
99 	.probe	= cobalt_raq_led_probe,
100 	.driver = {
101 		.name	= "cobalt-raq-leds",
102 	},
103 };
104 
105 builtin_platform_driver(cobalt_raq_led_driver);
106