^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) * MAXIM MAX77693/MAX77843 Haptic device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014,2015 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Jaewon Kim <jaewon02.kim@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Krzysztof Kozlowski <krzk@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This program is not provided / owned by Maxim Integrated Products.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/regmap.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/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/mfd/max77693.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/mfd/max77693-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/mfd/max77693-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mfd/max77843-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define MAX_MAGNITUDE_SHIFT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) enum max77693_haptic_motor_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MAX77693_HAPTIC_ERM = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MAX77693_HAPTIC_LRA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) enum max77693_haptic_pulse_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) MAX77693_HAPTIC_EXTERNAL_MODE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) MAX77693_HAPTIC_INTERNAL_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) enum max77693_haptic_pwm_divisor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) MAX77693_HAPTIC_PWM_DIVISOR_32 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) MAX77693_HAPTIC_PWM_DIVISOR_64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) MAX77693_HAPTIC_PWM_DIVISOR_128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) MAX77693_HAPTIC_PWM_DIVISOR_256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct max77693_haptic {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) enum max77693_types dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct regmap *regmap_pmic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct regmap *regmap_haptic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct pwm_device *pwm_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct regulator *motor_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bool suspend_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned int magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int pwm_duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) enum max77693_haptic_motor_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) enum max77693_haptic_pulse_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int max77693_haptic_set_duty_cycle(struct max77693_haptic *haptic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct pwm_args pargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pwm_get_args(haptic->pwm_dev, &pargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) delta = (pargs.period + haptic->pwm_duty) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) error = pwm_config(haptic->pwm_dev, delta, pargs.period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_err(haptic->dev, "failed to configure pwm: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int max77843_haptic_bias(struct max77693_haptic *haptic, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (haptic->dev_type != TYPE_MAX77843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) error = regmap_update_bits(haptic->regmap_haptic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) MAX77843_SYS_REG_MAINCTRL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) MAX77843_MAINCTRL1_BIASEN_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) on << MAINCTRL1_BIASEN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dev_err(haptic->dev, "failed to %s bias: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) on ? "enable" : "disable", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int max77693_haptic_configure(struct max77693_haptic *haptic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned int value, config_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) switch (haptic->dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) case TYPE_MAX77693:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) value = ((haptic->type << MAX77693_CONFIG2_MODE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) (enable << MAX77693_CONFIG2_MEN) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) (haptic->mode << MAX77693_CONFIG2_HTYP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) MAX77693_HAPTIC_PWM_DIVISOR_128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) config_reg = MAX77693_HAPTIC_REG_CONFIG2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) case TYPE_MAX77843:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) value = (haptic->type << MCONFIG_MODE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (enable << MCONFIG_MEN_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MAX77693_HAPTIC_PWM_DIVISOR_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) config_reg = MAX77843_HAP_REG_MCONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) error = regmap_write(haptic->regmap_haptic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) config_reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_err(haptic->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) "failed to update haptic config: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int max77693_haptic_lowsys(struct max77693_haptic *haptic, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (haptic->dev_type != TYPE_MAX77693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error = regmap_update_bits(haptic->regmap_pmic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) MAX77693_PMIC_REG_LSCNFG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) MAX77693_PMIC_LOW_SYS_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) enable << MAX77693_PMIC_LOW_SYS_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev_err(haptic->dev, "cannot update pmic regmap: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return error;
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void max77693_haptic_enable(struct max77693_haptic *haptic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (haptic->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) error = pwm_enable(haptic->pwm_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dev_err(haptic->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) "failed to enable haptic pwm device: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) error = max77693_haptic_lowsys(haptic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto err_enable_lowsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) error = max77693_haptic_configure(haptic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto err_enable_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) haptic->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) err_enable_config:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) max77693_haptic_lowsys(haptic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err_enable_lowsys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) pwm_disable(haptic->pwm_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void max77693_haptic_disable(struct max77693_haptic *haptic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!haptic->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) error = max77693_haptic_configure(haptic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) error = max77693_haptic_lowsys(haptic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) goto err_disable_lowsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) pwm_disable(haptic->pwm_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) haptic->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) err_disable_lowsys:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) max77693_haptic_configure(haptic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void max77693_haptic_play_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct max77693_haptic *haptic =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) container_of(work, struct max77693_haptic, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) error = max77693_haptic_set_duty_cycle(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) dev_err(haptic->dev, "failed to set duty cycle: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (haptic->magnitude)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) max77693_haptic_enable(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) max77693_haptic_disable(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int max77693_haptic_play_effect(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct max77693_haptic *haptic = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct pwm_args pargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) u64 period_mag_multi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) haptic->magnitude = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!haptic->magnitude)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) haptic->magnitude = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * The magnitude comes from force-feedback interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * The formula to convert magnitude to pwm_duty as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * - pwm_duty = (magnitude * pwm_period) / MAX_MAGNITUDE(0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) pwm_get_args(haptic->pwm_dev, &pargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) period_mag_multi = (u64)pargs.period * haptic->magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) haptic->pwm_duty = (unsigned int)(period_mag_multi >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) MAX_MAGNITUDE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) schedule_work(&haptic->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int max77693_haptic_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct max77693_haptic *haptic = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) error = max77843_haptic_bias(haptic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) error = regulator_enable(haptic->motor_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dev_err(haptic->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) "failed to enable regulator: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void max77693_haptic_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct max77693_haptic *haptic = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) cancel_work_sync(&haptic->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) max77693_haptic_disable(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) error = regulator_disable(haptic->motor_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_err(haptic->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) "failed to disable regulator: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) max77843_haptic_bias(haptic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static int max77693_haptic_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct max77693_haptic *haptic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) haptic = devm_kzalloc(&pdev->dev, sizeof(*haptic), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!haptic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) haptic->regmap_pmic = max77693->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) haptic->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) haptic->type = MAX77693_HAPTIC_LRA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) haptic->mode = MAX77693_HAPTIC_EXTERNAL_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) haptic->suspend_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* Variant-specific init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) haptic->dev_type = platform_get_device_id(pdev)->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) switch (haptic->dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case TYPE_MAX77693:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) haptic->regmap_haptic = max77693->regmap_haptic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case TYPE_MAX77843:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) haptic->regmap_haptic = max77693->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) dev_err(&pdev->dev, "unsupported device type: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) haptic->dev_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) INIT_WORK(&haptic->work, max77693_haptic_play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Get pwm and regulatot for haptic device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) haptic->pwm_dev = devm_pwm_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (IS_ERR(haptic->pwm_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dev_err(&pdev->dev, "failed to get pwm device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return PTR_ERR(haptic->pwm_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * FIXME: pwm_apply_args() should be removed when switching to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * atomic PWM API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) pwm_apply_args(haptic->pwm_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) haptic->motor_reg = devm_regulator_get(&pdev->dev, "haptic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (IS_ERR(haptic->motor_reg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dev_err(&pdev->dev, "failed to get regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return PTR_ERR(haptic->motor_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* Initialize input device for haptic device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) haptic->input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!haptic->input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) dev_err(&pdev->dev, "failed to allocate input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) haptic->input_dev->name = "max77693-haptic";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) haptic->input_dev->id.version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) haptic->input_dev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) haptic->input_dev->open = max77693_haptic_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) haptic->input_dev->close = max77693_haptic_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) input_set_drvdata(haptic->input_dev, haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) input_set_capability(haptic->input_dev, EV_FF, FF_RUMBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) error = input_ff_create_memless(haptic->input_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) max77693_haptic_play_effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) dev_err(&pdev->dev, "failed to create force-feedback\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) error = input_register_device(haptic->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) dev_err(&pdev->dev, "failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) platform_set_drvdata(pdev, haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int __maybe_unused max77693_haptic_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct max77693_haptic *haptic = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (haptic->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) max77693_haptic_disable(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) haptic->suspend_state = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return 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) static int __maybe_unused max77693_haptic_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct max77693_haptic *haptic = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (haptic->suspend_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) max77693_haptic_enable(haptic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) haptic->suspend_state = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static SIMPLE_DEV_PM_OPS(max77693_haptic_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) max77693_haptic_suspend, max77693_haptic_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static const struct platform_device_id max77693_haptic_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) { "max77693-haptic", TYPE_MAX77693 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) { "max77843-haptic", TYPE_MAX77843 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) MODULE_DEVICE_TABLE(platform, max77693_haptic_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static struct platform_driver max77693_haptic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) .name = "max77693-haptic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .pm = &max77693_haptic_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .probe = max77693_haptic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .id_table = max77693_haptic_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) module_platform_driver(max77693_haptic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) MODULE_DESCRIPTION("MAXIM 77693/77843 Haptic driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_ALIAS("platform:max77693-haptic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_LICENSE("GPL");