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