drm_encoder_slave.c revision 1.2 1 1.2 riastrad /* $NetBSD: drm_encoder_slave.c,v 1.2 2021/12/19 10:48:14 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_encoder_slave.c,v 1.2 2021/12/19 10:48:14 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/atomic.h>
37 1.1 riastrad #include <sys/errno.h>
38 1.1 riastrad #include <sys/kmem.h>
39 1.1 riastrad #include <sys/mutex.h>
40 1.1 riastrad #include <sys/rbtree.h>
41 1.1 riastrad #include <sys/systm.h>
42 1.1 riastrad
43 1.1 riastrad #include <linux/i2c.h>
44 1.1 riastrad
45 1.1 riastrad #include <drm/drm_encoder_slave.h>
46 1.1 riastrad
47 1.1 riastrad static struct {
48 1.1 riastrad kmutex_t lock;
49 1.1 riastrad rb_tree_t tree; /* struct drm_i2c_encoder_driver */
50 1.1 riastrad } drm_i2c_encoder_drivers;
51 1.1 riastrad
52 1.1 riastrad static int
53 1.1 riastrad compare_i2c_encoders(void *cookie __unused, const void *va, const void *vb)
54 1.1 riastrad {
55 1.1 riastrad const struct drm_i2c_encoder_driver *const da = va;
56 1.1 riastrad const struct drm_i2c_encoder_driver *const db = vb;
57 1.1 riastrad
58 1.1 riastrad return strncmp(da->i2c_driver.driver.name, db->i2c_driver.driver.name,
59 1.1 riastrad I2C_NAME_SIZE);
60 1.1 riastrad }
61 1.1 riastrad
62 1.1 riastrad static int
63 1.1 riastrad compare_i2c_encoder_key(void *cookie __unused, const void *n, const void *k)
64 1.1 riastrad {
65 1.1 riastrad const struct drm_i2c_encoder_driver *const driver = n;
66 1.1 riastrad const char *const name = k;
67 1.1 riastrad
68 1.1 riastrad return strncmp(driver->i2c_driver.driver.name, name, I2C_NAME_SIZE);
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad static rb_tree_ops_t drm_i2c_encoder_rb_ops = {
72 1.1 riastrad .rbto_compare_nodes = &compare_i2c_encoders,
73 1.1 riastrad .rbto_compare_key = &compare_i2c_encoder_key,
74 1.1 riastrad .rbto_node_offset = offsetof(struct drm_i2c_encoder_driver, rb_node),
75 1.1 riastrad .rbto_context = NULL,
76 1.1 riastrad };
77 1.1 riastrad
78 1.1 riastrad void
79 1.1 riastrad drm_i2c_encoders_init(void)
80 1.1 riastrad {
81 1.1 riastrad
82 1.1 riastrad mutex_init(&drm_i2c_encoder_drivers.lock, MUTEX_DEFAULT, IPL_NONE);
83 1.1 riastrad rb_tree_init(&drm_i2c_encoder_drivers.tree, &drm_i2c_encoder_rb_ops);
84 1.1 riastrad }
85 1.1 riastrad
86 1.1 riastrad void
87 1.1 riastrad drm_i2c_encoders_fini(void)
88 1.1 riastrad {
89 1.1 riastrad
90 1.1 riastrad KASSERT(RB_TREE_MIN(&drm_i2c_encoder_drivers.tree) == NULL);
91 1.1 riastrad #if 0 /* XXX no rb_tree_destroy */
92 1.1 riastrad rb_tree_destroy(&drm_i2c_encoder_drivers.tree);
93 1.1 riastrad #endif
94 1.1 riastrad mutex_destroy(&drm_i2c_encoder_drivers.lock);
95 1.1 riastrad }
96 1.1 riastrad
97 1.1 riastrad int
98 1.1 riastrad drm_i2c_encoder_register(struct module *owner __unused,
99 1.1 riastrad struct drm_i2c_encoder_driver *driver)
100 1.1 riastrad {
101 1.1 riastrad struct drm_i2c_encoder_driver *collision;
102 1.1 riastrad int ret = 0;
103 1.1 riastrad
104 1.1 riastrad mutex_enter(&drm_i2c_encoder_drivers.lock);
105 1.1 riastrad collision = rb_tree_insert_node(&drm_i2c_encoder_drivers.tree, driver);
106 1.1 riastrad if (collision != driver)
107 1.1 riastrad ret = -EEXIST;
108 1.1 riastrad mutex_exit(&drm_i2c_encoder_drivers.lock);
109 1.1 riastrad
110 1.1 riastrad return ret;
111 1.1 riastrad }
112 1.1 riastrad
113 1.1 riastrad void
114 1.1 riastrad drm_i2c_encoder_unregister(struct drm_i2c_encoder_driver *driver)
115 1.1 riastrad {
116 1.1 riastrad
117 1.1 riastrad /* XXX How to guarantee? */
118 1.1 riastrad KASSERT(driver->refcnt == 0);
119 1.1 riastrad
120 1.1 riastrad mutex_enter(&drm_i2c_encoder_drivers.lock);
121 1.1 riastrad rb_tree_remove_node(&drm_i2c_encoder_drivers.tree, driver);
122 1.1 riastrad mutex_exit(&drm_i2c_encoder_drivers.lock);
123 1.1 riastrad }
124 1.1 riastrad
125 1.1 riastrad struct drm_i2c_encoder_bus_priv {
126 1.1 riastrad struct i2c_client *i2c_client;
127 1.1 riastrad struct drm_i2c_encoder_driver *i2c_driver;
128 1.1 riastrad };
129 1.1 riastrad
130 1.1 riastrad int
131 1.1 riastrad drm_i2c_encoder_init(struct drm_device *dev, struct drm_encoder_slave *slave,
132 1.1 riastrad struct i2c_adapter *adapter, const struct i2c_board_info *info)
133 1.1 riastrad {
134 1.1 riastrad struct drm_i2c_encoder_driver *driver;
135 1.1 riastrad struct drm_i2c_encoder_bus_priv *bus_priv;
136 1.1 riastrad struct i2c_client *client;
137 1.1 riastrad unsigned refcnt;
138 1.1 riastrad int ret;
139 1.1 riastrad
140 1.1 riastrad bus_priv = kmem_alloc(sizeof(*bus_priv), KM_SLEEP);
141 1.1 riastrad slave->bus_priv = bus_priv;
142 1.1 riastrad
143 1.1 riastrad mutex_enter(&drm_i2c_encoder_drivers.lock);
144 1.1 riastrad driver = rb_tree_find_node(&drm_i2c_encoder_drivers.tree, info->type);
145 1.1 riastrad mutex_exit(&drm_i2c_encoder_drivers.lock);
146 1.1 riastrad if (driver == NULL) {
147 1.1 riastrad ret = -ENODEV;
148 1.1 riastrad goto fail0;
149 1.1 riastrad }
150 1.1 riastrad do {
151 1.1 riastrad refcnt = driver->refcnt;
152 1.1 riastrad if (refcnt == UINT_MAX) {
153 1.1 riastrad ret = -ENFILE;
154 1.1 riastrad goto fail0;
155 1.1 riastrad }
156 1.1 riastrad } while (atomic_cas_uint(&driver->refcnt, refcnt, refcnt + 1)
157 1.1 riastrad != refcnt);
158 1.1 riastrad bus_priv->i2c_driver = driver;
159 1.1 riastrad
160 1.1 riastrad client = i2c_new_device(adapter, info);
161 1.1 riastrad KASSERT(client != NULL);
162 1.1 riastrad bus_priv->i2c_client = client;
163 1.1 riastrad
164 1.1 riastrad ret = (*driver->encoder_init)(client, dev, slave);
165 1.1 riastrad if (ret)
166 1.1 riastrad goto fail1;
167 1.1 riastrad
168 1.1 riastrad if (info->platform_data)
169 1.1 riastrad (*slave->slave_funcs->set_config)(&slave->base,
170 1.1 riastrad info->platform_data);
171 1.1 riastrad
172 1.1 riastrad /* Success! */
173 1.1 riastrad return 0;
174 1.1 riastrad
175 1.1 riastrad fail1: bus_priv->i2c_client = NULL;
176 1.1 riastrad i2c_unregister_device(client);
177 1.1 riastrad bus_priv->i2c_driver = NULL;
178 1.1 riastrad atomic_dec_uint(&driver->refcnt);
179 1.1 riastrad fail0: KASSERT(ret < 0);
180 1.1 riastrad slave->bus_priv = NULL;
181 1.1 riastrad kmem_free(bus_priv, sizeof(*bus_priv));
182 1.1 riastrad return ret;
183 1.1 riastrad }
184 1.1 riastrad
185 1.1 riastrad void
186 1.1 riastrad drm_i2c_encoder_destroy(struct drm_encoder *encoder)
187 1.1 riastrad {
188 1.1 riastrad struct drm_encoder_slave *const slave = to_encoder_slave(encoder);
189 1.1 riastrad struct drm_i2c_encoder_bus_priv *const bus_priv = slave->bus_priv;
190 1.1 riastrad struct i2c_client *const client = bus_priv->i2c_client;
191 1.1 riastrad struct drm_i2c_encoder_driver *const driver = bus_priv->i2c_driver;
192 1.1 riastrad
193 1.1 riastrad bus_priv->i2c_client = NULL;
194 1.1 riastrad i2c_unregister_device(client);
195 1.1 riastrad bus_priv->i2c_driver = NULL;
196 1.1 riastrad atomic_dec_uint(&driver->refcnt);
197 1.1 riastrad }
198 1.1 riastrad
199 1.1 riastrad struct i2c_client *
200 1.1 riastrad drm_i2c_encoder_get_client(struct drm_encoder *encoder)
201 1.1 riastrad {
202 1.1 riastrad struct drm_encoder_slave *const slave = to_encoder_slave(encoder);
203 1.1 riastrad struct drm_i2c_encoder_bus_priv *const bus_priv = slave->bus_priv;
204 1.1 riastrad
205 1.1 riastrad return bus_priv->i2c_client;
206 1.1 riastrad }
207 1.1 riastrad
208 1.2 riastrad static inline const struct drm_encoder_slave_funcs *
209 1.1 riastrad slave_funcs(struct drm_encoder *encoder)
210 1.1 riastrad {
211 1.1 riastrad
212 1.1 riastrad return to_encoder_slave(encoder)->slave_funcs;
213 1.1 riastrad }
214 1.1 riastrad
215 1.1 riastrad void
216 1.1 riastrad drm_i2c_encoder_dpms(struct drm_encoder *encoder, int mode)
217 1.1 riastrad {
218 1.1 riastrad
219 1.1 riastrad (*slave_funcs(encoder)->dpms)(encoder, mode);
220 1.1 riastrad }
221 1.1 riastrad
222 1.1 riastrad void
223 1.1 riastrad drm_i2c_encoder_prepare(struct drm_encoder *encoder)
224 1.1 riastrad {
225 1.1 riastrad
226 1.1 riastrad drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
227 1.1 riastrad }
228 1.1 riastrad
229 1.1 riastrad void
230 1.1 riastrad drm_i2c_encoder_commit(struct drm_encoder *encoder)
231 1.1 riastrad {
232 1.1 riastrad
233 1.1 riastrad drm_i2c_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
234 1.1 riastrad }
235 1.1 riastrad
236 1.1 riastrad bool
237 1.1 riastrad drm_i2c_encoder_mode_fixup(struct drm_encoder *encoder,
238 1.1 riastrad const struct drm_display_mode *mode,
239 1.1 riastrad struct drm_display_mode *adjusted_mode)
240 1.1 riastrad {
241 1.1 riastrad
242 1.1 riastrad return (*slave_funcs(encoder)->mode_fixup)(encoder, mode,
243 1.1 riastrad adjusted_mode);
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad void
247 1.1 riastrad drm_i2c_encoder_mode_set(struct drm_encoder *encoder,
248 1.1 riastrad struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
249 1.1 riastrad {
250 1.1 riastrad
251 1.1 riastrad return (*slave_funcs(encoder)->mode_set)(encoder, mode, adjusted_mode);
252 1.1 riastrad }
253 1.1 riastrad
254 1.1 riastrad enum drm_connector_status
255 1.1 riastrad drm_i2c_encoder_detect(struct drm_encoder *encoder,
256 1.1 riastrad struct drm_connector *connector)
257 1.1 riastrad {
258 1.1 riastrad
259 1.1 riastrad return (*slave_funcs(encoder)->detect)(encoder, connector);
260 1.1 riastrad }
261 1.1 riastrad
262 1.1 riastrad void
263 1.1 riastrad drm_i2c_encoder_save(struct drm_encoder *encoder)
264 1.1 riastrad {
265 1.1 riastrad
266 1.1 riastrad (*slave_funcs(encoder)->save)(encoder);
267 1.1 riastrad }
268 1.1 riastrad
269 1.1 riastrad void
270 1.1 riastrad drm_i2c_encoder_restore(struct drm_encoder *encoder)
271 1.1 riastrad {
272 1.1 riastrad
273 1.1 riastrad (*slave_funcs(encoder)->restore)(encoder);
274 1.1 riastrad }
275