blob: 34a976d1e6c6a9db951305f02edc0f7fb29272d7 [file] [log] [blame]
Caleb Connolly64d33922023-11-30 15:11:47 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Qualcomm generic pmic gpio driver
4 *
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6 * (C) Copyright 2023 Linaro Ltd.
7 */
8
9#include <button.h>
10#include <dt-bindings/input/linux-event-codes.h>
11#include <dm.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
14#include <log.h>
15#include <power/pmic.h>
16#include <spmi/spmi.h>
17#include <linux/bitops.h>
18
19#define REG_TYPE 0x4
20#define REG_SUBTYPE 0x5
21
22struct qcom_pmic_btn_priv {
23 u32 base;
24 u32 status_bit;
25 int code;
26 struct udevice *pmic;
27};
28
29#define PON_INT_RT_STS 0x10
30#define KPDPWR_ON_INT_BIT 0
31#define RESIN_ON_INT_BIT 1
32
33#define NODE_IS_PWRKEY(node) (!strncmp(ofnode_get_name(node), "pwrkey", strlen("pwrkey")))
34#define NODE_IS_RESIN(node) (!strncmp(ofnode_get_name(node), "resin", strlen("resin")))
35
36static enum button_state_t qcom_pwrkey_get_state(struct udevice *dev)
37{
38 struct qcom_pmic_btn_priv *priv = dev_get_priv(dev);
39
40 int reg = pmic_reg_read(priv->pmic, priv->base + PON_INT_RT_STS);
41
42 if (reg < 0)
43 return 0;
44
45 return (reg & BIT(priv->status_bit)) != 0;
46}
47
48static int qcom_pwrkey_get_code(struct udevice *dev)
49{
50 struct qcom_pmic_btn_priv *priv = dev_get_priv(dev);
51
52 return priv->code;
53}
54
55static int qcom_pwrkey_probe(struct udevice *dev)
56{
57 struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
58 struct qcom_pmic_btn_priv *priv = dev_get_priv(dev);
59 ofnode node = dev_ofnode(dev);
60 int ret;
61 u64 base;
62
63 /* Ignore the top-level pon node */
64 if (!uc_plat->label)
65 return 0;
66
67 /* the pwrkey and resin nodes are children of the "pon" node, get the
68 * PMIC device to use in pmic_reg_* calls.
69 */
70 priv->pmic = dev->parent->parent;
71
72 /* Get the address of the parent pon node */
73 base = dev_read_addr(dev->parent);
74 if (base == FDT_ADDR_T_NONE) {
75 printf("%s: Can't find address\n", dev->name);
76 return -EINVAL;
77 }
78
79 priv->base = base;
80
81 /* Do a sanity check */
82 ret = pmic_reg_read(priv->pmic, priv->base + REG_TYPE);
83 if (ret != 0x1 && ret != 0xb) {
84 printf("%s: unexpected PMIC function type %d\n", dev->name, ret);
85 return -ENXIO;
86 }
87
88 ret = pmic_reg_read(priv->pmic, priv->base + REG_SUBTYPE);
89 if ((ret & 0x7) == 0) {
90 printf("%s: unexpected PMCI function subtype %d\n", dev->name, ret);
91 return -ENXIO;
92 }
93
94 if (NODE_IS_PWRKEY(node)) {
95 priv->status_bit = 0;
96 priv->code = KEY_ENTER;
97 } else if (NODE_IS_RESIN(node)) {
98 priv->status_bit = 1;
99 priv->code = KEY_DOWN;
100 } else {
101 /* Should not get here! */
102 printf("Invalid pon node '%s' should be 'pwrkey' or 'resin'\n",
103 ofnode_get_name(node));
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
110static int button_qcom_pmic_bind(struct udevice *parent)
111{
112 struct udevice *dev;
113 ofnode node;
114 int ret;
115
116 dev_for_each_subnode(node, parent) {
117 struct button_uc_plat *uc_plat;
118 const char *label;
119
120 if (!ofnode_is_enabled(node))
121 continue;
122
123 ret = device_bind_driver_to_node(parent, "qcom_pwrkey",
124 ofnode_get_name(node),
125 node, &dev);
126 if (ret) {
127 printf("Failed to bind %s! %d\n", label, ret);
128 return ret;
129 }
130 uc_plat = dev_get_uclass_plat(dev);
131 if (NODE_IS_PWRKEY(node)) {
132 uc_plat->label = "pwrkey";
133 } else if (NODE_IS_RESIN(node)) {
134 uc_plat->label = "vol_down";
135 } else {
136 printf("Unknown button node '%s' should be 'pwrkey' or 'resin'\n",
137 ofnode_get_name(node));
138 device_unbind(dev);
139 }
140 }
141
142 return 0;
143}
144
145static const struct button_ops button_qcom_pmic_ops = {
146 .get_state = qcom_pwrkey_get_state,
147 .get_code = qcom_pwrkey_get_code,
148};
149
150static const struct udevice_id qcom_pwrkey_ids[] = {
151 { .compatible = "qcom,pm8916-pon" },
152 { .compatible = "qcom,pm8941-pon" },
153 { .compatible = "qcom,pm8998-pon" },
154 { }
155};
156
157U_BOOT_DRIVER(qcom_pwrkey) = {
158 .name = "qcom_pwrkey",
159 .id = UCLASS_BUTTON,
160 .of_match = qcom_pwrkey_ids,
161 .bind = button_qcom_pmic_bind,
162 .probe = qcom_pwrkey_probe,
163 .ops = &button_qcom_pmic_ops,
164 .priv_auto = sizeof(struct qcom_pmic_btn_priv),
165};