^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Texas Instruments' TPS65217 and TPS65218 Power Button Input Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Felipe Balbi <balbi@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Marcin Niestroj <m.niestroj@grinn-global.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * it under the terms of the GNU General Public License version 2 as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This program is distributed "as is" WITHOUT ANY WARRANTY of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * kind, whether express or implied; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mfd/tps65217.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/mfd/tps65218.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct tps6521x_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int reg_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int pb_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static const struct tps6521x_data tps65217_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .reg_status = TPS65217_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .pb_mask = TPS65217_STATUS_PB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .name = "tps65217_pwrbutton",
^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) static const struct tps6521x_data tps65218_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .reg_status = TPS65218_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .pb_mask = TPS65218_STATUS_PB_STATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .name = "tps65218_pwrbutton",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct tps6521x_pwrbutton {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct input_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) const struct tps6521x_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static const struct of_device_id of_tps6521x_pb_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { .compatible = "ti,tps65217-pwrbutton", .data = &tps65217_data },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) { .compatible = "ti,tps65218-pwrbutton", .data = &tps65218_data },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) MODULE_DEVICE_TABLE(of, of_tps6521x_pb_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static irqreturn_t tps6521x_pb_irq(int irq, void *_pwr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct tps6521x_pwrbutton *pwr = _pwr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) const struct tps6521x_data *tps_data = pwr->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) error = regmap_read(pwr->regmap, tps_data->reg_status, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dev_err(pwr->dev, "can't read register: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^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) if (reg & tps_data->pb_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) input_report_key(pwr->idev, KEY_POWER, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pm_wakeup_event(pwr->dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) input_report_key(pwr->idev, KEY_POWER, 0);
^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) input_sync(pwr->idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int tps6521x_pb_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct tps6521x_pwrbutton *pwr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct input_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) const struct of_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) match = of_match_node(of_tps6521x_pb_match, dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!pwr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pwr->data = match->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) idev = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) idev->name = pwr->data->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) snprintf(pwr->phys, sizeof(pwr->phys), "%s/input0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pwr->data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) idev->phys = pwr->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) idev->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) idev->id.bustype = BUS_I2C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) input_set_capability(idev, EV_KEY, KEY_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pwr->regmap = dev_get_regmap(dev->parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pwr->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pwr->idev = idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) device_init_wakeup(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) error = devm_request_threaded_irq(dev, irq, NULL, tps6521x_pb_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) IRQF_TRIGGER_RISING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pwr->data->name, pwr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dev_err(dev, "failed to request IRQ #%d: %d\n", irq, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) error= input_register_device(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) dev_err(dev, "Can't register power button: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static const struct platform_device_id tps6521x_pwrbtn_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) { "tps65218-pwrbutton", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) { "tps65217-pwrbutton", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_DEVICE_TABLE(platform, tps6521x_pwrbtn_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static struct platform_driver tps6521x_pb_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .probe = tps6521x_pb_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .name = "tps6521x_pwrbutton",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .of_match_table = of_tps6521x_pb_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .id_table = tps6521x_pwrbtn_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) module_platform_driver(tps6521x_pb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_DESCRIPTION("TPS6521X Power Button");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");