rfbi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * OMAP2 Remote Frame Buffer Interface support
  3. *
  4. * Copyright (C) 2005 Nokia Corporation
  5. * Author: Juha Yrjölä <juha.yrjola@nokia.com>
  6. * Imre Deak <imre.deak@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/i2c.h>
  25. #include <linux/err.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <linux/platform_device.h>
  30. #include "omapfb.h"
  31. #include "dispc.h"
  32. /* To work around an RFBI transfer rate limitation */
  33. #define OMAP_RFBI_RATE_LIMIT 1
  34. #define RFBI_BASE 0x48050800
  35. #define RFBI_REVISION 0x0000
  36. #define RFBI_SYSCONFIG 0x0010
  37. #define RFBI_SYSSTATUS 0x0014
  38. #define RFBI_CONTROL 0x0040
  39. #define RFBI_PIXEL_CNT 0x0044
  40. #define RFBI_LINE_NUMBER 0x0048
  41. #define RFBI_CMD 0x004c
  42. #define RFBI_PARAM 0x0050
  43. #define RFBI_DATA 0x0054
  44. #define RFBI_READ 0x0058
  45. #define RFBI_STATUS 0x005c
  46. #define RFBI_CONFIG0 0x0060
  47. #define RFBI_ONOFF_TIME0 0x0064
  48. #define RFBI_CYCLE_TIME0 0x0068
  49. #define RFBI_DATA_CYCLE1_0 0x006c
  50. #define RFBI_DATA_CYCLE2_0 0x0070
  51. #define RFBI_DATA_CYCLE3_0 0x0074
  52. #define RFBI_VSYNC_WIDTH 0x0090
  53. #define RFBI_HSYNC_WIDTH 0x0094
  54. #define DISPC_BASE 0x48050400
  55. #define DISPC_CONTROL 0x0040
  56. #define DISPC_IRQ_FRAMEMASK 0x0001
  57. static struct {
  58. void __iomem *base;
  59. void (*lcdc_callback)(void *data);
  60. void *lcdc_callback_data;
  61. unsigned long l4_khz;
  62. int bits_per_cycle;
  63. struct omapfb_device *fbdev;
  64. struct clk *dss_ick;
  65. struct clk *dss1_fck;
  66. unsigned tearsync_pin_cnt;
  67. unsigned tearsync_mode;
  68. } rfbi;
  69. static inline void rfbi_write_reg(int idx, u32 val)
  70. {
  71. __raw_writel(val, rfbi.base + idx);
  72. }
  73. static inline u32 rfbi_read_reg(int idx)
  74. {
  75. return __raw_readl(rfbi.base + idx);
  76. }
  77. static int rfbi_get_clocks(void)
  78. {
  79. rfbi.dss_ick = clk_get(&rfbi.fbdev->dssdev->dev, "ick");
  80. if (IS_ERR(rfbi.dss_ick)) {
  81. dev_err(rfbi.fbdev->dev, "can't get ick\n");
  82. return PTR_ERR(rfbi.dss_ick);
  83. }
  84. rfbi.dss1_fck = clk_get(&rfbi.fbdev->dssdev->dev, "fck");
  85. if (IS_ERR(rfbi.dss1_fck)) {
  86. dev_err(rfbi.fbdev->dev, "can't get dss1_fck\n");
  87. clk_put(rfbi.dss_ick);
  88. return PTR_ERR(rfbi.dss1_fck);
  89. }
  90. return 0;
  91. }
  92. static void rfbi_put_clocks(void)
  93. {
  94. clk_put(rfbi.dss1_fck);
  95. clk_put(rfbi.dss_ick);
  96. }
  97. static void rfbi_enable_clocks(int enable)
  98. {
  99. if (enable) {
  100. clk_enable(rfbi.dss_ick);
  101. clk_enable(rfbi.dss1_fck);
  102. } else {
  103. clk_disable(rfbi.dss1_fck);
  104. clk_disable(rfbi.dss_ick);
  105. }
  106. }
  107. #ifdef VERBOSE
  108. static void rfbi_print_timings(void)
  109. {
  110. u32 l;
  111. u32 time;
  112. l = rfbi_read_reg(RFBI_CONFIG0);
  113. time = 1000000000 / rfbi.l4_khz;
  114. if (l & (1 << 4))
  115. time *= 2;
  116. dev_dbg(rfbi.fbdev->dev, "Tick time %u ps\n", time);
  117. l = rfbi_read_reg(RFBI_ONOFF_TIME0);
  118. dev_dbg(rfbi.fbdev->dev,
  119. "CSONTIME %d, CSOFFTIME %d, WEONTIME %d, WEOFFTIME %d, "
  120. "REONTIME %d, REOFFTIME %d\n",
  121. l & 0x0f, (l >> 4) & 0x3f, (l >> 10) & 0x0f, (l >> 14) & 0x3f,
  122. (l >> 20) & 0x0f, (l >> 24) & 0x3f);
  123. l = rfbi_read_reg(RFBI_CYCLE_TIME0);
  124. dev_dbg(rfbi.fbdev->dev,
  125. "WECYCLETIME %d, RECYCLETIME %d, CSPULSEWIDTH %d, "
  126. "ACCESSTIME %d\n",
  127. (l & 0x3f), (l >> 6) & 0x3f, (l >> 12) & 0x3f,
  128. (l >> 22) & 0x3f);
  129. }
  130. #else
  131. static void rfbi_print_timings(void) {}
  132. #endif
  133. static void rfbi_set_timings(const struct extif_timings *t)
  134. {
  135. u32 l;
  136. BUG_ON(!t->converted);
  137. rfbi_enable_clocks(1);
  138. rfbi_write_reg(RFBI_ONOFF_TIME0, t->tim[0]);
  139. rfbi_write_reg(RFBI_CYCLE_TIME0, t->tim[1]);
  140. l = rfbi_read_reg(RFBI_CONFIG0);
  141. l &= ~(1 << 4);
  142. l |= (t->tim[2] ? 1 : 0) << 4;
  143. rfbi_write_reg(RFBI_CONFIG0, l);
  144. rfbi_print_timings();
  145. rfbi_enable_clocks(0);
  146. }
  147. static void rfbi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
  148. {
  149. *clk_period = 1000000000 / rfbi.l4_khz;
  150. *max_clk_div = 2;
  151. }
  152. static int ps_to_rfbi_ticks(int time, int div)
  153. {
  154. unsigned long tick_ps;
  155. int ret;
  156. /* Calculate in picosecs to yield more exact results */
  157. tick_ps = 1000000000 / (rfbi.l4_khz) * div;
  158. ret = (time + tick_ps - 1) / tick_ps;
  159. return ret;
  160. }
  161. #ifdef OMAP_RFBI_RATE_LIMIT
  162. static unsigned long rfbi_get_max_tx_rate(void)
  163. {
  164. unsigned long l4_rate, dss1_rate;
  165. int min_l4_ticks = 0;
  166. int i;
  167. /* According to TI this can't be calculated so make the
  168. * adjustments for a couple of known frequencies and warn for
  169. * others.
  170. */
  171. static const struct {
  172. unsigned long l4_clk; /* HZ */
  173. unsigned long dss1_clk; /* HZ */
  174. unsigned long min_l4_ticks;
  175. } ftab[] = {
  176. { 55, 132, 7, }, /* 7.86 MPix/s */
  177. { 110, 110, 12, }, /* 9.16 MPix/s */
  178. { 110, 132, 10, }, /* 11 Mpix/s */
  179. { 120, 120, 10, }, /* 12 Mpix/s */
  180. { 133, 133, 10, }, /* 13.3 Mpix/s */
  181. };
  182. l4_rate = rfbi.l4_khz / 1000;
  183. dss1_rate = clk_get_rate(rfbi.dss1_fck) / 1000000;
  184. for (i = 0; i < ARRAY_SIZE(ftab); i++) {
  185. /* Use a window instead of an exact match, to account
  186. * for different DPLL multiplier / divider pairs.
  187. */
  188. if (abs(ftab[i].l4_clk - l4_rate) < 3 &&
  189. abs(ftab[i].dss1_clk - dss1_rate) < 3) {
  190. min_l4_ticks = ftab[i].min_l4_ticks;
  191. break;
  192. }
  193. }
  194. if (i == ARRAY_SIZE(ftab)) {
  195. /* Can't be sure, return anyway the maximum not
  196. * rate-limited. This might cause a problem only for the
  197. * tearing synchronisation.
  198. */
  199. dev_err(rfbi.fbdev->dev,
  200. "can't determine maximum RFBI transfer rate\n");
  201. return rfbi.l4_khz * 1000;
  202. }
  203. return rfbi.l4_khz * 1000 / min_l4_ticks;
  204. }
  205. #else
  206. static int rfbi_get_max_tx_rate(void)
  207. {
  208. return rfbi.l4_khz * 1000;
  209. }
  210. #endif
  211. static int rfbi_convert_timings(struct extif_timings *t)
  212. {
  213. u32 l;
  214. int reon, reoff, weon, weoff, cson, csoff, cs_pulse;
  215. int actim, recyc, wecyc;
  216. int div = t->clk_div;
  217. if (div <= 0 || div > 2)
  218. return -1;
  219. /* Make sure that after conversion it still holds that:
  220. * weoff > weon, reoff > reon, recyc >= reoff, wecyc >= weoff,
  221. * csoff > cson, csoff >= max(weoff, reoff), actim > reon
  222. */
  223. weon = ps_to_rfbi_ticks(t->we_on_time, div);
  224. weoff = ps_to_rfbi_ticks(t->we_off_time, div);
  225. if (weoff <= weon)
  226. weoff = weon + 1;
  227. if (weon > 0x0f)
  228. return -1;
  229. if (weoff > 0x3f)
  230. return -1;
  231. reon = ps_to_rfbi_ticks(t->re_on_time, div);
  232. reoff = ps_to_rfbi_ticks(t->re_off_time, div);
  233. if (reoff <= reon)
  234. reoff = reon + 1;
  235. if (reon > 0x0f)
  236. return -1;
  237. if (reoff > 0x3f)
  238. return -1;
  239. cson = ps_to_rfbi_ticks(t->cs_on_time, div);
  240. csoff = ps_to_rfbi_ticks(t->cs_off_time, div);
  241. if (csoff <= cson)
  242. csoff = cson + 1;
  243. if (csoff < max(weoff, reoff))
  244. csoff = max(weoff, reoff);
  245. if (cson > 0x0f)
  246. return -1;
  247. if (csoff > 0x3f)
  248. return -1;
  249. l = cson;
  250. l |= csoff << 4;
  251. l |= weon << 10;
  252. l |= weoff << 14;
  253. l |= reon << 20;
  254. l |= reoff << 24;
  255. t->tim[0] = l;
  256. actim = ps_to_rfbi_ticks(t->access_time, div);
  257. if (actim <= reon)
  258. actim = reon + 1;
  259. if (actim > 0x3f)
  260. return -1;
  261. wecyc = ps_to_rfbi_ticks(t->we_cycle_time, div);
  262. if (wecyc < weoff)
  263. wecyc = weoff;
  264. if (wecyc > 0x3f)
  265. return -1;
  266. recyc = ps_to_rfbi_ticks(t->re_cycle_time, div);
  267. if (recyc < reoff)
  268. recyc = reoff;
  269. if (recyc > 0x3f)
  270. return -1;
  271. cs_pulse = ps_to_rfbi_ticks(t->cs_pulse_width, div);
  272. if (cs_pulse > 0x3f)
  273. return -1;
  274. l = wecyc;
  275. l |= recyc << 6;
  276. l |= cs_pulse << 12;
  277. l |= actim << 22;
  278. t->tim[1] = l;
  279. t->tim[2] = div - 1;
  280. t->converted = 1;
  281. return 0;
  282. }
  283. static int rfbi_setup_tearsync(unsigned pin_cnt,
  284. unsigned hs_pulse_time, unsigned vs_pulse_time,
  285. int hs_pol_inv, int vs_pol_inv, int extif_div)
  286. {
  287. int hs, vs;
  288. int min;
  289. u32 l;
  290. if (pin_cnt != 1 && pin_cnt != 2)
  291. return -EINVAL;
  292. hs = ps_to_rfbi_ticks(hs_pulse_time, 1);
  293. vs = ps_to_rfbi_ticks(vs_pulse_time, 1);
  294. if (hs < 2)
  295. return -EDOM;
  296. if (pin_cnt == 2)
  297. min = 2;
  298. else
  299. min = 4;
  300. if (vs < min)
  301. return -EDOM;
  302. if (vs == hs)
  303. return -EINVAL;
  304. rfbi.tearsync_pin_cnt = pin_cnt;
  305. dev_dbg(rfbi.fbdev->dev,
  306. "setup_tearsync: pins %d hs %d vs %d hs_inv %d vs_inv %d\n",
  307. pin_cnt, hs, vs, hs_pol_inv, vs_pol_inv);
  308. rfbi_enable_clocks(1);
  309. rfbi_write_reg(RFBI_HSYNC_WIDTH, hs);
  310. rfbi_write_reg(RFBI_VSYNC_WIDTH, vs);
  311. l = rfbi_read_reg(RFBI_CONFIG0);
  312. if (hs_pol_inv)
  313. l &= ~(1 << 21);
  314. else
  315. l |= 1 << 21;
  316. if (vs_pol_inv)
  317. l &= ~(1 << 20);
  318. else
  319. l |= 1 << 20;
  320. rfbi_enable_clocks(0);
  321. return 0;
  322. }
  323. static int rfbi_enable_tearsync(int enable, unsigned line)
  324. {
  325. u32 l;
  326. dev_dbg(rfbi.fbdev->dev, "tearsync %d line %d mode %d\n",
  327. enable, line, rfbi.tearsync_mode);
  328. if (line > (1 << 11) - 1)
  329. return -EINVAL;
  330. rfbi_enable_clocks(1);
  331. l = rfbi_read_reg(RFBI_CONFIG0);
  332. l &= ~(0x3 << 2);
  333. if (enable) {
  334. rfbi.tearsync_mode = rfbi.tearsync_pin_cnt;
  335. l |= rfbi.tearsync_mode << 2;
  336. } else
  337. rfbi.tearsync_mode = 0;
  338. rfbi_write_reg(RFBI_CONFIG0, l);
  339. rfbi_write_reg(RFBI_LINE_NUMBER, line);
  340. rfbi_enable_clocks(0);
  341. return 0;
  342. }
  343. static void rfbi_write_command(const void *buf, unsigned int len)
  344. {
  345. rfbi_enable_clocks(1);
  346. if (rfbi.bits_per_cycle == 16) {
  347. const u16 *w = buf;
  348. BUG_ON(len & 1);
  349. for (; len; len -= 2)
  350. rfbi_write_reg(RFBI_CMD, *w++);
  351. } else {
  352. const u8 *b = buf;
  353. BUG_ON(rfbi.bits_per_cycle != 8);
  354. for (; len; len--)
  355. rfbi_write_reg(RFBI_CMD, *b++);
  356. }
  357. rfbi_enable_clocks(0);
  358. }
  359. static void rfbi_read_data(void *buf, unsigned int len)
  360. {
  361. rfbi_enable_clocks(1);
  362. if (rfbi.bits_per_cycle == 16) {
  363. u16 *w = buf;
  364. BUG_ON(len & ~1);
  365. for (; len; len -= 2) {
  366. rfbi_write_reg(RFBI_READ, 0);
  367. *w++ = rfbi_read_reg(RFBI_READ);
  368. }
  369. } else {
  370. u8 *b = buf;
  371. BUG_ON(rfbi.bits_per_cycle != 8);
  372. for (; len; len--) {
  373. rfbi_write_reg(RFBI_READ, 0);
  374. *b++ = rfbi_read_reg(RFBI_READ);
  375. }
  376. }
  377. rfbi_enable_clocks(0);
  378. }
  379. static void rfbi_write_data(const void *buf, unsigned int len)
  380. {
  381. rfbi_enable_clocks(1);
  382. if (rfbi.bits_per_cycle == 16) {
  383. const u16 *w = buf;
  384. BUG_ON(len & 1);
  385. for (; len; len -= 2)
  386. rfbi_write_reg(RFBI_PARAM, *w++);
  387. } else {
  388. const u8 *b = buf;
  389. BUG_ON(rfbi.bits_per_cycle != 8);
  390. for (; len; len--)
  391. rfbi_write_reg(RFBI_PARAM, *b++);
  392. }
  393. rfbi_enable_clocks(0);
  394. }
  395. static void rfbi_transfer_area(int width, int height,
  396. void (callback)(void * data), void *data)
  397. {
  398. u32 w;
  399. BUG_ON(callback == NULL);
  400. rfbi_enable_clocks(1);
  401. omap_dispc_set_lcd_size(width, height);
  402. rfbi.lcdc_callback = callback;
  403. rfbi.lcdc_callback_data = data;
  404. rfbi_write_reg(RFBI_PIXEL_CNT, width * height);
  405. w = rfbi_read_reg(RFBI_CONTROL);
  406. w |= 1; /* enable */
  407. if (!rfbi.tearsync_mode)
  408. w |= 1 << 4; /* internal trigger, reset by HW */
  409. rfbi_write_reg(RFBI_CONTROL, w);
  410. omap_dispc_enable_lcd_out(1);
  411. }
  412. static inline void _stop_transfer(void)
  413. {
  414. u32 w;
  415. w = rfbi_read_reg(RFBI_CONTROL);
  416. rfbi_write_reg(RFBI_CONTROL, w & ~(1 << 0));
  417. rfbi_enable_clocks(0);
  418. }
  419. static void rfbi_dma_callback(void *data)
  420. {
  421. _stop_transfer();
  422. rfbi.lcdc_callback(rfbi.lcdc_callback_data);
  423. }
  424. static void rfbi_set_bits_per_cycle(int bpc)
  425. {
  426. u32 l;
  427. rfbi_enable_clocks(1);
  428. l = rfbi_read_reg(RFBI_CONFIG0);
  429. l &= ~(0x03 << 0);
  430. switch (bpc) {
  431. case 8:
  432. break;
  433. case 16:
  434. l |= 3;
  435. break;
  436. default:
  437. BUG();
  438. }
  439. rfbi_write_reg(RFBI_CONFIG0, l);
  440. rfbi.bits_per_cycle = bpc;
  441. rfbi_enable_clocks(0);
  442. }
  443. static int rfbi_init(struct omapfb_device *fbdev)
  444. {
  445. u32 l;
  446. int r;
  447. rfbi.fbdev = fbdev;
  448. rfbi.base = ioremap(RFBI_BASE, SZ_1K);
  449. if (!rfbi.base) {
  450. dev_err(fbdev->dev, "can't ioremap RFBI\n");
  451. return -ENOMEM;
  452. }
  453. if ((r = rfbi_get_clocks()) < 0)
  454. return r;
  455. rfbi_enable_clocks(1);
  456. rfbi.l4_khz = clk_get_rate(rfbi.dss_ick) / 1000;
  457. /* Reset */
  458. rfbi_write_reg(RFBI_SYSCONFIG, 1 << 1);
  459. while (!(rfbi_read_reg(RFBI_SYSSTATUS) & (1 << 0)));
  460. l = rfbi_read_reg(RFBI_SYSCONFIG);
  461. /* Enable autoidle and smart-idle */
  462. l |= (1 << 0) | (2 << 3);
  463. rfbi_write_reg(RFBI_SYSCONFIG, l);
  464. /* 16-bit interface, ITE trigger mode, 16-bit data */
  465. l = (0x03 << 0) | (0x00 << 2) | (0x01 << 5) | (0x02 << 7);
  466. l |= (0 << 9) | (1 << 20) | (1 << 21);
  467. rfbi_write_reg(RFBI_CONFIG0, l);
  468. rfbi_write_reg(RFBI_DATA_CYCLE1_0, 0x00000010);
  469. l = rfbi_read_reg(RFBI_CONTROL);
  470. /* Select CS0, clear bypass mode */
  471. l = (0x01 << 2);
  472. rfbi_write_reg(RFBI_CONTROL, l);
  473. r = omap_dispc_request_irq(DISPC_IRQ_FRAMEMASK, rfbi_dma_callback,
  474. NULL);
  475. if (r < 0) {
  476. dev_err(fbdev->dev, "can't get DISPC irq\n");
  477. rfbi_enable_clocks(0);
  478. return r;
  479. }
  480. l = rfbi_read_reg(RFBI_REVISION);
  481. pr_info("omapfb: RFBI version %d.%d initialized\n",
  482. (l >> 4) & 0x0f, l & 0x0f);
  483. rfbi_enable_clocks(0);
  484. return 0;
  485. }
  486. static void rfbi_cleanup(void)
  487. {
  488. omap_dispc_free_irq(DISPC_IRQ_FRAMEMASK, rfbi_dma_callback, NULL);
  489. rfbi_put_clocks();
  490. iounmap(rfbi.base);
  491. }
  492. const struct lcd_ctrl_extif omap2_ext_if = {
  493. .init = rfbi_init,
  494. .cleanup = rfbi_cleanup,
  495. .get_clk_info = rfbi_get_clk_info,
  496. .get_max_tx_rate = rfbi_get_max_tx_rate,
  497. .set_bits_per_cycle = rfbi_set_bits_per_cycle,
  498. .convert_timings = rfbi_convert_timings,
  499. .set_timings = rfbi_set_timings,
  500. .write_command = rfbi_write_command,
  501. .read_data = rfbi_read_data,
  502. .write_data = rfbi_write_data,
  503. .transfer_area = rfbi_transfer_area,
  504. .setup_tearsync = rfbi_setup_tearsync,
  505. .enable_tearsync = rfbi_enable_tearsync,
  506. .max_transmit_size = (u32) ~0,
  507. };