aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/motor/max8997_vibrator.c
blob: 827793db806fe2b5292594af07d36d77d6ac189b (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
/*
 * haptic motor driver for max8997 - max8997_vibrator.c
 *
 * Copyright (C) 2011 Unknown Samsung Employees (Original file was missing copyright header)
 * Copyright (C) 2012 The CyanogenMod Project
 *                    Daniel Hillenbrand <codeworkx@cyanogenmod.com>
 *                    Andrew Dodd <atd7@cornell.edu>
 *
 * 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/mfd/max8997.h>
#include <linux/mfd/max8997-private.h>

#ifdef CONFIG_MACH_Q1_BD
#include "mach/gpio.h"
#endif

static int pwm_duty_max;
static int pwm_duty_min;

static unsigned long pwm_val = 50; /* duty in percent */
static int pwm_duty; /* duty value */

struct vibrator_drvdata {
	struct max8997_motor_data *pdata;
	struct pwm_device	*pwm;
	struct regulator *regulator;
	struct i2c_client *client;
	struct timed_output_dev dev;
	struct hrtimer timer;
	struct work_struct work;
	spinlock_t lock;
	bool running;
	int timeout;
};

#ifdef CONFIG_VIBETONZ
static struct vibrator_drvdata *g_data;
#endif

static int vibetonz_clk_on(struct device *dev, bool en)
{
	struct clk *vibetonz_clk = NULL;
	vibetonz_clk = clk_get(dev, "timers");
	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 i2c_max8997_hapticmotor(struct vibrator_drvdata *data, bool en)
{
	int ret;
	u8 value = data->pdata->reg2;

	/* the setting of reg2 should be set */
	if (0 == value)
#if defined(CONFIG_MACH_P2)
		value = MOTOR_LRA | EXT_PWM | DIVIDER_128;
#else
		value = MOTOR_LRA | EXT_PWM | DIVIDER_256;
#endif

	if (en)
		value |= MOTOR_EN;

	ret = max8997_write_reg(data->client,
				 MAX8997_MOTOR_REG_CONFIG2, value);
	if (ret < 0)
		pr_err("[VIB] i2c write error\n");
}

static enum hrtimer_restart vibrator_timer_func(struct hrtimer *_timer)
{
	struct vibrator_drvdata *data =
		container_of(_timer, struct vibrator_drvdata, timer);

	data->timeout = 0;

	schedule_work(&data->work);
	return HRTIMER_NORESTART;
}

static void vibrator_work(struct work_struct *_work)
{
	struct vibrator_drvdata *data =
		container_of(_work, struct vibrator_drvdata, work);

	pr_debug("[VIB] time = %dms\n", data->timeout);

	if (0 == data->timeout) {
		if (!data->running)
			return ;
		pwm_disable(data->pwm);
		i2c_max8997_hapticmotor(data, false);
		if (data->pdata->motor_en)
			data->pdata->motor_en(false);
		else
			regulator_force_disable(data->regulator);
		data->running = false;

	} else {
		if (data->running)
			return ;
		if (data->pdata->motor_en)
			data->pdata->motor_en(true);
		else
			regulator_enable(data->regulator);
		i2c_max8997_hapticmotor(data, true);
		pwm_config(data->pwm, pwm_duty, data->pdata->period);
		pr_info("[VIB] %s: pwm_config duty=%d\n", __func__, pwm_duty);
		pwm_enable(data->pwm);

		data->running = true;
	}
}

static int vibrator_get_time(struct timed_output_dev *_dev)
{
	struct vibrator_drvdata	*data =
		container_of(_dev, struct vibrator_drvdata, dev);

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

static void vibrator_enable(struct timed_output_dev *_dev, int value)
{
	struct vibrator_drvdata	*data =
		container_of(_dev, struct vibrator_drvdata, dev);
	unsigned long	flags;

	cancel_work_sync(&data->work);
	hrtimer_cancel(&data->timer);
	data->timeout = value;
	schedule_work(&data->work);
	spin_lock_irqsave(&data->lock, flags);
	if (value > 0) {
		if (value > data->pdata->max_timeout)
			value = data->pdata->max_timeout;

		hrtimer_start(&data->timer,
			ns_to_ktime((u64)value * NSEC_PER_MSEC),
			HRTIMER_MODE_REL);
	}
	spin_unlock_irqrestore(&data->lock, flags);
}

#ifdef CONFIG_VIBETONZ
void vibtonz_en(bool en)
{
	struct vibrator_drvdata	*data = g_data;

	if (en) {
		if (data->running)
			return ;
		if (data->pdata->motor_en)
			data->pdata->motor_en(true);
		else
			regulator_enable(data->regulator);
		i2c_max8997_hapticmotor(data, true);
		pwm_enable(data->pwm);
		data->running = true;
	} else {
		if (!data->running)
			return ;

		pwm_disable(data->pwm);
		i2c_max8997_hapticmotor(data, false);
		if (data->pdata->motor_en)
			data->pdata->motor_en(false);
		else
			regulator_force_disable(data->regulator);
		data->running = false;
	}
}
EXPORT_SYMBOL(vibtonz_en);

void vibtonz_pwm(int nForce)
{
	struct vibrator_drvdata	*data = g_data;
	/* add to avoid the glitch issue */
	static int prev_duty;
	int pwm_period = data->pdata->period;

#if defined(CONFIG_MACH_P4)
	if (pwm_duty > data->pdata->duty)
		pwm_duty = data->pdata->duty;
	else if (pwm_period - pwm_duty > data->pdata->duty)
		pwm_duty = pwm_period - data->pdata->duty;
#endif

	/* add to avoid the glitch issue */
	if (prev_duty != pwm_duty) {
		prev_duty = pwm_duty;
		pr_debug("[VIB] %s: setting pwm_duty=%d", __func__, pwm_duty);
		pwm_config(data->pwm, pwm_duty, pwm_period);
	}
}
EXPORT_SYMBOL(vibtonz_pwm);

static ssize_t pwm_value_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	int count;

	pwm_val = ((pwm_duty - pwm_duty_min) * 100) / pwm_duty_min;

	count = sprintf(buf, "%lu\n", pwm_val);
	pr_debug("[VIB] pwm_value: %lu\n", pwm_val);

	return count;
}

ssize_t pwm_value_store(struct device *dev,
		struct device_attribute *attr,
		const char *buf, size_t size)
{
	if (kstrtoul(buf, 0, &pwm_val))
		pr_err("[VIB] %s: error on storing pwm_value\n", __func__);

	pr_info("[VIB] %s: pwm_value=%lu\n", __func__, pwm_val);

	pwm_duty = (pwm_val * pwm_duty_min) / 100 + pwm_duty_min;

	/* make sure new pwm duty is in range */
	if(pwm_duty > pwm_duty_max) {
		pwm_duty = pwm_duty_max;
	}
	else if (pwm_duty < pwm_duty_min) {
		pwm_duty = pwm_duty_min;
	}

	pr_info("[VIB] %s: pwm_duty=%d\n", __func__, pwm_duty);

	return size;
}
static DEVICE_ATTR(pwm_value, S_IRUGO | S_IWUSR,
		pwm_value_show, pwm_value_store);
#endif

static int __devinit vibrator_probe(struct platform_device *pdev)
{
	struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
	struct max8997_platform_data *max8997_pdata
		= dev_get_platdata(max8997->dev);
	struct max8997_motor_data *pdata = max8997_pdata->motor;
	struct vibrator_drvdata *ddata;
	int error = 0;

	ddata = kzalloc(sizeof(struct vibrator_drvdata), GFP_KERNEL);
	if (NULL == ddata) {
		pr_err("[VIB] Failed to alloc memory\n");
		error = -ENOMEM;
		goto err_free_mem;
	}

	if (pdata->init_hw)
		pdata->init_hw();
	else {
		ddata->regulator = regulator_get(NULL, "vmotor");
		if (IS_ERR(ddata->regulator)) {
			pr_err("[VIB] Failed to get vmoter regulator.\n");
			error = -EFAULT;
			goto err_regulator_get;
		}
	}

	ddata->pdata = pdata;
	ddata->dev.name = "vibrator";
	ddata->dev.get_time = vibrator_get_time;
	ddata->dev.enable = vibrator_enable;
	ddata->client = max8997->hmotor;

	platform_set_drvdata(pdev, ddata);

	hrtimer_init(&ddata->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	ddata->timer.function = vibrator_timer_func;
	INIT_WORK(&ddata->work, vibrator_work);
	spin_lock_init(&ddata->lock);

	ddata->pwm = pwm_request(pdata->pwm_id, "vibrator");
	if (IS_ERR(ddata->pwm)) {
		pr_err("[VIB] Failed to request pwm.\n");
		error = -EFAULT;
		goto err_pwm_request;
	}
	pwm_config(ddata->pwm,
		ddata->pdata->period/2, ddata->pdata->period);

	vibetonz_clk_on(&pdev->dev, true);

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

	/* User controllable pwm level */
	error = device_create_file(ddata->dev.dev, &dev_attr_pwm_value);
	if (error < 0) {
		pr_err("[VIB] create sysfs fail: pwm_value\n");
	}

#ifdef CONFIG_VIBETONZ
	g_data = ddata;
	pwm_duty_max = g_data->pdata->duty;
	pwm_duty_min = pwm_duty_max/2;
	pwm_duty = (pwm_duty_min + pwm_duty_max)/2;
#endif

	return 0;

err_timed_output_register:
	timed_output_dev_unregister(&ddata->dev);
err_regulator_get:
	regulator_put(ddata->regulator);
err_pwm_request:
	pwm_free(ddata->pwm);
err_free_mem:
	kfree(ddata);
	return error;
}

static int __devexit vibrator_remove(struct platform_device *pdev)
{
	struct vibrator_drvdata *data = platform_get_drvdata(pdev);
	timed_output_dev_unregister(&data->dev);
#ifdef CONFIG_MACH_Q1_BD
	gpio_free(GPIO_MOTOR_EN);
#else
	regulator_put(data->regulator);
#endif
	pwm_free(data->pwm);
	kfree(data);
	return 0;
}

static int vibrator_suspend(struct platform_device *pdev,
			pm_message_t state)
{
	vibetonz_clk_on(&pdev->dev, false);
	return 0;
}
static int vibrator_resume(struct platform_device *pdev)
{
	vibetonz_clk_on(&pdev->dev, true);
	return 0;
}

static struct platform_driver vibrator_driver = {
	.probe	= vibrator_probe,
	.remove	= __devexit_p(vibrator_remove),
	.suspend = vibrator_suspend,
	.resume	= vibrator_resume,
	.driver	= {
		.name	= "max8997-hapticmotor",
		.owner	= THIS_MODULE,
	}
};

static int __init vibrator_init(void)
{
	return platform_driver_register(&vibrator_driver);
}

static void __exit vibrator_exit(void)
{
	platform_driver_unregister(&vibrator_driver);
}

late_initcall(vibrator_init);
module_exit(vibrator_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("vibrator driver");