simpledrm.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/clk.h>
  3. #include <linux/of_clk.h>
  4. #include <linux/minmax.h>
  5. #include <linux/of_address.h>
  6. #include <linux/platform_data/simplefb.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <drm/drm_aperture.h>
  11. #include <drm/drm_atomic.h>
  12. #include <drm/drm_atomic_state_helper.h>
  13. #include <drm/drm_connector.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_damage_helper.h>
  16. #include <drm/drm_device.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fbdev_shmem.h>
  19. #include <drm/drm_format_helper.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_panic.h>
  27. #include <drm/drm_probe_helper.h>
  28. #define DRIVER_NAME "simpledrm"
  29. #define DRIVER_DESC "DRM driver for simple-framebuffer platform devices"
  30. #define DRIVER_DATE "20200625"
  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. if (value > INT_MAX) {
  41. drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
  42. name, value);
  43. return -EINVAL;
  44. }
  45. return (int)value;
  46. }
  47. static int
  48. simplefb_get_validated_int0(struct drm_device *dev, const char *name,
  49. uint32_t value)
  50. {
  51. if (!value) {
  52. drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
  53. name, value);
  54. return -EINVAL;
  55. }
  56. return simplefb_get_validated_int(dev, name, value);
  57. }
  58. static const struct drm_format_info *
  59. simplefb_get_validated_format(struct drm_device *dev, const char *format_name)
  60. {
  61. static const struct simplefb_format formats[] = SIMPLEFB_FORMATS;
  62. const struct simplefb_format *fmt = formats;
  63. const struct simplefb_format *end = fmt + ARRAY_SIZE(formats);
  64. const struct drm_format_info *info;
  65. if (!format_name) {
  66. drm_err(dev, "simplefb: missing framebuffer format\n");
  67. return ERR_PTR(-EINVAL);
  68. }
  69. while (fmt < end) {
  70. if (!strcmp(format_name, fmt->name)) {
  71. info = drm_format_info(fmt->fourcc);
  72. if (!info)
  73. return ERR_PTR(-EINVAL);
  74. return info;
  75. }
  76. ++fmt;
  77. }
  78. drm_err(dev, "simplefb: unknown framebuffer format %s\n",
  79. format_name);
  80. return ERR_PTR(-EINVAL);
  81. }
  82. static int
  83. simplefb_get_width_pd(struct drm_device *dev,
  84. const struct simplefb_platform_data *pd)
  85. {
  86. return simplefb_get_validated_int0(dev, "width", pd->width);
  87. }
  88. static int
  89. simplefb_get_height_pd(struct drm_device *dev,
  90. const struct simplefb_platform_data *pd)
  91. {
  92. return simplefb_get_validated_int0(dev, "height", pd->height);
  93. }
  94. static int
  95. simplefb_get_stride_pd(struct drm_device *dev,
  96. const struct simplefb_platform_data *pd)
  97. {
  98. return simplefb_get_validated_int(dev, "stride", pd->stride);
  99. }
  100. static const struct drm_format_info *
  101. simplefb_get_format_pd(struct drm_device *dev,
  102. const struct simplefb_platform_data *pd)
  103. {
  104. return simplefb_get_validated_format(dev, pd->format);
  105. }
  106. static int
  107. simplefb_read_u32_of(struct drm_device *dev, struct device_node *of_node,
  108. const char *name, u32 *value)
  109. {
  110. int ret = of_property_read_u32(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_read_string_of(struct drm_device *dev, struct device_node *of_node,
  118. const char *name, const char **value)
  119. {
  120. int ret = of_property_read_string(of_node, name, value);
  121. if (ret)
  122. drm_err(dev, "simplefb: cannot parse framebuffer %s: error %d\n",
  123. name, ret);
  124. return ret;
  125. }
  126. static int
  127. simplefb_get_width_of(struct drm_device *dev, struct device_node *of_node)
  128. {
  129. u32 width;
  130. int ret = simplefb_read_u32_of(dev, of_node, "width", &width);
  131. if (ret)
  132. return ret;
  133. return simplefb_get_validated_int0(dev, "width", width);
  134. }
  135. static int
  136. simplefb_get_height_of(struct drm_device *dev, struct device_node *of_node)
  137. {
  138. u32 height;
  139. int ret = simplefb_read_u32_of(dev, of_node, "height", &height);
  140. if (ret)
  141. return ret;
  142. return simplefb_get_validated_int0(dev, "height", height);
  143. }
  144. static int
  145. simplefb_get_stride_of(struct drm_device *dev, struct device_node *of_node)
  146. {
  147. u32 stride;
  148. int ret = simplefb_read_u32_of(dev, of_node, "stride", &stride);
  149. if (ret)
  150. return ret;
  151. return simplefb_get_validated_int(dev, "stride", stride);
  152. }
  153. static const struct drm_format_info *
  154. simplefb_get_format_of(struct drm_device *dev, struct device_node *of_node)
  155. {
  156. const char *format;
  157. int ret = simplefb_read_string_of(dev, of_node, "format", &format);
  158. if (ret)
  159. return ERR_PTR(ret);
  160. return simplefb_get_validated_format(dev, format);
  161. }
  162. static struct resource *
  163. simplefb_get_memory_of(struct drm_device *dev, struct device_node *of_node)
  164. {
  165. struct device_node *np;
  166. struct resource *res;
  167. int err;
  168. np = of_parse_phandle(of_node, "memory-region", 0);
  169. if (!np)
  170. return NULL;
  171. res = devm_kzalloc(dev->dev, sizeof(*res), GFP_KERNEL);
  172. if (!res)
  173. return ERR_PTR(-ENOMEM);
  174. err = of_address_to_resource(np, 0, res);
  175. if (err)
  176. return ERR_PTR(err);
  177. if (of_property_present(of_node, "reg"))
  178. drm_warn(dev, "preferring \"memory-region\" over \"reg\" property\n");
  179. return res;
  180. }
  181. /*
  182. * Simple Framebuffer device
  183. */
  184. struct simpledrm_device {
  185. struct drm_device dev;
  186. /* clocks */
  187. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  188. unsigned int clk_count;
  189. struct clk **clks;
  190. #endif
  191. /* regulators */
  192. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  193. unsigned int regulator_count;
  194. struct regulator **regulators;
  195. #endif
  196. /* power-domains */
  197. #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
  198. int pwr_dom_count;
  199. struct device **pwr_dom_devs;
  200. struct device_link **pwr_dom_links;
  201. #endif
  202. /* simplefb settings */
  203. struct drm_display_mode mode;
  204. const struct drm_format_info *format;
  205. unsigned int pitch;
  206. /* memory management */
  207. struct iosys_map screen_base;
  208. /* modesetting */
  209. uint32_t formats[8];
  210. size_t nformats;
  211. struct drm_plane primary_plane;
  212. struct drm_crtc crtc;
  213. struct drm_encoder encoder;
  214. struct drm_connector connector;
  215. };
  216. static struct simpledrm_device *simpledrm_device_of_dev(struct drm_device *dev)
  217. {
  218. return container_of(dev, struct simpledrm_device, dev);
  219. }
  220. /*
  221. * Hardware
  222. */
  223. #if defined CONFIG_OF && defined CONFIG_COMMON_CLK
  224. /*
  225. * Clock handling code.
  226. *
  227. * Here we handle the clocks property of our "simple-framebuffer" dt node.
  228. * This is necessary so that we can make sure that any clocks needed by
  229. * the display engine that the bootloader set up for us (and for which it
  230. * provided a simplefb dt node), stay up, for the life of the simplefb
  231. * driver.
  232. *
  233. * When the driver unloads, we cleanly disable, and then release the clocks.
  234. *
  235. * We only complain about errors here, no action is taken as the most likely
  236. * error can only happen due to a mismatch between the bootloader which set
  237. * up simplefb, and the clock definitions in the device tree. Chances are
  238. * that there are no adverse effects, and if there are, a clean teardown of
  239. * the fb probe will not help us much either. So just complain and carry on,
  240. * and hope that the user actually gets a working fb at the end of things.
  241. */
  242. static void simpledrm_device_release_clocks(void *res)
  243. {
  244. struct simpledrm_device *sdev = res;
  245. unsigned int i;
  246. for (i = 0; i < sdev->clk_count; ++i) {
  247. if (sdev->clks[i]) {
  248. clk_disable_unprepare(sdev->clks[i]);
  249. clk_put(sdev->clks[i]);
  250. }
  251. }
  252. }
  253. static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
  254. {
  255. struct drm_device *dev = &sdev->dev;
  256. struct platform_device *pdev = to_platform_device(dev->dev);
  257. struct device_node *of_node = pdev->dev.of_node;
  258. struct clk *clock;
  259. unsigned int i;
  260. int ret;
  261. if (dev_get_platdata(&pdev->dev) || !of_node)
  262. return 0;
  263. sdev->clk_count = of_clk_get_parent_count(of_node);
  264. if (!sdev->clk_count)
  265. return 0;
  266. sdev->clks = drmm_kzalloc(dev, sdev->clk_count * sizeof(sdev->clks[0]),
  267. GFP_KERNEL);
  268. if (!sdev->clks)
  269. return -ENOMEM;
  270. for (i = 0; i < sdev->clk_count; ++i) {
  271. clock = of_clk_get(of_node, i);
  272. if (IS_ERR(clock)) {
  273. ret = PTR_ERR(clock);
  274. if (ret == -EPROBE_DEFER)
  275. goto err;
  276. drm_err(dev, "clock %u not found: %d\n", i, ret);
  277. continue;
  278. }
  279. ret = clk_prepare_enable(clock);
  280. if (ret) {
  281. drm_err(dev, "failed to enable clock %u: %d\n",
  282. i, ret);
  283. clk_put(clock);
  284. continue;
  285. }
  286. sdev->clks[i] = clock;
  287. }
  288. return devm_add_action_or_reset(&pdev->dev,
  289. simpledrm_device_release_clocks,
  290. sdev);
  291. err:
  292. while (i) {
  293. --i;
  294. if (sdev->clks[i]) {
  295. clk_disable_unprepare(sdev->clks[i]);
  296. clk_put(sdev->clks[i]);
  297. }
  298. }
  299. return ret;
  300. }
  301. #else
  302. static int simpledrm_device_init_clocks(struct simpledrm_device *sdev)
  303. {
  304. return 0;
  305. }
  306. #endif
  307. #if defined CONFIG_OF && defined CONFIG_REGULATOR
  308. #define SUPPLY_SUFFIX "-supply"
  309. /*
  310. * Regulator handling code.
  311. *
  312. * Here we handle the num-supplies and vin*-supply properties of our
  313. * "simple-framebuffer" dt node. This is necessary so that we can make sure
  314. * that any regulators needed by the display hardware that the bootloader
  315. * set up for us (and for which it provided a simplefb dt node), stay up,
  316. * for the life of the simplefb driver.
  317. *
  318. * When the driver unloads, we cleanly disable, and then release the
  319. * regulators.
  320. *
  321. * We only complain about errors here, no action is taken as the most likely
  322. * error can only happen due to a mismatch between the bootloader which set
  323. * up simplefb, and the regulator definitions in the device tree. Chances are
  324. * that there are no adverse effects, and if there are, a clean teardown of
  325. * the fb probe will not help us much either. So just complain and carry on,
  326. * and hope that the user actually gets a working fb at the end of things.
  327. */
  328. static void simpledrm_device_release_regulators(void *res)
  329. {
  330. struct simpledrm_device *sdev = res;
  331. unsigned int i;
  332. for (i = 0; i < sdev->regulator_count; ++i) {
  333. if (sdev->regulators[i]) {
  334. regulator_disable(sdev->regulators[i]);
  335. regulator_put(sdev->regulators[i]);
  336. }
  337. }
  338. }
  339. static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
  340. {
  341. struct drm_device *dev = &sdev->dev;
  342. struct platform_device *pdev = to_platform_device(dev->dev);
  343. struct device_node *of_node = pdev->dev.of_node;
  344. struct property *prop;
  345. struct regulator *regulator;
  346. const char *p;
  347. unsigned int count = 0, i = 0;
  348. int ret;
  349. if (dev_get_platdata(&pdev->dev) || !of_node)
  350. return 0;
  351. /* Count the number of regulator supplies */
  352. for_each_property_of_node(of_node, prop) {
  353. p = strstr(prop->name, SUPPLY_SUFFIX);
  354. if (p && p != prop->name)
  355. ++count;
  356. }
  357. if (!count)
  358. return 0;
  359. sdev->regulators = drmm_kzalloc(dev,
  360. count * sizeof(sdev->regulators[0]),
  361. GFP_KERNEL);
  362. if (!sdev->regulators)
  363. return -ENOMEM;
  364. for_each_property_of_node(of_node, prop) {
  365. char name[32]; /* 32 is max size of property name */
  366. size_t len;
  367. p = strstr(prop->name, SUPPLY_SUFFIX);
  368. if (!p || p == prop->name)
  369. continue;
  370. len = strlen(prop->name) - strlen(SUPPLY_SUFFIX) + 1;
  371. strscpy(name, prop->name, min(sizeof(name), len));
  372. regulator = regulator_get_optional(&pdev->dev, name);
  373. if (IS_ERR(regulator)) {
  374. ret = PTR_ERR(regulator);
  375. if (ret == -EPROBE_DEFER)
  376. goto err;
  377. drm_err(dev, "regulator %s not found: %d\n",
  378. name, ret);
  379. continue;
  380. }
  381. ret = regulator_enable(regulator);
  382. if (ret) {
  383. drm_err(dev, "failed to enable regulator %u: %d\n",
  384. i, ret);
  385. regulator_put(regulator);
  386. continue;
  387. }
  388. sdev->regulators[i++] = regulator;
  389. }
  390. sdev->regulator_count = i;
  391. return devm_add_action_or_reset(&pdev->dev,
  392. simpledrm_device_release_regulators,
  393. sdev);
  394. err:
  395. while (i) {
  396. --i;
  397. if (sdev->regulators[i]) {
  398. regulator_disable(sdev->regulators[i]);
  399. regulator_put(sdev->regulators[i]);
  400. }
  401. }
  402. return ret;
  403. }
  404. #else
  405. static int simpledrm_device_init_regulators(struct simpledrm_device *sdev)
  406. {
  407. return 0;
  408. }
  409. #endif
  410. #if defined CONFIG_OF && defined CONFIG_PM_GENERIC_DOMAINS
  411. /*
  412. * Generic power domain handling code.
  413. *
  414. * Here we handle the power-domains properties of our "simple-framebuffer"
  415. * dt node. This is only necessary if there is more than one power-domain.
  416. * A single power-domains is handled automatically by the driver core. Multiple
  417. * power-domains have to be handled by drivers since the driver core can't know
  418. * the correct power sequencing. Power sequencing is not an issue for simpledrm
  419. * since the bootloader has put the power domains already in the correct state.
  420. * simpledrm has only to ensure they remain active for its lifetime.
  421. *
  422. * When the driver unloads, we detach from the power-domains.
  423. *
  424. * We only complain about errors here, no action is taken as the most likely
  425. * error can only happen due to a mismatch between the bootloader which set
  426. * up the "simple-framebuffer" dt node, and the PM domain providers in the
  427. * device tree. Chances are that there are no adverse effects, and if there are,
  428. * a clean teardown of the fb probe will not help us much either. So just
  429. * complain and carry on, and hope that the user actually gets a working fb at
  430. * the end of things.
  431. */
  432. static void simpledrm_device_detach_genpd(void *res)
  433. {
  434. int i;
  435. struct simpledrm_device *sdev = res;
  436. if (sdev->pwr_dom_count <= 1)
  437. return;
  438. for (i = sdev->pwr_dom_count - 1; i >= 0; i--) {
  439. if (sdev->pwr_dom_links[i])
  440. device_link_del(sdev->pwr_dom_links[i]);
  441. if (!IS_ERR_OR_NULL(sdev->pwr_dom_devs[i]))
  442. dev_pm_domain_detach(sdev->pwr_dom_devs[i], true);
  443. }
  444. }
  445. static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
  446. {
  447. struct device *dev = sdev->dev.dev;
  448. int i;
  449. sdev->pwr_dom_count = of_count_phandle_with_args(dev->of_node, "power-domains",
  450. "#power-domain-cells");
  451. /*
  452. * Single power-domain devices are handled by driver core nothing to do
  453. * here. The same for device nodes without "power-domains" property.
  454. */
  455. if (sdev->pwr_dom_count <= 1)
  456. return 0;
  457. sdev->pwr_dom_devs = devm_kcalloc(dev, sdev->pwr_dom_count,
  458. sizeof(*sdev->pwr_dom_devs),
  459. GFP_KERNEL);
  460. if (!sdev->pwr_dom_devs)
  461. return -ENOMEM;
  462. sdev->pwr_dom_links = devm_kcalloc(dev, sdev->pwr_dom_count,
  463. sizeof(*sdev->pwr_dom_links),
  464. GFP_KERNEL);
  465. if (!sdev->pwr_dom_links)
  466. return -ENOMEM;
  467. for (i = 0; i < sdev->pwr_dom_count; i++) {
  468. sdev->pwr_dom_devs[i] = dev_pm_domain_attach_by_id(dev, i);
  469. if (IS_ERR(sdev->pwr_dom_devs[i])) {
  470. int ret = PTR_ERR(sdev->pwr_dom_devs[i]);
  471. if (ret == -EPROBE_DEFER) {
  472. simpledrm_device_detach_genpd(sdev);
  473. return ret;
  474. }
  475. drm_warn(&sdev->dev,
  476. "pm_domain_attach_by_id(%u) failed: %d\n", i, ret);
  477. continue;
  478. }
  479. sdev->pwr_dom_links[i] = device_link_add(dev,
  480. sdev->pwr_dom_devs[i],
  481. DL_FLAG_STATELESS |
  482. DL_FLAG_PM_RUNTIME |
  483. DL_FLAG_RPM_ACTIVE);
  484. if (!sdev->pwr_dom_links[i])
  485. drm_warn(&sdev->dev, "failed to link power-domain %d\n", i);
  486. }
  487. return devm_add_action_or_reset(dev, simpledrm_device_detach_genpd, sdev);
  488. }
  489. #else
  490. static int simpledrm_device_attach_genpd(struct simpledrm_device *sdev)
  491. {
  492. return 0;
  493. }
  494. #endif
  495. /*
  496. * Modesetting
  497. */
  498. static const uint64_t simpledrm_primary_plane_format_modifiers[] = {
  499. DRM_FORMAT_MOD_LINEAR,
  500. DRM_FORMAT_MOD_INVALID
  501. };
  502. static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
  503. struct drm_atomic_state *state)
  504. {
  505. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
  506. struct drm_shadow_plane_state *new_shadow_plane_state =
  507. to_drm_shadow_plane_state(new_plane_state);
  508. struct drm_framebuffer *new_fb = new_plane_state->fb;
  509. struct drm_crtc *new_crtc = new_plane_state->crtc;
  510. struct drm_crtc_state *new_crtc_state = NULL;
  511. struct drm_device *dev = plane->dev;
  512. struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
  513. int ret;
  514. if (new_crtc)
  515. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
  516. ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
  517. DRM_PLANE_NO_SCALING,
  518. DRM_PLANE_NO_SCALING,
  519. false, false);
  520. if (ret)
  521. return ret;
  522. else if (!new_plane_state->visible)
  523. return 0;
  524. if (new_fb->format != sdev->format) {
  525. void *buf;
  526. /* format conversion necessary; reserve buffer */
  527. buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
  528. sdev->pitch, GFP_KERNEL);
  529. if (!buf)
  530. return -ENOMEM;
  531. }
  532. return 0;
  533. }
  534. static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
  535. struct drm_atomic_state *state)
  536. {
  537. struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
  538. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
  539. struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
  540. struct drm_framebuffer *fb = plane_state->fb;
  541. struct drm_device *dev = plane->dev;
  542. struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
  543. struct drm_atomic_helper_damage_iter iter;
  544. struct drm_rect damage;
  545. int ret, idx;
  546. ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
  547. if (ret)
  548. return;
  549. if (!drm_dev_enter(dev, &idx))
  550. goto out_drm_gem_fb_end_cpu_access;
  551. drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
  552. drm_atomic_for_each_plane_damage(&iter, &damage) {
  553. struct drm_rect dst_clip = plane_state->dst;
  554. struct iosys_map dst = sdev->screen_base;
  555. if (!drm_rect_intersect(&dst_clip, &damage))
  556. continue;
  557. iosys_map_incr(&dst, drm_fb_clip_offset(sdev->pitch, sdev->format, &dst_clip));
  558. drm_fb_blit(&dst, &sdev->pitch, sdev->format->format, shadow_plane_state->data,
  559. fb, &damage, &shadow_plane_state->fmtcnv_state);
  560. }
  561. drm_dev_exit(idx);
  562. out_drm_gem_fb_end_cpu_access:
  563. drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
  564. }
  565. static void simpledrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
  566. struct drm_atomic_state *state)
  567. {
  568. struct drm_device *dev = plane->dev;
  569. struct simpledrm_device *sdev = simpledrm_device_of_dev(dev);
  570. int idx;
  571. if (!drm_dev_enter(dev, &idx))
  572. return;
  573. /* Clear screen to black if disabled */
  574. iosys_map_memset(&sdev->screen_base, 0, 0, sdev->pitch * sdev->mode.vdisplay);
  575. drm_dev_exit(idx);
  576. }
  577. static int simpledrm_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
  578. struct drm_scanout_buffer *sb)
  579. {
  580. struct simpledrm_device *sdev = simpledrm_device_of_dev(plane->dev);
  581. sb->width = sdev->mode.hdisplay;
  582. sb->height = sdev->mode.vdisplay;
  583. sb->format = sdev->format;
  584. sb->pitch[0] = sdev->pitch;
  585. sb->map[0] = sdev->screen_base;
  586. return 0;
  587. }
  588. static const struct drm_plane_helper_funcs simpledrm_primary_plane_helper_funcs = {
  589. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  590. .atomic_check = simpledrm_primary_plane_helper_atomic_check,
  591. .atomic_update = simpledrm_primary_plane_helper_atomic_update,
  592. .atomic_disable = simpledrm_primary_plane_helper_atomic_disable,
  593. .get_scanout_buffer = simpledrm_primary_plane_helper_get_scanout_buffer,
  594. };
  595. static const struct drm_plane_funcs simpledrm_primary_plane_funcs = {
  596. .update_plane = drm_atomic_helper_update_plane,
  597. .disable_plane = drm_atomic_helper_disable_plane,
  598. .destroy = drm_plane_cleanup,
  599. DRM_GEM_SHADOW_PLANE_FUNCS,
  600. };
  601. static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
  602. const struct drm_display_mode *mode)
  603. {
  604. struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev);
  605. return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode);
  606. }
  607. /*
  608. * The CRTC is always enabled. Screen updates are performed by
  609. * the primary plane's atomic_update function. Disabling clears
  610. * the screen in the primary plane's atomic_disable function.
  611. */
  612. static const struct drm_crtc_helper_funcs simpledrm_crtc_helper_funcs = {
  613. .mode_valid = simpledrm_crtc_helper_mode_valid,
  614. .atomic_check = drm_crtc_helper_atomic_check,
  615. };
  616. static const struct drm_crtc_funcs simpledrm_crtc_funcs = {
  617. .reset = drm_atomic_helper_crtc_reset,
  618. .destroy = drm_crtc_cleanup,
  619. .set_config = drm_atomic_helper_set_config,
  620. .page_flip = drm_atomic_helper_page_flip,
  621. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  622. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  623. };
  624. static const struct drm_encoder_funcs simpledrm_encoder_funcs = {
  625. .destroy = drm_encoder_cleanup,
  626. };
  627. static int simpledrm_connector_helper_get_modes(struct drm_connector *connector)
  628. {
  629. struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev);
  630. return drm_connector_helper_get_modes_fixed(connector, &sdev->mode);
  631. }
  632. static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = {
  633. .get_modes = simpledrm_connector_helper_get_modes,
  634. };
  635. static const struct drm_connector_funcs simpledrm_connector_funcs = {
  636. .reset = drm_atomic_helper_connector_reset,
  637. .fill_modes = drm_helper_probe_single_connector_modes,
  638. .destroy = drm_connector_cleanup,
  639. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  640. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  641. };
  642. static const struct drm_mode_config_funcs simpledrm_mode_config_funcs = {
  643. .fb_create = drm_gem_fb_create_with_dirty,
  644. .atomic_check = drm_atomic_helper_check,
  645. .atomic_commit = drm_atomic_helper_commit,
  646. };
  647. /*
  648. * Init / Cleanup
  649. */
  650. static struct drm_display_mode simpledrm_mode(unsigned int width,
  651. unsigned int height,
  652. unsigned int width_mm,
  653. unsigned int height_mm)
  654. {
  655. const struct drm_display_mode mode = {
  656. DRM_MODE_INIT(60, width, height, width_mm, height_mm)
  657. };
  658. return mode;
  659. }
  660. static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv,
  661. struct platform_device *pdev)
  662. {
  663. const struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
  664. struct device_node *of_node = pdev->dev.of_node;
  665. struct simpledrm_device *sdev;
  666. struct drm_device *dev;
  667. int width, height, stride;
  668. int width_mm = 0, height_mm = 0;
  669. struct device_node *panel_node;
  670. const struct drm_format_info *format;
  671. struct resource *res, *mem = NULL;
  672. struct drm_plane *primary_plane;
  673. struct drm_crtc *crtc;
  674. struct drm_encoder *encoder;
  675. struct drm_connector *connector;
  676. unsigned long max_width, max_height;
  677. size_t nformats;
  678. int ret;
  679. sdev = devm_drm_dev_alloc(&pdev->dev, drv, struct simpledrm_device, dev);
  680. if (IS_ERR(sdev))
  681. return ERR_CAST(sdev);
  682. dev = &sdev->dev;
  683. platform_set_drvdata(pdev, sdev);
  684. /*
  685. * Hardware settings
  686. */
  687. ret = simpledrm_device_init_clocks(sdev);
  688. if (ret)
  689. return ERR_PTR(ret);
  690. ret = simpledrm_device_init_regulators(sdev);
  691. if (ret)
  692. return ERR_PTR(ret);
  693. ret = simpledrm_device_attach_genpd(sdev);
  694. if (ret)
  695. return ERR_PTR(ret);
  696. if (pd) {
  697. width = simplefb_get_width_pd(dev, pd);
  698. if (width < 0)
  699. return ERR_PTR(width);
  700. height = simplefb_get_height_pd(dev, pd);
  701. if (height < 0)
  702. return ERR_PTR(height);
  703. stride = simplefb_get_stride_pd(dev, pd);
  704. if (stride < 0)
  705. return ERR_PTR(stride);
  706. format = simplefb_get_format_pd(dev, pd);
  707. if (IS_ERR(format))
  708. return ERR_CAST(format);
  709. } else if (of_node) {
  710. width = simplefb_get_width_of(dev, of_node);
  711. if (width < 0)
  712. return ERR_PTR(width);
  713. height = simplefb_get_height_of(dev, of_node);
  714. if (height < 0)
  715. return ERR_PTR(height);
  716. stride = simplefb_get_stride_of(dev, of_node);
  717. if (stride < 0)
  718. return ERR_PTR(stride);
  719. format = simplefb_get_format_of(dev, of_node);
  720. if (IS_ERR(format))
  721. return ERR_CAST(format);
  722. mem = simplefb_get_memory_of(dev, of_node);
  723. if (IS_ERR(mem))
  724. return ERR_CAST(mem);
  725. panel_node = of_parse_phandle(of_node, "panel", 0);
  726. if (panel_node) {
  727. simplefb_read_u32_of(dev, panel_node, "width-mm", &width_mm);
  728. simplefb_read_u32_of(dev, panel_node, "height-mm", &height_mm);
  729. of_node_put(panel_node);
  730. }
  731. } else {
  732. drm_err(dev, "no simplefb configuration found\n");
  733. return ERR_PTR(-ENODEV);
  734. }
  735. if (!stride) {
  736. stride = drm_format_info_min_pitch(format, 0, width);
  737. if (drm_WARN_ON(dev, !stride))
  738. return ERR_PTR(-EINVAL);
  739. }
  740. /*
  741. * Assume a monitor resolution of 96 dpi if physical dimensions
  742. * are not specified to get a somewhat reasonable screen size.
  743. */
  744. if (!width_mm)
  745. width_mm = DRM_MODE_RES_MM(width, 96ul);
  746. if (!height_mm)
  747. height_mm = DRM_MODE_RES_MM(height, 96ul);
  748. sdev->mode = simpledrm_mode(width, height, width_mm, height_mm);
  749. sdev->format = format;
  750. sdev->pitch = stride;
  751. drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sdev->mode));
  752. drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, stride=%d byte\n",
  753. &format->format, width, height, stride);
  754. /*
  755. * Memory management
  756. */
  757. if (mem) {
  758. void *screen_base;
  759. ret = devm_aperture_acquire_from_firmware(dev, mem->start, resource_size(mem));
  760. if (ret) {
  761. drm_err(dev, "could not acquire memory range %pr: %d\n", mem, ret);
  762. return ERR_PTR(ret);
  763. }
  764. drm_dbg(dev, "using system memory framebuffer at %pr\n", mem);
  765. screen_base = devm_memremap(dev->dev, mem->start, resource_size(mem), MEMREMAP_WC);
  766. if (IS_ERR(screen_base))
  767. return screen_base;
  768. iosys_map_set_vaddr(&sdev->screen_base, screen_base);
  769. } else {
  770. void __iomem *screen_base;
  771. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  772. if (!res)
  773. return ERR_PTR(-EINVAL);
  774. ret = devm_aperture_acquire_from_firmware(dev, res->start, resource_size(res));
  775. if (ret) {
  776. drm_err(dev, "could not acquire memory range %pr: %d\n", res, ret);
  777. return ERR_PTR(ret);
  778. }
  779. drm_dbg(dev, "using I/O memory framebuffer at %pr\n", res);
  780. mem = devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  781. drv->name);
  782. if (!mem) {
  783. /*
  784. * We cannot make this fatal. Sometimes this comes from magic
  785. * spaces our resource handlers simply don't know about. Use
  786. * the I/O-memory resource as-is and try to map that instead.
  787. */
  788. drm_warn(dev, "could not acquire memory region %pr\n", res);
  789. mem = res;
  790. }
  791. screen_base = devm_ioremap_wc(&pdev->dev, mem->start, resource_size(mem));
  792. if (!screen_base)
  793. return ERR_PTR(-ENOMEM);
  794. iosys_map_set_vaddr_iomem(&sdev->screen_base, screen_base);
  795. }
  796. /*
  797. * Modesetting
  798. */
  799. ret = drmm_mode_config_init(dev);
  800. if (ret)
  801. return ERR_PTR(ret);
  802. max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
  803. max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
  804. dev->mode_config.min_width = width;
  805. dev->mode_config.max_width = max_width;
  806. dev->mode_config.min_height = height;
  807. dev->mode_config.max_height = max_height;
  808. dev->mode_config.preferred_depth = format->depth;
  809. dev->mode_config.funcs = &simpledrm_mode_config_funcs;
  810. /* Primary plane */
  811. nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
  812. sdev->formats, ARRAY_SIZE(sdev->formats));
  813. primary_plane = &sdev->primary_plane;
  814. ret = drm_universal_plane_init(dev, primary_plane, 0, &simpledrm_primary_plane_funcs,
  815. sdev->formats, nformats,
  816. simpledrm_primary_plane_format_modifiers,
  817. DRM_PLANE_TYPE_PRIMARY, NULL);
  818. if (ret)
  819. return ERR_PTR(ret);
  820. drm_plane_helper_add(primary_plane, &simpledrm_primary_plane_helper_funcs);
  821. drm_plane_enable_fb_damage_clips(primary_plane);
  822. /* CRTC */
  823. crtc = &sdev->crtc;
  824. ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
  825. &simpledrm_crtc_funcs, NULL);
  826. if (ret)
  827. return ERR_PTR(ret);
  828. drm_crtc_helper_add(crtc, &simpledrm_crtc_helper_funcs);
  829. /* Encoder */
  830. encoder = &sdev->encoder;
  831. ret = drm_encoder_init(dev, encoder, &simpledrm_encoder_funcs,
  832. DRM_MODE_ENCODER_NONE, NULL);
  833. if (ret)
  834. return ERR_PTR(ret);
  835. encoder->possible_crtcs = drm_crtc_mask(crtc);
  836. /* Connector */
  837. connector = &sdev->connector;
  838. ret = drm_connector_init(dev, connector, &simpledrm_connector_funcs,
  839. DRM_MODE_CONNECTOR_Unknown);
  840. if (ret)
  841. return ERR_PTR(ret);
  842. drm_connector_helper_add(connector, &simpledrm_connector_helper_funcs);
  843. drm_connector_set_panel_orientation_with_quirk(connector,
  844. DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
  845. width, height);
  846. ret = drm_connector_attach_encoder(connector, encoder);
  847. if (ret)
  848. return ERR_PTR(ret);
  849. drm_mode_config_reset(dev);
  850. return sdev;
  851. }
  852. /*
  853. * DRM driver
  854. */
  855. DEFINE_DRM_GEM_FOPS(simpledrm_fops);
  856. static struct drm_driver simpledrm_driver = {
  857. DRM_GEM_SHMEM_DRIVER_OPS,
  858. .name = DRIVER_NAME,
  859. .desc = DRIVER_DESC,
  860. .date = DRIVER_DATE,
  861. .major = DRIVER_MAJOR,
  862. .minor = DRIVER_MINOR,
  863. .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
  864. .fops = &simpledrm_fops,
  865. };
  866. /*
  867. * Platform driver
  868. */
  869. static int simpledrm_probe(struct platform_device *pdev)
  870. {
  871. struct simpledrm_device *sdev;
  872. struct drm_device *dev;
  873. unsigned int color_mode;
  874. int ret;
  875. sdev = simpledrm_device_create(&simpledrm_driver, pdev);
  876. if (IS_ERR(sdev))
  877. return PTR_ERR(sdev);
  878. dev = &sdev->dev;
  879. ret = drm_dev_register(dev, 0);
  880. if (ret)
  881. return ret;
  882. color_mode = drm_format_info_bpp(sdev->format, 0);
  883. if (color_mode == 16)
  884. color_mode = sdev->format->depth; // can be 15 or 16
  885. drm_fbdev_shmem_setup(dev, color_mode);
  886. return 0;
  887. }
  888. static void simpledrm_remove(struct platform_device *pdev)
  889. {
  890. struct simpledrm_device *sdev = platform_get_drvdata(pdev);
  891. struct drm_device *dev = &sdev->dev;
  892. drm_dev_unplug(dev);
  893. }
  894. static const struct of_device_id simpledrm_of_match_table[] = {
  895. { .compatible = "simple-framebuffer", },
  896. { },
  897. };
  898. MODULE_DEVICE_TABLE(of, simpledrm_of_match_table);
  899. static struct platform_driver simpledrm_platform_driver = {
  900. .driver = {
  901. .name = "simple-framebuffer", /* connect to sysfb */
  902. .of_match_table = simpledrm_of_match_table,
  903. },
  904. .probe = simpledrm_probe,
  905. .remove_new = simpledrm_remove,
  906. };
  907. module_platform_driver(simpledrm_platform_driver);
  908. MODULE_DESCRIPTION(DRIVER_DESC);
  909. MODULE_LICENSE("GPL v2");