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); | |
303 | goto err3; | |
304 | } | |
305 | ||
306 | return; | |
307 | ||
308 | err3: | |
309 | kfree(cl->tx_cfg.channels); | |
310 | err2: | |
311 | kfree(cl->rx_cfg.channels); | |
312 | err: | |
313 | kfree(cl); | |
314 | pr_err("hsi client: missing or incorrect of property: err=%d\n", err); | |
315 | } | |
316 | ||
317 | void 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 | } | |
327 | EXPORT_SYMBOL_GPL(hsi_add_clients_from_dt); | |
328 | #endif | |
329 | ||
84914510 | 330 | int hsi_remove_client(struct device *dev, void *data __maybe_unused) |
a056ab8c | 331 | { |
a056ab8c CC |
332 | device_unregister(dev); |
333 | ||
334 | return 0; | |
335 | } | |
84914510 | 336 | EXPORT_SYMBOL_GPL(hsi_remove_client); |
a056ab8c CC |
337 | |
338 | static 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 | ||
5a218ceb | 346 | static void hsi_controller_release(struct device *dev) |
a056ab8c | 347 | { |
5a218ceb CC |
348 | struct hsi_controller *hsi = to_hsi_controller(dev); |
349 | ||
350 | kfree(hsi->port); | |
351 | kfree(hsi); | |
a056ab8c CC |
352 | } |
353 | ||
5a218ceb | 354 | static void hsi_port_release(struct device *dev) |
a056ab8c | 355 | { |
5a218ceb | 356 | kfree(to_hsi_port(dev)); |
a056ab8c CC |
357 | } |
358 | ||
a0bf37ed SR |
359 | /** |
360 | * hsi_unregister_port - Unregister an HSI port | |
361 | * @port: The HSI port to unregister | |
362 | */ | |
363 | void hsi_port_unregister_clients(struct hsi_port *port) | |
364 | { | |
365 | device_for_each_child(&port->device, NULL, hsi_remove_client); | |
366 | } | |
367 | EXPORT_SYMBOL_GPL(hsi_port_unregister_clients); | |
368 | ||
a056ab8c CC |
369 | /** |
370 | * hsi_unregister_controller - Unregister an HSI controller | |
371 | * @hsi: The HSI controller to register | |
372 | */ | |
373 | void 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 | } | |
378 | EXPORT_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 | */ | |
386 | int hsi_register_controller(struct hsi_controller *hsi) | |
387 | { | |
388 | unsigned int i; | |
389 | int err; | |
390 | ||
5a218ceb | 391 | err = device_add(&hsi->device); |
a056ab8c CC |
392 | if (err < 0) |
393 | return err; | |
394 | for (i = 0; i < hsi->num_ports; i++) { | |
5a218ceb | 395 | hsi->port[i]->device.parent = &hsi->device; |
5a218ceb | 396 | err = device_add(&hsi->port[i]->device); |
a056ab8c CC |
397 | if (err < 0) |
398 | goto out; | |
399 | } | |
400 | /* Populate HSI bus with HSI clients */ | |
401 | hsi_scan_board_info(hsi); | |
402 | ||
403 | return 0; | |
404 | out: | |
5a218ceb CC |
405 | while (i-- > 0) |
406 | device_del(&hsi->port[i]->device); | |
407 | device_del(&hsi->device); | |
a056ab8c CC |
408 | |
409 | return err; | |
410 | } | |
411 | EXPORT_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 | */ | |
419 | int 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 | } | |
425 | EXPORT_SYMBOL_GPL(hsi_register_client_driver); | |
426 | ||
427 | static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused) | |
428 | { | |
429 | return 0; | |
430 | } | |
431 | ||
432 | static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused) | |
433 | { | |
434 | return 0; | |
435 | } | |
436 | ||
5a218ceb CC |
437 | /** |
438 | * 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 | */ | |
446 | void 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 | } | |
458 | EXPORT_SYMBOL_GPL(hsi_put_controller); | |
459 | ||
a056ab8c CC |
460 | /** |
461 | * 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 | */ | |
467 | struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags) | |
468 | { | |
469 | struct hsi_controller *hsi; | |
5a218ceb | 470 | struct hsi_port **port; |
a056ab8c CC |
471 | unsigned int i; |
472 | ||
473 | if (!n_ports) | |
474 | return NULL; | |
475 | ||
a056ab8c CC |
476 | hsi = kzalloc(sizeof(*hsi), flags); |
477 | if (!hsi) | |
5a218ceb CC |
478 | return NULL; |
479 | port = kzalloc(sizeof(*port)*n_ports, flags); | |
480 | if (!port) { | |
481 | kfree(hsi); | |
482 | return NULL; | |
a056ab8c CC |
483 | } |
484 | hsi->num_ports = n_ports; | |
485 | hsi->port = port; | |
5a218ceb CC |
486 | 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); | |
ec1c56ff | 501 | ATOMIC_INIT_NOTIFIER_HEAD(&port[i]->n_head); |
5a218ceb CC |
502 | 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 | } | |
a056ab8c CC |
506 | |
507 | return hsi; | |
508 | out: | |
5a218ceb | 509 | hsi_put_controller(hsi); |
a056ab8c CC |
510 | |
511 | return NULL; | |
512 | } | |
513 | EXPORT_SYMBOL_GPL(hsi_alloc_controller); | |
514 | ||
a056ab8c CC |
515 | /** |
516 | * 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 | */ | |
521 | void hsi_free_msg(struct hsi_msg *msg) | |
522 | { | |
523 | if (!msg) | |
524 | return; | |
525 | sg_free_table(&msg->sgt); | |
526 | kfree(msg); | |
527 | } | |
528 | EXPORT_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 | */ | |
541 | struct 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 | } | |
561 | EXPORT_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 | */ | |
582 | int 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 | } | |
594 | EXPORT_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 | */ | |
603 | int 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; | |
620 | out: | |
621 | mutex_unlock(&port->lock); | |
622 | ||
623 | return err; | |
624 | } | |
625 | EXPORT_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 | */ | |
631 | void 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 | } | |
647 | EXPORT_SYMBOL_GPL(hsi_release_port); | |
648 | ||
ec1c56ff CC |
649 | static int hsi_event_notifier_call(struct notifier_block *nb, |
650 | unsigned long event, void *data __maybe_unused) | |
a056ab8c | 651 | { |
ec1c56ff CC |
652 | struct hsi_client *cl = container_of(nb, struct hsi_client, nb); |
653 | ||
654 | (*cl->ehandler)(cl, event); | |
a056ab8c CC |
655 | |
656 | return 0; | |
657 | } | |
658 | ||
ec1c56ff CC |
659 | /** |
660 | * hsi_register_port_event - Register a client to receive port events | |
661 | * @cl: HSI client that wants to receive port events | |
8eae508b | 662 | * @handler: Event handler callback |
ec1c56ff CC |
663 | * |
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 | */ | |
671 | int hsi_register_port_event(struct hsi_client *cl, | |
672 | void (*handler)(struct hsi_client *, unsigned long)) | |
a056ab8c | 673 | { |
ec1c56ff | 674 | struct hsi_port *port = hsi_get_port(cl); |
a056ab8c | 675 | |
ec1c56ff CC |
676 | 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); | |
a056ab8c | 684 | } |
ec1c56ff | 685 | EXPORT_SYMBOL_GPL(hsi_register_port_event); |
a056ab8c | 686 | |
ec1c56ff CC |
687 | /** |
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 | */ | |
696 | int hsi_unregister_port_event(struct hsi_client *cl) | |
a056ab8c | 697 | { |
ec1c56ff CC |
698 | struct hsi_port *port = hsi_get_port(cl); |
699 | int err; | |
a056ab8c | 700 | |
ec1c56ff | 701 | WARN_ON(!hsi_port_claimed(cl)); |
a056ab8c | 702 | |
ec1c56ff CC |
703 | err = atomic_notifier_chain_unregister(&port->n_head, &cl->nb); |
704 | if (!err) | |
705 | cl->ehandler = NULL; | |
706 | ||
707 | return err; | |
a056ab8c | 708 | } |
ec1c56ff | 709 | EXPORT_SYMBOL_GPL(hsi_unregister_port_event); |
a056ab8c CC |
710 | |
711 | /** | |
a2aa2473 | 712 | * hsi_event - Notifies clients about port events |
a056ab8c CC |
713 | * @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 | |
ec1c56ff CC |
723 | * |
724 | * Returns -errno on error, or 0 on success. | |
a056ab8c | 725 | */ |
ec1c56ff | 726 | int hsi_event(struct hsi_port *port, unsigned long event) |
a056ab8c | 727 | { |
ec1c56ff | 728 | return atomic_notifier_call_chain(&port->n_head, event, NULL); |
a056ab8c CC |
729 | } |
730 | EXPORT_SYMBOL_GPL(hsi_event); | |
731 | ||
a088cf16 SR |
732 | /** |
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 | */ | |
743 | int 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 | } | |
756 | EXPORT_SYMBOL_GPL(hsi_get_channel_id_by_name); | |
757 | ||
a056ab8c CC |
758 | static int __init hsi_init(void) |
759 | { | |
760 | return bus_register(&hsi_bus_type); | |
761 | } | |
762 | postcore_initcall(hsi_init); | |
763 | ||
764 | static void __exit hsi_exit(void) | |
765 | { | |
766 | bus_unregister(&hsi_bus_type); | |
767 | } | |
768 | module_exit(hsi_exit); | |
769 | ||
770 | MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>"); | |
771 | MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework"); | |
772 | MODULE_LICENSE("GPL v2"); |