0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/i2c.h>
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013
0014 #include <drm/drm_edid.h>
0015
0016 #include <video/omapfb_dss.h>
0017
0018 static const struct omap_video_timings dvic_default_timings = {
0019 .x_res = 640,
0020 .y_res = 480,
0021
0022 .pixelclock = 23500000,
0023
0024 .hfp = 48,
0025 .hsw = 32,
0026 .hbp = 80,
0027
0028 .vfp = 3,
0029 .vsw = 4,
0030 .vbp = 7,
0031
0032 .vsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
0033 .hsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
0034 .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
0035 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
0036 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE,
0037 };
0038
0039 struct panel_drv_data {
0040 struct omap_dss_device dssdev;
0041 struct omap_dss_device *in;
0042
0043 struct omap_video_timings timings;
0044
0045 struct i2c_adapter *i2c_adapter;
0046 };
0047
0048 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
0049
0050 static int dvic_connect(struct omap_dss_device *dssdev)
0051 {
0052 struct panel_drv_data *ddata = to_panel_data(dssdev);
0053 struct omap_dss_device *in = ddata->in;
0054
0055 if (omapdss_device_is_connected(dssdev))
0056 return 0;
0057
0058 return in->ops.dvi->connect(in, dssdev);
0059 }
0060
0061 static void dvic_disconnect(struct omap_dss_device *dssdev)
0062 {
0063 struct panel_drv_data *ddata = to_panel_data(dssdev);
0064 struct omap_dss_device *in = ddata->in;
0065
0066 if (!omapdss_device_is_connected(dssdev))
0067 return;
0068
0069 in->ops.dvi->disconnect(in, dssdev);
0070 }
0071
0072 static int dvic_enable(struct omap_dss_device *dssdev)
0073 {
0074 struct panel_drv_data *ddata = to_panel_data(dssdev);
0075 struct omap_dss_device *in = ddata->in;
0076 int r;
0077
0078 if (!omapdss_device_is_connected(dssdev))
0079 return -ENODEV;
0080
0081 if (omapdss_device_is_enabled(dssdev))
0082 return 0;
0083
0084 in->ops.dvi->set_timings(in, &ddata->timings);
0085
0086 r = in->ops.dvi->enable(in);
0087 if (r)
0088 return r;
0089
0090 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
0091
0092 return 0;
0093 }
0094
0095 static void dvic_disable(struct omap_dss_device *dssdev)
0096 {
0097 struct panel_drv_data *ddata = to_panel_data(dssdev);
0098 struct omap_dss_device *in = ddata->in;
0099
0100 if (!omapdss_device_is_enabled(dssdev))
0101 return;
0102
0103 in->ops.dvi->disable(in);
0104
0105 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
0106 }
0107
0108 static void dvic_set_timings(struct omap_dss_device *dssdev,
0109 struct omap_video_timings *timings)
0110 {
0111 struct panel_drv_data *ddata = to_panel_data(dssdev);
0112 struct omap_dss_device *in = ddata->in;
0113
0114 ddata->timings = *timings;
0115 dssdev->panel.timings = *timings;
0116
0117 in->ops.dvi->set_timings(in, timings);
0118 }
0119
0120 static void dvic_get_timings(struct omap_dss_device *dssdev,
0121 struct omap_video_timings *timings)
0122 {
0123 struct panel_drv_data *ddata = to_panel_data(dssdev);
0124
0125 *timings = ddata->timings;
0126 }
0127
0128 static int dvic_check_timings(struct omap_dss_device *dssdev,
0129 struct omap_video_timings *timings)
0130 {
0131 struct panel_drv_data *ddata = to_panel_data(dssdev);
0132 struct omap_dss_device *in = ddata->in;
0133
0134 return in->ops.dvi->check_timings(in, timings);
0135 }
0136
0137 static int dvic_ddc_read(struct i2c_adapter *adapter,
0138 unsigned char *buf, u16 count, u8 offset)
0139 {
0140 int r, retries;
0141
0142 for (retries = 3; retries > 0; retries--) {
0143 struct i2c_msg msgs[] = {
0144 {
0145 .addr = DDC_ADDR,
0146 .flags = 0,
0147 .len = 1,
0148 .buf = &offset,
0149 }, {
0150 .addr = DDC_ADDR,
0151 .flags = I2C_M_RD,
0152 .len = count,
0153 .buf = buf,
0154 }
0155 };
0156
0157 r = i2c_transfer(adapter, msgs, 2);
0158 if (r == 2)
0159 return 0;
0160
0161 if (r != -EAGAIN)
0162 break;
0163 }
0164
0165 return r < 0 ? r : -EIO;
0166 }
0167
0168 static int dvic_read_edid(struct omap_dss_device *dssdev,
0169 u8 *edid, int len)
0170 {
0171 struct panel_drv_data *ddata = to_panel_data(dssdev);
0172 int r, l, bytes_read;
0173
0174 if (!ddata->i2c_adapter)
0175 return -ENODEV;
0176
0177 l = min(EDID_LENGTH, len);
0178 r = dvic_ddc_read(ddata->i2c_adapter, edid, l, 0);
0179 if (r)
0180 return r;
0181
0182 bytes_read = l;
0183
0184
0185 if (len > EDID_LENGTH && edid[0x7e] > 0) {
0186 l = min(EDID_LENGTH, len - EDID_LENGTH);
0187
0188 r = dvic_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
0189 l, EDID_LENGTH);
0190 if (r)
0191 return r;
0192
0193 bytes_read += l;
0194 }
0195
0196 return bytes_read;
0197 }
0198
0199 static bool dvic_detect(struct omap_dss_device *dssdev)
0200 {
0201 struct panel_drv_data *ddata = to_panel_data(dssdev);
0202 unsigned char out;
0203 int r;
0204
0205 if (!ddata->i2c_adapter)
0206 return true;
0207
0208 r = dvic_ddc_read(ddata->i2c_adapter, &out, 1, 0);
0209
0210 return r == 0;
0211 }
0212
0213 static struct omap_dss_driver dvic_driver = {
0214 .connect = dvic_connect,
0215 .disconnect = dvic_disconnect,
0216
0217 .enable = dvic_enable,
0218 .disable = dvic_disable,
0219
0220 .set_timings = dvic_set_timings,
0221 .get_timings = dvic_get_timings,
0222 .check_timings = dvic_check_timings,
0223
0224 .get_resolution = omapdss_default_get_resolution,
0225
0226 .read_edid = dvic_read_edid,
0227 .detect = dvic_detect,
0228 };
0229
0230 static int dvic_probe_of(struct platform_device *pdev)
0231 {
0232 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
0233 struct device_node *node = pdev->dev.of_node;
0234 struct omap_dss_device *in;
0235 struct device_node *adapter_node;
0236 struct i2c_adapter *adapter;
0237
0238 in = omapdss_of_find_source_for_first_ep(node);
0239 if (IS_ERR(in)) {
0240 dev_err(&pdev->dev, "failed to find video source\n");
0241 return PTR_ERR(in);
0242 }
0243
0244 ddata->in = in;
0245
0246 adapter_node = of_parse_phandle(node, "ddc-i2c-bus", 0);
0247 if (adapter_node) {
0248 adapter = of_get_i2c_adapter_by_node(adapter_node);
0249 of_node_put(adapter_node);
0250 if (adapter == NULL) {
0251 dev_err(&pdev->dev, "failed to parse ddc-i2c-bus\n");
0252 omap_dss_put_device(ddata->in);
0253 return -EPROBE_DEFER;
0254 }
0255
0256 ddata->i2c_adapter = adapter;
0257 }
0258
0259 return 0;
0260 }
0261
0262 static int dvic_probe(struct platform_device *pdev)
0263 {
0264 struct panel_drv_data *ddata;
0265 struct omap_dss_device *dssdev;
0266 int r;
0267
0268 if (!pdev->dev.of_node)
0269 return -ENODEV;
0270
0271 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
0272 if (!ddata)
0273 return -ENOMEM;
0274
0275 platform_set_drvdata(pdev, ddata);
0276
0277 r = dvic_probe_of(pdev);
0278 if (r)
0279 return r;
0280
0281 ddata->timings = dvic_default_timings;
0282
0283 dssdev = &ddata->dssdev;
0284 dssdev->driver = &dvic_driver;
0285 dssdev->dev = &pdev->dev;
0286 dssdev->type = OMAP_DISPLAY_TYPE_DVI;
0287 dssdev->owner = THIS_MODULE;
0288 dssdev->panel.timings = dvic_default_timings;
0289
0290 r = omapdss_register_display(dssdev);
0291 if (r) {
0292 dev_err(&pdev->dev, "Failed to register panel\n");
0293 goto err_reg;
0294 }
0295
0296 return 0;
0297
0298 err_reg:
0299 omap_dss_put_device(ddata->in);
0300
0301 i2c_put_adapter(ddata->i2c_adapter);
0302
0303 return r;
0304 }
0305
0306 static int __exit dvic_remove(struct platform_device *pdev)
0307 {
0308 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
0309 struct omap_dss_device *dssdev = &ddata->dssdev;
0310 struct omap_dss_device *in = ddata->in;
0311
0312 omapdss_unregister_display(&ddata->dssdev);
0313
0314 dvic_disable(dssdev);
0315 dvic_disconnect(dssdev);
0316
0317 omap_dss_put_device(in);
0318
0319 i2c_put_adapter(ddata->i2c_adapter);
0320
0321 return 0;
0322 }
0323
0324 static const struct of_device_id dvic_of_match[] = {
0325 { .compatible = "omapdss,dvi-connector", },
0326 {},
0327 };
0328
0329 MODULE_DEVICE_TABLE(of, dvic_of_match);
0330
0331 static struct platform_driver dvi_connector_driver = {
0332 .probe = dvic_probe,
0333 .remove = __exit_p(dvic_remove),
0334 .driver = {
0335 .name = "connector-dvi",
0336 .of_match_table = dvic_of_match,
0337 .suppress_bind_attrs = true,
0338 },
0339 };
0340
0341 module_platform_driver(dvi_connector_driver);
0342
0343 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
0344 MODULE_DESCRIPTION("Generic DVI Connector driver");
0345 MODULE_LICENSE("GPL");