2 * OnKey device driver for DA9063 and DA9062 PMICs
3 * Copyright (C) 2015 Dialog Semiconductor Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/input.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/workqueue.h>
22 #include <linux/regmap.h>
24 #include <linux/mfd/da9063/core.h>
25 #include <linux/mfd/da9063/pdata.h>
26 #include <linux/mfd/da9063/registers.h>
27 #include <linux/mfd/da9062/core.h>
28 #include <linux/mfd/da9062/registers.h>
30 struct da906x_chip_config
{
33 int onkey_pwr_signalling
;
37 int onkey_nonkey_mask
;
38 int onkey_nonkey_lock_mask
;
39 int onkey_key_reset_mask
;
40 int onkey_shutdown_mask
;
46 struct delayed_work work
;
47 struct input_dev
*input
;
49 struct regmap
*regmap
;
50 const struct da906x_chip_config
*config
;
55 static const struct da906x_chip_config da9063_regs
= {
57 .onkey_status
= DA9063_REG_STATUS_A
,
58 .onkey_pwr_signalling
= DA9063_REG_CONTROL_B
,
59 .onkey_fault_log
= DA9063_REG_FAULT_LOG
,
60 .onkey_shutdown
= DA9063_REG_CONTROL_F
,
62 .onkey_nonkey_mask
= DA9063_NONKEY
,
63 .onkey_nonkey_lock_mask
= DA9063_NONKEY_LOCK
,
64 .onkey_key_reset_mask
= DA9063_KEY_RESET
,
65 .onkey_shutdown_mask
= DA9063_SHUTDOWN
,
67 .name
= DA9063_DRVNAME_ONKEY
,
70 static const struct da906x_chip_config da9062_regs
= {
72 .onkey_status
= DA9062AA_STATUS_A
,
73 .onkey_pwr_signalling
= DA9062AA_CONTROL_B
,
74 .onkey_fault_log
= DA9062AA_FAULT_LOG
,
75 .onkey_shutdown
= DA9062AA_CONTROL_F
,
77 .onkey_nonkey_mask
= DA9062AA_NONKEY_MASK
,
78 .onkey_nonkey_lock_mask
= DA9062AA_NONKEY_LOCK_MASK
,
79 .onkey_key_reset_mask
= DA9062AA_KEY_RESET_MASK
,
80 .onkey_shutdown_mask
= DA9062AA_SHUTDOWN_MASK
,
82 .name
= "da9062-onkey",
85 static const struct of_device_id da9063_compatible_reg_id_table
[] = {
86 { .compatible
= "dlg,da9063-onkey", .data
= &da9063_regs
},
87 { .compatible
= "dlg,da9062-onkey", .data
= &da9062_regs
},
91 static void da9063_poll_on(struct work_struct
*work
)
93 struct da9063_onkey
*onkey
= container_of(work
,
96 const struct da906x_chip_config
*config
= onkey
->config
;
102 /* Poll to see when the pin is released */
103 error
= regmap_read(onkey
->regmap
,
104 config
->onkey_status
,
108 "Failed to read ON status: %d\n", error
);
112 if (!(val
& config
->onkey_nonkey_mask
)) {
113 error
= regmap_update_bits(onkey
->regmap
,
114 config
->onkey_pwr_signalling
,
115 config
->onkey_nonkey_lock_mask
,
119 "Failed to reset the Key Delay %d\n", error
);
123 input_report_key(onkey
->input
, KEY_POWER
, 0);
124 input_sync(onkey
->input
);
130 * If the fault log KEY_RESET is detected, then clear it
131 * and shut down the system.
133 error
= regmap_read(onkey
->regmap
,
134 config
->onkey_fault_log
,
137 dev_warn(&onkey
->input
->dev
,
138 "Cannot read FAULT_LOG: %d\n", error
);
139 } else if (fault_log
& config
->onkey_key_reset_mask
) {
140 error
= regmap_write(onkey
->regmap
,
141 config
->onkey_fault_log
,
142 config
->onkey_key_reset_mask
);
144 dev_warn(&onkey
->input
->dev
,
145 "Cannot reset KEY_RESET fault log: %d\n",
148 /* at this point we do any S/W housekeeping
149 * and then send shutdown command
151 dev_dbg(&onkey
->input
->dev
,
152 "Sending SHUTDOWN to DA9063 ...\n");
153 error
= regmap_write(onkey
->regmap
,
154 config
->onkey_shutdown
,
155 config
->onkey_shutdown_mask
);
157 dev_err(&onkey
->input
->dev
,
158 "Cannot SHUTDOWN DA9063: %d\n",
165 schedule_delayed_work(&onkey
->work
, msecs_to_jiffies(50));
168 static irqreturn_t
da9063_onkey_irq_handler(int irq
, void *data
)
170 struct da9063_onkey
*onkey
= data
;
171 const struct da906x_chip_config
*config
= onkey
->config
;
175 error
= regmap_read(onkey
->regmap
,
176 config
->onkey_status
,
178 if (onkey
->key_power
&& !error
&& (val
& config
->onkey_nonkey_mask
)) {
179 input_report_key(onkey
->input
, KEY_POWER
, 1);
180 input_sync(onkey
->input
);
181 schedule_delayed_work(&onkey
->work
, 0);
182 dev_dbg(onkey
->dev
, "KEY_POWER long press.\n");
184 input_report_key(onkey
->input
, KEY_POWER
, 1);
185 input_sync(onkey
->input
);
186 input_report_key(onkey
->input
, KEY_POWER
, 0);
187 input_sync(onkey
->input
);
188 dev_dbg(onkey
->dev
, "KEY_POWER short press.\n");
194 static void da9063_cancel_poll(void *data
)
196 struct da9063_onkey
*onkey
= data
;
198 cancel_delayed_work_sync(&onkey
->work
);
201 static int da9063_onkey_probe(struct platform_device
*pdev
)
203 struct da9063
*da9063
= dev_get_drvdata(pdev
->dev
.parent
);
204 struct da9063_pdata
*pdata
= dev_get_platdata(da9063
->dev
);
205 struct da9063_onkey
*onkey
;
206 const struct of_device_id
*match
;
210 match
= of_match_node(da9063_compatible_reg_id_table
,
215 onkey
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9063_onkey
),
218 dev_err(&pdev
->dev
, "Failed to allocate memory.\n");
222 onkey
->config
= match
->data
;
223 onkey
->dev
= &pdev
->dev
;
225 onkey
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
226 if (!onkey
->regmap
) {
227 dev_err(&pdev
->dev
, "Parent regmap unavailable.\n");
232 onkey
->key_power
= pdata
->key_power
;
235 !of_property_read_bool(pdev
->dev
.of_node
,
236 "dlg,disable-key-power");
238 onkey
->input
= devm_input_allocate_device(&pdev
->dev
);
240 dev_err(&pdev
->dev
, "Failed to allocated input device.\n");
244 onkey
->input
->name
= onkey
->config
->name
;
245 snprintf(onkey
->phys
, sizeof(onkey
->phys
), "%s/input0",
246 onkey
->config
->name
);
247 onkey
->input
->phys
= onkey
->phys
;
248 onkey
->input
->dev
.parent
= &pdev
->dev
;
250 if (onkey
->key_power
)
251 input_set_capability(onkey
->input
, EV_KEY
, KEY_POWER
);
253 input_set_capability(onkey
->input
, EV_KEY
, KEY_SLEEP
);
255 INIT_DELAYED_WORK(&onkey
->work
, da9063_poll_on
);
257 error
= devm_add_action(&pdev
->dev
, da9063_cancel_poll
, onkey
);
260 "Failed to add cancel poll action: %d\n",
265 irq
= platform_get_irq_byname(pdev
, "ONKEY");
268 dev_err(&pdev
->dev
, "Failed to get platform IRQ: %d\n", error
);
272 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
273 NULL
, da9063_onkey_irq_handler
,
274 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
278 "Failed to request IRQ %d: %d\n", irq
, error
);
282 error
= input_register_device(onkey
->input
);
285 "Failed to register input device: %d\n", error
);
289 platform_set_drvdata(pdev
, onkey
);
293 static struct platform_driver da9063_onkey_driver
= {
294 .probe
= da9063_onkey_probe
,
296 .name
= DA9063_DRVNAME_ONKEY
,
297 .of_match_table
= da9063_compatible_reg_id_table
,
300 module_platform_driver(da9063_onkey_driver
);
302 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
303 MODULE_DESCRIPTION("Onkey device driver for Dialog DA9063 and DA9062");
304 MODULE_LICENSE("GPL");
305 MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY
);