| 1 |
|
| 2 |
|
| 3 |
#include <device/pnp_ops.h> |
| 4 |
#include <device/pnp.h> |
| 5 |
#include <stdint.h> |
| 6 |
|
| 7 |
#include "ite.h" |
| 8 |
#include "ite_gpio.h" |
| 9 |
|
| 10 |
|
| 11 |
#if CONFIG_SUPERIO_ITE_COMMON_NUM_GPIO_SETS == 0 |
| 12 |
#error "Maximum number of ITE SIO GPIO sets not provided" |
| 13 |
#endif |
| 14 |
|
| 15 |
#if CONFIG_SUPERIO_ITE_COMMON_NUM_GPIO_SETS > 10 |
| 16 |
#error "ITE SIO GPIO drivers only support up to 10 GPIO sets" |
| 17 |
#endif |
| 18 |
|
| 19 |
|
| 20 |
#define ITE_GPIO_REG_POLARITY(x) \ |
| 21 |
(((x) > 8) ? (0xd1 + ((x) - 9) * 5) \ |
| 22 |
: (0xb0 + ((x) - 1)) \ |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
#define ITE_GPIO_REG_PULLUP(x) \ |
| 27 |
(((x) > 8) ? (0xd4 + ((x) - 9) * 5) \ |
| 28 |
: (0xb8 + ((x) - 1)) \ |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
#define ITE_GPIO_REG_FN_SELECT(x) \ |
| 33 |
(((x) > 8) ? (0xd3 + ((x) - 9) * 5) \ |
| 34 |
: (0xc0 + ((x) - 1)) \ |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
#define ITE_GPIO_REG_OUTPUT(x) \ |
| 39 |
(((x) > 8) ? (0xd2 + ((x) - 9) * 5) \ |
| 40 |
: (0xc8 + ((x) - 1)) \ |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
#define ITE_GPIO_REG_LED_PINMAP(x) (0xf8 + ((x) & 1) * 2) |
| 45 |
#define ITE_GPIO_LED_PIN_LOC(set, pin) ((((set) & 7) << 3) | ((pin) & 7)) |
| 46 |
#define ITE_GPIO_LED_PIN_LOC_MASK 0x3f |
| 47 |
|
| 48 |
#define ITE_GPIO_REG_LED_CONTROL(x) (0xf9 + ((x) & 1) * 2) |
| 49 |
#define ITE_GPIO_LED_OUTPUT_LOW (1 << 0) |
| 50 |
#define ITE_GPIO_LED_PINMAP_CLEAR (1 << 4) |
| 51 |
#define ITE_GPIO_LED_SHORT_LOW_PULSE \ |
| 52 |
(CONFIG(SUPERIO_ITE_COMMON_GPIO_LED_FREQ_5BIT) ? (1 << 5) \ |
| 53 |
: (1 << 3) \ |
| 54 |
) |
| 55 |
#define ITE_GPIO_LED_FREQ_SEL(x) \ |
| 56 |
(CONFIG(SUPERIO_ITE_COMMON_GPIO_LED_FREQ_5BIT) \ |
| 57 |
? ((((x) & 0x18) << 3) | (((x) & 0x7) << 1)) \ |
| 58 |
: (((x) & 0x3) << 1) \ |
| 59 |
) |
| 60 |
#define ITE_GPIO_LED_FREQ_SEL_MASK \ |
| 61 |
(CONFIG(SUPERIO_ITE_COMMON_GPIO_LED_FREQ_5BIT) ? 0xce : 0x06) |
| 62 |
|
| 63 |
static bool ite_has_gpio_fn_select_reg(u8 set) |
| 64 |
{ |
| 65 |
|
| 66 |
if (CONFIG(SUPERIO_ITE_IT8718F)) |
| 67 |
return true; |
| 68 |
|
| 69 |
|
| 70 |
if (set < 6 || set > 8) |
| 71 |
return true; |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
static bool ite_has_gpio_polarity_reg(u8 set) |
| 77 |
{ |
| 78 |
|
| 79 |
if (CONFIG(SUPERIO_ITE_IT8718F)) |
| 80 |
return true; |
| 81 |
|
| 82 |
|
| 83 |
if (CONFIG(SUPERIO_ITE_IT8720F) || CONFIG(SUPERIO_ITE_IT8721F)) |
| 84 |
return true; |
| 85 |
|
| 86 |
|
| 87 |
if (set < 6 || set > 8) |
| 88 |
return true; |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
static bool ite_has_gpio_pullup_reg(u8 set) |
| 94 |
{ |
| 95 |
|
| 96 |
if ((CONFIG(SUPERIO_ITE_IT8718F) || CONFIG(SUPERIO_ITE_IT8720F)) && (set == 2)) |
| 97 |
return false; |
| 98 |
|
| 99 |
|
| 100 |
if (CONFIG(SUPERIO_ITE_IT8783EF) && (set == 6)) |
| 101 |
return false; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (set != 7 && set != 8) |
| 109 |
return true; |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void ite_gpio_setup(pnp_devfn_t gpiodev, u8 gpio_num, enum ite_gpio_direction in_out, |
| 123 |
enum ite_gpio_mode enable, u8 gpio_ctrl) |
| 124 |
{ |
| 125 |
u8 set = (gpio_num / 10); |
| 126 |
u8 pin = (gpio_num % 10); |
| 127 |
|
| 128 |
|
| 129 |
if (gpio_num < 10 || set > CONFIG_SUPERIO_ITE_COMMON_NUM_GPIO_SETS || pin > 7) |
| 130 |
return; |
| 131 |
|
| 132 |
pnp_enter_conf_state(gpiodev); |
| 133 |
pnp_set_logical_device(gpiodev); |
| 134 |
|
| 135 |
if (ite_has_gpio_fn_select_reg(set)) |
| 136 |
pnp_unset_and_set_config(gpiodev, ITE_GPIO_REG_FN_SELECT(set), |
| 137 |
1 << pin, (enable & 1) << pin); |
| 138 |
|
| 139 |
if (ite_has_gpio_polarity_reg(set)) |
| 140 |
pnp_unset_and_set_config(gpiodev, ITE_GPIO_REG_POLARITY(set), |
| 141 |
1 << pin, |
| 142 |
(gpio_ctrl & ITE_GPIO_POL_INVERT) ? 1 << pin : 0); |
| 143 |
|
| 144 |
|
| 145 |
pnp_unset_and_set_config(gpiodev, ITE_GPIO_REG_OUTPUT(set), 1 << pin, (in_out & 1) << pin); |
| 146 |
|
| 147 |
if (ite_has_gpio_pullup_reg(set)) |
| 148 |
pnp_unset_and_set_config(gpiodev, ITE_GPIO_REG_PULLUP(set), 1 << pin, |
| 149 |
(gpio_ctrl & ITE_GPIO_PULLUP_ENABLE) ? 1 << pin : 0); |
| 150 |
|
| 151 |
pnp_exit_conf_state(gpiodev); |
| 152 |
} |
| 153 |
|
| 154 |
void ite_gpio_setup_led(pnp_devfn_t gpiodev, u8 gpio_num, |
| 155 |
enum ite_gpio_led led_no, |
| 156 |
enum ite_led_frequency freq, |
| 157 |
u8 led_ctrl) |
| 158 |
{ |
| 159 |
u8 set = (gpio_num / 10); |
| 160 |
u8 pin = (gpio_num % 10); |
| 161 |
u8 reg = 0; |
| 162 |
|
| 163 |
|
| 164 |
if (gpio_num < 10 || set > CONFIG_SUPERIO_ITE_COMMON_NUM_GPIO_SETS || pin > 7) |
| 165 |
return; |
| 166 |
|
| 167 |
|
| 168 |
if (set > 5) |
| 169 |
return; |
| 170 |
|
| 171 |
pnp_enter_conf_state(gpiodev); |
| 172 |
pnp_set_logical_device(gpiodev); |
| 173 |
|
| 174 |
|
| 175 |
if (CONFIG(SUPERIO_ITE_COMMON_GPIO_LED_FREQ_5BIT) && (led_ctrl & ITE_LED_PINMAP_CLEAR)) |
| 176 |
reg |= ITE_GPIO_LED_PINMAP_CLEAR; |
| 177 |
|
| 178 |
if (led_ctrl & ITE_LED_OUTPUT_LOW) |
| 179 |
reg |= ITE_GPIO_LED_OUTPUT_LOW; |
| 180 |
|
| 181 |
if (led_ctrl & ITE_LED_SHORT_LOW_PULSE) |
| 182 |
reg |= ITE_GPIO_LED_SHORT_LOW_PULSE; |
| 183 |
|
| 184 |
reg |= ITE_GPIO_LED_FREQ_SEL(freq); |
| 185 |
pnp_write_config(gpiodev, ITE_GPIO_REG_LED_CONTROL(led_no), reg); |
| 186 |
|
| 187 |
reg = ITE_GPIO_LED_PIN_LOC(set, pin); |
| 188 |
pnp_write_config(gpiodev, ITE_GPIO_REG_LED_PINMAP(led_no), reg); |
| 189 |
|
| 190 |
pnp_exit_conf_state(gpiodev); |
| 191 |
} |