blob: 8e215f870c1d0d4dfa8450b70995f6af470905ae [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"
Viresh Kumar90f1b612015-08-12 09:19:33 +053030#include "firmware.h"
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080031#include "module.h"
Viresh Kumarcdee4f72015-06-22 16:42:26 +053032#include "control.h"
Greg Kroah-Hartmana93938a2014-12-19 14:56:30 -080033#include "interface.h"
Greg Kroah-Hartman3bdec692014-12-12 17:10:16 -050034#include "bundle.h"
Alex Elderc68adb22014-10-01 21:54:14 -050035#include "connection.h"
Alex Elder4ccb6b72014-10-28 19:36:00 -050036#include "protocol.h"
Alex Eldere88afa52014-10-01 21:54:15 -050037#include "operation.h"
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080038
39
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070040/* Matches up with the Greybus Protocol specification document */
Matt Porter52adb562014-09-18 15:25:43 -040041#define GREYBUS_VERSION_MAJOR 0x00
42#define GREYBUS_VERSION_MINOR 0x01
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070043
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044#define GREYBUS_DEVICE_ID_MATCH_DEVICE \
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -070045 (GREYBUS_DEVICE_ID_MATCH_VENDOR | GREYBUS_DEVICE_ID_MATCH_PRODUCT)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070047#define GREYBUS_DEVICE(v, p) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080048 .match_flags = GREYBUS_DEVICE_ID_MATCH_DEVICE, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070049 .vendor = (v), \
50 .product = (p),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080051
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070052#define GREYBUS_DEVICE_SERIAL(s) \
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080053 .match_flags = GREYBUS_DEVICE_ID_MATCH_SERIAL, \
Greg Kroah-Hartman6dca7b92014-09-01 13:42:43 -070054 .serial_number = (s),
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080055
Johan Hovold1dc53922015-09-02 18:03:21 +020056/* Maximum number of CPorts */
57#define CPORT_ID_MAX 4095 /* UniPro max id is 4095 */
58#define CPORT_ID_BAD U16_MAX
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080059
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070060/* For SP1 hardware, we are going to "hardcode" each device to have all logical
61 * blocks in order to be able to address them as one unified "unit". Then
62 * higher up layers will then be able to talk to them as one logical block and
63 * properly know how they are hooked together (i.e. which i2c port is on the
64 * same module as the gpio pins, etc.)
65 *
66 * So, put the "private" data structures here in greybus.h and link to them off
Alex Eldere1e9dbd2014-10-01 21:54:11 -050067 * of the "main" gb_module structure.
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -070068 */
69
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070070struct greybus_host_device;
71
72/* Greybus "Host driver" structure, needed by a host controller driver to be
73 * able to handle both SVC control as well as "real" greybus messages
74 */
75struct greybus_host_driver {
76 size_t hd_priv_size;
77
Johan Hovold3e136cc2015-07-01 12:37:21 +020078 int (*message_send)(struct greybus_host_device *hd, u16 dest_cport_id,
Johan Hovold7cf7bca2015-04-07 11:27:16 +020079 struct gb_message *message, gfp_t gfp_mask);
Johan Hovold3e136cc2015-07-01 12:37:21 +020080 void (*message_cancel)(struct gb_message *message);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070081};
82
83struct greybus_host_device {
Greg Kroah-Hartman8c53e072014-09-12 20:47:11 -070084 struct kref kref;
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -070085 struct device *parent;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070086 const struct greybus_host_driver *driver;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070087
Greg Kroah-Hartman1cd56a82014-12-19 14:56:36 -080088 struct list_head interfaces;
Alex Elder2c43ce42014-11-17 08:08:44 -060089 struct list_head connections;
Alex Elder177404b2014-10-03 14:14:24 -050090 struct ida cport_id_map;
Matt Porter98f4ab22014-10-21 01:52:27 -040091 u8 device_id;
Alex Elder1bb3c722014-10-02 12:30:03 -050092
Fabien Parent144670c2015-09-02 15:50:35 +020093 /* Number of CPorts supported by the UniPro IP */
94 size_t num_cports;
95
Alex Elder8b337302014-11-20 16:09:13 -060096 /* Host device buffer constraints */
Alex Elder8b337302014-11-20 16:09:13 -060097 size_t buffer_size_max;
98
Greg Kroah-Hartmana4d91502015-04-07 20:27:15 +020099 struct gb_endo *endo;
Viresh Kumard3d44842015-07-21 17:44:18 +0530100 struct gb_connection *initial_svc_connection;
Perry Hung75a60ed2015-07-24 19:02:33 -0400101 struct gb_svc *svc;
Greg Kroah-Hartmana4d91502015-04-07 20:27:15 +0200102
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700103 /* Private data for the host driver */
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700104 unsigned long hd_priv[0] __aligned(sizeof(s64));
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700105};
106
Greg Kroah-Hartman8faa8da2014-10-06 20:34:48 -0700107struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *hd,
Johan Hovoldd9336672015-05-19 11:22:43 +0200108 struct device *parent,
Fabien Parent144670c2015-09-02 15:50:35 +0200109 size_t buffer_size_max,
110 size_t num_cports);
Alex Eldereb765e42015-05-22 12:56:49 -0500111int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id,
112 u8 ap_intf_id);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700113void greybus_remove_hd(struct greybus_host_device *hd);
Greg Kroah-Hartmand5d19032014-08-11 19:03:20 +0800114
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800115struct greybus_driver {
116 const char *name;
117
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530118 int (*probe)(struct gb_bundle *bundle,
119 const struct greybus_bundle_id *id);
120 void (*disconnect)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800121
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530122 int (*suspend)(struct gb_bundle *bundle, pm_message_t message);
123 int (*resume)(struct gb_bundle *bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800124
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530125 const struct greybus_bundle_id *id_table;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800126
127 struct device_driver driver;
128};
129#define to_greybus_driver(d) container_of(d, struct greybus_driver, driver)
130
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800131/* Don't call these directly, use the module_greybus_driver() macro instead */
132int greybus_register_driver(struct greybus_driver *driver,
133 struct module *module, const char *mod_name);
Alex Elderfd1c2e52015-06-08 12:05:13 -0500134void greybus_deregister_driver(struct greybus_driver *driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800135
136/* define to get proper THIS_MODULE and KBUILD_MODNAME values */
137#define greybus_register(driver) \
138 greybus_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Alex Elderfd1c2e52015-06-08 12:05:13 -0500139#define greybus_deregister(driver) \
140 greybus_deregister_driver(driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800141
142/**
143 * module_greybus_driver() - Helper macro for registering a Greybus driver
144 * @__greybus_driver: greybus_driver structure
145 *
146 * Helper macro for Greybus drivers to set up proper module init / exit
147 * functions. Replaces module_init() and module_exit() and keeps people from
148 * printing pointless things to the kernel log when their driver is loaded.
149 */
150#define module_greybus_driver(__greybus_driver) \
151 module_driver(__greybus_driver, greybus_register, greybus_deregister)
152
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800153int greybus_disabled(void);
154
Greg Kroah-Hartman3d0421e2015-06-11 09:22:51 -0700155void gb_debugfs_init(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700156void gb_debugfs_cleanup(void);
Alexandre Bailone8f824b2015-03-18 15:42:51 +0100157struct dentry *gb_debugfs_get(void);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700158
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800159extern struct bus_type greybus_bus_type;
Greg Kroah-Hartman06340ef2014-09-01 19:05:54 -0700160
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200161extern struct device_type greybus_endo_type;
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800162extern struct device_type greybus_module_type;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800163extern struct device_type greybus_interface_type;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500164extern struct device_type greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800165extern struct device_type greybus_connection_type;
166
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +0200167static inline int is_gb_endo(const struct device *dev)
168{
169 return dev->type == &greybus_endo_type;
170}
171
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -0800172static inline int is_gb_module(const struct device *dev)
173{
174 return dev->type == &greybus_module_type;
175}
176
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800177static inline int is_gb_interface(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800178{
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800179 return dev->type == &greybus_interface_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800180}
181
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500182static inline int is_gb_bundle(const struct device *dev)
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800183{
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500184 return dev->type == &greybus_bundle_type;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -0800185}
186
187static inline int is_gb_connection(const struct device *dev)
188{
189 return dev->type == &greybus_connection_type;
190}
191
Fabien Parent144670c2015-09-02 15:50:35 +0200192static inline bool cport_id_valid(struct greybus_host_device *hd, u16 cport_id)
Alex Elder821c6202015-06-13 11:02:07 -0500193{
Fabien Parent144670c2015-09-02 15:50:35 +0200194 return cport_id != CPORT_ID_BAD && cport_id < hd->num_cports;
Alex Elder821c6202015-06-13 11:02:07 -0500195}
196
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800197#endif /* __KERNEL__ */
198#endif /* __LINUX_GREYBUS_H */