i2c-hid-of.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * HID over I2C Open Firmware Subclass
  3. *
  4. * Copyright (c) 2012 Benjamin Tissoires <[email protected]>
  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 <[email protected]>
  13. * Copyright (c) 2005 Michael Haboustak <[email protected]> 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/hid.h>
  24. #include <linux/i2c.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/pm.h>
  29. #include <linux/regulator/consumer.h>
  30. #include "i2c-hid.h"
  31. struct i2c_hid_of {
  32. struct i2chid_ops ops;
  33. struct i2c_client *client;
  34. struct regulator_bulk_data supplies[2];
  35. int post_power_delay_ms;
  36. };
  37. static int i2c_hid_of_power_up(struct i2chid_ops *ops)
  38. {
  39. struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
  40. struct device *dev = &ihid_of->client->dev;
  41. int ret;
  42. ret = regulator_bulk_enable(ARRAY_SIZE(ihid_of->supplies),
  43. ihid_of->supplies);
  44. if (ret) {
  45. dev_warn(dev, "Failed to enable supplies: %d\n", ret);
  46. return ret;
  47. }
  48. if (ihid_of->post_power_delay_ms)
  49. msleep(ihid_of->post_power_delay_ms);
  50. return 0;
  51. }
  52. static void i2c_hid_of_power_down(struct i2chid_ops *ops)
  53. {
  54. struct i2c_hid_of *ihid_of = container_of(ops, struct i2c_hid_of, ops);
  55. regulator_bulk_disable(ARRAY_SIZE(ihid_of->supplies),
  56. ihid_of->supplies);
  57. }
  58. static int i2c_hid_of_probe(struct i2c_client *client,
  59. const struct i2c_device_id *dev_id)
  60. {
  61. struct device *dev = &client->dev;
  62. struct i2c_hid_of *ihid_of;
  63. u16 hid_descriptor_address;
  64. u32 quirks = 0;
  65. int ret;
  66. u32 val;
  67. ihid_of = devm_kzalloc(&client->dev, sizeof(*ihid_of), GFP_KERNEL);
  68. if (!ihid_of)
  69. return -ENOMEM;
  70. ihid_of->ops.power_up = i2c_hid_of_power_up;
  71. ihid_of->ops.power_down = i2c_hid_of_power_down;
  72. ret = of_property_read_u32(dev->of_node, "hid-descr-addr", &val);
  73. if (ret) {
  74. dev_err(&client->dev, "HID register address not provided\n");
  75. return -ENODEV;
  76. }
  77. if (val >> 16) {
  78. dev_err(&client->dev, "Bad HID register address: 0x%08x\n",
  79. val);
  80. return -EINVAL;
  81. }
  82. hid_descriptor_address = val;
  83. if (!device_property_read_u32(&client->dev, "post-power-on-delay-ms",
  84. &val))
  85. ihid_of->post_power_delay_ms = val;
  86. ihid_of->supplies[0].supply = "vdd";
  87. ihid_of->supplies[1].supply = "vddl";
  88. ret = devm_regulator_bulk_get(&client->dev,
  89. ARRAY_SIZE(ihid_of->supplies),
  90. ihid_of->supplies);
  91. if (ret)
  92. return ret;
  93. if (device_property_read_bool(dev, "touchscreen-inverted-x"))
  94. quirks |= HID_QUIRK_X_INVERT;
  95. if (device_property_read_bool(dev, "touchscreen-inverted-y"))
  96. quirks |= HID_QUIRK_Y_INVERT;
  97. return i2c_hid_core_probe(client, &ihid_of->ops,
  98. hid_descriptor_address, quirks);
  99. }
  100. static const struct of_device_id i2c_hid_of_match[] = {
  101. { .compatible = "hid-over-i2c" },
  102. {},
  103. };
  104. MODULE_DEVICE_TABLE(of, i2c_hid_of_match);
  105. static const struct i2c_device_id i2c_hid_of_id_table[] = {
  106. { "hid", 0 },
  107. { "hid-over-i2c", 0 },
  108. { },
  109. };
  110. MODULE_DEVICE_TABLE(i2c, i2c_hid_of_id_table);
  111. static struct i2c_driver i2c_hid_of_driver = {
  112. .driver = {
  113. .name = "i2c_hid_of",
  114. .pm = &i2c_hid_core_pm,
  115. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  116. .of_match_table = of_match_ptr(i2c_hid_of_match),
  117. },
  118. .probe = i2c_hid_of_probe,
  119. .remove = i2c_hid_core_remove,
  120. .shutdown = i2c_hid_core_shutdown,
  121. .id_table = i2c_hid_of_id_table,
  122. };
  123. module_i2c_driver(i2c_hid_of_driver);
  124. MODULE_DESCRIPTION("HID over I2C OF driver");
  125. MODULE_AUTHOR("Benjamin Tissoires <[email protected]>");
  126. MODULE_LICENSE("GPL");