copy from :https://blog.csdn.net/weixin_42462202/article/details/99949396
Getting Started with Linux drivers (ii) operating hardware
article directories
Linux driver entry (b) operate the hardware
a universal practice
ioremap
iounmap
register read and write
two, gpiolib
gpio.h
gpiolib.c
the GPIO-cfg Samsung platform provides
Third, sum up
a universal practice
played MCU friends should know how to operate register
For example, and now I want to register e.g. 0xFF223440 write address data, microcontroller programming approach as follows
unsigned int * REG = volatile (unsigned int *) 0xFF223440;
* REG = Val;
also similar to the practice in Linux, but Linux can not directly access the register address, and go through the map to access, began to introduce the following
ioremap
mapped address
/ *
* Cookie: indicates the address to be mapped
* size: indicates the address range to be mapped
* /
ioremap (Cookie, size)
such as the above operation to register 0xFF223440, in practice as Linux
unsigned int * = REG volatile ioremap (0xFF223440,. 4);
* REG = Val;
course before application first mapping request_region (start, n, name) address space, but this is not required
After you have mapped address, when the driver exits, no longer use this address, it will iounmap unmap
iounmap
cancel address mapping
/ *
* The cookie: address mapping
* /
iounmap (the cookie)
such as the above example, when the driver exits, call
iounmap (reg);
after canceling map, if there is a previous application address space, you will need to release the application address space release_region (start, n)
Register read
in the above example, the read and write pointer registers accessed directly, the kernel provides a set of macro definitions to read and write registers
readb (c) // Read 8
readw (c) // 16-bit read
readl (c) /// read 32
writeb (v, c) // Write 8
writew (v, c) // 16-bit write
writel (v, c) // 32-bit write
two, gpiolib
described above is common practice, and can access any register set
For gpio kernel setting provides a more convenient way -gpiolib
gpiolib is a function of gpio operation, with these functions can be simplified to gpio operate without directly through register
Of course, only the operation gpiolib gpio, if want to operate gpio associated registers (e.g. lcd controller, a camera controller, etc.), it is still common to use the above method speaking, the following description starts gpiolib
gpio.h
for each one chip, mostly at the time of transplantation has a corresponding file a gpio.h
For each one chip, mostly at the time of transplantation has a corresponding file a gpio.h
S5PV210 platform to Samsung for example, gpio.h located arch \ arm \ mach-s5pv210 \ include \ mach \ gpio.h
Which defines a set of relevant macro gpio
S5PV210_GPA0 (_nr)
S5PV210_GPA1 (_nr)
S5PV210_GPB (_nr)
...
use these macros, by the function provided by the operating gpiolib gpio
gpiolib.c
gpiolib.c positioned drivers \ gpio \ gpiolib.c, which defines a set of operations gpio function, operation function gpio needs with the use of macros in gpio.h
int gpio_request(unsigned gpio, const char *label)
void gpio_free(unsigned gpio)
int gpio_direction_input(unsigned gpio)
int gpio_direction_output(unsigned gpio, int value)
int gpio_get_value(unsigned gpio)
void gpio_set_value(unsigned gpio, int value)
int gpio_to_irq(unsigned gpio)
gpio_request
Application gpio, before using gpio, should first apply gpio, of course, this is not required
gpio_free
Release gpio, the application for release of gpio
gpio_direction_input
Set the input gpio
gpio_direction_output
Gpio set to output low and high set gpio
gpio_get_value
Gpio get high and low
gpio_set_value
Set high and low gpio
gpio_to_irq
Get External gpio corresponding interrupt
If we set GPIOA0 S5PV210 fourth pin is high, I can do this
/ * Application * /
gpio_request (S5PV210_GPA0 (. 3), "m_gpio");
/ * Set * /
gpio_direction_output (S5PV210_GPA0 (. 3),. 1);
/ * Do not need to release * /
gpio_free (S5PV210_GPA0 (. 3));
From the above operation functions can be seen, gpio-lib gpio set only supports input or output, but realize that some can be multiplexed into gpio other functions (e.g. gpio camera interface, etc.), the following is a screenshot from a datasheet of S5PV210
Can be seen that not only can be set to GPE0-0 Input (input mode) and the Output (output mode), may also be multiplexed into CAM_A_PCLK (camera pixel clock pin port A)
So if there is no other support, we need to use common way to configure register, for which Samsung provides a platform and then a function of the operating gpio
Gpio-cfg Samsung platform provides
file located in the arch \ arm \ plat-samsung \ include \ plat \ gpio-cfg.h
Wherein the operation functions are divided into three categories, first gpio configuration, the second type is provided gpio pin state, the third type of drive strength provided gpio
Configuration gpio
#define S3C_GPIO_INPUT (S3C_GPIO_SPECIAL (0)) // input
#define S3C_GPIO_OUTPUT (S3C_GPIO_SPECIAL (1)) // output
#define S3C_GPIO_SFN (x) (S3C_GPIO_SPECIAL ( x)) // multiplexing function
int s3c_gpio_cfgpin (unsigned int pin, unsigned int to); // configuration
unsigned s3c_gpio_getcfg (unsigned int pin); // get the configuration
such as the above example, if you want to set the camera interface GPE0-0 A pixel clock pin, so configuration
s3c_gpio_cfgpin (S5PV210_GPE0 (0), S3C_GPIO_SFN (2));
setting pin state
#define S3C_GPIO_PULL_NONE ((__force s3c_gpio_pull_t) 0x00 ) // floating
#define S3C_GPIO_PULL_DOWN ((__force s3c_gpio_pull_t) 0x01 ) // dropdown
#define S3C_GPIO_PULL_UP ((__force s3c_gpio_pull_t) 0x02 ) // pullup
s3c_gpio_setpull int (unsigned int PIN, s3c_gpio_pull_t pull);
s3c_gpio_pull_t s3c_gpio_getpull (PIN unsigned int);
setting pin drive strength
#define S5P_GPIO_DRVSTR_LV1 ((__force s5p_gpio_drvstr_t)0x00)
#define S5P_GPIO_DRVSTR_LV2 ((__force s5p_gpio_drvstr_t)0x01)
#define S5P_GPIO_DRVSTR_LV3 ((__force s5p_gpio_drvstr_t)0x10)
#define S5P_GPIO_DRVSTR_LV4 ((__force s5p_gpio_drvstr_t)0x11)
s5p_gpio_drvstr_t s5p_gpio_get_drvstr (unsigned int PIN);
s5p_gpio_set_drvstr (unsigned int PIN, s5p_gpio_drvstr_t drvstr);
III Summary
method using a general purpose may be provided in any register, but more cumbersome
to use gpiolib can be more easily configured GPIO, but only GPIO, if necessary set non gpio, still need to use the general method
provided by the kernel only gpiolib gpio configured to input or output, other functions (e.g. multiplexed pins, disposed on the pull-down, etc.) can not be provided by gpiolib, Samsung platform provides another set of functions provided
----------------
Disclaimer: this article is CSDN blogger original article "JT students", following the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/weixin_42462202/article/details/99949396