1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ON pin driver for Dialog DA9055 PMICs 4 * 5 * Copyright(c) 2012 Dialog Semiconductor Ltd. 6 * 7 * Author: David Dajun Chen <dchen@diasemi.com> 8 */ 9 10 #include <linux/input.h> 11 #include <linux/module.h> 12 #include <linux/platform_device.h> 13 14 #include <linux/mfd/da9055/core.h> 15 #include <linux/mfd/da9055/reg.h> 16 17 struct da9055_onkey { 18 struct da9055 *da9055; 19 struct input_dev *input; 20 struct delayed_work work; 21 }; 22 23 static void da9055_onkey_query(struct da9055_onkey *onkey) 24 { 25 int key_stat; 26 27 key_stat = da9055_reg_read(onkey->da9055, DA9055_REG_STATUS_A); 28 if (key_stat < 0) { 29 dev_err(onkey->da9055->dev, 30 "Failed to read onkey event %d\n", key_stat); 31 } else { 32 key_stat &= DA9055_NOKEY_STS; 33 /* 34 * Onkey status bit is cleared when onkey button is released. 35 */ 36 if (!key_stat) { 37 input_report_key(onkey->input, KEY_POWER, 0); 38 input_sync(onkey->input); 39 } 40 } 41 42 /* 43 * Interrupt is generated only when the ONKEY pin is asserted. 44 * Hence the deassertion of the pin is simulated through work queue. 45 */ 46 if (key_stat) 47 schedule_delayed_work(&onkey->work, msecs_to_jiffies(10)); 48 49 } 50 51 static void da9055_onkey_work(struct work_struct *work) 52 { 53 struct da9055_onkey *onkey = container_of(work, struct da9055_onkey, 54 work.work); 55 56 da9055_onkey_query(onkey); 57 } 58 59 static irqreturn_t da9055_onkey_irq(int irq, void *data) 60 { 61 struct da9055_onkey *onkey = data; 62 63 input_report_key(onkey->input, KEY_POWER, 1); 64 input_sync(onkey->input); 65 66 da9055_onkey_query(onkey); 67 68 return IRQ_HANDLED; 69 } 70 71 static int da9055_onkey_probe(struct platform_device *pdev) 72 { 73 struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent); 74 struct da9055_onkey *onkey; 75 struct input_dev *input_dev; 76 int irq, err; 77 78 irq = platform_get_irq_byname(pdev, "ONKEY"); 79 if (irq < 0) { 80 dev_err(&pdev->dev, 81 "Failed to get an IRQ for input device, %d\n", irq); 82 return -EINVAL; 83 } 84 85 onkey = devm_kzalloc(&pdev->dev, sizeof(*onkey), GFP_KERNEL); 86 if (!onkey) { 87 dev_err(&pdev->dev, "Failed to allocate memory\n"); 88 return -ENOMEM; 89 } 90 91 input_dev = input_allocate_device(); 92 if (!input_dev) { 93 dev_err(&pdev->dev, "Failed to allocate memory\n"); 94 return -ENOMEM; 95 } 96 97 onkey->input = input_dev; 98 onkey->da9055 = da9055; 99 input_dev->name = "da9055-onkey"; 100 input_dev->phys = "da9055-onkey/input0"; 101 input_dev->dev.parent = &pdev->dev; 102 103 input_dev->evbit[0] = BIT_MASK(EV_KEY); 104 __set_bit(KEY_POWER, input_dev->keybit); 105 106 INIT_DELAYED_WORK(&onkey->work, da9055_onkey_work); 107 108 err = request_threaded_irq(irq, NULL, da9055_onkey_irq, 109 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, 110 "ONKEY", onkey); 111 if (err < 0) { 112 dev_err(&pdev->dev, 113 "Failed to register ONKEY IRQ %d, error = %d\n", 114 irq, err); 115 goto err_free_input; 116 } 117 118 err = input_register_device(input_dev); 119 if (err) { 120 dev_err(&pdev->dev, "Unable to register input device, %d\n", 121 err); 122 goto err_free_irq; 123 } 124 125 platform_set_drvdata(pdev, onkey); 126 127 return 0; 128 129 err_free_irq: 130 free_irq(irq, onkey); 131 cancel_delayed_work_sync(&onkey->work); 132 err_free_input: 133 input_free_device(input_dev); 134 135 return err; 136 } 137 138 static int da9055_onkey_remove(struct platform_device *pdev) 139 { 140 struct da9055_onkey *onkey = platform_get_drvdata(pdev); 141 int irq = platform_get_irq_byname(pdev, "ONKEY"); 142 143 irq = regmap_irq_get_virq(onkey->da9055->irq_data, irq); 144 free_irq(irq, onkey); 145 cancel_delayed_work_sync(&onkey->work); 146 input_unregister_device(onkey->input); 147 148 return 0; 149 } 150 151 static struct platform_driver da9055_onkey_driver = { 152 .probe = da9055_onkey_probe, 153 .remove = da9055_onkey_remove, 154 .driver = { 155 .name = "da9055-onkey", 156 }, 157 }; 158 159 module_platform_driver(da9055_onkey_driver); 160 161 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); 162 MODULE_DESCRIPTION("Onkey driver for DA9055"); 163 MODULE_LICENSE("GPL"); 164 MODULE_ALIAS("platform:da9055-onkey"); 165