clps711x-fb.c 9.1 KB

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