^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * MAX8997-haptic controller driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Donggeun Kim <dg77.kim@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is not provided / owned by Maxim Integrated Products.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mfd/max8997-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/max8997.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Haptic configuration 2 register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MAX8997_MOTOR_TYPE_SHIFT 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MAX8997_ENABLE_SHIFT 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define MAX8997_MODE_SHIFT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Haptic driver configuration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define MAX8997_CYCLE_SHIFT 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define MAX8997_SIG_PERIOD_SHIFT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define MAX8997_SIG_DUTY_SHIFT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define MAX8997_PWM_DUTY_SHIFT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct max8997_haptic {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct regulator *regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int pwm_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) enum max8997_haptic_pwm_divisor pwm_divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) enum max8997_haptic_motor_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) enum max8997_haptic_pulse_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int internal_mode_pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int pattern_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int max8997_haptic_set_duty_cycle(struct max8997_haptic *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (chip->mode == MAX8997_EXTERNAL_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int duty = chip->pwm_period * chip->level / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = pwm_config(chip->pwm, duty, chip->pwm_period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u8 duty_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) for (i = 0; i <= 64; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (chip->level <= i * 100 / 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) duty_index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) switch (chip->internal_mode_pattern) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) MAX8997_HAPTIC_REG_SIGPWMDC1, duty_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) MAX8997_HAPTIC_REG_SIGPWMDC2, duty_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) MAX8997_HAPTIC_REG_SIGPWMDC3, duty_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) MAX8997_HAPTIC_REG_SIGPWMDC4, duty_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void max8997_haptic_configure(struct max8997_haptic *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) u8 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) value = chip->type << MAX8997_MOTOR_TYPE_SHIFT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) chip->enabled << MAX8997_ENABLE_SHIFT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) chip->mode << MAX8997_MODE_SHIFT | chip->pwm_divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) max8997_write_reg(chip->client, MAX8997_HAPTIC_REG_CONF2, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (chip->mode == MAX8997_INTERNAL_MODE && chip->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) value = chip->internal_mode_pattern << MAX8997_CYCLE_SHIFT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) chip->internal_mode_pattern << MAX8997_SIG_PERIOD_SHIFT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) chip->internal_mode_pattern << MAX8997_SIG_DUTY_SHIFT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) chip->internal_mode_pattern << MAX8997_PWM_DUTY_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) MAX8997_HAPTIC_REG_DRVCONF, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) switch (chip->internal_mode_pattern) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) value = chip->pattern_cycle << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MAX8997_HAPTIC_REG_CYCLECONF1, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) value = chip->pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MAX8997_HAPTIC_REG_SIGCONF1, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) value = chip->pattern_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MAX8997_HAPTIC_REG_CYCLECONF1, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) value = chip->pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) MAX8997_HAPTIC_REG_SIGCONF2, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) value = chip->pattern_cycle << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MAX8997_HAPTIC_REG_CYCLECONF2, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) value = chip->pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) MAX8997_HAPTIC_REG_SIGCONF3, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) value = chip->pattern_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) MAX8997_HAPTIC_REG_CYCLECONF2, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) value = chip->pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) max8997_write_reg(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) MAX8997_HAPTIC_REG_SIGCONF4, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void max8997_haptic_enable(struct max8997_haptic *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) mutex_lock(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) error = max8997_haptic_set_duty_cycle(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_err(chip->dev, "set_pwm_cycle failed, error: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!chip->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) error = regulator_enable(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dev_err(chip->dev, "Failed to enable regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) max8997_haptic_configure(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (chip->mode == MAX8997_EXTERNAL_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) error = pwm_enable(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_err(chip->dev, "Failed to enable PWM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) regulator_disable(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) chip->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) mutex_unlock(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static void max8997_haptic_disable(struct max8997_haptic *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) mutex_lock(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (chip->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) chip->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) max8997_haptic_configure(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (chip->mode == MAX8997_EXTERNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pwm_disable(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) regulator_disable(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) mutex_unlock(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static void max8997_haptic_play_effect_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct max8997_haptic *chip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) container_of(work, struct max8997_haptic, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (chip->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) max8997_haptic_enable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) max8997_haptic_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int max8997_haptic_play_effect(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct max8997_haptic *chip = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) chip->level = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!chip->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) chip->level = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) schedule_work(&chip->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void max8997_haptic_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct max8997_haptic *chip = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) cancel_work_sync(&chip->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) max8997_haptic_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int max8997_haptic_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const struct max8997_platform_data *pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev_get_platdata(iodev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) const struct max8997_haptic_platform_data *haptic_pdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct max8997_haptic *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) haptic_pdata = pdata->haptic_pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!haptic_pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_err(&pdev->dev, "no haptic platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) chip = kzalloc(sizeof(struct max8997_haptic), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!chip || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dev_err(&pdev->dev, "unable to allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) INIT_WORK(&chip->work, max8997_haptic_play_effect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mutex_init(&chip->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) chip->client = iodev->haptic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) chip->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) chip->input_dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) chip->pwm_period = haptic_pdata->pwm_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) chip->type = haptic_pdata->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) chip->mode = haptic_pdata->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) chip->pwm_divisor = haptic_pdata->pwm_divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) switch (chip->mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case MAX8997_INTERNAL_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) chip->internal_mode_pattern =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) haptic_pdata->internal_mode_pattern;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) chip->pattern_cycle = haptic_pdata->pattern_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) chip->pattern_signal_period =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) haptic_pdata->pattern_signal_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) case MAX8997_EXTERNAL_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) chip->pwm = pwm_request(haptic_pdata->pwm_channel_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) "max8997-haptic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (IS_ERR(chip->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) error = PTR_ERR(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) "unable to request PWM for haptic, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * FIXME: pwm_apply_args() should be removed when switching to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * the atomic PWM API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) pwm_apply_args(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) "Invalid chip mode specified (%d)\n", chip->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) chip->regulator = regulator_get(&pdev->dev, "inmotor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (IS_ERR(chip->regulator)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) error = PTR_ERR(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) "unable to get regulator, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto err_free_pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) input_dev->name = "max8997-haptic";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) input_dev->id.version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) input_dev->close = max8997_haptic_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) input_set_drvdata(input_dev, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) input_set_capability(input_dev, EV_FF, FF_RUMBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) error = input_ff_create_memless(input_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) max8997_haptic_play_effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) "unable to create FF device, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto err_put_regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) error = input_register_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) "unable to register input device, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto err_destroy_ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) platform_set_drvdata(pdev, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) err_destroy_ff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) input_ff_destroy(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err_put_regulator:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) regulator_put(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) err_free_pwm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (chip->mode == MAX8997_EXTERNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) pwm_free(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int max8997_haptic_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct max8997_haptic *chip = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) input_unregister_device(chip->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) regulator_put(chip->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (chip->mode == MAX8997_EXTERNAL_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) pwm_free(chip->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int __maybe_unused max8997_haptic_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct max8997_haptic *chip = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) max8997_haptic_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static SIMPLE_DEV_PM_OPS(max8997_haptic_pm_ops, max8997_haptic_suspend, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static const struct platform_device_id max8997_haptic_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) { "max8997-haptic", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) MODULE_DEVICE_TABLE(platform, max8997_haptic_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static struct platform_driver max8997_haptic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .name = "max8997-haptic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) .pm = &max8997_haptic_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .probe = max8997_haptic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .remove = max8997_haptic_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .id_table = max8997_haptic_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) module_platform_driver(max8997_haptic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) MODULE_DESCRIPTION("max8997_haptic driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) MODULE_LICENSE("GPL");