blob: 8281baafe1efd39127a628c70ecb3b720034d6f1 [file] [log] [blame]
Tomi Valkeinenba2eac92011-09-01 09:17:41 +03001/*
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +02002 * TFP410 DPI-to-DVI chip
Tomi Valkeinenba2eac92011-09-01 09:17:41 +03003 *
4 * Copyright (C) 2011 Texas Instruments Inc
5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <video/omapdss.h>
23#include <linux/i2c.h>
Tomi Valkeinen2da35192011-12-22 10:37:33 +020024#include <linux/gpio.h>
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030025#include <drm/drm_edid.h>
26
Tomi Valkeinendac8eb52011-12-22 11:12:13 +020027#include <video/omap-panel-tfp410.h>
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030028
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020029static const struct omap_video_timings tfp410_default_timings = {
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030030 .x_res = 640,
31 .y_res = 480,
32
33 .pixel_clock = 23500,
34
35 .hfp = 48,
36 .hsw = 32,
37 .hbp = 80,
38
39 .vfp = 3,
40 .vsw = 4,
41 .vbp = 7,
Archit Tanejaa8d5e412012-06-25 12:26:38 +053042
43 .vsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
44 .hsync_level = OMAPDSS_SIG_ACTIVE_HIGH,
45 .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE,
46 .de_level = OMAPDSS_SIG_ACTIVE_HIGH,
47 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030048};
49
50struct panel_drv_data {
51 struct omap_dss_device *dssdev;
52
53 struct mutex lock;
Tomi Valkeinen2da35192011-12-22 10:37:33 +020054
55 int pd_gpio;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030056
Tomi Valkeinen958f2712012-02-17 12:19:48 +020057 struct i2c_adapter *i2c_adapter;
58};
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030059
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020060static int tfp410_power_on(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030061{
Tomi Valkeinen2da35192011-12-22 10:37:33 +020062 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030063 int r;
64
65 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
66 return 0;
67
Archit Tanejac4991442012-08-08 14:28:54 +053068 omapdss_dpi_set_timings(dssdev, &dssdev->panel.timings);
Archit Tanejac6b393d2012-07-06 15:30:52 +053069 omapdss_dpi_set_data_lines(dssdev, dssdev->phy.dpi.data_lines);
Archit Tanejac4991442012-08-08 14:28:54 +053070
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030071 r = omapdss_dpi_display_enable(dssdev);
72 if (r)
73 goto err0;
74
Tomi Valkeinen2da35192011-12-22 10:37:33 +020075 if (gpio_is_valid(ddata->pd_gpio))
Russ Dillaf461d62012-05-09 15:08:08 -070076 gpio_set_value_cansleep(ddata->pd_gpio, 1);
Tomi Valkeinen2da35192011-12-22 10:37:33 +020077
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030078 return 0;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030079err0:
80 return r;
81}
82
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020083static void tfp410_power_off(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030084{
Tomi Valkeinen2da35192011-12-22 10:37:33 +020085 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030086
87 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
88 return;
89
Tomi Valkeinen2da35192011-12-22 10:37:33 +020090 if (gpio_is_valid(ddata->pd_gpio))
Russ Dillaf461d62012-05-09 15:08:08 -070091 gpio_set_value_cansleep(ddata->pd_gpio, 0);
Tomi Valkeinen2da35192011-12-22 10:37:33 +020092
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030093 omapdss_dpi_display_disable(dssdev);
94}
95
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020096static int tfp410_probe(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030097{
98 struct panel_drv_data *ddata;
Tomi Valkeinen2da35192011-12-22 10:37:33 +020099 int r;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200100 int i2c_bus_num;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300101
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200102 ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300103 if (!ddata)
104 return -ENOMEM;
105
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200106 dssdev->panel.timings = tfp410_default_timings;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300107
108 ddata->dssdev = dssdev;
109 mutex_init(&ddata->lock);
110
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200111 if (dssdev->data) {
112 struct tfp410_platform_data *pdata = dssdev->data;
113
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200114 ddata->pd_gpio = pdata->power_down_gpio;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200115 i2c_bus_num = pdata->i2c_bus_num;
116 } else {
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200117 ddata->pd_gpio = -1;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200118 i2c_bus_num = -1;
119 }
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200120
121 if (gpio_is_valid(ddata->pd_gpio)) {
Tomi Valkeinena6df3fd2012-09-04 10:52:19 +0300122 r = devm_gpio_request_one(&dssdev->dev, ddata->pd_gpio,
123 GPIOF_OUT_INIT_LOW, "tfp410 pd");
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200124 if (r) {
125 dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
126 ddata->pd_gpio);
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200127 return r;
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200128 }
129 }
130
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200131 if (i2c_bus_num != -1) {
132 struct i2c_adapter *adapter;
133
134 adapter = i2c_get_adapter(i2c_bus_num);
135 if (!adapter) {
136 dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
137 i2c_bus_num);
Tomi Valkeinena6df3fd2012-09-04 10:52:19 +0300138 return -EINVAL;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200139 }
140
141 ddata->i2c_adapter = adapter;
142 }
143
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300144 dev_set_drvdata(&dssdev->dev, ddata);
145
146 return 0;
147}
148
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200149static void __exit tfp410_remove(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300150{
151 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
152
153 mutex_lock(&ddata->lock);
154
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200155 if (ddata->i2c_adapter)
156 i2c_put_adapter(ddata->i2c_adapter);
157
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300158 dev_set_drvdata(&dssdev->dev, NULL);
159
160 mutex_unlock(&ddata->lock);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300161}
162
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200163static int tfp410_enable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300164{
165 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
166 int r;
167
168 mutex_lock(&ddata->lock);
169
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200170 r = tfp410_power_on(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300171 if (r == 0)
172 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
173
174 mutex_unlock(&ddata->lock);
175
176 return r;
177}
178
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200179static void tfp410_disable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300180{
181 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
182
183 mutex_lock(&ddata->lock);
184
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200185 tfp410_power_off(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300186
187 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
188
189 mutex_unlock(&ddata->lock);
190}
191
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200192static void tfp410_set_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300193 struct omap_video_timings *timings)
194{
195 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
196
197 mutex_lock(&ddata->lock);
Archit Tanejac4991442012-08-08 14:28:54 +0530198 omapdss_dpi_set_timings(dssdev, timings);
Archit Tanejabdcae3c2012-08-08 14:29:48 +0530199 dssdev->panel.timings = *timings;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300200 mutex_unlock(&ddata->lock);
201}
202
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200203static void tfp410_get_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300204 struct omap_video_timings *timings)
205{
206 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
207
208 mutex_lock(&ddata->lock);
209 *timings = dssdev->panel.timings;
210 mutex_unlock(&ddata->lock);
211}
212
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200213static int tfp410_check_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300214 struct omap_video_timings *timings)
215{
216 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
217 int r;
218
219 mutex_lock(&ddata->lock);
220 r = dpi_check_timings(dssdev, timings);
221 mutex_unlock(&ddata->lock);
222
223 return r;
224}
225
226
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200227static int tfp410_ddc_read(struct i2c_adapter *adapter,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300228 unsigned char *buf, u16 count, u8 offset)
229{
230 int r, retries;
231
232 for (retries = 3; retries > 0; retries--) {
233 struct i2c_msg msgs[] = {
234 {
235 .addr = DDC_ADDR,
236 .flags = 0,
237 .len = 1,
238 .buf = &offset,
239 }, {
240 .addr = DDC_ADDR,
241 .flags = I2C_M_RD,
242 .len = count,
243 .buf = buf,
244 }
245 };
246
247 r = i2c_transfer(adapter, msgs, 2);
248 if (r == 2)
249 return 0;
250
251 if (r != -EAGAIN)
252 break;
253 }
254
255 return r < 0 ? r : -EIO;
256}
257
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200258static int tfp410_read_edid(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300259 u8 *edid, int len)
260{
261 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300262 int r, l, bytes_read;
263
264 mutex_lock(&ddata->lock);
265
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200266 if (!ddata->i2c_adapter) {
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300267 r = -ENODEV;
268 goto err;
269 }
270
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300271 l = min(EDID_LENGTH, len);
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200272 r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300273 if (r)
274 goto err;
275
276 bytes_read = l;
277
278 /* if there are extensions, read second block */
279 if (len > EDID_LENGTH && edid[0x7e] > 0) {
280 l = min(EDID_LENGTH, len - EDID_LENGTH);
281
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200282 r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300283 l, EDID_LENGTH);
284 if (r)
285 goto err;
286
287 bytes_read += l;
288 }
289
290 mutex_unlock(&ddata->lock);
291
292 return bytes_read;
293
294err:
295 mutex_unlock(&ddata->lock);
296 return r;
297}
298
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200299static bool tfp410_detect(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300300{
301 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300302 unsigned char out;
303 int r;
304
305 mutex_lock(&ddata->lock);
306
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200307 if (!ddata->i2c_adapter)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300308 goto out;
309
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200310 r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300311
312 mutex_unlock(&ddata->lock);
313
314 return r == 0;
315
316out:
317 mutex_unlock(&ddata->lock);
318 return true;
319}
320
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200321static struct omap_dss_driver tfp410_driver = {
322 .probe = tfp410_probe,
323 .remove = __exit_p(tfp410_remove),
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300324
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200325 .enable = tfp410_enable,
326 .disable = tfp410_disable,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300327
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200328 .set_timings = tfp410_set_timings,
329 .get_timings = tfp410_get_timings,
330 .check_timings = tfp410_check_timings,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300331
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200332 .read_edid = tfp410_read_edid,
333 .detect = tfp410_detect,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300334
335 .driver = {
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200336 .name = "tfp410",
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300337 .owner = THIS_MODULE,
338 },
339};
340
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200341static int __init tfp410_init(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300342{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200343 return omap_dss_register_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300344}
345
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200346static void __exit tfp410_exit(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300347{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200348 omap_dss_unregister_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300349}
350
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200351module_init(tfp410_init);
352module_exit(tfp410_exit);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300353MODULE_LICENSE("GPL");