2 * MAXIM MAX77693 Haptic device driver
4 * Copyright (C) 2014 Samsung Electronics
5 * Jaewon Kim <jaewon02.kim@samsung.com>
7 * This program is not provided / owned by Maxim Integrated Products.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/input.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/pwm.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/mfd/max77693.h>
27 #include <linux/mfd/max77693-private.h>
29 #define MAX_MAGNITUDE_SHIFT 16
31 enum max77693_haptic_motor_type
{
32 MAX77693_HAPTIC_ERM
= 0,
36 enum max77693_haptic_pulse_mode
{
37 MAX77693_HAPTIC_EXTERNAL_MODE
= 0,
38 MAX77693_HAPTIC_INTERNAL_MODE
,
41 enum max77693_haptic_pwm_divisor
{
42 MAX77693_HAPTIC_PWM_DIVISOR_32
= 0,
43 MAX77693_HAPTIC_PWM_DIVISOR_64
,
44 MAX77693_HAPTIC_PWM_DIVISOR_128
,
45 MAX77693_HAPTIC_PWM_DIVISOR_256
,
48 struct max77693_haptic
{
49 struct regmap
*regmap_pmic
;
50 struct regmap
*regmap_haptic
;
52 struct input_dev
*input_dev
;
53 struct pwm_device
*pwm_dev
;
54 struct regulator
*motor_reg
;
58 unsigned int magnitude
;
59 unsigned int pwm_duty
;
60 enum max77693_haptic_motor_type type
;
61 enum max77693_haptic_pulse_mode mode
;
62 enum max77693_haptic_pwm_divisor pwm_divisor
;
64 struct work_struct work
;
67 static int max77693_haptic_set_duty_cycle(struct max77693_haptic
*haptic
)
69 int delta
= (haptic
->pwm_dev
->period
+ haptic
->pwm_duty
) / 2;
72 error
= pwm_config(haptic
->pwm_dev
, delta
, haptic
->pwm_dev
->period
);
74 dev_err(haptic
->dev
, "failed to configure pwm: %d\n", error
);
81 static int max77693_haptic_configure(struct max77693_haptic
*haptic
,
87 value
= ((haptic
->type
<< MAX77693_CONFIG2_MODE
) |
88 (enable
<< MAX77693_CONFIG2_MEN
) |
89 (haptic
->mode
<< MAX77693_CONFIG2_HTYP
) |
90 (haptic
->pwm_divisor
));
92 error
= regmap_write(haptic
->regmap_haptic
,
93 MAX77693_HAPTIC_REG_CONFIG2
, value
);
96 "failed to update haptic config: %d\n", error
);
103 static int max77693_haptic_lowsys(struct max77693_haptic
*haptic
, bool enable
)
107 error
= regmap_update_bits(haptic
->regmap_pmic
,
108 MAX77693_PMIC_REG_LSCNFG
,
109 MAX77693_PMIC_LOW_SYS_MASK
,
110 enable
<< MAX77693_PMIC_LOW_SYS_SHIFT
);
112 dev_err(haptic
->dev
, "cannot update pmic regmap: %d\n", error
);
119 static void max77693_haptic_enable(struct max77693_haptic
*haptic
)
126 error
= pwm_enable(haptic
->pwm_dev
);
129 "failed to enable haptic pwm device: %d\n", error
);
133 error
= max77693_haptic_lowsys(haptic
, true);
135 goto err_enable_lowsys
;
137 error
= max77693_haptic_configure(haptic
, true);
139 goto err_enable_config
;
141 haptic
->enabled
= true;
146 max77693_haptic_lowsys(haptic
, false);
148 pwm_disable(haptic
->pwm_dev
);
151 static void max77693_haptic_disable(struct max77693_haptic
*haptic
)
155 if (!haptic
->enabled
)
158 error
= max77693_haptic_configure(haptic
, false);
162 error
= max77693_haptic_lowsys(haptic
, false);
164 goto err_disable_lowsys
;
166 pwm_disable(haptic
->pwm_dev
);
167 haptic
->enabled
= false;
172 max77693_haptic_configure(haptic
, true);
175 static void max77693_haptic_play_work(struct work_struct
*work
)
177 struct max77693_haptic
*haptic
=
178 container_of(work
, struct max77693_haptic
, work
);
181 error
= max77693_haptic_set_duty_cycle(haptic
);
183 dev_err(haptic
->dev
, "failed to set duty cycle: %d\n", error
);
187 if (haptic
->magnitude
)
188 max77693_haptic_enable(haptic
);
190 max77693_haptic_disable(haptic
);
193 static int max77693_haptic_play_effect(struct input_dev
*dev
, void *data
,
194 struct ff_effect
*effect
)
196 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
197 u64 period_mag_multi
;
199 haptic
->magnitude
= effect
->u
.rumble
.strong_magnitude
;
200 if (!haptic
->magnitude
)
201 haptic
->magnitude
= effect
->u
.rumble
.weak_magnitude
;
204 * The magnitude comes from force-feedback interface.
205 * The formula to convert magnitude to pwm_duty as follows:
206 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
208 period_mag_multi
= (u64
)haptic
->pwm_dev
->period
* haptic
->magnitude
;
209 haptic
->pwm_duty
= (unsigned int)(period_mag_multi
>>
210 MAX_MAGNITUDE_SHIFT
);
212 schedule_work(&haptic
->work
);
217 static int max77693_haptic_open(struct input_dev
*dev
)
219 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
222 error
= regulator_enable(haptic
->motor_reg
);
225 "failed to enable regulator: %d\n", error
);
232 static void max77693_haptic_close(struct input_dev
*dev
)
234 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
237 cancel_work_sync(&haptic
->work
);
238 max77693_haptic_disable(haptic
);
240 error
= regulator_disable(haptic
->motor_reg
);
243 "failed to disable regulator: %d\n", error
);
246 static int max77693_haptic_probe(struct platform_device
*pdev
)
248 struct max77693_dev
*max77693
= dev_get_drvdata(pdev
->dev
.parent
);
249 struct max77693_haptic
*haptic
;
252 haptic
= devm_kzalloc(&pdev
->dev
, sizeof(*haptic
), GFP_KERNEL
);
256 haptic
->regmap_pmic
= max77693
->regmap
;
257 haptic
->regmap_haptic
= max77693
->regmap_haptic
;
258 haptic
->dev
= &pdev
->dev
;
259 haptic
->type
= MAX77693_HAPTIC_LRA
;
260 haptic
->mode
= MAX77693_HAPTIC_EXTERNAL_MODE
;
261 haptic
->pwm_divisor
= MAX77693_HAPTIC_PWM_DIVISOR_128
;
262 haptic
->suspend_state
= false;
264 INIT_WORK(&haptic
->work
, max77693_haptic_play_work
);
266 /* Get pwm and regulatot for haptic device */
267 haptic
->pwm_dev
= devm_pwm_get(&pdev
->dev
, NULL
);
268 if (IS_ERR(haptic
->pwm_dev
)) {
269 dev_err(&pdev
->dev
, "failed to get pwm device\n");
270 return PTR_ERR(haptic
->pwm_dev
);
273 haptic
->motor_reg
= devm_regulator_get(&pdev
->dev
, "haptic");
274 if (IS_ERR(haptic
->motor_reg
)) {
275 dev_err(&pdev
->dev
, "failed to get regulator\n");
276 return PTR_ERR(haptic
->motor_reg
);
279 /* Initialize input device for haptic device */
280 haptic
->input_dev
= devm_input_allocate_device(&pdev
->dev
);
281 if (!haptic
->input_dev
) {
282 dev_err(&pdev
->dev
, "failed to allocate input device\n");
286 haptic
->input_dev
->name
= "max77693-haptic";
287 haptic
->input_dev
->id
.version
= 1;
288 haptic
->input_dev
->dev
.parent
= &pdev
->dev
;
289 haptic
->input_dev
->open
= max77693_haptic_open
;
290 haptic
->input_dev
->close
= max77693_haptic_close
;
291 input_set_drvdata(haptic
->input_dev
, haptic
);
292 input_set_capability(haptic
->input_dev
, EV_FF
, FF_RUMBLE
);
294 error
= input_ff_create_memless(haptic
->input_dev
, NULL
,
295 max77693_haptic_play_effect
);
297 dev_err(&pdev
->dev
, "failed to create force-feedback\n");
301 error
= input_register_device(haptic
->input_dev
);
303 dev_err(&pdev
->dev
, "failed to register input device\n");
307 platform_set_drvdata(pdev
, haptic
);
312 static int __maybe_unused
max77693_haptic_suspend(struct device
*dev
)
314 struct platform_device
*pdev
= to_platform_device(dev
);
315 struct max77693_haptic
*haptic
= platform_get_drvdata(pdev
);
317 if (haptic
->enabled
) {
318 max77693_haptic_disable(haptic
);
319 haptic
->suspend_state
= true;
325 static int __maybe_unused
max77693_haptic_resume(struct device
*dev
)
327 struct platform_device
*pdev
= to_platform_device(dev
);
328 struct max77693_haptic
*haptic
= platform_get_drvdata(pdev
);
330 if (haptic
->suspend_state
) {
331 max77693_haptic_enable(haptic
);
332 haptic
->suspend_state
= false;
338 static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops
,
339 max77693_haptic_suspend
, max77693_haptic_resume
);
341 static struct platform_driver max77693_haptic_driver
= {
343 .name
= "max77693-haptic",
344 .pm
= &max77693_haptic_pm_ops
,
346 .probe
= max77693_haptic_probe
,
348 module_platform_driver(max77693_haptic_driver
);
350 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
351 MODULE_DESCRIPTION("MAXIM MAX77693 Haptic driver");
352 MODULE_ALIAS("platform:max77693-haptic");
353 MODULE_LICENSE("GPL");