hwa742.c 27 KB

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