2 * OnKey device driver for DA9063, DA9062 and DA9061 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
},
90 MODULE_DEVICE_TABLE(of
, da9063_compatible_reg_id_table
);
92 static void da9063_poll_on(struct work_struct
*work
)
94 struct da9063_onkey
*onkey
= container_of(work
,
97 const struct da906x_chip_config
*config
= onkey
->config
;
103 /* Poll to see when the pin is released */
104 error
= regmap_read(onkey
->regmap
,
105 config
->onkey_status
,
109 "Failed to read ON status: %d\n", error
);
113 if (!(val
& config
->onkey_nonkey_mask
)) {
114 error
= regmap_update_bits(onkey
->regmap
,
115 config
->onkey_pwr_signalling
,
116 config
->onkey_nonkey_lock_mask
,
120 "Failed to reset the Key Delay %d\n", error
);
124 input_report_key(onkey
->input
, KEY_POWER
, 0);
125 input_sync(onkey
->input
);
131 * If the fault log KEY_RESET is detected, then clear it
132 * and shut down the system.
134 error
= regmap_read(onkey
->regmap
,
135 config
->onkey_fault_log
,
138 dev_warn(&onkey
->input
->dev
,
139 "Cannot read FAULT_LOG: %d\n", error
);
140 } else if (fault_log
& config
->onkey_key_reset_mask
) {
141 error
= regmap_write(onkey
->regmap
,
142 config
->onkey_fault_log
,
143 config
->onkey_key_reset_mask
);
145 dev_warn(&onkey
->input
->dev
,
146 "Cannot reset KEY_RESET fault log: %d\n",
149 /* at this point we do any S/W housekeeping
150 * and then send shutdown command
152 dev_dbg(&onkey
->input
->dev
,
153 "Sending SHUTDOWN to PMIC ...\n");
154 error
= regmap_write(onkey
->regmap
,
155 config
->onkey_shutdown
,
156 config
->onkey_shutdown_mask
);
158 dev_err(&onkey
->input
->dev
,
159 "Cannot SHUTDOWN PMIC: %d\n",
166 schedule_delayed_work(&onkey
->work
, msecs_to_jiffies(50));
169 static irqreturn_t
da9063_onkey_irq_handler(int irq
, void *data
)
171 struct da9063_onkey
*onkey
= data
;
172 const struct da906x_chip_config
*config
= onkey
->config
;
176 error
= regmap_read(onkey
->regmap
,
177 config
->onkey_status
,
179 if (onkey
->key_power
&& !error
&& (val
& config
->onkey_nonkey_mask
)) {
180 input_report_key(onkey
->input
, KEY_POWER
, 1);
181 input_sync(onkey
->input
);
182 schedule_delayed_work(&onkey
->work
, 0);
183 dev_dbg(onkey
->dev
, "KEY_POWER long press.\n");
185 input_report_key(onkey
->input
, KEY_POWER
, 1);
186 input_sync(onkey
->input
);
187 input_report_key(onkey
->input
, KEY_POWER
, 0);
188 input_sync(onkey
->input
);
189 dev_dbg(onkey
->dev
, "KEY_POWER short press.\n");
195 static void da9063_cancel_poll(void *data
)
197 struct da9063_onkey
*onkey
= data
;
199 cancel_delayed_work_sync(&onkey
->work
);
202 static int da9063_onkey_probe(struct platform_device
*pdev
)
204 struct da9063
*da9063
= dev_get_drvdata(pdev
->dev
.parent
);
205 struct da9063_pdata
*pdata
= dev_get_platdata(da9063
->dev
);
206 struct da9063_onkey
*onkey
;
207 const struct of_device_id
*match
;
211 match
= of_match_node(da9063_compatible_reg_id_table
,
216 onkey
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9063_onkey
),
219 dev_err(&pdev
->dev
, "Failed to allocate memory.\n");
223 onkey
->config
= match
->data
;
224 onkey
->dev
= &pdev
->dev
;
226 onkey
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
227 if (!onkey
->regmap
) {
228 dev_err(&pdev
->dev
, "Parent regmap unavailable.\n");
233 onkey
->key_power
= pdata
->key_power
;
236 !of_property_read_bool(pdev
->dev
.of_node
,
237 "dlg,disable-key-power");
239 onkey
->input
= devm_input_allocate_device(&pdev
->dev
);
241 dev_err(&pdev
->dev
, "Failed to allocated input device.\n");
245 onkey
->input
->name
= onkey
->config
->name
;
246 snprintf(onkey
->phys
, sizeof(onkey
->phys
), "%s/input0",
247 onkey
->config
->name
);
248 onkey
->input
->phys
= onkey
->phys
;
249 onkey
->input
->dev
.parent
= &pdev
->dev
;
251 if (onkey
->key_power
)
252 input_set_capability(onkey
->input
, EV_KEY
, KEY_POWER
);
254 input_set_capability(onkey
->input
, EV_KEY
, KEY_SLEEP
);
256 INIT_DELAYED_WORK(&onkey
->work
, da9063_poll_on
);
258 error
= devm_add_action(&pdev
->dev
, da9063_cancel_poll
, onkey
);
261 "Failed to add cancel poll action: %d\n",
266 irq
= platform_get_irq_byname(pdev
, "ONKEY");
269 dev_err(&pdev
->dev
, "Failed to get platform IRQ: %d\n", error
);
273 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
274 NULL
, da9063_onkey_irq_handler
,
275 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
279 "Failed to request IRQ %d: %d\n", irq
, error
);
283 error
= input_register_device(onkey
->input
);
286 "Failed to register input device: %d\n", error
);
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, DA9062 and DA9061");
304 MODULE_LICENSE("GPL");
305 MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY
);