aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/sfh7741.c
blob: 6664365f4741a71d55b6bdb5b0faa5641f228fae (plain)
1
2
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * SFH7741 Proximity Driver
 *
 * Copyright (C) 2010 Texas Instruments
 *
 * Author: Shubhrajyoti D <shubhrajyoti@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * 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/interrupt.h>
#include <linux/pm.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/input/sfh7741.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#define SFH7741_PROX_ON	1
#define SFH7741_PROX_OFF	0

#define SFH7741_SUSPEND_RESUME

#define SFH7741_POLL_RATE 250

struct sfh7741_drvdata {
	struct input_dev *input;
	struct delayed_work input_work;
	int irq;
	int prox_enable;
	int on_before_suspend;
	int req_poll_rate;
	/* mutex for sysfs operations */
	struct mutex lock;
	struct sfh7741_platform_data *pdata;
	void (*activate_func)(int state);
	int (*read_prox)(void);
};

static irqreturn_t sfh7741_isr(int irq, void *dev_id)
{
	struct sfh7741_drvdata *ddata = dev_id;
	int proximity;

	proximity = ddata->read_prox();
	input_report_abs(ddata->input, ABS_DISTANCE, proximity);
	input_sync(ddata->input);

	return IRQ_HANDLED;
}

static ssize_t set_prox_state(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	int state = SFH7741_PROX_OFF;
	struct platform_device *pdev = to_platform_device(dev);
	struct sfh7741_drvdata *ddata = platform_get_drvdata(pdev);

	if (sscanf(buf, "%u", &state) != 1)
		return -EINVAL;

	if (state != SFH7741_PROX_OFF)
		state = SFH7741_PROX_ON;

	mutex_lock(&ddata->lock);
	if (state != ddata->prox_enable) {
		if (state) {
			if (ddata->irq) {
				enable_irq(ddata->irq);
				if (ddata->pdata->flags & SFH7741_WAKEABLE_INT)
					enable_irq_wake(ddata->irq);
			} else {
				ddata->req_poll_rate = SFH7741_POLL_RATE;
				schedule_delayed_work(&ddata->input_work, 0);
			}
		} else {
			if (ddata->irq) {
				disable_irq_nosync(ddata->irq);
				if (ddata->pdata->flags & SFH7741_WAKEABLE_INT)
					disable_irq_wake(ddata->irq);
			} else {
				cancel_delayed_work_sync(&ddata->input_work);
				ddata->req_poll_rate = 0;
			}
		}

		ddata->activate_func(state);
		ddata->prox_enable = state;
	}

	mutex_unlock(&ddata->lock);
	return strnlen(buf, count);
}


static void sfh7741_work(struct work_struct *work)
{
	struct sfh7741_drvdata *ddata = container_of((struct delayed_work *)work,
						  struct sfh7741_drvdata,
						  input_work);
	int proximity;

	proximity = ddata->read_prox();
	input_report_abs(ddata->input, ABS_DISTANCE, proximity);
	input_sync(ddata->input);
	if (!ddata->irq)
		schedule_delayed_work(&ddata->input_work,
				msecs_to_jiffies(ddata->req_poll_rate));
}

static ssize_t show_prox_state(struct device *dev,
			struct device_attribute *attr,
			char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct sfh7741_drvdata *ddata = platform_get_drvdata(pdev);
	return sprintf(buf, "%u\n", ddata->prox_enable);
}
static DEVICE_ATTR(state, S_IWUSR | S_IRUGO, show_prox_state, set_prox_state);


static struct attribute *sfh7741_attributes[] = {
	&dev_attr_state.attr,
	NULL
};

static const struct attribute_group sfh7741_attr_group = {
	.attrs = sfh7741_attributes,
};

static int __devinit sfh7741_probe(struct platform_device *pdev)
{
	struct sfh7741_drvdata *ddata;
	struct input_dev *input;
	struct device *dev = &pdev->dev;
	int  error;

	pr_info("%s: Proximity sensor\n", __func__);

	if (pdev->dev.platform_data == NULL) {
		pr_err("%s: Platform data is NULL\n", __func__);
		return -EINVAL;
	}

	ddata = kzalloc(sizeof(struct sfh7741_drvdata),
			GFP_KERNEL);
	if (!ddata) {
		pr_err("%s:Could not allocate memory for Proximity\n",
			__func__);
		return -ENOMEM;
	}

	ddata->pdata = pdev->dev.platform_data;
	if (!ddata->pdata->activate_func ||
	    !ddata->pdata->read_prox) {
		pr_err("%s:Read Prox or Activate is NULL\n", __func__);
		error = -EINVAL;
		goto fail0;
	}

	input = input_allocate_device();
	if (!input) {
		pr_err("%s: Failed to allocate input device\n", __func__);
		error = -ENOMEM;
		goto fail0;
	}

	input->name = pdev->name;
	input->phys = "sfh7741/input0";
	input->dev.parent = &pdev->dev;

	input->id.bustype = BUS_HOST;
	ddata->irq = ddata->pdata->irq;
	ddata->prox_enable = ddata->pdata->prox_enable;
	ddata->req_poll_rate = 0;
	ddata->activate_func = ddata->pdata->activate_func;
	ddata->read_prox =  ddata->pdata->read_prox;

	ddata->input = input;
	__set_bit(EV_ABS, input->evbit);

	input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);

	error = input_register_device(input);
	if (error) {
		pr_err("%s: Unable to register input device,error: %d\n",
				__func__, error);
		goto fail1;
	}

	if (ddata->irq) {
		error = request_threaded_irq(ddata->irq , NULL ,
					sfh7741_isr,
					ddata->pdata->irq_flags,
					"sfh7741_irq", ddata);
		if (error) {
			pr_err("%s: Unable to claim irq %d; error %d\n",
				__func__, ddata->pdata->irq, error);
			goto fail2;
		}
	} else {
		INIT_DELAYED_WORK(&ddata->input_work, sfh7741_work);
	}

	mutex_init(&ddata->lock);

	platform_set_drvdata(pdev, ddata);

	error = sysfs_create_group(&dev->kobj, &sfh7741_attr_group);
	if (error) {
		pr_err("%s: Failed to create sysfs entries\n", __func__);
		mutex_destroy(&ddata->lock);
	}

	disable_irq_nosync(ddata->irq);

	return 0;

fail2:
	input_unregister_device(input);
	platform_set_drvdata(pdev, NULL);
fail1:
	input_free_device(input);
fail0:
	kfree(ddata);
	return error;

}

static int __devexit sfh7741_remove(struct platform_device *pdev)
{
	struct sfh7741_drvdata *ddata = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;
	mutex_destroy(&ddata->lock);
	sysfs_remove_group(&dev->kobj, &sfh7741_attr_group);
	free_irq(ddata->irq, (void *)ddata);
	input_unregister_device(ddata->input);
	kfree(ddata);
	return 0;
}

#ifdef CONFIG_PM
#ifdef SFH7741_SUSPEND_RESUME
static int sfh7741_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct sfh7741_drvdata *ddata = platform_get_drvdata(pdev);
	/* Save the prox state for the resume */
	ddata->on_before_suspend = ddata->prox_enable;

	return 0;
}

static int sfh7741_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct sfh7741_drvdata *ddata = platform_get_drvdata(pdev);
	int proximity = 0;

	if (ddata->on_before_suspend) {
		proximity = ddata->read_prox();
		input_report_abs(ddata->input, ABS_DISTANCE, proximity);
		input_sync(ddata->input);
	}
	return 0;
}

static const struct dev_pm_ops sfh7741_pm_ops = {
	.suspend	= sfh7741_suspend,
	.resume		= sfh7741_resume,
};
#endif
#endif

static struct platform_driver sfh7741_device_driver = {
	.probe		= sfh7741_probe,
	.remove		= __devexit_p(sfh7741_remove),
	.driver		= {
		.name	= SFH7741_NAME,
		.owner	= THIS_MODULE,
#ifdef CONFIG_PM
#ifdef SFH7741_SUSPEND_RESUME
		.pm	= &sfh7741_pm_ops,
#endif
#endif
	}
};

static int __init sfh7741_init(void)
{
	return platform_driver_register(&sfh7741_device_driver);
}

static void __exit sfh7741_exit(void)
{
	platform_driver_unregister(&sfh7741_device_driver);
}

module_init(sfh7741_init);
module_exit(sfh7741_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Texas Instruments");
MODULE_DESCRIPTION("Proximity sensor SFH7741 driver");
MODULE_ALIAS("platform:sfh7741");