Lines Matching +full:regulator +full:- +full:haptic
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MAXIM MAX77693/MAX77843 Haptic device driver
23 #include <linux/regulator/consumer.h>
25 #include <linux/mfd/max77693-common.h>
26 #include <linux/mfd/max77693-private.h>
27 #include <linux/mfd/max77843-private.h>
56 struct regulator *motor_reg;
68 static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic) in max77693_haptic_set_duty_cycle() argument
74 pwm_get_args(haptic->pwm_dev, &pargs); in max77693_haptic_set_duty_cycle()
75 delta = (pargs.period + haptic->pwm_duty) / 2; in max77693_haptic_set_duty_cycle()
76 error = pwm_config(haptic->pwm_dev, delta, pargs.period); in max77693_haptic_set_duty_cycle()
78 dev_err(haptic->dev, "failed to configure pwm: %d\n", error); in max77693_haptic_set_duty_cycle()
85 static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on) in max77843_haptic_bias() argument
89 if (haptic->dev_type != TYPE_MAX77843) in max77843_haptic_bias()
92 error = regmap_update_bits(haptic->regmap_haptic, in max77843_haptic_bias()
97 dev_err(haptic->dev, "failed to %s bias: %d\n", in max77843_haptic_bias()
105 static int max77693_haptic_configure(struct max77693_haptic *haptic, in max77693_haptic_configure() argument
111 switch (haptic->dev_type) { in max77693_haptic_configure()
113 value = ((haptic->type << MAX77693_CONFIG2_MODE) | in max77693_haptic_configure()
115 (haptic->mode << MAX77693_CONFIG2_HTYP) | in max77693_haptic_configure()
120 value = (haptic->type << MCONFIG_MODE_SHIFT) | in max77693_haptic_configure()
126 return -EINVAL; in max77693_haptic_configure()
129 error = regmap_write(haptic->regmap_haptic, in max77693_haptic_configure()
132 dev_err(haptic->dev, in max77693_haptic_configure()
133 "failed to update haptic config: %d\n", error); in max77693_haptic_configure()
140 static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable) in max77693_haptic_lowsys() argument
144 if (haptic->dev_type != TYPE_MAX77693) in max77693_haptic_lowsys()
147 error = regmap_update_bits(haptic->regmap_pmic, in max77693_haptic_lowsys()
152 dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error); in max77693_haptic_lowsys()
159 static void max77693_haptic_enable(struct max77693_haptic *haptic) in max77693_haptic_enable() argument
163 if (haptic->enabled) in max77693_haptic_enable()
166 error = pwm_enable(haptic->pwm_dev); in max77693_haptic_enable()
168 dev_err(haptic->dev, in max77693_haptic_enable()
169 "failed to enable haptic pwm device: %d\n", error); in max77693_haptic_enable()
173 error = max77693_haptic_lowsys(haptic, true); in max77693_haptic_enable()
177 error = max77693_haptic_configure(haptic, true); in max77693_haptic_enable()
181 haptic->enabled = true; in max77693_haptic_enable()
186 max77693_haptic_lowsys(haptic, false); in max77693_haptic_enable()
188 pwm_disable(haptic->pwm_dev); in max77693_haptic_enable()
191 static void max77693_haptic_disable(struct max77693_haptic *haptic) in max77693_haptic_disable() argument
195 if (!haptic->enabled) in max77693_haptic_disable()
198 error = max77693_haptic_configure(haptic, false); in max77693_haptic_disable()
202 error = max77693_haptic_lowsys(haptic, false); in max77693_haptic_disable()
206 pwm_disable(haptic->pwm_dev); in max77693_haptic_disable()
207 haptic->enabled = false; in max77693_haptic_disable()
212 max77693_haptic_configure(haptic, true); in max77693_haptic_disable()
217 struct max77693_haptic *haptic = in max77693_haptic_play_work() local
221 error = max77693_haptic_set_duty_cycle(haptic); in max77693_haptic_play_work()
223 dev_err(haptic->dev, "failed to set duty cycle: %d\n", error); in max77693_haptic_play_work()
227 if (haptic->magnitude) in max77693_haptic_play_work()
228 max77693_haptic_enable(haptic); in max77693_haptic_play_work()
230 max77693_haptic_disable(haptic); in max77693_haptic_play_work()
236 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_play_effect() local
240 haptic->magnitude = effect->u.rumble.strong_magnitude; in max77693_haptic_play_effect()
241 if (!haptic->magnitude) in max77693_haptic_play_effect()
242 haptic->magnitude = effect->u.rumble.weak_magnitude; in max77693_haptic_play_effect()
245 * The magnitude comes from force-feedback interface. in max77693_haptic_play_effect()
247 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF) in max77693_haptic_play_effect()
249 pwm_get_args(haptic->pwm_dev, &pargs); in max77693_haptic_play_effect()
250 period_mag_multi = (u64)pargs.period * haptic->magnitude; in max77693_haptic_play_effect()
251 haptic->pwm_duty = (unsigned int)(period_mag_multi >> in max77693_haptic_play_effect()
254 schedule_work(&haptic->work); in max77693_haptic_play_effect()
261 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_open() local
264 error = max77843_haptic_bias(haptic, true); in max77693_haptic_open()
268 error = regulator_enable(haptic->motor_reg); in max77693_haptic_open()
270 dev_err(haptic->dev, in max77693_haptic_open()
271 "failed to enable regulator: %d\n", error); in max77693_haptic_open()
280 struct max77693_haptic *haptic = input_get_drvdata(dev); in max77693_haptic_close() local
283 cancel_work_sync(&haptic->work); in max77693_haptic_close()
284 max77693_haptic_disable(haptic); in max77693_haptic_close()
286 error = regulator_disable(haptic->motor_reg); in max77693_haptic_close()
288 dev_err(haptic->dev, in max77693_haptic_close()
289 "failed to disable regulator: %d\n", error); in max77693_haptic_close()
291 max77843_haptic_bias(haptic, false); in max77693_haptic_close()
296 struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); in max77693_haptic_probe()
297 struct max77693_haptic *haptic; in max77693_haptic_probe() local
300 haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL); in max77693_haptic_probe()
301 if (!haptic) in max77693_haptic_probe()
302 return -ENOMEM; in max77693_haptic_probe()
304 haptic->regmap_pmic = max77693->regmap; in max77693_haptic_probe()
305 haptic->dev = &pdev->dev; in max77693_haptic_probe()
306 haptic->type = MAX77693_HAPTIC_LRA; in max77693_haptic_probe()
307 haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE; in max77693_haptic_probe()
308 haptic->suspend_state = false; in max77693_haptic_probe()
310 /* Variant-specific init */ in max77693_haptic_probe()
311 haptic->dev_type = max77693->type; in max77693_haptic_probe()
312 switch (haptic->dev_type) { in max77693_haptic_probe()
314 haptic->regmap_haptic = max77693->regmap_haptic; in max77693_haptic_probe()
317 haptic->regmap_haptic = max77693->regmap; in max77693_haptic_probe()
320 dev_err(&pdev->dev, "unsupported device type: %u\n", in max77693_haptic_probe()
321 haptic->dev_type); in max77693_haptic_probe()
322 return -EINVAL; in max77693_haptic_probe()
325 INIT_WORK(&haptic->work, max77693_haptic_play_work); in max77693_haptic_probe()
327 /* Get pwm and regulatot for haptic device */ in max77693_haptic_probe()
328 haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL); in max77693_haptic_probe()
329 if (IS_ERR(haptic->pwm_dev)) { in max77693_haptic_probe()
330 dev_err(&pdev->dev, "failed to get pwm device\n"); in max77693_haptic_probe()
331 return PTR_ERR(haptic->pwm_dev); in max77693_haptic_probe()
338 pwm_apply_args(haptic->pwm_dev); in max77693_haptic_probe()
340 haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic"); in max77693_haptic_probe()
341 if (IS_ERR(haptic->motor_reg)) { in max77693_haptic_probe()
342 dev_err(&pdev->dev, "failed to get regulator\n"); in max77693_haptic_probe()
343 return PTR_ERR(haptic->motor_reg); in max77693_haptic_probe()
346 /* Initialize input device for haptic device */ in max77693_haptic_probe()
347 haptic->input_dev = devm_input_allocate_device(&pdev->dev); in max77693_haptic_probe()
348 if (!haptic->input_dev) { in max77693_haptic_probe()
349 dev_err(&pdev->dev, "failed to allocate input device\n"); in max77693_haptic_probe()
350 return -ENOMEM; in max77693_haptic_probe()
353 haptic->input_dev->name = "max77693-haptic"; in max77693_haptic_probe()
354 haptic->input_dev->id.version = 1; in max77693_haptic_probe()
355 haptic->input_dev->dev.parent = &pdev->dev; in max77693_haptic_probe()
356 haptic->input_dev->open = max77693_haptic_open; in max77693_haptic_probe()
357 haptic->input_dev->close = max77693_haptic_close; in max77693_haptic_probe()
358 input_set_drvdata(haptic->input_dev, haptic); in max77693_haptic_probe()
359 input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE); in max77693_haptic_probe()
361 error = input_ff_create_memless(haptic->input_dev, NULL, in max77693_haptic_probe()
364 dev_err(&pdev->dev, "failed to create force-feedback\n"); in max77693_haptic_probe()
368 error = input_register_device(haptic->input_dev); in max77693_haptic_probe()
370 dev_err(&pdev->dev, "failed to register input device\n"); in max77693_haptic_probe()
374 platform_set_drvdata(pdev, haptic); in max77693_haptic_probe()
382 struct max77693_haptic *haptic = platform_get_drvdata(pdev); in max77693_haptic_suspend() local
384 if (haptic->enabled) { in max77693_haptic_suspend()
385 max77693_haptic_disable(haptic); in max77693_haptic_suspend()
386 haptic->suspend_state = true; in max77693_haptic_suspend()
395 struct max77693_haptic *haptic = platform_get_drvdata(pdev); in max77693_haptic_resume() local
397 if (haptic->suspend_state) { in max77693_haptic_resume()
398 max77693_haptic_enable(haptic); in max77693_haptic_resume()
399 haptic->suspend_state = false; in max77693_haptic_resume()
410 { "max77693-haptic", },
411 { "max77843-haptic", },
417 { .compatible = "maxim,max77693-haptic", },
418 { .compatible = "maxim,max77843-haptic", },
425 .name = "max77693-haptic",
436 MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");