Path: blob/master/drivers/gpu/drm/drm_encoder_slave.c
15111 views
/*1* Copyright (C) 2009 Francisco Jerez.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining5* a copy of this software and associated documentation files (the6* "Software"), to deal in the Software without restriction, including7* without limitation the rights to use, copy, modify, merge, publish,8* distribute, sublicense, and/or sell copies of the Software, and to9* permit persons to whom the Software is furnished to do so, subject to10* the following conditions:11*12* The above copyright notice and this permission notice (including the13* next paragraph) shall be included in all copies or substantial14* portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.19* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE20* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*24*/2526#include "drm_encoder_slave.h"2728/**29* drm_i2c_encoder_init - Initialize an I2C slave encoder30* @dev: DRM device.31* @encoder: Encoder to be attached to the I2C device. You aren't32* required to have called drm_encoder_init() before.33* @adap: I2C adapter that will be used to communicate with34* 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 its39* driver is transparently loaded) and attach it to the specified40* &drm_encoder_slave. The @slave_funcs field will be initialized with41* the hooks provided by the slave driver.42*43* If @info->platform_data is non-NULL it will be used as the initial44* 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*/49int drm_i2c_encoder_init(struct drm_device *dev,50struct drm_encoder_slave *encoder,51struct i2c_adapter *adap,52const struct i2c_board_info *info)53{54char modalias[sizeof(I2C_MODULE_PREFIX)55+ I2C_NAME_SIZE];56struct module *module = NULL;57struct i2c_client *client;58struct drm_i2c_encoder_driver *encoder_drv;59int err = 0;6061snprintf(modalias, sizeof(modalias),62"%s%s", I2C_MODULE_PREFIX, info->type);63request_module(modalias);6465client = i2c_new_device(adap, info);66if (!client) {67err = -ENOMEM;68goto fail;69}7071if (!client->driver) {72err = -ENODEV;73goto fail_unregister;74}7576module = client->driver->driver.owner;77if (!try_module_get(module)) {78err = -ENODEV;79goto fail_unregister;80}8182encoder->bus_priv = client;8384encoder_drv = to_drm_i2c_encoder_driver(client->driver);8586err = encoder_drv->encoder_init(client, dev, encoder);87if (err)88goto fail_unregister;8990if (info->platform_data)91encoder->slave_funcs->set_config(&encoder->base,92info->platform_data);9394return 0;9596fail_unregister:97i2c_unregister_device(client);98module_put(module);99fail:100return err;101}102EXPORT_SYMBOL(drm_i2c_encoder_init);103104/**105* drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder106* @drm_encoder: Encoder to be unregistered.107*108* This should be called from the @destroy method of an I2C slave109* encoder driver once I2C access is no longer needed.110*/111void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)112{113struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);114struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);115struct module *module = client->driver->driver.owner;116117i2c_unregister_device(client);118encoder->bus_priv = NULL;119120module_put(module);121}122EXPORT_SYMBOL(drm_i2c_encoder_destroy);123124125