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