da9063_onkey.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * OnKey device driver for DA9063 and DA9062 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. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/input.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/regmap.h>
  22. #include <linux/of.h>
  23. #include <linux/mfd/da9063/core.h>
  24. #include <linux/mfd/da9063/pdata.h>
  25. #include <linux/mfd/da9063/registers.h>
  26. #include <linux/mfd/da9062/core.h>
  27. #include <linux/mfd/da9062/registers.h>
  28. struct da906x_chip_config {
  29. /* REGS */
  30. int onkey_status;
  31. int onkey_pwr_signalling;
  32. int onkey_fault_log;
  33. int onkey_shutdown;
  34. /* MASKS */
  35. int onkey_nonkey_mask;
  36. int onkey_nonkey_lock_mask;
  37. int onkey_key_reset_mask;
  38. int onkey_shutdown_mask;
  39. /* NAMES */
  40. const char *name;
  41. };
  42. struct da9063_onkey {
  43. struct delayed_work work;
  44. struct input_dev *input;
  45. struct device *dev;
  46. struct regmap *regmap;
  47. const struct da906x_chip_config *config;
  48. char phys[32];
  49. bool key_power;
  50. };
  51. static const struct da906x_chip_config da9063_regs = {
  52. /* REGS */
  53. .onkey_status = DA9063_REG_STATUS_A,
  54. .onkey_pwr_signalling = DA9063_REG_CONTROL_B,
  55. .onkey_fault_log = DA9063_REG_FAULT_LOG,
  56. .onkey_shutdown = DA9063_REG_CONTROL_F,
  57. /* MASKS */
  58. .onkey_nonkey_mask = DA9063_NONKEY,
  59. .onkey_nonkey_lock_mask = DA9063_NONKEY_LOCK,
  60. .onkey_key_reset_mask = DA9063_KEY_RESET,
  61. .onkey_shutdown_mask = DA9063_SHUTDOWN,
  62. /* NAMES */
  63. .name = DA9063_DRVNAME_ONKEY,
  64. };
  65. static const struct da906x_chip_config da9062_regs = {
  66. /* REGS */
  67. .onkey_status = DA9062AA_STATUS_A,
  68. .onkey_pwr_signalling = DA9062AA_CONTROL_B,
  69. .onkey_fault_log = DA9062AA_FAULT_LOG,
  70. .onkey_shutdown = DA9062AA_CONTROL_F,
  71. /* MASKS */
  72. .onkey_nonkey_mask = DA9062AA_NONKEY_MASK,
  73. .onkey_nonkey_lock_mask = DA9062AA_NONKEY_LOCK_MASK,
  74. .onkey_key_reset_mask = DA9062AA_KEY_RESET_MASK,
  75. .onkey_shutdown_mask = DA9062AA_SHUTDOWN_MASK,
  76. /* NAMES */
  77. .name = "da9062-onkey",
  78. };
  79. static const struct of_device_id da9063_compatible_reg_id_table[] = {
  80. { .compatible = "dlg,da9063-onkey", .data = &da9063_regs },
  81. { .compatible = "dlg,da9062-onkey", .data = &da9062_regs },
  82. { },
  83. };
  84. static void da9063_poll_on(struct work_struct *work)
  85. {
  86. struct da9063_onkey *onkey = container_of(work,
  87. struct da9063_onkey,
  88. work.work);
  89. const struct da906x_chip_config *config = onkey->config;
  90. unsigned int val;
  91. int fault_log = 0;
  92. bool poll = true;
  93. int error;
  94. /* Poll to see when the pin is released */
  95. error = regmap_read(onkey->regmap,
  96. config->onkey_status,
  97. &val);
  98. if (error) {
  99. dev_err(onkey->dev,
  100. "Failed to read ON status: %d\n", error);
  101. goto err_poll;
  102. }
  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,
  107. 0);
  108. if (error) {
  109. dev_err(onkey->dev,
  110. "Failed to reset the Key Delay %d\n", error);
  111. goto err_poll;
  112. }
  113. input_report_key(onkey->input, KEY_POWER, 0);
  114. input_sync(onkey->input);
  115. poll = false;
  116. }
  117. /*
  118. * If the fault log KEY_RESET is detected, then clear it
  119. * and shut down the system.
  120. */
  121. error = regmap_read(onkey->regmap,
  122. config->onkey_fault_log,
  123. &fault_log);
  124. if (error) {
  125. dev_warn(&onkey->input->dev,
  126. "Cannot read FAULT_LOG: %d\n", error);
  127. } else if (fault_log & config->onkey_key_reset_mask) {
  128. error = regmap_write(onkey->regmap,
  129. config->onkey_fault_log,
  130. config->onkey_key_reset_mask);
  131. if (error) {
  132. dev_warn(&onkey->input->dev,
  133. "Cannot reset KEY_RESET fault log: %d\n",
  134. error);
  135. } else {
  136. /* at this point we do any S/W housekeeping
  137. * and then send shutdown command
  138. */
  139. dev_dbg(&onkey->input->dev,
  140. "Sending SHUTDOWN to DA9063 ...\n");
  141. error = regmap_write(onkey->regmap,
  142. config->onkey_shutdown,
  143. config->onkey_shutdown_mask);
  144. if (error)
  145. dev_err(&onkey->input->dev,
  146. "Cannot SHUTDOWN DA9063: %d\n",
  147. error);
  148. }
  149. }
  150. err_poll:
  151. if (poll)
  152. schedule_delayed_work(&onkey->work, msecs_to_jiffies(50));
  153. }
  154. static irqreturn_t da9063_onkey_irq_handler(int irq, void *data)
  155. {
  156. struct da9063_onkey *onkey = data;
  157. const struct da906x_chip_config *config = onkey->config;
  158. unsigned int val;
  159. int error;
  160. error = regmap_read(onkey->regmap,
  161. config->onkey_status,
  162. &val);
  163. if (onkey->key_power && !error && (val & config->onkey_nonkey_mask)) {
  164. input_report_key(onkey->input, KEY_POWER, 1);
  165. input_sync(onkey->input);
  166. schedule_delayed_work(&onkey->work, 0);
  167. dev_dbg(onkey->dev, "KEY_POWER pressed.\n");
  168. } else {
  169. input_report_key(onkey->input, KEY_SLEEP, 1);
  170. input_sync(onkey->input);
  171. input_report_key(onkey->input, KEY_SLEEP, 0);
  172. input_sync(onkey->input);
  173. dev_dbg(onkey->dev, "KEY_SLEEP pressed.\n");
  174. }
  175. return IRQ_HANDLED;
  176. }
  177. static void da9063_cancel_poll(void *data)
  178. {
  179. struct da9063_onkey *onkey = data;
  180. cancel_delayed_work_sync(&onkey->work);
  181. }
  182. static int da9063_onkey_probe(struct platform_device *pdev)
  183. {
  184. struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
  185. struct da9063_pdata *pdata = dev_get_platdata(da9063->dev);
  186. struct da9063_onkey *onkey;
  187. const struct of_device_id *match;
  188. int irq;
  189. int error;
  190. match = of_match_node(da9063_compatible_reg_id_table,
  191. pdev->dev.of_node);
  192. if (!match)
  193. return -ENXIO;
  194. onkey = devm_kzalloc(&pdev->dev, sizeof(struct da9063_onkey),
  195. GFP_KERNEL);
  196. if (!onkey) {
  197. dev_err(&pdev->dev, "Failed to allocate memory.\n");
  198. return -ENOMEM;
  199. }
  200. onkey->config = match->data;
  201. onkey->dev = &pdev->dev;
  202. onkey->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  203. if (!onkey->regmap) {
  204. dev_err(&pdev->dev, "Parent regmap unavailable.\n");
  205. return -ENXIO;
  206. }
  207. if (pdata)
  208. onkey->key_power = pdata->key_power;
  209. else
  210. onkey->key_power =
  211. !of_property_read_bool(pdev->dev.of_node,
  212. "dlg,disable-key-power");
  213. onkey->input = devm_input_allocate_device(&pdev->dev);
  214. if (!onkey->input) {
  215. dev_err(&pdev->dev, "Failed to allocated input device.\n");
  216. return -ENOMEM;
  217. }
  218. onkey->input->name = onkey->config->name;
  219. snprintf(onkey->phys, sizeof(onkey->phys), "%s/input0",
  220. onkey->config->name);
  221. onkey->input->phys = onkey->phys;
  222. onkey->input->dev.parent = &pdev->dev;
  223. if (onkey->key_power)
  224. input_set_capability(onkey->input, EV_KEY, KEY_POWER);
  225. input_set_capability(onkey->input, EV_KEY, KEY_SLEEP);
  226. INIT_DELAYED_WORK(&onkey->work, da9063_poll_on);
  227. error = devm_add_action(&pdev->dev, da9063_cancel_poll, onkey);
  228. if (error) {
  229. dev_err(&pdev->dev,
  230. "Failed to add cancel poll action: %d\n",
  231. error);
  232. return error;
  233. }
  234. irq = platform_get_irq_byname(pdev, "ONKEY");
  235. if (irq < 0) {
  236. error = irq;
  237. dev_err(&pdev->dev, "Failed to get platform IRQ: %d\n", error);
  238. return error;
  239. }
  240. error = devm_request_threaded_irq(&pdev->dev, irq,
  241. NULL, da9063_onkey_irq_handler,
  242. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  243. "ONKEY", onkey);
  244. if (error) {
  245. dev_err(&pdev->dev,
  246. "Failed to request IRQ %d: %d\n", irq, error);
  247. return error;
  248. }
  249. error = input_register_device(onkey->input);
  250. if (error) {
  251. dev_err(&pdev->dev,
  252. "Failed to register input device: %d\n", error);
  253. return error;
  254. }
  255. platform_set_drvdata(pdev, onkey);
  256. return 0;
  257. }
  258. static struct platform_driver da9063_onkey_driver = {
  259. .probe = da9063_onkey_probe,
  260. .driver = {
  261. .name = DA9063_DRVNAME_ONKEY,
  262. .of_match_table = da9063_compatible_reg_id_table,
  263. },
  264. };
  265. module_platform_driver(da9063_onkey_driver);
  266. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  267. MODULE_DESCRIPTION("Onkey device driver for Dialog DA9063 and DA9062");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS("platform:" DA9063_DRVNAME_ONKEY);