Commit | Line | Data |
---|---|---|
a3b3ca75 JK |
1 | /* |
2 | * MAXIM MAX77693 Haptic device driver | |
3 | * | |
4 | * Copyright (C) 2014 Samsung Electronics | |
5 | * Jaewon Kim <jaewon02.kim@samsung.com> | |
6 | * | |
7 | * This program is not provided / owned by Maxim Integrated Products. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | */ | |
14 | ||
15 | #include <linux/err.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/regmap.h> | |
19 | #include <linux/input.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/platform_device.h> | |
22 | #include <linux/pwm.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/workqueue.h> | |
25 | #include <linux/regulator/consumer.h> | |
26 | #include <linux/mfd/max77693.h> | |
61b305cd | 27 | #include <linux/mfd/max77693-common.h> |
a3b3ca75 JK |
28 | #include <linux/mfd/max77693-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 | struct regmap *regmap_pmic; | |
51 | struct regmap *regmap_haptic; | |
52 | struct device *dev; | |
53 | struct input_dev *input_dev; | |
54 | struct pwm_device *pwm_dev; | |
55 | struct regulator *motor_reg; | |
56 | ||
57 | bool enabled; | |
58 | bool suspend_state; | |
59 | unsigned int magnitude; | |
60 | unsigned int pwm_duty; | |
61 | enum max77693_haptic_motor_type type; | |
62 | enum max77693_haptic_pulse_mode mode; | |
63 | enum max77693_haptic_pwm_divisor pwm_divisor; | |
64 | ||
65 | struct work_struct work; | |
66 | }; | |
67 | ||
68 | static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic) | |
69 | { | |
70 | int delta = (haptic->pwm_dev->period + haptic->pwm_duty) / 2; | |
71 | int error; | |
72 | ||
73 | error = pwm_config(haptic->pwm_dev, delta, haptic->pwm_dev->period); | |
74 | if (error) { | |
75 | dev_err(haptic->dev, "failed to configure pwm: %d\n", error); | |
76 | return error; | |
77 | } | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
82 | static int max77693_haptic_configure(struct max77693_haptic *haptic, | |
83 | bool enable) | |
84 | { | |
85 | unsigned int value; | |
86 | int error; | |
87 | ||
88 | value = ((haptic->type << MAX77693_CONFIG2_MODE) | | |
89 | (enable << MAX77693_CONFIG2_MEN) | | |
90 | (haptic->mode << MAX77693_CONFIG2_HTYP) | | |
91 | (haptic->pwm_divisor)); | |
92 | ||
93 | error = regmap_write(haptic->regmap_haptic, | |
94 | MAX77693_HAPTIC_REG_CONFIG2, value); | |
95 | if (error) { | |
96 | dev_err(haptic->dev, | |
97 | "failed to update haptic config: %d\n", error); | |
98 | return error; | |
99 | } | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable) | |
105 | { | |
106 | int error; | |
107 | ||
108 | error = regmap_update_bits(haptic->regmap_pmic, | |
109 | MAX77693_PMIC_REG_LSCNFG, | |
110 | MAX77693_PMIC_LOW_SYS_MASK, | |
111 | enable << MAX77693_PMIC_LOW_SYS_SHIFT); | |
112 | if (error) { | |
113 | dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error); | |
114 | return error; | |
115 | } | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static void max77693_haptic_enable(struct max77693_haptic *haptic) | |
121 | { | |
122 | int error; | |
123 | ||
124 | if (haptic->enabled) | |
125 | return; | |
126 | ||
127 | error = pwm_enable(haptic->pwm_dev); | |
128 | if (error) { | |
129 | dev_err(haptic->dev, | |
130 | "failed to enable haptic pwm device: %d\n", error); | |
131 | return; | |
132 | } | |
133 | ||
134 | error = max77693_haptic_lowsys(haptic, true); | |
135 | if (error) | |
136 | goto err_enable_lowsys; | |
137 | ||
138 | error = max77693_haptic_configure(haptic, true); | |
139 | if (error) | |
140 | goto err_enable_config; | |
141 | ||
142 | haptic->enabled = true; | |
143 | ||
144 | return; | |
145 | ||
146 | err_enable_config: | |
147 | max77693_haptic_lowsys(haptic, false); | |
148 | err_enable_lowsys: | |
149 | pwm_disable(haptic->pwm_dev); | |
150 | } | |
151 | ||
152 | static void max77693_haptic_disable(struct max77693_haptic *haptic) | |
153 | { | |
154 | int error; | |
155 | ||
135d916f | 156 | if (!haptic->enabled) |
a3b3ca75 JK |
157 | return; |
158 | ||
159 | error = max77693_haptic_configure(haptic, false); | |
160 | if (error) | |
161 | return; | |
162 | ||
163 | error = max77693_haptic_lowsys(haptic, false); | |
164 | if (error) | |
165 | goto err_disable_lowsys; | |
166 | ||
167 | pwm_disable(haptic->pwm_dev); | |
168 | haptic->enabled = false; | |
169 | ||
170 | return; | |
171 | ||
172 | err_disable_lowsys: | |
173 | max77693_haptic_configure(haptic, true); | |
174 | } | |
175 | ||
176 | static void max77693_haptic_play_work(struct work_struct *work) | |
177 | { | |
178 | struct max77693_haptic *haptic = | |
179 | container_of(work, struct max77693_haptic, work); | |
180 | int error; | |
181 | ||
182 | error = max77693_haptic_set_duty_cycle(haptic); | |
183 | if (error) { | |
184 | dev_err(haptic->dev, "failed to set duty cycle: %d\n", error); | |
185 | return; | |
186 | } | |
187 | ||
188 | if (haptic->magnitude) | |
189 | max77693_haptic_enable(haptic); | |
190 | else | |
191 | max77693_haptic_disable(haptic); | |
192 | } | |
193 | ||
194 | static int max77693_haptic_play_effect(struct input_dev *dev, void *data, | |
195 | struct ff_effect *effect) | |
196 | { | |
197 | struct max77693_haptic *haptic = input_get_drvdata(dev); | |
fbefc5e7 | 198 | u64 period_mag_multi; |
a3b3ca75 JK |
199 | |
200 | haptic->magnitude = effect->u.rumble.strong_magnitude; | |
201 | if (!haptic->magnitude) | |
202 | haptic->magnitude = effect->u.rumble.weak_magnitude; | |
203 | ||
204 | /* | |
205 | * The magnitude comes from force-feedback interface. | |
206 | * The formula to convert magnitude to pwm_duty as follows: | |
207 | * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF) | |
208 | */ | |
fbefc5e7 | 209 | period_mag_multi = (u64)haptic->pwm_dev->period * haptic->magnitude; |
a3b3ca75 JK |
210 | haptic->pwm_duty = (unsigned int)(period_mag_multi >> |
211 | MAX_MAGNITUDE_SHIFT); | |
212 | ||
213 | schedule_work(&haptic->work); | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | static int max77693_haptic_open(struct input_dev *dev) | |
219 | { | |
220 | struct max77693_haptic *haptic = input_get_drvdata(dev); | |
221 | int error; | |
222 | ||
223 | error = regulator_enable(haptic->motor_reg); | |
224 | if (error) { | |
225 | dev_err(haptic->dev, | |
226 | "failed to enable regulator: %d\n", error); | |
227 | return error; | |
228 | } | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | static void max77693_haptic_close(struct input_dev *dev) | |
234 | { | |
235 | struct max77693_haptic *haptic = input_get_drvdata(dev); | |
236 | int error; | |
237 | ||
238 | cancel_work_sync(&haptic->work); | |
239 | max77693_haptic_disable(haptic); | |
240 | ||
241 | error = regulator_disable(haptic->motor_reg); | |
242 | if (error) | |
243 | dev_err(haptic->dev, | |
244 | "failed to disable regulator: %d\n", error); | |
245 | } | |
246 | ||
247 | static int max77693_haptic_probe(struct platform_device *pdev) | |
248 | { | |
249 | struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent); | |
250 | struct max77693_haptic *haptic; | |
251 | int error; | |
252 | ||
253 | haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL); | |
254 | if (!haptic) | |
255 | return -ENOMEM; | |
256 | ||
257 | haptic->regmap_pmic = max77693->regmap; | |
258 | haptic->regmap_haptic = max77693->regmap_haptic; | |
259 | haptic->dev = &pdev->dev; | |
260 | haptic->type = MAX77693_HAPTIC_LRA; | |
261 | haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE; | |
262 | haptic->pwm_divisor = MAX77693_HAPTIC_PWM_DIVISOR_128; | |
263 | haptic->suspend_state = false; | |
264 | ||
265 | INIT_WORK(&haptic->work, max77693_haptic_play_work); | |
266 | ||
267 | /* Get pwm and regulatot for haptic device */ | |
268 | haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL); | |
269 | if (IS_ERR(haptic->pwm_dev)) { | |
270 | dev_err(&pdev->dev, "failed to get pwm device\n"); | |
271 | return PTR_ERR(haptic->pwm_dev); | |
272 | } | |
273 | ||
274 | haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic"); | |
275 | if (IS_ERR(haptic->motor_reg)) { | |
276 | dev_err(&pdev->dev, "failed to get regulator\n"); | |
277 | return PTR_ERR(haptic->motor_reg); | |
278 | } | |
279 | ||
280 | /* Initialize input device for haptic device */ | |
281 | haptic->input_dev = devm_input_allocate_device(&pdev->dev); | |
282 | if (!haptic->input_dev) { | |
283 | dev_err(&pdev->dev, "failed to allocate input device\n"); | |
284 | return -ENOMEM; | |
285 | } | |
286 | ||
287 | haptic->input_dev->name = "max77693-haptic"; | |
288 | haptic->input_dev->id.version = 1; | |
289 | haptic->input_dev->dev.parent = &pdev->dev; | |
290 | haptic->input_dev->open = max77693_haptic_open; | |
291 | haptic->input_dev->close = max77693_haptic_close; | |
292 | input_set_drvdata(haptic->input_dev, haptic); | |
293 | input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE); | |
294 | ||
295 | error = input_ff_create_memless(haptic->input_dev, NULL, | |
296 | max77693_haptic_play_effect); | |
297 | if (error) { | |
298 | dev_err(&pdev->dev, "failed to create force-feedback\n"); | |
299 | return error; | |
300 | } | |
301 | ||
302 | error = input_register_device(haptic->input_dev); | |
303 | if (error) { | |
304 | dev_err(&pdev->dev, "failed to register input device\n"); | |
305 | return error; | |
306 | } | |
307 | ||
308 | platform_set_drvdata(pdev, haptic); | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
97a652a8 | 313 | static int __maybe_unused max77693_haptic_suspend(struct device *dev) |
a3b3ca75 JK |
314 | { |
315 | struct platform_device *pdev = to_platform_device(dev); | |
316 | struct max77693_haptic *haptic = platform_get_drvdata(pdev); | |
317 | ||
318 | if (haptic->enabled) { | |
319 | max77693_haptic_disable(haptic); | |
320 | haptic->suspend_state = true; | |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
97a652a8 | 326 | static int __maybe_unused max77693_haptic_resume(struct device *dev) |
a3b3ca75 JK |
327 | { |
328 | struct platform_device *pdev = to_platform_device(dev); | |
329 | struct max77693_haptic *haptic = platform_get_drvdata(pdev); | |
330 | ||
331 | if (haptic->suspend_state) { | |
332 | max77693_haptic_enable(haptic); | |
333 | haptic->suspend_state = false; | |
334 | } | |
335 | ||
336 | return 0; | |
337 | } | |
a3b3ca75 JK |
338 | |
339 | static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops, | |
340 | max77693_haptic_suspend, max77693_haptic_resume); | |
341 | ||
342 | static struct platform_driver max77693_haptic_driver = { | |
343 | .driver = { | |
344 | .name = "max77693-haptic", | |
a3b3ca75 JK |
345 | .pm = &max77693_haptic_pm_ops, |
346 | }, | |
347 | .probe = max77693_haptic_probe, | |
348 | }; | |
349 | module_platform_driver(max77693_haptic_driver); | |
350 | ||
351 | MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>"); | |
352 | MODULE_DESCRIPTION("MAXIM MAX77693 Haptic driver"); | |
353 | MODULE_ALIAS("platform:max77693-haptic"); | |
354 | MODULE_LICENSE("GPL"); |