blob: 0cf96eb8a60f610b3fc26f0bc827453eb1c201fc [file] [log] [blame]
Dennis Munsiefc5891c2006-10-03 01:14:42 -07001/*
2 * driver/vide/fb_ddc.c - DDC/EDID read support.
3 *
4 * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
8 * for more details.
9 */
10
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/fb.h>
14#include <linux/i2c-algo-bit.h>
15
16#include "edid.h"
17
18#define DDC_ADDR 0x50
19
20static unsigned char *fb_do_probe_ddc_edid(struct i2c_adapter *adapter)
21{
22 unsigned char start = 0x0;
Linus Torvalds4be70392006-11-16 22:18:28 -080023 unsigned char *buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
Dennis Munsiefc5891c2006-10-03 01:14:42 -070024 struct i2c_msg msgs[] = {
25 {
26 .addr = DDC_ADDR,
Linus Torvalds4be70392006-11-16 22:18:28 -080027 .flags = 0,
Dennis Munsiefc5891c2006-10-03 01:14:42 -070028 .len = 1,
29 .buf = &start,
30 }, {
31 .addr = DDC_ADDR,
32 .flags = I2C_M_RD,
33 .len = EDID_LENGTH,
Linus Torvalds4be70392006-11-16 22:18:28 -080034 .buf = buf,
Dennis Munsiefc5891c2006-10-03 01:14:42 -070035 }
36 };
Dennis Munsiefc5891c2006-10-03 01:14:42 -070037
Dennis Munsiefc5891c2006-10-03 01:14:42 -070038 if (!buf) {
39 dev_warn(&adapter->dev, "unable to allocate memory for EDID "
40 "block.\n");
41 return NULL;
42 }
Dennis Munsiefc5891c2006-10-03 01:14:42 -070043
44 if (i2c_transfer(adapter, msgs, 2) == 2)
45 return buf;
46
47 dev_warn(&adapter->dev, "unable to read EDID block.\n");
48 kfree(buf);
49 return NULL;
50}
51
52unsigned char *fb_ddc_read(struct i2c_adapter *adapter)
53{
54 struct i2c_algo_bit_data *algo_data = adapter->algo_data;
55 unsigned char *edid = NULL;
56 int i, j;
57
58 algo_data->setscl(algo_data->data, 1);
Dennis Munsiefc5891c2006-10-03 01:14:42 -070059
60 for (i = 0; i < 3; i++) {
61 /* For some old monitors we need the
62 * following process to initialize/stop DDC
63 */
Jean Delvareb64d7082007-11-28 16:21:35 -080064 algo_data->setsda(algo_data->data, 1);
Dennis Munsiefc5891c2006-10-03 01:14:42 -070065 msleep(13);
66
67 algo_data->setscl(algo_data->data, 1);
68 for (j = 0; j < 5; j++) {
69 msleep(10);
70 if (algo_data->getscl(algo_data->data))
71 break;
72 }
73 if (j == 5)
74 continue;
75
76 algo_data->setsda(algo_data->data, 0);
77 msleep(15);
78 algo_data->setscl(algo_data->data, 0);
79 msleep(15);
80 algo_data->setsda(algo_data->data, 1);
81 msleep(15);
82
83 /* Do the real work */
84 edid = fb_do_probe_ddc_edid(adapter);
85 algo_data->setsda(algo_data->data, 0);
86 algo_data->setscl(algo_data->data, 0);
87 msleep(15);
88
89 algo_data->setscl(algo_data->data, 1);
90 for (j = 0; j < 10; j++) {
91 msleep(10);
92 if (algo_data->getscl(algo_data->data))
93 break;
94 }
95
96 algo_data->setsda(algo_data->data, 1);
97 msleep(15);
98 algo_data->setscl(algo_data->data, 0);
Jean Delvareb64d7082007-11-28 16:21:35 -080099 algo_data->setsda(algo_data->data, 0);
Dennis Munsiefc5891c2006-10-03 01:14:42 -0700100 if (edid)
101 break;
102 }
103 /* Release the DDC lines when done or the Apple Cinema HD display
104 * will switch off
105 */
Jean Delvareb64d7082007-11-28 16:21:35 -0800106 algo_data->setsda(algo_data->data, 1);
107 algo_data->setscl(algo_data->data, 1);
Dennis Munsiefc5891c2006-10-03 01:14:42 -0700108
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200109 adapter->class |= I2C_CLASS_DDC;
Dennis Munsiefc5891c2006-10-03 01:14:42 -0700110 return edid;
111}
112
113EXPORT_SYMBOL_GPL(fb_ddc_read);
114
115MODULE_AUTHOR("Dennis Munsie <dmunsie@cecropia.com>");
116MODULE_DESCRIPTION("DDC/EDID reading support");
117MODULE_LICENSE("GPL");