summaryrefslogtreecommitdiff
path: root/drivers/video/mxc/ad9389.c
blob: 9765cfd1a17f2469cfb07f419eff2682d436ee6f (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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*
 * ad9389.c
 *
 * Copyright 2010 - Digi International, Inc. All Rights Reserved.
 *
 * Based on ad9889.c driver from Armadeus:
 * Copyright (C) 2009 Armadeus Systems <nicolas.colombain@armadeus.com>
 * And also on mxcfb_sii9022.c from Pegatron:
 * Copyright 2009 Pegatron Corporation. 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/fb.h>
#include <linux/console.h>
#include <video/ad9389.h>

#define HPD_INT			0x80
#define MSEN_INT		0x40
#define	VS_INT			0x20
#define	AUDIO_FIFO_FULL_INT	0x10
#define	ITU656_ERR_INT		0x08
#define EDID_RDY_INT		0x04
#define EDID_LENGTH		256
#define DRV_NAME		"ad9389"


#define DEBUG
#define I2C_DBG			0x0001
#define EDID_DBG		0x0002
#define REGS_DBG		0x0004
#define SCREEN_DBG		0x0008

//int debug = I2C_DBG | EDID_DBG | REGS_DBG | SCREEN_DBG;
int debug = 0;
#define DBG(flag, fmt, args...)	do {							\
					if (debug & flag)				\
						printk(KERN_DEBUG fmt, ## args);	\
				} while (0)

struct ad9389_dev *pad9389;


static inline int ad9389_read_reg(struct i2c_client *client, u8 reg)
{
	return i2c_smbus_read_byte_data(client, reg);
}

static inline int ad9389_write_reg(struct i2c_client *client, u8 reg, u8 val)
{
	DBG(I2C_DBG, "I2C WR[%02x] = %02x\n", reg, val);
	return i2c_smbus_write_byte_data(client, reg, val);
}

static inline int ad9389_update_reg(struct i2c_client *client, u8 reg, u8 mask, u8 val)
{
	u8 regval = i2c_smbus_read_byte_data(client, reg);
	regval &= ~mask;
	regval |= val;
	return ad9389_write_reg(client, reg, regval);
}

static void ad9389_set_av_mute(struct i2c_client *client, int mute)
{
	if (mute) {
		ad9389_update_reg(client, 0x45, 0x40, 0x40);
		ad9389_update_reg(client, 0x45, 0x80, 0);
	} else {
		ad9389_update_reg(client, 0x45, 0x40, 0x0);
		ad9389_update_reg(client, 0x45, 0x80, 0x80);
	}
}

static void ad9389_set_power_down(struct i2c_client *client, int powerd)
{
	ad9389_update_reg(client, 0x41, 0x40, powerd ? 0x40 : 0);
}

static int ad9389_disp_connected(struct i2c_client *client)
{
	return (ad9389_read_reg(client, 0x42) & 0x40) != 0;
}

static void ad9389_enable_i2s_ch(struct i2c_client *client, u8 enable, u8 ch)
{
	u8 mask;

	if (ch > 3)
		return;

	mask = 0x04 << ch;
	ad9389_update_reg(client, 0x0c, mask, enable ? mask : 0);
}

static int ad9389_is_enabled_i2s_ch(struct i2c_client *client, u8 ch)
{
	u8 mask;

	if (ch > 3)
		return -EINVAL;

	mask = 0x04 << ch;
	return (i2c_smbus_read_byte_data(client, 0x0c) & mask) != 0;
}

static void ad9389_set_i2s_sf(struct i2c_client *client, int sample_freq)
{
	ad9389_update_reg(client, 0x15, 0xf0, sample_freq << 4);
}

static void ad9389_set_hdmi_mode(struct i2c_client *client, int hdmi)
{
	ad9389_update_reg(client, 0xaf, 0x02, hdmi ? 0x02 : 0);
}

static void ad9389_set_if_cc(struct i2c_client *client, int chcnt)
{
	ad9389_update_reg(client, 0x50, 0xe0, chcnt << 5);
}

static void ad9389_set_spk_map(struct i2c_client *client, int map)
{
	ad9389_write_reg(client, 0x51, map);
}

static void ad9389_set_N(struct i2c_client *client, int N_val)
{
	ad9389_write_reg(client, 0x1, N_val >> 16);
	ad9389_write_reg(client, 0x2, N_val >> 8);
	ad9389_write_reg(client, 0x3, N_val & 0xff);
}

static int ad9389_get_N(struct i2c_client *client)
{
	int N_val;

	N_val = (ad9389_read_reg(client, 0x1) & 0x0f) << 16;
	N_val |= (ad9389_read_reg(client, 0x2) << 8);
	N_val |= ad9389_read_reg(client, 0x3);

	return N_val;
}

#ifdef USED
static void ad9389_audio_set_CTS(struct i2c_client *client, int cts)
{
	ad9389_update_reg(client, 0x04, 0x0f, (cts >> 16) & 0x0f);
	ad9389_write_reg(client, 0x05, cts >> 8);
	ad9389_write_reg(client, 0x6, cts & 0xff);
}

static void ad9389_set_i2s_nbits(struct i2c_client *client, int bits)
{
	if (bits <= 24)
		ad9389_write_reg(client, 0x0d, bits);
}

static void ad9389_set_low_freq_vrr(struct i2c_client *client, int lowf)
{
	ad9389_update_reg(client, 0x15, 0x01, lowf ? 1 : 0);
}
#endif

#ifdef DEBUG
static void ad9389_dump_edid(u8 *edid)
{
	int i;

	if (!(debug & EDID_DBG))
		return;

	printk("\nEDID data:\n");
	for (i = 0; i < EDID_LENGTH; i++) {
		if (i % 8 == 0)
			printk("\n");
		printk("%02x ", edid[i]);
	}
	printk("\n");
}

static void ad9389_dump_regs(struct i2c_client *client)
{
	int i;

	if (!(debug & REGS_DBG))
		return;

	printk("\nAD9389 regs:\n");
	for (i = 0; i < EDID_LENGTH; i++) {
		if (i % 8 == 0)
			printk("\n%03x: ", i);
		printk("%02x ", ad9389_read_reg(client, i));
	}
	printk(KERN_DEBUG "\n");
}

static void fb_dump_modeline( struct fb_videomode *modedb, int num)
{
	struct fb_videomode *mode;
	int i;

	if (!(debug & SCREEN_DBG))
		return;

	printk(KERN_DEBUG "Monitor/TV supported modelines:\n");

	for (i = 0; i < num; i++) {
		mode = &modedb[i];

		printk("   \"%dx%d%s%d\" %lu.%02lu ",
			mode->xres, mode->yres, (mode->vmode & FB_VMODE_INTERLACED) ? "i@" : "@", mode->refresh,
			(PICOS2KHZ(mode->pixclock) * 1000UL)/1000000,
			(PICOS2KHZ(mode->pixclock) ) % 1000);
		printk("%d %d %d %d ",
			mode->xres,
			mode->xres + mode->right_margin,
			mode->xres + mode->right_margin + mode->hsync_len,
			mode->xres + mode->right_margin + mode->hsync_len + mode->left_margin );
		printk("%d %d %d %d ",
			mode->yres,
			mode->yres + mode->lower_margin,
			mode->yres + mode->lower_margin + mode->vsync_len,
			mode->yres + mode->lower_margin + mode->vsync_len + mode->upper_margin );
		printk("%shsync %svsync\n", (mode->sync & FB_SYNC_HOR_HIGH_ACT) ? "+" : "-",
			   (mode->sync & FB_SYNC_VERT_HIGH_ACT) ? "+" : "-" );
	}
}
#else
static void ad9389_dump_edid(u8 *edid) {}
static void ad9389_dump_regs(struct i2c_client *client) {}
static void fb_dump_modeline( struct fb_videomode *modedb, int num) {}
#endif

static int ad9389_read_edid(struct i2c_client *client, u8 *edid)
{
	union i2c_smbus_data data;
	struct ad9389_pdata *config = client->dev.platform_data;
	struct ad9389_dev *ad9389 = i2c_get_clientdata(client);
	u8 *pd;
	int status, i;

	for (i = 0, pd = edid; i < EDID_LENGTH/I2C_SMBUS_BLOCK_MAX; i++, pd += I2C_SMBUS_BLOCK_MAX) {
		data.block[0] = I2C_SMBUS_BLOCK_MAX;
		status = i2c_smbus_xfer(ad9389->edid_ram->adapter, config->edid_addr,
					ad9389->edid_ram->flags,
					I2C_SMBUS_READ, i*I2C_SMBUS_BLOCK_MAX,
					I2C_SMBUS_I2C_BLOCK_DATA, &data);
		if (status < 0)
			return status;
		memcpy(pd, &data.block[1], data.block[0]);
	}

	return 0;
}

static int ad9389_parse_edid(struct fb_var_screeninfo *einfo, u8 *edid, int *dvi)
{
	int ret;

	if (einfo == NULL || edid == NULL || dvi == NULL)
		return -EINVAL;

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

	/* Assume dvi if no CEA extension */
	*dvi = 1;
	if (edid[126] > 0) {
		/* CEA extensions */
		if (edid[128] == 0x02 && edid[131] & 0x40) {
			*dvi = 0;
		}
	}

	ret = fb_parse_edid(edid, einfo);
	if (ret)
		return -ret;

	/* This is valid for version 1.3 of the EDID */
	if ((edid[18] == 1) && (edid[19] == 3)) {
		einfo->height = edid[21] * 10;
		einfo->width = edid[22] * 10;
	}

	return 0;
}

static void ad9389_audio_setup(struct ad9389_dev *ad9389)
{
	struct i2c_client *client = ad9389->client;

	/* disable I2S channels */
	ad9389_enable_i2s_ch(client, 0, 0);
	ad9389_enable_i2s_ch(client, 0, 1);
	ad9389_enable_i2s_ch(client, 0, 2);
	ad9389_enable_i2s_ch(client, 0, 3);

	/* Set sample freq, currently hardcoded to 44KHz */
	ad9389_set_i2s_sf(client, 0);

	/* By default, enable i2s ch0. Can be modified through the sysfs */
	ad9389_set_N(client, 6272);
	ad9389_set_if_cc(client, 1);
	ad9389_set_spk_map(client, 0);
	ad9389_enable_i2s_ch(client, 1, 0);
	ad9389_set_av_mute(client, 0);
}


static void ad9389_fb_init(struct fb_info *info)
{
	struct ad9389_dev *ad9389 = pad9389;
	struct i2c_client *client = ad9389->client;
	struct ad9389_pdata *pdata = client->dev.platform_data;
	static struct fb_var_screeninfo var;
	int ret = 0;

	dev_info(info->dev, "%s\n", __func__);

	if (!ad9389_disp_connected(client)) {
		ad9389_set_power_down(client, 1);
		if(pdata->disp_disconnected)
			pdata->disp_disconnected(ad9389);
		return;
	}

	if(pdata->disp_connected)
		pdata->disp_connected(ad9389);

	dev_info(info->dev, "%s, display connected\n", __func__);
	memset(&var, 0, sizeof(var));

	/* Disable Power down and set mute to on */
	ad9389_set_power_down(client, 0);
	ad9389_set_av_mute(client, 1);

	/* set static reserved registers*/
	ad9389_write_reg(client,0x0a, 0x01);
	ad9389_write_reg(client, 0x98, 0x03);
	ad9389_write_reg(client, 0x9C, 0x38);

	/* Write magic numbers */
	ad9389_write_reg(client, 0xA2, 0x87);
	ad9389_write_reg(client, 0xA3, 0x87);

	/* set capture edge */
	ad9389_write_reg(client, 0xba, 0x60);
	ad9389_write_reg(client, 0x47, 0x80);

	mdelay(250);

	ret = ad9389_read_edid(ad9389->client, ad9389->edid_data);
	if (!ret) {
		ad9389_dump_edid(ad9389->edid_data);
		ret = ad9389_parse_edid(&var, ad9389->edid_data, &ad9389->dvi);
		if (!ret) {
			if (!ad9389->dvi) {
				ad9389_set_hdmi_mode(client, 1);
				/* FIXME audio setup should be done once we know the pixclock */
				ad9389_audio_setup(ad9389);
			}

			fb_edid_to_monspecs(ad9389->edid_data, &info->monspecs);
			if (info->monspecs.modedb_len) {
				fb_dump_modeline(info->monspecs.modedb, info->monspecs.modedb_len);
				if (pdata->vmode_to_modelist)
					pdata->vmode_to_modelist(info->monspecs.modedb,
								 info->monspecs.modedb_len,
								 &info->modelist, &var);
				else
					fb_videomode_to_modelist(info->monspecs.modedb,
								 info->monspecs.modedb_len,
								 &info->modelist);
			}
		}
	} else {
		/* TODO */
		printk(KERN_WARNING "NO EDID information found, using default mode?\n");
	}

	ad9389_dump_regs(client);

	if (pdata->vmode_to_var)
		pdata->vmode_to_var(ad9389, &var);

	var.activate = FB_ACTIVATE_ALL;
	acquire_console_sem();
	info->flags |= FBINFO_MISC_USEREVENT;
	fb_set_var(info, &var);
	info->flags &= ~FBINFO_MISC_USEREVENT;
	fb_blank(info, FB_BLANK_UNBLANK);
	fb_show_logo(info, 0);
	release_console_sem();
}

int ad9389_fb_event(struct notifier_block *nb, unsigned long val, void *v)
{
	return 0;
}

static void ad9389_work(struct work_struct *work)
{
	struct ad9389_dev *ad9389 = container_of(work, struct ad9389_dev, work);
	struct i2c_client *client =  ad9389->client;
	unsigned char irq_reg1;
	unsigned char irq_reg2;

	dev_dbg(&client->dev, "%s\n", __func__);

	mutex_lock(&ad9389->irq_lock);

	/* Interrupts are disabled here... */
	irq_reg1 = ad9389_read_reg(client, 0x96);
	irq_reg2 = ad9389_read_reg(client, 0x97);

	while ((irq_reg1 & 0xc4) | (irq_reg2 & 0xc0)) {

		dev_dbg(&client->dev, "IRQ register %02x/%02x\n",
			irq_reg1, irq_reg2);

		/* hot plug detections interrupt? */
		if (irq_reg1 & HPD_INT) {
			dev_dbg(&client->dev, "HPD irq\n");
			ad9389_fb_init(ad9389->fbi);
		}

		/* check for EDID ready flag, then call EDID Handler */
		if (irq_reg1 & EDID_RDY_INT) {
			dev_dbg(&client->dev, "EDID_RDY_INT\n");
		}

		/* ack and check again */
		ad9389_write_reg(client, 0x96, irq_reg1);
		ad9389_write_reg(client, 0x97, irq_reg2);

		irq_reg1 = ad9389_read_reg(client, 0x96);
		irq_reg2 = ad9389_read_reg(client, 0x97);
	}

	mutex_unlock(&ad9389->irq_lock);
}


static irqreturn_t ad9389_handler(int irq, void *dev_id)
{
	struct ad9389_dev *dev = (struct ad9389_dev *) dev_id;

	dev_dbg(&dev->client->dev, "%s\n", __func__);
	schedule_work(&dev->work);

	return IRQ_HANDLED;
}

static ssize_t ad9389_show_mute(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	int read, ret = 0;

	read = ad9389_read_reg(client, 0x45);
	if (read & 0x40)
		ret = snprintf(buf, PAGE_SIZE, "on");
	else if (read & 0x80)
	  	ret = snprintf(buf, PAGE_SIZE, "off");

	return ret;
}

static ssize_t ad9389_store_mute(struct device *dev, struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	int mute;

	if (!strcmp(buf, "on"))
	      mute = 1;
	else if (!strcmp(buf, "off"))
	      mute = 0;
	else
	      return 0;

	ad9389_set_av_mute(client, mute);

	return count;
}

static ssize_t ad9389_show_N_param(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct i2c_client *client = to_i2c_client(dev);
	int N_val;

	N_val = ad9389_get_N(client);

	return snprintf(buf, PAGE_SIZE, "%d", N_val);
}

static ssize_t ad9389_store_N_param(struct device *dev, struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct i2c_client *client = to_i2c_client(dev);
	unsigned long N_val;

	N_val = simple_strtoul(buf, NULL, 10);
	ad9389_set_N(client, N_val);

	return count;
}

#define	ad9389_show_i2s_ch(num)						\
static ssize_t ad9389_show_i2s_ch##num(struct device *dev,		\
				   struct device_attribute *attr,	\
				   char *buf)				\
{									\
	struct i2c_client *client = to_i2c_client(dev);			\
	int read, ret = 0;						\
	read = ad9389_is_enabled_i2s_ch(client, num);			\
	if (read < 0)							\
		ret = snprintf(buf, PAGE_SIZE, "error");		\
	else if (read == 1)						\
		ret = snprintf(buf, PAGE_SIZE, "on");			\
	else 								\
	  	ret = snprintf(buf, PAGE_SIZE, "off");			\
	return ret;							\
}

#define	ad9389_store_i2s_ch(num)					\
static ssize_t ad9389_store_i2s_ch##num(struct device *dev,		\
				    struct device_attribute *attr,	\
				    const char *buf, size_t count)	\
{									\
	struct i2c_client *client = to_i2c_client(dev);			\
	int enable;							\
	if (!strcmp(buf, "on"))						\
		enable = 1;						\
	else if (!strcmp(buf, "off"))					\
		enable = 0;						\
	else								\
		return 0;						\
	ad9389_enable_i2s_ch(client, enable, num);			\
	return count;							\
}

#define audio_ch(num)			\
	static DEVICE_ATTR(i2s_ch##num, S_IWUSR | S_IRUGO, ad9389_show_i2s_ch##num, ad9389_store_i2s_ch##num)

static DEVICE_ATTR(mute, S_IWUSR | S_IRUGO, ad9389_show_mute, ad9389_store_mute);
static DEVICE_ATTR(N_param, S_IWUSR | S_IRUGO, ad9389_show_N_param, ad9389_store_N_param);

ad9389_show_i2s_ch(0)
ad9389_show_i2s_ch(1)
ad9389_show_i2s_ch(2)
ad9389_show_i2s_ch(3)
ad9389_store_i2s_ch(0)
ad9389_store_i2s_ch(1)
ad9389_store_i2s_ch(2)
ad9389_store_i2s_ch(3)
audio_ch(0);
audio_ch(1);
audio_ch(2);
audio_ch(3);

static struct attribute *ad9389_attributes[] = {
	&dev_attr_mute.attr,
	&dev_attr_i2s_ch0.attr,
	&dev_attr_i2s_ch1.attr,
	&dev_attr_i2s_ch2.attr,
	&dev_attr_i2s_ch3.attr,
	&dev_attr_N_param.attr,
	NULL
};

static const struct attribute_group ad9389_attr_group = {
	.attrs = ad9389_attributes,
};

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

static int ad9389_probe(struct i2c_client *client,
        const struct i2c_device_id *id)
{
	struct ad9389_pdata *pdata = client->dev.platform_data;
	struct ad9389_dev *ad9389;
	int ret = -EINVAL;

	/* Sanity checks */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

	if (!pdata) {
		printk(KERN_ERR DRV_NAME ": Platform data not supplied\n");
		return -ENOENT;
	}

	if (!client->irq) {
		printk(KERN_ERR DRV_NAME ": Invalid irq value\n");
		return -ENOENT;
	}

	ad9389 = kzalloc(sizeof(struct ad9389_dev), GFP_KERNEL);
	if (ad9389 == NULL)
		return -ENOMEM;

	ad9389->edid_data = kmalloc(EDID_LENGTH, GFP_KERNEL);
	if (ad9389->edid_data == NULL) {
		ret = -ENOMEM;
		goto err_edid_alloc;
	}

	pad9389 = ad9389;
	ad9389->client = client;
	i2c_set_clientdata(client, ad9389);

	INIT_WORK(&ad9389->work, ad9389_work);
	mutex_init(&ad9389->irq_lock);
	mutex_lock(&ad9389->irq_lock);

	/* platform specific initialization (gpio, irq...) */
	if (pdata->hw_init)
		pdata->hw_init(ad9389);

	ret = request_irq(client->irq, ad9389_handler,
			  IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, DRV_NAME, ad9389);
	if (ret < 0) {
		printk(KERN_ERR DRV_NAME ": Could not allocate IRQ (n %d)\n", client->irq);
		goto err_irq;
	}

	/**
	 * There is no good way to detect if the chip is present. We assume that its present
	 * because somebody answered (ack) on the device address...
	 */
	ret = ad9389_read_reg(client, 0x00);
	if (ret < 0) {
		printk(KERN_WARNING DRV_NAME ": i2c transfer error, (device present?)\n");
		ret = -ENODEV;
		goto err_presence;
	}

	ad9389->chiprev = (u8)ret;
	ad9389->edid_ram = i2c_new_dummy(client->adapter, pdata->edid_addr);
	if (!ad9389->edid_ram) {
		printk(KERN_WARNING DRV_NAME ": can't add i2c device at 0x%x\n", pdata->edid_addr);
		goto err_presence;
	}

	/* Register sysfs hooks */
	ret = sysfs_create_group(&client->dev.kobj, &ad9389_attr_group);
	if (ret)
		goto err_sysfs_file;

	ad9389->fbi = registered_fb[pdata->dispif];
	fb_register_client(&nb);

	/* Ack any active interrupt and enable irqs */
	ad9389_write_reg(client, 0x94, 0x84);
	ad9389_write_reg(client, 0x95, 0xc3);
	ad9389_write_reg(client, 0x96, 0x84);
	ad9389_write_reg(client, 0x97, 0xc3);

	mutex_unlock(&ad9389->irq_lock);

	ad9389_fb_init(registered_fb[pdata->dispif]);

	printk(KERN_INFO DRV_NAME ": device detected at address 0x%x, chip revision 0x%02x\n",
	       client->addr << 1, ad9389->chiprev);

	return 0;

err_sysfs_file:
	i2c_unregister_device(ad9389->edid_ram);
err_presence:
	free_irq(client->irq, ad9389);
err_irq:
	flush_scheduled_work();
	if (pdata->hw_deinit)
		pdata->hw_deinit(ad9389);
	kfree(ad9389->edid_data);
err_edid_alloc:
	kfree(ad9389);
	pad9389 = NULL;
	return ret;
}

static int ad9389_remove(struct i2c_client *client)
{
	struct ad9389_pdata *pdata = client->dev.platform_data;
	struct ad9389_dev *ad9389 = i2c_get_clientdata(client);

	free_irq(client->irq, ad9389);
	flush_scheduled_work();
	sysfs_remove_group(&client->dev.kobj, &ad9389_attr_group);
	i2c_unregister_device(ad9389->edid_ram);
	fb_unregister_client(&nb);
	kfree(ad9389->edid_data);
	kfree(ad9389);
	pad9389 = NULL;

	if (pdata->hw_deinit)
		pdata->hw_deinit(ad9389);

	return 0;
}

#ifdef CONFIG_PM
static int ad9389_suspend(struct i2c_client *client, pm_message_t state)
{
	dev_dbg(&client->dev, "PM suspend\n");
	ad9389_set_power_down(client, 1);

	return 0;
}

static int ad9389_resume(struct i2c_client *client)
{
	struct ad9389_dev *ad9389 = pad9389;
	int ret;
	static struct fb_var_screeninfo var;

	dev_dbg(&client->dev, "PM resume\n");

	/* Disable Power down and set mute to on */
	ad9389_set_power_down(client, 0);
	ad9389_set_av_mute(client, 1);

	/* set static reserved registers*/
	ad9389_write_reg(client,0x0a, 0x01);
	ad9389_write_reg(client, 0x98, 0x03);
	ad9389_write_reg(client, 0x9C, 0x38);

	/* Write magic numbers */
	ad9389_write_reg(client, 0xA2, 0x87);
	ad9389_write_reg(client, 0xA3, 0x87);

	/* set capture edge */
	ad9389_write_reg(client, 0xba, 0x60);
	ad9389_write_reg(client, 0x47, 0x80);

	mdelay(250);

	ret = ad9389_read_edid(ad9389->client, ad9389->edid_data);
	if (!ret) {
		ad9389_dump_edid(ad9389->edid_data);
		ret = ad9389_parse_edid(&var, ad9389->edid_data, &ad9389->dvi);
		if (!ret) {
			if (!ad9389->dvi) {
				ad9389_set_hdmi_mode(client, 1);
				/* FIXME audio setup should be done once we know the pixclock */
				ad9389_audio_setup(ad9389);
			}
		}
	}
	return 0;
}
#else
#define ad9389_suspend	NULL
#define ad9389_resume	NULL
#endif


static struct i2c_device_id ad9389_id[] = {
	{ "ad9389", 0 },
	{},
};
MODULE_DEVICE_TABLE(i2c, ad9389_id);

static struct i2c_driver ad9389_driver = {
	.driver = {
		.name = "ad9389",
	},
	.probe		= ad9389_probe,
	.remove		= ad9389_remove,
	.suspend	= ad9389_suspend,
	.resume		= ad9389_resume,
	.id_table	= ad9389_id,

};

static int __init ad9389_init(void)
{
	return i2c_add_driver(&ad9389_driver);
}

static void __exit ad9389_exit(void)
{
	i2c_del_driver(&ad9389_driver);
}

module_init(ad9389_init);
module_exit(ad9389_exit);

MODULE_DESCRIPTION("AD9389 hdmi/dvi driver");
MODULE_AUTHOR("Digi International Inc.");
MODULE_LICENSE("GPL");