summaryrefslogtreecommitdiff
path: root/drivers/sensors/brcm/ssp_c12sd.c
blob: 5ee20c1581c3edac011825cf797201e41d07757c (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * drivers/sensorhub/ssp_c12sd.c
 * Copyright (c) 2012 SAMSUNG ELECTRONICS
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/hrtimer.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio_event.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/earlysuspend.h>
#include <linux/sensor/guva_c12sd.h>
#include "ssp.h"

#define VENDOR_NAME	"GENICOM"
#define CHIP_NAME	"GUVA-C12SD"

#ifdef CONFIG_HAS_EARLYSUSPEND
/* early suspend */
static void ssp_early_suspend(struct early_suspend *handler);
static void ssp_late_resume(struct early_suspend *handler);
#endif

struct uv_info {
	struct uv_platform_data *pdata;
	struct workqueue_struct *uv_wq;
	struct work_struct work_uv;
	struct device *uv_dev;
	struct hrtimer uv_timer;
	struct mutex power_lock;
	struct mutex read_lock;
	struct input_dev *uv_input_dev;
	struct platform_device *pdev_uv_adc;
#ifdef CONFIG_HAS_EARLYSUSPEND
	struct early_suspend early_suspend;
#endif
	ktime_t uv_poll_delay;
	int uv_raw_data;
	bool onoff;
};

/* sysfs for input device */
static ssize_t uv_poll_delay_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uv_info *uv = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%lld\n",
		ktime_to_ns(uv->uv_poll_delay));
}

static ssize_t uv_poll_delay_store(struct device *dev,
	struct device_attribute *attr,
	const char *buf, size_t size)
{
	struct uv_info *uv = dev_get_drvdata(dev);
	int64_t new_delay;
	int err;

	err = kstrtoll(buf, 10, &new_delay);
	if (err < 0)
		return err;

	mutex_lock(&uv->power_lock);
	if (uv->onoff) {
		hrtimer_cancel(&uv->uv_timer);
		cancel_work_sync(&uv->work_uv);
	}

	if (new_delay != ktime_to_ns(uv->uv_poll_delay)) {
		uv->uv_poll_delay = ns_to_ktime(new_delay);
		pr_info("%s, poll_delay = %lld\n", __func__, new_delay);
	}

	if (uv->onoff)
		hrtimer_start(&uv->uv_timer, uv->uv_poll_delay,
			HRTIMER_MODE_REL);
	mutex_unlock(&uv->power_lock);

	return size;
}

static ssize_t uv_enable_store(struct device *dev,
	struct device_attribute *attr,
	const char *buf, size_t size)
{
	struct uv_info *uv = dev_get_drvdata(dev);
	bool new_value;

	if (sysfs_streq(buf, "1")) {
		new_value = true;
	} else if (sysfs_streq(buf, "0")) {
		new_value = false;
	} else {
		pr_err("%s: invalid value %d\n", __func__, *buf);
		return -EINVAL;
	}
	pr_info("%s, old = %d, new = %d\n", __func__, uv->onoff, new_value);
	mutex_lock(&uv->power_lock);
	if (new_value && !uv->onoff) { /* Enable */
		uv->onoff = new_value;
		if (uv->pdata->power_on)
			uv->pdata->power_on(uv->onoff);
		hrtimer_start(&uv->uv_timer, uv->uv_poll_delay,
			HRTIMER_MODE_REL);
	} else if (!new_value && uv->onoff) { /* Disable */
		hrtimer_cancel(&uv->uv_timer);
		uv->onoff = new_value;
		if (uv->pdata->power_on)
			uv->pdata->power_on(uv->onoff);
	} else
		pr_err("%s, new_enable = %d\n", __func__, new_value);
	mutex_unlock(&uv->power_lock);

	return size;
}

static ssize_t uv_enable_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uv_info *uv = dev_get_drvdata(dev);
	return snprintf(buf, PAGE_SIZE, "%d\n", uv->onoff);
}

static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
		   uv_poll_delay_show, uv_poll_delay_store);
static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
		   uv_enable_show, uv_enable_store);

static struct attribute *uv_sysfs_attrs[] = {
	&dev_attr_enable.attr,
	&dev_attr_poll_delay.attr,
	NULL
};

static struct attribute_group uv_attribute_group = {
	.attrs = uv_sysfs_attrs,
};

/* sysfs for uv sensor class */
static ssize_t get_vendor_name(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
}

static ssize_t get_chip_name(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_NAME);
}

static ssize_t get_adc_value(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uv_info *uv = dev_get_drvdata(dev);
	int adc = 0;
	 /* If power is not turned on, it won't work. */
	if (!uv->onoff) {
		mutex_lock(&uv->read_lock);
		if (uv->pdata->power_on)
			uv->pdata->power_on(true);
		mdelay(20);
		adc = uv->pdata->get_adc_value();
		if (uv->pdata->power_on)
			uv->pdata->power_on(false);
		mutex_unlock(&uv->read_lock);
	} else
		adc = uv->uv_raw_data;
	pr_info("%s: uv_adc = 0x%x, (%dmV)\n", __func__, adc, adc / 1000);

	return snprintf(buf, PAGE_SIZE, "%d\n", adc);
}

static ssize_t uv_power_on(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uv_info *uv = dev_get_drvdata(dev);

	if (!uv->onoff && uv->pdata->power_on)
		uv->pdata->power_on(true);
	pr_info("%s\n", __func__);

	return sprintf(buf, "%d\n", 1);
}

static ssize_t uv_power_off(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct uv_info *uv = dev_get_drvdata(dev);

	if (!uv->onoff && uv->pdata->power_on)
		uv->pdata->power_on(false);
	pr_info("%s\n", __func__);

	return sprintf(buf, "%d\n", 1);
}

static DEVICE_ATTR(vendor, S_IRUSR | S_IRGRP, get_vendor_name, NULL);
static DEVICE_ATTR(name, S_IRUSR | S_IRGRP, get_chip_name, NULL);
static DEVICE_ATTR(raw_data, S_IRUSR | S_IRGRP, get_adc_value, NULL);
static DEVICE_ATTR(power_on, S_IRUSR | S_IRGRP, uv_power_on, NULL);
static DEVICE_ATTR(power_off, S_IRUSR | S_IRGRP, uv_power_off, NULL);

static struct device_attribute *uv_sensor_attrs[] = {
	&dev_attr_vendor,
	&dev_attr_name,
	&dev_attr_raw_data,
	&dev_attr_power_on,
	&dev_attr_power_off,
	NULL
};

/* timer function for uv sensor polling */
static enum hrtimer_restart uv_timer_func(struct hrtimer *timer)
{
	struct uv_info *uv
	    = container_of(timer, struct uv_info, uv_timer);
	queue_work(uv->uv_wq, &uv->work_uv);
	hrtimer_forward_now(&uv->uv_timer, uv->uv_poll_delay);
	return HRTIMER_RESTART;
}

static void work_func_uv(struct work_struct *work)
{
	struct uv_info *uv
	    = container_of(work, struct uv_info, work_uv);

	mutex_lock(&uv->read_lock);
	uv->uv_raw_data = uv->pdata->get_adc_value();
	mutex_unlock(&uv->read_lock);

	input_report_rel(uv->uv_input_dev, REL_MISC, uv->uv_raw_data+1);
	input_sync(uv->uv_input_dev);
}

static int uv_probe(struct platform_device *pdev)
{
	struct uv_info *uv;
	struct uv_platform_data *pdata = pdev->dev.platform_data;
	int ret = 0;

	pr_info("%s: is started\n", __func__);
	if (!pdata) {
		pr_err("%s: pdata is NULL\n", __func__);
		return -ENODEV;
	}

	/* allocate memory */
	uv = kzalloc(sizeof(struct uv_info), GFP_KERNEL);
	if (uv == NULL) {
		pr_err("%s: Failed to allocate memory\n", __func__);
		return -ENOMEM;
	}

	uv->pdata = pdata;

	/* Setup for ADC */
	/* alloc platform device for adc client */
	uv->pdev_uv_adc = platform_device_alloc("uv-adc", -1);
	if (!uv->pdev_uv_adc) {
		pr_err("%s: could not allocation uv-adc\n", __func__);
		goto err_alloc_pdev;
	}

	if (pdata->adc_ap_init && pdata->adc_ap_exit) {
		uv->pdata->adc_ap_init = pdata->adc_ap_init;
		uv->pdata->adc_ap_exit = pdata->adc_ap_exit;
		ret = pdata->adc_ap_init(uv->pdev_uv_adc);
		if (!ret) {
			ret = -1;
			goto err_setup_adc;
		}
	}

	if (pdata->power_on) {
		uv->pdata->power_on = pdata->power_on;
		uv->pdata->power_on(false);
	}
	uv->onoff = false;

	if (pdata->get_adc_value)
		uv->pdata->get_adc_value = pdata->get_adc_value;

	mutex_init(&uv->power_lock);
	mutex_init(&uv->read_lock);

	/* allocate uv input device */
	uv->uv_input_dev = input_allocate_device();
	if (!uv->uv_input_dev) {
		pr_err("%s: could not allocate input device\n",
			__func__);
		goto err_input_allocate_device_uv;
	}

	input_set_drvdata(uv->uv_input_dev, uv);
	uv->uv_input_dev->name = "uv_sensor";
	input_set_capability(uv->uv_input_dev, EV_REL, REL_MISC);

	ret = input_register_device(uv->uv_input_dev);
	if (ret < 0) {
		pr_err("%s: could not register input device\n",
			__func__);
		input_free_device(uv->uv_input_dev);
		goto err_input_register_device_uv;
	}

	ret = sysfs_create_group(&uv->uv_input_dev->dev.kobj,
				&uv_attribute_group);
	if (ret) {
		pr_err("%s: could not create sysfs group\n",
			__func__);
		goto err_sysfs_create_group_uv;
	}

	/* timer init */
	hrtimer_init(&uv->uv_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	uv->uv_poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
	uv->uv_timer.function = uv_timer_func;

	/* workqueue init */
	uv->uv_wq = create_singlethread_workqueue("uv_wq");
	if (!uv->uv_wq) {
		ret = -ENOMEM;
		pr_err("%s: could not create uv workqueue\n",
			__func__);
		goto err_create_uv_workqueue;
	}
	INIT_WORK(&uv->work_uv, work_func_uv);

	/* sysfs for uv sensor */
	ret = sensors_register(uv->uv_dev, uv, uv_sensor_attrs,
		"uv_sensor");
	if (ret) {
		pr_err("%s: could not register uv device(%d)\n",
			__func__, ret);
		goto err_sensor_register_failed;
	}

	ret = sensors_create_symlink(uv->uv_input_dev);
	if (ret < 0) {
		pr_err("%s, sensors_create_symlinks failed!(%d)\n",
			__func__, ret);
		goto err_uv_input__sysfs_create_link;
	}

#ifdef CONFIG_HAS_EARLYSUSPEND
	uv->early_suspend.suspend = ssp_early_suspend;
	uv->early_suspend.resume = ssp_late_resume;
	register_early_suspend(&uv->early_suspend);
#endif

	platform_set_drvdata(pdev, uv);

	pr_info("%s, success\n", __func__);
	return 0;
err_uv_input__sysfs_create_link:
	sensors_unregister(uv->uv_dev, uv_sensor_attrs);
err_sensor_register_failed:
	destroy_workqueue(uv->uv_wq);
err_create_uv_workqueue:
	sysfs_remove_group(&uv->uv_input_dev->dev.kobj,
			   &uv_attribute_group);
err_sysfs_create_group_uv:
	input_unregister_device(uv->uv_input_dev);
err_input_register_device_uv:
err_input_allocate_device_uv:
	mutex_destroy(&uv->read_lock);
	mutex_destroy(&uv->power_lock);
	if (uv->pdata->adc_ap_exit)
		uv->pdata->adc_ap_exit(uv->pdev_uv_adc);
err_setup_adc:
	if (uv->pdev_uv_adc)
		platform_device_put(uv->pdev_uv_adc);
err_alloc_pdev:
	kfree(uv);
	return ret;
}

static int uv_remove(struct platform_device *pdev)
{
	struct uv_info *uv = dev_get_drvdata(&pdev->dev);

	pr_info("%s+\n", __func__);
	if (uv->onoff) {
		hrtimer_cancel(&uv->uv_timer);
		cancel_work_sync(&uv->work_uv);
	}
	destroy_workqueue(uv->uv_wq);
	sensors_remove_symlink(uv->uv_input_dev);

#ifdef CONFIG_HAS_EARLYSUSPEND
	unregister_early_suspend(&uv->early_suspend);
#endif
	sensors_unregister(uv->uv_dev, uv_sensor_attrs);
	sysfs_remove_group(&uv->uv_input_dev->dev.kobj,
			   &uv_attribute_group);
	input_unregister_device(uv->uv_input_dev);

	if (uv->onoff && uv->pdata->power_on)
		uv->pdata->power_on(false);
	if (uv->pdata->adc_ap_exit)
		uv->pdata->adc_ap_exit(uv->pdev_uv_adc);
	if (uv->pdev_uv_adc)
		platform_device_put(uv->pdev_uv_adc);
	mutex_destroy(&uv->read_lock);
	mutex_destroy(&uv->power_lock);
	kfree(uv);
	pr_info("%s-\n", __func__);
	return 0;
}

#ifdef CONFIG_HAS_EARLYSUSPEND
/* early suspend */
static void ssp_early_suspend(struct early_suspend *handler)
{
	struct uv_info *uv;
	uv = container_of(handler, struct uv_info, early_suspend);

	if (uv->onoff) {
		hrtimer_cancel(&uv->uv_timer);
		cancel_work_sync(&uv->work_uv);
		if (uv->pdata->power_on)
			uv->pdata->power_on(false);
	}
	pr_err("%s, enabled = %d\n", __func__, uv->onoff);
}

static void ssp_late_resume(struct early_suspend *handler)
{
	struct uv_info *uv;
	uv = container_of(handler, struct uv_info, early_suspend);

	if (uv->onoff) {
		if (uv->pdata->power_on)
			uv->pdata->power_on(true);
		hrtimer_start(&uv->uv_timer,
			uv->uv_poll_delay, HRTIMER_MODE_REL);
	}
	pr_err("%s, enabled = %d\n", __func__, uv->onoff);
}
#else
static int uv_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct uv_info *uv = dev_get_drvdata(&pdev->dev);

	if (uv->onoff) {
		hrtimer_cancel(&uv->uv_timer);
		cancel_work_sync(&uv->work_uv);
		if (uv->pdata->power_on)
			uv->pdata->power_on(false);
	}
	pr_err("%s, enabled = %d\n", __func__, uv->onoff);
	return 0;
}

static int uv_resume(struct platform_device *pdev)
{
	struct uv_info *uv = dev_get_drvdata(&pdev->dev);

	if (uv->onoff) {
		if (uv->pdata->power_on)
			uv->pdata->power_on(true);
		hrtimer_start(&uv->uv_timer,
			uv->uv_poll_delay, HRTIMER_MODE_REL);
	}
	pr_err("%s, enabled = %d\n", __func__, uv->onoff);
	return 0;
}
#endif

static void uv_shutdown(struct platform_device *pdev)
{
	struct uv_info *uv = dev_get_drvdata(&pdev->dev);

	pr_info("%s+\n", __func__);
	if (uv->onoff) {
		hrtimer_cancel(&uv->uv_timer);
		cancel_work_sync(&uv->work_uv);
	}
	destroy_workqueue(uv->uv_wq);
#ifdef CONFIG_HAS_EARLYSUSPEND
	unregister_early_suspend(&uv->early_suspend);
#endif
	sensors_unregister(uv->uv_dev, uv_sensor_attrs);
	sysfs_remove_group(&uv->uv_input_dev->dev.kobj,
			   &uv_attribute_group);
	input_unregister_device(uv->uv_input_dev);

	if (uv->onoff && uv->pdata->power_on)
		uv->pdata->power_on(false);
	if (uv->pdata->adc_ap_exit)
		uv->pdata->adc_ap_exit(uv->pdev_uv_adc);
	if (uv->pdev_uv_adc)
		platform_device_put(uv->pdev_uv_adc);
	mutex_destroy(&uv->read_lock);
	mutex_destroy(&uv->power_lock);
	kfree(uv);
	pr_info("%s-\n", __func__);
}

static struct platform_driver uv_driver = {
	.probe      = uv_probe,
	.remove     = uv_remove,
#ifndef CONFIG_HAS_EARLYSUSPEND
	.suspend    = uv_suspend,
	.resume     = uv_resume,
#endif
	.shutdown   = uv_shutdown,
	.driver = {
		.name   = "uv_sensor",
		.owner  = THIS_MODULE,
	},
};

static int __init uv_init(void)
{
	return platform_driver_register(&uv_driver);
}

static void __exit uv_exit(void)
{
	platform_driver_unregister(&uv_driver);
}

module_init(uv_init);
module_exit(uv_exit);

MODULE_DESCRIPTION("UV sensor device driver");
MODULE_AUTHOR("OHeon Kwon <koh82.kwon@samsung.com>");
MODULE_LICENSE("GPL");