drm_encoder_slave.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2009 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "drm_encoder_slave.h"
  27. /**
  28. * drm_i2c_encoder_init - Initialize an I2C slave encoder
  29. * @dev: DRM device.
  30. * @encoder: Encoder to be attached to the I2C device. You aren't
  31. * required to have called drm_encoder_init() before.
  32. * @adap: I2C adapter that will be used to communicate with
  33. * the device.
  34. * @info: Information that will be used to create the I2C device.
  35. * Required fields are @addr and @type.
  36. *
  37. * Create an I2C device on the specified bus (the module containing its
  38. * driver is transparently loaded) and attach it to the specified
  39. * &drm_encoder_slave. The @slave_funcs field will be initialized with
  40. * the hooks provided by the slave driver.
  41. *
  42. * If @info->platform_data is non-NULL it will be used as the initial
  43. * slave config.
  44. *
  45. * Returns 0 on success or a negative errno on failure, in particular,
  46. * -ENODEV is returned when no matching driver is found.
  47. */
  48. int drm_i2c_encoder_init(struct drm_device *dev,
  49. struct drm_encoder_slave *encoder,
  50. struct i2c_adapter *adap,
  51. const struct i2c_board_info *info)
  52. {
  53. char modalias[sizeof(I2C_MODULE_PREFIX)
  54. + I2C_NAME_SIZE];
  55. struct module *module = NULL;
  56. struct i2c_client *client;
  57. struct drm_i2c_encoder_driver *encoder_drv;
  58. int err = 0;
  59. snprintf(modalias, sizeof(modalias),
  60. "%s%s", I2C_MODULE_PREFIX, info->type);
  61. request_module(modalias);
  62. client = i2c_new_device(adap, info);
  63. if (!client) {
  64. err = -ENOMEM;
  65. goto fail;
  66. }
  67. if (!client->driver) {
  68. err = -ENODEV;
  69. goto fail_unregister;
  70. }
  71. module = client->driver->driver.owner;
  72. if (!try_module_get(module)) {
  73. err = -ENODEV;
  74. goto fail_unregister;
  75. }
  76. encoder->bus_priv = client;
  77. encoder_drv = to_drm_i2c_encoder_driver(client->driver);
  78. err = encoder_drv->encoder_init(client, dev, encoder);
  79. if (err)
  80. goto fail_unregister;
  81. if (info->platform_data)
  82. encoder->slave_funcs->set_config(&encoder->base,
  83. info->platform_data);
  84. return 0;
  85. fail_unregister:
  86. i2c_unregister_device(client);
  87. module_put(module);
  88. fail:
  89. return err;
  90. }
  91. EXPORT_SYMBOL(drm_i2c_encoder_init);
  92. /**
  93. * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
  94. * @drm_encoder: Encoder to be unregistered.
  95. *
  96. * This should be called from the @destroy method of an I2C slave
  97. * encoder driver once I2C access is no longer needed.
  98. */
  99. void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
  100. {
  101. struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
  102. struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
  103. struct module *module = client->driver->driver.owner;
  104. i2c_unregister_device(client);
  105. encoder->bus_priv = NULL;
  106. module_put(module);
  107. }
  108. EXPORT_SYMBOL(drm_i2c_encoder_destroy);