sh_mobile_lcdcfb.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  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/atomic.h>
  11. #include <linux/backlight.h>
  12. #include <linux/clk.h>
  13. #include <linux/console.h>
  14. #include <linux/ctype.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/slab.h>
  27. #include <linux/videodev2.h>
  28. #include <linux/vmalloc.h>
  29. #include <video/sh_mobile_lcdc.h>
  30. #include "sh_mobile_lcdcfb.h"
  31. /* ----------------------------------------------------------------------------
  32. * Overlay register definitions
  33. */
  34. #define LDBCR 0xb00
  35. #define LDBCR_UPC(n) (1 << ((n) + 16))
  36. #define LDBCR_UPF(n) (1 << ((n) + 8))
  37. #define LDBCR_UPD(n) (1 << ((n) + 0))
  38. #define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
  39. #define LDBBSIFR_EN (1 << 31)
  40. #define LDBBSIFR_VS (1 << 29)
  41. #define LDBBSIFR_BRSEL (1 << 28)
  42. #define LDBBSIFR_MX (1 << 27)
  43. #define LDBBSIFR_MY (1 << 26)
  44. #define LDBBSIFR_CV3 (3 << 24)
  45. #define LDBBSIFR_CV2 (2 << 24)
  46. #define LDBBSIFR_CV1 (1 << 24)
  47. #define LDBBSIFR_CV0 (0 << 24)
  48. #define LDBBSIFR_CV_MASK (3 << 24)
  49. #define LDBBSIFR_LAY_MASK (0xff << 16)
  50. #define LDBBSIFR_LAY_SHIFT 16
  51. #define LDBBSIFR_ROP3_MASK (0xff << 16)
  52. #define LDBBSIFR_ROP3_SHIFT 16
  53. #define LDBBSIFR_AL_PL8 (3 << 14)
  54. #define LDBBSIFR_AL_PL1 (2 << 14)
  55. #define LDBBSIFR_AL_PK (1 << 14)
  56. #define LDBBSIFR_AL_1 (0 << 14)
  57. #define LDBBSIFR_AL_MASK (3 << 14)
  58. #define LDBBSIFR_SWPL (1 << 10)
  59. #define LDBBSIFR_SWPW (1 << 9)
  60. #define LDBBSIFR_SWPB (1 << 8)
  61. #define LDBBSIFR_RY (1 << 7)
  62. #define LDBBSIFR_CHRR_420 (2 << 0)
  63. #define LDBBSIFR_CHRR_422 (1 << 0)
  64. #define LDBBSIFR_CHRR_444 (0 << 0)
  65. #define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
  66. #define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
  67. #define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
  68. #define LDBBSIFR_RPKF_MASK (0x1f << 0)
  69. #define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
  70. #define LDBBSSZR_BVSS_MASK (0xfff << 16)
  71. #define LDBBSSZR_BVSS_SHIFT 16
  72. #define LDBBSSZR_BHSS_MASK (0xfff << 0)
  73. #define LDBBSSZR_BHSS_SHIFT 0
  74. #define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
  75. #define LDBBLOCR_CVLC_MASK (0xfff << 16)
  76. #define LDBBLOCR_CVLC_SHIFT 16
  77. #define LDBBLOCR_CHLC_MASK (0xfff << 0)
  78. #define LDBBLOCR_CHLC_SHIFT 0
  79. #define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
  80. #define LDBBSMWR_BSMWA_MASK (0xffff << 16)
  81. #define LDBBSMWR_BSMWA_SHIFT 16
  82. #define LDBBSMWR_BSMW_MASK (0xffff << 0)
  83. #define LDBBSMWR_BSMW_SHIFT 0
  84. #define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
  85. #define LDBBSAYR_FG1A_MASK (0xff << 24)
  86. #define LDBBSAYR_FG1A_SHIFT 24
  87. #define LDBBSAYR_FG1R_MASK (0xff << 16)
  88. #define LDBBSAYR_FG1R_SHIFT 16
  89. #define LDBBSAYR_FG1G_MASK (0xff << 8)
  90. #define LDBBSAYR_FG1G_SHIFT 8
  91. #define LDBBSAYR_FG1B_MASK (0xff << 0)
  92. #define LDBBSAYR_FG1B_SHIFT 0
  93. #define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
  94. #define LDBBSACR_FG2A_MASK (0xff << 24)
  95. #define LDBBSACR_FG2A_SHIFT 24
  96. #define LDBBSACR_FG2R_MASK (0xff << 16)
  97. #define LDBBSACR_FG2R_SHIFT 16
  98. #define LDBBSACR_FG2G_MASK (0xff << 8)
  99. #define LDBBSACR_FG2G_SHIFT 8
  100. #define LDBBSACR_FG2B_MASK (0xff << 0)
  101. #define LDBBSACR_FG2B_SHIFT 0
  102. #define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
  103. #define LDBBSAAR_AP_MASK (0xff << 24)
  104. #define LDBBSAAR_AP_SHIFT 24
  105. #define LDBBSAAR_R_MASK (0xff << 16)
  106. #define LDBBSAAR_R_SHIFT 16
  107. #define LDBBSAAR_GY_MASK (0xff << 8)
  108. #define LDBBSAAR_GY_SHIFT 8
  109. #define LDBBSAAR_B_MASK (0xff << 0)
  110. #define LDBBSAAR_B_SHIFT 0
  111. #define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
  112. #define LDBBPPCR_AP_MASK (0xff << 24)
  113. #define LDBBPPCR_AP_SHIFT 24
  114. #define LDBBPPCR_R_MASK (0xff << 16)
  115. #define LDBBPPCR_R_SHIFT 16
  116. #define LDBBPPCR_GY_MASK (0xff << 8)
  117. #define LDBBPPCR_GY_SHIFT 8
  118. #define LDBBPPCR_B_MASK (0xff << 0)
  119. #define LDBBPPCR_B_SHIFT 0
  120. #define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
  121. #define LDBBBGCL_BGA_MASK (0xff << 24)
  122. #define LDBBBGCL_BGA_SHIFT 24
  123. #define LDBBBGCL_BGR_MASK (0xff << 16)
  124. #define LDBBBGCL_BGR_SHIFT 16
  125. #define LDBBBGCL_BGG_MASK (0xff << 8)
  126. #define LDBBBGCL_BGG_SHIFT 8
  127. #define LDBBBGCL_BGB_MASK (0xff << 0)
  128. #define LDBBBGCL_BGB_SHIFT 0
  129. #define SIDE_B_OFFSET 0x1000
  130. #define MIRROR_OFFSET 0x2000
  131. #define MAX_XRES 1920
  132. #define MAX_YRES 1080
  133. enum sh_mobile_lcdc_overlay_mode {
  134. LCDC_OVERLAY_BLEND,
  135. LCDC_OVERLAY_ROP3,
  136. };
  137. /*
  138. * struct sh_mobile_lcdc_overlay - LCDC display overlay
  139. *
  140. * @channel: LCDC channel this overlay belongs to
  141. * @cfg: Overlay configuration
  142. * @info: Frame buffer device
  143. * @index: Overlay index (0-3)
  144. * @base: Overlay registers base address
  145. * @enabled: True if the overlay is enabled
  146. * @mode: Overlay blending mode (alpha blend or ROP3)
  147. * @alpha: Global alpha blending value (0-255, for alpha blending mode)
  148. * @rop3: Raster operation (for ROP3 mode)
  149. * @fb_mem: Frame buffer virtual memory address
  150. * @fb_size: Frame buffer size in bytes
  151. * @dma_handle: Frame buffer DMA address
  152. * @base_addr_y: Overlay base address (RGB or luma component)
  153. * @base_addr_c: Overlay base address (chroma component)
  154. * @pan_y_offset: Panning linear offset in bytes (luma component)
  155. * @format: Current pixelf format
  156. * @xres: Horizontal visible resolution
  157. * @xres_virtual: Horizontal total resolution
  158. * @yres: Vertical visible resolution
  159. * @yres_virtual: Vertical total resolution
  160. * @pitch: Overlay line pitch
  161. * @pos_x: Horizontal overlay position
  162. * @pos_y: Vertical overlay position
  163. */
  164. struct sh_mobile_lcdc_overlay {
  165. struct sh_mobile_lcdc_chan *channel;
  166. const struct sh_mobile_lcdc_overlay_cfg *cfg;
  167. struct fb_info *info;
  168. unsigned int index;
  169. unsigned long base;
  170. bool enabled;
  171. enum sh_mobile_lcdc_overlay_mode mode;
  172. unsigned int alpha;
  173. unsigned int rop3;
  174. void *fb_mem;
  175. unsigned long fb_size;
  176. dma_addr_t dma_handle;
  177. unsigned long base_addr_y;
  178. unsigned long base_addr_c;
  179. unsigned long pan_y_offset;
  180. const struct sh_mobile_lcdc_format_info *format;
  181. unsigned int xres;
  182. unsigned int xres_virtual;
  183. unsigned int yres;
  184. unsigned int yres_virtual;
  185. unsigned int pitch;
  186. int pos_x;
  187. int pos_y;
  188. };
  189. struct sh_mobile_lcdc_priv {
  190. void __iomem *base;
  191. int irq;
  192. atomic_t hw_usecnt;
  193. struct device *dev;
  194. struct clk *dot_clk;
  195. unsigned long lddckr;
  196. struct sh_mobile_lcdc_chan ch[2];
  197. struct sh_mobile_lcdc_overlay overlays[4];
  198. struct notifier_block notifier;
  199. int started;
  200. int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
  201. };
  202. /* -----------------------------------------------------------------------------
  203. * Registers access
  204. */
  205. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  206. [LDDCKPAT1R] = 0x400,
  207. [LDDCKPAT2R] = 0x404,
  208. [LDMT1R] = 0x418,
  209. [LDMT2R] = 0x41c,
  210. [LDMT3R] = 0x420,
  211. [LDDFR] = 0x424,
  212. [LDSM1R] = 0x428,
  213. [LDSM2R] = 0x42c,
  214. [LDSA1R] = 0x430,
  215. [LDSA2R] = 0x434,
  216. [LDMLSR] = 0x438,
  217. [LDHCNR] = 0x448,
  218. [LDHSYNR] = 0x44c,
  219. [LDVLNR] = 0x450,
  220. [LDVSYNR] = 0x454,
  221. [LDPMR] = 0x460,
  222. [LDHAJR] = 0x4a0,
  223. };
  224. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  225. [LDDCKPAT1R] = 0x408,
  226. [LDDCKPAT2R] = 0x40c,
  227. [LDMT1R] = 0x600,
  228. [LDMT2R] = 0x604,
  229. [LDMT3R] = 0x608,
  230. [LDDFR] = 0x60c,
  231. [LDSM1R] = 0x610,
  232. [LDSM2R] = 0x614,
  233. [LDSA1R] = 0x618,
  234. [LDMLSR] = 0x620,
  235. [LDHCNR] = 0x624,
  236. [LDHSYNR] = 0x628,
  237. [LDVLNR] = 0x62c,
  238. [LDVSYNR] = 0x630,
  239. [LDPMR] = 0x63c,
  240. };
  241. static bool banked(int reg_nr)
  242. {
  243. switch (reg_nr) {
  244. case LDMT1R:
  245. case LDMT2R:
  246. case LDMT3R:
  247. case LDDFR:
  248. case LDSM1R:
  249. case LDSA1R:
  250. case LDSA2R:
  251. case LDMLSR:
  252. case LDHCNR:
  253. case LDHSYNR:
  254. case LDVLNR:
  255. case LDVSYNR:
  256. return true;
  257. }
  258. return false;
  259. }
  260. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  261. {
  262. return chan->cfg->chan == LCDC_CHAN_SUBLCD;
  263. }
  264. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  265. int reg_nr, unsigned long data)
  266. {
  267. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  268. if (banked(reg_nr))
  269. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  270. SIDE_B_OFFSET);
  271. }
  272. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  273. int reg_nr, unsigned long data)
  274. {
  275. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  276. MIRROR_OFFSET);
  277. }
  278. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  279. int reg_nr)
  280. {
  281. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  282. }
  283. static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
  284. int reg, unsigned long data)
  285. {
  286. iowrite32(data, ovl->channel->lcdc->base + reg);
  287. iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
  288. }
  289. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  290. unsigned long reg_offs, unsigned long data)
  291. {
  292. iowrite32(data, priv->base + reg_offs);
  293. }
  294. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  295. unsigned long reg_offs)
  296. {
  297. return ioread32(priv->base + reg_offs);
  298. }
  299. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  300. unsigned long reg_offs,
  301. unsigned long mask, unsigned long until)
  302. {
  303. while ((lcdc_read(priv, reg_offs) & mask) != until)
  304. cpu_relax();
  305. }
  306. /* -----------------------------------------------------------------------------
  307. * Clock management
  308. */
  309. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  310. {
  311. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  312. if (priv->dot_clk)
  313. clk_prepare_enable(priv->dot_clk);
  314. pm_runtime_get_sync(priv->dev);
  315. }
  316. }
  317. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  318. {
  319. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  320. pm_runtime_put(priv->dev);
  321. if (priv->dot_clk)
  322. clk_disable_unprepare(priv->dot_clk);
  323. }
  324. }
  325. static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
  326. int clock_source)
  327. {
  328. struct clk *clk;
  329. char *str;
  330. switch (clock_source) {
  331. case LCDC_CLK_BUS:
  332. str = "bus_clk";
  333. priv->lddckr = LDDCKR_ICKSEL_BUS;
  334. break;
  335. case LCDC_CLK_PERIPHERAL:
  336. str = "peripheral_clk";
  337. priv->lddckr = LDDCKR_ICKSEL_MIPI;
  338. break;
  339. case LCDC_CLK_EXTERNAL:
  340. str = NULL;
  341. priv->lddckr = LDDCKR_ICKSEL_HDMI;
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. if (str == NULL)
  347. return 0;
  348. clk = clk_get(priv->dev, str);
  349. if (IS_ERR(clk)) {
  350. dev_err(priv->dev, "cannot get dot clock %s\n", str);
  351. return PTR_ERR(clk);
  352. }
  353. priv->dot_clk = clk;
  354. return 0;
  355. }
  356. /* -----------------------------------------------------------------------------
  357. * Display, panel and deferred I/O
  358. */
  359. static void lcdc_sys_write_index(void *handle, unsigned long data)
  360. {
  361. struct sh_mobile_lcdc_chan *ch = handle;
  362. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
  363. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  364. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  365. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  366. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  367. }
  368. static void lcdc_sys_write_data(void *handle, unsigned long data)
  369. {
  370. struct sh_mobile_lcdc_chan *ch = handle;
  371. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
  372. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  373. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  374. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  375. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  376. }
  377. static unsigned long lcdc_sys_read_data(void *handle)
  378. {
  379. struct sh_mobile_lcdc_chan *ch = handle;
  380. lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
  381. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  382. lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
  383. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  384. udelay(1);
  385. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  386. return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
  387. }
  388. static struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  389. .write_index = lcdc_sys_write_index,
  390. .write_data = lcdc_sys_write_data,
  391. .read_data = lcdc_sys_read_data,
  392. };
  393. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  394. struct list_head *pagelist)
  395. {
  396. struct sh_mobile_lcdc_chan *ch = info->par;
  397. unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
  398. struct page *page;
  399. int nr_pages = 0;
  400. sg_init_table(ch->sglist, nr_pages_max);
  401. list_for_each_entry(page, pagelist, lru)
  402. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  403. return nr_pages;
  404. }
  405. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  406. struct list_head *pagelist)
  407. {
  408. struct sh_mobile_lcdc_chan *ch = info->par;
  409. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  410. /* enable clocks before accessing hardware */
  411. sh_mobile_lcdc_clk_on(ch->lcdc);
  412. /*
  413. * It's possible to get here without anything on the pagelist via
  414. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  415. * invocation. In the former case, the acceleration routines are
  416. * stepped in to when using the framebuffer console causing the
  417. * workqueue to be scheduled without any dirty pages on the list.
  418. *
  419. * Despite this, a panel update is still needed given that the
  420. * acceleration routines have their own methods for writing in
  421. * that still need to be updated.
  422. *
  423. * The fsync() and empty pagelist case could be optimized for,
  424. * but we don't bother, as any application exhibiting such
  425. * behaviour is fundamentally broken anyways.
  426. */
  427. if (!list_empty(pagelist)) {
  428. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  429. /* trigger panel update */
  430. dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  431. if (panel->start_transfer)
  432. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  433. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  434. dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
  435. DMA_TO_DEVICE);
  436. } else {
  437. if (panel->start_transfer)
  438. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  439. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  440. }
  441. }
  442. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  443. {
  444. struct fb_deferred_io *fbdefio = info->fbdefio;
  445. if (fbdefio)
  446. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  447. }
  448. static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
  449. {
  450. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  451. if (ch->tx_dev) {
  452. int ret;
  453. ret = ch->tx_dev->ops->display_on(ch->tx_dev);
  454. if (ret < 0)
  455. return;
  456. if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
  457. ch->info->state = FBINFO_STATE_SUSPENDED;
  458. }
  459. /* HDMI must be enabled before LCDC configuration */
  460. if (panel->display_on)
  461. panel->display_on();
  462. }
  463. static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
  464. {
  465. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  466. if (panel->display_off)
  467. panel->display_off();
  468. if (ch->tx_dev)
  469. ch->tx_dev->ops->display_off(ch->tx_dev);
  470. }
  471. static bool
  472. sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
  473. const struct fb_videomode *new_mode)
  474. {
  475. dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
  476. ch->display.mode.xres, ch->display.mode.yres,
  477. new_mode->xres, new_mode->yres);
  478. /* It can be a different monitor with an equal video-mode */
  479. if (fb_mode_is_equal(&ch->display.mode, new_mode))
  480. return false;
  481. dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
  482. ch->display.mode.yres, new_mode->yres);
  483. ch->display.mode = *new_mode;
  484. return true;
  485. }
  486. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  487. struct fb_info *info);
  488. static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
  489. enum sh_mobile_lcdc_entity_event event,
  490. const struct fb_videomode *mode,
  491. const struct fb_monspecs *monspec)
  492. {
  493. struct fb_info *info = ch->info;
  494. struct fb_var_screeninfo var;
  495. int ret = 0;
  496. switch (event) {
  497. case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
  498. /* HDMI plug in */
  499. console_lock();
  500. if (lock_fb_info(info)) {
  501. ch->display.width = monspec->max_x * 10;
  502. ch->display.height = monspec->max_y * 10;
  503. if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
  504. info->state == FBINFO_STATE_RUNNING) {
  505. /* First activation with the default monitor.
  506. * Just turn on, if we run a resume here, the
  507. * logo disappears.
  508. */
  509. info->var.width = ch->display.width;
  510. info->var.height = ch->display.height;
  511. sh_mobile_lcdc_display_on(ch);
  512. } else {
  513. /* New monitor or have to wake up */
  514. fb_set_suspend(info, 0);
  515. }
  516. unlock_fb_info(info);
  517. }
  518. console_unlock();
  519. break;
  520. case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
  521. /* HDMI disconnect */
  522. console_lock();
  523. if (lock_fb_info(info)) {
  524. fb_set_suspend(info, 1);
  525. unlock_fb_info(info);
  526. }
  527. console_unlock();
  528. break;
  529. case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
  530. /* Validate a proposed new mode */
  531. fb_videomode_to_var(&var, mode);
  532. var.bits_per_pixel = info->var.bits_per_pixel;
  533. var.grayscale = info->var.grayscale;
  534. ret = sh_mobile_lcdc_check_var(&var, info);
  535. break;
  536. }
  537. return ret;
  538. }
  539. /* -----------------------------------------------------------------------------
  540. * Format helpers
  541. */
  542. struct sh_mobile_lcdc_format_info {
  543. u32 fourcc;
  544. unsigned int bpp;
  545. bool yuv;
  546. u32 lddfr;
  547. };
  548. static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
  549. {
  550. .fourcc = V4L2_PIX_FMT_RGB565,
  551. .bpp = 16,
  552. .yuv = false,
  553. .lddfr = LDDFR_PKF_RGB16,
  554. }, {
  555. .fourcc = V4L2_PIX_FMT_BGR24,
  556. .bpp = 24,
  557. .yuv = false,
  558. .lddfr = LDDFR_PKF_RGB24,
  559. }, {
  560. .fourcc = V4L2_PIX_FMT_BGR32,
  561. .bpp = 32,
  562. .yuv = false,
  563. .lddfr = LDDFR_PKF_ARGB32,
  564. }, {
  565. .fourcc = V4L2_PIX_FMT_NV12,
  566. .bpp = 12,
  567. .yuv = true,
  568. .lddfr = LDDFR_CC | LDDFR_YF_420,
  569. }, {
  570. .fourcc = V4L2_PIX_FMT_NV21,
  571. .bpp = 12,
  572. .yuv = true,
  573. .lddfr = LDDFR_CC | LDDFR_YF_420,
  574. }, {
  575. .fourcc = V4L2_PIX_FMT_NV16,
  576. .bpp = 16,
  577. .yuv = true,
  578. .lddfr = LDDFR_CC | LDDFR_YF_422,
  579. }, {
  580. .fourcc = V4L2_PIX_FMT_NV61,
  581. .bpp = 16,
  582. .yuv = true,
  583. .lddfr = LDDFR_CC | LDDFR_YF_422,
  584. }, {
  585. .fourcc = V4L2_PIX_FMT_NV24,
  586. .bpp = 24,
  587. .yuv = true,
  588. .lddfr = LDDFR_CC | LDDFR_YF_444,
  589. }, {
  590. .fourcc = V4L2_PIX_FMT_NV42,
  591. .bpp = 24,
  592. .yuv = true,
  593. .lddfr = LDDFR_CC | LDDFR_YF_444,
  594. },
  595. };
  596. static const struct sh_mobile_lcdc_format_info *
  597. sh_mobile_format_info(u32 fourcc)
  598. {
  599. unsigned int i;
  600. for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
  601. if (sh_mobile_format_infos[i].fourcc == fourcc)
  602. return &sh_mobile_format_infos[i];
  603. }
  604. return NULL;
  605. }
  606. static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
  607. {
  608. if (var->grayscale > 1)
  609. return var->grayscale;
  610. switch (var->bits_per_pixel) {
  611. case 16:
  612. return V4L2_PIX_FMT_RGB565;
  613. case 24:
  614. return V4L2_PIX_FMT_BGR24;
  615. case 32:
  616. return V4L2_PIX_FMT_BGR32;
  617. default:
  618. return 0;
  619. }
  620. }
  621. static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
  622. {
  623. return var->grayscale > 1;
  624. }
  625. /* -----------------------------------------------------------------------------
  626. * Start, stop and IRQ
  627. */
  628. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  629. {
  630. struct sh_mobile_lcdc_priv *priv = data;
  631. struct sh_mobile_lcdc_chan *ch;
  632. unsigned long ldintr;
  633. int is_sub;
  634. int k;
  635. /* Acknowledge interrupts and disable further VSYNC End IRQs. */
  636. ldintr = lcdc_read(priv, _LDINTR);
  637. lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
  638. /* figure out if this interrupt is for main or sub lcd */
  639. is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
  640. /* wake up channel and disable clocks */
  641. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  642. ch = &priv->ch[k];
  643. if (!ch->enabled)
  644. continue;
  645. /* Frame End */
  646. if (ldintr & LDINTR_FS) {
  647. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  648. ch->frame_end = 1;
  649. wake_up(&ch->frame_end_wait);
  650. sh_mobile_lcdc_clk_off(priv);
  651. }
  652. }
  653. /* VSYNC End */
  654. if (ldintr & LDINTR_VES)
  655. complete(&ch->vsync_completion);
  656. }
  657. return IRQ_HANDLED;
  658. }
  659. static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
  660. {
  661. unsigned long ldintr;
  662. int ret;
  663. /* Enable VSync End interrupt and be careful not to acknowledge any
  664. * pending interrupt.
  665. */
  666. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  667. ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
  668. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  669. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  670. msecs_to_jiffies(100));
  671. if (!ret)
  672. return -ETIMEDOUT;
  673. return 0;
  674. }
  675. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  676. int start)
  677. {
  678. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  679. int k;
  680. /* start or stop the lcdc */
  681. if (start)
  682. lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
  683. else
  684. lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
  685. /* wait until power is applied/stopped on all channels */
  686. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  687. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  688. while (1) {
  689. tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
  690. & LDPMR_LPS;
  691. if (start && tmp == LDPMR_LPS)
  692. break;
  693. if (!start && tmp == 0)
  694. break;
  695. cpu_relax();
  696. }
  697. if (!start)
  698. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  699. }
  700. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  701. {
  702. const struct fb_var_screeninfo *var = &ch->info->var;
  703. const struct fb_videomode *mode = &ch->display.mode;
  704. unsigned long h_total, hsync_pos, display_h_total;
  705. u32 tmp;
  706. tmp = ch->ldmt1r_value;
  707. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
  708. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
  709. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
  710. tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
  711. tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
  712. tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
  713. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
  714. lcdc_write_chan(ch, LDMT1R, tmp);
  715. /* setup SYS bus */
  716. lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
  717. lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
  718. /* horizontal configuration */
  719. h_total = mode->xres + mode->hsync_len + mode->left_margin
  720. + mode->right_margin;
  721. tmp = h_total / 8; /* HTCN */
  722. tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
  723. lcdc_write_chan(ch, LDHCNR, tmp);
  724. hsync_pos = mode->xres + mode->right_margin;
  725. tmp = hsync_pos / 8; /* HSYNP */
  726. tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
  727. lcdc_write_chan(ch, LDHSYNR, tmp);
  728. /* vertical configuration */
  729. tmp = mode->yres + mode->vsync_len + mode->upper_margin
  730. + mode->lower_margin; /* VTLN */
  731. tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
  732. lcdc_write_chan(ch, LDVLNR, tmp);
  733. tmp = mode->yres + mode->lower_margin; /* VSYNP */
  734. tmp |= mode->vsync_len << 16; /* VSYNW */
  735. lcdc_write_chan(ch, LDVSYNR, tmp);
  736. /* Adjust horizontal synchronisation for HDMI */
  737. display_h_total = mode->xres + mode->hsync_len + mode->left_margin
  738. + mode->right_margin;
  739. tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
  740. | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
  741. lcdc_write_chan(ch, LDHAJR, tmp);
  742. lcdc_write_chan_mirror(ch, LDHAJR, tmp);
  743. }
  744. static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
  745. {
  746. u32 format = 0;
  747. if (!ovl->enabled) {
  748. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  749. lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
  750. lcdc_write(ovl->channel->lcdc, LDBCR,
  751. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  752. return;
  753. }
  754. ovl->base_addr_y = ovl->dma_handle;
  755. ovl->base_addr_c = ovl->dma_handle
  756. + ovl->xres_virtual * ovl->yres_virtual;
  757. switch (ovl->mode) {
  758. case LCDC_OVERLAY_BLEND:
  759. format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
  760. break;
  761. case LCDC_OVERLAY_ROP3:
  762. format = LDBBSIFR_EN | LDBBSIFR_BRSEL
  763. | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
  764. break;
  765. }
  766. switch (ovl->format->fourcc) {
  767. case V4L2_PIX_FMT_RGB565:
  768. case V4L2_PIX_FMT_NV21:
  769. case V4L2_PIX_FMT_NV61:
  770. case V4L2_PIX_FMT_NV42:
  771. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
  772. break;
  773. case V4L2_PIX_FMT_BGR24:
  774. case V4L2_PIX_FMT_NV12:
  775. case V4L2_PIX_FMT_NV16:
  776. case V4L2_PIX_FMT_NV24:
  777. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
  778. break;
  779. case V4L2_PIX_FMT_BGR32:
  780. default:
  781. format |= LDBBSIFR_SWPL;
  782. break;
  783. }
  784. switch (ovl->format->fourcc) {
  785. case V4L2_PIX_FMT_RGB565:
  786. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
  787. break;
  788. case V4L2_PIX_FMT_BGR24:
  789. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
  790. break;
  791. case V4L2_PIX_FMT_BGR32:
  792. format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
  793. break;
  794. case V4L2_PIX_FMT_NV12:
  795. case V4L2_PIX_FMT_NV21:
  796. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
  797. break;
  798. case V4L2_PIX_FMT_NV16:
  799. case V4L2_PIX_FMT_NV61:
  800. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
  801. break;
  802. case V4L2_PIX_FMT_NV24:
  803. case V4L2_PIX_FMT_NV42:
  804. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
  805. break;
  806. }
  807. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  808. lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
  809. lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
  810. (ovl->yres << LDBBSSZR_BVSS_SHIFT) |
  811. (ovl->xres << LDBBSSZR_BHSS_SHIFT));
  812. lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
  813. (ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
  814. (ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
  815. lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
  816. ovl->pitch << LDBBSMWR_BSMW_SHIFT);
  817. lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
  818. lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
  819. lcdc_write(ovl->channel->lcdc, LDBCR,
  820. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  821. }
  822. /*
  823. * __sh_mobile_lcdc_start - Configure and start the LCDC
  824. * @priv: LCDC device
  825. *
  826. * Configure all enabled channels and start the LCDC device. All external
  827. * devices (clocks, MERAM, panels, ...) are not touched by this function.
  828. */
  829. static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  830. {
  831. struct sh_mobile_lcdc_chan *ch;
  832. unsigned long tmp;
  833. int k, m;
  834. /* Enable LCDC channels. Read data from external memory, avoid using the
  835. * BEU for now.
  836. */
  837. lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
  838. /* Stop the LCDC first and disable all interrupts. */
  839. sh_mobile_lcdc_start_stop(priv, 0);
  840. lcdc_write(priv, _LDINTR, 0);
  841. /* Configure power supply, dot clocks and start them. */
  842. tmp = priv->lddckr;
  843. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  844. ch = &priv->ch[k];
  845. if (!ch->enabled)
  846. continue;
  847. /* Power supply */
  848. lcdc_write_chan(ch, LDPMR, 0);
  849. m = ch->cfg->clock_divider;
  850. if (!m)
  851. continue;
  852. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  853. * denominator.
  854. */
  855. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  856. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  857. if (m == 1)
  858. m = LDDCKR_MOSEL;
  859. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  860. }
  861. lcdc_write(priv, _LDDCKR, tmp);
  862. lcdc_write(priv, _LDDCKSTPR, 0);
  863. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  864. /* Setup geometry, format, frame buffer memory and operation mode. */
  865. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  866. ch = &priv->ch[k];
  867. if (!ch->enabled)
  868. continue;
  869. sh_mobile_lcdc_geometry(ch);
  870. tmp = ch->format->lddfr;
  871. if (ch->format->yuv) {
  872. switch (ch->colorspace) {
  873. case V4L2_COLORSPACE_REC709:
  874. tmp |= LDDFR_CF1;
  875. break;
  876. case V4L2_COLORSPACE_JPEG:
  877. tmp |= LDDFR_CF0;
  878. break;
  879. }
  880. }
  881. lcdc_write_chan(ch, LDDFR, tmp);
  882. lcdc_write_chan(ch, LDMLSR, ch->line_size);
  883. lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
  884. if (ch->format->yuv)
  885. lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
  886. /* When using deferred I/O mode, configure the LCDC for one-shot
  887. * operation and enable the frame end interrupt. Otherwise use
  888. * continuous read mode.
  889. */
  890. if (ch->ldmt1r_value & LDMT1R_IFM &&
  891. ch->cfg->sys_bus_cfg.deferred_io_msec) {
  892. lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
  893. lcdc_write(priv, _LDINTR, LDINTR_FE);
  894. } else {
  895. lcdc_write_chan(ch, LDSM1R, 0);
  896. }
  897. }
  898. /* Word and long word swap. */
  899. switch (priv->ch[0].format->fourcc) {
  900. case V4L2_PIX_FMT_RGB565:
  901. case V4L2_PIX_FMT_NV21:
  902. case V4L2_PIX_FMT_NV61:
  903. case V4L2_PIX_FMT_NV42:
  904. tmp = LDDDSR_LS | LDDDSR_WS;
  905. break;
  906. case V4L2_PIX_FMT_BGR24:
  907. case V4L2_PIX_FMT_NV12:
  908. case V4L2_PIX_FMT_NV16:
  909. case V4L2_PIX_FMT_NV24:
  910. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  911. break;
  912. case V4L2_PIX_FMT_BGR32:
  913. default:
  914. tmp = LDDDSR_LS;
  915. break;
  916. }
  917. lcdc_write(priv, _LDDDSR, tmp);
  918. /* Enable the display output. */
  919. lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
  920. sh_mobile_lcdc_start_stop(priv, 1);
  921. priv->started = 1;
  922. }
  923. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  924. {
  925. struct sh_mobile_lcdc_chan *ch;
  926. unsigned long tmp;
  927. int ret;
  928. int k;
  929. /* enable clocks before accessing the hardware */
  930. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  931. if (priv->ch[k].enabled)
  932. sh_mobile_lcdc_clk_on(priv);
  933. }
  934. /* reset */
  935. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
  936. lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
  937. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  938. const struct sh_mobile_lcdc_panel_cfg *panel;
  939. ch = &priv->ch[k];
  940. if (!ch->enabled)
  941. continue;
  942. panel = &ch->cfg->panel_cfg;
  943. if (panel->setup_sys) {
  944. ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
  945. if (ret)
  946. return ret;
  947. }
  948. }
  949. /* Compute frame buffer base address and pitch for each channel. */
  950. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  951. ch = &priv->ch[k];
  952. if (!ch->enabled)
  953. continue;
  954. ch->base_addr_y = ch->dma_handle;
  955. ch->base_addr_c = ch->dma_handle
  956. + ch->xres_virtual * ch->yres_virtual;
  957. ch->line_size = ch->pitch;
  958. }
  959. for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
  960. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
  961. sh_mobile_lcdc_overlay_setup(ovl);
  962. }
  963. /* Start the LCDC. */
  964. __sh_mobile_lcdc_start(priv);
  965. /* Setup deferred I/O, tell the board code to enable the panels, and
  966. * turn backlight on.
  967. */
  968. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  969. ch = &priv->ch[k];
  970. if (!ch->enabled)
  971. continue;
  972. tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
  973. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  974. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  975. ch->defio.delay = msecs_to_jiffies(tmp);
  976. ch->info->fbdefio = &ch->defio;
  977. fb_deferred_io_init(ch->info);
  978. }
  979. sh_mobile_lcdc_display_on(ch);
  980. if (ch->bl) {
  981. ch->bl->props.power = FB_BLANK_UNBLANK;
  982. backlight_update_status(ch->bl);
  983. }
  984. }
  985. return 0;
  986. }
  987. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  988. {
  989. struct sh_mobile_lcdc_chan *ch;
  990. int k;
  991. /* clean up deferred io and ask board code to disable panel */
  992. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  993. ch = &priv->ch[k];
  994. if (!ch->enabled)
  995. continue;
  996. /* deferred io mode:
  997. * flush frame, and wait for frame end interrupt
  998. * clean up deferred io and enable clock
  999. */
  1000. if (ch->info && ch->info->fbdefio) {
  1001. ch->frame_end = 0;
  1002. schedule_delayed_work(&ch->info->deferred_work, 0);
  1003. wait_event(ch->frame_end_wait, ch->frame_end);
  1004. fb_deferred_io_cleanup(ch->info);
  1005. ch->info->fbdefio = NULL;
  1006. sh_mobile_lcdc_clk_on(priv);
  1007. }
  1008. if (ch->bl) {
  1009. ch->bl->props.power = FB_BLANK_POWERDOWN;
  1010. backlight_update_status(ch->bl);
  1011. }
  1012. sh_mobile_lcdc_display_off(ch);
  1013. }
  1014. /* stop the lcdc */
  1015. if (priv->started) {
  1016. sh_mobile_lcdc_start_stop(priv, 0);
  1017. priv->started = 0;
  1018. }
  1019. /* stop clocks */
  1020. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  1021. if (priv->ch[k].enabled)
  1022. sh_mobile_lcdc_clk_off(priv);
  1023. }
  1024. static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1025. struct fb_info *info)
  1026. {
  1027. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  1028. return -EINVAL;
  1029. /* Make sure the virtual resolution is at least as big as the visible
  1030. * resolution.
  1031. */
  1032. if (var->xres_virtual < var->xres)
  1033. var->xres_virtual = var->xres;
  1034. if (var->yres_virtual < var->yres)
  1035. var->yres_virtual = var->yres;
  1036. if (sh_mobile_format_is_fourcc(var)) {
  1037. const struct sh_mobile_lcdc_format_info *format;
  1038. format = sh_mobile_format_info(var->grayscale);
  1039. if (format == NULL)
  1040. return -EINVAL;
  1041. var->bits_per_pixel = format->bpp;
  1042. /* Default to RGB and JPEG color-spaces for RGB and YUV formats
  1043. * respectively.
  1044. */
  1045. if (!format->yuv)
  1046. var->colorspace = V4L2_COLORSPACE_SRGB;
  1047. else if (var->colorspace != V4L2_COLORSPACE_REC709)
  1048. var->colorspace = V4L2_COLORSPACE_JPEG;
  1049. } else {
  1050. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  1051. var->bits_per_pixel = 16;
  1052. var->red.offset = 11;
  1053. var->red.length = 5;
  1054. var->green.offset = 5;
  1055. var->green.length = 6;
  1056. var->blue.offset = 0;
  1057. var->blue.length = 5;
  1058. var->transp.offset = 0;
  1059. var->transp.length = 0;
  1060. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  1061. var->bits_per_pixel = 24;
  1062. var->red.offset = 16;
  1063. var->red.length = 8;
  1064. var->green.offset = 8;
  1065. var->green.length = 8;
  1066. var->blue.offset = 0;
  1067. var->blue.length = 8;
  1068. var->transp.offset = 0;
  1069. var->transp.length = 0;
  1070. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  1071. var->bits_per_pixel = 32;
  1072. var->red.offset = 16;
  1073. var->red.length = 8;
  1074. var->green.offset = 8;
  1075. var->green.length = 8;
  1076. var->blue.offset = 0;
  1077. var->blue.length = 8;
  1078. var->transp.offset = 24;
  1079. var->transp.length = 8;
  1080. } else
  1081. return -EINVAL;
  1082. var->red.msb_right = 0;
  1083. var->green.msb_right = 0;
  1084. var->blue.msb_right = 0;
  1085. var->transp.msb_right = 0;
  1086. }
  1087. /* Make sure we don't exceed our allocated memory. */
  1088. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  1089. info->fix.smem_len)
  1090. return -EINVAL;
  1091. return 0;
  1092. }
  1093. /* -----------------------------------------------------------------------------
  1094. * Frame buffer operations - Overlays
  1095. */
  1096. static ssize_t
  1097. overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
  1098. {
  1099. struct fb_info *info = dev_get_drvdata(dev);
  1100. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1101. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
  1102. }
  1103. static ssize_t
  1104. overlay_alpha_store(struct device *dev, struct device_attribute *attr,
  1105. const char *buf, size_t count)
  1106. {
  1107. struct fb_info *info = dev_get_drvdata(dev);
  1108. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1109. unsigned int alpha;
  1110. char *endp;
  1111. alpha = simple_strtoul(buf, &endp, 10);
  1112. if (isspace(*endp))
  1113. endp++;
  1114. if (endp - buf != count)
  1115. return -EINVAL;
  1116. if (alpha > 255)
  1117. return -EINVAL;
  1118. if (ovl->alpha != alpha) {
  1119. ovl->alpha = alpha;
  1120. if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
  1121. sh_mobile_lcdc_overlay_setup(ovl);
  1122. }
  1123. return count;
  1124. }
  1125. static ssize_t
  1126. overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1127. {
  1128. struct fb_info *info = dev_get_drvdata(dev);
  1129. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1130. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
  1131. }
  1132. static ssize_t
  1133. overlay_mode_store(struct device *dev, struct device_attribute *attr,
  1134. const char *buf, size_t count)
  1135. {
  1136. struct fb_info *info = dev_get_drvdata(dev);
  1137. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1138. unsigned int mode;
  1139. char *endp;
  1140. mode = simple_strtoul(buf, &endp, 10);
  1141. if (isspace(*endp))
  1142. endp++;
  1143. if (endp - buf != count)
  1144. return -EINVAL;
  1145. if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
  1146. return -EINVAL;
  1147. if (ovl->mode != mode) {
  1148. ovl->mode = mode;
  1149. if (ovl->enabled)
  1150. sh_mobile_lcdc_overlay_setup(ovl);
  1151. }
  1152. return count;
  1153. }
  1154. static ssize_t
  1155. overlay_position_show(struct device *dev, struct device_attribute *attr,
  1156. char *buf)
  1157. {
  1158. struct fb_info *info = dev_get_drvdata(dev);
  1159. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1160. return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
  1161. }
  1162. static ssize_t
  1163. overlay_position_store(struct device *dev, struct device_attribute *attr,
  1164. const char *buf, size_t count)
  1165. {
  1166. struct fb_info *info = dev_get_drvdata(dev);
  1167. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1168. char *endp;
  1169. int pos_x;
  1170. int pos_y;
  1171. pos_x = simple_strtol(buf, &endp, 10);
  1172. if (*endp != ',')
  1173. return -EINVAL;
  1174. pos_y = simple_strtol(endp + 1, &endp, 10);
  1175. if (isspace(*endp))
  1176. endp++;
  1177. if (endp - buf != count)
  1178. return -EINVAL;
  1179. if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
  1180. ovl->pos_x = pos_x;
  1181. ovl->pos_y = pos_y;
  1182. if (ovl->enabled)
  1183. sh_mobile_lcdc_overlay_setup(ovl);
  1184. }
  1185. return count;
  1186. }
  1187. static ssize_t
  1188. overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
  1189. {
  1190. struct fb_info *info = dev_get_drvdata(dev);
  1191. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1192. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
  1193. }
  1194. static ssize_t
  1195. overlay_rop3_store(struct device *dev, struct device_attribute *attr,
  1196. const char *buf, size_t count)
  1197. {
  1198. struct fb_info *info = dev_get_drvdata(dev);
  1199. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1200. unsigned int rop3;
  1201. char *endp;
  1202. rop3 = simple_strtoul(buf, &endp, 10);
  1203. if (isspace(*endp))
  1204. endp++;
  1205. if (endp - buf != count)
  1206. return -EINVAL;
  1207. if (rop3 > 255)
  1208. return -EINVAL;
  1209. if (ovl->rop3 != rop3) {
  1210. ovl->rop3 = rop3;
  1211. if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
  1212. sh_mobile_lcdc_overlay_setup(ovl);
  1213. }
  1214. return count;
  1215. }
  1216. static const struct device_attribute overlay_sysfs_attrs[] = {
  1217. __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
  1218. overlay_alpha_show, overlay_alpha_store),
  1219. __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
  1220. overlay_mode_show, overlay_mode_store),
  1221. __ATTR(ovl_position, S_IRUGO|S_IWUSR,
  1222. overlay_position_show, overlay_position_store),
  1223. __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
  1224. overlay_rop3_show, overlay_rop3_store),
  1225. };
  1226. static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
  1227. .id = "SH Mobile LCDC",
  1228. .type = FB_TYPE_PACKED_PIXELS,
  1229. .visual = FB_VISUAL_TRUECOLOR,
  1230. .accel = FB_ACCEL_NONE,
  1231. .xpanstep = 1,
  1232. .ypanstep = 1,
  1233. .ywrapstep = 0,
  1234. .capabilities = FB_CAP_FOURCC,
  1235. };
  1236. static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
  1237. struct fb_info *info)
  1238. {
  1239. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1240. unsigned long base_addr_y;
  1241. unsigned long base_addr_c;
  1242. unsigned long y_offset;
  1243. unsigned long c_offset;
  1244. if (!ovl->format->yuv) {
  1245. y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
  1246. * ovl->format->bpp / 8;
  1247. c_offset = 0;
  1248. } else {
  1249. unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
  1250. unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
  1251. y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
  1252. c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
  1253. + var->xoffset * 2 / xsub;
  1254. }
  1255. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1256. * nothing to do in that case.
  1257. */
  1258. if (y_offset == ovl->pan_y_offset)
  1259. return 0;
  1260. /* Set the source address for the next refresh */
  1261. base_addr_y = ovl->dma_handle + y_offset;
  1262. base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
  1263. + c_offset;
  1264. ovl->base_addr_y = base_addr_y;
  1265. ovl->base_addr_c = base_addr_c;
  1266. ovl->pan_y_offset = y_offset;
  1267. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  1268. lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
  1269. lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
  1270. lcdc_write(ovl->channel->lcdc, LDBCR,
  1271. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  1272. return 0;
  1273. }
  1274. static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
  1275. unsigned long arg)
  1276. {
  1277. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1278. switch (cmd) {
  1279. case FBIO_WAITFORVSYNC:
  1280. return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
  1281. default:
  1282. return -ENOIOCTLCMD;
  1283. }
  1284. }
  1285. static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
  1286. struct fb_info *info)
  1287. {
  1288. return __sh_mobile_lcdc_check_var(var, info);
  1289. }
  1290. static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
  1291. {
  1292. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1293. ovl->format =
  1294. sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1295. ovl->xres = info->var.xres;
  1296. ovl->xres_virtual = info->var.xres_virtual;
  1297. ovl->yres = info->var.yres;
  1298. ovl->yres_virtual = info->var.yres_virtual;
  1299. if (ovl->format->yuv)
  1300. ovl->pitch = info->var.xres_virtual;
  1301. else
  1302. ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
  1303. sh_mobile_lcdc_overlay_setup(ovl);
  1304. info->fix.line_length = ovl->pitch;
  1305. if (sh_mobile_format_is_fourcc(&info->var)) {
  1306. info->fix.type = FB_TYPE_FOURCC;
  1307. info->fix.visual = FB_VISUAL_FOURCC;
  1308. } else {
  1309. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1310. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1311. }
  1312. return 0;
  1313. }
  1314. /* Overlay blanking. Disable the overlay when blanked. */
  1315. static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
  1316. {
  1317. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1318. ovl->enabled = !blank;
  1319. sh_mobile_lcdc_overlay_setup(ovl);
  1320. /* Prevent the backlight from receiving a blanking event by returning
  1321. * a non-zero value.
  1322. */
  1323. return 1;
  1324. }
  1325. static int
  1326. sh_mobile_lcdc_overlay_mmap(struct fb_info *info, struct vm_area_struct *vma)
  1327. {
  1328. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1329. return dma_mmap_coherent(ovl->channel->lcdc->dev, vma, ovl->fb_mem,
  1330. ovl->dma_handle, ovl->fb_size);
  1331. }
  1332. static struct fb_ops sh_mobile_lcdc_overlay_ops = {
  1333. .owner = THIS_MODULE,
  1334. .fb_read = fb_sys_read,
  1335. .fb_write = fb_sys_write,
  1336. .fb_fillrect = sys_fillrect,
  1337. .fb_copyarea = sys_copyarea,
  1338. .fb_imageblit = sys_imageblit,
  1339. .fb_blank = sh_mobile_lcdc_overlay_blank,
  1340. .fb_pan_display = sh_mobile_lcdc_overlay_pan,
  1341. .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
  1342. .fb_check_var = sh_mobile_lcdc_overlay_check_var,
  1343. .fb_set_par = sh_mobile_lcdc_overlay_set_par,
  1344. .fb_mmap = sh_mobile_lcdc_overlay_mmap,
  1345. };
  1346. static void
  1347. sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
  1348. {
  1349. struct fb_info *info = ovl->info;
  1350. if (info == NULL || info->dev == NULL)
  1351. return;
  1352. unregister_framebuffer(ovl->info);
  1353. }
  1354. static int
  1355. sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
  1356. {
  1357. struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
  1358. struct fb_info *info = ovl->info;
  1359. unsigned int i;
  1360. int ret;
  1361. if (info == NULL)
  1362. return 0;
  1363. ret = register_framebuffer(info);
  1364. if (ret < 0)
  1365. return ret;
  1366. dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
  1367. dev_name(lcdc->dev), ovl->index, info->var.xres,
  1368. info->var.yres, info->var.bits_per_pixel);
  1369. for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
  1370. ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
  1371. if (ret < 0)
  1372. return ret;
  1373. }
  1374. return 0;
  1375. }
  1376. static void
  1377. sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
  1378. {
  1379. struct fb_info *info = ovl->info;
  1380. if (info == NULL || info->device == NULL)
  1381. return;
  1382. framebuffer_release(info);
  1383. }
  1384. static int
  1385. sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
  1386. {
  1387. struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
  1388. struct fb_var_screeninfo *var;
  1389. struct fb_info *info;
  1390. /* Allocate and initialize the frame buffer device. */
  1391. info = framebuffer_alloc(0, priv->dev);
  1392. if (info == NULL) {
  1393. dev_err(priv->dev, "unable to allocate fb_info\n");
  1394. return -ENOMEM;
  1395. }
  1396. ovl->info = info;
  1397. info->flags = FBINFO_FLAG_DEFAULT;
  1398. info->fbops = &sh_mobile_lcdc_overlay_ops;
  1399. info->device = priv->dev;
  1400. info->screen_base = ovl->fb_mem;
  1401. info->par = ovl;
  1402. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1403. * for NV12 and NV21.
  1404. */
  1405. info->fix = sh_mobile_lcdc_overlay_fix;
  1406. snprintf(info->fix.id, sizeof(info->fix.id),
  1407. "SH Mobile LCDC Overlay %u", ovl->index);
  1408. info->fix.smem_start = ovl->dma_handle;
  1409. info->fix.smem_len = ovl->fb_size;
  1410. info->fix.line_length = ovl->pitch;
  1411. if (ovl->format->yuv)
  1412. info->fix.visual = FB_VISUAL_FOURCC;
  1413. else
  1414. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1415. switch (ovl->format->fourcc) {
  1416. case V4L2_PIX_FMT_NV12:
  1417. case V4L2_PIX_FMT_NV21:
  1418. info->fix.ypanstep = 2;
  1419. case V4L2_PIX_FMT_NV16:
  1420. case V4L2_PIX_FMT_NV61:
  1421. info->fix.xpanstep = 2;
  1422. }
  1423. /* Initialize variable screen information. */
  1424. var = &info->var;
  1425. memset(var, 0, sizeof(*var));
  1426. var->xres = ovl->xres;
  1427. var->yres = ovl->yres;
  1428. var->xres_virtual = ovl->xres_virtual;
  1429. var->yres_virtual = ovl->yres_virtual;
  1430. var->activate = FB_ACTIVATE_NOW;
  1431. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1432. * for YUV formats.
  1433. */
  1434. if (!ovl->format->yuv)
  1435. var->bits_per_pixel = ovl->format->bpp;
  1436. else
  1437. var->grayscale = ovl->format->fourcc;
  1438. return sh_mobile_lcdc_overlay_check_var(var, info);
  1439. }
  1440. /* -----------------------------------------------------------------------------
  1441. * Frame buffer operations - main frame buffer
  1442. */
  1443. static int sh_mobile_lcdc_setcolreg(u_int regno,
  1444. u_int red, u_int green, u_int blue,
  1445. u_int transp, struct fb_info *info)
  1446. {
  1447. u32 *palette = info->pseudo_palette;
  1448. if (regno >= PALETTE_NR)
  1449. return -EINVAL;
  1450. /* only FB_VISUAL_TRUECOLOR supported */
  1451. red >>= 16 - info->var.red.length;
  1452. green >>= 16 - info->var.green.length;
  1453. blue >>= 16 - info->var.blue.length;
  1454. transp >>= 16 - info->var.transp.length;
  1455. palette[regno] = (red << info->var.red.offset) |
  1456. (green << info->var.green.offset) |
  1457. (blue << info->var.blue.offset) |
  1458. (transp << info->var.transp.offset);
  1459. return 0;
  1460. }
  1461. static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  1462. .id = "SH Mobile LCDC",
  1463. .type = FB_TYPE_PACKED_PIXELS,
  1464. .visual = FB_VISUAL_TRUECOLOR,
  1465. .accel = FB_ACCEL_NONE,
  1466. .xpanstep = 1,
  1467. .ypanstep = 1,
  1468. .ywrapstep = 0,
  1469. .capabilities = FB_CAP_FOURCC,
  1470. };
  1471. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  1472. const struct fb_fillrect *rect)
  1473. {
  1474. sys_fillrect(info, rect);
  1475. sh_mobile_lcdc_deferred_io_touch(info);
  1476. }
  1477. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  1478. const struct fb_copyarea *area)
  1479. {
  1480. sys_copyarea(info, area);
  1481. sh_mobile_lcdc_deferred_io_touch(info);
  1482. }
  1483. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  1484. const struct fb_image *image)
  1485. {
  1486. sys_imageblit(info, image);
  1487. sh_mobile_lcdc_deferred_io_touch(info);
  1488. }
  1489. static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
  1490. struct fb_info *info)
  1491. {
  1492. struct sh_mobile_lcdc_chan *ch = info->par;
  1493. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1494. unsigned long ldrcntr;
  1495. unsigned long base_addr_y, base_addr_c;
  1496. unsigned long y_offset;
  1497. unsigned long c_offset;
  1498. if (!ch->format->yuv) {
  1499. y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
  1500. * ch->format->bpp / 8;
  1501. c_offset = 0;
  1502. } else {
  1503. unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
  1504. unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
  1505. y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
  1506. c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
  1507. + var->xoffset * 2 / xsub;
  1508. }
  1509. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1510. * nothing to do in that case.
  1511. */
  1512. if (y_offset == ch->pan_y_offset)
  1513. return 0;
  1514. /* Set the source address for the next refresh */
  1515. base_addr_y = ch->dma_handle + y_offset;
  1516. base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
  1517. + c_offset;
  1518. ch->base_addr_y = base_addr_y;
  1519. ch->base_addr_c = base_addr_c;
  1520. ch->pan_y_offset = y_offset;
  1521. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  1522. if (ch->format->yuv)
  1523. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  1524. ldrcntr = lcdc_read(priv, _LDRCNTR);
  1525. if (lcdc_chan_is_sublcd(ch))
  1526. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  1527. else
  1528. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  1529. sh_mobile_lcdc_deferred_io_touch(info);
  1530. return 0;
  1531. }
  1532. static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
  1533. unsigned long arg)
  1534. {
  1535. struct sh_mobile_lcdc_chan *ch = info->par;
  1536. int retval;
  1537. switch (cmd) {
  1538. case FBIO_WAITFORVSYNC:
  1539. retval = sh_mobile_lcdc_wait_for_vsync(ch);
  1540. break;
  1541. default:
  1542. retval = -ENOIOCTLCMD;
  1543. break;
  1544. }
  1545. return retval;
  1546. }
  1547. static void sh_mobile_fb_reconfig(struct fb_info *info)
  1548. {
  1549. struct sh_mobile_lcdc_chan *ch = info->par;
  1550. struct fb_var_screeninfo var;
  1551. struct fb_videomode mode;
  1552. struct fb_event event;
  1553. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  1554. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  1555. /* More framebuffer users are active */
  1556. return;
  1557. fb_var_to_videomode(&mode, &info->var);
  1558. if (fb_mode_is_equal(&ch->display.mode, &mode))
  1559. return;
  1560. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  1561. var = info->var;
  1562. fb_videomode_to_var(&var, &ch->display.mode);
  1563. var.width = ch->display.width;
  1564. var.height = ch->display.height;
  1565. var.activate = FB_ACTIVATE_NOW;
  1566. if (fb_set_var(info, &var) < 0)
  1567. /* Couldn't reconfigure, hopefully, can continue as before */
  1568. return;
  1569. /*
  1570. * fb_set_var() calls the notifier change internally, only if
  1571. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  1572. * user event, we have to call the chain ourselves.
  1573. */
  1574. event.info = info;
  1575. event.data = &ch->display.mode;
  1576. fb_notifier_call_chain(evnt, &event);
  1577. }
  1578. /*
  1579. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  1580. * user == 1, or with console sem held, if user == 0.
  1581. */
  1582. static int sh_mobile_lcdc_release(struct fb_info *info, int user)
  1583. {
  1584. struct sh_mobile_lcdc_chan *ch = info->par;
  1585. mutex_lock(&ch->open_lock);
  1586. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1587. ch->use_count--;
  1588. /* Nothing to reconfigure, when called from fbcon */
  1589. if (user) {
  1590. console_lock();
  1591. sh_mobile_fb_reconfig(info);
  1592. console_unlock();
  1593. }
  1594. mutex_unlock(&ch->open_lock);
  1595. return 0;
  1596. }
  1597. static int sh_mobile_lcdc_open(struct fb_info *info, int user)
  1598. {
  1599. struct sh_mobile_lcdc_chan *ch = info->par;
  1600. mutex_lock(&ch->open_lock);
  1601. ch->use_count++;
  1602. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1603. mutex_unlock(&ch->open_lock);
  1604. return 0;
  1605. }
  1606. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1607. struct fb_info *info)
  1608. {
  1609. struct sh_mobile_lcdc_chan *ch = info->par;
  1610. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1611. unsigned int best_dist = (unsigned int)-1;
  1612. unsigned int best_xres = 0;
  1613. unsigned int best_yres = 0;
  1614. unsigned int i;
  1615. int ret;
  1616. /* If board code provides us with a list of available modes, make sure
  1617. * we use one of them. Find the mode closest to the requested one. The
  1618. * distance between two modes is defined as the size of the
  1619. * non-overlapping parts of the two rectangles.
  1620. */
  1621. for (i = 0; i < ch->cfg->num_modes; ++i) {
  1622. const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
  1623. unsigned int dist;
  1624. /* We can only round up. */
  1625. if (var->xres > mode->xres || var->yres > mode->yres)
  1626. continue;
  1627. dist = var->xres * var->yres + mode->xres * mode->yres
  1628. - 2 * min(var->xres, mode->xres)
  1629. * min(var->yres, mode->yres);
  1630. if (dist < best_dist) {
  1631. best_xres = mode->xres;
  1632. best_yres = mode->yres;
  1633. best_dist = dist;
  1634. }
  1635. }
  1636. /* If no available mode can be used, return an error. */
  1637. if (ch->cfg->num_modes != 0) {
  1638. if (best_dist == (unsigned int)-1)
  1639. return -EINVAL;
  1640. var->xres = best_xres;
  1641. var->yres = best_yres;
  1642. }
  1643. ret = __sh_mobile_lcdc_check_var(var, info);
  1644. if (ret < 0)
  1645. return ret;
  1646. /* only accept the forced_fourcc for dual channel configurations */
  1647. if (p->forced_fourcc &&
  1648. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1649. return -EINVAL;
  1650. return 0;
  1651. }
  1652. static int sh_mobile_lcdc_set_par(struct fb_info *info)
  1653. {
  1654. struct sh_mobile_lcdc_chan *ch = info->par;
  1655. int ret;
  1656. sh_mobile_lcdc_stop(ch->lcdc);
  1657. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1658. ch->colorspace = info->var.colorspace;
  1659. ch->xres = info->var.xres;
  1660. ch->xres_virtual = info->var.xres_virtual;
  1661. ch->yres = info->var.yres;
  1662. ch->yres_virtual = info->var.yres_virtual;
  1663. if (ch->format->yuv)
  1664. ch->pitch = info->var.xres_virtual;
  1665. else
  1666. ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
  1667. ret = sh_mobile_lcdc_start(ch->lcdc);
  1668. if (ret < 0)
  1669. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1670. info->fix.line_length = ch->pitch;
  1671. if (sh_mobile_format_is_fourcc(&info->var)) {
  1672. info->fix.type = FB_TYPE_FOURCC;
  1673. info->fix.visual = FB_VISUAL_FOURCC;
  1674. } else {
  1675. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1676. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1677. }
  1678. return ret;
  1679. }
  1680. /*
  1681. * Screen blanking. Behavior is as follows:
  1682. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1683. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1684. * FB_BLANK_VSYNC,
  1685. * FB_BLANK_HSYNC,
  1686. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1687. */
  1688. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1689. {
  1690. struct sh_mobile_lcdc_chan *ch = info->par;
  1691. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1692. /* blank the screen? */
  1693. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1694. struct fb_fillrect rect = {
  1695. .width = ch->xres,
  1696. .height = ch->yres,
  1697. };
  1698. sh_mobile_lcdc_fillrect(info, &rect);
  1699. }
  1700. /* turn clocks on? */
  1701. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1702. sh_mobile_lcdc_clk_on(p);
  1703. }
  1704. /* turn clocks off? */
  1705. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1706. /* make sure the screen is updated with the black fill before
  1707. * switching the clocks off. one vsync is not enough since
  1708. * blanking may occur in the middle of a refresh. deferred io
  1709. * mode will reenable the clocks and update the screen in time,
  1710. * so it does not need this. */
  1711. if (!info->fbdefio) {
  1712. sh_mobile_lcdc_wait_for_vsync(ch);
  1713. sh_mobile_lcdc_wait_for_vsync(ch);
  1714. }
  1715. sh_mobile_lcdc_clk_off(p);
  1716. }
  1717. ch->blank_status = blank;
  1718. return 0;
  1719. }
  1720. static int
  1721. sh_mobile_lcdc_mmap(struct fb_info *info, struct vm_area_struct *vma)
  1722. {
  1723. struct sh_mobile_lcdc_chan *ch = info->par;
  1724. return dma_mmap_coherent(ch->lcdc->dev, vma, ch->fb_mem,
  1725. ch->dma_handle, ch->fb_size);
  1726. }
  1727. static struct fb_ops sh_mobile_lcdc_ops = {
  1728. .owner = THIS_MODULE,
  1729. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1730. .fb_read = fb_sys_read,
  1731. .fb_write = fb_sys_write,
  1732. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1733. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1734. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1735. .fb_blank = sh_mobile_lcdc_blank,
  1736. .fb_pan_display = sh_mobile_lcdc_pan,
  1737. .fb_ioctl = sh_mobile_lcdc_ioctl,
  1738. .fb_open = sh_mobile_lcdc_open,
  1739. .fb_release = sh_mobile_lcdc_release,
  1740. .fb_check_var = sh_mobile_lcdc_check_var,
  1741. .fb_set_par = sh_mobile_lcdc_set_par,
  1742. .fb_mmap = sh_mobile_lcdc_mmap,
  1743. };
  1744. static void
  1745. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1746. {
  1747. if (ch->info && ch->info->dev)
  1748. unregister_framebuffer(ch->info);
  1749. }
  1750. static int
  1751. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1752. {
  1753. struct fb_info *info = ch->info;
  1754. int ret;
  1755. if (info->fbdefio) {
  1756. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1757. ch->fb_size >> PAGE_SHIFT);
  1758. if (!ch->sglist)
  1759. return -ENOMEM;
  1760. }
  1761. info->bl_dev = ch->bl;
  1762. ret = register_framebuffer(info);
  1763. if (ret < 0)
  1764. return ret;
  1765. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1766. dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
  1767. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1768. info->var.bits_per_pixel);
  1769. /* deferred io mode: disable clock to save power */
  1770. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1771. sh_mobile_lcdc_clk_off(ch->lcdc);
  1772. return ret;
  1773. }
  1774. static void
  1775. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1776. {
  1777. struct fb_info *info = ch->info;
  1778. if (!info || !info->device)
  1779. return;
  1780. vfree(ch->sglist);
  1781. fb_dealloc_cmap(&info->cmap);
  1782. framebuffer_release(info);
  1783. }
  1784. static int
  1785. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1786. const struct fb_videomode *modes,
  1787. unsigned int num_modes)
  1788. {
  1789. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1790. struct fb_var_screeninfo *var;
  1791. struct fb_info *info;
  1792. int ret;
  1793. /* Allocate and initialize the frame buffer device. Create the modes
  1794. * list and allocate the color map.
  1795. */
  1796. info = framebuffer_alloc(0, priv->dev);
  1797. if (info == NULL) {
  1798. dev_err(priv->dev, "unable to allocate fb_info\n");
  1799. return -ENOMEM;
  1800. }
  1801. ch->info = info;
  1802. info->flags = FBINFO_FLAG_DEFAULT;
  1803. info->fbops = &sh_mobile_lcdc_ops;
  1804. info->device = priv->dev;
  1805. info->screen_base = ch->fb_mem;
  1806. info->pseudo_palette = &ch->pseudo_palette;
  1807. info->par = ch;
  1808. fb_videomode_to_modelist(modes, num_modes, &info->modelist);
  1809. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1810. if (ret < 0) {
  1811. dev_err(priv->dev, "unable to allocate cmap\n");
  1812. return ret;
  1813. }
  1814. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1815. * for NV12 and NV21.
  1816. */
  1817. info->fix = sh_mobile_lcdc_fix;
  1818. info->fix.smem_start = ch->dma_handle;
  1819. info->fix.smem_len = ch->fb_size;
  1820. info->fix.line_length = ch->pitch;
  1821. if (ch->format->yuv)
  1822. info->fix.visual = FB_VISUAL_FOURCC;
  1823. else
  1824. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1825. switch (ch->format->fourcc) {
  1826. case V4L2_PIX_FMT_NV12:
  1827. case V4L2_PIX_FMT_NV21:
  1828. info->fix.ypanstep = 2;
  1829. case V4L2_PIX_FMT_NV16:
  1830. case V4L2_PIX_FMT_NV61:
  1831. info->fix.xpanstep = 2;
  1832. }
  1833. /* Initialize variable screen information using the first mode as
  1834. * default.
  1835. */
  1836. var = &info->var;
  1837. fb_videomode_to_var(var, modes);
  1838. var->width = ch->display.width;
  1839. var->height = ch->display.height;
  1840. var->xres_virtual = ch->xres_virtual;
  1841. var->yres_virtual = ch->yres_virtual;
  1842. var->activate = FB_ACTIVATE_NOW;
  1843. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1844. * for YUV formats.
  1845. */
  1846. if (!ch->format->yuv)
  1847. var->bits_per_pixel = ch->format->bpp;
  1848. else
  1849. var->grayscale = ch->format->fourcc;
  1850. ret = sh_mobile_lcdc_check_var(var, info);
  1851. if (ret)
  1852. return ret;
  1853. return 0;
  1854. }
  1855. /* -----------------------------------------------------------------------------
  1856. * Backlight
  1857. */
  1858. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1859. {
  1860. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1861. int brightness = bdev->props.brightness;
  1862. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1863. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1864. brightness = 0;
  1865. ch->bl_brightness = brightness;
  1866. return ch->cfg->bl_info.set_brightness(brightness);
  1867. }
  1868. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1869. {
  1870. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1871. return ch->bl_brightness;
  1872. }
  1873. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1874. struct fb_info *info)
  1875. {
  1876. return (info->bl_dev == bdev);
  1877. }
  1878. static const struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1879. .options = BL_CORE_SUSPENDRESUME,
  1880. .update_status = sh_mobile_lcdc_update_bl,
  1881. .get_brightness = sh_mobile_lcdc_get_brightness,
  1882. .check_fb = sh_mobile_lcdc_check_fb,
  1883. };
  1884. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1885. struct sh_mobile_lcdc_chan *ch)
  1886. {
  1887. struct backlight_device *bl;
  1888. bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
  1889. &sh_mobile_lcdc_bl_ops, NULL);
  1890. if (IS_ERR(bl)) {
  1891. dev_err(parent, "unable to register backlight device: %ld\n",
  1892. PTR_ERR(bl));
  1893. return NULL;
  1894. }
  1895. bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
  1896. bl->props.brightness = bl->props.max_brightness;
  1897. backlight_update_status(bl);
  1898. return bl;
  1899. }
  1900. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1901. {
  1902. backlight_device_unregister(bdev);
  1903. }
  1904. /* -----------------------------------------------------------------------------
  1905. * Power management
  1906. */
  1907. static int sh_mobile_lcdc_suspend(struct device *dev)
  1908. {
  1909. struct platform_device *pdev = to_platform_device(dev);
  1910. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1911. return 0;
  1912. }
  1913. static int sh_mobile_lcdc_resume(struct device *dev)
  1914. {
  1915. struct platform_device *pdev = to_platform_device(dev);
  1916. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1917. }
  1918. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1919. {
  1920. struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
  1921. /* turn off LCDC hardware */
  1922. lcdc_write(priv, _LDCNT1R, 0);
  1923. return 0;
  1924. }
  1925. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1926. {
  1927. struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
  1928. __sh_mobile_lcdc_start(priv);
  1929. return 0;
  1930. }
  1931. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1932. .suspend = sh_mobile_lcdc_suspend,
  1933. .resume = sh_mobile_lcdc_resume,
  1934. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1935. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1936. };
  1937. /* -----------------------------------------------------------------------------
  1938. * Framebuffer notifier
  1939. */
  1940. /* locking: called with info->lock held */
  1941. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1942. unsigned long action, void *data)
  1943. {
  1944. struct fb_event *event = data;
  1945. struct fb_info *info = event->info;
  1946. struct sh_mobile_lcdc_chan *ch = info->par;
  1947. if (&ch->lcdc->notifier != nb)
  1948. return NOTIFY_DONE;
  1949. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1950. __func__, action, event->data);
  1951. switch(action) {
  1952. case FB_EVENT_SUSPEND:
  1953. sh_mobile_lcdc_display_off(ch);
  1954. sh_mobile_lcdc_stop(ch->lcdc);
  1955. break;
  1956. case FB_EVENT_RESUME:
  1957. mutex_lock(&ch->open_lock);
  1958. sh_mobile_fb_reconfig(info);
  1959. mutex_unlock(&ch->open_lock);
  1960. sh_mobile_lcdc_display_on(ch);
  1961. sh_mobile_lcdc_start(ch->lcdc);
  1962. }
  1963. return NOTIFY_OK;
  1964. }
  1965. /* -----------------------------------------------------------------------------
  1966. * Probe/remove and driver init/exit
  1967. */
  1968. static const struct fb_videomode default_720p = {
  1969. .name = "HDMI 720p",
  1970. .xres = 1280,
  1971. .yres = 720,
  1972. .left_margin = 220,
  1973. .right_margin = 110,
  1974. .hsync_len = 40,
  1975. .upper_margin = 20,
  1976. .lower_margin = 5,
  1977. .vsync_len = 5,
  1978. .pixclock = 13468,
  1979. .refresh = 60,
  1980. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  1981. };
  1982. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1983. {
  1984. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1985. unsigned int i;
  1986. fb_unregister_client(&priv->notifier);
  1987. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
  1988. sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
  1989. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1990. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  1991. sh_mobile_lcdc_stop(priv);
  1992. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
  1993. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  1994. sh_mobile_lcdc_overlay_fb_cleanup(ovl);
  1995. if (ovl->fb_mem)
  1996. dma_free_coherent(&pdev->dev, ovl->fb_size,
  1997. ovl->fb_mem, ovl->dma_handle);
  1998. }
  1999. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2000. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2001. if (ch->tx_dev) {
  2002. ch->tx_dev->lcdc = NULL;
  2003. module_put(ch->cfg->tx_dev->dev.driver->owner);
  2004. }
  2005. sh_mobile_lcdc_channel_fb_cleanup(ch);
  2006. if (ch->fb_mem)
  2007. dma_free_coherent(&pdev->dev, ch->fb_size,
  2008. ch->fb_mem, ch->dma_handle);
  2009. }
  2010. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2011. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2012. if (ch->bl)
  2013. sh_mobile_lcdc_bl_remove(ch->bl);
  2014. mutex_destroy(&ch->open_lock);
  2015. }
  2016. if (priv->dot_clk) {
  2017. pm_runtime_disable(&pdev->dev);
  2018. clk_put(priv->dot_clk);
  2019. }
  2020. if (priv->base)
  2021. iounmap(priv->base);
  2022. if (priv->irq)
  2023. free_irq(priv->irq, priv);
  2024. kfree(priv);
  2025. return 0;
  2026. }
  2027. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  2028. {
  2029. int interface_type = ch->cfg->interface_type;
  2030. switch (interface_type) {
  2031. case RGB8:
  2032. case RGB9:
  2033. case RGB12A:
  2034. case RGB12B:
  2035. case RGB16:
  2036. case RGB18:
  2037. case RGB24:
  2038. case SYS8A:
  2039. case SYS8B:
  2040. case SYS8C:
  2041. case SYS8D:
  2042. case SYS9:
  2043. case SYS12:
  2044. case SYS16A:
  2045. case SYS16B:
  2046. case SYS16C:
  2047. case SYS18:
  2048. case SYS24:
  2049. break;
  2050. default:
  2051. return -EINVAL;
  2052. }
  2053. /* SUBLCD only supports SYS interface */
  2054. if (lcdc_chan_is_sublcd(ch)) {
  2055. if (!(interface_type & LDMT1R_IFM))
  2056. return -EINVAL;
  2057. interface_type &= ~LDMT1R_IFM;
  2058. }
  2059. ch->ldmt1r_value = interface_type;
  2060. return 0;
  2061. }
  2062. static int
  2063. sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay *ovl)
  2064. {
  2065. const struct sh_mobile_lcdc_format_info *format;
  2066. struct device *dev = ovl->channel->lcdc->dev;
  2067. int ret;
  2068. if (ovl->cfg->fourcc == 0)
  2069. return 0;
  2070. /* Validate the format. */
  2071. format = sh_mobile_format_info(ovl->cfg->fourcc);
  2072. if (format == NULL) {
  2073. dev_err(dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
  2074. return -EINVAL;
  2075. }
  2076. ovl->enabled = false;
  2077. ovl->mode = LCDC_OVERLAY_BLEND;
  2078. ovl->alpha = 255;
  2079. ovl->rop3 = 0;
  2080. ovl->pos_x = 0;
  2081. ovl->pos_y = 0;
  2082. /* The default Y virtual resolution is twice the panel size to allow for
  2083. * double-buffering.
  2084. */
  2085. ovl->format = format;
  2086. ovl->xres = ovl->cfg->max_xres;
  2087. ovl->xres_virtual = ovl->xres;
  2088. ovl->yres = ovl->cfg->max_yres;
  2089. ovl->yres_virtual = ovl->yres * 2;
  2090. if (!format->yuv)
  2091. ovl->pitch = ovl->xres_virtual * format->bpp / 8;
  2092. else
  2093. ovl->pitch = ovl->xres_virtual;
  2094. /* Allocate frame buffer memory. */
  2095. ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
  2096. * format->bpp / 8 * 2;
  2097. ovl->fb_mem = dma_alloc_coherent(dev, ovl->fb_size, &ovl->dma_handle,
  2098. GFP_KERNEL);
  2099. if (!ovl->fb_mem) {
  2100. dev_err(dev, "unable to allocate buffer\n");
  2101. return -ENOMEM;
  2102. }
  2103. ret = sh_mobile_lcdc_overlay_fb_init(ovl);
  2104. if (ret < 0)
  2105. return ret;
  2106. return 0;
  2107. }
  2108. static int
  2109. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch)
  2110. {
  2111. const struct sh_mobile_lcdc_format_info *format;
  2112. const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
  2113. struct device *dev = ch->lcdc->dev;
  2114. const struct fb_videomode *max_mode;
  2115. const struct fb_videomode *mode;
  2116. unsigned int num_modes;
  2117. unsigned int max_size;
  2118. unsigned int i;
  2119. ch->notify = sh_mobile_lcdc_display_notify;
  2120. /* Validate the format. */
  2121. format = sh_mobile_format_info(cfg->fourcc);
  2122. if (format == NULL) {
  2123. dev_err(dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  2124. return -EINVAL;
  2125. }
  2126. /* Iterate through the modes to validate them and find the highest
  2127. * resolution.
  2128. */
  2129. max_mode = NULL;
  2130. max_size = 0;
  2131. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  2132. unsigned int size = mode->yres * mode->xres;
  2133. /* NV12/NV21 buffers must have even number of lines */
  2134. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  2135. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  2136. dev_err(dev, "yres must be multiple of 2 for "
  2137. "YCbCr420 mode.\n");
  2138. return -EINVAL;
  2139. }
  2140. if (size > max_size) {
  2141. max_mode = mode;
  2142. max_size = size;
  2143. }
  2144. }
  2145. if (!max_size)
  2146. max_size = MAX_XRES * MAX_YRES;
  2147. else
  2148. dev_dbg(dev, "Found largest videomode %ux%u\n",
  2149. max_mode->xres, max_mode->yres);
  2150. if (cfg->lcd_modes == NULL) {
  2151. mode = &default_720p;
  2152. num_modes = 1;
  2153. } else {
  2154. mode = cfg->lcd_modes;
  2155. num_modes = cfg->num_modes;
  2156. }
  2157. /* Use the first mode as default. The default Y virtual resolution is
  2158. * twice the panel size to allow for double-buffering.
  2159. */
  2160. ch->format = format;
  2161. ch->xres = mode->xres;
  2162. ch->xres_virtual = mode->xres;
  2163. ch->yres = mode->yres;
  2164. ch->yres_virtual = mode->yres * 2;
  2165. if (!format->yuv) {
  2166. ch->colorspace = V4L2_COLORSPACE_SRGB;
  2167. ch->pitch = ch->xres_virtual * format->bpp / 8;
  2168. } else {
  2169. ch->colorspace = V4L2_COLORSPACE_REC709;
  2170. ch->pitch = ch->xres_virtual;
  2171. }
  2172. ch->display.width = cfg->panel_cfg.width;
  2173. ch->display.height = cfg->panel_cfg.height;
  2174. ch->display.mode = *mode;
  2175. /* Allocate frame buffer memory. */
  2176. ch->fb_size = max_size * format->bpp / 8 * 2;
  2177. ch->fb_mem = dma_alloc_coherent(dev, ch->fb_size, &ch->dma_handle,
  2178. GFP_KERNEL);
  2179. if (ch->fb_mem == NULL) {
  2180. dev_err(dev, "unable to allocate buffer\n");
  2181. return -ENOMEM;
  2182. }
  2183. /* Initialize the transmitter device if present. */
  2184. if (cfg->tx_dev) {
  2185. if (!cfg->tx_dev->dev.driver ||
  2186. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  2187. dev_warn(dev, "unable to get transmitter device\n");
  2188. return -EINVAL;
  2189. }
  2190. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  2191. ch->tx_dev->lcdc = ch;
  2192. ch->tx_dev->def_mode = *mode;
  2193. }
  2194. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  2195. }
  2196. static int sh_mobile_lcdc_probe(struct platform_device *pdev)
  2197. {
  2198. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  2199. struct sh_mobile_lcdc_priv *priv;
  2200. struct resource *res;
  2201. int num_channels;
  2202. int error;
  2203. int irq, i;
  2204. if (!pdata) {
  2205. dev_err(&pdev->dev, "no platform data defined\n");
  2206. return -EINVAL;
  2207. }
  2208. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2209. irq = platform_get_irq(pdev, 0);
  2210. if (!res || irq < 0) {
  2211. dev_err(&pdev->dev, "cannot get platform resources\n");
  2212. return -ENOENT;
  2213. }
  2214. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2215. if (!priv)
  2216. return -ENOMEM;
  2217. priv->dev = &pdev->dev;
  2218. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  2219. mutex_init(&priv->ch[i].open_lock);
  2220. platform_set_drvdata(pdev, priv);
  2221. error = request_irq(irq, sh_mobile_lcdc_irq, 0,
  2222. dev_name(&pdev->dev), priv);
  2223. if (error) {
  2224. dev_err(&pdev->dev, "unable to request irq\n");
  2225. goto err1;
  2226. }
  2227. priv->irq = irq;
  2228. atomic_set(&priv->hw_usecnt, -1);
  2229. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  2230. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  2231. ch->lcdc = priv;
  2232. ch->cfg = &pdata->ch[i];
  2233. error = sh_mobile_lcdc_check_interface(ch);
  2234. if (error) {
  2235. dev_err(&pdev->dev, "unsupported interface type\n");
  2236. goto err1;
  2237. }
  2238. init_waitqueue_head(&ch->frame_end_wait);
  2239. init_completion(&ch->vsync_completion);
  2240. /* probe the backlight is there is one defined */
  2241. if (ch->cfg->bl_info.max_brightness)
  2242. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  2243. switch (pdata->ch[i].chan) {
  2244. case LCDC_CHAN_MAINLCD:
  2245. ch->enabled = LDCNT2R_ME;
  2246. ch->reg_offs = lcdc_offs_mainlcd;
  2247. num_channels++;
  2248. break;
  2249. case LCDC_CHAN_SUBLCD:
  2250. ch->enabled = LDCNT2R_SE;
  2251. ch->reg_offs = lcdc_offs_sublcd;
  2252. num_channels++;
  2253. break;
  2254. }
  2255. }
  2256. if (!num_channels) {
  2257. dev_err(&pdev->dev, "no channels defined\n");
  2258. error = -EINVAL;
  2259. goto err1;
  2260. }
  2261. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  2262. if (num_channels == 2)
  2263. priv->forced_fourcc = pdata->ch[0].fourcc;
  2264. priv->base = ioremap_nocache(res->start, resource_size(res));
  2265. if (!priv->base) {
  2266. error = -ENOMEM;
  2267. goto err1;
  2268. }
  2269. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  2270. if (error) {
  2271. dev_err(&pdev->dev, "unable to setup clocks\n");
  2272. goto err1;
  2273. }
  2274. /* Enable runtime PM. */
  2275. pm_runtime_enable(&pdev->dev);
  2276. for (i = 0; i < num_channels; i++) {
  2277. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2278. error = sh_mobile_lcdc_channel_init(ch);
  2279. if (error)
  2280. goto err1;
  2281. }
  2282. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2283. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2284. ovl->cfg = &pdata->overlays[i];
  2285. ovl->channel = &priv->ch[0];
  2286. error = sh_mobile_lcdc_overlay_init(ovl);
  2287. if (error)
  2288. goto err1;
  2289. }
  2290. error = sh_mobile_lcdc_start(priv);
  2291. if (error) {
  2292. dev_err(&pdev->dev, "unable to start hardware\n");
  2293. goto err1;
  2294. }
  2295. for (i = 0; i < num_channels; i++) {
  2296. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2297. error = sh_mobile_lcdc_channel_fb_register(ch);
  2298. if (error)
  2299. goto err1;
  2300. }
  2301. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2302. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2303. error = sh_mobile_lcdc_overlay_fb_register(ovl);
  2304. if (error)
  2305. goto err1;
  2306. }
  2307. /* Failure ignored */
  2308. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  2309. fb_register_client(&priv->notifier);
  2310. return 0;
  2311. err1:
  2312. sh_mobile_lcdc_remove(pdev);
  2313. return error;
  2314. }
  2315. static struct platform_driver sh_mobile_lcdc_driver = {
  2316. .driver = {
  2317. .name = "sh_mobile_lcdc_fb",
  2318. .pm = &sh_mobile_lcdc_dev_pm_ops,
  2319. },
  2320. .probe = sh_mobile_lcdc_probe,
  2321. .remove = sh_mobile_lcdc_remove,
  2322. };
  2323. module_platform_driver(sh_mobile_lcdc_driver);
  2324. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  2325. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  2326. MODULE_LICENSE("GPL v2");