cxl: Fix typo in debug print
[linux/fpc-iii.git] / drivers / input / misc / max77843-haptic.c
blobdccbb465a055ebe16c9929f987b0117cc38f7ee8
1 /*
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,
30 MAX77843_HAPTIC_LRA,
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;
42 struct device *dev;
43 struct input_dev *input_dev;
44 struct pwm_device *pwm_dev;
45 struct regulator *motor_reg;
46 struct work_struct work;
47 struct mutex mutex;
49 unsigned int magnitude;
50 unsigned int pwm_duty;
52 bool active;
53 bool suspended;
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;
62 int error;
64 error = pwm_config(haptic->pwm_dev, delta, haptic->pwm_dev->period);
65 if (error) {
66 dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
67 return error;
70 return 0;
73 static int max77843_haptic_bias(struct max77843_haptic *haptic, bool on)
75 int error;
77 error = regmap_update_bits(haptic->regmap_haptic,
78 MAX77843_SYS_REG_MAINCTRL1,
79 MAX77843_MAINCTRL1_BIASEN_MASK,
80 on << MAINCTRL1_BIASEN_SHIFT);
81 if (error) {
82 dev_err(haptic->dev, "failed to %s bias: %d\n",
83 on ? "enable" : "disable", error);
84 return error;
87 return 0;
90 static int max77843_haptic_config(struct max77843_haptic *haptic, bool enable)
92 unsigned int value;
93 int error;
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);
101 if (error) {
102 dev_err(haptic->dev,
103 "failed to update haptic config: %d\n", error);
104 return error;
107 return 0;
110 static int max77843_haptic_enable(struct max77843_haptic *haptic)
112 int error;
114 if (haptic->active)
115 return 0;
117 error = pwm_enable(haptic->pwm_dev);
118 if (error) {
119 dev_err(haptic->dev,
120 "failed to enable pwm device: %d\n", error);
121 return error;
124 error = max77843_haptic_config(haptic, true);
125 if (error)
126 goto err_config;
128 haptic->active = true;
130 return 0;
132 err_config:
133 pwm_disable(haptic->pwm_dev);
135 return error;
138 static int max77843_haptic_disable(struct max77843_haptic *haptic)
140 int error;
142 if (!haptic->active)
143 return 0;
145 error = max77843_haptic_config(haptic, false);
146 if (error)
147 return error;
149 pwm_disable(haptic->pwm_dev);
151 haptic->active = false;
153 return 0;
156 static void max77843_haptic_play_work(struct work_struct *work)
158 struct max77843_haptic *haptic =
159 container_of(work, struct max77843_haptic, work);
160 int error;
162 mutex_lock(&haptic->mutex);
164 if (haptic->suspended)
165 goto out_unlock;
167 if (haptic->magnitude) {
168 error = max77843_haptic_set_duty_cycle(haptic);
169 if (error) {
170 dev_err(haptic->dev,
171 "failed to set duty cycle: %d\n", error);
172 goto out_unlock;
175 error = max77843_haptic_enable(haptic);
176 if (error)
177 dev_err(haptic->dev,
178 "cannot enable haptic: %d\n", error);
179 } else {
180 error = max77843_haptic_disable(haptic);
181 if (error)
182 dev_err(haptic->dev,
183 "cannot disable haptic: %d\n", error);
186 out_unlock:
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);
206 return 0;
209 static int max77843_haptic_open(struct input_dev *dev)
211 struct max77843_haptic *haptic = input_get_drvdata(dev);
212 int error;
214 error = max77843_haptic_bias(haptic, true);
215 if (error)
216 return error;
218 error = regulator_enable(haptic->motor_reg);
219 if (error) {
220 dev_err(haptic->dev,
221 "failed to enable regulator: %d\n", error);
222 return error;
225 return 0;
228 static void max77843_haptic_close(struct input_dev *dev)
230 struct max77843_haptic *haptic = input_get_drvdata(dev);
231 int error;
233 cancel_work_sync(&haptic->work);
234 max77843_haptic_disable(haptic);
236 error = regulator_disable(haptic->motor_reg);
237 if (error)
238 dev_err(haptic->dev,
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;
248 int error;
250 haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
251 if (!haptic)
252 return -ENOMEM;
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");
277 return -ENOMEM;
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);
290 if (error) {
291 dev_err(&pdev->dev, "failed to create force-feedback\n");
292 return error;
295 error = input_register_device(haptic->input_dev);
296 if (error) {
297 dev_err(&pdev->dev, "failed to register input device\n");
298 return error;
301 platform_set_drvdata(pdev, haptic);
303 return 0;
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);
310 int error;
312 error = mutex_lock_interruptible(&haptic->mutex);
313 if (error)
314 return error;
316 max77843_haptic_disable(haptic);
318 haptic->suspended = true;
320 mutex_unlock(&haptic->mutex);
322 return 0;
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);
336 if (magnitude)
337 max77843_haptic_enable(haptic);
339 mutex_unlock(&haptic->mutex);
341 return 0;
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 = {
348 .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");