i2c-hid-of.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * HID over I2C Open Firmware Subclass
  3. *
  4. * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  5. * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
  6. * Copyright (c) 2012 Red Hat, Inc
  7. *
  8. * This code was forked out of the core code, which was partly based on
  9. * "USB HID support for Linux":
  10. *
  11. * Copyright (c) 1999 Andreas Gal
  12. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  13. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  14. * Copyright (c) 2007-2008 Oliver Neukum
  15. * Copyright (c) 2006-2010 Jiri Kosina
  16. *
  17. * This file is subject to the terms and conditions of the GNU General Public
  18. * License. See the file COPYING in the main directory of this archive for
  19. * more details.
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio/consumer.h>
  24. #include <linux/hid.h>
  25. #include <linux/i2c.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/pm.h>
  30. #include <linux/regulator/consumer.h>
  31. #include "i2c-hid.h"
  32. struct i2c_hid_of {
  33. struct i2chid_ops ops;
  34. struct i2c_client *client;
  35. struct gpio_desc *reset_gpio;
  36. struct regulator_bulk_data supplies[2];
  37. int post_power_delay_ms;
  38. int post_reset_delay_ms;
  39. };
  40. static int i2c_hid_of_power_up(struct i2chid_ops *ops)
  41. {
  42. struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
  43. struct device *dev = &ihid_of->client->dev;
  44. int ret;
  45. ret = regulator_bulk_enable(ARRAY_SIZE(ihid_of->supplies),
  46. ihid_of->supplies);
  47. if (ret) {
  48. dev_warn(dev, "Failed to enable supplies: %d\n", ret);
  49. return ret;
  50. }
  51. if (ihid_of->post_power_delay_ms)
  52. msleep(ihid_of->post_power_delay_ms);
  53. gpiod_set_value_cansleep(ihid_of->reset_gpio, 0);
  54. if (ihid_of->post_reset_delay_ms)
  55. msleep(ihid_of->post_reset_delay_ms);
  56. return 0;
  57. }
  58. static void i2c_hid_of_power_down(struct i2chid_ops *ops)
  59. {
  60. struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
  61. gpiod_set_value_cansleep(ihid_of->reset_gpio, 1);
  62. regulator_bulk_disable(ARRAY_SIZE(ihid_of->supplies),
  63. ihid_of->supplies);
  64. }
  65. static int i2c_hid_of_probe(struct i2c_client *client)
  66. {
  67. struct device *dev = &client->dev;
  68. struct i2c_hid_of *ihid_of;
  69. u16 hid_descriptor_address;
  70. u32 quirks = 0;
  71. int ret;
  72. u32 val;
  73. ihid_of = devm_kzalloc(dev, sizeof(*ihid_of), GFP_KERNEL);
  74. if (!ihid_of)
  75. return -ENOMEM;
  76. ihid_of->client = client;
  77. ihid_of->ops.power_up = i2c_hid_of_power_up;
  78. ihid_of->ops.power_down = i2c_hid_of_power_down;
  79. ret = device_property_read_u32(dev, "hid-descr-addr", &val);
  80. if (ret) {
  81. dev_err(dev, "HID register address not provided\n");
  82. return -ENODEV;
  83. }
  84. if (val >> 16) {
  85. dev_err(dev, "Bad HID register address: 0x%08x\n", val);
  86. return -EINVAL;
  87. }
  88. hid_descriptor_address = val;
  89. if (!device_property_read_u32(dev, "post-power-on-delay-ms", &val))
  90. ihid_of->post_power_delay_ms = val;
  91. /*
  92. * Note this is a kernel internal device-property set by x86 platform code,
  93. * this MUST not be used in devicetree files without first adding it to
  94. * the DT bindings.
  95. */
  96. if (!device_property_read_u32(dev, "post-reset-deassert-delay-ms", &val))
  97. ihid_of->post_reset_delay_ms = val;
  98. /* Start out with reset asserted */
  99. ihid_of->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  100. if (IS_ERR(ihid_of->reset_gpio))
  101. return PTR_ERR(ihid_of->reset_gpio);
  102. ihid_of->supplies[0].supply = "vdd";
  103. ihid_of->supplies[1].supply = "vddl";
  104. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ihid_of->supplies),
  105. ihid_of->supplies);
  106. if (ret)
  107. return ret;
  108. if (device_property_read_bool(dev, "touchscreen-inverted-x"))
  109. quirks |= HID_QUIRK_X_INVERT;
  110. if (device_property_read_bool(dev, "touchscreen-inverted-y"))
  111. quirks |= HID_QUIRK_Y_INVERT;
  112. return i2c_hid_core_probe(client, &ihid_of->ops,
  113. hid_descriptor_address, quirks);
  114. }
  115. #ifdef CONFIG_OF
  116. static const struct of_device_id i2c_hid_of_match[] = {
  117. { .compatible = "hid-over-i2c" },
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
  121. #endif
  122. static const struct i2c_device_id i2c_hid_of_id_table[] = {
  123. { "hid", 0 },
  124. { "hid-over-i2c", 0 },
  125. { },
  126. };
  127. MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
  128. static struct i2c_driver i2c_hid_of_driver = {
  129. .driver = {
  130. .name = "i2c_hid_of",
  131. .pm = &i2c_hid_core_pm,
  132. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  133. .of_match_table = of_match_ptr(i2c_hid_of_match),
  134. },
  135. .probe = i2c_hid_of_probe,
  136. .remove = i2c_hid_core_remove,
  137. .shutdown = i2c_hid_core_shutdown,
  138. .id_table = i2c_hid_of_id_table,
  139. };
  140. module_i2c_driver(i2c_hid_of_driver);
  141. MODULE_DESCRIPTION("HID over I2C OF driver");
  142. MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
  143. MODULE_LICENSE("GPL");