blob: fe9371271ce2420db8ca4d62ce2c6d183219ec2e [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>
Sebastian Reichela2aa2472014-03-28 22:59:43 +010029#include <linux/of.h>
30#include <linux/of_device.h>
Carlos Chineaa056ab82010-04-16 19:01:02 +030031#include "hsi_core.h"
32
Carlos Chineaa056ab82010-04-16 19:01:02 +030033static ssize_t modalias_show(struct device *dev,
34 struct device_attribute *a __maybe_unused, char *buf)
35{
36 return sprintf(buf, "hsi:%s\n", dev_name(dev));
37}
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070038static DEVICE_ATTR_RO(modalias);
Carlos Chineaa056ab82010-04-16 19:01:02 +030039
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070040static struct attribute *hsi_bus_dev_attrs[] = {
41 &dev_attr_modalias.attr,
42 NULL,
Carlos Chineaa056ab82010-04-16 19:01:02 +030043};
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070044ATTRIBUTE_GROUPS(hsi_bus_dev);
Carlos Chineaa056ab82010-04-16 19:01:02 +030045
46static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
47{
Carlos Chinea6f02b9e2012-04-10 15:11:24 +030048 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +030049
50 return 0;
51}
52
53static int hsi_bus_match(struct device *dev, struct device_driver *driver)
54{
Sebastian Reichela2aa2472014-03-28 22:59:43 +010055 if (of_driver_match_device(dev, driver))
56 return true;
57
58 if (strcmp(dev_name(dev), driver->name) == 0)
59 return true;
60
61 return false;
Carlos Chineaa056ab82010-04-16 19:01:02 +030062}
63
64static struct bus_type hsi_bus_type = {
65 .name = "hsi",
Greg Kroah-Hartman00173142013-10-06 23:55:50 -070066 .dev_groups = hsi_bus_dev_groups,
Carlos Chineaa056ab82010-04-16 19:01:02 +030067 .match = hsi_bus_match,
68 .uevent = hsi_bus_uevent,
69};
70
71static void hsi_client_release(struct device *dev)
72{
Sebastian Reichela088cf12014-03-28 22:48:23 +010073 struct hsi_client *cl = to_hsi_client(dev);
74
75 kfree(cl->tx_cfg.channels);
76 kfree(cl->rx_cfg.channels);
77 kfree(cl);
Carlos Chineaa056ab82010-04-16 19:01:02 +030078}
79
Sebastian Reichel84914512014-03-28 22:54:25 +010080struct hsi_client *hsi_new_client(struct hsi_port *port,
81 struct hsi_board_info *info)
Carlos Chineaa056ab82010-04-16 19:01:02 +030082{
83 struct hsi_client *cl;
Sebastian Reichela088cf12014-03-28 22:48:23 +010084 size_t size;
Carlos Chineaa056ab82010-04-16 19:01:02 +030085
86 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
87 if (!cl)
Sebastian Reichel84914512014-03-28 22:54:25 +010088 return NULL;
Sebastian Reichela088cf12014-03-28 22:48:23 +010089
Carlos Chineaa056ab82010-04-16 19:01:02 +030090 cl->tx_cfg = info->tx_cfg;
Sebastian Reichela088cf12014-03-28 22:48:23 +010091 if (cl->tx_cfg.channels) {
92 size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels);
93 cl->tx_cfg.channels = kzalloc(size , GFP_KERNEL);
94 memcpy(cl->tx_cfg.channels, info->tx_cfg.channels, size);
95 }
96
Carlos Chineaa056ab82010-04-16 19:01:02 +030097 cl->rx_cfg = info->rx_cfg;
Sebastian Reichela088cf12014-03-28 22:48:23 +010098 if (cl->rx_cfg.channels) {
99 size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels);
100 cl->rx_cfg.channels = kzalloc(size , GFP_KERNEL);
101 memcpy(cl->rx_cfg.channels, info->rx_cfg.channels, size);
102 }
103
Carlos Chineaa056ab82010-04-16 19:01:02 +0300104 cl->device.bus = &hsi_bus_type;
105 cl->device.parent = &port->device;
106 cl->device.release = hsi_client_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700107 dev_set_name(&cl->device, "%s", info->name);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300108 cl->device.platform_data = info->platform_data;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300109 if (info->archdata)
110 cl->device.archdata = *info->archdata;
111 if (device_register(&cl->device) < 0) {
112 pr_err("hsi: failed to register client: %s\n", info->name);
Carlos Chinea90e41f92012-04-11 11:01:11 +0300113 put_device(&cl->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300114 }
Sebastian Reichel84914512014-03-28 22:54:25 +0100115
116 return cl;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300117}
Sebastian Reichel84914512014-03-28 22:54:25 +0100118EXPORT_SYMBOL_GPL(hsi_new_client);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300119
120static void hsi_scan_board_info(struct hsi_controller *hsi)
121{
122 struct hsi_cl_info *cl_info;
123 struct hsi_port *p;
124
125 list_for_each_entry(cl_info, &hsi_board_list, list)
126 if (cl_info->info.hsi_id == hsi->id) {
127 p = hsi_find_port_num(hsi, cl_info->info.port);
128 if (!p)
129 continue;
130 hsi_new_client(p, &cl_info->info);
131 }
132}
133
Sebastian Reichela2aa2472014-03-28 22:59:43 +0100134#ifdef CONFIG_OF
135static struct hsi_board_info hsi_char_dev_info = {
136 .name = "hsi_char",
137};
138
139static int hsi_of_property_parse_mode(struct device_node *client, char *name,
140 unsigned int *result)
141{
142 const char *mode;
143 int err;
144
145 err = of_property_read_string(client, name, &mode);
146 if (err < 0)
147 return err;
148
149 if (strcmp(mode, "stream") == 0)
150 *result = HSI_MODE_STREAM;
151 else if (strcmp(mode, "frame") == 0)
152 *result = HSI_MODE_FRAME;
153 else
154 return -EINVAL;
155
156 return 0;
157}
158
159static int hsi_of_property_parse_flow(struct device_node *client, char *name,
160 unsigned int *result)
161{
162 const char *flow;
163 int err;
164
165 err = of_property_read_string(client, name, &flow);
166 if (err < 0)
167 return err;
168
169 if (strcmp(flow, "synchronized") == 0)
170 *result = HSI_FLOW_SYNC;
171 else if (strcmp(flow, "pipeline") == 0)
172 *result = HSI_FLOW_PIPE;
173 else
174 return -EINVAL;
175
176 return 0;
177}
178
179static int hsi_of_property_parse_arb_mode(struct device_node *client,
180 char *name, unsigned int *result)
181{
182 const char *arb_mode;
183 int err;
184
185 err = of_property_read_string(client, name, &arb_mode);
186 if (err < 0)
187 return err;
188
189 if (strcmp(arb_mode, "round-robin") == 0)
190 *result = HSI_ARB_RR;
191 else if (strcmp(arb_mode, "priority") == 0)
192 *result = HSI_ARB_PRIO;
193 else
194 return -EINVAL;
195
196 return 0;
197}
198
199static void hsi_add_client_from_dt(struct hsi_port *port,
200 struct device_node *client)
201{
202 struct hsi_client *cl;
203 struct hsi_channel channel;
204 struct property *prop;
205 char name[32];
206 int length, cells, err, i, max_chan, mode;
207
208 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
209 if (!cl)
210 return;
211
212 err = of_modalias_node(client, name, sizeof(name));
213 if (err)
214 goto err;
215
216 dev_set_name(&cl->device, "%s", name);
217
218 err = hsi_of_property_parse_mode(client, "hsi-mode", &mode);
219 if (err) {
220 err = hsi_of_property_parse_mode(client, "hsi-rx-mode",
221 &cl->rx_cfg.mode);
222 if (err)
223 goto err;
224
225 err = hsi_of_property_parse_mode(client, "hsi-tx-mode",
226 &cl->tx_cfg.mode);
227 if (err)
228 goto err;
229 } else {
230 cl->rx_cfg.mode = mode;
231 cl->tx_cfg.mode = mode;
232 }
233
234 err = of_property_read_u32(client, "hsi-speed-kbps",
235 &cl->tx_cfg.speed);
236 if (err)
237 goto err;
238 cl->rx_cfg.speed = cl->tx_cfg.speed;
239
240 err = hsi_of_property_parse_flow(client, "hsi-flow",
241 &cl->rx_cfg.flow);
242 if (err)
243 goto err;
244
245 err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode",
246 &cl->rx_cfg.arb_mode);
247 if (err)
248 goto err;
249
250 prop = of_find_property(client, "hsi-channel-ids", &length);
251 if (!prop) {
252 err = -EINVAL;
253 goto err;
254 }
255
256 cells = length / sizeof(u32);
257
258 cl->rx_cfg.num_channels = cells;
259 cl->tx_cfg.num_channels = cells;
260
261 cl->rx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
262 if (!cl->rx_cfg.channels) {
263 err = -ENOMEM;
264 goto err;
265 }
266
267 cl->tx_cfg.channels = kzalloc(cells * sizeof(channel), GFP_KERNEL);
268 if (!cl->tx_cfg.channels) {
269 err = -ENOMEM;
270 goto err2;
271 }
272
273 max_chan = 0;
274 for (i = 0; i < cells; i++) {
275 err = of_property_read_u32_index(client, "hsi-channel-ids", i,
276 &channel.id);
277 if (err)
278 goto err3;
279
280 err = of_property_read_string_index(client, "hsi-channel-names",
281 i, &channel.name);
282 if (err)
283 channel.name = NULL;
284
285 if (channel.id > max_chan)
286 max_chan = channel.id;
287
288 cl->rx_cfg.channels[i] = channel;
289 cl->tx_cfg.channels[i] = channel;
290 }
291
292 cl->rx_cfg.num_hw_channels = max_chan + 1;
293 cl->tx_cfg.num_hw_channels = max_chan + 1;
294
295 cl->device.bus = &hsi_bus_type;
296 cl->device.parent = &port->device;
297 cl->device.release = hsi_client_release;
298 cl->device.of_node = client;
299
300 if (device_register(&cl->device) < 0) {
301 pr_err("hsi: failed to register client: %s\n", name);
302 put_device(&cl->device);
303 goto err3;
304 }
305
306 return;
307
308err3:
309 kfree(cl->tx_cfg.channels);
310err2:
311 kfree(cl->rx_cfg.channels);
312err:
313 kfree(cl);
314 pr_err("hsi client: missing or incorrect of property: err=%d\n", err);
315}
316
317void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients)
318{
319 struct device_node *child;
320
321 /* register hsi-char device */
322 hsi_new_client(port, &hsi_char_dev_info);
323
324 for_each_available_child_of_node(clients, child)
325 hsi_add_client_from_dt(port, child);
326}
327EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt);
328#endif
329
Sebastian Reichel84914512014-03-28 22:54:25 +0100330int hsi_remove_client(struct device *dev, void *data __maybe_unused)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300331{
Carlos Chineaa056ab82010-04-16 19:01:02 +0300332 device_unregister(dev);
333
334 return 0;
335}
Sebastian Reichel84914512014-03-28 22:54:25 +0100336EXPORT_SYMBOL_GPL(hsi_remove_client);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300337
338static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
339{
340 device_for_each_child(dev, NULL, hsi_remove_client);
341 device_unregister(dev);
342
343 return 0;
344}
345
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300346static void hsi_controller_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300347{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300348 struct hsi_controller *hsi = to_hsi_controller(dev);
349
350 kfree(hsi->port);
351 kfree(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300352}
353
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300354static void hsi_port_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300355{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300356 kfree(to_hsi_port(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +0300357}
358
359/**
Sebastian Reichela0bf37e2013-10-06 20:23:49 +0200360 * hsi_unregister_port - Unregister an HSI port
361 * @port: The HSI port to unregister
362 */
363void hsi_port_unregister_clients(struct hsi_port *port)
364{
365 device_for_each_child(&port->device, NULL, hsi_remove_client);
366}
367EXPORT_SYMBOL_GPL(hsi_port_unregister_clients);
368
369/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300370 * hsi_unregister_controller - Unregister an HSI controller
371 * @hsi: The HSI controller to register
372 */
373void hsi_unregister_controller(struct hsi_controller *hsi)
374{
375 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
376 device_unregister(&hsi->device);
377}
378EXPORT_SYMBOL_GPL(hsi_unregister_controller);
379
380/**
381 * hsi_register_controller - Register an HSI controller and its ports
382 * @hsi: The HSI controller to register
383 *
384 * Returns -errno on failure, 0 on success.
385 */
386int hsi_register_controller(struct hsi_controller *hsi)
387{
388 unsigned int i;
389 int err;
390
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300391 err = device_add(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300392 if (err < 0)
393 return err;
394 for (i = 0; i < hsi->num_ports; i++) {
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300395 hsi->port[i]->device.parent = &hsi->device;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300396 err = device_add(&hsi->port[i]->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300397 if (err < 0)
398 goto out;
399 }
400 /* Populate HSI bus with HSI clients */
401 hsi_scan_board_info(hsi);
402
403 return 0;
404out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300405 while (i-- > 0)
406 device_del(&hsi->port[i]->device);
407 device_del(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300408
409 return err;
410}
411EXPORT_SYMBOL_GPL(hsi_register_controller);
412
413/**
414 * hsi_register_client_driver - Register an HSI client to the HSI bus
415 * @drv: HSI client driver to register
416 *
417 * Returns -errno on failure, 0 on success.
418 */
419int hsi_register_client_driver(struct hsi_client_driver *drv)
420{
421 drv->driver.bus = &hsi_bus_type;
422
423 return driver_register(&drv->driver);
424}
425EXPORT_SYMBOL_GPL(hsi_register_client_driver);
426
427static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
428{
429 return 0;
430}
431
432static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
433{
434 return 0;
435}
436
437/**
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300438 * hsi_put_controller - Free an HSI controller
439 *
440 * @hsi: Pointer to the HSI controller to freed
441 *
442 * HSI controller drivers should only use this function if they need
443 * to free their allocated hsi_controller structures before a successful
444 * call to hsi_register_controller. Other use is not allowed.
445 */
446void hsi_put_controller(struct hsi_controller *hsi)
447{
448 unsigned int i;
449
450 if (!hsi)
451 return;
452
453 for (i = 0; i < hsi->num_ports; i++)
454 if (hsi->port && hsi->port[i])
455 put_device(&hsi->port[i]->device);
456 put_device(&hsi->device);
457}
458EXPORT_SYMBOL_GPL(hsi_put_controller);
459
460/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300461 * hsi_alloc_controller - Allocate an HSI controller and its ports
462 * @n_ports: Number of ports on the HSI controller
463 * @flags: Kernel allocation flags
464 *
465 * Return NULL on failure or a pointer to an hsi_controller on success.
466 */
467struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
468{
469 struct hsi_controller *hsi;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300470 struct hsi_port **port;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300471 unsigned int i;
472
473 if (!n_ports)
474 return NULL;
475
Carlos Chineaa056ab82010-04-16 19:01:02 +0300476 hsi = kzalloc(sizeof(*hsi), flags);
477 if (!hsi)
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300478 return NULL;
479 port = kzalloc(sizeof(*port)*n_ports, flags);
480 if (!port) {
481 kfree(hsi);
482 return NULL;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300483 }
484 hsi->num_ports = n_ports;
485 hsi->port = port;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300486 hsi->device.release = hsi_controller_release;
487 device_initialize(&hsi->device);
488
489 for (i = 0; i < n_ports; i++) {
490 port[i] = kzalloc(sizeof(**port), flags);
491 if (port[i] == NULL)
492 goto out;
493 port[i]->num = i;
494 port[i]->async = hsi_dummy_msg;
495 port[i]->setup = hsi_dummy_cl;
496 port[i]->flush = hsi_dummy_cl;
497 port[i]->start_tx = hsi_dummy_cl;
498 port[i]->stop_tx = hsi_dummy_cl;
499 port[i]->release = hsi_dummy_cl;
500 mutex_init(&port[i]->lock);
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300501 ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head);
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300502 dev_set_name(&port[i]->device, "port%d", i);
503 hsi->port[i]->device.release = hsi_port_release;
504 device_initialize(&hsi->port[i]->device);
505 }
Carlos Chineaa056ab82010-04-16 19:01:02 +0300506
507 return hsi;
508out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300509 hsi_put_controller(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300510
511 return NULL;
512}
513EXPORT_SYMBOL_GPL(hsi_alloc_controller);
514
515/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300516 * hsi_free_msg - Free an HSI message
517 * @msg: Pointer to the HSI message
518 *
519 * Client is responsible to free the buffers pointed by the scatterlists.
520 */
521void hsi_free_msg(struct hsi_msg *msg)
522{
523 if (!msg)
524 return;
525 sg_free_table(&msg->sgt);
526 kfree(msg);
527}
528EXPORT_SYMBOL_GPL(hsi_free_msg);
529
530/**
531 * hsi_alloc_msg - Allocate an HSI message
532 * @nents: Number of memory entries
533 * @flags: Kernel allocation flags
534 *
535 * nents can be 0. This mainly makes sense for read transfer.
536 * In that case, HSI drivers will call the complete callback when
537 * there is data to be read without consuming it.
538 *
539 * Return NULL on failure or a pointer to an hsi_msg on success.
540 */
541struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
542{
543 struct hsi_msg *msg;
544 int err;
545
546 msg = kzalloc(sizeof(*msg), flags);
547 if (!msg)
548 return NULL;
549
550 if (!nents)
551 return msg;
552
553 err = sg_alloc_table(&msg->sgt, nents, flags);
554 if (unlikely(err)) {
555 kfree(msg);
556 msg = NULL;
557 }
558
559 return msg;
560}
561EXPORT_SYMBOL_GPL(hsi_alloc_msg);
562
563/**
564 * hsi_async - Submit an HSI transfer to the controller
565 * @cl: HSI client sending the transfer
566 * @msg: The HSI transfer passed to controller
567 *
568 * The HSI message must have the channel, ttype, complete and destructor
569 * fields set beforehand. If nents > 0 then the client has to initialize
570 * also the scatterlists to point to the buffers to write to or read from.
571 *
572 * HSI controllers relay on pre-allocated buffers from their clients and they
573 * do not allocate buffers on their own.
574 *
575 * Once the HSI message transfer finishes, the HSI controller calls the
576 * complete callback with the status and actual_len fields of the HSI message
577 * updated. The complete callback can be called before returning from
578 * hsi_async.
579 *
580 * Returns -errno on failure or 0 on success
581 */
582int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
583{
584 struct hsi_port *port = hsi_get_port(cl);
585
586 if (!hsi_port_claimed(cl))
587 return -EACCES;
588
589 WARN_ON_ONCE(!msg->destructor || !msg->complete);
590 msg->cl = cl;
591
592 return port->async(msg);
593}
594EXPORT_SYMBOL_GPL(hsi_async);
595
596/**
597 * hsi_claim_port - Claim the HSI client's port
598 * @cl: HSI client that wants to claim its port
599 * @share: Flag to indicate if the client wants to share the port or not.
600 *
601 * Returns -errno on failure, 0 on success.
602 */
603int hsi_claim_port(struct hsi_client *cl, unsigned int share)
604{
605 struct hsi_port *port = hsi_get_port(cl);
606 int err = 0;
607
608 mutex_lock(&port->lock);
609 if ((port->claimed) && (!port->shared || !share)) {
610 err = -EBUSY;
611 goto out;
612 }
613 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
614 err = -ENODEV;
615 goto out;
616 }
617 port->claimed++;
618 port->shared = !!share;
619 cl->pclaimed = 1;
620out:
621 mutex_unlock(&port->lock);
622
623 return err;
624}
625EXPORT_SYMBOL_GPL(hsi_claim_port);
626
627/**
628 * hsi_release_port - Release the HSI client's port
629 * @cl: HSI client which previously claimed its port
630 */
631void hsi_release_port(struct hsi_client *cl)
632{
633 struct hsi_port *port = hsi_get_port(cl);
634
635 mutex_lock(&port->lock);
636 /* Allow HW driver to do some cleanup */
637 port->release(cl);
638 if (cl->pclaimed)
639 port->claimed--;
640 BUG_ON(port->claimed < 0);
641 cl->pclaimed = 0;
642 if (!port->claimed)
643 port->shared = 0;
644 module_put(to_hsi_controller(port->device.parent)->owner);
645 mutex_unlock(&port->lock);
646}
647EXPORT_SYMBOL_GPL(hsi_release_port);
648
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300649static int hsi_event_notifier_call(struct notifier_block *nb,
650 unsigned long event, void *data __maybe_unused)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300651{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300652 struct hsi_client *cl = container_of(nb, struct hsi_client, nb);
653
654 (*cl->ehandler)(cl, event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300655
656 return 0;
657}
658
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300659/**
660 * hsi_register_port_event - Register a client to receive port events
661 * @cl: HSI client that wants to receive port events
Randy Dunlap8eae5082013-03-01 11:11:47 -0800662 * @handler: Event handler callback
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300663 *
664 * Clients should register a callback to be able to receive
665 * events from the ports. Registration should happen after
666 * claiming the port.
667 * The handler can be called in interrupt context.
668 *
669 * Returns -errno on error, or 0 on success.
670 */
671int hsi_register_port_event(struct hsi_client *cl,
672 void (*handler)(struct hsi_client *, unsigned long))
Carlos Chineaa056ab82010-04-16 19:01:02 +0300673{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300674 struct hsi_port *port = hsi_get_port(cl);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300675
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300676 if (!handler || cl->ehandler)
677 return -EINVAL;
678 if (!hsi_port_claimed(cl))
679 return -EACCES;
680 cl->ehandler = handler;
681 cl->nb.notifier_call = hsi_event_notifier_call;
682
683 return atomic_notifier_chain_register(&port->n_head, &cl->nb);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300684}
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300685EXPORT_SYMBOL_GPL(hsi_register_port_event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300686
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300687/**
688 * hsi_unregister_port_event - Stop receiving port events for a client
689 * @cl: HSI client that wants to stop receiving port events
690 *
691 * Clients should call this function before releasing their associated
692 * port.
693 *
694 * Returns -errno on error, or 0 on success.
695 */
696int hsi_unregister_port_event(struct hsi_client *cl)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300697{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300698 struct hsi_port *port = hsi_get_port(cl);
699 int err;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300700
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300701 WARN_ON(!hsi_port_claimed(cl));
Carlos Chineaa056ab82010-04-16 19:01:02 +0300702
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300703 err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb);
704 if (!err)
705 cl->ehandler = NULL;
706
707 return err;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300708}
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300709EXPORT_SYMBOL_GPL(hsi_unregister_port_event);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300710
711/**
Sebastian Reichela2aa2472014-03-28 22:59:43 +0100712 * hsi_event - Notifies clients about port events
Carlos Chineaa056ab82010-04-16 19:01:02 +0300713 * @port: Port where the event occurred
714 * @event: The event type
715 *
716 * Clients should not be concerned about wake line behavior. However, due
717 * to a race condition in HSI HW protocol, clients need to be notified
718 * about wake line changes, so they can implement a workaround for it.
719 *
720 * Events:
721 * HSI_EVENT_START_RX - Incoming wake line high
722 * HSI_EVENT_STOP_RX - Incoming wake line down
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300723 *
724 * Returns -errno on error, or 0 on success.
Carlos Chineaa056ab82010-04-16 19:01:02 +0300725 */
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300726int hsi_event(struct hsi_port *port, unsigned long event)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300727{
Carlos Chineaec1c56f2012-04-11 10:55:53 +0300728 return atomic_notifier_call_chain(&port->n_head, event, NULL);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300729}
730EXPORT_SYMBOL_GPL(hsi_event);
731
Sebastian Reichela088cf12014-03-28 22:48:23 +0100732/**
733 * hsi_get_channel_id_by_name - acquire channel id by channel name
734 * @cl: HSI client, which uses the channel
735 * @name: name the channel is known under
736 *
737 * Clients can call this function to get the hsi channel ids similar to
738 * requesting IRQs or GPIOs by name. This function assumes the same
739 * channel configuration is used for RX and TX.
740 *
741 * Returns -errno on error or channel id on success.
742 */
743int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name)
744{
745 int i;
746
747 if (!cl->rx_cfg.channels)
748 return -ENOENT;
749
750 for (i = 0; i < cl->rx_cfg.num_channels; i++)
751 if (!strcmp(cl->rx_cfg.channels[i].name, name))
752 return cl->rx_cfg.channels[i].id;
753
754 return -ENXIO;
755}
756EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name);
757
Carlos Chineaa056ab82010-04-16 19:01:02 +0300758static int __init hsi_init(void)
759{
760 return bus_register(&hsi_bus_type);
761}
762postcore_initcall(hsi_init);
763
764static void __exit hsi_exit(void)
765{
766 bus_unregister(&hsi_bus_type);
767}
768module_exit(hsi_exit);
769
770MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
771MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
772MODULE_LICENSE("GPL v2");