blob: e6d6849a27de32239801f0fc79006782749f2cff [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
27#include <video/omap-panel-dvi.h>
28
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,
42};
43
44struct panel_drv_data {
45 struct omap_dss_device *dssdev;
46
47 struct mutex lock;
Tomi Valkeinen2da35192011-12-22 10:37:33 +020048
49 int pd_gpio;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030050};
51
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020052static inline struct tfp410_platform_data
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030053*get_pdata(const struct omap_dss_device *dssdev)
54{
55 return dssdev->data;
56}
57
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020058static int tfp410_power_on(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030059{
Tomi Valkeinen2da35192011-12-22 10:37:33 +020060 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030061 int r;
62
63 if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
64 return 0;
65
66 r = omapdss_dpi_display_enable(dssdev);
67 if (r)
68 goto err0;
69
Tomi Valkeinen2da35192011-12-22 10:37:33 +020070 if (gpio_is_valid(ddata->pd_gpio))
71 gpio_set_value(ddata->pd_gpio, 1);
72
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030073 return 0;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030074err0:
75 return r;
76}
77
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020078static void tfp410_power_off(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030079{
Tomi Valkeinen2da35192011-12-22 10:37:33 +020080 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030081
82 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
83 return;
84
Tomi Valkeinen2da35192011-12-22 10:37:33 +020085 if (gpio_is_valid(ddata->pd_gpio))
86 gpio_set_value(ddata->pd_gpio, 0);
87
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030088 omapdss_dpi_display_disable(dssdev);
89}
90
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020091static int tfp410_probe(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030092{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020093 struct tfp410_platform_data *pdata = get_pdata(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030094 struct panel_drv_data *ddata;
Tomi Valkeinen2da35192011-12-22 10:37:33 +020095 int r;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030096
97 ddata = kzalloc(sizeof(*ddata), GFP_KERNEL);
98 if (!ddata)
99 return -ENOMEM;
100
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200101 dssdev->panel.timings = tfp410_default_timings;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300102 dssdev->panel.config = OMAP_DSS_LCD_TFT;
103
104 ddata->dssdev = dssdev;
105 mutex_init(&ddata->lock);
106
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200107 if (pdata)
108 ddata->pd_gpio = pdata->power_down_gpio;
109 else
110 ddata->pd_gpio = -1;
111
112 if (gpio_is_valid(ddata->pd_gpio)) {
113 r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
114 "tfp410 pd");
115 if (r) {
116 dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
117 ddata->pd_gpio);
118 ddata->pd_gpio = -1;
119 }
120 }
121
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300122 dev_set_drvdata(&dssdev->dev, ddata);
123
124 return 0;
125}
126
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200127static void __exit tfp410_remove(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300128{
129 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
130
131 mutex_lock(&ddata->lock);
132
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200133 if (gpio_is_valid(ddata->pd_gpio))
134 gpio_free(ddata->pd_gpio);
135
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300136 dev_set_drvdata(&dssdev->dev, NULL);
137
138 mutex_unlock(&ddata->lock);
139
140 kfree(ddata);
141}
142
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200143static int tfp410_enable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300144{
145 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
146 int r;
147
148 mutex_lock(&ddata->lock);
149
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200150 r = tfp410_power_on(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300151 if (r == 0)
152 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
153
154 mutex_unlock(&ddata->lock);
155
156 return r;
157}
158
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200159static void tfp410_disable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300160{
161 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
162
163 mutex_lock(&ddata->lock);
164
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200165 tfp410_power_off(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300166
167 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
168
169 mutex_unlock(&ddata->lock);
170}
171
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200172static int tfp410_suspend(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300173{
174 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
175
176 mutex_lock(&ddata->lock);
177
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200178 tfp410_power_off(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300179
180 dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
181
182 mutex_unlock(&ddata->lock);
183
184 return 0;
185}
186
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200187static int tfp410_resume(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300188{
189 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
190 int r;
191
192 mutex_lock(&ddata->lock);
193
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200194 r = tfp410_power_on(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300195 if (r == 0)
196 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
197
198 mutex_unlock(&ddata->lock);
199
200 return r;
201}
202
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200203static void tfp410_set_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 dpi_set_timings(dssdev, timings);
210 mutex_unlock(&ddata->lock);
211}
212
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200213static void tfp410_get_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
218 mutex_lock(&ddata->lock);
219 *timings = dssdev->panel.timings;
220 mutex_unlock(&ddata->lock);
221}
222
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200223static int tfp410_check_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300224 struct omap_video_timings *timings)
225{
226 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
227 int r;
228
229 mutex_lock(&ddata->lock);
230 r = dpi_check_timings(dssdev, timings);
231 mutex_unlock(&ddata->lock);
232
233 return r;
234}
235
236
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200237static int tfp410_ddc_read(struct i2c_adapter *adapter,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300238 unsigned char *buf, u16 count, u8 offset)
239{
240 int r, retries;
241
242 for (retries = 3; retries > 0; retries--) {
243 struct i2c_msg msgs[] = {
244 {
245 .addr = DDC_ADDR,
246 .flags = 0,
247 .len = 1,
248 .buf = &offset,
249 }, {
250 .addr = DDC_ADDR,
251 .flags = I2C_M_RD,
252 .len = count,
253 .buf = buf,
254 }
255 };
256
257 r = i2c_transfer(adapter, msgs, 2);
258 if (r == 2)
259 return 0;
260
261 if (r != -EAGAIN)
262 break;
263 }
264
265 return r < 0 ? r : -EIO;
266}
267
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200268static int tfp410_read_edid(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300269 u8 *edid, int len)
270{
271 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200272 struct tfp410_platform_data *pdata = get_pdata(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300273 struct i2c_adapter *adapter;
274 int r, l, bytes_read;
275
276 mutex_lock(&ddata->lock);
277
278 if (pdata->i2c_bus_num == 0) {
279 r = -ENODEV;
280 goto err;
281 }
282
283 adapter = i2c_get_adapter(pdata->i2c_bus_num);
284 if (!adapter) {
285 dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
286 pdata->i2c_bus_num);
287 r = -EINVAL;
288 goto err;
289 }
290
291 l = min(EDID_LENGTH, len);
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200292 r = tfp410_ddc_read(adapter, edid, l, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300293 if (r)
294 goto err;
295
296 bytes_read = l;
297
298 /* if there are extensions, read second block */
299 if (len > EDID_LENGTH && edid[0x7e] > 0) {
300 l = min(EDID_LENGTH, len - EDID_LENGTH);
301
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200302 r = tfp410_ddc_read(adapter, edid + EDID_LENGTH,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300303 l, EDID_LENGTH);
304 if (r)
305 goto err;
306
307 bytes_read += l;
308 }
309
310 mutex_unlock(&ddata->lock);
311
312 return bytes_read;
313
314err:
315 mutex_unlock(&ddata->lock);
316 return r;
317}
318
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200319static bool tfp410_detect(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300320{
321 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200322 struct tfp410_platform_data *pdata = get_pdata(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300323 struct i2c_adapter *adapter;
324 unsigned char out;
325 int r;
326
327 mutex_lock(&ddata->lock);
328
329 if (pdata->i2c_bus_num == 0)
330 goto out;
331
332 adapter = i2c_get_adapter(pdata->i2c_bus_num);
333 if (!adapter)
334 goto out;
335
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200336 r = tfp410_ddc_read(adapter, &out, 1, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300337
338 mutex_unlock(&ddata->lock);
339
340 return r == 0;
341
342out:
343 mutex_unlock(&ddata->lock);
344 return true;
345}
346
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200347static struct omap_dss_driver tfp410_driver = {
348 .probe = tfp410_probe,
349 .remove = __exit_p(tfp410_remove),
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300350
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200351 .enable = tfp410_enable,
352 .disable = tfp410_disable,
353 .suspend = tfp410_suspend,
354 .resume = tfp410_resume,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300355
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200356 .set_timings = tfp410_set_timings,
357 .get_timings = tfp410_get_timings,
358 .check_timings = tfp410_check_timings,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300359
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200360 .read_edid = tfp410_read_edid,
361 .detect = tfp410_detect,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300362
363 .driver = {
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200364 .name = "tfp410",
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300365 .owner = THIS_MODULE,
366 },
367};
368
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200369static int __init tfp410_init(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300370{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200371 return omap_dss_register_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300372}
373
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200374static void __exit tfp410_exit(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300375{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200376 omap_dss_unregister_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300377}
378
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200379module_init(tfp410_init);
380module_exit(tfp410_exit);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300381MODULE_LICENSE("GPL");