xref: /linux/drivers/gpu/drm/i2c/sil164_drv.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 /*
2  * Copyright (C) 2010 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 <drm/drmP.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/drm_encoder_slave.h>
32 #include <drm/i2c/sil164.h>
33 
34 struct sil164_priv {
35 	struct sil164_encoder_params config;
36 	struct i2c_client *duallink_slave;
37 
38 	uint8_t saved_state[0x10];
39 	uint8_t saved_slave_state[0x10];
40 };
41 
42 #define to_sil164_priv(x) \
43 	((struct sil164_priv *)to_encoder_slave(x)->slave_priv)
44 
45 #define sil164_dbg(client, format, ...) do {				\
46 		if (drm_debug & DRM_UT_KMS)				\
47 			dev_printk(KERN_DEBUG, &client->dev,		\
48 				   "%s: " format, __func__, ## __VA_ARGS__); \
49 	} while (0)
50 #define sil164_info(client, format, ...)		\
51 	dev_info(&client->dev, format, __VA_ARGS__)
52 #define sil164_err(client, format, ...)			\
53 	dev_err(&client->dev, format, __VA_ARGS__)
54 
55 #define SIL164_I2C_ADDR_MASTER			0x38
56 #define SIL164_I2C_ADDR_SLAVE			0x39
57 
58 /* HW register definitions */
59 
60 #define SIL164_VENDOR_LO			0x0
61 #define SIL164_VENDOR_HI			0x1
62 #define SIL164_DEVICE_LO			0x2
63 #define SIL164_DEVICE_HI			0x3
64 #define SIL164_REVISION				0x4
65 #define SIL164_FREQ_MIN				0x6
66 #define SIL164_FREQ_MAX				0x7
67 #define SIL164_CONTROL0				0x8
68 #  define SIL164_CONTROL0_POWER_ON		0x01
69 #  define SIL164_CONTROL0_EDGE_RISING		0x02
70 #  define SIL164_CONTROL0_INPUT_24BIT		0x04
71 #  define SIL164_CONTROL0_DUAL_EDGE		0x08
72 #  define SIL164_CONTROL0_HSYNC_ON		0x10
73 #  define SIL164_CONTROL0_VSYNC_ON		0x20
74 #define SIL164_DETECT				0x9
75 #  define SIL164_DETECT_INTR_STAT		0x01
76 #  define SIL164_DETECT_HOTPLUG_STAT		0x02
77 #  define SIL164_DETECT_RECEIVER_STAT		0x04
78 #  define SIL164_DETECT_INTR_MODE_RECEIVER	0x00
79 #  define SIL164_DETECT_INTR_MODE_HOTPLUG	0x08
80 #  define SIL164_DETECT_OUT_MODE_HIGH		0x00
81 #  define SIL164_DETECT_OUT_MODE_INTR		0x10
82 #  define SIL164_DETECT_OUT_MODE_RECEIVER	0x20
83 #  define SIL164_DETECT_OUT_MODE_HOTPLUG	0x30
84 #  define SIL164_DETECT_VSWING_STAT		0x80
85 #define SIL164_CONTROL1				0xa
86 #  define SIL164_CONTROL1_DESKEW_ENABLE		0x10
87 #  define SIL164_CONTROL1_DESKEW_INCR_SHIFT	5
88 #define SIL164_GPIO				0xb
89 #define SIL164_CONTROL2				0xc
90 #  define SIL164_CONTROL2_FILTER_ENABLE		0x01
91 #  define SIL164_CONTROL2_FILTER_SETTING_SHIFT	1
92 #  define SIL164_CONTROL2_DUALLINK_MASTER	0x40
93 #  define SIL164_CONTROL2_SYNC_CONT		0x80
94 #define SIL164_DUALLINK				0xd
95 #  define SIL164_DUALLINK_ENABLE		0x10
96 #  define SIL164_DUALLINK_SKEW_SHIFT		5
97 #define SIL164_PLLZONE				0xe
98 #  define SIL164_PLLZONE_STAT			0x08
99 #  define SIL164_PLLZONE_FORCE_ON		0x10
100 #  define SIL164_PLLZONE_FORCE_HIGH		0x20
101 
102 /* HW access functions */
103 
104 static void
105 sil164_write(struct i2c_client *client, uint8_t addr, uint8_t val)
106 {
107 	uint8_t buf[] = {addr, val};
108 	int ret;
109 
110 	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
111 	if (ret < 0)
112 		sil164_err(client, "Error %d writing to subaddress 0x%x\n",
113 			   ret, addr);
114 }
115 
116 static uint8_t
117 sil164_read(struct i2c_client *client, uint8_t addr)
118 {
119 	uint8_t val;
120 	int ret;
121 
122 	ret = i2c_master_send(client, &addr, sizeof(addr));
123 	if (ret < 0)
124 		goto fail;
125 
126 	ret = i2c_master_recv(client, &val, sizeof(val));
127 	if (ret < 0)
128 		goto fail;
129 
130 	return val;
131 
132 fail:
133 	sil164_err(client, "Error %d reading from subaddress 0x%x\n",
134 		   ret, addr);
135 	return 0;
136 }
137 
138 static void
139 sil164_save_state(struct i2c_client *client, uint8_t *state)
140 {
141 	int i;
142 
143 	for (i = 0x8; i <= 0xe; i++)
144 		state[i] = sil164_read(client, i);
145 }
146 
147 static void
148 sil164_restore_state(struct i2c_client *client, uint8_t *state)
149 {
150 	int i;
151 
152 	for (i = 0x8; i <= 0xe; i++)
153 		sil164_write(client, i, state[i]);
154 }
155 
156 static void
157 sil164_set_power_state(struct i2c_client *client, bool on)
158 {
159 	uint8_t control0 = sil164_read(client, SIL164_CONTROL0);
160 
161 	if (on)
162 		control0 |= SIL164_CONTROL0_POWER_ON;
163 	else
164 		control0 &= ~SIL164_CONTROL0_POWER_ON;
165 
166 	sil164_write(client, SIL164_CONTROL0, control0);
167 }
168 
169 static void
170 sil164_init_state(struct i2c_client *client,
171 		  struct sil164_encoder_params *config,
172 		  bool duallink)
173 {
174 	sil164_write(client, SIL164_CONTROL0,
175 		     SIL164_CONTROL0_HSYNC_ON |
176 		     SIL164_CONTROL0_VSYNC_ON |
177 		     (config->input_edge ? SIL164_CONTROL0_EDGE_RISING : 0) |
178 		     (config->input_width ? SIL164_CONTROL0_INPUT_24BIT : 0) |
179 		     (config->input_dual ? SIL164_CONTROL0_DUAL_EDGE : 0));
180 
181 	sil164_write(client, SIL164_DETECT,
182 		     SIL164_DETECT_INTR_STAT |
183 		     SIL164_DETECT_OUT_MODE_RECEIVER);
184 
185 	sil164_write(client, SIL164_CONTROL1,
186 		     (config->input_skew ? SIL164_CONTROL1_DESKEW_ENABLE : 0) |
187 		     (((config->input_skew + 4) & 0x7)
188 		      << SIL164_CONTROL1_DESKEW_INCR_SHIFT));
189 
190 	sil164_write(client, SIL164_CONTROL2,
191 		     SIL164_CONTROL2_SYNC_CONT |
192 		     (config->pll_filter ? 0 : SIL164_CONTROL2_FILTER_ENABLE) |
193 		     (4 << SIL164_CONTROL2_FILTER_SETTING_SHIFT));
194 
195 	sil164_write(client, SIL164_PLLZONE, 0);
196 
197 	if (duallink)
198 		sil164_write(client, SIL164_DUALLINK,
199 			     SIL164_DUALLINK_ENABLE |
200 			     (((config->duallink_skew + 4) & 0x7)
201 			      << SIL164_DUALLINK_SKEW_SHIFT));
202 	else
203 		sil164_write(client, SIL164_DUALLINK, 0);
204 }
205 
206 /* DRM encoder functions */
207 
208 static void
209 sil164_encoder_set_config(struct drm_encoder *encoder, void *params)
210 {
211 	struct sil164_priv *priv = to_sil164_priv(encoder);
212 
213 	priv->config = *(struct sil164_encoder_params *)params;
214 }
215 
216 static void
217 sil164_encoder_dpms(struct drm_encoder *encoder, int mode)
218 {
219 	struct sil164_priv *priv = to_sil164_priv(encoder);
220 	bool on = (mode == DRM_MODE_DPMS_ON);
221 	bool duallink = (on && encoder->crtc->mode.clock > 165000);
222 
223 	sil164_set_power_state(drm_i2c_encoder_get_client(encoder), on);
224 
225 	if (priv->duallink_slave)
226 		sil164_set_power_state(priv->duallink_slave, duallink);
227 }
228 
229 static void
230 sil164_encoder_save(struct drm_encoder *encoder)
231 {
232 	struct sil164_priv *priv = to_sil164_priv(encoder);
233 
234 	sil164_save_state(drm_i2c_encoder_get_client(encoder),
235 			  priv->saved_state);
236 
237 	if (priv->duallink_slave)
238 		sil164_save_state(priv->duallink_slave,
239 				  priv->saved_slave_state);
240 }
241 
242 static void
243 sil164_encoder_restore(struct drm_encoder *encoder)
244 {
245 	struct sil164_priv *priv = to_sil164_priv(encoder);
246 
247 	sil164_restore_state(drm_i2c_encoder_get_client(encoder),
248 			     priv->saved_state);
249 
250 	if (priv->duallink_slave)
251 		sil164_restore_state(priv->duallink_slave,
252 				     priv->saved_slave_state);
253 }
254 
255 static bool
256 sil164_encoder_mode_fixup(struct drm_encoder *encoder,
257 			  const struct drm_display_mode *mode,
258 			  struct drm_display_mode *adjusted_mode)
259 {
260 	return true;
261 }
262 
263 static int
264 sil164_encoder_mode_valid(struct drm_encoder *encoder,
265 			  struct drm_display_mode *mode)
266 {
267 	struct sil164_priv *priv = to_sil164_priv(encoder);
268 
269 	if (mode->clock < 32000)
270 		return MODE_CLOCK_LOW;
271 
272 	if (mode->clock > 330000 ||
273 	    (mode->clock > 165000 && !priv->duallink_slave))
274 		return MODE_CLOCK_HIGH;
275 
276 	return MODE_OK;
277 }
278 
279 static void
280 sil164_encoder_mode_set(struct drm_encoder *encoder,
281 			struct drm_display_mode *mode,
282 			struct drm_display_mode *adjusted_mode)
283 {
284 	struct sil164_priv *priv = to_sil164_priv(encoder);
285 	bool duallink = adjusted_mode->clock > 165000;
286 
287 	sil164_init_state(drm_i2c_encoder_get_client(encoder),
288 			  &priv->config, duallink);
289 
290 	if (priv->duallink_slave)
291 		sil164_init_state(priv->duallink_slave,
292 				  &priv->config, duallink);
293 
294 	sil164_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
295 }
296 
297 static enum drm_connector_status
298 sil164_encoder_detect(struct drm_encoder *encoder,
299 		      struct drm_connector *connector)
300 {
301 	struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
302 
303 	if (sil164_read(client, SIL164_DETECT) & SIL164_DETECT_HOTPLUG_STAT)
304 		return connector_status_connected;
305 	else
306 		return connector_status_disconnected;
307 }
308 
309 static int
310 sil164_encoder_get_modes(struct drm_encoder *encoder,
311 			 struct drm_connector *connector)
312 {
313 	return 0;
314 }
315 
316 static int
317 sil164_encoder_create_resources(struct drm_encoder *encoder,
318 				struct drm_connector *connector)
319 {
320 	return 0;
321 }
322 
323 static int
324 sil164_encoder_set_property(struct drm_encoder *encoder,
325 			    struct drm_connector *connector,
326 			    struct drm_property *property,
327 			    uint64_t val)
328 {
329 	return 0;
330 }
331 
332 static void
333 sil164_encoder_destroy(struct drm_encoder *encoder)
334 {
335 	struct sil164_priv *priv = to_sil164_priv(encoder);
336 
337 	if (priv->duallink_slave)
338 		i2c_unregister_device(priv->duallink_slave);
339 
340 	kfree(priv);
341 	drm_i2c_encoder_destroy(encoder);
342 }
343 
344 static struct drm_encoder_slave_funcs sil164_encoder_funcs = {
345 	.set_config = sil164_encoder_set_config,
346 	.destroy = sil164_encoder_destroy,
347 	.dpms = sil164_encoder_dpms,
348 	.save = sil164_encoder_save,
349 	.restore = sil164_encoder_restore,
350 	.mode_fixup = sil164_encoder_mode_fixup,
351 	.mode_valid = sil164_encoder_mode_valid,
352 	.mode_set = sil164_encoder_mode_set,
353 	.detect = sil164_encoder_detect,
354 	.get_modes = sil164_encoder_get_modes,
355 	.create_resources = sil164_encoder_create_resources,
356 	.set_property = sil164_encoder_set_property,
357 };
358 
359 /* I2C driver functions */
360 
361 static int
362 sil164_probe(struct i2c_client *client, const struct i2c_device_id *id)
363 {
364 	int vendor = sil164_read(client, SIL164_VENDOR_HI) << 8 |
365 		sil164_read(client, SIL164_VENDOR_LO);
366 	int device = sil164_read(client, SIL164_DEVICE_HI) << 8 |
367 		sil164_read(client, SIL164_DEVICE_LO);
368 	int rev = sil164_read(client, SIL164_REVISION);
369 
370 	if (vendor != 0x1 || device != 0x6) {
371 		sil164_dbg(client, "Unknown device %x:%x.%x\n",
372 			   vendor, device, rev);
373 		return -ENODEV;
374 	}
375 
376 	sil164_info(client, "Detected device %x:%x.%x\n",
377 		    vendor, device, rev);
378 
379 	return 0;
380 }
381 
382 static int
383 sil164_remove(struct i2c_client *client)
384 {
385 	return 0;
386 }
387 
388 static struct i2c_client *
389 sil164_detect_slave(struct i2c_client *client)
390 {
391 	struct i2c_adapter *adap = client->adapter;
392 	struct i2c_msg msg = {
393 		.addr = SIL164_I2C_ADDR_SLAVE,
394 		.len = 0,
395 	};
396 	const struct i2c_board_info info = {
397 		I2C_BOARD_INFO("sil164", SIL164_I2C_ADDR_SLAVE)
398 	};
399 
400 	if (i2c_transfer(adap, &msg, 1) != 1) {
401 		sil164_dbg(adap, "No dual-link slave found.");
402 		return NULL;
403 	}
404 
405 	return i2c_new_device(adap, &info);
406 }
407 
408 static int
409 sil164_encoder_init(struct i2c_client *client,
410 		    struct drm_device *dev,
411 		    struct drm_encoder_slave *encoder)
412 {
413 	struct sil164_priv *priv;
414 
415 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
416 	if (!priv)
417 		return -ENOMEM;
418 
419 	encoder->slave_priv = priv;
420 	encoder->slave_funcs = &sil164_encoder_funcs;
421 
422 	priv->duallink_slave = sil164_detect_slave(client);
423 
424 	return 0;
425 }
426 
427 static struct i2c_device_id sil164_ids[] = {
428 	{ "sil164", 0 },
429 	{ }
430 };
431 MODULE_DEVICE_TABLE(i2c, sil164_ids);
432 
433 static struct drm_i2c_encoder_driver sil164_driver = {
434 	.i2c_driver = {
435 		.probe = sil164_probe,
436 		.remove = sil164_remove,
437 		.driver = {
438 			.name = "sil164",
439 		},
440 		.id_table = sil164_ids,
441 	},
442 	.encoder_init = sil164_encoder_init,
443 };
444 
445 /* Module initialization */
446 
447 static int __init
448 sil164_init(void)
449 {
450 	return drm_i2c_encoder_register(THIS_MODULE, &sil164_driver);
451 }
452 
453 static void __exit
454 sil164_exit(void)
455 {
456 	drm_i2c_encoder_unregister(&sil164_driver);
457 }
458 
459 MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
460 MODULE_DESCRIPTION("Silicon Image sil164 TMDS transmitter driver");
461 MODULE_LICENSE("GPL and additional rights");
462 
463 module_init(sil164_init);
464 module_exit(sil164_exit);
465