axp20x-pek.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. #define AXP20X_PEK_STARTUP_MASK (0xc0)
  28. #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
  29. struct axp20x_info {
  30. const struct axp20x_time *startup_time;
  31. unsigned int startup_mask;
  32. const struct axp20x_time *shutdown_time;
  33. unsigned int shutdown_mask;
  34. };
  35. struct axp20x_pek {
  36. struct axp20x_dev *axp20x;
  37. struct input_dev *input;
  38. struct axp20x_info *info;
  39. int irq_dbr;
  40. int irq_dbf;
  41. };
  42. struct axp20x_time {
  43. unsigned int time;
  44. unsigned int idx;
  45. };
  46. static const struct axp20x_time startup_time[] = {
  47. { .time = 128, .idx = 0 },
  48. { .time = 1000, .idx = 2 },
  49. { .time = 3000, .idx = 1 },
  50. { .time = 2000, .idx = 3 },
  51. };
  52. static const struct axp20x_time axp221_startup_time[] = {
  53. { .time = 128, .idx = 0 },
  54. { .time = 1000, .idx = 1 },
  55. { .time = 2000, .idx = 2 },
  56. { .time = 3000, .idx = 3 },
  57. };
  58. static const struct axp20x_time shutdown_time[] = {
  59. { .time = 4000, .idx = 0 },
  60. { .time = 6000, .idx = 1 },
  61. { .time = 8000, .idx = 2 },
  62. { .time = 10000, .idx = 3 },
  63. };
  64. static const struct axp20x_info axp20x_info = {
  65. .startup_time = startup_time,
  66. .startup_mask = AXP20X_PEK_STARTUP_MASK,
  67. .shutdown_time = shutdown_time,
  68. .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
  69. };
  70. static const struct axp20x_info axp221_info = {
  71. .startup_time = axp221_startup_time,
  72. .startup_mask = AXP20X_PEK_STARTUP_MASK,
  73. .shutdown_time = shutdown_time,
  74. .shutdown_mask = AXP20X_PEK_SHUTDOWN_MASK,
  75. };
  76. static ssize_t axp20x_show_attr(struct device *dev,
  77. const struct axp20x_time *time,
  78. unsigned int mask, char *buf)
  79. {
  80. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  81. unsigned int val;
  82. int ret, i;
  83. ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
  84. if (ret != 0)
  85. return ret;
  86. val &= mask;
  87. val >>= ffs(mask) - 1;
  88. for (i = 0; i < 4; i++)
  89. if (val == time[i].idx)
  90. val = time[i].time;
  91. return sprintf(buf, "%u\n", val);
  92. }
  93. static ssize_t axp20x_show_attr_startup(struct device *dev,
  94. struct device_attribute *attr,
  95. char *buf)
  96. {
  97. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  98. return axp20x_show_attr(dev, axp20x_pek->info->startup_time,
  99. axp20x_pek->info->startup_mask, buf);
  100. }
  101. static ssize_t axp20x_show_attr_shutdown(struct device *dev,
  102. struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  106. return axp20x_show_attr(dev, axp20x_pek->info->shutdown_time,
  107. axp20x_pek->info->shutdown_mask, buf);
  108. }
  109. static ssize_t axp20x_store_attr(struct device *dev,
  110. const struct axp20x_time *time,
  111. unsigned int mask, const char *buf,
  112. size_t count)
  113. {
  114. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  115. char val_str[20];
  116. size_t len;
  117. int ret, i;
  118. unsigned int val, idx = 0;
  119. unsigned int best_err = UINT_MAX;
  120. val_str[sizeof(val_str) - 1] = '\0';
  121. strncpy(val_str, buf, sizeof(val_str) - 1);
  122. len = strlen(val_str);
  123. if (len && val_str[len - 1] == '\n')
  124. val_str[len - 1] = '\0';
  125. ret = kstrtouint(val_str, 10, &val);
  126. if (ret)
  127. return ret;
  128. for (i = 3; i >= 0; i--) {
  129. unsigned int err;
  130. err = abs(time[i].time - val);
  131. if (err < best_err) {
  132. best_err = err;
  133. idx = time[i].idx;
  134. }
  135. if (!err)
  136. break;
  137. }
  138. idx <<= ffs(mask) - 1;
  139. ret = regmap_update_bits(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY,
  140. mask, idx);
  141. if (ret != 0)
  142. return -EINVAL;
  143. return count;
  144. }
  145. static ssize_t axp20x_store_attr_startup(struct device *dev,
  146. struct device_attribute *attr,
  147. const char *buf, size_t count)
  148. {
  149. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  150. return axp20x_store_attr(dev, axp20x_pek->info->startup_time,
  151. axp20x_pek->info->startup_mask, buf, count);
  152. }
  153. static ssize_t axp20x_store_attr_shutdown(struct device *dev,
  154. struct device_attribute *attr,
  155. const char *buf, size_t count)
  156. {
  157. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  158. return axp20x_store_attr(dev, axp20x_pek->info->shutdown_time,
  159. axp20x_pek->info->shutdown_mask, buf, count);
  160. }
  161. DEVICE_ATTR(startup, 0644, axp20x_show_attr_startup, axp20x_store_attr_startup);
  162. DEVICE_ATTR(shutdown, 0644, axp20x_show_attr_shutdown,
  163. axp20x_store_attr_shutdown);
  164. static struct attribute *axp20x_attributes[] = {
  165. &dev_attr_startup.attr,
  166. &dev_attr_shutdown.attr,
  167. NULL,
  168. };
  169. static const struct attribute_group axp20x_attribute_group = {
  170. .attrs = axp20x_attributes,
  171. };
  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. dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
  196. axp20x_pek->irq_dbr);
  197. return axp20x_pek->irq_dbr;
  198. }
  199. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  200. axp20x_pek->irq_dbr);
  201. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  202. if (axp20x_pek->irq_dbf < 0) {
  203. dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
  204. axp20x_pek->irq_dbf);
  205. return axp20x_pek->irq_dbf;
  206. }
  207. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  208. axp20x_pek->irq_dbf);
  209. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  210. if (!axp20x_pek->input)
  211. return -ENOMEM;
  212. idev = axp20x_pek->input;
  213. idev->name = "axp20x-pek";
  214. idev->phys = "m1kbd/input2";
  215. idev->dev.parent = &pdev->dev;
  216. input_set_capability(idev, EV_KEY, KEY_POWER);
  217. input_set_drvdata(idev, axp20x_pek);
  218. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  219. axp20x_pek_irq, 0,
  220. "axp20x-pek-dbr", idev);
  221. if (error < 0) {
  222. dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
  223. axp20x_pek->irq_dbr, error);
  224. return error;
  225. }
  226. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  227. axp20x_pek_irq, 0,
  228. "axp20x-pek-dbf", idev);
  229. if (error < 0) {
  230. dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
  231. axp20x_pek->irq_dbf, error);
  232. return error;
  233. }
  234. error = input_register_device(idev);
  235. if (error) {
  236. dev_err(&pdev->dev, "Can't register input device: %d\n",
  237. error);
  238. return error;
  239. }
  240. if (axp20x_pek->axp20x->variant == AXP288_ID)
  241. enable_irq_wake(axp20x_pek->irq_dbr);
  242. return 0;
  243. }
  244. #ifdef CONFIG_ACPI
  245. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
  246. struct platform_device *pdev)
  247. {
  248. unsigned long long hrv = 0;
  249. acpi_status status;
  250. if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
  251. axp20x_pek->axp20x->variant == AXP288_ID) {
  252. status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent),
  253. "_HRV", NULL, &hrv);
  254. if (ACPI_FAILURE(status))
  255. dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
  256. /*
  257. * On Cherry Trail platforms (hrv == 3), do not register the
  258. * input device if there is an "INTCFD9" or "ACPI0011" gpio
  259. * button ACPI device, as that handles the power button too,
  260. * and otherwise we end up reporting all presses twice.
  261. */
  262. if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) ||
  263. acpi_dev_present("ACPI0011", NULL, -1)))
  264. return false;
  265. }
  266. return true;
  267. }
  268. #else
  269. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
  270. struct platform_device *pdev)
  271. {
  272. return true;
  273. }
  274. #endif
  275. static int axp20x_pek_probe(struct platform_device *pdev)
  276. {
  277. struct axp20x_pek *axp20x_pek;
  278. const struct platform_device_id *match = platform_get_device_id(pdev);
  279. int error;
  280. if (!match) {
  281. dev_err(&pdev->dev, "Failed to get platform_device_id\n");
  282. return -EINVAL;
  283. }
  284. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  285. GFP_KERNEL);
  286. if (!axp20x_pek)
  287. return -ENOMEM;
  288. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  289. if (axp20x_pek_should_register_input(axp20x_pek, pdev)) {
  290. error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
  291. if (error)
  292. return error;
  293. }
  294. axp20x_pek->info = (struct axp20x_info *)match->driver_data;
  295. error = devm_device_add_group(&pdev->dev, &axp20x_attribute_group);
  296. if (error) {
  297. dev_err(&pdev->dev, "Failed to create sysfs attributes: %d\n",
  298. error);
  299. return error;
  300. }
  301. platform_set_drvdata(pdev, axp20x_pek);
  302. return 0;
  303. }
  304. static int __maybe_unused axp20x_pek_resume_noirq(struct device *dev)
  305. {
  306. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  307. if (axp20x_pek->axp20x->variant != AXP288_ID)
  308. return 0;
  309. /*
  310. * Clear interrupts from button presses during suspend, to avoid
  311. * a wakeup power-button press getting reported to userspace.
  312. */
  313. regmap_write(axp20x_pek->axp20x->regmap,
  314. AXP20X_IRQ1_STATE + AXP288_IRQ_POKN / 8,
  315. BIT(AXP288_IRQ_POKN % 8));
  316. return 0;
  317. }
  318. static const struct dev_pm_ops axp20x_pek_pm_ops = {
  319. #ifdef CONFIG_PM_SLEEP
  320. .resume_noirq = axp20x_pek_resume_noirq,
  321. #endif
  322. };
  323. static const struct platform_device_id axp_pek_id_match[] = {
  324. {
  325. .name = "axp20x-pek",
  326. .driver_data = (kernel_ulong_t)&axp20x_info,
  327. },
  328. {
  329. .name = "axp221-pek",
  330. .driver_data = (kernel_ulong_t)&axp221_info,
  331. },
  332. { /* sentinel */ }
  333. };
  334. MODULE_DEVICE_TABLE(platform, axp_pek_id_match);
  335. static struct platform_driver axp20x_pek_driver = {
  336. .probe = axp20x_pek_probe,
  337. .id_table = axp_pek_id_match,
  338. .driver = {
  339. .name = "axp20x-pek",
  340. .pm = &axp20x_pek_pm_ops,
  341. },
  342. };
  343. module_platform_driver(axp20x_pek_driver);
  344. MODULE_DESCRIPTION("axp20x Power Button");
  345. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  346. MODULE_LICENSE("GPL");