1 /* 2 * OnKey device driver for DA9063, DA9062 and DA9061 PMICs 3 * Copyright (C) 2015 Dialog Semiconductor Ltd. 4 * 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. 9 * 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. 14 */ 15 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> 23 #include <linux/of.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> 29 30 struct da906x_chip_config { 31 /* REGS */ 32 int onkey_status; 33 int onkey_pwr_signalling; 34 int onkey_fault_log; 35 int onkey_shutdown; 36 /* MASKS */ 37 int onkey_nonkey_mask; 38 int onkey_nonkey_lock_mask; 39 int onkey_key_reset_mask; 40 int onkey_shutdown_mask; 41 /* NAMES */ 42 const char *name; 43 }; 44 45 struct da9063_onkey { 46 struct delayed_work work; 47 struct input_dev *input; 48 struct device *dev; 49 struct regmap *regmap; 50 const struct da906x_chip_config *config; 51 char phys[32]; 52 bool key_power; 53 }; 54 55 static const struct da906x_chip_config da9063_regs = { 56 /* 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, 61 /* MASKS */ 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, 66 /* NAMES */ 67 .name = DA9063_DRVNAME_ONKEY, 68 }; 69 70 static const struct da906x_chip_config da9062_regs = { 71 /* 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, 76 /* MASKS */ 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, 81 /* NAMES */ 82 .name = "da9062-onkey", 83 }; 84 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 }, 88 { }, 89 }; 90 MODULE_DEVICE_TABLE(of, da9063_compatible_reg_id_table); 91 92 static void da9063_poll_on(struct work_struct *work) 93 { 94 struct da9063_onkey *onkey = container_of(work, 95 struct da9063_onkey, 96 work.work); 97 const struct da906x_chip_config *config = onkey->config; 98 unsigned int val; 99 int fault_log = 0; 100 bool poll = true; 101 int error; 102 103 /* Poll to see when the pin is released */ 104 error = regmap_read(onkey->regmap, 105 config->onkey_status, 106 &val); 107 if (error) { 108 dev_err(onkey->dev, 109 "Failed to read ON status: %d\n", error); 110 goto err_poll; 111 } 112 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, 117 0); 118 if (error) { 119 dev_err(onkey->dev, 120 "Failed to reset the Key Delay %d\n", error); 121 goto err_poll; 122 } 123 124 input_report_key(onkey->input, KEY_POWER, 0); 125 input_sync(onkey->input); 126 127 poll = false; 128 } 129 130 /* 131 * If the fault log KEY_RESET is detected, then clear it 132 * and shut down the system. 133 */ 134 error = regmap_read(onkey->regmap, 135 config->onkey_fault_log, 136 &fault_log); 137 if (error) { 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); 144 if (error) { 145 dev_warn(&onkey->input->dev, 146 "Cannot reset KEY_RESET fault log: %d\n", 147 error); 148 } else { 149 /* at this point we do any S/W housekeeping 150 * and then send shutdown command 151 */ 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); 157 if (error) 158 dev_err(&onkey->input->dev, 159 "Cannot SHUTDOWN PMIC: %d\n", 160 error); 161 } 162 } 163 164 err_poll: 165 if (poll) 166 schedule_delayed_work(&onkey->work, msecs_to_jiffies(50)); 167 } 168 169 static irqreturn_t da9063_onkey_irq_handler(int irq, void *data) 170 { 171 struct da9063_onkey *onkey = data; 172 const struct da906x_chip_config *config = onkey->config; 173 unsigned int val; 174 int error; 175 176 error = regmap_read(onkey->regmap, 177 config->onkey_status, 178 &val); 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"); 184 } else { 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"); 190 } 191 192 return IRQ_HANDLED; 193 } 194 195 static void da9063_cancel_poll(void *data) 196 { 197 struct da9063_onkey *onkey = data; 198 199 cancel_delayed_work_sync(&onkey->work); 200 } 201 202 static int da9063_onkey_probe(struct platform_device *pdev) 203 { 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; 208 int irq; 209 int error; 210 211 match = of_match_node(da9063_compatible_reg_id_table, 212 pdev->dev.of_node); 213 if (!match) 214 return -ENXIO; 215 216 onkey = devm_kzalloc(&pdev->dev, sizeof(struct da9063_onkey), 217 GFP_KERNEL); 218 if (!onkey) { 219 dev_err(&pdev->dev, "Failed to allocate memory.\n"); 220 return -ENOMEM; 221 } 222 223 onkey->config = match->data; 224 onkey->dev = &pdev->dev; 225 226 onkey->regmap = dev_get_regmap(pdev->dev.parent, NULL); 227 if (!onkey->regmap) { 228 dev_err(&pdev->dev, "Parent regmap unavailable.\n"); 229 return -ENXIO; 230 } 231 232 if (pdata) 233 onkey->key_power = pdata->key_power; 234 else 235 onkey->key_power = 236 !of_property_read_bool(pdev->dev.of_node, 237 "dlg,disable-key-power"); 238 239 onkey->input = devm_input_allocate_device(&pdev->dev); 240 if (!onkey->input) { 241 dev_err(&pdev->dev, "Failed to allocated input device.\n"); 242 return -ENOMEM; 243 } 244 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; 250 251 if (onkey->key_power) 252 input_set_capability(onkey->input, EV_KEY, KEY_POWER); 253 254 input_set_capability(onkey->input, EV_KEY, KEY_SLEEP); 255 256 INIT_DELAYED_WORK(&onkey->work, da9063_poll_on); 257 258 error = devm_add_action(&pdev->dev, da9063_cancel_poll, onkey); 259 if (error) { 260 dev_err(&pdev->dev, 261 "Failed to add cancel poll action: %d\n", 262 error); 263 return error; 264 } 265 266 irq = platform_get_irq_byname(pdev, "ONKEY"); 267 if (irq < 0) { 268 error = irq; 269 dev_err(&pdev->dev, "Failed to get platform IRQ: %d\n", error); 270 return error; 271 } 272 273 error = devm_request_threaded_irq(&pdev->dev, irq, 274 NULL, da9063_onkey_irq_handler, 275 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 276 "ONKEY", onkey); 277 if (error) { 278 dev_err(&pdev->dev, 279 "Failed to request IRQ %d: %d\n", irq, error); 280 return error; 281 } 282 283 error = input_register_device(onkey->input); 284 if (error) { 285 dev_err(&pdev->dev, 286 "Failed to register input device: %d\n", error); 287 return error; 288 } 289 290 return 0; 291 } 292 293 static struct platform_driver da9063_onkey_driver = { 294 .probe = da9063_onkey_probe, 295 .driver = { 296 .name = DA9063_DRVNAME_ONKEY, 297 .of_match_table = da9063_compatible_reg_id_table, 298 }, 299 }; 300 module_platform_driver(da9063_onkey_driver); 301 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); 306