0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/err.h>
0015 #include <linux/pwm.h>
0016 #include <linux/input.h>
0017 #include <linux/mfd/max8997-private.h>
0018 #include <linux/mfd/max8997.h>
0019 #include <linux/regulator/consumer.h>
0020
0021
0022 #define MAX8997_MOTOR_TYPE_SHIFT 7
0023 #define MAX8997_ENABLE_SHIFT 6
0024 #define MAX8997_MODE_SHIFT 5
0025
0026
0027 #define MAX8997_CYCLE_SHIFT 6
0028 #define MAX8997_SIG_PERIOD_SHIFT 4
0029 #define MAX8997_SIG_DUTY_SHIFT 2
0030 #define MAX8997_PWM_DUTY_SHIFT 0
0031
0032 struct max8997_haptic {
0033 struct device *dev;
0034 struct i2c_client *client;
0035 struct input_dev *input_dev;
0036 struct regulator *regulator;
0037
0038 struct work_struct work;
0039 struct mutex mutex;
0040
0041 bool enabled;
0042 unsigned int level;
0043
0044 struct pwm_device *pwm;
0045 unsigned int pwm_period;
0046 enum max8997_haptic_pwm_divisor pwm_divisor;
0047
0048 enum max8997_haptic_motor_type type;
0049 enum max8997_haptic_pulse_mode mode;
0050
0051 unsigned int internal_mode_pattern;
0052 unsigned int pattern_cycle;
0053 unsigned int pattern_signal_period;
0054 };
0055
0056 static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
0057 {
0058 int ret = 0;
0059
0060 if (chip->mode == MAX8997_EXTERNAL_MODE) {
0061 unsigned int duty = chip->pwm_period * chip->level / 100;
0062 ret = pwm_config(chip->pwm, duty, chip->pwm_period);
0063 } else {
0064 u8 duty_index = 0;
0065
0066 duty_index = DIV_ROUND_UP(chip->level * 64, 100);
0067
0068 switch (chip->internal_mode_pattern) {
0069 case 0:
0070 max8997_write_reg(chip->client,
0071 MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
0072 break;
0073 case 1:
0074 max8997_write_reg(chip->client,
0075 MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
0076 break;
0077 case 2:
0078 max8997_write_reg(chip->client,
0079 MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
0080 break;
0081 case 3:
0082 max8997_write_reg(chip->client,
0083 MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
0084 break;
0085 default:
0086 break;
0087 }
0088 }
0089 return ret;
0090 }
0091
0092 static void max8997_haptic_configure(struct max8997_haptic *chip)
0093 {
0094 u8 value;
0095
0096 value = chip->type << MAX8997_MOTOR_TYPE_SHIFT |
0097 chip->enabled << MAX8997_ENABLE_SHIFT |
0098 chip->mode << MAX8997_MODE_SHIFT | chip->pwm_divisor;
0099 max8997_write_reg(chip->client, MAX8997_HAPTIC_REG_CONF2, value);
0100
0101 if (chip->mode == MAX8997_INTERNAL_MODE && chip->enabled) {
0102 value = chip->internal_mode_pattern << MAX8997_CYCLE_SHIFT |
0103 chip->internal_mode_pattern << MAX8997_SIG_PERIOD_SHIFT |
0104 chip->internal_mode_pattern << MAX8997_SIG_DUTY_SHIFT |
0105 chip->internal_mode_pattern << MAX8997_PWM_DUTY_SHIFT;
0106 max8997_write_reg(chip->client,
0107 MAX8997_HAPTIC_REG_DRVCONF, value);
0108
0109 switch (chip->internal_mode_pattern) {
0110 case 0:
0111 value = chip->pattern_cycle << 4;
0112 max8997_write_reg(chip->client,
0113 MAX8997_HAPTIC_REG_CYCLECONF1, value);
0114 value = chip->pattern_signal_period;
0115 max8997_write_reg(chip->client,
0116 MAX8997_HAPTIC_REG_SIGCONF1, value);
0117 break;
0118
0119 case 1:
0120 value = chip->pattern_cycle;
0121 max8997_write_reg(chip->client,
0122 MAX8997_HAPTIC_REG_CYCLECONF1, value);
0123 value = chip->pattern_signal_period;
0124 max8997_write_reg(chip->client,
0125 MAX8997_HAPTIC_REG_SIGCONF2, value);
0126 break;
0127
0128 case 2:
0129 value = chip->pattern_cycle << 4;
0130 max8997_write_reg(chip->client,
0131 MAX8997_HAPTIC_REG_CYCLECONF2, value);
0132 value = chip->pattern_signal_period;
0133 max8997_write_reg(chip->client,
0134 MAX8997_HAPTIC_REG_SIGCONF3, value);
0135 break;
0136
0137 case 3:
0138 value = chip->pattern_cycle;
0139 max8997_write_reg(chip->client,
0140 MAX8997_HAPTIC_REG_CYCLECONF2, value);
0141 value = chip->pattern_signal_period;
0142 max8997_write_reg(chip->client,
0143 MAX8997_HAPTIC_REG_SIGCONF4, value);
0144 break;
0145
0146 default:
0147 break;
0148 }
0149 }
0150 }
0151
0152 static void max8997_haptic_enable(struct max8997_haptic *chip)
0153 {
0154 int error;
0155
0156 mutex_lock(&chip->mutex);
0157
0158 error = max8997_haptic_set_duty_cycle(chip);
0159 if (error) {
0160 dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
0161 goto out;
0162 }
0163
0164 if (!chip->enabled) {
0165 error = regulator_enable(chip->regulator);
0166 if (error) {
0167 dev_err(chip->dev, "Failed to enable regulator\n");
0168 goto out;
0169 }
0170 max8997_haptic_configure(chip);
0171 if (chip->mode == MAX8997_EXTERNAL_MODE) {
0172 error = pwm_enable(chip->pwm);
0173 if (error) {
0174 dev_err(chip->dev, "Failed to enable PWM\n");
0175 regulator_disable(chip->regulator);
0176 goto out;
0177 }
0178 }
0179 chip->enabled = true;
0180 }
0181
0182 out:
0183 mutex_unlock(&chip->mutex);
0184 }
0185
0186 static void max8997_haptic_disable(struct max8997_haptic *chip)
0187 {
0188 mutex_lock(&chip->mutex);
0189
0190 if (chip->enabled) {
0191 chip->enabled = false;
0192 max8997_haptic_configure(chip);
0193 if (chip->mode == MAX8997_EXTERNAL_MODE)
0194 pwm_disable(chip->pwm);
0195 regulator_disable(chip->regulator);
0196 }
0197
0198 mutex_unlock(&chip->mutex);
0199 }
0200
0201 static void max8997_haptic_play_effect_work(struct work_struct *work)
0202 {
0203 struct max8997_haptic *chip =
0204 container_of(work, struct max8997_haptic, work);
0205
0206 if (chip->level)
0207 max8997_haptic_enable(chip);
0208 else
0209 max8997_haptic_disable(chip);
0210 }
0211
0212 static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
0213 struct ff_effect *effect)
0214 {
0215 struct max8997_haptic *chip = input_get_drvdata(dev);
0216
0217 chip->level = effect->u.rumble.strong_magnitude;
0218 if (!chip->level)
0219 chip->level = effect->u.rumble.weak_magnitude;
0220
0221 schedule_work(&chip->work);
0222
0223 return 0;
0224 }
0225
0226 static void max8997_haptic_close(struct input_dev *dev)
0227 {
0228 struct max8997_haptic *chip = input_get_drvdata(dev);
0229
0230 cancel_work_sync(&chip->work);
0231 max8997_haptic_disable(chip);
0232 }
0233
0234 static int max8997_haptic_probe(struct platform_device *pdev)
0235 {
0236 struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
0237 const struct max8997_platform_data *pdata =
0238 dev_get_platdata(iodev->dev);
0239 const struct max8997_haptic_platform_data *haptic_pdata = NULL;
0240 struct max8997_haptic *chip;
0241 struct input_dev *input_dev;
0242 int error;
0243
0244 if (pdata)
0245 haptic_pdata = pdata->haptic_pdata;
0246
0247 if (!haptic_pdata) {
0248 dev_err(&pdev->dev, "no haptic platform data\n");
0249 return -EINVAL;
0250 }
0251
0252 chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
0253 input_dev = input_allocate_device();
0254 if (!chip || !input_dev) {
0255 dev_err(&pdev->dev, "unable to allocate memory\n");
0256 error = -ENOMEM;
0257 goto err_free_mem;
0258 }
0259
0260 INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
0261 mutex_init(&chip->mutex);
0262
0263 chip->client = iodev->haptic;
0264 chip->dev = &pdev->dev;
0265 chip->input_dev = input_dev;
0266 chip->pwm_period = haptic_pdata->pwm_period;
0267 chip->type = haptic_pdata->type;
0268 chip->mode = haptic_pdata->mode;
0269 chip->pwm_divisor = haptic_pdata->pwm_divisor;
0270
0271 switch (chip->mode) {
0272 case MAX8997_INTERNAL_MODE:
0273 chip->internal_mode_pattern =
0274 haptic_pdata->internal_mode_pattern;
0275 chip->pattern_cycle = haptic_pdata->pattern_cycle;
0276 chip->pattern_signal_period =
0277 haptic_pdata->pattern_signal_period;
0278 break;
0279
0280 case MAX8997_EXTERNAL_MODE:
0281 chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
0282 "max8997-haptic");
0283 if (IS_ERR(chip->pwm)) {
0284 error = PTR_ERR(chip->pwm);
0285 dev_err(&pdev->dev,
0286 "unable to request PWM for haptic, error: %d\n",
0287 error);
0288 goto err_free_mem;
0289 }
0290
0291
0292
0293
0294
0295 pwm_apply_args(chip->pwm);
0296 break;
0297
0298 default:
0299 dev_err(&pdev->dev,
0300 "Invalid chip mode specified (%d)\n", chip->mode);
0301 error = -EINVAL;
0302 goto err_free_mem;
0303 }
0304
0305 chip->regulator = regulator_get(&pdev->dev, "inmotor");
0306 if (IS_ERR(chip->regulator)) {
0307 error = PTR_ERR(chip->regulator);
0308 dev_err(&pdev->dev,
0309 "unable to get regulator, error: %d\n",
0310 error);
0311 goto err_free_pwm;
0312 }
0313
0314 input_dev->name = "max8997-haptic";
0315 input_dev->id.version = 1;
0316 input_dev->dev.parent = &pdev->dev;
0317 input_dev->close = max8997_haptic_close;
0318 input_set_drvdata(input_dev, chip);
0319 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
0320
0321 error = input_ff_create_memless(input_dev, NULL,
0322 max8997_haptic_play_effect);
0323 if (error) {
0324 dev_err(&pdev->dev,
0325 "unable to create FF device, error: %d\n",
0326 error);
0327 goto err_put_regulator;
0328 }
0329
0330 error = input_register_device(input_dev);
0331 if (error) {
0332 dev_err(&pdev->dev,
0333 "unable to register input device, error: %d\n",
0334 error);
0335 goto err_destroy_ff;
0336 }
0337
0338 platform_set_drvdata(pdev, chip);
0339 return 0;
0340
0341 err_destroy_ff:
0342 input_ff_destroy(input_dev);
0343 err_put_regulator:
0344 regulator_put(chip->regulator);
0345 err_free_pwm:
0346 if (chip->mode == MAX8997_EXTERNAL_MODE)
0347 pwm_free(chip->pwm);
0348 err_free_mem:
0349 input_free_device(input_dev);
0350 kfree(chip);
0351
0352 return error;
0353 }
0354
0355 static int max8997_haptic_remove(struct platform_device *pdev)
0356 {
0357 struct max8997_haptic *chip = platform_get_drvdata(pdev);
0358
0359 input_unregister_device(chip->input_dev);
0360 regulator_put(chip->regulator);
0361
0362 if (chip->mode == MAX8997_EXTERNAL_MODE)
0363 pwm_free(chip->pwm);
0364
0365 kfree(chip);
0366
0367 return 0;
0368 }
0369
0370 static int __maybe_unused max8997_haptic_suspend(struct device *dev)
0371 {
0372 struct platform_device *pdev = to_platform_device(dev);
0373 struct max8997_haptic *chip = platform_get_drvdata(pdev);
0374
0375 max8997_haptic_disable(chip);
0376
0377 return 0;
0378 }
0379
0380 static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
0381
0382 static const struct platform_device_id max8997_haptic_id[] = {
0383 { "max8997-haptic", 0 },
0384 { },
0385 };
0386 MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
0387
0388 static struct platform_driver max8997_haptic_driver = {
0389 .driver = {
0390 .name = "max8997-haptic",
0391 .pm = &max8997_haptic_pm_ops,
0392 },
0393 .probe = max8997_haptic_probe,
0394 .remove = max8997_haptic_remove,
0395 .id_table = max8997_haptic_id,
0396 };
0397 module_platform_driver(max8997_haptic_driver);
0398
0399 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
0400 MODULE_DESCRIPTION("max8997_haptic driver");
0401 MODULE_LICENSE("GPL");