^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * axp20x power button driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Public License. See the file "COPYING" in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/mfd/axp20x.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define AXP20X_PEK_STARTUP_MASK (0xc0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct axp20x_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const struct axp20x_time *startup_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int startup_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const struct axp20x_time *shutdown_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int shutdown_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct axp20x_pek {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct axp20x_dev *axp20x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct axp20x_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int irq_dbr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int irq_dbf;
^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 axp20x_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static const struct axp20x_time startup_time[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { .time = 128, .idx = 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { .time = 1000, .idx = 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) { .time = 3000, .idx = 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) { .time = 2000, .idx = 3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static const struct axp20x_time axp221_startup_time[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { .time = 128, .idx = 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { .time = 1000, .idx = 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) { .time = 2000, .idx = 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) { .time = 3000, .idx = 3 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static const struct axp20x_time shutdown_time[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) { .time = 4000, .idx = 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) { .time = 6000, .idx = 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) { .time = 8000, .idx = 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) { .time = 10000, .idx = 3 },
^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) static const struct axp20x_info axp20x_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .startup_time = startup_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .startup_mask = AXP20X_PEK_STARTUP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .shutdown_time = shutdown_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const struct axp20x_info axp221_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .startup_time = axp221_startup_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .startup_mask = AXP20X_PEK_STARTUP_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .shutdown_time = shutdown_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static ssize_t axp20x_show_attr(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const struct axp20x_time *time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned int mask, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) val &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) val >>= ffs(mask) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (val == time[i].idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) val = time[i].time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return sprintf(buf, "%u\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static ssize_t axp20x_show_attr_startup(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) axp20x_pek->info->startup_mask, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static ssize_t axp20x_show_attr_shutdown(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) axp20x_pek->info->shutdown_mask, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static ssize_t axp20x_store_attr(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) const struct axp20x_time *time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned int mask, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) char val_str[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned int val, idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned int best_err = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) val_str[sizeof(val_str) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) strncpy(val_str, buf, sizeof(val_str) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) len = strlen(val_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (len && val_str[len - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) val_str[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = kstrtouint(val_str, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) for (i = 3; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) err = abs(time[i].time - val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (err < best_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) best_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) idx = time[i].idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) idx <<= ffs(mask) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) mask, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static ssize_t axp20x_store_attr_startup(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) axp20x_pek->info->startup_mask, buf, count);
^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) static ssize_t axp20x_store_attr_shutdown(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) axp20x_pek->info->shutdown_mask, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) axp20x_store_attr_startup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) axp20x_store_attr_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static struct attribute *axp20x_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) &dev_attr_startup.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) &dev_attr_shutdown.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ATTRIBUTE_GROUPS(axp20x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct axp20x_pek *axp20x_pek = pwr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct input_dev *idev = axp20x_pek->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * The power-button is connected to ground so a falling edge (dbf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * means it is pressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (irq == axp20x_pek->irq_dbf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) input_report_key(idev, KEY_POWER, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) else if (irq == axp20x_pek->irq_dbr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) input_report_key(idev, KEY_POWER, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) input_sync(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct input_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!axp20x_pek->input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) idev = axp20x_pek->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) idev->name = "axp20x-pek";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) idev->phys = "m1kbd/input2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) idev->dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) input_set_capability(idev, EV_KEY, KEY_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) input_set_drvdata(idev, axp20x_pek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) error = input_register_device(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dev_err(&pdev->dev, "Can't register input device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^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) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned long long hrv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) axp20x_pek->axp20x->variant == AXP288_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) "_HRV", NULL, &hrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * On Cherry Trail platforms (hrv == 3), do not register the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * input device if there is an "INTCFD9" or "ACPI0011" gpio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * button ACPI device, as that handles the power button too,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * and otherwise we end up reporting all presses twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) acpi_dev_present("ACPI0011", NULL, -1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int axp20x_pek_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct axp20x_pek *axp20x_pek;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) const struct platform_device_id *match = platform_get_device_id(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dev_err(&pdev->dev, "Failed to get platform_device_id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!axp20x_pek)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (axp20x_pek->irq_dbr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return axp20x_pek->irq_dbr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) axp20x_pek->irq_dbr = regmap_irq_get_virq(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) axp20x_pek->axp20x->regmap_irqc, axp20x_pek->irq_dbr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (axp20x_pek->irq_dbf < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return axp20x_pek->irq_dbf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) axp20x_pek->irq_dbf = regmap_irq_get_virq(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) axp20x_pek->axp20x->regmap_irqc, axp20x_pek->irq_dbf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (axp20x_pek_should_register_input(axp20x_pek, pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) axp20x_pek->info = (struct axp20x_info *)match->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) axp20x_pek_irq, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) "axp20x-pek-dbr", axp20x_pek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) axp20x_pek->irq_dbr, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) axp20x_pek_irq, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) "axp20x-pek-dbf", axp20x_pek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) axp20x_pek->irq_dbf, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) device_init_wakeup(&pdev->dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) platform_set_drvdata(pdev, axp20x_pek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int __maybe_unused axp20x_pek_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * As nested threaded IRQs are not automatically disabled during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * suspend, we must explicitly disable non-wakeup IRQs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (device_may_wakeup(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) enable_irq_wake(axp20x_pek->irq_dbf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) enable_irq_wake(axp20x_pek->irq_dbr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) disable_irq(axp20x_pek->irq_dbf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) disable_irq(axp20x_pek->irq_dbr);
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static int __maybe_unused axp20x_pek_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (device_may_wakeup(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) disable_irq_wake(axp20x_pek->irq_dbf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) disable_irq_wake(axp20x_pek->irq_dbr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) enable_irq(axp20x_pek->irq_dbf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) enable_irq(axp20x_pek->irq_dbr);
^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 axp20x_pek_resume_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (axp20x_pek->axp20x->variant != AXP288_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * Clear interrupts from button presses during suspend, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * a wakeup power-button press getting reported to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) regmap_write(axp20x_pek->axp20x->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) BIT(AXP288_IRQ_POKN % 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static const struct dev_pm_ops axp20x_pek_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) SET_SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .resume_noirq = axp20x_pek_resume_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static const struct platform_device_id axp_pek_id_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .name = "axp20x-pek",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .driver_data = (kernel_ulong_t)&axp20x_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .name = "axp221-pek",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .driver_data = (kernel_ulong_t)&axp221_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static struct platform_driver axp20x_pek_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) .probe = axp20x_pek_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) .id_table = axp_pek_id_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) .name = "axp20x-pek",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) .pm = &axp20x_pek_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .dev_groups = axp20x_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) module_platform_driver(axp20x_pek_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) MODULE_DESCRIPTION("axp20x Power Button");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) MODULE_LICENSE("GPL");