blob: 12526887ae2e72030ea2654a13ab69f96fc9cfe4 [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus driver and device API
3 *
Alex Elder4441f472015-05-22 12:59:16 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08006 *
7 * Released under the GPLv2 only.
8 */
9
10#ifndef __LINUX_GREYBUS_H
11#define __LINUX_GREYBUS_H
12
13#ifdef __KERNEL__
14
Alex Eldere1e9dbd2014-10-01 21:54:11 -050015#include <linux/kernel.h>
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070016#include <linux/types.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080017#include <linux/list.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050018#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080019#include <linux/device.h>
20#include <linux/module.h>
David Lin30a3bf72016-07-14 15:13:00 -050021#include <linux/pm_runtime.h>
Alex Elder177404b2014-10-03 14:14:24 -050022#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050023
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080024#include "greybus_id.h"
Alex Elder05ad1892014-09-09 13:55:03 -050025#include "greybus_manifest.h"
Alex Elder22e17ed2015-05-22 12:35:32 -050026#include "greybus_protocols.h"
Alex Elderb09c94a2014-10-01 21:54:16 -050027#include "manifest.h"
Johan Hovold7bc6faa2015-11-03 18:03:22 +010028#include "hd.h"
Alex Elder30c6d9d2015-05-22 13:02:08 -050029#include "svc.h"
Viresh Kumarcdee4f72015-06-22 16:42:26 +053030#include "control.h"
Johan Hovoldb15d97d2016-04-23 18:47:24 +020031#include "module.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080032#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050033#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050034#include "connection.h"
Alex Eldere88afa52014-10-01 21:54:15 -050035#include "operation.h"
Bryan O'Donoghue970dc852016-06-05 14:03:26 +010036#include "timesync.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070038/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040039#define GREYBUS_VERSION_MAJOR 0x00
40#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070041
Johan Hovold358e9402015-11-21 10:51:59 +010042#define GREYBUS_ID_MATCH_DEVICE \
43 (GREYBUS_ID_MATCH_VENDOR | GREYBUS_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070045#define GREYBUS_DEVICE(v, p) \
Johan Hovold358e9402015-11-21 10:51:59 +010046 .match_flags = GREYBUS_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070047 .vendor = (v), \
48 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080049
Johan Hovold3823c612015-11-21 10:52:04 +010050#define GREYBUS_DEVICE_CLASS(c) \
51 .match_flags = GREYBUS_ID_MATCH_CLASS, \
52 .class = (c),
53
Johan Hovold1dc53922015-09-02 18:03:21 +020054/* Maximum number of CPorts */
55#define CPORT_ID_MAX 4095 /* UniPro max id is 4095 */
56#define CPORT_ID_BAD U16_MAX
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080057
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080058struct greybus_driver {
59 const char *name;
60
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053061 int (*probe)(struct gb_bundle *bundle,
62 const struct greybus_bundle_id *id);
63 void (*disconnect)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080064
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053065 const struct greybus_bundle_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080066
67 struct device_driver driver;
68};
69#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
70
Johan Hovold13da9e12016-01-08 20:13:43 +010071static inline void greybus_set_drvdata(struct gb_bundle *bundle, void *data)
72{
73 dev_set_drvdata(&bundle->dev, data);
74}
75
76static inline void *greybus_get_drvdata(struct gb_bundle *bundle)
77{
78 return dev_get_drvdata(&bundle->dev);
79}
80
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080081/* Don't call these directly, use the module_greybus_driver() macro instead */
82int greybus_register_driver(struct greybus_driver *driver,
83 struct module *module, const char *mod_name);
Alex Elderfd1c2e52015-06-08 12:05:13 -050084void greybus_deregister_driver(struct greybus_driver *driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080085
86/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
87#define greybus_register(driver) \
88 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Alex Elderfd1c2e52015-06-08 12:05:13 -050089#define greybus_deregister(driver) \
90 greybus_deregister_driver(driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080091
92/**
93 * module_greybus_driver() - Helper macro for registering a Greybus driver
94 * @__greybus_driver: greybus_driver structure
95 *
96 * Helper macro for Greybus drivers to set up proper module init / exit
97 * functions. Replaces module_init() and module_exit() and keeps people from
98 * printing pointless things to the kernel log when their driver is loaded.
99 */
100#define module_greybus_driver(__greybus_driver) \
101 module_driver(__greybus_driver, greybus_register, greybus_deregister)
102
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800103int greybus_disabled(void);
104
Greg Kroah-Hartman3d0421e2015-06-11 09:22:51 -0700105void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700106void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100107struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700108
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800109extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700110
Johan Hovold2adaefb2015-11-25 15:59:02 +0100111extern struct device_type greybus_hd_type;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200112extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800113extern struct device_type greybus_interface_type;
Johan Hovolda6e5b012016-04-13 19:19:02 +0200114extern struct device_type greybus_control_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500115extern struct device_type greybus_bundle_type;
Johan Hovold88f7b962015-11-25 15:59:08 +0100116extern struct device_type greybus_svc_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800117
Johan Hovold2adaefb2015-11-25 15:59:02 +0100118static inline int is_gb_host_device(const struct device *dev)
119{
120 return dev->type == &greybus_hd_type;
121}
122
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200123static inline int is_gb_module(const struct device *dev)
124{
125 return dev->type == &greybus_module_type;
126}
127
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800128static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800129{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800130 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800131}
132
Johan Hovolda6e5b012016-04-13 19:19:02 +0200133static inline int is_gb_control(const struct device *dev)
134{
135 return dev->type == &greybus_control_type;
136}
137
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500138static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800139{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500140 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800141}
142
Johan Hovold88f7b962015-11-25 15:59:08 +0100143static inline int is_gb_svc(const struct device *dev)
144{
145 return dev->type == &greybus_svc_type;
146}
147
Johan Hovold25376362015-11-03 18:03:23 +0100148static inline bool cport_id_valid(struct gb_host_device *hd, u16 cport_id)
Alex Elder821c6202015-06-13 11:02:07 -0500149{
Fabien Parent144670c2015-09-02 15:50:35 +0200150 return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
Alex Elder821c6202015-06-13 11:02:07 -0500151}
152
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800153#endif /* __KERNEL__ */
154#endif /* __LINUX_GREYBUS_H */