Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ========================= |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 4 | Device Tree Overlay Notes |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 5 | ========================= |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 6 | |
| 7 | This document describes the implementation of the in-kernel |
| 8 | device tree overlay functionality residing in drivers/of/overlay.c and is a |
Mauro Carvalho Chehab | 26853a2 | 2020-04-15 16:45:20 +0200 | [diff] [blame] | 9 | companion document to Documentation/devicetree/dynamic-resolution-notes.rst[1] |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 10 | |
| 11 | How overlays work |
| 12 | ----------------- |
| 13 | |
| 14 | A Device Tree's overlay purpose is to modify the kernel's live tree, and |
Masanari Iida | ac3e8ea | 2015-01-02 22:54:39 +0900 | [diff] [blame] | 15 | have the modification affecting the state of the kernel in a way that |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 16 | is reflecting the changes. |
| 17 | Since the kernel mainly deals with devices, any new device node that result |
| 18 | in an active device should have it created while if the device node is either |
| 19 | disabled or removed all together, the affected device should be deregistered. |
| 20 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 21 | Lets take an example where we have a foo board with the following base tree:: |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 22 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 23 | ---- foo.dts --------------------------------------------------------------- |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 24 | /* FOO platform */ |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 25 | /dts-v1/; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 26 | / { |
| 27 | compatible = "corp,foo"; |
| 28 | |
| 29 | /* shared resources */ |
| 30 | res: res { |
| 31 | }; |
| 32 | |
| 33 | /* On chip peripherals */ |
| 34 | ocp: ocp { |
| 35 | /* peripherals that are always instantiated */ |
| 36 | peripheral1 { ... }; |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 37 | }; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 38 | }; |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 39 | ---- foo.dts --------------------------------------------------------------- |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 40 | |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 41 | The overlay bar.dts, |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 42 | :: |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 43 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 44 | ---- bar.dts - overlay target location by label ---------------------------- |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 45 | /dts-v1/; |
| 46 | /plugin/; |
| 47 | &ocp { |
| 48 | /* bar peripheral */ |
| 49 | bar { |
| 50 | compatible = "corp,bar"; |
| 51 | ... /* various properties and child nodes */ |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 52 | }; |
| 53 | }; |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 54 | ---- bar.dts --------------------------------------------------------------- |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 55 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 56 | when loaded (and resolved as described in [1]) should result in foo+bar.dts:: |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 57 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 58 | ---- foo+bar.dts ----------------------------------------------------------- |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 59 | /* FOO platform + bar peripheral */ |
| 60 | / { |
| 61 | compatible = "corp,foo"; |
| 62 | |
| 63 | /* shared resources */ |
| 64 | res: res { |
| 65 | }; |
| 66 | |
| 67 | /* On chip peripherals */ |
| 68 | ocp: ocp { |
| 69 | /* peripherals that are always instantiated */ |
| 70 | peripheral1 { ... }; |
| 71 | |
| 72 | /* bar peripheral */ |
| 73 | bar { |
| 74 | compatible = "corp,bar"; |
| 75 | ... /* various properties and child nodes */ |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 76 | }; |
| 77 | }; |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 78 | }; |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 79 | ---- foo+bar.dts ----------------------------------------------------------- |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 80 | |
Masanari Iida | ac3e8ea | 2015-01-02 22:54:39 +0900 | [diff] [blame] | 81 | As a result of the overlay, a new device node (bar) has been created |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 82 | so a bar platform device will be registered and if a matching device driver |
| 83 | is loaded the device will be created as expected. |
| 84 | |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 85 | If the base DT was not compiled with the -@ option then the "&ocp" label |
| 86 | will not be available to resolve the overlay node(s) to the proper location |
| 87 | in the base DT. In this case, the target path can be provided. The target |
| 88 | location by label syntax is preferred because the overlay can be applied to |
| 89 | any base DT containing the label, no matter where the label occurs in the DT. |
| 90 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 91 | The above bar.dts example modified to use target path syntax is:: |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 92 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 93 | ---- bar.dts - overlay target location by explicit path -------------------- |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 94 | /dts-v1/; |
| 95 | /plugin/; |
| 96 | &{/ocp} { |
| 97 | /* bar peripheral */ |
| 98 | bar { |
| 99 | compatible = "corp,bar"; |
| 100 | ... /* various properties and child nodes */ |
| 101 | } |
| 102 | }; |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 103 | ---- bar.dts --------------------------------------------------------------- |
Frank Rowand | 9ae8578 | 2020-01-27 18:37:18 -0600 | [diff] [blame] | 104 | |
| 105 | |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 106 | Overlay in-kernel API |
| 107 | -------------------------------- |
| 108 | |
| 109 | The API is quite easy to use. |
| 110 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 111 | 1) Call of_overlay_fdt_apply() to create and apply an overlay changeset. The |
| 112 | return value is an error or a cookie identifying this overlay. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 113 | |
Mauro Carvalho Chehab | 642e6e5c | 2020-04-15 16:45:22 +0200 | [diff] [blame] | 114 | 2) Call of_overlay_remove() to remove and cleanup the overlay changeset |
| 115 | previously created via the call to of_overlay_fdt_apply(). Removal of an |
| 116 | overlay changeset that is stacked by another will not be permitted. |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 117 | |
| 118 | Finally, if you need to remove all overlays in one-go, just call |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 119 | of_overlay_remove_all() which will remove every single one in the correct |
Pantelis Antoniou | 7518b589 | 2014-10-28 22:35:58 +0200 | [diff] [blame] | 120 | order. |
| 121 | |
Jan Kiszka | 83ef477 | 2018-04-26 13:00:30 +0200 | [diff] [blame] | 122 | In addition, there is the option to register notifiers that get called on |
| 123 | overlay operations. See of_overlay_notifier_register/unregister and |
| 124 | enum of_overlay_notify_action for details. |
| 125 | |
| 126 | Note that a notifier callback is not supposed to store pointers to a device |
| 127 | tree node or its content beyond OF_OVERLAY_POST_REMOVE corresponding to the |
| 128 | respective node it received. |