2 * MAX8997-haptic controller driver
4 * Copyright (C) 2012 Samsung Electronics
5 * Donggeun Kim <dg77.kim@samsung.com>
7 * This program is not provided / owned by Maxim Integrated Products.
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.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/platform_device.h>
28 #include <linux/err.h>
29 #include <linux/pwm.h>
30 #include <linux/input.h>
31 #include <linux/mfd/max8997-private.h>
32 #include <linux/mfd/max8997.h>
33 #include <linux/regulator/consumer.h>
35 /* Haptic configuration 2 register */
36 #define MAX8997_MOTOR_TYPE_SHIFT 7
37 #define MAX8997_ENABLE_SHIFT 6
38 #define MAX8997_MODE_SHIFT 5
40 /* Haptic driver configuration register */
41 #define MAX8997_CYCLE_SHIFT 6
42 #define MAX8997_SIG_PERIOD_SHIFT 4
43 #define MAX8997_SIG_DUTY_SHIFT 2
44 #define MAX8997_PWM_DUTY_SHIFT 0
46 struct max8997_haptic
{
48 struct i2c_client
*client
;
49 struct input_dev
*input_dev
;
50 struct regulator
*regulator
;
52 struct work_struct work
;
58 struct pwm_device
*pwm
;
59 unsigned int pwm_period
;
60 enum max8997_haptic_pwm_divisor pwm_divisor
;
62 enum max8997_haptic_motor_type type
;
63 enum max8997_haptic_pulse_mode mode
;
65 unsigned int internal_mode_pattern
;
66 unsigned int pattern_cycle
;
67 unsigned int pattern_signal_period
;
70 static int max8997_haptic_set_duty_cycle(struct max8997_haptic
*chip
)
74 if (chip
->mode
== MAX8997_EXTERNAL_MODE
) {
75 unsigned int duty
= chip
->pwm_period
* chip
->level
/ 100;
76 ret
= pwm_config(chip
->pwm
, duty
, chip
->pwm_period
);
81 for (i
= 0; i
<= 64; i
++) {
82 if (chip
->level
<= i
* 100 / 64) {
87 switch (chip
->internal_mode_pattern
) {
89 max8997_write_reg(chip
->client
,
90 MAX8997_HAPTIC_REG_SIGPWMDC1
, duty_index
);
93 max8997_write_reg(chip
->client
,
94 MAX8997_HAPTIC_REG_SIGPWMDC2
, duty_index
);
97 max8997_write_reg(chip
->client
,
98 MAX8997_HAPTIC_REG_SIGPWMDC3
, duty_index
);
101 max8997_write_reg(chip
->client
,
102 MAX8997_HAPTIC_REG_SIGPWMDC4
, duty_index
);
111 static void max8997_haptic_configure(struct max8997_haptic
*chip
)
115 value
= chip
->type
<< MAX8997_MOTOR_TYPE_SHIFT
|
116 chip
->enabled
<< MAX8997_ENABLE_SHIFT
|
117 chip
->mode
<< MAX8997_MODE_SHIFT
| chip
->pwm_divisor
;
118 max8997_write_reg(chip
->client
, MAX8997_HAPTIC_REG_CONF2
, value
);
120 if (chip
->mode
== MAX8997_INTERNAL_MODE
&& chip
->enabled
) {
121 value
= chip
->internal_mode_pattern
<< MAX8997_CYCLE_SHIFT
|
122 chip
->internal_mode_pattern
<< MAX8997_SIG_PERIOD_SHIFT
|
123 chip
->internal_mode_pattern
<< MAX8997_SIG_DUTY_SHIFT
|
124 chip
->internal_mode_pattern
<< MAX8997_PWM_DUTY_SHIFT
;
125 max8997_write_reg(chip
->client
,
126 MAX8997_HAPTIC_REG_DRVCONF
, value
);
128 switch (chip
->internal_mode_pattern
) {
130 value
= chip
->pattern_cycle
<< 4;
131 max8997_write_reg(chip
->client
,
132 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
133 value
= chip
->pattern_signal_period
;
134 max8997_write_reg(chip
->client
,
135 MAX8997_HAPTIC_REG_SIGCONF1
, value
);
139 value
= chip
->pattern_cycle
;
140 max8997_write_reg(chip
->client
,
141 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
142 value
= chip
->pattern_signal_period
;
143 max8997_write_reg(chip
->client
,
144 MAX8997_HAPTIC_REG_SIGCONF2
, value
);
148 value
= chip
->pattern_cycle
<< 4;
149 max8997_write_reg(chip
->client
,
150 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
151 value
= chip
->pattern_signal_period
;
152 max8997_write_reg(chip
->client
,
153 MAX8997_HAPTIC_REG_SIGCONF3
, value
);
157 value
= chip
->pattern_cycle
;
158 max8997_write_reg(chip
->client
,
159 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
160 value
= chip
->pattern_signal_period
;
161 max8997_write_reg(chip
->client
,
162 MAX8997_HAPTIC_REG_SIGCONF4
, value
);
171 static void max8997_haptic_enable(struct max8997_haptic
*chip
)
175 mutex_lock(&chip
->mutex
);
177 error
= max8997_haptic_set_duty_cycle(chip
);
179 dev_err(chip
->dev
, "set_pwm_cycle failed, error: %d\n", error
);
183 if (!chip
->enabled
) {
184 chip
->enabled
= true;
185 regulator_enable(chip
->regulator
);
186 max8997_haptic_configure(chip
);
187 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
188 pwm_enable(chip
->pwm
);
192 mutex_unlock(&chip
->mutex
);
195 static void max8997_haptic_disable(struct max8997_haptic
*chip
)
197 mutex_lock(&chip
->mutex
);
200 chip
->enabled
= false;
201 max8997_haptic_configure(chip
);
202 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
203 pwm_disable(chip
->pwm
);
204 regulator_disable(chip
->regulator
);
207 mutex_unlock(&chip
->mutex
);
210 static void max8997_haptic_play_effect_work(struct work_struct
*work
)
212 struct max8997_haptic
*chip
=
213 container_of(work
, struct max8997_haptic
, work
);
216 max8997_haptic_enable(chip
);
218 max8997_haptic_disable(chip
);
221 static int max8997_haptic_play_effect(struct input_dev
*dev
, void *data
,
222 struct ff_effect
*effect
)
224 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
226 chip
->level
= effect
->u
.rumble
.strong_magnitude
;
228 chip
->level
= effect
->u
.rumble
.weak_magnitude
;
230 schedule_work(&chip
->work
);
235 static void max8997_haptic_close(struct input_dev
*dev
)
237 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
239 cancel_work_sync(&chip
->work
);
240 max8997_haptic_disable(chip
);
243 static int max8997_haptic_probe(struct platform_device
*pdev
)
245 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
246 const struct max8997_platform_data
*pdata
=
247 dev_get_platdata(iodev
->dev
);
248 const struct max8997_haptic_platform_data
*haptic_pdata
=
250 struct max8997_haptic
*chip
;
251 struct input_dev
*input_dev
;
255 dev_err(&pdev
->dev
, "no haptic platform data\n");
259 chip
= kzalloc(sizeof(struct max8997_haptic
), GFP_KERNEL
);
260 input_dev
= input_allocate_device();
261 if (!chip
|| !input_dev
) {
262 dev_err(&pdev
->dev
, "unable to allocate memory\n");
267 INIT_WORK(&chip
->work
, max8997_haptic_play_effect_work
);
268 mutex_init(&chip
->mutex
);
270 chip
->client
= iodev
->haptic
;
271 chip
->dev
= &pdev
->dev
;
272 chip
->input_dev
= input_dev
;
273 chip
->pwm_period
= haptic_pdata
->pwm_period
;
274 chip
->type
= haptic_pdata
->type
;
275 chip
->mode
= haptic_pdata
->mode
;
276 chip
->pwm_divisor
= haptic_pdata
->pwm_divisor
;
278 switch (chip
->mode
) {
279 case MAX8997_INTERNAL_MODE
:
280 chip
->internal_mode_pattern
=
281 haptic_pdata
->internal_mode_pattern
;
282 chip
->pattern_cycle
= haptic_pdata
->pattern_cycle
;
283 chip
->pattern_signal_period
=
284 haptic_pdata
->pattern_signal_period
;
287 case MAX8997_EXTERNAL_MODE
:
288 chip
->pwm
= pwm_request(haptic_pdata
->pwm_channel_id
,
290 if (IS_ERR(chip
->pwm
)) {
291 error
= PTR_ERR(chip
->pwm
);
293 "unable to request PWM for haptic, error: %d\n",
301 "Invalid chip mode specified (%d)\n", chip
->mode
);
306 chip
->regulator
= regulator_get(&pdev
->dev
, "inmotor");
307 if (IS_ERR(chip
->regulator
)) {
308 error
= PTR_ERR(chip
->regulator
);
310 "unable to get regulator, error: %d\n",
315 input_dev
->name
= "max8997-haptic";
316 input_dev
->id
.version
= 1;
317 input_dev
->dev
.parent
= &pdev
->dev
;
318 input_dev
->close
= max8997_haptic_close
;
319 input_set_drvdata(input_dev
, chip
);
320 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
322 error
= input_ff_create_memless(input_dev
, NULL
,
323 max8997_haptic_play_effect
);
326 "unable to create FF device, error: %d\n",
328 goto err_put_regulator
;
331 error
= input_register_device(input_dev
);
334 "unable to register input device, error: %d\n",
339 platform_set_drvdata(pdev
, chip
);
343 input_ff_destroy(input_dev
);
345 regulator_put(chip
->regulator
);
347 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
350 input_free_device(input_dev
);
356 static int max8997_haptic_remove(struct platform_device
*pdev
)
358 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
360 input_unregister_device(chip
->input_dev
);
361 regulator_put(chip
->regulator
);
363 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
371 #ifdef CONFIG_PM_SLEEP
372 static int max8997_haptic_suspend(struct device
*dev
)
374 struct platform_device
*pdev
= to_platform_device(dev
);
375 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
377 max8997_haptic_disable(chip
);
383 static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops
, max8997_haptic_suspend
, NULL
);
385 static const struct platform_device_id max8997_haptic_id
[] = {
386 { "max8997-haptic", 0 },
389 MODULE_DEVICE_TABLE(i2c
, max8997_haptic_id
);
391 static struct platform_driver max8997_haptic_driver
= {
393 .name
= "max8997-haptic",
394 .owner
= THIS_MODULE
,
395 .pm
= &max8997_haptic_pm_ops
,
397 .probe
= max8997_haptic_probe
,
398 .remove
= max8997_haptic_remove
,
399 .id_table
= max8997_haptic_id
,
401 module_platform_driver(max8997_haptic_driver
);
403 MODULE_ALIAS("platform:max8997-haptic");
404 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
405 MODULE_DESCRIPTION("max8997_haptic driver");
406 MODULE_LICENSE("GPL");