adp5520-keys.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Keypad driver for Analog Devices ADP5520 MFD PMICs
  3. *
  4. * Copyright 2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/input.h>
  12. #include <linux/mfd/adp5520.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. struct adp5520_keys {
  16. struct input_dev *input;
  17. struct notifier_block notifier;
  18. struct device *master;
  19. unsigned short keycode[ADP5520_KEYMAPSIZE];
  20. };
  21. static void adp5520_keys_report_event(struct adp5520_keys *dev,
  22. unsigned short keymask, int value)
  23. {
  24. int i;
  25. for (i = 0; i < ADP5520_MAXKEYS; i++)
  26. if (keymask & (1 << i))
  27. input_report_key(dev->input, dev->keycode[i], value);
  28. input_sync(dev->input);
  29. }
  30. static int adp5520_keys_notifier(struct notifier_block *nb,
  31. unsigned long event, void *data)
  32. {
  33. struct adp5520_keys *dev;
  34. uint8_t reg_val_lo, reg_val_hi;
  35. unsigned short keymask;
  36. dev = container_of(nb, struct adp5520_keys, notifier);
  37. if (event & ADP5520_KP_INT) {
  38. adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, &reg_val_lo);
  39. adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, &reg_val_hi);
  40. keymask = (reg_val_hi << 8) | reg_val_lo;
  41. /* Read twice to clear */
  42. adp5520_read(dev->master, ADP5520_KP_INT_STAT_1, &reg_val_lo);
  43. adp5520_read(dev->master, ADP5520_KP_INT_STAT_2, &reg_val_hi);
  44. keymask |= (reg_val_hi << 8) | reg_val_lo;
  45. adp5520_keys_report_event(dev, keymask, 1);
  46. }
  47. if (event & ADP5520_KR_INT) {
  48. adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, &reg_val_lo);
  49. adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, &reg_val_hi);
  50. keymask = (reg_val_hi << 8) | reg_val_lo;
  51. /* Read twice to clear */
  52. adp5520_read(dev->master, ADP5520_KR_INT_STAT_1, &reg_val_lo);
  53. adp5520_read(dev->master, ADP5520_KR_INT_STAT_2, &reg_val_hi);
  54. keymask |= (reg_val_hi << 8) | reg_val_lo;
  55. adp5520_keys_report_event(dev, keymask, 0);
  56. }
  57. return 0;
  58. }
  59. static int adp5520_keys_probe(struct platform_device *pdev)
  60. {
  61. struct adp5520_keys_platform_data *pdata = dev_get_platdata(&pdev->dev);
  62. struct input_dev *input;
  63. struct adp5520_keys *dev;
  64. int ret, i;
  65. unsigned char en_mask, ctl_mask = 0;
  66. if (pdev->id != ID_ADP5520) {
  67. dev_err(&pdev->dev, "only ADP5520 supports Keypad\n");
  68. return -EINVAL;
  69. }
  70. if (!pdata) {
  71. dev_err(&pdev->dev, "missing platform data\n");
  72. return -EINVAL;
  73. }
  74. if (!(pdata->rows_en_mask && pdata->cols_en_mask))
  75. return -EINVAL;
  76. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  77. if (!dev) {
  78. dev_err(&pdev->dev, "failed to alloc memory\n");
  79. return -ENOMEM;
  80. }
  81. input = devm_input_allocate_device(&pdev->dev);
  82. if (!input)
  83. return -ENOMEM;
  84. dev->master = pdev->dev.parent;
  85. dev->input = input;
  86. input->name = pdev->name;
  87. input->phys = "adp5520-keys/input0";
  88. input->dev.parent = &pdev->dev;
  89. input_set_drvdata(input, dev);
  90. input->id.bustype = BUS_I2C;
  91. input->id.vendor = 0x0001;
  92. input->id.product = 0x5520;
  93. input->id.version = 0x0001;
  94. input->keycodesize = sizeof(dev->keycode[0]);
  95. input->keycodemax = pdata->keymapsize;
  96. input->keycode = dev->keycode;
  97. memcpy(dev->keycode, pdata->keymap,
  98. pdata->keymapsize * input->keycodesize);
  99. /* setup input device */
  100. __set_bit(EV_KEY, input->evbit);
  101. if (pdata->repeat)
  102. __set_bit(EV_REP, input->evbit);
  103. for (i = 0; i < input->keycodemax; i++)
  104. __set_bit(dev->keycode[i], input->keybit);
  105. __clear_bit(KEY_RESERVED, input->keybit);
  106. ret = input_register_device(input);
  107. if (ret) {
  108. dev_err(&pdev->dev, "unable to register input device\n");
  109. return ret;
  110. }
  111. en_mask = pdata->rows_en_mask | pdata->cols_en_mask;
  112. ret = adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_1, en_mask);
  113. if (en_mask & ADP5520_COL_C3)
  114. ctl_mask |= ADP5520_C3_MODE;
  115. if (en_mask & ADP5520_ROW_R3)
  116. ctl_mask |= ADP5520_R3_MODE;
  117. if (ctl_mask)
  118. ret |= adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
  119. ctl_mask);
  120. ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
  121. pdata->rows_en_mask);
  122. if (ret) {
  123. dev_err(&pdev->dev, "failed to write\n");
  124. return -EIO;
  125. }
  126. dev->notifier.notifier_call = adp5520_keys_notifier;
  127. ret = adp5520_register_notifier(dev->master, &dev->notifier,
  128. ADP5520_KP_IEN | ADP5520_KR_IEN);
  129. if (ret) {
  130. dev_err(&pdev->dev, "failed to register notifier\n");
  131. return ret;
  132. }
  133. platform_set_drvdata(pdev, dev);
  134. return 0;
  135. }
  136. static int adp5520_keys_remove(struct platform_device *pdev)
  137. {
  138. struct adp5520_keys *dev = platform_get_drvdata(pdev);
  139. adp5520_unregister_notifier(dev->master, &dev->notifier,
  140. ADP5520_KP_IEN | ADP5520_KR_IEN);
  141. return 0;
  142. }
  143. static struct platform_driver adp5520_keys_driver = {
  144. .driver = {
  145. .name = "adp5520-keys",
  146. },
  147. .probe = adp5520_keys_probe,
  148. .remove = adp5520_keys_remove,
  149. };
  150. module_platform_driver(adp5520_keys_driver);
  151. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  152. MODULE_DESCRIPTION("Keys ADP5520 Driver");
  153. MODULE_LICENSE("GPL");
  154. MODULE_ALIAS("platform:adp5520-keys");