2 * MAXIM MAX77693/MAX77843 Haptic device driver
4 * Copyright (C) 2014,2015 Samsung Electronics
5 * Jaewon Kim <jaewon02.kim@samsung.com>
6 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
8 * This program is not provided / owned by Maxim Integrated Products.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include <linux/input.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pwm.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/mfd/max77693.h>
28 #include <linux/mfd/max77693-common.h>
29 #include <linux/mfd/max77693-private.h>
30 #include <linux/mfd/max77843-private.h>
32 #define MAX_MAGNITUDE_SHIFT 16
34 enum max77693_haptic_motor_type
{
35 MAX77693_HAPTIC_ERM
= 0,
39 enum max77693_haptic_pulse_mode
{
40 MAX77693_HAPTIC_EXTERNAL_MODE
= 0,
41 MAX77693_HAPTIC_INTERNAL_MODE
,
44 enum max77693_haptic_pwm_divisor
{
45 MAX77693_HAPTIC_PWM_DIVISOR_32
= 0,
46 MAX77693_HAPTIC_PWM_DIVISOR_64
,
47 MAX77693_HAPTIC_PWM_DIVISOR_128
,
48 MAX77693_HAPTIC_PWM_DIVISOR_256
,
51 struct max77693_haptic
{
52 enum max77693_types dev_type
;
54 struct regmap
*regmap_pmic
;
55 struct regmap
*regmap_haptic
;
57 struct input_dev
*input_dev
;
58 struct pwm_device
*pwm_dev
;
59 struct regulator
*motor_reg
;
63 unsigned int magnitude
;
64 unsigned int pwm_duty
;
65 enum max77693_haptic_motor_type type
;
66 enum max77693_haptic_pulse_mode mode
;
68 struct work_struct work
;
71 static int max77693_haptic_set_duty_cycle(struct max77693_haptic
*haptic
)
73 int delta
= (haptic
->pwm_dev
->period
+ haptic
->pwm_duty
) / 2;
76 error
= pwm_config(haptic
->pwm_dev
, delta
, haptic
->pwm_dev
->period
);
78 dev_err(haptic
->dev
, "failed to configure pwm: %d\n", error
);
85 static int max77843_haptic_bias(struct max77693_haptic
*haptic
, bool on
)
89 if (haptic
->dev_type
!= TYPE_MAX77843
)
92 error
= regmap_update_bits(haptic
->regmap_haptic
,
93 MAX77843_SYS_REG_MAINCTRL1
,
94 MAX77843_MAINCTRL1_BIASEN_MASK
,
95 on
<< MAINCTRL1_BIASEN_SHIFT
);
97 dev_err(haptic
->dev
, "failed to %s bias: %d\n",
98 on
? "enable" : "disable", error
);
105 static int max77693_haptic_configure(struct max77693_haptic
*haptic
,
108 unsigned int value
, config_reg
;
111 switch (haptic
->dev_type
) {
113 value
= ((haptic
->type
<< MAX77693_CONFIG2_MODE
) |
114 (enable
<< MAX77693_CONFIG2_MEN
) |
115 (haptic
->mode
<< MAX77693_CONFIG2_HTYP
) |
116 MAX77693_HAPTIC_PWM_DIVISOR_128
);
117 config_reg
= MAX77693_HAPTIC_REG_CONFIG2
;
120 value
= (haptic
->type
<< MCONFIG_MODE_SHIFT
) |
121 (enable
<< MCONFIG_MEN_SHIFT
) |
122 MAX77693_HAPTIC_PWM_DIVISOR_128
;
123 config_reg
= MAX77843_HAP_REG_MCONFIG
;
129 error
= regmap_write(haptic
->regmap_haptic
,
133 "failed to update haptic config: %d\n", error
);
140 static int max77693_haptic_lowsys(struct max77693_haptic
*haptic
, bool enable
)
144 if (haptic
->dev_type
!= TYPE_MAX77693
)
147 error
= regmap_update_bits(haptic
->regmap_pmic
,
148 MAX77693_PMIC_REG_LSCNFG
,
149 MAX77693_PMIC_LOW_SYS_MASK
,
150 enable
<< MAX77693_PMIC_LOW_SYS_SHIFT
);
152 dev_err(haptic
->dev
, "cannot update pmic regmap: %d\n", error
);
159 static void max77693_haptic_enable(struct max77693_haptic
*haptic
)
166 error
= pwm_enable(haptic
->pwm_dev
);
169 "failed to enable haptic pwm device: %d\n", error
);
173 error
= max77693_haptic_lowsys(haptic
, true);
175 goto err_enable_lowsys
;
177 error
= max77693_haptic_configure(haptic
, true);
179 goto err_enable_config
;
181 haptic
->enabled
= true;
186 max77693_haptic_lowsys(haptic
, false);
188 pwm_disable(haptic
->pwm_dev
);
191 static void max77693_haptic_disable(struct max77693_haptic
*haptic
)
195 if (!haptic
->enabled
)
198 error
= max77693_haptic_configure(haptic
, false);
202 error
= max77693_haptic_lowsys(haptic
, false);
204 goto err_disable_lowsys
;
206 pwm_disable(haptic
->pwm_dev
);
207 haptic
->enabled
= false;
212 max77693_haptic_configure(haptic
, true);
215 static void max77693_haptic_play_work(struct work_struct
*work
)
217 struct max77693_haptic
*haptic
=
218 container_of(work
, struct max77693_haptic
, work
);
221 error
= max77693_haptic_set_duty_cycle(haptic
);
223 dev_err(haptic
->dev
, "failed to set duty cycle: %d\n", error
);
227 if (haptic
->magnitude
)
228 max77693_haptic_enable(haptic
);
230 max77693_haptic_disable(haptic
);
233 static int max77693_haptic_play_effect(struct input_dev
*dev
, void *data
,
234 struct ff_effect
*effect
)
236 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
237 u64 period_mag_multi
;
239 haptic
->magnitude
= effect
->u
.rumble
.strong_magnitude
;
240 if (!haptic
->magnitude
)
241 haptic
->magnitude
= effect
->u
.rumble
.weak_magnitude
;
244 * The magnitude comes from force-feedback interface.
245 * The formula to convert magnitude to pwm_duty as follows:
246 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
248 period_mag_multi
= (u64
)haptic
->pwm_dev
->period
* haptic
->magnitude
;
249 haptic
->pwm_duty
= (unsigned int)(period_mag_multi
>>
250 MAX_MAGNITUDE_SHIFT
);
252 schedule_work(&haptic
->work
);
257 static int max77693_haptic_open(struct input_dev
*dev
)
259 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
262 error
= max77843_haptic_bias(haptic
, true);
266 error
= regulator_enable(haptic
->motor_reg
);
269 "failed to enable regulator: %d\n", error
);
276 static void max77693_haptic_close(struct input_dev
*dev
)
278 struct max77693_haptic
*haptic
= input_get_drvdata(dev
);
281 cancel_work_sync(&haptic
->work
);
282 max77693_haptic_disable(haptic
);
284 error
= regulator_disable(haptic
->motor_reg
);
287 "failed to disable regulator: %d\n", error
);
289 max77843_haptic_bias(haptic
, false);
292 static int max77693_haptic_probe(struct platform_device
*pdev
)
294 struct max77693_dev
*max77693
= dev_get_drvdata(pdev
->dev
.parent
);
295 struct max77693_haptic
*haptic
;
298 haptic
= devm_kzalloc(&pdev
->dev
, sizeof(*haptic
), GFP_KERNEL
);
302 haptic
->regmap_pmic
= max77693
->regmap
;
303 haptic
->dev
= &pdev
->dev
;
304 haptic
->type
= MAX77693_HAPTIC_LRA
;
305 haptic
->mode
= MAX77693_HAPTIC_EXTERNAL_MODE
;
306 haptic
->suspend_state
= false;
308 /* Variant-specific init */
309 haptic
->dev_type
= platform_get_device_id(pdev
)->driver_data
;
310 switch (haptic
->dev_type
) {
312 haptic
->regmap_haptic
= max77693
->regmap_haptic
;
315 haptic
->regmap_haptic
= max77693
->regmap
;
318 dev_err(&pdev
->dev
, "unsupported device type: %u\n",
323 INIT_WORK(&haptic
->work
, max77693_haptic_play_work
);
325 /* Get pwm and regulatot for haptic device */
326 haptic
->pwm_dev
= devm_pwm_get(&pdev
->dev
, NULL
);
327 if (IS_ERR(haptic
->pwm_dev
)) {
328 dev_err(&pdev
->dev
, "failed to get pwm device\n");
329 return PTR_ERR(haptic
->pwm_dev
);
332 haptic
->motor_reg
= devm_regulator_get(&pdev
->dev
, "haptic");
333 if (IS_ERR(haptic
->motor_reg
)) {
334 dev_err(&pdev
->dev
, "failed to get regulator\n");
335 return PTR_ERR(haptic
->motor_reg
);
338 /* Initialize input device for haptic device */
339 haptic
->input_dev
= devm_input_allocate_device(&pdev
->dev
);
340 if (!haptic
->input_dev
) {
341 dev_err(&pdev
->dev
, "failed to allocate input device\n");
345 haptic
->input_dev
->name
= "max77693-haptic";
346 haptic
->input_dev
->id
.version
= 1;
347 haptic
->input_dev
->dev
.parent
= &pdev
->dev
;
348 haptic
->input_dev
->open
= max77693_haptic_open
;
349 haptic
->input_dev
->close
= max77693_haptic_close
;
350 input_set_drvdata(haptic
->input_dev
, haptic
);
351 input_set_capability(haptic
->input_dev
, EV_FF
, FF_RUMBLE
);
353 error
= input_ff_create_memless(haptic
->input_dev
, NULL
,
354 max77693_haptic_play_effect
);
356 dev_err(&pdev
->dev
, "failed to create force-feedback\n");
360 error
= input_register_device(haptic
->input_dev
);
362 dev_err(&pdev
->dev
, "failed to register input device\n");
366 platform_set_drvdata(pdev
, haptic
);
371 static int __maybe_unused
max77693_haptic_suspend(struct device
*dev
)
373 struct platform_device
*pdev
= to_platform_device(dev
);
374 struct max77693_haptic
*haptic
= platform_get_drvdata(pdev
);
376 if (haptic
->enabled
) {
377 max77693_haptic_disable(haptic
);
378 haptic
->suspend_state
= true;
384 static int __maybe_unused
max77693_haptic_resume(struct device
*dev
)
386 struct platform_device
*pdev
= to_platform_device(dev
);
387 struct max77693_haptic
*haptic
= platform_get_drvdata(pdev
);
389 if (haptic
->suspend_state
) {
390 max77693_haptic_enable(haptic
);
391 haptic
->suspend_state
= false;
397 static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops
,
398 max77693_haptic_suspend
, max77693_haptic_resume
);
400 static const struct platform_device_id max77693_haptic_id
[] = {
401 { "max77693-haptic", TYPE_MAX77693
},
402 { "max77843-haptic", TYPE_MAX77843
},
405 MODULE_DEVICE_TABLE(platform
, max77693_haptic_id
);
407 static struct platform_driver max77693_haptic_driver
= {
409 .name
= "max77693-haptic",
410 .pm
= &max77693_haptic_pm_ops
,
412 .probe
= max77693_haptic_probe
,
413 .id_table
= max77693_haptic_id
,
415 module_platform_driver(max77693_haptic_driver
);
417 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
418 MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>");
419 MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");
420 MODULE_ALIAS("platform:max77693-haptic");
421 MODULE_LICENSE("GPL");