blob: e96a9874b1a42ecd5ff836718f8bf4f184977065 [file] [log] [blame]
Carlos Chineaa056ab82010-04-16 19:01:02 +03001/*
2 * HSI core.
3 *
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 *
6 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 */
22#include <linux/hsi/hsi.h>
23#include <linux/compiler.h>
Carlos Chineaa056ab82010-04-16 19:01:02 +030024#include <linux/list.h>
Carlos Chineaa056ab82010-04-16 19:01:02 +030025#include <linux/kobject.h>
26#include <linux/slab.h>
27#include <linux/string.h>
Carlos Chineaec1c56f2012-04-11 10:55:53 +030028#include <linux/notifier.h>
Carlos Chineaa056ab82010-04-16 19:01:02 +030029#include "hsi_core.h"
30
Carlos Chineaa056ab82010-04-16 19:01:02 +030031static ssize_t modalias_show(struct device *dev,
32 struct device_attribute *a __maybe_unused, char *buf)
33{
34 return sprintf(buf, "hsi:%s\n", dev_name(dev));
35}
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070036static DEVICE_ATTR_RO(modalias);
Carlos Chineaa056ab82010-04-16 19:01:02 +030037
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070038static struct attribute *hsi_bus_dev_attrs[] = {
39 &dev_attr_modalias.attr,
40 NULL,
Carlos Chineaa056ab82010-04-16 19:01:02 +030041};
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070042ATTRIBUTE_GROUPS(hsi_bus_dev);
Carlos Chineaa056ab82010-04-16 19:01:02 +030043
44static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
45{
Carlos Chinea6f02b9e2012-04-10 15:11:24 +030046 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +030047
48 return 0;
49}
50
51static int hsi_bus_match(struct device *dev, struct device_driver *driver)
52{
53 return strcmp(dev_name(dev), driver->name) == 0;
54}
55
56static struct bus_type hsi_bus_type = {
57 .name = "hsi",
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070058 .dev_groups = hsi_bus_dev_groups,
Carlos Chineaa056ab82010-04-16 19:01:02 +030059 .match = hsi_bus_match,
60 .uevent = hsi_bus_uevent,
61};
62
63static void hsi_client_release(struct device *dev)
64{
65 kfree(to_hsi_client(dev));
66}
67
68static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
69{
70 struct hsi_client *cl;
Carlos Chineaa056ab82010-04-16 19:01:02 +030071
72 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
73 if (!cl)
74 return;
Carlos Chineaa056ab82010-04-16 19:01:02 +030075 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;
Kees Cook02aa2a32013-07-03 15:04:56 -070080 dev_set_name(&cl->device, "%s", info->name);
Carlos Chineaa056ab82010-04-16 19:01:02 +030081 cl->device.platform_data = info->platform_data;
Carlos Chineaa056ab82010-04-16 19:01:02 +030082 if (info->archdata)
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);
Carlos Chinea90e41f92012-04-11 11:01:11 +030086 put_device(&cl->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +030087 }
88}
89
90static void hsi_scan_board_info(struct hsi_controller *hsi)
91{
92 struct hsi_cl_info *cl_info;
93 struct hsi_port *p;
94
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);
98 if (!p)
99 continue;
100 hsi_new_client(p, &cl_info->info);
101 }
102}
103
104static int hsi_remove_client(struct device *dev, void *data __maybe_unused)
105{
Carlos Chineaa056ab82010-04-16 19:01:02 +0300106 device_unregister(dev);
107
108 return 0;
109}
110
111static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
112{
113 device_for_each_child(dev, NULL, hsi_remove_client);
114 device_unregister(dev);
115
116 return 0;
117}
118
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300119static void hsi_controller_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300120{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300121 struct hsi_controller *hsi = to_hsi_controller(dev);
122
123 kfree(hsi->port);
124 kfree(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300125}
126
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300127static void hsi_port_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300128{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300129 kfree(to_hsi_port(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +0300130}
131
132/**
Sebastian Reichela0bf37e2013-10-06 20:23:49 +0200133 * hsi_unregister_port - Unregister an HSI port
134 * @port: The HSI port to unregister
135 */
136void hsi_port_unregister_clients(struct hsi_port *port)
137{
138 device_for_each_child(&port->device, NULL, hsi_remove_client);
139}
140EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
141
142/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300143 * hsi_unregister_controller - Unregister an HSI controller
144 * @hsi: The HSI controller to register
145 */
146void hsi_unregister_controller(struct hsi_controller *hsi)
147{
148 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
149 device_unregister(&hsi->device);
150}
151EXPORT_SYMBOL_GPL(hsi_unregister_controller);
152
153/**
154 * hsi_register_controller - Register an HSI controller and its ports
155 * @hsi: The HSI controller to register
156 *
157 * Returns -errno on failure, 0 on success.
158 */
159int hsi_register_controller(struct hsi_controller *hsi)
160{
161 unsigned int i;
162 int err;
163
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300164 err = device_add(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300165 if (err < 0)
166 return err;
167 for (i = 0; i < hsi->num_ports; i++) {
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300168 hsi->port[i]->device.parent = &hsi->device;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300169 err = device_add(&hsi->port[i]->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300170 if (err < 0)
171 goto out;
172 }
173 /* Populate HSI bus with HSI clients */
174 hsi_scan_board_info(hsi);
175
176 return 0;
177out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300178 while (i-- > 0)
179 device_del(&hsi->port[i]->device);
180 device_del(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300181
182 return err;
183}
184EXPORT_SYMBOL_GPL(hsi_register_controller);
185
186/**
187 * hsi_register_client_driver - Register an HSI client to the HSI bus
188 * @drv: HSI client driver to register
189 *
190 * Returns -errno on failure, 0 on success.
191 */
192int hsi_register_client_driver(struct hsi_client_driver *drv)
193{
194 drv->driver.bus = &hsi_bus_type;
195
196 return driver_register(&drv->driver);
197}
198EXPORT_SYMBOL_GPL(hsi_register_client_driver);
199
200static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
201{
202 return 0;
203}
204
205static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
206{
207 return 0;
208}
209
210/**
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300211 * hsi_put_controller - Free an HSI controller
212 *
213 * @hsi: Pointer to the HSI controller to freed
214 *
215 * HSI controller drivers should only use this function if they need
216 * to free their allocated hsi_controller structures before a successful
217 * call to hsi_register_controller. Other use is not allowed.
218 */
219void hsi_put_controller(struct hsi_controller *hsi)
220{
221 unsigned int i;
222
223 if (!hsi)
224 return;
225
226 for (i = 0; i < hsi->num_ports; i++)
227 if (hsi->port && hsi->port[i])
228 put_device(&hsi->port[i]->device);
229 put_device(&hsi->device);
230}
231EXPORT_SYMBOL_GPL(hsi_put_controller);
232
233/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300234 * hsi_alloc_controller - Allocate an HSI controller and its ports
235 * @n_ports: Number of ports on the HSI controller
236 * @flags: Kernel allocation flags
237 *
238 * Return NULL on failure or a pointer to an hsi_controller on success.
239 */
240struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
241{
242 struct hsi_controller *hsi;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300243 struct hsi_port **port;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300244 unsigned int i;
245
246 if (!n_ports)
247 return NULL;
248
Carlos Chineaa056ab82010-04-16 19:01:02 +0300249 hsi = kzalloc(sizeof(*hsi), flags);
250 if (!hsi)
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300251 return NULL;
252 port = kzalloc(sizeof(*port)*n_ports, flags);
253 if (!port) {
254 kfree(hsi);
255 return NULL;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300256 }
257 hsi->num_ports = n_ports;
258 hsi->port = port;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300259 hsi->device.release = hsi_controller_release;
260 device_initialize(&hsi->device);
261
262 for (i = 0; i < n_ports; i++) {
263 port[i] = kzalloc(sizeof(**port), flags);
264 if (port[i] == NULL)
265 goto out;
266 port[i]->num = i;
267 port[i]->async = hsi_dummy_msg;
268 port[i]->setup = hsi_dummy_cl;
269 port[i]->flush = hsi_dummy_cl;
270 port[i]->start_tx = hsi_dummy_cl;
271 port[i]->stop_tx = hsi_dummy_cl;
272 port[i]->release = hsi_dummy_cl;
273 mutex_init(&port[i]->lock);
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300274 ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head);
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300275 dev_set_name(&port[i]->device, "port%d", i);
276 hsi->port[i]->device.release = hsi_port_release;
277 device_initialize(&hsi->port[i]->device);
278 }
Carlos Chineaa056ab82010-04-16 19:01:02 +0300279
280 return hsi;
281out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300282 hsi_put_controller(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300283
284 return NULL;
285}
286EXPORT_SYMBOL_GPL(hsi_alloc_controller);
287
288/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300289 * hsi_free_msg - Free an HSI message
290 * @msg: Pointer to the HSI message
291 *
292 * Client is responsible to free the buffers pointed by the scatterlists.
293 */
294void hsi_free_msg(struct hsi_msg *msg)
295{
296 if (!msg)
297 return;
298 sg_free_table(&msg->sgt);
299 kfree(msg);
300}
301EXPORT_SYMBOL_GPL(hsi_free_msg);
302
303/**
304 * hsi_alloc_msg - Allocate an HSI message
305 * @nents: Number of memory entries
306 * @flags: Kernel allocation flags
307 *
308 * nents can be 0. This mainly makes sense for read transfer.
309 * In that case, HSI drivers will call the complete callback when
310 * there is data to be read without consuming it.
311 *
312 * Return NULL on failure or a pointer to an hsi_msg on success.
313 */
314struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
315{
316 struct hsi_msg *msg;
317 int err;
318
319 msg = kzalloc(sizeof(*msg), flags);
320 if (!msg)
321 return NULL;
322
323 if (!nents)
324 return msg;
325
326 err = sg_alloc_table(&msg->sgt, nents, flags);
327 if (unlikely(err)) {
328 kfree(msg);
329 msg = NULL;
330 }
331
332 return msg;
333}
334EXPORT_SYMBOL_GPL(hsi_alloc_msg);
335
336/**
337 * hsi_async - Submit an HSI transfer to the controller
338 * @cl: HSI client sending the transfer
339 * @msg: The HSI transfer passed to controller
340 *
341 * The HSI message must have the channel, ttype, complete and destructor
342 * fields set beforehand. If nents > 0 then the client has to initialize
343 * also the scatterlists to point to the buffers to write to or read from.
344 *
345 * HSI controllers relay on pre-allocated buffers from their clients and they
346 * do not allocate buffers on their own.
347 *
348 * Once the HSI message transfer finishes, the HSI controller calls the
349 * complete callback with the status and actual_len fields of the HSI message
350 * updated. The complete callback can be called before returning from
351 * hsi_async.
352 *
353 * Returns -errno on failure or 0 on success
354 */
355int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
356{
357 struct hsi_port *port = hsi_get_port(cl);
358
359 if (!hsi_port_claimed(cl))
360 return -EACCES;
361
362 WARN_ON_ONCE(!msg->destructor || !msg->complete);
363 msg->cl = cl;
364
365 return port->async(msg);
366}
367EXPORT_SYMBOL_GPL(hsi_async);
368
369/**
370 * hsi_claim_port - Claim the HSI client's port
371 * @cl: HSI client that wants to claim its port
372 * @share: Flag to indicate if the client wants to share the port or not.
373 *
374 * Returns -errno on failure, 0 on success.
375 */
376int hsi_claim_port(struct hsi_client *cl, unsigned int share)
377{
378 struct hsi_port *port = hsi_get_port(cl);
379 int err = 0;
380
381 mutex_lock(&port->lock);
382 if ((port->claimed) && (!port->shared || !share)) {
383 err = -EBUSY;
384 goto out;
385 }
386 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
387 err = -ENODEV;
388 goto out;
389 }
390 port->claimed++;
391 port->shared = !!share;
392 cl->pclaimed = 1;
393out:
394 mutex_unlock(&port->lock);
395
396 return err;
397}
398EXPORT_SYMBOL_GPL(hsi_claim_port);
399
400/**
401 * hsi_release_port - Release the HSI client's port
402 * @cl: HSI client which previously claimed its port
403 */
404void hsi_release_port(struct hsi_client *cl)
405{
406 struct hsi_port *port = hsi_get_port(cl);
407
408 mutex_lock(&port->lock);
409 /* Allow HW driver to do some cleanup */
410 port->release(cl);
411 if (cl->pclaimed)
412 port->claimed--;
413 BUG_ON(port->claimed < 0);
414 cl->pclaimed = 0;
415 if (!port->claimed)
416 port->shared = 0;
417 module_put(to_hsi_controller(port->device.parent)->owner);
418 mutex_unlock(&port->lock);
419}
420EXPORT_SYMBOL_GPL(hsi_release_port);
421
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300422static int hsi_event_notifier_call(struct notifier_block *nb,
423 unsigned long event, void *data __maybe_unused)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300424{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300425 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
426
427 (*cl->ehandler)(cl, event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300428
429 return 0;
430}
431
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300432/**
433 * hsi_register_port_event - Register a client to receive port events
434 * @cl: HSI client that wants to receive port events
Randy Dunlap8eae5082013-03-01 11:11:47 -0800435 * @handler: Event handler callback
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300436 *
437 * Clients should register a callback to be able to receive
438 * events from the ports. Registration should happen after
439 * claiming the port.
440 * The handler can be called in interrupt context.
441 *
442 * Returns -errno on error, or 0 on success.
443 */
444int hsi_register_port_event(struct hsi_client *cl,
445 void (*handler)(struct hsi_client *, unsigned long))
Carlos Chineaa056ab82010-04-16 19:01:02 +0300446{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300447 struct hsi_port *port = hsi_get_port(cl);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300448
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300449 if (!handler || cl->ehandler)
450 return -EINVAL;
451 if (!hsi_port_claimed(cl))
452 return -EACCES;
453 cl->ehandler = handler;
454 cl->nb.notifier_call = hsi_event_notifier_call;
455
456 return atomic_notifier_chain_register(&port->n_head, &cl->nb);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300457}
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300458EXPORT_SYMBOL_GPL(hsi_register_port_event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300459
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300460/**
461 * hsi_unregister_port_event - Stop receiving port events for a client
462 * @cl: HSI client that wants to stop receiving port events
463 *
464 * Clients should call this function before releasing their associated
465 * port.
466 *
467 * Returns -errno on error, or 0 on success.
468 */
469int hsi_unregister_port_event(struct hsi_client *cl)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300470{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300471 struct hsi_port *port = hsi_get_port(cl);
472 int err;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300473
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300474 WARN_ON(!hsi_port_claimed(cl));
Carlos Chineaa056ab82010-04-16 19:01:02 +0300475
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300476 err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb);
477 if (!err)
478 cl->ehandler = NULL;
479
480 return err;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300481}
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300482EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300483
484/**
485 * hsi_event -Notifies clients about port events
486 * @port: Port where the event occurred
487 * @event: The event type
488 *
489 * Clients should not be concerned about wake line behavior. However, due
490 * to a race condition in HSI HW protocol, clients need to be notified
491 * about wake line changes, so they can implement a workaround for it.
492 *
493 * Events:
494 * HSI_EVENT_START_RX - Incoming wake line high
495 * HSI_EVENT_STOP_RX - Incoming wake line down
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300496 *
497 * Returns -errno on error, or 0 on success.
Carlos Chineaa056ab82010-04-16 19:01:02 +0300498 */
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300499int hsi_event(struct hsi_port *port, unsigned long event)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300500{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300501 return atomic_notifier_call_chain(&port->n_head, event, NULL);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300502}
503EXPORT_SYMBOL_GPL(hsi_event);
504
505static int __init hsi_init(void)
506{
507 return bus_register(&hsi_bus_type);
508}
509postcore_initcall(hsi_init);
510
511static void __exit hsi_exit(void)
512{
513 bus_unregister(&hsi_bus_type);
514}
515module_exit(hsi_exit);
516
517MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
518MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
519MODULE_LICENSE("GPL v2");