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)  * Driver for PCA9570 I2C GPO expander
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2020 Sungbo Eo <mans0n@gorani.run>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on gpio-tpic2810.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *	Andrew F. Davis <afd@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * struct pca9570 - GPIO driver data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @chip: GPIO controller chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @lock: Protects write sequences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @out: Buffer for device register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct pca9570 {
^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 mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u8 out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static int pca9570_read(struct pca9570 *gpio, u8 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	ret = i2c_smbus_read_byte(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	*value = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return 0;
^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 int pca9570_write(struct pca9570 *gpio, u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct i2c_client *client = to_i2c_client(gpio->chip.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return i2c_smbus_write_byte(client, value);
^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 pca9570_get_direction(struct gpio_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				 unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* This device always output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return GPIO_LINE_DIRECTION_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int pca9570_get(struct gpio_chip *chip, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct pca9570 *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u8 buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ret = pca9570_read(gpio, &buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return !!(buffer & BIT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void pca9570_set(struct gpio_chip *chip, unsigned offset, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct pca9570 *gpio = gpiochip_get_data(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u8 buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	mutex_lock(&gpio->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	buffer = gpio->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		buffer |= BIT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		buffer &= ~BIT(offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ret = pca9570_write(gpio, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	gpio->out = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	mutex_unlock(&gpio->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int pca9570_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct pca9570 *gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	gpio->chip.label = client->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	gpio->chip.parent = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	gpio->chip.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	gpio->chip.get_direction = pca9570_get_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	gpio->chip.get = pca9570_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	gpio->chip.set = pca9570_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	gpio->chip.base = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	gpio->chip.ngpio = (uintptr_t)device_get_match_data(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	gpio->chip.can_sleep = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_init(&gpio->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* Read the current output level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	pca9570_read(gpio, &gpio->out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	i2c_set_clientdata(client, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const struct i2c_device_id pca9570_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{ "pca9570", 4 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DEVICE_TABLE(i2c, pca9570_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static const struct of_device_id pca9570_of_match_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	{ .compatible = "nxp,pca9570", .data = (void *)4 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) MODULE_DEVICE_TABLE(of, pca9570_of_match_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct i2c_driver pca9570_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.name = "pca9570",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		.of_match_table = pca9570_of_match_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	.probe_new = pca9570_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	.id_table = pca9570_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) module_i2c_driver(pca9570_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) MODULE_AUTHOR("Sungbo Eo <mans0n@gorani.run>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) MODULE_DESCRIPTION("GPIO expander driver for PCA9570");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) MODULE_LICENSE("GPL v2");