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