Linux Audio

Check our new training course

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