1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OnKey device driver for DA9063, DA9062 and DA9061 PMICs
4 * Copyright (C) 2015 Dialog Semiconductor Ltd.
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/input.h>
10 #include <linux/interrupt.h>
11 #include <linux/platform_device.h>
12 #include <linux/workqueue.h>
13 #include <linux/regmap.h>
15 #include <linux/mfd/da9063/core.h>
16 #include <linux/mfd/da9063/registers.h>
17 #include <linux/mfd/da9062/core.h>
18 #include <linux/mfd/da9062/registers.h>
20 struct da906x_chip_config
{
23 int onkey_pwr_signalling
;
27 int onkey_nonkey_mask
;
28 int onkey_nonkey_lock_mask
;
29 int onkey_key_reset_mask
;
30 int onkey_shutdown_mask
;
36 struct delayed_work work
;
37 struct input_dev
*input
;
39 struct regmap
*regmap
;
40 const struct da906x_chip_config
*config
;
45 static const struct da906x_chip_config da9063_regs
= {
47 .onkey_status
= DA9063_REG_STATUS_A
,
48 .onkey_pwr_signalling
= DA9063_REG_CONTROL_B
,
49 .onkey_fault_log
= DA9063_REG_FAULT_LOG
,
50 .onkey_shutdown
= DA9063_REG_CONTROL_F
,
52 .onkey_nonkey_mask
= DA9063_NONKEY
,
53 .onkey_nonkey_lock_mask
= DA9063_NONKEY_LOCK
,
54 .onkey_key_reset_mask
= DA9063_KEY_RESET
,
55 .onkey_shutdown_mask
= DA9063_SHUTDOWN
,
57 .name
= DA9063_DRVNAME_ONKEY
,
60 static const struct da906x_chip_config da9062_regs
= {
62 .onkey_status
= DA9062AA_STATUS_A
,
63 .onkey_pwr_signalling
= DA9062AA_CONTROL_B
,
64 .onkey_fault_log
= DA9062AA_FAULT_LOG
,
65 .onkey_shutdown
= DA9062AA_CONTROL_F
,
67 .onkey_nonkey_mask
= DA9062AA_NONKEY_MASK
,
68 .onkey_nonkey_lock_mask
= DA9062AA_NONKEY_LOCK_MASK
,
69 .onkey_key_reset_mask
= DA9062AA_KEY_RESET_MASK
,
70 .onkey_shutdown_mask
= DA9062AA_SHUTDOWN_MASK
,
72 .name
= "da9062-onkey",
75 static const struct of_device_id da9063_compatible_reg_id_table
[] = {
76 { .compatible
= "dlg,da9063-onkey", .data
= &da9063_regs
},
77 { .compatible
= "dlg,da9062-onkey", .data
= &da9062_regs
},
80 MODULE_DEVICE_TABLE(of
, da9063_compatible_reg_id_table
);
82 static void da9063_poll_on(struct work_struct
*work
)
84 struct da9063_onkey
*onkey
= container_of(work
,
87 const struct da906x_chip_config
*config
= onkey
->config
;
93 /* Poll to see when the pin is released */
94 error
= regmap_read(onkey
->regmap
,
99 "Failed to read ON status: %d\n", error
);
103 if (!(val
& config
->onkey_nonkey_mask
)) {
104 error
= regmap_update_bits(onkey
->regmap
,
105 config
->onkey_pwr_signalling
,
106 config
->onkey_nonkey_lock_mask
,
110 "Failed to reset the Key Delay %d\n", error
);
114 input_report_key(onkey
->input
, KEY_POWER
, 0);
115 input_sync(onkey
->input
);
121 * If the fault log KEY_RESET is detected, then clear it
122 * and shut down the system.
124 error
= regmap_read(onkey
->regmap
,
125 config
->onkey_fault_log
,
128 dev_warn(&onkey
->input
->dev
,
129 "Cannot read FAULT_LOG: %d\n", error
);
130 } else if (fault_log
& config
->onkey_key_reset_mask
) {
131 error
= regmap_write(onkey
->regmap
,
132 config
->onkey_fault_log
,
133 config
->onkey_key_reset_mask
);
135 dev_warn(&onkey
->input
->dev
,
136 "Cannot reset KEY_RESET fault log: %d\n",
139 /* at this point we do any S/W housekeeping
140 * and then send shutdown command
142 dev_dbg(&onkey
->input
->dev
,
143 "Sending SHUTDOWN to PMIC ...\n");
144 error
= regmap_write(onkey
->regmap
,
145 config
->onkey_shutdown
,
146 config
->onkey_shutdown_mask
);
148 dev_err(&onkey
->input
->dev
,
149 "Cannot SHUTDOWN PMIC: %d\n",
156 schedule_delayed_work(&onkey
->work
, msecs_to_jiffies(50));
159 static irqreturn_t
da9063_onkey_irq_handler(int irq
, void *data
)
161 struct da9063_onkey
*onkey
= data
;
162 const struct da906x_chip_config
*config
= onkey
->config
;
166 error
= regmap_read(onkey
->regmap
,
167 config
->onkey_status
,
169 if (onkey
->key_power
&& !error
&& (val
& config
->onkey_nonkey_mask
)) {
170 input_report_key(onkey
->input
, KEY_POWER
, 1);
171 input_sync(onkey
->input
);
172 schedule_delayed_work(&onkey
->work
, 0);
173 dev_dbg(onkey
->dev
, "KEY_POWER long press.\n");
175 input_report_key(onkey
->input
, KEY_POWER
, 1);
176 input_sync(onkey
->input
);
177 input_report_key(onkey
->input
, KEY_POWER
, 0);
178 input_sync(onkey
->input
);
179 dev_dbg(onkey
->dev
, "KEY_POWER short press.\n");
185 static void da9063_cancel_poll(void *data
)
187 struct da9063_onkey
*onkey
= data
;
189 cancel_delayed_work_sync(&onkey
->work
);
192 static int da9063_onkey_probe(struct platform_device
*pdev
)
194 struct da9063_onkey
*onkey
;
195 const struct of_device_id
*match
;
199 match
= of_match_node(da9063_compatible_reg_id_table
,
204 onkey
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9063_onkey
),
207 dev_err(&pdev
->dev
, "Failed to allocate memory.\n");
211 onkey
->config
= match
->data
;
212 onkey
->dev
= &pdev
->dev
;
214 onkey
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
215 if (!onkey
->regmap
) {
216 dev_err(&pdev
->dev
, "Parent regmap unavailable.\n");
220 onkey
->key_power
= !of_property_read_bool(pdev
->dev
.of_node
,
221 "dlg,disable-key-power");
223 onkey
->input
= devm_input_allocate_device(&pdev
->dev
);
225 dev_err(&pdev
->dev
, "Failed to allocated input device.\n");
229 onkey
->input
->name
= onkey
->config
->name
;
230 snprintf(onkey
->phys
, sizeof(onkey
->phys
), "%s/input0",
231 onkey
->config
->name
);
232 onkey
->input
->phys
= onkey
->phys
;
233 onkey
->input
->dev
.parent
= &pdev
->dev
;
235 input_set_capability(onkey
->input
, EV_KEY
, KEY_POWER
);
237 INIT_DELAYED_WORK(&onkey
->work
, da9063_poll_on
);
239 error
= devm_add_action(&pdev
->dev
, da9063_cancel_poll
, onkey
);
242 "Failed to add cancel poll action: %d\n",
247 irq
= platform_get_irq_byname(pdev
, "ONKEY");
251 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
252 NULL
, da9063_onkey_irq_handler
,
253 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
257 "Failed to request IRQ %d: %d\n", irq
, error
);
261 error
= input_register_device(onkey
->input
);
264 "Failed to register input device: %d\n", error
);
271 static struct platform_driver da9063_onkey_driver
= {
272 .probe
= da9063_onkey_probe
,
274 .name
= DA9063_DRVNAME_ONKEY
,
275 .of_match_table
= da9063_compatible_reg_id_table
,
278 module_platform_driver(da9063_onkey_driver
);
280 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
281 MODULE_DESCRIPTION("Onkey device driver for Dialog DA9063, DA9062 and DA9061");
282 MODULE_LICENSE("GPL");
283 MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY
);