haptic: max77665: remove old system EDP framework
[linux-3.10.git] / drivers / input / misc / max77665_haptic.c
1 /*
2  * MAX77665-haptic controller driver
3  *
4  * Copyright (c) 2012-2013, NVIDIA Corporation, All Rights Reserved.
5  *
6  * Based on driver max8997_haptic.c
7  * Copyright (c) 2012 Samsung Electronics
8  *
9  * This program is not provided / owned by Maxim Integrated Products.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/platform_device.h>
30 #include <linux/err.h>
31 #include <linux/pwm.h>
32 #include <linux/input.h>
33 #include <linux/mfd/max77665.h>
34 #include <linux/input/max77665-haptic.h>
35 #include <linux/regulator/consumer.h>
36
37 struct max77665_haptic {
38         struct device *dev;
39         struct i2c_client *client;
40         struct input_dev *input_dev;
41         struct regulator *regulator;
42         struct work_struct work;
43
44         bool enabled;
45
46         unsigned int level;
47
48         struct pwm_device *pwm;
49         int pwm_period;
50         enum max77665_haptic_pwm_divisor pwm_divisor;
51
52         enum max77665_haptic_motor_type type;
53         enum max77665_haptic_pulse_mode mode;
54         enum max77665_haptic_invert invert;
55         enum max77665_haptic_continous_mode cont_mode;
56
57         int internal_mode_pattern;
58         int pattern_cycle;
59         int pattern_signal_period;
60         int feedback_duty_cycle;
61         int motor_startup_val;
62         int scf_val;
63 };
64
65 static int max77665_haptic_set_duty_cycle(struct max77665_haptic *chip)
66 {
67         int duty, i;
68         u8 duty_index = 0;
69         int ret = 0;
70         int reg1, reg2;
71         bool internal_mode_valid = true;
72
73         if (chip->mode == MAX77665_EXTERNAL_MODE) {
74                 duty = chip->pwm_period * chip->level / 100;
75                 ret = pwm_config(chip->pwm, duty, chip->pwm_period);
76         } else {
77                 for (i = 0; i < 64; i++) {
78                         if (chip->level <= (i + 1) * 100 / 64) {
79                                 duty_index = i;
80                                 break;
81                         }
82                 }
83                 switch (chip->internal_mode_pattern) {
84                 case 0:
85                         reg1 = MAX77665_HAPTIC_REG_SIGDC1;
86                         reg2 = MAX77665_HAPTIC_REG_SIGPWMDC1;
87                         break;
88                 case 1:
89                         reg1 = MAX77665_HAPTIC_REG_SIGDC1;
90                         reg2 = MAX77665_HAPTIC_REG_SIGPWMDC2;
91                         break;
92                 case 2:
93                         reg1 = MAX77665_HAPTIC_REG_SIGDC2;
94                         reg2 = MAX77665_HAPTIC_REG_SIGPWMDC3;
95                         break;
96                 case 3:
97                         reg1 = MAX77665_HAPTIC_REG_SIGDC1;
98                         reg2 = MAX77665_HAPTIC_REG_SIGPWMDC4;
99                         break;
100                 default:
101                         internal_mode_valid = false;
102                         break;
103                 }
104
105                 if (internal_mode_valid) {
106                         if (chip->internal_mode_pattern % 2 == 1)
107                                 max77665_write(chip->dev->parent,
108                                         MAX77665_I2C_SLAVE_HAPTIC,
109                                         reg1, chip->feedback_duty_cycle);
110                         else
111                                 max77665_write(chip->dev->parent,
112                                         MAX77665_I2C_SLAVE_HAPTIC,
113                                         reg1, chip->feedback_duty_cycle << 4);
114
115                         max77665_write(chip->dev->parent,
116                                 MAX77665_I2C_SLAVE_HAPTIC,
117                                 reg2, duty_index);
118                 }
119         }
120         return ret;
121 }
122
123 static void max77665_haptic_configure(struct max77665_haptic *chip)
124 {
125         u8 value1, value2, reg1, reg2;
126         bool internal_mode_valid = true;
127
128         value1 = chip->type << MAX77665_MOTOR_TYPE_SHIFT |
129                 chip->enabled << MAX77665_ENABLE_SHIFT |
130                 chip->mode << MAX77665_MODE_SHIFT | chip->pwm_divisor;
131         max77665_write(chip->dev->parent, MAX77665_I2C_SLAVE_HAPTIC,
132                 MAX77665_HAPTIC_REG_CONF2, value1);
133
134         if (chip->mode == MAX77665_INTERNAL_MODE && chip->enabled) {
135                 max77665_write(chip->dev->parent, MAX77665_I2C_SLAVE_PMIC,
136                                         MAX77665_PMIC_REG_LSCNFG, 0xAA);
137                 value1 = chip->invert << MAX77665_INVERT_SHIFT |
138                         chip->cont_mode << MAX77665_CONT_MODE_SHIFT |
139                         chip->motor_startup_val << MAX77665_MOTOR_STRT_SHIFT |
140                         chip->scf_val;
141
142                 max77665_write(chip->dev->parent, MAX77665_I2C_SLAVE_HAPTIC,
143                         MAX77665_HAPTIC_REG_CONF1, value1);
144
145                 switch (chip->internal_mode_pattern) {
146                 case 0:
147                         value1 = chip->pattern_cycle << 4;
148                         reg1 = MAX77665_HAPTIC_REG_CYCLECONF1;
149                         value2 = chip->pattern_signal_period;
150                         reg2 = MAX77665_HAPTIC_REG_SIGCONF1;
151                         break;
152                 case 1:
153                         value1 = chip->pattern_cycle;
154                         reg1 = MAX77665_HAPTIC_REG_CYCLECONF1;
155                         value2 = chip->pattern_signal_period;
156                         reg2 = MAX77665_HAPTIC_REG_SIGCONF2;
157                         break;
158                 case 2:
159                         value1 = chip->pattern_cycle << 4;
160                         reg1 = MAX77665_HAPTIC_REG_CYCLECONF2;
161                         value2 = chip->pattern_signal_period;
162                         reg2 = MAX77665_HAPTIC_REG_SIGCONF3;
163                         break;
164                 case 3:
165                         value1 = chip->pattern_cycle;
166                         reg1 = MAX77665_HAPTIC_REG_CYCLECONF2;
167                         value2 = chip->pattern_signal_period;
168                         reg2 = MAX77665_HAPTIC_REG_SIGCONF4;
169                         break;
170                 default:
171                         internal_mode_valid = false;
172                         break;
173                 }
174
175                 if (internal_mode_valid) {
176                         max77665_write(chip->dev->parent,
177                                 MAX77665_I2C_SLAVE_HAPTIC,
178                                 reg1, value1);
179                         max77665_write(chip->dev->parent,
180                                 MAX77665_I2C_SLAVE_HAPTIC,
181                                 reg2, value2);
182                         value1 = chip->internal_mode_pattern
183                                         << MAX77665_CYCLE_SHIFT |
184                               chip->internal_mode_pattern
185                                         << MAX77665_SIG_PERIOD_SHIFT |
186                               chip->internal_mode_pattern
187                                         << MAX77665_SIG_DUTY_SHIFT |
188                               chip->internal_mode_pattern
189                                         << MAX77665_PWM_DUTY_SHIFT;
190                         max77665_write(chip->dev->parent,
191                                 MAX77665_I2C_SLAVE_HAPTIC,
192                                 MAX77665_HAPTIC_REG_DRVCONF, value1);
193                 }
194         }
195 }
196
197 static void max77665_haptic_enable(struct max77665_haptic *chip, bool enable)
198 {
199         if (chip->enabled == enable)
200                 return;
201
202         chip->enabled = enable;
203
204         if (enable) {
205                 regulator_enable(chip->regulator);
206                 max77665_haptic_configure(chip);
207                 if (chip->mode == MAX77665_EXTERNAL_MODE)
208                         pwm_enable(chip->pwm);
209         } else {
210                 max77665_haptic_configure(chip);
211                 if (chip->mode == MAX77665_EXTERNAL_MODE)
212                         pwm_disable(chip->pwm);
213                 regulator_disable(chip->regulator);
214         }
215 }
216
217 static void max77665_haptic_play_effect_work(struct work_struct *work)
218 {
219         struct max77665_haptic *chip =
220                 container_of(work, struct max77665_haptic, work);
221         int ret;
222
223         if (chip->level) {
224                 ret = max77665_haptic_set_duty_cycle(chip);
225                 if (ret) {
226                         dev_err(chip->dev, "set_pwm_cycle failed\n");
227                         return;
228                 }
229
230                 max77665_haptic_enable(chip, true);
231         } else {
232                 max77665_haptic_enable(chip, false);
233         }
234 }
235
236
237 static int max77665_haptic_play_effect(struct input_dev *dev, void *data,
238                                   struct ff_effect *effect)
239 {
240         struct max77665_haptic *chip = input_get_drvdata(dev);
241
242         chip->level = effect->u.rumble.strong_magnitude;
243         if (!chip->level)
244                 chip->level = effect->u.rumble.weak_magnitude;
245
246         schedule_work(&chip->work);
247
248         return 0;
249 }
250
251 static ssize_t max77665_haptic_show(struct device *dev,
252                         struct device_attribute *attr, char *buf)
253 {
254         struct max77665_haptic *chip = dev_get_drvdata(dev);
255         int var = 0;
256
257         if (strcmp(attr->attr.name, "pattern_cycle") == 0)
258                 var = chip->pattern_cycle;
259         else if (strcmp(attr->attr.name, "pattern_signal_period") == 0)
260                 var = chip->pattern_signal_period;
261         else if (strcmp(attr->attr.name, "feedback_duty_cycle") == 0)
262                 var = chip->feedback_duty_cycle;
263         else if (strcmp(attr->attr.name, "scf_val") == 0)
264                 var = chip->scf_val;
265         else if (strcmp(attr->attr.name, "pwm_divisor") == 0)
266                 var = chip->pwm_divisor;
267
268         return sprintf(buf, "%d\n", var);
269 }
270
271 static ssize_t max77665_haptic_store(struct device *dev,
272                 struct device_attribute *attr, const char *buf, size_t count)
273 {
274         struct max77665_haptic *chip = dev_get_drvdata(dev);
275         int var;
276
277         sscanf(buf, "%d", &var);
278         if (strcmp(attr->attr.name, "pattern_cycle") == 0) {
279                 if (var >= 0 && var <= 15)
280                         chip->pattern_cycle = var;
281         } else if (strcmp(attr->attr.name, "pattern_signal_period") == 0) {
282                 if (var >= 0 && var <= 0xFF)
283                         chip->pattern_signal_period = var;
284         } else if (strcmp(attr->attr.name, "feedback_duty_cycle") == 0) {
285                 if (var >= 0 && var <= 15)
286                         chip->feedback_duty_cycle = var;
287         } else if (strcmp(attr->attr.name, "scf_val") == 0) {
288                 if (var >= 0 && var <= 7)
289                         chip->scf_val = var;
290         } else if (strcmp(attr->attr.name, "pwm_divisor") == 0) {
291                 if (var >= 0 && var <= 3)
292                         chip->pwm_divisor = var;
293         }
294
295         return count;
296 }
297
298
299 static ssize_t max77665_haptic_vibrator_ctrl(struct device *dev,
300                 struct device_attribute *attr, const char *buf, size_t count)
301 {
302         struct max77665_haptic *chip = dev_get_drvdata(dev);
303         int var;
304
305         sscanf(buf, "%d", &var);
306         if (var == 0) {                 /* stop vibrator */
307                 chip->level = 0;
308                 schedule_work(&chip->work);
309         } else if (var == 1) {
310                 chip->level = 100;
311                 schedule_work(&chip->work);
312         }
313
314         return count;
315 }
316
317 static DEVICE_ATTR(pattern_cycle, 0640, max77665_haptic_show,
318                                         max77665_haptic_store);
319 static DEVICE_ATTR(pattern_signal_period, 0640, max77665_haptic_show,
320                                         max77665_haptic_store);
321 static DEVICE_ATTR(feedback_duty_cycle, 0640, max77665_haptic_show,
322                                         max77665_haptic_store);
323 static DEVICE_ATTR(scf_val, 0640, max77665_haptic_show,
324                                         max77665_haptic_store);
325 static DEVICE_ATTR(pwm_divisor, 0640, max77665_haptic_show,
326                                         max77665_haptic_store);
327 static DEVICE_ATTR(vibrator_enable, 0640, NULL,
328                                         max77665_haptic_vibrator_ctrl);
329
330 static struct attribute *max77665_haptics_attr[] = {
331         &dev_attr_pattern_cycle.attr,
332         &dev_attr_pattern_signal_period.attr,
333         &dev_attr_feedback_duty_cycle.attr,
334         &dev_attr_scf_val.attr,
335         &dev_attr_pwm_divisor.attr,
336         &dev_attr_vibrator_enable.attr,
337         NULL,
338 };
339
340 static const struct attribute_group max77665_haptics_attr_group = {
341         .attrs = max77665_haptics_attr,
342 };
343
344 static int max77665_haptic_probe(struct platform_device *pdev)
345 {
346         struct max77665_haptic_platform_data *haptic_pdata =
347                                         pdev->dev.platform_data;
348         struct max77665_haptic *chip;
349         struct input_dev *input_dev;
350         int ret;
351
352         if (!haptic_pdata) {
353                 dev_err(&pdev->dev, "no haptic platform data\n");
354                 return -EINVAL;
355         }
356
357         chip = devm_kzalloc(&pdev->dev, sizeof(struct max77665_haptic),
358                                                         GFP_KERNEL);
359         if (!chip) {
360                 dev_err(&pdev->dev, "unable to allocate memory\n");
361                 return -ENOMEM;
362         }
363
364         input_dev = input_allocate_device();
365         if (!input_dev) {
366                 dev_err(&pdev->dev,
367                         "unable to allocate memory for input dev\n");
368                 ret = -ENOMEM;
369                 goto err_input_alloc;
370         }
371
372         chip->dev = &pdev->dev;
373         chip->input_dev = input_dev;
374         chip->pwm_period = haptic_pdata->pwm_period;
375         chip->type = haptic_pdata->type;
376         chip->mode = haptic_pdata->mode;
377         chip->pwm_divisor = haptic_pdata->pwm_divisor;
378
379         if (chip->mode == MAX77665_INTERNAL_MODE) {
380                 chip->internal_mode_pattern =
381                                 haptic_pdata->internal_mode_pattern;
382                 chip->pattern_cycle = haptic_pdata->pattern_cycle;
383                 chip->pattern_signal_period =
384                                 haptic_pdata->pattern_signal_period;
385                 chip->feedback_duty_cycle =
386                                 haptic_pdata->feedback_duty_cycle;
387                 chip->invert = haptic_pdata->invert;
388                 chip->cont_mode = haptic_pdata->cont_mode;
389                 chip->motor_startup_val = haptic_pdata->motor_startup_val;
390                 chip->scf_val = haptic_pdata->scf_val;
391         }
392
393         if (chip->mode == MAX77665_EXTERNAL_MODE) {
394                 chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
395                                         "max-vbrtr");
396                 if (IS_ERR(chip->pwm)) {
397                         dev_err(&pdev->dev,
398                                 "unable to request PWM for haptic\n");
399                         ret = PTR_ERR(chip->pwm);
400                         goto err_pwm;
401                 }
402         }
403
404         chip->regulator = regulator_get(&pdev->dev, "vdd_vbrtr");
405         if (IS_ERR(chip->regulator)) {
406                 dev_err(&pdev->dev, "unable to get regulator\n");
407                 ret = PTR_ERR(chip->regulator);
408                 goto err_regulator;
409         }
410
411 register_input:
412         dev_set_drvdata(&pdev->dev, chip);
413         input_dev->name = "max77665-haptic";
414         input_dev->id.version = 1;
415         input_dev->dev.parent = &pdev->dev;
416         input_set_drvdata(input_dev, chip);
417         input_set_capability(input_dev, EV_FF, FF_RUMBLE);
418
419         ret = input_ff_create_memless(input_dev, NULL,
420                                 max77665_haptic_play_effect);
421         if (ret) {
422                 dev_err(&pdev->dev,
423                         "unable to create FF device(ret : %d)\n", ret);
424                 goto err_ff_memless;
425         }
426         INIT_WORK(&chip->work,
427                         max77665_haptic_play_effect_work);
428
429         ret = input_register_device(input_dev);
430         if (ret) {
431                 dev_err(&pdev->dev,
432                         "unable to register input device(ret : %d)\n", ret);
433                 goto err_input_register;
434         }
435
436         ret = sysfs_create_group(&pdev->dev.kobj, &max77665_haptics_attr_group);
437         if (ret < 0) {
438                 dev_err(&pdev->dev,
439                         "unable to create sysfs %d\n", ret);
440         }
441
442         return 0;
443
444 err_input_register:
445         destroy_work_on_stack(&chip->work);
446         input_ff_destroy(input_dev);
447 err_ff_memless:
448         regulator_put(chip->regulator);
449 err_regulator:
450         if (chip->mode == MAX77665_EXTERNAL_MODE)
451                 pwm_free(chip->pwm);
452 err_pwm:
453         input_free_device(input_dev);
454 err_input_alloc:
455         kfree(chip);
456
457         return ret;
458 }
459
460 static int max77665_haptic_remove(struct platform_device *pdev)
461 {
462         struct max77665_haptic *chip = platform_get_drvdata(pdev);
463
464         destroy_work_on_stack(&chip->work);
465         input_unregister_device(chip->input_dev);
466         sysfs_remove_group(&pdev->dev.kobj, &max77665_haptics_attr_group);
467         regulator_put(chip->regulator);
468
469         if (chip->mode == MAX77665_EXTERNAL_MODE)
470                 pwm_free(chip->pwm);
471
472         kfree(chip);
473
474         return 0;
475 }
476
477 static int max77665_haptic_suspend(struct device *dev)
478 {
479         struct max77665_haptic *chip = platform_get_drvdata(pdev);
480
481         max77665_haptic_enable(chip, false);
482
483         return 0;
484 }
485
486 static SIMPLE_DEV_PM_OPS(max77665_haptic_pm_ops, max77665_haptic_suspend, NULL);
487
488 static const struct platform_device_id max77665_haptic_id[] = {
489         { "max77665-haptic", 0 },
490         { },
491 };
492 MODULE_DEVICE_TABLE(i2c, max77665_haptic_id);
493
494 static struct platform_driver max77665_haptic_driver = {
495         .driver = {
496                 .name   = "max77665-haptic",
497                 .owner  = THIS_MODULE,
498                 .pm     = &max77665_haptic_pm_ops,
499         },
500         .probe          = max77665_haptic_probe,
501         .remove         = max77665_haptic_remove,
502         .id_table       = max77665_haptic_id,
503 };
504
505 module_platform_driver(max77665_haptic_driver);
506
507 MODULE_ALIAS("platform:max77665-haptic");
508 MODULE_DESCRIPTION("max77665_haptic driver");
509 MODULE_LICENSE("GPL v2");