1 /*
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
27 #include <linux/module.h>
28
29 #include <dispnv04/i2c/encoder_i2c.h>
30
31 /**
32 * nouveau_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 * &nouveau_i2c_encoder. The @encoder_i2c_funcs field will be initialized with
44 * the hooks provided by the slave driver.
45 *
46 * If @info.platform_data is non-NULL it will be used as the initial
47 * slave config.
48 *
49 * Returns 0 on success or a negative errno on failure, in particular,
50 * -ENODEV is returned when no matching driver is found.
51 */
nouveau_i2c_encoder_init(struct drm_device * dev,struct nouveau_i2c_encoder * encoder,struct i2c_adapter * adap,const struct i2c_board_info * info)52 int nouveau_i2c_encoder_init(struct drm_device *dev,
53 struct nouveau_i2c_encoder *encoder,
54 struct i2c_adapter *adap,
55 const struct i2c_board_info *info)
56 {
57 struct module *module = NULL;
58 struct i2c_client *client;
59 struct nouveau_i2c_encoder_driver *encoder_drv;
60 int err = 0;
61
62 request_module("%s%s", I2C_MODULE_PREFIX, info->type);
63
64 client = i2c_new_client_device(adap, info);
65 if (!i2c_client_has_driver(client)) {
66 err = -ENODEV;
67 goto fail_unregister;
68 }
69
70 module = client->dev.driver->owner;
71 if (!try_module_get(module)) {
72 err = -ENODEV;
73 goto fail_unregister;
74 }
75
76 encoder->i2c_client = client;
77
78 encoder_drv = to_nouveau_i2c_encoder_driver(to_i2c_driver(client->dev.driver));
79
80 err = encoder_drv->encoder_init(client, dev, encoder);
81 if (err)
82 goto fail_module_put;
83
84 if (info->platform_data)
85 encoder->encoder_i2c_funcs->set_config(&encoder->base,
86 info->platform_data);
87
88 return 0;
89
90 fail_module_put:
91 module_put(module);
92 fail_unregister:
93 i2c_unregister_device(client);
94 return err;
95 }
96
97 /**
98 * nouveau_i2c_encoder_destroy - Unregister the I2C device backing an encoder
99 * @drm_encoder: Encoder to be unregistered.
100 *
101 * This should be called from the @destroy method of an I2C slave
102 * encoder driver once I2C access is no longer needed.
103 */
nouveau_i2c_encoder_destroy(struct drm_encoder * drm_encoder)104 void nouveau_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
105 {
106 struct nouveau_i2c_encoder *encoder = to_encoder_i2c(drm_encoder);
107 struct i2c_client *client = nouveau_i2c_encoder_get_client(drm_encoder);
108 struct module *module = client->dev.driver->owner;
109
110 i2c_unregister_device(client);
111 encoder->i2c_client = NULL;
112
113 module_put(module);
114 }
115 EXPORT_SYMBOL(nouveau_i2c_encoder_destroy);
116
117 /*
118 * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
119 */
120
nouveau_i2c_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)121 bool nouveau_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
122 const struct drm_display_mode *mode,
123 struct drm_display_mode *adjusted_mode)
124 {
125 if (!get_encoder_i2c_funcs(encoder)->mode_fixup)
126 return true;
127
128 return get_encoder_i2c_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
129 }
130
nouveau_i2c_encoder_detect(struct drm_encoder * encoder,struct drm_connector * connector)131 enum drm_connector_status nouveau_i2c_encoder_detect(struct drm_encoder *encoder,
132 struct drm_connector *connector)
133 {
134 return get_encoder_i2c_funcs(encoder)->detect(encoder, connector);
135 }
136
nouveau_i2c_encoder_save(struct drm_encoder * encoder)137 void nouveau_i2c_encoder_save(struct drm_encoder *encoder)
138 {
139 get_encoder_i2c_funcs(encoder)->save(encoder);
140 }
141
nouveau_i2c_encoder_restore(struct drm_encoder * encoder)142 void nouveau_i2c_encoder_restore(struct drm_encoder *encoder)
143 {
144 get_encoder_i2c_funcs(encoder)->restore(encoder);
145 }
146