mc13783-pwrbutton.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * Copyright (C) 2011 Philippe Rétornaz
  3. *
  4. * Based on twl4030-pwrbutton driver by:
  5. * Peter De Schrijver <peter.de-schrijver@nokia.com>
  6. * Felipe Balbi <felipe.balbi@nokia.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file "COPYING" in the main directory of this
  10. * archive for more details.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/input.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mfd/mc13783.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. struct mc13783_pwrb {
  31. struct input_dev *pwr;
  32. struct mc13xxx *mc13783;
  33. #define MC13783_PWRB_B1_POL_INVERT (1 << 0)
  34. #define MC13783_PWRB_B2_POL_INVERT (1 << 1)
  35. #define MC13783_PWRB_B3_POL_INVERT (1 << 2)
  36. int flags;
  37. unsigned short keymap[3];
  38. };
  39. #define MC13783_REG_INTERRUPT_SENSE_1 5
  40. #define MC13783_IRQSENSE1_ONOFD1S (1 << 3)
  41. #define MC13783_IRQSENSE1_ONOFD2S (1 << 4)
  42. #define MC13783_IRQSENSE1_ONOFD3S (1 << 5)
  43. #define MC13783_REG_POWER_CONTROL_2 15
  44. #define MC13783_POWER_CONTROL_2_ON1BDBNC 4
  45. #define MC13783_POWER_CONTROL_2_ON2BDBNC 6
  46. #define MC13783_POWER_CONTROL_2_ON3BDBNC 8
  47. #define MC13783_POWER_CONTROL_2_ON1BRSTEN (1 << 1)
  48. #define MC13783_POWER_CONTROL_2_ON2BRSTEN (1 << 2)
  49. #define MC13783_POWER_CONTROL_2_ON3BRSTEN (1 << 3)
  50. static irqreturn_t button_irq(int irq, void *_priv)
  51. {
  52. struct mc13783_pwrb *priv = _priv;
  53. int val;
  54. mc13xxx_irq_ack(priv->mc13783, irq);
  55. mc13xxx_reg_read(priv->mc13783, MC13783_REG_INTERRUPT_SENSE_1, &val);
  56. switch (irq) {
  57. case MC13783_IRQ_ONOFD1:
  58. val = val & MC13783_IRQSENSE1_ONOFD1S ? 1 : 0;
  59. if (priv->flags & MC13783_PWRB_B1_POL_INVERT)
  60. val ^= 1;
  61. input_report_key(priv->pwr, priv->keymap[0], val);
  62. break;
  63. case MC13783_IRQ_ONOFD2:
  64. val = val & MC13783_IRQSENSE1_ONOFD2S ? 1 : 0;
  65. if (priv->flags & MC13783_PWRB_B2_POL_INVERT)
  66. val ^= 1;
  67. input_report_key(priv->pwr, priv->keymap[1], val);
  68. break;
  69. case MC13783_IRQ_ONOFD3:
  70. val = val & MC13783_IRQSENSE1_ONOFD3S ? 1 : 0;
  71. if (priv->flags & MC13783_PWRB_B3_POL_INVERT)
  72. val ^= 1;
  73. input_report_key(priv->pwr, priv->keymap[2], val);
  74. break;
  75. }
  76. input_sync(priv->pwr);
  77. return IRQ_HANDLED;
  78. }
  79. static int mc13783_pwrbutton_probe(struct platform_device *pdev)
  80. {
  81. const struct mc13xxx_buttons_platform_data *pdata;
  82. struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
  83. struct input_dev *pwr;
  84. struct mc13783_pwrb *priv;
  85. int err = 0;
  86. int reg = 0;
  87. pdata = dev_get_platdata(&pdev->dev);
  88. if (!pdata) {
  89. dev_err(&pdev->dev, "missing platform data\n");
  90. return -ENODEV;
  91. }
  92. pwr = input_allocate_device();
  93. if (!pwr) {
  94. dev_dbg(&pdev->dev, "Can't allocate power button\n");
  95. return -ENOMEM;
  96. }
  97. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  98. if (!priv) {
  99. err = -ENOMEM;
  100. dev_dbg(&pdev->dev, "Can't allocate power button\n");
  101. goto free_input_dev;
  102. }
  103. reg |= (pdata->b1on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON1BDBNC;
  104. reg |= (pdata->b2on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON2BDBNC;
  105. reg |= (pdata->b3on_flags & 0x3) << MC13783_POWER_CONTROL_2_ON3BDBNC;
  106. priv->pwr = pwr;
  107. priv->mc13783 = mc13783;
  108. mc13xxx_lock(mc13783);
  109. if (pdata->b1on_flags & MC13783_BUTTON_ENABLE) {
  110. priv->keymap[0] = pdata->b1on_key;
  111. if (pdata->b1on_key != KEY_RESERVED)
  112. __set_bit(pdata->b1on_key, pwr->keybit);
  113. if (pdata->b1on_flags & MC13783_BUTTON_POL_INVERT)
  114. priv->flags |= MC13783_PWRB_B1_POL_INVERT;
  115. if (pdata->b1on_flags & MC13783_BUTTON_RESET_EN)
  116. reg |= MC13783_POWER_CONTROL_2_ON1BRSTEN;
  117. err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD1,
  118. button_irq, "b1on", priv);
  119. if (err) {
  120. dev_dbg(&pdev->dev, "Can't request irq\n");
  121. goto free_priv;
  122. }
  123. }
  124. if (pdata->b2on_flags & MC13783_BUTTON_ENABLE) {
  125. priv->keymap[1] = pdata->b2on_key;
  126. if (pdata->b2on_key != KEY_RESERVED)
  127. __set_bit(pdata->b2on_key, pwr->keybit);
  128. if (pdata->b2on_flags & MC13783_BUTTON_POL_INVERT)
  129. priv->flags |= MC13783_PWRB_B2_POL_INVERT;
  130. if (pdata->b2on_flags & MC13783_BUTTON_RESET_EN)
  131. reg |= MC13783_POWER_CONTROL_2_ON2BRSTEN;
  132. err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD2,
  133. button_irq, "b2on", priv);
  134. if (err) {
  135. dev_dbg(&pdev->dev, "Can't request irq\n");
  136. goto free_irq_b1;
  137. }
  138. }
  139. if (pdata->b3on_flags & MC13783_BUTTON_ENABLE) {
  140. priv->keymap[2] = pdata->b3on_key;
  141. if (pdata->b3on_key != KEY_RESERVED)
  142. __set_bit(pdata->b3on_key, pwr->keybit);
  143. if (pdata->b3on_flags & MC13783_BUTTON_POL_INVERT)
  144. priv->flags |= MC13783_PWRB_B3_POL_INVERT;
  145. if (pdata->b3on_flags & MC13783_BUTTON_RESET_EN)
  146. reg |= MC13783_POWER_CONTROL_2_ON3BRSTEN;
  147. err = mc13xxx_irq_request(mc13783, MC13783_IRQ_ONOFD3,
  148. button_irq, "b3on", priv);
  149. if (err) {
  150. dev_dbg(&pdev->dev, "Can't request irq: %d\n", err);
  151. goto free_irq_b2;
  152. }
  153. }
  154. mc13xxx_reg_rmw(mc13783, MC13783_REG_POWER_CONTROL_2, 0x3FE, reg);
  155. mc13xxx_unlock(mc13783);
  156. pwr->name = "mc13783_pwrbutton";
  157. pwr->phys = "mc13783_pwrbutton/input0";
  158. pwr->dev.parent = &pdev->dev;
  159. pwr->keycode = priv->keymap;
  160. pwr->keycodemax = ARRAY_SIZE(priv->keymap);
  161. pwr->keycodesize = sizeof(priv->keymap[0]);
  162. __set_bit(EV_KEY, pwr->evbit);
  163. err = input_register_device(pwr);
  164. if (err) {
  165. dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
  166. goto free_irq;
  167. }
  168. platform_set_drvdata(pdev, priv);
  169. return 0;
  170. free_irq:
  171. mc13xxx_lock(mc13783);
  172. if (pdata->b3on_flags & MC13783_BUTTON_ENABLE)
  173. mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD3, priv);
  174. free_irq_b2:
  175. if (pdata->b2on_flags & MC13783_BUTTON_ENABLE)
  176. mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD2, priv);
  177. free_irq_b1:
  178. if (pdata->b1on_flags & MC13783_BUTTON_ENABLE)
  179. mc13xxx_irq_free(mc13783, MC13783_IRQ_ONOFD1, priv);
  180. free_priv:
  181. mc13xxx_unlock(mc13783);
  182. kfree(priv);
  183. free_input_dev:
  184. input_free_device(pwr);
  185. return err;
  186. }
  187. static int mc13783_pwrbutton_remove(struct platform_device *pdev)
  188. {
  189. struct mc13783_pwrb *priv = platform_get_drvdata(pdev);
  190. const struct mc13xxx_buttons_platform_data *pdata;
  191. pdata = dev_get_platdata(&pdev->dev);
  192. mc13xxx_lock(priv->mc13783);
  193. if (pdata->b3on_flags & MC13783_BUTTON_ENABLE)
  194. mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD3, priv);
  195. if (pdata->b2on_flags & MC13783_BUTTON_ENABLE)
  196. mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD2, priv);
  197. if (pdata->b1on_flags & MC13783_BUTTON_ENABLE)
  198. mc13xxx_irq_free(priv->mc13783, MC13783_IRQ_ONOFD1, priv);
  199. mc13xxx_unlock(priv->mc13783);
  200. input_unregister_device(priv->pwr);
  201. kfree(priv);
  202. return 0;
  203. }
  204. static struct platform_driver mc13783_pwrbutton_driver = {
  205. .probe = mc13783_pwrbutton_probe,
  206. .remove = mc13783_pwrbutton_remove,
  207. .driver = {
  208. .name = "mc13783-pwrbutton",
  209. },
  210. };
  211. module_platform_driver(mc13783_pwrbutton_driver);
  212. MODULE_ALIAS("platform:mc13783-pwrbutton");
  213. MODULE_DESCRIPTION("MC13783 Power Button");
  214. MODULE_LICENSE("GPL v2");
  215. MODULE_AUTHOR("Philippe Retornaz");