Lines Matching +full:haptic +full:- +full:driver
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MAXIM MAX77693/MAX77843 Haptic device driver
24 #include <linux/mfd/max77693-common.h>
25 #include <linux/mfd/max77693-private.h>
26 #include <linux/mfd/max77843-private.h>
67 static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic) in max77693_haptic_set_duty_cycle() argument
73 pwm_get_args(haptic->pwm_dev, &pargs); in max77693_haptic_set_duty_cycle()
74 delta = (pargs.period + haptic->pwm_duty) / 2; in max77693_haptic_set_duty_cycle()
75 error = pwm_config(haptic->pwm_dev, delta, pargs.period); in max77693_haptic_set_duty_cycle()
77 dev_err(haptic->dev, "failed to configure pwm: %d\n", error); in max77693_haptic_set_duty_cycle()
84 static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on) in max77843_haptic_bias() argument
88 if (haptic->dev_type != TYPE_MAX77843) in max77843_haptic_bias()
91 error = regmap_update_bits(haptic->regmap_haptic, in max77843_haptic_bias()
96 dev_err(haptic->dev, "failed to %s bias: %d\n", in max77843_haptic_bias()
104 static int max77693_haptic_configure(struct max77693_haptic *haptic, in max77693_haptic_configure() argument
110 switch (haptic->dev_type) { in max77693_haptic_configure()
112 value = ((haptic->type << MAX77693_CONFIG2_MODE) | in max77693_haptic_configure()
114 (haptic->mode << MAX77693_CONFIG2_HTYP) | in max77693_haptic_configure()
119 value = (haptic->type << MCONFIG_MODE_SHIFT) | in max77693_haptic_configure()
125 return -EINVAL; in max77693_haptic_configure()
128 error = regmap_write(haptic->regmap_haptic, in max77693_haptic_configure()
131 dev_err(haptic->dev, in max77693_haptic_configure()
132 "failed to update haptic config: %d\n", error); in max77693_haptic_configure()
139 static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable) in max77693_haptic_lowsys() argument
143 if (haptic->dev_type != TYPE_MAX77693) in max77693_haptic_lowsys()
146 error = regmap_update_bits(haptic->regmap_pmic, in max77693_haptic_lowsys()
151 dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error); in max77693_haptic_lowsys()
158 static void max77693_haptic_enable(struct max77693_haptic *haptic) in max77693_haptic_enable() argument
162 if (haptic->enabled) in max77693_haptic_enable()
165 error = pwm_enable(haptic->pwm_dev); in max77693_haptic_enable()
167 dev_err(haptic->dev, in max77693_haptic_enable()
168 "failed to enable haptic pwm device: %d\n", error); in max77693_haptic_enable()
172 error = max77693_haptic_lowsys(haptic, true); in max77693_haptic_enable()
176 error = max77693_haptic_configure(haptic, true); in max77693_haptic_enable()
180 haptic->enabled = true; in max77693_haptic_enable()
185 max77693_haptic_lowsys(haptic, false); in max77693_haptic_enable()
187 pwm_disable(haptic->pwm_dev); in max77693_haptic_enable()
190 static void max77693_haptic_disable(struct max77693_haptic *haptic) in max77693_haptic_disable() argument
194 if (!haptic->enabled) in max77693_haptic_disable()
197 error = max77693_haptic_configure(haptic, false); in max77693_haptic_disable()
201 error = max77693_haptic_lowsys(haptic, false); in max77693_haptic_disable()
205 pwm_disable(haptic->pwm_dev); in max77693_haptic_disable()
206 haptic->enabled = false; in max77693_haptic_disable()
211 max77693_haptic_configure(haptic, true); in max77693_haptic_disable()
216 struct max77693_haptic *haptic = in max77693_haptic_play_work() local
220 error = max77693_haptic_set_duty_cycle(haptic); in max77693_haptic_play_work()
222 dev_err(haptic->dev, "failed to set duty cycle: %d\n", error); in max77693_haptic_play_work()
226 if (haptic->magnitude) in max77693_haptic_play_work()
227 max77693_haptic_enable(haptic); in max77693_haptic_play_work()
229 max77693_haptic_disable(haptic); in max77693_haptic_play_work()
235 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_play_effect() local
239 haptic->magnitude = effect->u.rumble.strong_magnitude; in max77693_haptic_play_effect()
240 if (!haptic->magnitude) in max77693_haptic_play_effect()
241 haptic->magnitude = effect->u.rumble.weak_magnitude; in max77693_haptic_play_effect()
244 * The magnitude comes from force-feedback interface. in max77693_haptic_play_effect()
246 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF) in max77693_haptic_play_effect()
248 pwm_get_args(haptic->pwm_dev, &pargs); in max77693_haptic_play_effect()
249 period_mag_multi = (u64)pargs.period * haptic->magnitude; in max77693_haptic_play_effect()
250 haptic->pwm_duty = (unsigned int)(period_mag_multi >> in max77693_haptic_play_effect()
253 schedule_work(&haptic->work); in max77693_haptic_play_effect()
260 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_open() local
263 error = max77843_haptic_bias(haptic, true); in max77693_haptic_open()
267 error = regulator_enable(haptic->motor_reg); in max77693_haptic_open()
269 dev_err(haptic->dev, in max77693_haptic_open()
279 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_close() local
282 cancel_work_sync(&haptic->work); in max77693_haptic_close()
283 max77693_haptic_disable(haptic); in max77693_haptic_close()
285 error = regulator_disable(haptic->motor_reg); in max77693_haptic_close()
287 dev_err(haptic->dev, in max77693_haptic_close()
290 max77843_haptic_bias(haptic, false); in max77693_haptic_close()
295 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); in max77693_haptic_probe()
296 struct max77693_haptic *haptic; in max77693_haptic_probe() local
299 haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL); in max77693_haptic_probe()
300 if (!haptic) in max77693_haptic_probe()
301 return -ENOMEM; in max77693_haptic_probe()
303 haptic->regmap_pmic = max77693->regmap; in max77693_haptic_probe()
304 haptic->dev = &pdev->dev; in max77693_haptic_probe()
305 haptic->type = MAX77693_HAPTIC_LRA; in max77693_haptic_probe()
306 haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE; in max77693_haptic_probe()
307 haptic->suspend_state = false; in max77693_haptic_probe()
309 /* Variant-specific init */ in max77693_haptic_probe()
310 haptic->dev_type = max77693->type; in max77693_haptic_probe()
311 switch (haptic->dev_type) { in max77693_haptic_probe()
313 haptic->regmap_haptic = max77693->regmap_haptic; in max77693_haptic_probe()
316 haptic->regmap_haptic = max77693->regmap; in max77693_haptic_probe()
319 dev_err(&pdev->dev, "unsupported device type: %u\n", in max77693_haptic_probe()
320 haptic->dev_type); in max77693_haptic_probe()
321 return -EINVAL; in max77693_haptic_probe()
324 INIT_WORK(&haptic->work, max77693_haptic_play_work); in max77693_haptic_probe()
326 /* Get pwm and regulatot for haptic device */ in max77693_haptic_probe()
327 haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL); in max77693_haptic_probe()
328 if (IS_ERR(haptic->pwm_dev)) { in max77693_haptic_probe()
329 dev_err(&pdev->dev, "failed to get pwm device\n"); in max77693_haptic_probe()
330 return PTR_ERR(haptic->pwm_dev); in max77693_haptic_probe()
337 pwm_apply_args(haptic->pwm_dev); in max77693_haptic_probe()
339 haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic"); in max77693_haptic_probe()
340 if (IS_ERR(haptic->motor_reg)) { in max77693_haptic_probe()
341 dev_err(&pdev->dev, "failed to get regulator\n"); in max77693_haptic_probe()
342 return PTR_ERR(haptic->motor_reg); in max77693_haptic_probe()
345 /* Initialize input device for haptic device */ in max77693_haptic_probe()
346 haptic->input_dev = devm_input_allocate_device(&pdev->dev); in max77693_haptic_probe()
347 if (!haptic->input_dev) { in max77693_haptic_probe()
348 dev_err(&pdev->dev, "failed to allocate input device\n"); in max77693_haptic_probe()
349 return -ENOMEM; in max77693_haptic_probe()
352 haptic->input_dev->name = "max77693-haptic"; in max77693_haptic_probe()
353 haptic->input_dev->id.version = 1; in max77693_haptic_probe()
354 haptic->input_dev->dev.parent = &pdev->dev; in max77693_haptic_probe()
355 haptic->input_dev->open = max77693_haptic_open; in max77693_haptic_probe()
356 haptic->input_dev->close = max77693_haptic_close; in max77693_haptic_probe()
357 input_set_drvdata(haptic->input_dev, haptic); in max77693_haptic_probe()
358 input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE); in max77693_haptic_probe()
360 error = input_ff_create_memless(haptic->input_dev, NULL, in max77693_haptic_probe()
363 dev_err(&pdev->dev, "failed to create force-feedback\n"); in max77693_haptic_probe()
367 error = input_register_device(haptic->input_dev); in max77693_haptic_probe()
369 dev_err(&pdev->dev, "failed to register input device\n"); in max77693_haptic_probe()
373 platform_set_drvdata(pdev, haptic); in max77693_haptic_probe()
381 struct max77693_haptic *haptic = platform_get_drvdata(pdev); in max77693_haptic_suspend() local
383 if (haptic->enabled) { in max77693_haptic_suspend()
384 max77693_haptic_disable(haptic); in max77693_haptic_suspend()
385 haptic->suspend_state = true; in max77693_haptic_suspend()
394 struct max77693_haptic *haptic = platform_get_drvdata(pdev); in max77693_haptic_resume() local
396 if (haptic->suspend_state) { in max77693_haptic_resume()
397 max77693_haptic_enable(haptic); in max77693_haptic_resume()
398 haptic->suspend_state = false; in max77693_haptic_resume()
409 { "max77693-haptic", },
410 { "max77843-haptic", },
416 { .compatible = "maxim,max77693-haptic", },
417 { .compatible = "maxim,max77843-haptic", },
423 .driver = {
424 .name = "max77693-haptic",
435 MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");