Files
OpenCellular/include/gpio.h
Aseda Aboagye e9883124ff gpio: Refactor IRQ handler pointer out of gpio_list
In the gpio_info struct, we had a irq_handler pointer defined even
though a majority of the GPIOs did not have irq handlers associated. By
removing the irq_handler pointer out of the struct, we can save some
space with some targets saving more than others. (For example, ~260
bytes for samus_pd).

This change also brings about a new define:

     GPIO_INT(name, port, pin, flags, signal)

And the existing GPIO macro has had the signal parameter removed since
they were just NULL.

     GPIO(name, port, pin, flags)

In each of the gpio.inc files, all the GPIOs with irq handlers must be
defined at the top of the file. This is because their enum values from
gpio_signal are used as the index to the gpio_irq_handlers table.

BUG=chromium:471331
BRANCH=none
TEST=Flashed ec to samus and samus_pd, verified lightbar tap, lid, power
button, keyboard, charging, all still working.
TEST=Moved a GPIO_INT declaration after a GPIO declaration and watched the build
fail.
TEST=make -j BOARD=peppy tests
TEST=make -j BOARD=auron tests
TEST=make -j BOARD=link tests

Change-Id: Id6e261b0a3cd63223ca92f2e96a80c95e85cdefb
Signed-off-by: Aseda Aboagye <aaboagye@google.com>
Reviewed-on: https://chromium-review.googlesource.com/263973
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Tested-by: Aseda Aboagye <aaboagye@chromium.org>
Commit-Queue: Aseda Aboagye <aaboagye@chromium.org>
Trybot-Ready: Aseda Aboagye <aaboagye@chromium.org>
Reviewed-by: Alec Berg <alecaberg@chromium.org>
2015-04-10 22:08:25 +00:00

233 lines
7.3 KiB
C

/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* GPIO module for Chrome EC */
#ifndef __CROS_EC_GPIO_H
#define __CROS_EC_GPIO_H
#include "common.h"
/* Flag definitions for gpio_info and gpio_alt_func */
/* The following are valid for both gpio_info and gpio_alt_func: */
#define GPIO_OPEN_DRAIN (1 << 0) /* Output type is open-drain */
#define GPIO_PULL_UP (1 << 1) /* Enable on-chip pullup */
#define GPIO_PULL_DOWN (1 << 2) /* Enable on-chip pulldown */
/* The following are valid for gpio_alt_func only */
#define GPIO_ANALOG (1 << 3) /* Set pin to analog-mode */
/* The following are valid for gpio_info only */
#define GPIO_INPUT (1 << 4) /* Input */
#define GPIO_OUTPUT (1 << 5) /* Output */
#define GPIO_LOW (1 << 6) /* If GPIO_OUTPUT, set level low */
#define GPIO_HIGH (1 << 7) /* If GPIO_OUTPUT, set level high */
#define GPIO_INT_F_RISING (1 << 8) /* Interrupt on rising edge */
#define GPIO_INT_F_FALLING (1 << 9) /* Interrupt on falling edge */
#define GPIO_INT_F_LOW (1 << 11) /* Interrupt on low level */
#define GPIO_INT_F_HIGH (1 << 12) /* Interrupt on high level */
#define GPIO_DEFAULT (1 << 13) /* Don't set up on boot */
#define GPIO_INT_DSLEEP (1 << 14) /* Interrupt in deep sleep */
#define GPIO_INT_SHARED (1 << 15) /* Shared among multiple pins */
/* Common flag combinations */
#define GPIO_OUT_LOW (GPIO_OUTPUT | GPIO_LOW)
#define GPIO_OUT_HIGH (GPIO_OUTPUT | GPIO_HIGH)
#define GPIO_ODR_HIGH (GPIO_OUTPUT | GPIO_OPEN_DRAIN | GPIO_HIGH)
#define GPIO_ODR_LOW (GPIO_OUTPUT | GPIO_OPEN_DRAIN | GPIO_LOW)
#define GPIO_INT_RISING (GPIO_INPUT | GPIO_INT_F_RISING)
#define GPIO_INT_FALLING (GPIO_INPUT | GPIO_INT_F_FALLING)
/* TODO(crosbug.com/p/24204): "EDGE" would have been clearer than "BOTH". */
#define GPIO_INT_BOTH (GPIO_INT_RISING | GPIO_INT_FALLING)
#define GPIO_INT_LOW (GPIO_INPUT | GPIO_INT_F_LOW)
#define GPIO_INT_HIGH (GPIO_INPUT | GPIO_INT_F_HIGH)
#define GPIO_INT_LEVEL (GPIO_INT_LOW | GPIO_INT_HIGH)
#define GPIO_INT_ANY (GPIO_INT_BOTH | GPIO_INT_LEVEL)
#define GPIO_INT_BOTH_DSLEEP (GPIO_INT_BOTH | GPIO_INT_DSLEEP)
/* NOTE: This is normally included from board.h, thru config.h and common.h But,
* some boards and unit tests don't have a gpio_signal enum defined, so we
* define an emtpy one here.*/
#ifndef __CROS_EC_GPIO_SIGNAL_H
enum gpio_signal {
NULL
};
#endif /* __CROS_EC_GPIO_SIGNAL_H */
/* GPIO signal definition structure, for use by board.c */
struct gpio_info {
/* Signal name */
const char *name;
/* Port base address */
uint32_t port;
/* Bitmask on that port (1 << N; 0 = signal not implemented) */
uint32_t mask;
/* Flags (GPIO_*; see above) */
uint32_t flags;
};
/* Signal information from board.c. Must match order from enum gpio_signal. */
extern const struct gpio_info gpio_list[];
/* Interrupt handler table for those GPIOs which have IRQ handlers.
*
* If the signal's interrupt is enabled, this will be called in the
* context of the GPIO interrupt handler.
*/
extern void (* const gpio_irq_handlers[])(enum gpio_signal signal);
extern const int gpio_ih_count;
#define GPIO_IH_COUNT gpio_ih_count
/* GPIO alternate function structure, for use by board.c */
struct gpio_alt_func {
/* Port base address */
uint32_t port;
/* Bitmask on that port (multiple bits allowed) */
uint32_t mask;
/* Alternate function number */
uint8_t func;
/* Module ID (as uint8_t, since enum would be 32-bit) */
uint8_t module_id;
/* Flags (GPIO_*; see above). */
uint16_t flags;
};
extern const struct gpio_alt_func gpio_alt_funcs[];
extern const int gpio_alt_funcs_count;
/**
* Pre-initialize GPIOs.
*
* This occurs before clocks or tasks are set up.
*/
void gpio_pre_init(void);
/**
* Configure GPIO pin functions for a module.
*
* @param id Module ID to initialize
* @param enable Enable alternate functions if 1; high-Z pins if 0.
*/
void gpio_config_module(enum module_id id, int enable);
/**
* Get the current value of a signal.
*
* @param signal Signal to get
* @return 0 if low, 1 if high.
*/
int gpio_get_level(enum gpio_signal signal);
/**
* Get faster access to a GPIO level.
*
* Use this function to find out the register address and mask for a GPIO
* value. Then you can just check that instead of calling gpio_get_level().
*
* @param signal Signal to return details for
* @param mask Mask value to use
* @return pointer to register to read to get GPIO value
*/
uint16_t *gpio_get_level_reg(enum gpio_signal signal, uint32_t *mask);
/**
* Return the name of a given GPIO signal.
*
* @param signal Signal to name
* @returns name of the given signal
*/
const char *gpio_get_name(enum gpio_signal signal);
/**
* Set the flags for a signal.
*
* @param signal Signal to set flags for
* @param flags New flags for the signal
*/
void gpio_set_flags(enum gpio_signal signal, int flags);
/**
* Set the value of a signal.
*
* @param signal Signal to set
* @param value New value for signal (0 = low, != high */
void gpio_set_level(enum gpio_signal signal, int value);
/**
* Enable interrupts for the signal.
*
* The signal must have been defined with
* an interrupt handler. Normally called by the module which handles the
* interrupt, once it's ready to start processing interrupts.
*
* @param signal Signal to enable interrrupts for
* @return EC_SUCCESS, or non-zero if error.
*/
int gpio_enable_interrupt(enum gpio_signal signal);
/**
* Disable interrupts for the signal.
*
* The signal must have been defined with
* an interrupt handler. Normally called by the module which handles the
* interrupt, if it doesn't want to process interrupts.
*
* @param signal Signal to disable interrupts for
* @return EC_SUCCESS, or non-zero if error.
*/
int gpio_disable_interrupt(enum gpio_signal signal);
/**
* Set flags for GPIO(s) by port and mask.
*
* Use gpio_set_flags() to set flags for an individual GPIO by id.
*
* Note that modules should usually declare their GPIO alternate functions in
* gpio_alt_funcs[] and call gpio_init_module() instead of calling this
* directly.
*
* @param port GPIO port to set (GPIO_*)
* @param mask Bitmask of pins on that port to affect
* @param flags Flags (GPIO_*; see above)
*/
void gpio_set_flags_by_mask(uint32_t port, uint32_t mask, uint32_t flags);
/**
* Set alternate function for GPIO(s).
*
* Note that modules should usually declare their GPIO alternate functions in
* gpio_alt_funcs[] and call gpio_init_module() instead of calling this
* directly.
*
* @param port GPIO port to set (GPIO_*)
* @param mask Bitmask of pins on that port to affect
* @param func Alternate function; if <0, configures the specified
* GPIOs for normal GPIO operation.
*/
void gpio_set_alternate_function(uint32_t port, uint32_t mask, int func);
/**
* Return true if the EC is warm booting.
*
* This function is used by the GPIO implementation and should not be called
* outside of that context.
*/
int gpio_is_reboot_warm(void);
/**
* Enable GPIO peripheral clocks.
*
* This function is used by the GPIO implementation and should not be called
* outside of that context.
*/
void gpio_enable_clocks(void);
#endif /* __CROS_EC_GPIO_H */