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