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)  * ON pin driver for Dialog DA9052 PMICs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright(c) 2012 Dialog Semiconductor Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: David Dajun Chen <dchen@diasemi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.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/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mfd/da9052/da9052.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/da9052/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) struct da9052_onkey {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	struct da9052 *da9052;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct delayed_work work;
^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 void da9052_onkey_query(struct da9052_onkey *onkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	ret = da9052_reg_read(onkey->da9052, DA9052_STATUS_A_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		dev_err(onkey->da9052->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			"Failed to read onkey event err=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		 * Since interrupt for deassertion of ONKEY pin is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		 * generated, onkey event state determines the onkey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		 * button state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		bool pressed = !(ret & DA9052_STATUSA_NONKEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		input_report_key(onkey->input, KEY_POWER, pressed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		input_sync(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		 * Interrupt is generated only when the ONKEY pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		 * is asserted.  Hence the deassertion of the pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		 * is simulated through work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (pressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			schedule_delayed_work(&onkey->work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 						msecs_to_jiffies(50));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void da9052_onkey_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct da9052_onkey *onkey = container_of(work, struct da9052_onkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 						  work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	da9052_onkey_query(onkey);
^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 irqreturn_t da9052_onkey_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct da9052_onkey *onkey = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	da9052_onkey_query(onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int da9052_onkey_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct da9052_onkey *onkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!da9052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		dev_err(&pdev->dev, "Failed to get the driver's data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	onkey = kzalloc(sizeof(*onkey), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!onkey || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		dev_err(&pdev->dev, "Failed to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	onkey->input = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	onkey->da9052 = da9052;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	input_dev->name = "da9052-onkey";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	input_dev->phys = "da9052-onkey/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	input_dev->evbit[0] = BIT_MASK(EV_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	__set_bit(KEY_POWER, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	error = da9052_request_irq(onkey->da9052, DA9052_IRQ_NONKEY, "ONKEY",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			    da9052_onkey_irq, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		dev_err(onkey->da9052->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			"Failed to register ONKEY IRQ: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	error = input_register_device(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		dev_err(&pdev->dev, "Unable to register input device, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		goto err_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	platform_set_drvdata(pdev, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	cancel_delayed_work_sync(&onkey->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	kfree(onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return error;
^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 int da9052_onkey_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct da9052_onkey *onkey = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	da9052_free_irq(onkey->da9052, DA9052_IRQ_NONKEY, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	cancel_delayed_work_sync(&onkey->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	input_unregister_device(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	kfree(onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static struct platform_driver da9052_onkey_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	.probe	= da9052_onkey_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.remove	= da9052_onkey_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		.name	= "da9052-onkey",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) module_platform_driver(da9052_onkey_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_DESCRIPTION("Onkey driver for DA9052");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) MODULE_ALIAS("platform:da9052-onkey");