1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MAX8997-haptic controller driver
5 * Copyright (C) 2012 Samsung Electronics
6 * Donggeun Kim <dg77.kim@samsung.com>
8 * This program is not provided / owned by Maxim Integrated Products.
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/err.h>
15 #include <linux/pwm.h>
16 #include <linux/input.h>
17 #include <linux/mfd/max8997-private.h>
18 #include <linux/mfd/max8997.h>
19 #include <linux/regulator/consumer.h>
21 /* Haptic configuration 2 register */
22 #define MAX8997_MOTOR_TYPE_SHIFT 7
23 #define MAX8997_ENABLE_SHIFT 6
24 #define MAX8997_MODE_SHIFT 5
26 /* Haptic driver configuration register */
27 #define MAX8997_CYCLE_SHIFT 6
28 #define MAX8997_SIG_PERIOD_SHIFT 4
29 #define MAX8997_SIG_DUTY_SHIFT 2
30 #define MAX8997_PWM_DUTY_SHIFT 0
32 struct max8997_haptic
{
34 struct i2c_client
*client
;
35 struct input_dev
*input_dev
;
36 struct regulator
*regulator
;
38 struct work_struct work
;
44 struct pwm_device
*pwm
;
45 unsigned int pwm_period
;
46 enum max8997_haptic_pwm_divisor pwm_divisor
;
48 enum max8997_haptic_motor_type type
;
49 enum max8997_haptic_pulse_mode mode
;
51 unsigned int internal_mode_pattern
;
52 unsigned int pattern_cycle
;
53 unsigned int pattern_signal_period
;
56 static int max8997_haptic_set_duty_cycle(struct max8997_haptic
*chip
)
60 if (chip
->mode
== MAX8997_EXTERNAL_MODE
) {
61 unsigned int duty
= chip
->pwm_period
* chip
->level
/ 100;
62 ret
= pwm_config(chip
->pwm
, duty
, chip
->pwm_period
);
67 for (i
= 0; i
<= 64; i
++) {
68 if (chip
->level
<= i
* 100 / 64) {
73 switch (chip
->internal_mode_pattern
) {
75 max8997_write_reg(chip
->client
,
76 MAX8997_HAPTIC_REG_SIGPWMDC1
, duty_index
);
79 max8997_write_reg(chip
->client
,
80 MAX8997_HAPTIC_REG_SIGPWMDC2
, duty_index
);
83 max8997_write_reg(chip
->client
,
84 MAX8997_HAPTIC_REG_SIGPWMDC3
, duty_index
);
87 max8997_write_reg(chip
->client
,
88 MAX8997_HAPTIC_REG_SIGPWMDC4
, duty_index
);
97 static void max8997_haptic_configure(struct max8997_haptic
*chip
)
101 value
= chip
->type
<< MAX8997_MOTOR_TYPE_SHIFT
|
102 chip
->enabled
<< MAX8997_ENABLE_SHIFT
|
103 chip
->mode
<< MAX8997_MODE_SHIFT
| chip
->pwm_divisor
;
104 max8997_write_reg(chip
->client
, MAX8997_HAPTIC_REG_CONF2
, value
);
106 if (chip
->mode
== MAX8997_INTERNAL_MODE
&& chip
->enabled
) {
107 value
= chip
->internal_mode_pattern
<< MAX8997_CYCLE_SHIFT
|
108 chip
->internal_mode_pattern
<< MAX8997_SIG_PERIOD_SHIFT
|
109 chip
->internal_mode_pattern
<< MAX8997_SIG_DUTY_SHIFT
|
110 chip
->internal_mode_pattern
<< MAX8997_PWM_DUTY_SHIFT
;
111 max8997_write_reg(chip
->client
,
112 MAX8997_HAPTIC_REG_DRVCONF
, value
);
114 switch (chip
->internal_mode_pattern
) {
116 value
= chip
->pattern_cycle
<< 4;
117 max8997_write_reg(chip
->client
,
118 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
119 value
= chip
->pattern_signal_period
;
120 max8997_write_reg(chip
->client
,
121 MAX8997_HAPTIC_REG_SIGCONF1
, value
);
125 value
= chip
->pattern_cycle
;
126 max8997_write_reg(chip
->client
,
127 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
128 value
= chip
->pattern_signal_period
;
129 max8997_write_reg(chip
->client
,
130 MAX8997_HAPTIC_REG_SIGCONF2
, value
);
134 value
= chip
->pattern_cycle
<< 4;
135 max8997_write_reg(chip
->client
,
136 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
137 value
= chip
->pattern_signal_period
;
138 max8997_write_reg(chip
->client
,
139 MAX8997_HAPTIC_REG_SIGCONF3
, value
);
143 value
= chip
->pattern_cycle
;
144 max8997_write_reg(chip
->client
,
145 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
146 value
= chip
->pattern_signal_period
;
147 max8997_write_reg(chip
->client
,
148 MAX8997_HAPTIC_REG_SIGCONF4
, value
);
157 static void max8997_haptic_enable(struct max8997_haptic
*chip
)
161 mutex_lock(&chip
->mutex
);
163 error
= max8997_haptic_set_duty_cycle(chip
);
165 dev_err(chip
->dev
, "set_pwm_cycle failed, error: %d\n", error
);
169 if (!chip
->enabled
) {
170 error
= regulator_enable(chip
->regulator
);
172 dev_err(chip
->dev
, "Failed to enable regulator\n");
175 max8997_haptic_configure(chip
);
176 if (chip
->mode
== MAX8997_EXTERNAL_MODE
) {
177 error
= pwm_enable(chip
->pwm
);
179 dev_err(chip
->dev
, "Failed to enable PWM\n");
180 regulator_disable(chip
->regulator
);
184 chip
->enabled
= true;
188 mutex_unlock(&chip
->mutex
);
191 static void max8997_haptic_disable(struct max8997_haptic
*chip
)
193 mutex_lock(&chip
->mutex
);
196 chip
->enabled
= false;
197 max8997_haptic_configure(chip
);
198 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
199 pwm_disable(chip
->pwm
);
200 regulator_disable(chip
->regulator
);
203 mutex_unlock(&chip
->mutex
);
206 static void max8997_haptic_play_effect_work(struct work_struct
*work
)
208 struct max8997_haptic
*chip
=
209 container_of(work
, struct max8997_haptic
, work
);
212 max8997_haptic_enable(chip
);
214 max8997_haptic_disable(chip
);
217 static int max8997_haptic_play_effect(struct input_dev
*dev
, void *data
,
218 struct ff_effect
*effect
)
220 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
222 chip
->level
= effect
->u
.rumble
.strong_magnitude
;
224 chip
->level
= effect
->u
.rumble
.weak_magnitude
;
226 schedule_work(&chip
->work
);
231 static void max8997_haptic_close(struct input_dev
*dev
)
233 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
235 cancel_work_sync(&chip
->work
);
236 max8997_haptic_disable(chip
);
239 static int max8997_haptic_probe(struct platform_device
*pdev
)
241 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
242 const struct max8997_platform_data
*pdata
=
243 dev_get_platdata(iodev
->dev
);
244 const struct max8997_haptic_platform_data
*haptic_pdata
= NULL
;
245 struct max8997_haptic
*chip
;
246 struct input_dev
*input_dev
;
250 haptic_pdata
= pdata
->haptic_pdata
;
253 dev_err(&pdev
->dev
, "no haptic platform data\n");
257 chip
= kzalloc(sizeof(struct max8997_haptic
), GFP_KERNEL
);
258 input_dev
= input_allocate_device();
259 if (!chip
|| !input_dev
) {
260 dev_err(&pdev
->dev
, "unable to allocate memory\n");
265 INIT_WORK(&chip
->work
, max8997_haptic_play_effect_work
);
266 mutex_init(&chip
->mutex
);
268 chip
->client
= iodev
->haptic
;
269 chip
->dev
= &pdev
->dev
;
270 chip
->input_dev
= input_dev
;
271 chip
->pwm_period
= haptic_pdata
->pwm_period
;
272 chip
->type
= haptic_pdata
->type
;
273 chip
->mode
= haptic_pdata
->mode
;
274 chip
->pwm_divisor
= haptic_pdata
->pwm_divisor
;
276 switch (chip
->mode
) {
277 case MAX8997_INTERNAL_MODE
:
278 chip
->internal_mode_pattern
=
279 haptic_pdata
->internal_mode_pattern
;
280 chip
->pattern_cycle
= haptic_pdata
->pattern_cycle
;
281 chip
->pattern_signal_period
=
282 haptic_pdata
->pattern_signal_period
;
285 case MAX8997_EXTERNAL_MODE
:
286 chip
->pwm
= pwm_request(haptic_pdata
->pwm_channel_id
,
288 if (IS_ERR(chip
->pwm
)) {
289 error
= PTR_ERR(chip
->pwm
);
291 "unable to request PWM for haptic, error: %d\n",
297 * FIXME: pwm_apply_args() should be removed when switching to
298 * the atomic PWM API.
300 pwm_apply_args(chip
->pwm
);
305 "Invalid chip mode specified (%d)\n", chip
->mode
);
310 chip
->regulator
= regulator_get(&pdev
->dev
, "inmotor");
311 if (IS_ERR(chip
->regulator
)) {
312 error
= PTR_ERR(chip
->regulator
);
314 "unable to get regulator, error: %d\n",
319 input_dev
->name
= "max8997-haptic";
320 input_dev
->id
.version
= 1;
321 input_dev
->dev
.parent
= &pdev
->dev
;
322 input_dev
->close
= max8997_haptic_close
;
323 input_set_drvdata(input_dev
, chip
);
324 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
326 error
= input_ff_create_memless(input_dev
, NULL
,
327 max8997_haptic_play_effect
);
330 "unable to create FF device, error: %d\n",
332 goto err_put_regulator
;
335 error
= input_register_device(input_dev
);
338 "unable to register input device, error: %d\n",
343 platform_set_drvdata(pdev
, chip
);
347 input_ff_destroy(input_dev
);
349 regulator_put(chip
->regulator
);
351 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
354 input_free_device(input_dev
);
360 static int max8997_haptic_remove(struct platform_device
*pdev
)
362 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
364 input_unregister_device(chip
->input_dev
);
365 regulator_put(chip
->regulator
);
367 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
375 static int __maybe_unused
max8997_haptic_suspend(struct device
*dev
)
377 struct platform_device
*pdev
= to_platform_device(dev
);
378 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
380 max8997_haptic_disable(chip
);
385 static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops
, max8997_haptic_suspend
, NULL
);
387 static const struct platform_device_id max8997_haptic_id
[] = {
388 { "max8997-haptic", 0 },
391 MODULE_DEVICE_TABLE(platform
, max8997_haptic_id
);
393 static struct platform_driver max8997_haptic_driver
= {
395 .name
= "max8997-haptic",
396 .pm
= &max8997_haptic_pm_ops
,
398 .probe
= max8997_haptic_probe
,
399 .remove
= max8997_haptic_remove
,
400 .id_table
= max8997_haptic_id
,
402 module_platform_driver(max8997_haptic_driver
);
404 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
405 MODULE_DESCRIPTION("max8997_haptic driver");
406 MODULE_LICENSE("GPL");