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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/*
* sec haptic driver for common code
*
* 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.
*/
#define pr_fmt(fmt) "[VIB] " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/sec_sysfs.h>
#include <linux/sec_haptic.h>
#if defined(CONFIG_SSP_MOTOR_CALLBACK)
#include <linux/ssp_motorcallback.h>
#endif
static struct sec_haptic_drvdata *pddata;
static int sec_haptic_set_frequency(struct sec_haptic_drvdata *ddata,
int num)
{
if (num >= 0 && num < ddata->multi_frequency) {
ddata->period = ddata->multi_freq_period[num];
ddata->duty = ddata->max_duty = ddata->multi_freq_duty[num];
ddata->intensity = MAX_INTENSITY;
ddata->freq_num = num;
} else if (num >= HAPTIC_ENGINE_FREQ_MIN && num <= HAPTIC_ENGINE_FREQ_MAX) {
ddata->period = MULTI_FREQ_DIVIDER / num;
ddata->duty = ddata->max_duty = (ddata->period * ddata->ratio) / 100;
ddata->intensity = MAX_INTENSITY;
ddata->freq_num = num;
} else {
pr_err("%s out of range %d\n", __func__, num);
return -EINVAL;
}
return 0;
}
static int sec_haptic_set_intensity(struct sec_haptic_drvdata *ddata,
int intensity)
{
int duty = ddata->period >> 1;
if (intensity < -(MAX_INTENSITY) || MAX_INTENSITY < intensity) {
pr_err("%s out of range %d\n", __func__, intensity);
return -EINVAL;
}
if (intensity == MAX_INTENSITY)
duty = ddata->max_duty;
else if (intensity == -(MAX_INTENSITY))
duty = ddata->period - ddata->max_duty;
else if (intensity != 0)
duty += (ddata->max_duty - duty) * intensity / MAX_INTENSITY;
else if (intensity == 0)
return ddata->set_intensity(ddata->data, duty);
ddata->intensity = intensity;
ddata->duty = duty;
return ddata->set_intensity(ddata->data, duty);
}
static int sec_haptic_enable(struct sec_haptic_drvdata *ddata, bool en)
{
int ret = ddata->enable(ddata->data, en);
#if defined(CONFIG_SSP_MOTOR_CALLBACK)
if (en && ddata->intensity > 0)
setSensorCallback(true, ddata->timeout);
else
setSensorCallback(false, 0);
#endif
return ret;
}
static void sec_haptic_set_overdrive(struct sec_haptic_drvdata *ddata, bool en)
{
ddata->ratio = en ? ddata->overdrive_ratio : ddata->normal_ratio;
}
static int sec_haptic_boost(struct sec_haptic_drvdata *ddata, bool en)
{
return ddata->boost(ddata->data, en);
}
static void sec_haptic_engine_run_packet(struct sec_haptic_drvdata *ddata,
struct vib_packet packet)
{
int frequency = packet.freq;
int intensity = packet.intensity;
int overdrive = packet.overdrive;
if (!ddata->f_packet_en) {
pr_err("haptic packet is empty\n");
return;
}
sec_haptic_set_overdrive(ddata, overdrive);
sec_haptic_set_frequency(ddata, frequency);
if (intensity) {
sec_haptic_set_intensity(ddata, intensity);
if (!ddata->packet_running) {
pr_info("[haptic engine] motor run\n");
sec_haptic_enable(ddata, true);
}
ddata->packet_running = true;
} else {
if (ddata->packet_running) {
pr_info("[haptic engine] motor stop\n");
sec_haptic_enable(ddata, false);
}
ddata->packet_running = false;
sec_haptic_set_intensity(ddata, intensity);
}
pr_info("%s [%d] freq:%d, intensity:%d, time:%d ratio: %d\n", __func__,
ddata->packet_cnt, frequency, intensity, ddata->timeout, ddata->ratio);
}
static void timed_output_enable(struct sec_haptic_drvdata *ddata, unsigned int value)
{
struct hrtimer *timer = &ddata->timer;
int ret = 0;
kthread_flush_worker(&ddata->kworker);
ret = hrtimer_cancel(timer);
value = min_t(int, value, (int)ddata->max_timeout);
if (value) {
mutex_lock(&ddata->mutex);
if (ddata->f_packet_en) {
ddata->packet_running = false;
ddata->timeout = ddata->test_pac[0].time;
sec_haptic_engine_run_packet(ddata, ddata->test_pac[0]);
} else {
ddata->timeout = value;
ret = sec_haptic_set_intensity(ddata, ddata->intensity);
if (ret)
pr_err("%s: error to enable pwm %d\n", __func__, ret);
ret = sec_haptic_enable(ddata, true);
if (ret)
pr_err("%s: error to enable i2c reg %d\n", __func__, ret);
if (ddata->multi_frequency)
pr_info("%d %u %ums\n", ddata->freq_num, ddata->duty, ddata->timeout);
else
pr_info("%u %ums\n", ddata->duty, ddata->timeout);
}
mutex_unlock(&ddata->mutex);
hrtimer_start(timer,
ns_to_ktime((u64)ddata->timeout * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
} else {
mutex_lock(&ddata->mutex);
ddata->f_packet_en = false;
ddata->packet_cnt = 0;
ddata->packet_size = 0;
sec_haptic_enable(ddata, false);
sec_haptic_set_intensity(ddata, 0);
pr_info("off\n");
mutex_unlock(&ddata->mutex);
}
}
static enum hrtimer_restart haptic_timer_func(struct hrtimer *timer)
{
struct sec_haptic_drvdata *ddata
= container_of(timer, struct sec_haptic_drvdata, timer);
pr_info("%s\n", __func__);
kthread_queue_work(&ddata->kworker, &ddata->kwork);
return HRTIMER_NORESTART;
}
static void sec_haptic_work(struct kthread_work *work)
{
struct sec_haptic_drvdata *ddata
= container_of(work, struct sec_haptic_drvdata, kwork);
struct hrtimer *timer = &ddata->timer;
mutex_lock(&ddata->mutex);
if (ddata->f_packet_en) {
ddata->packet_cnt++;
if (ddata->packet_cnt < ddata->packet_size) {
sec_haptic_engine_run_packet(ddata, ddata->test_pac[ddata->packet_cnt]);
ddata->timeout = ddata->test_pac[ddata->packet_cnt].time;
hrtimer_start(timer, ns_to_ktime((u64)ddata->timeout * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
goto unlock_without_vib_off;
} else {
ddata->f_packet_en = false;
ddata->packet_cnt = 0;
ddata->packet_size = 0;
}
}
sec_haptic_enable(ddata, false);
sec_haptic_set_intensity(ddata, 0);
pr_info("timer off\n");
unlock_without_vib_off:
mutex_unlock(&ddata->mutex);
}
static ssize_t duty_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int ret;
u16 duty;
ret = kstrtou16(buf, 0, &duty);
if (ret != 0) {
dev_err(dev, "fail to get duty.\n");
return count;
}
ddata->duty = ddata->max_duty = ddata->multi_freq_duty[0] = duty;
ddata->intensity = MAX_INTENSITY;
return count;
}
static ssize_t duty_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
return snprintf(buf, VIB_BUFSIZE,
"duty: %u\n", ddata->duty);
}
static ssize_t period_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int ret;
u16 period;
ret = kstrtou16(buf, 0, &period);
if (ret != 0) {
dev_err(dev, "fail to get period.\n");
return count;
}
ddata->period = ddata->multi_freq_period[0] = period;
return count;
}
static ssize_t period_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
return snprintf(buf, VIB_BUFSIZE,
"period: %u\n", ddata->period);
}
/* below nodes is SAMSUNG specific nodes */
static DEVICE_ATTR_RW(duty);
static DEVICE_ATTR_RW(period);
static struct attribute *sec_haptic_attributes[] = {
&dev_attr_duty.attr,
&dev_attr_period.attr,
NULL,
};
static struct attribute_group sec_haptic_attr_group = {
.attrs = sec_haptic_attributes,
};
static ssize_t intensity_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int intensity = 0, ret = 0;
ret = kstrtoint(buf, 0, &intensity);
if (ret) {
pr_err("fail to get intensity\n");
return -EINVAL;
}
pr_info("%s %d\n", __func__, intensity);
if ((intensity < 0) || (intensity > MAX_INTENSITY)) {
pr_err("[VIB]: %s out of range\n", __func__);
return -EINVAL;
}
ddata->intensity = intensity;
return count;
}
static ssize_t intensity_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
return snprintf(buf, VIB_BUFSIZE,
"intensity: %u\n", ddata->intensity);
}
static ssize_t force_touch_intensity_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int intensity = 0, ret = 0;
ret = kstrtoint(buf, 0, &intensity);
if (ret) {
pr_err("fail to get intensity\n");
return -EINVAL;
}
pr_info("%s %d\n", __func__, intensity);
if ((intensity < 0) || (intensity > MAX_INTENSITY)) {
pr_err("[VIB]: %s out of range\n", __func__);
return -EINVAL;
}
ddata->force_touch_intensity = intensity;
return count;
}
static ssize_t force_touch_intensity_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
return snprintf(buf, VIB_BUFSIZE,
"force touch intensity: %u\n", ddata->force_touch_intensity);
}
static ssize_t multi_freq_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int num, ret;
ret = kstrtoint(buf, 0, &num);
if (ret) {
pr_err("fail to get frequency\n");
return -EINVAL;
}
pr_info("%s %d\n", __func__, num);
ret = sec_haptic_set_frequency(ddata, num);
if (ret)
return ret;
return count;
}
static ssize_t multi_freq_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
return snprintf(buf, VIB_BUFSIZE,
"frequency: %d\n", ddata->freq_num);
}
static ssize_t haptic_engine_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int i = 0, _data = 0, tmp = 0;
if (sscanf(buf, "%6d", &_data) != 1)
return count;
if (_data > PACKET_MAX_SIZE * VIB_PACKET_MAX)
pr_info("%s, [%d] packet size over\n", __func__, _data);
else {
ddata->packet_size = _data / VIB_PACKET_MAX;
ddata->packet_cnt = 0;
ddata->f_packet_en = true;
buf = strstr(buf, " ");
for (i = 0; i < ddata->packet_size; i++) {
for (tmp = 0; tmp < VIB_PACKET_MAX; tmp++) {
if (buf == NULL) {
pr_err("%s, buf is NULL, Please check packet data again\n",
__func__);
ddata->f_packet_en = false;
return count;
}
if (sscanf(buf++, "%6d", &_data) != 1) {
pr_err("%s, packet data error, Please check packet data again\n",
__func__);
ddata->f_packet_en = false;
return count;
}
switch (tmp) {
case VIB_PACKET_TIME:
ddata->test_pac[i].time = _data;
break;
case VIB_PACKET_INTENSITY:
ddata->test_pac[i].intensity = _data;
break;
case VIB_PACKET_FREQUENCY:
ddata->test_pac[i].freq = _data;
break;
case VIB_PACKET_OVERDRIVE:
ddata->test_pac[i].overdrive = _data;
break;
}
buf = strstr(buf, " ");
}
}
}
return count;
}
static ssize_t haptic_engine_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int i = 0;
size_t size = 0;
for (i = 0; i < ddata->packet_size && ddata->f_packet_en &&
((4 * VIB_BUFSIZE + size) < PAGE_SIZE); i++) {
size += snprintf(&buf[size], VIB_BUFSIZE, "%u,", ddata->test_pac[i].time);
size += snprintf(&buf[size], VIB_BUFSIZE, "%u,", ddata->test_pac[i].intensity);
size += snprintf(&buf[size], VIB_BUFSIZE, "%u,", ddata->test_pac[i].freq);
size += snprintf(&buf[size], VIB_BUFSIZE, "%u,", ddata->test_pac[i].overdrive);
}
return size;
}
static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
struct hrtimer *timer = &ddata->timer;
int remaining = 0;
if (hrtimer_active(timer)) {
ktime_t remain = hrtimer_get_remaining(timer);
struct timeval t = ktime_to_timeval(remain);
remaining = t.tv_sec * 1000 + t.tv_usec / 1000;
}
return sprintf(buf, "%d\n", remaining);
}
static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t size)
{
struct sec_haptic_drvdata *ddata = dev_get_drvdata(dev);
int value;
int ret;
ret = kstrtoint(buf, 0, &value);
if (ret != 0)
return -EINVAL;
timed_output_enable(ddata, value);
return size;
}
static DEVICE_ATTR_RW(haptic_engine);
static DEVICE_ATTR_RW(multi_freq);
static DEVICE_ATTR_RW(intensity);
static DEVICE_ATTR_RW(force_touch_intensity);
static DEVICE_ATTR_RW(enable);
static struct attribute *timed_output_attributes[] = {
&dev_attr_intensity.attr,
&dev_attr_force_touch_intensity.attr,
&dev_attr_enable.attr,
NULL,
};
static struct attribute_group timed_output_attr_group = {
.attrs = timed_output_attributes,
};
static struct attribute *multi_freq_attributes[] = {
&dev_attr_haptic_engine.attr,
&dev_attr_multi_freq.attr,
NULL,
};
static struct attribute_group multi_freq_attr_group = {
.attrs = multi_freq_attributes,
};
int sec_haptic_register(struct sec_haptic_drvdata *ddata)
{
struct task_struct *kworker_task;
int ret = 0;
mutex_init(&ddata->mutex);
kthread_init_worker(&ddata->kworker);
kworker_task = kthread_run(kthread_worker_fn,
&ddata->kworker, "sec_haptic");
if (IS_ERR(kworker_task)) {
pr_err("Failed to create message pump task\n");
ret = -ENOMEM;
goto err_kthread;
}
kthread_init_work(&ddata->kwork, sec_haptic_work);
hrtimer_init(&ddata->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ddata->timer.function = haptic_timer_func;
ddata->to_class = class_create(THIS_MODULE, "timed_output");
if (IS_ERR(ddata->to_class)) {
ret = PTR_ERR(ddata->to_class);
goto err_class_create;
}
ddata->to_dev = device_create(ddata->to_class, NULL,
MKDEV(0, 0), ddata, "vibrator");
if (IS_ERR(ddata->dev)) {
ret = PTR_ERR(ddata->dev);
goto err_device_create;
}
ret = sysfs_create_group(&ddata->to_dev->kobj, &timed_output_attr_group);
if (ret) {
ret = -ENODEV;
pr_err("Failed to create sysfs %d\n", ret);
goto err_sysfs;
}
if (ddata->multi_frequency) {
ret = sysfs_create_group(&ddata->to_dev->kobj, &multi_freq_attr_group);
if (ret) {
ret = -ENODEV;
pr_err("Failed to create sysfs %d\n", ret);
goto err_sysfs2;
}
}
ddata->dev = sec_device_create(ddata, "motor");
if (IS_ERR(ddata->dev)) {
ret = -ENODEV;
pr_err("Failed to create device %d\n", ret);
goto exit_sec_devices;
}
ret = sysfs_create_group(&ddata->dev->kobj, &sec_haptic_attr_group);
if (ret) {
ret = -ENODEV;
pr_err("Failed to create sysfs %d\n", ret);
goto err_sysfs3;
}
pddata = ddata;
return ret;
err_sysfs3:
sysfs_remove_group(&ddata->dev->kobj, &sec_haptic_attr_group);
exit_sec_devices:
sysfs_remove_group(&ddata->to_dev->kobj, &multi_freq_attr_group);
err_sysfs2:
sysfs_remove_group(&ddata->to_dev->kobj, &timed_output_attr_group);
err_sysfs:
sec_device_destroy(ddata->dev->devt);
err_device_create:
err_class_create:
err_kthread:
return ret;
}
int sec_haptic_unregister(struct sec_haptic_drvdata *ddata)
{
sec_haptic_boost(ddata, BOOST_OFF);
sysfs_remove_group(&ddata->dev->kobj, &sec_haptic_attr_group);
sysfs_remove_group(&ddata->to_dev->kobj, &multi_freq_attr_group);
sysfs_remove_group(&ddata->to_dev->kobj, &timed_output_attr_group);
sec_device_destroy(ddata->dev->devt);
sec_haptic_enable(ddata, false);
device_destroy(ddata->to_class, MKDEV(0, 0));
class_destroy(ddata->to_class);
pddata = NULL;
kfree(ddata);
return 0;
}
extern int haptic_homekey_press(void)
{
struct sec_haptic_drvdata *ddata = pddata;
struct hrtimer *timer;
if (ddata == NULL)
return -1;
timer = &ddata->timer;
sec_haptic_boost(ddata, BOOST_ON);
mutex_lock(&ddata->mutex);
ddata->timeout = HOMEKEY_DURATION;
sec_haptic_set_frequency(ddata, HOMEKEY_PRESS_FREQ);
sec_haptic_set_intensity(ddata, ddata->force_touch_intensity);
sec_haptic_enable(ddata, true);
pr_info("%s freq:%d, intensity:%d, time:%d\n", __func__,
HOMEKEY_PRESS_FREQ, ddata->force_touch_intensity, ddata->timeout);
mutex_unlock(&ddata->mutex);
hrtimer_start(timer,
ns_to_ktime((u64)ddata->timeout * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
return 0;
}
extern int haptic_homekey_release(void)
{
struct sec_haptic_drvdata *ddata = pddata;
struct hrtimer *timer;
if (ddata == NULL)
return -1;
timer = &ddata->timer;
mutex_lock(&ddata->mutex);
ddata->timeout = HOMEKEY_DURATION;
sec_haptic_set_frequency(ddata, HOMEKEY_RELEASE_FREQ);
sec_haptic_set_intensity(ddata, ddata->force_touch_intensity);
sec_haptic_enable(ddata, true);
pr_info("%s freq:%d, intensity:%d, time:%d\n", __func__,
HOMEKEY_RELEASE_FREQ, ddata->force_touch_intensity, ddata->timeout);
mutex_unlock(&ddata->mutex);
hrtimer_start(timer,
ns_to_ktime((u64)ddata->timeout * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
return 0;
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("sec haptic driver");
|