xref: /linux/drivers/input/misc/mc13783-pwrbutton.c (revision 5298cc4cc753bbe4c530b41341834f6ef3344d0d)
130fc7ac3SPhilippe Rétornaz /**
230fc7ac3SPhilippe Rétornaz  * Copyright (C) 2011 Philippe Rétornaz
330fc7ac3SPhilippe Rétornaz  *
430fc7ac3SPhilippe Rétornaz  * Based on twl4030-pwrbutton driver by:
530fc7ac3SPhilippe Rétornaz  *     Peter De Schrijver <peter.de-schrijver@nokia.com>
630fc7ac3SPhilippe Rétornaz  *     Felipe Balbi <felipe.balbi@nokia.com>
730fc7ac3SPhilippe Rétornaz  *
830fc7ac3SPhilippe Rétornaz  * This file is subject to the terms and conditions of the GNU General
930fc7ac3SPhilippe Rétornaz  * Public License. See the file "COPYING" in the main directory of this
1030fc7ac3SPhilippe Rétornaz  * archive for more details.
1130fc7ac3SPhilippe Rétornaz  *
1230fc7ac3SPhilippe Rétornaz  * This program is distributed in the hope that it will be useful,
1330fc7ac3SPhilippe Rétornaz  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1430fc7ac3SPhilippe Rétornaz  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1530fc7ac3SPhilippe Rétornaz  * GNU General Public License for more details.
1630fc7ac3SPhilippe Rétornaz  *
1730fc7ac3SPhilippe Rétornaz  * You should have received a copy of the GNU General Public License
1830fc7ac3SPhilippe Rétornaz  * along with this program; if not, write to the Free Software
1930fc7ac3SPhilippe Rétornaz  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
2030fc7ac3SPhilippe Rétornaz  */
2130fc7ac3SPhilippe Rétornaz 
2230fc7ac3SPhilippe Rétornaz #include <linux/module.h>
2330fc7ac3SPhilippe Rétornaz #include <linux/init.h>
2430fc7ac3SPhilippe Rétornaz #include <linux/kernel.h>
2530fc7ac3SPhilippe Rétornaz #include <linux/errno.h>
2630fc7ac3SPhilippe Rétornaz #include <linux/input.h>
2730fc7ac3SPhilippe Rétornaz #include <linux/interrupt.h>
2830fc7ac3SPhilippe Rétornaz #include <linux/platform_device.h>
2930fc7ac3SPhilippe Rétornaz #include <linux/mfd/mc13783.h>
3030fc7ac3SPhilippe Rétornaz #include <linux/sched.h>
3130fc7ac3SPhilippe Rétornaz #include <linux/slab.h>
3230fc7ac3SPhilippe Rétornaz 
3330fc7ac3SPhilippe Rétornaz struct mc13783_pwrb {
3430fc7ac3SPhilippe Rétornaz 	struct input_dev *pwr;
3530fc7ac3SPhilippe Rétornaz 	struct mc13xxx *mc13783;
3630fc7ac3SPhilippe Rétornaz #define MC13783_PWRB_B1_POL_INVERT	(1 << 0)
3730fc7ac3SPhilippe Rétornaz #define MC13783_PWRB_B2_POL_INVERT	(1 << 1)
3830fc7ac3SPhilippe Rétornaz #define MC13783_PWRB_B3_POL_INVERT	(1 << 2)
3930fc7ac3SPhilippe Rétornaz 	int flags;
4030fc7ac3SPhilippe Rétornaz 	unsigned short keymap[3];
4130fc7ac3SPhilippe Rétornaz };
4230fc7ac3SPhilippe Rétornaz 
4330fc7ac3SPhilippe Rétornaz #define MC13783_REG_INTERRUPT_SENSE_1		5
4430fc7ac3SPhilippe Rétornaz #define MC13783_IRQSENSE1_ONOFD1S		(1 << 3)
4530fc7ac3SPhilippe Rétornaz #define MC13783_IRQSENSE1_ONOFD2S		(1 << 4)
4630fc7ac3SPhilippe Rétornaz #define MC13783_IRQSENSE1_ONOFD3S		(1 << 5)
4730fc7ac3SPhilippe Rétornaz 
4830fc7ac3SPhilippe Rétornaz #define MC13783_REG_POWER_CONTROL_2		15
4930fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON1BDBNC	4
5030fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON2BDBNC	6
5130fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON3BDBNC	8
5230fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON1BRSTEN	(1 << 1)
5330fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON2BRSTEN	(1 << 2)
5430fc7ac3SPhilippe Rétornaz #define MC13783_POWER_CONTROL_2_ON3BRSTEN	(1 << 3)
5530fc7ac3SPhilippe Rétornaz 
5630fc7ac3SPhilippe Rétornaz static irqreturn_t button_irq(int irq, void *_priv)
5730fc7ac3SPhilippe Rétornaz {
5830fc7ac3SPhilippe Rétornaz 	struct mc13783_pwrb *priv = _priv;
5930fc7ac3SPhilippe Rétornaz 	int val;
6030fc7ac3SPhilippe Rétornaz 
6130fc7ac3SPhilippe Rétornaz 	mc13xxx_irq_ack(priv->mc13783, irq);
6230fc7ac3SPhilippe Rétornaz 	mc13xxx_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val);
6330fc7ac3SPhilippe Rétornaz 
6430fc7ac3SPhilippe Rétornaz 	switch (irq) {
6530fc7ac3SPhilippe Rétornaz 	case MC13783_IRQ_ONOFD1:
6630fc7ac3SPhilippe Rétornaz 		val = val & MC13783_IRQSENSE1_ONOFD1S ? 1 : 0;
6730fc7ac3SPhilippe Rétornaz 		if (priv->flags & MC13783_PWRB_B1_POL_INVERT)
6830fc7ac3SPhilippe Rétornaz 			val ^= 1;
6930fc7ac3SPhilippe Rétornaz 		input_report_key(priv->pwr, priv->keymap[0], val);
7030fc7ac3SPhilippe Rétornaz 		break;
7130fc7ac3SPhilippe Rétornaz 
7230fc7ac3SPhilippe Rétornaz 	case MC13783_IRQ_ONOFD2:
7330fc7ac3SPhilippe Rétornaz 		val = val & MC13783_IRQSENSE1_ONOFD2S ? 1 : 0;
7430fc7ac3SPhilippe Rétornaz 		if (priv->flags & MC13783_PWRB_B2_POL_INVERT)
7530fc7ac3SPhilippe Rétornaz 			val ^= 1;
7630fc7ac3SPhilippe Rétornaz 		input_report_key(priv->pwr, priv->keymap[1], val);
7730fc7ac3SPhilippe Rétornaz 		break;
7830fc7ac3SPhilippe Rétornaz 
7930fc7ac3SPhilippe Rétornaz 	case MC13783_IRQ_ONOFD3:
8030fc7ac3SPhilippe Rétornaz 		val = val & MC13783_IRQSENSE1_ONOFD3S ? 1 : 0;
8130fc7ac3SPhilippe Rétornaz 		if (priv->flags & MC13783_PWRB_B3_POL_INVERT)
8230fc7ac3SPhilippe Rétornaz 			val ^= 1;
8330fc7ac3SPhilippe Rétornaz 		input_report_key(priv->pwr, priv->keymap[2], val);
8430fc7ac3SPhilippe Rétornaz 		break;
8530fc7ac3SPhilippe Rétornaz 	}
8630fc7ac3SPhilippe Rétornaz 
8730fc7ac3SPhilippe Rétornaz 	input_sync(priv->pwr);
8830fc7ac3SPhilippe Rétornaz 
8930fc7ac3SPhilippe Rétornaz 	return IRQ_HANDLED;
9030fc7ac3SPhilippe Rétornaz }
9130fc7ac3SPhilippe Rétornaz 
92*5298cc4cSBill Pemberton static int mc13783_pwrbutton_probe(struct platform_device *pdev)
9330fc7ac3SPhilippe Rétornaz {
9430fc7ac3SPhilippe Rétornaz 	const struct mc13xxx_buttons_platform_data *pdata;
9530fc7ac3SPhilippe Rétornaz 	struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
9630fc7ac3SPhilippe Rétornaz 	struct input_dev *pwr;
9730fc7ac3SPhilippe Rétornaz 	struct mc13783_pwrb *priv;
9830fc7ac3SPhilippe Rétornaz 	int err = 0;
9930fc7ac3SPhilippe Rétornaz 	int reg = 0;
10030fc7ac3SPhilippe Rétornaz 
10130fc7ac3SPhilippe Rétornaz 	pdata = dev_get_platdata(&pdev->dev);
10230fc7ac3SPhilippe Rétornaz 	if (!pdata) {
10330fc7ac3SPhilippe Rétornaz 		dev_err(&pdev->dev, "missing platform data\n");
10430fc7ac3SPhilippe Rétornaz 		return -ENODEV;
10530fc7ac3SPhilippe Rétornaz 	}
10630fc7ac3SPhilippe Rétornaz 
10730fc7ac3SPhilippe Rétornaz 	pwr = input_allocate_device();
10830fc7ac3SPhilippe Rétornaz 	if (!pwr) {
10930fc7ac3SPhilippe Rétornaz 		dev_dbg(&pdev->dev, "Can't allocate power button\n");
11030fc7ac3SPhilippe Rétornaz 		return -ENOMEM;
11130fc7ac3SPhilippe Rétornaz 	}
11230fc7ac3SPhilippe Rétornaz 
11330fc7ac3SPhilippe Rétornaz 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
11430fc7ac3SPhilippe Rétornaz 	if (!priv) {
11530fc7ac3SPhilippe Rétornaz 		err = -ENOMEM;
11630fc7ac3SPhilippe Rétornaz 		dev_dbg(&pdev->dev, "Can't allocate power button\n");
11730fc7ac3SPhilippe Rétornaz 		goto free_input_dev;
11830fc7ac3SPhilippe Rétornaz 	}
11930fc7ac3SPhilippe Rétornaz 
12030fc7ac3SPhilippe Rétornaz 	reg |= (pdata->b1on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON1BDBNC;
12130fc7ac3SPhilippe Rétornaz 	reg |= (pdata->b2on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON2BDBNC;
12230fc7ac3SPhilippe Rétornaz 	reg |= (pdata->b3on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON3BDBNC;
12330fc7ac3SPhilippe Rétornaz 
12430fc7ac3SPhilippe Rétornaz 	priv->pwr = pwr;
12530fc7ac3SPhilippe Rétornaz 	priv->mc13783 = mc13783;
12630fc7ac3SPhilippe Rétornaz 
12730fc7ac3SPhilippe Rétornaz 	mc13xxx_lock(mc13783);
12830fc7ac3SPhilippe Rétornaz 
12930fc7ac3SPhilippe Rétornaz 	if (pdata->b1on_flags & MC13783_BUTTON_ENABLE) {
13030fc7ac3SPhilippe Rétornaz 		priv->keymap[0] = pdata->b1on_key;
13130fc7ac3SPhilippe Rétornaz 		if (pdata->b1on_key != KEY_RESERVED)
13230fc7ac3SPhilippe Rétornaz 			__set_bit(pdata->b1on_key, pwr->keybit);
13330fc7ac3SPhilippe Rétornaz 
13430fc7ac3SPhilippe Rétornaz 		if (pdata->b1on_flags & MC13783_BUTTON_POL_INVERT)
13530fc7ac3SPhilippe Rétornaz 			priv->flags |= MC13783_PWRB_B1_POL_INVERT;
13630fc7ac3SPhilippe Rétornaz 
13730fc7ac3SPhilippe Rétornaz 		if (pdata->b1on_flags & MC13783_BUTTON_RESET_EN)
13830fc7ac3SPhilippe Rétornaz 			reg |= MC13783_POWER_CONTROL_2_ON1BRSTEN;
13930fc7ac3SPhilippe Rétornaz 
14030fc7ac3SPhilippe Rétornaz 		err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD1,
14130fc7ac3SPhilippe Rétornaz 					  button_irq, "b1on", priv);
14230fc7ac3SPhilippe Rétornaz 		if (err) {
14330fc7ac3SPhilippe Rétornaz 			dev_dbg(&pdev->dev, "Can't request irq\n");
14430fc7ac3SPhilippe Rétornaz 			goto free_priv;
14530fc7ac3SPhilippe Rétornaz 		}
14630fc7ac3SPhilippe Rétornaz 	}
14730fc7ac3SPhilippe Rétornaz 
14830fc7ac3SPhilippe Rétornaz 	if (pdata->b2on_flags & MC13783_BUTTON_ENABLE) {
14930fc7ac3SPhilippe Rétornaz 		priv->keymap[1] = pdata->b2on_key;
15030fc7ac3SPhilippe Rétornaz 		if (pdata->b2on_key != KEY_RESERVED)
15130fc7ac3SPhilippe Rétornaz 			__set_bit(pdata->b2on_key, pwr->keybit);
15230fc7ac3SPhilippe Rétornaz 
15330fc7ac3SPhilippe Rétornaz 		if (pdata->b2on_flags & MC13783_BUTTON_POL_INVERT)
15430fc7ac3SPhilippe Rétornaz 			priv->flags |= MC13783_PWRB_B2_POL_INVERT;
15530fc7ac3SPhilippe Rétornaz 
15630fc7ac3SPhilippe Rétornaz 		if (pdata->b2on_flags & MC13783_BUTTON_RESET_EN)
15730fc7ac3SPhilippe Rétornaz 			reg |= MC13783_POWER_CONTROL_2_ON2BRSTEN;
15830fc7ac3SPhilippe Rétornaz 
15930fc7ac3SPhilippe Rétornaz 		err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD2,
16030fc7ac3SPhilippe Rétornaz 					  button_irq, "b2on", priv);
16130fc7ac3SPhilippe Rétornaz 		if (err) {
16230fc7ac3SPhilippe Rétornaz 			dev_dbg(&pdev->dev, "Can't request irq\n");
16330fc7ac3SPhilippe Rétornaz 			goto free_irq_b1;
16430fc7ac3SPhilippe Rétornaz 		}
16530fc7ac3SPhilippe Rétornaz 	}
16630fc7ac3SPhilippe Rétornaz 
16730fc7ac3SPhilippe Rétornaz 	if (pdata->b3on_flags & MC13783_BUTTON_ENABLE) {
16830fc7ac3SPhilippe Rétornaz 		priv->keymap[2] = pdata->b3on_key;
16930fc7ac3SPhilippe Rétornaz 		if (pdata->b3on_key != KEY_RESERVED)
17030fc7ac3SPhilippe Rétornaz 			__set_bit(pdata->b3on_key, pwr->keybit);
17130fc7ac3SPhilippe Rétornaz 
17230fc7ac3SPhilippe Rétornaz 		if (pdata->b3on_flags & MC13783_BUTTON_POL_INVERT)
17330fc7ac3SPhilippe Rétornaz 			priv->flags |= MC13783_PWRB_B3_POL_INVERT;
17430fc7ac3SPhilippe Rétornaz 
17530fc7ac3SPhilippe Rétornaz 		if (pdata->b3on_flags & MC13783_BUTTON_RESET_EN)
17630fc7ac3SPhilippe Rétornaz 			reg |= MC13783_POWER_CONTROL_2_ON3BRSTEN;
17730fc7ac3SPhilippe Rétornaz 
17830fc7ac3SPhilippe Rétornaz 		err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD3,
17930fc7ac3SPhilippe Rétornaz 					  button_irq, "b3on", priv);
18030fc7ac3SPhilippe Rétornaz 		if (err) {
18130fc7ac3SPhilippe Rétornaz 			dev_dbg(&pdev->dev, "Can't request irq: %d\n", err);
18230fc7ac3SPhilippe Rétornaz 			goto free_irq_b2;
18330fc7ac3SPhilippe Rétornaz 		}
18430fc7ac3SPhilippe Rétornaz 	}
18530fc7ac3SPhilippe Rétornaz 
18630fc7ac3SPhilippe Rétornaz 	mc13xxx_reg_rmw(mc13783, MC13783_REG_POWER_CONTROL_2, 0x3FE, reg);
18730fc7ac3SPhilippe Rétornaz 
18830fc7ac3SPhilippe Rétornaz 	mc13xxx_unlock(mc13783);
18930fc7ac3SPhilippe Rétornaz 
19030fc7ac3SPhilippe Rétornaz 	pwr->name = "mc13783_pwrbutton";
19130fc7ac3SPhilippe Rétornaz 	pwr->phys = "mc13783_pwrbutton/input0";
19230fc7ac3SPhilippe Rétornaz 	pwr->dev.parent = &pdev->dev;
19330fc7ac3SPhilippe Rétornaz 
19430fc7ac3SPhilippe Rétornaz 	pwr->keycode = priv->keymap;
19530fc7ac3SPhilippe Rétornaz 	pwr->keycodemax = ARRAY_SIZE(priv->keymap);
19630fc7ac3SPhilippe Rétornaz 	pwr->keycodesize = sizeof(priv->keymap[0]);
19730fc7ac3SPhilippe Rétornaz 	__set_bit(EV_KEY, pwr->evbit);
19830fc7ac3SPhilippe Rétornaz 
19930fc7ac3SPhilippe Rétornaz 	err = input_register_device(pwr);
20030fc7ac3SPhilippe Rétornaz 	if (err) {
20130fc7ac3SPhilippe Rétornaz 		dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
20230fc7ac3SPhilippe Rétornaz 		goto free_irq;
20330fc7ac3SPhilippe Rétornaz 	}
20430fc7ac3SPhilippe Rétornaz 
20530fc7ac3SPhilippe Rétornaz 	platform_set_drvdata(pdev, priv);
20630fc7ac3SPhilippe Rétornaz 
20730fc7ac3SPhilippe Rétornaz 	return 0;
20830fc7ac3SPhilippe Rétornaz 
20930fc7ac3SPhilippe Rétornaz free_irq:
21030fc7ac3SPhilippe Rétornaz 	mc13xxx_lock(mc13783);
21130fc7ac3SPhilippe Rétornaz 
21230fc7ac3SPhilippe Rétornaz 	if (pdata->b3on_flags & MC13783_BUTTON_ENABLE)
21330fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD3, priv);
21430fc7ac3SPhilippe Rétornaz 
21530fc7ac3SPhilippe Rétornaz free_irq_b2:
21630fc7ac3SPhilippe Rétornaz 	if (pdata->b2on_flags & MC13783_BUTTON_ENABLE)
21730fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD2, priv);
21830fc7ac3SPhilippe Rétornaz 
21930fc7ac3SPhilippe Rétornaz free_irq_b1:
22030fc7ac3SPhilippe Rétornaz 	if (pdata->b1on_flags & MC13783_BUTTON_ENABLE)
22130fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD1, priv);
22230fc7ac3SPhilippe Rétornaz 
22330fc7ac3SPhilippe Rétornaz free_priv:
22430fc7ac3SPhilippe Rétornaz 	mc13xxx_unlock(mc13783);
22530fc7ac3SPhilippe Rétornaz 	kfree(priv);
22630fc7ac3SPhilippe Rétornaz 
22730fc7ac3SPhilippe Rétornaz free_input_dev:
22830fc7ac3SPhilippe Rétornaz 	input_free_device(pwr);
22930fc7ac3SPhilippe Rétornaz 
23030fc7ac3SPhilippe Rétornaz 	return err;
23130fc7ac3SPhilippe Rétornaz }
23230fc7ac3SPhilippe Rétornaz 
23330fc7ac3SPhilippe Rétornaz static int __devexit mc13783_pwrbutton_remove(struct platform_device *pdev)
23430fc7ac3SPhilippe Rétornaz {
23530fc7ac3SPhilippe Rétornaz 	struct mc13783_pwrb *priv = platform_get_drvdata(pdev);
23630fc7ac3SPhilippe Rétornaz 	const struct mc13xxx_buttons_platform_data *pdata;
23730fc7ac3SPhilippe Rétornaz 
23830fc7ac3SPhilippe Rétornaz 	pdata = dev_get_platdata(&pdev->dev);
23930fc7ac3SPhilippe Rétornaz 
24030fc7ac3SPhilippe Rétornaz 	mc13xxx_lock(priv->mc13783);
24130fc7ac3SPhilippe Rétornaz 
24230fc7ac3SPhilippe Rétornaz 	if (pdata->b3on_flags & MC13783_BUTTON_ENABLE)
24330fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD3, priv);
24430fc7ac3SPhilippe Rétornaz 	if (pdata->b2on_flags & MC13783_BUTTON_ENABLE)
24530fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD2, priv);
24630fc7ac3SPhilippe Rétornaz 	if (pdata->b1on_flags & MC13783_BUTTON_ENABLE)
24730fc7ac3SPhilippe Rétornaz 		mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD1, priv);
24830fc7ac3SPhilippe Rétornaz 
24930fc7ac3SPhilippe Rétornaz 	mc13xxx_unlock(priv->mc13783);
25030fc7ac3SPhilippe Rétornaz 
25130fc7ac3SPhilippe Rétornaz 	input_unregister_device(priv->pwr);
25230fc7ac3SPhilippe Rétornaz 	kfree(priv);
25330fc7ac3SPhilippe Rétornaz 	platform_set_drvdata(pdev, NULL);
25430fc7ac3SPhilippe Rétornaz 
25530fc7ac3SPhilippe Rétornaz 	return 0;
25630fc7ac3SPhilippe Rétornaz }
25730fc7ac3SPhilippe Rétornaz 
25818b2b181SAxel Lin static struct platform_driver mc13783_pwrbutton_driver = {
25930fc7ac3SPhilippe Rétornaz 	.probe		= mc13783_pwrbutton_probe,
2601cb0aa88SBill Pemberton 	.remove		= mc13783_pwrbutton_remove,
26130fc7ac3SPhilippe Rétornaz 	.driver		= {
26230fc7ac3SPhilippe Rétornaz 		.name	= "mc13783-pwrbutton",
26330fc7ac3SPhilippe Rétornaz 		.owner	= THIS_MODULE,
26430fc7ac3SPhilippe Rétornaz 	},
26530fc7ac3SPhilippe Rétornaz };
26630fc7ac3SPhilippe Rétornaz 
26718b2b181SAxel Lin module_platform_driver(mc13783_pwrbutton_driver);
26830fc7ac3SPhilippe Rétornaz 
26930fc7ac3SPhilippe Rétornaz MODULE_ALIAS("platform:mc13783-pwrbutton");
27030fc7ac3SPhilippe Rétornaz MODULE_DESCRIPTION("MC13783 Power Button");
27130fc7ac3SPhilippe Rétornaz MODULE_LICENSE("GPL v2");
27230fc7ac3SPhilippe Rétornaz MODULE_AUTHOR("Philippe Retornaz");
273