summaryrefslogtreecommitdiff
path: root/drivers/misc/m4sensorhub_als.c
blob: 6445d9f0181371d68758d35e0daf574b728bcf73 (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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
 *  Copyright (C) 2012-2014 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/types.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/m4sensorhub.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/delay.h>
#ifdef CONFIG_WAKEUP_SOURCE_NOTIFY
#include <linux/notifier.h>
#include <linux/als_notify.h>
#ifdef CONFIG_ALS_WHILE_CHARGING
#include <linux/wakeup_source_notify.h>
#endif /* CONFIG_ALS_WHILE_CHARGING */
#endif

#define m4als_err(format, args...)  KDEBUG(M4SH_ERROR, format, ## args)

#define M4ALS_DRIVER_NAME           "m4sensorhub_als"

#define M4ALS_IRQ_ENABLED_BIT       0
#define ALS_SAMPLERATE_WHILE_DOCKED	10 /* in seconds */

struct m4als_driver_data {
	struct platform_device      *pdev;
	struct m4sensorhub_data     *m4;
	struct mutex                mutex; /* controls driver entry points */
	struct input_dev            *indev;
	struct delayed_work         m4als_work;

	/* Beware of changing this from uint16, check als_notify.h
	since notifier uses values outside luminosity range for
	conveying enable/disable status */
	uint16_t        luminosity;
	int16_t         samplerate;
	int16_t         latest_samplerate;
	int16_t         fastest_rate;
	uint16_t        status;
#ifdef CONFIG_ALS_WHILE_CHARGING
	struct          notifier_block charger_nb;
	bool            chargerstatus;
#endif
};

#ifdef CONFIG_ALS_WHILE_CHARGING
static int charger_notify(struct notifier_block *self,
			unsigned long action, void *dev)
{
	struct m4als_driver_data *dd =
		container_of(self, struct m4als_driver_data, charger_nb);
	switch (action) {
	case DISPLAY_WAKE_EVENT_DOCKON:
	case DISPLAY_WAKE_EVENT_DOCKOFF:
		dd->chargerstatus = (action == DISPLAY_WAKE_EVENT_DOCKON);
		pr_info("%s: dd->chargerstatus is %d\n",
			__func__, dd->chargerstatus);
		break;
	}

	return NOTIFY_OK;
}
#endif

static void m4als_work_func(struct work_struct *work)
{
	int err = 0;
	struct m4als_driver_data *dd = container_of(work,
						    struct m4als_driver_data,
						    m4als_work.work);
	int size = 0;
	uint16_t luminosity = 0;

	mutex_lock(&(dd->mutex));

	size = m4sensorhub_reg_getsize(dd->m4, M4SH_REG_LIGHTSENSOR_SIGNAL);
	if (size < 0) {
		m4als_err("%s: Reading from invalid register %d.\n",
			  __func__, size);
		err = size;
		goto m4als_isr_fail;
	}

	err = m4sensorhub_reg_read(dd->m4, M4SH_REG_LIGHTSENSOR_SIGNAL,
		(char *)&luminosity);
	if (err < 0) {
		m4als_err("%s: Failed to read luminosity data.\n", __func__);
		goto m4als_isr_fail;
	} else if (err != size) {
		m4als_err("%s: Read %d bytes instead of %d.\n",
			  __func__, err, size);
		goto m4als_isr_fail;
	}

	dd->luminosity = luminosity;

#ifdef CONFIG_ALS_WHILE_CHARGING
	if (dd->chargerstatus == true) {
		als_notify_subscriber(luminosity);
	} else {
		input_event(dd->indev, EV_MSC, MSC_RAW, dd->luminosity);
		input_sync(dd->indev);
	}
#else
		input_event(dd->indev, EV_MSC, MSC_RAW, dd->luminosity);
		input_sync(dd->indev);
#endif

	if (dd->samplerate > 0)
		queue_delayed_work(system_freezable_wq, &(dd->m4als_work),
				      msecs_to_jiffies(dd->samplerate));

m4als_isr_fail:
	if (err < 0)
		m4als_err("%s: Failed with error code %d.\n", __func__, err);

	mutex_unlock(&(dd->mutex));

	return;
}

static int m4als_set_samplerate(struct m4als_driver_data *dd, int16_t rate)
{
	int err = 0;
	int size = 0;

#ifdef CONFIG_ALS_WHILE_CHARGING
	if (rate == -1 && dd->chargerstatus == true) {
		rate = ALS_SAMPLERATE_WHILE_DOCKED * 1000;
	}
#endif

	if ((rate >= 0) && (rate <= dd->fastest_rate))
		rate = dd->fastest_rate;

	/* This variabled is to be always updated ireespective of the
	   transaction status */
	dd->latest_samplerate = rate;

	if (rate == dd->samplerate)
		goto m4als_set_samplerate_fail;

	size = m4sensorhub_reg_getsize(dd->m4, M4SH_REG_LIGHTSENSOR_SAMPLERATE);
	if (size < 0) {
		m4als_err("%s: Writing to invalid register %d.\n",
			  __func__, size);
		err = size;
		goto m4als_set_samplerate_fail;
	}

	err = m4sensorhub_reg_write(dd->m4, M4SH_REG_LIGHTSENSOR_SAMPLERATE,
		(char *)&rate, m4sh_no_mask);
	if (err < 0) {
		m4als_err("%s: Failed to set sample rate.\n", __func__);
		goto m4als_set_samplerate_fail;
	} else if (err != size) {
		m4als_err("%s: Wrote %d bytes instead of %d.\n",
			  __func__, err, size);
		err = -EBADE;
		goto m4als_set_samplerate_fail;
	}
	cancel_delayed_work(&(dd->m4als_work));
	dd->samplerate = rate;
	if (dd->samplerate > 0) {
		queue_delayed_work(system_freezable_wq, &(dd->m4als_work),
				      msecs_to_jiffies(rate));
#ifdef CONFIG_WAKEUP_SOURCE_NOTIFY
		als_notify_subscriber(ALS_ENABLED);
#endif
	} else {
#ifdef CONFIG_WAKEUP_SOURCE_NOTIFY
		als_notify_subscriber(ALS_DISABLED);
#endif
	}

m4als_set_samplerate_fail:
	return err;
}

static ssize_t m4als_setrate_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct m4als_driver_data *dd = dev_get_drvdata(dev);

	return sprintf(buf, "Current rate: %hd\n", dd->samplerate);
}
static ssize_t m4als_setrate_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t size)
{
	int err = 0;
	struct m4als_driver_data *dd = dev_get_drvdata(dev);
	int value = 0;
	int regsize = 0;
	uint16_t luminosity = 0;

	mutex_lock(&(dd->mutex));

	err = kstrtoint(buf, 10, &value);
	if (err < 0) {
		m4als_err("%s: Failed to convert value.\n", __func__);
		goto m4als_enable_store_exit;
	}

	if ((value < -1) || (value > 32767)) {
		m4als_err("%s: Invalid sample rate requested = %d\n",
			  __func__, value);
		err = -EOVERFLOW;
		goto m4als_enable_store_exit;
	}

	err = m4als_set_samplerate(dd, value);
	if (err < 0) {
		m4als_err("%s: Failed to set sample rate.\n", __func__);
		goto m4als_enable_store_exit;
	}

	/* Read and send raw value for gesture wakeup */
	msleep(120);
	regsize = m4sensorhub_reg_getsize(dd->m4, M4SH_REG_LIGHTSENSOR_SIGNAL);
	if (regsize < 0) {
		m4als_err("%s: Reading from invalid register %d.\n",
			  __func__, regsize);
		err = regsize;
		goto m4als_enable_store_exit;
	}

	err = m4sensorhub_reg_read(dd->m4, M4SH_REG_LIGHTSENSOR_SIGNAL,
		(char *)&luminosity);
	if (err < 0) {
		m4als_err("%s: Failed to read luminosity data.\n", __func__);
		goto m4als_enable_store_exit;
	} else if (err != regsize) {
		m4als_err("%s: Read %d bytes instead of %d.\n",
			  __func__, err, regsize);
		goto m4als_enable_store_exit;
	}

	dd->luminosity = luminosity;

	input_event(dd->indev, EV_MSC, MSC_RAW, dd->luminosity);
	input_sync(dd->indev);

m4als_enable_store_exit:
	if (err < 0)
		m4als_err("%s: Failed with error code %d.\n", __func__, err);

	mutex_unlock(&(dd->mutex));

	return size;
}
static DEVICE_ATTR(als_setrate, S_IRUSR | S_IWUSR,
		m4als_setrate_show, m4als_setrate_store);

static ssize_t m4als_luminosity_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct m4als_driver_data *dd = dev_get_drvdata(dev);

	return sprintf(buf, "Current luminosity: %hd\n", dd->luminosity);
}
static DEVICE_ATTR(luminosity, S_IRUGO, m4als_luminosity_show, NULL);

static int m4als_create_sysfs(struct m4als_driver_data *dd)
{
	int err = 0;

	err = device_create_file(&(dd->pdev->dev), &dev_attr_als_setrate);
	if (err < 0) {
		m4als_err("%s: Failed to create als_setrate %s %d.\n",
			  __func__, "with error", err);
		goto m4als_create_sysfs_als_exit;
	}

	err = device_create_file(&(dd->pdev->dev), &dev_attr_luminosity);
	if (err < 0) {
		m4als_err("%s: Failed to create luminosity %s %d.\n",
			  __func__, "with error", err);
		goto m4als_create_sysfs_luminosity_fail;
	}

	goto m4als_create_sysfs_als_exit;

m4als_create_sysfs_luminosity_fail:
	device_remove_file(&(dd->pdev->dev), &dev_attr_als_setrate);
m4als_create_sysfs_als_exit:
	return err;
}

static int m4als_remove_sysfs(struct m4als_driver_data *dd)
{
	int err = 0;

	device_remove_file(&(dd->pdev->dev), &dev_attr_als_setrate);
	device_remove_file(&(dd->pdev->dev), &dev_attr_luminosity);

	return err;
}

static int m4als_create_m4eventdev(struct m4als_driver_data *dd)
{
	int err = 0;

	dd->indev = input_allocate_device();
	if (dd->indev == NULL) {
		m4als_err("%s: Failed to allocate input device.\n",
			  __func__);
		err = -ENODATA;
		goto m4als_create_m4eventdev_fail;
	}

	dd->indev->name = M4ALS_DRIVER_NAME;
	input_set_drvdata(dd->indev, dd);
	input_set_capability(dd->indev, EV_MSC, MSC_RAW);

	err = input_register_device(dd->indev);
	if (err < 0) {
		m4als_err("%s: Failed to register input device.\n",
			  __func__);
		input_free_device(dd->indev);
		dd->indev = NULL;
		goto m4als_create_m4eventdev_fail;
	}

m4als_create_m4eventdev_fail:
	return err;
}

static void m4als_panic_restore(struct m4sensorhub_data *m4sensorhub,
				void *data)
{
	int size, err;
	struct m4als_driver_data *dd = (struct m4als_driver_data *)data;

	if (dd == NULL) {
		m4als_err("%s: Driver data is null, unable to restore\n",
			  __func__);
		return;
	}

	mutex_lock(&(dd->mutex));

	size = m4sensorhub_reg_getsize(dd->m4,
				       M4SH_REG_LIGHTSENSOR_SAMPLERATE);
	err = m4sensorhub_reg_write(dd->m4, M4SH_REG_LIGHTSENSOR_SAMPLERATE,
				   (char *)&dd->samplerate, m4sh_no_mask);
	if (err < 0) {
		m4als_err("%s: Failed to set sample rate.\n", __func__);
	} else if (err != size) {
		m4als_err("%s: Wrote %d bytes instead of %d.\n",
			  __func__, err, size);
	}
	cancel_delayed_work(&(dd->m4als_work));
	if (dd->samplerate > 0)
		queue_delayed_work(system_freezable_wq, &(dd->m4als_work),
				      msecs_to_jiffies(dd->samplerate));
	mutex_unlock(&(dd->mutex));
}

static int m4als_driver_init(struct init_calldata *p_arg)
{
	struct m4als_driver_data *dd = p_arg->p_data;
	int err = 0;

	mutex_lock(&(dd->mutex));

	err = m4als_create_m4eventdev(dd);
	if (err < 0) {
		m4als_err("%s: Failed to create M4 event device.\n", __func__);
		goto m4als_driver_init_fail;
	}

	INIT_DELAYED_WORK(&(dd->m4als_work), m4als_work_func);

	err = m4sensorhub_panic_register(dd->m4, PANICHDL_ALS_RESTORE,
					 m4als_panic_restore, dd);
	if (err < 0)
		KDEBUG(M4SH_ERROR, "Als panic callback register failed\n");
	goto m4als_driver_init_exit;

m4als_driver_init_fail:
	m4als_err("%s: Init failed with error code %d.\n", __func__, err);
m4als_driver_init_exit:
	mutex_unlock(&(dd->mutex));
	return err;
}

static int m4als_probe(struct platform_device *pdev)
{
	struct m4als_driver_data *dd = NULL;
	int err = 0;

	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
	if (dd == NULL) {
		m4als_err("%s: Failed to allocate driver data.\n", __func__);
		err = -ENOMEM;
		goto m4als_probe_fail_nodd;
	}

	dd->pdev = pdev;
	mutex_init(&(dd->mutex));
	platform_set_drvdata(pdev, dd);
	dd->samplerate = -1; /* We always start disabled */
	dd->latest_samplerate = dd->samplerate;
	dd->fastest_rate = 80; /* in milli secs */

	dd->m4 = m4sensorhub_client_get_drvdata();
	if (dd->m4 == NULL) {
		m4als_err("%s: M4 sensor data is NULL.\n", __func__);
		err = -ENODATA;
		goto m4als_probe_fail;
	}

	err = m4sensorhub_register_initcall(m4als_driver_init, dd);
	if (err < 0) {
		m4als_err("%s: Failed to register initcall.\n", __func__);
		goto m4als_probe_fail;
	}

	err = m4als_create_sysfs(dd);
	if (err < 0) {
		m4als_err("%s: Failed to create sysfs.\n", __func__);
		goto m4als_driver_init_sysfs_fail;
	}

#ifdef CONFIG_ALS_WHILE_CHARGING
	dd->chargerstatus = false;
	dd->charger_nb.notifier_call = charger_notify;
	wakeup_source_register_notify(&dd->charger_nb);
#endif

	return 0;

m4als_driver_init_sysfs_fail:
	m4sensorhub_unregister_initcall(m4als_driver_init);
m4als_probe_fail:
	mutex_destroy(&(dd->mutex));
	kfree(dd);
m4als_probe_fail_nodd:
	m4als_err("%s: Probe failed with error code %d.\n", __func__, err);
	return err;
}

static int __exit m4als_remove(struct platform_device *pdev)
{
	struct m4als_driver_data *dd = platform_get_drvdata(pdev);

	mutex_lock(&(dd->mutex));
	cancel_delayed_work(&(dd->m4als_work));
	m4als_remove_sysfs(dd);
	m4sensorhub_unregister_initcall(m4als_driver_init);
#ifdef CONFIG_ALS_WHILE_CHARGING
	wakeup_source_unregister_notify(&dd->charger_nb);
#endif
	if (dd->indev != NULL)
		input_unregister_device(dd->indev);
	mutex_destroy(&(dd->mutex));
	kfree(dd);

	return 0;
}

static int m4als_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct m4als_driver_data *dd = platform_get_drvdata(pdev);
	if (m4als_set_samplerate(dd, dd->latest_samplerate) < 0)
		m4als_err("%s: setrate retry failed\n", __func__);
	return 0;
}

static struct of_device_id m4als_match_tbl[] = {
	{ .compatible = "mot,m4als" },
	{},
};

static struct platform_driver m4als_driver = {
	.probe		= m4als_probe,
	.remove		= __exit_p(m4als_remove),
	.shutdown	= NULL,
	.suspend        = m4als_suspend,
	.resume         = NULL,
	.driver		= {
		.name	= M4ALS_DRIVER_NAME,
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(m4als_match_tbl),
	},
};

static int __init m4als_init(void)
{
	return platform_driver_register(&m4als_driver);
}

static void __exit m4als_exit(void)
{
	platform_driver_unregister(&m4als_driver);
}

module_init(m4als_init);
module_exit(m4als_exit);

MODULE_ALIAS("platform:m4als");
MODULE_DESCRIPTION("M4 Sensor Hub Ambient Light client driver");
MODULE_AUTHOR("Motorola");
MODULE_LICENSE("GPL");