2 * HSI core header file.
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
6 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #ifndef __LINUX_HSI_H__
24 #define __LINUX_HSI_H__
26 #include <linux/device.h>
27 #include <linux/mutex.h>
28 #include <linux/scatterlist.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/notifier.h>
33 /* HSI message ttype */
34 #define HSI_MSG_READ 0
35 #define HSI_MSG_WRITE 1
37 /* HSI configuration values */
44 HSI_FLOW_SYNC
, /* Synchronized flow */
45 HSI_FLOW_PIPE
, /* Pipelined flow */
49 HSI_ARB_RR
, /* Round-robin arbitration */
50 HSI_ARB_PRIO
, /* Channel priority arbitration */
53 #define HSI_MAX_CHANNELS 16
55 /* HSI message status codes */
57 HSI_STATUS_COMPLETED
, /* Message transfer is completed */
58 HSI_STATUS_PENDING
, /* Message pending to be read/write (POLL) */
59 HSI_STATUS_PROCEEDING
, /* Message transfer is ongoing */
60 HSI_STATUS_QUEUED
, /* Message waiting to be served */
61 HSI_STATUS_ERROR
, /* Error when message transfer was ongoing */
64 /* HSI port event codes */
71 * struct hsi_channel - channel resource used by the hsi clients
81 * struct hsi_config - Configuration for RX/TX HSI modules
82 * @mode: Bit transmission mode (STREAM or FRAME)
83 * @channels: Channel resources used by the client
84 * @num_channels: Number of channel resources
85 * @num_hw_channels: Number of channels the transceiver is configured for [1..16]
86 * @speed: Max bit transmission speed (Kbit/s)
87 * @flow: RX flow type (SYNCHRONIZED or PIPELINE)
88 * @arb_mode: Arbitration mode for TX frame (Round robin, priority)
92 struct hsi_channel
*channels
;
93 unsigned int num_channels
;
94 unsigned int num_hw_channels
;
97 unsigned int flow
; /* RX only */
98 unsigned int arb_mode
; /* TX only */
103 * struct hsi_board_info - HSI client board info
104 * @name: Name for the HSI device
105 * @hsi_id: HSI controller id where the client sits
106 * @port: Port number in the controller where the client sits
107 * @tx_cfg: HSI TX configuration
108 * @rx_cfg: HSI RX configuration
109 * @platform_data: Platform related data
110 * @archdata: Architecture-dependent device data
112 struct hsi_board_info
{
116 struct hsi_config tx_cfg
;
117 struct hsi_config rx_cfg
;
119 struct dev_archdata
*archdata
;
122 #ifdef CONFIG_HSI_BOARDINFO
123 extern int hsi_register_board_info(struct hsi_board_info
const *info
,
126 static inline int hsi_register_board_info(struct hsi_board_info
const *info
,
131 #endif /* CONFIG_HSI_BOARDINFO */
134 * struct hsi_client - HSI client attached to an HSI port
135 * @device: Driver model representation of the device
136 * @tx_cfg: HSI TX configuration
137 * @rx_cfg: HSI RX configuration
140 struct device device
;
141 struct hsi_config tx_cfg
;
142 struct hsi_config rx_cfg
;
144 void (*ehandler
)(struct hsi_client
*, unsigned long);
145 unsigned int pclaimed
:1;
146 struct notifier_block nb
;
149 #define to_hsi_client(dev) container_of(dev, struct hsi_client, device)
151 static inline void hsi_client_set_drvdata(struct hsi_client
*cl
, void *data
)
153 dev_set_drvdata(&cl
->device
, data
);
156 static inline void *hsi_client_drvdata(struct hsi_client
*cl
)
158 return dev_get_drvdata(&cl
->device
);
161 int hsi_register_port_event(struct hsi_client
*cl
,
162 void (*handler
)(struct hsi_client
*, unsigned long));
163 int hsi_unregister_port_event(struct hsi_client
*cl
);
166 * struct hsi_client_driver - Driver associated to an HSI client
167 * @driver: Driver model representation of the driver
169 struct hsi_client_driver
{
170 struct device_driver driver
;
173 #define to_hsi_client_driver(drv) container_of(drv, struct hsi_client_driver,\
176 int hsi_register_client_driver(struct hsi_client_driver
*drv
);
178 static inline void hsi_unregister_client_driver(struct hsi_client_driver
*drv
)
180 driver_unregister(&drv
->driver
);
184 * struct hsi_msg - HSI message descriptor
185 * @link: Free to use by the current descriptor owner
186 * @cl: HSI device client that issues the transfer
187 * @sgt: Head of the scatterlist array
188 * @context: Client context data associated to the transfer
189 * @complete: Transfer completion callback
190 * @destructor: Destructor to free resources when flushing
191 * @status: Status of the transfer when completed
192 * @actual_len: Actual length of data transferred on completion
193 * @channel: Channel were to TX/RX the message
194 * @ttype: Transfer type (TX if set, RX otherwise)
195 * @break_frame: if true HSI will send/receive a break frame. Data buffers are
196 * ignored in the request.
199 struct list_head link
;
200 struct hsi_client
*cl
;
204 void (*complete
)(struct hsi_msg
*msg
);
205 void (*destructor
)(struct hsi_msg
*msg
);
208 unsigned int actual_len
;
209 unsigned int channel
;
210 unsigned int ttype
:1;
211 unsigned int break_frame
:1;
214 struct hsi_msg
*hsi_alloc_msg(unsigned int n_frag
, gfp_t flags
);
215 void hsi_free_msg(struct hsi_msg
*msg
);
218 * struct hsi_port - HSI port device
219 * @device: Driver model representation of the device
220 * @tx_cfg: Current TX path configuration
221 * @rx_cfg: Current RX path configuration
223 * @shared: Set when port can be shared by different clients
224 * @claimed: Reference count of clients which claimed the port
225 * @lock: Serialize port claim
226 * @async: Asynchronous transfer callback
227 * @setup: Callback to set the HSI client configuration
228 * @flush: Callback to clean the HW state and destroy all pending transfers
229 * @start_tx: Callback to inform that a client wants to TX data
230 * @stop_tx: Callback to inform that a client no longer wishes to TX data
231 * @release: Callback to inform that a client no longer uses the port
232 * @n_head: Notifier chain for signaling port events to the clients.
235 struct device device
;
236 struct hsi_config tx_cfg
;
237 struct hsi_config rx_cfg
;
239 unsigned int shared
:1;
242 int (*async
)(struct hsi_msg
*msg
);
243 int (*setup
)(struct hsi_client
*cl
);
244 int (*flush
)(struct hsi_client
*cl
);
245 int (*start_tx
)(struct hsi_client
*cl
);
246 int (*stop_tx
)(struct hsi_client
*cl
);
247 int (*release
)(struct hsi_client
*cl
);
249 struct blocking_notifier_head n_head
;
252 #define to_hsi_port(dev) container_of(dev, struct hsi_port, device)
253 #define hsi_get_port(cl) to_hsi_port((cl)->device.parent)
255 int hsi_event(struct hsi_port
*port
, unsigned long event
);
256 int hsi_claim_port(struct hsi_client
*cl
, unsigned int share
);
257 void hsi_release_port(struct hsi_client
*cl
);
259 static inline int hsi_port_claimed(struct hsi_client
*cl
)
264 static inline void hsi_port_set_drvdata(struct hsi_port
*port
, void *data
)
266 dev_set_drvdata(&port
->device
, data
);
269 static inline void *hsi_port_drvdata(struct hsi_port
*port
)
271 return dev_get_drvdata(&port
->device
);
275 * struct hsi_controller - HSI controller device
276 * @device: Driver model representation of the device
277 * @owner: Pointer to the module owning the controller
278 * @id: HSI controller ID
279 * @num_ports: Number of ports in the HSI controller
280 * @port: Array of HSI ports
282 struct hsi_controller
{
283 struct device device
;
284 struct module
*owner
;
286 unsigned int num_ports
;
287 struct hsi_port
**port
;
290 #define to_hsi_controller(dev) container_of(dev, struct hsi_controller, device)
292 struct hsi_controller
*hsi_alloc_controller(unsigned int n_ports
, gfp_t flags
);
293 void hsi_put_controller(struct hsi_controller
*hsi
);
294 int hsi_register_controller(struct hsi_controller
*hsi
);
295 void hsi_unregister_controller(struct hsi_controller
*hsi
);
296 struct hsi_client
*hsi_new_client(struct hsi_port
*port
,
297 struct hsi_board_info
*info
);
298 int hsi_remove_client(struct device
*dev
, void *data
);
299 void hsi_port_unregister_clients(struct hsi_port
*port
);
302 void hsi_add_clients_from_dt(struct hsi_port
*port
,
303 struct device_node
*clients
);
305 static inline void hsi_add_clients_from_dt(struct hsi_port
*port
,
306 struct device_node
*clients
)
312 static inline void hsi_controller_set_drvdata(struct hsi_controller
*hsi
,
315 dev_set_drvdata(&hsi
->device
, data
);
318 static inline void *hsi_controller_drvdata(struct hsi_controller
*hsi
)
320 return dev_get_drvdata(&hsi
->device
);
323 static inline struct hsi_port
*hsi_find_port_num(struct hsi_controller
*hsi
,
326 return (num
< hsi
->num_ports
) ? hsi
->port
[num
] : NULL
;
330 * API for HSI clients
332 int hsi_async(struct hsi_client
*cl
, struct hsi_msg
*msg
);
334 int hsi_get_channel_id_by_name(struct hsi_client
*cl
, char *name
);
337 * hsi_id - Get HSI controller ID associated to a client
338 * @cl: Pointer to a HSI client
340 * Return the controller id where the client is attached to
342 static inline unsigned int hsi_id(struct hsi_client
*cl
)
344 return to_hsi_controller(cl
->device
.parent
->parent
)->id
;
348 * hsi_port_id - Gets the port number a client is attached to
349 * @cl: Pointer to HSI client
351 * Return the port number associated to the client
353 static inline unsigned int hsi_port_id(struct hsi_client
*cl
)
355 return to_hsi_port(cl
->device
.parent
)->num
;
359 * hsi_setup - Configure the client's port
360 * @cl: Pointer to the HSI client
362 * When sharing ports, clients should either relay on a single
363 * client setup or have the same setup for all of them.
365 * Return -errno on failure, 0 on success
367 static inline int hsi_setup(struct hsi_client
*cl
)
369 if (!hsi_port_claimed(cl
))
371 return hsi_get_port(cl
)->setup(cl
);
375 * hsi_flush - Flush all pending transactions on the client's port
376 * @cl: Pointer to the HSI client
378 * This function will destroy all pending hsi_msg in the port and reset
379 * the HW port so it is ready to receive and transmit from a clean state.
381 * Return -errno on failure, 0 on success
383 static inline int hsi_flush(struct hsi_client
*cl
)
385 if (!hsi_port_claimed(cl
))
387 return hsi_get_port(cl
)->flush(cl
);
391 * hsi_async_read - Submit a read transfer
392 * @cl: Pointer to the HSI client
393 * @msg: HSI message descriptor of the transfer
395 * Return -errno on failure, 0 on success
397 static inline int hsi_async_read(struct hsi_client
*cl
, struct hsi_msg
*msg
)
399 msg
->ttype
= HSI_MSG_READ
;
400 return hsi_async(cl
, msg
);
404 * hsi_async_write - Submit a write transfer
405 * @cl: Pointer to the HSI client
406 * @msg: HSI message descriptor of the transfer
408 * Return -errno on failure, 0 on success
410 static inline int hsi_async_write(struct hsi_client
*cl
, struct hsi_msg
*msg
)
412 msg
->ttype
= HSI_MSG_WRITE
;
413 return hsi_async(cl
, msg
);
417 * hsi_start_tx - Signal the port that the client wants to start a TX
418 * @cl: Pointer to the HSI client
420 * Return -errno on failure, 0 on success
422 static inline int hsi_start_tx(struct hsi_client
*cl
)
424 if (!hsi_port_claimed(cl
))
426 return hsi_get_port(cl
)->start_tx(cl
);
430 * hsi_stop_tx - Signal the port that the client no longer wants to transmit
431 * @cl: Pointer to the HSI client
433 * Return -errno on failure, 0 on success
435 static inline int hsi_stop_tx(struct hsi_client
*cl
)
437 if (!hsi_port_claimed(cl
))
439 return hsi_get_port(cl
)->stop_tx(cl
);
441 #endif /* __LINUX_HSI_H__ */