hwa742.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Epson HWA742 LCD controller driver
  3. *
  4. * Copyright (C) 2004-2005 Nokia Corporation
  5. * Authors: Juha Yrjölä <juha.yrjola@nokia.com>
  6. * Imre Deak <imre.deak@nokia.com>
  7. * YUV support: Jussi Laako <jussi.laako@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/fb.h>
  26. #include <linux/delay.h>
  27. #include <linux/clk.h>
  28. #include <linux/interrupt.h>
  29. #include "omapfb.h"
  30. #define HWA742_REV_CODE_REG 0x0
  31. #define HWA742_CONFIG_REG 0x2
  32. #define HWA742_PLL_DIV_REG 0x4
  33. #define HWA742_PLL_0_REG 0x6
  34. #define HWA742_PLL_1_REG 0x8
  35. #define HWA742_PLL_2_REG 0xa
  36. #define HWA742_PLL_3_REG 0xc
  37. #define HWA742_PLL_4_REG 0xe
  38. #define HWA742_CLK_SRC_REG 0x12
  39. #define HWA742_PANEL_TYPE_REG 0x14
  40. #define HWA742_H_DISP_REG 0x16
  41. #define HWA742_H_NDP_REG 0x18
  42. #define HWA742_V_DISP_1_REG 0x1a
  43. #define HWA742_V_DISP_2_REG 0x1c
  44. #define HWA742_V_NDP_REG 0x1e
  45. #define HWA742_HS_W_REG 0x20
  46. #define HWA742_HP_S_REG 0x22
  47. #define HWA742_VS_W_REG 0x24
  48. #define HWA742_VP_S_REG 0x26
  49. #define HWA742_PCLK_POL_REG 0x28
  50. #define HWA742_INPUT_MODE_REG 0x2a
  51. #define HWA742_TRANSL_MODE_REG1 0x2e
  52. #define HWA742_DISP_MODE_REG 0x34
  53. #define HWA742_WINDOW_TYPE 0x36
  54. #define HWA742_WINDOW_X_START_0 0x38
  55. #define HWA742_WINDOW_X_START_1 0x3a
  56. #define HWA742_WINDOW_Y_START_0 0x3c
  57. #define HWA742_WINDOW_Y_START_1 0x3e
  58. #define HWA742_WINDOW_X_END_0 0x40
  59. #define HWA742_WINDOW_X_END_1 0x42
  60. #define HWA742_WINDOW_Y_END_0 0x44
  61. #define HWA742_WINDOW_Y_END_1 0x46
  62. #define HWA742_MEMORY_WRITE_LSB 0x48
  63. #define HWA742_MEMORY_WRITE_MSB 0x49
  64. #define HWA742_MEMORY_READ_0 0x4a
  65. #define HWA742_MEMORY_READ_1 0x4c
  66. #define HWA742_MEMORY_READ_2 0x4e
  67. #define HWA742_POWER_SAVE 0x56
  68. #define HWA742_NDP_CTRL 0x58
  69. #define HWA742_AUTO_UPDATE_TIME (HZ / 20)
  70. /* Reserve 4 request slots for requests in irq context */
  71. #define REQ_POOL_SIZE 24
  72. #define IRQ_REQ_POOL_SIZE 4
  73. #define REQ_FROM_IRQ_POOL 0x01
  74. #define REQ_COMPLETE 0
  75. #define REQ_PENDING 1
  76. struct update_param {
  77. int x, y, width, height;
  78. int color_mode;
  79. int flags;
  80. };
  81. struct hwa742_request {
  82. struct list_head entry;
  83. unsigned int flags;
  84. int (*handler)(struct hwa742_request *req);
  85. void (*complete)(void *data);
  86. void *complete_data;
  87. union {
  88. struct update_param update;
  89. struct completion *sync;
  90. } par;
  91. };
  92. struct {
  93. enum omapfb_update_mode update_mode;
  94. enum omapfb_update_mode update_mode_before_suspend;
  95. struct timer_list auto_update_timer;
  96. int stop_auto_update;
  97. struct omapfb_update_window auto_update_window;
  98. unsigned te_connected:1;
  99. unsigned vsync_only:1;
  100. struct hwa742_request req_pool[REQ_POOL_SIZE];
  101. struct list_head pending_req_list;
  102. struct list_head free_req_list;
  103. struct semaphore req_sema;
  104. spinlock_t req_lock;
  105. struct extif_timings reg_timings, lut_timings;
  106. int prev_color_mode;
  107. int prev_flags;
  108. int window_type;
  109. u32 max_transmit_size;
  110. u32 extif_clk_period;
  111. unsigned long pix_tx_time;
  112. unsigned long line_upd_time;
  113. struct omapfb_device *fbdev;
  114. struct lcd_ctrl_extif *extif;
  115. const struct lcd_ctrl *int_ctrl;
  116. struct clk *sys_ck;
  117. } hwa742;
  118. struct lcd_ctrl hwa742_ctrl;
  119. static u8 hwa742_read_reg(u8 reg)
  120. {
  121. u8 data;
  122. hwa742.extif->set_bits_per_cycle(8);
  123. hwa742.extif->write_command(&reg, 1);
  124. hwa742.extif->read_data(&data, 1);
  125. return data;
  126. }
  127. static void hwa742_write_reg(u8 reg, u8 data)
  128. {
  129. hwa742.extif->set_bits_per_cycle(8);
  130. hwa742.extif->write_command(&reg, 1);
  131. hwa742.extif->write_data(&data, 1);
  132. }
  133. static void set_window_regs(int x_start, int y_start, int x_end, int y_end)
  134. {
  135. u8 tmp[8];
  136. u8 cmd;
  137. x_end--;
  138. y_end--;
  139. tmp[0] = x_start;
  140. tmp[1] = x_start >> 8;
  141. tmp[2] = y_start;
  142. tmp[3] = y_start >> 8;
  143. tmp[4] = x_end;
  144. tmp[5] = x_end >> 8;
  145. tmp[6] = y_end;
  146. tmp[7] = y_end >> 8;
  147. hwa742.extif->set_bits_per_cycle(8);
  148. cmd = HWA742_WINDOW_X_START_0;
  149. hwa742.extif->write_command(&cmd, 1);
  150. hwa742.extif->write_data(tmp, 8);
  151. }
  152. static void set_format_regs(int conv, int transl, int flags)
  153. {
  154. if (flags & OMAPFB_FORMAT_FLAG_DOUBLE) {
  155. hwa742.window_type = ((hwa742.window_type & 0xfc) | 0x01);
  156. #ifdef VERBOSE
  157. dev_dbg(hwa742.fbdev->dev, "hwa742: enabled pixel doubling\n");
  158. #endif
  159. } else {
  160. hwa742.window_type = (hwa742.window_type & 0xfc);
  161. #ifdef VERBOSE
  162. dev_dbg(hwa742.fbdev->dev, "hwa742: disabled pixel doubling\n");
  163. #endif
  164. }
  165. hwa742_write_reg(HWA742_INPUT_MODE_REG, conv);
  166. hwa742_write_reg(HWA742_TRANSL_MODE_REG1, transl);
  167. hwa742_write_reg(HWA742_WINDOW_TYPE, hwa742.window_type);
  168. }
  169. static void enable_tearsync(int y, int width, int height, int screen_height,
  170. int force_vsync)
  171. {
  172. u8 b;
  173. b = hwa742_read_reg(HWA742_NDP_CTRL);
  174. b |= 1 << 2;
  175. hwa742_write_reg(HWA742_NDP_CTRL, b);
  176. if (likely(hwa742.vsync_only || force_vsync)) {
  177. hwa742.extif->enable_tearsync(1, 0);
  178. return;
  179. }
  180. if (width * hwa742.pix_tx_time < hwa742.line_upd_time) {
  181. hwa742.extif->enable_tearsync(1, 0);
  182. return;
  183. }
  184. if ((width * hwa742.pix_tx_time / 1000) * height <
  185. (y + height) * (hwa742.line_upd_time / 1000)) {
  186. hwa742.extif->enable_tearsync(1, 0);
  187. return;
  188. }
  189. hwa742.extif->enable_tearsync(1, y + 1);
  190. }
  191. static void disable_tearsync(void)
  192. {
  193. u8 b;
  194. hwa742.extif->enable_tearsync(0, 0);
  195. b = hwa742_read_reg(HWA742_NDP_CTRL);
  196. b &= ~(1 << 2);
  197. hwa742_write_reg(HWA742_NDP_CTRL, b);
  198. }
  199. static inline struct hwa742_request *alloc_req(void)
  200. {
  201. unsigned long flags;
  202. struct hwa742_request *req;
  203. int req_flags = 0;
  204. if (!in_interrupt())
  205. down(&hwa742.req_sema);
  206. else
  207. req_flags = REQ_FROM_IRQ_POOL;
  208. spin_lock_irqsave(&hwa742.req_lock, flags);
  209. BUG_ON(list_empty(&hwa742.free_req_list));
  210. req = list_entry(hwa742.free_req_list.next,
  211. struct hwa742_request, entry);
  212. list_del(&req->entry);
  213. spin_unlock_irqrestore(&hwa742.req_lock, flags);
  214. INIT_LIST_HEAD(&req->entry);
  215. req->flags = req_flags;
  216. return req;
  217. }
  218. static inline void free_req(struct hwa742_request *req)
  219. {
  220. unsigned long flags;
  221. spin_lock_irqsave(&hwa742.req_lock, flags);
  222. list_move(&req->entry, &hwa742.free_req_list);
  223. if (!(req->flags & REQ_FROM_IRQ_POOL))
  224. up(&hwa742.req_sema);
  225. spin_unlock_irqrestore(&hwa742.req_lock, flags);
  226. }
  227. static void process_pending_requests(void)
  228. {
  229. unsigned long flags;
  230. spin_lock_irqsave(&hwa742.req_lock, flags);
  231. while (!list_empty(&hwa742.pending_req_list)) {
  232. struct hwa742_request *req;
  233. void (*complete)(void *);
  234. void *complete_data;
  235. req = list_entry(hwa742.pending_req_list.next,
  236. struct hwa742_request, entry);
  237. spin_unlock_irqrestore(&hwa742.req_lock, flags);
  238. if (req->handler(req) == REQ_PENDING)
  239. return;
  240. complete = req->complete;
  241. complete_data = req->complete_data;
  242. free_req(req);
  243. if (complete)
  244. complete(complete_data);
  245. spin_lock_irqsave(&hwa742.req_lock, flags);
  246. }
  247. spin_unlock_irqrestore(&hwa742.req_lock, flags);
  248. }
  249. static void submit_req_list(struct list_head *head)
  250. {
  251. unsigned long flags;
  252. int process = 1;
  253. spin_lock_irqsave(&hwa742.req_lock, flags);
  254. if (likely(!list_empty(&hwa742.pending_req_list)))
  255. process = 0;
  256. list_splice_init(head, hwa742.pending_req_list.prev);
  257. spin_unlock_irqrestore(&hwa742.req_lock, flags);
  258. if (process)
  259. process_pending_requests();
  260. }
  261. static void request_complete(void *data)
  262. {
  263. struct hwa742_request *req = (struct hwa742_request *)data;
  264. void (*complete)(void *);
  265. void *complete_data;
  266. complete = req->complete;
  267. complete_data = req->complete_data;
  268. free_req(req);
  269. if (complete)
  270. complete(complete_data);
  271. process_pending_requests();
  272. }
  273. static int send_frame_handler(struct hwa742_request *req)
  274. {
  275. struct update_param *par = &req->par.update;
  276. int x = par->x;
  277. int y = par->y;
  278. int w = par->width;
  279. int h = par->height;
  280. int bpp;
  281. int conv, transl;
  282. unsigned long offset;
  283. int color_mode = par->color_mode;
  284. int flags = par->flags;
  285. int scr_width = hwa742.fbdev->panel->x_res;
  286. int scr_height = hwa742.fbdev->panel->y_res;
  287. #ifdef VERBOSE
  288. dev_dbg(hwa742.fbdev->dev, "x %d y %d w %d h %d scr_width %d "
  289. "color_mode %d flags %d\n",
  290. x, y, w, h, scr_width, color_mode, flags);
  291. #endif
  292. switch (color_mode) {
  293. case OMAPFB_COLOR_YUV422:
  294. bpp = 16;
  295. conv = 0x08;
  296. transl = 0x25;
  297. break;
  298. case OMAPFB_COLOR_YUV420:
  299. bpp = 12;
  300. conv = 0x09;
  301. transl = 0x25;
  302. break;
  303. case OMAPFB_COLOR_RGB565:
  304. bpp = 16;
  305. conv = 0x01;
  306. transl = 0x05;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. if (hwa742.prev_flags != flags ||
  312. hwa742.prev_color_mode != color_mode) {
  313. set_format_regs(conv, transl, flags);
  314. hwa742.prev_color_mode = color_mode;
  315. hwa742.prev_flags = flags;
  316. }
  317. flags = req->par.update.flags;
  318. if (flags & OMAPFB_FORMAT_FLAG_TEARSYNC)
  319. enable_tearsync(y, scr_width, h, scr_height,
  320. flags & OMAPFB_FORMAT_FLAG_FORCE_VSYNC);
  321. else
  322. disable_tearsync();
  323. set_window_regs(x, y, x + w, y + h);
  324. offset = (scr_width * y + x) * bpp / 8;
  325. hwa742.int_ctrl->setup_plane(OMAPFB_PLANE_GFX,
  326. OMAPFB_CHANNEL_OUT_LCD, offset, scr_width, 0, 0, w, h,
  327. color_mode);
  328. hwa742.extif->set_bits_per_cycle(16);
  329. hwa742.int_ctrl->enable_plane(OMAPFB_PLANE_GFX, 1);
  330. hwa742.extif->transfer_area(w, h, request_complete, req);
  331. return REQ_PENDING;
  332. }
  333. static void send_frame_complete(void *data)
  334. {
  335. hwa742.int_ctrl->enable_plane(OMAPFB_PLANE_GFX, 0);
  336. }
  337. #define ADD_PREQ(_x, _y, _w, _h) do { \
  338. req = alloc_req(); \
  339. req->handler = send_frame_handler; \
  340. req->complete = send_frame_complete; \
  341. req->par.update.x = _x; \
  342. req->par.update.y = _y; \
  343. req->par.update.width = _w; \
  344. req->par.update.height = _h; \
  345. req->par.update.color_mode = color_mode;\
  346. req->par.update.flags = flags; \
  347. list_add_tail(&req->entry, req_head); \
  348. } while(0)
  349. static void create_req_list(struct omapfb_update_window *win,
  350. struct list_head *req_head)
  351. {
  352. struct hwa742_request *req;
  353. int x = win->x;
  354. int y = win->y;
  355. int width = win->width;
  356. int height = win->height;
  357. int color_mode;
  358. int flags;
  359. flags = win->format & ~OMAPFB_FORMAT_MASK;
  360. color_mode = win->format & OMAPFB_FORMAT_MASK;
  361. if (x & 1) {
  362. ADD_PREQ(x, y, 1, height);
  363. width--;
  364. x++;
  365. flags &= ~OMAPFB_FORMAT_FLAG_TEARSYNC;
  366. }
  367. if (width & ~1) {
  368. unsigned int xspan = width & ~1;
  369. unsigned int ystart = y;
  370. unsigned int yspan = height;
  371. if (xspan * height * 2 > hwa742.max_transmit_size) {
  372. yspan = hwa742.max_transmit_size / (xspan * 2);
  373. ADD_PREQ(x, ystart, xspan, yspan);
  374. ystart += yspan;
  375. yspan = height - yspan;
  376. flags &= ~OMAPFB_FORMAT_FLAG_TEARSYNC;
  377. }
  378. ADD_PREQ(x, ystart, xspan, yspan);
  379. x += xspan;
  380. width -= xspan;
  381. flags &= ~OMAPFB_FORMAT_FLAG_TEARSYNC;
  382. }
  383. if (width)
  384. ADD_PREQ(x, y, 1, height);
  385. }
  386. static void auto_update_complete(void *data)
  387. {
  388. if (!hwa742.stop_auto_update)
  389. mod_timer(&hwa742.auto_update_timer,
  390. jiffies + HWA742_AUTO_UPDATE_TIME);
  391. }
  392. static void hwa742_update_window_auto(unsigned long arg)
  393. {
  394. LIST_HEAD(req_list);
  395. struct hwa742_request *last;
  396. create_req_list(&hwa742.auto_update_window, &req_list);
  397. last = list_entry(req_list.prev, struct hwa742_request, entry);
  398. last->complete = auto_update_complete;
  399. last->complete_data = NULL;
  400. submit_req_list(&req_list);
  401. }
  402. int hwa742_update_window_async(struct fb_info *fbi,
  403. struct omapfb_update_window *win,
  404. void (*complete_callback)(void *arg),
  405. void *complete_callback_data)
  406. {
  407. LIST_HEAD(req_list);
  408. struct hwa742_request *last;
  409. int r = 0;
  410. if (hwa742.update_mode != OMAPFB_MANUAL_UPDATE) {
  411. dev_dbg(hwa742.fbdev->dev, "invalid update mode\n");
  412. r = -EINVAL;
  413. goto out;
  414. }
  415. if (unlikely(win->format &
  416. ~(0x03 | OMAPFB_FORMAT_FLAG_DOUBLE |
  417. OMAPFB_FORMAT_FLAG_TEARSYNC | OMAPFB_FORMAT_FLAG_FORCE_VSYNC))) {
  418. dev_dbg(hwa742.fbdev->dev, "invalid window flag\n");
  419. r = -EINVAL;
  420. goto out;
  421. }
  422. create_req_list(win, &req_list);
  423. last = list_entry(req_list.prev, struct hwa742_request, entry);
  424. last->complete = complete_callback;
  425. last->complete_data = (void *)complete_callback_data;
  426. submit_req_list(&req_list);
  427. out:
  428. return r;
  429. }
  430. EXPORT_SYMBOL(hwa742_update_window_async);
  431. static int hwa742_setup_plane(int plane, int channel_out,
  432. unsigned long offset, int screen_width,
  433. int pos_x, int pos_y, int width, int height,
  434. int color_mode)
  435. {
  436. if (plane != OMAPFB_PLANE_GFX ||
  437. channel_out != OMAPFB_CHANNEL_OUT_LCD)
  438. return -EINVAL;
  439. return 0;
  440. }
  441. static int hwa742_enable_plane(int plane, int enable)
  442. {
  443. if (plane != 0)
  444. return -EINVAL;
  445. hwa742.int_ctrl->enable_plane(plane, enable);
  446. return 0;
  447. }
  448. static int sync_handler(struct hwa742_request *req)
  449. {
  450. complete(req->par.sync);
  451. return REQ_COMPLETE;
  452. }
  453. static void hwa742_sync(void)
  454. {
  455. LIST_HEAD(req_list);
  456. struct hwa742_request *req;
  457. struct completion comp;
  458. req = alloc_req();
  459. req->handler = sync_handler;
  460. req->complete = NULL;
  461. init_completion(&comp);
  462. req->par.sync = &comp;
  463. list_add(&req->entry, &req_list);
  464. submit_req_list(&req_list);
  465. wait_for_completion(&comp);
  466. }
  467. static void hwa742_bind_client(struct omapfb_notifier_block *nb)
  468. {
  469. dev_dbg(hwa742.fbdev->dev, "update_mode %d\n", hwa742.update_mode);
  470. if (hwa742.update_mode == OMAPFB_MANUAL_UPDATE) {
  471. omapfb_notify_clients(hwa742.fbdev, OMAPFB_EVENT_READY);
  472. }
  473. }
  474. static int hwa742_set_update_mode(enum omapfb_update_mode mode)
  475. {
  476. if (mode != OMAPFB_MANUAL_UPDATE && mode != OMAPFB_AUTO_UPDATE &&
  477. mode != OMAPFB_UPDATE_DISABLED)
  478. return -EINVAL;
  479. if (mode == hwa742.update_mode)
  480. return 0;
  481. dev_info(hwa742.fbdev->dev, "HWA742: setting update mode to %s\n",
  482. mode == OMAPFB_UPDATE_DISABLED ? "disabled" :
  483. (mode == OMAPFB_AUTO_UPDATE ? "auto" : "manual"));
  484. switch (hwa742.update_mode) {
  485. case OMAPFB_MANUAL_UPDATE:
  486. omapfb_notify_clients(hwa742.fbdev, OMAPFB_EVENT_DISABLED);
  487. break;
  488. case OMAPFB_AUTO_UPDATE:
  489. hwa742.stop_auto_update = 1;
  490. del_timer_sync(&hwa742.auto_update_timer);
  491. break;
  492. case OMAPFB_UPDATE_DISABLED:
  493. break;
  494. }
  495. hwa742.update_mode = mode;
  496. hwa742_sync();
  497. hwa742.stop_auto_update = 0;
  498. switch (mode) {
  499. case OMAPFB_MANUAL_UPDATE:
  500. omapfb_notify_clients(hwa742.fbdev, OMAPFB_EVENT_READY);
  501. break;
  502. case OMAPFB_AUTO_UPDATE:
  503. hwa742_update_window_auto(0);
  504. break;
  505. case OMAPFB_UPDATE_DISABLED:
  506. break;
  507. }
  508. return 0;
  509. }
  510. static enum omapfb_update_mode hwa742_get_update_mode(void)
  511. {
  512. return hwa742.update_mode;
  513. }
  514. static unsigned long round_to_extif_ticks(unsigned long ps, int div)
  515. {
  516. int bus_tick = hwa742.extif_clk_period * div;
  517. return (ps + bus_tick - 1) / bus_tick * bus_tick;
  518. }
  519. static int calc_reg_timing(unsigned long sysclk, int div)
  520. {
  521. struct extif_timings *t;
  522. unsigned long systim;
  523. /* CSOnTime 0, WEOnTime 2 ns, REOnTime 2 ns,
  524. * AccessTime 2 ns + 12.2 ns (regs),
  525. * WEOffTime = WEOnTime + 1 ns,
  526. * REOffTime = REOnTime + 16 ns (regs),
  527. * CSOffTime = REOffTime + 1 ns
  528. * ReadCycle = 2ns + 2*SYSCLK (regs),
  529. * WriteCycle = 2*SYSCLK + 2 ns,
  530. * CSPulseWidth = 10 ns */
  531. systim = 1000000000 / (sysclk / 1000);
  532. dev_dbg(hwa742.fbdev->dev, "HWA742 systim %lu ps extif_clk_period %u ps"
  533. "extif_clk_div %d\n", systim, hwa742.extif_clk_period, div);
  534. t = &hwa742.reg_timings;
  535. memset(t, 0, sizeof(*t));
  536. t->clk_div = div;
  537. t->cs_on_time = 0;
  538. t->we_on_time = round_to_extif_ticks(t->cs_on_time + 2000, div);
  539. t->re_on_time = round_to_extif_ticks(t->cs_on_time + 2000, div);
  540. t->access_time = round_to_extif_ticks(t->re_on_time + 12200, div);
  541. t->we_off_time = round_to_extif_ticks(t->we_on_time + 1000, div);
  542. t->re_off_time = round_to_extif_ticks(t->re_on_time + 16000, div);
  543. t->cs_off_time = round_to_extif_ticks(t->re_off_time + 1000, div);
  544. t->we_cycle_time = round_to_extif_ticks(2 * systim + 2000, div);
  545. if (t->we_cycle_time < t->we_off_time)
  546. t->we_cycle_time = t->we_off_time;
  547. t->re_cycle_time = round_to_extif_ticks(2 * systim + 2000, div);
  548. if (t->re_cycle_time < t->re_off_time)
  549. t->re_cycle_time = t->re_off_time;
  550. t->cs_pulse_width = 0;
  551. dev_dbg(hwa742.fbdev->dev, "[reg]cson %d csoff %d reon %d reoff %d\n",
  552. t->cs_on_time, t->cs_off_time, t->re_on_time, t->re_off_time);
  553. dev_dbg(hwa742.fbdev->dev, "[reg]weon %d weoff %d recyc %d wecyc %d\n",
  554. t->we_on_time, t->we_off_time, t->re_cycle_time,
  555. t->we_cycle_time);
  556. dev_dbg(hwa742.fbdev->dev, "[reg]rdaccess %d cspulse %d\n",
  557. t->access_time, t->cs_pulse_width);
  558. return hwa742.extif->convert_timings(t);
  559. }
  560. static int calc_lut_timing(unsigned long sysclk, int div)
  561. {
  562. struct extif_timings *t;
  563. unsigned long systim;
  564. /* CSOnTime 0, WEOnTime 2 ns, REOnTime 2 ns,
  565. * AccessTime 2 ns + 4 * SYSCLK + 26 (lut),
  566. * WEOffTime = WEOnTime + 1 ns,
  567. * REOffTime = REOnTime + 4*SYSCLK + 26 ns (lut),
  568. * CSOffTime = REOffTime + 1 ns
  569. * ReadCycle = 2ns + 4*SYSCLK + 26 ns (lut),
  570. * WriteCycle = 2*SYSCLK + 2 ns,
  571. * CSPulseWidth = 10 ns
  572. */
  573. systim = 1000000000 / (sysclk / 1000);
  574. dev_dbg(hwa742.fbdev->dev, "HWA742 systim %lu ps extif_clk_period %u ps"
  575. "extif_clk_div %d\n", systim, hwa742.extif_clk_period, div);
  576. t = &hwa742.lut_timings;
  577. memset(t, 0, sizeof(*t));
  578. t->clk_div = div;
  579. t->cs_on_time = 0;
  580. t->we_on_time = round_to_extif_ticks(t->cs_on_time + 2000, div);
  581. t->re_on_time = round_to_extif_ticks(t->cs_on_time + 2000, div);
  582. t->access_time = round_to_extif_ticks(t->re_on_time + 4 * systim +
  583. 26000, div);
  584. t->we_off_time = round_to_extif_ticks(t->we_on_time + 1000, div);
  585. t->re_off_time = round_to_extif_ticks(t->re_on_time + 4 * systim +
  586. 26000, div);
  587. t->cs_off_time = round_to_extif_ticks(t->re_off_time + 1000, div);
  588. t->we_cycle_time = round_to_extif_ticks(2 * systim + 2000, div);
  589. if (t->we_cycle_time < t->we_off_time)
  590. t->we_cycle_time = t->we_off_time;
  591. t->re_cycle_time = round_to_extif_ticks(2000 + 4 * systim + 26000, div);
  592. if (t->re_cycle_time < t->re_off_time)
  593. t->re_cycle_time = t->re_off_time;
  594. t->cs_pulse_width = 0;
  595. dev_dbg(hwa742.fbdev->dev, "[lut]cson %d csoff %d reon %d reoff %d\n",
  596. t->cs_on_time, t->cs_off_time, t->re_on_time, t->re_off_time);
  597. dev_dbg(hwa742.fbdev->dev, "[lut]weon %d weoff %d recyc %d wecyc %d\n",
  598. t->we_on_time, t->we_off_time, t->re_cycle_time,
  599. t->we_cycle_time);
  600. dev_dbg(hwa742.fbdev->dev, "[lut]rdaccess %d cspulse %d\n",
  601. t->access_time, t->cs_pulse_width);
  602. return hwa742.extif->convert_timings(t);
  603. }
  604. static int calc_extif_timings(unsigned long sysclk, int *extif_mem_div)
  605. {
  606. int max_clk_div;
  607. int div;
  608. hwa742.extif->get_clk_info(&hwa742.extif_clk_period, &max_clk_div);
  609. for (div = 1; div < max_clk_div; div++) {
  610. if (calc_reg_timing(sysclk, div) == 0)
  611. break;
  612. }
  613. if (div >= max_clk_div)
  614. goto err;
  615. *extif_mem_div = div;
  616. for (div = 1; div < max_clk_div; div++) {
  617. if (calc_lut_timing(sysclk, div) == 0)
  618. break;
  619. }
  620. if (div >= max_clk_div)
  621. goto err;
  622. return 0;
  623. err:
  624. dev_err(hwa742.fbdev->dev, "can't setup timings\n");
  625. return -1;
  626. }
  627. static void calc_hwa742_clk_rates(unsigned long ext_clk,
  628. unsigned long *sys_clk, unsigned long *pix_clk)
  629. {
  630. int pix_clk_src;
  631. int sys_div = 0, sys_mul = 0;
  632. int pix_div;
  633. pix_clk_src = hwa742_read_reg(HWA742_CLK_SRC_REG);
  634. pix_div = ((pix_clk_src >> 3) & 0x1f) + 1;
  635. if ((pix_clk_src & (0x3 << 1)) == 0) {
  636. /* Source is the PLL */
  637. sys_div = (hwa742_read_reg(HWA742_PLL_DIV_REG) & 0x3f) + 1;
  638. sys_mul = (hwa742_read_reg(HWA742_PLL_4_REG) & 0x7f) + 1;
  639. *sys_clk = ext_clk * sys_mul / sys_div;
  640. } else /* else source is ext clk, or oscillator */
  641. *sys_clk = ext_clk;
  642. *pix_clk = *sys_clk / pix_div; /* HZ */
  643. dev_dbg(hwa742.fbdev->dev,
  644. "ext_clk %ld pix_src %d pix_div %d sys_div %d sys_mul %d\n",
  645. ext_clk, pix_clk_src & (0x3 << 1), pix_div, sys_div, sys_mul);
  646. dev_dbg(hwa742.fbdev->dev, "sys_clk %ld pix_clk %ld\n",
  647. *sys_clk, *pix_clk);
  648. }
  649. static int setup_tearsync(unsigned long pix_clk, int extif_div)
  650. {
  651. int hdisp, vdisp;
  652. int hndp, vndp;
  653. int hsw, vsw;
  654. int hs, vs;
  655. int hs_pol_inv, vs_pol_inv;
  656. int use_hsvs, use_ndp;
  657. u8 b;
  658. hsw = hwa742_read_reg(HWA742_HS_W_REG);
  659. vsw = hwa742_read_reg(HWA742_VS_W_REG);
  660. hs_pol_inv = !(hsw & 0x80);
  661. vs_pol_inv = !(vsw & 0x80);
  662. hsw = hsw & 0x7f;
  663. vsw = vsw & 0x3f;
  664. hdisp = (hwa742_read_reg(HWA742_H_DISP_REG) & 0x7f) * 8;
  665. vdisp = hwa742_read_reg(HWA742_V_DISP_1_REG) +
  666. ((hwa742_read_reg(HWA742_V_DISP_2_REG) & 0x3) << 8);
  667. hndp = hwa742_read_reg(HWA742_H_NDP_REG) & 0x7f;
  668. vndp = hwa742_read_reg(HWA742_V_NDP_REG);
  669. /* time to transfer one pixel (16bpp) in ps */
  670. hwa742.pix_tx_time = hwa742.reg_timings.we_cycle_time;
  671. if (hwa742.extif->get_max_tx_rate != NULL) {
  672. /*
  673. * The external interface might have a rate limitation,
  674. * if so, we have to maximize our transfer rate.
  675. */
  676. unsigned long min_tx_time;
  677. unsigned long max_tx_rate = hwa742.extif->get_max_tx_rate();
  678. dev_dbg(hwa742.fbdev->dev, "max_tx_rate %ld HZ\n",
  679. max_tx_rate);
  680. min_tx_time = 1000000000 / (max_tx_rate / 1000); /* ps */
  681. if (hwa742.pix_tx_time < min_tx_time)
  682. hwa742.pix_tx_time = min_tx_time;
  683. }
  684. /* time to update one line in ps */
  685. hwa742.line_upd_time = (hdisp + hndp) * 1000000 / (pix_clk / 1000);
  686. hwa742.line_upd_time *= 1000;
  687. if (hdisp * hwa742.pix_tx_time > hwa742.line_upd_time)
  688. /*
  689. * transfer speed too low, we might have to use both
  690. * HS and VS
  691. */
  692. use_hsvs = 1;
  693. else
  694. /* decent transfer speed, we'll always use only VS */
  695. use_hsvs = 0;
  696. if (use_hsvs && (hs_pol_inv || vs_pol_inv)) {
  697. /*
  698. * HS or'ed with VS doesn't work, use the active high
  699. * TE signal based on HNDP / VNDP
  700. */
  701. use_ndp = 1;
  702. hs_pol_inv = 0;
  703. vs_pol_inv = 0;
  704. hs = hndp;
  705. vs = vndp;
  706. } else {
  707. /*
  708. * Use HS or'ed with VS as a TE signal if both are needed
  709. * or VNDP if only vsync is needed.
  710. */
  711. use_ndp = 0;
  712. hs = hsw;
  713. vs = vsw;
  714. if (!use_hsvs) {
  715. hs_pol_inv = 0;
  716. vs_pol_inv = 0;
  717. }
  718. }
  719. hs = hs * 1000000 / (pix_clk / 1000); /* ps */
  720. hs *= 1000;
  721. vs = vs * (hdisp + hndp) * 1000000 / (pix_clk / 1000); /* ps */
  722. vs *= 1000;
  723. if (vs <= hs)
  724. return -EDOM;
  725. /* set VS to 120% of HS to minimize VS detection time */
  726. vs = hs * 12 / 10;
  727. /* minimize HS too */
  728. hs = 10000;
  729. b = hwa742_read_reg(HWA742_NDP_CTRL);
  730. b &= ~0x3;
  731. b |= use_hsvs ? 1 : 0;
  732. b |= (use_ndp && use_hsvs) ? 0 : 2;
  733. hwa742_write_reg(HWA742_NDP_CTRL, b);
  734. hwa742.vsync_only = !use_hsvs;
  735. dev_dbg(hwa742.fbdev->dev,
  736. "pix_clk %ld HZ pix_tx_time %ld ps line_upd_time %ld ps\n",
  737. pix_clk, hwa742.pix_tx_time, hwa742.line_upd_time);
  738. dev_dbg(hwa742.fbdev->dev,
  739. "hs %d ps vs %d ps mode %d vsync_only %d\n",
  740. hs, vs, (b & 0x3), !use_hsvs);
  741. return hwa742.extif->setup_tearsync(1, hs, vs,
  742. hs_pol_inv, vs_pol_inv, extif_div);
  743. }
  744. static void hwa742_get_caps(int plane, struct omapfb_caps *caps)
  745. {
  746. hwa742.int_ctrl->get_caps(plane, caps);
  747. caps->ctrl |= OMAPFB_CAPS_MANUAL_UPDATE |
  748. OMAPFB_CAPS_WINDOW_PIXEL_DOUBLE;
  749. if (hwa742.te_connected)
  750. caps->ctrl |= OMAPFB_CAPS_TEARSYNC;
  751. caps->wnd_color |= (1 << OMAPFB_COLOR_RGB565) |
  752. (1 << OMAPFB_COLOR_YUV420);
  753. }
  754. static void hwa742_suspend(void)
  755. {
  756. hwa742.update_mode_before_suspend = hwa742.update_mode;
  757. hwa742_set_update_mode(OMAPFB_UPDATE_DISABLED);
  758. /* Enable sleep mode */
  759. hwa742_write_reg(HWA742_POWER_SAVE, 1 << 1);
  760. clk_disable(hwa742.sys_ck);
  761. }
  762. static void hwa742_resume(void)
  763. {
  764. clk_enable(hwa742.sys_ck);
  765. /* Disable sleep mode */
  766. hwa742_write_reg(HWA742_POWER_SAVE, 0);
  767. while (1) {
  768. /* Loop until PLL output is stabilized */
  769. if (hwa742_read_reg(HWA742_PLL_DIV_REG) & (1 << 7))
  770. break;
  771. set_current_state(TASK_UNINTERRUPTIBLE);
  772. schedule_timeout(msecs_to_jiffies(5));
  773. }
  774. hwa742_set_update_mode(hwa742.update_mode_before_suspend);
  775. }
  776. static int hwa742_init(struct omapfb_device *fbdev, int ext_mode,
  777. struct omapfb_mem_desc *req_vram)
  778. {
  779. int r = 0, i;
  780. u8 rev, conf;
  781. unsigned long ext_clk;
  782. unsigned long sys_clk, pix_clk;
  783. int extif_mem_div;
  784. struct omapfb_platform_data *omapfb_conf;
  785. BUG_ON(!fbdev->ext_if || !fbdev->int_ctrl);
  786. hwa742.fbdev = fbdev;
  787. hwa742.extif = fbdev->ext_if;
  788. hwa742.int_ctrl = fbdev->int_ctrl;
  789. omapfb_conf = dev_get_platdata(fbdev->dev);
  790. hwa742.sys_ck = clk_get(NULL, "hwa_sys_ck");
  791. spin_lock_init(&hwa742.req_lock);
  792. if ((r = hwa742.int_ctrl->init(fbdev, 1, req_vram)) < 0)
  793. goto err1;
  794. if ((r = hwa742.extif->init(fbdev)) < 0)
  795. goto err2;
  796. ext_clk = clk_get_rate(hwa742.sys_ck);
  797. if ((r = calc_extif_timings(ext_clk, &extif_mem_div)) < 0)
  798. goto err3;
  799. hwa742.extif->set_timings(&hwa742.reg_timings);
  800. clk_enable(hwa742.sys_ck);
  801. calc_hwa742_clk_rates(ext_clk, &sys_clk, &pix_clk);
  802. if ((r = calc_extif_timings(sys_clk, &extif_mem_div)) < 0)
  803. goto err4;
  804. hwa742.extif->set_timings(&hwa742.reg_timings);
  805. rev = hwa742_read_reg(HWA742_REV_CODE_REG);
  806. if ((rev & 0xfc) != 0x80) {
  807. dev_err(fbdev->dev, "HWA742: invalid revision %02x\n", rev);
  808. r = -ENODEV;
  809. goto err4;
  810. }
  811. if (!(hwa742_read_reg(HWA742_PLL_DIV_REG) & 0x80)) {
  812. dev_err(fbdev->dev,
  813. "HWA742: controller not initialized by the bootloader\n");
  814. r = -ENODEV;
  815. goto err4;
  816. }
  817. if ((r = setup_tearsync(pix_clk, extif_mem_div)) < 0) {
  818. dev_err(hwa742.fbdev->dev,
  819. "HWA742: can't setup tearing synchronization\n");
  820. goto err4;
  821. }
  822. hwa742.te_connected = 1;
  823. hwa742.max_transmit_size = hwa742.extif->max_transmit_size;
  824. hwa742.update_mode = OMAPFB_UPDATE_DISABLED;
  825. hwa742.auto_update_window.x = 0;
  826. hwa742.auto_update_window.y = 0;
  827. hwa742.auto_update_window.width = fbdev->panel->x_res;
  828. hwa742.auto_update_window.height = fbdev->panel->y_res;
  829. hwa742.auto_update_window.format = 0;
  830. init_timer(&hwa742.auto_update_timer);
  831. hwa742.auto_update_timer.function = hwa742_update_window_auto;
  832. hwa742.auto_update_timer.data = 0;
  833. hwa742.prev_color_mode = -1;
  834. hwa742.prev_flags = 0;
  835. hwa742.fbdev = fbdev;
  836. INIT_LIST_HEAD(&hwa742.free_req_list);
  837. INIT_LIST_HEAD(&hwa742.pending_req_list);
  838. for (i = 0; i < ARRAY_SIZE(hwa742.req_pool); i++)
  839. list_add(&hwa742.req_pool[i].entry, &hwa742.free_req_list);
  840. BUG_ON(i <= IRQ_REQ_POOL_SIZE);
  841. sema_init(&hwa742.req_sema, i - IRQ_REQ_POOL_SIZE);
  842. conf = hwa742_read_reg(HWA742_CONFIG_REG);
  843. dev_info(fbdev->dev, ": Epson HWA742 LCD controller rev %d "
  844. "initialized (CNF pins %x)\n", rev & 0x03, conf & 0x07);
  845. return 0;
  846. err4:
  847. clk_disable(hwa742.sys_ck);
  848. err3:
  849. hwa742.extif->cleanup();
  850. err2:
  851. hwa742.int_ctrl->cleanup();
  852. err1:
  853. return r;
  854. }
  855. static void hwa742_cleanup(void)
  856. {
  857. hwa742_set_update_mode(OMAPFB_UPDATE_DISABLED);
  858. hwa742.extif->cleanup();
  859. hwa742.int_ctrl->cleanup();
  860. clk_disable(hwa742.sys_ck);
  861. }
  862. struct lcd_ctrl hwa742_ctrl = {
  863. .name = "hwa742",
  864. .init = hwa742_init,
  865. .cleanup = hwa742_cleanup,
  866. .bind_client = hwa742_bind_client,
  867. .get_caps = hwa742_get_caps,
  868. .set_update_mode = hwa742_set_update_mode,
  869. .get_update_mode = hwa742_get_update_mode,
  870. .setup_plane = hwa742_setup_plane,
  871. .enable_plane = hwa742_enable_plane,
  872. .update_window = hwa742_update_window_async,
  873. .sync = hwa742_sync,
  874. .suspend = hwa742_suspend,
  875. .resume = hwa742_resume,
  876. };