blob: cec1f0c045579f3304454d8b98b3d853e6b8b818 [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>
24#include <linux/rwsem.h>
25#include <linux/list.h>
26#include <linux/spinlock.h>
27#include <linux/kobject.h>
28#include <linux/slab.h>
29#include <linux/string.h>
30#include "hsi_core.h"
31
Carlos Chineaa056ab82010-04-16 19:01:02 +030032static ssize_t modalias_show(struct device *dev,
33 struct device_attribute *a __maybe_unused, char *buf)
34{
35 return sprintf(buf, "hsi:%s\n", dev_name(dev));
36}
37
38static struct device_attribute hsi_bus_dev_attrs[] = {
39 __ATTR_RO(modalias),
40 __ATTR_NULL,
41};
42
43static int hsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
44{
Carlos Chinea6f02b9e2012-04-10 15:11:24 +030045 add_uevent_var(env, "MODALIAS=hsi:%s", dev_name(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +030046
47 return 0;
48}
49
50static int hsi_bus_match(struct device *dev, struct device_driver *driver)
51{
52 return strcmp(dev_name(dev), driver->name) == 0;
53}
54
55static struct bus_type hsi_bus_type = {
56 .name = "hsi",
57 .dev_attrs = hsi_bus_dev_attrs,
58 .match = hsi_bus_match,
59 .uevent = hsi_bus_uevent,
60};
61
62static void hsi_client_release(struct device *dev)
63{
64 kfree(to_hsi_client(dev));
65}
66
67static void hsi_new_client(struct hsi_port *port, struct hsi_board_info *info)
68{
69 struct hsi_client *cl;
70 unsigned long flags;
71
72 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
73 if (!cl)
74 return;
Carlos Chineaa056ab82010-04-16 19:01:02 +030075 cl->tx_cfg = info->tx_cfg;
76 cl->rx_cfg = info->rx_cfg;
77 cl->device.bus = &hsi_bus_type;
78 cl->device.parent = &port->device;
79 cl->device.release = hsi_client_release;
80 dev_set_name(&cl->device, info->name);
81 cl->device.platform_data = info->platform_data;
82 spin_lock_irqsave(&port->clock, flags);
83 list_add_tail(&cl->link, &port->clients);
84 spin_unlock_irqrestore(&port->clock, flags);
85 if (info->archdata)
86 cl->device.archdata = *info->archdata;
87 if (device_register(&cl->device) < 0) {
88 pr_err("hsi: failed to register client: %s\n", info->name);
Carlos Chinea90e41f92012-04-11 11:01:11 +030089 put_device(&cl->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +030090 }
91}
92
93static void hsi_scan_board_info(struct hsi_controller *hsi)
94{
95 struct hsi_cl_info *cl_info;
96 struct hsi_port *p;
97
98 list_for_each_entry(cl_info, &hsi_board_list, list)
99 if (cl_info->info.hsi_id == hsi->id) {
100 p = hsi_find_port_num(hsi, cl_info->info.port);
101 if (!p)
102 continue;
103 hsi_new_client(p, &cl_info->info);
104 }
105}
106
107static int hsi_remove_client(struct device *dev, void *data __maybe_unused)
108{
109 struct hsi_client *cl = to_hsi_client(dev);
110 struct hsi_port *port = to_hsi_port(dev->parent);
111 unsigned long flags;
112
113 spin_lock_irqsave(&port->clock, flags);
114 list_del(&cl->link);
115 spin_unlock_irqrestore(&port->clock, flags);
116 device_unregister(dev);
117
118 return 0;
119}
120
121static int hsi_remove_port(struct device *dev, void *data __maybe_unused)
122{
123 device_for_each_child(dev, NULL, hsi_remove_client);
124 device_unregister(dev);
125
126 return 0;
127}
128
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300129static void hsi_controller_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300130{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300131 struct hsi_controller *hsi = to_hsi_controller(dev);
132
133 kfree(hsi->port);
134 kfree(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300135}
136
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300137static void hsi_port_release(struct device *dev)
Carlos Chineaa056ab82010-04-16 19:01:02 +0300138{
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300139 kfree(to_hsi_port(dev));
Carlos Chineaa056ab82010-04-16 19:01:02 +0300140}
141
142/**
143 * hsi_unregister_controller - Unregister an HSI controller
144 * @hsi: The HSI controller to register
145 */
146void hsi_unregister_controller(struct hsi_controller *hsi)
147{
148 device_for_each_child(&hsi->device, NULL, hsi_remove_port);
149 device_unregister(&hsi->device);
150}
151EXPORT_SYMBOL_GPL(hsi_unregister_controller);
152
153/**
154 * hsi_register_controller - Register an HSI controller and its ports
155 * @hsi: The HSI controller to register
156 *
157 * Returns -errno on failure, 0 on success.
158 */
159int hsi_register_controller(struct hsi_controller *hsi)
160{
161 unsigned int i;
162 int err;
163
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300164 err = device_add(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300165 if (err < 0)
166 return err;
167 for (i = 0; i < hsi->num_ports; i++) {
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300168 hsi->port[i]->device.parent = &hsi->device;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300169 err = device_add(&hsi->port[i]->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300170 if (err < 0)
171 goto out;
172 }
173 /* Populate HSI bus with HSI clients */
174 hsi_scan_board_info(hsi);
175
176 return 0;
177out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300178 while (i-- > 0)
179 device_del(&hsi->port[i]->device);
180 device_del(&hsi->device);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300181
182 return err;
183}
184EXPORT_SYMBOL_GPL(hsi_register_controller);
185
186/**
187 * hsi_register_client_driver - Register an HSI client to the HSI bus
188 * @drv: HSI client driver to register
189 *
190 * Returns -errno on failure, 0 on success.
191 */
192int hsi_register_client_driver(struct hsi_client_driver *drv)
193{
194 drv->driver.bus = &hsi_bus_type;
195
196 return driver_register(&drv->driver);
197}
198EXPORT_SYMBOL_GPL(hsi_register_client_driver);
199
200static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
201{
202 return 0;
203}
204
205static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
206{
207 return 0;
208}
209
210/**
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300211 * hsi_put_controller - Free an HSI controller
212 *
213 * @hsi: Pointer to the HSI controller to freed
214 *
215 * HSI controller drivers should only use this function if they need
216 * to free their allocated hsi_controller structures before a successful
217 * call to hsi_register_controller. Other use is not allowed.
218 */
219void hsi_put_controller(struct hsi_controller *hsi)
220{
221 unsigned int i;
222
223 if (!hsi)
224 return;
225
226 for (i = 0; i < hsi->num_ports; i++)
227 if (hsi->port && hsi->port[i])
228 put_device(&hsi->port[i]->device);
229 put_device(&hsi->device);
230}
231EXPORT_SYMBOL_GPL(hsi_put_controller);
232
233/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300234 * hsi_alloc_controller - Allocate an HSI controller and its ports
235 * @n_ports: Number of ports on the HSI controller
236 * @flags: Kernel allocation flags
237 *
238 * Return NULL on failure or a pointer to an hsi_controller on success.
239 */
240struct hsi_controller *hsi_alloc_controller(unsigned int n_ports, gfp_t flags)
241{
242 struct hsi_controller *hsi;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300243 struct hsi_port **port;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300244 unsigned int i;
245
246 if (!n_ports)
247 return NULL;
248
Carlos Chineaa056ab82010-04-16 19:01:02 +0300249 hsi = kzalloc(sizeof(*hsi), flags);
250 if (!hsi)
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300251 return NULL;
252 port = kzalloc(sizeof(*port)*n_ports, flags);
253 if (!port) {
254 kfree(hsi);
255 return NULL;
Carlos Chineaa056ab82010-04-16 19:01:02 +0300256 }
257 hsi->num_ports = n_ports;
258 hsi->port = port;
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300259 hsi->device.release = hsi_controller_release;
260 device_initialize(&hsi->device);
261
262 for (i = 0; i < n_ports; i++) {
263 port[i] = kzalloc(sizeof(**port), flags);
264 if (port[i] == NULL)
265 goto out;
266 port[i]->num = i;
267 port[i]->async = hsi_dummy_msg;
268 port[i]->setup = hsi_dummy_cl;
269 port[i]->flush = hsi_dummy_cl;
270 port[i]->start_tx = hsi_dummy_cl;
271 port[i]->stop_tx = hsi_dummy_cl;
272 port[i]->release = hsi_dummy_cl;
273 mutex_init(&port[i]->lock);
274 INIT_LIST_HEAD(&hsi->port[i]->clients);
275 spin_lock_init(&hsi->port[i]->clock);
276 dev_set_name(&port[i]->device, "port%d", i);
277 hsi->port[i]->device.release = hsi_port_release;
278 device_initialize(&hsi->port[i]->device);
279 }
Carlos Chineaa056ab82010-04-16 19:01:02 +0300280
281 return hsi;
282out:
Carlos Chinea5a218ce2012-04-04 14:11:45 +0300283 hsi_put_controller(hsi);
Carlos Chineaa056ab82010-04-16 19:01:02 +0300284
285 return NULL;
286}
287EXPORT_SYMBOL_GPL(hsi_alloc_controller);
288
289/**
Carlos Chineaa056ab82010-04-16 19:01:02 +0300290 * hsi_free_msg - Free an HSI message
291 * @msg: Pointer to the HSI message
292 *
293 * Client is responsible to free the buffers pointed by the scatterlists.
294 */
295void hsi_free_msg(struct hsi_msg *msg)
296{
297 if (!msg)
298 return;
299 sg_free_table(&msg->sgt);
300 kfree(msg);
301}
302EXPORT_SYMBOL_GPL(hsi_free_msg);
303
304/**
305 * hsi_alloc_msg - Allocate an HSI message
306 * @nents: Number of memory entries
307 * @flags: Kernel allocation flags
308 *
309 * nents can be 0. This mainly makes sense for read transfer.
310 * In that case, HSI drivers will call the complete callback when
311 * there is data to be read without consuming it.
312 *
313 * Return NULL on failure or a pointer to an hsi_msg on success.
314 */
315struct hsi_msg *hsi_alloc_msg(unsigned int nents, gfp_t flags)
316{
317 struct hsi_msg *msg;
318 int err;
319
320 msg = kzalloc(sizeof(*msg), flags);
321 if (!msg)
322 return NULL;
323
324 if (!nents)
325 return msg;
326
327 err = sg_alloc_table(&msg->sgt, nents, flags);
328 if (unlikely(err)) {
329 kfree(msg);
330 msg = NULL;
331 }
332
333 return msg;
334}
335EXPORT_SYMBOL_GPL(hsi_alloc_msg);
336
337/**
338 * hsi_async - Submit an HSI transfer to the controller
339 * @cl: HSI client sending the transfer
340 * @msg: The HSI transfer passed to controller
341 *
342 * The HSI message must have the channel, ttype, complete and destructor
343 * fields set beforehand. If nents > 0 then the client has to initialize
344 * also the scatterlists to point to the buffers to write to or read from.
345 *
346 * HSI controllers relay on pre-allocated buffers from their clients and they
347 * do not allocate buffers on their own.
348 *
349 * Once the HSI message transfer finishes, the HSI controller calls the
350 * complete callback with the status and actual_len fields of the HSI message
351 * updated. The complete callback can be called before returning from
352 * hsi_async.
353 *
354 * Returns -errno on failure or 0 on success
355 */
356int hsi_async(struct hsi_client *cl, struct hsi_msg *msg)
357{
358 struct hsi_port *port = hsi_get_port(cl);
359
360 if (!hsi_port_claimed(cl))
361 return -EACCES;
362
363 WARN_ON_ONCE(!msg->destructor || !msg->complete);
364 msg->cl = cl;
365
366 return port->async(msg);
367}
368EXPORT_SYMBOL_GPL(hsi_async);
369
370/**
371 * hsi_claim_port - Claim the HSI client's port
372 * @cl: HSI client that wants to claim its port
373 * @share: Flag to indicate if the client wants to share the port or not.
374 *
375 * Returns -errno on failure, 0 on success.
376 */
377int hsi_claim_port(struct hsi_client *cl, unsigned int share)
378{
379 struct hsi_port *port = hsi_get_port(cl);
380 int err = 0;
381
382 mutex_lock(&port->lock);
383 if ((port->claimed) && (!port->shared || !share)) {
384 err = -EBUSY;
385 goto out;
386 }
387 if (!try_module_get(to_hsi_controller(port->device.parent)->owner)) {
388 err = -ENODEV;
389 goto out;
390 }
391 port->claimed++;
392 port->shared = !!share;
393 cl->pclaimed = 1;
394out:
395 mutex_unlock(&port->lock);
396
397 return err;
398}
399EXPORT_SYMBOL_GPL(hsi_claim_port);
400
401/**
402 * hsi_release_port - Release the HSI client's port
403 * @cl: HSI client which previously claimed its port
404 */
405void hsi_release_port(struct hsi_client *cl)
406{
407 struct hsi_port *port = hsi_get_port(cl);
408
409 mutex_lock(&port->lock);
410 /* Allow HW driver to do some cleanup */
411 port->release(cl);
412 if (cl->pclaimed)
413 port->claimed--;
414 BUG_ON(port->claimed < 0);
415 cl->pclaimed = 0;
416 if (!port->claimed)
417 port->shared = 0;
418 module_put(to_hsi_controller(port->device.parent)->owner);
419 mutex_unlock(&port->lock);
420}
421EXPORT_SYMBOL_GPL(hsi_release_port);
422
423static int hsi_start_rx(struct hsi_client *cl, void *data __maybe_unused)
424{
425 if (cl->hsi_start_rx)
426 (*cl->hsi_start_rx)(cl);
427
428 return 0;
429}
430
431static int hsi_stop_rx(struct hsi_client *cl, void *data __maybe_unused)
432{
433 if (cl->hsi_stop_rx)
434 (*cl->hsi_stop_rx)(cl);
435
436 return 0;
437}
438
439static int hsi_port_for_each_client(struct hsi_port *port, void *data,
440 int (*fn)(struct hsi_client *cl, void *data))
441{
442 struct hsi_client *cl;
443
444 spin_lock(&port->clock);
445 list_for_each_entry(cl, &port->clients, link) {
446 spin_unlock(&port->clock);
447 (*fn)(cl, data);
448 spin_lock(&port->clock);
449 }
450 spin_unlock(&port->clock);
451
452 return 0;
453}
454
455/**
456 * hsi_event -Notifies clients about port events
457 * @port: Port where the event occurred
458 * @event: The event type
459 *
460 * Clients should not be concerned about wake line behavior. However, due
461 * to a race condition in HSI HW protocol, clients need to be notified
462 * about wake line changes, so they can implement a workaround for it.
463 *
464 * Events:
465 * HSI_EVENT_START_RX - Incoming wake line high
466 * HSI_EVENT_STOP_RX - Incoming wake line down
467 */
468void hsi_event(struct hsi_port *port, unsigned int event)
469{
470 int (*fn)(struct hsi_client *cl, void *data);
471
472 switch (event) {
473 case HSI_EVENT_START_RX:
474 fn = hsi_start_rx;
475 break;
476 case HSI_EVENT_STOP_RX:
477 fn = hsi_stop_rx;
478 break;
479 default:
480 return;
481 }
482 hsi_port_for_each_client(port, NULL, fn);
483}
484EXPORT_SYMBOL_GPL(hsi_event);
485
486static int __init hsi_init(void)
487{
488 return bus_register(&hsi_bus_type);
489}
490postcore_initcall(hsi_init);
491
492static void __exit hsi_exit(void)
493{
494 bus_unregister(&hsi_bus_type);
495}
496module_exit(hsi_exit);
497
498MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
499MODULE_DESCRIPTION("High-speed Synchronous Serial Interface (HSI) framework");
500MODULE_LICENSE("GPL v2");