| 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
 | /*
 *  Copyright (C) 2012 Motorola, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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
 *
 *  Adds ability to program periodic interrupts from user space that
 *  can wake the phone out of low power modes.
 *
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/input.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/m4sensorhub.h>
#include <linux/slab.h>
#define STILLMODE_CLIENT_DRIVER_NAME "m4sensorhub_stillmode"
#define STILLMODE_DEFAULT_TIMEOUT  600 /* 10 minutes */
static DEFINE_MUTEX(state_access);
enum m4_stillmode_type {
	MOTION,
	STILL,
};
struct stillmode_client {
	struct m4sensorhub_data *m4sensorhub;
	struct input_dev *input_dev;
	enum m4_stillmode_type state;
	struct wake_lock wakelock;
	struct work_struct queued_work;
	u16 timeout;
};
static struct stillmode_client *g_stillmode_data;
static int stillmode_set_timeout(struct stillmode_client *stillmode_client_data,
				 u16 timeout)
{
	int ret;
	ret = m4sensorhub_reg_write(stillmode_client_data->m4sensorhub,
				    M4SH_REG_POWER_STILLMODETIMEOUT,
				    (char *)&timeout, m4sh_no_mask);
	if (ret == m4sensorhub_reg_getsize(stillmode_client_data->m4sensorhub,
				M4SH_REG_POWER_STILLMODETIMEOUT)) {
		stillmode_client_data->timeout = timeout;
		ret = 0;
	} else
		ret = -EIO;
	return ret;
}
static void stillmode_set_state(struct stillmode_client *stillmode_client_data,
			       enum m4_stillmode_type state)
{
	mutex_lock(&state_access);
	if (stillmode_client_data->state == state) {
		mutex_unlock(&state_access);
		printk(KERN_WARNING "M4SH duplicate stillmode update (%s)\n",
			(state == STILL) ? "still" : "moving");
	} else {
		stillmode_client_data->state = state;
		mutex_unlock(&state_access);
		/* Hold a 500ms wakelock to let data get to KineticManager */
		wake_lock_timeout(&stillmode_client_data->wakelock, 0.5 * HZ);
		input_report_switch(stillmode_client_data->input_dev,
				    SW_STILL_MODE,
				   (stillmode_client_data->state == STILL));
		input_sync(stillmode_client_data->input_dev);
		printk(KERN_INFO "stillmode state changed to %s (%d)\n",
			(state == STILL) ? "still" : "moving", state);
	}
}
static int m4_stillmode_exit(void)
{
	struct stillmode_client *stillmode_client_data = g_stillmode_data;
	int ret = 0;
	KDEBUG(M4SH_INFO, "Resetting stillmode timer\n");
	/* writing timeout value to M4 resets its timer */
	ret = stillmode_set_timeout(stillmode_client_data,
				    stillmode_client_data->timeout);
	if (ret == 0) {
		if (stillmode_client_data->state == STILL)
			stillmode_set_state(stillmode_client_data, MOTION);
	} else
		KDEBUG(M4SH_ERROR, "M4SH Error setting timeout (%d)\n", ret);
	return ret;
}
int m4sensorhub_stillmode_exit(void)
{
	return m4_stillmode_exit();
}
EXPORT_SYMBOL_GPL(m4sensorhub_stillmode_exit);
static void m4sensorhub_stillmode_work(struct work_struct *work)
{
	m4sensorhub_stillmode_exit();
}
static void m4_handle_stillmode_irq(enum m4sensorhub_irqs int_event,
					void *stillmode_data)
{
	struct stillmode_client *stillmode_client_data = stillmode_data;
	enum m4_stillmode_type new_state;
	KDEBUG(M4SH_INFO, "%s() got irq %d (%s)\n", __func__, int_event,
	int_event == M4SH_IRQ_STILL_DETECTED ? "STILL_MODE" : "MOTION_MODE");
	switch (int_event) {
	case (M4SH_IRQ_STILL_DETECTED):
		new_state = STILL;
		break;
	case (M4SH_IRQ_MOTION_DETECTED):
		new_state = MOTION;
		break;
	default:
		printk(KERN_ERR "%s() Unexpected irq: %d\n",
			__func__, int_event);
		return;
		break;
	}
	stillmode_set_state(stillmode_client_data, new_state);
}
static ssize_t m4_stillmode_getstate(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct stillmode_client *stillmode_client_data =
						 platform_get_drvdata(pdev);
	return sprintf(buf, "%d \n", stillmode_client_data->state);
}
static ssize_t m4_stillmode_setstate(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t size)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct stillmode_client *stillmode_client_data =
						 platform_get_drvdata(pdev);
	long value;
	int ret = size;
	if (((strict_strtoul(buf, 10, &value)) < 0) ||
	     (value != MOTION)) {
		KDEBUG(M4SH_ERROR, "M4SH stillmode invalid value: %ld.  Only "
				   "%d is allowed\n", value, MOTION);
		return -EINVAL;
	}
	if (value != stillmode_client_data->state)
		return m4sensorhub_stillmode_exit();
	return ret;
}
static ssize_t m4_stillmode_get_timeout(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct stillmode_client *stillmode_client_data =
						platform_get_drvdata(pdev);
	return sprintf(buf, "%d \n", stillmode_client_data->timeout);
}
static ssize_t m4_stillmode_set_timeout(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t size)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct stillmode_client *stillmode_client_data =
						 platform_get_drvdata(pdev);
	long value;
	int ret;
	if (((strict_strtoul(buf, 10, &value)) < 0) ||
	     (value < 0) || (value > USHRT_MAX)) {
		KDEBUG(M4SH_ERROR, "M4SH stillmode invalid timeout: %ld\n",
			value);
		return -EINVAL;
	}
	KDEBUG(M4SH_DEBUG, "%s() setting timeout to %ld\n", __func__, value);
	ret = stillmode_set_timeout(stillmode_client_data, value);
	return ((ret == 0) ? size : ret);
}
static DEVICE_ATTR(state, 0664, m4_stillmode_getstate,
		   m4_stillmode_setstate);
static DEVICE_ATTR(timeout, 0664, m4_stillmode_get_timeout,
		   m4_stillmode_set_timeout);
static int stillmode_client_probe(struct platform_device *pdev)
{
	int ret = -1;
	struct stillmode_client *stillmode_client_data;
	struct m4sensorhub_data *m4sensorhub = m4sensorhub_client_get_drvdata();
	if (!m4sensorhub)
		return -EFAULT;
	stillmode_client_data = kzalloc(sizeof(*stillmode_client_data),
						GFP_KERNEL);
	if (!stillmode_client_data)
		return -ENOMEM;
	g_stillmode_data = stillmode_client_data;
	stillmode_client_data->m4sensorhub = m4sensorhub;
	platform_set_drvdata(pdev, stillmode_client_data);
	stillmode_client_data->state = MOTION;
	stillmode_client_data->timeout = STILLMODE_DEFAULT_TIMEOUT;
	stillmode_client_data->input_dev = input_allocate_device();
	if (!stillmode_client_data->input_dev) {
		ret = -ENOMEM;
		KDEBUG(M4SH_ERROR, "%s: input device allocate failed: %d\n",
			__func__, ret);
		goto free_memory;
	}
	stillmode_client_data->input_dev->name = STILLMODE_CLIENT_DRIVER_NAME;
	set_bit(EV_SW, stillmode_client_data->input_dev->evbit);
	set_bit(SW_STILL_MODE, stillmode_client_data->input_dev->swbit);
	if (input_register_device(stillmode_client_data->input_dev)) {
		KDEBUG(M4SH_ERROR, "%s: input device register failed\n",
			__func__);
		input_free_device(stillmode_client_data->input_dev);
		goto free_memory;
	}
	wake_lock_init(&stillmode_client_data->wakelock, WAKE_LOCK_SUSPEND,
		       STILLMODE_CLIENT_DRIVER_NAME);
	INIT_WORK(&stillmode_client_data->queued_work,
		  m4sensorhub_stillmode_work);
	ret = m4sensorhub_irq_register(m4sensorhub, M4SH_IRQ_STILL_DETECTED,
						m4_handle_stillmode_irq,
						stillmode_client_data);
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "Error registering still mode IRQ: "
			"%d\n", ret);
		goto destroy_wakelock;
	}
	ret = m4sensorhub_irq_enable(m4sensorhub, M4SH_IRQ_STILL_DETECTED);
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "Error enabling still mode int: "
			"%d\n", ret);
		goto unregister_still_irq;
	}
	ret = m4sensorhub_irq_register(m4sensorhub, M4SH_IRQ_MOTION_DETECTED,
						m4_handle_stillmode_irq,
						stillmode_client_data);
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "Error registering moving mode IRQ: "
			"%d\n", ret);
		goto disable_still_irq;
	}
	ret = m4sensorhub_irq_enable(m4sensorhub, M4SH_IRQ_MOTION_DETECTED);
	if (ret < 0) {
		KDEBUG(M4SH_ERROR, "Error enabling moving mode int: "
			"%d\n", ret);
		goto unregister_moving_irq;
	}
	if (device_create_file(&pdev->dev, &dev_attr_state)) {
		KDEBUG(M4SH_ERROR, "Error creating stillmode sys entry\n");
		ret = -1;
		goto disable_moving_irq;
	}
	if (device_create_file(&pdev->dev, &dev_attr_timeout)) {
		KDEBUG(M4SH_ERROR, "Error creating timeout sys entry\n");
		ret = -1;
		goto remove_stillmode_sysfs;
	}
	/* initialize timer on M4 */
	m4sensorhub_stillmode_exit();
	KDEBUG(M4SH_INFO, "Initialized %s driver\n",
		STILLMODE_CLIENT_DRIVER_NAME);
	return 0;
remove_stillmode_sysfs:
	device_remove_file(&pdev->dev, &dev_attr_state);
disable_moving_irq:
	m4sensorhub_irq_disable(m4sensorhub, M4SH_IRQ_MOTION_DETECTED);
unregister_moving_irq:
	m4sensorhub_irq_unregister(m4sensorhub, M4SH_IRQ_MOTION_DETECTED);
disable_still_irq:
	m4sensorhub_irq_disable(m4sensorhub, M4SH_IRQ_STILL_DETECTED);
unregister_still_irq:
	m4sensorhub_irq_unregister(m4sensorhub, M4SH_IRQ_STILL_DETECTED);
destroy_wakelock:
	wake_lock_destroy(&stillmode_client_data->wakelock);
	input_unregister_device(stillmode_client_data->input_dev);
free_memory:
	platform_set_drvdata(pdev, NULL);
	m4sensorhub->pdev->stillmode_exit = NULL;
	stillmode_client_data->m4sensorhub = NULL;
	kfree(stillmode_client_data);
	g_stillmode_data = NULL;
	return ret;
}
static int __exit stillmode_client_remove(struct platform_device *pdev)
{
	struct stillmode_client *stillmode_client_data =
						platform_get_drvdata(pdev);
	struct m4sensorhub_data *m4sensorhub =
					stillmode_client_data->m4sensorhub;
	device_remove_file(&pdev->dev, &dev_attr_timeout);
	device_remove_file(&pdev->dev, &dev_attr_state);
	m4sensorhub_irq_disable(m4sensorhub, M4SH_IRQ_MOTION_DETECTED);
	m4sensorhub_irq_unregister(m4sensorhub, M4SH_IRQ_MOTION_DETECTED);
	m4sensorhub_irq_disable(m4sensorhub, M4SH_IRQ_STILL_DETECTED);
	m4sensorhub_irq_unregister(m4sensorhub, M4SH_IRQ_STILL_DETECTED);
	wake_lock_destroy(&stillmode_client_data->wakelock);
	input_unregister_device(stillmode_client_data->input_dev);
	platform_set_drvdata(pdev, NULL);
	m4sensorhub->pdev->stillmode_exit = NULL;
	stillmode_client_data->m4sensorhub = NULL;
	kfree(stillmode_client_data);
	g_stillmode_data = NULL;
	return 0;
}
static void stillmode_client_shutdown(struct platform_device *pdev)
{
	return;
}
#ifdef CONFIG_PM
static int stillmode_client_suspend(struct platform_device *pdev,
				pm_message_t message)
{
	return 0;
}
static int stillmode_client_resume(struct platform_device *pdev)
{
	return 0;
}
#else
#define stillmode_client_suspend NULL
#define stillmode_client_resume  NULL
#endif
static struct of_device_id m4stillmode_match_tbl[] = {
	{ .compatible = "mot,m4stillmode" },
	{},
};
static struct platform_driver stillmode_client_driver = {
	.probe		= stillmode_client_probe,
	.remove		= __exit_p(stillmode_client_remove),
	.shutdown	= stillmode_client_shutdown,
	.suspend	= stillmode_client_suspend,
	.resume		= stillmode_client_resume,
	.driver		= {
		.name	= STILLMODE_CLIENT_DRIVER_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(m4stillmode_match_tbl),
	},
};
static int __init stillmode_client_init(void)
{
	return platform_driver_register(&stillmode_client_driver);
}
static void __exit stillmode_client_exit(void)
{
	platform_driver_unregister(&stillmode_client_driver);
}
module_init(stillmode_client_init);
module_exit(stillmode_client_exit);
MODULE_ALIAS("platform:stillmode_client");
MODULE_DESCRIPTION("M4 sensorhub still mode client driver");
MODULE_AUTHOR("Motorola");
MODULE_LICENSE("GPL");
 |