drm_encoder_slave.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <linux/module.h>
  27. #include <drm/drm_encoder_slave.h>
  28. /**
  29. * drm_i2c_encoder_init - Initialize an I2C slave encoder
  30. * @dev: DRM device.
  31. * @encoder: Encoder to be attached to the I2C device. You aren't
  32. * required to have called drm_encoder_init() before.
  33. * @adap: I2C adapter that will be used to communicate with
  34. * the device.
  35. * @info: Information that will be used to create the I2C device.
  36. * Required fields are @addr and @type.
  37. *
  38. * Create an I2C device on the specified bus (the module containing its
  39. * driver is transparently loaded) and attach it to the specified
  40. * &drm_encoder_slave. The @slave_funcs field will be initialized with
  41. * the hooks provided by the slave driver.
  42. *
  43. * If @info->platform_data is non-NULL it will be used as the initial
  44. * slave config.
  45. *
  46. * Returns 0 on success or a negative errno on failure, in particular,
  47. * -ENODEV is returned when no matching driver is found.
  48. */
  49. int drm_i2c_encoder_init(struct drm_device *dev,
  50. struct drm_encoder_slave *encoder,
  51. struct i2c_adapter *adap,
  52. const struct i2c_board_info *info)
  53. {
  54. char modalias[sizeof(I2C_MODULE_PREFIX)
  55. + I2C_NAME_SIZE];
  56. struct module *module = NULL;
  57. struct i2c_client *client;
  58. struct drm_i2c_encoder_driver *encoder_drv;
  59. int err = 0;
  60. snprintf(modalias, sizeof(modalias),
  61. "%s%s", I2C_MODULE_PREFIX, info->type);
  62. request_module(modalias);
  63. client = i2c_new_device(adap, info);
  64. if (!client) {
  65. err = -ENOMEM;
  66. goto fail;
  67. }
  68. if (!client->driver) {
  69. err = -ENODEV;
  70. goto fail_unregister;
  71. }
  72. module = client->driver->driver.owner;
  73. if (!try_module_get(module)) {
  74. err = -ENODEV;
  75. goto fail_unregister;
  76. }
  77. encoder->bus_priv = client;
  78. encoder_drv = to_drm_i2c_encoder_driver(client->driver);
  79. err = encoder_drv->encoder_init(client, dev, encoder);
  80. if (err)
  81. goto fail_unregister;
  82. if (info->platform_data)
  83. encoder->slave_funcs->set_config(&encoder->base,
  84. info->platform_data);
  85. return 0;
  86. fail_unregister:
  87. i2c_unregister_device(client);
  88. module_put(module);
  89. fail:
  90. return err;
  91. }
  92. EXPORT_SYMBOL(drm_i2c_encoder_init);
  93. /**
  94. * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
  95. * @drm_encoder: Encoder to be unregistered.
  96. *
  97. * This should be called from the @destroy method of an I2C slave
  98. * encoder driver once I2C access is no longer needed.
  99. */
  100. void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
  101. {
  102. struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
  103. struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
  104. struct module *module = client->driver->driver.owner;
  105. i2c_unregister_device(client);
  106. encoder->bus_priv = NULL;
  107. module_put(module);
  108. }
  109. EXPORT_SYMBOL(drm_i2c_encoder_destroy);
  110. /*
  111. * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
  112. */
  113. static inline struct drm_encoder_slave_funcs *
  114. get_slave_funcs(struct drm_encoder *enc)
  115. {
  116. return to_encoder_slave(enc)->slave_funcs;
  117. }
  118. void drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
  119. {
  120. get_slave_funcs(encoder)->dpms(encoder, mode);
  121. }
  122. EXPORT_SYMBOL(drm_i2c_encoder_dpms);
  123. bool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
  124. const struct drm_display_mode *mode,
  125. struct drm_display_mode *adjusted_mode)
  126. {
  127. return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
  128. }
  129. EXPORT_SYMBOL(drm_i2c_encoder_mode_fixup);
  130. void drm_i2c_encoder_prepare(struct drm_encoder *encoder)
  131. {
  132. drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  133. }
  134. EXPORT_SYMBOL(drm_i2c_encoder_prepare);
  135. void drm_i2c_encoder_commit(struct drm_encoder *encoder)
  136. {
  137. drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  138. }
  139. EXPORT_SYMBOL(drm_i2c_encoder_commit);
  140. void drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
  141. struct drm_display_mode *mode,
  142. struct drm_display_mode *adjusted_mode)
  143. {
  144. get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
  145. }
  146. EXPORT_SYMBOL(drm_i2c_encoder_mode_set);
  147. enum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder,
  148. struct drm_connector *connector)
  149. {
  150. return get_slave_funcs(encoder)->detect(encoder, connector);
  151. }
  152. EXPORT_SYMBOL(drm_i2c_encoder_detect);
  153. void drm_i2c_encoder_save(struct drm_encoder *encoder)
  154. {
  155. get_slave_funcs(encoder)->save(encoder);
  156. }
  157. EXPORT_SYMBOL(drm_i2c_encoder_save);
  158. void drm_i2c_encoder_restore(struct drm_encoder *encoder)
  159. {
  160. get_slave_funcs(encoder)->restore(encoder);
  161. }
  162. EXPORT_SYMBOL(drm_i2c_encoder_restore);