Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	Keerthy <j-keerthy@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * modify it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * kind, whether expressed or implied; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * GNU General Public License version 2 for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Based on the LP873X driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/mfd/lp87565.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct lp87565_gpio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct gpio_chip chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct regmap *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static int lp87565_gpio_get(struct gpio_chip *chip, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct lp87565_gpio *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int ret, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	ret = regmap_read(gpio->map, LP87565_REG_GPIO_IN, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return !!(val & BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static void lp87565_gpio_set(struct gpio_chip *chip, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			     int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct lp87565_gpio *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	regmap_update_bits(gpio->map, LP87565_REG_GPIO_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			   BIT(offset), value ? BIT(offset) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int lp87565_gpio_get_direction(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				      unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct lp87565_gpio *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int ret, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	ret = regmap_read(gpio->map, LP87565_REG_GPIO_CONFIG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (val & BIT(offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return GPIO_LINE_DIRECTION_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return GPIO_LINE_DIRECTION_IN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static int lp87565_gpio_direction_input(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 					unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct lp87565_gpio *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return regmap_update_bits(gpio->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				  LP87565_REG_GPIO_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				  BIT(offset), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static int lp87565_gpio_direction_output(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 					 unsigned int offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct lp87565_gpio *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	lp87565_gpio_set(chip, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return regmap_update_bits(gpio->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				  LP87565_REG_GPIO_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				  BIT(offset), BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int lp87565_gpio_request(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct lp87565_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	switch (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		 * MUX can program the pin to be in EN1/2/3 pin mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		 * Or GPIO1/2/3 mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 * Setup the GPIO*_SEL MUX to GPIO mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		ret = regmap_update_bits(gpio->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 					 LP87565_REG_PIN_FUNCTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 					 BIT(offset), BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int lp87565_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				   unsigned long config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct lp87565_gpio *gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	switch (pinconf_to_config_param(config)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return regmap_update_bits(gpio->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 					  LP87565_REG_GPIO_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 					  BIT(offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					      __ffs(LP87565_GOIO1_OD)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 					  BIT(offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 					      __ffs(LP87565_GOIO1_OD)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	case PIN_CONFIG_DRIVE_PUSH_PULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return regmap_update_bits(gpio->map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 					  LP87565_REG_GPIO_CONFIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 					  BIT(offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 					      __ffs(LP87565_GOIO1_OD)), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return -ENOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static const struct gpio_chip template_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	.label			= "lp87565-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	.owner			= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	.request		= lp87565_gpio_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	.get_direction		= lp87565_gpio_get_direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	.direction_input	= lp87565_gpio_direction_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.direction_output	= lp87565_gpio_direction_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.get			= lp87565_gpio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.set			= lp87565_gpio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	.set_config		= lp87565_gpio_set_config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.base			= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.ngpio			= 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.can_sleep		= true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int lp87565_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct lp87565_gpio *gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct lp87565 *lp87565;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (!gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	lp87565 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	gpio->chip = template_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	gpio->chip.parent = lp87565->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	gpio->map = lp87565->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static const struct platform_device_id lp87565_gpio_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	{ "lp87565-q1-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_DEVICE_TABLE(platform, lp87565_gpio_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static struct platform_driver lp87565_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		.name = "lp87565-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.probe = lp87565_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.id_table = lp87565_gpio_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) module_platform_driver(lp87565_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) MODULE_DESCRIPTION("LP87565 GPIO driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) MODULE_LICENSE("GPL v2");