blob: d18b88b755c34f876dc2d4b9f0a202709dc87f5d [file] [log] [blame]
Francisco Jerez2066fac2009-08-02 04:19:17 +02001/*
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
Paul Gortmakere0cd3602011-08-30 11:04:30 -040027#include <linux/module.h>
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drm_encoder_slave.h>
Francisco Jerez2066fac2009-08-02 04:19:17 +020030
31/**
32 * drm_i2c_encoder_init - Initialize an I2C slave encoder
33 * @dev: DRM device.
34 * @encoder: Encoder to be attached to the I2C device. You aren't
35 * required to have called drm_encoder_init() before.
36 * @adap: I2C adapter that will be used to communicate with
37 * the device.
38 * @info: Information that will be used to create the I2C device.
39 * Required fields are @addr and @type.
40 *
41 * Create an I2C device on the specified bus (the module containing its
42 * driver is transparently loaded) and attach it to the specified
43 * &drm_encoder_slave. The @slave_funcs field will be initialized with
44 * the hooks provided by the slave driver.
45 *
Francisco Jereza1e09b62010-07-22 17:07:38 +020046 * If @info->platform_data is non-NULL it will be used as the initial
47 * slave config.
48 *
Francisco Jerez2066fac2009-08-02 04:19:17 +020049 * Returns 0 on success or a negative errno on failure, in particular,
50 * -ENODEV is returned when no matching driver is found.
51 */
52int drm_i2c_encoder_init(struct drm_device *dev,
53 struct drm_encoder_slave *encoder,
54 struct i2c_adapter *adap,
55 const struct i2c_board_info *info)
56{
Francisco Jerez2066fac2009-08-02 04:19:17 +020057 struct module *module = NULL;
58 struct i2c_client *client;
59 struct drm_i2c_encoder_driver *encoder_drv;
60 int err = 0;
61
Kees Cookd1fd3dd2013-05-07 12:32:16 -070062 request_module("%s%s", I2C_MODULE_PREFIX, info->type);
Francisco Jerez2066fac2009-08-02 04:19:17 +020063
64 client = i2c_new_device(adap, info);
65 if (!client) {
66 err = -ENOMEM;
67 goto fail;
68 }
69
Lars-Peter Clausen3ee81b72013-09-29 10:51:02 +020070 if (!client->dev.driver) {
Francisco Jerez2066fac2009-08-02 04:19:17 +020071 err = -ENODEV;
72 goto fail_unregister;
73 }
74
Lars-Peter Clausen3ee81b72013-09-29 10:51:02 +020075 module = client->dev.driver->owner;
Francisco Jerez2066fac2009-08-02 04:19:17 +020076 if (!try_module_get(module)) {
77 err = -ENODEV;
78 goto fail_unregister;
79 }
80
81 encoder->bus_priv = client;
82
Lars-Peter Clausen3ee81b72013-09-29 10:51:02 +020083 encoder_drv = to_drm_i2c_encoder_driver(to_i2c_driver(client->dev.driver));
Francisco Jerez2066fac2009-08-02 04:19:17 +020084
85 err = encoder_drv->encoder_init(client, dev, encoder);
86 if (err)
87 goto fail_unregister;
88
Francisco Jereza1e09b62010-07-22 17:07:38 +020089 if (info->platform_data)
90 encoder->slave_funcs->set_config(&encoder->base,
91 info->platform_data);
92
Francisco Jerez2066fac2009-08-02 04:19:17 +020093 return 0;
94
95fail_unregister:
96 i2c_unregister_device(client);
97 module_put(module);
98fail:
99 return err;
100}
101EXPORT_SYMBOL(drm_i2c_encoder_init);
102
103/**
104 * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
105 * @drm_encoder: Encoder to be unregistered.
106 *
107 * This should be called from the @destroy method of an I2C slave
108 * encoder driver once I2C access is no longer needed.
109 */
110void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
111{
112 struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
113 struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
Lars-Peter Clausen3ee81b72013-09-29 10:51:02 +0200114 struct module *module = client->dev.driver->owner;
Francisco Jerez2066fac2009-08-02 04:19:17 +0200115
116 i2c_unregister_device(client);
117 encoder->bus_priv = NULL;
118
119 module_put(module);
120}
121EXPORT_SYMBOL(drm_i2c_encoder_destroy);
Rob Clarka7c47d62013-01-08 17:50:48 -0600122
123/*
124 * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
125 */
126
127static inline struct drm_encoder_slave_funcs *
128get_slave_funcs(struct drm_encoder *enc)
129{
130 return to_encoder_slave(enc)->slave_funcs;
131}
132
133void drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
134{
135 get_slave_funcs(encoder)->dpms(encoder, mode);
136}
137EXPORT_SYMBOL(drm_i2c_encoder_dpms);
138
139bool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
140 const struct drm_display_mode *mode,
141 struct drm_display_mode *adjusted_mode)
142{
143 return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
144}
145EXPORT_SYMBOL(drm_i2c_encoder_mode_fixup);
146
147void drm_i2c_encoder_prepare(struct drm_encoder *encoder)
148{
149 drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
150}
151EXPORT_SYMBOL(drm_i2c_encoder_prepare);
152
153void drm_i2c_encoder_commit(struct drm_encoder *encoder)
154{
155 drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
156}
157EXPORT_SYMBOL(drm_i2c_encoder_commit);
158
159void drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
160 struct drm_display_mode *mode,
161 struct drm_display_mode *adjusted_mode)
162{
163 get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
164}
165EXPORT_SYMBOL(drm_i2c_encoder_mode_set);
166
167enum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder,
168 struct drm_connector *connector)
169{
170 return get_slave_funcs(encoder)->detect(encoder, connector);
171}
172EXPORT_SYMBOL(drm_i2c_encoder_detect);
173
174void drm_i2c_encoder_save(struct drm_encoder *encoder)
175{
176 get_slave_funcs(encoder)->save(encoder);
177}
178EXPORT_SYMBOL(drm_i2c_encoder_save);
179
180void drm_i2c_encoder_restore(struct drm_encoder *encoder)
181{
182 get_slave_funcs(encoder)->restore(encoder);
183}
184EXPORT_SYMBOL(drm_i2c_encoder_restore);