1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Shared helper for ROHM PMIC power button registration 4 * 5 * Copyright 2018, 2019 ROHM Semiconductors 6 * Copyright 2026 Google LLC 7 */ 8 9 #include <linux/device/devres.h> 10 #include <linux/gfp_types.h> 11 #include <linux/input.h> 12 #include <linux/mfd/core.h> 13 #include <linux/module.h> 14 #include <linux/property.h> 15 #include <linux/slab.h> 16 17 #include "rohm-pwrbutton.h" 18 19 #define GPIO_KEYS 0 /* Node corresponding to gpio-keys device itself */ 20 #define PWRON_KEY 1 /* Node describing power button in gpio-keys */ 21 22 static int rohm_pwrbutton_register_swnodes(const struct software_node *nodes) 23 { 24 const struct software_node * const node_group[] = { 25 &nodes[GPIO_KEYS], &nodes[PWRON_KEY], NULL 26 }; 27 28 return software_node_register_node_group(node_group); 29 } 30 31 static void rohm_pwrbutton_unregister_swnodes(void *data) 32 { 33 const struct software_node *nodes = data; 34 const struct software_node * const node_group[] = { 35 &nodes[GPIO_KEYS], &nodes[PWRON_KEY], NULL 36 }; 37 38 software_node_unregister_node_group(node_group); 39 } 40 41 int rohm_register_pwrbutton(struct device *dev, int irq, const char *name, 42 bool wakeup, struct irq_domain *irq_domain) 43 { 44 const struct resource res[] = { 45 DEFINE_RES_IRQ_NAMED(irq, name), 46 }; 47 struct mfd_cell gpio_keys_cell = { 48 .name = "gpio-keys", 49 .resources = res, 50 .num_resources = ARRAY_SIZE(res), 51 }; 52 struct property_entry *parent_props; 53 struct property_entry *child_props; 54 struct software_node *nodes; 55 int n_props; 56 int ret; 57 58 if (irq <= 0) 59 return -EINVAL; 60 61 nodes = devm_kcalloc(dev, 2, sizeof(*nodes), GFP_KERNEL); 62 if (!nodes) 63 return -ENOMEM; 64 65 nodes[GPIO_KEYS].name = devm_kasprintf(dev, GFP_KERNEL, "%s-power-key", dev_name(dev)); 66 if (!nodes[GPIO_KEYS].name) 67 return -ENOMEM; 68 69 parent_props = devm_kcalloc(dev, 2, sizeof(*parent_props), GFP_KERNEL); 70 if (!parent_props) 71 return -ENOMEM; 72 73 parent_props[0] = PROPERTY_ENTRY_STRING("label", name); 74 nodes[GPIO_KEYS].properties = parent_props; 75 76 n_props = 2; /* linux,code and terminator */ 77 if (wakeup) 78 n_props++; 79 80 child_props = devm_kcalloc(dev, n_props, sizeof(*child_props), GFP_KERNEL); 81 if (!child_props) 82 return -ENOMEM; 83 84 child_props[0] = PROPERTY_ENTRY_U32("linux,code", KEY_POWER); 85 if (wakeup) 86 child_props[1] = PROPERTY_ENTRY_BOOL("wakeup-source"); 87 88 nodes[PWRON_KEY].parent = &nodes[GPIO_KEYS]; 89 nodes[PWRON_KEY].properties = child_props; 90 91 ret = rohm_pwrbutton_register_swnodes(nodes); 92 if (ret) 93 return ret; 94 95 ret = devm_add_action_or_reset(dev, rohm_pwrbutton_unregister_swnodes, nodes); 96 if (ret) 97 return ret; 98 99 gpio_keys_cell.swnode = &nodes[GPIO_KEYS]; 100 101 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, &gpio_keys_cell, 1, 102 NULL, 0, irq_domain); 103 if (ret) 104 return dev_err_probe(dev, ret, "Failed to register power-button"); 105 106 return 0; 107 } 108 EXPORT_SYMBOL_GPL(rohm_register_pwrbutton); 109 110 MODULE_LICENSE("GPL"); 111 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>"); 112 MODULE_DESCRIPTION("Shared helper for ROHM PMIC power button registration"); 113