hwa742.c 27 KB

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