sh_mobile_lcdcfb.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /*
  2. * SuperH Mobile LCDC Framebuffer
  3. *
  4. * Copyright (c) 2008 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/mm.h>
  14. #include <linux/fb.h>
  15. #include <linux/clk.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/vmalloc.h>
  21. #include <video/sh_mobile_lcdc.h>
  22. #include <asm/atomic.h>
  23. #define PALETTE_NR 16
  24. /* shared registers */
  25. #define _LDDCKR 0x410
  26. #define _LDDCKSTPR 0x414
  27. #define _LDINTR 0x468
  28. #define _LDSR 0x46c
  29. #define _LDCNT1R 0x470
  30. #define _LDCNT2R 0x474
  31. #define _LDDDSR 0x47c
  32. #define _LDDWD0R 0x800
  33. #define _LDDRDR 0x840
  34. #define _LDDWAR 0x900
  35. #define _LDDRAR 0x904
  36. /* shared registers and their order for context save/restore */
  37. static int lcdc_shared_regs[] = {
  38. _LDDCKR,
  39. _LDDCKSTPR,
  40. _LDINTR,
  41. _LDDDSR,
  42. _LDCNT1R,
  43. _LDCNT2R,
  44. };
  45. #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
  46. /* per-channel registers */
  47. enum { LDDCKPAT1R, LDDCKPAT2R, LDMT1R, LDMT2R, LDMT3R, LDDFR, LDSM1R,
  48. LDSM2R, LDSA1R, LDMLSR, LDHCNR, LDHSYNR, LDVLNR, LDVSYNR, LDPMR,
  49. NR_CH_REGS };
  50. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  51. [LDDCKPAT1R] = 0x400,
  52. [LDDCKPAT2R] = 0x404,
  53. [LDMT1R] = 0x418,
  54. [LDMT2R] = 0x41c,
  55. [LDMT3R] = 0x420,
  56. [LDDFR] = 0x424,
  57. [LDSM1R] = 0x428,
  58. [LDSM2R] = 0x42c,
  59. [LDSA1R] = 0x430,
  60. [LDMLSR] = 0x438,
  61. [LDHCNR] = 0x448,
  62. [LDHSYNR] = 0x44c,
  63. [LDVLNR] = 0x450,
  64. [LDVSYNR] = 0x454,
  65. [LDPMR] = 0x460,
  66. };
  67. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  68. [LDDCKPAT1R] = 0x408,
  69. [LDDCKPAT2R] = 0x40c,
  70. [LDMT1R] = 0x600,
  71. [LDMT2R] = 0x604,
  72. [LDMT3R] = 0x608,
  73. [LDDFR] = 0x60c,
  74. [LDSM1R] = 0x610,
  75. [LDSM2R] = 0x614,
  76. [LDSA1R] = 0x618,
  77. [LDMLSR] = 0x620,
  78. [LDHCNR] = 0x624,
  79. [LDHSYNR] = 0x628,
  80. [LDVLNR] = 0x62c,
  81. [LDVSYNR] = 0x630,
  82. [LDPMR] = 0x63c,
  83. };
  84. #define START_LCDC 0x00000001
  85. #define LCDC_RESET 0x00000100
  86. #define DISPLAY_BEU 0x00000008
  87. #define LCDC_ENABLE 0x00000001
  88. #define LDINTR_FE 0x00000400
  89. #define LDINTR_FS 0x00000004
  90. struct sh_mobile_lcdc_priv;
  91. struct sh_mobile_lcdc_chan {
  92. struct sh_mobile_lcdc_priv *lcdc;
  93. unsigned long *reg_offs;
  94. unsigned long ldmt1r_value;
  95. unsigned long enabled; /* ME and SE in LDCNT2R */
  96. struct sh_mobile_lcdc_chan_cfg cfg;
  97. u32 pseudo_palette[PALETTE_NR];
  98. unsigned long saved_ch_regs[NR_CH_REGS];
  99. struct fb_info *info;
  100. dma_addr_t dma_handle;
  101. struct fb_deferred_io defio;
  102. struct scatterlist *sglist;
  103. unsigned long frame_end;
  104. wait_queue_head_t frame_end_wait;
  105. };
  106. struct sh_mobile_lcdc_priv {
  107. void __iomem *base;
  108. int irq;
  109. atomic_t hw_usecnt;
  110. struct device *dev;
  111. struct clk *dot_clk;
  112. unsigned long lddckr;
  113. struct sh_mobile_lcdc_chan ch[2];
  114. unsigned long saved_shared_regs[NR_SHARED_REGS];
  115. int started;
  116. };
  117. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  118. int reg_nr, unsigned long data)
  119. {
  120. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  121. }
  122. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  123. int reg_nr)
  124. {
  125. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  126. }
  127. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  128. unsigned long reg_offs, unsigned long data)
  129. {
  130. iowrite32(data, priv->base + reg_offs);
  131. }
  132. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  133. unsigned long reg_offs)
  134. {
  135. return ioread32(priv->base + reg_offs);
  136. }
  137. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  138. unsigned long reg_offs,
  139. unsigned long mask, unsigned long until)
  140. {
  141. while ((lcdc_read(priv, reg_offs) & mask) != until)
  142. cpu_relax();
  143. }
  144. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  145. {
  146. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  147. }
  148. static void lcdc_sys_write_index(void *handle, unsigned long data)
  149. {
  150. struct sh_mobile_lcdc_chan *ch = handle;
  151. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
  152. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  153. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  154. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  155. }
  156. static void lcdc_sys_write_data(void *handle, unsigned long data)
  157. {
  158. struct sh_mobile_lcdc_chan *ch = handle;
  159. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
  160. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  161. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  162. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  163. }
  164. static unsigned long lcdc_sys_read_data(void *handle)
  165. {
  166. struct sh_mobile_lcdc_chan *ch = handle;
  167. lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
  168. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  169. lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  170. udelay(1);
  171. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  172. return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
  173. }
  174. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  175. lcdc_sys_write_index,
  176. lcdc_sys_write_data,
  177. lcdc_sys_read_data,
  178. };
  179. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  180. {
  181. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  182. pm_runtime_get_sync(priv->dev);
  183. if (priv->dot_clk)
  184. clk_enable(priv->dot_clk);
  185. }
  186. }
  187. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  188. {
  189. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  190. if (priv->dot_clk)
  191. clk_disable(priv->dot_clk);
  192. pm_runtime_put(priv->dev);
  193. }
  194. }
  195. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  196. struct list_head *pagelist)
  197. {
  198. struct sh_mobile_lcdc_chan *ch = info->par;
  199. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  200. struct page *page;
  201. int nr_pages = 0;
  202. sg_init_table(ch->sglist, nr_pages_max);
  203. list_for_each_entry(page, pagelist, lru)
  204. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  205. return nr_pages;
  206. }
  207. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  208. struct list_head *pagelist)
  209. {
  210. struct sh_mobile_lcdc_chan *ch = info->par;
  211. unsigned int nr_pages;
  212. /* enable clocks before accessing hardware */
  213. sh_mobile_lcdc_clk_on(ch->lcdc);
  214. nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  215. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  216. /* trigger panel update */
  217. lcdc_write_chan(ch, LDSM2R, 1);
  218. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  219. }
  220. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  221. {
  222. struct fb_deferred_io *fbdefio = info->fbdefio;
  223. if (fbdefio)
  224. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  225. }
  226. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  227. {
  228. struct sh_mobile_lcdc_priv *priv = data;
  229. struct sh_mobile_lcdc_chan *ch;
  230. unsigned long tmp;
  231. int is_sub;
  232. int k;
  233. /* acknowledge interrupt */
  234. tmp = lcdc_read(priv, _LDINTR);
  235. tmp &= 0xffffff00; /* mask in high 24 bits */
  236. tmp |= 0x000000ff ^ LDINTR_FS; /* status in low 8 */
  237. lcdc_write(priv, _LDINTR, tmp);
  238. /* figure out if this interrupt is for main or sub lcd */
  239. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  240. /* wake up channel and disable clocks*/
  241. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  242. ch = &priv->ch[k];
  243. if (!ch->enabled)
  244. continue;
  245. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  246. ch->frame_end = 1;
  247. wake_up(&ch->frame_end_wait);
  248. sh_mobile_lcdc_clk_off(priv);
  249. }
  250. }
  251. return IRQ_HANDLED;
  252. }
  253. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  254. int start)
  255. {
  256. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  257. int k;
  258. /* start or stop the lcdc */
  259. if (start)
  260. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  261. else
  262. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  263. /* wait until power is applied/stopped on all channels */
  264. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  265. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  266. while (1) {
  267. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  268. if (start && tmp == 3)
  269. break;
  270. if (!start && tmp == 0)
  271. break;
  272. cpu_relax();
  273. }
  274. if (!start)
  275. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  276. }
  277. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  278. {
  279. struct sh_mobile_lcdc_chan *ch;
  280. struct fb_videomode *lcd_cfg;
  281. struct sh_mobile_lcdc_board_cfg *board_cfg;
  282. unsigned long tmp;
  283. int k, m;
  284. int ret = 0;
  285. /* enable clocks before accessing the hardware */
  286. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  287. if (priv->ch[k].enabled)
  288. sh_mobile_lcdc_clk_on(priv);
  289. /* reset */
  290. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  291. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  292. /* enable LCDC channels */
  293. tmp = lcdc_read(priv, _LDCNT2R);
  294. tmp |= priv->ch[0].enabled;
  295. tmp |= priv->ch[1].enabled;
  296. lcdc_write(priv, _LDCNT2R, tmp);
  297. /* read data from external memory, avoid using the BEU for now */
  298. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  299. /* stop the lcdc first */
  300. sh_mobile_lcdc_start_stop(priv, 0);
  301. /* configure clocks */
  302. tmp = priv->lddckr;
  303. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  304. ch = &priv->ch[k];
  305. if (!priv->ch[k].enabled)
  306. continue;
  307. m = ch->cfg.clock_divider;
  308. if (!m)
  309. continue;
  310. if (m == 1)
  311. m = 1 << 6;
  312. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  313. lcdc_write_chan(ch, LDDCKPAT1R, 0x00000000);
  314. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  315. }
  316. lcdc_write(priv, _LDDCKR, tmp);
  317. /* start dotclock again */
  318. lcdc_write(priv, _LDDCKSTPR, 0);
  319. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  320. /* interrupts are disabled to begin with */
  321. lcdc_write(priv, _LDINTR, 0);
  322. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  323. ch = &priv->ch[k];
  324. lcd_cfg = &ch->cfg.lcd_cfg;
  325. if (!ch->enabled)
  326. continue;
  327. tmp = ch->ldmt1r_value;
  328. tmp |= (lcd_cfg->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  329. tmp |= (lcd_cfg->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  330. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  331. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  332. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  333. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  334. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  335. lcdc_write_chan(ch, LDMT1R, tmp);
  336. /* setup SYS bus */
  337. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  338. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  339. /* horizontal configuration */
  340. tmp = lcd_cfg->xres + lcd_cfg->hsync_len;
  341. tmp += lcd_cfg->left_margin;
  342. tmp += lcd_cfg->right_margin;
  343. tmp /= 8; /* HTCN */
  344. tmp |= (lcd_cfg->xres / 8) << 16; /* HDCN */
  345. lcdc_write_chan(ch, LDHCNR, tmp);
  346. tmp = lcd_cfg->xres;
  347. tmp += lcd_cfg->right_margin;
  348. tmp /= 8; /* HSYNP */
  349. tmp |= (lcd_cfg->hsync_len / 8) << 16; /* HSYNW */
  350. lcdc_write_chan(ch, LDHSYNR, tmp);
  351. /* power supply */
  352. lcdc_write_chan(ch, LDPMR, 0);
  353. /* vertical configuration */
  354. tmp = lcd_cfg->yres + lcd_cfg->vsync_len;
  355. tmp += lcd_cfg->upper_margin;
  356. tmp += lcd_cfg->lower_margin; /* VTLN */
  357. tmp |= lcd_cfg->yres << 16; /* VDLN */
  358. lcdc_write_chan(ch, LDVLNR, tmp);
  359. tmp = lcd_cfg->yres;
  360. tmp += lcd_cfg->lower_margin; /* VSYNP */
  361. tmp |= lcd_cfg->vsync_len << 16; /* VSYNW */
  362. lcdc_write_chan(ch, LDVSYNR, tmp);
  363. board_cfg = &ch->cfg.board_cfg;
  364. if (board_cfg->setup_sys)
  365. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  366. &sh_mobile_lcdc_sys_bus_ops);
  367. if (ret)
  368. return ret;
  369. }
  370. /* word and long word swap */
  371. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  372. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  373. ch = &priv->ch[k];
  374. if (!priv->ch[k].enabled)
  375. continue;
  376. /* set bpp format in PKF[4:0] */
  377. tmp = lcdc_read_chan(ch, LDDFR);
  378. tmp &= ~(0x0001001f);
  379. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  380. lcdc_write_chan(ch, LDDFR, tmp);
  381. /* point out our frame buffer */
  382. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  383. /* set line size */
  384. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  385. /* setup deferred io if SYS bus */
  386. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  387. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  388. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  389. ch->defio.delay = msecs_to_jiffies(tmp);
  390. ch->info->fbdefio = &ch->defio;
  391. fb_deferred_io_init(ch->info);
  392. /* one-shot mode */
  393. lcdc_write_chan(ch, LDSM1R, 1);
  394. /* enable "Frame End Interrupt Enable" bit */
  395. lcdc_write(priv, _LDINTR, LDINTR_FE);
  396. } else {
  397. /* continuous read mode */
  398. lcdc_write_chan(ch, LDSM1R, 0);
  399. }
  400. }
  401. /* display output */
  402. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  403. /* start the lcdc */
  404. sh_mobile_lcdc_start_stop(priv, 1);
  405. priv->started = 1;
  406. /* tell the board code to enable the panel */
  407. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  408. ch = &priv->ch[k];
  409. if (!ch->enabled)
  410. continue;
  411. board_cfg = &ch->cfg.board_cfg;
  412. if (board_cfg->display_on)
  413. board_cfg->display_on(board_cfg->board_data);
  414. }
  415. return 0;
  416. }
  417. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  418. {
  419. struct sh_mobile_lcdc_chan *ch;
  420. struct sh_mobile_lcdc_board_cfg *board_cfg;
  421. int k;
  422. /* clean up deferred io and ask board code to disable panel */
  423. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  424. ch = &priv->ch[k];
  425. if (!ch->enabled)
  426. continue;
  427. /* deferred io mode:
  428. * flush frame, and wait for frame end interrupt
  429. * clean up deferred io and enable clock
  430. */
  431. if (ch->info->fbdefio) {
  432. ch->frame_end = 0;
  433. schedule_delayed_work(&ch->info->deferred_work, 0);
  434. wait_event(ch->frame_end_wait, ch->frame_end);
  435. fb_deferred_io_cleanup(ch->info);
  436. ch->info->fbdefio = NULL;
  437. sh_mobile_lcdc_clk_on(priv);
  438. }
  439. board_cfg = &ch->cfg.board_cfg;
  440. if (board_cfg->display_off)
  441. board_cfg->display_off(board_cfg->board_data);
  442. }
  443. /* stop the lcdc */
  444. if (priv->started) {
  445. sh_mobile_lcdc_start_stop(priv, 0);
  446. priv->started = 0;
  447. }
  448. /* stop clocks */
  449. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  450. if (priv->ch[k].enabled)
  451. sh_mobile_lcdc_clk_off(priv);
  452. }
  453. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  454. {
  455. int ifm, miftyp;
  456. switch (ch->cfg.interface_type) {
  457. case RGB8: ifm = 0; miftyp = 0; break;
  458. case RGB9: ifm = 0; miftyp = 4; break;
  459. case RGB12A: ifm = 0; miftyp = 5; break;
  460. case RGB12B: ifm = 0; miftyp = 6; break;
  461. case RGB16: ifm = 0; miftyp = 7; break;
  462. case RGB18: ifm = 0; miftyp = 10; break;
  463. case RGB24: ifm = 0; miftyp = 11; break;
  464. case SYS8A: ifm = 1; miftyp = 0; break;
  465. case SYS8B: ifm = 1; miftyp = 1; break;
  466. case SYS8C: ifm = 1; miftyp = 2; break;
  467. case SYS8D: ifm = 1; miftyp = 3; break;
  468. case SYS9: ifm = 1; miftyp = 4; break;
  469. case SYS12: ifm = 1; miftyp = 5; break;
  470. case SYS16A: ifm = 1; miftyp = 7; break;
  471. case SYS16B: ifm = 1; miftyp = 8; break;
  472. case SYS16C: ifm = 1; miftyp = 9; break;
  473. case SYS18: ifm = 1; miftyp = 10; break;
  474. case SYS24: ifm = 1; miftyp = 11; break;
  475. default: goto bad;
  476. }
  477. /* SUBLCD only supports SYS interface */
  478. if (lcdc_chan_is_sublcd(ch)) {
  479. if (ifm == 0)
  480. goto bad;
  481. else
  482. ifm = 0;
  483. }
  484. ch->ldmt1r_value = (ifm << 12) | miftyp;
  485. return 0;
  486. bad:
  487. return -EINVAL;
  488. }
  489. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  490. int clock_source,
  491. struct sh_mobile_lcdc_priv *priv)
  492. {
  493. char *str;
  494. int icksel;
  495. switch (clock_source) {
  496. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  497. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  498. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  499. default:
  500. return -EINVAL;
  501. }
  502. priv->lddckr = icksel << 16;
  503. if (str) {
  504. priv->dot_clk = clk_get(&pdev->dev, str);
  505. if (IS_ERR(priv->dot_clk)) {
  506. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  507. return PTR_ERR(priv->dot_clk);
  508. }
  509. }
  510. atomic_set(&priv->hw_usecnt, -1);
  511. /* Runtime PM support involves two step for this driver:
  512. * 1) Enable Runtime PM
  513. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  514. */
  515. pm_runtime_enable(priv->dev);
  516. pm_runtime_resume(priv->dev);
  517. return 0;
  518. }
  519. static int sh_mobile_lcdc_setcolreg(u_int regno,
  520. u_int red, u_int green, u_int blue,
  521. u_int transp, struct fb_info *info)
  522. {
  523. u32 *palette = info->pseudo_palette;
  524. if (regno >= PALETTE_NR)
  525. return -EINVAL;
  526. /* only FB_VISUAL_TRUECOLOR supported */
  527. red >>= 16 - info->var.red.length;
  528. green >>= 16 - info->var.green.length;
  529. blue >>= 16 - info->var.blue.length;
  530. transp >>= 16 - info->var.transp.length;
  531. palette[regno] = (red << info->var.red.offset) |
  532. (green << info->var.green.offset) |
  533. (blue << info->var.blue.offset) |
  534. (transp << info->var.transp.offset);
  535. return 0;
  536. }
  537. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  538. .id = "SH Mobile LCDC",
  539. .type = FB_TYPE_PACKED_PIXELS,
  540. .visual = FB_VISUAL_TRUECOLOR,
  541. .accel = FB_ACCEL_NONE,
  542. };
  543. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  544. const struct fb_fillrect *rect)
  545. {
  546. sys_fillrect(info, rect);
  547. sh_mobile_lcdc_deferred_io_touch(info);
  548. }
  549. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  550. const struct fb_copyarea *area)
  551. {
  552. sys_copyarea(info, area);
  553. sh_mobile_lcdc_deferred_io_touch(info);
  554. }
  555. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  556. const struct fb_image *image)
  557. {
  558. sys_imageblit(info, image);
  559. sh_mobile_lcdc_deferred_io_touch(info);
  560. }
  561. static struct fb_ops sh_mobile_lcdc_ops = {
  562. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  563. .fb_read = fb_sys_read,
  564. .fb_write = fb_sys_write,
  565. .fb_fillrect = sh_mobile_lcdc_fillrect,
  566. .fb_copyarea = sh_mobile_lcdc_copyarea,
  567. .fb_imageblit = sh_mobile_lcdc_imageblit,
  568. };
  569. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  570. {
  571. switch (bpp) {
  572. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  573. var->red.offset = 11;
  574. var->red.length = 5;
  575. var->green.offset = 5;
  576. var->green.length = 6;
  577. var->blue.offset = 0;
  578. var->blue.length = 5;
  579. var->transp.offset = 0;
  580. var->transp.length = 0;
  581. break;
  582. case 32: /* PKF[4:0] = 00000 - RGB 888
  583. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  584. * this may be because LDDDSR has word swap enabled..
  585. */
  586. var->red.offset = 0;
  587. var->red.length = 8;
  588. var->green.offset = 24;
  589. var->green.length = 8;
  590. var->blue.offset = 16;
  591. var->blue.length = 8;
  592. var->transp.offset = 0;
  593. var->transp.length = 0;
  594. break;
  595. default:
  596. return -EINVAL;
  597. }
  598. var->bits_per_pixel = bpp;
  599. var->red.msb_right = 0;
  600. var->green.msb_right = 0;
  601. var->blue.msb_right = 0;
  602. var->transp.msb_right = 0;
  603. return 0;
  604. }
  605. static int sh_mobile_lcdc_suspend(struct device *dev)
  606. {
  607. struct platform_device *pdev = to_platform_device(dev);
  608. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  609. return 0;
  610. }
  611. static int sh_mobile_lcdc_resume(struct device *dev)
  612. {
  613. struct platform_device *pdev = to_platform_device(dev);
  614. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  615. }
  616. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  617. {
  618. struct platform_device *pdev = to_platform_device(dev);
  619. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  620. struct sh_mobile_lcdc_chan *ch;
  621. int k, n;
  622. /* save per-channel registers */
  623. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  624. ch = &p->ch[k];
  625. if (!ch->enabled)
  626. continue;
  627. for (n = 0; n < NR_CH_REGS; n++)
  628. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  629. }
  630. /* save shared registers */
  631. for (n = 0; n < NR_SHARED_REGS; n++)
  632. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  633. /* turn off LCDC hardware */
  634. lcdc_write(p, _LDCNT1R, 0);
  635. return 0;
  636. }
  637. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  638. {
  639. struct platform_device *pdev = to_platform_device(dev);
  640. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  641. struct sh_mobile_lcdc_chan *ch;
  642. int k, n;
  643. /* restore per-channel registers */
  644. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  645. ch = &p->ch[k];
  646. if (!ch->enabled)
  647. continue;
  648. for (n = 0; n < NR_CH_REGS; n++)
  649. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  650. }
  651. /* restore shared registers */
  652. for (n = 0; n < NR_SHARED_REGS; n++)
  653. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  654. return 0;
  655. }
  656. static struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  657. .suspend = sh_mobile_lcdc_suspend,
  658. .resume = sh_mobile_lcdc_resume,
  659. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  660. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  661. };
  662. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  663. static int __init sh_mobile_lcdc_probe(struct platform_device *pdev)
  664. {
  665. struct fb_info *info;
  666. struct sh_mobile_lcdc_priv *priv;
  667. struct sh_mobile_lcdc_info *pdata;
  668. struct sh_mobile_lcdc_chan_cfg *cfg;
  669. struct resource *res;
  670. int error;
  671. void *buf;
  672. int i, j;
  673. if (!pdev->dev.platform_data) {
  674. dev_err(&pdev->dev, "no platform data defined\n");
  675. error = -EINVAL;
  676. goto err0;
  677. }
  678. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  679. i = platform_get_irq(pdev, 0);
  680. if (!res || i < 0) {
  681. dev_err(&pdev->dev, "cannot get platform resources\n");
  682. error = -ENOENT;
  683. goto err0;
  684. }
  685. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  686. if (!priv) {
  687. dev_err(&pdev->dev, "cannot allocate device data\n");
  688. error = -ENOMEM;
  689. goto err0;
  690. }
  691. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  692. dev_name(&pdev->dev), priv);
  693. if (error) {
  694. dev_err(&pdev->dev, "unable to request irq\n");
  695. goto err1;
  696. }
  697. priv->irq = i;
  698. priv->dev = &pdev->dev;
  699. platform_set_drvdata(pdev, priv);
  700. pdata = pdev->dev.platform_data;
  701. j = 0;
  702. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  703. priv->ch[j].lcdc = priv;
  704. memcpy(&priv->ch[j].cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  705. error = sh_mobile_lcdc_check_interface(&priv->ch[i]);
  706. if (error) {
  707. dev_err(&pdev->dev, "unsupported interface type\n");
  708. goto err1;
  709. }
  710. init_waitqueue_head(&priv->ch[i].frame_end_wait);
  711. switch (pdata->ch[i].chan) {
  712. case LCDC_CHAN_MAINLCD:
  713. priv->ch[j].enabled = 1 << 1;
  714. priv->ch[j].reg_offs = lcdc_offs_mainlcd;
  715. j++;
  716. break;
  717. case LCDC_CHAN_SUBLCD:
  718. priv->ch[j].enabled = 1 << 2;
  719. priv->ch[j].reg_offs = lcdc_offs_sublcd;
  720. j++;
  721. break;
  722. }
  723. }
  724. if (!j) {
  725. dev_err(&pdev->dev, "no channels defined\n");
  726. error = -EINVAL;
  727. goto err1;
  728. }
  729. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  730. if (error) {
  731. dev_err(&pdev->dev, "unable to setup clocks\n");
  732. goto err1;
  733. }
  734. priv->base = ioremap_nocache(res->start, (res->end - res->start) + 1);
  735. for (i = 0; i < j; i++) {
  736. cfg = &priv->ch[i].cfg;
  737. priv->ch[i].info = framebuffer_alloc(0, &pdev->dev);
  738. if (!priv->ch[i].info) {
  739. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  740. error = -ENOMEM;
  741. break;
  742. }
  743. info = priv->ch[i].info;
  744. info->fbops = &sh_mobile_lcdc_ops;
  745. info->var.xres = info->var.xres_virtual = cfg->lcd_cfg.xres;
  746. info->var.yres = info->var.yres_virtual = cfg->lcd_cfg.yres;
  747. info->var.width = cfg->lcd_size_cfg.width;
  748. info->var.height = cfg->lcd_size_cfg.height;
  749. info->var.activate = FB_ACTIVATE_NOW;
  750. error = sh_mobile_lcdc_set_bpp(&info->var, cfg->bpp);
  751. if (error)
  752. break;
  753. info->fix = sh_mobile_lcdc_fix;
  754. info->fix.line_length = cfg->lcd_cfg.xres * (cfg->bpp / 8);
  755. info->fix.smem_len = info->fix.line_length * cfg->lcd_cfg.yres;
  756. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  757. &priv->ch[i].dma_handle, GFP_KERNEL);
  758. if (!buf) {
  759. dev_err(&pdev->dev, "unable to allocate buffer\n");
  760. error = -ENOMEM;
  761. break;
  762. }
  763. info->pseudo_palette = &priv->ch[i].pseudo_palette;
  764. info->flags = FBINFO_FLAG_DEFAULT;
  765. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  766. if (error < 0) {
  767. dev_err(&pdev->dev, "unable to allocate cmap\n");
  768. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  769. buf, priv->ch[i].dma_handle);
  770. break;
  771. }
  772. memset(buf, 0, info->fix.smem_len);
  773. info->fix.smem_start = priv->ch[i].dma_handle;
  774. info->screen_base = buf;
  775. info->device = &pdev->dev;
  776. info->par = &priv->ch[i];
  777. }
  778. if (error)
  779. goto err1;
  780. error = sh_mobile_lcdc_start(priv);
  781. if (error) {
  782. dev_err(&pdev->dev, "unable to start hardware\n");
  783. goto err1;
  784. }
  785. for (i = 0; i < j; i++) {
  786. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  787. info = ch->info;
  788. if (info->fbdefio) {
  789. priv->ch->sglist = vmalloc(sizeof(struct scatterlist) *
  790. info->fix.smem_len >> PAGE_SHIFT);
  791. if (!priv->ch->sglist) {
  792. dev_err(&pdev->dev, "cannot allocate sglist\n");
  793. goto err1;
  794. }
  795. }
  796. error = register_framebuffer(info);
  797. if (error < 0)
  798. goto err1;
  799. dev_info(info->dev,
  800. "registered %s/%s as %dx%d %dbpp.\n",
  801. pdev->name,
  802. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  803. "mainlcd" : "sublcd",
  804. (int) ch->cfg.lcd_cfg.xres,
  805. (int) ch->cfg.lcd_cfg.yres,
  806. ch->cfg.bpp);
  807. /* deferred io mode: disable clock to save power */
  808. if (info->fbdefio)
  809. sh_mobile_lcdc_clk_off(priv);
  810. }
  811. return 0;
  812. err1:
  813. sh_mobile_lcdc_remove(pdev);
  814. err0:
  815. return error;
  816. }
  817. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  818. {
  819. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  820. struct fb_info *info;
  821. int i;
  822. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  823. if (priv->ch[i].info->dev)
  824. unregister_framebuffer(priv->ch[i].info);
  825. sh_mobile_lcdc_stop(priv);
  826. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  827. info = priv->ch[i].info;
  828. if (!info || !info->device)
  829. continue;
  830. if (priv->ch[i].sglist)
  831. vfree(priv->ch[i].sglist);
  832. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  833. info->screen_base, priv->ch[i].dma_handle);
  834. fb_dealloc_cmap(&info->cmap);
  835. framebuffer_release(info);
  836. }
  837. if (priv->dot_clk)
  838. clk_put(priv->dot_clk);
  839. pm_runtime_disable(priv->dev);
  840. if (priv->base)
  841. iounmap(priv->base);
  842. if (priv->irq)
  843. free_irq(priv->irq, priv);
  844. kfree(priv);
  845. return 0;
  846. }
  847. static struct platform_driver sh_mobile_lcdc_driver = {
  848. .driver = {
  849. .name = "sh_mobile_lcdc_fb",
  850. .owner = THIS_MODULE,
  851. .pm = &sh_mobile_lcdc_dev_pm_ops,
  852. },
  853. .probe = sh_mobile_lcdc_probe,
  854. .remove = sh_mobile_lcdc_remove,
  855. };
  856. static int __init sh_mobile_lcdc_init(void)
  857. {
  858. return platform_driver_register(&sh_mobile_lcdc_driver);
  859. }
  860. static void __exit sh_mobile_lcdc_exit(void)
  861. {
  862. platform_driver_unregister(&sh_mobile_lcdc_driver);
  863. }
  864. module_init(sh_mobile_lcdc_init);
  865. module_exit(sh_mobile_lcdc_exit);
  866. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  867. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  868. MODULE_LICENSE("GPL v2");