axp20x-pek.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if (irq == axp20x_pek->irq_dbr)
  141. input_report_key(idev, KEY_POWER, true);
  142. else if (irq == axp20x_pek->irq_dbf)
  143. input_report_key(idev, KEY_POWER, false);
  144. input_sync(idev);
  145. return IRQ_HANDLED;
  146. }
  147. static void axp20x_remove_sysfs_group(void *_data)
  148. {
  149. struct device *dev = _data;
  150. sysfs_remove_group(&dev->kobj, &axp20x_attribute_group);
  151. }
  152. static int axp20x_pek_probe(struct platform_device *pdev)
  153. {
  154. struct axp20x_pek *axp20x_pek;
  155. struct axp20x_dev *axp20x;
  156. struct input_dev *idev;
  157. int error;
  158. axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  159. GFP_KERNEL);
  160. if (!axp20x_pek)
  161. return -ENOMEM;
  162. axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  163. axp20x = axp20x_pek->axp20x;
  164. axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  165. if (axp20x_pek->irq_dbr < 0) {
  166. dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
  167. axp20x_pek->irq_dbr);
  168. return axp20x_pek->irq_dbr;
  169. }
  170. axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  171. axp20x_pek->irq_dbr);
  172. axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  173. if (axp20x_pek->irq_dbf < 0) {
  174. dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
  175. axp20x_pek->irq_dbf);
  176. return axp20x_pek->irq_dbf;
  177. }
  178. axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  179. axp20x_pek->irq_dbf);
  180. axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  181. if (!axp20x_pek->input)
  182. return -ENOMEM;
  183. idev = axp20x_pek->input;
  184. idev->name = "axp20x-pek";
  185. idev->phys = "m1kbd/input2";
  186. idev->dev.parent = &pdev->dev;
  187. input_set_capability(idev, EV_KEY, KEY_POWER);
  188. input_set_drvdata(idev, axp20x_pek);
  189. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  190. axp20x_pek_irq, 0,
  191. "axp20x-pek-dbr", idev);
  192. if (error < 0) {
  193. dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
  194. axp20x_pek->irq_dbr, error);
  195. return error;
  196. }
  197. error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  198. axp20x_pek_irq, 0,
  199. "axp20x-pek-dbf", idev);
  200. if (error < 0) {
  201. dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
  202. axp20x_pek->irq_dbf, error);
  203. return error;
  204. }
  205. error = sysfs_create_group(&pdev->dev.kobj, &axp20x_attribute_group);
  206. if (error) {
  207. dev_err(axp20x->dev, "Failed to create sysfs attributes: %d\n",
  208. error);
  209. return error;
  210. }
  211. error = devm_add_action(&pdev->dev,
  212. axp20x_remove_sysfs_group, &pdev->dev);
  213. if (error) {
  214. axp20x_remove_sysfs_group(&pdev->dev);
  215. dev_err(&pdev->dev, "Failed to add sysfs cleanup action: %d\n",
  216. error);
  217. return error;
  218. }
  219. error = input_register_device(idev);
  220. if (error) {
  221. dev_err(axp20x->dev, "Can't register input device: %d\n",
  222. error);
  223. return error;
  224. }
  225. platform_set_drvdata(pdev, axp20x_pek);
  226. return 0;
  227. }
  228. static struct platform_driver axp20x_pek_driver = {
  229. .probe = axp20x_pek_probe,
  230. .driver = {
  231. .name = "axp20x-pek",
  232. },
  233. };
  234. module_platform_driver(axp20x_pek_driver);
  235. MODULE_DESCRIPTION("axp20x Power Button");
  236. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  237. MODULE_LICENSE("GPL");