^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * twl6040-vibra.c - TWL6040 Vibrator driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Misael Lopez Cruz <misael.lopez@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright: (C) 2011 Texas Instruments, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Felipe Balbi <felipe.balbi@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Jari Vanhala <ext-javi.vanhala@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/twl6040.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/delay.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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define EFFECT_DIR_180_DEG 0x8000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Recommended modulation index 85% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define TWL6040_VIBRA_MOD 85
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define TWL6040_NUM_SUPPLIES 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct vibra_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct work_struct play_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int weak_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int strong_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int vibldrv_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int vibrdrv_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int viblmotor_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int vibrmotor_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct twl6040 *twl6040;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct vibra_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct twl6040 *twl6040 = info->twl6040;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u8 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (status & TWL6040_VIBLOCDET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) TWL6040_VIBENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (status & TWL6040_VIBROCDET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) TWL6040_VIBENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static void twl6040_vibra_enable(struct vibra_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct twl6040 *twl6040 = info->twl6040;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) dev_err(info->dev, "failed to enable regulators %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) twl6040_power(info->twl6040, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * ERRATA: Disable overcurrent protection for at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * 3ms when enabling vibrator drivers to avoid false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * overcurrent detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) TWL6040_VIBENA | TWL6040_VIBCTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) TWL6040_VIBENA | TWL6040_VIBCTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) usleep_range(3000, 3500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) TWL6040_VIBENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) TWL6040_VIBENA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) info->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void twl6040_vibra_disable(struct vibra_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct twl6040 *twl6040 = info->twl6040;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) twl6040_power(info->twl6040, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) info->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int speed, int direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int vpk, max_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u8 vibdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* output swing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) (100 * (vibdrv_res + motor_res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* 50mV per VIBDAT code step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) max_code = vpk / 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (max_code > TWL6040_VIBDAT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) max_code = TWL6040_VIBDAT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* scale speed to max allowed code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) vibdat = (u8)((speed * max_code) / USHRT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* 2's complement for direction > 180 degrees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) vibdat *= direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return vibdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void twl6040_vibra_set_effect(struct vibra_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct twl6040 *twl6040 = info->twl6040;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u8 vibdatl, vibdatr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int volt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* weak motor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) info->viblmotor_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) info->weak_speed, info->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* strong motor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) info->vibrmotor_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) info->strong_speed, info->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void vibra_play_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct vibra_info *info = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct vibra_info, play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* Do not allow effect, while the routing is set to use audio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ret = twl6040_get_vibralr_status(info->twl6040);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (ret & TWL6040_VIBSEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dev_info(info->dev, "Vibra is configured for audio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (info->weak_speed || info->strong_speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) twl6040_vibra_enable(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) twl6040_vibra_set_effect(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) } else if (info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) twl6040_vibra_disable(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int vibra_play(struct input_dev *input, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct vibra_info *info = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) info->weak_speed = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) info->strong_speed = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) schedule_work(&info->play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 0;
^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) static void twl6040_vibra_close(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct vibra_info *info = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) cancel_work_sync(&info->play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) twl6040_vibra_disable(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int __maybe_unused twl6040_vibra_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct vibra_info *info = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cancel_work_sync(&info->play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) twl6040_vibra_disable(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^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) static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int twl6040_vibra_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct device *twl6040_core_dev = pdev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct device_node *twl6040_core_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct vibra_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int vddvibl_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int vddvibr_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) twl6040_core_node = of_get_child_by_name(twl6040_core_dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) "vibra");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!twl6040_core_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dev_err(&pdev->dev, "parent of node is missing?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -EINVAL;
^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) info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) of_node_put(twl6040_core_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dev_err(&pdev->dev, "couldn't allocate memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) info->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) info->twl6040 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) &info->vibldrv_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) &info->vibrdrv_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) &info->viblmotor_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) &info->vibrmotor_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) of_node_put(twl6040_core_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if ((!info->vibldrv_res && !info->viblmotor_res) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) (!info->vibrdrv_res && !info->vibrmotor_res)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_err(info->dev, "invalid vibra driver/motor resistance\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -EINVAL;
^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) info->irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (info->irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) twl6040_vib_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) "twl6040_irq_vib", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) info->supplies[0].supply = "vddvibl";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) info->supplies[1].supply = "vddvibr";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * When booted with Device tree the regulators are attached to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * parent device (twl6040 MFD core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) error = devm_regulator_bulk_get(twl6040_core_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ARRAY_SIZE(info->supplies),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) info->supplies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) dev_err(info->dev, "couldn't get regulators %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (vddvibl_uV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) error = regulator_set_voltage(info->supplies[0].consumer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) vddvibl_uV, vddvibl_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return error;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (vddvibr_uV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) error = regulator_set_voltage(info->supplies[1].consumer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) vddvibr_uV, vddvibr_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) INIT_WORK(&info->play_work, vibra_play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) info->input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!info->input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) dev_err(info->dev, "couldn't allocate input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) input_set_drvdata(info->input_dev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) info->input_dev->name = "twl6040:vibrator";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) info->input_dev->id.version = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) info->input_dev->close = twl6040_vibra_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) __set_bit(FF_RUMBLE, info->input_dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) dev_err(info->dev, "couldn't register vibrator to FF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) error = input_register_device(info->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) dev_err(info->dev, "couldn't register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static struct platform_driver twl6040_vibra_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .probe = twl6040_vibra_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .name = "twl6040-vibra",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .pm = &twl6040_vibra_pm_ops,
^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) module_platform_driver(twl6040_vibra_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) MODULE_ALIAS("platform:twl6040-vibra");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MODULE_DESCRIPTION("TWL6040 Vibra driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");