sh_mobile_lcdcfb.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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/clk.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/slab.h>
  22. #include <linux/console.h>
  23. #include <video/sh_mobile_lcdc.h>
  24. #include <asm/atomic.h>
  25. #include "sh_mobile_lcdcfb.h"
  26. #define SIDE_B_OFFSET 0x1000
  27. #define MIRROR_OFFSET 0x2000
  28. /* shared registers */
  29. #define _LDDCKR 0x410
  30. #define _LDDCKSTPR 0x414
  31. #define _LDINTR 0x468
  32. #define _LDSR 0x46c
  33. #define _LDCNT1R 0x470
  34. #define _LDCNT2R 0x474
  35. #define _LDRCNTR 0x478
  36. #define _LDDDSR 0x47c
  37. #define _LDDWD0R 0x800
  38. #define _LDDRDR 0x840
  39. #define _LDDWAR 0x900
  40. #define _LDDRAR 0x904
  41. /* shared registers and their order for context save/restore */
  42. static int lcdc_shared_regs[] = {
  43. _LDDCKR,
  44. _LDDCKSTPR,
  45. _LDINTR,
  46. _LDDDSR,
  47. _LDCNT1R,
  48. _LDCNT2R,
  49. };
  50. #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
  51. #define DEFAULT_XRES 1280
  52. #define DEFAULT_YRES 1024
  53. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  54. [LDDCKPAT1R] = 0x400,
  55. [LDDCKPAT2R] = 0x404,
  56. [LDMT1R] = 0x418,
  57. [LDMT2R] = 0x41c,
  58. [LDMT3R] = 0x420,
  59. [LDDFR] = 0x424,
  60. [LDSM1R] = 0x428,
  61. [LDSM2R] = 0x42c,
  62. [LDSA1R] = 0x430,
  63. [LDMLSR] = 0x438,
  64. [LDHCNR] = 0x448,
  65. [LDHSYNR] = 0x44c,
  66. [LDVLNR] = 0x450,
  67. [LDVSYNR] = 0x454,
  68. [LDPMR] = 0x460,
  69. [LDHAJR] = 0x4a0,
  70. };
  71. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  72. [LDDCKPAT1R] = 0x408,
  73. [LDDCKPAT2R] = 0x40c,
  74. [LDMT1R] = 0x600,
  75. [LDMT2R] = 0x604,
  76. [LDMT3R] = 0x608,
  77. [LDDFR] = 0x60c,
  78. [LDSM1R] = 0x610,
  79. [LDSM2R] = 0x614,
  80. [LDSA1R] = 0x618,
  81. [LDMLSR] = 0x620,
  82. [LDHCNR] = 0x624,
  83. [LDHSYNR] = 0x628,
  84. [LDVLNR] = 0x62c,
  85. [LDVSYNR] = 0x630,
  86. [LDPMR] = 0x63c,
  87. };
  88. #define START_LCDC 0x00000001
  89. #define LCDC_RESET 0x00000100
  90. #define DISPLAY_BEU 0x00000008
  91. #define LCDC_ENABLE 0x00000001
  92. #define LDINTR_FE 0x00000400
  93. #define LDINTR_VSE 0x00000200
  94. #define LDINTR_VEE 0x00000100
  95. #define LDINTR_FS 0x00000004
  96. #define LDINTR_VSS 0x00000002
  97. #define LDINTR_VES 0x00000001
  98. #define LDRCNTR_SRS 0x00020000
  99. #define LDRCNTR_SRC 0x00010000
  100. #define LDRCNTR_MRS 0x00000002
  101. #define LDRCNTR_MRC 0x00000001
  102. #define LDSR_MRS 0x00000100
  103. static const struct fb_videomode default_720p = {
  104. .name = "HDMI 720p",
  105. .xres = 1280,
  106. .yres = 720,
  107. .left_margin = 200,
  108. .right_margin = 88,
  109. .hsync_len = 48,
  110. .upper_margin = 20,
  111. .lower_margin = 5,
  112. .vsync_len = 5,
  113. .pixclock = 13468,
  114. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  115. };
  116. struct sh_mobile_lcdc_priv {
  117. void __iomem *base;
  118. int irq;
  119. atomic_t hw_usecnt;
  120. struct device *dev;
  121. struct clk *dot_clk;
  122. unsigned long lddckr;
  123. struct sh_mobile_lcdc_chan ch[2];
  124. struct notifier_block notifier;
  125. unsigned long saved_shared_regs[NR_SHARED_REGS];
  126. int started;
  127. };
  128. static bool banked(int reg_nr)
  129. {
  130. switch (reg_nr) {
  131. case LDMT1R:
  132. case LDMT2R:
  133. case LDMT3R:
  134. case LDDFR:
  135. case LDSM1R:
  136. case LDSA1R:
  137. case LDMLSR:
  138. case LDHCNR:
  139. case LDHSYNR:
  140. case LDVLNR:
  141. case LDVSYNR:
  142. return true;
  143. }
  144. return false;
  145. }
  146. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  147. int reg_nr, unsigned long data)
  148. {
  149. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  150. if (banked(reg_nr))
  151. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  152. SIDE_B_OFFSET);
  153. }
  154. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  155. int reg_nr, unsigned long data)
  156. {
  157. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  158. MIRROR_OFFSET);
  159. }
  160. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  161. int reg_nr)
  162. {
  163. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  164. }
  165. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  166. unsigned long reg_offs, unsigned long data)
  167. {
  168. iowrite32(data, priv->base + reg_offs);
  169. }
  170. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  171. unsigned long reg_offs)
  172. {
  173. return ioread32(priv->base + reg_offs);
  174. }
  175. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  176. unsigned long reg_offs,
  177. unsigned long mask, unsigned long until)
  178. {
  179. while ((lcdc_read(priv, reg_offs) & mask) != until)
  180. cpu_relax();
  181. }
  182. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  183. {
  184. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  185. }
  186. static void lcdc_sys_write_index(void *handle, unsigned long data)
  187. {
  188. struct sh_mobile_lcdc_chan *ch = handle;
  189. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
  190. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  191. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  192. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  193. }
  194. static void lcdc_sys_write_data(void *handle, unsigned long data)
  195. {
  196. struct sh_mobile_lcdc_chan *ch = handle;
  197. lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
  198. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  199. lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  200. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  201. }
  202. static unsigned long lcdc_sys_read_data(void *handle)
  203. {
  204. struct sh_mobile_lcdc_chan *ch = handle;
  205. lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
  206. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  207. lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  208. udelay(1);
  209. lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
  210. return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
  211. }
  212. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  213. lcdc_sys_write_index,
  214. lcdc_sys_write_data,
  215. lcdc_sys_read_data,
  216. };
  217. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  218. {
  219. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  220. pm_runtime_get_sync(priv->dev);
  221. if (priv->dot_clk)
  222. clk_enable(priv->dot_clk);
  223. }
  224. }
  225. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  226. {
  227. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  228. if (priv->dot_clk)
  229. clk_disable(priv->dot_clk);
  230. pm_runtime_put(priv->dev);
  231. }
  232. }
  233. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  234. struct list_head *pagelist)
  235. {
  236. struct sh_mobile_lcdc_chan *ch = info->par;
  237. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  238. struct page *page;
  239. int nr_pages = 0;
  240. sg_init_table(ch->sglist, nr_pages_max);
  241. list_for_each_entry(page, pagelist, lru)
  242. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  243. return nr_pages;
  244. }
  245. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  246. struct list_head *pagelist)
  247. {
  248. struct sh_mobile_lcdc_chan *ch = info->par;
  249. struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
  250. /* enable clocks before accessing hardware */
  251. sh_mobile_lcdc_clk_on(ch->lcdc);
  252. /*
  253. * It's possible to get here without anything on the pagelist via
  254. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  255. * invocation. In the former case, the acceleration routines are
  256. * stepped in to when using the framebuffer console causing the
  257. * workqueue to be scheduled without any dirty pages on the list.
  258. *
  259. * Despite this, a panel update is still needed given that the
  260. * acceleration routines have their own methods for writing in
  261. * that still need to be updated.
  262. *
  263. * The fsync() and empty pagelist case could be optimized for,
  264. * but we don't bother, as any application exhibiting such
  265. * behaviour is fundamentally broken anyways.
  266. */
  267. if (!list_empty(pagelist)) {
  268. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  269. /* trigger panel update */
  270. dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  271. if (bcfg->start_transfer)
  272. bcfg->start_transfer(bcfg->board_data, ch,
  273. &sh_mobile_lcdc_sys_bus_ops);
  274. lcdc_write_chan(ch, LDSM2R, 1);
  275. dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  276. } else {
  277. if (bcfg->start_transfer)
  278. bcfg->start_transfer(bcfg->board_data, ch,
  279. &sh_mobile_lcdc_sys_bus_ops);
  280. lcdc_write_chan(ch, LDSM2R, 1);
  281. }
  282. }
  283. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  284. {
  285. struct fb_deferred_io *fbdefio = info->fbdefio;
  286. if (fbdefio)
  287. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  288. }
  289. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  290. {
  291. struct sh_mobile_lcdc_priv *priv = data;
  292. struct sh_mobile_lcdc_chan *ch;
  293. unsigned long tmp;
  294. unsigned long ldintr;
  295. int is_sub;
  296. int k;
  297. /* acknowledge interrupt */
  298. ldintr = tmp = lcdc_read(priv, _LDINTR);
  299. /*
  300. * disable further VSYNC End IRQs, preserve all other enabled IRQs,
  301. * write 0 to bits 0-6 to ack all triggered IRQs.
  302. */
  303. tmp &= 0xffffff00 & ~LDINTR_VEE;
  304. lcdc_write(priv, _LDINTR, tmp);
  305. /* figure out if this interrupt is for main or sub lcd */
  306. is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
  307. /* wake up channel and disable clocks */
  308. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  309. ch = &priv->ch[k];
  310. if (!ch->enabled)
  311. continue;
  312. /* Frame Start */
  313. if (ldintr & LDINTR_FS) {
  314. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  315. ch->frame_end = 1;
  316. wake_up(&ch->frame_end_wait);
  317. sh_mobile_lcdc_clk_off(priv);
  318. }
  319. }
  320. /* VSYNC End */
  321. if (ldintr & LDINTR_VES)
  322. complete(&ch->vsync_completion);
  323. }
  324. return IRQ_HANDLED;
  325. }
  326. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  327. int start)
  328. {
  329. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  330. int k;
  331. /* start or stop the lcdc */
  332. if (start)
  333. lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
  334. else
  335. lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
  336. /* wait until power is applied/stopped on all channels */
  337. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  338. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  339. while (1) {
  340. tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
  341. if (start && tmp == 3)
  342. break;
  343. if (!start && tmp == 0)
  344. break;
  345. cpu_relax();
  346. }
  347. if (!start)
  348. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  349. }
  350. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  351. {
  352. struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
  353. unsigned long h_total, hsync_pos, display_h_total;
  354. u32 tmp;
  355. tmp = ch->ldmt1r_value;
  356. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
  357. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
  358. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
  359. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
  360. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
  361. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
  362. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
  363. lcdc_write_chan(ch, LDMT1R, tmp);
  364. /* setup SYS bus */
  365. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  366. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  367. /* horizontal configuration */
  368. h_total = display_var->xres + display_var->hsync_len +
  369. display_var->left_margin + display_var->right_margin;
  370. tmp = h_total / 8; /* HTCN */
  371. tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
  372. lcdc_write_chan(ch, LDHCNR, tmp);
  373. hsync_pos = display_var->xres + display_var->right_margin;
  374. tmp = hsync_pos / 8; /* HSYNP */
  375. tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
  376. lcdc_write_chan(ch, LDHSYNR, tmp);
  377. /* vertical configuration */
  378. tmp = display_var->yres + display_var->vsync_len +
  379. display_var->upper_margin + display_var->lower_margin; /* VTLN */
  380. tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
  381. lcdc_write_chan(ch, LDVLNR, tmp);
  382. tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
  383. tmp |= display_var->vsync_len << 16; /* VSYNW */
  384. lcdc_write_chan(ch, LDVSYNR, tmp);
  385. /* Adjust horizontal synchronisation for HDMI */
  386. display_h_total = display_var->xres + display_var->hsync_len +
  387. display_var->left_margin + display_var->right_margin;
  388. tmp = ((display_var->xres & 7) << 24) |
  389. ((display_h_total & 7) << 16) |
  390. ((display_var->hsync_len & 7) << 8) |
  391. hsync_pos;
  392. lcdc_write_chan(ch, LDHAJR, tmp);
  393. }
  394. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  395. {
  396. struct sh_mobile_lcdc_chan *ch;
  397. struct sh_mobile_lcdc_board_cfg *board_cfg;
  398. unsigned long tmp;
  399. int k, m;
  400. int ret = 0;
  401. /* enable clocks before accessing the hardware */
  402. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  403. if (priv->ch[k].enabled)
  404. sh_mobile_lcdc_clk_on(priv);
  405. /* reset */
  406. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  407. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  408. /* enable LCDC channels */
  409. tmp = lcdc_read(priv, _LDCNT2R);
  410. tmp |= priv->ch[0].enabled;
  411. tmp |= priv->ch[1].enabled;
  412. lcdc_write(priv, _LDCNT2R, tmp);
  413. /* read data from external memory, avoid using the BEU for now */
  414. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  415. /* stop the lcdc first */
  416. sh_mobile_lcdc_start_stop(priv, 0);
  417. /* configure clocks */
  418. tmp = priv->lddckr;
  419. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  420. ch = &priv->ch[k];
  421. if (!priv->ch[k].enabled)
  422. continue;
  423. m = ch->cfg.clock_divider;
  424. if (!m)
  425. continue;
  426. if (m == 1)
  427. m = 1 << 6;
  428. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  429. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
  430. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  431. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  432. }
  433. lcdc_write(priv, _LDDCKR, tmp);
  434. /* start dotclock again */
  435. lcdc_write(priv, _LDDCKSTPR, 0);
  436. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  437. /* interrupts are disabled to begin with */
  438. lcdc_write(priv, _LDINTR, 0);
  439. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  440. ch = &priv->ch[k];
  441. if (!ch->enabled)
  442. continue;
  443. sh_mobile_lcdc_geometry(ch);
  444. /* power supply */
  445. lcdc_write_chan(ch, LDPMR, 0);
  446. board_cfg = &ch->cfg.board_cfg;
  447. if (board_cfg->setup_sys)
  448. ret = board_cfg->setup_sys(board_cfg->board_data, ch,
  449. &sh_mobile_lcdc_sys_bus_ops);
  450. if (ret)
  451. return ret;
  452. }
  453. /* word and long word swap */
  454. lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
  455. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  456. ch = &priv->ch[k];
  457. if (!priv->ch[k].enabled)
  458. continue;
  459. /* set bpp format in PKF[4:0] */
  460. tmp = lcdc_read_chan(ch, LDDFR);
  461. tmp &= ~0x0001001f;
  462. tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
  463. lcdc_write_chan(ch, LDDFR, tmp);
  464. /* point out our frame buffer */
  465. lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
  466. /* set line size */
  467. lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
  468. /* setup deferred io if SYS bus */
  469. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  470. if (ch->ldmt1r_value & (1 << 12) && tmp) {
  471. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  472. ch->defio.delay = msecs_to_jiffies(tmp);
  473. ch->info->fbdefio = &ch->defio;
  474. fb_deferred_io_init(ch->info);
  475. /* one-shot mode */
  476. lcdc_write_chan(ch, LDSM1R, 1);
  477. /* enable "Frame End Interrupt Enable" bit */
  478. lcdc_write(priv, _LDINTR, LDINTR_FE);
  479. } else {
  480. /* continuous read mode */
  481. lcdc_write_chan(ch, LDSM1R, 0);
  482. }
  483. }
  484. /* display output */
  485. lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
  486. /* start the lcdc */
  487. sh_mobile_lcdc_start_stop(priv, 1);
  488. priv->started = 1;
  489. /* tell the board code to enable the panel */
  490. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  491. ch = &priv->ch[k];
  492. if (!ch->enabled)
  493. continue;
  494. board_cfg = &ch->cfg.board_cfg;
  495. if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
  496. board_cfg->display_on(board_cfg->board_data, ch->info);
  497. module_put(board_cfg->owner);
  498. }
  499. }
  500. return 0;
  501. }
  502. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  503. {
  504. struct sh_mobile_lcdc_chan *ch;
  505. struct sh_mobile_lcdc_board_cfg *board_cfg;
  506. int k;
  507. /* clean up deferred io and ask board code to disable panel */
  508. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  509. ch = &priv->ch[k];
  510. if (!ch->enabled)
  511. continue;
  512. /* deferred io mode:
  513. * flush frame, and wait for frame end interrupt
  514. * clean up deferred io and enable clock
  515. */
  516. if (ch->info && ch->info->fbdefio) {
  517. ch->frame_end = 0;
  518. schedule_delayed_work(&ch->info->deferred_work, 0);
  519. wait_event(ch->frame_end_wait, ch->frame_end);
  520. fb_deferred_io_cleanup(ch->info);
  521. ch->info->fbdefio = NULL;
  522. sh_mobile_lcdc_clk_on(priv);
  523. }
  524. board_cfg = &ch->cfg.board_cfg;
  525. if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
  526. board_cfg->display_off(board_cfg->board_data);
  527. module_put(board_cfg->owner);
  528. }
  529. }
  530. /* stop the lcdc */
  531. if (priv->started) {
  532. sh_mobile_lcdc_start_stop(priv, 0);
  533. priv->started = 0;
  534. }
  535. /* stop clocks */
  536. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  537. if (priv->ch[k].enabled)
  538. sh_mobile_lcdc_clk_off(priv);
  539. }
  540. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  541. {
  542. int ifm, miftyp;
  543. switch (ch->cfg.interface_type) {
  544. case RGB8: ifm = 0; miftyp = 0; break;
  545. case RGB9: ifm = 0; miftyp = 4; break;
  546. case RGB12A: ifm = 0; miftyp = 5; break;
  547. case RGB12B: ifm = 0; miftyp = 6; break;
  548. case RGB16: ifm = 0; miftyp = 7; break;
  549. case RGB18: ifm = 0; miftyp = 10; break;
  550. case RGB24: ifm = 0; miftyp = 11; break;
  551. case SYS8A: ifm = 1; miftyp = 0; break;
  552. case SYS8B: ifm = 1; miftyp = 1; break;
  553. case SYS8C: ifm = 1; miftyp = 2; break;
  554. case SYS8D: ifm = 1; miftyp = 3; break;
  555. case SYS9: ifm = 1; miftyp = 4; break;
  556. case SYS12: ifm = 1; miftyp = 5; break;
  557. case SYS16A: ifm = 1; miftyp = 7; break;
  558. case SYS16B: ifm = 1; miftyp = 8; break;
  559. case SYS16C: ifm = 1; miftyp = 9; break;
  560. case SYS18: ifm = 1; miftyp = 10; break;
  561. case SYS24: ifm = 1; miftyp = 11; break;
  562. default: goto bad;
  563. }
  564. /* SUBLCD only supports SYS interface */
  565. if (lcdc_chan_is_sublcd(ch)) {
  566. if (ifm == 0)
  567. goto bad;
  568. else
  569. ifm = 0;
  570. }
  571. ch->ldmt1r_value = (ifm << 12) | miftyp;
  572. return 0;
  573. bad:
  574. return -EINVAL;
  575. }
  576. static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
  577. int clock_source,
  578. struct sh_mobile_lcdc_priv *priv)
  579. {
  580. char *str;
  581. int icksel;
  582. switch (clock_source) {
  583. case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
  584. case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
  585. case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
  586. default:
  587. return -EINVAL;
  588. }
  589. priv->lddckr = icksel << 16;
  590. if (str) {
  591. priv->dot_clk = clk_get(&pdev->dev, str);
  592. if (IS_ERR(priv->dot_clk)) {
  593. dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
  594. return PTR_ERR(priv->dot_clk);
  595. }
  596. }
  597. /* Runtime PM support involves two step for this driver:
  598. * 1) Enable Runtime PM
  599. * 2) Force Runtime PM Resume since hardware is accessed from probe()
  600. */
  601. priv->dev = &pdev->dev;
  602. pm_runtime_enable(priv->dev);
  603. pm_runtime_resume(priv->dev);
  604. return 0;
  605. }
  606. static int sh_mobile_lcdc_setcolreg(u_int regno,
  607. u_int red, u_int green, u_int blue,
  608. u_int transp, struct fb_info *info)
  609. {
  610. u32 *palette = info->pseudo_palette;
  611. if (regno >= PALETTE_NR)
  612. return -EINVAL;
  613. /* only FB_VISUAL_TRUECOLOR supported */
  614. red >>= 16 - info->var.red.length;
  615. green >>= 16 - info->var.green.length;
  616. blue >>= 16 - info->var.blue.length;
  617. transp >>= 16 - info->var.transp.length;
  618. palette[regno] = (red << info->var.red.offset) |
  619. (green << info->var.green.offset) |
  620. (blue << info->var.blue.offset) |
  621. (transp << info->var.transp.offset);
  622. return 0;
  623. }
  624. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  625. .id = "SH Mobile LCDC",
  626. .type = FB_TYPE_PACKED_PIXELS,
  627. .visual = FB_VISUAL_TRUECOLOR,
  628. .accel = FB_ACCEL_NONE,
  629. .xpanstep = 0,
  630. .ypanstep = 1,
  631. .ywrapstep = 0,
  632. };
  633. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  634. const struct fb_fillrect *rect)
  635. {
  636. sys_fillrect(info, rect);
  637. sh_mobile_lcdc_deferred_io_touch(info);
  638. }
  639. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  640. const struct fb_copyarea *area)
  641. {
  642. sys_copyarea(info, area);
  643. sh_mobile_lcdc_deferred_io_touch(info);
  644. }
  645. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  646. const struct fb_image *image)
  647. {
  648. sys_imageblit(info, image);
  649. sh_mobile_lcdc_deferred_io_touch(info);
  650. }
  651. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  652. struct fb_info *info)
  653. {
  654. struct sh_mobile_lcdc_chan *ch = info->par;
  655. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  656. unsigned long ldrcntr;
  657. unsigned long new_pan_offset;
  658. new_pan_offset = (var->yoffset * info->fix.line_length) +
  659. (var->xoffset * (info->var.bits_per_pixel / 8));
  660. if (new_pan_offset == ch->pan_offset)
  661. return 0; /* No change, do nothing */
  662. ldrcntr = lcdc_read(priv, _LDRCNTR);
  663. /* Set the source address for the next refresh */
  664. lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
  665. if (lcdc_chan_is_sublcd(ch))
  666. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  667. else
  668. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  669. ch->pan_offset = new_pan_offset;
  670. sh_mobile_lcdc_deferred_io_touch(info);
  671. return 0;
  672. }
  673. static int sh_mobile_wait_for_vsync(struct fb_info *info)
  674. {
  675. struct sh_mobile_lcdc_chan *ch = info->par;
  676. unsigned long ldintr;
  677. int ret;
  678. /* Enable VSync End interrupt */
  679. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  680. ldintr |= LDINTR_VEE;
  681. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  682. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  683. msecs_to_jiffies(100));
  684. if (!ret)
  685. return -ETIMEDOUT;
  686. return 0;
  687. }
  688. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  689. unsigned long arg)
  690. {
  691. int retval;
  692. switch (cmd) {
  693. case FBIO_WAITFORVSYNC:
  694. retval = sh_mobile_wait_for_vsync(info);
  695. break;
  696. default:
  697. retval = -ENOIOCTLCMD;
  698. break;
  699. }
  700. return retval;
  701. }
  702. static void sh_mobile_fb_reconfig(struct fb_info *info)
  703. {
  704. struct sh_mobile_lcdc_chan *ch = info->par;
  705. struct fb_videomode mode1, mode2;
  706. struct fb_event event;
  707. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  708. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  709. /* More framebuffer users are active */
  710. return;
  711. fb_var_to_videomode(&mode1, &ch->display_var);
  712. fb_var_to_videomode(&mode2, &info->var);
  713. if (fb_mode_is_equal(&mode1, &mode2))
  714. return;
  715. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  716. if (fb_set_var(info, &ch->display_var) < 0)
  717. /* Couldn't reconfigure, hopefully, can continue as before */
  718. return;
  719. info->fix.line_length = mode2.xres * (ch->cfg.bpp / 8);
  720. /*
  721. * fb_set_var() calls the notifier change internally, only if
  722. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  723. * user event, we have to call the chain ourselves.
  724. */
  725. event.info = info;
  726. event.data = &mode2;
  727. fb_notifier_call_chain(evnt, &event);
  728. }
  729. /*
  730. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  731. * user == 1, or with console sem held, if user == 0.
  732. */
  733. static int sh_mobile_release(struct fb_info *info, int user)
  734. {
  735. struct sh_mobile_lcdc_chan *ch = info->par;
  736. mutex_lock(&ch->open_lock);
  737. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  738. ch->use_count--;
  739. /* Nothing to reconfigure, when called from fbcon */
  740. if (user) {
  741. acquire_console_sem();
  742. sh_mobile_fb_reconfig(info);
  743. release_console_sem();
  744. }
  745. mutex_unlock(&ch->open_lock);
  746. return 0;
  747. }
  748. static int sh_mobile_open(struct fb_info *info, int user)
  749. {
  750. struct sh_mobile_lcdc_chan *ch = info->par;
  751. mutex_lock(&ch->open_lock);
  752. ch->use_count++;
  753. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  754. mutex_unlock(&ch->open_lock);
  755. return 0;
  756. }
  757. static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  758. {
  759. struct sh_mobile_lcdc_chan *ch = info->par;
  760. if (var->xres < 160 || var->xres > 1920 ||
  761. var->yres < 120 || var->yres > 1080 ||
  762. var->left_margin < 32 || var->left_margin > 320 ||
  763. var->right_margin < 12 || var->right_margin > 240 ||
  764. var->upper_margin < 12 || var->upper_margin > 120 ||
  765. var->lower_margin < 1 || var->lower_margin > 64 ||
  766. var->hsync_len < 32 || var->hsync_len > 240 ||
  767. var->vsync_len < 2 || var->vsync_len > 64 ||
  768. var->pixclock < 6000 || var->pixclock > 40000 ||
  769. var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
  770. dev_warn(info->dev, "Invalid info: %u %u %u %u %u %u %u %u %u!\n",
  771. var->xres, var->yres,
  772. var->left_margin, var->right_margin,
  773. var->upper_margin, var->lower_margin,
  774. var->hsync_len, var->vsync_len,
  775. var->pixclock);
  776. return -EINVAL;
  777. }
  778. return 0;
  779. }
  780. static struct fb_ops sh_mobile_lcdc_ops = {
  781. .owner = THIS_MODULE,
  782. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  783. .fb_read = fb_sys_read,
  784. .fb_write = fb_sys_write,
  785. .fb_fillrect = sh_mobile_lcdc_fillrect,
  786. .fb_copyarea = sh_mobile_lcdc_copyarea,
  787. .fb_imageblit = sh_mobile_lcdc_imageblit,
  788. .fb_pan_display = sh_mobile_fb_pan_display,
  789. .fb_ioctl = sh_mobile_ioctl,
  790. .fb_open = sh_mobile_open,
  791. .fb_release = sh_mobile_release,
  792. .fb_check_var = sh_mobile_check_var,
  793. };
  794. static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
  795. {
  796. switch (bpp) {
  797. case 16: /* PKF[4:0] = 00011 - RGB 565 */
  798. var->red.offset = 11;
  799. var->red.length = 5;
  800. var->green.offset = 5;
  801. var->green.length = 6;
  802. var->blue.offset = 0;
  803. var->blue.length = 5;
  804. var->transp.offset = 0;
  805. var->transp.length = 0;
  806. break;
  807. case 32: /* PKF[4:0] = 00000 - RGB 888
  808. * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
  809. * this may be because LDDDSR has word swap enabled..
  810. */
  811. var->red.offset = 0;
  812. var->red.length = 8;
  813. var->green.offset = 24;
  814. var->green.length = 8;
  815. var->blue.offset = 16;
  816. var->blue.length = 8;
  817. var->transp.offset = 0;
  818. var->transp.length = 0;
  819. break;
  820. default:
  821. return -EINVAL;
  822. }
  823. var->bits_per_pixel = bpp;
  824. var->red.msb_right = 0;
  825. var->green.msb_right = 0;
  826. var->blue.msb_right = 0;
  827. var->transp.msb_right = 0;
  828. return 0;
  829. }
  830. static int sh_mobile_lcdc_suspend(struct device *dev)
  831. {
  832. struct platform_device *pdev = to_platform_device(dev);
  833. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  834. return 0;
  835. }
  836. static int sh_mobile_lcdc_resume(struct device *dev)
  837. {
  838. struct platform_device *pdev = to_platform_device(dev);
  839. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  840. }
  841. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  842. {
  843. struct platform_device *pdev = to_platform_device(dev);
  844. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  845. struct sh_mobile_lcdc_chan *ch;
  846. int k, n;
  847. /* save per-channel registers */
  848. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  849. ch = &p->ch[k];
  850. if (!ch->enabled)
  851. continue;
  852. for (n = 0; n < NR_CH_REGS; n++)
  853. ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
  854. }
  855. /* save shared registers */
  856. for (n = 0; n < NR_SHARED_REGS; n++)
  857. p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
  858. /* turn off LCDC hardware */
  859. lcdc_write(p, _LDCNT1R, 0);
  860. return 0;
  861. }
  862. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  863. {
  864. struct platform_device *pdev = to_platform_device(dev);
  865. struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
  866. struct sh_mobile_lcdc_chan *ch;
  867. int k, n;
  868. /* restore per-channel registers */
  869. for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
  870. ch = &p->ch[k];
  871. if (!ch->enabled)
  872. continue;
  873. for (n = 0; n < NR_CH_REGS; n++)
  874. lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
  875. }
  876. /* restore shared registers */
  877. for (n = 0; n < NR_SHARED_REGS; n++)
  878. lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
  879. return 0;
  880. }
  881. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  882. .suspend = sh_mobile_lcdc_suspend,
  883. .resume = sh_mobile_lcdc_resume,
  884. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  885. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  886. };
  887. /* locking: called with info->lock held */
  888. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  889. unsigned long action, void *data)
  890. {
  891. struct fb_event *event = data;
  892. struct fb_info *info = event->info;
  893. struct sh_mobile_lcdc_chan *ch = info->par;
  894. struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
  895. int ret;
  896. if (&ch->lcdc->notifier != nb)
  897. return NOTIFY_DONE;
  898. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  899. __func__, action, event->data);
  900. switch(action) {
  901. case FB_EVENT_SUSPEND:
  902. if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
  903. board_cfg->display_off(board_cfg->board_data);
  904. module_put(board_cfg->owner);
  905. }
  906. pm_runtime_put(info->device);
  907. sh_mobile_lcdc_stop(ch->lcdc);
  908. break;
  909. case FB_EVENT_RESUME:
  910. mutex_lock(&ch->open_lock);
  911. sh_mobile_fb_reconfig(info);
  912. mutex_unlock(&ch->open_lock);
  913. /* HDMI must be enabled before LCDC configuration */
  914. if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
  915. board_cfg->display_on(board_cfg->board_data, info);
  916. module_put(board_cfg->owner);
  917. }
  918. ret = sh_mobile_lcdc_start(ch->lcdc);
  919. if (!ret)
  920. pm_runtime_get_sync(info->device);
  921. }
  922. return NOTIFY_OK;
  923. }
  924. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  925. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  926. {
  927. struct fb_info *info;
  928. struct sh_mobile_lcdc_priv *priv;
  929. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  930. struct resource *res;
  931. int error;
  932. void *buf;
  933. int i, j;
  934. if (!pdata) {
  935. dev_err(&pdev->dev, "no platform data defined\n");
  936. return -EINVAL;
  937. }
  938. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  939. i = platform_get_irq(pdev, 0);
  940. if (!res || i < 0) {
  941. dev_err(&pdev->dev, "cannot get platform resources\n");
  942. return -ENOENT;
  943. }
  944. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  945. if (!priv) {
  946. dev_err(&pdev->dev, "cannot allocate device data\n");
  947. return -ENOMEM;
  948. }
  949. platform_set_drvdata(pdev, priv);
  950. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  951. dev_name(&pdev->dev), priv);
  952. if (error) {
  953. dev_err(&pdev->dev, "unable to request irq\n");
  954. goto err1;
  955. }
  956. priv->irq = i;
  957. atomic_set(&priv->hw_usecnt, -1);
  958. j = 0;
  959. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  960. struct sh_mobile_lcdc_chan *ch = priv->ch + j;
  961. ch->lcdc = priv;
  962. memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  963. error = sh_mobile_lcdc_check_interface(ch);
  964. if (error) {
  965. dev_err(&pdev->dev, "unsupported interface type\n");
  966. goto err1;
  967. }
  968. init_waitqueue_head(&ch->frame_end_wait);
  969. init_completion(&ch->vsync_completion);
  970. ch->pan_offset = 0;
  971. switch (pdata->ch[i].chan) {
  972. case LCDC_CHAN_MAINLCD:
  973. ch->enabled = 1 << 1;
  974. ch->reg_offs = lcdc_offs_mainlcd;
  975. j++;
  976. break;
  977. case LCDC_CHAN_SUBLCD:
  978. ch->enabled = 1 << 2;
  979. ch->reg_offs = lcdc_offs_sublcd;
  980. j++;
  981. break;
  982. }
  983. }
  984. if (!j) {
  985. dev_err(&pdev->dev, "no channels defined\n");
  986. error = -EINVAL;
  987. goto err1;
  988. }
  989. priv->base = ioremap_nocache(res->start, resource_size(res));
  990. if (!priv->base)
  991. goto err1;
  992. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  993. if (error) {
  994. dev_err(&pdev->dev, "unable to setup clocks\n");
  995. goto err1;
  996. }
  997. for (i = 0; i < j; i++) {
  998. struct fb_var_screeninfo *var;
  999. const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
  1000. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1001. struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
  1002. const struct fb_videomode *mode = cfg->lcd_cfg;
  1003. unsigned long max_size = 0;
  1004. int k;
  1005. ch->info = framebuffer_alloc(0, &pdev->dev);
  1006. if (!ch->info) {
  1007. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  1008. error = -ENOMEM;
  1009. break;
  1010. }
  1011. info = ch->info;
  1012. var = &info->var;
  1013. info->fbops = &sh_mobile_lcdc_ops;
  1014. info->par = ch;
  1015. mutex_init(&ch->open_lock);
  1016. for (k = 0, lcd_cfg = mode;
  1017. k < cfg->num_cfg && lcd_cfg;
  1018. k++, lcd_cfg++) {
  1019. unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
  1020. if (size > max_size) {
  1021. max_cfg = lcd_cfg;
  1022. max_size = size;
  1023. }
  1024. }
  1025. if (!mode)
  1026. max_size = DEFAULT_XRES * DEFAULT_YRES;
  1027. else if (max_cfg)
  1028. dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
  1029. max_cfg->xres, max_cfg->yres);
  1030. info->fix = sh_mobile_lcdc_fix;
  1031. info->fix.smem_len = max_size * (cfg->bpp / 8) * 2;
  1032. if (!mode)
  1033. mode = &default_720p;
  1034. fb_videomode_to_var(var, mode);
  1035. /* Default Y virtual resolution is 2x panel size */
  1036. var->yres_virtual = var->yres * 2;
  1037. var->activate = FB_ACTIVATE_NOW;
  1038. error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
  1039. if (error)
  1040. break;
  1041. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  1042. &ch->dma_handle, GFP_KERNEL);
  1043. if (!buf) {
  1044. dev_err(&pdev->dev, "unable to allocate buffer\n");
  1045. error = -ENOMEM;
  1046. break;
  1047. }
  1048. info->pseudo_palette = &ch->pseudo_palette;
  1049. info->flags = FBINFO_FLAG_DEFAULT;
  1050. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1051. if (error < 0) {
  1052. dev_err(&pdev->dev, "unable to allocate cmap\n");
  1053. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1054. buf, ch->dma_handle);
  1055. break;
  1056. }
  1057. info->fix.smem_start = ch->dma_handle;
  1058. info->fix.line_length = var->xres * (cfg->bpp / 8);
  1059. info->screen_base = buf;
  1060. info->device = &pdev->dev;
  1061. ch->display_var = *var;
  1062. }
  1063. if (error)
  1064. goto err1;
  1065. error = sh_mobile_lcdc_start(priv);
  1066. if (error) {
  1067. dev_err(&pdev->dev, "unable to start hardware\n");
  1068. goto err1;
  1069. }
  1070. for (i = 0; i < j; i++) {
  1071. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1072. const struct fb_videomode *mode = ch->cfg.lcd_cfg;
  1073. if (!mode)
  1074. mode = &default_720p;
  1075. info = ch->info;
  1076. if (info->fbdefio) {
  1077. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1078. info->fix.smem_len >> PAGE_SHIFT);
  1079. if (!ch->sglist) {
  1080. dev_err(&pdev->dev, "cannot allocate sglist\n");
  1081. goto err1;
  1082. }
  1083. }
  1084. fb_videomode_to_modelist(mode, ch->cfg.num_cfg, &info->modelist);
  1085. error = register_framebuffer(info);
  1086. if (error < 0)
  1087. goto err1;
  1088. dev_info(info->dev,
  1089. "registered %s/%s as %dx%d %dbpp.\n",
  1090. pdev->name,
  1091. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  1092. "mainlcd" : "sublcd",
  1093. info->var.xres, info->var.yres,
  1094. ch->cfg.bpp);
  1095. /* deferred io mode: disable clock to save power */
  1096. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1097. sh_mobile_lcdc_clk_off(priv);
  1098. }
  1099. /* Failure ignored */
  1100. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1101. fb_register_client(&priv->notifier);
  1102. return 0;
  1103. err1:
  1104. sh_mobile_lcdc_remove(pdev);
  1105. return error;
  1106. }
  1107. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1108. {
  1109. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1110. struct fb_info *info;
  1111. int i;
  1112. fb_unregister_client(&priv->notifier);
  1113. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1114. if (priv->ch[i].info && priv->ch[i].info->dev)
  1115. unregister_framebuffer(priv->ch[i].info);
  1116. sh_mobile_lcdc_stop(priv);
  1117. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1118. info = priv->ch[i].info;
  1119. if (!info || !info->device)
  1120. continue;
  1121. if (priv->ch[i].sglist)
  1122. vfree(priv->ch[i].sglist);
  1123. if (info->screen_base)
  1124. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1125. info->screen_base,
  1126. priv->ch[i].dma_handle);
  1127. fb_dealloc_cmap(&info->cmap);
  1128. framebuffer_release(info);
  1129. }
  1130. if (priv->dot_clk)
  1131. clk_put(priv->dot_clk);
  1132. if (priv->dev)
  1133. pm_runtime_disable(priv->dev);
  1134. if (priv->base)
  1135. iounmap(priv->base);
  1136. if (priv->irq)
  1137. free_irq(priv->irq, priv);
  1138. kfree(priv);
  1139. return 0;
  1140. }
  1141. static struct platform_driver sh_mobile_lcdc_driver = {
  1142. .driver = {
  1143. .name = "sh_mobile_lcdc_fb",
  1144. .owner = THIS_MODULE,
  1145. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1146. },
  1147. .probe = sh_mobile_lcdc_probe,
  1148. .remove = sh_mobile_lcdc_remove,
  1149. };
  1150. static int __init sh_mobile_lcdc_init(void)
  1151. {
  1152. return platform_driver_register(&sh_mobile_lcdc_driver);
  1153. }
  1154. static void __exit sh_mobile_lcdc_exit(void)
  1155. {
  1156. platform_driver_unregister(&sh_mobile_lcdc_driver);
  1157. }
  1158. module_init(sh_mobile_lcdc_init);
  1159. module_exit(sh_mobile_lcdc_exit);
  1160. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1161. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1162. MODULE_LICENSE("GPL v2");