162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (C) 2009 Francisco Jerez.
362306a36Sopenharmony_ci * All Rights Reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
662306a36Sopenharmony_ci * a copy of this software and associated documentation files (the
762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
962306a36Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1162306a36Sopenharmony_ci * the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
1462306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial
1562306a36Sopenharmony_ci * portions of the Software.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1862306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1962306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2062306a36Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
2162306a36Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2262306a36Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2362306a36Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include <linux/module.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <drm/drm_encoder_slave.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * drm_i2c_encoder_init - Initialize an I2C slave encoder
3362306a36Sopenharmony_ci * @dev:	DRM device.
3462306a36Sopenharmony_ci * @encoder:    Encoder to be attached to the I2C device. You aren't
3562306a36Sopenharmony_ci *		required to have called drm_encoder_init() before.
3662306a36Sopenharmony_ci * @adap:	I2C adapter that will be used to communicate with
3762306a36Sopenharmony_ci *		the device.
3862306a36Sopenharmony_ci * @info:	Information that will be used to create the I2C device.
3962306a36Sopenharmony_ci *		Required fields are @addr and @type.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * Create an I2C device on the specified bus (the module containing its
4262306a36Sopenharmony_ci * driver is transparently loaded) and attach it to the specified
4362306a36Sopenharmony_ci * &drm_encoder_slave. The @slave_funcs field will be initialized with
4462306a36Sopenharmony_ci * the hooks provided by the slave driver.
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * If @info.platform_data is non-NULL it will be used as the initial
4762306a36Sopenharmony_ci * slave config.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Returns 0 on success or a negative errno on failure, in particular,
5062306a36Sopenharmony_ci * -ENODEV is returned when no matching driver is found.
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_ciint drm_i2c_encoder_init(struct drm_device *dev,
5362306a36Sopenharmony_ci			 struct drm_encoder_slave *encoder,
5462306a36Sopenharmony_ci			 struct i2c_adapter *adap,
5562306a36Sopenharmony_ci			 const struct i2c_board_info *info)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	struct module *module = NULL;
5862306a36Sopenharmony_ci	struct i2c_client *client;
5962306a36Sopenharmony_ci	struct drm_i2c_encoder_driver *encoder_drv;
6062306a36Sopenharmony_ci	int err = 0;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	request_module("%s%s", I2C_MODULE_PREFIX, info->type);
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	client = i2c_new_client_device(adap, info);
6562306a36Sopenharmony_ci	if (!i2c_client_has_driver(client)) {
6662306a36Sopenharmony_ci		err = -ENODEV;
6762306a36Sopenharmony_ci		goto fail_unregister;
6862306a36Sopenharmony_ci	}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	module = client->dev.driver->owner;
7162306a36Sopenharmony_ci	if (!try_module_get(module)) {
7262306a36Sopenharmony_ci		err = -ENODEV;
7362306a36Sopenharmony_ci		goto fail_unregister;
7462306a36Sopenharmony_ci	}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	encoder->bus_priv = client;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	encoder_drv = to_drm_i2c_encoder_driver(to_i2c_driver(client->dev.driver));
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	err = encoder_drv->encoder_init(client, dev, encoder);
8162306a36Sopenharmony_ci	if (err)
8262306a36Sopenharmony_ci		goto fail_module_put;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	if (info->platform_data)
8562306a36Sopenharmony_ci		encoder->slave_funcs->set_config(&encoder->base,
8662306a36Sopenharmony_ci						 info->platform_data);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	return 0;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cifail_module_put:
9162306a36Sopenharmony_ci	module_put(module);
9262306a36Sopenharmony_cifail_unregister:
9362306a36Sopenharmony_ci	i2c_unregister_device(client);
9462306a36Sopenharmony_ci	return err;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_init);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/**
9962306a36Sopenharmony_ci * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
10062306a36Sopenharmony_ci * @drm_encoder:	Encoder to be unregistered.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * This should be called from the @destroy method of an I2C slave
10362306a36Sopenharmony_ci * encoder driver once I2C access is no longer needed.
10462306a36Sopenharmony_ci */
10562306a36Sopenharmony_civoid drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
10862306a36Sopenharmony_ci	struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
10962306a36Sopenharmony_ci	struct module *module = client->dev.driver->owner;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	i2c_unregister_device(client);
11262306a36Sopenharmony_ci	encoder->bus_priv = NULL;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	module_put(module);
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_destroy);
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/*
11962306a36Sopenharmony_ci * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistatic inline const struct drm_encoder_slave_funcs *
12362306a36Sopenharmony_ciget_slave_funcs(struct drm_encoder *enc)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	return to_encoder_slave(enc)->slave_funcs;
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_civoid drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	get_slave_funcs(encoder)->dpms(encoder, mode);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_dpms);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cibool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
13562306a36Sopenharmony_ci		const struct drm_display_mode *mode,
13662306a36Sopenharmony_ci		struct drm_display_mode *adjusted_mode)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	if (!get_slave_funcs(encoder)->mode_fixup)
13962306a36Sopenharmony_ci		return true;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_mode_fixup);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_civoid drm_i2c_encoder_prepare(struct drm_encoder *encoder)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_prepare);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_civoid drm_i2c_encoder_commit(struct drm_encoder *encoder)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_commit);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_civoid drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
15862306a36Sopenharmony_ci		struct drm_display_mode *mode,
15962306a36Sopenharmony_ci		struct drm_display_mode *adjusted_mode)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_mode_set);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cienum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder,
16662306a36Sopenharmony_ci	    struct drm_connector *connector)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci	return get_slave_funcs(encoder)->detect(encoder, connector);
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_detect);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_civoid drm_i2c_encoder_save(struct drm_encoder *encoder)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	get_slave_funcs(encoder)->save(encoder);
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_save);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_civoid drm_i2c_encoder_restore(struct drm_encoder *encoder)
17962306a36Sopenharmony_ci{
18062306a36Sopenharmony_ci	get_slave_funcs(encoder)->restore(encoder);
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ciEXPORT_SYMBOL(drm_i2c_encoder_restore);
183