xref: /linux/drivers/input/misc/max77693-haptic.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MAXIM MAX77693/MAX77843 Haptic device driver
4  *
5  * Copyright (C) 2014,2015 Samsung Electronics
6  * Jaewon Kim <jaewon02.kim@samsung.com>
7  * Krzysztof Kozlowski <krzk@kernel.org>
8  *
9  * This program is not provided / owned by Maxim Integrated Products.
10  */
11 
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pwm.h>
20 #include <linux/slab.h>
21 #include <linux/string_choices.h>
22 #include <linux/workqueue.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/mfd/max77693.h>
25 #include <linux/mfd/max77693-common.h>
26 #include <linux/mfd/max77693-private.h>
27 #include <linux/mfd/max77843-private.h>
28 
29 #define MAX_MAGNITUDE_SHIFT	16
30 
31 enum max77693_haptic_motor_type {
32 	MAX77693_HAPTIC_ERM = 0,
33 	MAX77693_HAPTIC_LRA,
34 };
35 
36 enum max77693_haptic_pulse_mode {
37 	MAX77693_HAPTIC_EXTERNAL_MODE = 0,
38 	MAX77693_HAPTIC_INTERNAL_MODE,
39 };
40 
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,
46 };
47 
48 struct max77693_haptic {
49 	enum max77693_types dev_type;
50 
51 	struct regmap *regmap_pmic;
52 	struct regmap *regmap_haptic;
53 	struct device *dev;
54 	struct input_dev *input_dev;
55 	struct pwm_device *pwm_dev;
56 	struct regulator *motor_reg;
57 
58 	bool enabled;
59 	bool suspend_state;
60 	unsigned int magnitude;
61 	unsigned int pwm_duty;
62 	enum max77693_haptic_motor_type type;
63 	enum max77693_haptic_pulse_mode mode;
64 
65 	struct work_struct work;
66 };
67 
68 static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic)
69 {
70 	struct pwm_args pargs;
71 	int delta;
72 	int error;
73 
74 	pwm_get_args(haptic->pwm_dev, &pargs);
75 	delta = (pargs.period + haptic->pwm_duty) / 2;
76 	error = pwm_config(haptic->pwm_dev, delta, pargs.period);
77 	if (error) {
78 		dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
79 		return error;
80 	}
81 
82 	return 0;
83 }
84 
85 static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on)
86 {
87 	int error;
88 
89 	if (haptic->dev_type != TYPE_MAX77843)
90 		return 0;
91 
92 	error = regmap_update_bits(haptic->regmap_haptic,
93 				   MAX77843_SYS_REG_MAINCTRL1,
94 				   MAX77843_MAINCTRL1_BIASEN_MASK,
95 				   on << MAINCTRL1_BIASEN_SHIFT);
96 	if (error) {
97 		dev_err(haptic->dev, "failed to %s bias: %d\n",
98 			str_enable_disable(on), error);
99 		return error;
100 	}
101 
102 	return 0;
103 }
104 
105 static int max77693_haptic_configure(struct max77693_haptic *haptic,
106 				     bool enable)
107 {
108 	unsigned int value, config_reg;
109 	int error;
110 
111 	switch (haptic->dev_type) {
112 	case TYPE_MAX77693:
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;
118 		break;
119 	case TYPE_MAX77843:
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;
124 		break;
125 	default:
126 		return -EINVAL;
127 	}
128 
129 	error = regmap_write(haptic->regmap_haptic,
130 			     config_reg, value);
131 	if (error) {
132 		dev_err(haptic->dev,
133 			"failed to update haptic config: %d\n", error);
134 		return error;
135 	}
136 
137 	return 0;
138 }
139 
140 static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable)
141 {
142 	int error;
143 
144 	if (haptic->dev_type != TYPE_MAX77693)
145 		return 0;
146 
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);
151 	if (error) {
152 		dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error);
153 		return error;
154 	}
155 
156 	return 0;
157 }
158 
159 static void max77693_haptic_enable(struct max77693_haptic *haptic)
160 {
161 	int error;
162 
163 	if (haptic->enabled)
164 		return;
165 
166 	error = pwm_enable(haptic->pwm_dev);
167 	if (error) {
168 		dev_err(haptic->dev,
169 			"failed to enable haptic pwm device: %d\n", error);
170 		return;
171 	}
172 
173 	error = max77693_haptic_lowsys(haptic, true);
174 	if (error)
175 		goto err_enable_lowsys;
176 
177 	error = max77693_haptic_configure(haptic, true);
178 	if (error)
179 		goto err_enable_config;
180 
181 	haptic->enabled = true;
182 
183 	return;
184 
185 err_enable_config:
186 	max77693_haptic_lowsys(haptic, false);
187 err_enable_lowsys:
188 	pwm_disable(haptic->pwm_dev);
189 }
190 
191 static void max77693_haptic_disable(struct max77693_haptic *haptic)
192 {
193 	int error;
194 
195 	if (!haptic->enabled)
196 		return;
197 
198 	error = max77693_haptic_configure(haptic, false);
199 	if (error)
200 		return;
201 
202 	error = max77693_haptic_lowsys(haptic, false);
203 	if (error)
204 		goto err_disable_lowsys;
205 
206 	pwm_disable(haptic->pwm_dev);
207 	haptic->enabled = false;
208 
209 	return;
210 
211 err_disable_lowsys:
212 	max77693_haptic_configure(haptic, true);
213 }
214 
215 static void max77693_haptic_play_work(struct work_struct *work)
216 {
217 	struct max77693_haptic *haptic =
218 			container_of(work, struct max77693_haptic, work);
219 	int error;
220 
221 	error = max77693_haptic_set_duty_cycle(haptic);
222 	if (error) {
223 		dev_err(haptic->dev, "failed to set duty cycle: %d\n", error);
224 		return;
225 	}
226 
227 	if (haptic->magnitude)
228 		max77693_haptic_enable(haptic);
229 	else
230 		max77693_haptic_disable(haptic);
231 }
232 
233 static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
234 				       struct ff_effect *effect)
235 {
236 	struct max77693_haptic *haptic = input_get_drvdata(dev);
237 	struct pwm_args pargs;
238 	u64 period_mag_multi;
239 
240 	haptic->magnitude = effect->u.rumble.strong_magnitude;
241 	if (!haptic->magnitude)
242 		haptic->magnitude = effect->u.rumble.weak_magnitude;
243 
244 	/*
245 	 * The magnitude comes from force-feedback interface.
246 	 * The formula to convert magnitude to pwm_duty as follows:
247 	 * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
248 	 */
249 	pwm_get_args(haptic->pwm_dev, &pargs);
250 	period_mag_multi = (u64)pargs.period * haptic->magnitude;
251 	haptic->pwm_duty = (unsigned int)(period_mag_multi >>
252 						MAX_MAGNITUDE_SHIFT);
253 
254 	schedule_work(&haptic->work);
255 
256 	return 0;
257 }
258 
259 static int max77693_haptic_open(struct input_dev *dev)
260 {
261 	struct max77693_haptic *haptic = input_get_drvdata(dev);
262 	int error;
263 
264 	error = max77843_haptic_bias(haptic, true);
265 	if (error)
266 		return error;
267 
268 	error = regulator_enable(haptic->motor_reg);
269 	if (error) {
270 		dev_err(haptic->dev,
271 			"failed to enable regulator: %d\n", error);
272 		return error;
273 	}
274 
275 	return 0;
276 }
277 
278 static void max77693_haptic_close(struct input_dev *dev)
279 {
280 	struct max77693_haptic *haptic = input_get_drvdata(dev);
281 	int error;
282 
283 	cancel_work_sync(&haptic->work);
284 	max77693_haptic_disable(haptic);
285 
286 	error = regulator_disable(haptic->motor_reg);
287 	if (error)
288 		dev_err(haptic->dev,
289 			"failed to disable regulator: %d\n", error);
290 
291 	max77843_haptic_bias(haptic, false);
292 }
293 
294 static int max77693_haptic_probe(struct platform_device *pdev)
295 {
296 	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
297 	struct max77693_haptic *haptic;
298 	int error;
299 
300 	haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
301 	if (!haptic)
302 		return -ENOMEM;
303 
304 	haptic->regmap_pmic = max77693->regmap;
305 	haptic->dev = &pdev->dev;
306 	haptic->type = MAX77693_HAPTIC_LRA;
307 	haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE;
308 	haptic->suspend_state = false;
309 
310 	/* Variant-specific init */
311 	haptic->dev_type = max77693->type;
312 	switch (haptic->dev_type) {
313 	case TYPE_MAX77693:
314 		haptic->regmap_haptic = max77693->regmap_haptic;
315 		break;
316 	case TYPE_MAX77843:
317 		haptic->regmap_haptic = max77693->regmap;
318 		break;
319 	default:
320 		dev_err(&pdev->dev, "unsupported device type: %u\n",
321 			haptic->dev_type);
322 		return -EINVAL;
323 	}
324 
325 	INIT_WORK(&haptic->work, max77693_haptic_play_work);
326 
327 	/* Get pwm and regulatot for haptic device */
328 	haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
329 	if (IS_ERR(haptic->pwm_dev)) {
330 		dev_err(&pdev->dev, "failed to get pwm device\n");
331 		return PTR_ERR(haptic->pwm_dev);
332 	}
333 
334 	/*
335 	 * FIXME: pwm_apply_args() should be removed when switching to the
336 	 * atomic PWM API.
337 	 */
338 	pwm_apply_args(haptic->pwm_dev);
339 
340 	haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic");
341 	if (IS_ERR(haptic->motor_reg)) {
342 		dev_err(&pdev->dev, "failed to get regulator\n");
343 		return PTR_ERR(haptic->motor_reg);
344 	}
345 
346 	/* Initialize input device for haptic device */
347 	haptic->input_dev = devm_input_allocate_device(&pdev->dev);
348 	if (!haptic->input_dev) {
349 		dev_err(&pdev->dev, "failed to allocate input device\n");
350 		return -ENOMEM;
351 	}
352 
353 	haptic->input_dev->name = "max77693-haptic";
354 	haptic->input_dev->id.version = 1;
355 	haptic->input_dev->dev.parent = &pdev->dev;
356 	haptic->input_dev->open = max77693_haptic_open;
357 	haptic->input_dev->close = max77693_haptic_close;
358 	input_set_drvdata(haptic->input_dev, haptic);
359 	input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
360 
361 	error = input_ff_create_memless(haptic->input_dev, NULL,
362 				max77693_haptic_play_effect);
363 	if (error) {
364 		dev_err(&pdev->dev, "failed to create force-feedback\n");
365 		return error;
366 	}
367 
368 	error = input_register_device(haptic->input_dev);
369 	if (error) {
370 		dev_err(&pdev->dev, "failed to register input device\n");
371 		return error;
372 	}
373 
374 	platform_set_drvdata(pdev, haptic);
375 
376 	return 0;
377 }
378 
379 static int max77693_haptic_suspend(struct device *dev)
380 {
381 	struct platform_device *pdev = to_platform_device(dev);
382 	struct max77693_haptic *haptic = platform_get_drvdata(pdev);
383 
384 	if (haptic->enabled) {
385 		max77693_haptic_disable(haptic);
386 		haptic->suspend_state = true;
387 	}
388 
389 	return 0;
390 }
391 
392 static int max77693_haptic_resume(struct device *dev)
393 {
394 	struct platform_device *pdev = to_platform_device(dev);
395 	struct max77693_haptic *haptic = platform_get_drvdata(pdev);
396 
397 	if (haptic->suspend_state) {
398 		max77693_haptic_enable(haptic);
399 		haptic->suspend_state = false;
400 	}
401 
402 	return 0;
403 }
404 
405 static DEFINE_SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops,
406 				max77693_haptic_suspend,
407 				max77693_haptic_resume);
408 
409 static const struct platform_device_id max77693_haptic_id[] = {
410 	{ "max77693-haptic", },
411 	{ "max77843-haptic", },
412 	{},
413 };
414 MODULE_DEVICE_TABLE(platform, max77693_haptic_id);
415 
416 static const struct of_device_id of_max77693_haptic_dt_match[] = {
417 	{ .compatible = "maxim,max77693-haptic", },
418 	{ .compatible = "maxim,max77843-haptic", },
419 	{ /* sentinel */ },
420 };
421 MODULE_DEVICE_TABLE(of, of_max77693_haptic_dt_match);
422 
423 static struct platform_driver max77693_haptic_driver = {
424 	.driver		= {
425 		.name	= "max77693-haptic",
426 		.pm	= pm_sleep_ptr(&max77693_haptic_pm_ops),
427 		.of_match_table = of_max77693_haptic_dt_match,
428 	},
429 	.probe		= max77693_haptic_probe,
430 	.id_table	= max77693_haptic_id,
431 };
432 module_platform_driver(max77693_haptic_driver);
433 
434 MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
435 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
436 MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");
437 MODULE_LICENSE("GPL");
438