blob: 05eab4c5f9a0ddc8c448608aaf7415c3b1f958d7 [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>
Alex Elder177404b2014-10-03 14:14:24 -050021#include <linux/idr.h>
Alex Eldere1e9dbd2014-10-01 21:54:11 -050022
Alex Elder1bb3c722014-10-02 12:30:03 -050023#include "kernel_ver.h"
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"
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +020028#include "endo.h"
Alex Elder30c6d9d2015-05-22 13:02:08 -050029#include "svc.h"
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080030#include "module.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080031#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050032#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050033#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050034#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050035#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080036
37
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
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080042#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070043 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_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) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046 .match_flags = GREYBUS_DEVICE_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
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070050#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080051 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070052 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080053
Alex Elder1bb3c722014-10-02 12:30:03 -050054/* XXX I couldn't get my Kconfig file to be noticed for out-of-tree build */
55#ifndef CONFIG_HOST_DEV_CPORT_ID_MAX
56#define CONFIG_HOST_DEV_CPORT_ID_MAX 128
57#endif /* !CONFIG_HOST_DEV_CPORT_ID_MAX */
58
59/* Maximum number of CPorts usable by a host device */
60/* XXX This should really be determined by the AP module manifest */
61#define HOST_DEV_CPORT_ID_MAX CONFIG_HOST_DEV_CPORT_ID_MAX
62#define CPORT_ID_BAD U16_MAX /* UniPro max id is 4095 */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080063
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070064/* For SP1 hardware, we are going to "hardcode" each device to have all logical
65 * blocks in order to be able to address them as one unified "unit". Then
66 * higher up layers will then be able to talk to them as one logical block and
67 * properly know how they are hooked together (i.e. which i2c port is on the
68 * same module as the gpio pins, etc.)
69 *
70 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -050071 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070072 */
73
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070074struct greybus_host_device;
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070075struct svc_msg;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070076
77/* Greybus "Host driver" structure, needed by a host controller driver to be
78 * able to handle both SVC control as well as "real" greybus messages
79 */
80struct greybus_host_driver {
81 size_t hd_priv_size;
82
Johan Hovold7cf7bca2015-04-07 11:27:16 +020083 void *(*message_send)(struct greybus_host_device *hd, u16 dest_cport_id,
84 struct gb_message *message, gfp_t gfp_mask);
85 void (*message_cancel)(void *cookie);
Alex Elder0db32a62014-09-24 05:16:14 -050086 int (*submit_svc)(struct svc_msg *svc_msg,
Greg Kroah-Hartmanf036e052014-09-19 19:13:33 -070087 struct greybus_host_device *hd);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070088};
89
90struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070091 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -070092 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070093 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070094
Greg Kroah-Hartman1cd56a82014-12-19 14:56:36 -080095 struct list_head interfaces;
Alex Elder2c43ce42014-11-17 08:08:44 -060096 struct list_head connections;
Alex Elder177404b2014-10-03 14:14:24 -050097 struct ida cport_id_map;
Matt Porter98f4ab22014-10-21 01:52:27 -040098 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -050099
Alex Elder8b337302014-11-20 16:09:13 -0600100 /* Host device buffer constraints */
Alex Elder8b337302014-11-20 16:09:13 -0600101 size_t buffer_size_max;
102
Greg Kroah-Hartmana4d91502015-04-07 20:27:15 +0200103 struct gb_endo *endo;
104
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700105 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700106 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700107};
108
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700109struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Johan Hovoldd9336672015-05-19 11:22:43 +0200110 struct device *parent,
111 size_t buffer_size_max);
Alex Eldereb765e42015-05-22 12:56:49 -0500112int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id,
113 u8 ap_intf_id);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700114void greybus_remove_hd(struct greybus_host_device *hd);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800115
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800116struct greybus_driver {
117 const char *name;
118
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530119 int (*probe)(struct gb_bundle *bundle,
120 const struct greybus_bundle_id *id);
121 void (*disconnect)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800122
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530123 int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
124 int (*resume)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800125
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530126 const struct greybus_bundle_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800127
128 struct device_driver driver;
129};
130#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
131
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800132/* Don't call these directly, use the module_greybus_driver() macro instead */
133int greybus_register_driver(struct greybus_driver *driver,
134 struct module *module, const char *mod_name);
Alex Elderfd1c2e52015-06-08 12:05:13 -0500135void greybus_deregister_driver(struct greybus_driver *driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800136
137/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
138#define greybus_register(driver) \
139 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Alex Elderfd1c2e52015-06-08 12:05:13 -0500140#define greybus_deregister(driver) \
141 greybus_deregister_driver(driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800142
143/**
144 * module_greybus_driver() - Helper macro for registering a Greybus driver
145 * @__greybus_driver: greybus_driver structure
146 *
147 * Helper macro for Greybus drivers to set up proper module init / exit
148 * functions. Replaces module_init() and module_exit() and keeps people from
149 * printing pointless things to the kernel log when their driver is loaded.
150 */
151#define module_greybus_driver(__greybus_driver) \
152 module_driver(__greybus_driver, greybus_register, greybus_deregister)
153
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800154int greybus_disabled(void);
155
Alex Elder51c75fd2014-09-26 20:55:35 -0500156int greybus_svc_in(struct greybus_host_device *hd, u8 *data, int length);
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700157int gb_ap_init(void);
158void gb_ap_exit(void);
Greg Kroah-Hartman48f70472015-03-27 11:38:06 +0100159void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700160void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100161struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700162
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800163extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700164
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500165int svc_set_route_send(struct gb_bundle *bundle,
Alex Elder525f1462014-10-22 02:04:31 -0500166 struct greybus_host_device *hd);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800167
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200168extern struct device_type greybus_endo_type;
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800169extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800170extern struct device_type greybus_interface_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500171extern struct device_type greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800172extern struct device_type greybus_connection_type;
173
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200174static inline int is_gb_endo(const struct device *dev)
175{
176 return dev->type == &greybus_endo_type;
177}
178
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800179static inline int is_gb_module(const struct device *dev)
180{
181 return dev->type == &greybus_module_type;
182}
183
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800184static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800185{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800186 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800187}
188
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500189static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800190{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500191 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800192}
193
194static inline int is_gb_connection(const struct device *dev)
195{
196 return dev->type == &greybus_connection_type;
197}
198
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800199#endif /* __KERNEL__ */
200#endif /* __LINUX_GREYBUS_H */