Platform Device model and APIs in Linux
```
platform_device_add() is 2nd stage platform_device_register.It needed to called if and only if platform devices are allocated by platform_device_alloc().Else platform_device_register() is enough for platform device addition to the system/attaching to platform driver. ```<pre style="background-color: white; overflow: auto; padding: 1em;"><span style="font-size: 15.454545021057129px;"><span style="color: blue; font-family: Georgia, Times New Roman, serif;"><b>platform_set_drvdata(struct platform_device *pdev,void *devdata)</b></span><span style="font-family: Verdana, sans-serif;"> are normally used to set private before calling </span></span><span style="font-size: 15.454545021057129px;"><span style="color: blue; font-family: Georgia, Times New Roman, serif;"><b>platform_device_add()</b></span></span>
<pre style="overflow: auto; padding: 1em;"><span style="font-size: 0.95em;"><span style="color: blue; font-family: Georgia, Times New Roman, serif;"><b><a href="http://lxr.free-electrons.com/ident?i=module_platform_driver" style="border-bottom-color: #999999; border-bottom-style: dotted; border-bottom-width: 1px; text-decoration: none;">module_platform_driver</a>(struct platform_driver)</b></span></span><span style="color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold;">; </span><span style="font-family: Verdana, sans-serif;">is a macro used to implement module_init and call platform_driver_register and unregister it.</span>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Verdana, sans-serif;"> </span>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Verdana, sans-serif;"><b><u>Adding resource to platform devices</u></b></span>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Georgia, Times New Roman, serif; font-size: x-small;"> static struct resource foomatic_resources[] = { { .start = 0x10000000, .end = 0x10001000, .flags = IORESOURCE_MEM, .name = "io-memory" }, { .start = 20, .end = 20, .flags = IORESOURCE_IRQ, .name = "irq", } }; static struct platform_device my_foomatic = { .name = "foomatic", .resource = foomatic_resources, .num_resources = ARRAY_SIZE(foomatic_resources), };</span>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Georgia, Times New Roman, serif; font-size: x-small;"> </span>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Georgia, Times New Roman, serif; font-size: x-small;">To get those resource we use </span>
<pre style="overflow: auto; padding: 1em;"><b><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small;">struct <a href="http://lxr.free-electrons.com/ident?i=resource" style="border-bottom-color: #999999; border-bottom-style: dotted; border-bottom-width: 1px; text-decoration: none;">resource</a> *<a href="http://lxr.free-electrons.com/ident?i=platform_get_resource" style="border-bottom-color: #999999; border-bottom-style: dotted; border-bottom-width: 1px; text-decoration: none;">platform_get_resource</a>(struct <a href="http://lxr.free-electrons.com/ident?i=platform_device" style="border-bottom-color: #999999; border-bottom-style: dotted; border-bottom-width: 1px; text-decoration: none;">platform_device</a> *,unsigned int, unsigned int);</span></b>
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Georgia, Times New Roman, serif; font-size: x-small; white-space: normal;">(</span><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small; white-space: normal;">struct </span>
<pre style="overflow: auto; padding: 1em;"><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small; white-space: normal;"> *res) </span><span style="font-family: Verdana, sans-serif; font-size: small; white-space: normal;">is used to calculate the resource size.</span>
<pre style="color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold; overflow: auto; padding: 1em;">
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Verdana, sans-serif;">We can <u>add platform devices from device tree</u> ,then we can use </span><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small;"><b>MODULE _DEVICE_TABLE</b></span>
<pre style="overflow: auto; padding: 1em;"> <span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small;"><b> static const struct of_device_id my_of_ids[] = { { .compatible = "long,funky-device-tree-name" }, { } };</b></span>
<pre style="overflow: auto; padding: 1em;">When the platform driver is declared, it stores a pointer to this table in the driver substructure:
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Georgia, Times New Roman, serif; font-size: x-small;"> <span style="color: blue;"><b> static struct platform_driver my_driver = { /* ... */ .driver = { .name = "my-driver", .of_match_table = my_of_ids </b></span></span><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small;"><b>} };</b></span><span style="font-family: Verdana, sans-serif;"> The driver can also declare the ID table as a device table to enable autoloading of the module as the device tree is instantiated:</span><span style="color: #787878; font-family: Verdana, sans-serif;"> </span>
<pre style="overflow: auto; padding: 1em;"><span style="color: blue; font-family: Georgia, Times New Roman, serif; font-size: x-small;"><b> MODULE_DEVICE_TABLE(of, my_of_ids);</b></span>
<pre style="overflow: auto; padding: 1em;"><span style="color: #787878; font-family: Monaco, Courier New, Courier, monospace;"><span style="font-size: 15.454545021057129px;"><b> </b></span></span>
<pre style="color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold; overflow: auto; padding: 1em;">
<pre style="color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold; overflow: auto; padding: 1em;">
http://lwn.net/Articles/448499/http://lwn.net/Articles/448502/
<pre style="overflow: auto; padding: 1em;"><span style="font-family: Verdana, sans-serif;"> </span>
<pre style="background-color: white; color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold; overflow: auto; padding: 1em;"><b><i> </i></b>
<pre style="background-color: white; color: #787878; font-family: Monaco, 'Courier New', Courier, monospace; font-size: 0.95em; font-weight: bold; overflow: auto; padding: 1em;">