axp20x-pek.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/errno.h>
  16. #include <linux/irq.h>
  17. #include <linux/init.h>
  18. #include <linux/input.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mfd/axp20x.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/slab.h>
  26. #define AXP20X_PEK_STARTUP_MASK (0xc0)
  27. #define AXP20X_PEK_SHUTDOWN_MASK (0x03)
  28. struct axp20x_pek {
  29. struct axp20x_dev *axp20x;
  30. struct input_dev *input;
  31. int irq_dbr;
  32. int irq_dbf;
  33. };
  34. struct axp20x_time {
  35. unsigned int time;
  36. unsigned int idx;
  37. };
  38. static const struct axp20x_time startup_time[] = {
  39. { .time = 128, .idx = 0 },
  40. { .time = 1000, .idx = 2 },
  41. { .time = 3000, .idx = 1 },
  42. { .time = 2000, .idx = 3 },
  43. };
  44. static const struct axp20x_time shutdown_time[] = {
  45. { .time = 4000, .idx = 0 },
  46. { .time = 6000, .idx = 1 },
  47. { .time = 8000, .idx = 2 },
  48. { .time = 10000, .idx = 3 },
  49. };
  50. struct axp20x_pek_ext_attr {
  51. const struct axp20x_time *p_time;
  52. unsigned int mask;
  53. };
  54. static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
  55. .p_time = startup_time,
  56. .mask = AXP20X_PEK_STARTUP_MASK,
  57. };
  58. static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
  59. .p_time = shutdown_time,
  60. .mask = AXP20X_PEK_SHUTDOWN_MASK,
  61. };
  62. static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
  63. {
  64. return container_of(attr, struct dev_ext_attribute, attr)->var;
  65. }
  66. static ssize_t axp20x_show_ext_attr(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  70. struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  71. unsigned int val;
  72. int ret, i;
  73. ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
  74. if (ret != 0)
  75. return ret;
  76. val &= axp20x_ea->mask;
  77. val >>= ffs(axp20x_ea->mask) - 1;
  78. for (i = 0; i < 4; i++)
  79. if (val == axp20x_ea->p_time[i].idx)
  80. val = axp20x_ea->p_time[i].time;
  81. return sprintf(buf, "%u\n", val);
  82. }
  83. static ssize_t axp20x_store_ext_attr(struct device *dev,
  84. struct device_attribute *attr,
  85. const char *buf, size_t count)
  86. {
  87. struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  88. struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  89. char val_str[20];
  90. size_t len;
  91. int ret, i;
  92. unsigned int val, idx = 0;
  93. unsigned int best_err = UINT_MAX;
  94. val_str[sizeof(val_str) - 1] = '\0';
  95. strncpy(val_str, buf, sizeof(val_str) - 1);
  96. len = strlen(val_str);
  97. if (len && val_str[len - 1] == '\n')
  98. val_str[len - 1] = '\0';
  99. ret = kstrtouint(val_str, 10, &val);
  100. if (ret)
  101. return ret;
  102. for (i = 3; i >= 0; i--) {
  103. unsigned int err;
  104. err = abs(axp20x_ea->p_time[i].time - val);
  105. if (err < best_err) {
  106. best_err = err;
  107. idx = axp20x_ea->p_time[i].idx;
  108. }
  109. if (!err)
  110. break;
  111. }
  112. idx <<= ffs(axp20x_ea->mask) - 1;
  113. ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
  114. AXP20X_PEK_KEY,
  115. axp20x_ea->mask, idx);
  116. if (ret != 0)
  117. return -EINVAL;
  118. return count;
  119. }
  120. static struct dev_ext_attribute axp20x_dev_attr_startup = {
  121. .attr = __ATTR(startup, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  122. .var = &axp20x_pek_startup_ext_attr,
  123. };
  124. static struct dev_ext_attribute axp20x_dev_attr_shutdown = {
  125. .attr = __ATTR(shutdown, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  126. .var = &axp20x_pek_shutdown_ext_attr,
  127. };
  128. static struct attribute *axp20x_attributes[] = {
  129. &axp20x_dev_attr_startup.attr.attr,
  130. &axp20x_dev_attr_shutdown.attr.attr,
  131. NULL,
  132. };
  133. static const struct attribute_group axp20x_attribute_group = {
  134. .attrs = axp20x_attributes,
  135. };
  136. static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
  137. {
  138. struct input_dev *idev = pwr;
  139. struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
  140. /*
  141. * The power-button is connected to ground so a falling edge (dbf)
  142. * means it is pressed.
  143. */
  144. if (irq == axp20x_pek->irq_dbf)
  145. input_report_key(idev, KEY_POWER, true);
  146. else if (irq == axp20x_pek->irq_dbr)
  147. input_report_key(idev, KEY_POWER, false);
  148. input_sync(idev);
  149. return IRQ_HANDLED;
  150. }
  151. static void axp20x_remove_sysfs_group(void *_data)
  152. {
  153. struct device *dev = _data;
  154. sysfs_remove_group(&dev->kobj, &axp20x_attribute_group);
  155. }
  156. static int axp20x_pek_probe(struct platform_device *pdev)
  157. {
  158. struct axp20x_pek *axp20x_pek;
  159. struct axp20x_dev *axp20x;
  160. struct input_dev *idev;
  161. int error;
  162. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  163. GFP_KERNEL);
  164. if (!axp20x_pek)
  165. return -ENOMEM;
  166. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  167. axp20x = axp20x_pek->axp20x;
  168. axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  169. if (axp20x_pek->irq_dbr < 0) {
  170. dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
  171. axp20x_pek->irq_dbr);
  172. return axp20x_pek->irq_dbr;
  173. }
  174. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  175. axp20x_pek->irq_dbr);
  176. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  177. if (axp20x_pek->irq_dbf < 0) {
  178. dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
  179. axp20x_pek->irq_dbf);
  180. return axp20x_pek->irq_dbf;
  181. }
  182. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  183. axp20x_pek->irq_dbf);
  184. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  185. if (!axp20x_pek->input)
  186. return -ENOMEM;
  187. idev = axp20x_pek->input;
  188. idev->name = "axp20x-pek";
  189. idev->phys = "m1kbd/input2";
  190. idev->dev.parent = &pdev->dev;
  191. input_set_capability(idev, EV_KEY, KEY_POWER);
  192. input_set_drvdata(idev, axp20x_pek);
  193. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  194. axp20x_pek_irq, 0,
  195. "axp20x-pek-dbr", idev);
  196. if (error < 0) {
  197. dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
  198. axp20x_pek->irq_dbr, error);
  199. return error;
  200. }
  201. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  202. axp20x_pek_irq, 0,
  203. "axp20x-pek-dbf", idev);
  204. if (error < 0) {
  205. dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
  206. axp20x_pek->irq_dbf, error);
  207. return error;
  208. }
  209. error = sysfs_create_group(&pdev->dev.kobj, &axp20x_attribute_group);
  210. if (error) {
  211. dev_err(axp20x->dev, "Failed to create sysfs attributes: %d\n",
  212. error);
  213. return error;
  214. }
  215. error = devm_add_action(&pdev->dev,
  216. axp20x_remove_sysfs_group, &pdev->dev);
  217. if (error) {
  218. axp20x_remove_sysfs_group(&pdev->dev);
  219. dev_err(&pdev->dev, "Failed to add sysfs cleanup action: %d\n",
  220. error);
  221. return error;
  222. }
  223. error = input_register_device(idev);
  224. if (error) {
  225. dev_err(axp20x->dev, "Can't register input device: %d\n",
  226. error);
  227. return error;
  228. }
  229. platform_set_drvdata(pdev, axp20x_pek);
  230. return 0;
  231. }
  232. static struct platform_driver axp20x_pek_driver = {
  233. .probe = axp20x_pek_probe,
  234. .driver = {
  235. .name = "axp20x-pek",
  236. },
  237. };
  238. module_platform_driver(axp20x_pek_driver);
  239. MODULE_DESCRIPTION("axp20x Power Button");
  240. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  241. MODULE_LICENSE("GPL");
  242. MODULE_ALIAS("platform:axp20x-pek");