blob: fc7c5507f358abfcb5647fde888a6872ab8a5854 [file] [log] [blame]
Sagar Dharia4f240722014-01-15 13:50:58 -07001/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/completion.h>
18#include <linux/idr.h>
19#include <linux/pm_runtime.h>
20#include <linux/slimbus/slimbus.h>
21
22#define SLIM_PORT_HDL(la, f, p) ((la)<<24 | (f) << 16 | (p))
23
24#define SLIM_HDL_TO_LA(hdl) ((u32)((hdl) & 0xFF000000) >> 24)
25#define SLIM_HDL_TO_FLOW(hdl) (((u32)(hdl) & 0xFF0000) >> 16)
26#define SLIM_HDL_TO_PORT(hdl) ((u32)(hdl) & 0xFF)
27
Sagar Dharia29f35f02011-10-01 20:37:50 -060028#define SLIM_HDL_TO_CHIDX(hdl) ((u16)(hdl) & 0xFF)
Sagar Dhariab886e042012-10-17 22:41:57 -060029#define SLIM_GRP_TO_NCHAN(hdl) ((u16)(hdl >> 8) & 0xFF)
Sagar Dharia29f35f02011-10-01 20:37:50 -060030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#define SLIM_SLAVE_PORT(p, la) (((la)<<16) | (p))
32#define SLIM_MGR_PORT(p) ((0xFF << 16) | (p))
33#define SLIM_LA_MANAGER 0xFF
34
35#define SLIM_START_GRP (1 << 8)
36#define SLIM_END_GRP (1 << 9)
37
38#define SLIM_MAX_INTR_COEFF_3 (SLIM_SL_PER_SUPERFRAME/3)
39#define SLIM_MAX_INTR_COEFF_1 SLIM_SL_PER_SUPERFRAME
40
41static DEFINE_MUTEX(slim_lock);
42static DEFINE_IDR(ctrl_idr);
43static struct device_type slim_dev_type;
44static struct device_type slim_ctrl_type;
45
46static const struct slim_device_id *slim_match(const struct slim_device_id *id,
47 const struct slim_device *slim_dev)
48{
49 while (id->name[0]) {
50 if (strncmp(slim_dev->name, id->name, SLIMBUS_NAME_SIZE) == 0)
51 return id;
52 id++;
53 }
54 return NULL;
55}
56
57static int slim_device_match(struct device *dev, struct device_driver *driver)
58{
59 struct slim_device *slim_dev;
60 struct slim_driver *drv = to_slim_driver(driver);
61
62 if (dev->type == &slim_dev_type)
63 slim_dev = to_slim_device(dev);
64 else
65 return 0;
66 if (drv->id_table)
67 return slim_match(drv->id_table, slim_dev) != NULL;
68
69 if (driver->name)
70 return strncmp(slim_dev->name, driver->name, SLIMBUS_NAME_SIZE)
71 == 0;
72 return 0;
73}
74
75#ifdef CONFIG_PM_SLEEP
76static int slim_legacy_suspend(struct device *dev, pm_message_t mesg)
77{
78 struct slim_device *slim_dev = NULL;
79 struct slim_driver *driver;
80 if (dev->type == &slim_dev_type)
81 slim_dev = to_slim_device(dev);
82
83 if (!slim_dev || !dev->driver)
84 return 0;
85
86 driver = to_slim_driver(dev->driver);
87 if (!driver->suspend)
88 return 0;
89
90 return driver->suspend(slim_dev, mesg);
91}
92
93static int slim_legacy_resume(struct device *dev)
94{
95 struct slim_device *slim_dev = NULL;
96 struct slim_driver *driver;
97 if (dev->type == &slim_dev_type)
98 slim_dev = to_slim_device(dev);
99
100 if (!slim_dev || !dev->driver)
101 return 0;
102
103 driver = to_slim_driver(dev->driver);
104 if (!driver->resume)
105 return 0;
106
107 return driver->resume(slim_dev);
108}
109
110static int slim_pm_suspend(struct device *dev)
111{
112 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
113
114 if (pm)
115 return pm_generic_suspend(dev);
116 else
117 return slim_legacy_suspend(dev, PMSG_SUSPEND);
118}
119
120static int slim_pm_resume(struct device *dev)
121{
122 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
123
124 if (pm)
125 return pm_generic_resume(dev);
126 else
127 return slim_legacy_resume(dev);
128}
129
130#else
131#define slim_pm_suspend NULL
132#define slim_pm_resume NULL
133#endif
134
135static const struct dev_pm_ops slimbus_pm = {
136 .suspend = slim_pm_suspend,
137 .resume = slim_pm_resume,
138 SET_RUNTIME_PM_OPS(
139 pm_generic_suspend,
140 pm_generic_resume,
141 pm_generic_runtime_idle
142 )
143};
144struct bus_type slimbus_type = {
145 .name = "slimbus",
146 .match = slim_device_match,
147 .pm = &slimbus_pm,
148};
149EXPORT_SYMBOL_GPL(slimbus_type);
150
151struct device slimbus_dev = {
152 .init_name = "slimbus",
153};
154
155static void __exit slimbus_exit(void)
156{
157 device_unregister(&slimbus_dev);
158 bus_unregister(&slimbus_type);
159}
160
161static int __init slimbus_init(void)
162{
163 int retval;
164
165 retval = bus_register(&slimbus_type);
166 if (!retval)
167 retval = device_register(&slimbus_dev);
168
169 if (retval)
170 bus_unregister(&slimbus_type);
171
172 return retval;
173}
174postcore_initcall(slimbus_init);
175module_exit(slimbus_exit);
176
177static int slim_drv_probe(struct device *dev)
178{
179 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600180 struct slim_device *sbdev = to_slim_device(dev);
181 struct slim_controller *ctrl = sbdev->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600183 if (sdrv->probe) {
184 int ret;
185 ret = sdrv->probe(sbdev);
186 if (ret)
187 return ret;
188 if (sdrv->device_up)
189 queue_work(ctrl->wq, &sbdev->wd);
190 return 0;
191 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 return -ENODEV;
193}
194
195static int slim_drv_remove(struct device *dev)
196{
197 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600198 struct slim_device *sbdev = to_slim_device(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600200 sbdev->notified = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 if (sdrv->remove)
202 return sdrv->remove(to_slim_device(dev));
203 return -ENODEV;
204}
205
206static void slim_drv_shutdown(struct device *dev)
207{
208 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
209
210 if (sdrv->shutdown)
211 sdrv->shutdown(to_slim_device(dev));
212}
213
214/*
215 * slim_driver_register: Client driver registration with slimbus
216 * @drv:Client driver to be associated with client-device.
217 * This API will register the client driver with the slimbus
218 * It is called from the driver's module-init function.
219 */
220int slim_driver_register(struct slim_driver *drv)
221{
222 drv->driver.bus = &slimbus_type;
223 if (drv->probe)
224 drv->driver.probe = slim_drv_probe;
225
226 if (drv->remove)
227 drv->driver.remove = slim_drv_remove;
228
229 if (drv->shutdown)
230 drv->driver.shutdown = slim_drv_shutdown;
231
232 return driver_register(&drv->driver);
233}
234EXPORT_SYMBOL_GPL(slim_driver_register);
235
Sagar Dharia371bfa22013-01-22 17:51:41 -0700236/*
237 * slim_driver_unregister: Undo effects of slim_driver_register
238 * @drv: Client driver to be unregistered
239 */
240void slim_driver_unregister(struct slim_driver *drv)
241{
242 if (drv)
243 driver_unregister(&drv->driver);
244}
245EXPORT_SYMBOL_GPL(slim_driver_unregister);
246
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247#define slim_ctrl_attr_gr NULL
248
249static void slim_ctrl_release(struct device *dev)
250{
251 struct slim_controller *ctrl = to_slim_controller(dev);
252
253 complete(&ctrl->dev_released);
254}
255
256static struct device_type slim_ctrl_type = {
257 .groups = slim_ctrl_attr_gr,
258 .release = slim_ctrl_release,
259};
260
261static struct slim_controller *slim_ctrl_get(struct slim_controller *ctrl)
262{
263 if (!ctrl || !get_device(&ctrl->dev))
264 return NULL;
265
266 return ctrl;
267}
268
269static void slim_ctrl_put(struct slim_controller *ctrl)
270{
271 if (ctrl)
272 put_device(&ctrl->dev);
273}
274
275#define slim_device_attr_gr NULL
276#define slim_device_uevent NULL
277static void slim_dev_release(struct device *dev)
278{
279 struct slim_device *sbdev = to_slim_device(dev);
280 slim_ctrl_put(sbdev->ctrl);
281}
282
283static struct device_type slim_dev_type = {
284 .groups = slim_device_attr_gr,
285 .uevent = slim_device_uevent,
286 .release = slim_dev_release,
287};
288
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600289static void slim_report(struct work_struct *work)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600290{
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600291 struct slim_driver *sbdrv;
292 struct slim_device *sbdev =
293 container_of(work, struct slim_device, wd);
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600294 if (!sbdev->dev.driver)
295 return;
296 /* check if device-up or down needs to be called */
Sagar Dharia21f88552014-02-07 00:10:37 -0700297 if ((!sbdev->reported && !sbdev->notified) ||
298 (sbdev->reported && sbdev->notified))
299 return;
300
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600301 sbdrv = to_slim_driver(sbdev->dev.driver);
Sagar Dharia21f88552014-02-07 00:10:37 -0700302 /*
303 * address no longer valid, means device reported absent, whereas
304 * address valid, means device reported present
305 */
306 if (sbdev->notified && !sbdev->reported) {
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600307 sbdev->notified = false;
308 if (sbdrv->device_down)
309 sbdrv->device_down(sbdev);
Sagar Dharia21f88552014-02-07 00:10:37 -0700310 } else if (!sbdev->notified && sbdev->reported) {
Sagar Dharia4f240722014-01-15 13:50:58 -0700311 sbdev->notified = true;
Sagar Dharia21f88552014-02-07 00:10:37 -0700312 if (sbdrv->device_up)
313 sbdrv->device_up(sbdev);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600314 }
315}
316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317/*
318 * slim_add_device: Add a new device without register board info.
319 * @ctrl: Controller to which this device is to be added to.
320 * Called when device doesn't have an explicit client-driver to be probed, or
321 * the client-driver is a module installed dynamically.
322 */
323int slim_add_device(struct slim_controller *ctrl, struct slim_device *sbdev)
324{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 sbdev->dev.bus = &slimbus_type;
326 sbdev->dev.parent = ctrl->dev.parent;
327 sbdev->dev.type = &slim_dev_type;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600328 sbdev->dev.driver = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 sbdev->ctrl = ctrl;
330 slim_ctrl_get(ctrl);
331 dev_set_name(&sbdev->dev, "%s", sbdev->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 mutex_init(&sbdev->sldev_reconf);
333 INIT_LIST_HEAD(&sbdev->mark_define);
334 INIT_LIST_HEAD(&sbdev->mark_suspend);
335 INIT_LIST_HEAD(&sbdev->mark_removal);
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600336 INIT_WORK(&sbdev->wd, slim_report);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600337 mutex_lock(&ctrl->m_ctrl);
338 list_add_tail(&sbdev->dev_list, &ctrl->devs);
339 mutex_unlock(&ctrl->m_ctrl);
340 /* probe slave on this controller */
341 return device_register(&sbdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342}
343EXPORT_SYMBOL_GPL(slim_add_device);
344
345struct sbi_boardinfo {
346 struct list_head list;
347 struct slim_boardinfo board_info;
348};
349
350static LIST_HEAD(board_list);
351static LIST_HEAD(slim_ctrl_list);
352static DEFINE_MUTEX(board_lock);
353
354/* If controller is not present, only add to boards list */
355static void slim_match_ctrl_to_boardinfo(struct slim_controller *ctrl,
356 struct slim_boardinfo *bi)
357{
358 int ret;
359 if (ctrl->nr != bi->bus_num)
360 return;
361
362 ret = slim_add_device(ctrl, bi->slim_slave);
363 if (ret != 0)
364 dev_err(ctrl->dev.parent, "can't create new device for %s\n",
365 bi->slim_slave->name);
366}
367
368/*
369 * slim_register_board_info: Board-initialization routine.
370 * @info: List of all devices on all controllers present on the board.
371 * @n: number of entries.
372 * API enumerates respective devices on corresponding controller.
373 * Called from board-init function.
374 */
375int slim_register_board_info(struct slim_boardinfo const *info, unsigned n)
376{
377 struct sbi_boardinfo *bi;
378 int i;
379
380 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
381 if (!bi)
382 return -ENOMEM;
383
384 for (i = 0; i < n; i++, bi++, info++) {
385 struct slim_controller *ctrl;
386
387 memcpy(&bi->board_info, info, sizeof(*info));
388 mutex_lock(&board_lock);
389 list_add_tail(&bi->list, &board_list);
390 list_for_each_entry(ctrl, &slim_ctrl_list, list)
391 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
392 mutex_unlock(&board_lock);
393 }
394 return 0;
395}
396EXPORT_SYMBOL_GPL(slim_register_board_info);
397
398/*
Sagar Dhariaa6627e02012-08-28 12:20:49 -0600399 * slim_ctrl_add_boarddevs: Add devices registered by board-info
400 * @ctrl: Controller to which these devices are to be added to.
401 * This API is called by controller when it is up and running.
402 * If devices on a controller were registered before controller,
403 * this will make sure that they get probed when controller is up.
404 */
405void slim_ctrl_add_boarddevs(struct slim_controller *ctrl)
406{
407 struct sbi_boardinfo *bi;
408 mutex_lock(&board_lock);
409 list_add_tail(&ctrl->list, &slim_ctrl_list);
410 list_for_each_entry(bi, &board_list, list)
411 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
412 mutex_unlock(&board_lock);
413}
414EXPORT_SYMBOL_GPL(slim_ctrl_add_boarddevs);
415
416/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 * slim_busnum_to_ctrl: Map bus number to controller
418 * @busnum: Bus number
419 * Returns controller representing this bus number
420 */
421struct slim_controller *slim_busnum_to_ctrl(u32 bus_num)
422{
423 struct slim_controller *ctrl;
424 mutex_lock(&board_lock);
425 list_for_each_entry(ctrl, &slim_ctrl_list, list)
426 if (bus_num == ctrl->nr) {
427 mutex_unlock(&board_lock);
428 return ctrl;
429 }
430 mutex_unlock(&board_lock);
431 return NULL;
432}
433EXPORT_SYMBOL_GPL(slim_busnum_to_ctrl);
434
435static int slim_register_controller(struct slim_controller *ctrl)
436{
437 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438
439 /* Can't register until after driver model init */
440 if (WARN_ON(!slimbus_type.p)) {
441 ret = -EAGAIN;
442 goto out_list;
443 }
444
445 dev_set_name(&ctrl->dev, "sb-%d", ctrl->nr);
446 ctrl->dev.bus = &slimbus_type;
447 ctrl->dev.type = &slim_ctrl_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 ctrl->num_dev = 0;
Sagar Dharia98a7ecb2011-07-25 15:25:35 -0600449 if (!ctrl->min_cg)
450 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
451 if (!ctrl->max_cg)
452 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 mutex_init(&ctrl->m_ctrl);
454 mutex_init(&ctrl->sched.m_reconf);
455 ret = device_register(&ctrl->dev);
456 if (ret)
457 goto out_list;
458
459 dev_dbg(&ctrl->dev, "Bus [%s] registered:dev:%x\n", ctrl->name,
460 (u32)&ctrl->dev);
461
462 if (ctrl->nports) {
463 ctrl->ports = kzalloc(ctrl->nports * sizeof(struct slim_port),
464 GFP_KERNEL);
465 if (!ctrl->ports) {
466 ret = -ENOMEM;
467 goto err_port_failed;
468 }
469 }
470 if (ctrl->nchans) {
471 ctrl->chans = kzalloc(ctrl->nchans * sizeof(struct slim_ich),
472 GFP_KERNEL);
473 if (!ctrl->chans) {
474 ret = -ENOMEM;
475 goto err_chan_failed;
476 }
477
478 ctrl->sched.chc1 =
479 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
480 GFP_KERNEL);
481 if (!ctrl->sched.chc1) {
482 kfree(ctrl->chans);
483 ret = -ENOMEM;
484 goto err_chan_failed;
485 }
486 ctrl->sched.chc3 =
487 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
488 GFP_KERNEL);
489 if (!ctrl->sched.chc3) {
490 kfree(ctrl->sched.chc1);
491 kfree(ctrl->chans);
492 ret = -ENOMEM;
493 goto err_chan_failed;
494 }
495 }
496#ifdef DEBUG
497 ctrl->sched.slots = kzalloc(SLIM_SL_PER_SUPERFRAME, GFP_KERNEL);
498#endif
Sagar Dharia33f34442011-08-08 16:22:03 -0600499 init_completion(&ctrl->pause_comp);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600500
501 INIT_LIST_HEAD(&ctrl->devs);
502 ctrl->wq = create_singlethread_workqueue(dev_name(&ctrl->dev));
503 if (!ctrl->wq)
504 goto err_workq_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505
506 return 0;
507
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600508err_workq_failed:
509 kfree(ctrl->sched.chc3);
510 kfree(ctrl->sched.chc1);
511 kfree(ctrl->chans);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512err_chan_failed:
513 kfree(ctrl->ports);
514err_port_failed:
515 device_unregister(&ctrl->dev);
516out_list:
517 mutex_lock(&slim_lock);
518 idr_remove(&ctrl_idr, ctrl->nr);
519 mutex_unlock(&slim_lock);
520 return ret;
521}
522
523/* slim_remove_device: Remove the effect of slim_add_device() */
524void slim_remove_device(struct slim_device *sbdev)
525{
526 device_unregister(&sbdev->dev);
527}
528EXPORT_SYMBOL_GPL(slim_remove_device);
529
530static void slim_ctrl_remove_device(struct slim_controller *ctrl,
531 struct slim_boardinfo *bi)
532{
533 if (ctrl->nr == bi->bus_num)
534 slim_remove_device(bi->slim_slave);
535}
536
537/*
538 * slim_del_controller: Controller tear-down.
539 * Controller added with the above API is teared down using this API.
540 */
541int slim_del_controller(struct slim_controller *ctrl)
542{
543 struct slim_controller *found;
544 struct sbi_boardinfo *bi;
545
546 /* First make sure that this bus was added */
547 mutex_lock(&slim_lock);
548 found = idr_find(&ctrl_idr, ctrl->nr);
549 mutex_unlock(&slim_lock);
550 if (found != ctrl)
551 return -EINVAL;
552
553 /* Remove all clients */
554 mutex_lock(&board_lock);
555 list_for_each_entry(bi, &board_list, list)
556 slim_ctrl_remove_device(ctrl, &bi->board_info);
557 mutex_unlock(&board_lock);
558
559 init_completion(&ctrl->dev_released);
560 device_unregister(&ctrl->dev);
561
562 wait_for_completion(&ctrl->dev_released);
563 list_del(&ctrl->list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600564 destroy_workqueue(ctrl->wq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 /* free bus id */
566 mutex_lock(&slim_lock);
567 idr_remove(&ctrl_idr, ctrl->nr);
568 mutex_unlock(&slim_lock);
569
570 kfree(ctrl->sched.chc1);
571 kfree(ctrl->sched.chc3);
572#ifdef DEBUG
573 kfree(ctrl->sched.slots);
574#endif
575 kfree(ctrl->chans);
576 kfree(ctrl->ports);
577
578 return 0;
579}
580EXPORT_SYMBOL_GPL(slim_del_controller);
581
582/*
583 * slim_add_numbered_controller: Controller bring-up.
584 * @ctrl: Controller to be registered.
585 * A controller is registered with the framework using this API. ctrl->nr is the
586 * desired number with which slimbus framework registers the controller.
587 * Function will return -EBUSY if the number is in use.
588 */
589int slim_add_numbered_controller(struct slim_controller *ctrl)
590{
591 int id;
592 int status;
593
594 if (ctrl->nr & ~MAX_ID_MASK)
595 return -EINVAL;
596
597retry:
598 if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0)
599 return -ENOMEM;
600
601 mutex_lock(&slim_lock);
602 status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
603 if (status == 0 && id != ctrl->nr) {
604 status = -EAGAIN;
605 idr_remove(&ctrl_idr, id);
606 }
607 mutex_unlock(&slim_lock);
608 if (status == -EAGAIN)
609 goto retry;
610
611 if (status == 0)
612 status = slim_register_controller(ctrl);
613 return status;
614}
615EXPORT_SYMBOL_GPL(slim_add_numbered_controller);
616
617/*
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600618 * slim_report_absent: Controller calls this function when a device
619 * reports absent, OR when the device cannot be communicated with
620 * @sbdev: Device that cannot be reached, or sent report absent
621 */
622void slim_report_absent(struct slim_device *sbdev)
623{
624 struct slim_controller *ctrl;
625 int i;
626 if (!sbdev)
627 return;
628 ctrl = sbdev->ctrl;
629 if (!ctrl)
630 return;
631 /* invalidate logical addresses */
632 mutex_lock(&ctrl->m_ctrl);
633 for (i = 0; i < ctrl->num_dev; i++) {
634 if (sbdev->laddr == ctrl->addrt[i].laddr)
635 ctrl->addrt[i].valid = false;
636 }
637 mutex_unlock(&ctrl->m_ctrl);
Sagar Dharia21f88552014-02-07 00:10:37 -0700638 sbdev->reported = false;
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600639 queue_work(ctrl->wq, &sbdev->wd);
640}
641EXPORT_SYMBOL(slim_report_absent);
642
643/*
Sagar Dharia722f9e22013-12-11 23:59:22 -0700644 * slim_framer_booted: This function is called by controller after the active
645 * framer has booted (using Bus Reset sequence, or after it has shutdown and has
646 * come back up). Components, devices on the bus may be in undefined state,
647 * and this function triggers their drivers to do the needful
648 * to bring them back in Reset state so that they can acquire sync, report
649 * present and be operational again.
650 */
651void slim_framer_booted(struct slim_controller *ctrl)
652{
653 struct slim_device *sbdev;
654 struct list_head *pos, *next;
655 if (!ctrl)
656 return;
657 mutex_lock(&ctrl->m_ctrl);
658 list_for_each_safe(pos, next, &ctrl->devs) {
659 struct slim_driver *sbdrv;
660 sbdev = list_entry(pos, struct slim_device, dev_list);
661 mutex_unlock(&ctrl->m_ctrl);
662 if (sbdev && sbdev->dev.driver) {
663 sbdrv = to_slim_driver(sbdev->dev.driver);
664 if (sbdrv->reset_device)
665 sbdrv->reset_device(sbdev);
666 }
667 mutex_lock(&ctrl->m_ctrl);
668 }
669 mutex_unlock(&ctrl->m_ctrl);
670}
671EXPORT_SYMBOL(slim_framer_booted);
672
673/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 * slim_msg_response: Deliver Message response received from a device to the
675 * framework.
676 * @ctrl: Controller handle
677 * @reply: Reply received from the device
678 * @len: Length of the reply
679 * @tid: Transaction ID received with which framework can associate reply.
680 * Called by controller to inform framework about the response received.
681 * This helps in making the API asynchronous, and controller-driver doesn't need
682 * to manage 1 more table other than the one managed by framework mapping TID
683 * with buffers
684 */
685void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
686{
687 int i;
688 struct slim_msg_txn *txn;
689
690 mutex_lock(&ctrl->m_ctrl);
691 txn = ctrl->txnt[tid];
692 if (txn == NULL) {
693 dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
694 tid, len);
695 mutex_unlock(&ctrl->m_ctrl);
696 return;
697 }
698 for (i = 0; i < len; i++)
699 txn->rbuf[i] = reply[i];
700 if (txn->comp)
701 complete(txn->comp);
702 ctrl->txnt[tid] = NULL;
703 mutex_unlock(&ctrl->m_ctrl);
704 kfree(txn);
705}
706EXPORT_SYMBOL_GPL(slim_msg_response);
707
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600708static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709 u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
710 struct completion *comp, u8 la, u8 *tid)
711{
712 u8 i = 0;
713 int ret = 0;
714 struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
715 GFP_KERNEL);
716 if (!txn)
717 return -ENOMEM;
718 if (tid) {
719 mutex_lock(&ctrl->m_ctrl);
720 for (i = 0; i < ctrl->last_tid; i++) {
721 if (ctrl->txnt[i] == NULL)
722 break;
723 }
724 if (i >= ctrl->last_tid) {
725 if (ctrl->last_tid == 255) {
726 mutex_unlock(&ctrl->m_ctrl);
727 kfree(txn);
728 return -ENOMEM;
729 }
730 ctrl->txnt = krealloc(ctrl->txnt,
731 (i + 1) * sizeof(struct slim_msg_txn *),
732 GFP_KERNEL);
733 if (!ctrl->txnt) {
734 mutex_unlock(&ctrl->m_ctrl);
735 kfree(txn);
736 return -ENOMEM;
737 }
738 ctrl->last_tid++;
739 }
740 ctrl->txnt[i] = txn;
741 mutex_unlock(&ctrl->m_ctrl);
742 txn->tid = i;
743 *tid = i;
744 }
745 txn->mc = mc;
746 txn->mt = mt;
747 txn->dt = dt;
748 txn->ec = ec;
749 txn->la = la;
750 txn->rbuf = rbuf;
751 txn->wbuf = wbuf;
752 txn->rl = mlen;
753 txn->len = len;
754 txn->comp = comp;
755
756 ret = ctrl->xfer_msg(ctrl, txn);
757 if (!tid)
758 kfree(txn);
759 return ret;
760}
761
762static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600763 u8 e_len, u8 *entry)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764{
765 u8 i;
766 for (i = 0; i < ctrl->num_dev; i++) {
767 if (ctrl->addrt[i].valid &&
768 memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600769 *entry = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 return 0;
771 }
772 }
773 return -ENXIO;
774}
775
776/*
777 * slim_assign_laddr: Assign logical address to a device enumerated.
778 * @ctrl: Controller with which device is enumerated.
779 * @e_addr: 6-byte elemental address of the device.
780 * @e_len: buffer length for e_addr
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600781 * @laddr: Return logical address (if valid flag is false)
782 * @valid: true if laddr holds a valid address that controller wants to
783 * set for this enumeration address. Otherwise framework sets index into
784 * address table as logical address.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 * Called by controller in response to REPORT_PRESENT. Framework will assign
786 * a logical address to this enumeration address.
787 * Function returns -EXFULL to indicate that all logical addresses are already
788 * taken.
789 */
790int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600791 u8 e_len, u8 *laddr, bool valid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792{
793 int ret;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600794 u8 i = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600795 bool exists = false;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600796 struct slim_device *sbdev;
Sagar Dharia21f88552014-02-07 00:10:37 -0700797 struct list_head *pos, *next;
798
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 mutex_lock(&ctrl->m_ctrl);
800 /* already assigned */
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600801 if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
802 *laddr = ctrl->addrt[i].laddr;
803 exists = true;
804 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 if (ctrl->num_dev >= 254) {
806 ret = -EXFULL;
807 goto ret_assigned_laddr;
808 }
809 for (i = 0; i < ctrl->num_dev; i++) {
810 if (ctrl->addrt[i].valid == false)
811 break;
812 }
813 if (i == ctrl->num_dev) {
814 ctrl->addrt = krealloc(ctrl->addrt,
815 (ctrl->num_dev + 1) *
816 sizeof(struct slim_addrt),
817 GFP_KERNEL);
818 if (!ctrl->addrt) {
819 ret = -ENOMEM;
820 goto ret_assigned_laddr;
821 }
822 ctrl->num_dev++;
823 }
824 memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
825 ctrl->addrt[i].valid = true;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600826 /* Preferred address is index into table */
827 if (!valid)
828 *laddr = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 }
830
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600831 ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
832 *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 if (ret) {
834 ctrl->addrt[i].valid = false;
835 goto ret_assigned_laddr;
836 }
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600837 ctrl->addrt[i].laddr = *laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600839 dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840ret_assigned_laddr:
841 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600842 if (exists || ret)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600843 return ret;
844
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600845 pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600846 e_addr[1], e_addr[2]);
847 mutex_lock(&ctrl->m_ctrl);
Sagar Dharia21f88552014-02-07 00:10:37 -0700848 list_for_each_safe(pos, next, &ctrl->devs) {
849 sbdev = list_entry(pos, struct slim_device, dev_list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600850 if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
851 struct slim_driver *sbdrv;
Sagar Dharia33c84e62012-10-30 21:12:09 -0600852 sbdev->laddr = *laddr;
Sagar Dharia21f88552014-02-07 00:10:37 -0700853 sbdev->reported = true;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600854 if (sbdev->dev.driver) {
855 sbdrv = to_slim_driver(sbdev->dev.driver);
856 if (sbdrv->device_up)
857 queue_work(ctrl->wq, &sbdev->wd);
858 }
859 break;
860 }
861 }
862 mutex_unlock(&ctrl->m_ctrl);
863 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864}
865EXPORT_SYMBOL_GPL(slim_assign_laddr);
866
867/*
868 * slim_get_logical_addr: Return the logical address of a slimbus device.
869 * @sb: client handle requesting the adddress.
870 * @e_addr: Elemental address of the device.
871 * @e_len: Length of e_addr
872 * @laddr: output buffer to store the address
873 * context: can sleep
874 * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
875 * the device with this elemental address is not found.
876 */
877int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
878 u8 e_len, u8 *laddr)
879{
880 int ret = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600881 u8 entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 struct slim_controller *ctrl = sb->ctrl;
883 if (!ctrl || !laddr || !e_addr || e_len != 6)
884 return -EINVAL;
885 mutex_lock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600886 ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
887 if (!ret)
888 *laddr = ctrl->addrt[entry].laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600890 if (ret == -ENXIO && ctrl->get_laddr) {
891 ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
892 if (!ret)
893 ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
894 true);
895 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 return ret;
897}
898EXPORT_SYMBOL_GPL(slim_get_logical_addr);
899
900static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
901 u8 *rbuf, const u8 *wbuf, u8 len)
902{
903 if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
904 return -EINVAL;
905 switch (oper) {
906 case SLIM_MSG_MC_REQUEST_VALUE:
907 case SLIM_MSG_MC_REQUEST_INFORMATION:
908 if (rbuf == NULL)
909 return -EINVAL;
910 return 0;
911 case SLIM_MSG_MC_CHANGE_VALUE:
912 case SLIM_MSG_MC_CLEAR_INFORMATION:
913 if (wbuf == NULL)
914 return -EINVAL;
915 return 0;
916 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
917 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
918 if (rbuf == NULL || wbuf == NULL)
919 return -EINVAL;
920 return 0;
921 default:
922 return -EINVAL;
923 }
924}
925
926static u16 slim_slicecodefromsize(u32 req)
927{
928 u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
929 if (req >= 8)
930 return 0;
931 else
932 return codetosize[req];
933}
934
935static u16 slim_slicesize(u32 code)
936{
937 u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
938 if (code == 0)
939 code = 1;
940 if (code > 16)
941 code = 16;
942 return sizetocode[code - 1];
943}
944
945
946/* Message APIs Unicast message APIs used by slimbus slave drivers */
947
948/*
949 * Message API access routines.
950 * @sb: client handle requesting elemental message reads, writes.
951 * @msg: Input structure for start-offset, number of bytes to read.
952 * @rbuf: data buffer to be filled with values read.
953 * @len: data buffer size
954 * @wbuf: data buffer containing value/information to be written
955 * context: can sleep
956 * Returns:
957 * -EINVAL: Invalid parameters
958 * -ETIMEDOUT: If controller could not complete the request. This may happen if
959 * the bus lines are not clocked, controller is not powered-on, slave with
960 * given address is not enumerated/responding.
961 */
962int slim_request_val_element(struct slim_device *sb,
963 struct slim_ele_access *msg, u8 *buf, u8 len)
964{
965 struct slim_controller *ctrl = sb->ctrl;
966 if (!ctrl)
967 return -EINVAL;
968 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_VALUE, buf,
969 NULL, len);
970}
971EXPORT_SYMBOL_GPL(slim_request_val_element);
972
973int slim_request_inf_element(struct slim_device *sb,
974 struct slim_ele_access *msg, u8 *buf, u8 len)
975{
976 struct slim_controller *ctrl = sb->ctrl;
977 if (!ctrl)
978 return -EINVAL;
979 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_INFORMATION,
980 buf, NULL, len);
981}
982EXPORT_SYMBOL_GPL(slim_request_inf_element);
983
984int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
985 const u8 *buf, u8 len)
986{
987 struct slim_controller *ctrl = sb->ctrl;
988 if (!ctrl)
989 return -EINVAL;
990 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CHANGE_VALUE, NULL, buf,
991 len);
992}
993EXPORT_SYMBOL_GPL(slim_change_val_element);
994
995int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
996 u8 *buf, u8 len)
997{
998 struct slim_controller *ctrl = sb->ctrl;
999 if (!ctrl)
1000 return -EINVAL;
1001 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
1002 buf, len);
1003}
1004EXPORT_SYMBOL_GPL(slim_clear_inf_element);
1005
1006int slim_request_change_val_element(struct slim_device *sb,
1007 struct slim_ele_access *msg, u8 *rbuf,
1008 const u8 *wbuf, u8 len)
1009{
1010 struct slim_controller *ctrl = sb->ctrl;
1011 if (!ctrl)
1012 return -EINVAL;
1013 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
1014 rbuf, wbuf, len);
1015}
1016EXPORT_SYMBOL_GPL(slim_request_change_val_element);
1017
1018int slim_request_clear_inf_element(struct slim_device *sb,
1019 struct slim_ele_access *msg, u8 *rbuf,
1020 const u8 *wbuf, u8 len)
1021{
1022 struct slim_controller *ctrl = sb->ctrl;
1023 if (!ctrl)
1024 return -EINVAL;
1025 return slim_xfer_msg(ctrl, sb, msg,
1026 SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
1027 rbuf, wbuf, len);
1028}
1029EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
1030
1031/*
1032 * Broadcast message API:
1033 * call this API directly with sbdev = NULL.
1034 * For broadcast reads, make sure that buffers are big-enough to incorporate
1035 * replies from all logical addresses.
1036 * All controllers may not support broadcast
1037 */
1038int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001039 struct slim_ele_access *msg, u16 mc, u8 *rbuf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 const u8 *wbuf, u8 len)
1041{
1042 DECLARE_COMPLETION_ONSTACK(complete);
1043 int ret;
1044 u16 sl, cur;
1045 u16 ec;
1046 u8 tid, mlen = 6;
1047
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
1049 if (ret)
1050 goto xfer_err;
1051
1052 sl = slim_slicesize(len);
1053 dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
1054 msg->start_offset, len, mc, sl);
1055
1056 cur = slim_slicecodefromsize(sl);
1057 ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
1058
1059 if (wbuf)
1060 mlen += len;
1061 if (rbuf) {
1062 mlen++;
1063 if (!msg->comp)
1064 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1065 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1066 &complete, sbdev->laddr, &tid);
1067 else
1068 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1069 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1070 msg->comp, sbdev->laddr, &tid);
1071 /* sync read */
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001072 if (!ret && !msg->comp) {
1073 ret = wait_for_completion_timeout(&complete, HZ);
1074 if (!ret) {
1075 struct slim_msg_txn *txn;
1076 dev_err(&ctrl->dev, "slimbus Read timed out");
1077 mutex_lock(&ctrl->m_ctrl);
1078 txn = ctrl->txnt[tid];
1079 /* Invalidate the transaction */
1080 ctrl->txnt[tid] = NULL;
1081 mutex_unlock(&ctrl->m_ctrl);
1082 kfree(txn);
1083 ret = -ETIMEDOUT;
1084 } else
1085 ret = 0;
Sagar Dharia53a9f792012-09-04 19:56:18 -06001086 } else if (ret < 0 && !msg->comp) {
1087 struct slim_msg_txn *txn;
1088 dev_err(&ctrl->dev, "slimbus Read error");
1089 mutex_lock(&ctrl->m_ctrl);
1090 txn = ctrl->txnt[tid];
1091 /* Invalidate the transaction */
1092 ctrl->txnt[tid] = NULL;
1093 mutex_unlock(&ctrl->m_ctrl);
1094 kfree(txn);
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001095 }
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 } else
1098 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
1099 SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1100 NULL, sbdev->laddr, NULL);
1101xfer_err:
1102 return ret;
1103}
1104EXPORT_SYMBOL_GPL(slim_xfer_msg);
1105
1106/*
1107 * slim_alloc_mgrports: Allocate port on manager side.
1108 * @sb: device/client handle.
1109 * @req: Port request type.
1110 * @nports: Number of ports requested
1111 * @rh: output buffer to store the port handles
1112 * @hsz: size of buffer storing handles
1113 * context: can sleep
1114 * This port will be typically used by SW. e.g. client driver wants to receive
1115 * some data from audio codec HW using a data channel.
1116 * Port allocated using this API will be used to receive the data.
1117 * If half-duplex ports are requested, two adjacent ports are allocated for
1118 * 1 half-duplex port. So the handle-buffer size should be twice the number
1119 * of half-duplex ports to be allocated.
1120 * -EDQUOT is returned if all ports are in use.
1121 */
1122int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1123 int nports, u32 *rh, int hsz)
1124{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001125 int i, j;
1126 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 int nphysp = nports;
1128 struct slim_controller *ctrl = sb->ctrl;
1129
1130 if (!rh || !ctrl)
1131 return -EINVAL;
1132 if (req == SLIM_REQ_HALF_DUP)
1133 nphysp *= 2;
1134 if (hsz/sizeof(u32) < nphysp)
1135 return -EINVAL;
1136 mutex_lock(&ctrl->m_ctrl);
1137
1138 for (i = 0; i < ctrl->nports; i++) {
1139 bool multiok = true;
1140 if (ctrl->ports[i].state != SLIM_P_FREE)
1141 continue;
1142 /* Start half duplex channel at even port */
1143 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1144 continue;
1145 /* Allocate ports contiguously for multi-ch */
1146 if (ctrl->nports < (i + nphysp)) {
1147 i = ctrl->nports;
1148 break;
1149 }
1150 if (req == SLIM_REQ_MULTI_CH) {
1151 multiok = true;
1152 for (j = i; j < i + nphysp; j++) {
1153 if (ctrl->ports[j].state != SLIM_P_FREE) {
1154 multiok = false;
1155 break;
1156 }
1157 }
1158 if (!multiok)
1159 continue;
1160 }
1161 break;
1162 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001163 if (i >= ctrl->nports) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 ret = -EDQUOT;
Sagar Dharia100e7212013-05-17 18:20:57 -06001165 goto alloc_err;
1166 }
1167 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 for (j = i; j < i + nphysp; j++) {
1169 ctrl->ports[j].state = SLIM_P_UNCFG;
1170 ctrl->ports[j].req = req;
1171 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1172 ctrl->ports[j].flow = SLIM_SINK;
1173 else
1174 ctrl->ports[j].flow = SLIM_SRC;
Sagar Dharia100e7212013-05-17 18:20:57 -06001175 if (ctrl->alloc_port)
1176 ret = ctrl->alloc_port(ctrl, j);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 if (ret) {
1178 for (; j >= i; j--)
1179 ctrl->ports[j].state = SLIM_P_FREE;
1180 goto alloc_err;
1181 }
1182 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1183 }
1184alloc_err:
1185 mutex_unlock(&ctrl->m_ctrl);
1186 return ret;
1187}
1188EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1189
1190/* Deallocate the port(s) allocated using the API above */
1191int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1192{
1193 int i;
1194 struct slim_controller *ctrl = sb->ctrl;
1195
1196 if (!ctrl || !hdl)
1197 return -EINVAL;
1198
1199 mutex_lock(&ctrl->m_ctrl);
1200
1201 for (i = 0; i < nports; i++) {
1202 u8 pn;
1203 pn = SLIM_HDL_TO_PORT(hdl[i]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001204
1205 if (pn >= ctrl->nports || ctrl->ports[pn].state == SLIM_P_CFG) {
1206 int j, ret;
1207 if (pn >= ctrl->nports) {
1208 dev_err(&ctrl->dev, "invalid port number");
1209 ret = -EINVAL;
1210 } else {
1211 dev_err(&ctrl->dev,
1212 "Can't dealloc connected port:%d", i);
1213 ret = -EISCONN;
1214 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 for (j = i - 1; j >= 0; j--) {
1216 pn = SLIM_HDL_TO_PORT(hdl[j]);
1217 ctrl->ports[pn].state = SLIM_P_UNCFG;
1218 }
1219 mutex_unlock(&ctrl->m_ctrl);
Sagar Dharia100e7212013-05-17 18:20:57 -06001220 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001222 if (ctrl->dealloc_port)
1223 ctrl->dealloc_port(ctrl, pn);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 ctrl->ports[pn].state = SLIM_P_FREE;
1225 }
1226 mutex_unlock(&ctrl->m_ctrl);
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1230
1231/*
1232 * slim_get_slaveport: Get slave port handle
1233 * @la: slave device logical address.
1234 * @idx: port index at slave
1235 * @rh: return handle
1236 * @flw: Flow type (source or destination)
1237 * This API only returns a slave port's representation as expected by slimbus
1238 * driver. This port is not managed by the slimbus driver. Caller is expected
1239 * to have visibility of this port since it's a device-port.
1240 */
1241int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1242{
1243 if (rh == NULL)
1244 return -EINVAL;
1245 *rh = SLIM_PORT_HDL(la, flw, idx);
1246 return 0;
1247}
1248EXPORT_SYMBOL_GPL(slim_get_slaveport);
1249
1250static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1251 enum slim_port_flow flow)
1252{
1253 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001254 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 u8 buf[2];
1256 u32 la = SLIM_HDL_TO_LA(ph);
1257 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1258
1259 if (flow == SLIM_SRC)
1260 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1261 else
1262 mc = SLIM_MSG_MC_CONNECT_SINK;
1263 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001264 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 if (la == SLIM_LA_MANAGER)
1266 ctrl->ports[pn].flow = flow;
1267 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1268 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1269 NULL);
1270 if (!ret && la == SLIM_LA_MANAGER)
1271 ctrl->ports[pn].state = SLIM_P_CFG;
1272 return ret;
1273}
1274
1275static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1276{
1277 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001278 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001279 u32 la = SLIM_HDL_TO_LA(ph);
1280 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1281
1282 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1283 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1284 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1285 NULL, la, NULL);
1286 if (ret)
1287 return ret;
1288 if (la == SLIM_LA_MANAGER)
1289 ctrl->ports[pn].state = SLIM_P_UNCFG;
1290 return 0;
1291}
1292
1293/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001294 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001296 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001298 * Per slimbus specification, a channel may have 1 source port.
1299 * Channel specified in chanh needs to be allocated first.
1300 * Returns -EALREADY if source is already configured for this channel.
1301 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001302 * Returns -EINVAL if invalid direction is specified for non-manager port,
1303 * or if the manager side port number is out of bounds, or in incorrect state
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001305int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1306{
1307 struct slim_controller *ctrl = sb->ctrl;
1308 int ret;
1309 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1310 struct slim_ich *slc = &ctrl->chans[chan];
1311 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
Sagar Dharia100e7212013-05-17 18:20:57 -06001312 u8 la = SLIM_HDL_TO_LA(srch);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001313
Sagar Dharia100e7212013-05-17 18:20:57 -06001314 /* manager ports don't have direction when they are allocated */
1315 if (la != SLIM_LA_MANAGER && flow != SLIM_SRC)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001316 return -EINVAL;
1317
Sagar Dhariad2959352012-12-01 15:43:01 -07001318 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001319
Sagar Dharia100e7212013-05-17 18:20:57 -06001320 if (la == SLIM_LA_MANAGER) {
1321 u8 pn = SLIM_HDL_TO_PORT(srch);
1322 if (pn >= ctrl->nports ||
1323 ctrl->ports[pn].state != SLIM_P_UNCFG) {
1324 ret = -EINVAL;
1325 goto connect_src_err;
1326 }
1327 }
1328
Sagar Dharia29f35f02011-10-01 20:37:50 -06001329 if (slc->state == SLIM_CH_FREE) {
1330 ret = -ENOTCONN;
1331 goto connect_src_err;
1332 }
1333 /*
1334 * Once channel is removed, its ports can be considered disconnected
1335 * So its ports can be reassigned. Source port is zeroed
1336 * when channel is deallocated.
1337 */
1338 if (slc->srch) {
1339 ret = -EALREADY;
1340 goto connect_src_err;
1341 }
1342
1343 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1344
1345 if (!ret)
1346 slc->srch = srch;
1347
1348connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001349 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001350 return ret;
1351}
1352EXPORT_SYMBOL_GPL(slim_connect_src);
1353
1354/*
1355 * slim_connect_sink: Connect sink port(s) to channel.
1356 * @sb: client handle
1357 * @sinkh: sink handle(s) to be connected to this channel
1358 * @nsink: number of sinks
1359 * @chanh: Channel with which the ports need to be associated with.
1360 * Per slimbus specification, a channel may have multiple sink-ports.
1361 * Channel specified in chanh needs to be allocated first.
1362 * Returns -EALREADY if sink is already configured for this channel.
1363 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001364 * Returns -EINVAL if invalid parameters are passed, or invalid direction is
1365 * specified for non-manager port, or if the manager side port number is out of
1366 * bounds, or in incorrect state
Sagar Dharia29f35f02011-10-01 20:37:50 -06001367 */
1368int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001369{
1370 struct slim_controller *ctrl = sb->ctrl;
1371 int j;
1372 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001373 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 struct slim_ich *slc = &ctrl->chans[chan];
1375
Sagar Dharia29f35f02011-10-01 20:37:50 -06001376 if (!sinkh || !nsink)
1377 return -EINVAL;
1378
Sagar Dhariad2959352012-12-01 15:43:01 -07001379 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380
1381 /*
1382 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001383 * So its ports can be reassigned. Sink ports are freed when channel
1384 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001386 if (slc->state == SLIM_CH_FREE) {
1387 ret = -ENOTCONN;
1388 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001390
Sagar Dharia29f35f02011-10-01 20:37:50 -06001391 for (j = 0; j < nsink; j++) {
1392 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001393 u8 la = SLIM_HDL_TO_LA(sinkh[j]);
1394 u8 pn = SLIM_HDL_TO_PORT(sinkh[j]);
1395 if (la != SLIM_LA_MANAGER && flow != SLIM_SINK)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001396 ret = -EINVAL;
Sagar Dharia100e7212013-05-17 18:20:57 -06001397 else if (la == SLIM_LA_MANAGER &&
1398 (pn >= ctrl->nports ||
1399 ctrl->ports[pn].state != SLIM_P_UNCFG))
1400 ret = -EINVAL;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001401 else
1402 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001404 for (j = j - 1; j >= 0; j--)
1405 disconnect_port_ch(ctrl, sinkh[j]);
1406 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 }
1408 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001409
1410 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1411 GFP_KERNEL);
1412 if (!slc->sinkh) {
1413 ret = -ENOMEM;
1414 for (j = 0; j < nsink; j++)
1415 disconnect_port_ch(ctrl, sinkh[j]);
1416 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 }
1418
Sagar Dharia29f35f02011-10-01 20:37:50 -06001419 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1420 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421
Sagar Dharia29f35f02011-10-01 20:37:50 -06001422connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001423 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424 return ret;
1425}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001426EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427
1428/*
1429 * slim_disconnect_ports: Disconnect port(s) from channel
1430 * @sb: client handle
1431 * @ph: ports to be disconnected
1432 * @nph: number of ports.
1433 * Disconnects ports from a channel.
1434 */
1435int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1436{
1437 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001438 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001439
Sagar Dhariad2959352012-12-01 15:43:01 -07001440 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001441
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442 for (i = 0; i < nph; i++)
1443 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001444 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 return 0;
1446}
1447EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1448
1449/*
1450 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1451 * @sb: client handle
1452 * @ph: port-handle
1453 * @iobuf: buffer to be transferred or populated
1454 * @len: buffer size.
1455 * @comp: completion signal to indicate transfer done or error.
1456 * context: can sleep
1457 * Returns number of bytes transferred/received if used synchronously.
1458 * Will return 0 if used asynchronously.
1459 * Client will call slim_port_get_xfer_status to get error and/or number of
1460 * bytes transferred if used asynchronously.
1461 */
1462int slim_port_xfer(struct slim_device *sb, u32 ph, u8 *iobuf, u32 len,
1463 struct completion *comp)
1464{
1465 struct slim_controller *ctrl = sb->ctrl;
1466 u8 pn = SLIM_HDL_TO_PORT(ph);
1467 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1468 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1469}
1470EXPORT_SYMBOL_GPL(slim_port_xfer);
1471
1472/*
1473 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1474 * after completion is done.
1475 * @sb: client handle
1476 * @ph: port-handle
1477 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1478 * @done_len: Number of bytes transferred.
1479 * This can be called when port_xfer complition is signalled.
1480 * The API will return port transfer error (underflow/overflow/disconnect)
1481 * and/or done_len will reflect number of bytes transferred. Note that
1482 * done_len may be valid even if port error (overflow/underflow) has happened.
1483 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1484 * client has not supplied more data to be transferred, done_len will indicate
1485 * number of bytes transferred with underflow error. To avoid frequent underflow
1486 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1487 * channel has data to be transferred even if client is not ready to transfer
1488 * data all the time. done_buf will indicate address of the last buffer
1489 * processed from the multiple transfers.
1490 */
1491enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
1492 u8 **done_buf, u32 *done_len)
1493{
1494 struct slim_controller *ctrl = sb->ctrl;
1495 u8 pn = SLIM_HDL_TO_PORT(ph);
1496 u32 la = SLIM_HDL_TO_LA(ph);
1497 enum slim_port_err err;
1498 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1499 /*
1500 * Framework only has insight into ports managed by ported device
1501 * used by the manager and not slave
1502 */
1503 if (la != SLIM_LA_MANAGER) {
1504 if (done_buf)
1505 *done_buf = NULL;
1506 if (done_len)
1507 *done_len = 0;
1508 return SLIM_P_NOT_OWNED;
1509 }
1510 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1511 if (err == SLIM_P_INPROGRESS)
1512 err = ctrl->ports[pn].err;
1513 return err;
1514}
1515EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1516
1517static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1518{
1519 struct slim_ich **arr;
1520 int i, j;
1521 int *len;
1522 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001523 /* Channel is already active and other end is transmitting data */
1524 if (slc->state >= SLIM_CH_ACTIVE)
1525 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001526 if (slc->coeff == SLIM_COEFF_1) {
1527 arr = ctrl->sched.chc1;
1528 len = &ctrl->sched.num_cc1;
1529 } else {
1530 arr = ctrl->sched.chc3;
1531 len = &ctrl->sched.num_cc3;
1532 sl *= 3;
1533 }
1534
1535 *len += 1;
1536
1537 /* Insert the channel based on rootexp and seglen */
1538 for (i = 0; i < *len - 1; i++) {
1539 /*
1540 * Primary key: exp low to high.
1541 * Secondary key: seglen: high to low
1542 */
1543 if ((slc->rootexp > arr[i]->rootexp) ||
1544 ((slc->rootexp == arr[i]->rootexp) &&
1545 (slc->seglen < arr[i]->seglen)))
1546 continue;
1547 else
1548 break;
1549 }
1550 for (j = *len - 1; j > i; j--)
1551 arr[j] = arr[j - 1];
1552 arr[i] = slc;
Sagar Dhariad1468b72013-07-16 12:56:22 -06001553 if (!ctrl->allocbw)
1554 ctrl->sched.usedslots += sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555
1556 return;
1557}
1558
1559static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1560{
1561 struct slim_ich **arr;
1562 int i;
1563 u32 la, ph;
1564 int *len;
1565 if (slc->coeff == SLIM_COEFF_1) {
1566 arr = ctrl->sched.chc1;
1567 len = &ctrl->sched.num_cc1;
1568 } else {
1569 arr = ctrl->sched.chc3;
1570 len = &ctrl->sched.num_cc3;
1571 }
1572
1573 for (i = 0; i < *len; i++) {
1574 if (arr[i] == slc)
1575 break;
1576 }
1577 if (i >= *len)
1578 return -EXFULL;
1579 for (; i < *len - 1; i++)
1580 arr[i] = arr[i + 1];
1581 *len -= 1;
1582 arr[*len] = NULL;
1583
1584 slc->state = SLIM_CH_ALLOCATED;
1585 slc->newintr = 0;
1586 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001587 for (i = 0; i < slc->nsink; i++) {
1588 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589 la = SLIM_HDL_TO_LA(ph);
1590 /*
1591 * For ports managed by manager's ported device, no need to send
1592 * disconnect. It is client's responsibility to call disconnect
1593 * on ports owned by the slave device
1594 */
1595 if (la == SLIM_LA_MANAGER)
1596 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1597 }
1598
Sagar Dharia29f35f02011-10-01 20:37:50 -06001599 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001600 la = SLIM_HDL_TO_LA(ph);
1601 if (la == SLIM_LA_MANAGER)
1602 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1603
Sagar Dharia29f35f02011-10-01 20:37:50 -06001604 kfree(slc->sinkh);
1605 slc->sinkh = NULL;
1606 slc->srch = 0;
1607 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001608 return 0;
1609}
1610
1611static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1612{
1613 u32 rate = 0, rate4k = 0, rate11k = 0;
1614 u32 exp = 0;
1615 u32 pr = 0;
1616 bool exact = true;
1617 bool done = false;
1618 enum slim_ch_rate ratefam;
1619
1620 if (prop->prot >= SLIM_PUSH)
1621 return 0;
1622 if (prop->baser == SLIM_RATE_1HZ) {
1623 rate = prop->ratem / 4000;
1624 rate4k = rate;
1625 if (rate * 4000 == prop->ratem)
1626 ratefam = SLIM_RATE_4000HZ;
1627 else {
1628 rate = prop->ratem / 11025;
1629 rate11k = rate;
1630 if (rate * 11025 == prop->ratem)
1631 ratefam = SLIM_RATE_11025HZ;
1632 else
1633 ratefam = SLIM_RATE_1HZ;
1634 }
1635 } else {
1636 ratefam = prop->baser;
1637 rate = prop->ratem;
1638 }
1639 if (ratefam == SLIM_RATE_1HZ) {
1640 exact = false;
1641 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1642 rate = rate4k + 1;
1643 ratefam = SLIM_RATE_4000HZ;
1644 } else {
1645 rate = rate11k + 1;
1646 ratefam = SLIM_RATE_11025HZ;
1647 }
1648 }
1649 /* covert rate to coeff-exp */
1650 while (!done) {
1651 while ((rate & 0x1) != 0x1) {
1652 rate >>= 1;
1653 exp++;
1654 }
1655 if (rate > 3) {
1656 /* roundup if not exact */
1657 rate++;
1658 exact = false;
1659 } else
1660 done = true;
1661 }
1662 if (ratefam == SLIM_RATE_4000HZ) {
1663 if (rate == 1)
1664 pr = 0x10;
1665 else {
1666 pr = 0;
1667 exp++;
1668 }
1669 } else {
1670 pr = 8;
1671 exp++;
1672 }
1673 if (exp <= 7) {
1674 pr |= exp;
1675 if (exact)
1676 pr |= 0x80;
1677 } else
1678 pr = 0;
1679 return pr;
1680}
1681
1682static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1683{
1684 struct slim_controller *ctrl = sb->ctrl;
1685 u32 chrate = 0;
1686 u32 exp = 0;
1687 u32 coeff = 0;
1688 bool exact = true;
1689 bool done = false;
1690 int ret = 0;
1691 struct slim_ich *slc = &ctrl->chans[chan];
1692 struct slim_ch *prop = &slc->prop;
1693
1694 slc->prrate = slim_calc_prrate(ctrl, prop);
1695 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1696 if (prop->baser == SLIM_RATE_4000HZ)
1697 chrate = 4000 * prop->ratem;
1698 else if (prop->baser == SLIM_RATE_11025HZ)
1699 chrate = 11025 * prop->ratem;
1700 else
1701 chrate = prop->ratem;
1702 /* max allowed sample freq = 768 seg/frame */
1703 if (chrate > 3600000)
1704 return -EDQUOT;
1705 if (prop->baser == SLIM_RATE_4000HZ &&
1706 ctrl->a_framer->superfreq == 4000)
1707 coeff = prop->ratem;
1708 else if (prop->baser == SLIM_RATE_11025HZ &&
1709 ctrl->a_framer->superfreq == 3675)
1710 coeff = 3 * prop->ratem;
1711 else {
1712 u32 tempr = 0;
1713 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1714 coeff = tempr / ctrl->a_framer->rootfreq;
1715 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1716 coeff++;
1717 exact = false;
1718 }
1719 }
1720
1721 /* convert coeff to coeff-exponent */
1722 exp = 0;
1723 while (!done) {
1724 while ((coeff & 0x1) != 0x1) {
1725 coeff >>= 1;
1726 exp++;
1727 }
1728 if (coeff > 3) {
1729 coeff++;
1730 exact = false;
1731 } else
1732 done = true;
1733 }
1734 if (prop->prot == SLIM_HARD_ISO && !exact)
1735 return -EPROTONOSUPPORT;
1736 else if (prop->prot == SLIM_AUTO_ISO) {
1737 if (exact)
1738 prop->prot = SLIM_HARD_ISO;
1739 else {
1740 /* Push-Pull not supported for now */
1741 return -EPROTONOSUPPORT;
1742 }
1743 }
1744 slc->rootexp = exp;
1745 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1746 if (prop->prot != SLIM_HARD_ISO)
1747 slc->seglen++;
1748 if (prop->prot >= SLIM_EXT_SMPLX)
1749 slc->seglen++;
1750 /* convert coeff to enum */
1751 if (coeff == 1) {
1752 if (exp > 9)
1753 ret = -EIO;
1754 coeff = SLIM_COEFF_1;
1755 } else {
1756 if (exp > 8)
1757 ret = -EIO;
1758 coeff = SLIM_COEFF_3;
1759 }
1760 slc->coeff = coeff;
1761
1762 return ret;
1763}
1764
1765/*
1766 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1767 * @sb: client handle.
1768 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001769 * Slimbus channels are limited to 256 per specification.
1770 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001771 * Although slimbus specification supports 256 channels, a controller may not
1772 * support that many channels.
1773 */
1774int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1775{
1776 struct slim_controller *ctrl = sb->ctrl;
1777 u16 i;
1778
1779 if (!ctrl)
1780 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001781 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 for (i = 0; i < ctrl->nchans; i++) {
1783 if (ctrl->chans[i].state == SLIM_CH_FREE)
1784 break;
1785 }
1786 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001787 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788 return -EXFULL;
1789 }
1790 *chanh = i;
1791 ctrl->chans[i].nextgrp = 0;
1792 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001793 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794
Sagar Dhariad2959352012-12-01 15:43:01 -07001795 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 return 0;
1797}
1798EXPORT_SYMBOL_GPL(slim_alloc_ch);
1799
1800/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001801 * slim_query_ch: Get reference-counted handle for a channel number. Every
1802 * channel is reference counted by upto one as producer and the others as
1803 * consumer)
1804 * @sb: client handle
1805 * @chan: slimbus channel number
1806 * @chanh: return channel handle
1807 * If request channel number is not in use, it is allocated, and reference
1808 * count is set to one. If the channel was was already allocated, this API
1809 * will return handle to that channel and reference count is incremented.
1810 * -EXFULL is returned if all channels are in use
1811 */
1812int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1813{
1814 struct slim_controller *ctrl = sb->ctrl;
1815 u16 i, j;
1816 int ret = 0;
1817 if (!ctrl || !chanh)
1818 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001819 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001820 /* start with modulo number */
1821 i = ch % ctrl->nchans;
1822
1823 for (j = 0; j < ctrl->nchans; j++) {
1824 if (ctrl->chans[i].chan == ch) {
1825 *chanh = i;
1826 ctrl->chans[i].ref++;
1827 if (ctrl->chans[i].state == SLIM_CH_FREE)
1828 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1829 goto query_out;
1830 }
1831 i = (i + 1) % ctrl->nchans;
1832 }
1833
1834 /* Channel not in table yet */
1835 ret = -EXFULL;
1836 for (j = 0; j < ctrl->nchans; j++) {
1837 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1838 ctrl->chans[i].state =
1839 SLIM_CH_ALLOCATED;
1840 *chanh = i;
1841 ctrl->chans[i].ref++;
1842 ctrl->chans[i].chan = ch;
1843 ctrl->chans[i].nextgrp = 0;
1844 ret = 0;
1845 break;
1846 }
1847 i = (i + 1) % ctrl->nchans;
1848 }
1849query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001850 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001851 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1852 ch, i, ctrl->chans[i].ref, ret);
1853 return ret;
1854}
1855EXPORT_SYMBOL_GPL(slim_query_ch);
1856
1857/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 * slim_dealloc_ch: Deallocate channel allocated using the API above
1859 * -EISCONN is returned if the channel is tried to be deallocated without
1860 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001861 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1862 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 */
1864int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1865{
1866 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001867 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 struct slim_ich *slc = &ctrl->chans[chan];
1869 if (!ctrl)
1870 return -EINVAL;
1871
Sagar Dhariad2959352012-12-01 15:43:01 -07001872 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001873 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001874 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001875 return -ENOTCONN;
1876 }
1877 if (slc->ref > 1) {
1878 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001879 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001880 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1881 slc->chan, chanh, slc->ref);
1882 return 0;
1883 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001884 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1885 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001886 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 return -EISCONN;
1888 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001889 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001891 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001892 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1893 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 return 0;
1895}
1896EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1897
1898/*
1899 * slim_get_ch_state: Channel state.
1900 * This API returns the channel's state (active, suspended, inactive etc)
1901 */
1902enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1903{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001904 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 struct slim_ich *slc = &sb->ctrl->chans[chan];
1906 return slc->state;
1907}
1908EXPORT_SYMBOL_GPL(slim_get_ch_state);
1909
1910/*
1911 * slim_define_ch: Define a channel.This API defines channel parameters for a
1912 * given channel.
1913 * @sb: client handle.
1914 * @prop: slim_ch structure with channel parameters desired to be used.
1915 * @chanh: list of channels to be defined.
1916 * @nchan: number of channels in a group (1 if grp is false)
1917 * @grp: Are the channels grouped
1918 * @grph: return group handle if grouping of channels is desired.
1919 * Channels can be grouped if multiple channels use same parameters
1920 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1921 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001922 * -EISCONN is returned if channel is already used with different parameters.
1923 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 */
1925int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1926 u8 nchan, bool grp, u16 *grph)
1927{
1928 struct slim_controller *ctrl = sb->ctrl;
1929 int i, ret = 0;
1930
1931 if (!ctrl || !chanh || !prop || !nchan)
1932 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001933 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001935 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001936 struct slim_ich *slc = &ctrl->chans[chan];
1937 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001938 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001939 if (slc->state < SLIM_CH_ALLOCATED) {
1940 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941 goto err_define_ch;
1942 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001943 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1944 if (prop->ratem != slc->prop.ratem ||
1945 prop->sampleszbits != slc->prop.sampleszbits ||
1946 prop->baser != slc->prop.baser) {
1947 ret = -EISCONN;
1948 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001950 } else if (slc->state > SLIM_CH_DEFINED) {
1951 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001953 } else {
1954 ctrl->chans[chan].prop = *prop;
1955 ret = slim_nextdefine_ch(sb, chan);
1956 if (ret)
1957 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 }
1959 if (i < (nchan - 1))
1960 ctrl->chans[chan].nextgrp = chanh[i + 1];
1961 if (i == 0)
1962 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1963 if (i == (nchan - 1))
1964 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965 }
1966
1967 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001968 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001969 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001970 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001971 struct slim_ich *slc = &ctrl->chans[chan];
1972 if (slc->state == SLIM_CH_ALLOCATED)
1973 slc->state = SLIM_CH_DEFINED;
1974 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001975err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001976 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07001977 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978 return ret;
1979}
1980EXPORT_SYMBOL_GPL(slim_define_ch);
1981
1982static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
1983{
1984 u32 code = 0;
1985 if (*ctrlw == *subfrml) {
1986 *ctrlw = 8;
1987 *subfrml = 8;
1988 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
1989 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
1990 return 0;
1991 }
1992 if (*subfrml == 6) {
1993 code = 0;
1994 *msgsl = 256;
1995 } else if (*subfrml == 8) {
1996 code = 1;
1997 *msgsl = 192;
1998 } else if (*subfrml == 24) {
1999 code = 2;
2000 *msgsl = 64;
2001 } else { /* 32 */
2002 code = 3;
2003 *msgsl = 48;
2004 }
2005
2006 if (*ctrlw < 8) {
2007 if (*ctrlw >= 6) {
2008 *ctrlw = 6;
2009 code |= 0x14;
2010 } else {
2011 if (*ctrlw == 5)
2012 *ctrlw = 4;
2013 code |= (*ctrlw << 2);
2014 }
2015 } else {
2016 code -= 2;
2017 if (*ctrlw >= 24) {
2018 *ctrlw = 24;
2019 code |= 0x1e;
2020 } else if (*ctrlw >= 16) {
2021 *ctrlw = 16;
2022 code |= 0x1c;
2023 } else if (*ctrlw >= 12) {
2024 *ctrlw = 12;
2025 code |= 0x1a;
2026 } else {
2027 *ctrlw = 8;
2028 code |= 0x18;
2029 }
2030 }
2031
2032 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2033 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2034 return code;
2035}
2036
2037static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
2038 int sz, u32 shft)
2039{
2040 int i;
2041 u32 oldoff;
2042 for (i = 0; i < sz; i++) {
2043 struct slim_ich *slc;
2044 if (ach[i] == NULL)
2045 continue;
2046 slc = ach[i];
2047 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2048 continue;
2049 oldoff = slc->newoff;
2050 slc->newoff += shft;
2051 /* seg. offset must be <= interval */
2052 if (slc->newoff >= slc->newintr)
2053 slc->newoff -= slc->newintr;
2054 }
2055}
2056
2057static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
2058 u32 *ctrlw, u32 *subfrml)
2059{
2060 int coeff1, coeff3;
2061 enum slim_ch_coeff bias;
2062 struct slim_controller *ctrl = sb->ctrl;
2063 int last1 = ctrl->sched.num_cc1 - 1;
2064 int last3 = ctrl->sched.num_cc3 - 1;
2065
2066 /*
2067 * Find first channels with coeff 1 & 3 as starting points for
2068 * scheduling
2069 */
2070 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
2071 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
2072 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2073 continue;
2074 else
2075 break;
2076 }
2077 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
2078 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
2079 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2080 continue;
2081 else
2082 break;
2083 }
2084 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
2085 *ctrlw = 8;
2086 *subfrml = 8;
2087 return 0;
2088 } else if (coeff3 == ctrl->sched.num_cc3)
2089 bias = SLIM_COEFF_1;
2090 else
2091 bias = SLIM_COEFF_3;
2092
2093 /*
2094 * Find last chan in coeff1, 3 list, we will use to know when we
2095 * have done scheduling all coeff1 channels
2096 */
2097 while (last1 >= 0) {
2098 if (ctrl->sched.chc1[last1] != NULL &&
2099 (ctrl->sched.chc1[last1])->state !=
2100 SLIM_CH_PENDING_REMOVAL)
2101 break;
2102 last1--;
2103 }
2104 while (last3 >= 0) {
2105 if (ctrl->sched.chc3[last3] != NULL &&
2106 (ctrl->sched.chc3[last3])->state !=
2107 SLIM_CH_PENDING_REMOVAL)
2108 break;
2109 last3--;
2110 }
2111
2112 if (bias == SLIM_COEFF_1) {
2113 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
2114 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2115 int curexp, finalexp;
2116 u32 curintr, curmaxsl;
2117 int opensl1[2];
2118 int maxctrlw1;
2119
2120 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2121 curexp = (int)expshft - 1;
2122
2123 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2124 curmaxsl = curintr >> 1;
2125 opensl1[0] = opensl1[1] = curmaxsl;
2126
2127 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2128 curintr >>= 1;
2129 curmaxsl >>= 1;
2130
2131 /* update 4K family open slot records */
2132 if (opensl1[1] < opensl1[0])
2133 opensl1[1] -= curmaxsl;
2134 else
2135 opensl1[1] = opensl1[0] - curmaxsl;
2136 opensl1[0] = curmaxsl;
2137 if (opensl1[1] < 0) {
2138 opensl1[0] += opensl1[1];
2139 opensl1[1] = 0;
2140 }
2141 if (opensl1[0] <= 0) {
2142 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2143 __LINE__);
2144 return -EXFULL;
2145 }
2146 curexp++;
2147 /* schedule 4k family channels */
2148
2149 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2150 (int)(slc1->rootexp + expshft))) {
2151 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2152 coeff1++;
2153 slc1 = ctrl->sched.chc1[coeff1];
2154 continue;
2155 }
2156 if (opensl1[1] >= opensl1[0] ||
2157 (finalexp == (int)slc1->rootexp &&
2158 curintr <= 24 &&
2159 opensl1[0] == curmaxsl)) {
2160 opensl1[1] -= slc1->seglen;
2161 slc1->newoff = curmaxsl + opensl1[1];
2162 if (opensl1[1] < 0 &&
2163 opensl1[0] == curmaxsl) {
2164 opensl1[0] += opensl1[1];
2165 opensl1[1] = 0;
2166 if (opensl1[0] < 0) {
2167 dev_dbg(&ctrl->dev,
2168 "reconfig failed:%d\n",
2169 __LINE__);
2170 return -EXFULL;
2171 }
2172 }
2173 } else {
2174 if (slc1->seglen > opensl1[0]) {
2175 dev_dbg(&ctrl->dev,
2176 "reconfig failed:%d\n",
2177 __LINE__);
2178 return -EXFULL;
2179 }
2180 slc1->newoff = opensl1[0] -
2181 slc1->seglen;
2182 opensl1[0] = slc1->newoff;
2183 }
2184 slc1->newintr = curintr;
2185 coeff1++;
2186 slc1 = ctrl->sched.chc1[coeff1];
2187 }
2188 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002189 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002190 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002191 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192 if (opensl1[1] > opensl1[0]) {
2193 int temp = opensl1[0];
2194 opensl1[0] = opensl1[1];
2195 opensl1[1] = temp;
2196 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2197 ctrl->sched.num_cc1, curmaxsl);
2198 }
2199 /* choose subframe mode to maximize bw */
2200 maxctrlw1 = opensl1[0];
2201 if (opensl1[0] == curmaxsl)
2202 maxctrlw1 += opensl1[1];
2203 if (curintr >= 24) {
2204 *subfrml = 24;
2205 *ctrlw = maxctrlw1;
2206 } else if (curintr == 12) {
2207 if (maxctrlw1 > opensl1[1] * 4) {
2208 *subfrml = 24;
2209 *ctrlw = maxctrlw1;
2210 } else {
2211 *subfrml = 6;
2212 *ctrlw = opensl1[1];
2213 }
2214 } else {
2215 *subfrml = 6;
2216 *ctrlw = maxctrlw1;
2217 }
2218 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002219 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002220 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2221 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2222 int curexp, finalexp, exp1;
2223 u32 curintr, curmaxsl;
2224 int opensl3[2];
2225 int opensl1[6];
2226 bool opensl1valid = false;
2227 int maxctrlw1, maxctrlw3, i;
2228 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2229 if (last1 >= 0) {
2230 slc1 = ctrl->sched.chc1[coeff1];
2231 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2232 if (exp1 > finalexp)
2233 finalexp = exp1;
2234 }
2235 curexp = (int)expshft - 1;
2236
2237 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2238 curmaxsl = curintr >> 1;
2239 opensl3[0] = opensl3[1] = curmaxsl;
2240
2241 while (coeff1 < ctrl->sched.num_cc1 ||
2242 coeff3 < ctrl->sched.num_cc3 ||
2243 curintr > 32) {
2244 curintr >>= 1;
2245 curmaxsl >>= 1;
2246
2247 /* update 12k family open slot records */
2248 if (opensl3[1] < opensl3[0])
2249 opensl3[1] -= curmaxsl;
2250 else
2251 opensl3[1] = opensl3[0] - curmaxsl;
2252 opensl3[0] = curmaxsl;
2253 if (opensl3[1] < 0) {
2254 opensl3[0] += opensl3[1];
2255 opensl3[1] = 0;
2256 }
2257 if (opensl3[0] <= 0) {
2258 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2259 __LINE__);
2260 return -EXFULL;
2261 }
2262 curexp++;
2263
2264 /* schedule 12k family channels */
2265 while (coeff3 < ctrl->sched.num_cc3 &&
2266 curexp == (int)slc3->rootexp + expshft) {
2267 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2268 coeff3++;
2269 slc3 = ctrl->sched.chc3[coeff3];
2270 continue;
2271 }
2272 opensl1valid = false;
2273 if (opensl3[1] >= opensl3[0] ||
2274 (finalexp == (int)slc3->rootexp &&
2275 curintr <= 32 &&
2276 opensl3[0] == curmaxsl &&
2277 last1 < 0)) {
2278 opensl3[1] -= slc3->seglen;
2279 slc3->newoff = curmaxsl + opensl3[1];
2280 if (opensl3[1] < 0 &&
2281 opensl3[0] == curmaxsl) {
2282 opensl3[0] += opensl3[1];
2283 opensl3[1] = 0;
2284 }
2285 if (opensl3[0] < 0) {
2286 dev_dbg(&ctrl->dev,
2287 "reconfig failed:%d\n",
2288 __LINE__);
2289 return -EXFULL;
2290 }
2291 } else {
2292 if (slc3->seglen > opensl3[0]) {
2293 dev_dbg(&ctrl->dev,
2294 "reconfig failed:%d\n",
2295 __LINE__);
2296 return -EXFULL;
2297 }
2298 slc3->newoff = opensl3[0] -
2299 slc3->seglen;
2300 opensl3[0] = slc3->newoff;
2301 }
2302 slc3->newintr = curintr;
2303 coeff3++;
2304 slc3 = ctrl->sched.chc3[coeff3];
2305 }
2306 /* update 4k openslot records */
2307 if (opensl1valid == false) {
2308 for (i = 0; i < 3; i++) {
2309 opensl1[i * 2] = opensl3[0];
2310 opensl1[(i * 2) + 1] = opensl3[1];
2311 }
2312 } else {
2313 int opensl1p[6];
2314 memcpy(opensl1p, opensl1, sizeof(opensl1));
2315 for (i = 0; i < 3; i++) {
2316 if (opensl1p[i] < opensl1p[i + 3])
2317 opensl1[(i * 2) + 1] =
2318 opensl1p[i];
2319 else
2320 opensl1[(i * 2) + 1] =
2321 opensl1p[i + 3];
2322 }
2323 for (i = 0; i < 3; i++) {
2324 opensl1[(i * 2) + 1] -= curmaxsl;
2325 opensl1[i * 2] = curmaxsl;
2326 if (opensl1[(i * 2) + 1] < 0) {
2327 opensl1[i * 2] +=
2328 opensl1[(i * 2) + 1];
2329 opensl1[(i * 2) + 1] = 0;
2330 }
2331 if (opensl1[i * 2] < 0) {
2332 dev_dbg(&ctrl->dev,
2333 "reconfig failed:%d\n",
2334 __LINE__);
2335 return -EXFULL;
2336 }
2337 }
2338 }
2339 /* schedule 4k family channels */
2340 while (coeff1 < ctrl->sched.num_cc1 &&
2341 curexp == (int)slc1->rootexp + expshft) {
2342 /* searchorder effective when opensl valid */
2343 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2344 int maxopensl = 0;
2345 int maxi = 0;
2346 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2347 coeff1++;
2348 slc1 = ctrl->sched.chc1[coeff1];
2349 continue;
2350 }
2351 opensl1valid = true;
2352 for (i = 0; i < 6; i++) {
2353 if (opensl1[srcho[i]] > maxopensl) {
2354 maxopensl = opensl1[srcho[i]];
2355 maxi = srcho[i];
2356 }
2357 }
2358 opensl1[maxi] -= slc1->seglen;
2359 slc1->newoff = (curmaxsl * maxi) +
2360 opensl1[maxi];
2361 if (opensl1[maxi] < 0) {
2362 if (((maxi & 1) == 1) &&
2363 (opensl1[maxi - 1] == curmaxsl)) {
2364 opensl1[maxi - 1] +=
2365 opensl1[maxi];
2366 if (opensl3[0] >
2367 opensl1[maxi - 1])
2368 opensl3[0] =
2369 opensl1[maxi - 1];
2370 opensl3[1] = 0;
2371 opensl1[maxi] = 0;
2372 if (opensl1[maxi - 1] < 0) {
2373 dev_dbg(&ctrl->dev,
2374 "reconfig failed:%d\n",
2375 __LINE__);
2376 return -EXFULL;
2377 }
2378 } else {
2379 dev_dbg(&ctrl->dev,
2380 "reconfig failed:%d\n",
2381 __LINE__);
2382 return -EXFULL;
2383 }
2384 } else {
2385 if (opensl3[maxi & 1] > opensl1[maxi])
2386 opensl3[maxi & 1] =
2387 opensl1[maxi];
2388 }
2389 slc1->newintr = curintr * 3;
2390 coeff1++;
2391 slc1 = ctrl->sched.chc1[coeff1];
2392 }
2393 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002394 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002395 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002396 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2398 if (opensl3[1] > opensl3[0]) {
2399 int temp = opensl3[0];
2400 opensl3[0] = opensl3[1];
2401 opensl3[1] = temp;
2402 temp = opensl1[5];
2403 opensl1[5] = opensl1[4];
2404 opensl1[4] = opensl1[3];
2405 opensl1[3] = opensl1[2];
2406 opensl1[2] = opensl1[1];
2407 opensl1[1] = opensl1[0];
2408 opensl1[0] = temp;
2409 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2410 ctrl->sched.num_cc1, curmaxsl);
2411 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2412 ctrl->sched.num_cc3, curmaxsl);
2413 }
2414 /* subframe mode to maximize BW */
2415 maxctrlw3 = opensl3[0];
2416 maxctrlw1 = opensl1[0];
2417 if (opensl3[0] == curmaxsl)
2418 maxctrlw3 += opensl3[1];
2419 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2420 maxctrlw1 += opensl1[i + 1];
2421 if (curintr >= 32) {
2422 *subfrml = 32;
2423 *ctrlw = maxctrlw3;
2424 } else if (curintr == 16) {
2425 if (maxctrlw3 > (opensl3[1] * 4)) {
2426 *subfrml = 32;
2427 *ctrlw = maxctrlw3;
2428 } else {
2429 *subfrml = 8;
2430 *ctrlw = opensl3[1];
2431 }
2432 } else {
2433 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2434 *subfrml = 24;
2435 *ctrlw = maxctrlw1;
2436 } else {
2437 *subfrml = 8;
2438 *ctrlw = maxctrlw3;
2439 }
2440 }
2441 }
2442 return 0;
2443}
2444
2445#ifdef DEBUG
2446static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2447 u32 subfrml, u32 clkgear)
2448{
2449 int sl, i;
2450 int cc1 = 0;
2451 int cc3 = 0;
2452 struct slim_ich *slc = NULL;
2453 if (!ctrl->sched.slots)
2454 return 0;
2455 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2456 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2457 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2458 for (i = 0; i < ctrlw; i++)
2459 ctrl->sched.slots[sl + i] = 33;
2460 }
2461 while (cc1 < ctrl->sched.num_cc1) {
2462 slc = ctrl->sched.chc1[cc1];
2463 if (slc == NULL) {
2464 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2465 cc1);
2466 return -EIO;
2467 }
2468 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2469 (slc - ctrl->chans), slc->newoff,
2470 slc->newintr, slc->seglen);
2471
2472 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2473 for (sl = slc->newoff;
2474 sl < SLIM_SL_PER_SUPERFRAME;
2475 sl += slc->newintr) {
2476 for (i = 0; i < slc->seglen; i++) {
2477 if (ctrl->sched.slots[sl + i])
2478 return -EXFULL;
2479 ctrl->sched.slots[sl + i] = cc1 + 1;
2480 }
2481 }
2482 }
2483 cc1++;
2484 }
2485 while (cc3 < ctrl->sched.num_cc3) {
2486 slc = ctrl->sched.chc3[cc3];
2487 if (slc == NULL) {
2488 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2489 cc3);
2490 return -EIO;
2491 }
2492 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2493 (slc - ctrl->chans), slc->newoff,
2494 slc->newintr, slc->seglen);
2495 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2496 for (sl = slc->newoff;
2497 sl < SLIM_SL_PER_SUPERFRAME;
2498 sl += slc->newintr) {
2499 for (i = 0; i < slc->seglen; i++) {
2500 if (ctrl->sched.slots[sl + i])
2501 return -EXFULL;
2502 ctrl->sched.slots[sl + i] = cc3 + 1;
2503 }
2504 }
2505 }
2506 cc3++;
2507 }
2508
2509 return 0;
2510}
2511#else
2512static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2513 u32 subfrml, u32 clkgear)
2514{
2515 return 0;
2516}
2517#endif
2518
2519static void slim_sort_chan_grp(struct slim_controller *ctrl,
2520 struct slim_ich *slc)
2521{
2522 u8 last = (u8)-1;
2523 u8 second = 0;
2524
2525 for (; last > 0; last--) {
2526 struct slim_ich *slc1 = slc;
2527 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002528 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 slc2 = &ctrl->chans[next];
2530 for (second = 1; second <= last && slc2 &&
2531 (slc2->state == SLIM_CH_ACTIVE ||
2532 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2533 if (slc1->newoff > slc2->newoff) {
2534 u32 temp = slc2->newoff;
2535 slc2->newoff = slc1->newoff;
2536 slc1->newoff = temp;
2537 }
2538 if (slc2->nextgrp & SLIM_END_GRP) {
2539 last = second;
2540 break;
2541 }
2542 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002543 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002544 slc2 = &ctrl->chans[next];
2545 }
2546 if (slc2 == NULL)
2547 last = second - 1;
2548 }
2549}
2550
2551
2552static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2553{
2554 u32 msgsl = 0;
2555 u32 ctrlw = 0;
2556 u32 subfrml = 0;
2557 int ret = -EIO;
2558 struct slim_controller *ctrl = sb->ctrl;
2559 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2560 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2561 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2562 *clkgear = SLIM_MAX_CLK_GEAR;
2563
2564 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2565 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2566 ctrl->sched.usedslots,
2567 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002568 /*
2569 * If number of slots are 0, that means channels are inactive.
2570 * It is very likely that the manager will call clock pause very soon.
2571 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2572 * minimum amount of time.
2573 */
2574 if (ctrl->sched.usedslots != 0) {
2575 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2576 *clkgear -= 1;
2577 usedsl *= 2;
2578 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002579 }
2580
2581 /*
2582 * Try scheduling data channels at current clock gear, if all channels
2583 * can be scheduled, or reserved BW can't be satisfied, increase clock
2584 * gear and try again
2585 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002586 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2588
2589 if (ret == 0) {
2590 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002591 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002593 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594 continue;
2595 else
2596 break;
2597 }
2598 }
2599 if (ret == 0) {
2600 int i;
2601 /* Sort channel-groups */
2602 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2603 struct slim_ich *slc = ctrl->sched.chc1[i];
2604 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2605 continue;
2606 if ((slc->nextgrp & SLIM_START_GRP) &&
2607 !(slc->nextgrp & SLIM_END_GRP)) {
2608 slim_sort_chan_grp(ctrl, slc);
2609 }
2610 }
2611 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2612 struct slim_ich *slc = ctrl->sched.chc3[i];
2613 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2614 continue;
2615 if ((slc->nextgrp & SLIM_START_GRP) &&
2616 !(slc->nextgrp & SLIM_END_GRP)) {
2617 slim_sort_chan_grp(ctrl, slc);
2618 }
2619 }
2620
2621 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2622 }
2623
2624 return ret;
2625}
2626
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002627static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2628{
2629 struct slim_ich **arr;
2630 int len, i;
2631 if (coeff == SLIM_COEFF_1) {
2632 arr = ctrl->sched.chc1;
2633 len = ctrl->sched.num_cc1;
2634 } else {
2635 arr = ctrl->sched.chc3;
2636 len = ctrl->sched.num_cc3;
2637 }
2638 for (i = 0; i < len; i++) {
2639 struct slim_ich *slc = arr[i];
2640 if (slc->state == SLIM_CH_ACTIVE ||
2641 slc->state == SLIM_CH_SUSPENDED)
2642 slc->offset = slc->newoff;
2643 slc->interval = slc->newintr;
2644 }
2645}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002646static void slim_chan_changes(struct slim_device *sb, bool revert)
2647{
2648 struct slim_controller *ctrl = sb->ctrl;
2649 while (!list_empty(&sb->mark_define)) {
2650 struct slim_ich *slc;
2651 struct slim_pending_ch *pch =
2652 list_entry(sb->mark_define.next,
2653 struct slim_pending_ch, pending);
2654 slc = &ctrl->chans[pch->chan];
2655 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002656 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2657 u32 sl = slc->seglen << slc->rootexp;
2658 if (slc->coeff == SLIM_COEFF_3)
2659 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002660 if (!ctrl->allocbw)
2661 ctrl->sched.usedslots -= sl;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002662 slim_remove_ch(ctrl, slc);
2663 slc->state = SLIM_CH_DEFINED;
2664 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002666 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002667 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668 }
2669 list_del_init(&pch->pending);
2670 kfree(pch);
2671 }
2672
2673 while (!list_empty(&sb->mark_removal)) {
2674 struct slim_pending_ch *pch =
2675 list_entry(sb->mark_removal.next,
2676 struct slim_pending_ch, pending);
2677 struct slim_ich *slc = &ctrl->chans[pch->chan];
2678 u32 sl = slc->seglen << slc->rootexp;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002679 if (revert || slc->def > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680 if (slc->coeff == SLIM_COEFF_3)
2681 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002682 if (!ctrl->allocbw)
2683 ctrl->sched.usedslots += sl;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002684 if (revert)
2685 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002686 slc->state = SLIM_CH_ACTIVE;
2687 } else
2688 slim_remove_ch(ctrl, slc);
2689 list_del_init(&pch->pending);
2690 kfree(pch);
2691 }
2692
2693 while (!list_empty(&sb->mark_suspend)) {
2694 struct slim_pending_ch *pch =
2695 list_entry(sb->mark_suspend.next,
2696 struct slim_pending_ch, pending);
2697 struct slim_ich *slc = &ctrl->chans[pch->chan];
2698 if (revert)
2699 slc->state = SLIM_CH_ACTIVE;
2700 list_del_init(&pch->pending);
2701 kfree(pch);
2702 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002703 /* Change already active channel if reconfig succeeded */
2704 if (!revert) {
2705 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2706 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2707 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002708}
2709
2710/*
2711 * slim_reconfigure_now: Request reconfiguration now.
2712 * @sb: client handle
2713 * This API does what commit flag in other scheduling APIs do.
2714 * -EXFULL is returned if there is no space in TDM to reserve the
2715 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2716 * progress.
2717 */
2718int slim_reconfigure_now(struct slim_device *sb)
2719{
2720 u8 i;
2721 u8 wbuf[4];
2722 u32 clkgear, subframe;
2723 u32 curexp;
2724 int ret;
2725 struct slim_controller *ctrl = sb->ctrl;
2726 u32 expshft;
2727 u32 segdist;
2728 struct slim_pending_ch *pch;
2729
Sagar Dharia80a55e12012-08-16 16:43:58 -06002730 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002731 /*
2732 * If there are no pending changes from this client, avoid sending
2733 * the reconfiguration sequence
2734 */
2735 if (sb->pending_msgsl == sb->cur_msgsl &&
2736 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002737 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002738 struct list_head *pos, *next;
2739 list_for_each_safe(pos, next, &sb->mark_removal) {
2740 struct slim_ich *slc;
2741 pch = list_entry(pos, struct slim_pending_ch, pending);
2742 slc = &ctrl->chans[pch->chan];
2743 if (slc->def > 0)
2744 slc->def--;
2745 /* Disconnect source port to free it up */
2746 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2747 slc->srch = 0;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002748 /*
2749 * If controller overrides BW allocation,
2750 * delete this in remove channel itself
2751 */
2752 if (slc->def != 0 && !ctrl->allocbw) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002753 list_del(&pch->pending);
2754 kfree(pch);
2755 }
2756 }
2757 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002758 mutex_unlock(&ctrl->sched.m_reconf);
2759 pr_info("SLIM_CL: skip reconfig sequence");
2760 return 0;
2761 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002762 }
2763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002764 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2765 list_for_each_entry(pch, &sb->mark_define, pending) {
2766 struct slim_ich *slc = &ctrl->chans[pch->chan];
2767 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002768 if (slc->state < SLIM_CH_ACTIVE)
2769 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002770 }
2771
2772 list_for_each_entry(pch, &sb->mark_removal, pending) {
2773 struct slim_ich *slc = &ctrl->chans[pch->chan];
2774 u32 sl = slc->seglen << slc->rootexp;
2775 if (slc->coeff == SLIM_COEFF_3)
2776 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002777 if (!ctrl->allocbw)
2778 ctrl->sched.usedslots -= sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002779 slc->state = SLIM_CH_PENDING_REMOVAL;
2780 }
2781 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2782 struct slim_ich *slc = &ctrl->chans[pch->chan];
2783 slc->state = SLIM_CH_SUSPENDED;
2784 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002785
Sagar Dharia4aec9232012-07-24 23:44:26 -06002786 /*
2787 * Controller can override default channel scheduling algorithm.
2788 * (e.g. if controller needs to use fixed channel scheduling based
2789 * on number of channels)
2790 */
2791 if (ctrl->allocbw)
2792 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2793 else
2794 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002795
2796 if (!ret) {
2797 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2798 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2799 NULL, NULL, 0, 3, NULL, 0, NULL);
2800 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2801 }
2802
2803 if (!ret && subframe != ctrl->sched.subfrmcode) {
2804 wbuf[0] = (u8)(subframe & 0xFF);
2805 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2806 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2807 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2808 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2809 (int)wbuf[0], ret);
2810 }
2811 if (!ret && clkgear != ctrl->clkgear) {
2812 wbuf[0] = (u8)(clkgear & 0xFF);
2813 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2814 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2815 NULL, wbuf, 1, 4, NULL, 0, NULL);
2816 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2817 (int)wbuf[0], ret);
2818 }
2819 if (ret)
2820 goto revert_reconfig;
2821
2822 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2823 /* activate/remove channel */
2824 list_for_each_entry(pch, &sb->mark_define, pending) {
2825 struct slim_ich *slc = &ctrl->chans[pch->chan];
2826 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002827 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002828 wbuf[1] = slc->prrate;
2829 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2830 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2831 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2832 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2833 /* Right now, channel link bit is not supported */
2834 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2835 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2836 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2837 NULL, 0, NULL);
2838 if (ret)
2839 goto revert_reconfig;
2840
2841 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2842 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2843 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2844 NULL, 0, NULL);
2845 if (ret)
2846 goto revert_reconfig;
2847 }
2848
2849 list_for_each_entry(pch, &sb->mark_removal, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002850 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002851 dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002852 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002853 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2854 SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
2855 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2856 NULL, 0, NULL);
2857 if (ret)
2858 goto revert_reconfig;
2859 }
2860 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002861 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002863 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002864 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2865 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2866 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2867 NULL, 0, NULL);
2868 if (ret)
2869 goto revert_reconfig;
2870 }
2871
2872 /* Define CC1 channel */
2873 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2874 struct slim_ich *slc = ctrl->sched.chc1[i];
2875 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2876 continue;
2877 curexp = slc->rootexp + expshft;
2878 segdist = (slc->newoff << curexp) & 0x1FF;
2879 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002880 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2881 slc->newintr, slc->interval, segdist);
2882 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2883 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002884
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002885 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886 slc->newintr != slc->interval ||
2887 slc->newoff != slc->offset) {
2888 segdist |= 0x200;
2889 segdist >>= curexp;
2890 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002891 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002892 wbuf[1] = (u8)(segdist & 0xFF);
2893 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2894 (slc->prop.prot << 4);
2895 wbuf[3] = slc->seglen;
2896 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2897 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2898 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2899 7, NULL, 0, NULL);
2900 if (ret)
2901 goto revert_reconfig;
2902 }
2903 }
2904
2905 /* Define CC3 channels */
2906 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2907 struct slim_ich *slc = ctrl->sched.chc3[i];
2908 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2909 continue;
2910 curexp = slc->rootexp + expshft;
2911 segdist = (slc->newoff << curexp) & 0x1FF;
2912 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002913 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2914 slc->newintr, slc->interval, segdist);
2915 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2916 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002918 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919 slc->newintr != slc->interval ||
2920 slc->newoff != slc->offset) {
2921 segdist |= 0x200;
2922 segdist >>= curexp;
2923 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002924 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002925 wbuf[1] = (u8)(segdist & 0xFF);
2926 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2927 (slc->prop.prot << 4);
2928 wbuf[3] = (u8)(slc->seglen);
2929 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2930 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2931 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2932 7, NULL, 0, NULL);
2933 if (ret)
2934 goto revert_reconfig;
2935 }
2936 }
2937 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2938 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2939 NULL, 0, 3, NULL, 0, NULL);
2940 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2941 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002942 ctrl->sched.subfrmcode = subframe;
2943 ctrl->clkgear = clkgear;
2944 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2945 sb->cur_msgsl = sb->pending_msgsl;
2946 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002947 mutex_unlock(&ctrl->sched.m_reconf);
2948 return 0;
2949 }
2950
2951revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002952 /* Revert channel changes */
2953 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002954 mutex_unlock(&ctrl->sched.m_reconf);
2955 return ret;
2956}
2957EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2958
2959static int add_pending_ch(struct list_head *listh, u8 chan)
2960{
2961 struct slim_pending_ch *pch;
2962 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2963 if (!pch)
2964 return -ENOMEM;
2965 pch->chan = chan;
2966 list_add_tail(&pch->pending, listh);
2967 return 0;
2968}
2969
2970/*
2971 * slim_control_ch: Channel control API.
2972 * @sb: client handle
2973 * @chanh: group or channel handle to be controlled
2974 * @chctrl: Control command (activate/suspend/remove)
2975 * @commit: flag to indicate whether the control should take effect right-away.
2976 * This API activates, removes or suspends a channel (or group of channels)
2977 * chanh indicates the channel or group handle (returned by the define_ch API).
2978 * Reconfiguration may be time-consuming since it can change all other active
2979 * channel allocations on the bus, change in clock gear used by the slimbus,
2980 * and change in the control space width used for messaging.
2981 * commit makes sure that multiple channels can be activated/deactivated before
2982 * reconfiguration is started.
2983 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
2984 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
2985 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002986 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002987 */
2988int slim_control_ch(struct slim_device *sb, u16 chanh,
2989 enum slim_ch_control chctrl, bool commit)
2990{
2991 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002992 int ret = 0;
2993 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06002994 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06002995 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002996 struct slim_ich *slc = &ctrl->chans[chan];
2997 if (!(slc->nextgrp & SLIM_START_GRP))
2998 return -EINVAL;
2999
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003000 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003001 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303002 struct slim_pending_ch *pch;
3003 u8 add_mark_removal = true;
3004
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003005 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003006 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
3007 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003008 if (slc->state < SLIM_CH_DEFINED) {
3009 ret = -ENOTCONN;
3010 break;
3011 }
3012 if (chctrl == SLIM_CH_SUSPEND) {
3013 ret = add_pending_ch(&sb->mark_suspend, chan);
3014 if (ret)
3015 break;
3016 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003017 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003018 ret = -EISCONN;
3019 break;
3020 }
3021 ret = add_pending_ch(&sb->mark_define, chan);
3022 if (ret)
3023 break;
3024 } else {
3025 if (slc->state < SLIM_CH_ACTIVE) {
3026 ret = -ENOTCONN;
3027 break;
3028 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05303029 /* If channel removal request comes when pending
3030 * in the mark_define, remove it from the define
3031 * list instead of adding it to removal list
3032 */
3033 if (!list_empty(&sb->mark_define)) {
3034 struct list_head *pos, *next;
3035 list_for_each_safe(pos, next,
3036 &sb->mark_define) {
3037 pch = list_entry(pos,
3038 struct slim_pending_ch,
3039 pending);
Kiran Gundabcd74d02013-10-08 16:32:13 +05303040 if (pch->chan == chan) {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303041 list_del(&pch->pending);
3042 kfree(pch);
3043 add_mark_removal = false;
3044 break;
3045 }
3046 }
3047 }
3048 if (add_mark_removal == true) {
3049 ret = add_pending_ch(&sb->mark_removal, chan);
3050 if (ret)
3051 break;
3052 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003053 }
3054
Sagar Dhariab886e042012-10-17 22:41:57 -06003055 nchan++;
3056 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06003057 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06003058 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003059 if (!ret && commit == true)
3060 ret = slim_reconfigure_now(sb);
3061 mutex_unlock(&sb->sldev_reconf);
3062 return ret;
3063}
3064EXPORT_SYMBOL_GPL(slim_control_ch);
3065
3066/*
3067 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
3068 * @sb: client handle
3069 * @bw_bps: message bandwidth in bits per second to be requested
3070 * @commit: indicates whether the reconfiguration needs to be acted upon.
3071 * This API call can be grouped with slim_control_ch API call with only one of
3072 * the APIs specifying the commit flag to avoid reconfiguration being called too
3073 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
3074 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
3075 * is already in progress.
3076 */
3077int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
3078{
3079 struct slim_controller *ctrl = sb->ctrl;
3080 int ret = 0;
3081 int sl;
3082 mutex_lock(&sb->sldev_reconf);
3083 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
3084 sl = SLIM_SL_PER_SUPERFRAME;
3085 else {
3086 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
3087 (ctrl->a_framer->rootfreq/2 - 1)) /
3088 (ctrl->a_framer->rootfreq/2);
3089 }
3090 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
3091 sb->cur_msgsl);
3092 sb->pending_msgsl = sl;
3093 if (commit == true)
3094 ret = slim_reconfigure_now(sb);
3095 mutex_unlock(&sb->sldev_reconf);
3096 return ret;
3097}
3098EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
3099
Sagar Dharia33f34442011-08-08 16:22:03 -06003100/*
3101 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
3102 * paused or woken up out of clock pause
3103 * or woken up from clock pause
3104 * @ctrl: controller requesting bus to be paused or woken up
3105 * @wakeup: Wakeup this controller from clock pause.
3106 * @restart: Restart time value per spec used for clock pause. This value
3107 * isn't used when controller is to be woken up.
3108 * This API executes clock pause reconfiguration sequence if wakeup is false.
3109 * If wakeup is true, controller's wakeup is called
3110 * Slimbus clock is idle and can be disabled by the controller later.
3111 */
3112int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
3113{
3114 int ret = 0;
3115 int i;
3116
3117 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
3118 return -EINVAL;
3119 mutex_lock(&ctrl->m_ctrl);
3120 if (wakeup) {
3121 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
3122 mutex_unlock(&ctrl->m_ctrl);
3123 return 0;
3124 }
3125 wait_for_completion(&ctrl->pause_comp);
3126 /*
3127 * Slimbus framework will call controller wakeup
3128 * Controller should make sure that it sets active framer
3129 * out of clock pause by doing appropriate setting
3130 */
3131 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3132 ret = ctrl->wakeup(ctrl);
Sagar Dharia129c7d82013-08-08 19:35:50 -06003133 /*
3134 * If wakeup fails, make sure that next attempt can succeed.
3135 * Since we already consumed pause_comp, complete it so
3136 * that next wakeup isn't blocked forever
3137 */
Sagar Dharia33f34442011-08-08 16:22:03 -06003138 if (!ret)
3139 ctrl->clk_state = SLIM_CLK_ACTIVE;
Sagar Dharia129c7d82013-08-08 19:35:50 -06003140 else
3141 complete(&ctrl->pause_comp);
Sagar Dharia33f34442011-08-08 16:22:03 -06003142 mutex_unlock(&ctrl->m_ctrl);
3143 return ret;
3144 } else {
3145 switch (ctrl->clk_state) {
3146 case SLIM_CLK_ENTERING_PAUSE:
3147 case SLIM_CLK_PAUSE_FAILED:
3148 /*
3149 * If controller is already trying to enter clock pause,
3150 * let it finish.
3151 * In case of error, retry
3152 * In both cases, previous clock pause has signalled
3153 * completion.
3154 */
3155 wait_for_completion(&ctrl->pause_comp);
3156 /* retry upon failure */
3157 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3158 ctrl->clk_state = SLIM_CLK_ACTIVE;
3159 break;
3160 } else {
3161 mutex_unlock(&ctrl->m_ctrl);
3162 /*
3163 * Signal completion so that wakeup can wait on
3164 * it.
3165 */
3166 complete(&ctrl->pause_comp);
3167 return 0;
3168 }
3169 break;
3170 case SLIM_CLK_PAUSED:
3171 /* already paused */
3172 mutex_unlock(&ctrl->m_ctrl);
3173 return 0;
3174 case SLIM_CLK_ACTIVE:
3175 default:
3176 break;
3177 }
3178 }
3179 /* Pending response for a message */
3180 for (i = 0; i < ctrl->last_tid; i++) {
3181 if (ctrl->txnt[i]) {
3182 ret = -EBUSY;
Sagar Dharia33beca02012-10-22 16:21:46 -06003183 pr_info("slim_clk_pause: txn-rsp for %d pending", i);
Sagar Dharia33f34442011-08-08 16:22:03 -06003184 mutex_unlock(&ctrl->m_ctrl);
3185 return -EBUSY;
3186 }
3187 }
3188 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3189 mutex_unlock(&ctrl->m_ctrl);
3190
3191 mutex_lock(&ctrl->sched.m_reconf);
3192 /* Data channels active */
3193 if (ctrl->sched.usedslots) {
Sagar Dharia33beca02012-10-22 16:21:46 -06003194 pr_info("slim_clk_pause: data channel active");
Sagar Dharia33f34442011-08-08 16:22:03 -06003195 ret = -EBUSY;
3196 goto clk_pause_ret;
3197 }
3198
3199 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003200 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3201 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003202 if (ret)
3203 goto clk_pause_ret;
3204
3205 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003206 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3207 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3208 if (ret)
3209 goto clk_pause_ret;
3210
3211 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3212 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3213 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003214 if (ret)
3215 goto clk_pause_ret;
3216
3217clk_pause_ret:
3218 if (ret)
3219 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3220 else
3221 ctrl->clk_state = SLIM_CLK_PAUSED;
3222 complete(&ctrl->pause_comp);
3223 mutex_unlock(&ctrl->sched.m_reconf);
3224 return ret;
3225}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003226EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003227
3228MODULE_LICENSE("GPL v2");
3229MODULE_VERSION("0.1");
3230MODULE_DESCRIPTION("Slimbus module");
3231MODULE_ALIAS("platform:slimbus");