axp20x-pek.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * axp20x power button driver.
  3. *
  4. * Copyright (C) 2013 Carlo Caione <[email protected]>
  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. char val_str[20];
  117. size_t len;
  118. int ret, i;
  119. unsigned int val, idx = 0;
  120. unsigned int best_err = UINT_MAX;
  121. val_str[sizeof(val_str) - 1] = '\0';
  122. strncpy(val_str, buf, sizeof(val_str) - 1);
  123. len = strlen(val_str);
  124. if (len && val_str[len - 1] == '\n')
  125. val_str[len - 1] = '\0';
  126. ret = kstrtouint(val_str, 10, &val);
  127. if (ret)
  128. return ret;
  129. for (i = 3; i >= 0; i--) {
  130. unsigned int err;
  131. err = abs(time[i].time - val);
  132. if (err < best_err) {
  133. best_err = err;
  134. idx = time[i].idx;
  135. }
  136. if (!err)
  137. break;
  138. }
  139. idx <<= ffs(mask) - 1;
  140. ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
  141. mask, idx);
  142. if (ret != 0)
  143. return -EINVAL;
  144. return count;
  145. }
  146. static ssize_t axp20x_store_attr_startup(struct device *dev,
  147. struct device_attribute *attr,
  148. const char *buf, size_t count)
  149. {
  150. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  151. return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
  152. axp20x_pek->info->startup_mask, buf, count);
  153. }
  154. static ssize_t axp20x_store_attr_shutdown(struct device *dev,
  155. struct device_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  159. return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
  160. axp20x_pek->info->shutdown_mask, buf, count);
  161. }
  162. static DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup,
  163. axp20x_store_attr_startup);
  164. static DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
  165. axp20x_store_attr_shutdown);
  166. static struct attribute *axp20x_attrs[] = {
  167. &dev_attr_startup.attr,
  168. &dev_attr_shutdown.attr,
  169. NULL,
  170. };
  171. ATTRIBUTE_GROUPS(axp20x);
  172. static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
  173. {
  174. struct input_dev *idev = pwr;
  175. struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
  176. /*
  177. * The power-button is connected to ground so a falling edge (dbf)
  178. * means it is pressed.
  179. */
  180. if (irq == axp20x_pek->irq_dbf)
  181. input_report_key(idev, KEY_POWER, true);
  182. else if (irq == axp20x_pek->irq_dbr)
  183. input_report_key(idev, KEY_POWER, false);
  184. input_sync(idev);
  185. return IRQ_HANDLED;
  186. }
  187. static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
  188. struct platform_device *pdev)
  189. {
  190. struct axp20x_dev *axp20x = axp20x_pek->axp20x;
  191. struct input_dev *idev;
  192. int error;
  193. axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  194. if (axp20x_pek->irq_dbr < 0)
  195. return axp20x_pek->irq_dbr;
  196. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  197. axp20x_pek->irq_dbr);
  198. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  199. if (axp20x_pek->irq_dbf < 0)
  200. return axp20x_pek->irq_dbf;
  201. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  202. axp20x_pek->irq_dbf);
  203. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  204. if (!axp20x_pek->input)
  205. return -ENOMEM;
  206. idev = axp20x_pek->input;
  207. idev->name = "axp20x-pek";
  208. idev->phys = "m1kbd/input2";
  209. idev->dev.parent = &pdev->dev;
  210. input_set_capability(idev, EV_KEY, KEY_POWER);
  211. input_set_drvdata(idev, axp20x_pek);
  212. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  213. axp20x_pek_irq, 0,
  214. "axp20x-pek-dbr", idev);
  215. if (error < 0) {
  216. dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
  217. axp20x_pek->irq_dbr, error);
  218. return error;
  219. }
  220. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  221. axp20x_pek_irq, 0,
  222. "axp20x-pek-dbf", idev);
  223. if (error < 0) {
  224. dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
  225. axp20x_pek->irq_dbf, error);
  226. return error;
  227. }
  228. error = input_register_device(idev);
  229. if (error) {
  230. dev_err(&pdev->dev, "Can't register input device: %d\n",
  231. error);
  232. return error;
  233. }
  234. device_init_wakeup(&pdev->dev, true);
  235. return 0;
  236. }
  237. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek)
  238. {
  239. if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
  240. axp20x_pek->axp20x->variant == AXP288_ID) {
  241. /*
  242. * On Cherry Trail platforms (hrv == 3), do not register the
  243. * input device if there is an "INTCFD9" or "ACPI0011" gpio
  244. * button ACPI device, as that handles the power button too,
  245. * and otherwise we end up reporting all presses twice.
  246. */
  247. if (soc_intel_is_cht() &&
  248. (acpi_dev_present("INTCFD9", NULL, -1) ||
  249. acpi_dev_present("ACPI0011", NULL, -1)))
  250. return false;
  251. }
  252. return true;
  253. }
  254. static int axp20x_pek_probe(struct platform_device *pdev)
  255. {
  256. struct axp20x_pek *axp20x_pek;
  257. const struct platform_device_id *match = platform_get_device_id(pdev);
  258. int error;
  259. if (!match) {
  260. dev_err(&pdev->dev, "Failed to get platform_device_id\n");
  261. return -EINVAL;
  262. }
  263. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  264. GFP_KERNEL);
  265. if (!axp20x_pek)
  266. return -ENOMEM;
  267. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  268. if (axp20x_pek_should_register_input(axp20x_pek)) {
  269. error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
  270. if (error)
  271. return error;
  272. }
  273. axp20x_pek->info = (struct axp20x_info *)match->driver_data;
  274. platform_set_drvdata(pdev, axp20x_pek);
  275. return 0;
  276. }
  277. static int __maybe_unused axp20x_pek_suspend(struct device *dev)
  278. {
  279. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  280. /*
  281. * As nested threaded IRQs are not automatically disabled during
  282. * suspend, we must explicitly disable non-wakeup IRQs.
  283. */
  284. if (device_may_wakeup(dev)) {
  285. enable_irq_wake(axp20x_pek->irq_dbf);
  286. enable_irq_wake(axp20x_pek->irq_dbr);
  287. } else {
  288. disable_irq(axp20x_pek->irq_dbf);
  289. disable_irq(axp20x_pek->irq_dbr);
  290. }
  291. return 0;
  292. }
  293. static int __maybe_unused axp20x_pek_resume(struct device *dev)
  294. {
  295. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  296. if (device_may_wakeup(dev)) {
  297. disable_irq_wake(axp20x_pek->irq_dbf);
  298. disable_irq_wake(axp20x_pek->irq_dbr);
  299. } else {
  300. enable_irq(axp20x_pek->irq_dbf);
  301. enable_irq(axp20x_pek->irq_dbr);
  302. }
  303. return 0;
  304. }
  305. static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
  306. {
  307. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  308. if (axp20x_pek->axp20x->variant != AXP288_ID)
  309. return 0;
  310. /*
  311. * Clear interrupts from button presses during suspend, to avoid
  312. * a wakeup power-button press getting reported to userspace.
  313. */
  314. regmap_write(axp20x_pek->axp20x->regmap,
  315. AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
  316. BIT(AXP288_IRQ_POKN % 8));
  317. return 0;
  318. }
  319. static const struct dev_pm_ops axp20x_pek_pm_ops = {
  320. SET_SYSTEM_SLEEP_PM_OPS(axp20x_pek_suspend, axp20x_pek_resume)
  321. #ifdef CONFIG_PM_SLEEP
  322. .resume_noirq = axp20x_pek_resume_noirq,
  323. #endif
  324. };
  325. static const struct platform_device_id axp_pek_id_match[] = {
  326. {
  327. .name = "axp20x-pek",
  328. .driver_data = (kernel_ulong_t)&axp20x_info,
  329. },
  330. {
  331. .name = "axp221-pek",
  332. .driver_data = (kernel_ulong_t)&axp221_info,
  333. },
  334. { /* sentinel */ }
  335. };
  336. MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
  337. static struct platform_driver axp20x_pek_driver = {
  338. .probe = axp20x_pek_probe,
  339. .id_table = axp_pek_id_match,
  340. .driver = {
  341. .name = "axp20x-pek",
  342. .pm = &axp20x_pek_pm_ops,
  343. .dev_groups = axp20x_groups,
  344. },
  345. };
  346. module_platform_driver(axp20x_pek_driver);
  347. MODULE_DESCRIPTION("axp20x Power Button");
  348. MODULE_AUTHOR("Carlo Caione <[email protected]>");
  349. MODULE_LICENSE("GPL");