2 * MAXIM MAX77693 Haptic device driver
4 * Copyright (C) 2015 Samsung Electronics
5 * Author: Jaewon Kim <jaewon02.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/input.h>
17 #include <linux/mfd/max77843-private.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/pwm.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
26 #define MAX_MAGNITUDE_SHIFT 16
28 enum max77843_haptic_motor_type
{
29 MAX77843_HAPTIC_ERM
= 0,
33 enum max77843_haptic_pwm_divisor
{
34 MAX77843_HAPTIC_PWM_DIVISOR_32
= 0,
35 MAX77843_HAPTIC_PWM_DIVISOR_64
,
36 MAX77843_HAPTIC_PWM_DIVISOR_128
,
37 MAX77843_HAPTIC_PWM_DIVISOR_256
,
40 struct max77843_haptic
{
41 struct regmap
*regmap_haptic
;
43 struct input_dev
*input_dev
;
44 struct pwm_device
*pwm_dev
;
45 struct regulator
*motor_reg
;
46 struct work_struct work
;
49 unsigned int magnitude
;
50 unsigned int pwm_duty
;
55 enum max77843_haptic_motor_type type
;
56 enum max77843_haptic_pwm_divisor pwm_divisor
;
59 static int max77843_haptic_set_duty_cycle(struct max77843_haptic
*haptic
)
61 int delta
= (haptic
->pwm_dev
->period
+ haptic
->pwm_duty
) / 2;
64 error
= pwm_config(haptic
->pwm_dev
, delta
, haptic
->pwm_dev
->period
);
66 dev_err(haptic
->dev
, "failed to configure pwm: %d\n", error
);
73 static int max77843_haptic_bias(struct max77843_haptic
*haptic
, bool on
)
77 error
= regmap_update_bits(haptic
->regmap_haptic
,
78 MAX77843_SYS_REG_MAINCTRL1
,
79 MAX77843_MAINCTRL1_BIASEN_MASK
,
80 on
<< MAINCTRL1_BIASEN_SHIFT
);
82 dev_err(haptic
->dev
, "failed to %s bias: %d\n",
83 on
? "enable" : "disable", error
);
90 static int max77843_haptic_config(struct max77843_haptic
*haptic
, bool enable
)
95 value
= (haptic
->type
<< MCONFIG_MODE_SHIFT
) |
96 (enable
<< MCONFIG_MEN_SHIFT
) |
97 (haptic
->pwm_divisor
<< MCONFIG_PDIV_SHIFT
);
99 error
= regmap_write(haptic
->regmap_haptic
,
100 MAX77843_HAP_REG_MCONFIG
, value
);
103 "failed to update haptic config: %d\n", error
);
110 static int max77843_haptic_enable(struct max77843_haptic
*haptic
)
117 error
= pwm_enable(haptic
->pwm_dev
);
120 "failed to enable pwm device: %d\n", error
);
124 error
= max77843_haptic_config(haptic
, true);
128 haptic
->active
= true;
133 pwm_disable(haptic
->pwm_dev
);
138 static int max77843_haptic_disable(struct max77843_haptic
*haptic
)
145 error
= max77843_haptic_config(haptic
, false);
149 pwm_disable(haptic
->pwm_dev
);
151 haptic
->active
= false;
156 static void max77843_haptic_play_work(struct work_struct
*work
)
158 struct max77843_haptic
*haptic
=
159 container_of(work
, struct max77843_haptic
, work
);
162 mutex_lock(&haptic
->mutex
);
164 if (haptic
->suspended
)
167 if (haptic
->magnitude
) {
168 error
= max77843_haptic_set_duty_cycle(haptic
);
171 "failed to set duty cycle: %d\n", error
);
175 error
= max77843_haptic_enable(haptic
);
178 "cannot enable haptic: %d\n", error
);
180 error
= max77843_haptic_disable(haptic
);
183 "cannot disable haptic: %d\n", error
);
187 mutex_unlock(&haptic
->mutex
);
190 static int max77843_haptic_play_effect(struct input_dev
*dev
, void *data
,
191 struct ff_effect
*effect
)
193 struct max77843_haptic
*haptic
= input_get_drvdata(dev
);
194 u64 period_mag_multi
;
196 haptic
->magnitude
= effect
->u
.rumble
.strong_magnitude
;
197 if (!haptic
->magnitude
)
198 haptic
->magnitude
= effect
->u
.rumble
.weak_magnitude
;
200 period_mag_multi
= (u64
)haptic
->pwm_dev
->period
* haptic
->magnitude
;
201 haptic
->pwm_duty
= (unsigned int)(period_mag_multi
>>
202 MAX_MAGNITUDE_SHIFT
);
204 schedule_work(&haptic
->work
);
209 static int max77843_haptic_open(struct input_dev
*dev
)
211 struct max77843_haptic
*haptic
= input_get_drvdata(dev
);
214 error
= max77843_haptic_bias(haptic
, true);
218 error
= regulator_enable(haptic
->motor_reg
);
221 "failed to enable regulator: %d\n", error
);
228 static void max77843_haptic_close(struct input_dev
*dev
)
230 struct max77843_haptic
*haptic
= input_get_drvdata(dev
);
233 cancel_work_sync(&haptic
->work
);
234 max77843_haptic_disable(haptic
);
236 error
= regulator_disable(haptic
->motor_reg
);
239 "failed to disable regulator: %d\n", error
);
241 max77843_haptic_bias(haptic
, false);
244 static int max77843_haptic_probe(struct platform_device
*pdev
)
246 struct max77843
*max77843
= dev_get_drvdata(pdev
->dev
.parent
);
247 struct max77843_haptic
*haptic
;
250 haptic
= devm_kzalloc(&pdev
->dev
, sizeof(*haptic
), GFP_KERNEL
);
254 haptic
->regmap_haptic
= max77843
->regmap
;
255 haptic
->dev
= &pdev
->dev
;
256 haptic
->type
= MAX77843_HAPTIC_LRA
;
257 haptic
->pwm_divisor
= MAX77843_HAPTIC_PWM_DIVISOR_128
;
259 INIT_WORK(&haptic
->work
, max77843_haptic_play_work
);
260 mutex_init(&haptic
->mutex
);
262 haptic
->pwm_dev
= devm_pwm_get(&pdev
->dev
, NULL
);
263 if (IS_ERR(haptic
->pwm_dev
)) {
264 dev_err(&pdev
->dev
, "failed to get pwm device\n");
265 return PTR_ERR(haptic
->pwm_dev
);
268 haptic
->motor_reg
= devm_regulator_get_exclusive(&pdev
->dev
, "haptic");
269 if (IS_ERR(haptic
->motor_reg
)) {
270 dev_err(&pdev
->dev
, "failed to get regulator\n");
271 return PTR_ERR(haptic
->motor_reg
);
274 haptic
->input_dev
= devm_input_allocate_device(&pdev
->dev
);
275 if (!haptic
->input_dev
) {
276 dev_err(&pdev
->dev
, "failed to allocate input device\n");
280 haptic
->input_dev
->name
= "max77843-haptic";
281 haptic
->input_dev
->id
.version
= 1;
282 haptic
->input_dev
->dev
.parent
= &pdev
->dev
;
283 haptic
->input_dev
->open
= max77843_haptic_open
;
284 haptic
->input_dev
->close
= max77843_haptic_close
;
285 input_set_drvdata(haptic
->input_dev
, haptic
);
286 input_set_capability(haptic
->input_dev
, EV_FF
, FF_RUMBLE
);
288 error
= input_ff_create_memless(haptic
->input_dev
, NULL
,
289 max77843_haptic_play_effect
);
291 dev_err(&pdev
->dev
, "failed to create force-feedback\n");
295 error
= input_register_device(haptic
->input_dev
);
297 dev_err(&pdev
->dev
, "failed to register input device\n");
301 platform_set_drvdata(pdev
, haptic
);
306 static int __maybe_unused
max77843_haptic_suspend(struct device
*dev
)
308 struct platform_device
*pdev
= to_platform_device(dev
);
309 struct max77843_haptic
*haptic
= platform_get_drvdata(pdev
);
312 error
= mutex_lock_interruptible(&haptic
->mutex
);
316 max77843_haptic_disable(haptic
);
318 haptic
->suspended
= true;
320 mutex_unlock(&haptic
->mutex
);
325 static int __maybe_unused
max77843_haptic_resume(struct device
*dev
)
327 struct platform_device
*pdev
= to_platform_device(dev
);
328 struct max77843_haptic
*haptic
= platform_get_drvdata(pdev
);
329 unsigned int magnitude
;
331 mutex_lock(&haptic
->mutex
);
333 haptic
->suspended
= false;
335 magnitude
= ACCESS_ONCE(haptic
->magnitude
);
337 max77843_haptic_enable(haptic
);
339 mutex_unlock(&haptic
->mutex
);
344 static SIMPLE_DEV_PM_OPS(max77843_haptic_pm_ops
,
345 max77843_haptic_suspend
, max77843_haptic_resume
);
347 static struct platform_driver max77843_haptic_driver
= {
349 .name
= "max77843-haptic",
350 .pm
= &max77843_haptic_pm_ops
,
352 .probe
= max77843_haptic_probe
,
354 module_platform_driver(max77843_haptic_driver
);
356 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
357 MODULE_DESCRIPTION("MAXIM MAX77843 Haptic driver");
358 MODULE_LICENSE("GPL");