xref: /linux/drivers/pinctrl/mvebu/pinctrl-mvebu.h (revision 44aa9d0604f578cdd839d2f5295a9d920fb54999)
106763c74SThomas Petazzoni /*
206763c74SThomas Petazzoni  * Marvell MVEBU pinctrl driver
306763c74SThomas Petazzoni  *
406763c74SThomas Petazzoni  * Authors: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
506763c74SThomas Petazzoni  *          Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
606763c74SThomas Petazzoni  *
706763c74SThomas Petazzoni  * This program is free software; you can redistribute it and/or modify
806763c74SThomas Petazzoni  * it under the terms of the GNU General Public License as published by
906763c74SThomas Petazzoni  * the Free Software Foundation; either version 2 of the License, or
1006763c74SThomas Petazzoni  * (at your option) any later version.
1106763c74SThomas Petazzoni  */
1206763c74SThomas Petazzoni 
1306763c74SThomas Petazzoni #ifndef __PINCTRL_MVEBU_H__
1406763c74SThomas Petazzoni #define __PINCTRL_MVEBU_H__
1506763c74SThomas Petazzoni 
1606763c74SThomas Petazzoni /**
1720955c5fSRussell King  * struct mvebu_mpp_ctrl_data - private data for the mpp ctrl operations
1820955c5fSRussell King  * @base: base address of pinctrl hardware
1920955c5fSRussell King  */
2020955c5fSRussell King struct mvebu_mpp_ctrl_data {
2120955c5fSRussell King 	void __iomem *base;
2220955c5fSRussell King };
2320955c5fSRussell King 
2420955c5fSRussell King /**
2506763c74SThomas Petazzoni  * struct mvebu_mpp_ctrl - describe a mpp control
2606763c74SThomas Petazzoni  * @name: name of the control group
2706763c74SThomas Petazzoni  * @pid: first pin id handled by this control
2806763c74SThomas Petazzoni  * @npins: number of pins controlled by this control
2906763c74SThomas Petazzoni  * @mpp_get: (optional) special function to get mpp setting
3006763c74SThomas Petazzoni  * @mpp_set: (optional) special function to set mpp setting
3106763c74SThomas Petazzoni  * @mpp_gpio_req: (optional) special function to request gpio
3206763c74SThomas Petazzoni  * @mpp_gpio_dir: (optional) special function to set gpio direction
3306763c74SThomas Petazzoni  *
3406763c74SThomas Petazzoni  * A mpp_ctrl describes a muxable unit, e.g. pin, group of pins, or
3506763c74SThomas Petazzoni  * internal function, inside the SoC. Each muxable unit can be switched
3606763c74SThomas Petazzoni  * between two or more different settings, e.g. assign mpp pin 13 to
3706763c74SThomas Petazzoni  * uart1 or sata.
3806763c74SThomas Petazzoni  *
39cffa7a6bSThomas Petazzoni  * The mpp_get/_set functions are mandatory and are used to get/set a
40cffa7a6bSThomas Petazzoni  * specific mode. The optional mpp_gpio_req/_dir functions can be used
41cffa7a6bSThomas Petazzoni  * to allow pin settings with varying gpio pins.
4206763c74SThomas Petazzoni  */
4306763c74SThomas Petazzoni struct mvebu_mpp_ctrl {
4406763c74SThomas Petazzoni 	const char *name;
4506763c74SThomas Petazzoni 	u8 pid;
4606763c74SThomas Petazzoni 	u8 npins;
4706763c74SThomas Petazzoni 	unsigned *pins;
4820955c5fSRussell King 	int (*mpp_get)(struct mvebu_mpp_ctrl_data *data, unsigned pid,
4920955c5fSRussell King 		       unsigned long *config);
5020955c5fSRussell King 	int (*mpp_set)(struct mvebu_mpp_ctrl_data *data, unsigned pid,
5120955c5fSRussell King 		       unsigned long config);
5220955c5fSRussell King 	int (*mpp_gpio_req)(struct mvebu_mpp_ctrl_data *data, unsigned pid);
5320955c5fSRussell King 	int (*mpp_gpio_dir)(struct mvebu_mpp_ctrl_data *data, unsigned pid,
5420955c5fSRussell King 			    bool input);
5506763c74SThomas Petazzoni };
5606763c74SThomas Petazzoni 
5706763c74SThomas Petazzoni /**
5806763c74SThomas Petazzoni  * struct mvebu_mpp_ctrl_setting - describe a mpp ctrl setting
5906763c74SThomas Petazzoni  * @val: ctrl setting value
6006763c74SThomas Petazzoni  * @name: ctrl setting name, e.g. uart2, spi0 - unique per mpp_mode
6106763c74SThomas Petazzoni  * @subname: (optional) additional ctrl setting name, e.g. rts, cts
6206763c74SThomas Petazzoni  * @variant: (optional) variant identifier mask
6306763c74SThomas Petazzoni  * @flags: (private) flags to store gpi/gpo/gpio capabilities
6406763c74SThomas Petazzoni  *
6506763c74SThomas Petazzoni  * A ctrl_setting describes a specific internal mux function that a mpp pin
6606763c74SThomas Petazzoni  * can be switched to. The value (val) will be written in the corresponding
6706763c74SThomas Petazzoni  * register for common mpp pin configuration registers on MVEBU. SoC specific
6806763c74SThomas Petazzoni  * mpp_get/_set function may use val to distinguish between different settings.
6906763c74SThomas Petazzoni  *
7006763c74SThomas Petazzoni  * The name will be used to switch to this setting in DT description, e.g.
7106763c74SThomas Petazzoni  * marvell,function = "uart2". subname is only for debugging purposes.
7206763c74SThomas Petazzoni  *
7306763c74SThomas Petazzoni  * If name is one of "gpi", "gpo", "gpio" gpio capabilities are
7406763c74SThomas Petazzoni  * parsed during initialization and stored in flags.
7506763c74SThomas Petazzoni  *
7606763c74SThomas Petazzoni  * The variant can be used to combine different revisions of one SoC to a
7706763c74SThomas Petazzoni  * common pinctrl driver. It is matched (AND) with variant of soc_info to
7806763c74SThomas Petazzoni  * determine if a setting is available on the current SoC revision.
7906763c74SThomas Petazzoni  */
8006763c74SThomas Petazzoni struct mvebu_mpp_ctrl_setting {
8106763c74SThomas Petazzoni 	u8 val;
8206763c74SThomas Petazzoni 	const char *name;
8306763c74SThomas Petazzoni 	const char *subname;
8406763c74SThomas Petazzoni 	u8 variant;
8506763c74SThomas Petazzoni 	u8 flags;
8606763c74SThomas Petazzoni #define  MVEBU_SETTING_GPO	(1 << 0)
8706763c74SThomas Petazzoni #define  MVEBU_SETTING_GPI	(1 << 1)
8806763c74SThomas Petazzoni };
8906763c74SThomas Petazzoni 
9006763c74SThomas Petazzoni /**
9106763c74SThomas Petazzoni  * struct mvebu_mpp_mode - link ctrl and settings
9206763c74SThomas Petazzoni  * @pid: first pin id handled by this mode
9306763c74SThomas Petazzoni  * @settings: list of settings available for this mode
9406763c74SThomas Petazzoni  *
9506763c74SThomas Petazzoni  * A mode connects all available settings with the corresponding mpp_ctrl
9606763c74SThomas Petazzoni  * given by pid.
9706763c74SThomas Petazzoni  */
9806763c74SThomas Petazzoni struct mvebu_mpp_mode {
9906763c74SThomas Petazzoni 	u8 pid;
10006763c74SThomas Petazzoni 	struct mvebu_mpp_ctrl_setting *settings;
10106763c74SThomas Petazzoni };
10206763c74SThomas Petazzoni 
10306763c74SThomas Petazzoni /**
10406763c74SThomas Petazzoni  * struct mvebu_pinctrl_soc_info - SoC specific info passed to pinctrl-mvebu
10506763c74SThomas Petazzoni  * @variant: variant mask of soc_info
10606763c74SThomas Petazzoni  * @controls: list of available mvebu_mpp_ctrls
10720955c5fSRussell King  * @control_data: optional array, one entry for each control
10806763c74SThomas Petazzoni  * @ncontrols: number of available mvebu_mpp_ctrls
10906763c74SThomas Petazzoni  * @modes: list of available mvebu_mpp_modes
11006763c74SThomas Petazzoni  * @nmodes: number of available mvebu_mpp_modes
11106763c74SThomas Petazzoni  * @gpioranges: list of pinctrl_gpio_ranges
11206763c74SThomas Petazzoni  * @ngpioranges: number of available pinctrl_gpio_ranges
11306763c74SThomas Petazzoni  *
11406763c74SThomas Petazzoni  * This struct describes all pinctrl related information for a specific SoC.
11506763c74SThomas Petazzoni  * If variant is unequal 0 it will be matched (AND) with variant of each
11606763c74SThomas Petazzoni  * setting and allows to distinguish between different revisions of one SoC.
11706763c74SThomas Petazzoni  */
11806763c74SThomas Petazzoni struct mvebu_pinctrl_soc_info {
11906763c74SThomas Petazzoni 	u8 variant;
12030be3fb9SRussell King 	const struct mvebu_mpp_ctrl *controls;
12120955c5fSRussell King 	struct mvebu_mpp_ctrl_data *control_data;
12206763c74SThomas Petazzoni 	int ncontrols;
12306763c74SThomas Petazzoni 	struct mvebu_mpp_mode *modes;
12406763c74SThomas Petazzoni 	int nmodes;
12506763c74SThomas Petazzoni 	struct pinctrl_gpio_range *gpioranges;
12606763c74SThomas Petazzoni 	int ngpioranges;
12706763c74SThomas Petazzoni };
12806763c74SThomas Petazzoni 
12906763c74SThomas Petazzoni #define MPP_FUNC_CTRL(_idl, _idh, _name, _func)			\
13006763c74SThomas Petazzoni 	{							\
13106763c74SThomas Petazzoni 		.name = _name,					\
13206763c74SThomas Petazzoni 		.pid = _idl,					\
13306763c74SThomas Petazzoni 		.npins = _idh - _idl + 1,			\
13406763c74SThomas Petazzoni 		.pins = (unsigned[_idh - _idl + 1]) { },	\
13506763c74SThomas Petazzoni 		.mpp_get = _func ## _get,			\
13606763c74SThomas Petazzoni 		.mpp_set = _func ## _set,			\
13706763c74SThomas Petazzoni 		.mpp_gpio_req = NULL,				\
13806763c74SThomas Petazzoni 		.mpp_gpio_dir = NULL,				\
13906763c74SThomas Petazzoni 	}
14006763c74SThomas Petazzoni 
14106763c74SThomas Petazzoni #define MPP_FUNC_GPIO_CTRL(_idl, _idh, _name, _func)		\
14206763c74SThomas Petazzoni 	{							\
14306763c74SThomas Petazzoni 		.name = _name,					\
14406763c74SThomas Petazzoni 		.pid = _idl,					\
14506763c74SThomas Petazzoni 		.npins = _idh - _idl + 1,			\
14606763c74SThomas Petazzoni 		.pins = (unsigned[_idh - _idl + 1]) { },	\
14706763c74SThomas Petazzoni 		.mpp_get = _func ## _get,			\
14806763c74SThomas Petazzoni 		.mpp_set = _func ## _set,			\
14906763c74SThomas Petazzoni 		.mpp_gpio_req = _func ## _gpio_req,		\
15006763c74SThomas Petazzoni 		.mpp_gpio_dir = _func ## _gpio_dir,		\
15106763c74SThomas Petazzoni 	}
15206763c74SThomas Petazzoni 
15306763c74SThomas Petazzoni #define _MPP_VAR_FUNCTION(_val, _name, _subname, _mask)		\
15406763c74SThomas Petazzoni 	{							\
15506763c74SThomas Petazzoni 		.val = _val,					\
15606763c74SThomas Petazzoni 		.name = _name,					\
15706763c74SThomas Petazzoni 		.subname = _subname,				\
15806763c74SThomas Petazzoni 		.variant = _mask,				\
15906763c74SThomas Petazzoni 		.flags = 0,					\
16006763c74SThomas Petazzoni 	}
16106763c74SThomas Petazzoni 
16206763c74SThomas Petazzoni #if defined(CONFIG_DEBUG_FS)
16306763c74SThomas Petazzoni #define MPP_VAR_FUNCTION(_val, _name, _subname, _mask)		\
16406763c74SThomas Petazzoni 	_MPP_VAR_FUNCTION(_val, _name, _subname, _mask)
16506763c74SThomas Petazzoni #else
16606763c74SThomas Petazzoni #define MPP_VAR_FUNCTION(_val, _name, _subname, _mask)		\
16706763c74SThomas Petazzoni 	_MPP_VAR_FUNCTION(_val, _name, NULL, _mask)
16806763c74SThomas Petazzoni #endif
16906763c74SThomas Petazzoni 
17006763c74SThomas Petazzoni #define MPP_FUNCTION(_val, _name, _subname)			\
17106763c74SThomas Petazzoni 	MPP_VAR_FUNCTION(_val, _name, _subname, (u8)-1)
17206763c74SThomas Petazzoni 
17306763c74SThomas Petazzoni #define MPP_MODE(_id, ...)					\
17406763c74SThomas Petazzoni 	{							\
17506763c74SThomas Petazzoni 		.pid = _id,					\
17606763c74SThomas Petazzoni 		.settings = (struct mvebu_mpp_ctrl_setting[]){	\
17706763c74SThomas Petazzoni 			__VA_ARGS__, { } },			\
17806763c74SThomas Petazzoni 	}
17906763c74SThomas Petazzoni 
18006763c74SThomas Petazzoni #define MPP_GPIO_RANGE(_id, _pinbase, _gpiobase, _npins)	\
18106763c74SThomas Petazzoni 	{							\
18206763c74SThomas Petazzoni 		.name = "mvebu-gpio",				\
18306763c74SThomas Petazzoni 		.id = _id,					\
18406763c74SThomas Petazzoni 		.pin_base = _pinbase,				\
18506763c74SThomas Petazzoni 		.base = _gpiobase,				\
18606763c74SThomas Petazzoni 		.npins = _npins,				\
18706763c74SThomas Petazzoni 	}
18806763c74SThomas Petazzoni 
189f5b85e42SSebastian Hesselbarth #define MVEBU_MPPS_PER_REG	8
190f5b85e42SSebastian Hesselbarth #define MVEBU_MPP_BITS		4
191f5b85e42SSebastian Hesselbarth #define MVEBU_MPP_MASK		0xf
192f5b85e42SSebastian Hesselbarth 
193f5b85e42SSebastian Hesselbarth static inline int default_mpp_ctrl_get(void __iomem *base, unsigned int pid,
194f5b85e42SSebastian Hesselbarth 				       unsigned long *config)
195f5b85e42SSebastian Hesselbarth {
196f5b85e42SSebastian Hesselbarth 	unsigned off = (pid / MVEBU_MPPS_PER_REG) * MVEBU_MPP_BITS;
197f5b85e42SSebastian Hesselbarth 	unsigned shift = (pid % MVEBU_MPPS_PER_REG) * MVEBU_MPP_BITS;
198f5b85e42SSebastian Hesselbarth 
199f5b85e42SSebastian Hesselbarth 	*config = (readl(base + off) >> shift) & MVEBU_MPP_MASK;
200f5b85e42SSebastian Hesselbarth 
201f5b85e42SSebastian Hesselbarth 	return 0;
202f5b85e42SSebastian Hesselbarth }
203f5b85e42SSebastian Hesselbarth 
204f5b85e42SSebastian Hesselbarth static inline int default_mpp_ctrl_set(void __iomem *base, unsigned int pid,
205f5b85e42SSebastian Hesselbarth 				       unsigned long config)
206f5b85e42SSebastian Hesselbarth {
207f5b85e42SSebastian Hesselbarth 	unsigned off = (pid / MVEBU_MPPS_PER_REG) * MVEBU_MPP_BITS;
208f5b85e42SSebastian Hesselbarth 	unsigned shift = (pid % MVEBU_MPPS_PER_REG) * MVEBU_MPP_BITS;
209f5b85e42SSebastian Hesselbarth 	unsigned long reg;
210f5b85e42SSebastian Hesselbarth 
211f5b85e42SSebastian Hesselbarth 	reg = readl(base + off) & ~(MVEBU_MPP_MASK << shift);
212f5b85e42SSebastian Hesselbarth 	writel(reg | (config << shift), base + off);
213f5b85e42SSebastian Hesselbarth 
214f5b85e42SSebastian Hesselbarth 	return 0;
215f5b85e42SSebastian Hesselbarth }
216f5b85e42SSebastian Hesselbarth 
217*44aa9d06SRussell King int mvebu_mmio_mpp_ctrl_get(struct mvebu_mpp_ctrl_data *data, unsigned pid,
218*44aa9d06SRussell King 			       unsigned long *config);
219*44aa9d06SRussell King int mvebu_mmio_mpp_ctrl_set(struct mvebu_mpp_ctrl_data *data, unsigned pid,
220*44aa9d06SRussell King 			       unsigned long config);
221*44aa9d06SRussell King 
22206763c74SThomas Petazzoni int mvebu_pinctrl_probe(struct platform_device *pdev);
223*44aa9d06SRussell King int mvebu_pinctrl_simple_mmio_probe(struct platform_device *pdev);
22406763c74SThomas Petazzoni 
22506763c74SThomas Petazzoni #endif
226