1a88638c4SRandy Dunlap /*
2104a5f3cSDmitry Torokhov * MAX8925 ONKEY driver
33734574cSHaojian Zhuang *
43734574cSHaojian Zhuang * Copyright (C) 2009 Marvell International Ltd.
53734574cSHaojian Zhuang * Haojian Zhuang <haojian.zhuang@marvell.com>
63734574cSHaojian Zhuang *
73734574cSHaojian Zhuang * This file is subject to the terms and conditions of the GNU General
83734574cSHaojian Zhuang * Public License. See the file "COPYING" in the main directory of this
93734574cSHaojian Zhuang * archive for more details.
103734574cSHaojian Zhuang *
113734574cSHaojian Zhuang * This program is distributed in the hope that it will be useful,
123734574cSHaojian Zhuang * but WITHOUT ANY WARRANTY; without even the implied warranty of
133734574cSHaojian Zhuang * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
143734574cSHaojian Zhuang * GNU General Public License for more details.
153734574cSHaojian Zhuang *
163734574cSHaojian Zhuang * You should have received a copy of the GNU General Public License
173734574cSHaojian Zhuang * along with this program; if not, write to the Free Software
183734574cSHaojian Zhuang * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
193734574cSHaojian Zhuang */
203734574cSHaojian Zhuang
213734574cSHaojian Zhuang #include <linux/kernel.h>
223734574cSHaojian Zhuang #include <linux/module.h>
233734574cSHaojian Zhuang #include <linux/platform_device.h>
243734574cSHaojian Zhuang #include <linux/i2c.h>
253734574cSHaojian Zhuang #include <linux/input.h>
263734574cSHaojian Zhuang #include <linux/interrupt.h>
273734574cSHaojian Zhuang #include <linux/mfd/max8925.h>
283734574cSHaojian Zhuang #include <linux/slab.h>
2904115e41SHimangi Saraogi #include <linux/device.h>
303734574cSHaojian Zhuang
312d95ae3bSHaojian Zhuang #define SW_INPUT (1 << 7) /* 0/1 -- up/down */
323734574cSHaojian Zhuang #define HARDRESET_EN (1 << 7)
333734574cSHaojian Zhuang #define PWREN_EN (1 << 7)
343734574cSHaojian Zhuang
353734574cSHaojian Zhuang struct max8925_onkey_info {
363734574cSHaojian Zhuang struct input_dev *idev;
373734574cSHaojian Zhuang struct i2c_client *i2c;
382d95ae3bSHaojian Zhuang struct device *dev;
39104a5f3cSDmitry Torokhov unsigned int irq[2];
403734574cSHaojian Zhuang };
413734574cSHaojian Zhuang
423734574cSHaojian Zhuang /*
432d95ae3bSHaojian Zhuang * MAX8925 gives us an interrupt when ONKEY is pressed or released.
443734574cSHaojian Zhuang * max8925_set_bits() operates I2C bus and may sleep. So implement
453734574cSHaojian Zhuang * it in thread IRQ handler.
463734574cSHaojian Zhuang */
max8925_onkey_handler(int irq,void * data)473734574cSHaojian Zhuang static irqreturn_t max8925_onkey_handler(int irq, void *data)
483734574cSHaojian Zhuang {
493734574cSHaojian Zhuang struct max8925_onkey_info *info = data;
50104a5f3cSDmitry Torokhov int state;
513734574cSHaojian Zhuang
52104a5f3cSDmitry Torokhov state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
53104a5f3cSDmitry Torokhov
54104a5f3cSDmitry Torokhov input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
553734574cSHaojian Zhuang input_sync(info->idev);
563734574cSHaojian Zhuang
57104a5f3cSDmitry Torokhov dev_dbg(info->dev, "onkey state:%d\n", state);
582d95ae3bSHaojian Zhuang
593734574cSHaojian Zhuang /* Enable hardreset to halt if system isn't shutdown on time */
603734574cSHaojian Zhuang max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
613734574cSHaojian Zhuang HARDRESET_EN, HARDRESET_EN);
623734574cSHaojian Zhuang
633734574cSHaojian Zhuang return IRQ_HANDLED;
643734574cSHaojian Zhuang }
653734574cSHaojian Zhuang
max8925_onkey_probe(struct platform_device * pdev)665298cc4cSBill Pemberton static int max8925_onkey_probe(struct platform_device *pdev)
673734574cSHaojian Zhuang {
683734574cSHaojian Zhuang struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
693734574cSHaojian Zhuang struct max8925_onkey_info *info;
70104a5f3cSDmitry Torokhov struct input_dev *input;
712d95ae3bSHaojian Zhuang int irq[2], error;
722d95ae3bSHaojian Zhuang
732d95ae3bSHaojian Zhuang irq[0] = platform_get_irq(pdev, 0);
740bec8b7eSStephen Boyd if (irq[0] < 0)
752d95ae3bSHaojian Zhuang return -EINVAL;
76104a5f3cSDmitry Torokhov
772d95ae3bSHaojian Zhuang irq[1] = platform_get_irq(pdev, 1);
780bec8b7eSStephen Boyd if (irq[1] < 0)
792d95ae3bSHaojian Zhuang return -EINVAL;
803734574cSHaojian Zhuang
8104115e41SHimangi Saraogi info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_onkey_info),
8204115e41SHimangi Saraogi GFP_KERNEL);
8304115e41SHimangi Saraogi if (!info)
8404115e41SHimangi Saraogi return -ENOMEM;
8504115e41SHimangi Saraogi
8604115e41SHimangi Saraogi input = devm_input_allocate_device(&pdev->dev);
8704115e41SHimangi Saraogi if (!input)
8804115e41SHimangi Saraogi return -ENOMEM;
893734574cSHaojian Zhuang
90104a5f3cSDmitry Torokhov info->idev = input;
913734574cSHaojian Zhuang info->i2c = chip->i2c;
922d95ae3bSHaojian Zhuang info->dev = &pdev->dev;
93104a5f3cSDmitry Torokhov info->irq[0] = irq[0];
94104a5f3cSDmitry Torokhov info->irq[1] = irq[1];
95104a5f3cSDmitry Torokhov
96104a5f3cSDmitry Torokhov input->name = "max8925_on";
97104a5f3cSDmitry Torokhov input->phys = "max8925_on/input0";
98104a5f3cSDmitry Torokhov input->id.bustype = BUS_I2C;
99104a5f3cSDmitry Torokhov input->dev.parent = &pdev->dev;
100104a5f3cSDmitry Torokhov input_set_capability(input, EV_KEY, KEY_POWER);
101104a5f3cSDmitry Torokhov
10204115e41SHimangi Saraogi error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL,
10304115e41SHimangi Saraogi max8925_onkey_handler, IRQF_ONESHOT,
10404115e41SHimangi Saraogi "onkey-down", info);
1052d95ae3bSHaojian Zhuang if (error < 0) {
1062d95ae3bSHaojian Zhuang dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
1072d95ae3bSHaojian Zhuang irq[0], error);
10804115e41SHimangi Saraogi return error;
1092d95ae3bSHaojian Zhuang }
110104a5f3cSDmitry Torokhov
11104115e41SHimangi Saraogi error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL,
11204115e41SHimangi Saraogi max8925_onkey_handler, IRQF_ONESHOT,
11304115e41SHimangi Saraogi "onkey-up", info);
1142d95ae3bSHaojian Zhuang if (error < 0) {
1152d95ae3bSHaojian Zhuang dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
1162d95ae3bSHaojian Zhuang irq[1], error);
11704115e41SHimangi Saraogi return error;
1182d95ae3bSHaojian Zhuang }
1193734574cSHaojian Zhuang
1203734574cSHaojian Zhuang error = input_register_device(info->idev);
1213734574cSHaojian Zhuang if (error) {
1223734574cSHaojian Zhuang dev_err(chip->dev, "Can't register input device: %d\n", error);
12304115e41SHimangi Saraogi return error;
1243734574cSHaojian Zhuang }
1253734574cSHaojian Zhuang
1263734574cSHaojian Zhuang platform_set_drvdata(pdev, info);
127104a5f3cSDmitry Torokhov device_init_wakeup(&pdev->dev, 1);
1283734574cSHaojian Zhuang
1293734574cSHaojian Zhuang return 0;
1303734574cSHaojian Zhuang }
1313734574cSHaojian Zhuang
max8925_onkey_suspend(struct device * dev)132*1bf94844SJonathan Cameron static int max8925_onkey_suspend(struct device *dev)
133adab30d7SKevin Liu {
134adab30d7SKevin Liu struct platform_device *pdev = to_platform_device(dev);
135adab30d7SKevin Liu struct max8925_onkey_info *info = platform_get_drvdata(pdev);
136adab30d7SKevin Liu struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
137adab30d7SKevin Liu
138adab30d7SKevin Liu if (device_may_wakeup(dev)) {
139104a5f3cSDmitry Torokhov chip->wakeup_flag |= 1 << info->irq[0];
140104a5f3cSDmitry Torokhov chip->wakeup_flag |= 1 << info->irq[1];
141adab30d7SKevin Liu }
142adab30d7SKevin Liu
143adab30d7SKevin Liu return 0;
144adab30d7SKevin Liu }
145adab30d7SKevin Liu
max8925_onkey_resume(struct device * dev)146*1bf94844SJonathan Cameron static int max8925_onkey_resume(struct device *dev)
147adab30d7SKevin Liu {
148adab30d7SKevin Liu struct platform_device *pdev = to_platform_device(dev);
149adab30d7SKevin Liu struct max8925_onkey_info *info = platform_get_drvdata(pdev);
150adab30d7SKevin Liu struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
151adab30d7SKevin Liu
152adab30d7SKevin Liu if (device_may_wakeup(dev)) {
153104a5f3cSDmitry Torokhov chip->wakeup_flag &= ~(1 << info->irq[0]);
154104a5f3cSDmitry Torokhov chip->wakeup_flag &= ~(1 << info->irq[1]);
155adab30d7SKevin Liu }
156adab30d7SKevin Liu
157adab30d7SKevin Liu return 0;
158adab30d7SKevin Liu }
159adab30d7SKevin Liu
160*1bf94844SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops,
161*1bf94844SJonathan Cameron max8925_onkey_suspend, max8925_onkey_resume);
162adab30d7SKevin Liu
1633734574cSHaojian Zhuang static struct platform_driver max8925_onkey_driver = {
1643734574cSHaojian Zhuang .driver = {
1653734574cSHaojian Zhuang .name = "max8925-onkey",
166*1bf94844SJonathan Cameron .pm = pm_sleep_ptr(&max8925_onkey_pm_ops),
1673734574cSHaojian Zhuang },
1683734574cSHaojian Zhuang .probe = max8925_onkey_probe,
1693734574cSHaojian Zhuang };
170840a746bSJJ Ding module_platform_driver(max8925_onkey_driver);
1713734574cSHaojian Zhuang
1723734574cSHaojian Zhuang MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
1733734574cSHaojian Zhuang MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1743734574cSHaojian Zhuang MODULE_LICENSE("GPL");
175