^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Copyright (C) 2018 BayLibre SAS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // ONKEY driver for MAXIM 77650/77651 charger/power-supply.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mfd/max77650.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define MAX77650_ONKEY_MODE_MASK BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define MAX77650_ONKEY_MODE_PUSH 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define MAX77650_ONKEY_MODE_SLIDE BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct max77650_onkey {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static irqreturn_t max77650_onkey_falling(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct max77650_onkey *onkey = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) input_report_key(onkey->input, onkey->code, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) input_sync(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static irqreturn_t max77650_onkey_rising(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct max77650_onkey *onkey = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) input_report_key(onkey->input, onkey->code, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) input_sync(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int max77650_onkey_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int irq_r, irq_f, error, mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct max77650_onkey *onkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct device *dev, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct regmap *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) parent = dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) map = dev_get_regmap(parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) onkey = devm_kzalloc(dev, sizeof(*onkey), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!onkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) error = device_property_read_u32(dev, "linux,code", &onkey->code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) onkey->code = KEY_POWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (device_property_read_bool(dev, "maxim,onkey-slide")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) mode = MAX77650_ONKEY_MODE_SLIDE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) type = EV_SW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) mode = MAX77650_ONKEY_MODE_PUSH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) type = EV_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) error = regmap_update_bits(map, MAX77650_REG_CNFG_GLBL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) MAX77650_ONKEY_MODE_MASK, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) irq_f = platform_get_irq_byname(pdev, "nEN_F");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (irq_f < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return irq_f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) irq_r = platform_get_irq_byname(pdev, "nEN_R");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (irq_r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return irq_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) onkey->input = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!onkey->input)
^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) onkey->input->name = "max77650_onkey";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) onkey->input->phys = "max77650_onkey/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) onkey->input->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) input_set_capability(onkey->input, type, onkey->code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) error = devm_request_any_context_irq(dev, irq_f, max77650_onkey_falling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) IRQF_ONESHOT, "onkey-down", onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) error = devm_request_any_context_irq(dev, irq_r, max77650_onkey_rising,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) IRQF_ONESHOT, "onkey-up", onkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return input_register_device(onkey->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static const struct of_device_id max77650_onkey_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) { .compatible = "maxim,max77650-onkey" },
^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) MODULE_DEVICE_TABLE(of, max77650_onkey_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct platform_driver max77650_onkey_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .name = "max77650-onkey",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .of_match_table = max77650_onkey_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .probe = max77650_onkey_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) module_platform_driver(max77650_onkey_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_DESCRIPTION("MAXIM 77650/77651 ONKEY driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MODULE_ALIAS("platform:max77650-onkey");