1 /* 2 * ON pin driver for Dialog DA9052 PMICs 3 * 4 * Copyright(c) 2012 Dialog Semiconductor Ltd. 5 * 6 * Author: David Dajun Chen <dchen@diasemi.com> 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2 of the License, or (at your 11 * option) any later version. 12 */ 13 14 #include <linux/init.h> 15 #include <linux/input.h> 16 #include <linux/module.h> 17 #include <linux/platform_device.h> 18 #include <linux/workqueue.h> 19 20 #include <linux/mfd/da9052/da9052.h> 21 #include <linux/mfd/da9052/reg.h> 22 23 struct da9052_onkey { 24 struct da9052 *da9052; 25 struct input_dev *input; 26 struct delayed_work work; 27 unsigned int irq; 28 }; 29 30 static void da9052_onkey_query(struct da9052_onkey *onkey) 31 { 32 int key_stat; 33 34 key_stat = da9052_reg_read(onkey->da9052, DA9052_EVENT_B_REG); 35 if (key_stat < 0) { 36 dev_err(onkey->da9052->dev, 37 "Failed to read onkey event %d\n", key_stat); 38 } else { 39 /* 40 * Since interrupt for deassertion of ONKEY pin is not 41 * generated, onkey event state determines the onkey 42 * button state. 43 */ 44 key_stat &= DA9052_EVENTB_ENONKEY; 45 input_report_key(onkey->input, KEY_POWER, key_stat); 46 input_sync(onkey->input); 47 } 48 49 /* 50 * Interrupt is generated only when the ONKEY pin is asserted. 51 * Hence the deassertion of the pin is simulated through work queue. 52 */ 53 if (key_stat) 54 schedule_delayed_work(&onkey->work, msecs_to_jiffies(50)); 55 } 56 57 static void da9052_onkey_work(struct work_struct *work) 58 { 59 struct da9052_onkey *onkey = container_of(work, struct da9052_onkey, 60 work.work); 61 62 da9052_onkey_query(onkey); 63 } 64 65 static irqreturn_t da9052_onkey_irq(int irq, void *data) 66 { 67 struct da9052_onkey *onkey = data; 68 69 da9052_onkey_query(onkey); 70 71 return IRQ_HANDLED; 72 } 73 74 static int __devinit da9052_onkey_probe(struct platform_device *pdev) 75 { 76 struct da9052 *da9052 = dev_get_drvdata(pdev->dev.parent); 77 struct da9052_onkey *onkey; 78 struct input_dev *input_dev; 79 int irq; 80 int error; 81 82 if (!da9052) { 83 dev_err(&pdev->dev, "Failed to get the driver's data\n"); 84 return -EINVAL; 85 } 86 87 irq = platform_get_irq_byname(pdev, "ONKEY"); 88 if (irq < 0) { 89 dev_err(&pdev->dev, 90 "Failed to get an IRQ for input device, %d\n", irq); 91 return -EINVAL; 92 } 93 94 onkey = kzalloc(sizeof(*onkey), GFP_KERNEL); 95 input_dev = input_allocate_device(); 96 if (!onkey || !input_dev) { 97 dev_err(&pdev->dev, "Failed to allocate memory\n"); 98 error = -ENOMEM; 99 goto err_free_mem; 100 } 101 102 onkey->input = input_dev; 103 onkey->da9052 = da9052; 104 onkey->irq = irq; 105 INIT_DELAYED_WORK(&onkey->work, da9052_onkey_work); 106 107 input_dev->name = "da9052-onkey"; 108 input_dev->phys = "da9052-onkey/input0"; 109 input_dev->dev.parent = &pdev->dev; 110 111 input_dev->evbit[0] = BIT_MASK(EV_KEY); 112 __set_bit(KEY_POWER, input_dev->keybit); 113 114 error = request_threaded_irq(onkey->irq, NULL, da9052_onkey_irq, 115 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 116 "ONKEY", onkey); 117 if (error < 0) { 118 dev_err(onkey->da9052->dev, 119 "Failed to register ONKEY IRQ %d, error = %d\n", 120 onkey->irq, error); 121 goto err_free_mem; 122 } 123 124 error = input_register_device(onkey->input); 125 if (error) { 126 dev_err(&pdev->dev, "Unable to register input device, %d\n", 127 error); 128 goto err_free_irq; 129 } 130 131 platform_set_drvdata(pdev, onkey); 132 return 0; 133 134 err_free_irq: 135 free_irq(onkey->irq, onkey); 136 cancel_delayed_work_sync(&onkey->work); 137 err_free_mem: 138 input_free_device(input_dev); 139 kfree(onkey); 140 141 return error; 142 } 143 144 static int __devexit da9052_onkey_remove(struct platform_device *pdev) 145 { 146 struct da9052_onkey *onkey = platform_get_drvdata(pdev); 147 148 free_irq(onkey->irq, onkey); 149 cancel_delayed_work_sync(&onkey->work); 150 151 input_unregister_device(onkey->input); 152 kfree(onkey); 153 154 return 0; 155 } 156 157 static struct platform_driver da9052_onkey_driver = { 158 .probe = da9052_onkey_probe, 159 .remove = __devexit_p(da9052_onkey_remove), 160 .driver = { 161 .name = "da9052-onkey", 162 .owner = THIS_MODULE, 163 }, 164 }; 165 module_platform_driver(da9052_onkey_driver); 166 167 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); 168 MODULE_DESCRIPTION("Onkey driver for DA9052"); 169 MODULE_LICENSE("GPL"); 170 MODULE_ALIAS("platform:da9052-onkey"); 171