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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * GPIO driver for Analog Devices ADP5520 MFD PMICs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^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/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mfd/adp5520.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) struct adp5520_gpio {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct device *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	struct gpio_chip gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	unsigned char lut[ADP5520_MAXGPIOS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	unsigned long output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct adp5520_gpio *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	uint8_t reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	dev = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 * There are dedicated registers for GPIO IN/OUT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 * Make sure we return the right value, even when configured as output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (test_bit(off, &dev->output))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return !!(reg_val & dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static void adp5520_gpio_set_value(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		unsigned off, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct adp5520_gpio *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	dev = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct adp5520_gpio *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	dev = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	clear_bit(off, &dev->output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				dev->lut[off]);
^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 adp5520_gpio_direction_output(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		unsigned off, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct adp5520_gpio *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	dev = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	set_bit(off, &dev->output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 					dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 					dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					dev->lut[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return ret;
^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 adp5520_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct adp5520_gpio *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	int ret, i, gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned char ctl_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (pdata == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		dev_err(&pdev->dev, "missing platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (pdev->id != ID_ADP5520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	dev->master = pdev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (pdata->gpio_en_mask & (1 << i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			dev->lut[gpios++] = 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (gpios < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	gc = &dev->gpio_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	gc->direction_input  = adp5520_gpio_direction_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	gc->direction_output = adp5520_gpio_direction_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	gc->get = adp5520_gpio_get_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	gc->set = adp5520_gpio_set_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	gc->can_sleep = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	gc->base = pdata->gpio_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	gc->ngpio = gpios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	gc->label = pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	gc->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		pdata->gpio_en_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		ctl_mask |= ADP5520_C3_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ctl_mask |= ADP5520_R3_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (ctl_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			ctl_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		pdata->gpio_pullup_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		dev_err(&pdev->dev, "failed to write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		goto err;
^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) 	ret = devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	platform_set_drvdata(pdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct platform_driver adp5520_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		.name	= "adp5520-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.probe		= adp5520_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) module_platform_driver(adp5520_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) MODULE_DESCRIPTION("GPIO ADP5520 Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) MODULE_ALIAS("platform:adp5520-gpio");