simpledrm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/aperture.h>
  3. #include <linux/clk.h>
  4. #include <linux/minmax.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_clk.h>
  7. #include <linux/of_reserved_mem.h>
  8. #include <linux/platform_data/simplefb.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_domain.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <drm/clients/drm_client_setup.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_state_helper.h>
  15. #include <drm/drm_connector.h>
  16. #include <drm/drm_damage_helper.h>
  17. #include <drm/drm_device.h>
  18. #include <drm/drm_drv.h>
  19. #include <drm/drm_fbdev_shmem.h>
  20. #include <drm/drm_framebuffer.h>
  21. #include <drm/drm_gem_atomic_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/drm_gem_shmem_helper.h>
  24. #include <drm/drm_managed.h>
  25. #include <drm/drm_modeset_helper_vtables.h>
  26. #include <drm/drm_print.h>
  27. #include <drm/drm_probe_helper.h>
  28. #include "drm_sysfb_helper.h"
  29. #define DRIVER_NAME "simpledrm"
  30. #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
  31. #define DRIVER_MAJOR 1
  32. #define DRIVER_MINOR 0
  33. /*
  34. * Helpers for simplefb
  35. */
  36. static int
  37. simplefb_get_validated_int(struct drm_device *dev, const char *name,
  38. uint32_t value)
  39. {
  40. return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
  41. }
  42. static int
  43. simplefb_get_validated_int0(struct drm_device *dev, const char *name,
  44. uint32_t value)
  45. {
  46. return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
  47. }
  48. static const struct drm_format_info *
  49. simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
  50. {
  51. static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
  52. const struct simplefb_format *fmt = formats;
  53. const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
  54. const struct drm_format_info *info;
  55. if (!format_name) {
  56. drm_err(dev, "simplefb: missing framebuffer format\n");
  57. return ERR_PTR(-EINVAL);
  58. }
  59. while (fmt < end) {
  60. if (!strcmp(format_name, fmt->name)) {
  61. info = drm_format_info(fmt->fourcc);
  62. if (!info)
  63. return ERR_PTR(-EINVAL);
  64. return info;
  65. }
  66. ++fmt;
  67. }
  68. drm_err(dev, "simplefb: unknown framebuffer format %s\n",
  69. format_name);
  70. return ERR_PTR(-EINVAL);
  71. }
  72. static int
  73. simplefb_get_width_pd(struct drm_device *dev,
  74. const struct simplefb_platform_data *pd)
  75. {
  76. return simplefb_get_validated_int0(dev, "width", pd->width);
  77. }
  78. static int
  79. simplefb_get_height_pd(struct drm_device *dev,
  80. const struct simplefb_platform_data *pd)
  81. {
  82. return simplefb_get_validated_int0(dev, "height", pd->height);
  83. }
  84. static int
  85. simplefb_get_stride_pd(struct drm_device *dev,
  86. const struct simplefb_platform_data *pd)
  87. {
  88. return simplefb_get_validated_int(dev, "stride", pd->stride);
  89. }
  90. static const struct drm_format_info *
  91. simplefb_get_format_pd(struct drm_device *dev,
  92. const struct simplefb_platform_data *pd)
  93. {
  94. return simplefb_get_validated_format(dev, pd->format);
  95. }
  96. static int
  97. simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
  98. const char *name, u32 *value)
  99. {
  100. int ret = of_property_read_u32(of_node, name, value);
  101. if (ret)
  102. drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
  103. name, ret);
  104. return ret;
  105. }
  106. static int
  107. simplefb_read_string_of(struct drm_device *dev, struct device_node *of_node,
  108. const char *name, const char **value)
  109. {
  110. int ret = of_property_read_string(of_node, name, value);
  111. if (ret)
  112. drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
  113. name, ret);
  114. return ret;
  115. }
  116. static int
  117. simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
  118. {
  119. u32 width;
  120. int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
  121. if (ret)
  122. return ret;
  123. return simplefb_get_validated_int0(dev, "width", width);
  124. }
  125. static int
  126. simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
  127. {
  128. u32 height;
  129. int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
  130. if (ret)
  131. return ret;
  132. return simplefb_get_validated_int0(dev, "height", height);
  133. }
  134. static int
  135. simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
  136. {
  137. u32 stride;
  138. int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
  139. if (ret)
  140. return ret;
  141. return simplefb_get_validated_int(dev, "stride", stride);
  142. }
  143. static const struct drm_format_info *
  144. simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
  145. {
  146. const char *format;
  147. int ret = simplefb_read_string_of(dev, of_node, "format", &format);
  148. if (ret)
  149. return ERR_PTR(ret);
  150. return simplefb_get_validated_format(dev, format);
  151. }
  152. static struct resource *
  153. simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
  154. {
  155. struct resource r, *res;
  156. int err;
  157. err = of_reserved_mem_region_to_resource(of_node, 0, &r);
  158. if (err)
  159. return NULL;
  160. res = devm_kmemdup(dev->dev, &r, sizeof(r), GFP_KERNEL);
  161. if (!res)
  162. return ERR_PTR(-ENOMEM);
  163. if (of_property_present(of_node, "reg"))
  164. drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
  165. return res;
  166. }
  167. /*
  168. * Simple Framebuffer device
  169. */
  170. struct simpledrm_device {
  171. struct drm_sysfb_device sysfb;
  172. /* clocks */
  173. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  174. unsigned int clk_count;
  175. struct clk **clks;
  176. #endif
  177. /* regulators */
  178. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  179. unsigned int regulator_count;
  180. struct regulator **regulators;
  181. #endif
  182. /* power-domains */
  183. #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
  184. int pwr_dom_count;
  185. struct device **pwr_dom_devs;
  186. struct device_link **pwr_dom_links;
  187. #endif
  188. /* modesetting */
  189. u32 formats[DRM_SYSFB_PLANE_NFORMATS(1)];
  190. struct drm_plane primary_plane;
  191. struct drm_crtc crtc;
  192. struct drm_encoder encoder;
  193. struct drm_connector connector;
  194. };
  195. /*
  196. * Hardware
  197. */
  198. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  199. /*
  200. * Clock handling code.
  201. *
  202. * Here we handle the clocks property of our "simple-framebuffer" dt node.
  203. * This is necessary so that we can make sure that any clocks needed by
  204. * the display engine that the bootloader set up for us (and for which it
  205. * provided a simplefb dt node), stay up, for the life of the simplefb
  206. * driver.
  207. *
  208. * When the driver unloads, we cleanly disable, and then release the clocks.
  209. *
  210. * We only complain about errors here, no action is taken as the most likely
  211. * error can only happen due to a mismatch between the bootloader which set
  212. * up simplefb, and the clock definitions in the device tree. Chances are
  213. * that there are no adverse effects, and if there are, a clean teardown of
  214. * the fb probe will not help us much either. So just complain and carry on,
  215. * and hope that the user actually gets a working fb at the end of things.
  216. */
  217. static void simpledrm_device_release_clocks(void *res)
  218. {
  219. struct simpledrm_device *sdev = res;
  220. unsigned int i;
  221. for (i = 0; i < sdev->clk_count; ++i) {
  222. if (sdev->clks[i]) {
  223. clk_disable_unprepare(sdev->clks[i]);
  224. clk_put(sdev->clks[i]);
  225. }
  226. }
  227. }
  228. static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
  229. {
  230. struct drm_device *dev = &sdev->sysfb.dev;
  231. struct platform_device *pdev = to_platform_device(dev->dev);
  232. struct device_node *of_node = pdev->dev.of_node;
  233. struct clk *clock;
  234. unsigned int i;
  235. int ret;
  236. if (dev_get_platdata(&pdev->dev) || !of_node)
  237. return 0;
  238. sdev->clk_count = of_clk_get_parent_count(of_node);
  239. if (!sdev->clk_count)
  240. return 0;
  241. sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
  242. GFP_KERNEL);
  243. if (!sdev->clks)
  244. return -ENOMEM;
  245. for (i = 0; i < sdev->clk_count; ++i) {
  246. clock = of_clk_get(of_node, i);
  247. if (IS_ERR(clock)) {
  248. ret = PTR_ERR(clock);
  249. if (ret == -EPROBE_DEFER)
  250. goto err;
  251. drm_err(dev, "clock %u not found: %d\n", i, ret);
  252. continue;
  253. }
  254. ret = clk_prepare_enable(clock);
  255. if (ret) {
  256. drm_err(dev, "failed to enable clock %u: %d\n",
  257. i, ret);
  258. clk_put(clock);
  259. continue;
  260. }
  261. sdev->clks[i] = clock;
  262. }
  263. return devm_add_action_or_reset(&pdev->dev,
  264. simpledrm_device_release_clocks,
  265. sdev);
  266. err:
  267. while (i) {
  268. --i;
  269. if (sdev->clks[i]) {
  270. clk_disable_unprepare(sdev->clks[i]);
  271. clk_put(sdev->clks[i]);
  272. }
  273. }
  274. return ret;
  275. }
  276. #else
  277. static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
  278. {
  279. return 0;
  280. }
  281. #endif
  282. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  283. #define SUPPLY_SUFFIX "-supply"
  284. /*
  285. * Regulator handling code.
  286. *
  287. * Here we handle the num-supplies and vin*-supply properties of our
  288. * "simple-framebuffer" dt node. This is necessary so that we can make sure
  289. * that any regulators needed by the display hardware that the bootloader
  290. * set up for us (and for which it provided a simplefb dt node), stay up,
  291. * for the life of the simplefb driver.
  292. *
  293. * When the driver unloads, we cleanly disable, and then release the
  294. * regulators.
  295. *
  296. * We only complain about errors here, no action is taken as the most likely
  297. * error can only happen due to a mismatch between the bootloader which set
  298. * up simplefb, and the regulator definitions in the device tree. Chances are
  299. * that there are no adverse effects, and if there are, a clean teardown of
  300. * the fb probe will not help us much either. So just complain and carry on,
  301. * and hope that the user actually gets a working fb at the end of things.
  302. */
  303. static void simpledrm_device_release_regulators(void *res)
  304. {
  305. struct simpledrm_device *sdev = res;
  306. unsigned int i;
  307. for (i = 0; i < sdev->regulator_count; ++i) {
  308. if (sdev->regulators[i]) {
  309. regulator_disable(sdev->regulators[i]);
  310. regulator_put(sdev->regulators[i]);
  311. }
  312. }
  313. }
  314. static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
  315. {
  316. struct drm_device *dev = &sdev->sysfb.dev;
  317. struct platform_device *pdev = to_platform_device(dev->dev);
  318. struct device_node *of_node = pdev->dev.of_node;
  319. struct property *prop;
  320. struct regulator *regulator;
  321. const char *p;
  322. unsigned int count = 0, i = 0;
  323. int ret;
  324. if (dev_get_platdata(&pdev->dev) || !of_node)
  325. return 0;
  326. /* Count the number of regulator supplies */
  327. for_each_property_of_node(of_node, prop) {
  328. p = strstr(prop->name, SUPPLY_SUFFIX);
  329. if (p && p != prop->name)
  330. ++count;
  331. }
  332. if (!count)
  333. return 0;
  334. sdev->regulators = drmm_kzalloc(dev,
  335. count * sizeof(sdev->regulators[0]),
  336. GFP_KERNEL);
  337. if (!sdev->regulators)
  338. return -ENOMEM;
  339. for_each_property_of_node(of_node, prop) {
  340. char name[32]; /* 32 is max size of property name */
  341. size_t len;
  342. p = strstr(prop->name, SUPPLY_SUFFIX);
  343. if (!p || p == prop->name)
  344. continue;
  345. len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
  346. strscpy(name, prop->name, min(sizeof(name), len));
  347. regulator = regulator_get_optional(&pdev->dev, name);
  348. if (IS_ERR(regulator)) {
  349. ret = PTR_ERR(regulator);
  350. if (ret == -EPROBE_DEFER)
  351. goto err;
  352. drm_err(dev, "regulator %s not found: %d\n",
  353. name, ret);
  354. continue;
  355. }
  356. ret = regulator_enable(regulator);
  357. if (ret) {
  358. drm_err(dev, "failed to enable regulator %u: %d\n",
  359. i, ret);
  360. regulator_put(regulator);
  361. continue;
  362. }
  363. sdev->regulators[i++] = regulator;
  364. }
  365. sdev->regulator_count = i;
  366. return devm_add_action_or_reset(&pdev->dev,
  367. simpledrm_device_release_regulators,
  368. sdev);
  369. err:
  370. while (i) {
  371. --i;
  372. if (sdev->regulators[i]) {
  373. regulator_disable(sdev->regulators[i]);
  374. regulator_put(sdev->regulators[i]);
  375. }
  376. }
  377. return ret;
  378. }
  379. #else
  380. static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
  381. {
  382. return 0;
  383. }
  384. #endif
  385. #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
  386. /*
  387. * Generic power domain handling code.
  388. *
  389. * Here we handle the power-domains properties of our "simple-framebuffer"
  390. * dt node. This is only necessary if there is more than one power-domain.
  391. * A single power-domains is handled automatically by the driver core. Multiple
  392. * power-domains have to be handled by drivers since the driver core can't know
  393. * the correct power sequencing. Power sequencing is not an issue for simpledrm
  394. * since the bootloader has put the power domains already in the correct state.
  395. * simpledrm has only to ensure they remain active for its lifetime.
  396. *
  397. * When the driver unloads, we detach from the power-domains.
  398. *
  399. * We only complain about errors here, no action is taken as the most likely
  400. * error can only happen due to a mismatch between the bootloader which set
  401. * up the "simple-framebuffer" dt node, and the PM domain providers in the
  402. * device tree. Chances are that there are no adverse effects, and if there are,
  403. * a clean teardown of the fb probe will not help us much either. So just
  404. * complain and carry on, and hope that the user actually gets a working fb at
  405. * the end of things.
  406. */
  407. static void simpledrm_device_detach_genpd(void *res)
  408. {
  409. int i;
  410. struct simpledrm_device *sdev = res;
  411. if (sdev->pwr_dom_count <= 1)
  412. return;
  413. for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
  414. if (sdev->pwr_dom_links[i])
  415. device_link_del(sdev->pwr_dom_links[i]);
  416. if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
  417. dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
  418. }
  419. }
  420. static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
  421. {
  422. struct device *dev = sdev->sysfb.dev.dev;
  423. int i;
  424. sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
  425. "#power-domain-cells");
  426. /*
  427. * Single power-domain devices are handled by driver core nothing to do
  428. * here. The same for device nodes without "power-domains" property.
  429. */
  430. if (sdev->pwr_dom_count <= 1)
  431. return 0;
  432. sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
  433. sizeof(*sdev->pwr_dom_devs),
  434. GFP_KERNEL);
  435. if (!sdev->pwr_dom_devs)
  436. return -ENOMEM;
  437. sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
  438. sizeof(*sdev->pwr_dom_links),
  439. GFP_KERNEL);
  440. if (!sdev->pwr_dom_links)
  441. return -ENOMEM;
  442. for (i = 0; i < sdev->pwr_dom_count; i++) {
  443. sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
  444. if (IS_ERR(sdev->pwr_dom_devs[i])) {
  445. int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
  446. if (ret == -EPROBE_DEFER) {
  447. simpledrm_device_detach_genpd(sdev);
  448. return ret;
  449. }
  450. drm_warn(&sdev->sysfb.dev,
  451. "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
  452. continue;
  453. }
  454. sdev->pwr_dom_links[i] = device_link_add(dev,
  455. sdev->pwr_dom_devs[i],
  456. DL_FLAG_STATELESS |
  457. DL_FLAG_PM_RUNTIME |
  458. DL_FLAG_RPM_ACTIVE);
  459. if (!sdev->pwr_dom_links[i])
  460. drm_warn(&sdev->sysfb.dev, "failed to link power-domain %d\n", i);
  461. }
  462. return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
  463. }
  464. #else
  465. static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
  466. {
  467. return 0;
  468. }
  469. #endif
  470. /*
  471. * Modesetting
  472. */
  473. static const u64 simpledrm_primary_plane_format_modifiers[] = {
  474. DRM_SYSFB_PLANE_FORMAT_MODIFIERS,
  475. };
  476. static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
  477. DRM_SYSFB_PLANE_HELPER_FUNCS,
  478. };
  479. static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
  480. DRM_SYSFB_PLANE_FUNCS,
  481. .destroy = drm_plane_cleanup,
  482. };
  483. static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
  484. DRM_SYSFB_CRTC_HELPER_FUNCS,
  485. };
  486. static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
  487. DRM_SYSFB_CRTC_FUNCS,
  488. .destroy = drm_crtc_cleanup,
  489. };
  490. static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
  491. .destroy = drm_encoder_cleanup,
  492. };
  493. static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
  494. DRM_SYSFB_CONNECTOR_HELPER_FUNCS,
  495. };
  496. static const struct drm_connector_funcs simpledrm_connector_funcs = {
  497. DRM_SYSFB_CONNECTOR_FUNCS,
  498. .destroy = drm_connector_cleanup,
  499. };
  500. static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
  501. DRM_SYSFB_MODE_CONFIG_FUNCS,
  502. };
  503. /*
  504. * Init / Cleanup
  505. */
  506. static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
  507. struct platform_device *pdev)
  508. {
  509. const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
  510. struct device_node *of_node = pdev->dev.of_node;
  511. struct simpledrm_device *sdev;
  512. struct drm_sysfb_device *sysfb;
  513. struct drm_device *dev;
  514. int width, height, stride;
  515. int width_mm = 0, height_mm = 0;
  516. struct device_node *panel_node;
  517. const struct drm_format_info *format;
  518. struct resource *res, *mem = NULL;
  519. struct drm_plane *primary_plane;
  520. struct drm_crtc *crtc;
  521. struct drm_encoder *encoder;
  522. struct drm_connector *connector;
  523. unsigned long max_width, max_height;
  524. size_t nformats;
  525. int ret;
  526. sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, sysfb.dev);
  527. if (IS_ERR(sdev))
  528. return ERR_CAST(sdev);
  529. sysfb = &sdev->sysfb;
  530. dev = &sysfb->dev;
  531. platform_set_drvdata(pdev, sdev);
  532. /*
  533. * Hardware settings
  534. */
  535. ret = simpledrm_device_init_clocks(sdev);
  536. if (ret)
  537. return ERR_PTR(ret);
  538. ret = simpledrm_device_init_regulators(sdev);
  539. if (ret)
  540. return ERR_PTR(ret);
  541. ret = simpledrm_device_attach_genpd(sdev);
  542. if (ret)
  543. return ERR_PTR(ret);
  544. if (pd) {
  545. width = simplefb_get_width_pd(dev, pd);
  546. if (width < 0)
  547. return ERR_PTR(width);
  548. height = simplefb_get_height_pd(dev, pd);
  549. if (height < 0)
  550. return ERR_PTR(height);
  551. stride = simplefb_get_stride_pd(dev, pd);
  552. if (stride < 0)
  553. return ERR_PTR(stride);
  554. format = simplefb_get_format_pd(dev, pd);
  555. if (IS_ERR(format))
  556. return ERR_CAST(format);
  557. } else if (of_node) {
  558. width = simplefb_get_width_of(dev, of_node);
  559. if (width < 0)
  560. return ERR_PTR(width);
  561. height = simplefb_get_height_of(dev, of_node);
  562. if (height < 0)
  563. return ERR_PTR(height);
  564. stride = simplefb_get_stride_of(dev, of_node);
  565. if (stride < 0)
  566. return ERR_PTR(stride);
  567. format = simplefb_get_format_of(dev, of_node);
  568. if (IS_ERR(format))
  569. return ERR_CAST(format);
  570. mem = simplefb_get_memory_of(dev, of_node);
  571. if (IS_ERR(mem))
  572. return ERR_CAST(mem);
  573. panel_node = of_parse_phandle(of_node, "panel", 0);
  574. if (panel_node) {
  575. simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
  576. simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
  577. of_node_put(panel_node);
  578. }
  579. } else {
  580. drm_err(dev, "no simplefb configuration found\n");
  581. return ERR_PTR(-ENODEV);
  582. }
  583. if (!stride) {
  584. stride = drm_format_info_min_pitch(format, 0, width);
  585. if (drm_WARN_ON(dev, !stride))
  586. return ERR_PTR(-EINVAL);
  587. }
  588. sysfb->fb_mode = drm_sysfb_mode(width, height, width_mm, height_mm);
  589. sysfb->fb_format = format;
  590. sysfb->fb_pitch = stride;
  591. drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sysfb->fb_mode));
  592. drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
  593. &format->format, width, height, stride);
  594. /*
  595. * Memory management
  596. */
  597. if (mem) {
  598. void *screen_base;
  599. ret = devm_aperture_acquire_for_platform_device(pdev, mem->start,
  600. resource_size(mem));
  601. if (ret) {
  602. drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
  603. return ERR_PTR(ret);
  604. }
  605. drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
  606. screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
  607. if (IS_ERR(screen_base))
  608. return screen_base;
  609. iosys_map_set_vaddr(&sysfb->fb_addr, screen_base);
  610. } else {
  611. void __iomem *screen_base;
  612. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  613. if (!res)
  614. return ERR_PTR(-EINVAL);
  615. ret = devm_aperture_acquire_for_platform_device(pdev, res->start,
  616. resource_size(res));
  617. if (ret) {
  618. drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
  619. return ERR_PTR(ret);
  620. }
  621. drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
  622. mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  623. drv->name);
  624. if (!mem) {
  625. /*
  626. * We cannot make this fatal. Sometimes this comes from magic
  627. * spaces our resource handlers simply don't know about. Use
  628. * the I/O-memory resource as-is and try to map that instead.
  629. */
  630. drm_warn(dev, "could not acquire memory region %pr\n", res);
  631. mem = res;
  632. }
  633. screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
  634. if (!screen_base)
  635. return ERR_PTR(-ENOMEM);
  636. iosys_map_set_vaddr_iomem(&sysfb->fb_addr, screen_base);
  637. }
  638. /*
  639. * Modesetting
  640. */
  641. ret = drmm_mode_config_init(dev);
  642. if (ret)
  643. return ERR_PTR(ret);
  644. max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
  645. max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
  646. dev->mode_config.min_width = width;
  647. dev->mode_config.max_width = max_width;
  648. dev->mode_config.min_height = height;
  649. dev->mode_config.max_height = max_height;
  650. dev->mode_config.preferred_depth = format->depth;
  651. dev->mode_config.funcs = &simpledrm_mode_config_funcs;
  652. /* Primary plane */
  653. nformats = drm_sysfb_build_fourcc_list(dev, &format->format, 1,
  654. sdev->formats, ARRAY_SIZE(sdev->formats));
  655. primary_plane = &sdev->primary_plane;
  656. ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
  657. sdev->formats, nformats,
  658. simpledrm_primary_plane_format_modifiers,
  659. DRM_PLANE_TYPE_PRIMARY, NULL);
  660. if (ret)
  661. return ERR_PTR(ret);
  662. drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
  663. drm_plane_enable_fb_damage_clips(primary_plane);
  664. /* CRTC */
  665. crtc = &sdev->crtc;
  666. ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
  667. &simpledrm_crtc_funcs, NULL);
  668. if (ret)
  669. return ERR_PTR(ret);
  670. drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
  671. /* Encoder */
  672. encoder = &sdev->encoder;
  673. ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
  674. DRM_MODE_ENCODER_NONE, NULL);
  675. if (ret)
  676. return ERR_PTR(ret);
  677. encoder->possible_crtcs = drm_crtc_mask(crtc);
  678. /* Connector */
  679. connector = &sdev->connector;
  680. ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
  681. DRM_MODE_CONNECTOR_Unknown);
  682. if (ret)
  683. return ERR_PTR(ret);
  684. drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
  685. drm_connector_set_panel_orientation_with_quirk(connector,
  686. DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
  687. width, height);
  688. ret = drm_connector_attach_encoder(connector, encoder);
  689. if (ret)
  690. return ERR_PTR(ret);
  691. drm_mode_config_reset(dev);
  692. return sdev;
  693. }
  694. /*
  695. * DRM driver
  696. */
  697. DEFINE_DRM_GEM_FOPS(simpledrm_fops);
  698. static struct drm_driver simpledrm_driver = {
  699. DRM_GEM_SHMEM_DRIVER_OPS,
  700. DRM_FBDEV_SHMEM_DRIVER_OPS,
  701. .name = DRIVER_NAME,
  702. .desc = DRIVER_DESC,
  703. .major = DRIVER_MAJOR,
  704. .minor = DRIVER_MINOR,
  705. .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
  706. .fops = &simpledrm_fops,
  707. };
  708. /*
  709. * Platform driver
  710. */
  711. static int simpledrm_probe(struct platform_device *pdev)
  712. {
  713. struct simpledrm_device *sdev;
  714. struct drm_sysfb_device *sysfb;
  715. struct drm_device *dev;
  716. int ret;
  717. sdev = simpledrm_device_create(&simpledrm_driver, pdev);
  718. if (IS_ERR(sdev))
  719. return PTR_ERR(sdev);
  720. sysfb = &sdev->sysfb;
  721. dev = &sysfb->dev;
  722. ret = drm_dev_register(dev, 0);
  723. if (ret)
  724. return ret;
  725. drm_client_setup(dev, sdev->sysfb.fb_format);
  726. return 0;
  727. }
  728. static void simpledrm_remove(struct platform_device *pdev)
  729. {
  730. struct simpledrm_device *sdev = platform_get_drvdata(pdev);
  731. struct drm_device *dev = &sdev->sysfb.dev;
  732. drm_dev_unplug(dev);
  733. }
  734. static const struct of_device_id simpledrm_of_match_table[] = {
  735. { .compatible = "simple-framebuffer", },
  736. { },
  737. };
  738. MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
  739. static struct platform_driver simpledrm_platform_driver = {
  740. .driver = {
  741. .name = "simple-framebuffer", /* connect to sysfb */
  742. .of_match_table = simpledrm_of_match_table,
  743. },
  744. .probe = simpledrm_probe,
  745. .remove = simpledrm_remove,
  746. };
  747. module_platform_driver(simpledrm_platform_driver);
  748. MODULE_DESCRIPTION(DRIVER_DESC);
  749. MODULE_LICENSE("GPL v2");