SlideShare a Scribd company logo
Introduction to pin muxing and
GPIO control under Linux
Neil Armstrong - BayLibre
#lfelc @superna9999
Hardware Level
Hardware Level
Pins or Balls on SoC Package
Quad Flat Package (QFP)
Ball Grid Array (BGA)
Quad Flat No-leads package (QFN)
Hardware Level
Wires from the die to the
external package pins or balls.
Hardware Level
Main HW block of a “PAD”
Can handle multiple functions for
different internal HW blocks.
GPIO is one of the functions
Input can be muxed of more often
directly connected to all HW blocks.
Hardware Level
SoC pads are generally “Tri-State”:
- Hi-Z: high impedance, not driven by
the SoC
- 1 level: Driven to the pad voltage
- 0 level: Driven to ground
Hardware Level
An Open-Drain pad can only drive to 0
(ground)
Used for example for the I2C and 1-wire
busses.
Hardware Level
An Open-Source pad can only drive to 1
(VCC).
Not widely used but exists.
Hardware Level
Analog pad HW also optionally have:
- Pull-Up: small internal resistor to VCC
- Pull-Down: small internal resistor to GND
- Drive strength: to select different output signal strengths
- ….
Hardware Level
PAD functions can be selected:
- Individually: most common => a number identifies the function
- By Group: groups multiple PADs for a specific function at once
Some weird setups exists:
- Multi-function PADs without GPIO
- GPIO-only PADs
- Function can be selected on multiple PADs (generally only output)
Software Level: Legacy
Software Level: Legacy
Before introduction of the PINCTRL framework:
ANARCHY !
Code was mainly in arch/arm/ and was from:
- Very basic hard coded pin setup for SoC and Board
to
- Extremely complex framework with conflict checking
Software Level: Legacy
Lessons learned:
- Pin Conflict Checking is almost impossible to implement
- Would need an insane level of code and HW description to handle
- Is in fact useless, proper code review and testing is enough
- We need dynamic pin mux setup
- Needed for low-power devices (Phones, Tablets, …)
- Some Drivers needs pin mux / config change at runtime
- No more hardcoding
- maintaining SoC/Board description C code is like hell
- Not scalable
Software Level: Legacy
GPIO API was also ANARCHY.
API was not described in include/linux, but in each
arch/arm/mach-XXXX/include/mach/gpio.h
Each SoC potentially had a different:
- API
- Implementation
- Behavior
Software Level: Legacy
- API was too basic
- Implementations were all different
- Multiple GPIO controllers was complex too handle
- Nested GPIO controllers was also impossible to handle
- I2C, SPI, USB, … GPIO extenders
- Advanced API was only available on some SoC
- GPIO IRQ was also very SoC specific
Software Level: PINCTRL Framework
Software Level: PINCTRL Framework
The “pin control subsystem” submitted by Linus Walleij
was merged in Linux 3.2-rc2 in Oct 13, 2011
This creates a subsystem for handling of pin control devices.
These are devices that control different aspects of package pins.
Currently it handles pinmuxing, i.e. assigning electronic functions to groups of pins [...].
The plan is to also handle other I/O pin control aspects [...] to remove a lot of ARM arch
code as well as feature-creepy GPIO drivers which are implementing the same thing over
and over again.
Software Level: PINCTRL Framework
Definition of PIN CONTROLLER:
A pin controller is a piece of hardware, usually a set of registers, that can
control PINs.
It may be able to multiplex, bias, set load capacitance, set drive strength
etc for individual pins or groups of pins.
Software Level: PINCTRL Framework
Definition of PIN:
PINS are equal to pads, fingers, balls or whatever packaging input or output line
you want to control and these are denoted by unsigned integers in the range
0..maxpin.
This number space is local to each PIN CONTROLLER, so there may be several
such number spaces in a system.
This pin space may be sparse - i.e. there may be gaps in the space with
numbers where no pin exists.
Software Level: PINCTRL Framework
Pin groups
Many controllers need to deal with groups of pins, so the pin controller
subsystem has a mechanism for enumerating groups of pins and retrieving
the actual enumerated pins that are part of a certain group.
Software Level: PINCTRL Framework
PINMUX, also known as padmux, ballmux, alternate functions or mission
modes is a way for chip vendors producing some kind of electrical
packages to use a certain physical pin (ball, pad, finger, etc) for multiple
mutually exclusive functions, depending on the application.
By "application" in this context we usually mean a way of soldering or wiring
the package into an electronic system, even though the framework makes
it possible to also change the function at runtime.
Software Level: PINCTRL Framework
PINS
A0
A1
A2
…
B2
B3
...
G4
G5
G6
…
FUNCTIONS
i2c0: {I2C0}
uart1: {UART1,
UART1_RTS, UART1_CTS}
...
PINGROUPS
I2C0 {P8, K2}
UART1 {C5, K2, P3}
UART1_RTS {P8}
UART1_CTS {P9}
...
Software Level: PINCTRL Framework
● Pins can have holes, and “pretty names” like Ball ID
○ The name reflects what the vendor uses to distinguish pins
○ But each must be unique
● Pingroups can have 1 to many pins
○ Multiple pingroups can share the same pins
● Functions associates a name to a set of pingroups
○ Multiple functions can share the same pingroups
○ But only one of these conflicting functions can be set at the same time
Software Level: PINCTRL Framework
● Pins <-> Groups <-> Function association is done in driver to match the
underlying hardware
● Pinmux Framework needs :
○ Pins list
const struct pinctrl_pin_desc *pins;
unsigned int npins;
○ Pin control ops
const struct pinctrl_ops *pctlops;
○ Pin muxing ops
const struct pinmux_ops *pmxops;
○ Pin config ops
const struct pinconf_ops *confops;
Software Level: PINCTRL Framework
● const struct pinctrl_ops *pctlops;
○ int (*get_groups_count) (struct pinctrl_dev *pctldev)
■ Get the number of pin groups
○ const char *(*get_group_name) (struct pinctrl_dev *pctldev, unsigned selector);
■ Get the name (string identifier) of a pin group
○ int (*get_group_pins) (struct pinctrl_dev *pctldev, unsigned selector,
const unsigned **pins, unsigned *num_pins);
■ Get the pins involved in a pin group
Software Level: PINCTRL Framework
● struct pinmux_ops
○ int (*get_functions_count) (struct pinctrl_dev *pctldev);
■ Get the number of functions
○ const char *(*get_function_name) (struct pinctrl_dev *pctldev, unsigned selector);
■ Get the name of a function
○ int (*get_function_groups) (struct pinctrl_dev *pctldev, unsigned selector,
const char * const **groups, unsigned *num_groups);
■ Get the pin groups involved in a function
○ int (*set_mux) (struct pinctrl_dev *pctldev, unsigned func_selector,
unsigned group_selector);
■ Set the pins of a groups in a function
Software Level: PINCTRL Framework
● Pinctrl subsystem was designed *before* Device Tree
● Pinmux mapping was hard-coded in arch/arm/mach-
● Now mapping is described in DT (or ACPI)
● Usage is still the same
○ p = devm_pinctrl_get(dev);
○ S = pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT);
○ pinctrl_select_state(p, s);
Software Level: PINCTRL Framework
● DT Mapping
○ uart1_pins: uart1 {
mux {
groups = "UART1", "UART1_RTS", "UART1_CTS";
function = "uart1";
};
};
● From a device node
○ uart@123456 {
pinctrl-0 = <&uart1_pins>;
pinctrl-names = "default";
…
};
FUNCTIONS
i2c0: {I2C0}
uart1: {UART1,
UART1_RTS, UART1_CTS}
...
PINGROUPS
I2C0 {P8, K2}
UART1 {C5, K2, P3}
UART1_RTS {P8}
UART1_CTS {P9}
...
Software Level: GPIO Framework
Software Level: GPIO Framework
The “gpio provider infrastructure” submitted by David Brownell
was merged in Linux 2.6.25-rc2 in Feb 5, 2008
Provide new implementation infrastructure that platforms may choose to
use when implementing the GPIO programming interface.
Platforms can update their GPIO support to use this.
Finally an unified GPIO API and implementation.
Software Level: GPIO Framework
The “gpio provider infrastructure” is named “gpiolib”.
Each GPIO was identified as an unique number.
This number was used in data platform to identify GPIOs to use by drivers.
Handling of GPIO extender was possible, but this static numbering made it limited.
Software Level: GPIO Framework
The “gpio provider infrastructure” was progressively replaced by:
GPIOD
It replaced the “legacy” GPIO interface by maintaining the original API
while introducing a new GPIO framework based around a struct gpio_desc
for each GPIO for the consumers.
Internally, an unique number still identifies each GPIO.
Software Level: GPIO Framework
The legacy API is still usable from:
include/linux/gpio.h
But is removed bits per bits in favor or:
include/linux/gpio/ with consumer.h for drivers.
Software Level: GPIO Framework
Like the PINCTRL framework, the consumer and implementation API are
separate.
For the GPIO controller implementation:
include/linux/gpio/driver.h
For the GPIO consumer:
include/linux/gpio/consumer.h
Software Level: GPIO Framework
include/linux/gpio/driver.h
Basic GPIO controller registration:
chip->gpio_chip.label = “NAME”;
direction_output() will be called to set input or output
chip->gpio_chip.direction_output = direction_output;
get_value() will be called to get the GPIO input value (0 or 1)
chip->gpio_chip.get = get_value;
set_value() will be called to set the GPIO output value (0 or 1)
chip->gpio_chip.set = set_value;
chip->gpio_chip.base = -1;
chip->gpio_chip.ngpio = NUMBER_GPIOS;
parent links the GPIO controller to the device
chip->gpio_chip.parent = dev;
gpiochip_add_data(&chip->gpio_chip, chip);
Software Level: GPIO Framework
include/linux/gpio/consumer.h
With Device-Tree, gpios are linked to device nodes with:
audio-amplifier-0 {
compatible = "simple-audio-amplifier";
enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>;
status = "okay";
};
Software Level: GPIO Framework
audio-amplifier-0 {
compatible = "simple-audio-amplifier";
enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>;
status = "okay";
};
enable-gpios
“enable” is the “function” name associated to the GPIO, can be enything
like “reset”, “shutdown”, ….
Software Level: GPIO Framework
audio-amplifier-0 {
compatible = "simple-audio-amplifier";
enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>;
status = "okay";
};
<&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>;
GPIO Controller
phandle
GPIO Identifier/Offset
=> Controller/Platform
Specific
GPIO Flags
Software Level: GPIO Framework
=> include/dt-bindings/gpio/gpio.h
/* Bit 0 express polarity */
#define GPIO_ACTIVE_HIGH 0
#define GPIO_ACTIVE_LOW 1
/* Bit 1 express single-endedness */
#define GPIO_PUSH_PULL 0
#define GPIO_SINGLE_ENDED 2
/* Bit 2 express Open drain or open source */
#define GPIO_LINE_OPEN_SOURCE 0
#define GPIO_LINE_OPEN_DRAIN 4
/* Open Drain/Collector is the combination of single-ended open drain interface.
* Open Source/Emitter is the combination of single-ended open source interface. */
#define GPIO_OPEN_DRAIN (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_DRAIN)
#define GPIO_OPEN_SOURCE (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_SOURCE)
/* Bit 3 express GPIO suspend/resume and reset persistence */
#define GPIO_PERSISTENT 0
#define GPIO_TRANSITORY 8
/* Bit 4 express pull up */
#define GPIO_PULL_UP 16
/* Bit 5 express pull down */
#define GPIO_PULL_DOWN 32
Software Level: GPIO Framework
include/linux/gpio/consumer.h
Get the “enable” GPIO, and initialize it to “LOW” output:
gpiod_enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
Set value:
gpiod_set_value(gpiod_enable, val);
Software Level: GPIO Framework
include/linux/gpio/consumer.h
With the DT flags (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW), the GPIO core will invert the
GPIO value to match the “ACTIVE” polarity.
Active polarity is when the signal “acts”
● !RESET IC port will expect GPIO_ACTIVE_LOW
● EN IC port will expect GPIO_ACTIVE_HIGH
Software Level: GPIO Framework
include/linux/gpio/consumer.h
For example, if GPIO_ACTIVE_LOW is passed in DT flag:
● gpiod_set_value(gpiod_enable, 0); => will set the GPIO to 1
● gpiod_set_value(gpiod_enable, 1); => will set the GPIO to 0
Software Level: PINCTRL & GPIO
Software Level: PINCTRL & GPIO
● The PINCTRL framework is able to “link” itself to GPIO
● Often the GPIO controller is “inside”/”next” the PINCTRL HW
● Can specify “ranges” between a PIN controller and GPIO controllers
Thanks for listening !
Scan me for the slides

More Related Content

PDF
Elc Europe 2020 : u-boot- porting and maintaining a bootloader for a multimed...
Neil Armstrong
 
PDF
Linux Conference Australia 2018 : Device Tree, past, present, future
Neil Armstrong
 
PDF
ELC-NA 2020: War story - Using mainline linux for an Android TV bsp
Neil Armstrong
 
PDF
Lee 2020 what the clock !
Neil Armstrong
 
PDF
ELC-E 2016 Neil Armstrong - No, it's never too late to upstream your legacy l...
Neil Armstrong
 
PDF
ELC-E 2019 Device tree, past, present, future
Neil Armstrong
 
PDF
Ostech war story using mainline linux for an android tv bsp
Neil Armstrong
 
PDF
LAS16-403: GDB Linux Kernel Awareness
Linaro
 
Elc Europe 2020 : u-boot- porting and maintaining a bootloader for a multimed...
Neil Armstrong
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Neil Armstrong
 
ELC-NA 2020: War story - Using mainline linux for an Android TV bsp
Neil Armstrong
 
Lee 2020 what the clock !
Neil Armstrong
 
ELC-E 2016 Neil Armstrong - No, it's never too late to upstream your legacy l...
Neil Armstrong
 
ELC-E 2019 Device tree, past, present, future
Neil Armstrong
 
Ostech war story using mainline linux for an android tv bsp
Neil Armstrong
 
LAS16-403: GDB Linux Kernel Awareness
Linaro
 

What's hot (20)

PDF
LAS16-500: The Rise and Fall of Assembler and the VGIC from Hell
Linaro
 
PDF
LAS16-310: Introducing the first 96Boards TV Platform: Poplar by Hisilicon
Linaro
 
PDF
LAS16-109: LAS16-109: The status quo and the future of 96Boards
Linaro
 
PDF
LAS16-TR03: Upstreaming 201
Linaro
 
PDF
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
PDF
LAS16-201: ART JIT in Android N
Linaro
 
PDF
LAS16-100K1: Welcome Keynote
Linaro
 
PDF
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
Linaro
 
PDF
Las16 200 - firmware summit - ras what is it- why do we need it
Linaro
 
PDF
LAS16-209: Finished and Upcoming Projects in LMG
Linaro
 
PDF
LAS16-301: OpenStack on Aarch64, running in production, upstream improvements...
Linaro
 
PDF
MOVED: RDK/WPE Port on DB410C - SFO17-206
Linaro
 
PPTX
LAS16-106: GNU Toolchain Development Lifecycle
Linaro
 
PDF
Embedded Recipes 2019 - Pipewire a new foundation for embedded multimedia
Anne Nicolas
 
PDF
LAS16-300: Mini Conference 2 Cortex-M Software - Device Configuration
Linaro
 
PDF
Deploy STM32 family on Zephyr - SFO17-102
Linaro
 
PDF
LAS16-200: SCMI - System Management and Control Interface
Linaro
 
PDF
LAS16-405:OpenDataPlane: Software Defined Dataplane leader
Linaro
 
PDF
LAS16-210: Hardware Assisted Tracing on ARM with CoreSight and OpenCSD
Linaro
 
PDF
BKK16-400A LuvOS and ACPI Compliance Testing
Linaro
 
LAS16-500: The Rise and Fall of Assembler and the VGIC from Hell
Linaro
 
LAS16-310: Introducing the first 96Boards TV Platform: Poplar by Hisilicon
Linaro
 
LAS16-109: LAS16-109: The status quo and the future of 96Boards
Linaro
 
LAS16-TR03: Upstreaming 201
Linaro
 
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
LAS16-201: ART JIT in Android N
Linaro
 
LAS16-100K1: Welcome Keynote
Linaro
 
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
Linaro
 
Las16 200 - firmware summit - ras what is it- why do we need it
Linaro
 
LAS16-209: Finished and Upcoming Projects in LMG
Linaro
 
LAS16-301: OpenStack on Aarch64, running in production, upstream improvements...
Linaro
 
MOVED: RDK/WPE Port on DB410C - SFO17-206
Linaro
 
LAS16-106: GNU Toolchain Development Lifecycle
Linaro
 
Embedded Recipes 2019 - Pipewire a new foundation for embedded multimedia
Anne Nicolas
 
LAS16-300: Mini Conference 2 Cortex-M Software - Device Configuration
Linaro
 
Deploy STM32 family on Zephyr - SFO17-102
Linaro
 
LAS16-200: SCMI - System Management and Control Interface
Linaro
 
LAS16-405:OpenDataPlane: Software Defined Dataplane leader
Linaro
 
LAS16-210: Hardware Assisted Tracing on ARM with CoreSight and OpenCSD
Linaro
 
BKK16-400A LuvOS and ACPI Compliance Testing
Linaro
 
Ad

Similar to ELC North America 2021 Introduction to pin muxing and gpio control under linux (20)

PDF
Micro-controllers (PIC) based Application Development
Emertxe Information Technologies Pvt Ltd
 
PDF
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Anne Nicolas
 
PDF
DEF CON 27- SHEILA A BERTA - backdooring hardware devices by injecting malici...
Felipe Prado
 
PPTX
Microcontroller from basic_to_advanced
Imran Sheikh
 
PDF
Embedded_Systems_firstcourse_UniversitéToulouse.pdf
TinhinaneAitMessaoud
 
PPTX
Iot Workshop NITT 2015
Srivignessh Pss
 
PPTX
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
vasankarponnapalli2
 
PPTX
Python-in-Embedded-systems.pptx
TuynLCh
 
PDF
Hardware hacking
Tavish Naruka
 
PPTX
EC18713 EMBEDDED SYSTEMS LABORATORY, EC18713
ARULPECE
 
PPTX
How to use an Arduino
AntonAndreev13
 
PPTX
Msp 430 architecture module 1
SARALA T
 
PDF
Using ARM Dev.Board in physical experimental instruments
a_n0v
 
PPT
Introduction to Microprocessor & Microcontroller.ppt
ManishTomar79
 
PPTX
Introduction to embedded system & density based traffic light system
Rani Loganathan
 
PPT
Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Premier Farnell
 
PDF
Raspberry Pi - best friend for all your GPIO needs
Dobrica Pavlinušić
 
PDF
MTE104-L2: Overview of Microcontrollers
Abdalla Ahmed
 
PPT
Unit 4 Part I.ppt this contains notes for
vaishnavibhapkar1207
 
PDF
Project ACRN GPIO mediator introduction
Project ACRN
 
Micro-controllers (PIC) based Application Development
Emertxe Information Technologies Pvt Ltd
 
Kernel Recipes 2018 - New GPIO interface for linux user space - Bartosz Golas...
Anne Nicolas
 
DEF CON 27- SHEILA A BERTA - backdooring hardware devices by injecting malici...
Felipe Prado
 
Microcontroller from basic_to_advanced
Imran Sheikh
 
Embedded_Systems_firstcourse_UniversitéToulouse.pdf
TinhinaneAitMessaoud
 
Iot Workshop NITT 2015
Srivignessh Pss
 
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
vasankarponnapalli2
 
Python-in-Embedded-systems.pptx
TuynLCh
 
Hardware hacking
Tavish Naruka
 
EC18713 EMBEDDED SYSTEMS LABORATORY, EC18713
ARULPECE
 
How to use an Arduino
AntonAndreev13
 
Msp 430 architecture module 1
SARALA T
 
Using ARM Dev.Board in physical experimental instruments
a_n0v
 
Introduction to Microprocessor & Microcontroller.ppt
ManishTomar79
 
Introduction to embedded system & density based traffic light system
Rani Loganathan
 
Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Premier Farnell
 
Raspberry Pi - best friend for all your GPIO needs
Dobrica Pavlinušić
 
MTE104-L2: Overview of Microcontrollers
Abdalla Ahmed
 
Unit 4 Part I.ppt this contains notes for
vaishnavibhapkar1207
 
Project ACRN GPIO mediator introduction
Project ACRN
 
Ad

More from Neil Armstrong (7)

PDF
Linux Conf Australia 2018 - Kernel Miniconf - Mainline Linux on Amlogic SoCs
Neil Armstrong
 
PDF
Embedded Recipes 2017 - Mainline Linux on Amlogic SoCs
Neil Armstrong
 
PDF
ELC North America 2017- Mainline Linux on Amlogic SoCs
Neil Armstrong
 
KEY
Fête de l'internet d'Antibes 2011 - Recherche sur Internet
Neil Armstrong
 
PDF
Fete internet antibes 2011 - Recherche sur Internet
Neil Armstrong
 
PDF
IPv6, un second souffle pour l’internet
Neil Armstrong
 
PDF
Intellicore Tech Talk 10 - Apache Web Server Internals
Neil Armstrong
 
Linux Conf Australia 2018 - Kernel Miniconf - Mainline Linux on Amlogic SoCs
Neil Armstrong
 
Embedded Recipes 2017 - Mainline Linux on Amlogic SoCs
Neil Armstrong
 
ELC North America 2017- Mainline Linux on Amlogic SoCs
Neil Armstrong
 
Fête de l'internet d'Antibes 2011 - Recherche sur Internet
Neil Armstrong
 
Fete internet antibes 2011 - Recherche sur Internet
Neil Armstrong
 
IPv6, un second souffle pour l’internet
Neil Armstrong
 
Intellicore Tech Talk 10 - Apache Web Server Internals
Neil Armstrong
 

Recently uploaded (20)

PPTX
basic_parts-of_computer-1618-754-622.pptx
patelravi16187
 
PPTX
Mobile-Device-Management-MDM-Architecture.pptx
pranavnandwanshi99
 
PPTX
PPT on the topic of programming language
dishasindhava
 
PPTX
Operating-Systems-A-Journey ( by information
parthbhanushali307
 
PDF
Abbreviations in NC-ISM_syllabus.pdf hejsnsjs
raipureastha08
 
PPTX
Intro_S4HANA_Using_Global_Bike_Slides_SD_en_v4.1.pptx
trishalasharma7
 
PPTX
Basics of Memristors and fundamentals.pptx
onterusmail
 
PPTX
PHISHING ATTACKS. _. _.pptx[]
kumarrana7525
 
PPTX
西班牙海牙认证瓦伦西亚国际大学毕业证与成绩单文凭复刻快速办理毕业证书
sw6vvn9s
 
PPTX
原版UMiami毕业证文凭迈阿密大学学费单定制学历在线制作硕士毕业证
jicaaeb0
 
PDF
Endalamaw Kebede.pdfvvbhjjnhgggftygtttfgh
SirajudinAkmel1
 
PPTX
2.Important-Definihhhhhhtions18 (1).pptx
trishalasharma7
 
PPTX
cocomo-220726173706-141e08f0.tyuiuuupptx
DharaniMani4
 
PPTX
13. ANAESTHETICS AND ALCOHOLS.pptx fucking
sriramraja650
 
PDF
INTEL CPU 3RD GEN.pdf variadas de computacion
juancardozzo26
 
PPT
Susunan & Bagian DRAWING 153UWYHSGDGH.ppt
RezaFbriadi
 
PPTX
great itemsgreat itemsgreat itemsgreat items.pptx
saurabh13smr
 
PPT
3 01032017tyuiryhjrhyureyhjkfdhghfrugjhf
DharaniMani4
 
PPTX
Boolean Algebra-Properties and Theorems.pptx
bhavanavarri5458
 
PPTX
Basics of Memristors from zero to hero.pptx
onterusmail
 
basic_parts-of_computer-1618-754-622.pptx
patelravi16187
 
Mobile-Device-Management-MDM-Architecture.pptx
pranavnandwanshi99
 
PPT on the topic of programming language
dishasindhava
 
Operating-Systems-A-Journey ( by information
parthbhanushali307
 
Abbreviations in NC-ISM_syllabus.pdf hejsnsjs
raipureastha08
 
Intro_S4HANA_Using_Global_Bike_Slides_SD_en_v4.1.pptx
trishalasharma7
 
Basics of Memristors and fundamentals.pptx
onterusmail
 
PHISHING ATTACKS. _. _.pptx[]
kumarrana7525
 
西班牙海牙认证瓦伦西亚国际大学毕业证与成绩单文凭复刻快速办理毕业证书
sw6vvn9s
 
原版UMiami毕业证文凭迈阿密大学学费单定制学历在线制作硕士毕业证
jicaaeb0
 
Endalamaw Kebede.pdfvvbhjjnhgggftygtttfgh
SirajudinAkmel1
 
2.Important-Definihhhhhhtions18 (1).pptx
trishalasharma7
 
cocomo-220726173706-141e08f0.tyuiuuupptx
DharaniMani4
 
13. ANAESTHETICS AND ALCOHOLS.pptx fucking
sriramraja650
 
INTEL CPU 3RD GEN.pdf variadas de computacion
juancardozzo26
 
Susunan & Bagian DRAWING 153UWYHSGDGH.ppt
RezaFbriadi
 
great itemsgreat itemsgreat itemsgreat items.pptx
saurabh13smr
 
3 01032017tyuiryhjrhyureyhjkfdhghfrugjhf
DharaniMani4
 
Boolean Algebra-Properties and Theorems.pptx
bhavanavarri5458
 
Basics of Memristors from zero to hero.pptx
onterusmail
 

ELC North America 2021 Introduction to pin muxing and gpio control under linux

  • 1. Introduction to pin muxing and GPIO control under Linux Neil Armstrong - BayLibre #lfelc @superna9999
  • 3. Hardware Level Pins or Balls on SoC Package Quad Flat Package (QFP) Ball Grid Array (BGA) Quad Flat No-leads package (QFN)
  • 4. Hardware Level Wires from the die to the external package pins or balls.
  • 5. Hardware Level Main HW block of a “PAD” Can handle multiple functions for different internal HW blocks. GPIO is one of the functions Input can be muxed of more often directly connected to all HW blocks.
  • 6. Hardware Level SoC pads are generally “Tri-State”: - Hi-Z: high impedance, not driven by the SoC - 1 level: Driven to the pad voltage - 0 level: Driven to ground
  • 7. Hardware Level An Open-Drain pad can only drive to 0 (ground) Used for example for the I2C and 1-wire busses.
  • 8. Hardware Level An Open-Source pad can only drive to 1 (VCC). Not widely used but exists.
  • 9. Hardware Level Analog pad HW also optionally have: - Pull-Up: small internal resistor to VCC - Pull-Down: small internal resistor to GND - Drive strength: to select different output signal strengths - ….
  • 10. Hardware Level PAD functions can be selected: - Individually: most common => a number identifies the function - By Group: groups multiple PADs for a specific function at once Some weird setups exists: - Multi-function PADs without GPIO - GPIO-only PADs - Function can be selected on multiple PADs (generally only output)
  • 12. Software Level: Legacy Before introduction of the PINCTRL framework: ANARCHY ! Code was mainly in arch/arm/ and was from: - Very basic hard coded pin setup for SoC and Board to - Extremely complex framework with conflict checking
  • 13. Software Level: Legacy Lessons learned: - Pin Conflict Checking is almost impossible to implement - Would need an insane level of code and HW description to handle - Is in fact useless, proper code review and testing is enough - We need dynamic pin mux setup - Needed for low-power devices (Phones, Tablets, …) - Some Drivers needs pin mux / config change at runtime - No more hardcoding - maintaining SoC/Board description C code is like hell - Not scalable
  • 14. Software Level: Legacy GPIO API was also ANARCHY. API was not described in include/linux, but in each arch/arm/mach-XXXX/include/mach/gpio.h Each SoC potentially had a different: - API - Implementation - Behavior
  • 15. Software Level: Legacy - API was too basic - Implementations were all different - Multiple GPIO controllers was complex too handle - Nested GPIO controllers was also impossible to handle - I2C, SPI, USB, … GPIO extenders - Advanced API was only available on some SoC - GPIO IRQ was also very SoC specific
  • 17. Software Level: PINCTRL Framework The “pin control subsystem” submitted by Linus Walleij was merged in Linux 3.2-rc2 in Oct 13, 2011 This creates a subsystem for handling of pin control devices. These are devices that control different aspects of package pins. Currently it handles pinmuxing, i.e. assigning electronic functions to groups of pins [...]. The plan is to also handle other I/O pin control aspects [...] to remove a lot of ARM arch code as well as feature-creepy GPIO drivers which are implementing the same thing over and over again.
  • 18. Software Level: PINCTRL Framework Definition of PIN CONTROLLER: A pin controller is a piece of hardware, usually a set of registers, that can control PINs. It may be able to multiplex, bias, set load capacitance, set drive strength etc for individual pins or groups of pins.
  • 19. Software Level: PINCTRL Framework Definition of PIN: PINS are equal to pads, fingers, balls or whatever packaging input or output line you want to control and these are denoted by unsigned integers in the range 0..maxpin. This number space is local to each PIN CONTROLLER, so there may be several such number spaces in a system. This pin space may be sparse - i.e. there may be gaps in the space with numbers where no pin exists.
  • 20. Software Level: PINCTRL Framework Pin groups Many controllers need to deal with groups of pins, so the pin controller subsystem has a mechanism for enumerating groups of pins and retrieving the actual enumerated pins that are part of a certain group.
  • 21. Software Level: PINCTRL Framework PINMUX, also known as padmux, ballmux, alternate functions or mission modes is a way for chip vendors producing some kind of electrical packages to use a certain physical pin (ball, pad, finger, etc) for multiple mutually exclusive functions, depending on the application. By "application" in this context we usually mean a way of soldering or wiring the package into an electronic system, even though the framework makes it possible to also change the function at runtime.
  • 22. Software Level: PINCTRL Framework PINS A0 A1 A2 … B2 B3 ... G4 G5 G6 … FUNCTIONS i2c0: {I2C0} uart1: {UART1, UART1_RTS, UART1_CTS} ... PINGROUPS I2C0 {P8, K2} UART1 {C5, K2, P3} UART1_RTS {P8} UART1_CTS {P9} ...
  • 23. Software Level: PINCTRL Framework ● Pins can have holes, and “pretty names” like Ball ID ○ The name reflects what the vendor uses to distinguish pins ○ But each must be unique ● Pingroups can have 1 to many pins ○ Multiple pingroups can share the same pins ● Functions associates a name to a set of pingroups ○ Multiple functions can share the same pingroups ○ But only one of these conflicting functions can be set at the same time
  • 24. Software Level: PINCTRL Framework ● Pins <-> Groups <-> Function association is done in driver to match the underlying hardware ● Pinmux Framework needs : ○ Pins list const struct pinctrl_pin_desc *pins; unsigned int npins; ○ Pin control ops const struct pinctrl_ops *pctlops; ○ Pin muxing ops const struct pinmux_ops *pmxops; ○ Pin config ops const struct pinconf_ops *confops;
  • 25. Software Level: PINCTRL Framework ● const struct pinctrl_ops *pctlops; ○ int (*get_groups_count) (struct pinctrl_dev *pctldev) ■ Get the number of pin groups ○ const char *(*get_group_name) (struct pinctrl_dev *pctldev, unsigned selector); ■ Get the name (string identifier) of a pin group ○ int (*get_group_pins) (struct pinctrl_dev *pctldev, unsigned selector, const unsigned **pins, unsigned *num_pins); ■ Get the pins involved in a pin group
  • 26. Software Level: PINCTRL Framework ● struct pinmux_ops ○ int (*get_functions_count) (struct pinctrl_dev *pctldev); ■ Get the number of functions ○ const char *(*get_function_name) (struct pinctrl_dev *pctldev, unsigned selector); ■ Get the name of a function ○ int (*get_function_groups) (struct pinctrl_dev *pctldev, unsigned selector, const char * const **groups, unsigned *num_groups); ■ Get the pin groups involved in a function ○ int (*set_mux) (struct pinctrl_dev *pctldev, unsigned func_selector, unsigned group_selector); ■ Set the pins of a groups in a function
  • 27. Software Level: PINCTRL Framework ● Pinctrl subsystem was designed *before* Device Tree ● Pinmux mapping was hard-coded in arch/arm/mach- ● Now mapping is described in DT (or ACPI) ● Usage is still the same ○ p = devm_pinctrl_get(dev); ○ S = pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT); ○ pinctrl_select_state(p, s);
  • 28. Software Level: PINCTRL Framework ● DT Mapping ○ uart1_pins: uart1 { mux { groups = "UART1", "UART1_RTS", "UART1_CTS"; function = "uart1"; }; }; ● From a device node ○ uart@123456 { pinctrl-0 = <&uart1_pins>; pinctrl-names = "default"; … }; FUNCTIONS i2c0: {I2C0} uart1: {UART1, UART1_RTS, UART1_CTS} ... PINGROUPS I2C0 {P8, K2} UART1 {C5, K2, P3} UART1_RTS {P8} UART1_CTS {P9} ...
  • 29. Software Level: GPIO Framework
  • 30. Software Level: GPIO Framework The “gpio provider infrastructure” submitted by David Brownell was merged in Linux 2.6.25-rc2 in Feb 5, 2008 Provide new implementation infrastructure that platforms may choose to use when implementing the GPIO programming interface. Platforms can update their GPIO support to use this. Finally an unified GPIO API and implementation.
  • 31. Software Level: GPIO Framework The “gpio provider infrastructure” is named “gpiolib”. Each GPIO was identified as an unique number. This number was used in data platform to identify GPIOs to use by drivers. Handling of GPIO extender was possible, but this static numbering made it limited.
  • 32. Software Level: GPIO Framework The “gpio provider infrastructure” was progressively replaced by: GPIOD It replaced the “legacy” GPIO interface by maintaining the original API while introducing a new GPIO framework based around a struct gpio_desc for each GPIO for the consumers. Internally, an unique number still identifies each GPIO.
  • 33. Software Level: GPIO Framework The legacy API is still usable from: include/linux/gpio.h But is removed bits per bits in favor or: include/linux/gpio/ with consumer.h for drivers.
  • 34. Software Level: GPIO Framework Like the PINCTRL framework, the consumer and implementation API are separate. For the GPIO controller implementation: include/linux/gpio/driver.h For the GPIO consumer: include/linux/gpio/consumer.h
  • 35. Software Level: GPIO Framework include/linux/gpio/driver.h Basic GPIO controller registration: chip->gpio_chip.label = “NAME”; direction_output() will be called to set input or output chip->gpio_chip.direction_output = direction_output; get_value() will be called to get the GPIO input value (0 or 1) chip->gpio_chip.get = get_value; set_value() will be called to set the GPIO output value (0 or 1) chip->gpio_chip.set = set_value; chip->gpio_chip.base = -1; chip->gpio_chip.ngpio = NUMBER_GPIOS; parent links the GPIO controller to the device chip->gpio_chip.parent = dev; gpiochip_add_data(&chip->gpio_chip, chip);
  • 36. Software Level: GPIO Framework include/linux/gpio/consumer.h With Device-Tree, gpios are linked to device nodes with: audio-amplifier-0 { compatible = "simple-audio-amplifier"; enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>; status = "okay"; };
  • 37. Software Level: GPIO Framework audio-amplifier-0 { compatible = "simple-audio-amplifier"; enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>; status = "okay"; }; enable-gpios “enable” is the “function” name associated to the GPIO, can be enything like “reset”, “shutdown”, ….
  • 38. Software Level: GPIO Framework audio-amplifier-0 { compatible = "simple-audio-amplifier"; enable-gpios = <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>; status = "okay"; }; <&gpio_ao GPIOAO_2 GPIO_ACTIVE_HIGH>; GPIO Controller phandle GPIO Identifier/Offset => Controller/Platform Specific GPIO Flags
  • 39. Software Level: GPIO Framework => include/dt-bindings/gpio/gpio.h /* Bit 0 express polarity */ #define GPIO_ACTIVE_HIGH 0 #define GPIO_ACTIVE_LOW 1 /* Bit 1 express single-endedness */ #define GPIO_PUSH_PULL 0 #define GPIO_SINGLE_ENDED 2 /* Bit 2 express Open drain or open source */ #define GPIO_LINE_OPEN_SOURCE 0 #define GPIO_LINE_OPEN_DRAIN 4 /* Open Drain/Collector is the combination of single-ended open drain interface. * Open Source/Emitter is the combination of single-ended open source interface. */ #define GPIO_OPEN_DRAIN (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_DRAIN) #define GPIO_OPEN_SOURCE (GPIO_SINGLE_ENDED | GPIO_LINE_OPEN_SOURCE) /* Bit 3 express GPIO suspend/resume and reset persistence */ #define GPIO_PERSISTENT 0 #define GPIO_TRANSITORY 8 /* Bit 4 express pull up */ #define GPIO_PULL_UP 16 /* Bit 5 express pull down */ #define GPIO_PULL_DOWN 32
  • 40. Software Level: GPIO Framework include/linux/gpio/consumer.h Get the “enable” GPIO, and initialize it to “LOW” output: gpiod_enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW); Set value: gpiod_set_value(gpiod_enable, val);
  • 41. Software Level: GPIO Framework include/linux/gpio/consumer.h With the DT flags (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW), the GPIO core will invert the GPIO value to match the “ACTIVE” polarity. Active polarity is when the signal “acts” ● !RESET IC port will expect GPIO_ACTIVE_LOW ● EN IC port will expect GPIO_ACTIVE_HIGH
  • 42. Software Level: GPIO Framework include/linux/gpio/consumer.h For example, if GPIO_ACTIVE_LOW is passed in DT flag: ● gpiod_set_value(gpiod_enable, 0); => will set the GPIO to 1 ● gpiod_set_value(gpiod_enable, 1); => will set the GPIO to 0
  • 44. Software Level: PINCTRL & GPIO ● The PINCTRL framework is able to “link” itself to GPIO ● Often the GPIO controller is “inside”/”next” the PINCTRL HW ● Can specify “ranges” between a PIN controller and GPIO controllers
  • 45. Thanks for listening ! Scan me for the slides