xref: /linux/drivers/gpio/gpio-wcove.c (revision 0c3b532ad3fbf82884a2e7e83e37c7dcdd4d1d99)
1fd30b72eSAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
20ba19cfcSBin Gao /*
30ba19cfcSBin Gao  * Intel Whiskey Cove PMIC GPIO Driver
40ba19cfcSBin Gao  *
50ba19cfcSBin Gao  * This driver is written based on gpio-crystalcove.c
60ba19cfcSBin Gao  *
70ba19cfcSBin Gao  * Copyright (C) 2016 Intel Corporation. All rights reserved.
80ba19cfcSBin Gao  */
90ba19cfcSBin Gao 
100ba19cfcSBin Gao #include <linux/bitops.h>
110ba19cfcSBin Gao #include <linux/gpio/driver.h>
1239684807SAndy Shevchenko #include <linux/interrupt.h>
130ba19cfcSBin Gao #include <linux/mfd/intel_soc_pmic.h>
1439684807SAndy Shevchenko #include <linux/module.h>
150ba19cfcSBin Gao #include <linux/platform_device.h>
160ba19cfcSBin Gao #include <linux/regmap.h>
170ba19cfcSBin Gao #include <linux/seq_file.h>
180ba19cfcSBin Gao 
190ba19cfcSBin Gao /*
200ba19cfcSBin Gao  * Whiskey Cove PMIC has 13 physical GPIO pins divided into 3 banks:
210ba19cfcSBin Gao  * Bank 0: Pin  0 - 6
220ba19cfcSBin Gao  * Bank 1: Pin  7 - 10
230ba19cfcSBin Gao  * Bank 2: Pin 11 - 12
240ba19cfcSBin Gao  * Each pin has one output control register and one input control register.
250ba19cfcSBin Gao  */
260ba19cfcSBin Gao #define BANK0_NR_PINS		7
270ba19cfcSBin Gao #define BANK1_NR_PINS		4
280ba19cfcSBin Gao #define BANK2_NR_PINS		2
290ba19cfcSBin Gao #define WCOVE_GPIO_NUM		(BANK0_NR_PINS + BANK1_NR_PINS + BANK2_NR_PINS)
300ba19cfcSBin Gao #define WCOVE_VGPIO_NUM		94
310ba19cfcSBin Gao /* GPIO output control registers (one per pin): 0x4e44 - 0x4e50 */
320ba19cfcSBin Gao #define GPIO_OUT_CTRL_BASE	0x4e44
330ba19cfcSBin Gao /* GPIO input control registers (one per pin): 0x4e51 - 0x4e5d */
340ba19cfcSBin Gao #define GPIO_IN_CTRL_BASE	0x4e51
350ba19cfcSBin Gao 
360ba19cfcSBin Gao /*
370ba19cfcSBin Gao  * GPIO interrupts are organized in two groups:
380ba19cfcSBin Gao  * Group 0: Bank 0 pins (Pin 0 - 6)
390ba19cfcSBin Gao  * Group 1: Bank 1 and Bank 2 pins (Pin 7 - 12)
400ba19cfcSBin Gao  * Each group has two registers (one bit per pin): status and mask.
410ba19cfcSBin Gao  */
420ba19cfcSBin Gao #define GROUP0_NR_IRQS		7
430ba19cfcSBin Gao #define GROUP1_NR_IRQS		6
440ba19cfcSBin Gao #define IRQ_MASK_BASE		0x4e19
450ba19cfcSBin Gao #define IRQ_STATUS_BASE		0x4e0b
46881ebd22SKuppuswamy Sathyanarayanan #define GPIO_IRQ0_MASK		GENMASK(6, 0)
47881ebd22SKuppuswamy Sathyanarayanan #define GPIO_IRQ1_MASK		GENMASK(5, 0)
480ba19cfcSBin Gao #define UPDATE_IRQ_TYPE		BIT(0)
490ba19cfcSBin Gao #define UPDATE_IRQ_MASK		BIT(1)
500ba19cfcSBin Gao 
510ba19cfcSBin Gao #define CTLI_INTCNT_DIS		(0 << 1)
520ba19cfcSBin Gao #define CTLI_INTCNT_NE		(1 << 1)
530ba19cfcSBin Gao #define CTLI_INTCNT_PE		(2 << 1)
540ba19cfcSBin Gao #define CTLI_INTCNT_BE		(3 << 1)
550ba19cfcSBin Gao 
560ba19cfcSBin Gao #define CTLO_DIR_IN		(0 << 5)
570ba19cfcSBin Gao #define CTLO_DIR_OUT		(1 << 5)
580ba19cfcSBin Gao 
590ba19cfcSBin Gao #define CTLO_DRV_MASK		(1 << 4)
600ba19cfcSBin Gao #define CTLO_DRV_OD		(0 << 4)
610ba19cfcSBin Gao #define CTLO_DRV_CMOS		(1 << 4)
620ba19cfcSBin Gao 
630ba19cfcSBin Gao #define CTLO_DRV_REN		(1 << 3)
640ba19cfcSBin Gao 
650ba19cfcSBin Gao #define CTLO_RVAL_2KDOWN	(0 << 1)
660ba19cfcSBin Gao #define CTLO_RVAL_2KUP		(1 << 1)
670ba19cfcSBin Gao #define CTLO_RVAL_50KDOWN	(2 << 1)
680ba19cfcSBin Gao #define CTLO_RVAL_50KUP		(3 << 1)
690ba19cfcSBin Gao 
700ba19cfcSBin Gao #define CTLO_INPUT_SET		(CTLO_DRV_CMOS | CTLO_DRV_REN | CTLO_RVAL_2KUP)
710ba19cfcSBin Gao #define CTLO_OUTPUT_SET		(CTLO_DIR_OUT | CTLO_INPUT_SET)
720ba19cfcSBin Gao 
730ba19cfcSBin Gao enum ctrl_register {
740ba19cfcSBin Gao 	CTRL_IN,
750ba19cfcSBin Gao 	CTRL_OUT,
765a2a46aeSAndy Shevchenko 	IRQ_STATUS,
775a2a46aeSAndy Shevchenko 	IRQ_MASK,
780ba19cfcSBin Gao };
790ba19cfcSBin Gao 
800ba19cfcSBin Gao /*
810ba19cfcSBin Gao  * struct wcove_gpio - Whiskey Cove GPIO controller
820ba19cfcSBin Gao  * @buslock: for bus lock/sync and unlock.
830ba19cfcSBin Gao  * @chip: the abstract gpio_chip structure.
840ba19cfcSBin Gao  * @dev: the gpio device
850ba19cfcSBin Gao  * @regmap: the regmap from the parent device.
860ba19cfcSBin Gao  * @regmap_irq_chip: the regmap of the gpio irq chip.
870ba19cfcSBin Gao  * @update: pending IRQ setting update, to be written to the chip upon unlock.
880ba19cfcSBin Gao  * @intcnt: the Interrupt Detect value to be written.
890ba19cfcSBin Gao  * @set_irq_mask: true if the IRQ mask needs to be set, false to clear.
900ba19cfcSBin Gao  */
910ba19cfcSBin Gao struct wcove_gpio {
920ba19cfcSBin Gao 	struct mutex buslock;
930ba19cfcSBin Gao 	struct gpio_chip chip;
940ba19cfcSBin Gao 	struct device *dev;
950ba19cfcSBin Gao 	struct regmap *regmap;
960ba19cfcSBin Gao 	struct regmap_irq_chip_data *regmap_irq_chip;
970ba19cfcSBin Gao 	int update;
980ba19cfcSBin Gao 	int intcnt;
990ba19cfcSBin Gao 	bool set_irq_mask;
1000ba19cfcSBin Gao };
1010ba19cfcSBin Gao 
1025d993664SAndy Shevchenko static inline int to_reg(int gpio, enum ctrl_register type)
1030ba19cfcSBin Gao {
1045d993664SAndy Shevchenko 	unsigned int reg = type == CTRL_IN ? GPIO_IN_CTRL_BASE : GPIO_OUT_CTRL_BASE;
1050ba19cfcSBin Gao 
1063a02dc97SKuppuswamy Sathyanarayanan 	if (gpio >= WCOVE_GPIO_NUM)
107*0c3b532aSAndy Shevchenko 		return -ENOTSUPP;
1080ba19cfcSBin Gao 
1095d993664SAndy Shevchenko 	return reg + gpio;
1100ba19cfcSBin Gao }
1110ba19cfcSBin Gao 
1125a2a46aeSAndy Shevchenko static inline int to_ireg(int gpio, enum ctrl_register type, unsigned int *mask)
1130ba19cfcSBin Gao {
1145a2a46aeSAndy Shevchenko 	unsigned int reg = type == IRQ_STATUS ? IRQ_STATUS_BASE : IRQ_MASK_BASE;
1150ba19cfcSBin Gao 
1160ba19cfcSBin Gao 	if (gpio < GROUP0_NR_IRQS) {
1175a2a46aeSAndy Shevchenko 		reg += 0;
1185a2a46aeSAndy Shevchenko 		*mask = BIT(gpio);
1190ba19cfcSBin Gao 	} else {
1205a2a46aeSAndy Shevchenko 		reg += 1;
1215a2a46aeSAndy Shevchenko 		*mask = BIT(gpio - GROUP0_NR_IRQS);
1220ba19cfcSBin Gao 	}
1230ba19cfcSBin Gao 
1245a2a46aeSAndy Shevchenko 	return reg;
1255a2a46aeSAndy Shevchenko }
1265a2a46aeSAndy Shevchenko 
127f3019092SAndy Shevchenko static void wcove_update_irq_mask(struct wcove_gpio *wg, irq_hw_number_t gpio)
1285a2a46aeSAndy Shevchenko {
1295a2a46aeSAndy Shevchenko 	unsigned int mask, reg = to_ireg(gpio, IRQ_MASK, &mask);
1305a2a46aeSAndy Shevchenko 
1310ba19cfcSBin Gao 	if (wg->set_irq_mask)
1329fe5fcd6SAndy Shevchenko 		regmap_set_bits(wg->regmap, reg, mask);
1330ba19cfcSBin Gao 	else
1349fe5fcd6SAndy Shevchenko 		regmap_clear_bits(wg->regmap, reg, mask);
1350ba19cfcSBin Gao }
1360ba19cfcSBin Gao 
137f3019092SAndy Shevchenko static void wcove_update_irq_ctrl(struct wcove_gpio *wg, irq_hw_number_t gpio)
1380ba19cfcSBin Gao {
1393a02dc97SKuppuswamy Sathyanarayanan 	int reg = to_reg(gpio, CTRL_IN);
1403a02dc97SKuppuswamy Sathyanarayanan 
1410ba19cfcSBin Gao 	regmap_update_bits(wg->regmap, reg, CTLI_INTCNT_BE, wg->intcnt);
1420ba19cfcSBin Gao }
1430ba19cfcSBin Gao 
1440ba19cfcSBin Gao static int wcove_gpio_dir_in(struct gpio_chip *chip, unsigned int gpio)
1450ba19cfcSBin Gao {
1460ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
1473a02dc97SKuppuswamy Sathyanarayanan 	int reg = to_reg(gpio, CTRL_OUT);
1480ba19cfcSBin Gao 
1493a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
1503a02dc97SKuppuswamy Sathyanarayanan 		return 0;
1513a02dc97SKuppuswamy Sathyanarayanan 
1523a02dc97SKuppuswamy Sathyanarayanan 	return regmap_write(wg->regmap, reg, CTLO_INPUT_SET);
1530ba19cfcSBin Gao }
1540ba19cfcSBin Gao 
1550ba19cfcSBin Gao static int wcove_gpio_dir_out(struct gpio_chip *chip, unsigned int gpio,
1560ba19cfcSBin Gao 				    int value)
1570ba19cfcSBin Gao {
1580ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
1593a02dc97SKuppuswamy Sathyanarayanan 	int reg = to_reg(gpio, CTRL_OUT);
1600ba19cfcSBin Gao 
1613a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
1623a02dc97SKuppuswamy Sathyanarayanan 		return 0;
1633a02dc97SKuppuswamy Sathyanarayanan 
1643a02dc97SKuppuswamy Sathyanarayanan 	return regmap_write(wg->regmap, reg, CTLO_OUTPUT_SET | value);
1650ba19cfcSBin Gao }
1660ba19cfcSBin Gao 
1677d9e59ceSBin Gao static int wcove_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
1687d9e59ceSBin Gao {
1697d9e59ceSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
1707d9e59ceSBin Gao 	unsigned int val;
1713a02dc97SKuppuswamy Sathyanarayanan 	int ret, reg = to_reg(gpio, CTRL_OUT);
1727d9e59ceSBin Gao 
1733a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
174e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_OUT;
1753a02dc97SKuppuswamy Sathyanarayanan 
1763a02dc97SKuppuswamy Sathyanarayanan 	ret = regmap_read(wg->regmap, reg, &val);
1777d9e59ceSBin Gao 	if (ret)
1787d9e59ceSBin Gao 		return ret;
1797d9e59ceSBin Gao 
180e42615ecSMatti Vaittinen 	if (val & CTLO_DIR_OUT)
181e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_OUT;
182e42615ecSMatti Vaittinen 
183e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
1847d9e59ceSBin Gao }
1857d9e59ceSBin Gao 
1860ba19cfcSBin Gao static int wcove_gpio_get(struct gpio_chip *chip, unsigned int gpio)
1870ba19cfcSBin Gao {
1880ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
1890ba19cfcSBin Gao 	unsigned int val;
1903a02dc97SKuppuswamy Sathyanarayanan 	int ret, reg = to_reg(gpio, CTRL_IN);
1910ba19cfcSBin Gao 
1923a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
1933a02dc97SKuppuswamy Sathyanarayanan 		return 0;
1943a02dc97SKuppuswamy Sathyanarayanan 
1953a02dc97SKuppuswamy Sathyanarayanan 	ret = regmap_read(wg->regmap, reg, &val);
1960ba19cfcSBin Gao 	if (ret)
1970ba19cfcSBin Gao 		return ret;
1980ba19cfcSBin Gao 
1990ba19cfcSBin Gao 	return val & 0x1;
2000ba19cfcSBin Gao }
2010ba19cfcSBin Gao 
202cb19c7f3SAndy Shevchenko static void wcove_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
2030ba19cfcSBin Gao {
2040ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
2053a02dc97SKuppuswamy Sathyanarayanan 	int reg = to_reg(gpio, CTRL_OUT);
2063a02dc97SKuppuswamy Sathyanarayanan 
2073a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
2083a02dc97SKuppuswamy Sathyanarayanan 		return;
2090ba19cfcSBin Gao 
2100ba19cfcSBin Gao 	if (value)
2119fe5fcd6SAndy Shevchenko 		regmap_set_bits(wg->regmap, reg, 1);
2120ba19cfcSBin Gao 	else
2139fe5fcd6SAndy Shevchenko 		regmap_clear_bits(wg->regmap, reg, 1);
2140ba19cfcSBin Gao }
2150ba19cfcSBin Gao 
2162956b5d9SMika Westerberg static int wcove_gpio_set_config(struct gpio_chip *chip, unsigned int gpio,
2172956b5d9SMika Westerberg 				 unsigned long config)
2180ba19cfcSBin Gao {
2190ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
2203a02dc97SKuppuswamy Sathyanarayanan 	int reg = to_reg(gpio, CTRL_OUT);
2213a02dc97SKuppuswamy Sathyanarayanan 
2223a02dc97SKuppuswamy Sathyanarayanan 	if (reg < 0)
2233a02dc97SKuppuswamy Sathyanarayanan 		return 0;
2240ba19cfcSBin Gao 
2252956b5d9SMika Westerberg 	switch (pinconf_to_config_param(config)) {
2262956b5d9SMika Westerberg 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
2273a02dc97SKuppuswamy Sathyanarayanan 		return regmap_update_bits(wg->regmap, reg, CTLO_DRV_MASK,
2283a02dc97SKuppuswamy Sathyanarayanan 					  CTLO_DRV_OD);
2292956b5d9SMika Westerberg 	case PIN_CONFIG_DRIVE_PUSH_PULL:
2303a02dc97SKuppuswamy Sathyanarayanan 		return regmap_update_bits(wg->regmap, reg, CTLO_DRV_MASK,
2313a02dc97SKuppuswamy Sathyanarayanan 					  CTLO_DRV_CMOS);
2320ba19cfcSBin Gao 	default:
2330ba19cfcSBin Gao 		break;
2340ba19cfcSBin Gao 	}
2350ba19cfcSBin Gao 
2360ba19cfcSBin Gao 	return -ENOTSUPP;
2370ba19cfcSBin Gao }
2380ba19cfcSBin Gao 
2390ba19cfcSBin Gao static int wcove_irq_type(struct irq_data *data, unsigned int type)
2400ba19cfcSBin Gao {
2410ba19cfcSBin Gao 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
2420ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
243f3019092SAndy Shevchenko 	irq_hw_number_t gpio = irqd_to_hwirq(data);
2440ba19cfcSBin Gao 
245f3019092SAndy Shevchenko 	if (gpio >= WCOVE_GPIO_NUM)
2463a02dc97SKuppuswamy Sathyanarayanan 		return 0;
2473a02dc97SKuppuswamy Sathyanarayanan 
2480ba19cfcSBin Gao 	switch (type) {
2490ba19cfcSBin Gao 	case IRQ_TYPE_NONE:
2500ba19cfcSBin Gao 		wg->intcnt = CTLI_INTCNT_DIS;
2510ba19cfcSBin Gao 		break;
2520ba19cfcSBin Gao 	case IRQ_TYPE_EDGE_BOTH:
2530ba19cfcSBin Gao 		wg->intcnt = CTLI_INTCNT_BE;
2540ba19cfcSBin Gao 		break;
2550ba19cfcSBin Gao 	case IRQ_TYPE_EDGE_RISING:
2560ba19cfcSBin Gao 		wg->intcnt = CTLI_INTCNT_PE;
2570ba19cfcSBin Gao 		break;
2580ba19cfcSBin Gao 	case IRQ_TYPE_EDGE_FALLING:
2590ba19cfcSBin Gao 		wg->intcnt = CTLI_INTCNT_NE;
2600ba19cfcSBin Gao 		break;
2610ba19cfcSBin Gao 	default:
2620ba19cfcSBin Gao 		return -EINVAL;
2630ba19cfcSBin Gao 	}
2640ba19cfcSBin Gao 
2650ba19cfcSBin Gao 	wg->update |= UPDATE_IRQ_TYPE;
2660ba19cfcSBin Gao 
2670ba19cfcSBin Gao 	return 0;
2680ba19cfcSBin Gao }
2690ba19cfcSBin Gao 
2700ba19cfcSBin Gao static void wcove_bus_lock(struct irq_data *data)
2710ba19cfcSBin Gao {
2720ba19cfcSBin Gao 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
2730ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
2740ba19cfcSBin Gao 
2750ba19cfcSBin Gao 	mutex_lock(&wg->buslock);
2760ba19cfcSBin Gao }
2770ba19cfcSBin Gao 
2780ba19cfcSBin Gao static void wcove_bus_sync_unlock(struct irq_data *data)
2790ba19cfcSBin Gao {
2800ba19cfcSBin Gao 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
2810ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
282f3019092SAndy Shevchenko 	irq_hw_number_t gpio = irqd_to_hwirq(data);
2830ba19cfcSBin Gao 
2840ba19cfcSBin Gao 	if (wg->update & UPDATE_IRQ_TYPE)
2850ba19cfcSBin Gao 		wcove_update_irq_ctrl(wg, gpio);
2860ba19cfcSBin Gao 	if (wg->update & UPDATE_IRQ_MASK)
2870ba19cfcSBin Gao 		wcove_update_irq_mask(wg, gpio);
2880ba19cfcSBin Gao 	wg->update = 0;
2890ba19cfcSBin Gao 
2900ba19cfcSBin Gao 	mutex_unlock(&wg->buslock);
2910ba19cfcSBin Gao }
2920ba19cfcSBin Gao 
2930ba19cfcSBin Gao static void wcove_irq_unmask(struct irq_data *data)
2940ba19cfcSBin Gao {
2950ba19cfcSBin Gao 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
2960ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
297f3019092SAndy Shevchenko 	irq_hw_number_t gpio = irqd_to_hwirq(data);
2980ba19cfcSBin Gao 
299f3019092SAndy Shevchenko 	if (gpio >= WCOVE_GPIO_NUM)
3003a02dc97SKuppuswamy Sathyanarayanan 		return;
3013a02dc97SKuppuswamy Sathyanarayanan 
30241a18c49SAndy Shevchenko 	gpiochip_enable_irq(chip, gpio);
30341a18c49SAndy Shevchenko 
3040ba19cfcSBin Gao 	wg->set_irq_mask = false;
3050ba19cfcSBin Gao 	wg->update |= UPDATE_IRQ_MASK;
3060ba19cfcSBin Gao }
3070ba19cfcSBin Gao 
3080ba19cfcSBin Gao static void wcove_irq_mask(struct irq_data *data)
3090ba19cfcSBin Gao {
3100ba19cfcSBin Gao 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
3110ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
312f3019092SAndy Shevchenko 	irq_hw_number_t gpio = irqd_to_hwirq(data);
3130ba19cfcSBin Gao 
314f3019092SAndy Shevchenko 	if (gpio >= WCOVE_GPIO_NUM)
3153a02dc97SKuppuswamy Sathyanarayanan 		return;
3163a02dc97SKuppuswamy Sathyanarayanan 
3170ba19cfcSBin Gao 	wg->set_irq_mask = true;
3180ba19cfcSBin Gao 	wg->update |= UPDATE_IRQ_MASK;
31941a18c49SAndy Shevchenko 
32041a18c49SAndy Shevchenko 	gpiochip_disable_irq(chip, gpio);
3210ba19cfcSBin Gao }
3220ba19cfcSBin Gao 
32341a18c49SAndy Shevchenko static const struct irq_chip wcove_irqchip = {
3240ba19cfcSBin Gao 	.name			= "Whiskey Cove",
3250ba19cfcSBin Gao 	.irq_mask		= wcove_irq_mask,
3260ba19cfcSBin Gao 	.irq_unmask		= wcove_irq_unmask,
3270ba19cfcSBin Gao 	.irq_set_type		= wcove_irq_type,
3280ba19cfcSBin Gao 	.irq_bus_lock		= wcove_bus_lock,
3290ba19cfcSBin Gao 	.irq_bus_sync_unlock	= wcove_bus_sync_unlock,
33041a18c49SAndy Shevchenko 	.flags			= IRQCHIP_IMMUTABLE,
33141a18c49SAndy Shevchenko 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
3320ba19cfcSBin Gao };
3330ba19cfcSBin Gao 
3340ba19cfcSBin Gao static irqreturn_t wcove_gpio_irq_handler(int irq, void *data)
3350ba19cfcSBin Gao {
3360ba19cfcSBin Gao 	struct wcove_gpio *wg = (struct wcove_gpio *)data;
3375a2a46aeSAndy Shevchenko 	unsigned int virq, gpio;
3382edba74cSAndy Shevchenko 	unsigned long pending;
3390ba19cfcSBin Gao 	u8 p[2];
3400ba19cfcSBin Gao 
3410ba19cfcSBin Gao 	if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
3420ba19cfcSBin Gao 		dev_err(wg->dev, "Failed to read irq status register\n");
3430ba19cfcSBin Gao 		return IRQ_NONE;
3440ba19cfcSBin Gao 	}
3450ba19cfcSBin Gao 
346881ebd22SKuppuswamy Sathyanarayanan 	pending = (p[0] & GPIO_IRQ0_MASK) | ((p[1] & GPIO_IRQ1_MASK) << 7);
3470ba19cfcSBin Gao 	if (!pending)
3480ba19cfcSBin Gao 		return IRQ_NONE;
3490ba19cfcSBin Gao 
3500ba19cfcSBin Gao 	/* Iterate until no interrupt is pending */
3510ba19cfcSBin Gao 	while (pending) {
3520ba19cfcSBin Gao 		/* One iteration is for all pending bits */
3532edba74cSAndy Shevchenko 		for_each_set_bit(gpio, &pending, WCOVE_GPIO_NUM) {
3545a2a46aeSAndy Shevchenko 			unsigned int mask, reg = to_ireg(gpio, IRQ_STATUS, &mask);
3555a2a46aeSAndy Shevchenko 
356f0fbe7bcSThierry Reding 			virq = irq_find_mapping(wg->chip.irq.domain, gpio);
3570ba19cfcSBin Gao 			handle_nested_irq(virq);
3585a2a46aeSAndy Shevchenko 			regmap_set_bits(wg->regmap, reg, mask);
3590ba19cfcSBin Gao 		}
3600ba19cfcSBin Gao 
3610ba19cfcSBin Gao 		/* Next iteration */
3620ba19cfcSBin Gao 		if (regmap_bulk_read(wg->regmap, IRQ_STATUS_BASE, p, 2)) {
3630ba19cfcSBin Gao 			dev_err(wg->dev, "Failed to read irq status\n");
3640ba19cfcSBin Gao 			break;
3650ba19cfcSBin Gao 		}
3660ba19cfcSBin Gao 
367881ebd22SKuppuswamy Sathyanarayanan 		pending = (p[0] & GPIO_IRQ0_MASK) | ((p[1] & GPIO_IRQ1_MASK) << 7);
3680ba19cfcSBin Gao 	}
3690ba19cfcSBin Gao 
3700ba19cfcSBin Gao 	return IRQ_HANDLED;
3710ba19cfcSBin Gao }
3720ba19cfcSBin Gao 
3732b71b66aSAndy Shevchenko static void wcove_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
3740ba19cfcSBin Gao {
3750ba19cfcSBin Gao 	unsigned int ctlo, ctli, irq_mask, irq_status;
3760ba19cfcSBin Gao 	struct wcove_gpio *wg = gpiochip_get_data(chip);
3775a2a46aeSAndy Shevchenko 	int gpio, mask, ret = 0;
3780ba19cfcSBin Gao 
3790ba19cfcSBin Gao 	for (gpio = 0; gpio < WCOVE_GPIO_NUM; gpio++) {
3800ba19cfcSBin Gao 		ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_OUT), &ctlo);
3810ba19cfcSBin Gao 		ret += regmap_read(wg->regmap, to_reg(gpio, CTRL_IN), &ctli);
3822b71b66aSAndy Shevchenko 		if (ret) {
3832b71b66aSAndy Shevchenko 			dev_err(wg->dev, "Failed to read registers: CTRL out/in\n");
3842b71b66aSAndy Shevchenko 			break;
3852b71b66aSAndy Shevchenko 		}
3862b71b66aSAndy Shevchenko 
3875a2a46aeSAndy Shevchenko 		ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_MASK, &mask), &irq_mask);
3885a2a46aeSAndy Shevchenko 		ret += regmap_read(wg->regmap, to_ireg(gpio, IRQ_STATUS, &mask), &irq_status);
3890ba19cfcSBin Gao 		if (ret) {
3902b71b66aSAndy Shevchenko 			dev_err(wg->dev, "Failed to read registers: IRQ status/mask\n");
3910ba19cfcSBin Gao 			break;
3920ba19cfcSBin Gao 		}
3930ba19cfcSBin Gao 
3940ba19cfcSBin Gao 		seq_printf(s, " gpio-%-2d %s %s %s %s ctlo=%2x,%s %s\n",
3950ba19cfcSBin Gao 			   gpio, ctlo & CTLO_DIR_OUT ? "out" : "in ",
3960ba19cfcSBin Gao 			   ctli & 0x1 ? "hi" : "lo",
3970ba19cfcSBin Gao 			   ctli & CTLI_INTCNT_NE ? "fall" : "    ",
3980ba19cfcSBin Gao 			   ctli & CTLI_INTCNT_PE ? "rise" : "    ",
3990ba19cfcSBin Gao 			   ctlo,
4005a2a46aeSAndy Shevchenko 			   irq_mask & mask ? "mask  " : "unmask",
4015a2a46aeSAndy Shevchenko 			   irq_status & mask ? "pending" : "       ");
4020ba19cfcSBin Gao 	}
4030ba19cfcSBin Gao }
4040ba19cfcSBin Gao 
4050ba19cfcSBin Gao static int wcove_gpio_probe(struct platform_device *pdev)
4060ba19cfcSBin Gao {
4070ba19cfcSBin Gao 	struct intel_soc_pmic *pmic;
4080ba19cfcSBin Gao 	struct wcove_gpio *wg;
4090ba19cfcSBin Gao 	int virq, ret, irq;
4100ba19cfcSBin Gao 	struct device *dev;
41122f61d4eSLinus Walleij 	struct gpio_irq_chip *girq;
4120ba19cfcSBin Gao 
4130ba19cfcSBin Gao 	/*
4140ba19cfcSBin Gao 	 * This gpio platform device is created by a mfd device (see
4150ba19cfcSBin Gao 	 * drivers/mfd/intel_soc_pmic_bxtwc.c for details). Information
4160ba19cfcSBin Gao 	 * shared by all sub-devices created by the mfd device, the regmap
4170ba19cfcSBin Gao 	 * pointer for instance, is stored as driver data of the mfd device
4180ba19cfcSBin Gao 	 * driver.
4190ba19cfcSBin Gao 	 */
4200ba19cfcSBin Gao 	pmic = dev_get_drvdata(pdev->dev.parent);
4210ba19cfcSBin Gao 	if (!pmic)
4220ba19cfcSBin Gao 		return -ENODEV;
4230ba19cfcSBin Gao 
4240ba19cfcSBin Gao 	irq = platform_get_irq(pdev, 0);
4250ba19cfcSBin Gao 	if (irq < 0)
4260ba19cfcSBin Gao 		return irq;
4270ba19cfcSBin Gao 
4280ba19cfcSBin Gao 	dev = &pdev->dev;
4290ba19cfcSBin Gao 
4300ba19cfcSBin Gao 	wg = devm_kzalloc(dev, sizeof(*wg), GFP_KERNEL);
4310ba19cfcSBin Gao 	if (!wg)
4320ba19cfcSBin Gao 		return -ENOMEM;
4330ba19cfcSBin Gao 
434a1d28c59SKuppuswamy Sathyanarayanan 	wg->regmap_irq_chip = pmic->irq_chip_data;
4350ba19cfcSBin Gao 
4360ba19cfcSBin Gao 	platform_set_drvdata(pdev, wg);
4370ba19cfcSBin Gao 
4380ba19cfcSBin Gao 	mutex_init(&wg->buslock);
4390ba19cfcSBin Gao 	wg->chip.label = KBUILD_MODNAME;
4400ba19cfcSBin Gao 	wg->chip.direction_input = wcove_gpio_dir_in;
4410ba19cfcSBin Gao 	wg->chip.direction_output = wcove_gpio_dir_out;
4427d9e59ceSBin Gao 	wg->chip.get_direction = wcove_gpio_get_direction;
4430ba19cfcSBin Gao 	wg->chip.get = wcove_gpio_get;
4440ba19cfcSBin Gao 	wg->chip.set = wcove_gpio_set;
445481a4209SZheng Yongjun 	wg->chip.set_config = wcove_gpio_set_config;
4460ba19cfcSBin Gao 	wg->chip.base = -1;
4470ba19cfcSBin Gao 	wg->chip.ngpio = WCOVE_VGPIO_NUM;
4480ba19cfcSBin Gao 	wg->chip.can_sleep = true;
4490ba19cfcSBin Gao 	wg->chip.parent = pdev->dev.parent;
4500ba19cfcSBin Gao 	wg->chip.dbg_show = wcove_gpio_dbg_show;
4510ba19cfcSBin Gao 	wg->dev = dev;
4520ba19cfcSBin Gao 	wg->regmap = pmic->regmap;
4530ba19cfcSBin Gao 
4540ba19cfcSBin Gao 	virq = regmap_irq_get_virq(wg->regmap_irq_chip, irq);
4550ba19cfcSBin Gao 	if (virq < 0) {
4560ba19cfcSBin Gao 		dev_err(dev, "Failed to get virq by irq %d\n", irq);
4570ba19cfcSBin Gao 		return virq;
4580ba19cfcSBin Gao 	}
4590ba19cfcSBin Gao 
46022f61d4eSLinus Walleij 	girq = &wg->chip.irq;
46141a18c49SAndy Shevchenko 	gpio_irq_chip_set_chip(girq, &wcove_irqchip);
46222f61d4eSLinus Walleij 	/* This will let us handle the parent IRQ in the driver */
46322f61d4eSLinus Walleij 	girq->parent_handler = NULL;
46422f61d4eSLinus Walleij 	girq->num_parents = 0;
46522f61d4eSLinus Walleij 	girq->parents = NULL;
46622f61d4eSLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
46722f61d4eSLinus Walleij 	girq->handler = handle_simple_irq;
46822f61d4eSLinus Walleij 	girq->threaded = true;
46922f61d4eSLinus Walleij 
47022cc4220SAndy Shevchenko 	ret = devm_request_threaded_irq(dev, virq, NULL, wcove_gpio_irq_handler,
47122cc4220SAndy Shevchenko 					IRQF_ONESHOT, pdev->name, wg);
47222cc4220SAndy Shevchenko 	if (ret) {
47322cc4220SAndy Shevchenko 		dev_err(dev, "Failed to request irq %d\n", virq);
47422cc4220SAndy Shevchenko 		return ret;
47522cc4220SAndy Shevchenko 	}
47622cc4220SAndy Shevchenko 
47722f61d4eSLinus Walleij 	ret = devm_gpiochip_add_data(dev, &wg->chip, wg);
47822f61d4eSLinus Walleij 	if (ret) {
47922f61d4eSLinus Walleij 		dev_err(dev, "Failed to add gpiochip: %d\n", ret);
48022f61d4eSLinus Walleij 		return ret;
48122f61d4eSLinus Walleij 	}
48235ca3f61SLinus Walleij 
483a1d28c59SKuppuswamy Sathyanarayanan 	/* Enable GPIO0 interrupts */
4849fe5fcd6SAndy Shevchenko 	ret = regmap_clear_bits(wg->regmap, IRQ_MASK_BASE + 0, GPIO_IRQ0_MASK);
485a1d28c59SKuppuswamy Sathyanarayanan 	if (ret)
486a1d28c59SKuppuswamy Sathyanarayanan 		return ret;
487a1d28c59SKuppuswamy Sathyanarayanan 
488a1d28c59SKuppuswamy Sathyanarayanan 	/* Enable GPIO1 interrupts */
4899fe5fcd6SAndy Shevchenko 	ret = regmap_clear_bits(wg->regmap, IRQ_MASK_BASE + 1, GPIO_IRQ1_MASK);
490a1d28c59SKuppuswamy Sathyanarayanan 	if (ret)
491a1d28c59SKuppuswamy Sathyanarayanan 		return ret;
492a1d28c59SKuppuswamy Sathyanarayanan 
4930ba19cfcSBin Gao 	return 0;
4940ba19cfcSBin Gao }
4950ba19cfcSBin Gao 
4960ba19cfcSBin Gao /*
4970ba19cfcSBin Gao  * Whiskey Cove PMIC itself is a analog device(but with digital control
4980ba19cfcSBin Gao  * interface) providing power management support for other devices in
4990ba19cfcSBin Gao  * the accompanied SoC, so we have no .pm for Whiskey Cove GPIO driver.
5000ba19cfcSBin Gao  */
5010ba19cfcSBin Gao static struct platform_driver wcove_gpio_driver = {
5020ba19cfcSBin Gao 	.driver = {
5030ba19cfcSBin Gao 		.name = "bxt_wcove_gpio",
5040ba19cfcSBin Gao 	},
5050ba19cfcSBin Gao 	.probe = wcove_gpio_probe,
5060ba19cfcSBin Gao };
5070ba19cfcSBin Gao 
5080ba19cfcSBin Gao module_platform_driver(wcove_gpio_driver);
5090ba19cfcSBin Gao 
5100ba19cfcSBin Gao MODULE_AUTHOR("Ajay Thomas <ajay.thomas.david.rajamanickam@intel.com>");
5110ba19cfcSBin Gao MODULE_AUTHOR("Bin Gao <bin.gao@intel.com>");
5120ba19cfcSBin Gao MODULE_DESCRIPTION("Intel Whiskey Cove GPIO Driver");
5130ba19cfcSBin Gao MODULE_LICENSE("GPL v2");
5140ba19cfcSBin Gao MODULE_ALIAS("platform:bxt_wcove_gpio");
515