blob: fecf5ecb28421dfd1cb6aa40e0f70baf6321f052 [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/*
Sagar Dharia1f2e4a82014-01-27 20:55:59 -07001110 * User message:
1111 * slim_user_msg: Send user message that is interpreted by destination device
1112 * @sb: Client handle sending the message
1113 * @la: Destination device for this user message
1114 * @mt: Message Type (Soruce-referred, or Destination-referred)
1115 * @mc: Message Code
1116 * @msg: Message structure (start offset, number of bytes) to be sent
1117 * @buf: data buffer to be sent
1118 * @len: data buffer size in bytes
1119 */
1120int slim_user_msg(struct slim_device *sb, u8 la, u8 mt, u8 mc,
1121 struct slim_ele_access *msg, u8 *buf, u8 len)
1122{
1123 if (!sb || !sb->ctrl || !msg || mt == SLIM_MSG_MT_CORE)
1124 return -EINVAL;
1125 if (!sb->ctrl->xfer_user_msg)
1126 return -EPROTONOSUPPORT;
1127 return sb->ctrl->xfer_user_msg(sb->ctrl, la, mt, mc, msg, buf, len);
1128}
1129EXPORT_SYMBOL(slim_user_msg);
1130
1131/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 * slim_alloc_mgrports: Allocate port on manager side.
1133 * @sb: device/client handle.
1134 * @req: Port request type.
1135 * @nports: Number of ports requested
1136 * @rh: output buffer to store the port handles
1137 * @hsz: size of buffer storing handles
1138 * context: can sleep
1139 * This port will be typically used by SW. e.g. client driver wants to receive
1140 * some data from audio codec HW using a data channel.
1141 * Port allocated using this API will be used to receive the data.
1142 * If half-duplex ports are requested, two adjacent ports are allocated for
1143 * 1 half-duplex port. So the handle-buffer size should be twice the number
1144 * of half-duplex ports to be allocated.
1145 * -EDQUOT is returned if all ports are in use.
1146 */
1147int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1148 int nports, u32 *rh, int hsz)
1149{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001150 int i, j;
1151 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 int nphysp = nports;
1153 struct slim_controller *ctrl = sb->ctrl;
1154
1155 if (!rh || !ctrl)
1156 return -EINVAL;
1157 if (req == SLIM_REQ_HALF_DUP)
1158 nphysp *= 2;
1159 if (hsz/sizeof(u32) < nphysp)
1160 return -EINVAL;
1161 mutex_lock(&ctrl->m_ctrl);
1162
1163 for (i = 0; i < ctrl->nports; i++) {
1164 bool multiok = true;
1165 if (ctrl->ports[i].state != SLIM_P_FREE)
1166 continue;
1167 /* Start half duplex channel at even port */
1168 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1169 continue;
1170 /* Allocate ports contiguously for multi-ch */
1171 if (ctrl->nports < (i + nphysp)) {
1172 i = ctrl->nports;
1173 break;
1174 }
1175 if (req == SLIM_REQ_MULTI_CH) {
1176 multiok = true;
1177 for (j = i; j < i + nphysp; j++) {
1178 if (ctrl->ports[j].state != SLIM_P_FREE) {
1179 multiok = false;
1180 break;
1181 }
1182 }
1183 if (!multiok)
1184 continue;
1185 }
1186 break;
1187 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001188 if (i >= ctrl->nports) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 ret = -EDQUOT;
Sagar Dharia100e7212013-05-17 18:20:57 -06001190 goto alloc_err;
1191 }
1192 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 for (j = i; j < i + nphysp; j++) {
1194 ctrl->ports[j].state = SLIM_P_UNCFG;
1195 ctrl->ports[j].req = req;
1196 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1197 ctrl->ports[j].flow = SLIM_SINK;
1198 else
1199 ctrl->ports[j].flow = SLIM_SRC;
Sagar Dharia100e7212013-05-17 18:20:57 -06001200 if (ctrl->alloc_port)
1201 ret = ctrl->alloc_port(ctrl, j);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 if (ret) {
1203 for (; j >= i; j--)
1204 ctrl->ports[j].state = SLIM_P_FREE;
1205 goto alloc_err;
1206 }
1207 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1208 }
1209alloc_err:
1210 mutex_unlock(&ctrl->m_ctrl);
1211 return ret;
1212}
1213EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1214
1215/* Deallocate the port(s) allocated using the API above */
1216int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1217{
1218 int i;
1219 struct slim_controller *ctrl = sb->ctrl;
1220
1221 if (!ctrl || !hdl)
1222 return -EINVAL;
1223
1224 mutex_lock(&ctrl->m_ctrl);
1225
1226 for (i = 0; i < nports; i++) {
1227 u8 pn;
1228 pn = SLIM_HDL_TO_PORT(hdl[i]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001229
1230 if (pn >= ctrl->nports || ctrl->ports[pn].state == SLIM_P_CFG) {
1231 int j, ret;
1232 if (pn >= ctrl->nports) {
1233 dev_err(&ctrl->dev, "invalid port number");
1234 ret = -EINVAL;
1235 } else {
1236 dev_err(&ctrl->dev,
1237 "Can't dealloc connected port:%d", i);
1238 ret = -EISCONN;
1239 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 for (j = i - 1; j >= 0; j--) {
1241 pn = SLIM_HDL_TO_PORT(hdl[j]);
1242 ctrl->ports[pn].state = SLIM_P_UNCFG;
1243 }
1244 mutex_unlock(&ctrl->m_ctrl);
Sagar Dharia100e7212013-05-17 18:20:57 -06001245 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001247 if (ctrl->dealloc_port)
1248 ctrl->dealloc_port(ctrl, pn);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249 ctrl->ports[pn].state = SLIM_P_FREE;
1250 }
1251 mutex_unlock(&ctrl->m_ctrl);
1252 return 0;
1253}
1254EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1255
1256/*
1257 * slim_get_slaveport: Get slave port handle
1258 * @la: slave device logical address.
1259 * @idx: port index at slave
1260 * @rh: return handle
1261 * @flw: Flow type (source or destination)
1262 * This API only returns a slave port's representation as expected by slimbus
1263 * driver. This port is not managed by the slimbus driver. Caller is expected
1264 * to have visibility of this port since it's a device-port.
1265 */
1266int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1267{
1268 if (rh == NULL)
1269 return -EINVAL;
1270 *rh = SLIM_PORT_HDL(la, flw, idx);
1271 return 0;
1272}
1273EXPORT_SYMBOL_GPL(slim_get_slaveport);
1274
1275static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1276 enum slim_port_flow flow)
1277{
1278 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001279 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280 u8 buf[2];
1281 u32 la = SLIM_HDL_TO_LA(ph);
1282 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1283
1284 if (flow == SLIM_SRC)
1285 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1286 else
1287 mc = SLIM_MSG_MC_CONNECT_SINK;
1288 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001289 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 if (la == SLIM_LA_MANAGER)
1291 ctrl->ports[pn].flow = flow;
1292 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1293 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1294 NULL);
1295 if (!ret && la == SLIM_LA_MANAGER)
1296 ctrl->ports[pn].state = SLIM_P_CFG;
1297 return ret;
1298}
1299
1300static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1301{
1302 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001303 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 u32 la = SLIM_HDL_TO_LA(ph);
1305 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1306
1307 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1308 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1309 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1310 NULL, la, NULL);
1311 if (ret)
1312 return ret;
1313 if (la == SLIM_LA_MANAGER)
1314 ctrl->ports[pn].state = SLIM_P_UNCFG;
1315 return 0;
1316}
1317
1318/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001319 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001321 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001323 * Per slimbus specification, a channel may have 1 source port.
1324 * Channel specified in chanh needs to be allocated first.
1325 * Returns -EALREADY if source is already configured for this channel.
1326 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001327 * Returns -EINVAL if invalid direction is specified for non-manager port,
1328 * or if the manager side port number is out of bounds, or in incorrect state
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001330int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1331{
1332 struct slim_controller *ctrl = sb->ctrl;
1333 int ret;
1334 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1335 struct slim_ich *slc = &ctrl->chans[chan];
1336 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
Sagar Dharia100e7212013-05-17 18:20:57 -06001337 u8 la = SLIM_HDL_TO_LA(srch);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001338
Sagar Dharia100e7212013-05-17 18:20:57 -06001339 /* manager ports don't have direction when they are allocated */
1340 if (la != SLIM_LA_MANAGER && flow != SLIM_SRC)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001341 return -EINVAL;
1342
Sagar Dhariad2959352012-12-01 15:43:01 -07001343 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001344
Sagar Dharia100e7212013-05-17 18:20:57 -06001345 if (la == SLIM_LA_MANAGER) {
1346 u8 pn = SLIM_HDL_TO_PORT(srch);
1347 if (pn >= ctrl->nports ||
1348 ctrl->ports[pn].state != SLIM_P_UNCFG) {
1349 ret = -EINVAL;
1350 goto connect_src_err;
1351 }
1352 }
1353
Sagar Dharia29f35f02011-10-01 20:37:50 -06001354 if (slc->state == SLIM_CH_FREE) {
1355 ret = -ENOTCONN;
1356 goto connect_src_err;
1357 }
1358 /*
1359 * Once channel is removed, its ports can be considered disconnected
1360 * So its ports can be reassigned. Source port is zeroed
1361 * when channel is deallocated.
1362 */
1363 if (slc->srch) {
1364 ret = -EALREADY;
1365 goto connect_src_err;
1366 }
1367
1368 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1369
1370 if (!ret)
1371 slc->srch = srch;
1372
1373connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001374 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001375 return ret;
1376}
1377EXPORT_SYMBOL_GPL(slim_connect_src);
1378
1379/*
1380 * slim_connect_sink: Connect sink port(s) to channel.
1381 * @sb: client handle
1382 * @sinkh: sink handle(s) to be connected to this channel
1383 * @nsink: number of sinks
1384 * @chanh: Channel with which the ports need to be associated with.
1385 * Per slimbus specification, a channel may have multiple sink-ports.
1386 * Channel specified in chanh needs to be allocated first.
1387 * Returns -EALREADY if sink is already configured for this channel.
1388 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001389 * Returns -EINVAL if invalid parameters are passed, or invalid direction is
1390 * specified for non-manager port, or if the manager side port number is out of
1391 * bounds, or in incorrect state
Sagar Dharia29f35f02011-10-01 20:37:50 -06001392 */
1393int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394{
1395 struct slim_controller *ctrl = sb->ctrl;
1396 int j;
1397 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001398 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 struct slim_ich *slc = &ctrl->chans[chan];
1400
Sagar Dharia29f35f02011-10-01 20:37:50 -06001401 if (!sinkh || !nsink)
1402 return -EINVAL;
1403
Sagar Dhariad2959352012-12-01 15:43:01 -07001404 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405
1406 /*
1407 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001408 * So its ports can be reassigned. Sink ports are freed when channel
1409 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001411 if (slc->state == SLIM_CH_FREE) {
1412 ret = -ENOTCONN;
1413 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001415
Sagar Dharia29f35f02011-10-01 20:37:50 -06001416 for (j = 0; j < nsink; j++) {
1417 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001418 u8 la = SLIM_HDL_TO_LA(sinkh[j]);
1419 u8 pn = SLIM_HDL_TO_PORT(sinkh[j]);
1420 if (la != SLIM_LA_MANAGER && flow != SLIM_SINK)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001421 ret = -EINVAL;
Sagar Dharia100e7212013-05-17 18:20:57 -06001422 else if (la == SLIM_LA_MANAGER &&
1423 (pn >= ctrl->nports ||
1424 ctrl->ports[pn].state != SLIM_P_UNCFG))
1425 ret = -EINVAL;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001426 else
1427 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001429 for (j = j - 1; j >= 0; j--)
1430 disconnect_port_ch(ctrl, sinkh[j]);
1431 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 }
1433 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001434
1435 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1436 GFP_KERNEL);
1437 if (!slc->sinkh) {
1438 ret = -ENOMEM;
1439 for (j = 0; j < nsink; j++)
1440 disconnect_port_ch(ctrl, sinkh[j]);
1441 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442 }
1443
Sagar Dharia29f35f02011-10-01 20:37:50 -06001444 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1445 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001446
Sagar Dharia29f35f02011-10-01 20:37:50 -06001447connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001448 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449 return ret;
1450}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001451EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452
1453/*
1454 * slim_disconnect_ports: Disconnect port(s) from channel
1455 * @sb: client handle
1456 * @ph: ports to be disconnected
1457 * @nph: number of ports.
1458 * Disconnects ports from a channel.
1459 */
1460int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1461{
1462 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001463 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001464
Sagar Dhariad2959352012-12-01 15:43:01 -07001465 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 for (i = 0; i < nph; i++)
1468 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001469 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 return 0;
1471}
1472EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1473
1474/*
1475 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1476 * @sb: client handle
1477 * @ph: port-handle
1478 * @iobuf: buffer to be transferred or populated
1479 * @len: buffer size.
1480 * @comp: completion signal to indicate transfer done or error.
1481 * context: can sleep
1482 * Returns number of bytes transferred/received if used synchronously.
1483 * Will return 0 if used asynchronously.
1484 * Client will call slim_port_get_xfer_status to get error and/or number of
1485 * bytes transferred if used asynchronously.
1486 */
Sagar Dharia1bbf38c2014-02-07 19:40:12 -07001487int slim_port_xfer(struct slim_device *sb, u32 ph, phys_addr_t iobuf, u32 len,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488 struct completion *comp)
1489{
1490 struct slim_controller *ctrl = sb->ctrl;
1491 u8 pn = SLIM_HDL_TO_PORT(ph);
1492 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1493 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1494}
1495EXPORT_SYMBOL_GPL(slim_port_xfer);
1496
1497/*
1498 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1499 * after completion is done.
1500 * @sb: client handle
1501 * @ph: port-handle
1502 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1503 * @done_len: Number of bytes transferred.
1504 * This can be called when port_xfer complition is signalled.
1505 * The API will return port transfer error (underflow/overflow/disconnect)
1506 * and/or done_len will reflect number of bytes transferred. Note that
1507 * done_len may be valid even if port error (overflow/underflow) has happened.
1508 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1509 * client has not supplied more data to be transferred, done_len will indicate
1510 * number of bytes transferred with underflow error. To avoid frequent underflow
1511 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1512 * channel has data to be transferred even if client is not ready to transfer
1513 * data all the time. done_buf will indicate address of the last buffer
1514 * processed from the multiple transfers.
1515 */
1516enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
Sagar Dharia1bbf38c2014-02-07 19:40:12 -07001517 phys_addr_t *done_buf, u32 *done_len)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001518{
1519 struct slim_controller *ctrl = sb->ctrl;
1520 u8 pn = SLIM_HDL_TO_PORT(ph);
1521 u32 la = SLIM_HDL_TO_LA(ph);
1522 enum slim_port_err err;
1523 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1524 /*
1525 * Framework only has insight into ports managed by ported device
1526 * used by the manager and not slave
1527 */
1528 if (la != SLIM_LA_MANAGER) {
1529 if (done_buf)
Sagar Dharia1bbf38c2014-02-07 19:40:12 -07001530 *done_buf = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 if (done_len)
1532 *done_len = 0;
1533 return SLIM_P_NOT_OWNED;
1534 }
1535 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1536 if (err == SLIM_P_INPROGRESS)
1537 err = ctrl->ports[pn].err;
1538 return err;
1539}
1540EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1541
1542static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1543{
1544 struct slim_ich **arr;
1545 int i, j;
1546 int *len;
1547 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001548 /* Channel is already active and other end is transmitting data */
1549 if (slc->state >= SLIM_CH_ACTIVE)
1550 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001551 if (slc->coeff == SLIM_COEFF_1) {
1552 arr = ctrl->sched.chc1;
1553 len = &ctrl->sched.num_cc1;
1554 } else {
1555 arr = ctrl->sched.chc3;
1556 len = &ctrl->sched.num_cc3;
1557 sl *= 3;
1558 }
1559
1560 *len += 1;
1561
1562 /* Insert the channel based on rootexp and seglen */
1563 for (i = 0; i < *len - 1; i++) {
1564 /*
1565 * Primary key: exp low to high.
1566 * Secondary key: seglen: high to low
1567 */
1568 if ((slc->rootexp > arr[i]->rootexp) ||
1569 ((slc->rootexp == arr[i]->rootexp) &&
1570 (slc->seglen < arr[i]->seglen)))
1571 continue;
1572 else
1573 break;
1574 }
1575 for (j = *len - 1; j > i; j--)
1576 arr[j] = arr[j - 1];
1577 arr[i] = slc;
Sagar Dhariad1468b72013-07-16 12:56:22 -06001578 if (!ctrl->allocbw)
1579 ctrl->sched.usedslots += sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580
1581 return;
1582}
1583
1584static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1585{
1586 struct slim_ich **arr;
1587 int i;
1588 u32 la, ph;
1589 int *len;
1590 if (slc->coeff == SLIM_COEFF_1) {
1591 arr = ctrl->sched.chc1;
1592 len = &ctrl->sched.num_cc1;
1593 } else {
1594 arr = ctrl->sched.chc3;
1595 len = &ctrl->sched.num_cc3;
1596 }
1597
1598 for (i = 0; i < *len; i++) {
1599 if (arr[i] == slc)
1600 break;
1601 }
1602 if (i >= *len)
1603 return -EXFULL;
1604 for (; i < *len - 1; i++)
1605 arr[i] = arr[i + 1];
1606 *len -= 1;
1607 arr[*len] = NULL;
1608
1609 slc->state = SLIM_CH_ALLOCATED;
1610 slc->newintr = 0;
1611 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001612 for (i = 0; i < slc->nsink; i++) {
1613 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614 la = SLIM_HDL_TO_LA(ph);
1615 /*
1616 * For ports managed by manager's ported device, no need to send
1617 * disconnect. It is client's responsibility to call disconnect
1618 * on ports owned by the slave device
1619 */
1620 if (la == SLIM_LA_MANAGER)
1621 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1622 }
1623
Sagar Dharia29f35f02011-10-01 20:37:50 -06001624 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 la = SLIM_HDL_TO_LA(ph);
1626 if (la == SLIM_LA_MANAGER)
1627 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1628
Sagar Dharia29f35f02011-10-01 20:37:50 -06001629 kfree(slc->sinkh);
1630 slc->sinkh = NULL;
1631 slc->srch = 0;
1632 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633 return 0;
1634}
1635
1636static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1637{
1638 u32 rate = 0, rate4k = 0, rate11k = 0;
1639 u32 exp = 0;
1640 u32 pr = 0;
1641 bool exact = true;
1642 bool done = false;
1643 enum slim_ch_rate ratefam;
1644
1645 if (prop->prot >= SLIM_PUSH)
1646 return 0;
1647 if (prop->baser == SLIM_RATE_1HZ) {
1648 rate = prop->ratem / 4000;
1649 rate4k = rate;
1650 if (rate * 4000 == prop->ratem)
1651 ratefam = SLIM_RATE_4000HZ;
1652 else {
1653 rate = prop->ratem / 11025;
1654 rate11k = rate;
1655 if (rate * 11025 == prop->ratem)
1656 ratefam = SLIM_RATE_11025HZ;
1657 else
1658 ratefam = SLIM_RATE_1HZ;
1659 }
1660 } else {
1661 ratefam = prop->baser;
1662 rate = prop->ratem;
1663 }
1664 if (ratefam == SLIM_RATE_1HZ) {
1665 exact = false;
1666 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1667 rate = rate4k + 1;
1668 ratefam = SLIM_RATE_4000HZ;
1669 } else {
1670 rate = rate11k + 1;
1671 ratefam = SLIM_RATE_11025HZ;
1672 }
1673 }
1674 /* covert rate to coeff-exp */
1675 while (!done) {
1676 while ((rate & 0x1) != 0x1) {
1677 rate >>= 1;
1678 exp++;
1679 }
1680 if (rate > 3) {
1681 /* roundup if not exact */
1682 rate++;
1683 exact = false;
1684 } else
1685 done = true;
1686 }
1687 if (ratefam == SLIM_RATE_4000HZ) {
1688 if (rate == 1)
1689 pr = 0x10;
1690 else {
1691 pr = 0;
1692 exp++;
1693 }
1694 } else {
1695 pr = 8;
1696 exp++;
1697 }
1698 if (exp <= 7) {
1699 pr |= exp;
1700 if (exact)
1701 pr |= 0x80;
1702 } else
1703 pr = 0;
1704 return pr;
1705}
1706
1707static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1708{
1709 struct slim_controller *ctrl = sb->ctrl;
1710 u32 chrate = 0;
1711 u32 exp = 0;
1712 u32 coeff = 0;
1713 bool exact = true;
1714 bool done = false;
1715 int ret = 0;
1716 struct slim_ich *slc = &ctrl->chans[chan];
1717 struct slim_ch *prop = &slc->prop;
1718
1719 slc->prrate = slim_calc_prrate(ctrl, prop);
1720 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1721 if (prop->baser == SLIM_RATE_4000HZ)
1722 chrate = 4000 * prop->ratem;
1723 else if (prop->baser == SLIM_RATE_11025HZ)
1724 chrate = 11025 * prop->ratem;
1725 else
1726 chrate = prop->ratem;
1727 /* max allowed sample freq = 768 seg/frame */
1728 if (chrate > 3600000)
1729 return -EDQUOT;
1730 if (prop->baser == SLIM_RATE_4000HZ &&
1731 ctrl->a_framer->superfreq == 4000)
1732 coeff = prop->ratem;
1733 else if (prop->baser == SLIM_RATE_11025HZ &&
1734 ctrl->a_framer->superfreq == 3675)
1735 coeff = 3 * prop->ratem;
1736 else {
1737 u32 tempr = 0;
1738 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1739 coeff = tempr / ctrl->a_framer->rootfreq;
1740 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1741 coeff++;
1742 exact = false;
1743 }
1744 }
1745
1746 /* convert coeff to coeff-exponent */
1747 exp = 0;
1748 while (!done) {
1749 while ((coeff & 0x1) != 0x1) {
1750 coeff >>= 1;
1751 exp++;
1752 }
1753 if (coeff > 3) {
1754 coeff++;
1755 exact = false;
1756 } else
1757 done = true;
1758 }
1759 if (prop->prot == SLIM_HARD_ISO && !exact)
1760 return -EPROTONOSUPPORT;
1761 else if (prop->prot == SLIM_AUTO_ISO) {
1762 if (exact)
1763 prop->prot = SLIM_HARD_ISO;
1764 else {
1765 /* Push-Pull not supported for now */
1766 return -EPROTONOSUPPORT;
1767 }
1768 }
1769 slc->rootexp = exp;
1770 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1771 if (prop->prot != SLIM_HARD_ISO)
1772 slc->seglen++;
1773 if (prop->prot >= SLIM_EXT_SMPLX)
1774 slc->seglen++;
1775 /* convert coeff to enum */
1776 if (coeff == 1) {
1777 if (exp > 9)
1778 ret = -EIO;
1779 coeff = SLIM_COEFF_1;
1780 } else {
1781 if (exp > 8)
1782 ret = -EIO;
1783 coeff = SLIM_COEFF_3;
1784 }
1785 slc->coeff = coeff;
1786
1787 return ret;
1788}
1789
1790/*
1791 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1792 * @sb: client handle.
1793 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001794 * Slimbus channels are limited to 256 per specification.
1795 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 * Although slimbus specification supports 256 channels, a controller may not
1797 * support that many channels.
1798 */
1799int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1800{
1801 struct slim_controller *ctrl = sb->ctrl;
1802 u16 i;
1803
1804 if (!ctrl)
1805 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001806 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 for (i = 0; i < ctrl->nchans; i++) {
1808 if (ctrl->chans[i].state == SLIM_CH_FREE)
1809 break;
1810 }
1811 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001812 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001813 return -EXFULL;
1814 }
1815 *chanh = i;
1816 ctrl->chans[i].nextgrp = 0;
1817 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001818 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001819
Sagar Dhariad2959352012-12-01 15:43:01 -07001820 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(slim_alloc_ch);
1824
1825/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001826 * slim_query_ch: Get reference-counted handle for a channel number. Every
1827 * channel is reference counted by upto one as producer and the others as
1828 * consumer)
1829 * @sb: client handle
1830 * @chan: slimbus channel number
1831 * @chanh: return channel handle
1832 * If request channel number is not in use, it is allocated, and reference
1833 * count is set to one. If the channel was was already allocated, this API
1834 * will return handle to that channel and reference count is incremented.
1835 * -EXFULL is returned if all channels are in use
1836 */
1837int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1838{
1839 struct slim_controller *ctrl = sb->ctrl;
1840 u16 i, j;
1841 int ret = 0;
1842 if (!ctrl || !chanh)
1843 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001844 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001845 /* start with modulo number */
1846 i = ch % ctrl->nchans;
1847
1848 for (j = 0; j < ctrl->nchans; j++) {
1849 if (ctrl->chans[i].chan == ch) {
1850 *chanh = i;
1851 ctrl->chans[i].ref++;
1852 if (ctrl->chans[i].state == SLIM_CH_FREE)
1853 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1854 goto query_out;
1855 }
1856 i = (i + 1) % ctrl->nchans;
1857 }
1858
1859 /* Channel not in table yet */
1860 ret = -EXFULL;
1861 for (j = 0; j < ctrl->nchans; j++) {
1862 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1863 ctrl->chans[i].state =
1864 SLIM_CH_ALLOCATED;
1865 *chanh = i;
1866 ctrl->chans[i].ref++;
1867 ctrl->chans[i].chan = ch;
1868 ctrl->chans[i].nextgrp = 0;
1869 ret = 0;
1870 break;
1871 }
1872 i = (i + 1) % ctrl->nchans;
1873 }
1874query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001875 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001876 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1877 ch, i, ctrl->chans[i].ref, ret);
1878 return ret;
1879}
1880EXPORT_SYMBOL_GPL(slim_query_ch);
1881
1882/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 * slim_dealloc_ch: Deallocate channel allocated using the API above
1884 * -EISCONN is returned if the channel is tried to be deallocated without
1885 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001886 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1887 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 */
1889int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1890{
1891 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001892 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 struct slim_ich *slc = &ctrl->chans[chan];
1894 if (!ctrl)
1895 return -EINVAL;
1896
Sagar Dhariad2959352012-12-01 15:43:01 -07001897 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001898 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001899 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001900 return -ENOTCONN;
1901 }
1902 if (slc->ref > 1) {
1903 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001904 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001905 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1906 slc->chan, chanh, slc->ref);
1907 return 0;
1908 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001909 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1910 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001911 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001912 return -EISCONN;
1913 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001914 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001915 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001916 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001917 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1918 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 return 0;
1920}
1921EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1922
1923/*
1924 * slim_get_ch_state: Channel state.
1925 * This API returns the channel's state (active, suspended, inactive etc)
1926 */
1927enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1928{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001929 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 struct slim_ich *slc = &sb->ctrl->chans[chan];
1931 return slc->state;
1932}
1933EXPORT_SYMBOL_GPL(slim_get_ch_state);
1934
1935/*
1936 * slim_define_ch: Define a channel.This API defines channel parameters for a
1937 * given channel.
1938 * @sb: client handle.
1939 * @prop: slim_ch structure with channel parameters desired to be used.
1940 * @chanh: list of channels to be defined.
1941 * @nchan: number of channels in a group (1 if grp is false)
1942 * @grp: Are the channels grouped
1943 * @grph: return group handle if grouping of channels is desired.
1944 * Channels can be grouped if multiple channels use same parameters
1945 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1946 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001947 * -EISCONN is returned if channel is already used with different parameters.
1948 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 */
1950int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1951 u8 nchan, bool grp, u16 *grph)
1952{
1953 struct slim_controller *ctrl = sb->ctrl;
1954 int i, ret = 0;
1955
1956 if (!ctrl || !chanh || !prop || !nchan)
1957 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001958 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001960 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001961 struct slim_ich *slc = &ctrl->chans[chan];
1962 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001963 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001964 if (slc->state < SLIM_CH_ALLOCATED) {
1965 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966 goto err_define_ch;
1967 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001968 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1969 if (prop->ratem != slc->prop.ratem ||
1970 prop->sampleszbits != slc->prop.sampleszbits ||
1971 prop->baser != slc->prop.baser) {
1972 ret = -EISCONN;
1973 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001975 } else if (slc->state > SLIM_CH_DEFINED) {
1976 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001978 } else {
1979 ctrl->chans[chan].prop = *prop;
1980 ret = slim_nextdefine_ch(sb, chan);
1981 if (ret)
1982 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 }
1984 if (i < (nchan - 1))
1985 ctrl->chans[chan].nextgrp = chanh[i + 1];
1986 if (i == 0)
1987 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1988 if (i == (nchan - 1))
1989 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001990 }
1991
1992 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001993 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001994 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001995 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001996 struct slim_ich *slc = &ctrl->chans[chan];
1997 if (slc->state == SLIM_CH_ALLOCATED)
1998 slc->state = SLIM_CH_DEFINED;
1999 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002001 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07002002 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 return ret;
2004}
2005EXPORT_SYMBOL_GPL(slim_define_ch);
2006
2007static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
2008{
2009 u32 code = 0;
2010 if (*ctrlw == *subfrml) {
2011 *ctrlw = 8;
2012 *subfrml = 8;
2013 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
2014 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
2015 return 0;
2016 }
2017 if (*subfrml == 6) {
2018 code = 0;
2019 *msgsl = 256;
2020 } else if (*subfrml == 8) {
2021 code = 1;
2022 *msgsl = 192;
2023 } else if (*subfrml == 24) {
2024 code = 2;
2025 *msgsl = 64;
2026 } else { /* 32 */
2027 code = 3;
2028 *msgsl = 48;
2029 }
2030
2031 if (*ctrlw < 8) {
2032 if (*ctrlw >= 6) {
2033 *ctrlw = 6;
2034 code |= 0x14;
2035 } else {
2036 if (*ctrlw == 5)
2037 *ctrlw = 4;
2038 code |= (*ctrlw << 2);
2039 }
2040 } else {
2041 code -= 2;
2042 if (*ctrlw >= 24) {
2043 *ctrlw = 24;
2044 code |= 0x1e;
2045 } else if (*ctrlw >= 16) {
2046 *ctrlw = 16;
2047 code |= 0x1c;
2048 } else if (*ctrlw >= 12) {
2049 *ctrlw = 12;
2050 code |= 0x1a;
2051 } else {
2052 *ctrlw = 8;
2053 code |= 0x18;
2054 }
2055 }
2056
2057 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2058 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2059 return code;
2060}
2061
2062static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
2063 int sz, u32 shft)
2064{
2065 int i;
2066 u32 oldoff;
2067 for (i = 0; i < sz; i++) {
2068 struct slim_ich *slc;
2069 if (ach[i] == NULL)
2070 continue;
2071 slc = ach[i];
2072 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2073 continue;
2074 oldoff = slc->newoff;
2075 slc->newoff += shft;
2076 /* seg. offset must be <= interval */
2077 if (slc->newoff >= slc->newintr)
2078 slc->newoff -= slc->newintr;
2079 }
2080}
2081
2082static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
2083 u32 *ctrlw, u32 *subfrml)
2084{
2085 int coeff1, coeff3;
2086 enum slim_ch_coeff bias;
2087 struct slim_controller *ctrl = sb->ctrl;
2088 int last1 = ctrl->sched.num_cc1 - 1;
2089 int last3 = ctrl->sched.num_cc3 - 1;
2090
2091 /*
2092 * Find first channels with coeff 1 & 3 as starting points for
2093 * scheduling
2094 */
2095 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
2096 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
2097 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2098 continue;
2099 else
2100 break;
2101 }
2102 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
2103 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
2104 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2105 continue;
2106 else
2107 break;
2108 }
2109 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
2110 *ctrlw = 8;
2111 *subfrml = 8;
2112 return 0;
2113 } else if (coeff3 == ctrl->sched.num_cc3)
2114 bias = SLIM_COEFF_1;
2115 else
2116 bias = SLIM_COEFF_3;
2117
2118 /*
2119 * Find last chan in coeff1, 3 list, we will use to know when we
2120 * have done scheduling all coeff1 channels
2121 */
2122 while (last1 >= 0) {
2123 if (ctrl->sched.chc1[last1] != NULL &&
2124 (ctrl->sched.chc1[last1])->state !=
2125 SLIM_CH_PENDING_REMOVAL)
2126 break;
2127 last1--;
2128 }
2129 while (last3 >= 0) {
2130 if (ctrl->sched.chc3[last3] != NULL &&
2131 (ctrl->sched.chc3[last3])->state !=
2132 SLIM_CH_PENDING_REMOVAL)
2133 break;
2134 last3--;
2135 }
2136
2137 if (bias == SLIM_COEFF_1) {
2138 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
2139 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2140 int curexp, finalexp;
2141 u32 curintr, curmaxsl;
2142 int opensl1[2];
2143 int maxctrlw1;
2144
2145 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2146 curexp = (int)expshft - 1;
2147
2148 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2149 curmaxsl = curintr >> 1;
2150 opensl1[0] = opensl1[1] = curmaxsl;
2151
2152 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2153 curintr >>= 1;
2154 curmaxsl >>= 1;
2155
2156 /* update 4K family open slot records */
2157 if (opensl1[1] < opensl1[0])
2158 opensl1[1] -= curmaxsl;
2159 else
2160 opensl1[1] = opensl1[0] - curmaxsl;
2161 opensl1[0] = curmaxsl;
2162 if (opensl1[1] < 0) {
2163 opensl1[0] += opensl1[1];
2164 opensl1[1] = 0;
2165 }
2166 if (opensl1[0] <= 0) {
2167 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2168 __LINE__);
2169 return -EXFULL;
2170 }
2171 curexp++;
2172 /* schedule 4k family channels */
2173
2174 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2175 (int)(slc1->rootexp + expshft))) {
2176 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2177 coeff1++;
2178 slc1 = ctrl->sched.chc1[coeff1];
2179 continue;
2180 }
2181 if (opensl1[1] >= opensl1[0] ||
2182 (finalexp == (int)slc1->rootexp &&
2183 curintr <= 24 &&
2184 opensl1[0] == curmaxsl)) {
2185 opensl1[1] -= slc1->seglen;
2186 slc1->newoff = curmaxsl + opensl1[1];
2187 if (opensl1[1] < 0 &&
2188 opensl1[0] == curmaxsl) {
2189 opensl1[0] += opensl1[1];
2190 opensl1[1] = 0;
2191 if (opensl1[0] < 0) {
2192 dev_dbg(&ctrl->dev,
2193 "reconfig failed:%d\n",
2194 __LINE__);
2195 return -EXFULL;
2196 }
2197 }
2198 } else {
2199 if (slc1->seglen > opensl1[0]) {
2200 dev_dbg(&ctrl->dev,
2201 "reconfig failed:%d\n",
2202 __LINE__);
2203 return -EXFULL;
2204 }
2205 slc1->newoff = opensl1[0] -
2206 slc1->seglen;
2207 opensl1[0] = slc1->newoff;
2208 }
2209 slc1->newintr = curintr;
2210 coeff1++;
2211 slc1 = ctrl->sched.chc1[coeff1];
2212 }
2213 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002214 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002215 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002216 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 if (opensl1[1] > opensl1[0]) {
2218 int temp = opensl1[0];
2219 opensl1[0] = opensl1[1];
2220 opensl1[1] = temp;
2221 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2222 ctrl->sched.num_cc1, curmaxsl);
2223 }
2224 /* choose subframe mode to maximize bw */
2225 maxctrlw1 = opensl1[0];
2226 if (opensl1[0] == curmaxsl)
2227 maxctrlw1 += opensl1[1];
2228 if (curintr >= 24) {
2229 *subfrml = 24;
2230 *ctrlw = maxctrlw1;
2231 } else if (curintr == 12) {
2232 if (maxctrlw1 > opensl1[1] * 4) {
2233 *subfrml = 24;
2234 *ctrlw = maxctrlw1;
2235 } else {
2236 *subfrml = 6;
2237 *ctrlw = opensl1[1];
2238 }
2239 } else {
2240 *subfrml = 6;
2241 *ctrlw = maxctrlw1;
2242 }
2243 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002244 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002245 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2246 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2247 int curexp, finalexp, exp1;
2248 u32 curintr, curmaxsl;
2249 int opensl3[2];
2250 int opensl1[6];
2251 bool opensl1valid = false;
2252 int maxctrlw1, maxctrlw3, i;
2253 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2254 if (last1 >= 0) {
2255 slc1 = ctrl->sched.chc1[coeff1];
2256 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2257 if (exp1 > finalexp)
2258 finalexp = exp1;
2259 }
2260 curexp = (int)expshft - 1;
2261
2262 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2263 curmaxsl = curintr >> 1;
2264 opensl3[0] = opensl3[1] = curmaxsl;
2265
2266 while (coeff1 < ctrl->sched.num_cc1 ||
2267 coeff3 < ctrl->sched.num_cc3 ||
2268 curintr > 32) {
2269 curintr >>= 1;
2270 curmaxsl >>= 1;
2271
2272 /* update 12k family open slot records */
2273 if (opensl3[1] < opensl3[0])
2274 opensl3[1] -= curmaxsl;
2275 else
2276 opensl3[1] = opensl3[0] - curmaxsl;
2277 opensl3[0] = curmaxsl;
2278 if (opensl3[1] < 0) {
2279 opensl3[0] += opensl3[1];
2280 opensl3[1] = 0;
2281 }
2282 if (opensl3[0] <= 0) {
2283 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2284 __LINE__);
2285 return -EXFULL;
2286 }
2287 curexp++;
2288
2289 /* schedule 12k family channels */
2290 while (coeff3 < ctrl->sched.num_cc3 &&
2291 curexp == (int)slc3->rootexp + expshft) {
2292 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2293 coeff3++;
2294 slc3 = ctrl->sched.chc3[coeff3];
2295 continue;
2296 }
2297 opensl1valid = false;
2298 if (opensl3[1] >= opensl3[0] ||
2299 (finalexp == (int)slc3->rootexp &&
2300 curintr <= 32 &&
2301 opensl3[0] == curmaxsl &&
2302 last1 < 0)) {
2303 opensl3[1] -= slc3->seglen;
2304 slc3->newoff = curmaxsl + opensl3[1];
2305 if (opensl3[1] < 0 &&
2306 opensl3[0] == curmaxsl) {
2307 opensl3[0] += opensl3[1];
2308 opensl3[1] = 0;
2309 }
2310 if (opensl3[0] < 0) {
2311 dev_dbg(&ctrl->dev,
2312 "reconfig failed:%d\n",
2313 __LINE__);
2314 return -EXFULL;
2315 }
2316 } else {
2317 if (slc3->seglen > opensl3[0]) {
2318 dev_dbg(&ctrl->dev,
2319 "reconfig failed:%d\n",
2320 __LINE__);
2321 return -EXFULL;
2322 }
2323 slc3->newoff = opensl3[0] -
2324 slc3->seglen;
2325 opensl3[0] = slc3->newoff;
2326 }
2327 slc3->newintr = curintr;
2328 coeff3++;
2329 slc3 = ctrl->sched.chc3[coeff3];
2330 }
2331 /* update 4k openslot records */
2332 if (opensl1valid == false) {
2333 for (i = 0; i < 3; i++) {
2334 opensl1[i * 2] = opensl3[0];
2335 opensl1[(i * 2) + 1] = opensl3[1];
2336 }
2337 } else {
2338 int opensl1p[6];
2339 memcpy(opensl1p, opensl1, sizeof(opensl1));
2340 for (i = 0; i < 3; i++) {
2341 if (opensl1p[i] < opensl1p[i + 3])
2342 opensl1[(i * 2) + 1] =
2343 opensl1p[i];
2344 else
2345 opensl1[(i * 2) + 1] =
2346 opensl1p[i + 3];
2347 }
2348 for (i = 0; i < 3; i++) {
2349 opensl1[(i * 2) + 1] -= curmaxsl;
2350 opensl1[i * 2] = curmaxsl;
2351 if (opensl1[(i * 2) + 1] < 0) {
2352 opensl1[i * 2] +=
2353 opensl1[(i * 2) + 1];
2354 opensl1[(i * 2) + 1] = 0;
2355 }
2356 if (opensl1[i * 2] < 0) {
2357 dev_dbg(&ctrl->dev,
2358 "reconfig failed:%d\n",
2359 __LINE__);
2360 return -EXFULL;
2361 }
2362 }
2363 }
2364 /* schedule 4k family channels */
2365 while (coeff1 < ctrl->sched.num_cc1 &&
2366 curexp == (int)slc1->rootexp + expshft) {
2367 /* searchorder effective when opensl valid */
2368 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2369 int maxopensl = 0;
2370 int maxi = 0;
2371 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2372 coeff1++;
2373 slc1 = ctrl->sched.chc1[coeff1];
2374 continue;
2375 }
2376 opensl1valid = true;
2377 for (i = 0; i < 6; i++) {
2378 if (opensl1[srcho[i]] > maxopensl) {
2379 maxopensl = opensl1[srcho[i]];
2380 maxi = srcho[i];
2381 }
2382 }
2383 opensl1[maxi] -= slc1->seglen;
2384 slc1->newoff = (curmaxsl * maxi) +
2385 opensl1[maxi];
2386 if (opensl1[maxi] < 0) {
2387 if (((maxi & 1) == 1) &&
2388 (opensl1[maxi - 1] == curmaxsl)) {
2389 opensl1[maxi - 1] +=
2390 opensl1[maxi];
2391 if (opensl3[0] >
2392 opensl1[maxi - 1])
2393 opensl3[0] =
2394 opensl1[maxi - 1];
2395 opensl3[1] = 0;
2396 opensl1[maxi] = 0;
2397 if (opensl1[maxi - 1] < 0) {
2398 dev_dbg(&ctrl->dev,
2399 "reconfig failed:%d\n",
2400 __LINE__);
2401 return -EXFULL;
2402 }
2403 } else {
2404 dev_dbg(&ctrl->dev,
2405 "reconfig failed:%d\n",
2406 __LINE__);
2407 return -EXFULL;
2408 }
2409 } else {
2410 if (opensl3[maxi & 1] > opensl1[maxi])
2411 opensl3[maxi & 1] =
2412 opensl1[maxi];
2413 }
2414 slc1->newintr = curintr * 3;
2415 coeff1++;
2416 slc1 = ctrl->sched.chc1[coeff1];
2417 }
2418 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002419 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002420 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002421 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2423 if (opensl3[1] > opensl3[0]) {
2424 int temp = opensl3[0];
2425 opensl3[0] = opensl3[1];
2426 opensl3[1] = temp;
2427 temp = opensl1[5];
2428 opensl1[5] = opensl1[4];
2429 opensl1[4] = opensl1[3];
2430 opensl1[3] = opensl1[2];
2431 opensl1[2] = opensl1[1];
2432 opensl1[1] = opensl1[0];
2433 opensl1[0] = temp;
2434 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2435 ctrl->sched.num_cc1, curmaxsl);
2436 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2437 ctrl->sched.num_cc3, curmaxsl);
2438 }
2439 /* subframe mode to maximize BW */
2440 maxctrlw3 = opensl3[0];
2441 maxctrlw1 = opensl1[0];
2442 if (opensl3[0] == curmaxsl)
2443 maxctrlw3 += opensl3[1];
2444 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2445 maxctrlw1 += opensl1[i + 1];
2446 if (curintr >= 32) {
2447 *subfrml = 32;
2448 *ctrlw = maxctrlw3;
2449 } else if (curintr == 16) {
2450 if (maxctrlw3 > (opensl3[1] * 4)) {
2451 *subfrml = 32;
2452 *ctrlw = maxctrlw3;
2453 } else {
2454 *subfrml = 8;
2455 *ctrlw = opensl3[1];
2456 }
2457 } else {
2458 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2459 *subfrml = 24;
2460 *ctrlw = maxctrlw1;
2461 } else {
2462 *subfrml = 8;
2463 *ctrlw = maxctrlw3;
2464 }
2465 }
2466 }
2467 return 0;
2468}
2469
2470#ifdef DEBUG
2471static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2472 u32 subfrml, u32 clkgear)
2473{
2474 int sl, i;
2475 int cc1 = 0;
2476 int cc3 = 0;
2477 struct slim_ich *slc = NULL;
2478 if (!ctrl->sched.slots)
2479 return 0;
2480 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2481 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2482 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2483 for (i = 0; i < ctrlw; i++)
2484 ctrl->sched.slots[sl + i] = 33;
2485 }
2486 while (cc1 < ctrl->sched.num_cc1) {
2487 slc = ctrl->sched.chc1[cc1];
2488 if (slc == NULL) {
2489 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2490 cc1);
2491 return -EIO;
2492 }
2493 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2494 (slc - ctrl->chans), slc->newoff,
2495 slc->newintr, slc->seglen);
2496
2497 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2498 for (sl = slc->newoff;
2499 sl < SLIM_SL_PER_SUPERFRAME;
2500 sl += slc->newintr) {
2501 for (i = 0; i < slc->seglen; i++) {
2502 if (ctrl->sched.slots[sl + i])
2503 return -EXFULL;
2504 ctrl->sched.slots[sl + i] = cc1 + 1;
2505 }
2506 }
2507 }
2508 cc1++;
2509 }
2510 while (cc3 < ctrl->sched.num_cc3) {
2511 slc = ctrl->sched.chc3[cc3];
2512 if (slc == NULL) {
2513 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2514 cc3);
2515 return -EIO;
2516 }
2517 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2518 (slc - ctrl->chans), slc->newoff,
2519 slc->newintr, slc->seglen);
2520 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2521 for (sl = slc->newoff;
2522 sl < SLIM_SL_PER_SUPERFRAME;
2523 sl += slc->newintr) {
2524 for (i = 0; i < slc->seglen; i++) {
2525 if (ctrl->sched.slots[sl + i])
2526 return -EXFULL;
2527 ctrl->sched.slots[sl + i] = cc3 + 1;
2528 }
2529 }
2530 }
2531 cc3++;
2532 }
2533
2534 return 0;
2535}
2536#else
2537static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2538 u32 subfrml, u32 clkgear)
2539{
2540 return 0;
2541}
2542#endif
2543
2544static void slim_sort_chan_grp(struct slim_controller *ctrl,
2545 struct slim_ich *slc)
2546{
2547 u8 last = (u8)-1;
2548 u8 second = 0;
2549
2550 for (; last > 0; last--) {
2551 struct slim_ich *slc1 = slc;
2552 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002553 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554 slc2 = &ctrl->chans[next];
2555 for (second = 1; second <= last && slc2 &&
2556 (slc2->state == SLIM_CH_ACTIVE ||
2557 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2558 if (slc1->newoff > slc2->newoff) {
2559 u32 temp = slc2->newoff;
2560 slc2->newoff = slc1->newoff;
2561 slc1->newoff = temp;
2562 }
2563 if (slc2->nextgrp & SLIM_END_GRP) {
2564 last = second;
2565 break;
2566 }
2567 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002568 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 slc2 = &ctrl->chans[next];
2570 }
2571 if (slc2 == NULL)
2572 last = second - 1;
2573 }
2574}
2575
2576
2577static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2578{
2579 u32 msgsl = 0;
2580 u32 ctrlw = 0;
2581 u32 subfrml = 0;
2582 int ret = -EIO;
2583 struct slim_controller *ctrl = sb->ctrl;
2584 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2585 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2586 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2587 *clkgear = SLIM_MAX_CLK_GEAR;
2588
2589 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2590 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2591 ctrl->sched.usedslots,
2592 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002593 /*
2594 * If number of slots are 0, that means channels are inactive.
2595 * It is very likely that the manager will call clock pause very soon.
2596 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2597 * minimum amount of time.
2598 */
2599 if (ctrl->sched.usedslots != 0) {
2600 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2601 *clkgear -= 1;
2602 usedsl *= 2;
2603 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002604 }
2605
2606 /*
2607 * Try scheduling data channels at current clock gear, if all channels
2608 * can be scheduled, or reserved BW can't be satisfied, increase clock
2609 * gear and try again
2610 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002611 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002612 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2613
2614 if (ret == 0) {
2615 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002616 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002617 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002618 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002619 continue;
2620 else
2621 break;
2622 }
2623 }
2624 if (ret == 0) {
2625 int i;
2626 /* Sort channel-groups */
2627 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2628 struct slim_ich *slc = ctrl->sched.chc1[i];
2629 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2630 continue;
2631 if ((slc->nextgrp & SLIM_START_GRP) &&
2632 !(slc->nextgrp & SLIM_END_GRP)) {
2633 slim_sort_chan_grp(ctrl, slc);
2634 }
2635 }
2636 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2637 struct slim_ich *slc = ctrl->sched.chc3[i];
2638 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2639 continue;
2640 if ((slc->nextgrp & SLIM_START_GRP) &&
2641 !(slc->nextgrp & SLIM_END_GRP)) {
2642 slim_sort_chan_grp(ctrl, slc);
2643 }
2644 }
2645
2646 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2647 }
2648
2649 return ret;
2650}
2651
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002652static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2653{
2654 struct slim_ich **arr;
2655 int len, i;
2656 if (coeff == SLIM_COEFF_1) {
2657 arr = ctrl->sched.chc1;
2658 len = ctrl->sched.num_cc1;
2659 } else {
2660 arr = ctrl->sched.chc3;
2661 len = ctrl->sched.num_cc3;
2662 }
2663 for (i = 0; i < len; i++) {
2664 struct slim_ich *slc = arr[i];
2665 if (slc->state == SLIM_CH_ACTIVE ||
2666 slc->state == SLIM_CH_SUSPENDED)
2667 slc->offset = slc->newoff;
2668 slc->interval = slc->newintr;
2669 }
2670}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002671static void slim_chan_changes(struct slim_device *sb, bool revert)
2672{
2673 struct slim_controller *ctrl = sb->ctrl;
2674 while (!list_empty(&sb->mark_define)) {
2675 struct slim_ich *slc;
2676 struct slim_pending_ch *pch =
2677 list_entry(sb->mark_define.next,
2678 struct slim_pending_ch, pending);
2679 slc = &ctrl->chans[pch->chan];
2680 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002681 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2682 u32 sl = slc->seglen << slc->rootexp;
2683 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 Dharia4ec2ff42011-09-26 10:20:17 -06002687 slim_remove_ch(ctrl, slc);
2688 slc->state = SLIM_CH_DEFINED;
2689 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002690 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002691 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002692 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002693 }
2694 list_del_init(&pch->pending);
2695 kfree(pch);
2696 }
2697
2698 while (!list_empty(&sb->mark_removal)) {
2699 struct slim_pending_ch *pch =
2700 list_entry(sb->mark_removal.next,
2701 struct slim_pending_ch, pending);
2702 struct slim_ich *slc = &ctrl->chans[pch->chan];
2703 u32 sl = slc->seglen << slc->rootexp;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002704 if (revert || slc->def > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002705 if (slc->coeff == SLIM_COEFF_3)
2706 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002707 if (!ctrl->allocbw)
2708 ctrl->sched.usedslots += sl;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002709 if (revert)
2710 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002711 slc->state = SLIM_CH_ACTIVE;
2712 } else
2713 slim_remove_ch(ctrl, slc);
2714 list_del_init(&pch->pending);
2715 kfree(pch);
2716 }
2717
2718 while (!list_empty(&sb->mark_suspend)) {
2719 struct slim_pending_ch *pch =
2720 list_entry(sb->mark_suspend.next,
2721 struct slim_pending_ch, pending);
2722 struct slim_ich *slc = &ctrl->chans[pch->chan];
2723 if (revert)
2724 slc->state = SLIM_CH_ACTIVE;
2725 list_del_init(&pch->pending);
2726 kfree(pch);
2727 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002728 /* Change already active channel if reconfig succeeded */
2729 if (!revert) {
2730 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2731 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2732 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002733}
2734
2735/*
2736 * slim_reconfigure_now: Request reconfiguration now.
2737 * @sb: client handle
2738 * This API does what commit flag in other scheduling APIs do.
2739 * -EXFULL is returned if there is no space in TDM to reserve the
2740 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2741 * progress.
2742 */
2743int slim_reconfigure_now(struct slim_device *sb)
2744{
2745 u8 i;
2746 u8 wbuf[4];
2747 u32 clkgear, subframe;
2748 u32 curexp;
2749 int ret;
2750 struct slim_controller *ctrl = sb->ctrl;
2751 u32 expshft;
2752 u32 segdist;
2753 struct slim_pending_ch *pch;
2754
Sagar Dharia80a55e12012-08-16 16:43:58 -06002755 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002756 /*
2757 * If there are no pending changes from this client, avoid sending
2758 * the reconfiguration sequence
2759 */
2760 if (sb->pending_msgsl == sb->cur_msgsl &&
2761 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002762 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002763 struct list_head *pos, *next;
2764 list_for_each_safe(pos, next, &sb->mark_removal) {
2765 struct slim_ich *slc;
2766 pch = list_entry(pos, struct slim_pending_ch, pending);
2767 slc = &ctrl->chans[pch->chan];
2768 if (slc->def > 0)
2769 slc->def--;
2770 /* Disconnect source port to free it up */
2771 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2772 slc->srch = 0;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002773 /*
2774 * If controller overrides BW allocation,
2775 * delete this in remove channel itself
2776 */
2777 if (slc->def != 0 && !ctrl->allocbw) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002778 list_del(&pch->pending);
2779 kfree(pch);
2780 }
2781 }
2782 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002783 mutex_unlock(&ctrl->sched.m_reconf);
2784 pr_info("SLIM_CL: skip reconfig sequence");
2785 return 0;
2786 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002787 }
2788
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002789 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2790 list_for_each_entry(pch, &sb->mark_define, pending) {
2791 struct slim_ich *slc = &ctrl->chans[pch->chan];
2792 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002793 if (slc->state < SLIM_CH_ACTIVE)
2794 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002795 }
2796
2797 list_for_each_entry(pch, &sb->mark_removal, pending) {
2798 struct slim_ich *slc = &ctrl->chans[pch->chan];
2799 u32 sl = slc->seglen << slc->rootexp;
2800 if (slc->coeff == SLIM_COEFF_3)
2801 sl *= 3;
Sagar Dhariad1468b72013-07-16 12:56:22 -06002802 if (!ctrl->allocbw)
2803 ctrl->sched.usedslots -= sl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002804 slc->state = SLIM_CH_PENDING_REMOVAL;
2805 }
2806 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2807 struct slim_ich *slc = &ctrl->chans[pch->chan];
2808 slc->state = SLIM_CH_SUSPENDED;
2809 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002810
Sagar Dharia4aec9232012-07-24 23:44:26 -06002811 /*
2812 * Controller can override default channel scheduling algorithm.
2813 * (e.g. if controller needs to use fixed channel scheduling based
2814 * on number of channels)
2815 */
2816 if (ctrl->allocbw)
2817 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2818 else
2819 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002820
2821 if (!ret) {
2822 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2823 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2824 NULL, NULL, 0, 3, NULL, 0, NULL);
2825 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2826 }
2827
2828 if (!ret && subframe != ctrl->sched.subfrmcode) {
2829 wbuf[0] = (u8)(subframe & 0xFF);
2830 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2831 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2832 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2833 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2834 (int)wbuf[0], ret);
2835 }
2836 if (!ret && clkgear != ctrl->clkgear) {
2837 wbuf[0] = (u8)(clkgear & 0xFF);
2838 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2839 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2840 NULL, wbuf, 1, 4, NULL, 0, NULL);
2841 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2842 (int)wbuf[0], ret);
2843 }
2844 if (ret)
2845 goto revert_reconfig;
2846
2847 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2848 /* activate/remove channel */
2849 list_for_each_entry(pch, &sb->mark_define, pending) {
2850 struct slim_ich *slc = &ctrl->chans[pch->chan];
2851 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002852 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002853 wbuf[1] = slc->prrate;
2854 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2855 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2856 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2857 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2858 /* Right now, channel link bit is not supported */
2859 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2860 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2861 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2862 NULL, 0, NULL);
2863 if (ret)
2864 goto revert_reconfig;
2865
2866 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2867 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2868 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2869 NULL, 0, NULL);
2870 if (ret)
2871 goto revert_reconfig;
2872 }
2873
2874 list_for_each_entry(pch, &sb->mark_removal, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002875 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002876 dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002877 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002878 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2879 SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
2880 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2881 NULL, 0, NULL);
2882 if (ret)
2883 goto revert_reconfig;
2884 }
2885 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002886 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002888 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002889 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2890 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2891 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2892 NULL, 0, NULL);
2893 if (ret)
2894 goto revert_reconfig;
2895 }
2896
2897 /* Define CC1 channel */
2898 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2899 struct slim_ich *slc = ctrl->sched.chc1[i];
2900 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2901 continue;
2902 curexp = slc->rootexp + expshft;
2903 segdist = (slc->newoff << curexp) & 0x1FF;
2904 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002905 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2906 slc->newintr, slc->interval, segdist);
2907 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2908 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002909
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002910 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002911 slc->newintr != slc->interval ||
2912 slc->newoff != slc->offset) {
2913 segdist |= 0x200;
2914 segdist >>= curexp;
2915 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002916 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917 wbuf[1] = (u8)(segdist & 0xFF);
2918 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2919 (slc->prop.prot << 4);
2920 wbuf[3] = slc->seglen;
2921 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2922 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2923 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2924 7, NULL, 0, NULL);
2925 if (ret)
2926 goto revert_reconfig;
2927 }
2928 }
2929
2930 /* Define CC3 channels */
2931 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2932 struct slim_ich *slc = ctrl->sched.chc3[i];
2933 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2934 continue;
2935 curexp = slc->rootexp + expshft;
2936 segdist = (slc->newoff << curexp) & 0x1FF;
2937 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002938 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2939 slc->newintr, slc->interval, segdist);
2940 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2941 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002942
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002943 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002944 slc->newintr != slc->interval ||
2945 slc->newoff != slc->offset) {
2946 segdist |= 0x200;
2947 segdist >>= curexp;
2948 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002949 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002950 wbuf[1] = (u8)(segdist & 0xFF);
2951 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2952 (slc->prop.prot << 4);
2953 wbuf[3] = (u8)(slc->seglen);
2954 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2955 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2956 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2957 7, NULL, 0, NULL);
2958 if (ret)
2959 goto revert_reconfig;
2960 }
2961 }
2962 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2963 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2964 NULL, 0, 3, NULL, 0, NULL);
2965 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2966 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002967 ctrl->sched.subfrmcode = subframe;
2968 ctrl->clkgear = clkgear;
2969 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2970 sb->cur_msgsl = sb->pending_msgsl;
2971 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002972 mutex_unlock(&ctrl->sched.m_reconf);
2973 return 0;
2974 }
2975
2976revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002977 /* Revert channel changes */
2978 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002979 mutex_unlock(&ctrl->sched.m_reconf);
2980 return ret;
2981}
2982EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2983
2984static int add_pending_ch(struct list_head *listh, u8 chan)
2985{
2986 struct slim_pending_ch *pch;
2987 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2988 if (!pch)
2989 return -ENOMEM;
2990 pch->chan = chan;
2991 list_add_tail(&pch->pending, listh);
2992 return 0;
2993}
2994
2995/*
2996 * slim_control_ch: Channel control API.
2997 * @sb: client handle
2998 * @chanh: group or channel handle to be controlled
2999 * @chctrl: Control command (activate/suspend/remove)
3000 * @commit: flag to indicate whether the control should take effect right-away.
3001 * This API activates, removes or suspends a channel (or group of channels)
3002 * chanh indicates the channel or group handle (returned by the define_ch API).
3003 * Reconfiguration may be time-consuming since it can change all other active
3004 * channel allocations on the bus, change in clock gear used by the slimbus,
3005 * and change in the control space width used for messaging.
3006 * commit makes sure that multiple channels can be activated/deactivated before
3007 * reconfiguration is started.
3008 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
3009 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
3010 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07003011 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003012 */
3013int slim_control_ch(struct slim_device *sb, u16 chanh,
3014 enum slim_ch_control chctrl, bool commit)
3015{
3016 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003017 int ret = 0;
3018 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06003019 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06003020 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07003021 struct slim_ich *slc = &ctrl->chans[chan];
3022 if (!(slc->nextgrp & SLIM_START_GRP))
3023 return -EINVAL;
3024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003025 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003026 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303027 struct slim_pending_ch *pch;
3028 u8 add_mark_removal = true;
3029
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003030 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003031 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
3032 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003033 if (slc->state < SLIM_CH_DEFINED) {
3034 ret = -ENOTCONN;
3035 break;
3036 }
3037 if (chctrl == SLIM_CH_SUSPEND) {
3038 ret = add_pending_ch(&sb->mark_suspend, chan);
3039 if (ret)
3040 break;
3041 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003042 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003043 ret = -EISCONN;
3044 break;
3045 }
3046 ret = add_pending_ch(&sb->mark_define, chan);
3047 if (ret)
3048 break;
3049 } else {
3050 if (slc->state < SLIM_CH_ACTIVE) {
3051 ret = -ENOTCONN;
3052 break;
3053 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05303054 /* If channel removal request comes when pending
3055 * in the mark_define, remove it from the define
3056 * list instead of adding it to removal list
3057 */
3058 if (!list_empty(&sb->mark_define)) {
3059 struct list_head *pos, *next;
3060 list_for_each_safe(pos, next,
3061 &sb->mark_define) {
3062 pch = list_entry(pos,
3063 struct slim_pending_ch,
3064 pending);
Kiran Gundabcd74d02013-10-08 16:32:13 +05303065 if (pch->chan == chan) {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303066 list_del(&pch->pending);
3067 kfree(pch);
3068 add_mark_removal = false;
3069 break;
3070 }
3071 }
3072 }
3073 if (add_mark_removal == true) {
3074 ret = add_pending_ch(&sb->mark_removal, chan);
3075 if (ret)
3076 break;
3077 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003078 }
3079
Sagar Dhariab886e042012-10-17 22:41:57 -06003080 nchan++;
3081 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06003082 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06003083 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003084 if (!ret && commit == true)
3085 ret = slim_reconfigure_now(sb);
3086 mutex_unlock(&sb->sldev_reconf);
3087 return ret;
3088}
3089EXPORT_SYMBOL_GPL(slim_control_ch);
3090
3091/*
3092 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
3093 * @sb: client handle
3094 * @bw_bps: message bandwidth in bits per second to be requested
3095 * @commit: indicates whether the reconfiguration needs to be acted upon.
3096 * This API call can be grouped with slim_control_ch API call with only one of
3097 * the APIs specifying the commit flag to avoid reconfiguration being called too
3098 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
3099 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
3100 * is already in progress.
3101 */
3102int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
3103{
3104 struct slim_controller *ctrl = sb->ctrl;
3105 int ret = 0;
3106 int sl;
3107 mutex_lock(&sb->sldev_reconf);
3108 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
3109 sl = SLIM_SL_PER_SUPERFRAME;
3110 else {
3111 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
3112 (ctrl->a_framer->rootfreq/2 - 1)) /
3113 (ctrl->a_framer->rootfreq/2);
3114 }
3115 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
3116 sb->cur_msgsl);
3117 sb->pending_msgsl = sl;
3118 if (commit == true)
3119 ret = slim_reconfigure_now(sb);
3120 mutex_unlock(&sb->sldev_reconf);
3121 return ret;
3122}
3123EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
3124
Sagar Dharia33f34442011-08-08 16:22:03 -06003125/*
3126 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
3127 * paused or woken up out of clock pause
3128 * or woken up from clock pause
3129 * @ctrl: controller requesting bus to be paused or woken up
3130 * @wakeup: Wakeup this controller from clock pause.
3131 * @restart: Restart time value per spec used for clock pause. This value
3132 * isn't used when controller is to be woken up.
3133 * This API executes clock pause reconfiguration sequence if wakeup is false.
3134 * If wakeup is true, controller's wakeup is called
3135 * Slimbus clock is idle and can be disabled by the controller later.
3136 */
3137int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
3138{
3139 int ret = 0;
3140 int i;
3141
3142 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
3143 return -EINVAL;
3144 mutex_lock(&ctrl->m_ctrl);
3145 if (wakeup) {
3146 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
3147 mutex_unlock(&ctrl->m_ctrl);
3148 return 0;
3149 }
3150 wait_for_completion(&ctrl->pause_comp);
3151 /*
3152 * Slimbus framework will call controller wakeup
3153 * Controller should make sure that it sets active framer
3154 * out of clock pause by doing appropriate setting
3155 */
3156 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3157 ret = ctrl->wakeup(ctrl);
Sagar Dharia129c7d82013-08-08 19:35:50 -06003158 /*
3159 * If wakeup fails, make sure that next attempt can succeed.
3160 * Since we already consumed pause_comp, complete it so
3161 * that next wakeup isn't blocked forever
3162 */
Sagar Dharia33f34442011-08-08 16:22:03 -06003163 if (!ret)
3164 ctrl->clk_state = SLIM_CLK_ACTIVE;
Sagar Dharia129c7d82013-08-08 19:35:50 -06003165 else
3166 complete(&ctrl->pause_comp);
Sagar Dharia33f34442011-08-08 16:22:03 -06003167 mutex_unlock(&ctrl->m_ctrl);
3168 return ret;
3169 } else {
3170 switch (ctrl->clk_state) {
3171 case SLIM_CLK_ENTERING_PAUSE:
3172 case SLIM_CLK_PAUSE_FAILED:
3173 /*
3174 * If controller is already trying to enter clock pause,
3175 * let it finish.
3176 * In case of error, retry
3177 * In both cases, previous clock pause has signalled
3178 * completion.
3179 */
3180 wait_for_completion(&ctrl->pause_comp);
3181 /* retry upon failure */
3182 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3183 ctrl->clk_state = SLIM_CLK_ACTIVE;
3184 break;
3185 } else {
3186 mutex_unlock(&ctrl->m_ctrl);
3187 /*
3188 * Signal completion so that wakeup can wait on
3189 * it.
3190 */
3191 complete(&ctrl->pause_comp);
3192 return 0;
3193 }
3194 break;
3195 case SLIM_CLK_PAUSED:
3196 /* already paused */
3197 mutex_unlock(&ctrl->m_ctrl);
3198 return 0;
3199 case SLIM_CLK_ACTIVE:
3200 default:
3201 break;
3202 }
3203 }
3204 /* Pending response for a message */
3205 for (i = 0; i < ctrl->last_tid; i++) {
3206 if (ctrl->txnt[i]) {
3207 ret = -EBUSY;
Sagar Dharia33beca02012-10-22 16:21:46 -06003208 pr_info("slim_clk_pause: txn-rsp for %d pending", i);
Sagar Dharia33f34442011-08-08 16:22:03 -06003209 mutex_unlock(&ctrl->m_ctrl);
3210 return -EBUSY;
3211 }
3212 }
3213 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3214 mutex_unlock(&ctrl->m_ctrl);
3215
3216 mutex_lock(&ctrl->sched.m_reconf);
3217 /* Data channels active */
3218 if (ctrl->sched.usedslots) {
Sagar Dharia33beca02012-10-22 16:21:46 -06003219 pr_info("slim_clk_pause: data channel active");
Sagar Dharia33f34442011-08-08 16:22:03 -06003220 ret = -EBUSY;
3221 goto clk_pause_ret;
3222 }
3223
3224 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003225 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3226 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003227 if (ret)
3228 goto clk_pause_ret;
3229
3230 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003231 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3232 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3233 if (ret)
3234 goto clk_pause_ret;
3235
3236 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3237 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3238 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003239 if (ret)
3240 goto clk_pause_ret;
3241
3242clk_pause_ret:
3243 if (ret)
3244 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3245 else
3246 ctrl->clk_state = SLIM_CLK_PAUSED;
3247 complete(&ctrl->pause_comp);
3248 mutex_unlock(&ctrl->sched.m_reconf);
3249 return ret;
3250}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003251EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003252
3253MODULE_LICENSE("GPL v2");
3254MODULE_VERSION("0.1");
3255MODULE_DESCRIPTION("Slimbus module");
3256MODULE_ALIAS("platform:slimbus");