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) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * GPIO driver for RICOH583 power management chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Laxman dewangan <ldewangan@nvidia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Based on code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Copyright (C) 2011 RICOH COMPANY,LTD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mfd/rc5t583.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct rc5t583_gpio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct gpio_chip gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct rc5t583 *rc5t583;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int rc5t583_gpio_get(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct device *parent = rc5t583_gpio->rc5t583->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	uint8_t val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	ret = rc5t583_read(parent, RC5T583_GPIO_MON_IOIN, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return !!(val & BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static void rc5t583_gpio_set(struct gpio_chip *gc, unsigned int offset, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct device *parent = rc5t583_gpio->rc5t583->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		rc5t583_set_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		rc5t583_clear_bits(parent, RC5T583_GPIO_IOOUT, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int rc5t583_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct device *parent = rc5t583_gpio->rc5t583->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ret = rc5t583_clear_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	/* Set pin to gpio mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int rc5t583_gpio_dir_output(struct gpio_chip *gc, unsigned offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct device *parent = rc5t583_gpio->rc5t583->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	rc5t583_gpio_set(gc, offset, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	ret = rc5t583_set_bits(parent, RC5T583_GPIO_IOSEL, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* Set pin to gpio mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return rc5t583_clear_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int rc5t583_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (offset < RC5T583_MAX_GPIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return rc5t583_gpio->rc5t583->irq_base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 				RC5T583_IRQ_GPIO0 + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return -EINVAL;
^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 void rc5t583_gpio_free(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct rc5t583_gpio *rc5t583_gpio = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct device *parent = rc5t583_gpio->rc5t583->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	rc5t583_set_bits(parent, RC5T583_GPIO_PGSEL, BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int rc5t583_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct rc5t583_gpio *rc5t583_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	rc5t583_gpio = devm_kzalloc(&pdev->dev, sizeof(*rc5t583_gpio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (!rc5t583_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	rc5t583_gpio->gpio_chip.label = "gpio-rc5t583",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	rc5t583_gpio->gpio_chip.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	rc5t583_gpio->gpio_chip.free = rc5t583_gpio_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	rc5t583_gpio->gpio_chip.direction_input = rc5t583_gpio_dir_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	rc5t583_gpio->gpio_chip.direction_output = rc5t583_gpio_dir_output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	rc5t583_gpio->gpio_chip.set = rc5t583_gpio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	rc5t583_gpio->gpio_chip.get = rc5t583_gpio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	rc5t583_gpio->gpio_chip.to_irq = rc5t583_gpio_to_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	rc5t583_gpio->gpio_chip.ngpio = RC5T583_MAX_GPIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	rc5t583_gpio->gpio_chip.can_sleep = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	rc5t583_gpio->gpio_chip.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	rc5t583_gpio->gpio_chip.base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	rc5t583_gpio->rc5t583 = rc5t583;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (pdata && pdata->gpio_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		rc5t583_gpio->gpio_chip.base = pdata->gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	platform_set_drvdata(pdev, rc5t583_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return devm_gpiochip_add_data(&pdev->dev, &rc5t583_gpio->gpio_chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				      rc5t583_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct platform_driver rc5t583_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.name    = "rc5t583-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.probe		= rc5t583_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int __init rc5t583_gpio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return platform_driver_register(&rc5t583_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) subsys_initcall(rc5t583_gpio_init);