Commit | Line | Data |
---|---|---|
a056ab8c CC |
1 | /* |
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> | |
a056ab8c | 24 | #include <linux/list.h> |
a056ab8c CC |
25 | #include <linux/kobject.h> |
26 | #include <linux/slab.h> | |
27 | #include <linux/string.h> | |
ec1c56ff | 28 | #include <linux/notifier.h> |
a2aa2473 SR |
29 | #include <linux/of.h> |
30 | #include <linux/of_device.h> | |
a056ab8c CC |
31 | #include "hsi_core.h" |
32 | ||
a056ab8c CC |
33 | static 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 | } | |
00173146 | 38 | static DEVICE_ATTR_RO(modalias); |
a056ab8c | 39 | |
00173146 GKH |
40 | static struct attribute *hsi_bus_dev_attrs[] = { |
41 | &dev_attr_modalias.attr, | |
42 | NULL, | |
a056ab8c | 43 | }; |
00173146 | 44 | ATTRIBUTE_GROUPS(hsi_bus_dev); |
a056ab8c CC |
45 | |
46 | static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) | |
47 | { | |
6f02b9e9 | 48 | add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev)); |
a056ab8c CC |
49 | |
50 | return 0; | |
51 | } | |
52 | ||
53 | static int hsi_bus_match(struct device *dev, struct device_driver *driver) | |
54 | { | |
a2aa2473 SR |
55 | 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; | |
a056ab8c CC |
62 | } |
63 | ||
64 | static struct bus_type hsi_bus_type = { | |
65 | .name = "hsi", | |
00173146 | 66 | .dev_groups = hsi_bus_dev_groups, |
a056ab8c CC |
67 | .match = hsi_bus_match, |
68 | .uevent = hsi_bus_uevent, | |
69 | }; | |
70 | ||
71 | static void hsi_client_release(struct device *dev) | |
72 | { | |
a088cf16 SR |
73 | struct hsi_client *cl = to_hsi_client(dev); |
74 | ||
75 | kfree(cl->tx_cfg.channels); | |
76 | kfree(cl->rx_cfg.channels); | |
77 | kfree(cl); | |
a056ab8c CC |
78 | } |
79 | ||
84914510 SR |
80 | struct hsi_client *hsi_new_client(struct hsi_port *port, |
81 | struct hsi_board_info *info) | |
a056ab8c CC |
82 | { |
83 | struct hsi_client *cl; | |
a088cf16 | 84 | size_t size; |
a056ab8c CC |
85 | |
86 | cl = kzalloc(sizeof(*cl), GFP_KERNEL); | |
87 | if (!cl) | |
84914510 | 88 | return NULL; |
a088cf16 | 89 | |
a056ab8c | 90 | cl->tx_cfg = info->tx_cfg; |
a088cf16 SR |
91 | 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 | ||
a056ab8c | 97 | cl->rx_cfg = info->rx_cfg; |
a088cf16 SR |
98 | 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 | ||
a056ab8c CC |
104 | cl->device.bus = &hsi_bus_type; |
105 | cl->device.parent = &port->device; | |
106 | cl->device.release = hsi_client_release; | |
02aa2a37 | 107 | dev_set_name(&cl->device, "%s", info->name); |
a056ab8c | 108 | cl->device.platform_data = info->platform_data; |
a056ab8c CC |
109 | 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); | |
90e41f9d | 113 | put_device(&cl->device); |
a056ab8c | 114 | } |
84914510 SR |
115 | |
116 | return cl; | |
a056ab8c | 117 | } |
84914510 | 118 | EXPORT_SYMBOL_GPL(hsi_new_client); |
a056ab8c CC |
119 | |
120 | static 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 | ||
a2aa2473 SR |
134 | #ifdef CONFIG_OF |
135 | static struct hsi_board_info hsi_char_dev_info = { | |
136 | .name = "hsi_char", | |
137 | }; | |
138 | ||
139 | static 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 | ||
159 | static 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 | ||
179 | static 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 | ||
199 | static 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); | |
a2aa2473 SR |
303 | } |
304 | ||
305 | return; | |
306 | ||
307 | err3: | |
308 | kfree(cl->tx_cfg.channels); | |
309 | err2: | |
310 | kfree(cl->rx_cfg.channels); | |
311 | err: | |
312 | kfree(cl); | |
313 | pr_err("hsi client: missing or incorrect of property: err=%d\n", err); | |
314 | } | |
315 | ||
316 | void hsi_add_clients_from_dt(struct hsi_port *port, struct device_node *clients) | |
317 | { | |
318 | struct device_node *child; | |
319 | ||
320 | /* register hsi-char device */ | |
321 | hsi_new_client(port, &hsi_char_dev_info); | |
322 | ||
323 | for_each_available_child_of_node(clients, child) | |
324 | hsi_add_client_from_dt(port, child); | |
325 | } | |
326 | EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt); | |
327 | #endif | |
328 | ||
84914510 | 329 | int hsi_remove_client(struct device *dev, void *data __maybe_unused) |
a056ab8c | 330 | { |
a056ab8c CC |
331 | device_unregister(dev); |
332 | ||
333 | return 0; | |
334 | } | |
84914510 | 335 | EXPORT_SYMBOL_GPL(hsi_remove_client); |
a056ab8c CC |
336 | |
337 | static int hsi_remove_port(struct device *dev, void *data __maybe_unused) | |
338 | { | |
339 | device_for_each_child(dev, NULL, hsi_remove_client); | |
340 | device_unregister(dev); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
5a218ceb | 345 | static void hsi_controller_release(struct device *dev) |
a056ab8c | 346 | { |
5a218ceb CC |
347 | struct hsi_controller *hsi = to_hsi_controller(dev); |
348 | ||
349 | kfree(hsi->port); | |
350 | kfree(hsi); | |
a056ab8c CC |
351 | } |
352 | ||
5a218ceb | 353 | static void hsi_port_release(struct device *dev) |
a056ab8c | 354 | { |
5a218ceb | 355 | kfree(to_hsi_port(dev)); |
a056ab8c CC |
356 | } |
357 | ||
a0bf37ed SR |
358 | /** |
359 | * hsi_unregister_port - Unregister an HSI port | |
360 | * @port: The HSI port to unregister | |
361 | */ | |
362 | void hsi_port_unregister_clients(struct hsi_port *port) | |
363 | { | |
364 | device_for_each_child(&port->device, NULL, hsi_remove_client); | |
365 | } | |
366 | EXPORT_SYMBOL_GPL(hsi_port_unregister_clients); | |
367 | ||
a056ab8c CC |
368 | /** |
369 | * hsi_unregister_controller - Unregister an HSI controller | |
370 | * @hsi: The HSI controller to register | |
371 | */ | |
372 | void hsi_unregister_controller(struct hsi_controller *hsi) | |
373 | { | |
374 | device_for_each_child(&hsi->device, NULL, hsi_remove_port); | |
375 | device_unregister(&hsi->device); | |
376 | } | |
377 | EXPORT_SYMBOL_GPL(hsi_unregister_controller); | |
378 | ||
379 | /** | |
380 | * hsi_register_controller - Register an HSI controller and its ports | |
381 | * @hsi: The HSI controller to register | |
382 | * | |
383 | * Returns -errno on failure, 0 on success. | |
384 | */ | |
385 | int hsi_register_controller(struct hsi_controller *hsi) | |
386 | { | |
387 | unsigned int i; | |
388 | int err; | |
389 | ||
5a218ceb | 390 | err = device_add(&hsi->device); |
a056ab8c CC |
391 | if (err < 0) |
392 | return err; | |
393 | for (i = 0; i < hsi->num_ports; i++) { | |
5a218ceb | 394 | hsi->port[i]->device.parent = &hsi->device; |
5a218ceb | 395 | err = device_add(&hsi->port[i]->device); |
a056ab8c CC |
396 | if (err < 0) |
397 | goto out; | |
398 | } | |
399 | /* Populate HSI bus with HSI clients */ | |
400 | hsi_scan_board_info(hsi); | |
401 | ||
402 | return 0; | |
403 | out: | |
5a218ceb CC |
404 | while (i-- > 0) |
405 | device_del(&hsi->port[i]->device); | |
406 | device_del(&hsi->device); | |
a056ab8c CC |
407 | |
408 | return err; | |
409 | } | |
410 | EXPORT_SYMBOL_GPL(hsi_register_controller); | |
411 | ||
412 | /** | |
413 | * hsi_register_client_driver - Register an HSI client to the HSI bus | |
414 | * @drv: HSI client driver to register | |
415 | * | |
416 | * Returns -errno on failure, 0 on success. | |
417 | */ | |
418 | int hsi_register_client_driver(struct hsi_client_driver *drv) | |
419 | { | |
420 | drv->driver.bus = &hsi_bus_type; | |
421 | ||
422 | return driver_register(&drv->driver); | |
423 | } | |
424 | EXPORT_SYMBOL_GPL(hsi_register_client_driver); | |
425 | ||
426 | static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused) | |
427 | { | |
428 | return 0; | |
429 | } | |
430 | ||
431 | static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused) | |
432 | { | |
433 | return 0; | |
434 | } | |
435 | ||
5a218ceb CC |
436 | /** |
437 | * hsi_put_controller - Free an HSI controller | |
438 | * | |
439 | * @hsi: Pointer to the HSI controller to freed | |
440 | * | |
441 | * HSI controller drivers should only use this function if they need | |
442 | * to free their allocated hsi_controller structures before a successful | |
443 | * call to hsi_register_controller. Other use is not allowed. | |
444 | */ | |
445 | void hsi_put_controller(struct hsi_controller *hsi) | |
446 | { | |
447 | unsigned int i; | |
448 | ||
449 | if (!hsi) | |
450 | return; | |
451 | ||
452 | for (i = 0; i < hsi->num_ports; i++) | |
453 | if (hsi->port && hsi->port[i]) | |
454 | put_device(&hsi->port[i]->device); | |
455 | put_device(&hsi->device); | |
456 | } | |
457 | EXPORT_SYMBOL_GPL(hsi_put_controller); | |
458 | ||
a056ab8c CC |
459 | /** |
460 | * hsi_alloc_controller - Allocate an HSI controller and its ports | |
461 | * @n_ports: Number of ports on the HSI controller | |
462 | * @flags: Kernel allocation flags | |
463 | * | |
464 | * Return NULL on failure or a pointer to an hsi_controller on success. | |
465 | */ | |
466 | struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) | |
467 | { | |
468 | struct hsi_controller *hsi; | |
5a218ceb | 469 | struct hsi_port **port; |
a056ab8c CC |
470 | unsigned int i; |
471 | ||
472 | if (!n_ports) | |
473 | return NULL; | |
474 | ||
a056ab8c CC |
475 | hsi = kzalloc(sizeof(*hsi), flags); |
476 | if (!hsi) | |
5a218ceb CC |
477 | return NULL; |
478 | port = kzalloc(sizeof(*port)*n_ports, flags); | |
479 | if (!port) { | |
480 | kfree(hsi); | |
481 | return NULL; | |
a056ab8c CC |
482 | } |
483 | hsi->num_ports = n_ports; | |
484 | hsi->port = port; | |
5a218ceb CC |
485 | hsi->device.release = hsi_controller_release; |
486 | device_initialize(&hsi->device); | |
487 | ||
488 | for (i = 0; i < n_ports; i++) { | |
489 | port[i] = kzalloc(sizeof(**port), flags); | |
490 | if (port[i] == NULL) | |
491 | goto out; | |
492 | port[i]->num = i; | |
493 | port[i]->async = hsi_dummy_msg; | |
494 | port[i]->setup = hsi_dummy_cl; | |
495 | port[i]->flush = hsi_dummy_cl; | |
496 | port[i]->start_tx = hsi_dummy_cl; | |
497 | port[i]->stop_tx = hsi_dummy_cl; | |
498 | port[i]->release = hsi_dummy_cl; | |
499 | mutex_init(&port[i]->lock); | |
ec1c56ff | 500 | ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head); |
5a218ceb CC |
501 | dev_set_name(&port[i]->device, "port%d", i); |
502 | hsi->port[i]->device.release = hsi_port_release; | |
503 | device_initialize(&hsi->port[i]->device); | |
504 | } | |
a056ab8c CC |
505 | |
506 | return hsi; | |
507 | out: | |
5a218ceb | 508 | hsi_put_controller(hsi); |
a056ab8c CC |
509 | |
510 | return NULL; | |
511 | } | |
512 | EXPORT_SYMBOL_GPL(hsi_alloc_controller); | |
513 | ||
a056ab8c CC |
514 | /** |
515 | * hsi_free_msg - Free an HSI message | |
516 | * @msg: Pointer to the HSI message | |
517 | * | |
518 | * Client is responsible to free the buffers pointed by the scatterlists. | |
519 | */ | |
520 | void hsi_free_msg(struct hsi_msg *msg) | |
521 | { | |
522 | if (!msg) | |
523 | return; | |
524 | sg_free_table(&msg->sgt); | |
525 | kfree(msg); | |
526 | } | |
527 | EXPORT_SYMBOL_GPL(hsi_free_msg); | |
528 | ||
529 | /** | |
530 | * hsi_alloc_msg - Allocate an HSI message | |
531 | * @nents: Number of memory entries | |
532 | * @flags: Kernel allocation flags | |
533 | * | |
534 | * nents can be 0. This mainly makes sense for read transfer. | |
535 | * In that case, HSI drivers will call the complete callback when | |
536 | * there is data to be read without consuming it. | |
537 | * | |
538 | * Return NULL on failure or a pointer to an hsi_msg on success. | |
539 | */ | |
540 | struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags) | |
541 | { | |
542 | struct hsi_msg *msg; | |
543 | int err; | |
544 | ||
545 | msg = kzalloc(sizeof(*msg), flags); | |
546 | if (!msg) | |
547 | return NULL; | |
548 | ||
549 | if (!nents) | |
550 | return msg; | |
551 | ||
552 | err = sg_alloc_table(&msg->sgt, nents, flags); | |
553 | if (unlikely(err)) { | |
554 | kfree(msg); | |
555 | msg = NULL; | |
556 | } | |
557 | ||
558 | return msg; | |
559 | } | |
560 | EXPORT_SYMBOL_GPL(hsi_alloc_msg); | |
561 | ||
562 | /** | |
563 | * hsi_async - Submit an HSI transfer to the controller | |
564 | * @cl: HSI client sending the transfer | |
565 | * @msg: The HSI transfer passed to controller | |
566 | * | |
567 | * The HSI message must have the channel, ttype, complete and destructor | |
568 | * fields set beforehand. If nents > 0 then the client has to initialize | |
569 | * also the scatterlists to point to the buffers to write to or read from. | |
570 | * | |
571 | * HSI controllers relay on pre-allocated buffers from their clients and they | |
572 | * do not allocate buffers on their own. | |
573 | * | |
574 | * Once the HSI message transfer finishes, the HSI controller calls the | |
575 | * complete callback with the status and actual_len fields of the HSI message | |
576 | * updated. The complete callback can be called before returning from | |
577 | * hsi_async. | |
578 | * | |
579 | * Returns -errno on failure or 0 on success | |
580 | */ | |
581 | int hsi_async(struct hsi_client *cl, struct hsi_msg *msg) | |
582 | { | |
583 | struct hsi_port *port = hsi_get_port(cl); | |
584 | ||
585 | if (!hsi_port_claimed(cl)) | |
586 | return -EACCES; | |
587 | ||
588 | WARN_ON_ONCE(!msg->destructor || !msg->complete); | |
589 | msg->cl = cl; | |
590 | ||
591 | return port->async(msg); | |
592 | } | |
593 | EXPORT_SYMBOL_GPL(hsi_async); | |
594 | ||
595 | /** | |
596 | * hsi_claim_port - Claim the HSI client's port | |
597 | * @cl: HSI client that wants to claim its port | |
598 | * @share: Flag to indicate if the client wants to share the port or not. | |
599 | * | |
600 | * Returns -errno on failure, 0 on success. | |
601 | */ | |
602 | int hsi_claim_port(struct hsi_client *cl, unsigned int share) | |
603 | { | |
604 | struct hsi_port *port = hsi_get_port(cl); | |
605 | int err = 0; | |
606 | ||
607 | mutex_lock(&port->lock); | |
608 | if ((port->claimed) && (!port->shared || !share)) { | |
609 | err = -EBUSY; | |
610 | goto out; | |
611 | } | |
612 | if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) { | |
613 | err = -ENODEV; | |
614 | goto out; | |
615 | } | |
616 | port->claimed++; | |
617 | port->shared = !!share; | |
618 | cl->pclaimed = 1; | |
619 | out: | |
620 | mutex_unlock(&port->lock); | |
621 | ||
622 | return err; | |
623 | } | |
624 | EXPORT_SYMBOL_GPL(hsi_claim_port); | |
625 | ||
626 | /** | |
627 | * hsi_release_port - Release the HSI client's port | |
628 | * @cl: HSI client which previously claimed its port | |
629 | */ | |
630 | void hsi_release_port(struct hsi_client *cl) | |
631 | { | |
632 | struct hsi_port *port = hsi_get_port(cl); | |
633 | ||
634 | mutex_lock(&port->lock); | |
635 | /* Allow HW driver to do some cleanup */ | |
636 | port->release(cl); | |
637 | if (cl->pclaimed) | |
638 | port->claimed--; | |
639 | BUG_ON(port->claimed < 0); | |
640 | cl->pclaimed = 0; | |
641 | if (!port->claimed) | |
642 | port->shared = 0; | |
643 | module_put(to_hsi_controller(port->device.parent)->owner); | |
644 | mutex_unlock(&port->lock); | |
645 | } | |
646 | EXPORT_SYMBOL_GPL(hsi_release_port); | |
647 | ||
ec1c56ff CC |
648 | static int hsi_event_notifier_call(struct notifier_block *nb, |
649 | unsigned long event, void *data __maybe_unused) | |
a056ab8c | 650 | { |
ec1c56ff CC |
651 | struct hsi_client *cl = container_of(nb, struct hsi_client, nb); |
652 | ||
653 | (*cl->ehandler)(cl, event); | |
a056ab8c CC |
654 | |
655 | return 0; | |
656 | } | |
657 | ||
ec1c56ff CC |
658 | /** |
659 | * hsi_register_port_event - Register a client to receive port events | |
660 | * @cl: HSI client that wants to receive port events | |
8eae508b | 661 | * @handler: Event handler callback |
ec1c56ff CC |
662 | * |
663 | * Clients should register a callback to be able to receive | |
664 | * events from the ports. Registration should happen after | |
665 | * claiming the port. | |
666 | * The handler can be called in interrupt context. | |
667 | * | |
668 | * Returns -errno on error, or 0 on success. | |
669 | */ | |
670 | int hsi_register_port_event(struct hsi_client *cl, | |
671 | void (*handler)(struct hsi_client *, unsigned long)) | |
a056ab8c | 672 | { |
ec1c56ff | 673 | struct hsi_port *port = hsi_get_port(cl); |
a056ab8c | 674 | |
ec1c56ff CC |
675 | if (!handler || cl->ehandler) |
676 | return -EINVAL; | |
677 | if (!hsi_port_claimed(cl)) | |
678 | return -EACCES; | |
679 | cl->ehandler = handler; | |
680 | cl->nb.notifier_call = hsi_event_notifier_call; | |
681 | ||
682 | return atomic_notifier_chain_register(&port->n_head, &cl->nb); | |
a056ab8c | 683 | } |
ec1c56ff | 684 | EXPORT_SYMBOL_GPL(hsi_register_port_event); |
a056ab8c | 685 | |
ec1c56ff CC |
686 | /** |
687 | * hsi_unregister_port_event - Stop receiving port events for a client | |
688 | * @cl: HSI client that wants to stop receiving port events | |
689 | * | |
690 | * Clients should call this function before releasing their associated | |
691 | * port. | |
692 | * | |
693 | * Returns -errno on error, or 0 on success. | |
694 | */ | |
695 | int hsi_unregister_port_event(struct hsi_client *cl) | |
a056ab8c | 696 | { |
ec1c56ff CC |
697 | struct hsi_port *port = hsi_get_port(cl); |
698 | int err; | |
a056ab8c | 699 | |
ec1c56ff | 700 | WARN_ON(!hsi_port_claimed(cl)); |
a056ab8c | 701 | |
ec1c56ff CC |
702 | err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb); |
703 | if (!err) | |
704 | cl->ehandler = NULL; | |
705 | ||
706 | return err; | |
a056ab8c | 707 | } |
ec1c56ff | 708 | EXPORT_SYMBOL_GPL(hsi_unregister_port_event); |
a056ab8c CC |
709 | |
710 | /** | |
a2aa2473 | 711 | * hsi_event - Notifies clients about port events |
a056ab8c CC |
712 | * @port: Port where the event occurred |
713 | * @event: The event type | |
714 | * | |
715 | * Clients should not be concerned about wake line behavior. However, due | |
716 | * to a race condition in HSI HW protocol, clients need to be notified | |
717 | * about wake line changes, so they can implement a workaround for it. | |
718 | * | |
719 | * Events: | |
720 | * HSI_EVENT_START_RX - Incoming wake line high | |
721 | * HSI_EVENT_STOP_RX - Incoming wake line down | |
ec1c56ff CC |
722 | * |
723 | * Returns -errno on error, or 0 on success. | |
a056ab8c | 724 | */ |
ec1c56ff | 725 | int hsi_event(struct hsi_port *port, unsigned long event) |
a056ab8c | 726 | { |
ec1c56ff | 727 | return atomic_notifier_call_chain(&port->n_head, event, NULL); |
a056ab8c CC |
728 | } |
729 | EXPORT_SYMBOL_GPL(hsi_event); | |
730 | ||
a088cf16 SR |
731 | /** |
732 | * hsi_get_channel_id_by_name - acquire channel id by channel name | |
733 | * @cl: HSI client, which uses the channel | |
734 | * @name: name the channel is known under | |
735 | * | |
736 | * Clients can call this function to get the hsi channel ids similar to | |
737 | * requesting IRQs or GPIOs by name. This function assumes the same | |
738 | * channel configuration is used for RX and TX. | |
739 | * | |
740 | * Returns -errno on error or channel id on success. | |
741 | */ | |
742 | int hsi_get_channel_id_by_name(struct hsi_client *cl, char *name) | |
743 | { | |
744 | int i; | |
745 | ||
746 | if (!cl->rx_cfg.channels) | |
747 | return -ENOENT; | |
748 | ||
749 | for (i = 0; i < cl->rx_cfg.num_channels; i++) | |
750 | if (!strcmp(cl->rx_cfg.channels[i].name, name)) | |
751 | return cl->rx_cfg.channels[i].id; | |
752 | ||
753 | return -ENXIO; | |
754 | } | |
755 | EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name); | |
756 | ||
a056ab8c CC |
757 | static int __init hsi_init(void) |
758 | { | |
759 | return bus_register(&hsi_bus_type); | |
760 | } | |
761 | postcore_initcall(hsi_init); | |
762 | ||
763 | static void __exit hsi_exit(void) | |
764 | { | |
765 | bus_unregister(&hsi_bus_type); | |
766 | } | |
767 | module_exit(hsi_exit); | |
768 | ||
769 | MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>"); | |
770 | MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework"); | |
771 | MODULE_LICENSE("GPL v2"); |