summaryrefstree
path: root/kernel/drivers/motor/max77833_haptic.c
blob: 705ba58e05ed6a7642baa41929313647350a69da (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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
/*
 * haptic motor driver for max77833 - max77673_haptic.c
 *
 * Copyright (C) 2011 ByungChang Cha <bc.cha@samsung.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.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/timed_output.h>
#include <linux/hrtimer.h>
#include <linux/pwm.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/i2c.h>
#include <linux/regulator/consumer.h>
#include <linux/mfd/max77833.h>
#include <linux/mfd/max77833-private.h>
#include <plat/devs.h>
#include <linux/sec_sysfs.h>
#include <linux/power_supply.h>
#include <linux/kthread.h>

#define TEST_MODE_TIME	10000
#define MAX_INTENSITY	10000

#define MOTOR_LRA			(1<<6)
#define MOTOR_EN_PWM	0x23

#define EXT_PWM				(0<<5)
#define DIVIDER_128			(1<<1)
#define MAX77833_REG_MAINCTRL1_MRDBTMER_MASK	(0x7)
#define MAX77833_REG_MAINCTRL1_MREN		(1 << 3)
#define MAX77833_REG_MAINCTRL1_BIASEN		(1 << 7)

static struct device *motor_dev;

struct max77833_haptic_data {
	struct max77833_dev *max77833;
	struct i2c_client *i2c;
	struct max77833_haptic_platform_data *pdata;

	struct pwm_device *pwm;
	struct timed_output_dev tout_dev;
	struct hrtimer timer;
	struct kthread_worker kworker;
	struct kthread_work kwork;
	spinlock_t lock;
	bool running;
	u32 intensity;
	u32 timeout;
	int duty;
};

static struct max77833_haptic_data *g_hap_data;
static int prev_duty;
static int prev_temperature_level;

static int temperature_check(void)
{
	struct power_supply *psy;
	union power_supply_propval temp;
	int value;
	int ret;

	psy = power_supply_get_by_name("ac");
	if (!psy)
		return -1;
	ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
	if (ret)
		return -2;
	value = temp.intval / 10;

	printk("[VIB] %s : %d degree!\n",__func__, value);

	if (value >= 50) return 2;	/* high_temperature */
	else if (value <= 0) return 0;	/* low_temperature */
	else if (value > 0 && value < 50) return 1;	/* room_temperature */

	return value;
}

static void max77833_haptic_i2c(struct max77833_haptic_data *hap_data, bool en)
{
	int ret;

	pr_info("[VIB] %s %d\n", __func__, en);

	if (en) {
		ret = max77833_update_reg(hap_data->i2c,
				MAX77833_LRA_ENABLE_2, 0xdf, MOTOR_EN_PWM);
	} else {
		ret = max77833_update_reg(hap_data->i2c,
				MAX77833_LRA_ENABLE_2, 0x20, MOTOR_EN_PWM);
	}
	if (ret)
		pr_err("[VIB] i2c MOTOR_EN_PWM update error %d\n", ret);
}

static int haptic_get_time(struct timed_output_dev *tout_dev)
{
	struct max77833_haptic_data *hap_data
		= container_of(tout_dev, struct max77833_haptic_data, tout_dev);

	struct hrtimer *timer = &hap_data->timer;
	if (hrtimer_active(timer)) {
		ktime_t remain = hrtimer_get_remaining(timer);
		struct timeval t = ktime_to_timeval(remain);
		return t.tv_sec * 1000 + t.tv_usec / 1000;
	}
	return 0;
}

static void haptic_enable(struct timed_output_dev *tout_dev, int value)
{
	struct max77833_haptic_data *hap_data
		= container_of(tout_dev, struct max77833_haptic_data, tout_dev);

	struct hrtimer *timer = &hap_data->timer;
	unsigned long flags;
	int error = 0, temperature_level;

	flush_kthread_worker(&hap_data->kworker);
	hrtimer_cancel(timer);
	hap_data->timeout = value;

	if (value > 0) {
		if (!hap_data->running) {
			pwm_config(hap_data->pwm, hap_data->duty,
					hap_data->pdata->period);
			pwm_enable(hap_data->pwm);		

			temperature_level = temperature_check();
			if (temperature_level != prev_temperature_level) {
				switch(temperature_level)
				{
					case 0:
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MIN_FREQ_LOW, hap_data->pdata->auto_res_min_low_low_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MIN_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MAX_FREQ_LOW, hap_data->pdata->auto_res_max_low_low_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MAX_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_INIT_GUESS_LOW, hap_data->pdata->auto_res_init_low_low_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_INIT_GUESS_LOW, error);
						}
						break;
					case 1:
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MIN_FREQ_LOW, hap_data->pdata->auto_res_min_low);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MIN_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MAX_FREQ_LOW, hap_data->pdata->auto_res_max_low);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MAX_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_INIT_GUESS_LOW, hap_data->pdata->auto_res_init_low);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_INIT_GUESS_LOW, error);
						}
						break;
					case 2:
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MIN_FREQ_LOW, hap_data->pdata->auto_res_min_low_high_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MIN_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_MAX_FREQ_LOW, hap_data->pdata->auto_res_max_low_high_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_MAX_FREQ_LOW, error);
						}
						error = max77833_write_reg(hap_data->i2c,
							MAX77833_AUTORES_INIT_GUESS_LOW, hap_data->pdata->auto_res_init_low_high_temp);
						if (error < 0) {
							pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
							__func__, MAX77833_AUTORES_INIT_GUESS_LOW, error);
						}
						break;
					default:
						pr_err("[VIB] %s Failed to read temperature [%d]\n",
						__func__, temperature_level);
						break;
				}
			}
			prev_temperature_level = temperature_level;

			max77833_haptic_i2c(hap_data, true);
			hap_data->running = true;
		}

		spin_lock_irqsave(&hap_data->lock, flags);
		pr_debug("[VIB] %s value %d\n", __func__, value);
		value = min(value, (int)hap_data->pdata->max_timeout);
		hrtimer_start(timer, ns_to_ktime((u64)value * NSEC_PER_MSEC),
				HRTIMER_MODE_REL);
		spin_unlock_irqrestore(&hap_data->lock, flags);
	}
	else
		queue_kthread_work(&hap_data->kworker, &hap_data->kwork);
}

static enum hrtimer_restart haptic_timer_func(struct hrtimer *timer)
{
	struct max77833_haptic_data *hap_data
		= container_of(timer, struct max77833_haptic_data, timer);

	hap_data->timeout = 0;
	queue_kthread_work(&hap_data->kworker, &hap_data->kwork);
	return HRTIMER_NORESTART;
}

static int vibetonz_clk_on(struct device *dev, bool en)
{
	struct clk *vibetonz_clk = NULL;

#if defined(CONFIG_OF)
	struct device_node *np;

	np = of_find_node_by_name(NULL,"pwm");
	if (np == NULL) {
		pr_err("[VIB] %s : pwm error to get dt node\n", __func__);
		return -EINVAL;
	}

	vibetonz_clk = of_clk_get_by_name(np, "gate_timers");
	if (!vibetonz_clk) {
		pr_info("[VIB] %s fail to get the vibetonz_clk\n", __func__);
		return -EINVAL;
	}
#else
	vibetonz_clk = clk_get(dev, "timers");
#endif
	pr_info("[VIB] DEV NAME %s %lu\n",
			dev_name(dev), clk_get_rate(vibetonz_clk));

	if (IS_ERR(vibetonz_clk)) {
		pr_err("[VIB] failed to get clock for the motor\n");
		goto err_clk_get;
	}

	if (en)
		clk_enable(vibetonz_clk);
	else
		clk_disable(vibetonz_clk);

	clk_put(vibetonz_clk);
	return 0;

err_clk_get:
	clk_put(vibetonz_clk);
	return -EINVAL;
}

static void haptic_work(struct kthread_work *work)
{
	struct max77833_haptic_data *hap_data
		= container_of(work, struct max77833_haptic_data, kwork);

	pr_info("[VIB] %s\n", __func__);

	if (hap_data->running) {
		pwm_disable(hap_data->pwm);
		max77833_haptic_i2c(hap_data, false);
		hap_data->running = false;
	}
}

#if defined(CONFIG_OF)
static struct max77833_haptic_platform_data *of_max77833_haptic_dt(struct device *dev)
{
	struct device_node *np_root = dev->parent->of_node;
	struct device_node *np_haptic;
	struct max77833_haptic_platform_data *pdata;
	u32 temp;
	int ret;

	pdata = kzalloc(sizeof(struct max77833_haptic_platform_data), GFP_KERNEL);
	if (!pdata) {
		pr_err("%s: failed to allocate driver data\n", __func__);
		return NULL;
	}

	printk("%s : start dt parsing\n", __func__);

	np_haptic = of_find_node_by_name(np_root, "haptic");
	if (np_haptic == NULL) {
		pr_err("[VIB] %s : error to get dt node\n", __func__);
		goto err_parsing_dt;
	}

	ret = of_property_read_u32(np_haptic, "haptic,max_timeout", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node max_timeout\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->max_timeout = (u16)temp;

	ret = of_property_read_u32(np_haptic, "haptic,duty", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node duty\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->duty = (u16)temp;

	ret = of_property_read_u32(np_haptic, "haptic,period", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node period\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->period = (u16)temp;

	ret = of_property_read_u32(np_haptic, "haptic,pwm_id", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node pwm_id\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->pwm_id = (u16)temp;

	/* auto resonance */
	ret = of_property_read_u32(np_haptic, "haptic,auto_res_min_low", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_min_low\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_min_low = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_min_low_high_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_min_low_high_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_min_low_high_temp = (u8)temp;

        ret = of_property_read_u32(np_haptic, "haptic,auto_res_min_low_low_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_min_low_low_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_min_low_low_temp = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_max_low", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_max_low\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_max_low = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_max_low_high_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_max_low_high_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_max_low_high_temp = (u8)temp;

        ret = of_property_read_u32(np_haptic, "haptic,auto_res_max_low_low_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_max_low_low_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_max_low_low_temp = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_init_low", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_init_low\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_init_low = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_init_low_high_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_init_low_high_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_init_low_high_temp = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_init_low_low_temp", &temp);
        if (IS_ERR_VALUE(ret)) {
                pr_err("[VIB] %s : error to get dt node auto_res_init_low_low_temp\n", __func__);
                goto err_parsing_dt;
        } else
                pdata->auto_res_init_low_low_temp = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_min_high", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_min_high\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_min_high = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_max_high", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_max_high\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_max_high = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_init_high", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_init_high\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_init_high = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_lock_window", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_lock_window\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_lock_window = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_update_freq", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_update_freq\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_update_freq = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,auto_res_enable", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node auto_res_enable\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->auto_res_enable = (u8)temp;

	ret = of_property_read_u32(np_haptic, "haptic,nominal_strength", &temp);
	if (IS_ERR_VALUE(ret)) {
		pr_err("[VIB] %s : error to get dt node nominal_strength\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->nominal_strength = (u8)temp;

	/* debugging */
	printk("[VIB] %s : max_timeout = %d\n", __func__, pdata->max_timeout);
	printk("[VIB] %s : duty = %d\n", __func__, pdata->duty);
	printk("[VIB] %s : period = %d\n", __func__, pdata->period);
	printk("[VIB] %s : pwm_id = %d\n", __func__, pdata->pwm_id);
	printk("[VIB] %s : auto_res_min_low = 0x%x\n", __func__, pdata->auto_res_min_low);
	printk("[VIB] %s : auto_res_max_low = 0x%x\n", __func__, pdata->auto_res_max_low);
	printk("[VIB] %s : auto_res_init_low = 0x%xd\n", __func__, pdata->auto_res_init_low);
	printk("[VIB] %s : auto_res_min_high = 0x%x\n", __func__, pdata->auto_res_min_high);
	printk("[VIB] %s : auto_res_max_high = 0x%x\n", __func__, pdata->auto_res_max_high);
	printk("[VIB] %s : auto_res_init_high = 0x%x\n", __func__, pdata->auto_res_init_high);
	printk("[VIB] %s : auto_res_lock_window = 0x%x\n", __func__, pdata->auto_res_lock_window);
	printk("[VIB] %s : auto_res_update_freq = 0x%x\n", __func__, pdata->auto_res_update_freq);
	printk("[VIB] %s : auto_res_enable = 0x%x\n", __func__, pdata->auto_res_enable);
	printk("[VIB] %s : nominal_strength = 0x%x\n", __func__, pdata->nominal_strength);

	pdata->init_hw = NULL;
	pdata->motor_en = NULL;

	return pdata;

err_parsing_dt:
	kfree(pdata);
	return NULL;
}
#endif
static ssize_t store_duty(struct device *dev,
	struct device_attribute *devattr, const char *buf, size_t count)
{
	char buff[10] = {0,};
	int cnt, ret;
	u16 duty;

	cnt = count;
	cnt = (buf[cnt-1] == '\n') ? cnt-1 : cnt;
	memcpy(buff, buf, cnt);
	buff[cnt] = '\0';

	ret = kstrtou16(buff, 0, &duty);
	if (ret != 0) {
		dev_err(dev, "[VIB] fail to get duty.\n");
		return count;
	}
	g_hap_data->pdata->duty = (u16)duty;

	return count;
}

static ssize_t store_period(struct device *dev,
	struct device_attribute *devattr, const char *buf, size_t count)
{
	char buff[10] = {0,};
	int cnt, ret;
	u16 period;

	cnt = count;
	cnt = (buf[cnt-1] == '\n') ? cnt-1 : cnt;
	memcpy(buff, buf, cnt);
	buff[cnt] = '\0';

	ret = kstrtou16(buff, 0, &period);
	if (ret != 0) {
		dev_err(dev, "[VIB] fail to get period.\n");
		return count;
	}
	g_hap_data->pdata->period = (u16)period;

	return count;
}

static ssize_t show_duty_period(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "duty: %u, period%u\n", g_hap_data->pdata->duty,
						g_hap_data->pdata->period);
}

/* below nodes is SAMSUNG specific nodes */
static DEVICE_ATTR(set_duty, 0220, NULL, store_duty);
static DEVICE_ATTR(set_period, 0220, NULL, store_period);
static DEVICE_ATTR(show_duty_period, 0440, show_duty_period, NULL);

static struct attribute *sec_motor_attributes[] = {
	&dev_attr_set_duty.attr,
	&dev_attr_set_period.attr,
	&dev_attr_show_duty_period.attr,
	NULL,
};

static struct attribute_group sec_motor_attr_group = {
	.attrs = sec_motor_attributes,
};

static ssize_t intensity_store(struct device *dev,
	struct device_attribute *devattr, const char *buf, size_t count)
{
	struct timed_output_dev *tdev = dev_get_drvdata(dev);
	struct max77833_haptic_data *drvdata
		= container_of(tdev, struct max77833_haptic_data, tout_dev);
	int duty = drvdata->pdata->period >> 1;
	int intensity = 0, ret = 0;

	ret = kstrtoint(buf, 0, &intensity);

	if (intensity < 0 || MAX_INTENSITY < intensity) {
		pr_err("out of rage\n");
		return -EINVAL;
	}

	if (MAX_INTENSITY == intensity)
		duty = drvdata->pdata->duty;
	else if (0 != intensity) {
		long long tmp = drvdata->pdata->duty >> 1;

		tmp *= (intensity / 100);
		duty += (int)(tmp / 100);
	}

	drvdata->intensity = intensity;
	drvdata->duty = duty;

	pwm_config(drvdata->pwm, duty, drvdata->pdata->period);

	return count;
}

static ssize_t intensity_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct timed_output_dev *tdev = dev_get_drvdata(dev);
	struct max77833_haptic_data *drvdata
		= container_of(tdev, struct max77833_haptic_data, tout_dev);

	return sprintf(buf, "intensity: %u\n",
			(drvdata->intensity * 100));
}

static DEVICE_ATTR(intensity, 0660, intensity_show, intensity_store);

static int max77833_haptic_probe(struct platform_device *pdev)
{
	int error = 0;
	struct max77833_dev *max77833 = dev_get_drvdata(pdev->dev.parent);
	struct max77833_platform_data *max77833_pdata
		= dev_get_platdata(max77833->dev);
	struct max77833_haptic_platform_data *pdata
		= max77833_pdata->haptic_data;
	struct max77833_haptic_data *hap_data;
	struct task_struct *kworker_task;

	pr_info("[VIB] ++ %s\n", __func__);

#if defined(CONFIG_OF)
	if (pdata == NULL) {
		pdata = of_max77833_haptic_dt(&pdev->dev);
		if (!pdata) {
			pr_err("[VIB] max77833-haptic : %s not found haptic dt!\n",
					__func__);
			return -1;
		}
	}
#else
	if (pdata == NULL) {
		pr_err("[VIB] %s: no pdata\n", __func__);
		return -ENODEV;
	}
#endif /* CONFIG_OF */

	hap_data = kzalloc(sizeof(struct max77833_haptic_data), GFP_KERNEL);
	if (!hap_data) {
		pr_err("[VIB] %s: no hap_pdata\n", __func__);
		kfree(pdata);
		return -ENOMEM;
	}
	platform_set_drvdata(pdev, hap_data);
	g_hap_data = hap_data;
	hap_data->max77833 = max77833;
	hap_data->i2c = max77833->i2c;
	hap_data->pdata = pdata;

	init_kthread_worker(&hap_data->kworker);
	kworker_task = kthread_run(kthread_worker_fn,
		   &hap_data->kworker, "max77843_haptic");
	if (IS_ERR(kworker_task)) {
		pr_err("Failed to create message pump task\n");
		error = -ENOMEM;
		goto err_kthread;
	}
	init_kthread_work(&hap_data->kwork, haptic_work);

	spin_lock_init(&(hap_data->lock));

	hap_data->pwm = pwm_request(hap_data->pdata->pwm_id, "vibrator");
	if (IS_ERR(hap_data->pwm)) {
		error = -EFAULT;
		pr_err("[VIB] Failed to request pwm, err num: %d\n", error);
		goto err_pwm_request;
	}

	pwm_config(hap_data->pwm, pdata->period / 2, pdata->period);
	prev_duty = hap_data->pdata->period / 2;
	vibetonz_clk_on(&pdev->dev, true);

	/* hrtimer init */
	hrtimer_init(&hap_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	hap_data->timer.function = haptic_timer_func;

	/* timed_output_dev init*/
	hap_data->tout_dev.name = "vibrator";
	hap_data->tout_dev.get_time = haptic_get_time;
	hap_data->tout_dev.enable = haptic_enable;

	motor_dev = sec_device_create(hap_data, "motor");
	if (IS_ERR(motor_dev)) {
		error = -ENODEV;
		pr_err("[VIB] Failed to create device\
				for samsung specific motor, err num: %d\n", error);
		goto exit_sec_devices;
	}
	error = sysfs_create_group(&motor_dev->kobj, &sec_motor_attr_group);
	if (error) {
		error = -ENODEV;
		pr_err("[VIB] Failed to create sysfs group\
				for samsung specific motor, err num: %d\n", error);
		goto exit_sysfs;
	}

#ifdef CONFIG_ANDROID_TIMED_OUTPUT
	error = timed_output_dev_register(&hap_data->tout_dev);
	if (error < 0) {
		error = -EFAULT;
		pr_err("[VIB] Failed to register timed_output : %d\n", error);
		goto err_timed_output_register;
	}
#endif

	error = sysfs_create_file(&hap_data->tout_dev.dev->kobj,
				&dev_attr_intensity.attr);
	if (error < 0) {
		pr_err("Failed to register sysfs : %d\n", error);
		goto err_timed_output_register;
	}

	prev_temperature_level = 1;

	/* autoresonance range setting */
	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_CONFIG, 0x00);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_CONFIG, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_MIN_FREQ_LOW, pdata->auto_res_min_low);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_MIN_FREQ_LOW, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_MAX_FREQ_LOW, pdata->auto_res_max_low);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_MAX_FREQ_LOW, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_INIT_GUESS_LOW, pdata->auto_res_init_low);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_INIT_GUESS_LOW, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_NOMINAL_STRENGTH, pdata->nominal_strength);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_NOMINAL_STRENGTH, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_RES_MIN_FREQ_HIGH, pdata->auto_res_min_high);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_RES_MIN_FREQ_HIGH, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_RES_MAX_FREQ_HIGH, pdata->auto_res_max_high);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_RES_MAX_FREQ_HIGH, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_INIT_GUESS_HIGH, pdata->auto_res_init_high);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_INIT_GUESS_HIGH, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_LOCK_WINDOW, pdata->auto_res_lock_window);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_LOCK_WINDOW, error);
	}

	error = max77833_write_reg(hap_data->i2c,
		MAX77833_AUTORES_UPDATE_FREQ, pdata->auto_res_update_freq);
	if (error < 0) {
		pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
				__func__, MAX77833_AUTORES_UPDATE_FREQ, error);
	}

	if (!pdata->auto_res_enable) {
		error = max77833_write_reg(hap_data->i2c, MAX77833_OPTION_REG1, 0x11);
		if (error < 0) {
			pr_err("[VIB] %s Failed to write REG(0x%02x) [%d]\n",
					__func__, MAX77833_OPTION_REG1, error);
		}
	}
	return error;

err_timed_output_register:
	sysfs_remove_group(&motor_dev->kobj, &sec_motor_attr_group);
exit_sysfs:
	sec_device_destroy(motor_dev->devt);
exit_sec_devices:
	pwm_free(hap_data->pwm);
err_pwm_request:
err_kthread:
	kfree(hap_data);
	kfree(pdata);
	g_hap_data = NULL;
	return error;
}

static int __devexit max77833_haptic_remove(struct platform_device *pdev)
{
	struct max77833_haptic_data *data = platform_get_drvdata(pdev);
#ifdef CONFIG_ANDROID_TIMED_OUTPUT
	timed_output_dev_unregister(&data->tout_dev);
#endif

	sysfs_remove_group(&motor_dev->kobj, &sec_motor_attr_group);
	sec_device_destroy(motor_dev->devt);
	pwm_free(data->pwm);
	max77833_haptic_i2c(data, false);
	kfree(data);
	g_hap_data = NULL;

	return 0;
}

static int max77833_haptic_suspend(struct platform_device *pdev,
		pm_message_t state)
{
	struct max77833_haptic_data *data = platform_get_drvdata(pdev);
	pr_info("[VIB] %s\n", __func__);
	flush_kthread_worker(&data->kworker);
	hrtimer_cancel(&g_hap_data->timer);
	max77833_haptic_i2c(data, false);
	return 0;
}
static int max77833_haptic_resume(struct platform_device *pdev)
{
	pr_info("[VIB] %s\n", __func__);
	return 0;
}

static struct platform_driver max77833_haptic_driver = {
	.probe		= max77833_haptic_probe,
	.remove		= max77833_haptic_remove,
	.suspend	= max77833_haptic_suspend,
	.resume		= max77833_haptic_resume,
	.driver = {
		.name	= "max77833-haptic",
		.owner	= THIS_MODULE,
	},
};

static int __init max77833_haptic_init(void)
{
	pr_debug("[VIB] %s\n", __func__);
	return platform_driver_register(&max77833_haptic_driver);
}
module_init(max77833_haptic_init);

static void __exit max77833_haptic_exit(void)
{
	platform_driver_unregister(&max77833_haptic_driver);
}
module_exit(max77833_haptic_exit);

MODULE_AUTHOR("ByungChang Cha <bc.cha@samsung.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("max77833 haptic driver");