summaryrefstree
path: root/kernel/drivers/motor/sec_vib.c
blob: fa135660f693ccb5206f4adb440fc9a8c826bdb0 (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
/* drivers/motor/dcmotor.c

 * Copyright (C) 2015 Samsung Electronics Co. Ltd. All Rights Reserved.
 *
 * 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/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/hrtimer.h>
#include <linux/err.h>
#include <linux/regulator/consumer.h>
#include <linux/of_gpio.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/sched.h>
#include <linux/jiffies.h>

#include "../staging/android/timed_output.h"

#define SEC_VIB_NAME "sec_vib"
#define DEBUG_MOTOR_LOG

struct sec_vib_pdata {
	const char *regulator;
	int max_timeout;
};

#if defined(DEBUG_MOTOR_LOG)
struct debug_log {
	u64 start;
	u64 stop;
};
#endif

struct sec_vib_drvdata {
	struct regulator *regulator;
	struct timed_output_dev dev;
	struct hrtimer timer;
	struct work_struct work;
#if defined(DEBUG_MOTOR_LOG)
	struct debug_log buff;
	struct delayed_work log_work;
#endif
	spinlock_t lock;
	bool running;
	int max_timeout;
	int timeout;
};

static enum hrtimer_restart sec_vib_timer_func(struct hrtimer *timer)
{
	struct sec_vib_drvdata *ddata =
		container_of(timer, struct sec_vib_drvdata, timer);

	ddata->timeout = 0;
	schedule_work(&ddata->work);
	return HRTIMER_NORESTART;
}

static int sec_vib_get_time(struct timed_output_dev *dev)
{
	struct sec_vib_drvdata *ddata =
		container_of(dev, struct sec_vib_drvdata, dev);

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

static void sec_vib_enable(struct timed_output_dev *dev, int value)
{
	struct sec_vib_drvdata *ddata =
		container_of(dev, struct sec_vib_drvdata, dev);
	unsigned long	flags;

	cancel_work_sync(&ddata->work);
	hrtimer_cancel(&ddata->timer);

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

	if (value > ddata->max_timeout)
		value = ddata->max_timeout;

	spin_lock_irqsave(&ddata->lock, flags);
	ddata->timeout = value;
	schedule_work(&ddata->work);

	if (value > 0) {
		hrtimer_start(&ddata->timer,
			ktime_set(value / 1000, (value % 1000) * 1000000),
			HRTIMER_MODE_REL);
	}

	spin_unlock_irqrestore(&ddata->lock, flags);
}

static void sec_vib_work(struct work_struct *work)
{
	struct sec_vib_drvdata *ddata =
		container_of(work, struct sec_vib_drvdata, work);
	int ret = 0;

	if (ddata->timeout > 0) {
		if (ddata->running)
			return;

		ret = regulator_enable(ddata->regulator);
#if defined(DEBUG_MOTOR_LOG)
		ddata->buff.start = local_clock();
#endif
		ddata->running = true;
	} else {
		if (!ddata->running)
			return;

		regulator_disable(ddata->regulator);
#if defined(DEBUG_MOTOR_LOG)
		ddata->buff.stop = local_clock();
#endif
		ddata->running = false;
	}

	if (ret)
		printk(KERN_ERR "[VIB] regulator error!(%d)\n", ret);
}

#if defined(CONFIG_OF)
static struct sec_vib_pdata *sec_vib_get_dt(struct device *dev)
{
	struct device_node *node, *child_node = NULL;
	struct sec_vib_pdata *pdata;
	int ret = 0;

	node = dev->of_node;
	if (!node) {
		ret = -ENODEV;
		goto err_out;
	}

	child_node = of_get_next_child(node, child_node);
	if (!child_node) {
		printk("[VIB] failed to get dt node\n");
		ret = -EINVAL;
		goto err_out;
	}

	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
	if (!pdata) {
		printk("[VIB] failed to alloc\n");
		ret = -ENOMEM;
		goto err_out;
	}

	of_property_read_u32(child_node, "sec_vib,max_timeout", &pdata->max_timeout);
	of_property_read_string(child_node, "sec_vib,regulator", &pdata->regulator);

	return pdata;

err_out:
	return ERR_PTR(ret);
}
#endif

#if defined(DEBUG_MOTOR_LOG)
void sec_vib_log_show(struct work_struct *work)
{
	struct sec_vib_drvdata *ddata =
			container_of(work, struct sec_vib_drvdata, log_work.work);
	u64 start_sec = ddata->buff.start;
	u64 stop_sec = ddata->buff.stop;
	unsigned long start_nsec = do_div(start_sec, 1000000000);
	unsigned long stop_nsec = do_div(stop_sec, 1000000000);

	printk(KERN_DEBUG
		"[VIB] %s [%lu.%03lu : %lu.%03lu] %s\n",
		__func__,
		(unsigned long)start_sec, start_nsec / 1000000,
		(unsigned long)stop_sec, stop_nsec / 1000000,
		regulator_is_enabled(ddata->regulator) ? "on" : "off");

	schedule_delayed_work(&ddata->log_work, msecs_to_jiffies(60000));
}
#endif

static int sec_vib_probe(struct platform_device *pdev)
{
	struct sec_vib_pdata *pdata = pdev->dev.platform_data;
	struct sec_vib_drvdata *ddata;
	int ret = 0;

	if (!pdata) {
#if defined(CONFIG_OF)
		pdata = sec_vib_get_dt(&pdev->dev);
		if (IS_ERR(pdata)) {
			printk(KERN_ERR "[VIB] there is no device tree!\n");
			ret = -ENODEV;
			goto err_out;
		}
#else
		printk(KERN_ERR "[VIB] there is no platform data!\n");
		ret = -ENODEV;
		goto err_out;
#endif
	}

	ddata = kzalloc(sizeof(struct sec_vib_drvdata), GFP_KERNEL);
	if (!ddata) {
		ret = -ENOMEM;
		goto err_free;
	}

	ddata->regulator = regulator_get(NULL, pdata->regulator);
	if (IS_ERR(ddata->regulator)) {
		printk(KERN_ERR "[VIB] failed get %s\n", pdata->regulator);
		ret = PTR_ERR(ddata->regulator);
		goto err_free;
	}

	hrtimer_init(&ddata->timer, CLOCK_MONOTONIC,
			HRTIMER_MODE_REL);

	ddata->timer.function = sec_vib_timer_func;
	spin_lock_init(&ddata->lock);

	ddata->max_timeout = pdata->max_timeout;

	INIT_WORK(&(ddata->work), sec_vib_work);
#if defined(DEBUG_MOTOR_LOG)
	INIT_DELAYED_WORK(&ddata->log_work, sec_vib_log_show);
	schedule_delayed_work(&ddata->log_work, msecs_to_jiffies(60000));
#endif

	ddata->dev.name = "vibrator";
	ddata->dev.get_time = sec_vib_get_time;
	ddata->dev.enable = sec_vib_enable;

	ret = timed_output_dev_register(&ddata->dev);
	if (ret < 0)
		goto err_free;

	platform_set_drvdata(pdev, ddata);

	return 0;

err_free:
	kfree(ddata);
err_out:
	return ret;
}

static int sec_vib_remove(struct platform_device *pdev)
{
	struct sec_vib_drvdata *ddata = platform_get_drvdata(pdev);

	timed_output_dev_unregister(&ddata->dev);
	kfree(ddata);
	return 0;
}

#if defined(CONFIG_OF)
static struct of_device_id sec_vib_dt_ids[] = {
	{ .compatible = "sec_vib" },
	{ }
};
MODULE_DEVICE_TABLE(of, sec_vib_dt_ids);
#endif /* CONFIG_OF */

static struct platform_driver sec_vib_driver = {
	.probe		= sec_vib_probe,
	.remove		= sec_vib_remove,
	.driver		= {
		.name		= SEC_VIB_NAME,
		.owner		= THIS_MODULE,
		.of_match_table	= of_match_ptr(sec_vib_dt_ids),
	},
};

static int __init sec_vib_init(void)
{
	return platform_driver_register(&sec_vib_driver);
}
module_init(sec_vib_init);

static void __exit sec_vib_exit(void)
{
	platform_driver_unregister(&sec_vib_driver);
}
module_exit(sec_vib_exit);

MODULE_AUTHOR("Samsung Electronics");
MODULE_DESCRIPTION("dc motor driver");
MODULE_LICENSE("GPL");