aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/motor/isa1200_vibrator.c
blob: 0f2da516183f98a4e58a43f9049c49c91afa1bc4 (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
/* drivers/motor/isa1200_vibrator.c
 *
 * Copyright (C) 2011 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/hrtimer.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/pwm.h>
#include <linux/wakelock.h>
#include <linux/mutex.h>
#include <linux/clk.h>
#include <linux/pwm.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/timed_output.h>
#include <linux/isa1200_vibrator.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

static unsigned long pwm_val = 50; /* duty in percent */
static int isapwm_duty = 999; /* duty value, 1000=100%, 500=50%, 0=0% Default is 999*/

#if 0
#define MOTOR_DEBUG
#endif

struct isa1200_vibrator_drvdata {
	struct timed_output_dev dev;
	struct hrtimer timer;
	struct work_struct work;
	struct clk *vib_clk;
	struct pwm_device	*pwm;
	struct i2c_client *client;
	int (*gpio_en) (bool) ;
	spinlock_t lock;
	bool running;
	int timeout;
	int max_timeout;
	u8 ctrl0;
	u8 ctrl1;
	u8 ctrl2;
	u8 ctrl4;
	u8 pll;
	u8 duty;
	u8 period;
	int pwm_id;
	u16 pwm_duty;
	u16 pwm_period;
};


static struct isa1200_vibrator_drvdata	*g_drvdata;

static int isa1200_vibrator_i2c_write(struct i2c_client *client,
					u8 addr, u8 val)
{
	int error = 0;
	error = i2c_smbus_write_byte_data(client, addr, val);
	if (error)
		printk(KERN_ERR "[VIB] Failed to write addr=[0x%x], val=[0x%x]\n",
				addr, val);

	return error;
}

int vibtonz_i2c_write(u8 addr, int length, u8 *data)
{
	if (NULL == g_drvdata) {
		printk(KERN_ERR "[VIB] driver is not ready\n");
		return -EFAULT;
	}

	if (2 != length)
		printk(KERN_ERR "[VIB] length should be 2(len:%d)\n",
			length);

#ifdef MOTOR_DEBUG
	printk(KERN_DEBUG "[VIB] data : %x, %x\n",
		data[0], data[1]);
#endif

	return isa1200_vibrator_i2c_write(g_drvdata->client,
		data[0], data[1]);
}

void vibtonz_clk_enable(bool en)
{
	if (NULL == g_drvdata) {
		printk(KERN_ERR "[VIB] driver is not ready\n");
		return ;
	}

	if (en) {
		if (g_drvdata->vib_clk)
			clk_enable(g_drvdata->vib_clk);
		else
			pwm_enable(g_drvdata->pwm);
	} else {
		if (g_drvdata->vib_clk)
			clk_disable(g_drvdata->vib_clk);
		else
			pwm_disable(g_drvdata->pwm);
	}
}

void vibtonz_clk_config(int duty)
{
	u32 duty_ns = 0;

	duty_ns = (u32)(g_drvdata->pwm_period * duty)
		/ 1000;

	pwm_config(g_drvdata->pwm,
		duty_ns,
		g_drvdata->pwm_period);

}

void vibtonz_chip_enable(bool en)
{
	if (NULL == g_drvdata) {
		printk(KERN_ERR "[VIB] driver is not ready\n");
		return ;
	}
	g_drvdata->gpio_en(en ? true : false);
}

static void isa1200_vibrator_hw_init(struct isa1200_vibrator_drvdata *data)
{
	data->gpio_en(true);
	msleep(20);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG0, data->ctrl0);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG1, data->ctrl1);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG2, data->ctrl2);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_PLL_REG, data->pll);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG4, data->ctrl4);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_PWM_DUTY_REG, data->period/2);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_PWM_PERIOD_REG, data->period);

#ifdef MOTOR_DEBUG
	printk(KERN_DEBUG "[VIB] ctrl0 = 0x%x\n", data->ctrl0);
	printk(KERN_DEBUG "[VIB] ctrl1 = 0x%x\n", data->ctrl1);
	printk(KERN_DEBUG "[VIB] ctrl2 = 0x%x\n", data->ctrl2);
	printk(KERN_DEBUG "[VIB] pll = 0x%x\n", data->pll);
	printk(KERN_DEBUG "[VIB] ctrl4 = 0x%x\n", data->ctrl4);
	printk(KERN_DEBUG "[VIB] duty = 0x%x\n", data->period/2);
	printk(KERN_DEBUG "[VIB] period = 0x%x\n", data->period);
#endif

}

static void isa1200_vibrator_on(struct isa1200_vibrator_drvdata *data)
{
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG0, data->ctrl0 | CTL0_NORMAL_OP);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_PWM_DUTY_REG, data->duty - 3);
#ifdef MOTOR_DEBUG
	printk(KERN_DEBUG "[VIB] ctrl0 = 0x%x\n", data->ctrl0 | CTL0_NORMAL_OP);
	printk(KERN_DEBUG "[VIB] duty = 0x%x\n", data->duty);
#endif
}

static void isa1200_vibrator_off(struct isa1200_vibrator_drvdata *data)
{
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_PWM_DUTY_REG, data->period / 2);
	isa1200_vibrator_i2c_write(data->client,
		HAPTIC_CONTROL_REG0, data->ctrl0);
}

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

	data->timeout = 0;

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

static void isa1200_vibrator_work(struct work_struct *_work)
{
	struct isa1200_vibrator_drvdata *data =
		container_of(_work, struct isa1200_vibrator_drvdata, work);

	if (0 == data->timeout) {
		if (!data->running)
			return ;

		data->running = false;
		isa1200_vibrator_off(data);
		vibtonz_clk_config(500);
		vibtonz_clk_enable(false);
	} else {
		if (data->running)
			return ;

		data->running = true;
		vibtonz_clk_config(isapwm_duty);
		vibtonz_clk_enable(true);
		mdelay(1);
		isa1200_vibrator_on(data);
	}
}

static int isa1200_vibrator_get_time(struct timed_output_dev *_dev)
{
	struct isa1200_vibrator_drvdata	*data =
		container_of(_dev, struct isa1200_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 isa1200_vibrator_enable(struct timed_output_dev *_dev, int value)
{
	struct isa1200_vibrator_drvdata	*data =
		container_of(_dev, struct isa1200_vibrator_drvdata, dev);
	unsigned long	flags;

#ifdef MOTOR_DEBUG
	printk(KERN_DEBUG "[VIB] time = %dms\n", value);
#endif
	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->max_timeout)
			value = data->max_timeout;

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

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

       pwm_val = ((isapwm_duty - 500) * 100) / 500;

       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);

       isapwm_duty = (pwm_val * 500) / 100 + 500;

       /* make sure new pwm duty is in range */
      if(isapwm_duty > 1000)
      {
         isapwm_duty = 1000;
      }
      else if (isapwm_duty < 500)
      {
        isapwm_duty = 500;
      }

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

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

static int __devinit isa1200_vibrator_i2c_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct isa1200_vibrator_platform_data *pdata =
		client->dev.platform_data;
	struct isa1200_vibrator_drvdata *ddata;
	int ret = 0;

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

	ddata->client = client;
	ddata->ctrl0 = pdata->ctrl0;
	ddata->ctrl1 = pdata->ctrl1;
	ddata->ctrl2 = pdata->ctrl2;
	ddata->ctrl4 = pdata->ctrl4;
	ddata->gpio_en = pdata->gpio_en;
	if (pdata->get_clk) {
		ddata->vib_clk = pdata->get_clk();
		if (IS_ERR(ddata->vib_clk)) {
			pr_err("[VIB] Failed to request vib_clk.\n");
			ret = -EFAULT;
			goto err_request_clk;
		}
	} else {
		ddata->vib_clk = NULL;
		ddata->pwm = pwm_request(pdata->pwm_id, "vibrator");
		if (IS_ERR(ddata->pwm)) {
			pr_err("[VIB] Failed to request pwm.\n");
			ret = -EFAULT;
			goto err_request_clk;
		}
		ddata->pwm_id = pdata->pwm_id;
		ddata->pwm_duty = pdata->pwm_duty;
		ddata->pwm_period = pdata->pwm_period;
	}
	ddata->max_timeout = pdata->max_timeout;
	ddata->pll = pdata->pll;
	ddata->duty = pdata->duty;
	ddata->period = pdata->period;

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

	ddata->dev.name = "vibrator";
	ddata->dev.get_time = isa1200_vibrator_get_time;
	ddata->dev.enable = isa1200_vibrator_enable;

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

	i2c_set_clientdata(client, ddata);
	isa1200_vibrator_hw_init(ddata);

	ret = timed_output_dev_register(&ddata->dev);
	if (ret < 0) {
		printk(KERN_ERR "[VIB] Failed to register timed_output : -%d\n", ret);
		goto err_to_dev_reg;
	}

	g_drvdata = ddata;

	return 0;

err_to_dev_reg:
err_request_clk:
	kfree(ddata);
err_free_mem:
	return ret;

}

static int isa1200_vibrator_suspend(struct i2c_client *client, pm_message_t mesg)
{
	struct isa1200_vibrator_drvdata *ddata  = i2c_get_clientdata(client);
	ddata->gpio_en(false);
	return 0;
}

static int isa1200_vibrator_resume(struct i2c_client *client)
{
	struct isa1200_vibrator_drvdata *ddata  = i2c_get_clientdata(client);
	isa1200_vibrator_hw_init(ddata);
	return 0;
}

static const struct i2c_device_id isa1200_vibrator_device_id[] = {
	{"isa1200_vibrator", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, isa1200_vibrator_device_id);

static struct i2c_driver isa1200_vibrator_i2c_driver = {
	.driver = {
		.name = "isa1200_vibrator",
		.owner = THIS_MODULE,
	},
	.probe     = isa1200_vibrator_i2c_probe,
	.id_table  = isa1200_vibrator_device_id,
	.suspend	= isa1200_vibrator_suspend,
	.resume	= isa1200_vibrator_resume,
};

static int __init isa1200_vibrator_init(void)
{
	return i2c_add_driver(&isa1200_vibrator_i2c_driver);
}

static void __exit isa1200_vibrator_exit(void)
{
	i2c_del_driver(&isa1200_vibrator_i2c_driver);
}

module_init(isa1200_vibrator_init);
module_exit(isa1200_vibrator_exit);