Linux-libre 5.7.3-gnu
[librecmc/linux-libre.git] / arch / unicore32 / kernel / puv3-nb0916.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/unicore32/kernel/puv3-nb0916.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
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/mtd/physmap.h>
15 #include <linux/io.h>
16 #include <linux/reboot.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/pwm.h>
20 #include <linux/pwm_backlight.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio_keys.h>
23 #include <linux/input.h>
24
25 #include <mach/hardware.h>
26
27 static struct physmap_flash_data physmap_flash_data = {
28         .width          = 1,
29 };
30
31 static struct resource physmap_flash_resource = {
32         .start          = 0xFFF80000,
33         .end            = 0xFFFFFFFF,
34         .flags          = IORESOURCE_MEM,
35 };
36
37 static struct resource puv3_i2c_resources[] = {
38         [0] = {
39                 .start = io_v2p(PKUNITY_I2C_BASE),
40                 .end   = io_v2p(PKUNITY_I2C_BASE) + 0xff,
41                 .flags = IORESOURCE_MEM,
42         },
43         [1] = {
44                 .start = IRQ_I2C,
45                 .end   = IRQ_I2C,
46                 .flags = IORESOURCE_IRQ,
47         }
48 };
49
50 static struct pwm_lookup nb0916_pwm_lookup[] = {
51         PWM_LOOKUP("PKUnity-v3-PWM", 0, "pwm-backlight", NULL, 70 * 1024,
52                    PWM_POLARITY_NORMAL),
53 };
54
55 static struct platform_pwm_backlight_data nb0916_backlight_data = {
56         .max_brightness = 100,
57         .dft_brightness = 100,
58 };
59
60 static struct gpio_keys_button nb0916_gpio_keys[] = {
61         {
62                 .type   = EV_KEY,
63                 .code   = KEY_POWER,
64                 .gpio   = GPI_SOFF_REQ,
65                 .desc   = "Power Button",
66                 .wakeup = 1,
67                 .active_low = 1,
68         },
69         {
70                 .type   = EV_KEY,
71                 .code   = BTN_TOUCH,
72                 .gpio   = GPI_BTN_TOUCH,
73                 .desc   = "Touchpad Button",
74                 .wakeup = 1,
75                 .active_low = 1,
76         },
77 };
78
79 static struct gpio_keys_platform_data nb0916_gpio_button_data = {
80         .buttons        = nb0916_gpio_keys,
81         .nbuttons       = ARRAY_SIZE(nb0916_gpio_keys),
82 };
83
84 static irqreturn_t nb0916_lcdcaseoff_handler(int irq, void *dev_id)
85 {
86         if (gpio_get_value(GPI_LCD_CASE_OFF))
87                 gpio_set_value(GPO_LCD_EN, 1);
88         else
89                 gpio_set_value(GPO_LCD_EN, 0);
90
91         return IRQ_HANDLED;
92 }
93
94 static irqreturn_t nb0916_overheat_handler(int irq, void *dev_id)
95 {
96         machine_halt();
97         /* SYSTEM HALT, NO RETURN */
98         return IRQ_HANDLED;
99 }
100
101 static struct i2c_board_info __initdata puv3_i2c_devices[] = {
102         {       I2C_BOARD_INFO("lm75",          I2C_TAR_THERMAL),       },
103         {       I2C_BOARD_INFO("bq27200",       I2C_TAR_PWIC),          },
104         {       I2C_BOARD_INFO("24c02",         I2C_TAR_EEPROM),        },
105 };
106
107 int __init mach_nb0916_init(void)
108 {
109         i2c_register_board_info(0, puv3_i2c_devices,
110                         ARRAY_SIZE(puv3_i2c_devices));
111
112         platform_device_register_simple("PKUnity-v3-I2C", -1,
113                         puv3_i2c_resources, ARRAY_SIZE(puv3_i2c_resources));
114
115         pwm_add_table(nb0916_pwm_lookup, ARRAY_SIZE(nb0916_pwm_lookup));
116
117         platform_device_register_data(NULL, "pwm-backlight", -1,
118                         &nb0916_backlight_data, sizeof(nb0916_backlight_data));
119
120         platform_device_register_data(NULL, "gpio-keys", -1,
121                         &nb0916_gpio_button_data, sizeof(nb0916_gpio_button_data));
122
123         platform_device_register_resndata(NULL, "physmap-flash", -1,
124                         &physmap_flash_resource, 1,
125                         &physmap_flash_data, sizeof(physmap_flash_data));
126
127         if (request_irq(gpio_to_irq(GPI_LCD_CASE_OFF),
128                 &nb0916_lcdcaseoff_handler,
129                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
130                 "NB0916 lcd case off", NULL) < 0) {
131
132                 printk(KERN_DEBUG "LCD-Case-OFF IRQ %d not available\n",
133                         gpio_to_irq(GPI_LCD_CASE_OFF));
134         }
135
136         if (request_irq(gpio_to_irq(GPI_OTP_INT), &nb0916_overheat_handler,
137                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
138                 "NB0916 overheating protection", NULL) < 0) {
139
140                 printk(KERN_DEBUG "Overheating Protection IRQ %d not available\n",
141                         gpio_to_irq(GPI_OTP_INT));
142         }
143
144         return 0;
145 }
146
147 subsys_initcall_sync(mach_nb0916_init);