1 /* $NetBSD: drm_encoder_slave.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $ */ 2 3 /* 4 * Copyright (C) 2009 Francisco Jerez. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial 17 * portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: drm_encoder_slave.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $"); 31 32 #include <linux/module.h> 33 34 #include <drm/drm_encoder_slave.h> 35 36 /** 37 * drm_i2c_encoder_init - Initialize an I2C slave encoder 38 * @dev: DRM device. 39 * @encoder: Encoder to be attached to the I2C device. You aren't 40 * required to have called drm_encoder_init() before. 41 * @adap: I2C adapter that will be used to communicate with 42 * the device. 43 * @info: Information that will be used to create the I2C device. 44 * Required fields are @addr and @type. 45 * 46 * Create an I2C device on the specified bus (the module containing its 47 * driver is transparently loaded) and attach it to the specified 48 * &drm_encoder_slave. The @slave_funcs field will be initialized with 49 * the hooks provided by the slave driver. 50 * 51 * If @info->platform_data is non-NULL it will be used as the initial 52 * slave config. 53 * 54 * Returns 0 on success or a negative errno on failure, in particular, 55 * -ENODEV is returned when no matching driver is found. 56 */ 57 int drm_i2c_encoder_init(struct drm_device *dev, 58 struct drm_encoder_slave *encoder, 59 struct i2c_adapter *adap, 60 const struct i2c_board_info *info) 61 { 62 struct module *module = NULL; 63 struct i2c_client *client; 64 struct drm_i2c_encoder_driver *encoder_drv; 65 int err = 0; 66 67 request_module("%s%s", I2C_MODULE_PREFIX, info->type); 68 69 client = i2c_new_device(adap, info); 70 if (!client) { 71 err = -ENOMEM; 72 goto fail; 73 } 74 75 if (!client->dev.driver) { 76 err = -ENODEV; 77 goto fail_unregister; 78 } 79 80 module = client->dev.driver->owner; 81 if (!try_module_get(module)) { 82 err = -ENODEV; 83 goto fail_unregister; 84 } 85 86 encoder->bus_priv = client; 87 88 encoder_drv = to_drm_i2c_encoder_driver(to_i2c_driver(client->dev.driver)); 89 90 err = encoder_drv->encoder_init(client, dev, encoder); 91 if (err) 92 goto fail_unregister; 93 94 if (info->platform_data) 95 encoder->slave_funcs->set_config(&encoder->base, 96 info->platform_data); 97 98 return 0; 99 100 fail_unregister: 101 i2c_unregister_device(client); 102 module_put(module); 103 fail: 104 return err; 105 } 106 EXPORT_SYMBOL(drm_i2c_encoder_init); 107 108 /** 109 * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder 110 * @drm_encoder: Encoder to be unregistered. 111 * 112 * This should be called from the @destroy method of an I2C slave 113 * encoder driver once I2C access is no longer needed. 114 */ 115 void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder) 116 { 117 struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder); 118 struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder); 119 struct module *module = client->dev.driver->owner; 120 121 i2c_unregister_device(client); 122 encoder->bus_priv = NULL; 123 124 module_put(module); 125 } 126 EXPORT_SYMBOL(drm_i2c_encoder_destroy); 127 128 /* 129 * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs: 130 */ 131 132 static inline struct drm_encoder_slave_funcs * 133 get_slave_funcs(struct drm_encoder *enc) 134 { 135 return to_encoder_slave(enc)->slave_funcs; 136 } 137 138 void drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode) 139 { 140 get_slave_funcs(encoder)->dpms(encoder, mode); 141 } 142 EXPORT_SYMBOL(drm_i2c_encoder_dpms); 143 144 bool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder, 145 const struct drm_display_mode *mode, 146 struct drm_display_mode *adjusted_mode) 147 { 148 return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode); 149 } 150 EXPORT_SYMBOL(drm_i2c_encoder_mode_fixup); 151 152 void drm_i2c_encoder_prepare(struct drm_encoder *encoder) 153 { 154 drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF); 155 } 156 EXPORT_SYMBOL(drm_i2c_encoder_prepare); 157 158 void drm_i2c_encoder_commit(struct drm_encoder *encoder) 159 { 160 drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON); 161 } 162 EXPORT_SYMBOL(drm_i2c_encoder_commit); 163 164 void drm_i2c_encoder_mode_set(struct drm_encoder *encoder, 165 struct drm_display_mode *mode, 166 struct drm_display_mode *adjusted_mode) 167 { 168 get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode); 169 } 170 EXPORT_SYMBOL(drm_i2c_encoder_mode_set); 171 172 enum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder, 173 struct drm_connector *connector) 174 { 175 return get_slave_funcs(encoder)->detect(encoder, connector); 176 } 177 EXPORT_SYMBOL(drm_i2c_encoder_detect); 178 179 void drm_i2c_encoder_save(struct drm_encoder *encoder) 180 { 181 get_slave_funcs(encoder)->save(encoder); 182 } 183 EXPORT_SYMBOL(drm_i2c_encoder_save); 184 185 void drm_i2c_encoder_restore(struct drm_encoder *encoder) 186 { 187 get_slave_funcs(encoder)->restore(encoder); 188 } 189 EXPORT_SYMBOL(drm_i2c_encoder_restore); 190