Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * platform_device.h - generic, centralized driver model
0004  *
0005  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
0006  *
0007  * See Documentation/driver-api/driver-model/ for more information.
0008  */
0009 
0010 #ifndef _PLATFORM_DEVICE_H_
0011 #define _PLATFORM_DEVICE_H_
0012 
0013 #include <linux/device.h>
0014 
0015 #define PLATFORM_DEVID_NONE (-1)
0016 #define PLATFORM_DEVID_AUTO (-2)
0017 
0018 struct irq_affinity;
0019 struct mfd_cell;
0020 struct property_entry;
0021 struct platform_device_id;
0022 
0023 struct platform_device {
0024     const char  *name;
0025     int     id;
0026     bool        id_auto;
0027     struct device   dev;
0028     u64     platform_dma_mask;
0029     struct device_dma_parameters dma_parms;
0030     u32     num_resources;
0031     struct resource *resource;
0032 
0033     const struct platform_device_id *id_entry;
0034     /*
0035      * Driver name to force a match.  Do not set directly, because core
0036      * frees it.  Use driver_set_override() to set or clear it.
0037      */
0038     const char *driver_override;
0039 
0040     /* MFD cell pointer */
0041     struct mfd_cell *mfd_cell;
0042 
0043     /* arch specific additions */
0044     struct pdev_archdata    archdata;
0045 };
0046 
0047 #define platform_get_device_id(pdev)    ((pdev)->id_entry)
0048 
0049 #define dev_is_platform(dev) ((dev)->bus == &platform_bus_type)
0050 #define to_platform_device(x) container_of((x), struct platform_device, dev)
0051 
0052 extern int platform_device_register(struct platform_device *);
0053 extern void platform_device_unregister(struct platform_device *);
0054 
0055 extern struct bus_type platform_bus_type;
0056 extern struct device platform_bus;
0057 
0058 extern struct resource *platform_get_resource(struct platform_device *,
0059                           unsigned int, unsigned int);
0060 extern struct resource *platform_get_mem_or_io(struct platform_device *,
0061                            unsigned int);
0062 
0063 extern struct device *
0064 platform_find_device_by_driver(struct device *start,
0065                    const struct device_driver *drv);
0066 extern void __iomem *
0067 devm_platform_get_and_ioremap_resource(struct platform_device *pdev,
0068                 unsigned int index, struct resource **res);
0069 extern void __iomem *
0070 devm_platform_ioremap_resource(struct platform_device *pdev,
0071                    unsigned int index);
0072 extern void __iomem *
0073 devm_platform_ioremap_resource_byname(struct platform_device *pdev,
0074                       const char *name);
0075 extern int platform_get_irq(struct platform_device *, unsigned int);
0076 extern int platform_get_irq_optional(struct platform_device *, unsigned int);
0077 extern int platform_irq_count(struct platform_device *);
0078 extern int devm_platform_get_irqs_affinity(struct platform_device *dev,
0079                        struct irq_affinity *affd,
0080                        unsigned int minvec,
0081                        unsigned int maxvec,
0082                        int **irqs);
0083 extern struct resource *platform_get_resource_byname(struct platform_device *,
0084                              unsigned int,
0085                              const char *);
0086 extern int platform_get_irq_byname(struct platform_device *, const char *);
0087 extern int platform_get_irq_byname_optional(struct platform_device *dev,
0088                         const char *name);
0089 extern int platform_add_devices(struct platform_device **, int);
0090 
0091 struct platform_device_info {
0092         struct device *parent;
0093         struct fwnode_handle *fwnode;
0094         bool of_node_reused;
0095 
0096         const char *name;
0097         int id;
0098 
0099         const struct resource *res;
0100         unsigned int num_res;
0101 
0102         const void *data;
0103         size_t size_data;
0104         u64 dma_mask;
0105 
0106         const struct property_entry *properties;
0107 };
0108 extern struct platform_device *platform_device_register_full(
0109         const struct platform_device_info *pdevinfo);
0110 
0111 /**
0112  * platform_device_register_resndata - add a platform-level device with
0113  * resources and platform-specific data
0114  *
0115  * @parent: parent device for the device we're adding
0116  * @name: base name of the device we're adding
0117  * @id: instance id
0118  * @res: set of resources that needs to be allocated for the device
0119  * @num: number of resources
0120  * @data: platform specific data for this platform device
0121  * @size: size of platform specific data
0122  *
0123  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
0124  */
0125 static inline struct platform_device *platform_device_register_resndata(
0126         struct device *parent, const char *name, int id,
0127         const struct resource *res, unsigned int num,
0128         const void *data, size_t size) {
0129 
0130     struct platform_device_info pdevinfo = {
0131         .parent = parent,
0132         .name = name,
0133         .id = id,
0134         .res = res,
0135         .num_res = num,
0136         .data = data,
0137         .size_data = size,
0138         .dma_mask = 0,
0139     };
0140 
0141     return platform_device_register_full(&pdevinfo);
0142 }
0143 
0144 /**
0145  * platform_device_register_simple - add a platform-level device and its resources
0146  * @name: base name of the device we're adding
0147  * @id: instance id
0148  * @res: set of resources that needs to be allocated for the device
0149  * @num: number of resources
0150  *
0151  * This function creates a simple platform device that requires minimal
0152  * resource and memory management. Canned release function freeing memory
0153  * allocated for the device allows drivers using such devices to be
0154  * unloaded without waiting for the last reference to the device to be
0155  * dropped.
0156  *
0157  * This interface is primarily intended for use with legacy drivers which
0158  * probe hardware directly.  Because such drivers create sysfs device nodes
0159  * themselves, rather than letting system infrastructure handle such device
0160  * enumeration tasks, they don't fully conform to the Linux driver model.
0161  * In particular, when such drivers are built as modules, they can't be
0162  * "hotplugged".
0163  *
0164  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
0165  */
0166 static inline struct platform_device *platform_device_register_simple(
0167         const char *name, int id,
0168         const struct resource *res, unsigned int num)
0169 {
0170     return platform_device_register_resndata(NULL, name, id,
0171             res, num, NULL, 0);
0172 }
0173 
0174 /**
0175  * platform_device_register_data - add a platform-level device with platform-specific data
0176  * @parent: parent device for the device we're adding
0177  * @name: base name of the device we're adding
0178  * @id: instance id
0179  * @data: platform specific data for this platform device
0180  * @size: size of platform specific data
0181  *
0182  * This function creates a simple platform device that requires minimal
0183  * resource and memory management. Canned release function freeing memory
0184  * allocated for the device allows drivers using such devices to be
0185  * unloaded without waiting for the last reference to the device to be
0186  * dropped.
0187  *
0188  * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
0189  */
0190 static inline struct platform_device *platform_device_register_data(
0191         struct device *parent, const char *name, int id,
0192         const void *data, size_t size)
0193 {
0194     return platform_device_register_resndata(parent, name, id,
0195             NULL, 0, data, size);
0196 }
0197 
0198 extern struct platform_device *platform_device_alloc(const char *name, int id);
0199 extern int platform_device_add_resources(struct platform_device *pdev,
0200                      const struct resource *res,
0201                      unsigned int num);
0202 extern int platform_device_add_data(struct platform_device *pdev,
0203                     const void *data, size_t size);
0204 extern int platform_device_add(struct platform_device *pdev);
0205 extern void platform_device_del(struct platform_device *pdev);
0206 extern void platform_device_put(struct platform_device *pdev);
0207 
0208 struct platform_driver {
0209     int (*probe)(struct platform_device *);
0210     int (*remove)(struct platform_device *);
0211     void (*shutdown)(struct platform_device *);
0212     int (*suspend)(struct platform_device *, pm_message_t state);
0213     int (*resume)(struct platform_device *);
0214     struct device_driver driver;
0215     const struct platform_device_id *id_table;
0216     bool prevent_deferred_probe;
0217     /*
0218      * For most device drivers, no need to care about this flag as long as
0219      * all DMAs are handled through the kernel DMA API. For some special
0220      * ones, for example VFIO drivers, they know how to manage the DMA
0221      * themselves and set this flag so that the IOMMU layer will allow them
0222      * to setup and manage their own I/O address space.
0223      */
0224     bool driver_managed_dma;
0225 };
0226 
0227 #define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
0228                  driver))
0229 
0230 /*
0231  * use a macro to avoid include chaining to get THIS_MODULE
0232  */
0233 #define platform_driver_register(drv) \
0234     __platform_driver_register(drv, THIS_MODULE)
0235 extern int __platform_driver_register(struct platform_driver *,
0236                     struct module *);
0237 extern void platform_driver_unregister(struct platform_driver *);
0238 
0239 /* non-hotpluggable platform devices may use this so that probe() and
0240  * its support may live in __init sections, conserving runtime memory.
0241  */
0242 #define platform_driver_probe(drv, probe) \
0243     __platform_driver_probe(drv, probe, THIS_MODULE)
0244 extern int __platform_driver_probe(struct platform_driver *driver,
0245         int (*probe)(struct platform_device *), struct module *module);
0246 
0247 static inline void *platform_get_drvdata(const struct platform_device *pdev)
0248 {
0249     return dev_get_drvdata(&pdev->dev);
0250 }
0251 
0252 static inline void platform_set_drvdata(struct platform_device *pdev,
0253                     void *data)
0254 {
0255     dev_set_drvdata(&pdev->dev, data);
0256 }
0257 
0258 /* module_platform_driver() - Helper macro for drivers that don't do
0259  * anything special in module init/exit.  This eliminates a lot of
0260  * boilerplate.  Each module may only use this macro once, and
0261  * calling it replaces module_init() and module_exit()
0262  */
0263 #define module_platform_driver(__platform_driver) \
0264     module_driver(__platform_driver, platform_driver_register, \
0265             platform_driver_unregister)
0266 
0267 /* builtin_platform_driver() - Helper macro for builtin drivers that
0268  * don't do anything special in driver init.  This eliminates some
0269  * boilerplate.  Each driver may only use this macro once, and
0270  * calling it replaces device_initcall().  Note this is meant to be
0271  * a parallel of module_platform_driver() above, but w/o _exit stuff.
0272  */
0273 #define builtin_platform_driver(__platform_driver) \
0274     builtin_driver(__platform_driver, platform_driver_register)
0275 
0276 /* module_platform_driver_probe() - Helper macro for drivers that don't do
0277  * anything special in module init/exit.  This eliminates a lot of
0278  * boilerplate.  Each module may only use this macro once, and
0279  * calling it replaces module_init() and module_exit()
0280  */
0281 #define module_platform_driver_probe(__platform_driver, __platform_probe) \
0282 static int __init __platform_driver##_init(void) \
0283 { \
0284     return platform_driver_probe(&(__platform_driver), \
0285                      __platform_probe);    \
0286 } \
0287 module_init(__platform_driver##_init); \
0288 static void __exit __platform_driver##_exit(void) \
0289 { \
0290     platform_driver_unregister(&(__platform_driver)); \
0291 } \
0292 module_exit(__platform_driver##_exit);
0293 
0294 /* builtin_platform_driver_probe() - Helper macro for drivers that don't do
0295  * anything special in device init.  This eliminates some boilerplate.  Each
0296  * driver may only use this macro once, and using it replaces device_initcall.
0297  * This is meant to be a parallel of module_platform_driver_probe above, but
0298  * without the __exit parts.
0299  */
0300 #define builtin_platform_driver_probe(__platform_driver, __platform_probe) \
0301 static int __init __platform_driver##_init(void) \
0302 { \
0303     return platform_driver_probe(&(__platform_driver), \
0304                      __platform_probe);    \
0305 } \
0306 device_initcall(__platform_driver##_init); \
0307 
0308 #define platform_create_bundle(driver, probe, res, n_res, data, size) \
0309     __platform_create_bundle(driver, probe, res, n_res, data, size, THIS_MODULE)
0310 extern struct platform_device *__platform_create_bundle(
0311     struct platform_driver *driver, int (*probe)(struct platform_device *),
0312     struct resource *res, unsigned int n_res,
0313     const void *data, size_t size, struct module *module);
0314 
0315 int __platform_register_drivers(struct platform_driver * const *drivers,
0316                 unsigned int count, struct module *owner);
0317 void platform_unregister_drivers(struct platform_driver * const *drivers,
0318                  unsigned int count);
0319 
0320 #define platform_register_drivers(drivers, count) \
0321     __platform_register_drivers(drivers, count, THIS_MODULE)
0322 
0323 #ifdef CONFIG_SUSPEND
0324 extern int platform_pm_suspend(struct device *dev);
0325 extern int platform_pm_resume(struct device *dev);
0326 #else
0327 #define platform_pm_suspend     NULL
0328 #define platform_pm_resume      NULL
0329 #endif
0330 
0331 #ifdef CONFIG_HIBERNATE_CALLBACKS
0332 extern int platform_pm_freeze(struct device *dev);
0333 extern int platform_pm_thaw(struct device *dev);
0334 extern int platform_pm_poweroff(struct device *dev);
0335 extern int platform_pm_restore(struct device *dev);
0336 #else
0337 #define platform_pm_freeze      NULL
0338 #define platform_pm_thaw        NULL
0339 #define platform_pm_poweroff        NULL
0340 #define platform_pm_restore     NULL
0341 #endif
0342 
0343 #ifdef CONFIG_PM_SLEEP
0344 #define USE_PLATFORM_PM_SLEEP_OPS \
0345     .suspend = platform_pm_suspend, \
0346     .resume = platform_pm_resume, \
0347     .freeze = platform_pm_freeze, \
0348     .thaw = platform_pm_thaw, \
0349     .poweroff = platform_pm_poweroff, \
0350     .restore = platform_pm_restore,
0351 #else
0352 #define USE_PLATFORM_PM_SLEEP_OPS
0353 #endif
0354 
0355 #ifndef CONFIG_SUPERH
0356 /*
0357  * REVISIT: This stub is needed for all non-SuperH users of early platform
0358  * drivers. It should go away once we introduce the new platform_device-based
0359  * early driver framework.
0360  */
0361 static inline int is_sh_early_platform_device(struct platform_device *pdev)
0362 {
0363     return 0;
0364 }
0365 #endif /* CONFIG_SUPERH */
0366 
0367 /* For now only SuperH uses it */
0368 void early_platform_cleanup(void);
0369 
0370 #endif /* _PLATFORM_DEVICE_H_ */