1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2011-2017, The Linux Foundation
4 */
5
6#ifndef _LINUX_SLIMBUS_H
7#define _LINUX_SLIMBUS_H
8#include <linux/device.h>
9#include <linux/module.h>
10#include <linux/completion.h>
11#include <linux/mod_devicetable.h>
12
13extern const struct bus_type slimbus_bus;
14
15/**
16 * struct slim_eaddr - Enumeration address for a SLIMbus device
17 * @instance: Instance value
18 * @dev_index: Device index
19 * @prod_code: Product code
20 * @manf_id: Manufacturer Id for the device
21 */
22struct slim_eaddr {
23	u8 instance;
24	u8 dev_index;
25	u16 prod_code;
26	u16 manf_id;
27} __packed;
28
29/**
30 * enum slim_device_status - slim device status
31 * @SLIM_DEVICE_STATUS_DOWN: Slim device is absent or not reported yet.
32 * @SLIM_DEVICE_STATUS_UP: Slim device is announced on the bus.
33 * @SLIM_DEVICE_STATUS_RESERVED: Reserved for future use.
34 */
35enum slim_device_status {
36	SLIM_DEVICE_STATUS_DOWN = 0,
37	SLIM_DEVICE_STATUS_UP,
38	SLIM_DEVICE_STATUS_RESERVED,
39};
40
41struct slim_controller;
42
43/**
44 * struct slim_device - Slim device handle.
45 * @dev: Driver model representation of the device.
46 * @e_addr: Enumeration address of this device.
47 * @status: slim device status
48 * @ctrl: slim controller instance.
49 * @laddr: 1-byte Logical address of this device.
50 * @is_laddr_valid: indicates if the laddr is valid or not
51 * @stream_list: List of streams on this device
52 * @stream_list_lock: lock to protect the stream list
53 *
54 * This is the client/device handle returned when a SLIMbus
55 * device is registered with a controller.
56 * Pointer to this structure is used by client-driver as a handle.
57 */
58struct slim_device {
59	struct device		dev;
60	struct slim_eaddr	e_addr;
61	struct slim_controller	*ctrl;
62	enum slim_device_status	status;
63	u8			laddr;
64	bool			is_laddr_valid;
65	struct list_head	stream_list;
66	spinlock_t stream_list_lock;
67};
68
69#define to_slim_device(d) container_of(d, struct slim_device, dev)
70
71/**
72 * struct slim_driver - SLIMbus 'generic device' (slave) device driver
73 *				(similar to 'spi_device' on SPI)
74 * @probe: Binds this driver to a SLIMbus device.
75 * @remove: Unbinds this driver from the SLIMbus device.
76 * @shutdown: Standard shutdown callback used during powerdown/halt.
77 * @device_status: This callback is called when
78 *	- The device reports present and gets a laddr assigned
79 *	- The device reports absent, or the bus goes down.
80 * @driver: SLIMbus device drivers should initialize name and owner field of
81 *	    this structure
82 * @id_table: List of SLIMbus devices supported by this driver
83 */
84
85struct slim_driver {
86	int	(*probe)(struct slim_device *sl);
87	void	(*remove)(struct slim_device *sl);
88	void	(*shutdown)(struct slim_device *sl);
89	int	(*device_status)(struct slim_device *sl,
90				 enum slim_device_status s);
91	struct device_driver		driver;
92	const struct slim_device_id	*id_table;
93};
94#define to_slim_driver(d) container_of(d, struct slim_driver, driver)
95
96/**
97 * struct slim_val_inf - Slimbus value or information element
98 * @start_offset: Specifies starting offset in information/value element map
99 * @rbuf: buffer to read the values
100 * @wbuf: buffer to write
101 * @num_bytes: upto 16. This ensures that the message will fit the slicesize
102 *		per SLIMbus spec
103 * @comp: completion for asynchronous operations, valid only if TID is
104 *	  required for transaction, like REQUEST operations.
105 *	  Rest of the transactions are synchronous anyway.
106 */
107struct slim_val_inf {
108	u16			start_offset;
109	u8			num_bytes;
110	u8			*rbuf;
111	const u8		*wbuf;
112	struct	completion	*comp;
113};
114
115#define SLIM_DEVICE_MAX_CHANNELS	256
116/* A SLIMBus Device may have frmo 0 to 31 Ports (inclusive) */
117#define SLIM_DEVICE_MAX_PORTS		32
118
119/**
120 * struct slim_stream_config - SLIMbus stream configuration
121 *	Configuring a stream is done at hw_params or prepare call
122 *	from audio drivers where they have all the required information
123 *	regarding rate, number of channels and so on.
124 *	There is a 1:1 mapping of channel and ports.
125 *
126 * @rate: data rate
127 * @bps: bits per data sample
128 * @ch_count: number of channels
129 * @chs: pointer to list of channel numbers
130 * @port_mask: port mask of ports to use for this stream
131 * @direction: direction of the stream, SNDRV_PCM_STREAM_PLAYBACK
132 *	or SNDRV_PCM_STREAM_CAPTURE.
133 */
134struct slim_stream_config {
135	unsigned int rate;
136	unsigned int bps;
137	/* MAX 256 channels */
138	unsigned int ch_count;
139	unsigned int *chs;
140	/* Max 32 ports per device */
141	unsigned long port_mask;
142	int direction;
143};
144
145/*
146 * use a macro to avoid include chaining to get THIS_MODULE
147 */
148#define slim_driver_register(drv) \
149	__slim_driver_register(drv, THIS_MODULE)
150int __slim_driver_register(struct slim_driver *drv, struct module *owner);
151void slim_driver_unregister(struct slim_driver *drv);
152
153/**
154 * module_slim_driver() - Helper macro for registering a SLIMbus driver
155 * @__slim_driver: slimbus_driver struct
156 *
157 * Helper macro for SLIMbus drivers which do not do anything special in module
158 * init/exit. This eliminates a lot of boilerplate. Each module may only
159 * use this macro once, and calling it replaces module_init() and module_exit()
160 */
161#define module_slim_driver(__slim_driver) \
162	module_driver(__slim_driver, slim_driver_register, \
163			slim_driver_unregister)
164
165static inline void *slim_get_devicedata(const struct slim_device *dev)
166{
167	return dev_get_drvdata(&dev->dev);
168}
169
170static inline void slim_set_devicedata(struct slim_device *dev, void *data)
171{
172	dev_set_drvdata(&dev->dev, data);
173}
174
175struct slim_device *of_slim_get_device(struct slim_controller *ctrl,
176				       struct device_node *np);
177struct slim_device *slim_get_device(struct slim_controller *ctrl,
178				    struct slim_eaddr *e_addr);
179int slim_get_logical_addr(struct slim_device *sbdev);
180
181/* Information Element management messages */
182#define SLIM_MSG_MC_REQUEST_INFORMATION          0x20
183#define SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION    0x21
184#define SLIM_MSG_MC_REPLY_INFORMATION            0x24
185#define SLIM_MSG_MC_CLEAR_INFORMATION            0x28
186#define SLIM_MSG_MC_REPORT_INFORMATION           0x29
187
188/* Value Element management messages */
189#define SLIM_MSG_MC_REQUEST_VALUE                0x60
190#define SLIM_MSG_MC_REQUEST_CHANGE_VALUE         0x61
191#define SLIM_MSG_MC_REPLY_VALUE                  0x64
192#define SLIM_MSG_MC_CHANGE_VALUE                 0x68
193
194int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
195		  u8 mc);
196int slim_readb(struct slim_device *sdev, u32 addr);
197int slim_writeb(struct slim_device *sdev, u32 addr, u8 value);
198int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val);
199int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val);
200
201/* SLIMbus Stream apis */
202struct slim_stream_runtime;
203struct slim_stream_runtime *slim_stream_allocate(struct slim_device *dev,
204						 const char *sname);
205int slim_stream_prepare(struct slim_stream_runtime *stream,
206			struct slim_stream_config *c);
207int slim_stream_enable(struct slim_stream_runtime *stream);
208int slim_stream_disable(struct slim_stream_runtime *stream);
209int slim_stream_unprepare(struct slim_stream_runtime *stream);
210int slim_stream_free(struct slim_stream_runtime *stream);
211
212#endif /* _LINUX_SLIMBUS_H */
213