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