^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 DA9055 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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/da9055/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/da9055/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct da9055_onkey {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct da9055 *da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct delayed_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void da9055_onkey_query(struct da9055_onkey *onkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int key_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) key_stat = da9055_reg_read(onkey->da9055, DA9055_REG_STATUS_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (key_stat < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) dev_err(onkey->da9055->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "Failed to read onkey event %d\n", key_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) key_stat &= DA9055_NOKEY_STS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Onkey status bit is cleared when onkey button is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!key_stat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) input_report_key(onkey->input, KEY_POWER, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) input_sync(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^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) * Interrupt is generated only when the ONKEY pin is asserted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Hence the deassertion of the pin is simulated through work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (key_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) schedule_delayed_work(&onkey->work, msecs_to_jiffies(10));
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void da9055_onkey_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct da9055_onkey *onkey = container_of(work, struct da9055_onkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) da9055_onkey_query(onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static irqreturn_t da9055_onkey_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct da9055_onkey *onkey = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) input_report_key(onkey->input, KEY_POWER, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) input_sync(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) da9055_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 da9055_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 da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct da9055_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 irq, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) irq = platform_get_irq_byname(pdev, "ONKEY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (irq < 0)
^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) onkey = devm_kzalloc(&pdev->dev, sizeof(*onkey), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!onkey) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dev_err(&pdev->dev, "Failed to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) dev_err(&pdev->dev, "Failed to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -ENOMEM;
^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) onkey->input = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) onkey->da9055 = da9055;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input_dev->name = "da9055-onkey";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) input_dev->phys = "da9055-onkey/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) input_dev->evbit[0] = BIT_MASK(EV_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __set_bit(KEY_POWER, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) INIT_DELAYED_WORK(&onkey->work, da9055_onkey_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) err = request_threaded_irq(irq, NULL, da9055_onkey_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "ONKEY", onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "Failed to register ONKEY IRQ %d, error = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) irq, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto err_free_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) err = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dev_err(&pdev->dev, "Unable to register input device, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) goto err_free_irq;
^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) platform_set_drvdata(pdev, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) free_irq(irq, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) cancel_delayed_work_sync(&onkey->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) err_free_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int da9055_onkey_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct da9055_onkey *onkey = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int irq = platform_get_irq_byname(pdev, "ONKEY");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) irq = regmap_irq_get_virq(onkey->da9055->irq_data, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) free_irq(irq, onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) cancel_delayed_work_sync(&onkey->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) input_unregister_device(onkey->input);
^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 da9055_onkey_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .probe = da9055_onkey_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .remove = da9055_onkey_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .name = "da9055-onkey",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) },
^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) module_platform_driver(da9055_onkey_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODULE_DESCRIPTION("Onkey driver for DA9055");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) MODULE_ALIAS("platform:da9055-onkey");