1*41ec0267Sriastradh /* $NetBSD: drm_encoder_slave.c,v 1.3 2021/12/18 23:44:57 riastradh Exp $ */
2efa246c0Sriastradh
3fcd0cb28Sriastradh /*
4fcd0cb28Sriastradh * Copyright (C) 2009 Francisco Jerez.
5fcd0cb28Sriastradh * All Rights Reserved.
6fcd0cb28Sriastradh *
7fcd0cb28Sriastradh * Permission is hereby granted, free of charge, to any person obtaining
8fcd0cb28Sriastradh * a copy of this software and associated documentation files (the
9fcd0cb28Sriastradh * "Software"), to deal in the Software without restriction, including
10fcd0cb28Sriastradh * without limitation the rights to use, copy, modify, merge, publish,
11fcd0cb28Sriastradh * distribute, sublicense, and/or sell copies of the Software, and to
12fcd0cb28Sriastradh * permit persons to whom the Software is furnished to do so, subject to
13fcd0cb28Sriastradh * the following conditions:
14fcd0cb28Sriastradh *
15fcd0cb28Sriastradh * The above copyright notice and this permission notice (including the
16fcd0cb28Sriastradh * next paragraph) shall be included in all copies or substantial
17fcd0cb28Sriastradh * portions of the Software.
18fcd0cb28Sriastradh *
19fcd0cb28Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20fcd0cb28Sriastradh * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21fcd0cb28Sriastradh * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22fcd0cb28Sriastradh * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23fcd0cb28Sriastradh * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24fcd0cb28Sriastradh * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25fcd0cb28Sriastradh * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26fcd0cb28Sriastradh *
27fcd0cb28Sriastradh */
28fcd0cb28Sriastradh
29efa246c0Sriastradh #include <sys/cdefs.h>
30*41ec0267Sriastradh __KERNEL_RCSID(0, "$NetBSD: drm_encoder_slave.c,v 1.3 2021/12/18 23:44:57 riastradh Exp $");
31efa246c0Sriastradh
32fcd0cb28Sriastradh #include <linux/module.h>
33fcd0cb28Sriastradh
34fcd0cb28Sriastradh #include <drm/drm_encoder_slave.h>
35fcd0cb28Sriastradh
36fcd0cb28Sriastradh /**
37fcd0cb28Sriastradh * drm_i2c_encoder_init - Initialize an I2C slave encoder
38fcd0cb28Sriastradh * @dev: DRM device.
39fcd0cb28Sriastradh * @encoder: Encoder to be attached to the I2C device. You aren't
40fcd0cb28Sriastradh * required to have called drm_encoder_init() before.
41fcd0cb28Sriastradh * @adap: I2C adapter that will be used to communicate with
42fcd0cb28Sriastradh * the device.
43fcd0cb28Sriastradh * @info: Information that will be used to create the I2C device.
44fcd0cb28Sriastradh * Required fields are @addr and @type.
45fcd0cb28Sriastradh *
46fcd0cb28Sriastradh * Create an I2C device on the specified bus (the module containing its
47fcd0cb28Sriastradh * driver is transparently loaded) and attach it to the specified
48fcd0cb28Sriastradh * &drm_encoder_slave. The @slave_funcs field will be initialized with
49fcd0cb28Sriastradh * the hooks provided by the slave driver.
50fcd0cb28Sriastradh *
51*41ec0267Sriastradh * If @info.platform_data is non-NULL it will be used as the initial
52fcd0cb28Sriastradh * slave config.
53fcd0cb28Sriastradh *
54fcd0cb28Sriastradh * Returns 0 on success or a negative errno on failure, in particular,
55fcd0cb28Sriastradh * -ENODEV is returned when no matching driver is found.
56fcd0cb28Sriastradh */
drm_i2c_encoder_init(struct drm_device * dev,struct drm_encoder_slave * encoder,struct i2c_adapter * adap,const struct i2c_board_info * info)57fcd0cb28Sriastradh int drm_i2c_encoder_init(struct drm_device *dev,
58fcd0cb28Sriastradh struct drm_encoder_slave *encoder,
59fcd0cb28Sriastradh struct i2c_adapter *adap,
60fcd0cb28Sriastradh const struct i2c_board_info *info)
61fcd0cb28Sriastradh {
62fcd0cb28Sriastradh struct module *module = NULL;
63fcd0cb28Sriastradh struct i2c_client *client;
64fcd0cb28Sriastradh struct drm_i2c_encoder_driver *encoder_drv;
65fcd0cb28Sriastradh int err = 0;
66fcd0cb28Sriastradh
679d20d926Sriastradh request_module("%s%s", I2C_MODULE_PREFIX, info->type);
68fcd0cb28Sriastradh
69fcd0cb28Sriastradh client = i2c_new_device(adap, info);
70fcd0cb28Sriastradh if (!client) {
71fcd0cb28Sriastradh err = -ENOMEM;
72fcd0cb28Sriastradh goto fail;
73fcd0cb28Sriastradh }
74fcd0cb28Sriastradh
759d20d926Sriastradh if (!client->dev.driver) {
76fcd0cb28Sriastradh err = -ENODEV;
77fcd0cb28Sriastradh goto fail_unregister;
78fcd0cb28Sriastradh }
79fcd0cb28Sriastradh
809d20d926Sriastradh module = client->dev.driver->owner;
81fcd0cb28Sriastradh if (!try_module_get(module)) {
82fcd0cb28Sriastradh err = -ENODEV;
83fcd0cb28Sriastradh goto fail_unregister;
84fcd0cb28Sriastradh }
85fcd0cb28Sriastradh
86fcd0cb28Sriastradh encoder->bus_priv = client;
87fcd0cb28Sriastradh
889d20d926Sriastradh encoder_drv = to_drm_i2c_encoder_driver(to_i2c_driver(client->dev.driver));
89fcd0cb28Sriastradh
90fcd0cb28Sriastradh err = encoder_drv->encoder_init(client, dev, encoder);
91fcd0cb28Sriastradh if (err)
92fcd0cb28Sriastradh goto fail_unregister;
93fcd0cb28Sriastradh
94fcd0cb28Sriastradh if (info->platform_data)
95fcd0cb28Sriastradh encoder->slave_funcs->set_config(&encoder->base,
96fcd0cb28Sriastradh info->platform_data);
97fcd0cb28Sriastradh
98fcd0cb28Sriastradh return 0;
99fcd0cb28Sriastradh
100fcd0cb28Sriastradh fail_unregister:
101fcd0cb28Sriastradh i2c_unregister_device(client);
102fcd0cb28Sriastradh module_put(module);
103fcd0cb28Sriastradh fail:
104fcd0cb28Sriastradh return err;
105fcd0cb28Sriastradh }
106fcd0cb28Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_init);
107fcd0cb28Sriastradh
108fcd0cb28Sriastradh /**
109fcd0cb28Sriastradh * drm_i2c_encoder_destroy - Unregister the I2C device backing an encoder
110fcd0cb28Sriastradh * @drm_encoder: Encoder to be unregistered.
111fcd0cb28Sriastradh *
112fcd0cb28Sriastradh * This should be called from the @destroy method of an I2C slave
113fcd0cb28Sriastradh * encoder driver once I2C access is no longer needed.
114fcd0cb28Sriastradh */
drm_i2c_encoder_destroy(struct drm_encoder * drm_encoder)115fcd0cb28Sriastradh void drm_i2c_encoder_destroy(struct drm_encoder *drm_encoder)
116fcd0cb28Sriastradh {
117fcd0cb28Sriastradh struct drm_encoder_slave *encoder = to_encoder_slave(drm_encoder);
118fcd0cb28Sriastradh struct i2c_client *client = drm_i2c_encoder_get_client(drm_encoder);
1199d20d926Sriastradh struct module *module = client->dev.driver->owner;
120fcd0cb28Sriastradh
121fcd0cb28Sriastradh i2c_unregister_device(client);
122fcd0cb28Sriastradh encoder->bus_priv = NULL;
123fcd0cb28Sriastradh
124fcd0cb28Sriastradh module_put(module);
125fcd0cb28Sriastradh }
126fcd0cb28Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_destroy);
1279d20d926Sriastradh
1289d20d926Sriastradh /*
1299d20d926Sriastradh * Wrapper fxns which can be plugged in to drm_encoder_helper_funcs:
1309d20d926Sriastradh */
1319d20d926Sriastradh
132*41ec0267Sriastradh static inline const struct drm_encoder_slave_funcs *
get_slave_funcs(struct drm_encoder * enc)1339d20d926Sriastradh get_slave_funcs(struct drm_encoder *enc)
1349d20d926Sriastradh {
1359d20d926Sriastradh return to_encoder_slave(enc)->slave_funcs;
1369d20d926Sriastradh }
1379d20d926Sriastradh
drm_i2c_encoder_dpms(struct drm_encoder * encoder,int mode)1389d20d926Sriastradh void drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
1399d20d926Sriastradh {
1409d20d926Sriastradh get_slave_funcs(encoder)->dpms(encoder, mode);
1419d20d926Sriastradh }
1429d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_dpms);
1439d20d926Sriastradh
drm_i2c_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)1449d20d926Sriastradh bool drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
1459d20d926Sriastradh const struct drm_display_mode *mode,
1469d20d926Sriastradh struct drm_display_mode *adjusted_mode)
1479d20d926Sriastradh {
148*41ec0267Sriastradh if (!get_slave_funcs(encoder)->mode_fixup)
149*41ec0267Sriastradh return true;
150*41ec0267Sriastradh
1519d20d926Sriastradh return get_slave_funcs(encoder)->mode_fixup(encoder, mode, adjusted_mode);
1529d20d926Sriastradh }
1539d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_mode_fixup);
1549d20d926Sriastradh
drm_i2c_encoder_prepare(struct drm_encoder * encoder)1559d20d926Sriastradh void drm_i2c_encoder_prepare(struct drm_encoder *encoder)
1569d20d926Sriastradh {
1579d20d926Sriastradh drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
1589d20d926Sriastradh }
1599d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_prepare);
1609d20d926Sriastradh
drm_i2c_encoder_commit(struct drm_encoder * encoder)1619d20d926Sriastradh void drm_i2c_encoder_commit(struct drm_encoder *encoder)
1629d20d926Sriastradh {
1639d20d926Sriastradh drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
1649d20d926Sriastradh }
1659d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_commit);
1669d20d926Sriastradh
drm_i2c_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)1679d20d926Sriastradh void drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
1689d20d926Sriastradh struct drm_display_mode *mode,
1699d20d926Sriastradh struct drm_display_mode *adjusted_mode)
1709d20d926Sriastradh {
1719d20d926Sriastradh get_slave_funcs(encoder)->mode_set(encoder, mode, adjusted_mode);
1729d20d926Sriastradh }
1739d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_mode_set);
1749d20d926Sriastradh
drm_i2c_encoder_detect(struct drm_encoder * encoder,struct drm_connector * connector)1759d20d926Sriastradh enum drm_connector_status drm_i2c_encoder_detect(struct drm_encoder *encoder,
1769d20d926Sriastradh struct drm_connector *connector)
1779d20d926Sriastradh {
1789d20d926Sriastradh return get_slave_funcs(encoder)->detect(encoder, connector);
1799d20d926Sriastradh }
1809d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_detect);
1819d20d926Sriastradh
drm_i2c_encoder_save(struct drm_encoder * encoder)1829d20d926Sriastradh void drm_i2c_encoder_save(struct drm_encoder *encoder)
1839d20d926Sriastradh {
1849d20d926Sriastradh get_slave_funcs(encoder)->save(encoder);
1859d20d926Sriastradh }
1869d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_save);
1879d20d926Sriastradh
drm_i2c_encoder_restore(struct drm_encoder * encoder)1889d20d926Sriastradh void drm_i2c_encoder_restore(struct drm_encoder *encoder)
1899d20d926Sriastradh {
1909d20d926Sriastradh get_slave_funcs(encoder)->restore(encoder);
1919d20d926Sriastradh }
1929d20d926Sriastradh EXPORT_SYMBOL(drm_i2c_encoder_restore);
193