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)  *  drivers/switch/switch_gpio.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2008 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Mike Lockwood <lockwood@android.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This software is licensed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * License version 2, as published by the Free Software Foundation, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * may be copied, distributed, and modified under those terms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * GNU General Public License for more details.
^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) 
^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/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/switch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) struct gpio_switch_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct switch_dev sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	unsigned gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	const char *name_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	const char *name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	const char *state_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	const char *state_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static void gpio_switch_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct gpio_switch_data	*data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		container_of(work, struct gpio_switch_data, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	state = gpio_get_value(data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	switch_set_state(&data->sdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct gpio_switch_data *switch_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	    (struct gpio_switch_data *)dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	schedule_work(&switch_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static ssize_t switch_gpio_print_state(struct switch_dev *sdev, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct gpio_switch_data	*switch_data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		container_of(sdev, struct gpio_switch_data, sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	const char *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (switch_get_state(sdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		state = switch_data->state_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		state = switch_data->state_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		return sprintf(buf, "%s\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int gpio_switch_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct gpio_switch_platform_data *pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct gpio_switch_data *switch_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	switch_data = kzalloc(sizeof(struct gpio_switch_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!switch_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	switch_data->sdev.name = pdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	switch_data->gpio = pdata->gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	switch_data->name_on = pdata->name_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	switch_data->name_off = pdata->name_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	switch_data->state_on = pdata->state_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	switch_data->state_off = pdata->state_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	switch_data->sdev.print_state = switch_gpio_print_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ret = switch_dev_register(&switch_data->sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		goto err_switch_dev_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	ret = gpio_request(switch_data->gpio, pdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto err_request_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ret = gpio_direction_input(switch_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		goto err_set_gpio_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	INIT_WORK(&switch_data->work, gpio_switch_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	switch_data->irq = gpio_to_irq(switch_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (switch_data->irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		ret = switch_data->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto err_detect_irq_num_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ret = request_irq(switch_data->irq, gpio_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			  IRQF_TRIGGER_LOW, pdev->name, switch_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto err_request_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Perform initial detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	gpio_switch_work(&switch_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err_request_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err_detect_irq_num_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err_set_gpio_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	gpio_free(switch_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) err_request_gpio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	switch_dev_unregister(&switch_data->sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) err_switch_dev_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	kfree(switch_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int gpio_switch_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct gpio_switch_data *switch_data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	cancel_work_sync(&switch_data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	gpio_free(switch_data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	switch_dev_unregister(&switch_data->sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	kfree(switch_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static struct platform_driver gpio_switch_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.probe		= gpio_switch_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.remove		= gpio_switch_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		.name	= "switch-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int __init gpio_switch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return platform_driver_register(&gpio_switch_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void __exit gpio_switch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	platform_driver_unregister(&gpio_switch_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_init(gpio_switch_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) module_exit(gpio_switch_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) MODULE_DESCRIPTION("GPIO Switch driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) MODULE_LICENSE("GPL");