15b6c26a9SCarlo Caione /* 25b6c26a9SCarlo Caione * axp20x power button driver. 35b6c26a9SCarlo Caione * 45b6c26a9SCarlo Caione * Copyright (C) 2013 Carlo Caione <carlo@caione.org> 55b6c26a9SCarlo Caione * 65b6c26a9SCarlo Caione * This file is subject to the terms and conditions of the GNU General 75b6c26a9SCarlo Caione * Public License. See the file "COPYING" in the main directory of this 85b6c26a9SCarlo Caione * archive for more details. 95b6c26a9SCarlo Caione * 105b6c26a9SCarlo Caione * This program is distributed in the hope that it will be useful, 115b6c26a9SCarlo Caione * but WITHOUT ANY WARRANTY; without even the implied warranty of 125b6c26a9SCarlo Caione * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135b6c26a9SCarlo Caione * GNU General Public License for more details. 145b6c26a9SCarlo Caione */ 155b6c26a9SCarlo Caione 169b13a4caSHans de Goede #include <linux/acpi.h> 175b6c26a9SCarlo Caione #include <linux/errno.h> 185b6c26a9SCarlo Caione #include <linux/irq.h> 195b6c26a9SCarlo Caione #include <linux/init.h> 205b6c26a9SCarlo Caione #include <linux/input.h> 215b6c26a9SCarlo Caione #include <linux/interrupt.h> 225b6c26a9SCarlo Caione #include <linux/kernel.h> 235b6c26a9SCarlo Caione #include <linux/mfd/axp20x.h> 245b6c26a9SCarlo Caione #include <linux/module.h> 255b6c26a9SCarlo Caione #include <linux/platform_device.h> 265b6c26a9SCarlo Caione #include <linux/regmap.h> 275b6c26a9SCarlo Caione #include <linux/slab.h> 285b6c26a9SCarlo Caione 295b6c26a9SCarlo Caione #define AXP20X_PEK_STARTUP_MASK (0xc0) 305b6c26a9SCarlo Caione #define AXP20X_PEK_SHUTDOWN_MASK (0x03) 315b6c26a9SCarlo Caione 32fbc1b323SQuentin Schulz struct axp20x_info { 33fbc1b323SQuentin Schulz const struct axp20x_time *startup_time; 34fbc1b323SQuentin Schulz unsigned int startup_mask; 35fbc1b323SQuentin Schulz const struct axp20x_time *shutdown_time; 36fbc1b323SQuentin Schulz unsigned int shutdown_mask; 37fbc1b323SQuentin Schulz }; 38fbc1b323SQuentin Schulz 395b6c26a9SCarlo Caione struct axp20x_pek { 405b6c26a9SCarlo Caione struct axp20x_dev *axp20x; 415b6c26a9SCarlo Caione struct input_dev *input; 42fbc1b323SQuentin Schulz struct axp20x_info *info; 435b6c26a9SCarlo Caione int irq_dbr; 445b6c26a9SCarlo Caione int irq_dbf; 455b6c26a9SCarlo Caione }; 465b6c26a9SCarlo Caione 475b6c26a9SCarlo Caione struct axp20x_time { 485b6c26a9SCarlo Caione unsigned int time; 495b6c26a9SCarlo Caione unsigned int idx; 505b6c26a9SCarlo Caione }; 515b6c26a9SCarlo Caione 525b6c26a9SCarlo Caione static const struct axp20x_time startup_time[] = { 535b6c26a9SCarlo Caione { .time = 128, .idx = 0 }, 545b6c26a9SCarlo Caione { .time = 1000, .idx = 2 }, 555b6c26a9SCarlo Caione { .time = 3000, .idx = 1 }, 565b6c26a9SCarlo Caione { .time = 2000, .idx = 3 }, 575b6c26a9SCarlo Caione }; 585b6c26a9SCarlo Caione 59c3cc9447SQuentin Schulz static const struct axp20x_time axp221_startup_time[] = { 60c3cc9447SQuentin Schulz { .time = 128, .idx = 0 }, 61c3cc9447SQuentin Schulz { .time = 1000, .idx = 1 }, 62c3cc9447SQuentin Schulz { .time = 2000, .idx = 2 }, 63c3cc9447SQuentin Schulz { .time = 3000, .idx = 3 }, 64c3cc9447SQuentin Schulz }; 65c3cc9447SQuentin Schulz 665b6c26a9SCarlo Caione static const struct axp20x_time shutdown_time[] = { 675b6c26a9SCarlo Caione { .time = 4000, .idx = 0 }, 685b6c26a9SCarlo Caione { .time = 6000, .idx = 1 }, 695b6c26a9SCarlo Caione { .time = 8000, .idx = 2 }, 705b6c26a9SCarlo Caione { .time = 10000, .idx = 3 }, 715b6c26a9SCarlo Caione }; 725b6c26a9SCarlo Caione 73fbc1b323SQuentin Schulz static const struct axp20x_info axp20x_info = { 74fbc1b323SQuentin Schulz .startup_time = startup_time, 75fbc1b323SQuentin Schulz .startup_mask = AXP20X_PEK_STARTUP_MASK, 76fbc1b323SQuentin Schulz .shutdown_time = shutdown_time, 77fbc1b323SQuentin Schulz .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK, 785b6c26a9SCarlo Caione }; 795b6c26a9SCarlo Caione 80c3cc9447SQuentin Schulz static const struct axp20x_info axp221_info = { 81c3cc9447SQuentin Schulz .startup_time = axp221_startup_time, 82c3cc9447SQuentin Schulz .startup_mask = AXP20X_PEK_STARTUP_MASK, 83c3cc9447SQuentin Schulz .shutdown_time = shutdown_time, 84c3cc9447SQuentin Schulz .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK, 85c3cc9447SQuentin Schulz }; 86c3cc9447SQuentin Schulz 87fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr(struct device *dev, 88fbc1b323SQuentin Schulz const struct axp20x_time *time, 89fbc1b323SQuentin Schulz unsigned int mask, char *buf) 905b6c26a9SCarlo Caione { 915b6c26a9SCarlo Caione struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 925b6c26a9SCarlo Caione unsigned int val; 935b6c26a9SCarlo Caione int ret, i; 945b6c26a9SCarlo Caione 955b6c26a9SCarlo Caione ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val); 965b6c26a9SCarlo Caione if (ret != 0) 975b6c26a9SCarlo Caione return ret; 985b6c26a9SCarlo Caione 99fbc1b323SQuentin Schulz val &= mask; 100fbc1b323SQuentin Schulz val >>= ffs(mask) - 1; 1015b6c26a9SCarlo Caione 1025b6c26a9SCarlo Caione for (i = 0; i < 4; i++) 103fbc1b323SQuentin Schulz if (val == time[i].idx) 104fbc1b323SQuentin Schulz val = time[i].time; 1055b6c26a9SCarlo Caione 1065b6c26a9SCarlo Caione return sprintf(buf, "%u\n", val); 1075b6c26a9SCarlo Caione } 1085b6c26a9SCarlo Caione 109fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr_startup(struct device *dev, 1105b6c26a9SCarlo Caione struct device_attribute *attr, 111fbc1b323SQuentin Schulz char *buf) 1125b6c26a9SCarlo Caione { 1135b6c26a9SCarlo Caione struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 114fbc1b323SQuentin Schulz 115fbc1b323SQuentin Schulz return axp20x_show_attr(dev, axp20x_pek->info->startup_time, 116fbc1b323SQuentin Schulz axp20x_pek->info->startup_mask, buf); 117fbc1b323SQuentin Schulz } 118fbc1b323SQuentin Schulz 119fbc1b323SQuentin Schulz static ssize_t axp20x_show_attr_shutdown(struct device *dev, 120fbc1b323SQuentin Schulz struct device_attribute *attr, 121fbc1b323SQuentin Schulz char *buf) 122fbc1b323SQuentin Schulz { 123fbc1b323SQuentin Schulz struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 124fbc1b323SQuentin Schulz 125fbc1b323SQuentin Schulz return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time, 126fbc1b323SQuentin Schulz axp20x_pek->info->shutdown_mask, buf); 127fbc1b323SQuentin Schulz } 128fbc1b323SQuentin Schulz 129fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr(struct device *dev, 130fbc1b323SQuentin Schulz const struct axp20x_time *time, 131fbc1b323SQuentin Schulz unsigned int mask, const char *buf, 132fbc1b323SQuentin Schulz size_t count) 133fbc1b323SQuentin Schulz { 134fbc1b323SQuentin Schulz struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 1355b6c26a9SCarlo Caione char val_str[20]; 1365b6c26a9SCarlo Caione size_t len; 1375b6c26a9SCarlo Caione int ret, i; 1385b6c26a9SCarlo Caione unsigned int val, idx = 0; 1395b6c26a9SCarlo Caione unsigned int best_err = UINT_MAX; 1405b6c26a9SCarlo Caione 1415b6c26a9SCarlo Caione val_str[sizeof(val_str) - 1] = '\0'; 1425b6c26a9SCarlo Caione strncpy(val_str, buf, sizeof(val_str) - 1); 1435b6c26a9SCarlo Caione len = strlen(val_str); 1445b6c26a9SCarlo Caione 1455b6c26a9SCarlo Caione if (len && val_str[len - 1] == '\n') 1465b6c26a9SCarlo Caione val_str[len - 1] = '\0'; 1475b6c26a9SCarlo Caione 1485b6c26a9SCarlo Caione ret = kstrtouint(val_str, 10, &val); 1495b6c26a9SCarlo Caione if (ret) 1505b6c26a9SCarlo Caione return ret; 1515b6c26a9SCarlo Caione 1525b6c26a9SCarlo Caione for (i = 3; i >= 0; i--) { 1535b6c26a9SCarlo Caione unsigned int err; 1545b6c26a9SCarlo Caione 155fbc1b323SQuentin Schulz err = abs(time[i].time - val); 1565b6c26a9SCarlo Caione if (err < best_err) { 1575b6c26a9SCarlo Caione best_err = err; 158fbc1b323SQuentin Schulz idx = time[i].idx; 1595b6c26a9SCarlo Caione } 1605b6c26a9SCarlo Caione 1615b6c26a9SCarlo Caione if (!err) 1625b6c26a9SCarlo Caione break; 1635b6c26a9SCarlo Caione } 1645b6c26a9SCarlo Caione 165fbc1b323SQuentin Schulz idx <<= ffs(mask) - 1; 166fbc1b323SQuentin Schulz ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, 167fbc1b323SQuentin Schulz mask, idx); 1685b6c26a9SCarlo Caione if (ret != 0) 1695b6c26a9SCarlo Caione return -EINVAL; 170b388de88SDmitry Torokhov 1715b6c26a9SCarlo Caione return count; 1725b6c26a9SCarlo Caione } 1735b6c26a9SCarlo Caione 174fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr_startup(struct device *dev, 175fbc1b323SQuentin Schulz struct device_attribute *attr, 176fbc1b323SQuentin Schulz const char *buf, size_t count) 177fbc1b323SQuentin Schulz { 178fbc1b323SQuentin Schulz struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 1795b6c26a9SCarlo Caione 180fbc1b323SQuentin Schulz return axp20x_store_attr(dev, axp20x_pek->info->startup_time, 181fbc1b323SQuentin Schulz axp20x_pek->info->startup_mask, buf, count); 182fbc1b323SQuentin Schulz } 183fbc1b323SQuentin Schulz 184fbc1b323SQuentin Schulz static ssize_t axp20x_store_attr_shutdown(struct device *dev, 185fbc1b323SQuentin Schulz struct device_attribute *attr, 186fbc1b323SQuentin Schulz const char *buf, size_t count) 187fbc1b323SQuentin Schulz { 188fbc1b323SQuentin Schulz struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 189fbc1b323SQuentin Schulz 190fbc1b323SQuentin Schulz return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time, 191fbc1b323SQuentin Schulz axp20x_pek->info->shutdown_mask, buf, count); 192fbc1b323SQuentin Schulz } 193fbc1b323SQuentin Schulz 194cbe821a2SBen Dooks (Codethink) static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup, 195cbe821a2SBen Dooks (Codethink) axp20x_store_attr_startup); 196cbe821a2SBen Dooks (Codethink) static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown, 197fbc1b323SQuentin Schulz axp20x_store_attr_shutdown); 198b388de88SDmitry Torokhov 199d99995a4SGreg Kroah-Hartman static struct attribute *axp20x_attrs[] = { 200fbc1b323SQuentin Schulz &dev_attr_startup.attr, 201fbc1b323SQuentin Schulz &dev_attr_shutdown.attr, 202b388de88SDmitry Torokhov NULL, 203b388de88SDmitry Torokhov }; 204d99995a4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(axp20x); 2055b6c26a9SCarlo Caione 2065b6c26a9SCarlo Caione static irqreturn_t axp20x_pek_irq(int irq, void *pwr) 2075b6c26a9SCarlo Caione { 2089747070cSHans de Goede struct axp20x_pek *axp20x_pek = pwr; 2099747070cSHans de Goede struct input_dev *idev = axp20x_pek->input; 2109747070cSHans de Goede 2119747070cSHans de Goede if (!idev) 2129747070cSHans de Goede return IRQ_HANDLED; 2135b6c26a9SCarlo Caione 214eeeee40fSHans de Goede /* 215eeeee40fSHans de Goede * The power-button is connected to ground so a falling edge (dbf) 216eeeee40fSHans de Goede * means it is pressed. 217eeeee40fSHans de Goede */ 218eeeee40fSHans de Goede if (irq == axp20x_pek->irq_dbf) 2195b6c26a9SCarlo Caione input_report_key(idev, KEY_POWER, true); 220eeeee40fSHans de Goede else if (irq == axp20x_pek->irq_dbr) 2215b6c26a9SCarlo Caione input_report_key(idev, KEY_POWER, false); 2225b6c26a9SCarlo Caione 2235b6c26a9SCarlo Caione input_sync(idev); 2245b6c26a9SCarlo Caione 2255b6c26a9SCarlo Caione return IRQ_HANDLED; 2265b6c26a9SCarlo Caione } 2275b6c26a9SCarlo Caione 228f2bd5a9eSHans de Goede static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek, 229f2bd5a9eSHans de Goede struct platform_device *pdev) 2305b6c26a9SCarlo Caione { 2315b6c26a9SCarlo Caione struct input_dev *idev; 2325b6c26a9SCarlo Caione int error; 2335b6c26a9SCarlo Caione 2345b6c26a9SCarlo Caione axp20x_pek->input = devm_input_allocate_device(&pdev->dev); 2355b6c26a9SCarlo Caione if (!axp20x_pek->input) 2365b6c26a9SCarlo Caione return -ENOMEM; 2375b6c26a9SCarlo Caione 2385b6c26a9SCarlo Caione idev = axp20x_pek->input; 2395b6c26a9SCarlo Caione 2405b6c26a9SCarlo Caione idev->name = "axp20x-pek"; 2415b6c26a9SCarlo Caione idev->phys = "m1kbd/input2"; 2425b6c26a9SCarlo Caione idev->dev.parent = &pdev->dev; 2435b6c26a9SCarlo Caione 2445b6c26a9SCarlo Caione input_set_capability(idev, EV_KEY, KEY_POWER); 2455b6c26a9SCarlo Caione 2465b6c26a9SCarlo Caione input_set_drvdata(idev, axp20x_pek); 2475b6c26a9SCarlo Caione 248f2bd5a9eSHans de Goede error = input_register_device(idev); 249f2bd5a9eSHans de Goede if (error) { 250f2bd5a9eSHans de Goede dev_err(&pdev->dev, "Can't register input device: %d\n", 251f2bd5a9eSHans de Goede error); 252f2bd5a9eSHans de Goede return error; 253f2bd5a9eSHans de Goede } 254f2bd5a9eSHans de Goede 255f2bd5a9eSHans de Goede return 0; 256f2bd5a9eSHans de Goede } 257f2bd5a9eSHans de Goede 2588d4b3137SHans de Goede #ifdef CONFIG_ACPI 2598d4b3137SHans de Goede static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek, 2608d4b3137SHans de Goede struct platform_device *pdev) 2618d4b3137SHans de Goede { 2628d4b3137SHans de Goede unsigned long long hrv = 0; 2638d4b3137SHans de Goede acpi_status status; 2648d4b3137SHans de Goede 2658d4b3137SHans de Goede if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) && 2668d4b3137SHans de Goede axp20x_pek->axp20x->variant == AXP288_ID) { 2678d4b3137SHans de Goede status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent), 2688d4b3137SHans de Goede "_HRV", NULL, &hrv); 2698d4b3137SHans de Goede if (ACPI_FAILURE(status)) 2708d4b3137SHans de Goede dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n"); 2718d4b3137SHans de Goede 2728d4b3137SHans de Goede /* 2738d4b3137SHans de Goede * On Cherry Trail platforms (hrv == 3), do not register the 2740fd5f221SHans de Goede * input device if there is an "INTCFD9" or "ACPI0011" gpio 2758d4b3137SHans de Goede * button ACPI device, as that handles the power button too, 2768d4b3137SHans de Goede * and otherwise we end up reporting all presses twice. 2778d4b3137SHans de Goede */ 2780fd5f221SHans de Goede if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) || 2790fd5f221SHans de Goede acpi_dev_present("ACPI0011", NULL, -1))) 2808d4b3137SHans de Goede return false; 2818d4b3137SHans de Goede 2828d4b3137SHans de Goede } 2838d4b3137SHans de Goede 2848d4b3137SHans de Goede return true; 2858d4b3137SHans de Goede } 2868d4b3137SHans de Goede #else 2878d4b3137SHans de Goede static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek, 2888d4b3137SHans de Goede struct platform_device *pdev) 2898d4b3137SHans de Goede { 2908d4b3137SHans de Goede return true; 2918d4b3137SHans de Goede } 2928d4b3137SHans de Goede #endif 2938d4b3137SHans de Goede 294f2bd5a9eSHans de Goede static int axp20x_pek_probe(struct platform_device *pdev) 295f2bd5a9eSHans de Goede { 296f2bd5a9eSHans de Goede struct axp20x_pek *axp20x_pek; 297fbc1b323SQuentin Schulz const struct platform_device_id *match = platform_get_device_id(pdev); 298f2bd5a9eSHans de Goede int error; 299f2bd5a9eSHans de Goede 300fbc1b323SQuentin Schulz if (!match) { 301fbc1b323SQuentin Schulz dev_err(&pdev->dev, "Failed to get platform_device_id\n"); 302fbc1b323SQuentin Schulz return -EINVAL; 303fbc1b323SQuentin Schulz } 304fbc1b323SQuentin Schulz 305f2bd5a9eSHans de Goede axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek), 306f2bd5a9eSHans de Goede GFP_KERNEL); 307f2bd5a9eSHans de Goede if (!axp20x_pek) 308f2bd5a9eSHans de Goede return -ENOMEM; 309f2bd5a9eSHans de Goede 310f2bd5a9eSHans de Goede axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent); 311f2bd5a9eSHans de Goede 3129747070cSHans de Goede axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR"); 3139747070cSHans de Goede if (axp20x_pek->irq_dbr < 0) 3149747070cSHans de Goede return axp20x_pek->irq_dbr; 3159747070cSHans de Goede axp20x_pek->irq_dbr = regmap_irq_get_virq( 3169747070cSHans de Goede axp20x_pek->axp20x->regmap_irqc, axp20x_pek->irq_dbr); 3179747070cSHans de Goede 3189747070cSHans de Goede axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF"); 3199747070cSHans de Goede if (axp20x_pek->irq_dbf < 0) 3209747070cSHans de Goede return axp20x_pek->irq_dbf; 3219747070cSHans de Goede axp20x_pek->irq_dbf = regmap_irq_get_virq( 3229747070cSHans de Goede axp20x_pek->axp20x->regmap_irqc, axp20x_pek->irq_dbf); 3239747070cSHans de Goede 3248d4b3137SHans de Goede if (axp20x_pek_should_register_input(axp20x_pek, pdev)) { 325f2bd5a9eSHans de Goede error = axp20x_pek_probe_input_device(axp20x_pek, pdev); 326f2bd5a9eSHans de Goede if (error) 327f2bd5a9eSHans de Goede return error; 3289b13a4caSHans de Goede } 329f2bd5a9eSHans de Goede 330fbc1b323SQuentin Schulz axp20x_pek->info = (struct axp20x_info *)match->driver_data; 331fbc1b323SQuentin Schulz 3329747070cSHans de Goede error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr, 3339747070cSHans de Goede axp20x_pek_irq, 0, 3349747070cSHans de Goede "axp20x-pek-dbr", axp20x_pek); 3359747070cSHans de Goede if (error < 0) { 3369747070cSHans de Goede dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n", 3379747070cSHans de Goede axp20x_pek->irq_dbr, error); 3389747070cSHans de Goede return error; 3399747070cSHans de Goede } 3409747070cSHans de Goede 3419747070cSHans de Goede error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf, 3429747070cSHans de Goede axp20x_pek_irq, 0, 3439747070cSHans de Goede "axp20x-pek-dbf", axp20x_pek); 3449747070cSHans de Goede if (error < 0) { 3459747070cSHans de Goede dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n", 3469747070cSHans de Goede axp20x_pek->irq_dbf, error); 3479747070cSHans de Goede return error; 3489747070cSHans de Goede } 3499747070cSHans de Goede 3509747070cSHans de Goede device_init_wakeup(&pdev->dev, true); 3519747070cSHans de Goede 352b388de88SDmitry Torokhov platform_set_drvdata(pdev, axp20x_pek); 3535b6c26a9SCarlo Caione 3545b6c26a9SCarlo Caione return 0; 3555b6c26a9SCarlo Caione } 3565b6c26a9SCarlo Caione 357fe77f9bbSSamuel Holland static int __maybe_unused axp20x_pek_suspend(struct device *dev) 358fe77f9bbSSamuel Holland { 359fe77f9bbSSamuel Holland struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 360fe77f9bbSSamuel Holland 361fe77f9bbSSamuel Holland /* 362fe77f9bbSSamuel Holland * As nested threaded IRQs are not automatically disabled during 363fe77f9bbSSamuel Holland * suspend, we must explicitly disable non-wakeup IRQs. 364fe77f9bbSSamuel Holland */ 365fe77f9bbSSamuel Holland if (device_may_wakeup(dev)) { 366fe77f9bbSSamuel Holland enable_irq_wake(axp20x_pek->irq_dbf); 367fe77f9bbSSamuel Holland enable_irq_wake(axp20x_pek->irq_dbr); 368fe77f9bbSSamuel Holland } else { 369fe77f9bbSSamuel Holland disable_irq(axp20x_pek->irq_dbf); 370fe77f9bbSSamuel Holland disable_irq(axp20x_pek->irq_dbr); 371fe77f9bbSSamuel Holland } 372fe77f9bbSSamuel Holland 373fe77f9bbSSamuel Holland return 0; 374fe77f9bbSSamuel Holland } 375fe77f9bbSSamuel Holland 376fe77f9bbSSamuel Holland static int __maybe_unused axp20x_pek_resume(struct device *dev) 377fe77f9bbSSamuel Holland { 378fe77f9bbSSamuel Holland struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 379fe77f9bbSSamuel Holland 380fe77f9bbSSamuel Holland if (device_may_wakeup(dev)) { 381fe77f9bbSSamuel Holland disable_irq_wake(axp20x_pek->irq_dbf); 382fe77f9bbSSamuel Holland disable_irq_wake(axp20x_pek->irq_dbr); 383fe77f9bbSSamuel Holland } else { 384fe77f9bbSSamuel Holland enable_irq(axp20x_pek->irq_dbf); 385fe77f9bbSSamuel Holland enable_irq(axp20x_pek->irq_dbr); 386fe77f9bbSSamuel Holland } 387fe77f9bbSSamuel Holland 388fe77f9bbSSamuel Holland return 0; 389fe77f9bbSSamuel Holland } 390fe77f9bbSSamuel Holland 39158be7689SHans de Goede static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev) 39258be7689SHans de Goede { 39358be7689SHans de Goede struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); 39458be7689SHans de Goede 39558be7689SHans de Goede if (axp20x_pek->axp20x->variant != AXP288_ID) 39658be7689SHans de Goede return 0; 39758be7689SHans de Goede 39858be7689SHans de Goede /* 39958be7689SHans de Goede * Clear interrupts from button presses during suspend, to avoid 40058be7689SHans de Goede * a wakeup power-button press getting reported to userspace. 40158be7689SHans de Goede */ 40258be7689SHans de Goede regmap_write(axp20x_pek->axp20x->regmap, 40358be7689SHans de Goede AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8, 40458be7689SHans de Goede BIT(AXP288_IRQ_POKN % 8)); 40558be7689SHans de Goede 40658be7689SHans de Goede return 0; 40758be7689SHans de Goede } 40858be7689SHans de Goede 40958be7689SHans de Goede static const struct dev_pm_ops axp20x_pek_pm_ops = { 410fe77f9bbSSamuel Holland SET_SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume) 41158be7689SHans de Goede #ifdef CONFIG_PM_SLEEP 41258be7689SHans de Goede .resume_noirq = axp20x_pek_resume_noirq, 41358be7689SHans de Goede #endif 41458be7689SHans de Goede }; 41558be7689SHans de Goede 416fbc1b323SQuentin Schulz static const struct platform_device_id axp_pek_id_match[] = { 417fbc1b323SQuentin Schulz { 418fbc1b323SQuentin Schulz .name = "axp20x-pek", 419fbc1b323SQuentin Schulz .driver_data = (kernel_ulong_t)&axp20x_info, 420fbc1b323SQuentin Schulz }, 421c3cc9447SQuentin Schulz { 422c3cc9447SQuentin Schulz .name = "axp221-pek", 423c3cc9447SQuentin Schulz .driver_data = (kernel_ulong_t)&axp221_info, 424c3cc9447SQuentin Schulz }, 425fbc1b323SQuentin Schulz { /* sentinel */ } 426fbc1b323SQuentin Schulz }; 427481c209fSHans de Goede MODULE_DEVICE_TABLE(platform, axp_pek_id_match); 428fbc1b323SQuentin Schulz 4295b6c26a9SCarlo Caione static struct platform_driver axp20x_pek_driver = { 4305b6c26a9SCarlo Caione .probe = axp20x_pek_probe, 431fbc1b323SQuentin Schulz .id_table = axp_pek_id_match, 4325b6c26a9SCarlo Caione .driver = { 4335b6c26a9SCarlo Caione .name = "axp20x-pek", 43458be7689SHans de Goede .pm = &axp20x_pek_pm_ops, 435d99995a4SGreg Kroah-Hartman .dev_groups = axp20x_groups, 4365b6c26a9SCarlo Caione }, 4375b6c26a9SCarlo Caione }; 4385b6c26a9SCarlo Caione module_platform_driver(axp20x_pek_driver); 4395b6c26a9SCarlo Caione 4405b6c26a9SCarlo Caione MODULE_DESCRIPTION("axp20x Power Button"); 4415b6c26a9SCarlo Caione MODULE_AUTHOR("Carlo Caione <carlo@caione.org>"); 4425b6c26a9SCarlo Caione MODULE_LICENSE("GPL"); 443