162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Core pinctrl/GPIO driver for Intel GPIO controllers
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2015, Intel Corporation
662306a36Sopenharmony_ci * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
762306a36Sopenharmony_ci *          Mika Westerberg <mika.westerberg@linux.intel.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef PINCTRL_INTEL_H
1162306a36Sopenharmony_ci#define PINCTRL_INTEL_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/bits.h>
1462306a36Sopenharmony_ci#include <linux/compiler_types.h>
1562306a36Sopenharmony_ci#include <linux/gpio/driver.h>
1662306a36Sopenharmony_ci#include <linux/irq.h>
1762306a36Sopenharmony_ci#include <linux/kernel.h>
1862306a36Sopenharmony_ci#include <linux/pm.h>
1962306a36Sopenharmony_ci#include <linux/pinctrl/pinctrl.h>
2062306a36Sopenharmony_ci#include <linux/spinlock_types.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct platform_device;
2362306a36Sopenharmony_cistruct device;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/**
2662306a36Sopenharmony_ci * struct intel_pingroup - Description about group of pins
2762306a36Sopenharmony_ci * @grp: Generic data of the pin group (name and pins)
2862306a36Sopenharmony_ci * @mode: Native mode in which the group is muxed out @pins. Used if @modes is %NULL.
2962306a36Sopenharmony_ci * @modes: If not %NULL this will hold mode for each pin in @pins
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_cistruct intel_pingroup {
3262306a36Sopenharmony_ci	struct pingroup grp;
3362306a36Sopenharmony_ci	unsigned short mode;
3462306a36Sopenharmony_ci	const unsigned int *modes;
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/**
3862306a36Sopenharmony_ci * struct intel_function - Description about a function
3962306a36Sopenharmony_ci * @func: Generic data of the pin function (name and groups of pins)
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_cistruct intel_function {
4262306a36Sopenharmony_ci	struct pinfunction func;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#define INTEL_PINCTRL_MAX_GPP_SIZE	32
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/**
4862306a36Sopenharmony_ci * struct intel_padgroup - Hardware pad group information
4962306a36Sopenharmony_ci * @reg_num: GPI_IS register number
5062306a36Sopenharmony_ci * @base: Starting pin of this group
5162306a36Sopenharmony_ci * @size: Size of this group (maximum is %INTEL_PINCTRL_MAX_GPP_SIZE).
5262306a36Sopenharmony_ci * @gpio_base: Starting GPIO base of this group
5362306a36Sopenharmony_ci * @padown_num: PAD_OWN register number (assigned by the core driver)
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * If pad groups of a community are not the same size, use this structure
5662306a36Sopenharmony_ci * to specify them.
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_cistruct intel_padgroup {
5962306a36Sopenharmony_ci	unsigned int reg_num;
6062306a36Sopenharmony_ci	unsigned int base;
6162306a36Sopenharmony_ci	unsigned int size;
6262306a36Sopenharmony_ci	int gpio_base;
6362306a36Sopenharmony_ci	unsigned int padown_num;
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * enum - Special treatment for GPIO base in pad group
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * @INTEL_GPIO_BASE_ZERO:	force GPIO base to be 0
7062306a36Sopenharmony_ci * @INTEL_GPIO_BASE_NOMAP:	no GPIO mapping should be created
7162306a36Sopenharmony_ci * @INTEL_GPIO_BASE_MATCH:	matches with starting pin number
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_cienum {
7462306a36Sopenharmony_ci	INTEL_GPIO_BASE_ZERO	= -2,
7562306a36Sopenharmony_ci	INTEL_GPIO_BASE_NOMAP	= -1,
7662306a36Sopenharmony_ci	INTEL_GPIO_BASE_MATCH	= 0,
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/**
8062306a36Sopenharmony_ci * struct intel_community - Intel pin community description
8162306a36Sopenharmony_ci * @barno: MMIO BAR number where registers for this community reside
8262306a36Sopenharmony_ci * @padown_offset: Register offset of PAD_OWN register from @regs. If %0
8362306a36Sopenharmony_ci *                 then there is no support for owner.
8462306a36Sopenharmony_ci * @padcfglock_offset: Register offset of PADCFGLOCK from @regs. If %0 then
8562306a36Sopenharmony_ci *                     locking is not supported.
8662306a36Sopenharmony_ci * @hostown_offset: Register offset of HOSTSW_OWN from @regs. If %0 then it
8762306a36Sopenharmony_ci *                  is assumed that the host owns the pin (rather than
8862306a36Sopenharmony_ci *                  ACPI).
8962306a36Sopenharmony_ci * @is_offset: Register offset of GPI_IS from @regs.
9062306a36Sopenharmony_ci * @ie_offset: Register offset of GPI_IE from @regs.
9162306a36Sopenharmony_ci * @features: Additional features supported by the hardware
9262306a36Sopenharmony_ci * @pin_base: Starting pin of pins in this community
9362306a36Sopenharmony_ci * @npins: Number of pins in this community
9462306a36Sopenharmony_ci * @gpp_size: Maximum number of pads in each group, such as PADCFGLOCK,
9562306a36Sopenharmony_ci *            HOSTSW_OWN, GPI_IS, GPI_IE. Used when @gpps is %NULL.
9662306a36Sopenharmony_ci * @gpp_num_padown_regs: Number of pad registers each pad group consumes at
9762306a36Sopenharmony_ci *			 minimum. Used when @gpps is %NULL.
9862306a36Sopenharmony_ci * @gpps: Pad groups if the controller has variable size pad groups
9962306a36Sopenharmony_ci * @ngpps: Number of pad groups in this community
10062306a36Sopenharmony_ci * @pad_map: Optional non-linear mapping of the pads
10162306a36Sopenharmony_ci * @nirqs: Optional total number of IRQs this community can generate
10262306a36Sopenharmony_ci * @acpi_space_id: Optional address space ID for ACPI OpRegion handler
10362306a36Sopenharmony_ci * @regs: Community specific common registers (reserved for core driver)
10462306a36Sopenharmony_ci * @pad_regs: Community specific pad registers (reserved for core driver)
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * In older Intel GPIO host controllers, this driver supports, each pad group
10762306a36Sopenharmony_ci * is of equal size (except the last one). In that case the driver can just
10862306a36Sopenharmony_ci * fill in @gpp_size and @gpp_num_padown_regs fields and let the core driver
10962306a36Sopenharmony_ci * to handle the rest.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * In newer Intel GPIO host controllers each pad group is of variable size,
11262306a36Sopenharmony_ci * so the client driver can pass custom @gpps and @ngpps instead.
11362306a36Sopenharmony_ci */
11462306a36Sopenharmony_cistruct intel_community {
11562306a36Sopenharmony_ci	unsigned int barno;
11662306a36Sopenharmony_ci	unsigned int padown_offset;
11762306a36Sopenharmony_ci	unsigned int padcfglock_offset;
11862306a36Sopenharmony_ci	unsigned int hostown_offset;
11962306a36Sopenharmony_ci	unsigned int is_offset;
12062306a36Sopenharmony_ci	unsigned int ie_offset;
12162306a36Sopenharmony_ci	unsigned int features;
12262306a36Sopenharmony_ci	unsigned int pin_base;
12362306a36Sopenharmony_ci	size_t npins;
12462306a36Sopenharmony_ci	unsigned int gpp_size;
12562306a36Sopenharmony_ci	unsigned int gpp_num_padown_regs;
12662306a36Sopenharmony_ci	const struct intel_padgroup *gpps;
12762306a36Sopenharmony_ci	size_t ngpps;
12862306a36Sopenharmony_ci	const unsigned int *pad_map;
12962306a36Sopenharmony_ci	unsigned short nirqs;
13062306a36Sopenharmony_ci	unsigned short acpi_space_id;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	/* Reserved for the core driver */
13362306a36Sopenharmony_ci	void __iomem *regs;
13462306a36Sopenharmony_ci	void __iomem *pad_regs;
13562306a36Sopenharmony_ci};
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/* Additional features supported by the hardware */
13862306a36Sopenharmony_ci#define PINCTRL_FEATURE_DEBOUNCE	BIT(0)
13962306a36Sopenharmony_ci#define PINCTRL_FEATURE_1K_PD		BIT(1)
14062306a36Sopenharmony_ci#define PINCTRL_FEATURE_GPIO_HW_INFO	BIT(2)
14162306a36Sopenharmony_ci#define PINCTRL_FEATURE_PWM		BIT(3)
14262306a36Sopenharmony_ci#define PINCTRL_FEATURE_BLINK		BIT(4)
14362306a36Sopenharmony_ci#define PINCTRL_FEATURE_EXP		BIT(5)
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define __INTEL_COMMUNITY(b, s, e, g, n, gs, gn, soc)		\
14662306a36Sopenharmony_ci	{							\
14762306a36Sopenharmony_ci		.barno = (b),					\
14862306a36Sopenharmony_ci		.padown_offset = soc ## _PAD_OWN,		\
14962306a36Sopenharmony_ci		.padcfglock_offset = soc ## _PADCFGLOCK,	\
15062306a36Sopenharmony_ci		.hostown_offset = soc ## _HOSTSW_OWN,		\
15162306a36Sopenharmony_ci		.is_offset = soc ## _GPI_IS,			\
15262306a36Sopenharmony_ci		.ie_offset = soc ## _GPI_IE,			\
15362306a36Sopenharmony_ci		.gpp_size = (gs),				\
15462306a36Sopenharmony_ci		.gpp_num_padown_regs = (gn),			\
15562306a36Sopenharmony_ci		.pin_base = (s),				\
15662306a36Sopenharmony_ci		.npins = ((e) - (s) + 1),			\
15762306a36Sopenharmony_ci		.gpps = (g),					\
15862306a36Sopenharmony_ci		.ngpps = (n),					\
15962306a36Sopenharmony_ci	}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci#define INTEL_COMMUNITY_GPPS(b, s, e, g, soc)			\
16262306a36Sopenharmony_ci	__INTEL_COMMUNITY(b, s, e, g, ARRAY_SIZE(g), 0, 0, soc)
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci#define INTEL_COMMUNITY_SIZE(b, s, e, gs, gn, soc)		\
16562306a36Sopenharmony_ci	__INTEL_COMMUNITY(b, s, e, NULL, 0, gs, gn, soc)
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/**
16862306a36Sopenharmony_ci * PIN_GROUP - Declare a pin group
16962306a36Sopenharmony_ci * @n: Name of the group
17062306a36Sopenharmony_ci * @p: An array of pins this group consists
17162306a36Sopenharmony_ci * @m: Mode which the pins are put when this group is active. Can be either
17262306a36Sopenharmony_ci *     a single integer or an array of integers in which case mode is per
17362306a36Sopenharmony_ci *     pin.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_ci#define PIN_GROUP(n, p, m)								\
17662306a36Sopenharmony_ci	{										\
17762306a36Sopenharmony_ci		.grp = PINCTRL_PINGROUP((n), (p), ARRAY_SIZE((p))),			\
17862306a36Sopenharmony_ci		.mode = __builtin_choose_expr(__builtin_constant_p((m)), (m), 0),	\
17962306a36Sopenharmony_ci		.modes = __builtin_choose_expr(__builtin_constant_p((m)), NULL, (m)),	\
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#define FUNCTION(n, g)							\
18362306a36Sopenharmony_ci	{								\
18462306a36Sopenharmony_ci		.func = PINCTRL_PINFUNCTION((n), (g), ARRAY_SIZE(g)),	\
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/**
18862306a36Sopenharmony_ci * struct intel_pinctrl_soc_data - Intel pin controller per-SoC configuration
18962306a36Sopenharmony_ci * @uid: ACPI _UID for the probe driver use if needed
19062306a36Sopenharmony_ci * @pins: Array if pins this pinctrl controls
19162306a36Sopenharmony_ci * @npins: Number of pins in the array
19262306a36Sopenharmony_ci * @groups: Array of pin groups
19362306a36Sopenharmony_ci * @ngroups: Number of groups in the array
19462306a36Sopenharmony_ci * @functions: Array of functions
19562306a36Sopenharmony_ci * @nfunctions: Number of functions in the array
19662306a36Sopenharmony_ci * @communities: Array of communities this pinctrl handles
19762306a36Sopenharmony_ci * @ncommunities: Number of communities in the array
19862306a36Sopenharmony_ci *
19962306a36Sopenharmony_ci * The @communities is used as a template by the core driver. It will make
20062306a36Sopenharmony_ci * copy of all communities and fill in rest of the information.
20162306a36Sopenharmony_ci */
20262306a36Sopenharmony_cistruct intel_pinctrl_soc_data {
20362306a36Sopenharmony_ci	const char *uid;
20462306a36Sopenharmony_ci	const struct pinctrl_pin_desc *pins;
20562306a36Sopenharmony_ci	size_t npins;
20662306a36Sopenharmony_ci	const struct intel_pingroup *groups;
20762306a36Sopenharmony_ci	size_t ngroups;
20862306a36Sopenharmony_ci	const struct intel_function *functions;
20962306a36Sopenharmony_ci	size_t nfunctions;
21062306a36Sopenharmony_ci	const struct intel_community *communities;
21162306a36Sopenharmony_ci	size_t ncommunities;
21262306a36Sopenharmony_ci};
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciconst struct intel_pinctrl_soc_data *intel_pinctrl_get_soc_data(struct platform_device *pdev);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_cistruct intel_pad_context;
21762306a36Sopenharmony_cistruct intel_community_context;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/**
22062306a36Sopenharmony_ci * struct intel_pinctrl_context - context to be saved during suspend-resume
22162306a36Sopenharmony_ci * @pads: Opaque context per pad (driver dependent)
22262306a36Sopenharmony_ci * @communities: Opaque context per community (driver dependent)
22362306a36Sopenharmony_ci */
22462306a36Sopenharmony_cistruct intel_pinctrl_context {
22562306a36Sopenharmony_ci	struct intel_pad_context *pads;
22662306a36Sopenharmony_ci	struct intel_community_context *communities;
22762306a36Sopenharmony_ci};
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci/**
23062306a36Sopenharmony_ci * struct intel_pinctrl - Intel pinctrl private structure
23162306a36Sopenharmony_ci * @dev: Pointer to the device structure
23262306a36Sopenharmony_ci * @lock: Lock to serialize register access
23362306a36Sopenharmony_ci * @pctldesc: Pin controller description
23462306a36Sopenharmony_ci * @pctldev: Pointer to the pin controller device
23562306a36Sopenharmony_ci * @chip: GPIO chip in this pin controller
23662306a36Sopenharmony_ci * @soc: SoC/PCH specific pin configuration data
23762306a36Sopenharmony_ci * @communities: All communities in this pin controller
23862306a36Sopenharmony_ci * @ncommunities: Number of communities in this pin controller
23962306a36Sopenharmony_ci * @context: Configuration saved over system sleep
24062306a36Sopenharmony_ci * @irq: pinctrl/GPIO chip irq number
24162306a36Sopenharmony_ci */
24262306a36Sopenharmony_cistruct intel_pinctrl {
24362306a36Sopenharmony_ci	struct device *dev;
24462306a36Sopenharmony_ci	raw_spinlock_t lock;
24562306a36Sopenharmony_ci	struct pinctrl_desc pctldesc;
24662306a36Sopenharmony_ci	struct pinctrl_dev *pctldev;
24762306a36Sopenharmony_ci	struct gpio_chip chip;
24862306a36Sopenharmony_ci	const struct intel_pinctrl_soc_data *soc;
24962306a36Sopenharmony_ci	struct intel_community *communities;
25062306a36Sopenharmony_ci	size_t ncommunities;
25162306a36Sopenharmony_ci	struct intel_pinctrl_context context;
25262306a36Sopenharmony_ci	int irq;
25362306a36Sopenharmony_ci};
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ciint intel_pinctrl_probe_by_hid(struct platform_device *pdev);
25662306a36Sopenharmony_ciint intel_pinctrl_probe_by_uid(struct platform_device *pdev);
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
25962306a36Sopenharmony_ciint intel_pinctrl_suspend_noirq(struct device *dev);
26062306a36Sopenharmony_ciint intel_pinctrl_resume_noirq(struct device *dev);
26162306a36Sopenharmony_ci#endif
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci#define INTEL_PINCTRL_PM_OPS(_name)					\
26462306a36Sopenharmony_ciconst struct dev_pm_ops _name = {					\
26562306a36Sopenharmony_ci	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pinctrl_suspend_noirq,	\
26662306a36Sopenharmony_ci				      intel_pinctrl_resume_noirq)	\
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistruct intel_community *intel_get_community(struct intel_pinctrl *pctrl, unsigned int pin);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ciint intel_get_groups_count(struct pinctrl_dev *pctldev);
27262306a36Sopenharmony_ciconst char *intel_get_group_name(struct pinctrl_dev *pctldev, unsigned int group);
27362306a36Sopenharmony_ciint intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
27462306a36Sopenharmony_ci			 const unsigned int **pins, unsigned int *npins);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciint intel_get_functions_count(struct pinctrl_dev *pctldev);
27762306a36Sopenharmony_ciconst char *intel_get_function_name(struct pinctrl_dev *pctldev, unsigned int function);
27862306a36Sopenharmony_ciint intel_get_function_groups(struct pinctrl_dev *pctldev, unsigned int function,
27962306a36Sopenharmony_ci			      const char * const **groups, unsigned int * const ngroups);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci#endif /* PINCTRL_INTEL_H */
282