summaryrefslogtreecommitdiff
path: root/drivers/misc/sun6i-vibrator.c
blob: 814569077a554666698e1be550c9b2ee3d2d3a17 (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
/* Vibrator driver for sun6i platform 
 * ported from msm pmic vibrator driver
 *  by tom cubie <tangliang@reuuimllatech.com>
 *
 * Copyright (C) 2011 ReuuiMlla Technology.
 *
 * Copyright (C) 2008 HTC Corporation.
 * Copyright (C) 2007 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/hrtimer.h>
#include <linux/gpio.h>
#include <linux/module.h>

//#include <linux/ktime.h>

#include <mach/system.h>
#include <mach/hardware.h>
#include <mach/sys_config.h>
#include "../drivers/staging/android/timed_output.h"

static struct work_struct vibrator_work;
static struct hrtimer vibe_timer;
static spinlock_t vibe_lock;
static int vibe_state;
static int vibe_off;
static struct gpio_hdle {
	script_item_u	val;
	script_item_value_type_e  type;		
}vibe_gpio_hdle;

enum {
	DEBUG_INIT = 1U << 0,
	DEBUG_DATA_INFO = 1U << 1,
	DEBUG_SUSPEND = 1U << 2,
};
static u32 debug_mask = 0;
#define dprintk(level_mask, fmt, arg...)	if (unlikely(debug_mask & level_mask)) \
	printk(KERN_DEBUG fmt , ## arg)

module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);

static void set_sun6i_vibrator(int on)
{
	dprintk(DEBUG_DATA_INFO, "sun6i_vibrator on %d\n", on);
	if(on) {
		__gpio_set_value(vibe_gpio_hdle.val.gpio.gpio, !vibe_off);
	} else {
		__gpio_set_value(vibe_gpio_hdle.val.gpio.gpio, vibe_off);
	}
}

static void update_vibrator(struct work_struct *work)
{
	set_sun6i_vibrator(vibe_state);
}

static void vibrator_enable(struct timed_output_dev *dev, int value)
{
	unsigned long	flags;

	spin_lock_irqsave(&vibe_lock, flags);
	hrtimer_cancel(&vibe_timer);

	dprintk(DEBUG_DATA_INFO, "sun6i_vibrator enable %d\n", value);

	if (value <= 0)
		vibe_state = 0;
	else {
		value = (value > 15000 ? 15000 : value);
		vibe_state = 1;
		hrtimer_start(&vibe_timer,
			ktime_set(value / 1000, (value % 1000) * 1000000),
			HRTIMER_MODE_REL);
	}
	spin_unlock_irqrestore(&vibe_lock, flags);

	schedule_work(&vibrator_work);
}

static int vibrator_get_time(struct timed_output_dev *dev)
{
	struct timespec time_tmp;
	if (hrtimer_active(&vibe_timer)) {
		ktime_t r = hrtimer_get_remaining(&vibe_timer);
		time_tmp = ktime_to_timespec(r);
		//return r.tv.sec * 1000 + r.tv.nsec/1000000;
		return time_tmp.tv_sec* 1000 + time_tmp.tv_nsec/1000000;
	} else
		return 0;
}

static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
	vibe_state = 0;
	schedule_work(&vibrator_work);
	dprintk(DEBUG_DATA_INFO, "sun6i_vibrator timer expired\n");
	return HRTIMER_NORESTART;
}

static struct timed_output_dev sun6i_vibrator = {
	.name = "sun6i-vibrator",
	.get_time = vibrator_get_time,
	.enable = vibrator_enable,
};

static int __init sun6i_vibrator_init(void)
{
	int vibe_used;
	script_item_u	val;
	script_item_value_type_e  type;

	dprintk(DEBUG_INIT, "hello, sun6i_vibrator init\n");

	type = script_get_item("motor_para", "motor_used", &val);
	if (SCIRPT_ITEM_VALUE_TYPE_INT != type) {
		printk(KERN_ERR "%s script_parser_fetch \"motor_para\" motor_used = %d\n",
				__FUNCTION__, val.val);
		goto exit1;
	}
	vibe_used = val.val;

	if(!vibe_used) {
		printk(KERN_ERR"%s motor is not used in config\n", __FUNCTION__);
		goto exit1;
	}

	vibe_gpio_hdle.type = script_get_item("motor_para", "motor_shake", &(vibe_gpio_hdle.val));
	if(SCIRPT_ITEM_VALUE_TYPE_PIO != vibe_gpio_hdle.type) {
		printk(KERN_ERR "vibrator motor_shake type err!");
		goto exit1;
	}
	dprintk(DEBUG_INIT, "value is: gpio %d, mul_sel %d, pull %d, drv_level %d, data %d\n", 
	    vibe_gpio_hdle.val.gpio.gpio, vibe_gpio_hdle.val.gpio.mul_sel, vibe_gpio_hdle.val.gpio.pull, 
	    vibe_gpio_hdle.val.gpio.drv_level, vibe_gpio_hdle.val.gpio.data);

	vibe_off = vibe_gpio_hdle.val.gpio.data;
	dprintk(DEBUG_INIT, "vibe_off is %d\n", vibe_off);

	if(0 != gpio_request(vibe_gpio_hdle.val.gpio.gpio, NULL)) {
		printk(KERN_ERR "ERROR: vibe Gpio_request is failed\n");
	}
	
	if (0 != sw_gpio_setall_range(&vibe_gpio_hdle.val.gpio, 1)) {
		printk(KERN_ERR "vibe gpio set err!");
		goto exit;
	}

	__gpio_set_value(vibe_gpio_hdle.val.gpio.gpio, vibe_off);

	INIT_WORK(&vibrator_work, update_vibrator);

	spin_lock_init(&vibe_lock);
	vibe_state = 0;
	hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	vibe_timer.function = vibrator_timer_func;

	timed_output_dev_register(&sun6i_vibrator);

	dprintk(DEBUG_INIT, "sun6i_vibrator init end\n");

	return 0;
exit:
	gpio_free(vibe_gpio_hdle.val.gpio.gpio);
exit1:	
	return -1;
}

static void __exit sun6i_vibrator_exit(void)
{
	dprintk(DEBUG_INIT, "bye, sun6i_vibrator_exit\n");
	timed_output_dev_unregister(&sun6i_vibrator);
	gpio_free(vibe_gpio_hdle.val.gpio.gpio);	
}
module_init(sun6i_vibrator_init);
module_exit(sun6i_vibrator_exit);

/* Module information */
MODULE_DESCRIPTION("timed output vibrator device for sun6i");
MODULE_LICENSE("GPL");