fbdev/fb_notify: fix blank_mode pointer crash
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / tve / rk1000 / rk1000_tve.c
1 /*
2  * rk1000_tv.c
3  *
4  * Driver for rockchip rk1000 tv control
5  *  Copyright (C) 2009
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 as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  *
13  */
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/fcntl.h>
19 #include <linux/fs.h>
20 #include <linux/fb.h>
21 #include <linux/rk_fb.h>
22 #ifdef CONFIG_OF
23 #include <linux/of.h>
24 #include <linux/of_gpio.h>
25 #include <dt-bindings/rkfb/rk_fb.h>
26 #endif
27 #include "rk1000_tve.h"
28
29 struct rk1000_tve rk1000_tve;
30 int cvbsformat = -1;
31
32 int rk1000_tv_write_block(u8 reg, u8 *buf, u8 len)
33 {
34         int i, ret;
35
36         for (i = 0; i < len; i++) {
37                 ret = rk1000_i2c_send(I2C_ADDR_TVE, reg + i, buf[i]);
38                 if (ret)
39                         break;
40         }
41         return ret;
42 }
43
44 int rk1000_control_write_block(u8 reg, u8 *buf, u8 len)
45 {
46         int i;
47         int ret;
48
49         for (i = 0; i < len; i++) {
50                 ret = rk1000_i2c_send(I2C_ADDR_CTRL, reg + i, buf[i]);
51                 if (ret)
52                         break;
53         }
54         return ret;
55 }
56
57 static int __init bootloader_rk1000_setup(char *str)
58 {
59         if (str) {
60                 pr_info("cvbs init tve.format is %s\n", str);
61                 if (kstrtoint(str, 0, &cvbsformat) < 0)
62                         cvbsformat = -1;
63         }
64         return 0;
65 }
66
67 early_param("tve.format", bootloader_rk1000_setup);
68
69 int rk1000_switch_fb(const struct fb_videomode *modedb, int tv_mode)
70 {
71         struct rk_screen *screen;
72
73         if (modedb == NULL)
74                 return -1;
75         screen =  kzalloc(sizeof(*screen), GFP_KERNEL);
76         if (screen == NULL)
77                 return -1;
78         memset(screen, 0, sizeof(*screen));
79         /* screen type & face */
80         screen->type = SCREEN_RGB;
81         screen->face = OUT_P888;
82         screen->mode = *modedb;
83         screen->mode.vmode = 0;
84         /* Pin polarity */
85         if (FB_SYNC_HOR_HIGH_ACT & modedb->sync)
86                 screen->pin_hsync = 1;
87         else
88                 screen->pin_hsync = 0;
89         if (FB_SYNC_VERT_HIGH_ACT & modedb->sync)
90                 screen->pin_vsync = 1;
91         else
92                 screen->pin_vsync = 0;
93         screen->pin_den = 0;
94         screen->pin_dclk = 0;
95         /* Swap rule */
96         screen->swap_rb = 0;
97         screen->swap_rg = 0;
98         screen->swap_gb = 0;
99         screen->swap_delta = 0;
100         screen->swap_dumy = 0;
101         screen->overscan.left = 95;
102         screen->overscan.top = 95;
103         screen->overscan.right = 95;
104         screen->overscan.bottom = 95;
105         /* Operation function*/
106         screen->init = NULL;
107         screen->standby = NULL;
108         switch (tv_mode) {
109         #ifdef CONFIG_RK1000_TVOUT_CVBS
110         case TVOUT_CVBS_NTSC:
111                 screen->init = rk1000_tv_ntsc_init;
112         break;
113         case TVOUT_CVBS_PAL:
114                 screen->init = rk1000_tv_pal_init;
115         break;
116         #endif
117         #ifdef CONFIG_RK1000_TVOUT_YPBPR
118         case TVOUT_YPBPR_720X480P_60:
119                 screen->init = rk1000_tv_ypbpr480_init;
120         break;
121         case TVOUT_YPBPR_720X576P_50:
122                 screen->init = rk1000_tv_ypbpr576_init;
123         break;
124         case TVOUT_YPBPR_1280X720P_50:
125                 screen->init = rk1000_tv_ypbpr720_50_init;
126         break;
127         case TVOUT_YPBPR_1280X720P_60:
128                 screen->init = rk1000_tv_ypbpr720_60_init;
129         break;
130         #endif
131         default:
132                 kfree(screen);
133                 return -1;
134         }
135         rk_fb_switch_screen(screen, 1 , rk1000_tve.video_source);
136         rk1000_tve.mode = tv_mode;
137         kfree(screen);
138         if (gpio_is_valid(rk1000_tve.io_switch.gpio)) {
139                 if (tv_mode < TVOUT_YPBPR_720X480P_60)
140                         gpio_direction_output(rk1000_tve.io_switch.gpio,
141                                               !(rk1000_tve.io_switch.active));
142                 else
143                         gpio_direction_output(rk1000_tve.io_switch.gpio,
144                                               rk1000_tve.io_switch.active);
145         }
146         return 0;
147 }
148
149 int rk1000_tv_standby(int type)
150 {
151         unsigned char val;
152         int ret;
153         int ypbpr;
154         int cvbs;
155         struct rk_screen screen;
156
157         ypbpr = 0;
158         cvbs = 0;
159         if (rk1000_tve.ypbpr)
160                 ypbpr = rk1000_tve.ypbpr->enable;
161         if (rk1000_tve.cvbs)
162                 cvbs = rk1000_tve.cvbs->enable;
163         if (cvbs || ypbpr)
164                 return 0;
165 /*      val = 0x00;
166         ret = rk1000_control_write_block(0x03, &val, 1);
167         if (ret < 0) {
168                 pr_err("rk1000_control_write_block err!\n");
169                 return ret;
170         } */
171         val = 0x07;
172         ret = rk1000_tv_write_block(0x03, &val, 1);
173         if (ret < 0) {
174                 pr_err("rk1000_tv_write_block err!\n");
175                 return ret;
176         }
177         screen.type = SCREEN_RGB;
178         rk_fb_switch_screen(&screen, 0 , rk1000_tve.video_source);
179         pr_err("rk1000 tve standby\n");
180         return 0;
181 }
182
183 static int rk1000_tve_initial(void)
184 {
185         struct rk_screen screen;
186
187         /* RK1000 tvencoder i2c reg need dclk, so we open lcdc.*/
188         memset(&screen, 0, sizeof(struct rk_screen));
189         /* screen type & face */
190         screen.type = SCREEN_RGB;
191         screen.face = OUT_P888;
192         /* Screen size */
193         screen.mode.xres = 720;
194         screen.mode.yres = 480;
195         /* Timing */
196         screen.mode.pixclock = 27000000;
197         screen.mode.refresh = 60;
198         screen.mode.left_margin = 116;
199         screen.mode.right_margin = 16;
200         screen.mode.hsync_len = 6;
201         screen.mode.upper_margin = 25;
202         screen.mode.lower_margin = 14;
203         screen.mode.vsync_len = 6;
204         rk_fb_switch_screen(&screen, 2 , rk1000_tve.video_source);
205         /* Power down RK1000 output DAC. */
206         if (cvbsformat < 0)
207                 return rk1000_i2c_send(I2C_ADDR_TVE, 0x03, 0x07);
208         else
209                 return 0;
210 }
211
212
213 static void rk1000_early_suspend(void *h)
214 {
215         pr_info("rk1000_early_suspend\n");
216         if (rk1000_tve.ypbpr) {
217                 if (rk1000_tve.ypbpr->enable)
218                         rk1000_tve.ypbpr->ddev->ops->setenable(
219                         rk1000_tve.ypbpr->ddev, 0);
220                 if (!rk1000_tve.ypbpr->suspend)
221                         rk1000_tve.ypbpr->suspend = 1;
222         }
223         if (rk1000_tve.cvbs) {
224                 if (rk1000_tve.cvbs->enable)
225                         rk1000_tve.cvbs->ddev->ops->setenable(
226                         rk1000_tve.cvbs->ddev, 0);
227                 if (!rk1000_tve.cvbs->suspend)
228                         rk1000_tve.cvbs->suspend = 1;
229         }
230 }
231
232
233 static void rk1000_early_resume(void *h)
234 {
235         pr_info("rk1000 tve exit early resume\n");
236         if (rk1000_tve.cvbs) {
237                 if (rk1000_tve.cvbs->suspend)
238                         rk1000_tve.cvbs->suspend = 0;
239                 if (rk1000_tve.mode < TVOUT_YPBPR_720X480P_60) {
240                         rk_display_device_enable(
241                         (rk1000_tve.cvbs)->ddev);
242                 }
243         }
244         if (rk1000_tve.ypbpr) {
245                 if (rk1000_tve.ypbpr->suspend)
246                         rk1000_tve.ypbpr->suspend = 0;
247                 if (rk1000_tve.mode > TVOUT_CVBS_PAL) {
248                         rk_display_device_enable(
249                         (rk1000_tve.ypbpr)->ddev);
250                 }
251         }
252 }
253
254
255 static int rk1000_fb_event_notify(struct notifier_block *self,
256                                   unsigned long action, void *data)
257 {
258         struct fb_event *event;
259
260         event = data;
261         if (action == FB_EARLY_EVENT_BLANK) {
262                 switch (*((int *)event->data)) {
263                 case FB_BLANK_UNBLANK:
264                 break;
265                 default:
266                 rk1000_early_suspend(NULL);
267                 break;
268                 }
269         } else if (action == FB_EVENT_BLANK) {
270                 switch (*((int *)event->data)) {
271                 case FB_BLANK_UNBLANK:
272                         rk1000_early_resume(NULL);
273                 break;
274                 default:
275                 break;
276                 }
277         }
278         return NOTIFY_OK;
279 }
280
281 static struct notifier_block rk1000_fb_notifier = {
282         .notifier_call = rk1000_fb_event_notify,
283 };
284
285 static int rk1000_tve_probe(struct i2c_client *client,
286                             const struct i2c_device_id *id)
287 {
288         struct device_node *tve_np;
289         enum of_gpio_flags flags;
290         int rc;
291
292         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
293                 return -ENODEV;
294
295         memset(&rk1000_tve, 0, sizeof(struct rk1000_tve));
296         rk1000_tve.client = client;
297 #ifdef CONFIG_OF
298         tve_np = client->dev.of_node;
299         rk1000_tve.io_switch.gpio = of_get_named_gpio_flags(tve_np,
300                                                             "gpio-reset",
301                                                             0, &flags);
302         if (gpio_is_valid(rk1000_tve.io_switch.gpio)) {
303                 rc = gpio_request(rk1000_tve.io_switch.gpio,
304                                   "rk1000-tve-swicth-io");
305                 if (!rc) {
306                         rk1000_tve.io_switch.active = !(flags &
307                                                         OF_GPIO_ACTIVE_LOW);
308                         gpio_direction_output(rk1000_tve.io_switch.gpio,
309                                               !(rk1000_tve.io_switch.active));
310                 } else
311                         pr_err("gpio request rk1000-tve-swicth-io err: %d\n",
312                                rk1000_tve.io_switch.gpio);
313         }
314         of_property_read_u32(tve_np, "rockchip,source", &(rc));
315         rk1000_tve.video_source = rc;
316         of_property_read_u32(tve_np, "rockchip,prop", &(rc));
317         rk1000_tve.property = rc - 1;
318         pr_err("video src is lcdc%d, prop is %d\n", rk1000_tve.video_source,
319                rk1000_tve.property);
320 #endif
321         if (cvbsformat >= 0) {
322                 rk1000_tve.mode = cvbsformat + 1;
323         } else {
324                 rk1000_tve.mode = RK1000_TVOUT_DEAULT;
325                 rc = rk1000_tve_initial();
326                 if (rc) {
327                         dev_err(&client->dev,
328                                 "rk1000 tvencoder probe error %d\n", rc);
329                         return -EINVAL;
330                 }
331         }
332
333 #ifdef CONFIG_RK1000_TVOUT_YPBPR
334         rk1000_register_display_ypbpr(&client->dev);
335 #endif
336
337 #ifdef CONFIG_RK1000_TVOUT_CVBS
338         rk1000_register_display_cvbs(&client->dev);
339 #endif
340         #ifdef CONFIG_HAS_EARLYSUSPEND
341         rk1000_tve.early_suspend.suspend = rk1000_early_suspend;
342         rk1000_tve.early_suspend.resume = rk1000_early_resume;
343         rk1000_tve.early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB - 11;
344         register_early_suspend(&(rk1000_tve.early_suspend));
345         #endif
346         fb_register_client(&rk1000_fb_notifier);
347         pr_info("rk1000 tvencoder ver 2.0 probe ok\n");
348         return 0;
349 }
350
351 static int rk1000_tve_remove(struct i2c_client *client)
352 {
353         return 0;
354 }
355
356 static void rk1000_tve_shutdown(struct i2c_client *client)
357 {
358         rk1000_i2c_send(I2C_ADDR_TVE, 0x03, 0x07);
359 }
360
361 static const struct i2c_device_id rk1000_tve_id[] = {
362         { "rk1000_tve", 0 },
363         { }
364 };
365 MODULE_DEVICE_TABLE(i2c, rk1000_tve_id);
366
367 static struct i2c_driver rk1000_tve_driver = {
368         .driver         = {
369                 .name   = "rk1000_tve",
370         },
371         .id_table = rk1000_tve_id,
372         .probe = rk1000_tve_probe,
373         .remove = rk1000_tve_remove,
374         .shutdown = rk1000_tve_shutdown,
375 };
376
377 static int __init rk1000_tve_init(void)
378 {
379         int ret;
380
381         ret = i2c_add_driver(&rk1000_tve_driver);
382         if (ret < 0)
383                 pr_err("i2c_add_driver err, ret = %d\n", ret);
384         return ret;
385 }
386
387 static void __exit rk1000_tve_exit(void)
388 {
389         i2c_del_driver(&rk1000_tve_driver);
390 }
391
392 late_initcall(rk1000_tve_init);
393 module_exit(rk1000_tve_exit);
394
395 /* Module information */
396 MODULE_DESCRIPTION("ROCKCHIP rk1000 TV Encoder ");
397 MODULE_LICENSE("GPL");