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/devm-helpers.h>
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/input.h>
11 #include <linux/interrupt.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_wakeirq.h>
15 #include <linux/property.h>
16 #include <linux/workqueue.h>
17 #include <linux/regmap.h>
18 #include <linux/mfd/da9063/core.h>
19 #include <linux/mfd/da9063/registers.h>
20 #include <linux/mfd/da9062/core.h>
21 #include <linux/mfd/da9062/registers.h>
23 struct da906x_chip_config
{
26 int onkey_pwr_signalling
;
30 int onkey_nonkey_mask
;
31 int onkey_nonkey_lock_mask
;
32 int onkey_key_reset_mask
;
33 int onkey_shutdown_mask
;
39 struct delayed_work work
;
40 struct input_dev
*input
;
42 struct regmap
*regmap
;
43 const struct da906x_chip_config
*config
;
48 static const struct da906x_chip_config da9063_regs
= {
50 .onkey_status
= DA9063_REG_STATUS_A
,
51 .onkey_pwr_signalling
= DA9063_REG_CONTROL_B
,
52 .onkey_fault_log
= DA9063_REG_FAULT_LOG
,
53 .onkey_shutdown
= DA9063_REG_CONTROL_F
,
55 .onkey_nonkey_mask
= DA9063_NONKEY
,
56 .onkey_nonkey_lock_mask
= DA9063_NONKEY_LOCK
,
57 .onkey_key_reset_mask
= DA9063_KEY_RESET
,
58 .onkey_shutdown_mask
= DA9063_SHUTDOWN
,
60 .name
= DA9063_DRVNAME_ONKEY
,
63 static const struct da906x_chip_config da9062_regs
= {
65 .onkey_status
= DA9062AA_STATUS_A
,
66 .onkey_pwr_signalling
= DA9062AA_CONTROL_B
,
67 .onkey_fault_log
= DA9062AA_FAULT_LOG
,
68 .onkey_shutdown
= DA9062AA_CONTROL_F
,
70 .onkey_nonkey_mask
= DA9062AA_NONKEY_MASK
,
71 .onkey_nonkey_lock_mask
= DA9062AA_NONKEY_LOCK_MASK
,
72 .onkey_key_reset_mask
= DA9062AA_KEY_RESET_MASK
,
73 .onkey_shutdown_mask
= DA9062AA_SHUTDOWN_MASK
,
75 .name
= "da9062-onkey",
78 static void da9063_poll_on(struct work_struct
*work
)
80 struct da9063_onkey
*onkey
= container_of(work
,
83 const struct da906x_chip_config
*config
= onkey
->config
;
89 /* Poll to see when the pin is released */
90 error
= regmap_read(onkey
->regmap
,
95 "Failed to read ON status: %d\n", error
);
99 if (!(val
& config
->onkey_nonkey_mask
)) {
100 error
= regmap_update_bits(onkey
->regmap
,
101 config
->onkey_pwr_signalling
,
102 config
->onkey_nonkey_lock_mask
,
106 "Failed to reset the Key Delay %d\n", error
);
110 input_report_key(onkey
->input
, KEY_POWER
, 0);
111 input_sync(onkey
->input
);
117 * If the fault log KEY_RESET is detected, then clear it
118 * and shut down the system.
120 error
= regmap_read(onkey
->regmap
,
121 config
->onkey_fault_log
,
124 dev_warn(&onkey
->input
->dev
,
125 "Cannot read FAULT_LOG: %d\n", error
);
126 } else if (fault_log
& config
->onkey_key_reset_mask
) {
127 error
= regmap_write(onkey
->regmap
,
128 config
->onkey_fault_log
,
129 config
->onkey_key_reset_mask
);
131 dev_warn(&onkey
->input
->dev
,
132 "Cannot reset KEY_RESET fault log: %d\n",
135 /* at this point we do any S/W housekeeping
136 * and then send shutdown command
138 dev_dbg(&onkey
->input
->dev
,
139 "Sending SHUTDOWN to PMIC ...\n");
140 error
= regmap_write(onkey
->regmap
,
141 config
->onkey_shutdown
,
142 config
->onkey_shutdown_mask
);
144 dev_err(&onkey
->input
->dev
,
145 "Cannot SHUTDOWN PMIC: %d\n",
152 schedule_delayed_work(&onkey
->work
, msecs_to_jiffies(50));
155 static irqreturn_t
da9063_onkey_irq_handler(int irq
, void *data
)
157 struct da9063_onkey
*onkey
= data
;
158 const struct da906x_chip_config
*config
= onkey
->config
;
162 error
= regmap_read(onkey
->regmap
,
163 config
->onkey_status
,
165 if (onkey
->key_power
&& !error
&& (val
& config
->onkey_nonkey_mask
)) {
166 input_report_key(onkey
->input
, KEY_POWER
, 1);
167 input_sync(onkey
->input
);
168 schedule_delayed_work(&onkey
->work
, 0);
169 dev_dbg(onkey
->dev
, "KEY_POWER long press.\n");
171 input_report_key(onkey
->input
, KEY_POWER
, 1);
172 input_sync(onkey
->input
);
173 input_report_key(onkey
->input
, KEY_POWER
, 0);
174 input_sync(onkey
->input
);
175 dev_dbg(onkey
->dev
, "KEY_POWER short press.\n");
181 static int da9063_onkey_probe(struct platform_device
*pdev
)
183 struct da9063_onkey
*onkey
;
187 onkey
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9063_onkey
),
192 onkey
->config
= device_get_match_data(&pdev
->dev
);
196 onkey
->dev
= &pdev
->dev
;
198 onkey
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
200 return dev_err_probe(&pdev
->dev
, -ENXIO
,
201 "Parent regmap unavailable.\n");
203 onkey
->key_power
= !device_property_read_bool(&pdev
->dev
,
204 "dlg,disable-key-power");
206 onkey
->input
= devm_input_allocate_device(&pdev
->dev
);
210 onkey
->input
->name
= onkey
->config
->name
;
211 snprintf(onkey
->phys
, sizeof(onkey
->phys
), "%s/input0",
212 onkey
->config
->name
);
213 onkey
->input
->phys
= onkey
->phys
;
215 input_set_capability(onkey
->input
, EV_KEY
, KEY_POWER
);
217 error
= devm_delayed_work_autocancel(&pdev
->dev
, &onkey
->work
,
222 irq
= platform_get_irq_byname(pdev
, "ONKEY");
226 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
227 NULL
, da9063_onkey_irq_handler
,
228 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
231 return dev_err_probe(&pdev
->dev
, error
,
232 "Failed to allocate onkey IRQ\n");
234 error
= dev_pm_set_wake_irq(&pdev
->dev
, irq
);
237 "Failed to set IRQ %d as a wake IRQ: %d\n",
240 device_init_wakeup(&pdev
->dev
, true);
242 error
= input_register_device(onkey
->input
);
249 static const struct of_device_id da9063_compatible_reg_id_table
[] = {
250 { .compatible
= "dlg,da9063-onkey", .data
= &da9063_regs
},
251 { .compatible
= "dlg,da9062-onkey", .data
= &da9062_regs
},
254 MODULE_DEVICE_TABLE(of
, da9063_compatible_reg_id_table
);
256 static struct platform_driver da9063_onkey_driver
= {
257 .probe
= da9063_onkey_probe
,
259 .name
= DA9063_DRVNAME_ONKEY
,
260 .of_match_table
= da9063_compatible_reg_id_table
,
263 module_platform_driver(da9063_onkey_driver
);
265 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
266 MODULE_DESCRIPTION("Onkey device driver for Dialog DA9063, DA9062 and DA9061");
267 MODULE_LICENSE("GPL");
268 MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY
);