summaryrefslogtreecommitdiff
path: root/drivers/staging/android/ab5500-timed-vibra.c
blob: 35c627a6285165ddeff313a03054280f7560ad0b (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
/*
 * ab5500-vibra.c - driver for vibrator in ST-Ericsson AB5500 chip
 *
 * Copyright (C) 2011 ST-Ericsson SA.
 *
 * License Terms: GNU General Public License v2
 *
 * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
 */

#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/err.h>
#include "timed_output.h"

#include <linux/mfd/abx500.h>		/* abx500_* */
#include <linux/mfd/abx500/ab5500.h>
#include <linux/mfd/abx500/ab5500-gpadc.h>
#include <linux/ab5500-vibra.h>

#define AB5500_VIBRA_DEV_NAME          "ab5500:vibra"
#define AB5500_VIBRA_DRV_NAME		"ab5500-vibrator"

/* Vibrator Register Address Offsets */
#define AB5500_VIB_CTRL		0x10
#define AB5500_VIB_VOLT		0x11
#define AB5500_VIB_FUND_FREQ	0x12	/* Linear vibra resonance freq. */
#define AB5500_VIB_FUND_DUTY	0x13
#define AB5500_KELVIN_ANA	0xB1
#define AB5500_VIBRA_KELVIN	0xFE

/* Vibrator Control */
#define AB5500_VIB_DISABLE	(0x80)
#define AB5500_VIB_PWR_ON	(0x40)
#define AB5500_VIB_FUND_EN	(0x20)
#define AB5500_VIB_FREQ_SHIFT	(0)
#define AB5500_VIB_DUTY_SHIFT	(3)
#define AB5500_VIB_VOLT_SHIFT	(0)
#define AB5500_VIB_PULSE_SHIFT	(4)
#define VIBRA_KELVIN_ENABLE	(0x90)
#define VIBRA_KELVIN_VOUT	(0x20)

/* Vibrator Freq. (in HZ) and Duty */
enum ab5500_vibra_freq {
	AB5500_VIB_FREQ_1HZ = 1,
	AB5500_VIB_FREQ_2HZ,
	AB5500_VIB_FREQ_4HZ,
	AB5500_VIB_FREQ_8HZ,
};

enum ab5500_vibra_duty {
	AB5500_VIB_DUTY_100 = 0,
	AB5500_VIB_DUTY_75 = 8,
	AB5500_VIB_DUTY_50 = 16,
	AB5500_VIB_DUTY_25 = 24,
};

/* Linear vibrator resonance freq. duty */
#define AB5500_VIB_RDUTY_50	(0x7F)

/* Vibration magnitudes */
#define AB5500_VIB_FREQ_MAX	(4)
#define AB5500_VIB_DUTY_MAX	(4)

static u8 vib_freq[AB5500_VIB_FREQ_MAX] = {
	AB5500_VIB_FREQ_1HZ,
	AB5500_VIB_FREQ_2HZ,
	AB5500_VIB_FREQ_4HZ,
	AB5500_VIB_FREQ_8HZ,
};

static u8 vib_duty[AB5500_VIB_DUTY_MAX] = {
	AB5500_VIB_DUTY_100,
	AB5500_VIB_DUTY_75,
	AB5500_VIB_DUTY_50,
	AB5500_VIB_DUTY_25,
};

/**
 * struct ab5500_vibra - Vibrator driver interal info.
 * @tdev:		Pointer to timed output device structure
 * @dev:		Reference to vibra device structure
 * @vibra_workqueue:    Pointer to vibrator workqueue structure
 * @vibra_work:		Vibrator work
 * @gpadc:		Gpadc instance
 * @vibra_wait:		Vibrator wait queue head
 * @vibra_lock:		Vibrator lock
 * @timeout_ms:		Indicates how long time the vibrator will be enabled
 * @timeout_start:	Start of vibrator in jiffies
 * @pdata:		Local pointer to platform data with vibrator parameters
 * @magnitude:		required vibration strength
 * @enable:		Vibrator running status
 * @eol:		Vibrator end of life(eol) status
 **/
struct ab5500_vibra {
	struct timed_output_dev		tdev;
	struct device			*dev;
	struct workqueue_struct		*vibra_workqueue;
	struct work_struct		vibra_work;
	struct ab5500_gpadc		*gpadc;
	wait_queue_head_t		vibra_wait;
	spinlock_t			vibra_lock;
	unsigned int			timeout_ms;
	unsigned long			timeout_start;
	struct ab5500_vibra_platform_data *pdata;
	u8				magnitude;
	bool				enable;
	bool				eol;
};

static inline u8 vibra_magnitude(u8 mag)
{
	mag /= (AB5500_VIB_FREQ_MAX * AB5500_VIB_DUTY_MAX);
	mag = vib_freq[mag / AB5500_VIB_FREQ_MAX] << AB5500_VIB_FREQ_SHIFT;
	mag |= vib_duty[mag % AB5500_VIB_DUTY_MAX] << AB5500_VIB_DUTY_SHIFT;

	return mag;
}

static int ab5500_setup_vibra_kelvin(struct ab5500_vibra* vibra)
{
	int ret;

	/* Establish the kelvin IP connection to be measured */
	ret = abx500_set_register_interruptible(vibra->dev,
		AB5500_BANK_VIT_IO_I2C_CLK_TST_OTP,
		AB5500_KELVIN_ANA, VIBRA_KELVIN_ENABLE);
	if (ret < 0) {
		dev_err(vibra->dev, "failed to set kelvin network\n");
		return ret;
	}

	/* Select vibra parameter to be measured */
	ret = abx500_set_register_interruptible(vibra->dev, AB5500_BANK_VIBRA,
		AB5500_VIBRA_KELVIN, VIBRA_KELVIN_VOUT);
	if (ret < 0)
		dev_err(vibra->dev, "failed to select the kelvin param\n");

	return ret;
}

static int ab5500_vibra_start(struct ab5500_vibra* vibra)
{
	u8 ctrl = 0;

	ctrl = AB5500_VIB_PWR_ON |
		vibra_magnitude(vibra->magnitude);

	if (vibra->pdata->type == AB5500_VIB_LINEAR)
		ctrl |= AB5500_VIB_FUND_EN;

	return abx500_set_register_interruptible(vibra->dev,
			AB5500_BANK_VIBRA, AB5500_VIB_CTRL, ctrl);
}

static int ab5500_vibra_stop(struct ab5500_vibra* vibra)
{
	return abx500_mask_and_set_register_interruptible(vibra->dev,
			AB5500_BANK_VIBRA, AB5500_VIB_CTRL,
			AB5500_VIB_PWR_ON, 0);
}

static int ab5500_vibra_eol_check(struct ab5500_vibra* vibra)
{
	int ret, vout;

	ret = ab5500_setup_vibra_kelvin(vibra);
	if (ret < 0) {
		dev_err(vibra->dev, "failed to setup kelvin network\n");
		return ret;
	}

	/* Start vibra to measure voltage */
	ret = ab5500_vibra_start(vibra);
	if (ret < 0) {
		dev_err(vibra->dev, "failed to start vibra\n");
		return ret;
	}
	/* 20ms delay required for voltage rampup */
	wait_event_interruptible_timeout(vibra->vibra_wait,
			0, msecs_to_jiffies(20));

	vout  = ab5500_gpadc_convert(vibra->gpadc, VIBRA_KELVIN);
	if (vout < 0) {
		dev_err(vibra->dev, "failed to read gpadc vibra\n");
		return vout;
	}

	/* Stop vibra after measuring voltage */
	ret  = ab5500_vibra_stop(vibra);
	if (ret < 0) {
		dev_err(vibra->dev, "failed to stop vibra\n");
		return ret;
	}
	/* Check for vibra eol condition */
	if (vout < vibra->pdata->eol_voltage) {
		vibra->eol = true;
		dev_err(vibra->dev, "Vibra eol detected. Disabling vibra!\n");
	}

	return ret;
}

/**
 * ab5500_vibra_work() - Vibrator work, turns on/off vibrator
 * @work: Pointer to work structure
 *
 * This function is called from workqueue, turns on/off vibrator
 **/
static void ab5500_vibra_work(struct work_struct *work)
{
	struct ab5500_vibra *vibra = container_of(work,
			struct ab5500_vibra, vibra_work);
	unsigned long flags;
	int ret;

	ret = ab5500_vibra_start(vibra);
	if (ret < 0)
		dev_err(vibra->dev, "reg[%d] w failed: %d\n",
				AB5500_VIB_CTRL, ret);

	wait_event_interruptible_timeout(vibra->vibra_wait,
			0, msecs_to_jiffies(vibra->timeout_ms));

	ret = ab5500_vibra_stop(vibra);
	if (ret < 0)
		dev_err(vibra->dev, "reg[%d] w failed: %d\n",
				AB5500_VIB_CTRL, ret);

	spin_lock_irqsave(&vibra->vibra_lock, flags);

	vibra->timeout_start = 0;
	vibra->enable = false;

	spin_unlock_irqrestore(&vibra->vibra_lock, flags);
}

/**
 * vibra_enable() - Enables vibrator
 * @tdev:      Pointer to timed output device structure
 * @timeout:	Time indicating how long vibrator will be enabled
 *
 * This function enables vibrator
 **/
static void vibra_enable(struct timed_output_dev *tdev, int timeout)
{
	struct ab5500_vibra *vibra = dev_get_drvdata(tdev->dev);
	unsigned long flags;

	spin_lock_irqsave(&vibra->vibra_lock, flags);

	if ((!vibra->enable || timeout) && !vibra->eol) {
		vibra->enable = true;

		vibra->timeout_ms = timeout;
		vibra->timeout_start = jiffies;
		queue_work(vibra->vibra_workqueue, &vibra->vibra_work);
	}

	spin_unlock_irqrestore(&vibra->vibra_lock, flags);
}

/**
 * vibra_get_time() - Returns remaining time to disabling vibration
 * @tdev:      Pointer to timed output device structure
 *
 * This function returns time remaining to disabling vibration
 *
 * Returns:
 *	Returns remaining time to disabling vibration
 **/
static int vibra_get_time(struct timed_output_dev *tdev)
{
	struct ab5500_vibra *vibra = dev_get_drvdata(tdev->dev);
	unsigned int ms;
	unsigned long flags;

	spin_lock_irqsave(&vibra->vibra_lock, flags);

	if (vibra->enable)
		ms = jiffies_to_msecs(vibra->timeout_start +
				msecs_to_jiffies(vibra->timeout_ms) - jiffies);
	else
		ms = 0;

	spin_unlock_irqrestore(&vibra->vibra_lock, flags);

	return ms;
}

static int ab5500_vibra_reg_init(struct ab5500_vibra *vibra)
{
	int ret = 0;
	u8 ctrl = 0;
	u8 pulse = 0;

	ctrl = (AB5500_VIB_DUTY_50 << AB5500_VIB_DUTY_SHIFT) |
		(AB5500_VIB_FREQ_8HZ << AB5500_VIB_FREQ_SHIFT);

	if (vibra->pdata->type == AB5500_VIB_LINEAR) {
		ctrl |= AB5500_VIB_FUND_EN;

		if (vibra->pdata->voltage > AB5500_VIB_VOLT_MAX)
			vibra->pdata->voltage = AB5500_VIB_VOLT_MAX;

		pulse = (vibra->pdata->pulse << AB5500_VIB_PULSE_SHIFT) |
			(vibra->pdata->voltage << AB5500_VIB_VOLT_SHIFT);
		ret = abx500_set_register_interruptible(vibra->dev,
				AB5500_BANK_VIBRA, AB5500_VIB_VOLT,
				pulse);
		if (ret < 0) {
			dev_err(vibra->dev,
				"reg[%#x] w %#x failed: %d\n",
				AB5500_VIB_VOLT, vibra->pdata->voltage, ret);
			return ret;
		}

		ret = abx500_set_register_interruptible(vibra->dev,
				AB5500_BANK_VIBRA, AB5500_VIB_FUND_FREQ,
				vibra->pdata->res_freq);
		if (ret < 0) {
			dev_err(vibra->dev, "reg[%#x] w %#x failed: %d\n",
					AB5500_VIB_FUND_FREQ,
					vibra->pdata->res_freq, ret);
			return ret;
		}

		ret = abx500_set_register_interruptible(vibra->dev,
				AB5500_BANK_VIBRA, AB5500_VIB_FUND_DUTY,
				AB5500_VIB_RDUTY_50);
		if (ret < 0) {
			dev_err(vibra->dev, "reg[%#x] w %#x failed: %d\n",
					AB5500_VIB_FUND_DUTY,
					AB5500_VIB_RDUTY_50, ret);
			return ret;
		}
	}

	ret = abx500_set_register_interruptible(vibra->dev,
			AB5500_BANK_VIBRA, AB5500_VIB_CTRL, ctrl);
	if (ret < 0) {
		dev_err(vibra->dev, "reg[%#x] w %#x failed: %d\n",
				AB5500_VIB_CTRL, ctrl, ret);
		return ret;
	}

	return ret;
}

static int ab5500_vibra_register_dev(struct ab5500_vibra *vibra,
		struct platform_device *pdev)
{
	int ret = 0;

	ret = timed_output_dev_register(&vibra->tdev);
	if (ret) {
		dev_err(&pdev->dev, "failed to register timed output device\n");
		goto err_out;
	}

	dev_set_drvdata(vibra->tdev.dev, vibra);


	/* Create workqueue just for timed output vibrator */
	vibra->vibra_workqueue =
		create_singlethread_workqueue("ste-timed-output-vibra");
	if (!vibra->vibra_workqueue) {
		dev_err(&pdev->dev, "failed to allocate workqueue\n");
		ret = -ENOMEM;
		goto exit_output_unregister;
	}

	init_waitqueue_head(&vibra->vibra_wait);
	INIT_WORK(&vibra->vibra_work, ab5500_vibra_work);
	spin_lock_init(&vibra->vibra_lock);

	platform_set_drvdata(pdev, vibra);

	return ret;

exit_output_unregister:
	timed_output_dev_unregister(&vibra->tdev);
err_out:
	return ret;
}

static int __devinit ab5500_vibra_probe(struct platform_device *pdev)
{
	struct ab5500_vibra_platform_data *pdata = pdev->dev.platform_data;
	struct ab5500_vibra *vibra = NULL;
	int ret = 0;

	if (pdata == NULL) {
		dev_err(&pdev->dev, "platform data required. Quitting...\n");
		return -ENODEV;
	}

	vibra = kzalloc(sizeof(struct ab5500_vibra), GFP_KERNEL);
	if (vibra == NULL)
		return -ENOMEM;

	vibra->tdev.name = "vibrator";
	vibra->tdev.enable = vibra_enable;
	vibra->tdev.get_time = vibra_get_time;
	vibra->timeout_start = 0;
	vibra->enable = false;
	vibra->magnitude = pdata->magnitude;
	vibra->pdata = pdata;
	vibra->dev = &pdev->dev;

	if (vibra->pdata->eol_voltage) {
		vibra->gpadc = ab5500_gpadc_get("ab5500-adc.0");
		if (IS_ERR(vibra->gpadc))
			goto err_alloc;
	}

	if (vibra->pdata->type == AB5500_VIB_LINEAR)
		dev_info(&pdev->dev, "Linear Type Vibrators\n");
	else
		dev_info(&pdev->dev, "Rotary Type Vibrators\n");

	ret = ab5500_vibra_reg_init(vibra);
	if (ret < 0)
		goto err_alloc;

	ret = ab5500_vibra_register_dev(vibra, pdev);
	if (ret < 0)
		goto err_alloc;

	/* Perform vibra eol diagnostics if eol_voltage is set */
	if (vibra->pdata->eol_voltage) {
		ret = ab5500_vibra_eol_check(vibra);
		if (ret < 0)
			dev_warn(&pdev->dev, "EOL check failed\n");
	}

	dev_info(&pdev->dev, "initialization success\n");

	return ret;

err_alloc:
	kfree(vibra);

	return ret;
}

static int __devexit ab5500_vibra_remove(struct platform_device *pdev)
{
	struct ab5500_vibra *vibra = platform_get_drvdata(pdev);

	timed_output_dev_unregister(&vibra->tdev);
	destroy_workqueue(vibra->vibra_workqueue);
	kfree(vibra);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

static struct platform_driver ab5500_vibra_driver = {
	.probe		= ab5500_vibra_probe,
	.remove		= __devexit_p(ab5500_vibra_remove),
	.driver		= {
		.name	= AB5500_VIBRA_DRV_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init ab5500_vibra_module_init(void)
{
	return platform_driver_register(&ab5500_vibra_driver);
}

static void __exit ab5500_vibra_module_exit(void)
{
	platform_driver_unregister(&ab5500_vibra_driver);
}

module_init(ab5500_vibra_module_init);
module_exit(ab5500_vibra_module_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");
MODULE_DESCRIPTION("Timed Output Driver for AB5500 Vibrator");