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
22 #include <linux/hsi/hsi.h>
23 #include <linux/compiler.h>
24 #include <linux/list.h>
25 #include <linux/kobject.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/notifier.h>
31 static ssize_t
modalias_show(struct device
*dev
,
32 struct device_attribute
*a __maybe_unused
, char *buf
)
34 return sprintf(buf
, "hsi:%s\n", dev_name(dev
));
36 static DEVICE_ATTR_RO(modalias
);
38 static struct attribute
*hsi_bus_dev_attrs
[] = {
39 &dev_attr_modalias
.attr
,
42 ATTRIBUTE_GROUPS(hsi_bus_dev
);
44 static int hsi_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
46 add_uevent_var(env
, "MODALIAS=hsi:%s", dev_name(dev
));
51 static int hsi_bus_match(struct device
*dev
, struct device_driver
*driver
)
53 return strcmp(dev_name(dev
), driver
->name
) == 0;
56 static struct bus_type hsi_bus_type
= {
58 .dev_groups
= hsi_bus_dev_groups
,
59 .match
= hsi_bus_match
,
60 .uevent
= hsi_bus_uevent
,
63 static void hsi_client_release(struct device
*dev
)
65 kfree(to_hsi_client(dev
));
68 static void hsi_new_client(struct hsi_port
*port
, struct hsi_board_info
*info
)
70 struct hsi_client
*cl
;
72 cl
= kzalloc(sizeof(*cl
), GFP_KERNEL
);
75 cl
->tx_cfg
= info
->tx_cfg
;
76 cl
->rx_cfg
= info
->rx_cfg
;
77 cl
->device
.bus
= &hsi_bus_type
;
78 cl
->device
.parent
= &port
->device
;
79 cl
->device
.release
= hsi_client_release
;
80 dev_set_name(&cl
->device
, "%s", info
->name
);
81 cl
->device
.platform_data
= info
->platform_data
;
83 cl
->device
.archdata
= *info
->archdata
;
84 if (device_register(&cl
->device
) < 0) {
85 pr_err("hsi: failed to register client: %s\n", info
->name
);
86 put_device(&cl
->device
);
90 static void hsi_scan_board_info(struct hsi_controller
*hsi
)
92 struct hsi_cl_info
*cl_info
;
95 list_for_each_entry(cl_info
, &hsi_board_list
, list
)
96 if (cl_info
->info
.hsi_id
== hsi
->id
) {
97 p
= hsi_find_port_num(hsi
, cl_info
->info
.port
);
100 hsi_new_client(p
, &cl_info
->info
);
104 static int hsi_remove_client(struct device
*dev
, void *data __maybe_unused
)
106 device_unregister(dev
);
111 static int hsi_remove_port(struct device
*dev
, void *data __maybe_unused
)
113 device_for_each_child(dev
, NULL
, hsi_remove_client
);
114 device_unregister(dev
);
119 static void hsi_controller_release(struct device
*dev
)
121 struct hsi_controller
*hsi
= to_hsi_controller(dev
);
127 static void hsi_port_release(struct device
*dev
)
129 kfree(to_hsi_port(dev
));
133 * hsi_unregister_controller - Unregister an HSI controller
134 * @hsi: The HSI controller to register
136 void hsi_unregister_controller(struct hsi_controller
*hsi
)
138 device_for_each_child(&hsi
->device
, NULL
, hsi_remove_port
);
139 device_unregister(&hsi
->device
);
141 EXPORT_SYMBOL_GPL(hsi_unregister_controller
);
144 * hsi_register_controller - Register an HSI controller and its ports
145 * @hsi: The HSI controller to register
147 * Returns -errno on failure, 0 on success.
149 int hsi_register_controller(struct hsi_controller
*hsi
)
154 err
= device_add(&hsi
->device
);
157 for (i
= 0; i
< hsi
->num_ports
; i
++) {
158 hsi
->port
[i
]->device
.parent
= &hsi
->device
;
159 err
= device_add(&hsi
->port
[i
]->device
);
163 /* Populate HSI bus with HSI clients */
164 hsi_scan_board_info(hsi
);
169 device_del(&hsi
->port
[i
]->device
);
170 device_del(&hsi
->device
);
174 EXPORT_SYMBOL_GPL(hsi_register_controller
);
177 * hsi_register_client_driver - Register an HSI client to the HSI bus
178 * @drv: HSI client driver to register
180 * Returns -errno on failure, 0 on success.
182 int hsi_register_client_driver(struct hsi_client_driver
*drv
)
184 drv
->driver
.bus
= &hsi_bus_type
;
186 return driver_register(&drv
->driver
);
188 EXPORT_SYMBOL_GPL(hsi_register_client_driver
);
190 static inline int hsi_dummy_msg(struct hsi_msg
*msg __maybe_unused
)
195 static inline int hsi_dummy_cl(struct hsi_client
*cl __maybe_unused
)
201 * hsi_put_controller - Free an HSI controller
203 * @hsi: Pointer to the HSI controller to freed
205 * HSI controller drivers should only use this function if they need
206 * to free their allocated hsi_controller structures before a successful
207 * call to hsi_register_controller. Other use is not allowed.
209 void hsi_put_controller(struct hsi_controller
*hsi
)
216 for (i
= 0; i
< hsi
->num_ports
; i
++)
217 if (hsi
->port
&& hsi
->port
[i
])
218 put_device(&hsi
->port
[i
]->device
);
219 put_device(&hsi
->device
);
221 EXPORT_SYMBOL_GPL(hsi_put_controller
);
224 * hsi_alloc_controller - Allocate an HSI controller and its ports
225 * @n_ports: Number of ports on the HSI controller
226 * @flags: Kernel allocation flags
228 * Return NULL on failure or a pointer to an hsi_controller on success.
230 struct hsi_controller
*hsi_alloc_controller(unsigned int n_ports
, gfp_t flags
)
232 struct hsi_controller
*hsi
;
233 struct hsi_port
**port
;
239 hsi
= kzalloc(sizeof(*hsi
), flags
);
242 port
= kzalloc(sizeof(*port
)*n_ports
, flags
);
247 hsi
->num_ports
= n_ports
;
249 hsi
->device
.release
= hsi_controller_release
;
250 device_initialize(&hsi
->device
);
252 for (i
= 0; i
< n_ports
; i
++) {
253 port
[i
] = kzalloc(sizeof(**port
), flags
);
257 port
[i
]->async
= hsi_dummy_msg
;
258 port
[i
]->setup
= hsi_dummy_cl
;
259 port
[i
]->flush
= hsi_dummy_cl
;
260 port
[i
]->start_tx
= hsi_dummy_cl
;
261 port
[i
]->stop_tx
= hsi_dummy_cl
;
262 port
[i
]->release
= hsi_dummy_cl
;
263 mutex_init(&port
[i
]->lock
);
264 ATOMIC_INIT_NOTIFIER_HEAD(&port
[i
]->n_head
);
265 dev_set_name(&port
[i
]->device
, "port%d", i
);
266 hsi
->port
[i
]->device
.release
= hsi_port_release
;
267 device_initialize(&hsi
->port
[i
]->device
);
272 hsi_put_controller(hsi
);
276 EXPORT_SYMBOL_GPL(hsi_alloc_controller
);
279 * hsi_free_msg - Free an HSI message
280 * @msg: Pointer to the HSI message
282 * Client is responsible to free the buffers pointed by the scatterlists.
284 void hsi_free_msg(struct hsi_msg
*msg
)
288 sg_free_table(&msg
->sgt
);
291 EXPORT_SYMBOL_GPL(hsi_free_msg
);
294 * hsi_alloc_msg - Allocate an HSI message
295 * @nents: Number of memory entries
296 * @flags: Kernel allocation flags
298 * nents can be 0. This mainly makes sense for read transfer.
299 * In that case, HSI drivers will call the complete callback when
300 * there is data to be read without consuming it.
302 * Return NULL on failure or a pointer to an hsi_msg on success.
304 struct hsi_msg
*hsi_alloc_msg(unsigned int nents
, gfp_t flags
)
309 msg
= kzalloc(sizeof(*msg
), flags
);
316 err
= sg_alloc_table(&msg
->sgt
, nents
, flags
);
324 EXPORT_SYMBOL_GPL(hsi_alloc_msg
);
327 * hsi_async - Submit an HSI transfer to the controller
328 * @cl: HSI client sending the transfer
329 * @msg: The HSI transfer passed to controller
331 * The HSI message must have the channel, ttype, complete and destructor
332 * fields set beforehand. If nents > 0 then the client has to initialize
333 * also the scatterlists to point to the buffers to write to or read from.
335 * HSI controllers relay on pre-allocated buffers from their clients and they
336 * do not allocate buffers on their own.
338 * Once the HSI message transfer finishes, the HSI controller calls the
339 * complete callback with the status and actual_len fields of the HSI message
340 * updated. The complete callback can be called before returning from
343 * Returns -errno on failure or 0 on success
345 int hsi_async(struct hsi_client
*cl
, struct hsi_msg
*msg
)
347 struct hsi_port
*port
= hsi_get_port(cl
);
349 if (!hsi_port_claimed(cl
))
352 WARN_ON_ONCE(!msg
->destructor
|| !msg
->complete
);
355 return port
->async(msg
);
357 EXPORT_SYMBOL_GPL(hsi_async
);
360 * hsi_claim_port - Claim the HSI client's port
361 * @cl: HSI client that wants to claim its port
362 * @share: Flag to indicate if the client wants to share the port or not.
364 * Returns -errno on failure, 0 on success.
366 int hsi_claim_port(struct hsi_client
*cl
, unsigned int share
)
368 struct hsi_port
*port
= hsi_get_port(cl
);
371 mutex_lock(&port
->lock
);
372 if ((port
->claimed
) && (!port
->shared
|| !share
)) {
376 if (!try_module_get(to_hsi_controller(port
->device
.parent
)->owner
)) {
381 port
->shared
= !!share
;
384 mutex_unlock(&port
->lock
);
388 EXPORT_SYMBOL_GPL(hsi_claim_port
);
391 * hsi_release_port - Release the HSI client's port
392 * @cl: HSI client which previously claimed its port
394 void hsi_release_port(struct hsi_client
*cl
)
396 struct hsi_port
*port
= hsi_get_port(cl
);
398 mutex_lock(&port
->lock
);
399 /* Allow HW driver to do some cleanup */
403 BUG_ON(port
->claimed
< 0);
407 module_put(to_hsi_controller(port
->device
.parent
)->owner
);
408 mutex_unlock(&port
->lock
);
410 EXPORT_SYMBOL_GPL(hsi_release_port
);
412 static int hsi_event_notifier_call(struct notifier_block
*nb
,
413 unsigned long event
, void *data __maybe_unused
)
415 struct hsi_client
*cl
= container_of(nb
, struct hsi_client
, nb
);
417 (*cl
->ehandler
)(cl
, event
);
423 * hsi_register_port_event - Register a client to receive port events
424 * @cl: HSI client that wants to receive port events
425 * @handler: Event handler callback
427 * Clients should register a callback to be able to receive
428 * events from the ports. Registration should happen after
430 * The handler can be called in interrupt context.
432 * Returns -errno on error, or 0 on success.
434 int hsi_register_port_event(struct hsi_client
*cl
,
435 void (*handler
)(struct hsi_client
*, unsigned long))
437 struct hsi_port
*port
= hsi_get_port(cl
);
439 if (!handler
|| cl
->ehandler
)
441 if (!hsi_port_claimed(cl
))
443 cl
->ehandler
= handler
;
444 cl
->nb
.notifier_call
= hsi_event_notifier_call
;
446 return atomic_notifier_chain_register(&port
->n_head
, &cl
->nb
);
448 EXPORT_SYMBOL_GPL(hsi_register_port_event
);
451 * hsi_unregister_port_event - Stop receiving port events for a client
452 * @cl: HSI client that wants to stop receiving port events
454 * Clients should call this function before releasing their associated
457 * Returns -errno on error, or 0 on success.
459 int hsi_unregister_port_event(struct hsi_client
*cl
)
461 struct hsi_port
*port
= hsi_get_port(cl
);
464 WARN_ON(!hsi_port_claimed(cl
));
466 err
= atomic_notifier_chain_unregister(&port
->n_head
, &cl
->nb
);
472 EXPORT_SYMBOL_GPL(hsi_unregister_port_event
);
475 * hsi_event -Notifies clients about port events
476 * @port: Port where the event occurred
477 * @event: The event type
479 * Clients should not be concerned about wake line behavior. However, due
480 * to a race condition in HSI HW protocol, clients need to be notified
481 * about wake line changes, so they can implement a workaround for it.
484 * HSI_EVENT_START_RX - Incoming wake line high
485 * HSI_EVENT_STOP_RX - Incoming wake line down
487 * Returns -errno on error, or 0 on success.
489 int hsi_event(struct hsi_port
*port
, unsigned long event
)
491 return atomic_notifier_call_chain(&port
->n_head
, event
, NULL
);
493 EXPORT_SYMBOL_GPL(hsi_event
);
495 static int __init
hsi_init(void)
497 return bus_register(&hsi_bus_type
);
499 postcore_initcall(hsi_init
);
501 static void __exit
hsi_exit(void)
503 bus_unregister(&hsi_bus_type
);
505 module_exit(hsi_exit
);
507 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
508 MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
509 MODULE_LICENSE("GPL v2");