Linux-libre 5.7.3-gnu
[librecmc/linux-libre.git] / arch / unicore32 / kernel / gpio.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/unicore32/kernel/gpio.c
4  *
5  * Code specific to PKUnity SoC and UniCore ISA
6  *
7  *      Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
8  *      Copyright (C) 2001-2010 Guan Xuetao
9  */
10 /* in FPGA, no GPIO support */
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/gpio/driver.h>
15 /* FIXME: needed for gpio_set_value() - convert to use descriptors or hogs */
16 #include <linux/gpio.h>
17 #include <mach/hardware.h>
18
19 #ifdef CONFIG_LEDS
20 #include <linux/leds.h>
21 #include <linux/platform_device.h>
22
23 static const struct gpio_led puv3_gpio_leds[] = {
24         { .name = "cpuhealth", .gpio = GPO_CPU_HEALTH, .active_low = 0,
25                 .default_trigger = "heartbeat", },
26         { .name = "hdd_led", .gpio = GPO_HDD_LED, .active_low = 1,
27                 .default_trigger = "disk-activity", },
28 };
29
30 static const struct gpio_led_platform_data puv3_gpio_led_data = {
31         .num_leds =     ARRAY_SIZE(puv3_gpio_leds),
32         .leds =         (void *) puv3_gpio_leds,
33 };
34
35 static struct platform_device puv3_gpio_gpio_leds = {
36         .name =         "leds-gpio",
37         .id =           -1,
38         .dev = {
39                 .platform_data = (void *) &puv3_gpio_led_data,
40         }
41 };
42
43 static int __init puv3_gpio_leds_init(void)
44 {
45         platform_device_register(&puv3_gpio_gpio_leds);
46         return 0;
47 }
48
49 device_initcall(puv3_gpio_leds_init);
50 #endif
51
52 static int puv3_gpio_get(struct gpio_chip *chip, unsigned offset)
53 {
54         return !!(readl(GPIO_GPLR) & GPIO_GPIO(offset));
55 }
56
57 static void puv3_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
58 {
59         if (value)
60                 writel(GPIO_GPIO(offset), GPIO_GPSR);
61         else
62                 writel(GPIO_GPIO(offset), GPIO_GPCR);
63 }
64
65 static int puv3_direction_input(struct gpio_chip *chip, unsigned offset)
66 {
67         unsigned long flags;
68
69         local_irq_save(flags);
70         writel(readl(GPIO_GPDR) & ~GPIO_GPIO(offset), GPIO_GPDR);
71         local_irq_restore(flags);
72         return 0;
73 }
74
75 static int puv3_direction_output(struct gpio_chip *chip, unsigned offset,
76                 int value)
77 {
78         unsigned long flags;
79
80         local_irq_save(flags);
81         puv3_gpio_set(chip, offset, value);
82         writel(readl(GPIO_GPDR) | GPIO_GPIO(offset), GPIO_GPDR);
83         local_irq_restore(flags);
84         return 0;
85 }
86
87 static struct gpio_chip puv3_gpio_chip = {
88         .label                  = "gpio",
89         .direction_input        = puv3_direction_input,
90         .direction_output       = puv3_direction_output,
91         .set                    = puv3_gpio_set,
92         .get                    = puv3_gpio_get,
93         .base                   = 0,
94         .ngpio                  = GPIO_MAX + 1,
95 };
96
97 void __init puv3_init_gpio(void)
98 {
99         writel(GPIO_DIR, GPIO_GPDR);
100 #if     defined(CONFIG_PUV3_NB0916) || defined(CONFIG_PUV3_SMW0919)     \
101         || defined(CONFIG_PUV3_DB0913)
102         gpio_set_value(GPO_WIFI_EN, 1);
103         gpio_set_value(GPO_HDD_LED, 1);
104         gpio_set_value(GPO_VGA_EN, 1);
105         gpio_set_value(GPO_LCD_EN, 1);
106         gpio_set_value(GPO_CAM_PWR_EN, 0);
107         gpio_set_value(GPO_LCD_VCC_EN, 1);
108         gpio_set_value(GPO_SOFT_OFF, 1);
109         gpio_set_value(GPO_BT_EN, 1);
110         gpio_set_value(GPO_FAN_ON, 0);
111         gpio_set_value(GPO_SPKR, 0);
112         gpio_set_value(GPO_CPU_HEALTH, 1);
113         gpio_set_value(GPO_LAN_SEL, 1);
114 /*
115  * DO NOT modify the GPO_SET_V1 and GPO_SET_V2 in kernel
116  *      gpio_set_value(GPO_SET_V1, 1);
117  *      gpio_set_value(GPO_SET_V2, 1);
118  */
119 #endif
120         gpiochip_add_data(&puv3_gpio_chip, NULL);
121 }