max8925_onkey.c 5.2 KB
Newer Older
1
/**
2
 * MAX8925 ONKEY driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Copyright (C) 2009 Marvell International Ltd.
 *      Haojian Zhuang <haojian.zhuang@marvell.com>
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License. See the file "COPYING" in the main directory of this
 * archive for more details.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/mfd/max8925.h>
#include <linux/slab.h>

30
#define SW_INPUT		(1 << 7)	/* 0/1 -- up/down */
31 32 33 34 35 36
#define HARDRESET_EN		(1 << 7)
#define PWREN_EN		(1 << 7)

struct max8925_onkey_info {
	struct input_dev	*idev;
	struct i2c_client	*i2c;
37
	struct device		*dev;
38
	unsigned int		irq[2];
39 40 41
};

/*
42
 * MAX8925 gives us an interrupt when ONKEY is pressed or released.
43 44 45 46 47 48
 * max8925_set_bits() operates I2C bus and may sleep. So implement
 * it in thread IRQ handler.
 */
static irqreturn_t max8925_onkey_handler(int irq, void *data)
{
	struct max8925_onkey_info *info = data;
49 50 51 52 53
	int state;

	state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);

	input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
54 55
	input_sync(info->idev);

56
	dev_dbg(info->dev, "onkey state:%d\n", state);
57

58 59 60 61 62 63 64
	/* Enable hardreset to halt if system isn't shutdown on time */
	max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
			 HARDRESET_EN, HARDRESET_EN);

	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
65
static int max8925_onkey_probe(struct platform_device *pdev)
66 67 68
{
	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
	struct max8925_onkey_info *info;
69
	struct input_dev *input;
70 71 72 73 74 75 76
	int irq[2], error;

	irq[0] = platform_get_irq(pdev, 0);
	if (irq[0] < 0) {
		dev_err(&pdev->dev, "No IRQ resource!\n");
		return -EINVAL;
	}
77

78 79 80 81 82
	irq[1] = platform_get_irq(pdev, 1);
	if (irq[1] < 0) {
		dev_err(&pdev->dev, "No IRQ resource!\n");
		return -EINVAL;
	}
83 84

	info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
85 86 87 88 89
	input = input_allocate_device();
	if (!info || !input) {
		error = -ENOMEM;
		goto err_free_mem;
	}
90

91
	info->idev = input;
92
	info->i2c = chip->i2c;
93
	info->dev = &pdev->dev;
94 95 96 97 98 99 100 101 102
	info->irq[0] = irq[0];
	info->irq[1] = irq[1];

	input->name = "max8925_on";
	input->phys = "max8925_on/input0";
	input->id.bustype = BUS_I2C;
	input->dev.parent = &pdev->dev;
	input_set_capability(input, EV_KEY, KEY_POWER);

103 104 105 106 107
	error = request_threaded_irq(irq[0], NULL, max8925_onkey_handler,
				     IRQF_ONESHOT, "onkey-down", info);
	if (error < 0) {
		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
			irq[0], error);
108
		goto err_free_mem;
109
	}
110

111 112 113 114 115
	error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler,
				     IRQF_ONESHOT, "onkey-up", info);
	if (error < 0) {
		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
			irq[1], error);
116
		goto err_free_irq0;
117
	}
118 119 120 121

	error = input_register_device(info->idev);
	if (error) {
		dev_err(chip->dev, "Can't register input device: %d\n", error);
122
		goto err_free_irq1;
123 124 125
	}

	platform_set_drvdata(pdev, info);
126
	device_init_wakeup(&pdev->dev, 1);
127 128 129

	return 0;

130 131 132 133 134 135
err_free_irq1:
	free_irq(irq[1], info);
err_free_irq0:
	free_irq(irq[0], info);
err_free_mem:
	input_free_device(input);
136
	kfree(info);
137

138 139 140
	return error;
}

B
Bill Pemberton 已提交
141
static int max8925_onkey_remove(struct platform_device *pdev)
142 143
{
	struct max8925_onkey_info *info = platform_get_drvdata(pdev);
144
	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
145

146 147
	free_irq(info->irq[0] + chip->irq_base, info);
	free_irq(info->irq[1] + chip->irq_base, info);
148 149 150 151 152 153 154 155
	input_unregister_device(info->idev);
	kfree(info);

	platform_set_drvdata(pdev, NULL);

	return 0;
}

156 157 158 159 160 161 162 163
#ifdef CONFIG_PM_SLEEP
static int max8925_onkey_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct max8925_onkey_info *info = platform_get_drvdata(pdev);
	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);

	if (device_may_wakeup(dev)) {
164 165
		chip->wakeup_flag |= 1 << info->irq[0];
		chip->wakeup_flag |= 1 << info->irq[1];
166 167 168 169 170 171 172 173 174 175 176 177
	}

	return 0;
}

static int max8925_onkey_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct max8925_onkey_info *info = platform_get_drvdata(pdev);
	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);

	if (device_may_wakeup(dev)) {
178 179
		chip->wakeup_flag &= ~(1 << info->irq[0]);
		chip->wakeup_flag &= ~(1 << info->irq[1]);
180 181 182 183 184 185 186 187
	}

	return 0;
}
#endif

static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);

188 189 190 191
static struct platform_driver max8925_onkey_driver = {
	.driver		= {
		.name	= "max8925-onkey",
		.owner	= THIS_MODULE,
192
		.pm	= &max8925_onkey_pm_ops,
193 194
	},
	.probe		= max8925_onkey_probe,
B
Bill Pemberton 已提交
195
	.remove		= max8925_onkey_remove,
196
};
197
module_platform_driver(max8925_onkey_driver);
198 199 200 201

MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
MODULE_LICENSE("GPL");