summaryrefslogtreecommitdiff
path: root/drivers/video/fsl-sii902x.c
blob: 7b0145f2cdaf2bb187b71a1929b1101aa3a8b1d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (C) 2010-2014 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/console.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/fb.h>
#include <linux/fsl_devices.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include "edid.h"

#define SII902X_INPUT_BUS_FMT	0x08
#define SII902X_TPI_AVI_INPUT_FMT	0x09
#define SII902X_TPI_AVI_OUTPUT_FMT	0x0A
#define SII902X_SYS_CONTROL	0x1A
#define SII902X_SYS_CTR_DDC_REQ	BIT(2)
#define SII902X_SYS_CTR_DDC_BUS_AVAI	(BIT(2) | BIT(1))
#define SII902X_TPI_FAMILY_DEV_ID	0x1B
#define SII902X_TPI_DEV_REV_ID	0x1C
#define SII902X_TPI_REV_LEVEL_ID	0x1D
#define SII902X_POWER_STATE	0x1E
#define SII902X_TPI_AUDIO_CFG0	0x24
#define SII902X_TPI_AUDIO_CFG1	0x25
#define SII902X_TPI_AUDIO_CFG2	0x26
#define SII902X_TPI_AUDIO_CFG3	0x27
#define SII902X_TPI_HDCP_REV	0x30
#define SII902X_TPI_INT_ENABLE	0x3C
#define SII902X_TPI_INT_STATUS	0x3D
#define SII902X_TPI_INT_PLUG_IN	BIT(2)
#define SII902X_GENERAL_PURPOSE_IO0	0xBC
#define SII902X_GENERAL_PURPOSE_IO1	0xBD
#define SII902X_GENERAL_PURPOSE_IO2	0xBE
#define SII902X_TRANS_MODE_DIFF	0xC7

bool g_enable_hdmi;

struct sii902x_data {
	struct i2c_client *client;
	struct delayed_work det_work;
	struct fb_info *fbi;
	struct regmap *regmap;
	unsigned int irq;
	u8 cable_plugin;
} *sii902x;

static struct i2c_client *sii902x_to_i2c(struct sii902x_data *sii902x)
{
	return sii902x->client;
}

static s32 sii902x_write(const struct i2c_client *client,
			u8 command, u8 value)
{
	return i2c_smbus_write_byte_data(client, command, value);
}

static s32 sii902x_read(const struct i2c_client *client, u8 command)
{
	int val;

	val = i2c_smbus_read_word_data(client, command);

	return val & 0xff;
}

static ssize_t sii902x_show_name(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	strcpy(buf, sii902x->fbi->fix.id);
	sprintf(buf+strlen(buf), "\n");

	return strlen(buf);
}

static DEVICE_ATTR(fb_name, S_IRUGO, sii902x_show_name, NULL);

static ssize_t sii902x_show_state(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	if (sii902x->cable_plugin == 0)
		strcpy(buf, "plugout\n");
	else
		strcpy(buf, "plugin\n");

	return strlen(buf);
}

static DEVICE_ATTR(cable_state, S_IRUGO, sii902x_show_state, NULL);

static void sii902x_power_up_tx(struct sii902x_data *sii902x)
{
	struct i2c_client *client = sii902x_to_i2c(sii902x);
	int val;

	val = sii902x_read(client, SII902X_POWER_STATE);
	val &= ~0x3;
	sii902x_write(client, SII902X_POWER_STATE, val);
}

static void sii902x_setup(struct fb_info *fbi)
{
	u16 data[4];
	u32 refresh;
	u8 *tmp;
	int i;

	/* Power up */
	sii902x_power_up_tx(sii902x);

	/* set TPI video mode */
	data[0] = PICOS2KHZ(fbi->var.pixclock) / 10;
	data[2] = fbi->var.hsync_len + fbi->var.left_margin +
		  fbi->var.xres + fbi->var.right_margin;
	data[3] = fbi->var.vsync_len + fbi->var.upper_margin +
		  fbi->var.yres + fbi->var.lower_margin;
	refresh = data[2] * data[3];
	refresh = (PICOS2KHZ(fbi->var.pixclock) * 1000) / refresh;
	data[1] = refresh * 100;
	tmp = (u8 *)data;
	for (i = 0; i < 8; i++)
		sii902x_write(sii902x->client, i, tmp[i]);

	/* input bus/pixel: full pixel wide (24bit), rising edge */
	sii902x_write(sii902x->client, SII902X_INPUT_BUS_FMT, 0x70);
	/* Set input format to RGB */
	sii902x_write(sii902x->client, SII902X_TPI_AVI_INPUT_FMT, 0x00);
	/* set output format to RGB */
	sii902x_write(sii902x->client, SII902X_TPI_AVI_OUTPUT_FMT, 0x00);
	/* audio setup */
	sii902x_write(sii902x->client, SII902X_TPI_AUDIO_CFG1, 0x00);
	sii902x_write(sii902x->client, SII902X_TPI_AUDIO_CFG2, 0x40);
	sii902x_write(sii902x->client, SII902X_TPI_AUDIO_CFG3, 0x00);
}

static int __sii902x_read_edid(struct i2c_adapter *adp,
			unsigned char *edid, u8 *buf)
{
	unsigned short addr = 0x50;
	int ret;

	struct i2c_msg msg[2] = {
		{
		.addr	= addr,
		.flags	= 0,
		.len	= 1,
		.buf	= buf,
		}, {
		.addr	= addr,
		.flags	= I2C_M_RD,
		.len	= EDID_LENGTH,
		.buf	= edid,
		},
	};

	if (adp == NULL)
		return -EINVAL;

	memset(edid, 0, EDID_LENGTH);

	ret = i2c_transfer(adp, msg, 2);
	if (ret < 0)
		return ret;

	/* If 0x50 fails, try 0x37. */
	if (edid[1] == 0x00) {
		msg[0].addr = msg[1].addr = 0x37;
		ret = i2c_transfer(adp, msg, 2);
		if (ret < 0)
			return ret;
	}

	if (edid[1] == 0x00)
		return -ENOENT;

	return 0;
}
/* make sure edid has 256 bytes*/
static int __sii902x_get_edid(struct i2c_adapter *adp,
			      struct fb_info *fbi)
{
	u8 *edid;
	u8 buf[2] = {0, 0};
	int num, ret;

	edid = kzalloc(EDID_LENGTH, GFP_KERNEL);
	if (!edid)
		return -ENOMEM;

	ret = __sii902x_read_edid(adp, edid, buf);
	if (ret)
		return ret;

	/* edid first block parsing */
	memset(&fbi->monspecs, 0, sizeof(fbi->monspecs));
	fb_edid_to_monspecs(edid, &fbi->monspecs);

	/* need read ext block? Only support one more blk now */
	num = edid[0x7E];
	if (num) {
		buf[0] = 0x80;
		ret = __sii902x_read_edid(adp, edid, buf);
		if (ret)
			return ret;

		fb_edid_add_monspecs(edid, &fbi->monspecs);
	}

	kfree(edid);
	return 0;
}
static int sii902x_get_edid(struct fb_info *fbi)
{
	int old, dat, ret, cnt = 100;

	old = sii902x_read(sii902x->client, SII902X_SYS_CONTROL);

	sii902x_write(sii902x->client, SII902X_SYS_CONTROL,
			old | SII902X_SYS_CTR_DDC_REQ);
	do {
		cnt--;
		msleep(20);
		dat = sii902x_read(sii902x->client, SII902X_SYS_CONTROL);
	} while ((!(dat & 0x2)) && cnt);

	if (!cnt) {
		ret = -1;
		goto done;
	}

	sii902x_write(sii902x->client, SII902X_SYS_CONTROL,
			old | SII902X_SYS_CTR_DDC_BUS_AVAI);

	/* edid reading */
	ret = __sii902x_get_edid(sii902x->client->adapter, fbi);

	cnt = 100;
	do {
		cnt--;
		sii902x_write(sii902x->client, SII902X_SYS_CONTROL,
				old & ~SII902X_SYS_CTR_DDC_BUS_AVAI);
		msleep(20);
		dat = sii902x_read(sii902x->client, SII902X_SYS_CONTROL);
	} while ((dat & 0x6) && cnt);

	if (!cnt)
		ret = -1;

done:
	sii902x_write(sii902x->client, SII902X_SYS_CONTROL, old);
	return ret;
}

static void det_worker(struct work_struct *work)
{
	struct fb_info *fbi = sii902x->fbi;
	struct fb_monspecs *monspecs = &fbi->monspecs;
	int val, ret;
	char event_string[16];
	char *envp[] = { event_string, NULL };

	val = sii902x_read(sii902x->client, SII902X_TPI_INT_STATUS);
	if (!(val & 0x1) && !g_enable_hdmi)
		goto err;

	/* cable connection changes */
	if (val & SII902X_TPI_INT_PLUG_IN || g_enable_hdmi) {
		sii902x->cable_plugin = 1;
		sprintf(event_string, "EVENT=plugin");

		ret = sii902x_get_edid(fbi);
		if (ret < 0) {
			dev_err(&sii902x->client->dev, "read edid fail\n");
			goto err;
		}

		/* make sure fb is powerdown */
		console_lock();
		fb_blank(fbi, FB_BLANK_POWERDOWN);
		console_unlock();

		if (monspecs->modedb_len > 0) {
			int i;
			const struct fb_videomode *mode;
			struct fb_videomode m;

			fb_destroy_modelist(&fbi->modelist);

			for (i = 0; i < monspecs->modedb_len; i++) {
				/* We do not support interlaced mode for now */
				if (!(monspecs->modedb[i].vmode &
					FB_VMODE_INTERLACED))
					fb_add_videomode(&monspecs->modedb[i],
							&fbi->modelist);
			}

			fb_var_to_videomode(&m, &fbi->var);
			mode = fb_find_nearest_mode(&m,
					&fbi->modelist);

			fb_videomode_to_var(&fbi->var, mode);

			fbi->var.activate |= FB_ACTIVATE_FORCE;
			console_lock();
			fbi->flags |= FBINFO_MISC_USEREVENT;
			fb_set_var(fbi, &fbi->var);
			fbi->flags &= ~FBINFO_MISC_USEREVENT;
			console_unlock();
		}

		console_lock();
		fb_blank(fbi, FB_BLANK_UNBLANK);
		console_unlock();
	} else {
		sii902x->cable_plugin = 0;
		sprintf(event_string, "EVENT=plugout");
		console_lock();
		fb_blank(fbi, FB_BLANK_POWERDOWN);
		console_unlock();
	}
	kobject_uevent_env(&sii902x->client->dev.kobj,
			KOBJ_CHANGE, envp);

err:
	sii902x_write(sii902x->client, SII902X_TPI_INT_STATUS, val);
}

static irqreturn_t sii902x_detect_handler(int irq, void *data)
{
	if (g_enable_hdmi)
		g_enable_hdmi = false;

	if (sii902x->fbi)
		schedule_delayed_work(&(sii902x->det_work),
				msecs_to_jiffies(20));
	return IRQ_HANDLED;
}

static void sii902x_poweron(void)
{
	/* Turn on DVI or HDMI */
	sii902x_write(sii902x->client, SII902X_SYS_CONTROL, 0x00);
}

static void sii902x_poweroff(void)
{
	/* disable tmds before changing resolution */
	sii902x_write(sii902x->client, SII902X_SYS_CONTROL, 0x10);
}

static int sii902x_fb_event(struct notifier_block *nb,
			    unsigned long val, void *v)
{
	struct fb_event *event = v;
	struct fb_info *fbi = event->info;

	switch (val) {
	case FB_EVENT_FB_REGISTERED:
		if (sii902x->fbi != NULL)
			break;
		sii902x->fbi = fbi;
		if (g_enable_hdmi && sii902x->fbi) {
			schedule_delayed_work(&(sii902x->det_work),
					msecs_to_jiffies(20));
		}
		break;
	case FB_EVENT_MODE_CHANGE:
		sii902x_setup(fbi);
		break;
	case FB_EVENT_BLANK:
		if (*((int *)event->data) == FB_BLANK_UNBLANK)
			sii902x_poweron();
		else
			sii902x_poweroff();
		break;
	}

	return 0;
}

static void sii902x_chip_id(struct sii902x_data *sii902x)
{
	struct i2c_client *client = sii902x_to_i2c(sii902x);
	int val;

	/* read device ID */
	val = sii902x_read(client, SII902X_TPI_FAMILY_DEV_ID);
	pr_info("Sii902x: read id = 0x%02X", val);
	val = sii902x_read(client, SII902X_TPI_DEV_REV_ID);
	pr_info("-0x%02X", val);
	val = sii902x_read(client, SII902X_TPI_REV_LEVEL_ID);
	pr_info("-0x%02X", val);
	val = sii902x_read(client, SII902X_TPI_HDCP_REV);
	pr_info("-0x%02X\n", val);
}

static int sii902x_initialize(struct sii902x_data *sii902x)
{
	struct i2c_client *client = sii902x_to_i2c(sii902x);
	int ret, cnt;

	for (cnt = 0; cnt < 5; cnt++) {
		/* Set 902x in hardware TPI mode on and jump out of D3 state */
		ret = sii902x_write(client, SII902X_TRANS_MODE_DIFF, 0x00);
		if (ret < 0)
			break;
	}
	if (0 != ret)
		dev_err(&client->dev, "cound not find device\n");

	return ret;
}

static void sii902x_enable_source(struct sii902x_data *sii902x)
{
	struct i2c_client *client = sii902x_to_i2c(sii902x);
	int val;

	sii902x_write(client, SII902X_GENERAL_PURPOSE_IO0, 0x01);
	sii902x_write(client, SII902X_GENERAL_PURPOSE_IO1, 0x82);
	val = sii902x_read(client, SII902X_GENERAL_PURPOSE_IO2);
	val |= 0x1;
	sii902x_write(client, SII902X_GENERAL_PURPOSE_IO2, val);
}

static struct notifier_block nb = {
	.notifier_call = sii902x_fb_event,
};

static int sii902x_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
	int ret, err;
	struct fb_info edid_fbi;

	if (!g_enable_hdmi)
		return -EPERM;

	if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) {
		dev_err(&client->dev, "i2c_check_functionality error\n");
		return -ENODEV;
	}

	sii902x = devm_kzalloc(&client->dev, sizeof(*sii902x), GFP_KERNEL);
	if (!sii902x)
		return -ENOMEM;

	sii902x->client = client;
	i2c_set_clientdata(client, sii902x);

	err = sii902x_initialize(sii902x);
	if (err)
		return err;

	sii902x_chip_id(sii902x);
	sii902x_power_up_tx(sii902x);
	sii902x_enable_source(sii902x);

	/* try to read edid */
	if (sii902x_get_edid(&edid_fbi) < 0)
		dev_warn(&client->dev, "Can not read edid\n");

	if (client->irq) {
		ret = devm_request_irq(&client->dev, client->irq,
				sii902x_detect_handler, 0,
				"SII902x_det", sii902x);
		if (ret < 0)
			dev_warn(&client->dev,
				"cound not request det irq %d\n",
				client->irq);
		else {
			INIT_DELAYED_WORK(&(sii902x->det_work), det_worker);
			/*enable cable hot plug irq*/
			sii902x_write(client, SII902X_TPI_INT_ENABLE, 0x01);
		}
		ret = device_create_file(&client->dev, &dev_attr_fb_name);
		if (ret < 0)
			dev_warn(&client->dev,
				"cound not create sys node for fb name\n");
		ret = device_create_file(&client->dev, &dev_attr_cable_state);
		if (ret < 0)
			dev_warn(&client->dev,
				"cound not create sys node for cable state\n");
	}

	fb_register_client(&nb);

	return 0;
}

static int sii902x_remove(struct i2c_client *client)
{
	fb_unregister_client(&nb);
	sii902x_poweroff();
	return 0;
}

static const struct i2c_device_id sii902x_id[] = {
	{ "sii902x", 0 },
	{},
};
MODULE_DEVICE_TABLE(i2c, sii902x_id);

static const struct of_device_id sii902x_dt_ids[] = {
	{ .compatible = "fsl,sii902x", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, sii902x_dt_ids);

static struct i2c_driver sii902x_i2c_driver = {
	.driver = {
		.name = "sii902x",
		.owner = THIS_MODULE,
		.of_match_table = sii902x_dt_ids,
	},
	.probe = sii902x_probe,
	.remove = sii902x_remove,
	.id_table = sii902x_id,
};
module_i2c_driver(sii902x_i2c_driver);

static int __init enable_hdmi_setup(char *str)
{
	g_enable_hdmi = true;

	return 1;
}
__setup("hdmi", enable_hdmi_setup);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("SII902x DVI/HDMI driver");
MODULE_LICENSE("GPL");