axp20x-pek.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * axp20x power button driver.
  3. *
  4. * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file "COPYING" in the main directory of this
  8. * archive for more details.
  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/acpi.h>
  16. #include <linux/errno.h>
  17. #include <linux/irq.h>
  18. #include <linux/init.h>
  19. #include <linux/input.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mfd/axp20x.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_data/x86/soc.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/slab.h>
  28. #define AXP20X_PEK_STARTUP_MASK (0xc0)
  29. #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
  30. struct axp20x_info {
  31. const struct axp20x_time *startup_time;
  32. unsigned int startup_mask;
  33. const struct axp20x_time *shutdown_time;
  34. unsigned int shutdown_mask;
  35. };
  36. struct axp20x_pek {
  37. struct axp20x_dev *axp20x;
  38. struct input_dev *input;
  39. struct axp20x_info *info;
  40. int irq_dbr;
  41. int irq_dbf;
  42. };
  43. struct axp20x_time {
  44. unsigned int time;
  45. unsigned int idx;
  46. };
  47. static const struct axp20x_time startup_time[] = {
  48. { .time = 128, .idx = 0 },
  49. { .time = 1000, .idx = 2 },
  50. { .time = 3000, .idx = 1 },
  51. { .time = 2000, .idx = 3 },
  52. };
  53. static const struct axp20x_time axp221_startup_time[] = {
  54. { .time = 128, .idx = 0 },
  55. { .time = 1000, .idx = 1 },
  56. { .time = 2000, .idx = 2 },
  57. { .time = 3000, .idx = 3 },
  58. };
  59. static const struct axp20x_time shutdown_time[] = {
  60. { .time = 4000, .idx = 0 },
  61. { .time = 6000, .idx = 1 },
  62. { .time = 8000, .idx = 2 },
  63. { .time = 10000, .idx = 3 },
  64. };
  65. static const struct axp20x_info axp20x_info = {
  66. .startup_time = startup_time,
  67. .startup_mask = AXP20X_PEK_STARTUP_MASK,
  68. .shutdown_time = shutdown_time,
  69. .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
  70. };
  71. static const struct axp20x_info axp221_info = {
  72. .startup_time = axp221_startup_time,
  73. .startup_mask = AXP20X_PEK_STARTUP_MASK,
  74. .shutdown_time = shutdown_time,
  75. .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
  76. };
  77. static ssize_t axp20x_show_attr(struct device *dev,
  78. const struct axp20x_time *time,
  79. unsigned int mask, char *buf)
  80. {
  81. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  82. unsigned int val;
  83. int ret, i;
  84. ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
  85. if (ret != 0)
  86. return ret;
  87. val &= mask;
  88. val >>= ffs(mask) - 1;
  89. for (i = 0; i < 4; i++)
  90. if (val == time[i].idx)
  91. val = time[i].time;
  92. return sprintf(buf, "%u\n", val);
  93. }
  94. static ssize_t axp20x_show_attr_startup(struct device *dev,
  95. struct device_attribute *attr,
  96. char *buf)
  97. {
  98. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  99. return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
  100. axp20x_pek->info->startup_mask, buf);
  101. }
  102. static ssize_t axp20x_show_attr_shutdown(struct device *dev,
  103. struct device_attribute *attr,
  104. char *buf)
  105. {
  106. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  107. return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
  108. axp20x_pek->info->shutdown_mask, buf);
  109. }
  110. static ssize_t axp20x_store_attr(struct device *dev,
  111. const struct axp20x_time *time,
  112. unsigned int mask, const char *buf,
  113. size_t count)
  114. {
  115. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  116. int ret, i;
  117. unsigned int val, idx = 0;
  118. unsigned int best_err = UINT_MAX;
  119. ret = kstrtouint(buf, 10, &val);
  120. if (ret)
  121. return ret;
  122. for (i = 3; i >= 0; i--) {
  123. unsigned int err;
  124. err = abs(time[i].time - val);
  125. if (err < best_err) {
  126. best_err = err;
  127. idx = time[i].idx;
  128. }
  129. if (!err)
  130. break;
  131. }
  132. idx <<= ffs(mask) - 1;
  133. ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
  134. mask, idx);
  135. if (ret != 0)
  136. return -EINVAL;
  137. return count;
  138. }
  139. static ssize_t axp20x_store_attr_startup(struct device *dev,
  140. struct device_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  144. return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
  145. axp20x_pek->info->startup_mask, buf, count);
  146. }
  147. static ssize_t axp20x_store_attr_shutdown(struct device *dev,
  148. struct device_attribute *attr,
  149. const char *buf, size_t count)
  150. {
  151. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  152. return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
  153. axp20x_pek->info->shutdown_mask, buf, count);
  154. }
  155. static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
  156. axp20x_store_attr_startup);
  157. static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
  158. axp20x_store_attr_shutdown);
  159. static struct attribute *axp20x_attrs[] = {
  160. &dev_attr_startup.attr,
  161. &dev_attr_shutdown.attr,
  162. NULL,
  163. };
  164. ATTRIBUTE_GROUPS(axp20x);
  165. static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
  166. {
  167. struct input_dev *idev = pwr;
  168. struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
  169. /*
  170. * The power-button is connected to ground so a falling edge (dbf)
  171. * means it is pressed.
  172. */
  173. if (irq == axp20x_pek->irq_dbf)
  174. input_report_key(idev, KEY_POWER, true);
  175. else if (irq == axp20x_pek->irq_dbr)
  176. input_report_key(idev, KEY_POWER, false);
  177. input_sync(idev);
  178. return IRQ_HANDLED;
  179. }
  180. static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
  181. struct platform_device *pdev)
  182. {
  183. struct axp20x_dev *axp20x = axp20x_pek->axp20x;
  184. struct input_dev *idev;
  185. int error;
  186. axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  187. if (axp20x_pek->irq_dbr < 0)
  188. return axp20x_pek->irq_dbr;
  189. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  190. axp20x_pek->irq_dbr);
  191. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  192. if (axp20x_pek->irq_dbf < 0)
  193. return axp20x_pek->irq_dbf;
  194. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  195. axp20x_pek->irq_dbf);
  196. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  197. if (!axp20x_pek->input)
  198. return -ENOMEM;
  199. idev = axp20x_pek->input;
  200. idev->name = "axp20x-pek";
  201. idev->phys = "m1kbd/input2";
  202. idev->dev.parent = &pdev->dev;
  203. input_set_capability(idev, EV_KEY, KEY_POWER);
  204. input_set_drvdata(idev, axp20x_pek);
  205. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  206. axp20x_pek_irq, 0,
  207. "axp20x-pek-dbr", idev);
  208. if (error < 0) {
  209. dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
  210. axp20x_pek->irq_dbr, error);
  211. return error;
  212. }
  213. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  214. axp20x_pek_irq, 0,
  215. "axp20x-pek-dbf", idev);
  216. if (error < 0) {
  217. dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
  218. axp20x_pek->irq_dbf, error);
  219. return error;
  220. }
  221. error = input_register_device(idev);
  222. if (error) {
  223. dev_err(&pdev->dev, "Can't register input device: %d\n",
  224. error);
  225. return error;
  226. }
  227. device_init_wakeup(&pdev->dev, true);
  228. return 0;
  229. }
  230. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek)
  231. {
  232. if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
  233. axp20x_pek->axp20x->variant == AXP288_ID) {
  234. /*
  235. * On Cherry Trail platforms (hrv == 3), do not register the
  236. * input device if there is an "INTCFD9" or "ACPI0011" gpio
  237. * button ACPI device, as that handles the power button too,
  238. * and otherwise we end up reporting all presses twice.
  239. */
  240. if (soc_intel_is_cht() &&
  241. (acpi_dev_present("INTCFD9", NULL, -1) ||
  242. acpi_dev_present("ACPI0011", NULL, -1)))
  243. return false;
  244. }
  245. return true;
  246. }
  247. static int axp20x_pek_probe(struct platform_device *pdev)
  248. {
  249. struct axp20x_pek *axp20x_pek;
  250. const struct platform_device_id *match = platform_get_device_id(pdev);
  251. int error;
  252. if (!match) {
  253. dev_err(&pdev->dev, "Failed to get platform_device_id\n");
  254. return -EINVAL;
  255. }
  256. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  257. GFP_KERNEL);
  258. if (!axp20x_pek)
  259. return -ENOMEM;
  260. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  261. if (axp20x_pek_should_register_input(axp20x_pek)) {
  262. error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
  263. if (error)
  264. return error;
  265. }
  266. axp20x_pek->info = (struct axp20x_info *)match->driver_data;
  267. platform_set_drvdata(pdev, axp20x_pek);
  268. return 0;
  269. }
  270. static int axp20x_pek_suspend(struct device *dev)
  271. {
  272. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  273. /*
  274. * As nested threaded IRQs are not automatically disabled during
  275. * suspend, we must explicitly disable non-wakeup IRQs.
  276. */
  277. if (device_may_wakeup(dev)) {
  278. enable_irq_wake(axp20x_pek->irq_dbf);
  279. enable_irq_wake(axp20x_pek->irq_dbr);
  280. } else {
  281. disable_irq(axp20x_pek->irq_dbf);
  282. disable_irq(axp20x_pek->irq_dbr);
  283. }
  284. return 0;
  285. }
  286. static int axp20x_pek_resume(struct device *dev)
  287. {
  288. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  289. if (device_may_wakeup(dev)) {
  290. disable_irq_wake(axp20x_pek->irq_dbf);
  291. disable_irq_wake(axp20x_pek->irq_dbr);
  292. } else {
  293. enable_irq(axp20x_pek->irq_dbf);
  294. enable_irq(axp20x_pek->irq_dbr);
  295. }
  296. return 0;
  297. }
  298. static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
  299. {
  300. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  301. if (axp20x_pek->axp20x->variant != AXP288_ID)
  302. return 0;
  303. /*
  304. * Clear interrupts from button presses during suspend, to avoid
  305. * a wakeup power-button press getting reported to userspace.
  306. */
  307. regmap_write(axp20x_pek->axp20x->regmap,
  308. AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
  309. BIT(AXP288_IRQ_POKN % 8));
  310. return 0;
  311. }
  312. static const struct dev_pm_ops axp20x_pek_pm_ops = {
  313. SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
  314. .resume_noirq = pm_sleep_ptr(axp20x_pek_resume_noirq),
  315. };
  316. static const struct platform_device_id axp_pek_id_match[] = {
  317. {
  318. .name = "axp20x-pek",
  319. .driver_data = (kernel_ulong_t)&axp20x_info,
  320. },
  321. {
  322. .name = "axp221-pek",
  323. .driver_data = (kernel_ulong_t)&axp221_info,
  324. },
  325. { /* sentinel */ }
  326. };
  327. MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
  328. static struct platform_driver axp20x_pek_driver = {
  329. .probe = axp20x_pek_probe,
  330. .id_table = axp_pek_id_match,
  331. .driver = {
  332. .name = "axp20x-pek",
  333. .pm = pm_sleep_ptr(&axp20x_pek_pm_ops),
  334. .dev_groups = axp20x_groups,
  335. },
  336. };
  337. module_platform_driver(axp20x_pek_driver);
  338. MODULE_DESCRIPTION("axp20x Power Button");
  339. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  340. MODULE_LICENSE("GPL");