Skip to content

Commit 1a724ae

Browse files
committed
porting kernel gpio subsystem
Signed-off-by: IotaHydrae <writeforever@foxmail.com>
1 parent 54d247f commit 1a724ae

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed

software/epink-lvgl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ file(GLOB_RECURSE SOURCES "lvgl/src/*.c"
3030
"net/*.c"
3131
"misc/*.c"
3232
"fs/*.c"
33+
"drivers/gpio/*.c"
3334
"drivers/video/*.c"
3435
"drivers/sensors/*.c"
3536
"drivers/i2c/*.c"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file gpiolib.c
3+
* @author IotaHydrae (writeforever@foxmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2023-03-30
7+
*
8+
* MIT License
9+
*
10+
* Copyright 2022 IotaHydrae(writeforever@foxmail.com)
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in all
20+
* copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
*
29+
*/
30+
31+
#include <linux/device.h>
32+
#include <linux/gpio/driver.h>
33+
34+
#include "gpiolib.h"
35+
36+
int gpiochip_add(struct gpio_chip *gc)
37+
{
38+
unsigned long flags;
39+
unsigned int i;
40+
u32 ngpios = 0;
41+
int base = 0;
42+
int ret = 0;
43+
44+
45+
46+
return 0;
47+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* @file gpiolib.h
3+
* @author IotaHydrae (writeforever@foxmail.com)
4+
* @brief
5+
* @version 0.1
6+
* @date 2023-03-30
7+
*
8+
* MIT License
9+
*
10+
* Copyright 2022 IotaHydrae(writeforever@foxmail.com)
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in all
20+
* copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
*
29+
*/
30+
31+
/* SPDX-License-Identifier: GPL-2.0 */
32+
/*
33+
* Internal GPIO functions.
34+
*
35+
* Copyright (C) 2013, Intel Corporation
36+
* Copyright (C) 2023, iotahydrae
37+
* Author: Mika Westerberg <mika.westerberg@linux.intel.com>
38+
*/
39+
40+
#ifndef GPIOLIB_H
41+
#define GPIOLIB_H
42+
43+
#include <common/types.h>
44+
#include <common/list.h>
45+
#include <linux/device.h>
46+
47+
#define GPIOCHIP_NAME "gpiochip"
48+
49+
struct gpio_device;
50+
struct gpio_desc;
51+
52+
/**
53+
* struct gpio_device - internal state container for GPIO devices
54+
* @dev: the GPIO device struct
55+
* @chrdev: character device for the GPIO device
56+
* @id: numerical ID number for the GPIO chip
57+
* @mockdev: class device used by the deprecated sysfs interface (may be
58+
* NULL)
59+
* @owner: helps prevent removal of modules exporting active GPIOs
60+
* @chip: pointer to the corresponding gpiochip, holding static
61+
* data for this device
62+
* @descs: array of ngpio descriptors.
63+
* @ngpio: the number of GPIO lines on this GPIO device, equal to the size
64+
* of the @descs array.
65+
* @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
66+
* at device creation time.
67+
* @label: a descriptive name for the GPIO device, such as the part number
68+
* or name of the IP component in a System on Chip.
69+
* @data: per-instance data assigned by the driver
70+
* @list: links gpio_device:s together for traversal
71+
* @notifier: used to notify subscribers about lines being requested, released
72+
* or reconfigured
73+
* @sem: protects the structure from a NULL-pointer dereference of @chip by
74+
* user-space operations when the device gets unregistered during
75+
* a hot-unplug event
76+
* @pin_ranges: range of pins served by the GPIO driver
77+
*
78+
* This state container holds most of the runtime variable data
79+
* for a GPIO device and can hold references and live on after the
80+
* GPIO chip has been removed, if it is still being used from
81+
* userspace.
82+
*/
83+
struct gpio_device {
84+
struct device dev;
85+
// struct cdev chrdev;
86+
int id;
87+
struct device *mockdev;
88+
// struct module *owner;
89+
struct gpio_chip *chip;
90+
// struct gpio_desc *descs;
91+
int base;
92+
u16 ngpio;
93+
const char *label;
94+
void *data;
95+
struct list_head list;
96+
// struct blocking_notifier_head notifier;
97+
// struct rw_semaphore sem;
98+
99+
#ifdef CONFIG_PINCTRL
100+
/*
101+
* If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
102+
* describe the actual pin range which they serve in an SoC. This
103+
* information would be used by pinctrl subsystem to configure
104+
* corresponding pins for gpio usage.
105+
*/
106+
struct list_head pin_ranges;
107+
#endif
108+
};
109+
110+
/**
111+
* struct gpio_desc - Opaque descriptor for a GPIO
112+
*
113+
* @gdev: Pointer to the parent GPIO device
114+
* @flags: Binary descriptor flags
115+
* @label: Name of the consumer
116+
* @name: Line name
117+
* @hog: Pointer to the device node that hogs this line (if any)
118+
* @debounce_period_us: Debounce period in microseconds
119+
*
120+
* These are obtained using gpiod_get() and are preferable to the old
121+
* integer-based handles.
122+
*
123+
* Contrary to integers, a pointer to a &struct gpio_desc is guaranteed to be
124+
* valid until the GPIO is released.
125+
*/
126+
struct gpio_desc {
127+
struct gpio_device *gdev;
128+
unsigned long flags;
129+
/* flag symbols are bit numbers */
130+
#define FLAG_REQUESTED 0
131+
#define FLAG_IS_OUT 1
132+
#define FLAG_EXPORT 2 /* protected by sysfs_lock */
133+
#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
134+
#define FLAG_ACTIVE_LOW 6 /* value has active low */
135+
#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
136+
#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
137+
#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
138+
#define FLAG_IRQ_IS_ENABLED 10 /* GPIO is connected to an enabled IRQ */
139+
#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
140+
#define FLAG_TRANSITORY 12 /* GPIO may lose value in sleep or reset */
141+
#define FLAG_PULL_UP 13 /* GPIO has pull up enabled */
142+
#define FLAG_PULL_DOWN 14 /* GPIO has pull down enabled */
143+
#define FLAG_BIAS_DISABLE 15 /* GPIO has pull disabled */
144+
#define FLAG_EDGE_RISING 16 /* GPIO CDEV detects rising edge events */
145+
#define FLAG_EDGE_FALLING 17 /* GPIO CDEV detects falling edge events */
146+
#define FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
147+
#define FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
148+
149+
/* Connection label */
150+
const char *label;
151+
/* Name of the GPIO */
152+
const char *name;
153+
#ifdef CONFIG_OF_DYNAMIC
154+
struct device_node *hog;
155+
#endif
156+
#ifdef CONFIG_GPIO_CDEV
157+
/* debounce period in microseconds */
158+
unsigned int debounce_period_us;
159+
#endif
160+
};
161+
162+
int gpiod_request(struct gpio_desc *desc, const char *label);
163+
void gpiod_free(struct gpio_desc *desc);
164+
165+
#endif

0 commit comments

Comments
 (0)