Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2009 Francisco Jerez.
0003  * All Rights Reserved.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining
0006  * a copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sublicense, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * The above copyright notice and this permission notice (including the
0014  * next paragraph) shall be included in all copies or substantial
0015  * portions of the Software.
0016  *
0017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0018  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0020  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
0021  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0022  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0023  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0024  *
0025  */
0026 
0027 #include <linux/module.h>
0028 
0029 #include <drm/drm_encoder_slave.h>
0030 
0031 /**
0032  * drm_i2c_encoder_init - Initialize an I2C slave encoder
0033  * @dev:    DRM device.
0034  * @encoder:    Encoder to be attached to the I2C device. You aren't
0035  *      required to have called drm_encoder_init() before.
0036  * @adap:   I2C adapter that will be used to communicate with
0037  *      the device.
0038  * @info:   Information that will be used to create the I2C device.
0039  *      Required fields are @addr and @type.
0040  *
0041  * Create an I2C device on the specified bus (the module containing its
0042  * driver is transparently loaded) and attach it to the specified
0043  * &drm_encoder_slave. The @slave_funcs field will be initialized with
0044  * the hooks provided by the slave driver.
0045  *
0046  * If @info.platform_data is non-NULL it will be used as the initial
0047  * slave config.
0048  *
0049  * Returns 0 on success or a negative errno on failure, in particular,
0050  * -ENODEV is returned when no matching driver is found.
0051  */
0052 int drm_i2c_encoder_init(struct drm_device *dev,
0053              struct drm_encoder_slave *encoder,
0054              struct i2c_adapter *adap,
0055              const struct i2c_board_info *info)
0056 {
0057     struct module *module = NULL;
0058     struct i2c_client *client;
0059     struct drm_i2c_encoder_driver *encoder_drv;
0060     int err = 0;
0061 
0062     request_module("%s%s", I2C_MODULE_PREFIX, info->type);
0063 
0064     client = i2c_new_client_device(adap, info);
0065     if (!i2c_client_has_driver(client)) {
0066         err = -ENODEV;
0067         goto fail_unregister;
0068     }
0069 
0070     module = client->dev.driver->owner;
0071     if (!try_module_get(module)) {
0072         err = -ENODEV;
0073         goto fail_unregister;
0074     }
0075 
0076     encoder->bus_priv = client;
0077 
0078     encoder_drv = to_drm_i2c_encoder_driver(to_i2c_driver(client->dev.driver));
0079 
0080     err = encoder_drv->encoder_init(client, dev, encoder);
0081     if (err)
0082         goto fail_module_put;
0083 
0084     if (info->platform_data)
0085         encoder->slave_funcs->set_config(&encoder->base,
0086                          info->platform_data);
0087 
0088     return 0;
0089 
0090 fail_module_put:
0091     module_put(module);
0092 fail_unregister:
0093     i2c_unregister_device(client);
0094     return err;
0095 }
0096 EXPORT_SYMBOL(drm_i2c_encoder_init);
0097 
0098 /**
0099  * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
0100  * @drm_encoder:    Encoder to be unregistered.
0101  *
0102  * This should be called from the @destroy method of an I2C slave
0103  * encoder driver once I2C access is no longer needed.
0104  */
0105 void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
0106 {
0107     struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
0108     struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
0109     struct module *module = client->dev.driver->owner;
0110 
0111     i2c_unregister_device(client);
0112     encoder->bus_priv = NULL;
0113 
0114     module_put(module);
0115 }
0116 EXPORT_SYMBOL(drm_i2c_encoder_destroy);
0117 
0118 /*
0119  * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
0120  */
0121 
0122 static inline const struct drm_encoder_slave_funcs *
0123 get_slave_funcs(struct drm_encoder *enc)
0124 {
0125     return to_encoder_slave(enc)->slave_funcs;
0126 }
0127 
0128 void drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
0129 {
0130     get_slave_funcs(encoder)->dpms(encoder, mode);
0131 }
0132 EXPORT_SYMBOL(drm_i2c_encoder_dpms);
0133 
0134 bool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
0135         const struct drm_display_mode *mode,
0136         struct drm_display_mode *adjusted_mode)
0137 {
0138     if (!get_slave_funcs(encoder)->mode_fixup)
0139         return true;
0140 
0141     return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
0142 }
0143 EXPORT_SYMBOL(drm_i2c_encoder_mode_fixup);
0144 
0145 void drm_i2c_encoder_prepare(struct drm_encoder *encoder)
0146 {
0147     drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
0148 }
0149 EXPORT_SYMBOL(drm_i2c_encoder_prepare);
0150 
0151 void drm_i2c_encoder_commit(struct drm_encoder *encoder)
0152 {
0153     drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
0154 }
0155 EXPORT_SYMBOL(drm_i2c_encoder_commit);
0156 
0157 void drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
0158         struct drm_display_mode *mode,
0159         struct drm_display_mode *adjusted_mode)
0160 {
0161     get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
0162 }
0163 EXPORT_SYMBOL(drm_i2c_encoder_mode_set);
0164 
0165 enum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder,
0166         struct drm_connector *connector)
0167 {
0168     return get_slave_funcs(encoder)->detect(encoder, connector);
0169 }
0170 EXPORT_SYMBOL(drm_i2c_encoder_detect);
0171 
0172 void drm_i2c_encoder_save(struct drm_encoder *encoder)
0173 {
0174     get_slave_funcs(encoder)->save(encoder);
0175 }
0176 EXPORT_SYMBOL(drm_i2c_encoder_save);
0177 
0178 void drm_i2c_encoder_restore(struct drm_encoder *encoder)
0179 {
0180     get_slave_funcs(encoder)->restore(encoder);
0181 }
0182 EXPORT_SYMBOL(drm_i2c_encoder_restore);