1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020 Marvell International Ltd.
5 * Defines some GPIO information used in multiple places
8 #ifndef __CVMX_HELPER_GPIO_H__
9 #define __CVMX_HELPER_GPIO_H__
11 #define CVMX_GPIO_NAME_LEN 32 /** Length of name */
14 CVMX_GPIO_PIN_OCTEON, /** GPIO pin is directly connected to OCTEON */
15 CVMX_GPIO_PIN_PCA953X, /** GPIO pin is NXP PCA953X compat chip */
16 CVMX_GPIO_PIN_PCA957X,
17 CVMX_GPIO_PIN_PCF857X, /** GPIO pin is NXP PCF857X compat chip */
18 CVMX_GPIO_PIN_PCA9698, /** GPIO pin is NXP PCA9698 compat chip */
19 CVMX_GPIO_PIN_CS4343, /** Inphi/Cortina CS4343 GPIO pins */
20 CVMX_GPIO_PIN_OTHER, /** GPIO pin is something else */
23 enum cvmx_gpio_operation {
24 CVMX_GPIO_OP_CONFIG, /** Initial configuration of the GPIO pin */
25 CVMX_GPIO_OP_SET, /** Set pin */
26 CVMX_GPIO_OP_CLEAR, /** Clear pin */
27 CVMX_GPIO_OP_READ, /** Read pin */
28 CVMX_GPIO_OP_TOGGLE, /** Toggle pin */
29 CVMX_GPIO_OP_BLINK_START, /** Put in blink mode (if supported) */
30 CVMX_GPIO_OP_BLINK_STOP, /** Takes the pin out of blink mode */
31 CVMX_GPIO_OP_SET_LINK, /** Put in link monitoring mode */
32 CVMX_GPIO_OP_SET_ACT, /** Put in RX activity mode */
36 * Inphi CS4343 output source select values for the GPIO_GPIOX output_src_sel.
38 enum cvmx_inphi_cs4343_gpio_gpio_output_src_sel {
39 GPIO_SEL_DRIVE = 0, /** Value of GPIOX_DRIVE */
40 GPIO_SEL_DELAY = 1, /** Drive delayed */
41 GPIO_SEL_TOGGLE = 2, /** Used for blinking */
42 GPIO_SEL_EXT = 3, /** External function */
43 GPIO_SEL_EXT_DELAY = 4, /** External function delayed */
46 /** Inphi GPIO_GPIOX configuration register */
47 union cvmx_inphi_cs4343_gpio_cfg_reg {
51 /** Data source for the GPIO output */
52 u16 output_src_sel : 3;
53 /** 1 = GPIO output is inverted before being output */
54 u16 invert_output : 1;
55 /** 1 = GPIO input is inverted before being processed */
57 /** 0 = 2.5v/1.8v signalling, 1 = 1.2v signalling */
60 * 0 = output selected by outen bit
61 * 1 = output controlled by selected GPIO output source
64 /** 0 = GPIO is input only, 1 = GPIO output driver enabled */
67 u16 pullup_1k; /** 1 = enable 1K pad pullup */
68 u16 pullup_10k; /** 1 = enable 10K pad pullup */
72 #define CVMX_INPHI_CS4343_GPIO_CFG_OFFSET 0x0
75 * This selects which port the GPIO gets its signals from when configured
78 enum cvmx_inphi_cs4343_gpio_output_cfg_port {
79 PORT_0_HOST_RX = 0, /** Port pair 0 host RX */
80 PORT_0_LINE_RX = 1, /** Port pair 0 line RX */
81 PORT_1_HOST_RX = 2, /** Port pair 1 host RX */
82 PORT_1_LINE_RX = 3, /** Port pair 1 line RX */
83 PORT_3_HOST_RX = 4, /** Port pair 3 host RX */
84 PORT_3_LINE_RX = 5, /** Port pair 3 line RX */
85 PORT_2_HOST_RX = 6, /** Port pair 2 host RX */
86 PORT_2_LINE_RX = 7, /** Port pair 2 line RX */
87 COMMON = 8, /** Common */
90 enum cvmx_inphi_cs4343_gpio_output_cfg_function {
91 RX_LOS = 0, /** Port - 1 = Receive LOS (from DSP) */
92 RX_LOL = 1, /** Port - 1 = Receive LOL (inverted from MSEQ) */
93 EDC_CONVERGED = 2, /** Port - 1 = EDC converged (from DSP) */
94 /** Port - 1 = PRBS checker in sync (inverted from SDS) */
96 COMMON_LOGIC_0 = 0, /** Common - Logic 0 */
97 COMMON_GPIO1_INPUT = 1, /** Common - GPIO 1 input */
98 COMMON_GPIO2_INPUT = 2, /** Common - GPIO 2 input */
99 COMMON_GPIO3_INPUT = 3, /** Common - GPIO 3 input */
100 COMMON_GPIO4_INPUT = 4, /** Common - GPIO 4 input */
101 COMMON_INTERR_INPUT = 5, /** Common - INTERR input */
102 /** Common - Interrupt output from GLOBAL_INT register */
103 COMMON_GLOBAL_INT = 6,
104 /** Common - Interrupt output from GPIO_INT register */
106 /** Common - Temp/voltage monitor interrupt */
107 COMMON_MONITOR_INT = 8,
108 /** Common - Selected clock output of global clock monitor */
109 COMMON_GBL_CLKMON_CLK = 9,
112 union cvmx_inphi_cs4343_gpio_output_cfg {
116 u16 port : 4; /** port */
117 u16 function : 4; /** function */
121 #define CVMX_INPHI_CS4343_GPIO_OUTPUT_CFG_OFFSET 0x1
123 union cvmx_inphi_cs4343_gpio_drive {
127 u16 value : 1; /** output value */
131 #define CVMX_INPHI_CS4343_GPIO_DRIVE_OFFSET 0x2
133 union cvmx_inphi_cs4343_gpio_value {
137 u16 value : 1; /** input value (read-only) */
141 #define CVMX_INPHI_CS4343_GPIO_VALUE_OFFSET 0x3
143 union cvmx_inphi_cs4343_gpio_toggle {
146 /** Toggle rate in ms, multiply by 2 to get period in ms */
151 #define CVMX_INPHI_CS4343_GPIO_TOGGLE_OFFSET 0x4
153 union cvmx_inphi_cs4343_gpio_delay {
156 /** On delay for GPIO output in ms when enabled */
161 #define CVMX_INPHI_CS4343_GPIO_DELAY_OFFSET 0x5
164 * GPIO flags associated with a GPIO pin (can be combined)
166 enum cvmx_gpio_flags {
167 CVMX_GPIO_ACTIVE_HIGH = 0, /** Active high (default) */
168 CVMX_GPIO_ACTIVE_LOW = 1, /** Active low (inverted) */
169 CVMX_GPIO_OPEN_COLLECTOR = 2, /** Output is open-collector */
172 /** Default timer number to use for outputting a frequency [0..3] */
173 #define CVMX_GPIO_DEFAULT_TIMER 3
175 /** Configuration data for native Octeon GPIO pins */
176 struct cvmx_octeon_gpio_data {
177 int cpu_node; /** CPU node for GPIO pin */
178 int timer; /** Timer number used when in toggle mode, 0-3 */
181 struct cvmx_pcf857x_gpio_data {
182 unsigned int latch_out;
185 #define CVMX_INPHI_CS4343_EFUSE_PDF_SKU_REG 0x19f
186 #define CVMX_INPHI_CS4343_SKU_CS4223 0x10
187 #define CVMX_INPHI_CS4343_SKU_CS4224 0x11
188 #define CVMX_INPHI_CS4343_SKU_CS4343 0x12
189 #define CVMX_INPHI_CS4343_SKU_CS4221 0x13
190 #define CVMX_INPHI_CS4343_SKU_CS4227 0x14
191 #define CVMX_INPHI_CS4343_SKU_CS4341 0x16
193 struct cvmx_cs4343_gpio_data {
194 int reg_offset; /** Base register address for GPIO */
195 enum cvmx_gpio_operation last_op;
196 u8 link_port; /** Link port number for link status */
197 u16 sku; /** Value from CS4224_EFUSE_PDF_SKU register */
201 bool is_cs4343; /** True if dual package */
202 struct phy_device *phydev;
205 struct cvmx_fdt_gpio_info;
207 /** Function called for GPIO operations */
208 typedef int (*cvmx_fdt_gpio_op_func_t)(struct cvmx_fdt_gpio_info *, enum cvmx_gpio_operation);
213 struct cvmx_fdt_gpio_info {
214 struct cvmx_fdt_gpio_info *next; /** For list of GPIOs */
215 char name[CVMX_GPIO_NAME_LEN]; /** Name of GPIO */
216 int pin; /** GPIO pin number */
217 enum cvmx_gpio_type gpio_type; /** Type of GPIO controller */
218 int of_offset; /** Offset in device tree */
220 struct cvmx_fdt_i2c_bus_info *i2c_bus; /** I2C bus descriptor */
221 int i2c_addr; /** Address on i2c bus */
222 enum cvmx_gpio_flags flags; /** Flags associated with pin */
223 int num_pins; /** Total number of pins */
224 unsigned int latch_out; /** Latched output for 857x */
225 /** Rate in ms between toggle states */
227 /** Pointer to user data for user-defined functions */
229 /** Function to set, clear, toggle, etc. */
230 cvmx_fdt_gpio_op_func_t op_func;
231 /* Two values are used to detect the initial case where nothing has
232 * been configured. Initially, all of the following will be false
233 * which will force the initial state to be properly set.
235 /** True if the GPIO pin is currently set, useful for toggle */
237 /** Set if configured to invert */
239 /** Set if input is to be inverted */
241 /** Set if direction is configured as output */
243 /** Set if direction is configured as input */
245 /** Pin is set to toggle periodically */
247 /** True if LED is used to indicate link status */
249 /** True if LED is used to indicate rx activity */
251 /** True if LED is used to indicate tx activity */
253 /** True if LED is used to indicate networking errors */
255 /** True if LED can automatically show link */
259 /** LED datastructure */
260 struct cvmx_fdt_gpio_led {
261 struct cvmx_fdt_gpio_led *next, *prev; /** List of LEDs */
262 char name[CVMX_GPIO_NAME_LEN]; /** Name */
263 struct cvmx_fdt_gpio_info *gpio; /** GPIO for LED */
264 int of_offset; /** Device tree node */
265 /** True if active low, note that GPIO contains this info */
270 * Returns the operation function for the GPIO phandle
272 * @param[in] fdt_addr Pointer to FDT
273 * @param phandle phandle of GPIO entry
275 * @return Pointer to op function or NULL if not found.
277 cvmx_fdt_gpio_op_func_t cvmx_fdt_gpio_get_op_func(const void *fdt_addr, int phandle);
280 * Given a phandle to a GPIO device return the type of GPIO device it is.
282 * @param[in] fdt_addr Address of flat device tree
283 * @param phandle phandle to GPIO
284 * @param[out] size Number of pins (optional, may be NULL)
286 * @return Type of GPIO device or PIN_ERROR if error
288 enum cvmx_gpio_type cvmx_fdt_get_gpio_type(const void *fdt_addr, int phandle, int *size);
291 * Return a GPIO handle given a GPIO phandle of the form <&gpio pin flags>
293 * @param[in] fdt_addr Address of flat device tree
294 * @param of_offset node offset of GPIO device
295 * @param prop_name name of property
297 * @return pointer to GPIO handle or NULL if error
299 struct cvmx_fdt_gpio_info *cvmx_fdt_gpio_get_info(const void *fdt_addr, int of_offset,
300 const char *prop_name);
303 * Return a GPIO handle given a GPIO phandle of the form <&gpio pin flags>
305 * @param[in] fdt_addr Address of flat device tree
306 * @param of_offset node offset for property
307 * @param prop_name name of property
309 * @return pointer to GPIO handle or NULL if error
311 struct cvmx_fdt_gpio_info *cvmx_fdt_gpio_get_info_phandle(const void *fdt_addr, int of_offset,
312 const char *prop_name);
315 * Parses a GPIO entry and fills in the gpio info data structure
317 * @param[in] fdt_addr Address of FDT
318 * @param phandle phandle for GPIO
319 * @param pin pin number
320 * @param flags flags set (1 = invert)
321 * @param[out] gpio GPIO info data structure
323 * @return 0 for success, -1 on error
325 int cvmx_fdt_parse_gpio(const void *fdt_addr, int phandle, int pin, u32 flags,
326 struct cvmx_fdt_gpio_info *gpio);
329 * @param gpio GPIO descriptor to assign timer to
330 * @param timer Octeon hardware timer number [0..3]
332 void cvmx_fdt_gpio_set_timer(struct cvmx_fdt_gpio_info *gpio, int timer);
335 * Given a GPIO pin descriptor, input the value of that pin
337 * @param pin GPIO pin descriptor
339 * @return 0 if low, 1 if high, -1 on error. Note that the input will be
340 * inverted if the CVMX_GPIO_ACTIVE_LOW flag bit is set.
342 int cvmx_fdt_gpio_get(struct cvmx_fdt_gpio_info *pin);
345 * Sets a GPIO pin given the GPIO descriptor
347 * @param gpio GPIO pin descriptor
348 * @param value value to set it to, 0 or 1
350 * @return 0 on success, -1 on error.
352 * NOTE: If the CVMX_GPIO_ACTIVE_LOW flag is set then the output value will be
355 int cvmx_fdt_gpio_set(struct cvmx_fdt_gpio_info *gpio, int value);
358 * Sets the blink frequency for a GPIO pin
360 * @param gpio GPIO handle
361 * @param freq Frequency in hz [0..500]
363 void cvmx_fdt_gpio_set_freq(struct cvmx_fdt_gpio_info *gpio, int freq);
366 * Enables or disables blinking a GPIO pin
368 * @param gpio GPIO handle
369 * @param blink True to start blinking, false to stop
371 * @return 0 for success, -1 on error
372 * NOTE: Not all GPIO types support blinking.
374 int cvmx_fdt_gpio_set_blink(struct cvmx_fdt_gpio_info *gpio, bool blink);
377 * Alternates between link and blink mode
379 * @param gpio GPIO handle
380 * @param blink True to start blinking, false to use link status
382 * @return 0 for success, -1 on error
383 * NOTE: Not all GPIO types support this.
385 int cvmx_fdt_gpio_set_link_blink(struct cvmx_fdt_gpio_info *gpio, bool blink);
387 static inline bool cvmx_fdt_gpio_hw_link_supported(const struct cvmx_fdt_gpio_info *gpio)
389 return gpio->hw_link;
393 * Configures a GPIO pin as input or output
395 * @param gpio GPIO pin to configure
396 * @param output Set to true to make output, false for input
398 void cvmx_fdt_gpio_set_output(struct cvmx_fdt_gpio_info *gpio, bool output);
401 * Allocates an LED data structure
402 * @param[in] name name to assign LED
403 * @param of_offset Device tree offset
404 * @param gpio GPIO assigned to LED (can be NULL)
405 * @param last Previous LED to build a list
407 * @return pointer to LED data structure or NULL if out of memory
409 struct cvmx_fdt_gpio_led *cvmx_alloc_led(const char *name, int of_offset,
410 struct cvmx_fdt_gpio_info *gpio,
411 struct cvmx_fdt_gpio_led *last);
414 * Parses an LED in the device tree
416 * @param[in] fdt_addr Pointer to flat device tree
417 * @param led_of_offset Device tree offset of LED
418 * @param gpio GPIO data structure to use (can be NULL)
419 * @param last Previous LED if this is a group of LEDs
421 * @return Pointer to LED data structure or NULL if error
423 struct cvmx_fdt_gpio_led *cvmx_fdt_parse_led(const void *fdt_addr, int led_of_offset,
424 struct cvmx_fdt_gpio_info *gpio,
425 struct cvmx_fdt_gpio_led *last);
427 #endif /* __CVMX_HELPER_GPIO_H__ */