169854031SHaojian Zhuang /*
269854031SHaojian Zhuang  * 88pm860x_onkey.c - Marvell 88PM860x ONKEY driver
369854031SHaojian Zhuang  *
469854031SHaojian Zhuang  * Copyright (C) 2009-2010 Marvell International Ltd.
569854031SHaojian Zhuang  *      Haojian Zhuang <haojian.zhuang@marvell.com>
669854031SHaojian Zhuang  *
769854031SHaojian Zhuang  * This file is subject to the terms and conditions of the GNU General
869854031SHaojian Zhuang  * Public License. See the file "COPYING" in the main directory of this
969854031SHaojian Zhuang  * archive for more details.
1069854031SHaojian Zhuang  *
1169854031SHaojian Zhuang  * This program is distributed in the hope that it will be useful,
1269854031SHaojian Zhuang  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1369854031SHaojian Zhuang  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1469854031SHaojian Zhuang  * GNU General Public License for more details.
1569854031SHaojian Zhuang  *
1669854031SHaojian Zhuang  * You should have received a copy of the GNU General Public License
1769854031SHaojian Zhuang  * along with this program; if not, write to the Free Software
1869854031SHaojian Zhuang  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1969854031SHaojian Zhuang  */
2069854031SHaojian Zhuang 
2169854031SHaojian Zhuang #include <linux/kernel.h>
2269854031SHaojian Zhuang #include <linux/module.h>
2369854031SHaojian Zhuang #include <linux/platform_device.h>
2469854031SHaojian Zhuang #include <linux/i2c.h>
2569854031SHaojian Zhuang #include <linux/input.h>
2669854031SHaojian Zhuang #include <linux/interrupt.h>
2769854031SHaojian Zhuang #include <linux/mfd/88pm860x.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
2969854031SHaojian Zhuang 
3069854031SHaojian Zhuang #define PM8607_WAKEUP		0x0b
3169854031SHaojian Zhuang 
3269854031SHaojian Zhuang #define LONG_ONKEY_EN		(1 << 1)
3369854031SHaojian Zhuang #define ONKEY_STATUS		(1 << 0)
3469854031SHaojian Zhuang 
3569854031SHaojian Zhuang struct pm860x_onkey_info {
3669854031SHaojian Zhuang 	struct input_dev	*idev;
3769854031SHaojian Zhuang 	struct pm860x_chip	*chip;
3869854031SHaojian Zhuang 	struct i2c_client	*i2c;
3969854031SHaojian Zhuang 	struct device		*dev;
4069854031SHaojian Zhuang 	int			irq;
4169854031SHaojian Zhuang };
4269854031SHaojian Zhuang 
4369854031SHaojian Zhuang /* 88PM860x gives us an interrupt when ONKEY is held */
4469854031SHaojian Zhuang static irqreturn_t pm860x_onkey_handler(int irq, void *data)
4569854031SHaojian Zhuang {
4669854031SHaojian Zhuang 	struct pm860x_onkey_info *info = data;
4769854031SHaojian Zhuang 	int ret;
4869854031SHaojian Zhuang 
4969854031SHaojian Zhuang 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
5069854031SHaojian Zhuang 	ret &= ONKEY_STATUS;
5169854031SHaojian Zhuang 	input_report_key(info->idev, KEY_POWER, ret);
5269854031SHaojian Zhuang 	input_sync(info->idev);
5369854031SHaojian Zhuang 
5469854031SHaojian Zhuang 	/* Enable 8-second long onkey detection */
5569854031SHaojian Zhuang 	pm860x_set_bits(info->i2c, PM8607_WAKEUP, 3, LONG_ONKEY_EN);
5669854031SHaojian Zhuang 	return IRQ_HANDLED;
5769854031SHaojian Zhuang }
5869854031SHaojian Zhuang 
5969854031SHaojian Zhuang static int __devinit pm860x_onkey_probe(struct platform_device *pdev)
6069854031SHaojian Zhuang {
6169854031SHaojian Zhuang 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
6269854031SHaojian Zhuang 	struct pm860x_onkey_info *info;
6369854031SHaojian Zhuang 	int irq, ret;
6469854031SHaojian Zhuang 
6569854031SHaojian Zhuang 	irq = platform_get_irq(pdev, 0);
6669854031SHaojian Zhuang 	if (irq < 0) {
6769854031SHaojian Zhuang 		dev_err(&pdev->dev, "No IRQ resource!\n");
6869854031SHaojian Zhuang 		return -EINVAL;
6969854031SHaojian Zhuang 	}
7069854031SHaojian Zhuang 
7169854031SHaojian Zhuang 	info = kzalloc(sizeof(struct pm860x_onkey_info), GFP_KERNEL);
7269854031SHaojian Zhuang 	if (!info)
7369854031SHaojian Zhuang 		return -ENOMEM;
7469854031SHaojian Zhuang 	info->chip = chip;
7569854031SHaojian Zhuang 	info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
7669854031SHaojian Zhuang 	info->dev = &pdev->dev;
77c9f560b3SHaojian Zhuang 	info->irq = irq;
7869854031SHaojian Zhuang 
7969854031SHaojian Zhuang 	info->idev = input_allocate_device();
8069854031SHaojian Zhuang 	if (!info->idev) {
8169854031SHaojian Zhuang 		dev_err(chip->dev, "Failed to allocate input dev\n");
8269854031SHaojian Zhuang 		ret = -ENOMEM;
8369854031SHaojian Zhuang 		goto out;
8469854031SHaojian Zhuang 	}
8569854031SHaojian Zhuang 
8669854031SHaojian Zhuang 	info->idev->name = "88pm860x_on";
8769854031SHaojian Zhuang 	info->idev->phys = "88pm860x_on/input0";
8869854031SHaojian Zhuang 	info->idev->id.bustype = BUS_I2C;
8969854031SHaojian Zhuang 	info->idev->dev.parent = &pdev->dev;
9069854031SHaojian Zhuang 	info->idev->evbit[0] = BIT_MASK(EV_KEY);
9169854031SHaojian Zhuang 	info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
9269854031SHaojian Zhuang 
9369854031SHaojian Zhuang 	ret = input_register_device(info->idev);
9469854031SHaojian Zhuang 	if (ret) {
9569854031SHaojian Zhuang 		dev_err(chip->dev, "Can't register input device: %d\n", ret);
9669854031SHaojian Zhuang 		goto out_reg;
9769854031SHaojian Zhuang 	}
9869854031SHaojian Zhuang 
9969854031SHaojian Zhuang 	ret = request_threaded_irq(info->irq, NULL, pm860x_onkey_handler,
10069854031SHaojian Zhuang 				   IRQF_ONESHOT, "onkey", info);
10169854031SHaojian Zhuang 	if (ret < 0) {
10269854031SHaojian Zhuang 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
10369854031SHaojian Zhuang 			info->irq, ret);
10469854031SHaojian Zhuang 		goto out_irq;
10569854031SHaojian Zhuang 	}
10669854031SHaojian Zhuang 
10769854031SHaojian Zhuang 	platform_set_drvdata(pdev, info);
10869854031SHaojian Zhuang 	return 0;
10969854031SHaojian Zhuang 
11069854031SHaojian Zhuang out_irq:
11169854031SHaojian Zhuang 	input_unregister_device(info->idev);
11269854031SHaojian Zhuang 	kfree(info);
11369854031SHaojian Zhuang 	return ret;
11469854031SHaojian Zhuang 
11569854031SHaojian Zhuang out_reg:
11669854031SHaojian Zhuang 	input_free_device(info->idev);
11769854031SHaojian Zhuang out:
11869854031SHaojian Zhuang 	kfree(info);
11969854031SHaojian Zhuang 	return ret;
12069854031SHaojian Zhuang }
12169854031SHaojian Zhuang 
12269854031SHaojian Zhuang static int __devexit pm860x_onkey_remove(struct platform_device *pdev)
12369854031SHaojian Zhuang {
12469854031SHaojian Zhuang 	struct pm860x_onkey_info *info = platform_get_drvdata(pdev);
12569854031SHaojian Zhuang 
12669854031SHaojian Zhuang 	free_irq(info->irq, info);
12769854031SHaojian Zhuang 	input_unregister_device(info->idev);
12869854031SHaojian Zhuang 	kfree(info);
12969854031SHaojian Zhuang 	return 0;
13069854031SHaojian Zhuang }
13169854031SHaojian Zhuang 
13269854031SHaojian Zhuang static struct platform_driver pm860x_onkey_driver = {
13369854031SHaojian Zhuang 	.driver		= {
13469854031SHaojian Zhuang 		.name	= "88pm860x-onkey",
13569854031SHaojian Zhuang 		.owner	= THIS_MODULE,
13669854031SHaojian Zhuang 	},
13769854031SHaojian Zhuang 	.probe		= pm860x_onkey_probe,
13869854031SHaojian Zhuang 	.remove		= __devexit_p(pm860x_onkey_remove),
13969854031SHaojian Zhuang };
14069854031SHaojian Zhuang 
14169854031SHaojian Zhuang static int __init pm860x_onkey_init(void)
14269854031SHaojian Zhuang {
14369854031SHaojian Zhuang 	return platform_driver_register(&pm860x_onkey_driver);
14469854031SHaojian Zhuang }
14569854031SHaojian Zhuang module_init(pm860x_onkey_init);
14669854031SHaojian Zhuang 
14769854031SHaojian Zhuang static void __exit pm860x_onkey_exit(void)
14869854031SHaojian Zhuang {
14969854031SHaojian Zhuang 	platform_driver_unregister(&pm860x_onkey_driver);
15069854031SHaojian Zhuang }
15169854031SHaojian Zhuang module_exit(pm860x_onkey_exit);
15269854031SHaojian Zhuang 
15369854031SHaojian Zhuang MODULE_DESCRIPTION("Marvell 88PM860x ONKEY driver");
15469854031SHaojian Zhuang MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
15569854031SHaojian Zhuang MODULE_LICENSE("GPL");
156