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