blob: caf7a87f75a3d66dd636d421cdfe9440d8eb7f66 [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];
Kiran Gunda1aaae922014-02-04 07:45:03 +0530692 if (txn == NULL || txn->rbuf == NULL) {
693 if (txn == NULL)
694 dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 tid, len);
Kiran Gunda1aaae922014-02-04 07:45:03 +0530696 else
697 dev_err(&ctrl->dev, "Invalid client buffer passed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 mutex_unlock(&ctrl->m_ctrl);
699 return;
700 }
701 for (i = 0; i < len; i++)
702 txn->rbuf[i] = reply[i];
703 if (txn->comp)
704 complete(txn->comp);
705 ctrl->txnt[tid] = NULL;
706 mutex_unlock(&ctrl->m_ctrl);
707 kfree(txn);
708}
709EXPORT_SYMBOL_GPL(slim_msg_response);
710
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600711static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
713 struct completion *comp, u8 la, u8 *tid)
714{
715 u8 i = 0;
716 int ret = 0;
717 struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
718 GFP_KERNEL);
719 if (!txn)
720 return -ENOMEM;
721 if (tid) {
722 mutex_lock(&ctrl->m_ctrl);
723 for (i = 0; i < ctrl->last_tid; i++) {
724 if (ctrl->txnt[i] == NULL)
725 break;
726 }
727 if (i >= ctrl->last_tid) {
728 if (ctrl->last_tid == 255) {
729 mutex_unlock(&ctrl->m_ctrl);
730 kfree(txn);
731 return -ENOMEM;
732 }
733 ctrl->txnt = krealloc(ctrl->txnt,
734 (i + 1) * sizeof(struct slim_msg_txn *),
735 GFP_KERNEL);
736 if (!ctrl->txnt) {
737 mutex_unlock(&ctrl->m_ctrl);
738 kfree(txn);
739 return -ENOMEM;
740 }
741 ctrl->last_tid++;
742 }
743 ctrl->txnt[i] = txn;
744 mutex_unlock(&ctrl->m_ctrl);
745 txn->tid = i;
746 *tid = i;
747 }
748 txn->mc = mc;
749 txn->mt = mt;
750 txn->dt = dt;
751 txn->ec = ec;
752 txn->la = la;
753 txn->rbuf = rbuf;
754 txn->wbuf = wbuf;
755 txn->rl = mlen;
756 txn->len = len;
757 txn->comp = comp;
758
759 ret = ctrl->xfer_msg(ctrl, txn);
760 if (!tid)
761 kfree(txn);
762 return ret;
763}
764
765static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600766 u8 e_len, u8 *entry)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767{
768 u8 i;
769 for (i = 0; i < ctrl->num_dev; i++) {
770 if (ctrl->addrt[i].valid &&
771 memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600772 *entry = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 return 0;
774 }
775 }
776 return -ENXIO;
777}
778
779/*
780 * slim_assign_laddr: Assign logical address to a device enumerated.
781 * @ctrl: Controller with which device is enumerated.
782 * @e_addr: 6-byte elemental address of the device.
783 * @e_len: buffer length for e_addr
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600784 * @laddr: Return logical address (if valid flag is false)
785 * @valid: true if laddr holds a valid address that controller wants to
786 * set for this enumeration address. Otherwise framework sets index into
787 * address table as logical address.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 * Called by controller in response to REPORT_PRESENT. Framework will assign
789 * a logical address to this enumeration address.
790 * Function returns -EXFULL to indicate that all logical addresses are already
791 * taken.
792 */
793int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600794 u8 e_len, u8 *laddr, bool valid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795{
796 int ret;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600797 u8 i = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600798 bool exists = false;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600799 struct slim_device *sbdev;
Sagar Dharia21f88552014-02-07 00:10:37 -0700800 struct list_head *pos, *next;
801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 mutex_lock(&ctrl->m_ctrl);
803 /* already assigned */
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600804 if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
805 *laddr = ctrl->addrt[i].laddr;
806 exists = true;
807 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 if (ctrl->num_dev >= 254) {
809 ret = -EXFULL;
810 goto ret_assigned_laddr;
811 }
812 for (i = 0; i < ctrl->num_dev; i++) {
813 if (ctrl->addrt[i].valid == false)
814 break;
815 }
816 if (i == ctrl->num_dev) {
817 ctrl->addrt = krealloc(ctrl->addrt,
818 (ctrl->num_dev + 1) *
819 sizeof(struct slim_addrt),
820 GFP_KERNEL);
821 if (!ctrl->addrt) {
822 ret = -ENOMEM;
823 goto ret_assigned_laddr;
824 }
825 ctrl->num_dev++;
826 }
827 memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
828 ctrl->addrt[i].valid = true;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600829 /* Preferred address is index into table */
830 if (!valid)
831 *laddr = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 }
833
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600834 ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
835 *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 if (ret) {
837 ctrl->addrt[i].valid = false;
838 goto ret_assigned_laddr;
839 }
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600840 ctrl->addrt[i].laddr = *laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600842 dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843ret_assigned_laddr:
844 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600845 if (exists || ret)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600846 return ret;
847
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600848 pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600849 e_addr[1], e_addr[2]);
850 mutex_lock(&ctrl->m_ctrl);
Sagar Dharia21f88552014-02-07 00:10:37 -0700851 list_for_each_safe(pos, next, &ctrl->devs) {
852 sbdev = list_entry(pos, struct slim_device, dev_list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600853 if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
854 struct slim_driver *sbdrv;
Sagar Dharia33c84e62012-10-30 21:12:09 -0600855 sbdev->laddr = *laddr;
Sagar Dharia21f88552014-02-07 00:10:37 -0700856 sbdev->reported = true;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600857 if (sbdev->dev.driver) {
858 sbdrv = to_slim_driver(sbdev->dev.driver);
859 if (sbdrv->device_up)
860 queue_work(ctrl->wq, &sbdev->wd);
861 }
862 break;
863 }
864 }
865 mutex_unlock(&ctrl->m_ctrl);
866 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867}
868EXPORT_SYMBOL_GPL(slim_assign_laddr);
869
870/*
871 * slim_get_logical_addr: Return the logical address of a slimbus device.
872 * @sb: client handle requesting the adddress.
873 * @e_addr: Elemental address of the device.
874 * @e_len: Length of e_addr
875 * @laddr: output buffer to store the address
876 * context: can sleep
877 * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
878 * the device with this elemental address is not found.
879 */
880int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
881 u8 e_len, u8 *laddr)
882{
883 int ret = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600884 u8 entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885 struct slim_controller *ctrl = sb->ctrl;
886 if (!ctrl || !laddr || !e_addr || e_len != 6)
887 return -EINVAL;
888 mutex_lock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600889 ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
890 if (!ret)
891 *laddr = ctrl->addrt[entry].laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600893 if (ret == -ENXIO && ctrl->get_laddr) {
894 ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
895 if (!ret)
896 ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
897 true);
898 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 return ret;
900}
901EXPORT_SYMBOL_GPL(slim_get_logical_addr);
902
903static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
904 u8 *rbuf, const u8 *wbuf, u8 len)
905{
906 if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
907 return -EINVAL;
908 switch (oper) {
909 case SLIM_MSG_MC_REQUEST_VALUE:
910 case SLIM_MSG_MC_REQUEST_INFORMATION:
911 if (rbuf == NULL)
912 return -EINVAL;
913 return 0;
914 case SLIM_MSG_MC_CHANGE_VALUE:
915 case SLIM_MSG_MC_CLEAR_INFORMATION:
916 if (wbuf == NULL)
917 return -EINVAL;
918 return 0;
919 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
920 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
921 if (rbuf == NULL || wbuf == NULL)
922 return -EINVAL;
923 return 0;
924 default:
925 return -EINVAL;
926 }
927}
928
929static u16 slim_slicecodefromsize(u32 req)
930{
931 u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
932 if (req >= 8)
933 return 0;
934 else
935 return codetosize[req];
936}
937
938static u16 slim_slicesize(u32 code)
939{
940 u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
941 if (code == 0)
942 code = 1;
943 if (code > 16)
944 code = 16;
945 return sizetocode[code - 1];
946}
947
948
949/* Message APIs Unicast message APIs used by slimbus slave drivers */
950
951/*
952 * Message API access routines.
953 * @sb: client handle requesting elemental message reads, writes.
954 * @msg: Input structure for start-offset, number of bytes to read.
955 * @rbuf: data buffer to be filled with values read.
956 * @len: data buffer size
957 * @wbuf: data buffer containing value/information to be written
958 * context: can sleep
959 * Returns:
960 * -EINVAL: Invalid parameters
961 * -ETIMEDOUT: If controller could not complete the request. This may happen if
962 * the bus lines are not clocked, controller is not powered-on, slave with
963 * given address is not enumerated/responding.
964 */
965int slim_request_val_element(struct slim_device *sb,
966 struct slim_ele_access *msg, u8 *buf, u8 len)
967{
968 struct slim_controller *ctrl = sb->ctrl;
969 if (!ctrl)
970 return -EINVAL;
971 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_VALUE, buf,
972 NULL, len);
973}
974EXPORT_SYMBOL_GPL(slim_request_val_element);
975
976int slim_request_inf_element(struct slim_device *sb,
977 struct slim_ele_access *msg, u8 *buf, u8 len)
978{
979 struct slim_controller *ctrl = sb->ctrl;
980 if (!ctrl)
981 return -EINVAL;
982 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_INFORMATION,
983 buf, NULL, len);
984}
985EXPORT_SYMBOL_GPL(slim_request_inf_element);
986
987int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
988 const u8 *buf, u8 len)
989{
990 struct slim_controller *ctrl = sb->ctrl;
991 if (!ctrl)
992 return -EINVAL;
993 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CHANGE_VALUE, NULL, buf,
994 len);
995}
996EXPORT_SYMBOL_GPL(slim_change_val_element);
997
998int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
999 u8 *buf, u8 len)
1000{
1001 struct slim_controller *ctrl = sb->ctrl;
1002 if (!ctrl)
1003 return -EINVAL;
1004 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
1005 buf, len);
1006}
1007EXPORT_SYMBOL_GPL(slim_clear_inf_element);
1008
1009int slim_request_change_val_element(struct slim_device *sb,
1010 struct slim_ele_access *msg, u8 *rbuf,
1011 const u8 *wbuf, u8 len)
1012{
1013 struct slim_controller *ctrl = sb->ctrl;
1014 if (!ctrl)
1015 return -EINVAL;
1016 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
1017 rbuf, wbuf, len);
1018}
1019EXPORT_SYMBOL_GPL(slim_request_change_val_element);
1020
1021int slim_request_clear_inf_element(struct slim_device *sb,
1022 struct slim_ele_access *msg, u8 *rbuf,
1023 const u8 *wbuf, u8 len)
1024{
1025 struct slim_controller *ctrl = sb->ctrl;
1026 if (!ctrl)
1027 return -EINVAL;
1028 return slim_xfer_msg(ctrl, sb, msg,
1029 SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
1030 rbuf, wbuf, len);
1031}
1032EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
1033
1034/*
1035 * Broadcast message API:
1036 * call this API directly with sbdev = NULL.
1037 * For broadcast reads, make sure that buffers are big-enough to incorporate
1038 * replies from all logical addresses.
1039 * All controllers may not support broadcast
1040 */
1041int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001042 struct slim_ele_access *msg, u16 mc, u8 *rbuf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 const u8 *wbuf, u8 len)
1044{
1045 DECLARE_COMPLETION_ONSTACK(complete);
1046 int ret;
1047 u16 sl, cur;
1048 u16 ec;
1049 u8 tid, mlen = 6;
1050
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
1052 if (ret)
1053 goto xfer_err;
1054
1055 sl = slim_slicesize(len);
1056 dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
1057 msg->start_offset, len, mc, sl);
1058
1059 cur = slim_slicecodefromsize(sl);
1060 ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
1061
1062 if (wbuf)
1063 mlen += len;
1064 if (rbuf) {
1065 mlen++;
1066 if (!msg->comp)
1067 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1068 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1069 &complete, sbdev->laddr, &tid);
1070 else
1071 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1072 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1073 msg->comp, sbdev->laddr, &tid);
1074 /* sync read */
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001075 if (!ret && !msg->comp) {
1076 ret = wait_for_completion_timeout(&complete, HZ);
1077 if (!ret) {
1078 struct slim_msg_txn *txn;
1079 dev_err(&ctrl->dev, "slimbus Read timed out");
1080 mutex_lock(&ctrl->m_ctrl);
1081 txn = ctrl->txnt[tid];
1082 /* Invalidate the transaction */
1083 ctrl->txnt[tid] = NULL;
1084 mutex_unlock(&ctrl->m_ctrl);
1085 kfree(txn);
1086 ret = -ETIMEDOUT;
1087 } else
1088 ret = 0;
Sagar Dharia53a9f792012-09-04 19:56:18 -06001089 } else if (ret < 0 && !msg->comp) {
1090 struct slim_msg_txn *txn;
1091 dev_err(&ctrl->dev, "slimbus Read error");
1092 mutex_lock(&ctrl->m_ctrl);
1093 txn = ctrl->txnt[tid];
1094 /* Invalidate the transaction */
1095 ctrl->txnt[tid] = NULL;
1096 mutex_unlock(&ctrl->m_ctrl);
1097 kfree(txn);
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001098 }
1099
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 } else
1101 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
1102 SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1103 NULL, sbdev->laddr, NULL);
1104xfer_err:
1105 return ret;
1106}
1107EXPORT_SYMBOL_GPL(slim_xfer_msg);
1108
1109/*
1110 * slim_alloc_mgrports: Allocate port on manager side.
1111 * @sb: device/client handle.
1112 * @req: Port request type.
1113 * @nports: Number of ports requested
1114 * @rh: output buffer to store the port handles
1115 * @hsz: size of buffer storing handles
1116 * context: can sleep
1117 * This port will be typically used by SW. e.g. client driver wants to receive
1118 * some data from audio codec HW using a data channel.
1119 * Port allocated using this API will be used to receive the data.
1120 * If half-duplex ports are requested, two adjacent ports are allocated for
1121 * 1 half-duplex port. So the handle-buffer size should be twice the number
1122 * of half-duplex ports to be allocated.
1123 * -EDQUOT is returned if all ports are in use.
1124 */
1125int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1126 int nports, u32 *rh, int hsz)
1127{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001128 int i, j;
1129 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130 int nphysp = nports;
1131 struct slim_controller *ctrl = sb->ctrl;
1132
1133 if (!rh || !ctrl)
1134 return -EINVAL;
1135 if (req == SLIM_REQ_HALF_DUP)
1136 nphysp *= 2;
1137 if (hsz/sizeof(u32) < nphysp)
1138 return -EINVAL;
1139 mutex_lock(&ctrl->m_ctrl);
1140
1141 for (i = 0; i < ctrl->nports; i++) {
1142 bool multiok = true;
1143 if (ctrl->ports[i].state != SLIM_P_FREE)
1144 continue;
1145 /* Start half duplex channel at even port */
1146 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1147 continue;
1148 /* Allocate ports contiguously for multi-ch */
1149 if (ctrl->nports < (i + nphysp)) {
1150 i = ctrl->nports;
1151 break;
1152 }
1153 if (req == SLIM_REQ_MULTI_CH) {
1154 multiok = true;
1155 for (j = i; j < i + nphysp; j++) {
1156 if (ctrl->ports[j].state != SLIM_P_FREE) {
1157 multiok = false;
1158 break;
1159 }
1160 }
1161 if (!multiok)
1162 continue;
1163 }
1164 break;
1165 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001166 if (i >= ctrl->nports) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 ret = -EDQUOT;
Sagar Dharia100e7212013-05-17 18:20:57 -06001168 goto alloc_err;
1169 }
1170 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 for (j = i; j < i + nphysp; j++) {
1172 ctrl->ports[j].state = SLIM_P_UNCFG;
1173 ctrl->ports[j].req = req;
1174 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1175 ctrl->ports[j].flow = SLIM_SINK;
1176 else
1177 ctrl->ports[j].flow = SLIM_SRC;
Sagar Dharia100e7212013-05-17 18:20:57 -06001178 if (ctrl->alloc_port)
1179 ret = ctrl->alloc_port(ctrl, j);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 if (ret) {
1181 for (; j >= i; j--)
1182 ctrl->ports[j].state = SLIM_P_FREE;
1183 goto alloc_err;
1184 }
1185 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1186 }
1187alloc_err:
1188 mutex_unlock(&ctrl->m_ctrl);
1189 return ret;
1190}
1191EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1192
1193/* Deallocate the port(s) allocated using the API above */
1194int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1195{
1196 int i;
1197 struct slim_controller *ctrl = sb->ctrl;
1198
1199 if (!ctrl || !hdl)
1200 return -EINVAL;
1201
1202 mutex_lock(&ctrl->m_ctrl);
1203
1204 for (i = 0; i < nports; i++) {
1205 u8 pn;
1206 pn = SLIM_HDL_TO_PORT(hdl[i]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001207
1208 if (pn >= ctrl->nports || ctrl->ports[pn].state == SLIM_P_CFG) {
1209 int j, ret;
1210 if (pn >= ctrl->nports) {
1211 dev_err(&ctrl->dev, "invalid port number");
1212 ret = -EINVAL;
1213 } else {
1214 dev_err(&ctrl->dev,
1215 "Can't dealloc connected port:%d", i);
1216 ret = -EISCONN;
1217 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 for (j = i - 1; j >= 0; j--) {
1219 pn = SLIM_HDL_TO_PORT(hdl[j]);
1220 ctrl->ports[pn].state = SLIM_P_UNCFG;
1221 }
1222 mutex_unlock(&ctrl->m_ctrl);
Sagar Dharia100e7212013-05-17 18:20:57 -06001223 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001225 if (ctrl->dealloc_port)
1226 ctrl->dealloc_port(ctrl, pn);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 ctrl->ports[pn].state = SLIM_P_FREE;
1228 }
1229 mutex_unlock(&ctrl->m_ctrl);
1230 return 0;
1231}
1232EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1233
1234/*
1235 * slim_get_slaveport: Get slave port handle
1236 * @la: slave device logical address.
1237 * @idx: port index at slave
1238 * @rh: return handle
1239 * @flw: Flow type (source or destination)
1240 * This API only returns a slave port's representation as expected by slimbus
1241 * driver. This port is not managed by the slimbus driver. Caller is expected
1242 * to have visibility of this port since it's a device-port.
1243 */
1244int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1245{
1246 if (rh == NULL)
1247 return -EINVAL;
1248 *rh = SLIM_PORT_HDL(la, flw, idx);
1249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(slim_get_slaveport);
1252
1253static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1254 enum slim_port_flow flow)
1255{
1256 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001257 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258 u8 buf[2];
1259 u32 la = SLIM_HDL_TO_LA(ph);
1260 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1261
1262 if (flow == SLIM_SRC)
1263 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1264 else
1265 mc = SLIM_MSG_MC_CONNECT_SINK;
1266 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001267 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001268 if (la == SLIM_LA_MANAGER)
1269 ctrl->ports[pn].flow = flow;
1270 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1271 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1272 NULL);
1273 if (!ret && la == SLIM_LA_MANAGER)
1274 ctrl->ports[pn].state = SLIM_P_CFG;
1275 return ret;
1276}
1277
1278static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1279{
1280 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001281 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 u32 la = SLIM_HDL_TO_LA(ph);
1283 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1284
1285 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1286 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1287 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1288 NULL, la, NULL);
1289 if (ret)
1290 return ret;
1291 if (la == SLIM_LA_MANAGER)
1292 ctrl->ports[pn].state = SLIM_P_UNCFG;
1293 return 0;
1294}
1295
1296/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001297 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001299 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001301 * Per slimbus specification, a channel may have 1 source port.
1302 * Channel specified in chanh needs to be allocated first.
1303 * Returns -EALREADY if source is already configured for this channel.
1304 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001305 * Returns -EINVAL if invalid direction is specified for non-manager port,
1306 * or if the manager side port number is out of bounds, or in incorrect state
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001308int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1309{
1310 struct slim_controller *ctrl = sb->ctrl;
1311 int ret;
1312 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1313 struct slim_ich *slc = &ctrl->chans[chan];
1314 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
Sagar Dharia100e7212013-05-17 18:20:57 -06001315 u8 la = SLIM_HDL_TO_LA(srch);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001316
Sagar Dharia100e7212013-05-17 18:20:57 -06001317 /* manager ports don't have direction when they are allocated */
1318 if (la != SLIM_LA_MANAGER && flow != SLIM_SRC)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001319 return -EINVAL;
1320
Sagar Dhariad2959352012-12-01 15:43:01 -07001321 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001322
Sagar Dharia100e7212013-05-17 18:20:57 -06001323 if (la == SLIM_LA_MANAGER) {
1324 u8 pn = SLIM_HDL_TO_PORT(srch);
1325 if (pn >= ctrl->nports ||
1326 ctrl->ports[pn].state != SLIM_P_UNCFG) {
1327 ret = -EINVAL;
1328 goto connect_src_err;
1329 }
1330 }
1331
Sagar Dharia29f35f02011-10-01 20:37:50 -06001332 if (slc->state == SLIM_CH_FREE) {
1333 ret = -ENOTCONN;
1334 goto connect_src_err;
1335 }
1336 /*
1337 * Once channel is removed, its ports can be considered disconnected
1338 * So its ports can be reassigned. Source port is zeroed
1339 * when channel is deallocated.
1340 */
1341 if (slc->srch) {
1342 ret = -EALREADY;
1343 goto connect_src_err;
1344 }
1345
1346 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1347
1348 if (!ret)
1349 slc->srch = srch;
1350
1351connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001352 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001353 return ret;
1354}
1355EXPORT_SYMBOL_GPL(slim_connect_src);
1356
1357/*
1358 * slim_connect_sink: Connect sink port(s) to channel.
1359 * @sb: client handle
1360 * @sinkh: sink handle(s) to be connected to this channel
1361 * @nsink: number of sinks
1362 * @chanh: Channel with which the ports need to be associated with.
1363 * Per slimbus specification, a channel may have multiple sink-ports.
1364 * Channel specified in chanh needs to be allocated first.
1365 * Returns -EALREADY if sink is already configured for this channel.
1366 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001367 * Returns -EINVAL if invalid parameters are passed, or invalid direction is
1368 * specified for non-manager port, or if the manager side port number is out of
1369 * bounds, or in incorrect state
Sagar Dharia29f35f02011-10-01 20:37:50 -06001370 */
1371int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372{
1373 struct slim_controller *ctrl = sb->ctrl;
1374 int j;
1375 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001376 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 struct slim_ich *slc = &ctrl->chans[chan];
1378
Sagar Dharia29f35f02011-10-01 20:37:50 -06001379 if (!sinkh || !nsink)
1380 return -EINVAL;
1381
Sagar Dhariad2959352012-12-01 15:43:01 -07001382 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383
1384 /*
1385 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001386 * So its ports can be reassigned. Sink ports are freed when channel
1387 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001389 if (slc->state == SLIM_CH_FREE) {
1390 ret = -ENOTCONN;
1391 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001393
Sagar Dharia29f35f02011-10-01 20:37:50 -06001394 for (j = 0; j < nsink; j++) {
1395 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001396 u8 la = SLIM_HDL_TO_LA(sinkh[j]);
1397 u8 pn = SLIM_HDL_TO_PORT(sinkh[j]);
1398 if (la != SLIM_LA_MANAGER && flow != SLIM_SINK)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001399 ret = -EINVAL;
Sagar Dharia100e7212013-05-17 18:20:57 -06001400 else if (la == SLIM_LA_MANAGER &&
1401 (pn >= ctrl->nports ||
1402 ctrl->ports[pn].state != SLIM_P_UNCFG))
1403 ret = -EINVAL;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001404 else
1405 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001407 for (j = j - 1; j >= 0; j--)
1408 disconnect_port_ch(ctrl, sinkh[j]);
1409 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 }
1411 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001412
1413 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1414 GFP_KERNEL);
1415 if (!slc->sinkh) {
1416 ret = -ENOMEM;
1417 for (j = 0; j < nsink; j++)
1418 disconnect_port_ch(ctrl, sinkh[j]);
1419 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 }
1421
Sagar Dharia29f35f02011-10-01 20:37:50 -06001422 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1423 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424
Sagar Dharia29f35f02011-10-01 20:37:50 -06001425connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001426 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427 return ret;
1428}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001429EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430
1431/*
1432 * slim_disconnect_ports: Disconnect port(s) from channel
1433 * @sb: client handle
1434 * @ph: ports to be disconnected
1435 * @nph: number of ports.
1436 * Disconnects ports from a channel.
1437 */
1438int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1439{
1440 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001441 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001442
Sagar Dhariad2959352012-12-01 15:43:01 -07001443 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001444
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 for (i = 0; i < nph; i++)
1446 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001447 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1451
1452/*
1453 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1454 * @sb: client handle
1455 * @ph: port-handle
1456 * @iobuf: buffer to be transferred or populated
1457 * @len: buffer size.
1458 * @comp: completion signal to indicate transfer done or error.
1459 * context: can sleep
1460 * Returns number of bytes transferred/received if used synchronously.
1461 * Will return 0 if used asynchronously.
1462 * Client will call slim_port_get_xfer_status to get error and/or number of
1463 * bytes transferred if used asynchronously.
1464 */
1465int slim_port_xfer(struct slim_device *sb, u32 ph, u8 *iobuf, u32 len,
1466 struct completion *comp)
1467{
1468 struct slim_controller *ctrl = sb->ctrl;
1469 u8 pn = SLIM_HDL_TO_PORT(ph);
1470 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1471 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1472}
1473EXPORT_SYMBOL_GPL(slim_port_xfer);
1474
1475/*
1476 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1477 * after completion is done.
1478 * @sb: client handle
1479 * @ph: port-handle
1480 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1481 * @done_len: Number of bytes transferred.
1482 * This can be called when port_xfer complition is signalled.
1483 * The API will return port transfer error (underflow/overflow/disconnect)
1484 * and/or done_len will reflect number of bytes transferred. Note that
1485 * done_len may be valid even if port error (overflow/underflow) has happened.
1486 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1487 * client has not supplied more data to be transferred, done_len will indicate
1488 * number of bytes transferred with underflow error. To avoid frequent underflow
1489 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1490 * channel has data to be transferred even if client is not ready to transfer
1491 * data all the time. done_buf will indicate address of the last buffer
1492 * processed from the multiple transfers.
1493 */
1494enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
1495 u8 **done_buf, u32 *done_len)
1496{
1497 struct slim_controller *ctrl = sb->ctrl;
1498 u8 pn = SLIM_HDL_TO_PORT(ph);
1499 u32 la = SLIM_HDL_TO_LA(ph);
1500 enum slim_port_err err;
1501 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1502 /*
1503 * Framework only has insight into ports managed by ported device
1504 * used by the manager and not slave
1505 */
1506 if (la != SLIM_LA_MANAGER) {
1507 if (done_buf)
1508 *done_buf = NULL;
1509 if (done_len)
1510 *done_len = 0;
1511 return SLIM_P_NOT_OWNED;
1512 }
1513 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1514 if (err == SLIM_P_INPROGRESS)
1515 err = ctrl->ports[pn].err;
1516 return err;
1517}
1518EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1519
1520static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1521{
1522 struct slim_ich **arr;
1523 int i, j;
1524 int *len;
1525 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001526 /* Channel is already active and other end is transmitting data */
1527 if (slc->state >= SLIM_CH_ACTIVE)
1528 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 if (slc->coeff == SLIM_COEFF_1) {
1530 arr = ctrl->sched.chc1;
1531 len = &ctrl->sched.num_cc1;
1532 } else {
1533 arr = ctrl->sched.chc3;
1534 len = &ctrl->sched.num_cc3;
1535 sl *= 3;
1536 }
1537
1538 *len += 1;
1539
1540 /* Insert the channel based on rootexp and seglen */
1541 for (i = 0; i < *len - 1; i++) {
1542 /*
1543 * Primary key: exp low to high.
1544 * Secondary key: seglen: high to low
1545 */
1546 if ((slc->rootexp > arr[i]->rootexp) ||
1547 ((slc->rootexp == arr[i]->rootexp) &&
1548 (slc->seglen < arr[i]->seglen)))
1549 continue;
1550 else
1551 break;
1552 }
1553 for (j = *len - 1; j > i; j--)
1554 arr[j] = arr[j - 1];
1555 arr[i] = slc;
Sagar Dhariad1468b72013-07-16 12:56:22 -06001556 if (!ctrl->allocbw)
1557 ctrl->sched.usedslots += sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558
1559 return;
1560}
1561
1562static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1563{
1564 struct slim_ich **arr;
1565 int i;
1566 u32 la, ph;
1567 int *len;
1568 if (slc->coeff == SLIM_COEFF_1) {
1569 arr = ctrl->sched.chc1;
1570 len = &ctrl->sched.num_cc1;
1571 } else {
1572 arr = ctrl->sched.chc3;
1573 len = &ctrl->sched.num_cc3;
1574 }
1575
1576 for (i = 0; i < *len; i++) {
1577 if (arr[i] == slc)
1578 break;
1579 }
1580 if (i >= *len)
1581 return -EXFULL;
1582 for (; i < *len - 1; i++)
1583 arr[i] = arr[i + 1];
1584 *len -= 1;
1585 arr[*len] = NULL;
1586
1587 slc->state = SLIM_CH_ALLOCATED;
1588 slc->newintr = 0;
1589 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001590 for (i = 0; i < slc->nsink; i++) {
1591 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592 la = SLIM_HDL_TO_LA(ph);
1593 /*
1594 * For ports managed by manager's ported device, no need to send
1595 * disconnect. It is client's responsibility to call disconnect
1596 * on ports owned by the slave device
1597 */
1598 if (la == SLIM_LA_MANAGER)
1599 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1600 }
1601
Sagar Dharia29f35f02011-10-01 20:37:50 -06001602 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603 la = SLIM_HDL_TO_LA(ph);
1604 if (la == SLIM_LA_MANAGER)
1605 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1606
Sagar Dharia29f35f02011-10-01 20:37:50 -06001607 kfree(slc->sinkh);
1608 slc->sinkh = NULL;
1609 slc->srch = 0;
1610 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 return 0;
1612}
1613
1614static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1615{
1616 u32 rate = 0, rate4k = 0, rate11k = 0;
1617 u32 exp = 0;
1618 u32 pr = 0;
1619 bool exact = true;
1620 bool done = false;
1621 enum slim_ch_rate ratefam;
1622
1623 if (prop->prot >= SLIM_PUSH)
1624 return 0;
1625 if (prop->baser == SLIM_RATE_1HZ) {
1626 rate = prop->ratem / 4000;
1627 rate4k = rate;
1628 if (rate * 4000 == prop->ratem)
1629 ratefam = SLIM_RATE_4000HZ;
1630 else {
1631 rate = prop->ratem / 11025;
1632 rate11k = rate;
1633 if (rate * 11025 == prop->ratem)
1634 ratefam = SLIM_RATE_11025HZ;
1635 else
1636 ratefam = SLIM_RATE_1HZ;
1637 }
1638 } else {
1639 ratefam = prop->baser;
1640 rate = prop->ratem;
1641 }
1642 if (ratefam == SLIM_RATE_1HZ) {
1643 exact = false;
1644 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1645 rate = rate4k + 1;
1646 ratefam = SLIM_RATE_4000HZ;
1647 } else {
1648 rate = rate11k + 1;
1649 ratefam = SLIM_RATE_11025HZ;
1650 }
1651 }
1652 /* covert rate to coeff-exp */
1653 while (!done) {
1654 while ((rate & 0x1) != 0x1) {
1655 rate >>= 1;
1656 exp++;
1657 }
1658 if (rate > 3) {
1659 /* roundup if not exact */
1660 rate++;
1661 exact = false;
1662 } else
1663 done = true;
1664 }
1665 if (ratefam == SLIM_RATE_4000HZ) {
1666 if (rate == 1)
1667 pr = 0x10;
1668 else {
1669 pr = 0;
1670 exp++;
1671 }
1672 } else {
1673 pr = 8;
1674 exp++;
1675 }
1676 if (exp <= 7) {
1677 pr |= exp;
1678 if (exact)
1679 pr |= 0x80;
1680 } else
1681 pr = 0;
1682 return pr;
1683}
1684
1685static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1686{
1687 struct slim_controller *ctrl = sb->ctrl;
1688 u32 chrate = 0;
1689 u32 exp = 0;
1690 u32 coeff = 0;
1691 bool exact = true;
1692 bool done = false;
1693 int ret = 0;
1694 struct slim_ich *slc = &ctrl->chans[chan];
1695 struct slim_ch *prop = &slc->prop;
1696
1697 slc->prrate = slim_calc_prrate(ctrl, prop);
1698 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1699 if (prop->baser == SLIM_RATE_4000HZ)
1700 chrate = 4000 * prop->ratem;
1701 else if (prop->baser == SLIM_RATE_11025HZ)
1702 chrate = 11025 * prop->ratem;
1703 else
1704 chrate = prop->ratem;
1705 /* max allowed sample freq = 768 seg/frame */
1706 if (chrate > 3600000)
1707 return -EDQUOT;
1708 if (prop->baser == SLIM_RATE_4000HZ &&
1709 ctrl->a_framer->superfreq == 4000)
1710 coeff = prop->ratem;
1711 else if (prop->baser == SLIM_RATE_11025HZ &&
1712 ctrl->a_framer->superfreq == 3675)
1713 coeff = 3 * prop->ratem;
1714 else {
1715 u32 tempr = 0;
1716 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1717 coeff = tempr / ctrl->a_framer->rootfreq;
1718 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1719 coeff++;
1720 exact = false;
1721 }
1722 }
1723
1724 /* convert coeff to coeff-exponent */
1725 exp = 0;
1726 while (!done) {
1727 while ((coeff & 0x1) != 0x1) {
1728 coeff >>= 1;
1729 exp++;
1730 }
1731 if (coeff > 3) {
1732 coeff++;
1733 exact = false;
1734 } else
1735 done = true;
1736 }
1737 if (prop->prot == SLIM_HARD_ISO && !exact)
1738 return -EPROTONOSUPPORT;
1739 else if (prop->prot == SLIM_AUTO_ISO) {
1740 if (exact)
1741 prop->prot = SLIM_HARD_ISO;
1742 else {
1743 /* Push-Pull not supported for now */
1744 return -EPROTONOSUPPORT;
1745 }
1746 }
1747 slc->rootexp = exp;
1748 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1749 if (prop->prot != SLIM_HARD_ISO)
1750 slc->seglen++;
1751 if (prop->prot >= SLIM_EXT_SMPLX)
1752 slc->seglen++;
1753 /* convert coeff to enum */
1754 if (coeff == 1) {
1755 if (exp > 9)
1756 ret = -EIO;
1757 coeff = SLIM_COEFF_1;
1758 } else {
1759 if (exp > 8)
1760 ret = -EIO;
1761 coeff = SLIM_COEFF_3;
1762 }
1763 slc->coeff = coeff;
1764
1765 return ret;
1766}
1767
1768/*
1769 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1770 * @sb: client handle.
1771 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001772 * Slimbus channels are limited to 256 per specification.
1773 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774 * Although slimbus specification supports 256 channels, a controller may not
1775 * support that many channels.
1776 */
1777int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1778{
1779 struct slim_controller *ctrl = sb->ctrl;
1780 u16 i;
1781
1782 if (!ctrl)
1783 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001784 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785 for (i = 0; i < ctrl->nchans; i++) {
1786 if (ctrl->chans[i].state == SLIM_CH_FREE)
1787 break;
1788 }
1789 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001790 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791 return -EXFULL;
1792 }
1793 *chanh = i;
1794 ctrl->chans[i].nextgrp = 0;
1795 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001796 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797
Sagar Dhariad2959352012-12-01 15:43:01 -07001798 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799 return 0;
1800}
1801EXPORT_SYMBOL_GPL(slim_alloc_ch);
1802
1803/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001804 * slim_query_ch: Get reference-counted handle for a channel number. Every
1805 * channel is reference counted by upto one as producer and the others as
1806 * consumer)
1807 * @sb: client handle
1808 * @chan: slimbus channel number
1809 * @chanh: return channel handle
1810 * If request channel number is not in use, it is allocated, and reference
1811 * count is set to one. If the channel was was already allocated, this API
1812 * will return handle to that channel and reference count is incremented.
1813 * -EXFULL is returned if all channels are in use
1814 */
1815int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1816{
1817 struct slim_controller *ctrl = sb->ctrl;
1818 u16 i, j;
1819 int ret = 0;
1820 if (!ctrl || !chanh)
1821 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001822 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001823 /* start with modulo number */
1824 i = ch % ctrl->nchans;
1825
1826 for (j = 0; j < ctrl->nchans; j++) {
1827 if (ctrl->chans[i].chan == ch) {
1828 *chanh = i;
1829 ctrl->chans[i].ref++;
1830 if (ctrl->chans[i].state == SLIM_CH_FREE)
1831 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1832 goto query_out;
1833 }
1834 i = (i + 1) % ctrl->nchans;
1835 }
1836
1837 /* Channel not in table yet */
1838 ret = -EXFULL;
1839 for (j = 0; j < ctrl->nchans; j++) {
1840 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1841 ctrl->chans[i].state =
1842 SLIM_CH_ALLOCATED;
1843 *chanh = i;
1844 ctrl->chans[i].ref++;
1845 ctrl->chans[i].chan = ch;
1846 ctrl->chans[i].nextgrp = 0;
1847 ret = 0;
1848 break;
1849 }
1850 i = (i + 1) % ctrl->nchans;
1851 }
1852query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001853 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001854 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1855 ch, i, ctrl->chans[i].ref, ret);
1856 return ret;
1857}
1858EXPORT_SYMBOL_GPL(slim_query_ch);
1859
1860/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 * slim_dealloc_ch: Deallocate channel allocated using the API above
1862 * -EISCONN is returned if the channel is tried to be deallocated without
1863 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001864 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1865 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 */
1867int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1868{
1869 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001870 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871 struct slim_ich *slc = &ctrl->chans[chan];
1872 if (!ctrl)
1873 return -EINVAL;
1874
Sagar Dhariad2959352012-12-01 15:43:01 -07001875 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001876 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001877 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001878 return -ENOTCONN;
1879 }
1880 if (slc->ref > 1) {
1881 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001882 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001883 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1884 slc->chan, chanh, slc->ref);
1885 return 0;
1886 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1888 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001889 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 return -EISCONN;
1891 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001892 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001894 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001895 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1896 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 return 0;
1898}
1899EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1900
1901/*
1902 * slim_get_ch_state: Channel state.
1903 * This API returns the channel's state (active, suspended, inactive etc)
1904 */
1905enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1906{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001907 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 struct slim_ich *slc = &sb->ctrl->chans[chan];
1909 return slc->state;
1910}
1911EXPORT_SYMBOL_GPL(slim_get_ch_state);
1912
1913/*
1914 * slim_define_ch: Define a channel.This API defines channel parameters for a
1915 * given channel.
1916 * @sb: client handle.
1917 * @prop: slim_ch structure with channel parameters desired to be used.
1918 * @chanh: list of channels to be defined.
1919 * @nchan: number of channels in a group (1 if grp is false)
1920 * @grp: Are the channels grouped
1921 * @grph: return group handle if grouping of channels is desired.
1922 * Channels can be grouped if multiple channels use same parameters
1923 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1924 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001925 * -EISCONN is returned if channel is already used with different parameters.
1926 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001927 */
1928int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1929 u8 nchan, bool grp, u16 *grph)
1930{
1931 struct slim_controller *ctrl = sb->ctrl;
1932 int i, ret = 0;
1933
1934 if (!ctrl || !chanh || !prop || !nchan)
1935 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001936 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001938 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001939 struct slim_ich *slc = &ctrl->chans[chan];
1940 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001942 if (slc->state < SLIM_CH_ALLOCATED) {
1943 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 goto err_define_ch;
1945 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001946 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1947 if (prop->ratem != slc->prop.ratem ||
1948 prop->sampleszbits != slc->prop.sampleszbits ||
1949 prop->baser != slc->prop.baser) {
1950 ret = -EISCONN;
1951 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001953 } else if (slc->state > SLIM_CH_DEFINED) {
1954 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001956 } else {
1957 ctrl->chans[chan].prop = *prop;
1958 ret = slim_nextdefine_ch(sb, chan);
1959 if (ret)
1960 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 }
1962 if (i < (nchan - 1))
1963 ctrl->chans[chan].nextgrp = chanh[i + 1];
1964 if (i == 0)
1965 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1966 if (i == (nchan - 1))
1967 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001968 }
1969
1970 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001971 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001972 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001973 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001974 struct slim_ich *slc = &ctrl->chans[chan];
1975 if (slc->state == SLIM_CH_ALLOCATED)
1976 slc->state = SLIM_CH_DEFINED;
1977 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001979 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07001980 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981 return ret;
1982}
1983EXPORT_SYMBOL_GPL(slim_define_ch);
1984
1985static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
1986{
1987 u32 code = 0;
1988 if (*ctrlw == *subfrml) {
1989 *ctrlw = 8;
1990 *subfrml = 8;
1991 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
1992 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
1993 return 0;
1994 }
1995 if (*subfrml == 6) {
1996 code = 0;
1997 *msgsl = 256;
1998 } else if (*subfrml == 8) {
1999 code = 1;
2000 *msgsl = 192;
2001 } else if (*subfrml == 24) {
2002 code = 2;
2003 *msgsl = 64;
2004 } else { /* 32 */
2005 code = 3;
2006 *msgsl = 48;
2007 }
2008
2009 if (*ctrlw < 8) {
2010 if (*ctrlw >= 6) {
2011 *ctrlw = 6;
2012 code |= 0x14;
2013 } else {
2014 if (*ctrlw == 5)
2015 *ctrlw = 4;
2016 code |= (*ctrlw << 2);
2017 }
2018 } else {
2019 code -= 2;
2020 if (*ctrlw >= 24) {
2021 *ctrlw = 24;
2022 code |= 0x1e;
2023 } else if (*ctrlw >= 16) {
2024 *ctrlw = 16;
2025 code |= 0x1c;
2026 } else if (*ctrlw >= 12) {
2027 *ctrlw = 12;
2028 code |= 0x1a;
2029 } else {
2030 *ctrlw = 8;
2031 code |= 0x18;
2032 }
2033 }
2034
2035 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2036 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2037 return code;
2038}
2039
2040static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
2041 int sz, u32 shft)
2042{
2043 int i;
2044 u32 oldoff;
2045 for (i = 0; i < sz; i++) {
2046 struct slim_ich *slc;
2047 if (ach[i] == NULL)
2048 continue;
2049 slc = ach[i];
2050 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2051 continue;
2052 oldoff = slc->newoff;
2053 slc->newoff += shft;
2054 /* seg. offset must be <= interval */
2055 if (slc->newoff >= slc->newintr)
2056 slc->newoff -= slc->newintr;
2057 }
2058}
2059
2060static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
2061 u32 *ctrlw, u32 *subfrml)
2062{
2063 int coeff1, coeff3;
2064 enum slim_ch_coeff bias;
2065 struct slim_controller *ctrl = sb->ctrl;
2066 int last1 = ctrl->sched.num_cc1 - 1;
2067 int last3 = ctrl->sched.num_cc3 - 1;
2068
2069 /*
2070 * Find first channels with coeff 1 & 3 as starting points for
2071 * scheduling
2072 */
2073 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
2074 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
2075 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2076 continue;
2077 else
2078 break;
2079 }
2080 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
2081 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
2082 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2083 continue;
2084 else
2085 break;
2086 }
2087 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
2088 *ctrlw = 8;
2089 *subfrml = 8;
2090 return 0;
2091 } else if (coeff3 == ctrl->sched.num_cc3)
2092 bias = SLIM_COEFF_1;
2093 else
2094 bias = SLIM_COEFF_3;
2095
2096 /*
2097 * Find last chan in coeff1, 3 list, we will use to know when we
2098 * have done scheduling all coeff1 channels
2099 */
2100 while (last1 >= 0) {
2101 if (ctrl->sched.chc1[last1] != NULL &&
2102 (ctrl->sched.chc1[last1])->state !=
2103 SLIM_CH_PENDING_REMOVAL)
2104 break;
2105 last1--;
2106 }
2107 while (last3 >= 0) {
2108 if (ctrl->sched.chc3[last3] != NULL &&
2109 (ctrl->sched.chc3[last3])->state !=
2110 SLIM_CH_PENDING_REMOVAL)
2111 break;
2112 last3--;
2113 }
2114
2115 if (bias == SLIM_COEFF_1) {
2116 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
2117 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2118 int curexp, finalexp;
2119 u32 curintr, curmaxsl;
2120 int opensl1[2];
2121 int maxctrlw1;
2122
2123 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2124 curexp = (int)expshft - 1;
2125
2126 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2127 curmaxsl = curintr >> 1;
2128 opensl1[0] = opensl1[1] = curmaxsl;
2129
2130 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2131 curintr >>= 1;
2132 curmaxsl >>= 1;
2133
2134 /* update 4K family open slot records */
2135 if (opensl1[1] < opensl1[0])
2136 opensl1[1] -= curmaxsl;
2137 else
2138 opensl1[1] = opensl1[0] - curmaxsl;
2139 opensl1[0] = curmaxsl;
2140 if (opensl1[1] < 0) {
2141 opensl1[0] += opensl1[1];
2142 opensl1[1] = 0;
2143 }
2144 if (opensl1[0] <= 0) {
2145 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2146 __LINE__);
2147 return -EXFULL;
2148 }
2149 curexp++;
2150 /* schedule 4k family channels */
2151
2152 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2153 (int)(slc1->rootexp + expshft))) {
2154 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2155 coeff1++;
2156 slc1 = ctrl->sched.chc1[coeff1];
2157 continue;
2158 }
2159 if (opensl1[1] >= opensl1[0] ||
2160 (finalexp == (int)slc1->rootexp &&
2161 curintr <= 24 &&
2162 opensl1[0] == curmaxsl)) {
2163 opensl1[1] -= slc1->seglen;
2164 slc1->newoff = curmaxsl + opensl1[1];
2165 if (opensl1[1] < 0 &&
2166 opensl1[0] == curmaxsl) {
2167 opensl1[0] += opensl1[1];
2168 opensl1[1] = 0;
2169 if (opensl1[0] < 0) {
2170 dev_dbg(&ctrl->dev,
2171 "reconfig failed:%d\n",
2172 __LINE__);
2173 return -EXFULL;
2174 }
2175 }
2176 } else {
2177 if (slc1->seglen > opensl1[0]) {
2178 dev_dbg(&ctrl->dev,
2179 "reconfig failed:%d\n",
2180 __LINE__);
2181 return -EXFULL;
2182 }
2183 slc1->newoff = opensl1[0] -
2184 slc1->seglen;
2185 opensl1[0] = slc1->newoff;
2186 }
2187 slc1->newintr = curintr;
2188 coeff1++;
2189 slc1 = ctrl->sched.chc1[coeff1];
2190 }
2191 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002192 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002193 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002194 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 if (opensl1[1] > opensl1[0]) {
2196 int temp = opensl1[0];
2197 opensl1[0] = opensl1[1];
2198 opensl1[1] = temp;
2199 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2200 ctrl->sched.num_cc1, curmaxsl);
2201 }
2202 /* choose subframe mode to maximize bw */
2203 maxctrlw1 = opensl1[0];
2204 if (opensl1[0] == curmaxsl)
2205 maxctrlw1 += opensl1[1];
2206 if (curintr >= 24) {
2207 *subfrml = 24;
2208 *ctrlw = maxctrlw1;
2209 } else if (curintr == 12) {
2210 if (maxctrlw1 > opensl1[1] * 4) {
2211 *subfrml = 24;
2212 *ctrlw = maxctrlw1;
2213 } else {
2214 *subfrml = 6;
2215 *ctrlw = opensl1[1];
2216 }
2217 } else {
2218 *subfrml = 6;
2219 *ctrlw = maxctrlw1;
2220 }
2221 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002222 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002223 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2224 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2225 int curexp, finalexp, exp1;
2226 u32 curintr, curmaxsl;
2227 int opensl3[2];
2228 int opensl1[6];
2229 bool opensl1valid = false;
2230 int maxctrlw1, maxctrlw3, i;
2231 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2232 if (last1 >= 0) {
2233 slc1 = ctrl->sched.chc1[coeff1];
2234 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2235 if (exp1 > finalexp)
2236 finalexp = exp1;
2237 }
2238 curexp = (int)expshft - 1;
2239
2240 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2241 curmaxsl = curintr >> 1;
2242 opensl3[0] = opensl3[1] = curmaxsl;
2243
2244 while (coeff1 < ctrl->sched.num_cc1 ||
2245 coeff3 < ctrl->sched.num_cc3 ||
2246 curintr > 32) {
2247 curintr >>= 1;
2248 curmaxsl >>= 1;
2249
2250 /* update 12k family open slot records */
2251 if (opensl3[1] < opensl3[0])
2252 opensl3[1] -= curmaxsl;
2253 else
2254 opensl3[1] = opensl3[0] - curmaxsl;
2255 opensl3[0] = curmaxsl;
2256 if (opensl3[1] < 0) {
2257 opensl3[0] += opensl3[1];
2258 opensl3[1] = 0;
2259 }
2260 if (opensl3[0] <= 0) {
2261 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2262 __LINE__);
2263 return -EXFULL;
2264 }
2265 curexp++;
2266
2267 /* schedule 12k family channels */
2268 while (coeff3 < ctrl->sched.num_cc3 &&
2269 curexp == (int)slc3->rootexp + expshft) {
2270 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2271 coeff3++;
2272 slc3 = ctrl->sched.chc3[coeff3];
2273 continue;
2274 }
2275 opensl1valid = false;
2276 if (opensl3[1] >= opensl3[0] ||
2277 (finalexp == (int)slc3->rootexp &&
2278 curintr <= 32 &&
2279 opensl3[0] == curmaxsl &&
2280 last1 < 0)) {
2281 opensl3[1] -= slc3->seglen;
2282 slc3->newoff = curmaxsl + opensl3[1];
2283 if (opensl3[1] < 0 &&
2284 opensl3[0] == curmaxsl) {
2285 opensl3[0] += opensl3[1];
2286 opensl3[1] = 0;
2287 }
2288 if (opensl3[0] < 0) {
2289 dev_dbg(&ctrl->dev,
2290 "reconfig failed:%d\n",
2291 __LINE__);
2292 return -EXFULL;
2293 }
2294 } else {
2295 if (slc3->seglen > opensl3[0]) {
2296 dev_dbg(&ctrl->dev,
2297 "reconfig failed:%d\n",
2298 __LINE__);
2299 return -EXFULL;
2300 }
2301 slc3->newoff = opensl3[0] -
2302 slc3->seglen;
2303 opensl3[0] = slc3->newoff;
2304 }
2305 slc3->newintr = curintr;
2306 coeff3++;
2307 slc3 = ctrl->sched.chc3[coeff3];
2308 }
2309 /* update 4k openslot records */
2310 if (opensl1valid == false) {
2311 for (i = 0; i < 3; i++) {
2312 opensl1[i * 2] = opensl3[0];
2313 opensl1[(i * 2) + 1] = opensl3[1];
2314 }
2315 } else {
2316 int opensl1p[6];
2317 memcpy(opensl1p, opensl1, sizeof(opensl1));
2318 for (i = 0; i < 3; i++) {
2319 if (opensl1p[i] < opensl1p[i + 3])
2320 opensl1[(i * 2) + 1] =
2321 opensl1p[i];
2322 else
2323 opensl1[(i * 2) + 1] =
2324 opensl1p[i + 3];
2325 }
2326 for (i = 0; i < 3; i++) {
2327 opensl1[(i * 2) + 1] -= curmaxsl;
2328 opensl1[i * 2] = curmaxsl;
2329 if (opensl1[(i * 2) + 1] < 0) {
2330 opensl1[i * 2] +=
2331 opensl1[(i * 2) + 1];
2332 opensl1[(i * 2) + 1] = 0;
2333 }
2334 if (opensl1[i * 2] < 0) {
2335 dev_dbg(&ctrl->dev,
2336 "reconfig failed:%d\n",
2337 __LINE__);
2338 return -EXFULL;
2339 }
2340 }
2341 }
2342 /* schedule 4k family channels */
2343 while (coeff1 < ctrl->sched.num_cc1 &&
2344 curexp == (int)slc1->rootexp + expshft) {
2345 /* searchorder effective when opensl valid */
2346 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2347 int maxopensl = 0;
2348 int maxi = 0;
2349 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2350 coeff1++;
2351 slc1 = ctrl->sched.chc1[coeff1];
2352 continue;
2353 }
2354 opensl1valid = true;
2355 for (i = 0; i < 6; i++) {
2356 if (opensl1[srcho[i]] > maxopensl) {
2357 maxopensl = opensl1[srcho[i]];
2358 maxi = srcho[i];
2359 }
2360 }
2361 opensl1[maxi] -= slc1->seglen;
2362 slc1->newoff = (curmaxsl * maxi) +
2363 opensl1[maxi];
2364 if (opensl1[maxi] < 0) {
2365 if (((maxi & 1) == 1) &&
2366 (opensl1[maxi - 1] == curmaxsl)) {
2367 opensl1[maxi - 1] +=
2368 opensl1[maxi];
2369 if (opensl3[0] >
2370 opensl1[maxi - 1])
2371 opensl3[0] =
2372 opensl1[maxi - 1];
2373 opensl3[1] = 0;
2374 opensl1[maxi] = 0;
2375 if (opensl1[maxi - 1] < 0) {
2376 dev_dbg(&ctrl->dev,
2377 "reconfig failed:%d\n",
2378 __LINE__);
2379 return -EXFULL;
2380 }
2381 } else {
2382 dev_dbg(&ctrl->dev,
2383 "reconfig failed:%d\n",
2384 __LINE__);
2385 return -EXFULL;
2386 }
2387 } else {
2388 if (opensl3[maxi & 1] > opensl1[maxi])
2389 opensl3[maxi & 1] =
2390 opensl1[maxi];
2391 }
2392 slc1->newintr = curintr * 3;
2393 coeff1++;
2394 slc1 = ctrl->sched.chc1[coeff1];
2395 }
2396 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002397 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002398 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002399 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2401 if (opensl3[1] > opensl3[0]) {
2402 int temp = opensl3[0];
2403 opensl3[0] = opensl3[1];
2404 opensl3[1] = temp;
2405 temp = opensl1[5];
2406 opensl1[5] = opensl1[4];
2407 opensl1[4] = opensl1[3];
2408 opensl1[3] = opensl1[2];
2409 opensl1[2] = opensl1[1];
2410 opensl1[1] = opensl1[0];
2411 opensl1[0] = temp;
2412 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2413 ctrl->sched.num_cc1, curmaxsl);
2414 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2415 ctrl->sched.num_cc3, curmaxsl);
2416 }
2417 /* subframe mode to maximize BW */
2418 maxctrlw3 = opensl3[0];
2419 maxctrlw1 = opensl1[0];
2420 if (opensl3[0] == curmaxsl)
2421 maxctrlw3 += opensl3[1];
2422 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2423 maxctrlw1 += opensl1[i + 1];
2424 if (curintr >= 32) {
2425 *subfrml = 32;
2426 *ctrlw = maxctrlw3;
2427 } else if (curintr == 16) {
2428 if (maxctrlw3 > (opensl3[1] * 4)) {
2429 *subfrml = 32;
2430 *ctrlw = maxctrlw3;
2431 } else {
2432 *subfrml = 8;
2433 *ctrlw = opensl3[1];
2434 }
2435 } else {
2436 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2437 *subfrml = 24;
2438 *ctrlw = maxctrlw1;
2439 } else {
2440 *subfrml = 8;
2441 *ctrlw = maxctrlw3;
2442 }
2443 }
2444 }
2445 return 0;
2446}
2447
2448#ifdef DEBUG
2449static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2450 u32 subfrml, u32 clkgear)
2451{
2452 int sl, i;
2453 int cc1 = 0;
2454 int cc3 = 0;
2455 struct slim_ich *slc = NULL;
2456 if (!ctrl->sched.slots)
2457 return 0;
2458 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2459 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2460 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2461 for (i = 0; i < ctrlw; i++)
2462 ctrl->sched.slots[sl + i] = 33;
2463 }
2464 while (cc1 < ctrl->sched.num_cc1) {
2465 slc = ctrl->sched.chc1[cc1];
2466 if (slc == NULL) {
2467 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2468 cc1);
2469 return -EIO;
2470 }
2471 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2472 (slc - ctrl->chans), slc->newoff,
2473 slc->newintr, slc->seglen);
2474
2475 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2476 for (sl = slc->newoff;
2477 sl < SLIM_SL_PER_SUPERFRAME;
2478 sl += slc->newintr) {
2479 for (i = 0; i < slc->seglen; i++) {
2480 if (ctrl->sched.slots[sl + i])
2481 return -EXFULL;
2482 ctrl->sched.slots[sl + i] = cc1 + 1;
2483 }
2484 }
2485 }
2486 cc1++;
2487 }
2488 while (cc3 < ctrl->sched.num_cc3) {
2489 slc = ctrl->sched.chc3[cc3];
2490 if (slc == NULL) {
2491 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2492 cc3);
2493 return -EIO;
2494 }
2495 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2496 (slc - ctrl->chans), slc->newoff,
2497 slc->newintr, slc->seglen);
2498 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2499 for (sl = slc->newoff;
2500 sl < SLIM_SL_PER_SUPERFRAME;
2501 sl += slc->newintr) {
2502 for (i = 0; i < slc->seglen; i++) {
2503 if (ctrl->sched.slots[sl + i])
2504 return -EXFULL;
2505 ctrl->sched.slots[sl + i] = cc3 + 1;
2506 }
2507 }
2508 }
2509 cc3++;
2510 }
2511
2512 return 0;
2513}
2514#else
2515static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2516 u32 subfrml, u32 clkgear)
2517{
2518 return 0;
2519}
2520#endif
2521
2522static void slim_sort_chan_grp(struct slim_controller *ctrl,
2523 struct slim_ich *slc)
2524{
2525 u8 last = (u8)-1;
2526 u8 second = 0;
2527
2528 for (; last > 0; last--) {
2529 struct slim_ich *slc1 = slc;
2530 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002531 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 slc2 = &ctrl->chans[next];
2533 for (second = 1; second <= last && slc2 &&
2534 (slc2->state == SLIM_CH_ACTIVE ||
2535 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2536 if (slc1->newoff > slc2->newoff) {
2537 u32 temp = slc2->newoff;
2538 slc2->newoff = slc1->newoff;
2539 slc1->newoff = temp;
2540 }
2541 if (slc2->nextgrp & SLIM_END_GRP) {
2542 last = second;
2543 break;
2544 }
2545 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002546 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547 slc2 = &ctrl->chans[next];
2548 }
2549 if (slc2 == NULL)
2550 last = second - 1;
2551 }
2552}
2553
2554
2555static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2556{
2557 u32 msgsl = 0;
2558 u32 ctrlw = 0;
2559 u32 subfrml = 0;
2560 int ret = -EIO;
2561 struct slim_controller *ctrl = sb->ctrl;
2562 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2563 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2564 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2565 *clkgear = SLIM_MAX_CLK_GEAR;
2566
2567 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2568 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2569 ctrl->sched.usedslots,
2570 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002571 /*
2572 * If number of slots are 0, that means channels are inactive.
2573 * It is very likely that the manager will call clock pause very soon.
2574 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2575 * minimum amount of time.
2576 */
2577 if (ctrl->sched.usedslots != 0) {
2578 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2579 *clkgear -= 1;
2580 usedsl *= 2;
2581 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002582 }
2583
2584 /*
2585 * Try scheduling data channels at current clock gear, if all channels
2586 * can be scheduled, or reserved BW can't be satisfied, increase clock
2587 * gear and try again
2588 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002589 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2591
2592 if (ret == 0) {
2593 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002594 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002596 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002597 continue;
2598 else
2599 break;
2600 }
2601 }
2602 if (ret == 0) {
2603 int i;
2604 /* Sort channel-groups */
2605 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2606 struct slim_ich *slc = ctrl->sched.chc1[i];
2607 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2608 continue;
2609 if ((slc->nextgrp & SLIM_START_GRP) &&
2610 !(slc->nextgrp & SLIM_END_GRP)) {
2611 slim_sort_chan_grp(ctrl, slc);
2612 }
2613 }
2614 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2615 struct slim_ich *slc = ctrl->sched.chc3[i];
2616 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2617 continue;
2618 if ((slc->nextgrp & SLIM_START_GRP) &&
2619 !(slc->nextgrp & SLIM_END_GRP)) {
2620 slim_sort_chan_grp(ctrl, slc);
2621 }
2622 }
2623
2624 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2625 }
2626
2627 return ret;
2628}
2629
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002630static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2631{
2632 struct slim_ich **arr;
2633 int len, i;
2634 if (coeff == SLIM_COEFF_1) {
2635 arr = ctrl->sched.chc1;
2636 len = ctrl->sched.num_cc1;
2637 } else {
2638 arr = ctrl->sched.chc3;
2639 len = ctrl->sched.num_cc3;
2640 }
2641 for (i = 0; i < len; i++) {
2642 struct slim_ich *slc = arr[i];
2643 if (slc->state == SLIM_CH_ACTIVE ||
2644 slc->state == SLIM_CH_SUSPENDED)
2645 slc->offset = slc->newoff;
2646 slc->interval = slc->newintr;
2647 }
2648}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649static void slim_chan_changes(struct slim_device *sb, bool revert)
2650{
2651 struct slim_controller *ctrl = sb->ctrl;
2652 while (!list_empty(&sb->mark_define)) {
2653 struct slim_ich *slc;
2654 struct slim_pending_ch *pch =
2655 list_entry(sb->mark_define.next,
2656 struct slim_pending_ch, pending);
2657 slc = &ctrl->chans[pch->chan];
2658 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002659 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2660 u32 sl = slc->seglen << slc->rootexp;
2661 if (slc->coeff == SLIM_COEFF_3)
2662 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002663 if (!ctrl->allocbw)
2664 ctrl->sched.usedslots -= sl;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002665 slim_remove_ch(ctrl, slc);
2666 slc->state = SLIM_CH_DEFINED;
2667 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002670 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002671 }
2672 list_del_init(&pch->pending);
2673 kfree(pch);
2674 }
2675
2676 while (!list_empty(&sb->mark_removal)) {
2677 struct slim_pending_ch *pch =
2678 list_entry(sb->mark_removal.next,
2679 struct slim_pending_ch, pending);
2680 struct slim_ich *slc = &ctrl->chans[pch->chan];
2681 u32 sl = slc->seglen << slc->rootexp;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002682 if (revert || slc->def > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002683 if (slc->coeff == SLIM_COEFF_3)
2684 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002685 if (!ctrl->allocbw)
2686 ctrl->sched.usedslots += sl;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002687 if (revert)
2688 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002689 slc->state = SLIM_CH_ACTIVE;
2690 } else
2691 slim_remove_ch(ctrl, slc);
2692 list_del_init(&pch->pending);
2693 kfree(pch);
2694 }
2695
2696 while (!list_empty(&sb->mark_suspend)) {
2697 struct slim_pending_ch *pch =
2698 list_entry(sb->mark_suspend.next,
2699 struct slim_pending_ch, pending);
2700 struct slim_ich *slc = &ctrl->chans[pch->chan];
2701 if (revert)
2702 slc->state = SLIM_CH_ACTIVE;
2703 list_del_init(&pch->pending);
2704 kfree(pch);
2705 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002706 /* Change already active channel if reconfig succeeded */
2707 if (!revert) {
2708 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2709 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2710 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002711}
2712
2713/*
2714 * slim_reconfigure_now: Request reconfiguration now.
2715 * @sb: client handle
2716 * This API does what commit flag in other scheduling APIs do.
2717 * -EXFULL is returned if there is no space in TDM to reserve the
2718 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2719 * progress.
2720 */
2721int slim_reconfigure_now(struct slim_device *sb)
2722{
2723 u8 i;
2724 u8 wbuf[4];
2725 u32 clkgear, subframe;
2726 u32 curexp;
2727 int ret;
2728 struct slim_controller *ctrl = sb->ctrl;
2729 u32 expshft;
2730 u32 segdist;
2731 struct slim_pending_ch *pch;
2732
Sagar Dharia80a55e12012-08-16 16:43:58 -06002733 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002734 /*
2735 * If there are no pending changes from this client, avoid sending
2736 * the reconfiguration sequence
2737 */
2738 if (sb->pending_msgsl == sb->cur_msgsl &&
2739 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002740 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002741 struct list_head *pos, *next;
2742 list_for_each_safe(pos, next, &sb->mark_removal) {
2743 struct slim_ich *slc;
2744 pch = list_entry(pos, struct slim_pending_ch, pending);
2745 slc = &ctrl->chans[pch->chan];
2746 if (slc->def > 0)
2747 slc->def--;
2748 /* Disconnect source port to free it up */
2749 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2750 slc->srch = 0;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002751 /*
2752 * If controller overrides BW allocation,
2753 * delete this in remove channel itself
2754 */
2755 if (slc->def != 0 && !ctrl->allocbw) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002756 list_del(&pch->pending);
2757 kfree(pch);
2758 }
2759 }
2760 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002761 mutex_unlock(&ctrl->sched.m_reconf);
2762 pr_info("SLIM_CL: skip reconfig sequence");
2763 return 0;
2764 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002765 }
2766
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2768 list_for_each_entry(pch, &sb->mark_define, pending) {
2769 struct slim_ich *slc = &ctrl->chans[pch->chan];
2770 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002771 if (slc->state < SLIM_CH_ACTIVE)
2772 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002773 }
2774
2775 list_for_each_entry(pch, &sb->mark_removal, pending) {
2776 struct slim_ich *slc = &ctrl->chans[pch->chan];
2777 u32 sl = slc->seglen << slc->rootexp;
2778 if (slc->coeff == SLIM_COEFF_3)
2779 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002780 if (!ctrl->allocbw)
2781 ctrl->sched.usedslots -= sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782 slc->state = SLIM_CH_PENDING_REMOVAL;
2783 }
2784 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2785 struct slim_ich *slc = &ctrl->chans[pch->chan];
2786 slc->state = SLIM_CH_SUSPENDED;
2787 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002788
Sagar Dharia4aec9232012-07-24 23:44:26 -06002789 /*
2790 * Controller can override default channel scheduling algorithm.
2791 * (e.g. if controller needs to use fixed channel scheduling based
2792 * on number of channels)
2793 */
2794 if (ctrl->allocbw)
2795 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2796 else
2797 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002798
2799 if (!ret) {
2800 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2801 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2802 NULL, NULL, 0, 3, NULL, 0, NULL);
2803 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2804 }
2805
2806 if (!ret && subframe != ctrl->sched.subfrmcode) {
2807 wbuf[0] = (u8)(subframe & 0xFF);
2808 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2809 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2810 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2811 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2812 (int)wbuf[0], ret);
2813 }
2814 if (!ret && clkgear != ctrl->clkgear) {
2815 wbuf[0] = (u8)(clkgear & 0xFF);
2816 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2817 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2818 NULL, wbuf, 1, 4, NULL, 0, NULL);
2819 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2820 (int)wbuf[0], ret);
2821 }
2822 if (ret)
2823 goto revert_reconfig;
2824
2825 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2826 /* activate/remove channel */
2827 list_for_each_entry(pch, &sb->mark_define, pending) {
2828 struct slim_ich *slc = &ctrl->chans[pch->chan];
2829 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002830 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002831 wbuf[1] = slc->prrate;
2832 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2833 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2834 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2835 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2836 /* Right now, channel link bit is not supported */
2837 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2838 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2839 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2840 NULL, 0, NULL);
2841 if (ret)
2842 goto revert_reconfig;
2843
2844 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2845 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2846 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2847 NULL, 0, NULL);
2848 if (ret)
2849 goto revert_reconfig;
2850 }
2851
2852 list_for_each_entry(pch, &sb->mark_removal, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002853 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002854 dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002855 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2857 SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
2858 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2859 NULL, 0, NULL);
2860 if (ret)
2861 goto revert_reconfig;
2862 }
2863 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002864 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002865 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002866 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002867 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2868 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2869 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2870 NULL, 0, NULL);
2871 if (ret)
2872 goto revert_reconfig;
2873 }
2874
2875 /* Define CC1 channel */
2876 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2877 struct slim_ich *slc = ctrl->sched.chc1[i];
2878 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2879 continue;
2880 curexp = slc->rootexp + expshft;
2881 segdist = (slc->newoff << curexp) & 0x1FF;
2882 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002883 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2884 slc->newintr, slc->interval, segdist);
2885 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2886 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002888 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002889 slc->newintr != slc->interval ||
2890 slc->newoff != slc->offset) {
2891 segdist |= 0x200;
2892 segdist >>= curexp;
2893 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002894 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002895 wbuf[1] = (u8)(segdist & 0xFF);
2896 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2897 (slc->prop.prot << 4);
2898 wbuf[3] = slc->seglen;
2899 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2900 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2901 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2902 7, NULL, 0, NULL);
2903 if (ret)
2904 goto revert_reconfig;
2905 }
2906 }
2907
2908 /* Define CC3 channels */
2909 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2910 struct slim_ich *slc = ctrl->sched.chc3[i];
2911 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2912 continue;
2913 curexp = slc->rootexp + expshft;
2914 segdist = (slc->newoff << curexp) & 0x1FF;
2915 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002916 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2917 slc->newintr, slc->interval, segdist);
2918 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2919 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002920
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002921 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002922 slc->newintr != slc->interval ||
2923 slc->newoff != slc->offset) {
2924 segdist |= 0x200;
2925 segdist >>= curexp;
2926 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002927 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002928 wbuf[1] = (u8)(segdist & 0xFF);
2929 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2930 (slc->prop.prot << 4);
2931 wbuf[3] = (u8)(slc->seglen);
2932 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2933 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2934 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2935 7, NULL, 0, NULL);
2936 if (ret)
2937 goto revert_reconfig;
2938 }
2939 }
2940 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2941 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2942 NULL, 0, 3, NULL, 0, NULL);
2943 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2944 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002945 ctrl->sched.subfrmcode = subframe;
2946 ctrl->clkgear = clkgear;
2947 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2948 sb->cur_msgsl = sb->pending_msgsl;
2949 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002950 mutex_unlock(&ctrl->sched.m_reconf);
2951 return 0;
2952 }
2953
2954revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002955 /* Revert channel changes */
2956 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002957 mutex_unlock(&ctrl->sched.m_reconf);
2958 return ret;
2959}
2960EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2961
2962static int add_pending_ch(struct list_head *listh, u8 chan)
2963{
2964 struct slim_pending_ch *pch;
2965 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2966 if (!pch)
2967 return -ENOMEM;
2968 pch->chan = chan;
2969 list_add_tail(&pch->pending, listh);
2970 return 0;
2971}
2972
2973/*
2974 * slim_control_ch: Channel control API.
2975 * @sb: client handle
2976 * @chanh: group or channel handle to be controlled
2977 * @chctrl: Control command (activate/suspend/remove)
2978 * @commit: flag to indicate whether the control should take effect right-away.
2979 * This API activates, removes or suspends a channel (or group of channels)
2980 * chanh indicates the channel or group handle (returned by the define_ch API).
2981 * Reconfiguration may be time-consuming since it can change all other active
2982 * channel allocations on the bus, change in clock gear used by the slimbus,
2983 * and change in the control space width used for messaging.
2984 * commit makes sure that multiple channels can be activated/deactivated before
2985 * reconfiguration is started.
2986 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
2987 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
2988 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002989 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002990 */
2991int slim_control_ch(struct slim_device *sb, u16 chanh,
2992 enum slim_ch_control chctrl, bool commit)
2993{
2994 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002995 int ret = 0;
2996 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06002997 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06002998 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002999 struct slim_ich *slc = &ctrl->chans[chan];
3000 if (!(slc->nextgrp & SLIM_START_GRP))
3001 return -EINVAL;
3002
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003003 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003004 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303005 struct slim_pending_ch *pch;
3006 u8 add_mark_removal = true;
3007
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003008 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003009 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
3010 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003011 if (slc->state < SLIM_CH_DEFINED) {
3012 ret = -ENOTCONN;
3013 break;
3014 }
3015 if (chctrl == SLIM_CH_SUSPEND) {
3016 ret = add_pending_ch(&sb->mark_suspend, chan);
3017 if (ret)
3018 break;
3019 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003020 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003021 ret = -EISCONN;
3022 break;
3023 }
3024 ret = add_pending_ch(&sb->mark_define, chan);
3025 if (ret)
3026 break;
3027 } else {
3028 if (slc->state < SLIM_CH_ACTIVE) {
3029 ret = -ENOTCONN;
3030 break;
3031 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05303032 /* If channel removal request comes when pending
3033 * in the mark_define, remove it from the define
3034 * list instead of adding it to removal list
3035 */
3036 if (!list_empty(&sb->mark_define)) {
3037 struct list_head *pos, *next;
3038 list_for_each_safe(pos, next,
3039 &sb->mark_define) {
3040 pch = list_entry(pos,
3041 struct slim_pending_ch,
3042 pending);
Kiran Gundabcd74d02013-10-08 16:32:13 +05303043 if (pch->chan == chan) {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303044 list_del(&pch->pending);
3045 kfree(pch);
3046 add_mark_removal = false;
3047 break;
3048 }
3049 }
3050 }
3051 if (add_mark_removal == true) {
3052 ret = add_pending_ch(&sb->mark_removal, chan);
3053 if (ret)
3054 break;
3055 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003056 }
3057
Sagar Dhariab886e042012-10-17 22:41:57 -06003058 nchan++;
3059 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06003060 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06003061 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003062 if (!ret && commit == true)
3063 ret = slim_reconfigure_now(sb);
3064 mutex_unlock(&sb->sldev_reconf);
3065 return ret;
3066}
3067EXPORT_SYMBOL_GPL(slim_control_ch);
3068
3069/*
3070 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
3071 * @sb: client handle
3072 * @bw_bps: message bandwidth in bits per second to be requested
3073 * @commit: indicates whether the reconfiguration needs to be acted upon.
3074 * This API call can be grouped with slim_control_ch API call with only one of
3075 * the APIs specifying the commit flag to avoid reconfiguration being called too
3076 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
3077 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
3078 * is already in progress.
3079 */
3080int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
3081{
3082 struct slim_controller *ctrl = sb->ctrl;
3083 int ret = 0;
3084 int sl;
3085 mutex_lock(&sb->sldev_reconf);
3086 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
3087 sl = SLIM_SL_PER_SUPERFRAME;
3088 else {
3089 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
3090 (ctrl->a_framer->rootfreq/2 - 1)) /
3091 (ctrl->a_framer->rootfreq/2);
3092 }
3093 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
3094 sb->cur_msgsl);
3095 sb->pending_msgsl = sl;
3096 if (commit == true)
3097 ret = slim_reconfigure_now(sb);
3098 mutex_unlock(&sb->sldev_reconf);
3099 return ret;
3100}
3101EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
3102
Sagar Dharia33f34442011-08-08 16:22:03 -06003103/*
3104 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
3105 * paused or woken up out of clock pause
3106 * or woken up from clock pause
3107 * @ctrl: controller requesting bus to be paused or woken up
3108 * @wakeup: Wakeup this controller from clock pause.
3109 * @restart: Restart time value per spec used for clock pause. This value
3110 * isn't used when controller is to be woken up.
3111 * This API executes clock pause reconfiguration sequence if wakeup is false.
3112 * If wakeup is true, controller's wakeup is called
3113 * Slimbus clock is idle and can be disabled by the controller later.
3114 */
3115int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
3116{
3117 int ret = 0;
3118 int i;
3119
3120 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
3121 return -EINVAL;
3122 mutex_lock(&ctrl->m_ctrl);
3123 if (wakeup) {
3124 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
3125 mutex_unlock(&ctrl->m_ctrl);
3126 return 0;
3127 }
3128 wait_for_completion(&ctrl->pause_comp);
3129 /*
3130 * Slimbus framework will call controller wakeup
3131 * Controller should make sure that it sets active framer
3132 * out of clock pause by doing appropriate setting
3133 */
3134 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3135 ret = ctrl->wakeup(ctrl);
Sagar Dharia129c7d82013-08-08 19:35:50 -06003136 /*
3137 * If wakeup fails, make sure that next attempt can succeed.
3138 * Since we already consumed pause_comp, complete it so
3139 * that next wakeup isn't blocked forever
3140 */
Sagar Dharia33f34442011-08-08 16:22:03 -06003141 if (!ret)
3142 ctrl->clk_state = SLIM_CLK_ACTIVE;
Sagar Dharia129c7d82013-08-08 19:35:50 -06003143 else
3144 complete(&ctrl->pause_comp);
Sagar Dharia33f34442011-08-08 16:22:03 -06003145 mutex_unlock(&ctrl->m_ctrl);
3146 return ret;
3147 } else {
3148 switch (ctrl->clk_state) {
3149 case SLIM_CLK_ENTERING_PAUSE:
3150 case SLIM_CLK_PAUSE_FAILED:
3151 /*
3152 * If controller is already trying to enter clock pause,
3153 * let it finish.
3154 * In case of error, retry
3155 * In both cases, previous clock pause has signalled
3156 * completion.
3157 */
3158 wait_for_completion(&ctrl->pause_comp);
3159 /* retry upon failure */
3160 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3161 ctrl->clk_state = SLIM_CLK_ACTIVE;
3162 break;
3163 } else {
3164 mutex_unlock(&ctrl->m_ctrl);
3165 /*
3166 * Signal completion so that wakeup can wait on
3167 * it.
3168 */
3169 complete(&ctrl->pause_comp);
3170 return 0;
3171 }
3172 break;
3173 case SLIM_CLK_PAUSED:
3174 /* already paused */
3175 mutex_unlock(&ctrl->m_ctrl);
3176 return 0;
3177 case SLIM_CLK_ACTIVE:
3178 default:
3179 break;
3180 }
3181 }
3182 /* Pending response for a message */
3183 for (i = 0; i < ctrl->last_tid; i++) {
3184 if (ctrl->txnt[i]) {
3185 ret = -EBUSY;
Sagar Dharia33beca02012-10-22 16:21:46 -06003186 pr_info("slim_clk_pause: txn-rsp for %d pending", i);
Sagar Dharia33f34442011-08-08 16:22:03 -06003187 mutex_unlock(&ctrl->m_ctrl);
3188 return -EBUSY;
3189 }
3190 }
3191 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3192 mutex_unlock(&ctrl->m_ctrl);
3193
3194 mutex_lock(&ctrl->sched.m_reconf);
3195 /* Data channels active */
3196 if (ctrl->sched.usedslots) {
Sagar Dharia33beca02012-10-22 16:21:46 -06003197 pr_info("slim_clk_pause: data channel active");
Sagar Dharia33f34442011-08-08 16:22:03 -06003198 ret = -EBUSY;
3199 goto clk_pause_ret;
3200 }
3201
3202 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003203 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3204 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003205 if (ret)
3206 goto clk_pause_ret;
3207
3208 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003209 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3210 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3211 if (ret)
3212 goto clk_pause_ret;
3213
3214 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3215 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3216 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003217 if (ret)
3218 goto clk_pause_ret;
3219
3220clk_pause_ret:
3221 if (ret)
3222 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3223 else
3224 ctrl->clk_state = SLIM_CLK_PAUSED;
3225 complete(&ctrl->pause_comp);
3226 mutex_unlock(&ctrl->sched.m_reconf);
3227 return ret;
3228}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003229EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003230
3231MODULE_LICENSE("GPL v2");
3232MODULE_VERSION("0.1");
3233MODULE_DESCRIPTION("Slimbus module");
3234MODULE_ALIAS("platform:slimbus");