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/init.h>
27 #include <linux/slab.h>
28 #include <linux/platform_device.h>
29 #include <linux/err.h>
30 #include <linux/pwm.h>
31 #include <linux/input.h>
32 #include <linux/mfd/max8997-private.h>
33 #include <linux/mfd/max8997.h>
34 #include <linux/regulator/consumer.h>
36 /* Haptic configuration 2 register */
37 #define MAX8997_MOTOR_TYPE_SHIFT 7
38 #define MAX8997_ENABLE_SHIFT 6
39 #define MAX8997_MODE_SHIFT 5
41 /* Haptic driver configuration register */
42 #define MAX8997_CYCLE_SHIFT 6
43 #define MAX8997_SIG_PERIOD_SHIFT 4
44 #define MAX8997_SIG_DUTY_SHIFT 2
45 #define MAX8997_PWM_DUTY_SHIFT 0
47 struct max8997_haptic
{
49 struct i2c_client
*client
;
50 struct input_dev
*input_dev
;
51 struct regulator
*regulator
;
53 struct work_struct work
;
59 struct pwm_device
*pwm
;
60 unsigned int pwm_period
;
61 enum max8997_haptic_pwm_divisor pwm_divisor
;
63 enum max8997_haptic_motor_type type
;
64 enum max8997_haptic_pulse_mode mode
;
66 unsigned int internal_mode_pattern
;
67 unsigned int pattern_cycle
;
68 unsigned int pattern_signal_period
;
71 static int max8997_haptic_set_duty_cycle(struct max8997_haptic
*chip
)
75 if (chip
->mode
== MAX8997_EXTERNAL_MODE
) {
76 unsigned int duty
= chip
->pwm_period
* chip
->level
/ 100;
77 ret
= pwm_config(chip
->pwm
, duty
, chip
->pwm_period
);
82 for (i
= 0; i
<= 64; i
++) {
83 if (chip
->level
<= i
* 100 / 64) {
88 switch (chip
->internal_mode_pattern
) {
90 max8997_write_reg(chip
->client
,
91 MAX8997_HAPTIC_REG_SIGPWMDC1
, duty_index
);
94 max8997_write_reg(chip
->client
,
95 MAX8997_HAPTIC_REG_SIGPWMDC2
, duty_index
);
98 max8997_write_reg(chip
->client
,
99 MAX8997_HAPTIC_REG_SIGPWMDC3
, duty_index
);
102 max8997_write_reg(chip
->client
,
103 MAX8997_HAPTIC_REG_SIGPWMDC4
, duty_index
);
112 static void max8997_haptic_configure(struct max8997_haptic
*chip
)
116 value
= chip
->type
<< MAX8997_MOTOR_TYPE_SHIFT
|
117 chip
->enabled
<< MAX8997_ENABLE_SHIFT
|
118 chip
->mode
<< MAX8997_MODE_SHIFT
| chip
->pwm_divisor
;
119 max8997_write_reg(chip
->client
, MAX8997_HAPTIC_REG_CONF2
, value
);
121 if (chip
->mode
== MAX8997_INTERNAL_MODE
&& chip
->enabled
) {
122 value
= chip
->internal_mode_pattern
<< MAX8997_CYCLE_SHIFT
|
123 chip
->internal_mode_pattern
<< MAX8997_SIG_PERIOD_SHIFT
|
124 chip
->internal_mode_pattern
<< MAX8997_SIG_DUTY_SHIFT
|
125 chip
->internal_mode_pattern
<< MAX8997_PWM_DUTY_SHIFT
;
126 max8997_write_reg(chip
->client
,
127 MAX8997_HAPTIC_REG_DRVCONF
, value
);
129 switch (chip
->internal_mode_pattern
) {
131 value
= chip
->pattern_cycle
<< 4;
132 max8997_write_reg(chip
->client
,
133 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
134 value
= chip
->pattern_signal_period
;
135 max8997_write_reg(chip
->client
,
136 MAX8997_HAPTIC_REG_SIGCONF1
, value
);
140 value
= chip
->pattern_cycle
;
141 max8997_write_reg(chip
->client
,
142 MAX8997_HAPTIC_REG_CYCLECONF1
, value
);
143 value
= chip
->pattern_signal_period
;
144 max8997_write_reg(chip
->client
,
145 MAX8997_HAPTIC_REG_SIGCONF2
, value
);
149 value
= chip
->pattern_cycle
<< 4;
150 max8997_write_reg(chip
->client
,
151 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
152 value
= chip
->pattern_signal_period
;
153 max8997_write_reg(chip
->client
,
154 MAX8997_HAPTIC_REG_SIGCONF3
, value
);
158 value
= chip
->pattern_cycle
;
159 max8997_write_reg(chip
->client
,
160 MAX8997_HAPTIC_REG_CYCLECONF2
, value
);
161 value
= chip
->pattern_signal_period
;
162 max8997_write_reg(chip
->client
,
163 MAX8997_HAPTIC_REG_SIGCONF4
, value
);
172 static void max8997_haptic_enable(struct max8997_haptic
*chip
)
176 mutex_lock(&chip
->mutex
);
178 error
= max8997_haptic_set_duty_cycle(chip
);
180 dev_err(chip
->dev
, "set_pwm_cycle failed, error: %d\n", error
);
184 if (!chip
->enabled
) {
185 chip
->enabled
= true;
186 regulator_enable(chip
->regulator
);
187 max8997_haptic_configure(chip
);
188 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
189 pwm_enable(chip
->pwm
);
193 mutex_unlock(&chip
->mutex
);
196 static void max8997_haptic_disable(struct max8997_haptic
*chip
)
198 mutex_lock(&chip
->mutex
);
201 chip
->enabled
= false;
202 max8997_haptic_configure(chip
);
203 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
204 pwm_disable(chip
->pwm
);
205 regulator_disable(chip
->regulator
);
208 mutex_unlock(&chip
->mutex
);
211 static void max8997_haptic_play_effect_work(struct work_struct
*work
)
213 struct max8997_haptic
*chip
=
214 container_of(work
, struct max8997_haptic
, work
);
217 max8997_haptic_enable(chip
);
219 max8997_haptic_disable(chip
);
222 static int max8997_haptic_play_effect(struct input_dev
*dev
, void *data
,
223 struct ff_effect
*effect
)
225 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
227 chip
->level
= effect
->u
.rumble
.strong_magnitude
;
229 chip
->level
= effect
->u
.rumble
.weak_magnitude
;
231 schedule_work(&chip
->work
);
236 static void max8997_haptic_close(struct input_dev
*dev
)
238 struct max8997_haptic
*chip
= input_get_drvdata(dev
);
240 cancel_work_sync(&chip
->work
);
241 max8997_haptic_disable(chip
);
244 static int __devinit
max8997_haptic_probe(struct platform_device
*pdev
)
246 struct max8997_dev
*iodev
= dev_get_drvdata(pdev
->dev
.parent
);
247 const struct max8997_platform_data
*pdata
=
248 dev_get_platdata(iodev
->dev
);
249 const struct max8997_haptic_platform_data
*haptic_pdata
=
251 struct max8997_haptic
*chip
;
252 struct input_dev
*input_dev
;
256 dev_err(&pdev
->dev
, "no haptic platform data\n");
260 chip
= kzalloc(sizeof(struct max8997_haptic
), GFP_KERNEL
);
261 input_dev
= input_allocate_device();
262 if (!chip
|| !input_dev
) {
263 dev_err(&pdev
->dev
, "unable to allocate memory\n");
268 INIT_WORK(&chip
->work
, max8997_haptic_play_effect_work
);
269 mutex_init(&chip
->mutex
);
271 chip
->client
= iodev
->haptic
;
272 chip
->dev
= &pdev
->dev
;
273 chip
->input_dev
= input_dev
;
274 chip
->pwm_period
= haptic_pdata
->pwm_period
;
275 chip
->type
= haptic_pdata
->type
;
276 chip
->mode
= haptic_pdata
->mode
;
277 chip
->pwm_divisor
= haptic_pdata
->pwm_divisor
;
279 switch (chip
->mode
) {
280 case MAX8997_INTERNAL_MODE
:
281 chip
->internal_mode_pattern
=
282 haptic_pdata
->internal_mode_pattern
;
283 chip
->pattern_cycle
= haptic_pdata
->pattern_cycle
;
284 chip
->pattern_signal_period
=
285 haptic_pdata
->pattern_signal_period
;
288 case MAX8997_EXTERNAL_MODE
:
289 chip
->pwm
= pwm_request(haptic_pdata
->pwm_channel_id
,
291 if (IS_ERR(chip
->pwm
)) {
292 error
= PTR_ERR(chip
->pwm
);
294 "unable to request PWM for haptic, error: %d\n",
302 "Invalid chip mode specified (%d)\n", chip
->mode
);
307 chip
->regulator
= regulator_get(&pdev
->dev
, "inmotor");
308 if (IS_ERR(chip
->regulator
)) {
309 error
= PTR_ERR(chip
->regulator
);
311 "unable to get regulator, error: %d\n",
316 input_dev
->name
= "max8997-haptic";
317 input_dev
->id
.version
= 1;
318 input_dev
->dev
.parent
= &pdev
->dev
;
319 input_dev
->close
= max8997_haptic_close
;
320 input_set_drvdata(input_dev
, chip
);
321 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
323 error
= input_ff_create_memless(input_dev
, NULL
,
324 max8997_haptic_play_effect
);
327 "unable to create FF device, error: %d\n",
329 goto err_put_regulator
;
332 error
= input_register_device(input_dev
);
335 "unable to register input device, error: %d\n",
340 platform_set_drvdata(pdev
, chip
);
344 input_ff_destroy(input_dev
);
346 regulator_put(chip
->regulator
);
348 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
351 input_free_device(input_dev
);
357 static int __devexit
max8997_haptic_remove(struct platform_device
*pdev
)
359 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
361 input_unregister_device(chip
->input_dev
);
362 regulator_put(chip
->regulator
);
364 if (chip
->mode
== MAX8997_EXTERNAL_MODE
)
372 #ifdef CONFIG_PM_SLEEP
373 static int max8997_haptic_suspend(struct device
*dev
)
375 struct platform_device
*pdev
= to_platform_device(dev
);
376 struct max8997_haptic
*chip
= platform_get_drvdata(pdev
);
378 max8997_haptic_disable(chip
);
384 static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops
, max8997_haptic_suspend
, NULL
);
386 static const struct platform_device_id max8997_haptic_id
[] = {
387 { "max8997-haptic", 0 },
390 MODULE_DEVICE_TABLE(i2c
, max8997_haptic_id
);
392 static struct platform_driver max8997_haptic_driver
= {
394 .name
= "max8997-haptic",
395 .owner
= THIS_MODULE
,
396 .pm
= &max8997_haptic_pm_ops
,
398 .probe
= max8997_haptic_probe
,
399 .remove
= __devexit_p(max8997_haptic_remove
),
400 .id_table
= max8997_haptic_id
,
402 module_platform_driver(max8997_haptic_driver
);
404 MODULE_ALIAS("platform:max8997-haptic");
405 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
406 MODULE_DESCRIPTION("max8997_haptic driver");
407 MODULE_LICENSE("GPL");