blob: 40cc0cfa5d179c77ce8c4bcf0282391162140766 [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
68 r = omapdss_dpi_display_enable(dssdev);
69 if (r)
70 goto err0;
71
Tomi Valkeinen2da35192011-12-22 10:37:33 +020072 if (gpio_is_valid(ddata->pd_gpio))
Russ Dillaf461d62012-05-09 15:08:08 -070073 gpio_set_value_cansleep(ddata->pd_gpio, 1);
Tomi Valkeinen2da35192011-12-22 10:37:33 +020074
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030075 return 0;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030076err0:
77 return r;
78}
79
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020080static void tfp410_power_off(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030081{
Tomi Valkeinen2da35192011-12-22 10:37:33 +020082 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030083
84 if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE)
85 return;
86
Tomi Valkeinen2da35192011-12-22 10:37:33 +020087 if (gpio_is_valid(ddata->pd_gpio))
Russ Dillaf461d62012-05-09 15:08:08 -070088 gpio_set_value_cansleep(ddata->pd_gpio, 0);
Tomi Valkeinen2da35192011-12-22 10:37:33 +020089
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030090 omapdss_dpi_display_disable(dssdev);
91}
92
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +020093static int tfp410_probe(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030094{
95 struct panel_drv_data *ddata;
Tomi Valkeinen2da35192011-12-22 10:37:33 +020096 int r;
Tomi Valkeinen958f2712012-02-17 12:19:48 +020097 int i2c_bus_num;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +030098
Tomi Valkeinen958f2712012-02-17 12:19:48 +020099 ddata = devm_kzalloc(&dssdev->dev, sizeof(*ddata), GFP_KERNEL);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300100 if (!ddata)
101 return -ENOMEM;
102
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200103 dssdev->panel.timings = tfp410_default_timings;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300104
105 ddata->dssdev = dssdev;
106 mutex_init(&ddata->lock);
107
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200108 if (dssdev->data) {
109 struct tfp410_platform_data *pdata = dssdev->data;
110
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200111 ddata->pd_gpio = pdata->power_down_gpio;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200112 i2c_bus_num = pdata->i2c_bus_num;
113 } else {
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200114 ddata->pd_gpio = -1;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200115 i2c_bus_num = -1;
116 }
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200117
118 if (gpio_is_valid(ddata->pd_gpio)) {
119 r = gpio_request_one(ddata->pd_gpio, GPIOF_OUT_INIT_LOW,
120 "tfp410 pd");
121 if (r) {
122 dev_err(&dssdev->dev, "Failed to request PD GPIO %d\n",
123 ddata->pd_gpio);
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200124 return r;
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200125 }
126 }
127
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200128 if (i2c_bus_num != -1) {
129 struct i2c_adapter *adapter;
130
131 adapter = i2c_get_adapter(i2c_bus_num);
132 if (!adapter) {
133 dev_err(&dssdev->dev, "Failed to get I2C adapter, bus %d\n",
134 i2c_bus_num);
135 r = -EINVAL;
136 goto err_i2c;
137 }
138
139 ddata->i2c_adapter = adapter;
140 }
141
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300142 dev_set_drvdata(&dssdev->dev, ddata);
143
144 return 0;
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200145err_i2c:
146 if (gpio_is_valid(ddata->pd_gpio))
147 gpio_free(ddata->pd_gpio);
148 return r;
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300149}
150
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200151static void __exit tfp410_remove(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300152{
153 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
154
155 mutex_lock(&ddata->lock);
156
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200157 if (ddata->i2c_adapter)
158 i2c_put_adapter(ddata->i2c_adapter);
159
Tomi Valkeinen2da35192011-12-22 10:37:33 +0200160 if (gpio_is_valid(ddata->pd_gpio))
161 gpio_free(ddata->pd_gpio);
162
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300163 dev_set_drvdata(&dssdev->dev, NULL);
164
165 mutex_unlock(&ddata->lock);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300166}
167
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200168static int tfp410_enable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300169{
170 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
171 int r;
172
173 mutex_lock(&ddata->lock);
174
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200175 r = tfp410_power_on(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300176 if (r == 0)
177 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
178
179 mutex_unlock(&ddata->lock);
180
181 return r;
182}
183
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200184static void tfp410_disable(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300185{
186 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
187
188 mutex_lock(&ddata->lock);
189
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200190 tfp410_power_off(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300191
192 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
193
194 mutex_unlock(&ddata->lock);
195}
196
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200197static int tfp410_suspend(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300198{
199 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
200
201 mutex_lock(&ddata->lock);
202
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200203 tfp410_power_off(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300204
205 dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
206
207 mutex_unlock(&ddata->lock);
208
209 return 0;
210}
211
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200212static int tfp410_resume(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300213{
214 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
215 int r;
216
217 mutex_lock(&ddata->lock);
218
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200219 r = tfp410_power_on(dssdev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300220 if (r == 0)
221 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
222
223 mutex_unlock(&ddata->lock);
224
225 return r;
226}
227
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200228static void tfp410_set_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300229 struct omap_video_timings *timings)
230{
231 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
232
233 mutex_lock(&ddata->lock);
234 dpi_set_timings(dssdev, timings);
235 mutex_unlock(&ddata->lock);
236}
237
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200238static void tfp410_get_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300239 struct omap_video_timings *timings)
240{
241 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
242
243 mutex_lock(&ddata->lock);
244 *timings = dssdev->panel.timings;
245 mutex_unlock(&ddata->lock);
246}
247
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200248static int tfp410_check_timings(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300249 struct omap_video_timings *timings)
250{
251 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
252 int r;
253
254 mutex_lock(&ddata->lock);
255 r = dpi_check_timings(dssdev, timings);
256 mutex_unlock(&ddata->lock);
257
258 return r;
259}
260
261
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200262static int tfp410_ddc_read(struct i2c_adapter *adapter,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300263 unsigned char *buf, u16 count, u8 offset)
264{
265 int r, retries;
266
267 for (retries = 3; retries > 0; retries--) {
268 struct i2c_msg msgs[] = {
269 {
270 .addr = DDC_ADDR,
271 .flags = 0,
272 .len = 1,
273 .buf = &offset,
274 }, {
275 .addr = DDC_ADDR,
276 .flags = I2C_M_RD,
277 .len = count,
278 .buf = buf,
279 }
280 };
281
282 r = i2c_transfer(adapter, msgs, 2);
283 if (r == 2)
284 return 0;
285
286 if (r != -EAGAIN)
287 break;
288 }
289
290 return r < 0 ? r : -EIO;
291}
292
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200293static int tfp410_read_edid(struct omap_dss_device *dssdev,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300294 u8 *edid, int len)
295{
296 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300297 int r, l, bytes_read;
298
299 mutex_lock(&ddata->lock);
300
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200301 if (!ddata->i2c_adapter) {
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300302 r = -ENODEV;
303 goto err;
304 }
305
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300306 l = min(EDID_LENGTH, len);
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200307 r = tfp410_ddc_read(ddata->i2c_adapter, edid, l, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300308 if (r)
309 goto err;
310
311 bytes_read = l;
312
313 /* if there are extensions, read second block */
314 if (len > EDID_LENGTH && edid[0x7e] > 0) {
315 l = min(EDID_LENGTH, len - EDID_LENGTH);
316
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200317 r = tfp410_ddc_read(ddata->i2c_adapter, edid + EDID_LENGTH,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300318 l, EDID_LENGTH);
319 if (r)
320 goto err;
321
322 bytes_read += l;
323 }
324
325 mutex_unlock(&ddata->lock);
326
327 return bytes_read;
328
329err:
330 mutex_unlock(&ddata->lock);
331 return r;
332}
333
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200334static bool tfp410_detect(struct omap_dss_device *dssdev)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300335{
336 struct panel_drv_data *ddata = dev_get_drvdata(&dssdev->dev);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300337 unsigned char out;
338 int r;
339
340 mutex_lock(&ddata->lock);
341
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200342 if (!ddata->i2c_adapter)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300343 goto out;
344
Tomi Valkeinen958f2712012-02-17 12:19:48 +0200345 r = tfp410_ddc_read(ddata->i2c_adapter, &out, 1, 0);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300346
347 mutex_unlock(&ddata->lock);
348
349 return r == 0;
350
351out:
352 mutex_unlock(&ddata->lock);
353 return true;
354}
355
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200356static struct omap_dss_driver tfp410_driver = {
357 .probe = tfp410_probe,
358 .remove = __exit_p(tfp410_remove),
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300359
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200360 .enable = tfp410_enable,
361 .disable = tfp410_disable,
362 .suspend = tfp410_suspend,
363 .resume = tfp410_resume,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300364
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200365 .set_timings = tfp410_set_timings,
366 .get_timings = tfp410_get_timings,
367 .check_timings = tfp410_check_timings,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300368
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200369 .read_edid = tfp410_read_edid,
370 .detect = tfp410_detect,
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300371
372 .driver = {
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200373 .name = "tfp410",
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300374 .owner = THIS_MODULE,
375 },
376};
377
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200378static int __init tfp410_init(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300379{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200380 return omap_dss_register_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300381}
382
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200383static void __exit tfp410_exit(void)
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300384{
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200385 omap_dss_unregister_driver(&tfp410_driver);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300386}
387
Tomi Valkeinen2e6f2ee2012-03-05 14:29:28 +0200388module_init(tfp410_init);
389module_exit(tfp410_exit);
Tomi Valkeinenba2eac92011-09-01 09:17:41 +0300390MODULE_LICENSE("GPL");