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