Merge branch 'akpm' (patches from Andrew)
[linux-2.6/btrfs-unstable.git] / drivers / input / misc / max8997_haptic.c
bloba806ba3818f7267dd2036dd870c4f60ca28dc0d5
1 /*
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 {
47 struct device *dev;
48 struct i2c_client *client;
49 struct input_dev *input_dev;
50 struct regulator *regulator;
52 struct work_struct work;
53 struct mutex mutex;
55 bool enabled;
56 unsigned int level;
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)
72 int ret = 0;
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);
77 } else {
78 int i;
79 u8 duty_index = 0;
81 for (i = 0; i <= 64; i++) {
82 if (chip->level <= i * 100 / 64) {
83 duty_index = i;
84 break;
87 switch (chip->internal_mode_pattern) {
88 case 0:
89 max8997_write_reg(chip->client,
90 MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
91 break;
92 case 1:
93 max8997_write_reg(chip->client,
94 MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
95 break;
96 case 2:
97 max8997_write_reg(chip->client,
98 MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
99 break;
100 case 3:
101 max8997_write_reg(chip->client,
102 MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
103 break;
104 default:
105 break;
108 return ret;
111 static void max8997_haptic_configure(struct max8997_haptic *chip)
113 u8 value;
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) {
129 case 0:
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);
136 break;
138 case 1:
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);
145 break;
147 case 2:
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);
154 break;
156 case 3:
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);
163 break;
165 default:
166 break;
171 static void max8997_haptic_enable(struct max8997_haptic *chip)
173 int error;
175 mutex_lock(&chip->mutex);
177 error = max8997_haptic_set_duty_cycle(chip);
178 if (error) {
179 dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
180 goto out;
183 if (!chip->enabled) {
184 error = regulator_enable(chip->regulator);
185 if (error) {
186 dev_err(chip->dev, "Failed to enable regulator\n");
187 goto out;
189 max8997_haptic_configure(chip);
190 if (chip->mode == MAX8997_EXTERNAL_MODE) {
191 error = pwm_enable(chip->pwm);
192 if (error) {
193 dev_err(chip->dev, "Failed to enable PWM\n");
194 regulator_disable(chip->regulator);
195 goto out;
198 chip->enabled = true;
201 out:
202 mutex_unlock(&chip->mutex);
205 static void max8997_haptic_disable(struct max8997_haptic *chip)
207 mutex_lock(&chip->mutex);
209 if (chip->enabled) {
210 chip->enabled = false;
211 max8997_haptic_configure(chip);
212 if (chip->mode == MAX8997_EXTERNAL_MODE)
213 pwm_disable(chip->pwm);
214 regulator_disable(chip->regulator);
217 mutex_unlock(&chip->mutex);
220 static void max8997_haptic_play_effect_work(struct work_struct *work)
222 struct max8997_haptic *chip =
223 container_of(work, struct max8997_haptic, work);
225 if (chip->level)
226 max8997_haptic_enable(chip);
227 else
228 max8997_haptic_disable(chip);
231 static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
232 struct ff_effect *effect)
234 struct max8997_haptic *chip = input_get_drvdata(dev);
236 chip->level = effect->u.rumble.strong_magnitude;
237 if (!chip->level)
238 chip->level = effect->u.rumble.weak_magnitude;
240 schedule_work(&chip->work);
242 return 0;
245 static void max8997_haptic_close(struct input_dev *dev)
247 struct max8997_haptic *chip = input_get_drvdata(dev);
249 cancel_work_sync(&chip->work);
250 max8997_haptic_disable(chip);
253 static int max8997_haptic_probe(struct platform_device *pdev)
255 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
256 const struct max8997_platform_data *pdata =
257 dev_get_platdata(iodev->dev);
258 const struct max8997_haptic_platform_data *haptic_pdata =
259 pdata->haptic_pdata;
260 struct max8997_haptic *chip;
261 struct input_dev *input_dev;
262 int error;
264 if (!haptic_pdata) {
265 dev_err(&pdev->dev, "no haptic platform data\n");
266 return -EINVAL;
269 chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
270 input_dev = input_allocate_device();
271 if (!chip || !input_dev) {
272 dev_err(&pdev->dev, "unable to allocate memory\n");
273 error = -ENOMEM;
274 goto err_free_mem;
277 INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
278 mutex_init(&chip->mutex);
280 chip->client = iodev->haptic;
281 chip->dev = &pdev->dev;
282 chip->input_dev = input_dev;
283 chip->pwm_period = haptic_pdata->pwm_period;
284 chip->type = haptic_pdata->type;
285 chip->mode = haptic_pdata->mode;
286 chip->pwm_divisor = haptic_pdata->pwm_divisor;
288 switch (chip->mode) {
289 case MAX8997_INTERNAL_MODE:
290 chip->internal_mode_pattern =
291 haptic_pdata->internal_mode_pattern;
292 chip->pattern_cycle = haptic_pdata->pattern_cycle;
293 chip->pattern_signal_period =
294 haptic_pdata->pattern_signal_period;
295 break;
297 case MAX8997_EXTERNAL_MODE:
298 chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
299 "max8997-haptic");
300 if (IS_ERR(chip->pwm)) {
301 error = PTR_ERR(chip->pwm);
302 dev_err(&pdev->dev,
303 "unable to request PWM for haptic, error: %d\n",
304 error);
305 goto err_free_mem;
307 break;
309 default:
310 dev_err(&pdev->dev,
311 "Invalid chip mode specified (%d)\n", chip->mode);
312 error = -EINVAL;
313 goto err_free_mem;
316 chip->regulator = regulator_get(&pdev->dev, "inmotor");
317 if (IS_ERR(chip->regulator)) {
318 error = PTR_ERR(chip->regulator);
319 dev_err(&pdev->dev,
320 "unable to get regulator, error: %d\n",
321 error);
322 goto err_free_pwm;
325 input_dev->name = "max8997-haptic";
326 input_dev->id.version = 1;
327 input_dev->dev.parent = &pdev->dev;
328 input_dev->close = max8997_haptic_close;
329 input_set_drvdata(input_dev, chip);
330 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
332 error = input_ff_create_memless(input_dev, NULL,
333 max8997_haptic_play_effect);
334 if (error) {
335 dev_err(&pdev->dev,
336 "unable to create FF device, error: %d\n",
337 error);
338 goto err_put_regulator;
341 error = input_register_device(input_dev);
342 if (error) {
343 dev_err(&pdev->dev,
344 "unable to register input device, error: %d\n",
345 error);
346 goto err_destroy_ff;
349 platform_set_drvdata(pdev, chip);
350 return 0;
352 err_destroy_ff:
353 input_ff_destroy(input_dev);
354 err_put_regulator:
355 regulator_put(chip->regulator);
356 err_free_pwm:
357 if (chip->mode == MAX8997_EXTERNAL_MODE)
358 pwm_free(chip->pwm);
359 err_free_mem:
360 input_free_device(input_dev);
361 kfree(chip);
363 return error;
366 static int max8997_haptic_remove(struct platform_device *pdev)
368 struct max8997_haptic *chip = platform_get_drvdata(pdev);
370 input_unregister_device(chip->input_dev);
371 regulator_put(chip->regulator);
373 if (chip->mode == MAX8997_EXTERNAL_MODE)
374 pwm_free(chip->pwm);
376 kfree(chip);
378 return 0;
381 static int __maybe_unused max8997_haptic_suspend(struct device *dev)
383 struct platform_device *pdev = to_platform_device(dev);
384 struct max8997_haptic *chip = platform_get_drvdata(pdev);
386 max8997_haptic_disable(chip);
388 return 0;
391 static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
393 static const struct platform_device_id max8997_haptic_id[] = {
394 { "max8997-haptic", 0 },
395 { },
397 MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
399 static struct platform_driver max8997_haptic_driver = {
400 .driver = {
401 .name = "max8997-haptic",
402 .pm = &max8997_haptic_pm_ops,
404 .probe = max8997_haptic_probe,
405 .remove = max8997_haptic_remove,
406 .id_table = max8997_haptic_id,
408 module_platform_driver(max8997_haptic_driver);
410 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
411 MODULE_DESCRIPTION("max8997_haptic driver");
412 MODULE_LICENSE("GPL");