clps711x-fb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Cirrus Logic CLPS711X FB driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5. * Based on driver by Russell King <rmk@arm.linux.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/fb.h>
  14. #include <linux/io.h>
  15. #include <linux/lcd.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/mfd/syscon/clps711x.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <video/of_display_timing.h>
  24. #define CLPS711X_FB_NAME "clps711x-fb"
  25. #define CLPS711X_FB_BPP_MAX (4)
  26. /* Registers relative to LCDCON */
  27. #define CLPS711X_LCDCON (0x0000)
  28. # define LCDCON_GSEN BIT(30)
  29. # define LCDCON_GSMD BIT(31)
  30. #define CLPS711X_PALLSW (0x0280)
  31. #define CLPS711X_PALMSW (0x02c0)
  32. #define CLPS711X_FBADDR (0x0d40)
  33. struct clps711x_fb_info {
  34. struct clk *clk;
  35. void __iomem *base;
  36. struct regmap *syscon;
  37. resource_size_t buffsize;
  38. struct fb_videomode mode;
  39. struct regulator *lcd_pwr;
  40. u32 ac_prescale;
  41. bool cmap_invert;
  42. };
  43. static int clps711x_fb_setcolreg(u_int regno, u_int red, u_int green,
  44. u_int blue, u_int transp, struct fb_info *info)
  45. {
  46. struct clps711x_fb_info *cfb = info->par;
  47. u32 level, mask, shift;
  48. if (regno >= BIT(info->var.bits_per_pixel))
  49. return -EINVAL;
  50. shift = 4 * (regno & 7);
  51. mask = 0xf << shift;
  52. /* gray = 0.30*R + 0.58*G + 0.11*B */
  53. level = (((red * 77 + green * 151 + blue * 28) >> 20) << shift) & mask;
  54. if (cfb->cmap_invert)
  55. level = 0xf - level;
  56. regno = (regno < 8) ? CLPS711X_PALLSW : CLPS711X_PALMSW;
  57. writel((readl(cfb->base + regno) & ~mask) | level, cfb->base + regno);
  58. return 0;
  59. }
  60. static int clps711x_fb_check_var(struct fb_var_screeninfo *var,
  61. struct fb_info *info)
  62. {
  63. u32 val;
  64. if (var->bits_per_pixel < 1 ||
  65. var->bits_per_pixel > CLPS711X_FB_BPP_MAX)
  66. return -EINVAL;
  67. if (!var->pixclock)
  68. return -EINVAL;
  69. val = DIV_ROUND_UP(var->xres, 16) - 1;
  70. if (val < 0x01 || val > 0x3f)
  71. return -EINVAL;
  72. val = DIV_ROUND_UP(var->yres * var->xres * var->bits_per_pixel, 128);
  73. val--;
  74. if (val < 0x001 || val > 0x1fff)
  75. return -EINVAL;
  76. var->transp.msb_right = 0;
  77. var->transp.offset = 0;
  78. var->transp.length = 0;
  79. var->red.msb_right = 0;
  80. var->red.offset = 0;
  81. var->red.length = var->bits_per_pixel;
  82. var->green = var->red;
  83. var->blue = var->red;
  84. var->grayscale = var->bits_per_pixel > 1;
  85. return 0;
  86. }
  87. static int clps711x_fb_set_par(struct fb_info *info)
  88. {
  89. struct clps711x_fb_info *cfb = info->par;
  90. resource_size_t size;
  91. u32 lcdcon, pps;
  92. size = (info->var.xres * info->var.yres * info->var.bits_per_pixel) / 8;
  93. if (size > cfb->buffsize)
  94. return -EINVAL;
  95. switch (info->var.bits_per_pixel) {
  96. case 1:
  97. info->fix.visual = FB_VISUAL_MONO01;
  98. break;
  99. case 2:
  100. case 4:
  101. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. info->fix.line_length = info->var.xres * info->var.bits_per_pixel / 8;
  107. info->fix.smem_len = size;
  108. lcdcon = (info->var.xres * info->var.yres *
  109. info->var.bits_per_pixel) / 128 - 1;
  110. lcdcon |= ((info->var.xres / 16) - 1) << 13;
  111. lcdcon |= (cfb->ac_prescale & 0x1f) << 25;
  112. pps = clk_get_rate(cfb->clk) / (PICOS2KHZ(info->var.pixclock) * 1000);
  113. if (pps)
  114. pps--;
  115. lcdcon |= (pps & 0x3f) << 19;
  116. if (info->var.bits_per_pixel == 4)
  117. lcdcon |= LCDCON_GSMD;
  118. if (info->var.bits_per_pixel >= 2)
  119. lcdcon |= LCDCON_GSEN;
  120. /* LCDCON must only be changed while the LCD is disabled */
  121. regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
  122. writel(lcdcon, cfb->base + CLPS711X_LCDCON);
  123. regmap_update_bits(cfb->syscon, SYSCON_OFFSET,
  124. SYSCON1_LCDEN, SYSCON1_LCDEN);
  125. return 0;
  126. }
  127. static int clps711x_fb_blank(int blank, struct fb_info *info)
  128. {
  129. /* Return happy */
  130. return 0;
  131. }
  132. static struct fb_ops clps711x_fb_ops = {
  133. .owner = THIS_MODULE,
  134. .fb_setcolreg = clps711x_fb_setcolreg,
  135. .fb_check_var = clps711x_fb_check_var,
  136. .fb_set_par = clps711x_fb_set_par,
  137. .fb_blank = clps711x_fb_blank,
  138. .fb_fillrect = sys_fillrect,
  139. .fb_copyarea = sys_copyarea,
  140. .fb_imageblit = sys_imageblit,
  141. };
  142. static int clps711x_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
  143. {
  144. struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
  145. return (!fi || fi->par == cfb) ? 1 : 0;
  146. }
  147. static int clps711x_lcd_get_power(struct lcd_device *lcddev)
  148. {
  149. struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
  150. if (!IS_ERR_OR_NULL(cfb->lcd_pwr))
  151. if (!regulator_is_enabled(cfb->lcd_pwr))
  152. return FB_BLANK_NORMAL;
  153. return FB_BLANK_UNBLANK;
  154. }
  155. static int clps711x_lcd_set_power(struct lcd_device *lcddev, int blank)
  156. {
  157. struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
  158. if (!IS_ERR_OR_NULL(cfb->lcd_pwr)) {
  159. if (blank == FB_BLANK_UNBLANK) {
  160. if (!regulator_is_enabled(cfb->lcd_pwr))
  161. return regulator_enable(cfb->lcd_pwr);
  162. } else {
  163. if (regulator_is_enabled(cfb->lcd_pwr))
  164. return regulator_disable(cfb->lcd_pwr);
  165. }
  166. }
  167. return 0;
  168. }
  169. static struct lcd_ops clps711x_lcd_ops = {
  170. .check_fb = clps711x_lcd_check_fb,
  171. .get_power = clps711x_lcd_get_power,
  172. .set_power = clps711x_lcd_set_power,
  173. };
  174. static int clps711x_fb_probe(struct platform_device *pdev)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct device_node *disp, *np = dev->of_node;
  178. struct clps711x_fb_info *cfb;
  179. struct lcd_device *lcd;
  180. struct fb_info *info;
  181. struct resource *res;
  182. int ret = -ENOENT;
  183. u32 val;
  184. if (fb_get_options(CLPS711X_FB_NAME, NULL))
  185. return -ENODEV;
  186. info = framebuffer_alloc(sizeof(*cfb), dev);
  187. if (!info)
  188. return -ENOMEM;
  189. cfb = info->par;
  190. platform_set_drvdata(pdev, info);
  191. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. if (!res)
  193. goto out_fb_release;
  194. cfb->base = devm_ioremap(dev, res->start, resource_size(res));
  195. if (!cfb->base) {
  196. ret = -ENOMEM;
  197. goto out_fb_release;
  198. }
  199. info->fix.mmio_start = res->start;
  200. info->fix.mmio_len = resource_size(res);
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  202. info->screen_base = devm_ioremap_resource(dev, res);
  203. if (IS_ERR(info->screen_base)) {
  204. ret = PTR_ERR(info->screen_base);
  205. goto out_fb_release;
  206. }
  207. /* Physical address should be aligned to 256 MiB */
  208. if (res->start & 0x0fffffff) {
  209. ret = -EINVAL;
  210. goto out_fb_release;
  211. }
  212. info->apertures = alloc_apertures(1);
  213. if (!info->apertures) {
  214. ret = -ENOMEM;
  215. goto out_fb_release;
  216. }
  217. cfb->buffsize = resource_size(res);
  218. info->fix.smem_start = res->start;
  219. info->apertures->ranges[0].base = info->fix.smem_start;
  220. info->apertures->ranges[0].size = cfb->buffsize;
  221. cfb->clk = devm_clk_get(dev, NULL);
  222. if (IS_ERR(cfb->clk)) {
  223. ret = PTR_ERR(cfb->clk);
  224. goto out_fb_release;
  225. }
  226. cfb->syscon =
  227. syscon_regmap_lookup_by_compatible("cirrus,clps711x-syscon1");
  228. if (IS_ERR(cfb->syscon)) {
  229. ret = PTR_ERR(cfb->syscon);
  230. goto out_fb_release;
  231. }
  232. disp = of_parse_phandle(np, "display", 0);
  233. if (!disp) {
  234. dev_err(&pdev->dev, "No display defined\n");
  235. ret = -ENODATA;
  236. goto out_fb_release;
  237. }
  238. ret = of_get_fb_videomode(disp, &cfb->mode, OF_USE_NATIVE_MODE);
  239. if (ret)
  240. goto out_fb_release;
  241. of_property_read_u32(disp, "ac-prescale", &cfb->ac_prescale);
  242. cfb->cmap_invert = of_property_read_bool(disp, "cmap-invert");
  243. ret = of_property_read_u32(disp, "bits-per-pixel",
  244. &info->var.bits_per_pixel);
  245. if (ret)
  246. goto out_fb_release;
  247. /* Force disable LCD on any mismatch */
  248. if (info->fix.smem_start != (readb(cfb->base + CLPS711X_FBADDR) << 28))
  249. regmap_update_bits(cfb->syscon, SYSCON_OFFSET,
  250. SYSCON1_LCDEN, 0);
  251. ret = regmap_read(cfb->syscon, SYSCON_OFFSET, &val);
  252. if (ret)
  253. goto out_fb_release;
  254. if (!(val & SYSCON1_LCDEN)) {
  255. /* Setup start FB address */
  256. writeb(info->fix.smem_start >> 28, cfb->base + CLPS711X_FBADDR);
  257. /* Clean FB memory */
  258. memset_io(info->screen_base, 0, cfb->buffsize);
  259. }
  260. cfb->lcd_pwr = devm_regulator_get(dev, "lcd");
  261. if (PTR_ERR(cfb->lcd_pwr) == -EPROBE_DEFER) {
  262. ret = -EPROBE_DEFER;
  263. goto out_fb_release;
  264. }
  265. info->fbops = &clps711x_fb_ops;
  266. info->flags = FBINFO_DEFAULT;
  267. info->var.activate = FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
  268. info->var.height = -1;
  269. info->var.width = -1;
  270. info->var.vmode = FB_VMODE_NONINTERLACED;
  271. info->fix.type = FB_TYPE_PACKED_PIXELS;
  272. info->fix.accel = FB_ACCEL_NONE;
  273. strlcpy(info->fix.id, CLPS711X_FB_NAME, sizeof(info->fix.id));
  274. fb_videomode_to_var(&info->var, &cfb->mode);
  275. ret = fb_alloc_cmap(&info->cmap, BIT(CLPS711X_FB_BPP_MAX), 0);
  276. if (ret)
  277. goto out_fb_release;
  278. ret = fb_set_var(info, &info->var);
  279. if (ret)
  280. goto out_fb_dealloc_cmap;
  281. ret = register_framebuffer(info);
  282. if (ret)
  283. goto out_fb_dealloc_cmap;
  284. lcd = devm_lcd_device_register(dev, "clps711x-lcd", dev, cfb,
  285. &clps711x_lcd_ops);
  286. if (!IS_ERR(lcd))
  287. return 0;
  288. ret = PTR_ERR(lcd);
  289. unregister_framebuffer(info);
  290. out_fb_dealloc_cmap:
  291. regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
  292. fb_dealloc_cmap(&info->cmap);
  293. out_fb_release:
  294. framebuffer_release(info);
  295. return ret;
  296. }
  297. static int clps711x_fb_remove(struct platform_device *pdev)
  298. {
  299. struct fb_info *info = platform_get_drvdata(pdev);
  300. struct clps711x_fb_info *cfb = info->par;
  301. regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
  302. unregister_framebuffer(info);
  303. fb_dealloc_cmap(&info->cmap);
  304. framebuffer_release(info);
  305. return 0;
  306. }
  307. static const struct of_device_id clps711x_fb_dt_ids[] = {
  308. { .compatible = "cirrus,clps711x-fb", },
  309. { }
  310. };
  311. MODULE_DEVICE_TABLE(of, clps711x_fb_dt_ids);
  312. static struct platform_driver clps711x_fb_driver = {
  313. .driver = {
  314. .name = CLPS711X_FB_NAME,
  315. .of_match_table = clps711x_fb_dt_ids,
  316. },
  317. .probe = clps711x_fb_probe,
  318. .remove = clps711x_fb_remove,
  319. };
  320. module_platform_driver(clps711x_fb_driver);
  321. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  322. MODULE_DESCRIPTION("Cirrus Logic CLPS711X FB driver");
  323. MODULE_LICENSE("GPL");