summaryrefstree
path: root/kernel/drivers/vibrator/maxim/max77705_vibrator.c
blob: 001961a8b870b40d19804cd4eda16c92013ac80e (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * haptic motor driver for max77705_vibrator.c
 *
 * 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.
 */

#define pr_fmt(fmt) "[VIB] max77705_vib: " fmt

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pwm.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/i2c.h>
#include <linux/regulator/consumer.h>
#include <linux/mfd/max77705.h>
#include <linux/mfd/max77705-private.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/vibrator/sec_vibrator.h>

#define MOTOR_LRA                       (1<<7)
#define MOTOR_EN                        (1<<6)
#define EXT_PWM                         (0<<5)
#define DIVIDER_128                     (1<<1)
#define MRDBTMER_MASK                   (0x7)
#define MREN                            (1 << 3)
#define BIASEN                          (1 << 7)

/* 1s / 128(divider value) * 10(150Hz -> 1500 so multiply 10) = 78125000 ns */
#define FREQ_DIVIDER			78125000

struct max77705_vibrator_drvdata {
	struct max77705_dev *max77705;
	struct sec_vibrator_drvdata sec_vib_ddata;
	struct i2c_client *i2c;
	struct max77705_vibrator_pdata *pdata;
	struct pwm_device *pwm;
	struct regulator *regulator;
	int ratio;
	int max_duty;
	int period;
	int duty;
};

static int max77705_vib_set_ratio_with_temp(struct device *dev, int temperature)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;

	if (temperature >= pdata->high_temp_ref)
		ddata->ratio = pdata->high_temp_ratio;
	else
		ddata->ratio = pdata->normal_ratio;

	pr_info("temperature is %d, duty ratio set to %d\n", temperature, ddata->ratio);

	return 0;
}

static int max77705_vibrator_i2c_enable(struct max77705_vibrator_drvdata *ddata,
		bool en)
{
	int ret = max77705_update_reg(ddata->i2c,
			MAX77705_PMIC_REG_MCONFIG,
			en ? 0xff : 0x0, MOTOR_LRA | MOTOR_EN);
	if (ret)
		pr_err("i2c LRA and EN update error %d\n", ret);

	return ret;
}

static void max77705_vibrator_init_reg(
		struct max77705_vibrator_drvdata *ddata)
{
	int ret = 0;

	ret = max77705_update_reg(ddata->i2c,
			MAX77705_PMIC_REG_MAINCTRL1, 0xff, BIASEN);
	if (ret)
		pr_err("i2c REG_BIASEN update error %d\n", ret);

	ret = max77705_update_reg(ddata->i2c,
		MAX77705_PMIC_REG_MCONFIG, 0xff, MOTOR_LRA);
	if (ret)
		pr_err("i2c MOTOR_LPA update error %d\n", ret);

	ret = max77705_update_reg(ddata->i2c,
		MAX77705_PMIC_REG_MCONFIG, 0xff, DIVIDER_128);
	if (ret)
		pr_err("i2c DIVIDER_128 update error %d\n", ret);
}

static int max77705_vibrator_enable(struct device *dev, bool en)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	int ret = 0;

	ret = max77705_vibrator_i2c_enable(ddata, en);

	return ret;
}

static int max77705_vib_set_intensity(struct device *dev, int intensity)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	int duty = ddata->period >> 1;
	int ret = 0;

	if (intensity < -(MAX_INTENSITY) || MAX_INTENSITY < intensity) {
		pr_err("%s out of range %d\n", __func__, intensity);
		return -EINVAL;
	}

	if (intensity == MAX_INTENSITY)
		duty = ddata->max_duty;
	else if (intensity == -(MAX_INTENSITY))
		duty = ddata->period - ddata->max_duty;
	else if (intensity != 0)
		duty += (ddata->max_duty - duty) * intensity / MAX_INTENSITY;

	ddata->duty = duty;

	ret = pwm_config(ddata->pwm, duty, ddata->period);
	if (ret < 0) {
		pr_err("failed to config pwm %d\n", ret);
		return ret;
	}
	if (duty != ddata->period >> 1) {
		ret = pwm_enable(ddata->pwm);
		if (ret < 0)
			pr_err("failed to enable pwm %d\n", ret);
	} else {
		pwm_disable(ddata->pwm);
	}

	return ret;
}

static int max77705_vib_set_frequency(struct device *dev, int num)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);

	if (num >= 0 && num < ddata->pdata->freq_nums) {
		ddata->period = FREQ_DIVIDER / ddata->pdata->freq_array[num];
		ddata->duty = ddata->max_duty =
			(ddata->period * ddata->ratio) / 100;
	} else if (num >= HAPTIC_ENGINE_FREQ_MIN &&
			num <= HAPTIC_ENGINE_FREQ_MAX) {
		ddata->period = FREQ_DIVIDER / num;
		ddata->duty = ddata->max_duty =
			(ddata->period * ddata->ratio) / 100;
	} else {
		pr_err("%s out of range %d\n", __func__, num);
		return -EINVAL;
	}

	return 0;
}

static int max77705_vib_set_overdrive(struct device *dev, bool en)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);

	if (ddata->pdata->overdrive_ratio)
		ddata->ratio = en ? ddata->pdata->overdrive_ratio :
			ddata->pdata->normal_ratio;

	return 0;
}

static int max77705_vib_get_motor_type(struct device *dev, char *buf)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	int ret = snprintf(buf, VIB_BUFSIZE, "%s\n", ddata->pdata->motor_type);

	return ret;
}

#if defined(CONFIG_SEC_VIBRATOR)
static bool max77705_get_calibration(struct device *dev)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;

	return pdata->calibration;
}

static int max77705_get_step_size(struct device *dev, int *step_size)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;

	pr_info("%s step_size=%d\n", __func__, pdata->steps);

	if (pdata->steps == 0)
		return -ENODATA;

	*step_size = pdata->steps;

	return 0;
}


static int max77705_get_intensities(struct device *dev, int *buf)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;
	int i;

	if (pdata->intensities[1] == 0)
		return -ENODATA;

	for (i = 0; i < pdata->steps; i++)
		buf[i] = pdata->intensities[i];

	return 0;
}

static int max77705_set_intensities(struct device *dev, int *buf)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;
	int i;

	for (i = 0; i < pdata->steps; i++)
		pdata->intensities[i] = buf[i];

	return 0;
}

static int max77705_get_haptic_intensities(struct device *dev, int *buf)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;
	int i;

	if (pdata->haptic_intensities[1] == 0)
		return -ENODATA;

	for (i = 0; i < pdata->steps; i++)
		buf[i] = pdata->haptic_intensities[i];

	return 0;
}

static int max77705_set_haptic_intensities(struct device *dev, int *buf)
{
	struct max77705_vibrator_drvdata *ddata = dev_get_drvdata(dev);
	struct max77705_vibrator_pdata *pdata = ddata->pdata;
	int i;

	for (i = 0; i < pdata->steps; i++)
		pdata->haptic_intensities[i] = buf[i];

	return 0;
}
#endif /* if defined(CONFIG_SEC_VIBRATOR) */

static const struct sec_vibrator_ops max77705_multi_freq_vib_ops = {
	.enable = max77705_vibrator_enable,
	.set_intensity = max77705_vib_set_intensity,
	.set_frequency = max77705_vib_set_frequency,
	.set_overdrive = max77705_vib_set_overdrive,
	.set_force_touch_intensity = max77705_vib_set_intensity,
	.get_motor_type = max77705_vib_get_motor_type,
	.set_tuning_with_temp = max77705_vib_set_ratio_with_temp,
#if defined(CONFIG_SEC_VIBRATOR)
	.get_calibration = max77705_get_calibration,
	.get_step_size = max77705_get_step_size,
	.get_intensities = max77705_get_intensities,
	.set_intensities = max77705_set_intensities,
	.get_haptic_intensities = max77705_get_haptic_intensities,
	.set_haptic_intensities = max77705_set_haptic_intensities,
#endif
};

static const struct sec_vibrator_ops max77705_single_freq_vib_ops = {
	.enable = max77705_vibrator_enable,
	.set_intensity = max77705_vib_set_intensity,
	.get_motor_type = max77705_vib_get_motor_type,
	.set_tuning_with_temp = max77705_vib_set_ratio_with_temp,
#if defined(CONFIG_SEC_VIBRATOR)
	.get_calibration = max77705_get_calibration,
	.get_step_size = max77705_get_step_size,
	.get_intensities = max77705_get_intensities,
	.set_intensities = max77705_set_intensities,
	.get_haptic_intensities = max77705_get_haptic_intensities,
	.set_haptic_intensities = max77705_set_haptic_intensities,
#endif
};

#if defined(CONFIG_SEC_VIBRATOR)
static int of_sec_vibrator_dt(struct max77705_vibrator_pdata *pdata, struct device_node *np)
{
	int ret = 0;
	int i;
	unsigned int val = 0;
	int *intensities = NULL;

	pr_info("%s\n", __func__);
	pdata->calibration = false;

	/* number of steps */
	ret = of_property_read_u32(np, "samsung,steps", &val);
	if (ret) {
		pr_err("%s out of range(%d)\n", __func__, val);
		return -EINVAL;
	}
	pdata->steps = (int)val;

	/* allocate memory for intensities */
	pdata->intensities = kmalloc_array(pdata->steps, sizeof(int), GFP_KERNEL);
	if (!pdata->intensities)
		return -ENOMEM;
	intensities = pdata->intensities;

	/* intensities */
	ret = of_property_read_u32_array(np, "samsung,intensities", intensities, pdata->steps);
	if (ret) {
		pr_err("intensities are not specified\n");
		ret = -EINVAL;
		goto err_getting_int;
	}

	for (i = 0; i < pdata->steps; i++) {
		if ((intensities[i] < 0) || (intensities[i] > MAX_INTENSITY)) {
			pr_err("%s out of range(%d)\n", __func__, intensities[i]);
			ret = -EINVAL;
			goto err_getting_int;
		}
	}
	intensities = NULL;

	/* allocate memory for haptic_intensities */
	pdata->haptic_intensities = kmalloc_array(pdata->steps, sizeof(int), GFP_KERNEL);
	if (!pdata->haptic_intensities) {
		ret = -ENOMEM;
		goto err_alloc_haptic;
	}
	intensities = pdata->haptic_intensities;

	/* haptic intensities */
	ret = of_property_read_u32_array(np, "samsung,haptic_intensities", intensities, pdata->steps);
	if (ret) {
		pr_err("haptic_intensities are not specified\n");
		ret = -EINVAL;
		goto err_haptic;
	}
	for (i = 0; i < pdata->steps; i++) {
		if ((intensities[i] < 0) || (intensities[i] > MAX_INTENSITY)) {
			pr_err("%s out of range(%d)\n", __func__, intensities[i]);
			ret = -EINVAL;
			goto err_haptic;
		}
	}

	/* update calibration statue */
	pdata->calibration = true;

	return ret;

err_haptic:
	kfree(pdata->haptic_intensities);
err_alloc_haptic:
	pdata->haptic_intensities = NULL;
err_getting_int:
	kfree(pdata->intensities);
	pdata->intensities = NULL;
	pdata->steps = 0;

	return ret;
}
#endif /* if defined(CONFIG_SEC_VIBRATOR) */

#if defined(CONFIG_OF)
static struct max77705_vibrator_pdata *of_max77705_vibrator_dt(
		struct device *dev)
{
	struct device_node *np;
	struct max77705_vibrator_pdata *pdata;
	u32 temp;
	int ret, i;

	pdata = devm_kzalloc(dev, sizeof(struct max77705_vibrator_pdata),
			GFP_KERNEL);
	if (!pdata)
		return NULL;

	np = dev->of_node;
	if (np == NULL) {
		pr_err("%s: error to get dt node\n", __func__);
		goto err_parsing_dt;
	}

	ret = of_property_read_u32(np, "haptic,multi_frequency", &temp);
	if (ret) {
		pr_info("%s: multi_frequency isn't used\n", __func__);
		pdata->freq_nums = 0;
	} else
		pdata->freq_nums = (int)temp;

	if (pdata->freq_nums) {
		pdata->freq_array = devm_kzalloc(dev,
				sizeof(u32)*pdata->freq_nums, GFP_KERNEL);
		if (!pdata->freq_array) {
			pr_err("%s: failed to allocate freq_array data\n",
					__func__);
			goto err_parsing_dt;
		}

		ret = of_property_read_u32_array(np, "haptic,frequency",
				pdata->freq_array, pdata->freq_nums);
		if (ret) {
			pr_err("%s: error to get dt node frequency\n",
					__func__);
			goto err_parsing_dt;
		}

		pdata->freq = pdata->freq_array[0];
	} else {
		ret = of_property_read_u32(np,
				"haptic,frequency", &temp);
		if (ret) {
			pr_err("%s: error to get dt node frequency\n",
					__func__);
			goto err_parsing_dt;
		} else
			pdata->freq = (int)temp;
	}

	ret = of_property_read_u32(np,
			"haptic,normal_ratio", &temp);
	if (ret) {
		pr_err("%s: error to get dt node normal_ratio\n", __func__);
		goto err_parsing_dt;
	} else
		pdata->normal_ratio = (int)temp;

	ret = of_property_read_u32(np,
			"haptic,overdrive_ratio", &temp);
	if (ret)
		pr_info("%s: overdrive_ratio isn't used\n", __func__);
	else
		pdata->overdrive_ratio = (int)temp;

	ret = of_property_read_u32(np,
			"haptic,high_temp_ref", &temp);
	if (ret)
		pr_info("%s: high temperature concept isn't used\n", __func__);
	else
		pdata->high_temp_ref = (int)temp * 10;

	ret = of_property_read_u32(np,
			"haptic,high_temp_ratio", &temp);
	if (ret)
		pr_info("%s: high_temp_ratio isn't used\n", __func__);
	else
		pdata->high_temp_ratio = (int)temp;

	pdata->pwm = devm_of_pwm_get(dev, np, NULL);
	if (IS_ERR(pdata->pwm)) {
		pr_err("%s: error to get pwms\n", __func__);
		goto err_parsing_dt;
	}

	ret = of_property_read_string(np, "haptic,motor_type",
			&pdata->motor_type);
	if (ret)
		pr_err("%s: motor_type is undefined\n", __func__);

	if (pdata->freq_nums) {
		pr_info("multi frequency: %d\n", pdata->freq_nums);
		for (i = 0; i < pdata->freq_nums; i++)
			pr_info("frequency[%d]: %d.%dHz\n", i,
					pdata->freq_array[i]/10,
					pdata->freq_array[i]%10);
	} else {
		pr_info("frequency: %d.%dHz\n", pdata->freq/10, pdata->freq%10);
	}

#if defined(CONFIG_SEC_VIBRATOR)
	ret = of_sec_vibrator_dt(pdata, np);
	if (ret < 0)
		pr_err("sec_vibrator dt read fail\n");
#endif

	pr_info("normal_ratio: %d\n", pdata->normal_ratio);
	pr_info("overdrive_ratio: %d\n", pdata->overdrive_ratio);
	pr_info("high temperature reference: %d\n", pdata->high_temp_ref);
	pr_info("high temperature ratio: %d\n", pdata->high_temp_ratio);
	pr_info("motor_type: %s\n", pdata->motor_type);

	return pdata;

err_parsing_dt:
	devm_kfree(dev, pdata);
	return NULL;
}
#endif

static int max77705_vibrator_probe(struct platform_device *pdev)
{
	struct max77705_dev *max77705 = dev_get_drvdata(pdev->dev.parent);
	struct max77705_platform_data *max77705_pdata
		= dev_get_platdata(max77705->dev);
	struct max77705_vibrator_pdata *pdata = max77705_pdata->vibrator_data;
	struct max77705_vibrator_drvdata *ddata;

#if defined(CONFIG_OF)
	if (pdata == NULL) {
		pdata = of_max77705_vibrator_dt(&pdev->dev);
		if (unlikely(!pdata)) {
			pr_err("max77705-haptic : %s not found haptic dt!\n",
					__func__);
			return -ENODEV;
		}
	}
#else
	if (unlikely(!pdata)) {
		pr_err("%s: no pdata\n", __func__);
		return -ENODEV;
	}
#endif /* CONFIG_OF */

	ddata = devm_kzalloc(&pdev->dev,
			sizeof(struct max77705_vibrator_drvdata), GFP_KERNEL);
	if (unlikely(!ddata)) {
		pr_err("%s: no ddata\n", __func__);
		return -ENODEV;
	}

	platform_set_drvdata(pdev, ddata);
	ddata->max77705 = max77705;
	ddata->i2c = max77705->i2c;
	ddata->pdata = pdata;
	ddata->period = FREQ_DIVIDER / pdata->freq;
	ddata->ratio = pdata->normal_ratio;
	ddata->max_duty = ddata->duty = ddata->period * ddata->ratio / 100;
	ddata->pwm = pdata->pwm;

	if (IS_ERR(ddata->pwm)) {
		pr_err("%s: Failed to get pwm\n", __func__);
		return -EFAULT;
	} else
		pwm_config(ddata->pwm, ddata->duty, ddata->period);

	max77705_vibrator_init_reg(ddata);
	max77705_vibrator_i2c_enable(ddata, false);

	ddata->sec_vib_ddata.dev = &pdev->dev;
	if (pdata->freq_nums)
		ddata->sec_vib_ddata.vib_ops = &max77705_multi_freq_vib_ops;
	else
		ddata->sec_vib_ddata.vib_ops = &max77705_single_freq_vib_ops;
	sec_vibrator_register(&ddata->sec_vib_ddata);

	return 0;
}

static int max77705_vibrator_remove(struct platform_device *pdev)
{
	struct max77705_vibrator_drvdata *ddata
		= platform_get_drvdata(pdev);

	sec_vibrator_unregister(&ddata->sec_vib_ddata);
	max77705_vibrator_i2c_enable(ddata, false);
	return 0;
}

static int max77705_vibrator_suspend(struct platform_device *pdev,
		pm_message_t state)
{
	struct device *dev = &pdev->dev;

	if (!IS_ERR(dev->pins->sleep_state)) {
		pr_info("%s: set sleep pinctrl\n", __func__);
		pinctrl_select_state(dev->pins->p, dev->pins->sleep_state);
	}

	return 0;
}

static int max77705_vibrator_resume(struct platform_device *pdev)
{
	struct max77705_vibrator_drvdata *ddata
		= platform_get_drvdata(pdev);

	struct device *dev = &pdev->dev;

	if (!IS_ERR(dev->pins->default_state)) {
		pr_info("%s: set default pinctrl\n", __func__);
		pinctrl_select_state(dev->pins->p, dev->pins->default_state);
	}

	max77705_vibrator_init_reg(ddata);

	return 0;
}

static void max77705_vibrator_shutdown(struct platform_device *pdev)
{
}

static struct platform_driver max77705_vibrator_driver = {
	.probe		= max77705_vibrator_probe,
	.remove		= max77705_vibrator_remove,
	.suspend	= max77705_vibrator_suspend,
	.resume		= max77705_vibrator_resume,
	.shutdown	= max77705_vibrator_shutdown,
	.driver = {
		.name	= "max77705_vibrator",
		.owner	= THIS_MODULE,
	},
};

static int __init max77705_vibrator_init(void)
{
	return platform_driver_register(&max77705_vibrator_driver);
}
module_init(max77705_vibrator_init);

static void __exit max77705_vibrator_exit(void)
{
	platform_driver_unregister(&max77705_vibrator_driver);
}
module_exit(max77705_vibrator_exit);

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