Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Bachmann ot200 leds driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *         Christian Gmeiner <christian.gmeiner@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * License: GPL as published by the FSF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) struct ot200_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct led_classdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned long port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u8 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * The device has three leds on the back panel (led_err, led_init and led_run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * and can handle up to seven leds on the front panel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static struct ot200_led leds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		.name = "led_run",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		.port = 0x5a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		.mask = BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		.name = "led_init",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		.port = 0x5a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		.mask = BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		.name = "led_err",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		.port = 0x5a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		.mask = BIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		.name = "led_1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		.mask = BIT(6),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		.name = "led_2",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		.mask = BIT(5),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		.name = "led_3",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		.mask = BIT(4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		.name = "led_4",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.mask = BIT(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		.name = "led_5",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		.mask = BIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		.name = "led_6",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		.mask = BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		.name = "led_7",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		.port = 0x49,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		.mask = BIT(0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static DEFINE_SPINLOCK(value_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * we need to store the current led states, as it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * possible to read the current led state via inb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static u8 leds_back;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static u8 leds_front;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static void ot200_led_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct ot200_led *led = container_of(led_cdev, struct ot200_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8 *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	spin_lock_irqsave(&value_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (led->port == 0x49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		val = &leds_front;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	else if (led->port == 0x5a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		val = &leds_back;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (value == LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		*val &= ~led->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		*val |= led->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	outb(*val, led->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	spin_unlock_irqrestore(&value_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int ot200_led_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	for (i = 0; i < ARRAY_SIZE(leds); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		leds[i].cdev.name = leds[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		leds[i].cdev.brightness_set = ot200_led_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ret = devm_led_classdev_register(&pdev->dev, &leds[i].cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	leds_front = 0;		/* turn off all front leds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	leds_back = BIT(1);	/* turn on init led */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	outb(leds_front, 0x49);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	outb(leds_back, 0x5a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static struct platform_driver ot200_led_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	.probe		= ot200_led_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		.name	= "leds-ot200",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) module_platform_driver(ot200_led_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) MODULE_AUTHOR("Sebastian A. Siewior <bigeasy@linutronix.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_DESCRIPTION("ot200 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_ALIAS("platform:leds-ot200");