axp20x-pek.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_pek {
  30. struct axp20x_dev *axp20x;
  31. struct input_dev *input;
  32. int irq_dbr;
  33. int irq_dbf;
  34. };
  35. struct axp20x_time {
  36. unsigned int time;
  37. unsigned int idx;
  38. };
  39. static const struct axp20x_time startup_time[] = {
  40. { .time = 128, .idx = 0 },
  41. { .time = 1000, .idx = 2 },
  42. { .time = 3000, .idx = 1 },
  43. { .time = 2000, .idx = 3 },
  44. };
  45. static const struct axp20x_time shutdown_time[] = {
  46. { .time = 4000, .idx = 0 },
  47. { .time = 6000, .idx = 1 },
  48. { .time = 8000, .idx = 2 },
  49. { .time = 10000, .idx = 3 },
  50. };
  51. struct axp20x_pek_ext_attr {
  52. const struct axp20x_time *p_time;
  53. unsigned int mask;
  54. };
  55. static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
  56. .p_time = startup_time,
  57. .mask = AXP20X_PEK_STARTUP_MASK,
  58. };
  59. static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
  60. .p_time = shutdown_time,
  61. .mask = AXP20X_PEK_SHUTDOWN_MASK,
  62. };
  63. static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
  64. {
  65. return container_of(attr, struct dev_ext_attribute, attr)->var;
  66. }
  67. static ssize_t axp20x_show_ext_attr(struct device *dev,
  68. struct device_attribute *attr, char *buf)
  69. {
  70. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  71. struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  72. unsigned int val;
  73. int ret, i;
  74. ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
  75. if (ret != 0)
  76. return ret;
  77. val &= axp20x_ea->mask;
  78. val >>= ffs(axp20x_ea->mask) - 1;
  79. for (i = 0; i < 4; i++)
  80. if (val == axp20x_ea->p_time[i].idx)
  81. val = axp20x_ea->p_time[i].time;
  82. return sprintf(buf, "%u\n", val);
  83. }
  84. static ssize_t axp20x_store_ext_attr(struct device *dev,
  85. struct device_attribute *attr,
  86. const char *buf, size_t count)
  87. {
  88. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  89. struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  90. char val_str[20];
  91. size_t len;
  92. int ret, i;
  93. unsigned int val, idx = 0;
  94. unsigned int best_err = UINT_MAX;
  95. val_str[sizeof(val_str) - 1] = '\0';
  96. strncpy(val_str, buf, sizeof(val_str) - 1);
  97. len = strlen(val_str);
  98. if (len && val_str[len - 1] == '\n')
  99. val_str[len - 1] = '\0';
  100. ret = kstrtouint(val_str, 10, &val);
  101. if (ret)
  102. return ret;
  103. for (i = 3; i >= 0; i--) {
  104. unsigned int err;
  105. err = abs(axp20x_ea->p_time[i].time - val);
  106. if (err < best_err) {
  107. best_err = err;
  108. idx = axp20x_ea->p_time[i].idx;
  109. }
  110. if (!err)
  111. break;
  112. }
  113. idx <<= ffs(axp20x_ea->mask) - 1;
  114. ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
  115. AXP20X_PEK_KEY,
  116. axp20x_ea->mask, idx);
  117. if (ret != 0)
  118. return -EINVAL;
  119. return count;
  120. }
  121. static struct dev_ext_attribute axp20x_dev_attr_startup = {
  122. .attr = __ATTR(startup, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  123. .var = &axp20x_pek_startup_ext_attr,
  124. };
  125. static struct dev_ext_attribute axp20x_dev_attr_shutdown = {
  126. .attr = __ATTR(shutdown, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  127. .var = &axp20x_pek_shutdown_ext_attr,
  128. };
  129. static struct attribute *axp20x_attributes[] = {
  130. &axp20x_dev_attr_startup.attr.attr,
  131. &axp20x_dev_attr_shutdown.attr.attr,
  132. NULL,
  133. };
  134. static const struct attribute_group axp20x_attribute_group = {
  135. .attrs = axp20x_attributes,
  136. };
  137. static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
  138. {
  139. struct input_dev *idev = pwr;
  140. struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
  141. /*
  142. * The power-button is connected to ground so a falling edge (dbf)
  143. * means it is pressed.
  144. */
  145. if (irq == axp20x_pek->irq_dbf)
  146. input_report_key(idev, KEY_POWER, true);
  147. else if (irq == axp20x_pek->irq_dbr)
  148. input_report_key(idev, KEY_POWER, false);
  149. input_sync(idev);
  150. return IRQ_HANDLED;
  151. }
  152. static void axp20x_remove_sysfs_group(void *_data)
  153. {
  154. struct device *dev = _data;
  155. sysfs_remove_group(&dev->kobj, &axp20x_attribute_group);
  156. }
  157. static int axp20x_pek_probe_input_device(struct axp20x_pek *axp20x_pek,
  158. struct platform_device *pdev)
  159. {
  160. struct axp20x_dev *axp20x = axp20x_pek->axp20x;
  161. struct input_dev *idev;
  162. int error;
  163. axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  164. if (axp20x_pek->irq_dbr < 0) {
  165. dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
  166. axp20x_pek->irq_dbr);
  167. return axp20x_pek->irq_dbr;
  168. }
  169. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  170. axp20x_pek->irq_dbr);
  171. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  172. if (axp20x_pek->irq_dbf < 0) {
  173. dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
  174. axp20x_pek->irq_dbf);
  175. return axp20x_pek->irq_dbf;
  176. }
  177. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  178. axp20x_pek->irq_dbf);
  179. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  180. if (!axp20x_pek->input)
  181. return -ENOMEM;
  182. idev = axp20x_pek->input;
  183. idev->name = "axp20x-pek";
  184. idev->phys = "m1kbd/input2";
  185. idev->dev.parent = &pdev->dev;
  186. input_set_capability(idev, EV_KEY, KEY_POWER);
  187. input_set_drvdata(idev, axp20x_pek);
  188. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  189. axp20x_pek_irq, 0,
  190. "axp20x-pek-dbr", idev);
  191. if (error < 0) {
  192. dev_err(&pdev->dev, "Failed to request dbr IRQ#%d: %d\n",
  193. axp20x_pek->irq_dbr, error);
  194. return error;
  195. }
  196. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  197. axp20x_pek_irq, 0,
  198. "axp20x-pek-dbf", idev);
  199. if (error < 0) {
  200. dev_err(&pdev->dev, "Failed to request dbf IRQ#%d: %d\n",
  201. axp20x_pek->irq_dbf, error);
  202. return error;
  203. }
  204. error = input_register_device(idev);
  205. if (error) {
  206. dev_err(&pdev->dev, "Can't register input device: %d\n",
  207. error);
  208. return error;
  209. }
  210. return 0;
  211. }
  212. #ifdef CONFIG_ACPI
  213. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
  214. struct platform_device *pdev)
  215. {
  216. unsigned long long hrv = 0;
  217. acpi_status status;
  218. if (IS_ENABLED(CONFIG_INPUT_SOC_BUTTON_ARRAY) &&
  219. axp20x_pek->axp20x->variant == AXP288_ID) {
  220. status = acpi_evaluate_integer(ACPI_HANDLE(pdev->dev.parent),
  221. "_HRV", NULL, &hrv);
  222. if (ACPI_FAILURE(status))
  223. dev_err(&pdev->dev, "Failed to get PMIC hardware revision\n");
  224. /*
  225. * On Cherry Trail platforms (hrv == 3), do not register the
  226. * input device if there is an "INTCFD9" or "ACPI0011" gpio
  227. * button ACPI device, as that handles the power button too,
  228. * and otherwise we end up reporting all presses twice.
  229. */
  230. if (hrv == 3 && (acpi_dev_present("INTCFD9", NULL, -1) ||
  231. acpi_dev_present("ACPI0011", NULL, -1)))
  232. return false;
  233. }
  234. return true;
  235. }
  236. #else
  237. static bool axp20x_pek_should_register_input(struct axp20x_pek *axp20x_pek,
  238. struct platform_device *pdev)
  239. {
  240. return true;
  241. }
  242. #endif
  243. static int axp20x_pek_probe(struct platform_device *pdev)
  244. {
  245. struct axp20x_pek *axp20x_pek;
  246. int error;
  247. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  248. GFP_KERNEL);
  249. if (!axp20x_pek)
  250. return -ENOMEM;
  251. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  252. if (axp20x_pek_should_register_input(axp20x_pek, pdev)) {
  253. error = axp20x_pek_probe_input_device(axp20x_pek, pdev);
  254. if (error)
  255. return error;
  256. }
  257. error = sysfs_create_group(&pdev->dev.kobj, &axp20x_attribute_group);
  258. if (error) {
  259. dev_err(&pdev->dev, "Failed to create sysfs attributes: %d\n",
  260. error);
  261. return error;
  262. }
  263. error = devm_add_action(&pdev->dev,
  264. axp20x_remove_sysfs_group, &pdev->dev);
  265. if (error) {
  266. axp20x_remove_sysfs_group(&pdev->dev);
  267. dev_err(&pdev->dev, "Failed to add sysfs cleanup action: %d\n",
  268. error);
  269. return error;
  270. }
  271. platform_set_drvdata(pdev, axp20x_pek);
  272. return 0;
  273. }
  274. static struct platform_driver axp20x_pek_driver = {
  275. .probe = axp20x_pek_probe,
  276. .driver = {
  277. .name = "axp20x-pek",
  278. },
  279. };
  280. module_platform_driver(axp20x_pek_driver);
  281. MODULE_DESCRIPTION("axp20x Power Button");
  282. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  283. MODULE_LICENSE("GPL");
  284. MODULE_ALIAS("platform:axp20x-pek");