Commit | Line | Data |
---|---|---|
2b27bdcc | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a056ab8c CC |
2 | /* |
3 | * HSI core. | |
4 | * | |
5 | * Copyright (C) 2010 Nokia Corporation. All rights reserved. | |
6 | * | |
7 | * Contact: Carlos Chinea <carlos.chinea@nokia.com> | |
a056ab8c CC |
8 | */ |
9 | #include <linux/hsi/hsi.h> | |
10 | #include <linux/compiler.h> | |
a056ab8c | 11 | #include <linux/list.h> |
a056ab8c CC |
12 | #include <linux/kobject.h> |
13 | #include <linux/slab.h> | |
14 | #include <linux/string.h> | |
ec1c56ff | 15 | #include <linux/notifier.h> |
a2aa2473 SR |
16 | #include <linux/of.h> |
17 | #include <linux/of_device.h> | |
a056ab8c CC |
18 | #include "hsi_core.h" |
19 | ||
a056ab8c CC |
20 | static ssize_t modalias_show(struct device *dev, |
21 | struct device_attribute *a __maybe_unused, char *buf) | |
22 | { | |
23 | return sprintf(buf, "hsi:%s\n", dev_name(dev)); | |
24 | } | |
00173146 | 25 | static DEVICE_ATTR_RO(modalias); |
a056ab8c | 26 | |
00173146 GKH |
27 | static struct attribute *hsi_bus_dev_attrs[] = { |
28 | &dev_attr_modalias.attr, | |
29 | NULL, | |
a056ab8c | 30 | }; |
00173146 | 31 | ATTRIBUTE_GROUPS(hsi_bus_dev); |
a056ab8c CC |
32 | |
33 | static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) | |
34 | { | |
6f02b9e9 | 35 | add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev)); |
a056ab8c CC |
36 | |
37 | return 0; | |
38 | } | |
39 | ||
40 | static int hsi_bus_match(struct device *dev, struct device_driver *driver) | |
41 | { | |
a2aa2473 SR |
42 | if (of_driver_match_device(dev, driver)) |
43 | return true; | |
44 | ||
45 | if (strcmp(dev_name(dev), driver->name) == 0) | |
46 | return true; | |
47 | ||
48 | return false; | |
a056ab8c CC |
49 | } |
50 | ||
51 | static struct bus_type hsi_bus_type = { | |
52 | .name = "hsi", | |
00173146 | 53 | .dev_groups = hsi_bus_dev_groups, |
a056ab8c CC |
54 | .match = hsi_bus_match, |
55 | .uevent = hsi_bus_uevent, | |
56 | }; | |
57 | ||
58 | static void hsi_client_release(struct device *dev) | |
59 | { | |
a088cf16 SR |
60 | struct hsi_client *cl = to_hsi_client(dev); |
61 | ||
62 | kfree(cl->tx_cfg.channels); | |
63 | kfree(cl->rx_cfg.channels); | |
64 | kfree(cl); | |
a056ab8c CC |
65 | } |
66 | ||
84914510 SR |
67 | struct hsi_client *hsi_new_client(struct hsi_port *port, |
68 | struct hsi_board_info *info) | |
a056ab8c CC |
69 | { |
70 | struct hsi_client *cl; | |
a088cf16 | 71 | size_t size; |
a056ab8c CC |
72 | |
73 | cl = kzalloc(sizeof(*cl), GFP_KERNEL); | |
74 | if (!cl) | |
d2c85ac2 | 75 | goto err; |
a088cf16 | 76 | |
a056ab8c | 77 | cl->tx_cfg = info->tx_cfg; |
a088cf16 SR |
78 | if (cl->tx_cfg.channels) { |
79 | size = cl->tx_cfg.num_channels * sizeof(*cl->tx_cfg.channels); | |
b32bd7e7 MFW |
80 | cl->tx_cfg.channels = kmemdup(info->tx_cfg.channels, size, |
81 | GFP_KERNEL); | |
d2c85ac2 IY |
82 | if (!cl->tx_cfg.channels) |
83 | goto err_tx; | |
a088cf16 SR |
84 | } |
85 | ||
a056ab8c | 86 | cl->rx_cfg = info->rx_cfg; |
a088cf16 SR |
87 | if (cl->rx_cfg.channels) { |
88 | size = cl->rx_cfg.num_channels * sizeof(*cl->rx_cfg.channels); | |
b32bd7e7 MFW |
89 | cl->rx_cfg.channels = kmemdup(info->rx_cfg.channels, size, |
90 | GFP_KERNEL); | |
d2c85ac2 IY |
91 | if (!cl->rx_cfg.channels) |
92 | goto err_rx; | |
a088cf16 SR |
93 | } |
94 | ||
a056ab8c CC |
95 | cl->device.bus = &hsi_bus_type; |
96 | cl->device.parent = &port->device; | |
97 | cl->device.release = hsi_client_release; | |
02aa2a37 | 98 | dev_set_name(&cl->device, "%s", info->name); |
a056ab8c | 99 | cl->device.platform_data = info->platform_data; |
a056ab8c CC |
100 | if (info->archdata) |
101 | cl->device.archdata = *info->archdata; | |
102 | if (device_register(&cl->device) < 0) { | |
103 | pr_err("hsi: failed to register client: %s\n", info->name); | |
90e41f9d | 104 | put_device(&cl->device); |
a056ab8c | 105 | } |
84914510 SR |
106 | |
107 | return cl; | |
d2c85ac2 IY |
108 | err_rx: |
109 | kfree(cl->tx_cfg.channels); | |
110 | err_tx: | |
111 | kfree(cl); | |
112 | err: | |
113 | return NULL; | |
a056ab8c | 114 | } |
84914510 | 115 | EXPORT_SYMBOL_GPL(hsi_new_client); |
a056ab8c CC |
116 | |
117 | static void hsi_scan_board_info(struct hsi_controller *hsi) | |
118 | { | |
119 | struct hsi_cl_info *cl_info; | |
120 | struct hsi_port *p; | |
121 | ||
122 | list_for_each_entry(cl_info, &hsi_board_list, list) | |
123 | if (cl_info->info.hsi_id == hsi->id) { | |
124 | p = hsi_find_port_num(hsi, cl_info->info.port); | |
125 | if (!p) | |
126 | continue; | |
127 | hsi_new_client(p, &cl_info->info); | |
128 | } | |
129 | } | |
130 | ||
a2aa2473 SR |
131 | #ifdef CONFIG_OF |
132 | static struct hsi_board_info hsi_char_dev_info = { | |
133 | .name = "hsi_char", | |
134 | }; | |
135 | ||
136 | static int hsi_of_property_parse_mode(struct device_node *client, char *name, | |
137 | unsigned int *result) | |
138 | { | |
139 | const char *mode; | |
140 | int err; | |
141 | ||
142 | err = of_property_read_string(client, name, &mode); | |
143 | if (err < 0) | |
144 | return err; | |
145 | ||
146 | if (strcmp(mode, "stream") == 0) | |
147 | *result = HSI_MODE_STREAM; | |
148 | else if (strcmp(mode, "frame") == 0) | |
149 | *result = HSI_MODE_FRAME; | |
150 | else | |
151 | return -EINVAL; | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | static int hsi_of_property_parse_flow(struct device_node *client, char *name, | |
157 | unsigned int *result) | |
158 | { | |
159 | const char *flow; | |
160 | int err; | |
161 | ||
162 | err = of_property_read_string(client, name, &flow); | |
163 | if (err < 0) | |
164 | return err; | |
165 | ||
166 | if (strcmp(flow, "synchronized") == 0) | |
167 | *result = HSI_FLOW_SYNC; | |
168 | else if (strcmp(flow, "pipeline") == 0) | |
169 | *result = HSI_FLOW_PIPE; | |
170 | else | |
171 | return -EINVAL; | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | static int hsi_of_property_parse_arb_mode(struct device_node *client, | |
177 | char *name, unsigned int *result) | |
178 | { | |
179 | const char *arb_mode; | |
180 | int err; | |
181 | ||
182 | err = of_property_read_string(client, name, &arb_mode); | |
183 | if (err < 0) | |
184 | return err; | |
185 | ||
186 | if (strcmp(arb_mode, "round-robin") == 0) | |
187 | *result = HSI_ARB_RR; | |
188 | else if (strcmp(arb_mode, "priority") == 0) | |
189 | *result = HSI_ARB_PRIO; | |
190 | else | |
191 | return -EINVAL; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static void hsi_add_client_from_dt(struct hsi_port *port, | |
197 | struct device_node *client) | |
198 | { | |
199 | struct hsi_client *cl; | |
200 | struct hsi_channel channel; | |
201 | struct property *prop; | |
202 | char name[32]; | |
203 | int length, cells, err, i, max_chan, mode; | |
204 | ||
205 | cl = kzalloc(sizeof(*cl), GFP_KERNEL); | |
206 | if (!cl) | |
207 | return; | |
208 | ||
209 | err = of_modalias_node(client, name, sizeof(name)); | |
210 | if (err) | |
211 | goto err; | |
212 | ||
a2aa2473 SR |
213 | err = hsi_of_property_parse_mode(client, "hsi-mode", &mode); |
214 | if (err) { | |
215 | err = hsi_of_property_parse_mode(client, "hsi-rx-mode", | |
216 | &cl->rx_cfg.mode); | |
217 | if (err) | |
218 | goto err; | |
219 | ||
220 | err = hsi_of_property_parse_mode(client, "hsi-tx-mode", | |
221 | &cl->tx_cfg.mode); | |
222 | if (err) | |
223 | goto err; | |
224 | } else { | |
225 | cl->rx_cfg.mode = mode; | |
226 | cl->tx_cfg.mode = mode; | |
227 | } | |
228 | ||
229 | err = of_property_read_u32(client, "hsi-speed-kbps", | |
230 | &cl->tx_cfg.speed); | |
231 | if (err) | |
232 | goto err; | |
233 | cl->rx_cfg.speed = cl->tx_cfg.speed; | |
234 | ||
235 | err = hsi_of_property_parse_flow(client, "hsi-flow", | |
236 | &cl->rx_cfg.flow); | |
237 | if (err) | |
238 | goto err; | |
239 | ||
240 | err = hsi_of_property_parse_arb_mode(client, "hsi-arb-mode", | |
241 | &cl->rx_cfg.arb_mode); | |
242 | if (err) | |
243 | goto err; | |
244 | ||
245 | prop = of_find_property(client, "hsi-channel-ids", &length); | |
246 | if (!prop) { | |
247 | err = -EINVAL; | |
248 | goto err; | |
249 | } | |
250 | ||
251 | cells = length / sizeof(u32); | |
252 | ||
253 | cl->rx_cfg.num_channels = cells; | |
254 | cl->tx_cfg.num_channels = cells; | |
67ddd757 | 255 | cl->rx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL); |
a2aa2473 SR |
256 | if (!cl->rx_cfg.channels) { |
257 | err = -ENOMEM; | |
258 | goto err; | |
259 | } | |
260 | ||
67ddd757 | 261 | cl->tx_cfg.channels = kcalloc(cells, sizeof(channel), GFP_KERNEL); |
a2aa2473 SR |
262 | if (!cl->tx_cfg.channels) { |
263 | err = -ENOMEM; | |
264 | goto err2; | |
265 | } | |
266 | ||
267 | max_chan = 0; | |
268 | for (i = 0; i < cells; i++) { | |
269 | err = of_property_read_u32_index(client, "hsi-channel-ids", i, | |
270 | &channel.id); | |
271 | if (err) | |
272 | goto err3; | |
273 | ||
274 | err = of_property_read_string_index(client, "hsi-channel-names", | |
275 | i, &channel.name); | |
276 | if (err) | |
277 | channel.name = NULL; | |
278 | ||
279 | if (channel.id > max_chan) | |
280 | max_chan = channel.id; | |
281 | ||
282 | cl->rx_cfg.channels[i] = channel; | |
283 | cl->tx_cfg.channels[i] = channel; | |
284 | } | |
285 | ||
286 | cl->rx_cfg.num_hw_channels = max_chan + 1; | |
287 | cl->tx_cfg.num_hw_channels = max_chan + 1; | |
288 | ||
289 | cl->device.bus = &hsi_bus_type; | |
290 | cl->device.parent = &port->device; | |
291 | cl->device.release = hsi_client_release; | |
292 | cl->device.of_node = client; | |
293 | ||
5c08b0f7 | 294 | dev_set_name(&cl->device, "%s", name); |
a2aa2473 SR |
295 | if (device_register(&cl->device) < 0) { |
296 | pr_err("hsi: failed to register client: %s\n", name); | |
297 | put_device(&cl->device); | |
a2aa2473 SR |
298 | } |
299 | ||
300 | return; | |
301 | ||
302 | err3: | |
303 | kfree(cl->tx_cfg.channels); | |
304 | err2: | |
305 | kfree(cl->rx_cfg.channels); | |
306 | err: | |
307 | kfree(cl); | |
308 | pr_err("hsi client: missing or incorrect of property: err=%d\n", err); | |
309 | } | |
310 | ||
311 | void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients) | |
312 | { | |
313 | struct device_node *child; | |
314 | ||
315 | /* register hsi-char device */ | |
316 | hsi_new_client(port, &hsi_char_dev_info); | |
317 | ||
318 | for_each_available_child_of_node(clients, child) | |
319 | hsi_add_client_from_dt(port, child); | |
320 | } | |
321 | EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt); | |
322 | #endif | |
323 | ||
84914510 | 324 | int hsi_remove_client(struct device *dev, void *data __maybe_unused) |
a056ab8c | 325 | { |
a056ab8c CC |
326 | device_unregister(dev); |
327 | ||
328 | return 0; | |
329 | } | |
84914510 | 330 | EXPORT_SYMBOL_GPL(hsi_remove_client); |
a056ab8c CC |
331 | |
332 | static int hsi_remove_port(struct device *dev, void *data __maybe_unused) | |
333 | { | |
334 | device_for_each_child(dev, NULL, hsi_remove_client); | |
335 | device_unregister(dev); | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
5a218ceb | 340 | static void hsi_controller_release(struct device *dev) |
a056ab8c | 341 | { |
5a218ceb CC |
342 | struct hsi_controller *hsi = to_hsi_controller(dev); |
343 | ||
344 | kfree(hsi->port); | |
345 | kfree(hsi); | |
a056ab8c CC |
346 | } |
347 | ||
5a218ceb | 348 | static void hsi_port_release(struct device *dev) |
a056ab8c | 349 | { |
5a218ceb | 350 | kfree(to_hsi_port(dev)); |
a056ab8c CC |
351 | } |
352 | ||
a0bf37ed | 353 | /** |
8a77ed6d | 354 | * hsi_port_unregister_clients - Unregister an HSI port |
a0bf37ed SR |
355 | * @port: The HSI port to unregister |
356 | */ | |
357 | void hsi_port_unregister_clients(struct hsi_port *port) | |
358 | { | |
359 | device_for_each_child(&port->device, NULL, hsi_remove_client); | |
360 | } | |
361 | EXPORT_SYMBOL_GPL(hsi_port_unregister_clients); | |
362 | ||
a056ab8c CC |
363 | /** |
364 | * hsi_unregister_controller - Unregister an HSI controller | |
365 | * @hsi: The HSI controller to register | |
366 | */ | |
367 | void hsi_unregister_controller(struct hsi_controller *hsi) | |
368 | { | |
369 | device_for_each_child(&hsi->device, NULL, hsi_remove_port); | |
370 | device_unregister(&hsi->device); | |
371 | } | |
372 | EXPORT_SYMBOL_GPL(hsi_unregister_controller); | |
373 | ||
374 | /** | |
375 | * hsi_register_controller - Register an HSI controller and its ports | |
376 | * @hsi: The HSI controller to register | |
377 | * | |
378 | * Returns -errno on failure, 0 on success. | |
379 | */ | |
380 | int hsi_register_controller(struct hsi_controller *hsi) | |
381 | { | |
382 | unsigned int i; | |
383 | int err; | |
384 | ||
5a218ceb | 385 | err = device_add(&hsi->device); |
a056ab8c CC |
386 | if (err < 0) |
387 | return err; | |
388 | for (i = 0; i < hsi->num_ports; i++) { | |
5a218ceb | 389 | hsi->port[i]->device.parent = &hsi->device; |
5a218ceb | 390 | err = device_add(&hsi->port[i]->device); |
a056ab8c CC |
391 | if (err < 0) |
392 | goto out; | |
393 | } | |
394 | /* Populate HSI bus with HSI clients */ | |
395 | hsi_scan_board_info(hsi); | |
396 | ||
397 | return 0; | |
398 | out: | |
5a218ceb CC |
399 | while (i-- > 0) |
400 | device_del(&hsi->port[i]->device); | |
401 | device_del(&hsi->device); | |
a056ab8c CC |
402 | |
403 | return err; | |
404 | } | |
405 | EXPORT_SYMBOL_GPL(hsi_register_controller); | |
406 | ||
407 | /** | |
408 | * hsi_register_client_driver - Register an HSI client to the HSI bus | |
409 | * @drv: HSI client driver to register | |
410 | * | |
411 | * Returns -errno on failure, 0 on success. | |
412 | */ | |
413 | int hsi_register_client_driver(struct hsi_client_driver *drv) | |
414 | { | |
415 | drv->driver.bus = &hsi_bus_type; | |
416 | ||
417 | return driver_register(&drv->driver); | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(hsi_register_client_driver); | |
420 | ||
421 | static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused) | |
422 | { | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused) | |
427 | { | |
428 | return 0; | |
429 | } | |
430 | ||
5a218ceb CC |
431 | /** |
432 | * hsi_put_controller - Free an HSI controller | |
433 | * | |
434 | * @hsi: Pointer to the HSI controller to freed | |
435 | * | |
436 | * HSI controller drivers should only use this function if they need | |
437 | * to free their allocated hsi_controller structures before a successful | |
438 | * call to hsi_register_controller. Other use is not allowed. | |
439 | */ | |
440 | void hsi_put_controller(struct hsi_controller *hsi) | |
441 | { | |
442 | unsigned int i; | |
443 | ||
444 | if (!hsi) | |
445 | return; | |
446 | ||
447 | for (i = 0; i < hsi->num_ports; i++) | |
448 | if (hsi->port && hsi->port[i]) | |
449 | put_device(&hsi->port[i]->device); | |
450 | put_device(&hsi->device); | |
451 | } | |
452 | EXPORT_SYMBOL_GPL(hsi_put_controller); | |
453 | ||
a056ab8c CC |
454 | /** |
455 | * hsi_alloc_controller - Allocate an HSI controller and its ports | |
456 | * @n_ports: Number of ports on the HSI controller | |
457 | * @flags: Kernel allocation flags | |
458 | * | |
459 | * Return NULL on failure or a pointer to an hsi_controller on success. | |
460 | */ | |
461 | struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) | |
462 | { | |
463 | struct hsi_controller *hsi; | |
5a218ceb | 464 | struct hsi_port **port; |
a056ab8c CC |
465 | unsigned int i; |
466 | ||
467 | if (!n_ports) | |
468 | return NULL; | |
469 | ||
a056ab8c CC |
470 | hsi = kzalloc(sizeof(*hsi), flags); |
471 | if (!hsi) | |
5a218ceb | 472 | return NULL; |
67ddd757 | 473 | port = kcalloc(n_ports, sizeof(*port), flags); |
5a218ceb CC |
474 | if (!port) { |
475 | kfree(hsi); | |
476 | return NULL; | |
a056ab8c CC |
477 | } |
478 | hsi->num_ports = n_ports; | |
479 | hsi->port = port; | |
5a218ceb CC |
480 | hsi->device.release = hsi_controller_release; |
481 | device_initialize(&hsi->device); | |
482 | ||
483 | for (i = 0; i < n_ports; i++) { | |
484 | port[i] = kzalloc(sizeof(**port), flags); | |
485 | if (port[i] == NULL) | |
486 | goto out; | |
487 | port[i]->num = i; | |
488 | port[i]->async = hsi_dummy_msg; | |
489 | port[i]->setup = hsi_dummy_cl; | |
490 | port[i]->flush = hsi_dummy_cl; | |
491 | port[i]->start_tx = hsi_dummy_cl; | |
492 | port[i]->stop_tx = hsi_dummy_cl; | |
493 | port[i]->release = hsi_dummy_cl; | |
494 | mutex_init(&port[i]->lock); | |
de5a3774 | 495 | BLOCKING_INIT_NOTIFIER_HEAD(&port[i]->n_head); |
5a218ceb CC |
496 | dev_set_name(&port[i]->device, "port%d", i); |
497 | hsi->port[i]->device.release = hsi_port_release; | |
498 | device_initialize(&hsi->port[i]->device); | |
499 | } | |
a056ab8c CC |
500 | |
501 | return hsi; | |
502 | out: | |
5a218ceb | 503 | hsi_put_controller(hsi); |
a056ab8c CC |
504 | |
505 | return NULL; | |
506 | } | |
507 | EXPORT_SYMBOL_GPL(hsi_alloc_controller); | |
508 | ||
a056ab8c CC |
509 | /** |
510 | * hsi_free_msg - Free an HSI message | |
511 | * @msg: Pointer to the HSI message | |
512 | * | |
513 | * Client is responsible to free the buffers pointed by the scatterlists. | |
514 | */ | |
515 | void hsi_free_msg(struct hsi_msg *msg) | |
516 | { | |
517 | if (!msg) | |
518 | return; | |
519 | sg_free_table(&msg->sgt); | |
520 | kfree(msg); | |
521 | } | |
522 | EXPORT_SYMBOL_GPL(hsi_free_msg); | |
523 | ||
524 | /** | |
525 | * hsi_alloc_msg - Allocate an HSI message | |
526 | * @nents: Number of memory entries | |
527 | * @flags: Kernel allocation flags | |
528 | * | |
529 | * nents can be 0. This mainly makes sense for read transfer. | |
530 | * In that case, HSI drivers will call the complete callback when | |
531 | * there is data to be read without consuming it. | |
532 | * | |
533 | * Return NULL on failure or a pointer to an hsi_msg on success. | |
534 | */ | |
535 | struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags) | |
536 | { | |
537 | struct hsi_msg *msg; | |
538 | int err; | |
539 | ||
540 | msg = kzalloc(sizeof(*msg), flags); | |
541 | if (!msg) | |
542 | return NULL; | |
543 | ||
544 | if (!nents) | |
545 | return msg; | |
546 | ||
547 | err = sg_alloc_table(&msg->sgt, nents, flags); | |
548 | if (unlikely(err)) { | |
549 | kfree(msg); | |
550 | msg = NULL; | |
551 | } | |
552 | ||
553 | return msg; | |
554 | } | |
555 | EXPORT_SYMBOL_GPL(hsi_alloc_msg); | |
556 | ||
557 | /** | |
558 | * hsi_async - Submit an HSI transfer to the controller | |
559 | * @cl: HSI client sending the transfer | |
560 | * @msg: The HSI transfer passed to controller | |
561 | * | |
562 | * The HSI message must have the channel, ttype, complete and destructor | |
563 | * fields set beforehand. If nents > 0 then the client has to initialize | |
564 | * also the scatterlists to point to the buffers to write to or read from. | |
565 | * | |
566 | * HSI controllers relay on pre-allocated buffers from their clients and they | |
567 | * do not allocate buffers on their own. | |
568 | * | |
569 | * Once the HSI message transfer finishes, the HSI controller calls the | |
570 | * complete callback with the status and actual_len fields of the HSI message | |
571 | * updated. The complete callback can be called before returning from | |
572 | * hsi_async. | |
573 | * | |
574 | * Returns -errno on failure or 0 on success | |
575 | */ | |
576 | int hsi_async(struct hsi_client *cl, struct hsi_msg *msg) | |
577 | { | |
578 | struct hsi_port *port = hsi_get_port(cl); | |
579 | ||
580 | if (!hsi_port_claimed(cl)) | |
581 | return -EACCES; | |
582 | ||
583 | WARN_ON_ONCE(!msg->destructor || !msg->complete); | |
584 | msg->cl = cl; | |
585 | ||
586 | return port->async(msg); | |
587 | } | |
588 | EXPORT_SYMBOL_GPL(hsi_async); | |
589 | ||
590 | /** | |
591 | * hsi_claim_port - Claim the HSI client's port | |
592 | * @cl: HSI client that wants to claim its port | |
593 | * @share: Flag to indicate if the client wants to share the port or not. | |
594 | * | |
595 | * Returns -errno on failure, 0 on success. | |
596 | */ | |
597 | int hsi_claim_port(struct hsi_client *cl, unsigned int share) | |
598 | { | |
599 | struct hsi_port *port = hsi_get_port(cl); | |
600 | int err = 0; | |
601 | ||
602 | mutex_lock(&port->lock); | |
603 | if ((port->claimed) && (!port->shared || !share)) { | |
604 | err = -EBUSY; | |
605 | goto out; | |
606 | } | |
607 | if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) { | |
608 | err = -ENODEV; | |
609 | goto out; | |
610 | } | |
611 | port->claimed++; | |
612 | port->shared = !!share; | |
613 | cl->pclaimed = 1; | |
614 | out: | |
615 | mutex_unlock(&port->lock); | |
616 | ||
617 | return err; | |
618 | } | |
619 | EXPORT_SYMBOL_GPL(hsi_claim_port); | |
620 | ||
621 | /** | |
622 | * hsi_release_port - Release the HSI client's port | |
623 | * @cl: HSI client which previously claimed its port | |
624 | */ | |
625 | void hsi_release_port(struct hsi_client *cl) | |
626 | { | |
627 | struct hsi_port *port = hsi_get_port(cl); | |
628 | ||
629 | mutex_lock(&port->lock); | |
630 | /* Allow HW driver to do some cleanup */ | |
631 | port->release(cl); | |
632 | if (cl->pclaimed) | |
633 | port->claimed--; | |
634 | BUG_ON(port->claimed < 0); | |
635 | cl->pclaimed = 0; | |
636 | if (!port->claimed) | |
637 | port->shared = 0; | |
638 | module_put(to_hsi_controller(port->device.parent)->owner); | |
639 | mutex_unlock(&port->lock); | |
640 | } | |
641 | EXPORT_SYMBOL_GPL(hsi_release_port); | |
642 | ||
ec1c56ff CC |
643 | static int hsi_event_notifier_call(struct notifier_block *nb, |
644 | unsigned long event, void *data __maybe_unused) | |
a056ab8c | 645 | { |
ec1c56ff CC |
646 | struct hsi_client *cl = container_of(nb, struct hsi_client, nb); |
647 | ||
648 | (*cl->ehandler)(cl, event); | |
a056ab8c CC |
649 | |
650 | return 0; | |
651 | } | |
652 | ||
ec1c56ff CC |
653 | /** |
654 | * hsi_register_port_event - Register a client to receive port events | |
655 | * @cl: HSI client that wants to receive port events | |
8eae508b | 656 | * @handler: Event handler callback |
ec1c56ff CC |
657 | * |
658 | * Clients should register a callback to be able to receive | |
659 | * events from the ports. Registration should happen after | |
660 | * claiming the port. | |
661 | * The handler can be called in interrupt context. | |
662 | * | |
663 | * Returns -errno on error, or 0 on success. | |
664 | */ | |
665 | int hsi_register_port_event(struct hsi_client *cl, | |
666 | void (*handler)(struct hsi_client *, unsigned long)) | |
a056ab8c | 667 | { |
ec1c56ff | 668 | struct hsi_port *port = hsi_get_port(cl); |
a056ab8c | 669 | |
ec1c56ff CC |
670 | if (!handler || cl->ehandler) |
671 | return -EINVAL; | |
672 | if (!hsi_port_claimed(cl)) | |
673 | return -EACCES; | |
674 | cl->ehandler = handler; | |
675 | cl->nb.notifier_call = hsi_event_notifier_call; | |
676 | ||
de5a3774 | 677 | return blocking_notifier_chain_register(&port->n_head, &cl->nb); |
a056ab8c | 678 | } |
ec1c56ff | 679 | EXPORT_SYMBOL_GPL(hsi_register_port_event); |
a056ab8c | 680 | |
ec1c56ff CC |
681 | /** |
682 | * hsi_unregister_port_event - Stop receiving port events for a client | |
683 | * @cl: HSI client that wants to stop receiving port events | |
684 | * | |
685 | * Clients should call this function before releasing their associated | |
686 | * port. | |
687 | * | |
688 | * Returns -errno on error, or 0 on success. | |
689 | */ | |
690 | int hsi_unregister_port_event(struct hsi_client *cl) | |
a056ab8c | 691 | { |
ec1c56ff CC |
692 | struct hsi_port *port = hsi_get_port(cl); |
693 | int err; | |
a056ab8c | 694 | |
ec1c56ff | 695 | WARN_ON(!hsi_port_claimed(cl)); |
a056ab8c | 696 | |
de5a3774 | 697 | err = blocking_notifier_chain_unregister(&port->n_head, &cl->nb); |
ec1c56ff CC |
698 | if (!err) |
699 | cl->ehandler = NULL; | |
700 | ||
701 | return err; | |
a056ab8c | 702 | } |
ec1c56ff | 703 | EXPORT_SYMBOL_GPL(hsi_unregister_port_event); |
a056ab8c CC |
704 | |
705 | /** | |
a2aa2473 | 706 | * hsi_event - Notifies clients about port events |
a056ab8c CC |
707 | * @port: Port where the event occurred |
708 | * @event: The event type | |
709 | * | |
710 | * Clients should not be concerned about wake line behavior. However, due | |
711 | * to a race condition in HSI HW protocol, clients need to be notified | |
712 | * about wake line changes, so they can implement a workaround for it. | |
713 | * | |
714 | * Events: | |
715 | * HSI_EVENT_START_RX - Incoming wake line high | |
716 | * HSI_EVENT_STOP_RX - Incoming wake line down | |
ec1c56ff CC |
717 | * |
718 | * Returns -errno on error, or 0 on success. | |
a056ab8c | 719 | */ |
ec1c56ff | 720 | int hsi_event(struct hsi_port *port, unsigned long event) |
a056ab8c | 721 | { |
de5a3774 | 722 | return blocking_notifier_call_chain(&port->n_head, event, NULL); |
a056ab8c CC |
723 | } |
724 | EXPORT_SYMBOL_GPL(hsi_event); | |
725 | ||
a088cf16 SR |
726 | /** |
727 | * hsi_get_channel_id_by_name - acquire channel id by channel name | |
728 | * @cl: HSI client, which uses the channel | |
729 | * @name: name the channel is known under | |
730 | * | |
731 | * Clients can call this function to get the hsi channel ids similar to | |
732 | * requesting IRQs or GPIOs by name. This function assumes the same | |
733 | * channel configuration is used for RX and TX. | |
734 | * | |
735 | * Returns -errno on error or channel id on success. | |
736 | */ | |
737 | int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name) | |
738 | { | |
739 | int i; | |
740 | ||
741 | if (!cl->rx_cfg.channels) | |
742 | return -ENOENT; | |
743 | ||
744 | for (i = 0; i < cl->rx_cfg.num_channels; i++) | |
745 | if (!strcmp(cl->rx_cfg.channels[i].name, name)) | |
746 | return cl->rx_cfg.channels[i].id; | |
747 | ||
748 | return -ENXIO; | |
749 | } | |
750 | EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name); | |
751 | ||
a056ab8c CC |
752 | static int __init hsi_init(void) |
753 | { | |
754 | return bus_register(&hsi_bus_type); | |
755 | } | |
756 | postcore_initcall(hsi_init); | |
757 | ||
758 | static void __exit hsi_exit(void) | |
759 | { | |
760 | bus_unregister(&hsi_bus_type); | |
761 | } | |
762 | module_exit(hsi_exit); | |
763 | ||
764 | MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>"); | |
765 | MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework"); | |
766 | MODULE_LICENSE("GPL v2"); |