blob: 8d0d1f37841441e36636f46c3fb8e43dec3930a8 [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{
291 u8 laddr;
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600292 int ret, i;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600293 struct slim_driver *sbdrv;
294 struct slim_device *sbdev =
295 container_of(work, struct slim_device, wd);
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600296 struct slim_controller *ctrl = sbdev->ctrl;
297 if (!sbdev->dev.driver)
298 return;
299 /* check if device-up or down needs to be called */
300 mutex_lock(&ctrl->m_ctrl);
301 /* address no longer valid, means device reported absent */
302 for (i = 0; i < ctrl->num_dev; i++) {
303 if (sbdev->laddr == ctrl->addrt[i].laddr &&
304 ctrl->addrt[i].valid == false &&
305 sbdev->notified)
306 break;
307 }
308 mutex_unlock(&ctrl->m_ctrl);
309 sbdrv = to_slim_driver(sbdev->dev.driver);
310 if (i < ctrl->num_dev) {
311 sbdev->notified = false;
312 if (sbdrv->device_down)
313 sbdrv->device_down(sbdev);
314 return;
315 }
Sagar Dharia4f240722014-01-15 13:50:58 -0700316 if (sbdev->notified || !sbdrv)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600317 return;
318 ret = slim_get_logical_addr(sbdev, sbdev->e_addr, 6, &laddr);
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600319 if (!ret) {
Sagar Dharia4f240722014-01-15 13:50:58 -0700320 sbdev->notified = true;
321 sbdrv->device_up(sbdev);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600322 }
323}
324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325/*
326 * slim_add_device: Add a new device without register board info.
327 * @ctrl: Controller to which this device is to be added to.
328 * Called when device doesn't have an explicit client-driver to be probed, or
329 * the client-driver is a module installed dynamically.
330 */
331int slim_add_device(struct slim_controller *ctrl, struct slim_device *sbdev)
332{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 sbdev->dev.bus = &slimbus_type;
334 sbdev->dev.parent = ctrl->dev.parent;
335 sbdev->dev.type = &slim_dev_type;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600336 sbdev->dev.driver = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 sbdev->ctrl = ctrl;
338 slim_ctrl_get(ctrl);
339 dev_set_name(&sbdev->dev, "%s", sbdev->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 mutex_init(&sbdev->sldev_reconf);
341 INIT_LIST_HEAD(&sbdev->mark_define);
342 INIT_LIST_HEAD(&sbdev->mark_suspend);
343 INIT_LIST_HEAD(&sbdev->mark_removal);
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600344 INIT_WORK(&sbdev->wd, slim_report);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600345 mutex_lock(&ctrl->m_ctrl);
346 list_add_tail(&sbdev->dev_list, &ctrl->devs);
347 mutex_unlock(&ctrl->m_ctrl);
348 /* probe slave on this controller */
349 return device_register(&sbdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350}
351EXPORT_SYMBOL_GPL(slim_add_device);
352
353struct sbi_boardinfo {
354 struct list_head list;
355 struct slim_boardinfo board_info;
356};
357
358static LIST_HEAD(board_list);
359static LIST_HEAD(slim_ctrl_list);
360static DEFINE_MUTEX(board_lock);
361
362/* If controller is not present, only add to boards list */
363static void slim_match_ctrl_to_boardinfo(struct slim_controller *ctrl,
364 struct slim_boardinfo *bi)
365{
366 int ret;
367 if (ctrl->nr != bi->bus_num)
368 return;
369
370 ret = slim_add_device(ctrl, bi->slim_slave);
371 if (ret != 0)
372 dev_err(ctrl->dev.parent, "can't create new device for %s\n",
373 bi->slim_slave->name);
374}
375
376/*
377 * slim_register_board_info: Board-initialization routine.
378 * @info: List of all devices on all controllers present on the board.
379 * @n: number of entries.
380 * API enumerates respective devices on corresponding controller.
381 * Called from board-init function.
382 */
383int slim_register_board_info(struct slim_boardinfo const *info, unsigned n)
384{
385 struct sbi_boardinfo *bi;
386 int i;
387
388 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
389 if (!bi)
390 return -ENOMEM;
391
392 for (i = 0; i < n; i++, bi++, info++) {
393 struct slim_controller *ctrl;
394
395 memcpy(&bi->board_info, info, sizeof(*info));
396 mutex_lock(&board_lock);
397 list_add_tail(&bi->list, &board_list);
398 list_for_each_entry(ctrl, &slim_ctrl_list, list)
399 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
400 mutex_unlock(&board_lock);
401 }
402 return 0;
403}
404EXPORT_SYMBOL_GPL(slim_register_board_info);
405
406/*
Sagar Dhariaa6627e02012-08-28 12:20:49 -0600407 * slim_ctrl_add_boarddevs: Add devices registered by board-info
408 * @ctrl: Controller to which these devices are to be added to.
409 * This API is called by controller when it is up and running.
410 * If devices on a controller were registered before controller,
411 * this will make sure that they get probed when controller is up.
412 */
413void slim_ctrl_add_boarddevs(struct slim_controller *ctrl)
414{
415 struct sbi_boardinfo *bi;
416 mutex_lock(&board_lock);
417 list_add_tail(&ctrl->list, &slim_ctrl_list);
418 list_for_each_entry(bi, &board_list, list)
419 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
420 mutex_unlock(&board_lock);
421}
422EXPORT_SYMBOL_GPL(slim_ctrl_add_boarddevs);
423
424/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 * slim_busnum_to_ctrl: Map bus number to controller
426 * @busnum: Bus number
427 * Returns controller representing this bus number
428 */
429struct slim_controller *slim_busnum_to_ctrl(u32 bus_num)
430{
431 struct slim_controller *ctrl;
432 mutex_lock(&board_lock);
433 list_for_each_entry(ctrl, &slim_ctrl_list, list)
434 if (bus_num == ctrl->nr) {
435 mutex_unlock(&board_lock);
436 return ctrl;
437 }
438 mutex_unlock(&board_lock);
439 return NULL;
440}
441EXPORT_SYMBOL_GPL(slim_busnum_to_ctrl);
442
443static int slim_register_controller(struct slim_controller *ctrl)
444{
445 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
447 /* Can't register until after driver model init */
448 if (WARN_ON(!slimbus_type.p)) {
449 ret = -EAGAIN;
450 goto out_list;
451 }
452
453 dev_set_name(&ctrl->dev, "sb-%d", ctrl->nr);
454 ctrl->dev.bus = &slimbus_type;
455 ctrl->dev.type = &slim_ctrl_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 ctrl->num_dev = 0;
Sagar Dharia98a7ecb2011-07-25 15:25:35 -0600457 if (!ctrl->min_cg)
458 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
459 if (!ctrl->max_cg)
460 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 mutex_init(&ctrl->m_ctrl);
462 mutex_init(&ctrl->sched.m_reconf);
463 ret = device_register(&ctrl->dev);
464 if (ret)
465 goto out_list;
466
467 dev_dbg(&ctrl->dev, "Bus [%s] registered:dev:%x\n", ctrl->name,
468 (u32)&ctrl->dev);
469
470 if (ctrl->nports) {
471 ctrl->ports = kzalloc(ctrl->nports * sizeof(struct slim_port),
472 GFP_KERNEL);
473 if (!ctrl->ports) {
474 ret = -ENOMEM;
475 goto err_port_failed;
476 }
477 }
478 if (ctrl->nchans) {
479 ctrl->chans = kzalloc(ctrl->nchans * sizeof(struct slim_ich),
480 GFP_KERNEL);
481 if (!ctrl->chans) {
482 ret = -ENOMEM;
483 goto err_chan_failed;
484 }
485
486 ctrl->sched.chc1 =
487 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
488 GFP_KERNEL);
489 if (!ctrl->sched.chc1) {
490 kfree(ctrl->chans);
491 ret = -ENOMEM;
492 goto err_chan_failed;
493 }
494 ctrl->sched.chc3 =
495 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
496 GFP_KERNEL);
497 if (!ctrl->sched.chc3) {
498 kfree(ctrl->sched.chc1);
499 kfree(ctrl->chans);
500 ret = -ENOMEM;
501 goto err_chan_failed;
502 }
503 }
504#ifdef DEBUG
505 ctrl->sched.slots = kzalloc(SLIM_SL_PER_SUPERFRAME, GFP_KERNEL);
506#endif
Sagar Dharia33f34442011-08-08 16:22:03 -0600507 init_completion(&ctrl->pause_comp);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600508
509 INIT_LIST_HEAD(&ctrl->devs);
510 ctrl->wq = create_singlethread_workqueue(dev_name(&ctrl->dev));
511 if (!ctrl->wq)
512 goto err_workq_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513
514 return 0;
515
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600516err_workq_failed:
517 kfree(ctrl->sched.chc3);
518 kfree(ctrl->sched.chc1);
519 kfree(ctrl->chans);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520err_chan_failed:
521 kfree(ctrl->ports);
522err_port_failed:
523 device_unregister(&ctrl->dev);
524out_list:
525 mutex_lock(&slim_lock);
526 idr_remove(&ctrl_idr, ctrl->nr);
527 mutex_unlock(&slim_lock);
528 return ret;
529}
530
531/* slim_remove_device: Remove the effect of slim_add_device() */
532void slim_remove_device(struct slim_device *sbdev)
533{
534 device_unregister(&sbdev->dev);
535}
536EXPORT_SYMBOL_GPL(slim_remove_device);
537
538static void slim_ctrl_remove_device(struct slim_controller *ctrl,
539 struct slim_boardinfo *bi)
540{
541 if (ctrl->nr == bi->bus_num)
542 slim_remove_device(bi->slim_slave);
543}
544
545/*
546 * slim_del_controller: Controller tear-down.
547 * Controller added with the above API is teared down using this API.
548 */
549int slim_del_controller(struct slim_controller *ctrl)
550{
551 struct slim_controller *found;
552 struct sbi_boardinfo *bi;
553
554 /* First make sure that this bus was added */
555 mutex_lock(&slim_lock);
556 found = idr_find(&ctrl_idr, ctrl->nr);
557 mutex_unlock(&slim_lock);
558 if (found != ctrl)
559 return -EINVAL;
560
561 /* Remove all clients */
562 mutex_lock(&board_lock);
563 list_for_each_entry(bi, &board_list, list)
564 slim_ctrl_remove_device(ctrl, &bi->board_info);
565 mutex_unlock(&board_lock);
566
567 init_completion(&ctrl->dev_released);
568 device_unregister(&ctrl->dev);
569
570 wait_for_completion(&ctrl->dev_released);
571 list_del(&ctrl->list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600572 destroy_workqueue(ctrl->wq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 /* free bus id */
574 mutex_lock(&slim_lock);
575 idr_remove(&ctrl_idr, ctrl->nr);
576 mutex_unlock(&slim_lock);
577
578 kfree(ctrl->sched.chc1);
579 kfree(ctrl->sched.chc3);
580#ifdef DEBUG
581 kfree(ctrl->sched.slots);
582#endif
583 kfree(ctrl->chans);
584 kfree(ctrl->ports);
585
586 return 0;
587}
588EXPORT_SYMBOL_GPL(slim_del_controller);
589
590/*
591 * slim_add_numbered_controller: Controller bring-up.
592 * @ctrl: Controller to be registered.
593 * A controller is registered with the framework using this API. ctrl->nr is the
594 * desired number with which slimbus framework registers the controller.
595 * Function will return -EBUSY if the number is in use.
596 */
597int slim_add_numbered_controller(struct slim_controller *ctrl)
598{
599 int id;
600 int status;
601
602 if (ctrl->nr & ~MAX_ID_MASK)
603 return -EINVAL;
604
605retry:
606 if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0)
607 return -ENOMEM;
608
609 mutex_lock(&slim_lock);
610 status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
611 if (status == 0 && id != ctrl->nr) {
612 status = -EAGAIN;
613 idr_remove(&ctrl_idr, id);
614 }
615 mutex_unlock(&slim_lock);
616 if (status == -EAGAIN)
617 goto retry;
618
619 if (status == 0)
620 status = slim_register_controller(ctrl);
621 return status;
622}
623EXPORT_SYMBOL_GPL(slim_add_numbered_controller);
624
625/*
Sagar Dhariaf68d71f2013-07-31 17:43:46 -0600626 * slim_report_absent: Controller calls this function when a device
627 * reports absent, OR when the device cannot be communicated with
628 * @sbdev: Device that cannot be reached, or sent report absent
629 */
630void slim_report_absent(struct slim_device *sbdev)
631{
632 struct slim_controller *ctrl;
633 int i;
634 if (!sbdev)
635 return;
636 ctrl = sbdev->ctrl;
637 if (!ctrl)
638 return;
639 /* invalidate logical addresses */
640 mutex_lock(&ctrl->m_ctrl);
641 for (i = 0; i < ctrl->num_dev; i++) {
642 if (sbdev->laddr == ctrl->addrt[i].laddr)
643 ctrl->addrt[i].valid = false;
644 }
645 mutex_unlock(&ctrl->m_ctrl);
646 queue_work(ctrl->wq, &sbdev->wd);
647}
648EXPORT_SYMBOL(slim_report_absent);
649
650/*
Sagar Dharia722f9e22013-12-11 23:59:22 -0700651 * slim_framer_booted: This function is called by controller after the active
652 * framer has booted (using Bus Reset sequence, or after it has shutdown and has
653 * come back up). Components, devices on the bus may be in undefined state,
654 * and this function triggers their drivers to do the needful
655 * to bring them back in Reset state so that they can acquire sync, report
656 * present and be operational again.
657 */
658void slim_framer_booted(struct slim_controller *ctrl)
659{
660 struct slim_device *sbdev;
661 struct list_head *pos, *next;
662 if (!ctrl)
663 return;
664 mutex_lock(&ctrl->m_ctrl);
665 list_for_each_safe(pos, next, &ctrl->devs) {
666 struct slim_driver *sbdrv;
667 sbdev = list_entry(pos, struct slim_device, dev_list);
668 mutex_unlock(&ctrl->m_ctrl);
669 if (sbdev && sbdev->dev.driver) {
670 sbdrv = to_slim_driver(sbdev->dev.driver);
671 if (sbdrv->reset_device)
672 sbdrv->reset_device(sbdev);
673 }
674 mutex_lock(&ctrl->m_ctrl);
675 }
676 mutex_unlock(&ctrl->m_ctrl);
677}
678EXPORT_SYMBOL(slim_framer_booted);
679
680/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 * slim_msg_response: Deliver Message response received from a device to the
682 * framework.
683 * @ctrl: Controller handle
684 * @reply: Reply received from the device
685 * @len: Length of the reply
686 * @tid: Transaction ID received with which framework can associate reply.
687 * Called by controller to inform framework about the response received.
688 * This helps in making the API asynchronous, and controller-driver doesn't need
689 * to manage 1 more table other than the one managed by framework mapping TID
690 * with buffers
691 */
692void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
693{
694 int i;
695 struct slim_msg_txn *txn;
696
697 mutex_lock(&ctrl->m_ctrl);
698 txn = ctrl->txnt[tid];
Kiran Gunda1aaae922014-02-04 07:45:03 +0530699 if (txn == NULL || txn->rbuf == NULL) {
700 if (txn == NULL)
701 dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 tid, len);
Kiran Gunda1aaae922014-02-04 07:45:03 +0530703 else
704 dev_err(&ctrl->dev, "Invalid client buffer passed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 mutex_unlock(&ctrl->m_ctrl);
706 return;
707 }
708 for (i = 0; i < len; i++)
709 txn->rbuf[i] = reply[i];
710 if (txn->comp)
711 complete(txn->comp);
712 ctrl->txnt[tid] = NULL;
713 mutex_unlock(&ctrl->m_ctrl);
714 kfree(txn);
715}
716EXPORT_SYMBOL_GPL(slim_msg_response);
717
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600718static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
720 struct completion *comp, u8 la, u8 *tid)
721{
722 u8 i = 0;
723 int ret = 0;
724 struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
725 GFP_KERNEL);
726 if (!txn)
727 return -ENOMEM;
728 if (tid) {
729 mutex_lock(&ctrl->m_ctrl);
730 for (i = 0; i < ctrl->last_tid; i++) {
731 if (ctrl->txnt[i] == NULL)
732 break;
733 }
734 if (i >= ctrl->last_tid) {
735 if (ctrl->last_tid == 255) {
736 mutex_unlock(&ctrl->m_ctrl);
737 kfree(txn);
738 return -ENOMEM;
739 }
740 ctrl->txnt = krealloc(ctrl->txnt,
741 (i + 1) * sizeof(struct slim_msg_txn *),
742 GFP_KERNEL);
743 if (!ctrl->txnt) {
744 mutex_unlock(&ctrl->m_ctrl);
745 kfree(txn);
746 return -ENOMEM;
747 }
748 ctrl->last_tid++;
749 }
750 ctrl->txnt[i] = txn;
751 mutex_unlock(&ctrl->m_ctrl);
752 txn->tid = i;
753 *tid = i;
754 }
755 txn->mc = mc;
756 txn->mt = mt;
757 txn->dt = dt;
758 txn->ec = ec;
759 txn->la = la;
760 txn->rbuf = rbuf;
761 txn->wbuf = wbuf;
762 txn->rl = mlen;
763 txn->len = len;
764 txn->comp = comp;
765
766 ret = ctrl->xfer_msg(ctrl, txn);
767 if (!tid)
768 kfree(txn);
769 return ret;
770}
771
772static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600773 u8 e_len, u8 *entry)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774{
775 u8 i;
776 for (i = 0; i < ctrl->num_dev; i++) {
777 if (ctrl->addrt[i].valid &&
778 memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600779 *entry = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 return 0;
781 }
782 }
783 return -ENXIO;
784}
785
786/*
787 * slim_assign_laddr: Assign logical address to a device enumerated.
788 * @ctrl: Controller with which device is enumerated.
789 * @e_addr: 6-byte elemental address of the device.
790 * @e_len: buffer length for e_addr
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600791 * @laddr: Return logical address (if valid flag is false)
792 * @valid: true if laddr holds a valid address that controller wants to
793 * set for this enumeration address. Otherwise framework sets index into
794 * address table as logical address.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 * Called by controller in response to REPORT_PRESENT. Framework will assign
796 * a logical address to this enumeration address.
797 * Function returns -EXFULL to indicate that all logical addresses are already
798 * taken.
799 */
800int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600801 u8 e_len, u8 *laddr, bool valid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802{
803 int ret;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600804 u8 i = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600805 bool exists = false;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600806 struct slim_device *sbdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 mutex_lock(&ctrl->m_ctrl);
808 /* already assigned */
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600809 if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
810 *laddr = ctrl->addrt[i].laddr;
811 exists = true;
812 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 if (ctrl->num_dev >= 254) {
814 ret = -EXFULL;
815 goto ret_assigned_laddr;
816 }
817 for (i = 0; i < ctrl->num_dev; i++) {
818 if (ctrl->addrt[i].valid == false)
819 break;
820 }
821 if (i == ctrl->num_dev) {
822 ctrl->addrt = krealloc(ctrl->addrt,
823 (ctrl->num_dev + 1) *
824 sizeof(struct slim_addrt),
825 GFP_KERNEL);
826 if (!ctrl->addrt) {
827 ret = -ENOMEM;
828 goto ret_assigned_laddr;
829 }
830 ctrl->num_dev++;
831 }
832 memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
833 ctrl->addrt[i].valid = true;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600834 /* Preferred address is index into table */
835 if (!valid)
836 *laddr = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 }
838
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600839 ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
840 *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 if (ret) {
842 ctrl->addrt[i].valid = false;
843 goto ret_assigned_laddr;
844 }
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600845 ctrl->addrt[i].laddr = *laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600847 dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848ret_assigned_laddr:
849 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600850 if (exists || ret)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600851 return ret;
852
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600853 pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600854 e_addr[1], e_addr[2]);
855 mutex_lock(&ctrl->m_ctrl);
856 list_for_each_entry(sbdev, &ctrl->devs, dev_list) {
857 if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
858 struct slim_driver *sbdrv;
Sagar Dharia33c84e62012-10-30 21:12:09 -0600859 sbdev->laddr = *laddr;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600860 if (sbdev->dev.driver) {
861 sbdrv = to_slim_driver(sbdev->dev.driver);
862 if (sbdrv->device_up)
863 queue_work(ctrl->wq, &sbdev->wd);
864 }
865 break;
866 }
867 }
868 mutex_unlock(&ctrl->m_ctrl);
869 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870}
871EXPORT_SYMBOL_GPL(slim_assign_laddr);
872
873/*
874 * slim_get_logical_addr: Return the logical address of a slimbus device.
875 * @sb: client handle requesting the adddress.
876 * @e_addr: Elemental address of the device.
877 * @e_len: Length of e_addr
878 * @laddr: output buffer to store the address
879 * context: can sleep
880 * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
881 * the device with this elemental address is not found.
882 */
883int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
884 u8 e_len, u8 *laddr)
885{
886 int ret = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600887 u8 entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 struct slim_controller *ctrl = sb->ctrl;
889 if (!ctrl || !laddr || !e_addr || e_len != 6)
890 return -EINVAL;
891 mutex_lock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600892 ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
893 if (!ret)
894 *laddr = ctrl->addrt[entry].laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600896 if (ret == -ENXIO && ctrl->get_laddr) {
897 ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
898 if (!ret)
899 ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
900 true);
901 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 return ret;
903}
904EXPORT_SYMBOL_GPL(slim_get_logical_addr);
905
906static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
907 u8 *rbuf, const u8 *wbuf, u8 len)
908{
909 if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
910 return -EINVAL;
911 switch (oper) {
912 case SLIM_MSG_MC_REQUEST_VALUE:
913 case SLIM_MSG_MC_REQUEST_INFORMATION:
914 if (rbuf == NULL)
915 return -EINVAL;
916 return 0;
917 case SLIM_MSG_MC_CHANGE_VALUE:
918 case SLIM_MSG_MC_CLEAR_INFORMATION:
919 if (wbuf == NULL)
920 return -EINVAL;
921 return 0;
922 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
923 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
924 if (rbuf == NULL || wbuf == NULL)
925 return -EINVAL;
926 return 0;
927 default:
928 return -EINVAL;
929 }
930}
931
932static u16 slim_slicecodefromsize(u32 req)
933{
934 u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
935 if (req >= 8)
936 return 0;
937 else
938 return codetosize[req];
939}
940
941static u16 slim_slicesize(u32 code)
942{
943 u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
944 if (code == 0)
945 code = 1;
946 if (code > 16)
947 code = 16;
948 return sizetocode[code - 1];
949}
950
951
952/* Message APIs Unicast message APIs used by slimbus slave drivers */
953
954/*
955 * Message API access routines.
956 * @sb: client handle requesting elemental message reads, writes.
957 * @msg: Input structure for start-offset, number of bytes to read.
958 * @rbuf: data buffer to be filled with values read.
959 * @len: data buffer size
960 * @wbuf: data buffer containing value/information to be written
961 * context: can sleep
962 * Returns:
963 * -EINVAL: Invalid parameters
964 * -ETIMEDOUT: If controller could not complete the request. This may happen if
965 * the bus lines are not clocked, controller is not powered-on, slave with
966 * given address is not enumerated/responding.
967 */
968int slim_request_val_element(struct slim_device *sb,
969 struct slim_ele_access *msg, u8 *buf, u8 len)
970{
971 struct slim_controller *ctrl = sb->ctrl;
972 if (!ctrl)
973 return -EINVAL;
974 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_VALUE, buf,
975 NULL, len);
976}
977EXPORT_SYMBOL_GPL(slim_request_val_element);
978
979int slim_request_inf_element(struct slim_device *sb,
980 struct slim_ele_access *msg, u8 *buf, u8 len)
981{
982 struct slim_controller *ctrl = sb->ctrl;
983 if (!ctrl)
984 return -EINVAL;
985 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_INFORMATION,
986 buf, NULL, len);
987}
988EXPORT_SYMBOL_GPL(slim_request_inf_element);
989
990int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
991 const u8 *buf, u8 len)
992{
993 struct slim_controller *ctrl = sb->ctrl;
994 if (!ctrl)
995 return -EINVAL;
996 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CHANGE_VALUE, NULL, buf,
997 len);
998}
999EXPORT_SYMBOL_GPL(slim_change_val_element);
1000
1001int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
1002 u8 *buf, u8 len)
1003{
1004 struct slim_controller *ctrl = sb->ctrl;
1005 if (!ctrl)
1006 return -EINVAL;
1007 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
1008 buf, len);
1009}
1010EXPORT_SYMBOL_GPL(slim_clear_inf_element);
1011
1012int slim_request_change_val_element(struct slim_device *sb,
1013 struct slim_ele_access *msg, u8 *rbuf,
1014 const u8 *wbuf, u8 len)
1015{
1016 struct slim_controller *ctrl = sb->ctrl;
1017 if (!ctrl)
1018 return -EINVAL;
1019 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
1020 rbuf, wbuf, len);
1021}
1022EXPORT_SYMBOL_GPL(slim_request_change_val_element);
1023
1024int slim_request_clear_inf_element(struct slim_device *sb,
1025 struct slim_ele_access *msg, u8 *rbuf,
1026 const u8 *wbuf, u8 len)
1027{
1028 struct slim_controller *ctrl = sb->ctrl;
1029 if (!ctrl)
1030 return -EINVAL;
1031 return slim_xfer_msg(ctrl, sb, msg,
1032 SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
1033 rbuf, wbuf, len);
1034}
1035EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
1036
1037/*
1038 * Broadcast message API:
1039 * call this API directly with sbdev = NULL.
1040 * For broadcast reads, make sure that buffers are big-enough to incorporate
1041 * replies from all logical addresses.
1042 * All controllers may not support broadcast
1043 */
1044int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001045 struct slim_ele_access *msg, u16 mc, u8 *rbuf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 const u8 *wbuf, u8 len)
1047{
1048 DECLARE_COMPLETION_ONSTACK(complete);
1049 int ret;
1050 u16 sl, cur;
1051 u16 ec;
1052 u8 tid, mlen = 6;
1053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
1055 if (ret)
1056 goto xfer_err;
1057
1058 sl = slim_slicesize(len);
1059 dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
1060 msg->start_offset, len, mc, sl);
1061
1062 cur = slim_slicecodefromsize(sl);
1063 ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
1064
1065 if (wbuf)
1066 mlen += len;
1067 if (rbuf) {
1068 mlen++;
1069 if (!msg->comp)
1070 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1071 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1072 &complete, sbdev->laddr, &tid);
1073 else
1074 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
1075 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1076 msg->comp, sbdev->laddr, &tid);
1077 /* sync read */
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001078 if (!ret && !msg->comp) {
1079 ret = wait_for_completion_timeout(&complete, HZ);
1080 if (!ret) {
1081 struct slim_msg_txn *txn;
1082 dev_err(&ctrl->dev, "slimbus Read timed out");
1083 mutex_lock(&ctrl->m_ctrl);
1084 txn = ctrl->txnt[tid];
1085 /* Invalidate the transaction */
1086 ctrl->txnt[tid] = NULL;
1087 mutex_unlock(&ctrl->m_ctrl);
1088 kfree(txn);
1089 ret = -ETIMEDOUT;
1090 } else
1091 ret = 0;
Sagar Dharia53a9f792012-09-04 19:56:18 -06001092 } else if (ret < 0 && !msg->comp) {
1093 struct slim_msg_txn *txn;
1094 dev_err(&ctrl->dev, "slimbus Read error");
1095 mutex_lock(&ctrl->m_ctrl);
1096 txn = ctrl->txnt[tid];
1097 /* Invalidate the transaction */
1098 ctrl->txnt[tid] = NULL;
1099 mutex_unlock(&ctrl->m_ctrl);
1100 kfree(txn);
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001101 }
1102
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 } else
1104 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
1105 SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1106 NULL, sbdev->laddr, NULL);
1107xfer_err:
1108 return ret;
1109}
1110EXPORT_SYMBOL_GPL(slim_xfer_msg);
1111
1112/*
1113 * slim_alloc_mgrports: Allocate port on manager side.
1114 * @sb: device/client handle.
1115 * @req: Port request type.
1116 * @nports: Number of ports requested
1117 * @rh: output buffer to store the port handles
1118 * @hsz: size of buffer storing handles
1119 * context: can sleep
1120 * This port will be typically used by SW. e.g. client driver wants to receive
1121 * some data from audio codec HW using a data channel.
1122 * Port allocated using this API will be used to receive the data.
1123 * If half-duplex ports are requested, two adjacent ports are allocated for
1124 * 1 half-duplex port. So the handle-buffer size should be twice the number
1125 * of half-duplex ports to be allocated.
1126 * -EDQUOT is returned if all ports are in use.
1127 */
1128int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1129 int nports, u32 *rh, int hsz)
1130{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001131 int i, j;
1132 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133 int nphysp = nports;
1134 struct slim_controller *ctrl = sb->ctrl;
1135
1136 if (!rh || !ctrl)
1137 return -EINVAL;
1138 if (req == SLIM_REQ_HALF_DUP)
1139 nphysp *= 2;
1140 if (hsz/sizeof(u32) < nphysp)
1141 return -EINVAL;
1142 mutex_lock(&ctrl->m_ctrl);
1143
1144 for (i = 0; i < ctrl->nports; i++) {
1145 bool multiok = true;
1146 if (ctrl->ports[i].state != SLIM_P_FREE)
1147 continue;
1148 /* Start half duplex channel at even port */
1149 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1150 continue;
1151 /* Allocate ports contiguously for multi-ch */
1152 if (ctrl->nports < (i + nphysp)) {
1153 i = ctrl->nports;
1154 break;
1155 }
1156 if (req == SLIM_REQ_MULTI_CH) {
1157 multiok = true;
1158 for (j = i; j < i + nphysp; j++) {
1159 if (ctrl->ports[j].state != SLIM_P_FREE) {
1160 multiok = false;
1161 break;
1162 }
1163 }
1164 if (!multiok)
1165 continue;
1166 }
1167 break;
1168 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001169 if (i >= ctrl->nports) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 ret = -EDQUOT;
Sagar Dharia100e7212013-05-17 18:20:57 -06001171 goto alloc_err;
1172 }
1173 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 for (j = i; j < i + nphysp; j++) {
1175 ctrl->ports[j].state = SLIM_P_UNCFG;
1176 ctrl->ports[j].req = req;
1177 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1178 ctrl->ports[j].flow = SLIM_SINK;
1179 else
1180 ctrl->ports[j].flow = SLIM_SRC;
Sagar Dharia100e7212013-05-17 18:20:57 -06001181 if (ctrl->alloc_port)
1182 ret = ctrl->alloc_port(ctrl, j);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 if (ret) {
1184 for (; j >= i; j--)
1185 ctrl->ports[j].state = SLIM_P_FREE;
1186 goto alloc_err;
1187 }
1188 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1189 }
1190alloc_err:
1191 mutex_unlock(&ctrl->m_ctrl);
1192 return ret;
1193}
1194EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1195
1196/* Deallocate the port(s) allocated using the API above */
1197int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1198{
1199 int i;
1200 struct slim_controller *ctrl = sb->ctrl;
1201
1202 if (!ctrl || !hdl)
1203 return -EINVAL;
1204
1205 mutex_lock(&ctrl->m_ctrl);
1206
1207 for (i = 0; i < nports; i++) {
1208 u8 pn;
1209 pn = SLIM_HDL_TO_PORT(hdl[i]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001210
1211 if (pn >= ctrl->nports || ctrl->ports[pn].state == SLIM_P_CFG) {
1212 int j, ret;
1213 if (pn >= ctrl->nports) {
1214 dev_err(&ctrl->dev, "invalid port number");
1215 ret = -EINVAL;
1216 } else {
1217 dev_err(&ctrl->dev,
1218 "Can't dealloc connected port:%d", i);
1219 ret = -EISCONN;
1220 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 for (j = i - 1; j >= 0; j--) {
1222 pn = SLIM_HDL_TO_PORT(hdl[j]);
1223 ctrl->ports[pn].state = SLIM_P_UNCFG;
1224 }
1225 mutex_unlock(&ctrl->m_ctrl);
Sagar Dharia100e7212013-05-17 18:20:57 -06001226 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 }
Sagar Dharia100e7212013-05-17 18:20:57 -06001228 if (ctrl->dealloc_port)
1229 ctrl->dealloc_port(ctrl, pn);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 ctrl->ports[pn].state = SLIM_P_FREE;
1231 }
1232 mutex_unlock(&ctrl->m_ctrl);
1233 return 0;
1234}
1235EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1236
1237/*
1238 * slim_get_slaveport: Get slave port handle
1239 * @la: slave device logical address.
1240 * @idx: port index at slave
1241 * @rh: return handle
1242 * @flw: Flow type (source or destination)
1243 * This API only returns a slave port's representation as expected by slimbus
1244 * driver. This port is not managed by the slimbus driver. Caller is expected
1245 * to have visibility of this port since it's a device-port.
1246 */
1247int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1248{
1249 if (rh == NULL)
1250 return -EINVAL;
1251 *rh = SLIM_PORT_HDL(la, flw, idx);
1252 return 0;
1253}
1254EXPORT_SYMBOL_GPL(slim_get_slaveport);
1255
1256static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1257 enum slim_port_flow flow)
1258{
1259 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001260 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001261 u8 buf[2];
1262 u32 la = SLIM_HDL_TO_LA(ph);
1263 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1264
1265 if (flow == SLIM_SRC)
1266 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1267 else
1268 mc = SLIM_MSG_MC_CONNECT_SINK;
1269 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001270 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 if (la == SLIM_LA_MANAGER)
1272 ctrl->ports[pn].flow = flow;
1273 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1274 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1275 NULL);
1276 if (!ret && la == SLIM_LA_MANAGER)
1277 ctrl->ports[pn].state = SLIM_P_CFG;
1278 return ret;
1279}
1280
1281static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1282{
1283 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001284 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 u32 la = SLIM_HDL_TO_LA(ph);
1286 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1287
1288 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1289 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1290 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1291 NULL, la, NULL);
1292 if (ret)
1293 return ret;
1294 if (la == SLIM_LA_MANAGER)
1295 ctrl->ports[pn].state = SLIM_P_UNCFG;
1296 return 0;
1297}
1298
1299/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001300 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001302 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001303 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001304 * Per slimbus specification, a channel may have 1 source port.
1305 * Channel specified in chanh needs to be allocated first.
1306 * Returns -EALREADY if source is already configured for this channel.
1307 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001308 * Returns -EINVAL if invalid direction is specified for non-manager port,
1309 * or if the manager side port number is out of bounds, or in incorrect state
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001311int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1312{
1313 struct slim_controller *ctrl = sb->ctrl;
1314 int ret;
1315 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1316 struct slim_ich *slc = &ctrl->chans[chan];
1317 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
Sagar Dharia100e7212013-05-17 18:20:57 -06001318 u8 la = SLIM_HDL_TO_LA(srch);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001319
Sagar Dharia100e7212013-05-17 18:20:57 -06001320 /* manager ports don't have direction when they are allocated */
1321 if (la != SLIM_LA_MANAGER && flow != SLIM_SRC)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001322 return -EINVAL;
1323
Sagar Dhariad2959352012-12-01 15:43:01 -07001324 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001325
Sagar Dharia100e7212013-05-17 18:20:57 -06001326 if (la == SLIM_LA_MANAGER) {
1327 u8 pn = SLIM_HDL_TO_PORT(srch);
1328 if (pn >= ctrl->nports ||
1329 ctrl->ports[pn].state != SLIM_P_UNCFG) {
1330 ret = -EINVAL;
1331 goto connect_src_err;
1332 }
1333 }
1334
Sagar Dharia29f35f02011-10-01 20:37:50 -06001335 if (slc->state == SLIM_CH_FREE) {
1336 ret = -ENOTCONN;
1337 goto connect_src_err;
1338 }
1339 /*
1340 * Once channel is removed, its ports can be considered disconnected
1341 * So its ports can be reassigned. Source port is zeroed
1342 * when channel is deallocated.
1343 */
1344 if (slc->srch) {
1345 ret = -EALREADY;
1346 goto connect_src_err;
1347 }
1348
1349 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1350
1351 if (!ret)
1352 slc->srch = srch;
1353
1354connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001355 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001356 return ret;
1357}
1358EXPORT_SYMBOL_GPL(slim_connect_src);
1359
1360/*
1361 * slim_connect_sink: Connect sink port(s) to channel.
1362 * @sb: client handle
1363 * @sinkh: sink handle(s) to be connected to this channel
1364 * @nsink: number of sinks
1365 * @chanh: Channel with which the ports need to be associated with.
1366 * Per slimbus specification, a channel may have multiple sink-ports.
1367 * Channel specified in chanh needs to be allocated first.
1368 * Returns -EALREADY if sink is already configured for this channel.
1369 * Returns -ENOTCONN if channel is not allocated
Sagar Dharia100e7212013-05-17 18:20:57 -06001370 * Returns -EINVAL if invalid parameters are passed, or invalid direction is
1371 * specified for non-manager port, or if the manager side port number is out of
1372 * bounds, or in incorrect state
Sagar Dharia29f35f02011-10-01 20:37:50 -06001373 */
1374int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375{
1376 struct slim_controller *ctrl = sb->ctrl;
1377 int j;
1378 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001379 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380 struct slim_ich *slc = &ctrl->chans[chan];
1381
Sagar Dharia29f35f02011-10-01 20:37:50 -06001382 if (!sinkh || !nsink)
1383 return -EINVAL;
1384
Sagar Dhariad2959352012-12-01 15:43:01 -07001385 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386
1387 /*
1388 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001389 * So its ports can be reassigned. Sink ports are freed when channel
1390 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001391 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001392 if (slc->state == SLIM_CH_FREE) {
1393 ret = -ENOTCONN;
1394 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001396
Sagar Dharia29f35f02011-10-01 20:37:50 -06001397 for (j = 0; j < nsink; j++) {
1398 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
Sagar Dharia100e7212013-05-17 18:20:57 -06001399 u8 la = SLIM_HDL_TO_LA(sinkh[j]);
1400 u8 pn = SLIM_HDL_TO_PORT(sinkh[j]);
1401 if (la != SLIM_LA_MANAGER && flow != SLIM_SINK)
Sagar Dharia29f35f02011-10-01 20:37:50 -06001402 ret = -EINVAL;
Sagar Dharia100e7212013-05-17 18:20:57 -06001403 else if (la == SLIM_LA_MANAGER &&
1404 (pn >= ctrl->nports ||
1405 ctrl->ports[pn].state != SLIM_P_UNCFG))
1406 ret = -EINVAL;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001407 else
1408 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001410 for (j = j - 1; j >= 0; j--)
1411 disconnect_port_ch(ctrl, sinkh[j]);
1412 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 }
1414 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001415
1416 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1417 GFP_KERNEL);
1418 if (!slc->sinkh) {
1419 ret = -ENOMEM;
1420 for (j = 0; j < nsink; j++)
1421 disconnect_port_ch(ctrl, sinkh[j]);
1422 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423 }
1424
Sagar Dharia29f35f02011-10-01 20:37:50 -06001425 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1426 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427
Sagar Dharia29f35f02011-10-01 20:37:50 -06001428connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001429 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430 return ret;
1431}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001432EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433
1434/*
1435 * slim_disconnect_ports: Disconnect port(s) from channel
1436 * @sb: client handle
1437 * @ph: ports to be disconnected
1438 * @nph: number of ports.
1439 * Disconnects ports from a channel.
1440 */
1441int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1442{
1443 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001444 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001445
Sagar Dhariad2959352012-12-01 15:43:01 -07001446 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 for (i = 0; i < nph; i++)
1449 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001450 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451 return 0;
1452}
1453EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1454
1455/*
1456 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1457 * @sb: client handle
1458 * @ph: port-handle
1459 * @iobuf: buffer to be transferred or populated
1460 * @len: buffer size.
1461 * @comp: completion signal to indicate transfer done or error.
1462 * context: can sleep
1463 * Returns number of bytes transferred/received if used synchronously.
1464 * Will return 0 if used asynchronously.
1465 * Client will call slim_port_get_xfer_status to get error and/or number of
1466 * bytes transferred if used asynchronously.
1467 */
1468int slim_port_xfer(struct slim_device *sb, u32 ph, u8 *iobuf, u32 len,
1469 struct completion *comp)
1470{
1471 struct slim_controller *ctrl = sb->ctrl;
1472 u8 pn = SLIM_HDL_TO_PORT(ph);
1473 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1474 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1475}
1476EXPORT_SYMBOL_GPL(slim_port_xfer);
1477
1478/*
1479 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1480 * after completion is done.
1481 * @sb: client handle
1482 * @ph: port-handle
1483 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1484 * @done_len: Number of bytes transferred.
1485 * This can be called when port_xfer complition is signalled.
1486 * The API will return port transfer error (underflow/overflow/disconnect)
1487 * and/or done_len will reflect number of bytes transferred. Note that
1488 * done_len may be valid even if port error (overflow/underflow) has happened.
1489 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1490 * client has not supplied more data to be transferred, done_len will indicate
1491 * number of bytes transferred with underflow error. To avoid frequent underflow
1492 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1493 * channel has data to be transferred even if client is not ready to transfer
1494 * data all the time. done_buf will indicate address of the last buffer
1495 * processed from the multiple transfers.
1496 */
1497enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
1498 u8 **done_buf, u32 *done_len)
1499{
1500 struct slim_controller *ctrl = sb->ctrl;
1501 u8 pn = SLIM_HDL_TO_PORT(ph);
1502 u32 la = SLIM_HDL_TO_LA(ph);
1503 enum slim_port_err err;
1504 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1505 /*
1506 * Framework only has insight into ports managed by ported device
1507 * used by the manager and not slave
1508 */
1509 if (la != SLIM_LA_MANAGER) {
1510 if (done_buf)
1511 *done_buf = NULL;
1512 if (done_len)
1513 *done_len = 0;
1514 return SLIM_P_NOT_OWNED;
1515 }
1516 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1517 if (err == SLIM_P_INPROGRESS)
1518 err = ctrl->ports[pn].err;
1519 return err;
1520}
1521EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1522
1523static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1524{
1525 struct slim_ich **arr;
1526 int i, j;
1527 int *len;
1528 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001529 /* Channel is already active and other end is transmitting data */
1530 if (slc->state >= SLIM_CH_ACTIVE)
1531 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532 if (slc->coeff == SLIM_COEFF_1) {
1533 arr = ctrl->sched.chc1;
1534 len = &ctrl->sched.num_cc1;
1535 } else {
1536 arr = ctrl->sched.chc3;
1537 len = &ctrl->sched.num_cc3;
1538 sl *= 3;
1539 }
1540
1541 *len += 1;
1542
1543 /* Insert the channel based on rootexp and seglen */
1544 for (i = 0; i < *len - 1; i++) {
1545 /*
1546 * Primary key: exp low to high.
1547 * Secondary key: seglen: high to low
1548 */
1549 if ((slc->rootexp > arr[i]->rootexp) ||
1550 ((slc->rootexp == arr[i]->rootexp) &&
1551 (slc->seglen < arr[i]->seglen)))
1552 continue;
1553 else
1554 break;
1555 }
1556 for (j = *len - 1; j > i; j--)
1557 arr[j] = arr[j - 1];
1558 arr[i] = slc;
1559 ctrl->sched.usedslots += sl;
1560
1561 return;
1562}
1563
1564static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1565{
1566 struct slim_ich **arr;
1567 int i;
1568 u32 la, ph;
1569 int *len;
1570 if (slc->coeff == SLIM_COEFF_1) {
1571 arr = ctrl->sched.chc1;
1572 len = &ctrl->sched.num_cc1;
1573 } else {
1574 arr = ctrl->sched.chc3;
1575 len = &ctrl->sched.num_cc3;
1576 }
1577
1578 for (i = 0; i < *len; i++) {
1579 if (arr[i] == slc)
1580 break;
1581 }
1582 if (i >= *len)
1583 return -EXFULL;
1584 for (; i < *len - 1; i++)
1585 arr[i] = arr[i + 1];
1586 *len -= 1;
1587 arr[*len] = NULL;
1588
1589 slc->state = SLIM_CH_ALLOCATED;
1590 slc->newintr = 0;
1591 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001592 for (i = 0; i < slc->nsink; i++) {
1593 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594 la = SLIM_HDL_TO_LA(ph);
1595 /*
1596 * For ports managed by manager's ported device, no need to send
1597 * disconnect. It is client's responsibility to call disconnect
1598 * on ports owned by the slave device
1599 */
1600 if (la == SLIM_LA_MANAGER)
1601 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1602 }
1603
Sagar Dharia29f35f02011-10-01 20:37:50 -06001604 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 la = SLIM_HDL_TO_LA(ph);
1606 if (la == SLIM_LA_MANAGER)
1607 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1608
Sagar Dharia29f35f02011-10-01 20:37:50 -06001609 kfree(slc->sinkh);
1610 slc->sinkh = NULL;
1611 slc->srch = 0;
1612 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613 return 0;
1614}
1615
1616static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1617{
1618 u32 rate = 0, rate4k = 0, rate11k = 0;
1619 u32 exp = 0;
1620 u32 pr = 0;
1621 bool exact = true;
1622 bool done = false;
1623 enum slim_ch_rate ratefam;
1624
1625 if (prop->prot >= SLIM_PUSH)
1626 return 0;
1627 if (prop->baser == SLIM_RATE_1HZ) {
1628 rate = prop->ratem / 4000;
1629 rate4k = rate;
1630 if (rate * 4000 == prop->ratem)
1631 ratefam = SLIM_RATE_4000HZ;
1632 else {
1633 rate = prop->ratem / 11025;
1634 rate11k = rate;
1635 if (rate * 11025 == prop->ratem)
1636 ratefam = SLIM_RATE_11025HZ;
1637 else
1638 ratefam = SLIM_RATE_1HZ;
1639 }
1640 } else {
1641 ratefam = prop->baser;
1642 rate = prop->ratem;
1643 }
1644 if (ratefam == SLIM_RATE_1HZ) {
1645 exact = false;
1646 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1647 rate = rate4k + 1;
1648 ratefam = SLIM_RATE_4000HZ;
1649 } else {
1650 rate = rate11k + 1;
1651 ratefam = SLIM_RATE_11025HZ;
1652 }
1653 }
1654 /* covert rate to coeff-exp */
1655 while (!done) {
1656 while ((rate & 0x1) != 0x1) {
1657 rate >>= 1;
1658 exp++;
1659 }
1660 if (rate > 3) {
1661 /* roundup if not exact */
1662 rate++;
1663 exact = false;
1664 } else
1665 done = true;
1666 }
1667 if (ratefam == SLIM_RATE_4000HZ) {
1668 if (rate == 1)
1669 pr = 0x10;
1670 else {
1671 pr = 0;
1672 exp++;
1673 }
1674 } else {
1675 pr = 8;
1676 exp++;
1677 }
1678 if (exp <= 7) {
1679 pr |= exp;
1680 if (exact)
1681 pr |= 0x80;
1682 } else
1683 pr = 0;
1684 return pr;
1685}
1686
1687static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1688{
1689 struct slim_controller *ctrl = sb->ctrl;
1690 u32 chrate = 0;
1691 u32 exp = 0;
1692 u32 coeff = 0;
1693 bool exact = true;
1694 bool done = false;
1695 int ret = 0;
1696 struct slim_ich *slc = &ctrl->chans[chan];
1697 struct slim_ch *prop = &slc->prop;
1698
1699 slc->prrate = slim_calc_prrate(ctrl, prop);
1700 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1701 if (prop->baser == SLIM_RATE_4000HZ)
1702 chrate = 4000 * prop->ratem;
1703 else if (prop->baser == SLIM_RATE_11025HZ)
1704 chrate = 11025 * prop->ratem;
1705 else
1706 chrate = prop->ratem;
1707 /* max allowed sample freq = 768 seg/frame */
1708 if (chrate > 3600000)
1709 return -EDQUOT;
1710 if (prop->baser == SLIM_RATE_4000HZ &&
1711 ctrl->a_framer->superfreq == 4000)
1712 coeff = prop->ratem;
1713 else if (prop->baser == SLIM_RATE_11025HZ &&
1714 ctrl->a_framer->superfreq == 3675)
1715 coeff = 3 * prop->ratem;
1716 else {
1717 u32 tempr = 0;
1718 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1719 coeff = tempr / ctrl->a_framer->rootfreq;
1720 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1721 coeff++;
1722 exact = false;
1723 }
1724 }
1725
1726 /* convert coeff to coeff-exponent */
1727 exp = 0;
1728 while (!done) {
1729 while ((coeff & 0x1) != 0x1) {
1730 coeff >>= 1;
1731 exp++;
1732 }
1733 if (coeff > 3) {
1734 coeff++;
1735 exact = false;
1736 } else
1737 done = true;
1738 }
1739 if (prop->prot == SLIM_HARD_ISO && !exact)
1740 return -EPROTONOSUPPORT;
1741 else if (prop->prot == SLIM_AUTO_ISO) {
1742 if (exact)
1743 prop->prot = SLIM_HARD_ISO;
1744 else {
1745 /* Push-Pull not supported for now */
1746 return -EPROTONOSUPPORT;
1747 }
1748 }
1749 slc->rootexp = exp;
1750 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1751 if (prop->prot != SLIM_HARD_ISO)
1752 slc->seglen++;
1753 if (prop->prot >= SLIM_EXT_SMPLX)
1754 slc->seglen++;
1755 /* convert coeff to enum */
1756 if (coeff == 1) {
1757 if (exp > 9)
1758 ret = -EIO;
1759 coeff = SLIM_COEFF_1;
1760 } else {
1761 if (exp > 8)
1762 ret = -EIO;
1763 coeff = SLIM_COEFF_3;
1764 }
1765 slc->coeff = coeff;
1766
1767 return ret;
1768}
1769
1770/*
1771 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1772 * @sb: client handle.
1773 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001774 * Slimbus channels are limited to 256 per specification.
1775 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 * Although slimbus specification supports 256 channels, a controller may not
1777 * support that many channels.
1778 */
1779int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1780{
1781 struct slim_controller *ctrl = sb->ctrl;
1782 u16 i;
1783
1784 if (!ctrl)
1785 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001786 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 for (i = 0; i < ctrl->nchans; i++) {
1788 if (ctrl->chans[i].state == SLIM_CH_FREE)
1789 break;
1790 }
1791 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001792 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793 return -EXFULL;
1794 }
1795 *chanh = i;
1796 ctrl->chans[i].nextgrp = 0;
1797 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001798 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799
Sagar Dhariad2959352012-12-01 15:43:01 -07001800 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 return 0;
1802}
1803EXPORT_SYMBOL_GPL(slim_alloc_ch);
1804
1805/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001806 * slim_query_ch: Get reference-counted handle for a channel number. Every
1807 * channel is reference counted by upto one as producer and the others as
1808 * consumer)
1809 * @sb: client handle
1810 * @chan: slimbus channel number
1811 * @chanh: return channel handle
1812 * If request channel number is not in use, it is allocated, and reference
1813 * count is set to one. If the channel was was already allocated, this API
1814 * will return handle to that channel and reference count is incremented.
1815 * -EXFULL is returned if all channels are in use
1816 */
1817int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1818{
1819 struct slim_controller *ctrl = sb->ctrl;
1820 u16 i, j;
1821 int ret = 0;
1822 if (!ctrl || !chanh)
1823 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001824 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001825 /* start with modulo number */
1826 i = ch % ctrl->nchans;
1827
1828 for (j = 0; j < ctrl->nchans; j++) {
1829 if (ctrl->chans[i].chan == ch) {
1830 *chanh = i;
1831 ctrl->chans[i].ref++;
1832 if (ctrl->chans[i].state == SLIM_CH_FREE)
1833 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1834 goto query_out;
1835 }
1836 i = (i + 1) % ctrl->nchans;
1837 }
1838
1839 /* Channel not in table yet */
1840 ret = -EXFULL;
1841 for (j = 0; j < ctrl->nchans; j++) {
1842 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1843 ctrl->chans[i].state =
1844 SLIM_CH_ALLOCATED;
1845 *chanh = i;
1846 ctrl->chans[i].ref++;
1847 ctrl->chans[i].chan = ch;
1848 ctrl->chans[i].nextgrp = 0;
1849 ret = 0;
1850 break;
1851 }
1852 i = (i + 1) % ctrl->nchans;
1853 }
1854query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001855 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001856 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1857 ch, i, ctrl->chans[i].ref, ret);
1858 return ret;
1859}
1860EXPORT_SYMBOL_GPL(slim_query_ch);
1861
1862/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 * slim_dealloc_ch: Deallocate channel allocated using the API above
1864 * -EISCONN is returned if the channel is tried to be deallocated without
1865 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001866 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1867 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 */
1869int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1870{
1871 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001872 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001873 struct slim_ich *slc = &ctrl->chans[chan];
1874 if (!ctrl)
1875 return -EINVAL;
1876
Sagar Dhariad2959352012-12-01 15:43:01 -07001877 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001878 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001879 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001880 return -ENOTCONN;
1881 }
1882 if (slc->ref > 1) {
1883 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001884 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001885 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1886 slc->chan, chanh, slc->ref);
1887 return 0;
1888 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1890 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001891 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 return -EISCONN;
1893 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001894 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001896 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001897 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1898 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899 return 0;
1900}
1901EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1902
1903/*
1904 * slim_get_ch_state: Channel state.
1905 * This API returns the channel's state (active, suspended, inactive etc)
1906 */
1907enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1908{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001909 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 struct slim_ich *slc = &sb->ctrl->chans[chan];
1911 return slc->state;
1912}
1913EXPORT_SYMBOL_GPL(slim_get_ch_state);
1914
1915/*
1916 * slim_define_ch: Define a channel.This API defines channel parameters for a
1917 * given channel.
1918 * @sb: client handle.
1919 * @prop: slim_ch structure with channel parameters desired to be used.
1920 * @chanh: list of channels to be defined.
1921 * @nchan: number of channels in a group (1 if grp is false)
1922 * @grp: Are the channels grouped
1923 * @grph: return group handle if grouping of channels is desired.
1924 * Channels can be grouped if multiple channels use same parameters
1925 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1926 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001927 * -EISCONN is returned if channel is already used with different parameters.
1928 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 */
1930int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1931 u8 nchan, bool grp, u16 *grph)
1932{
1933 struct slim_controller *ctrl = sb->ctrl;
1934 int i, ret = 0;
1935
1936 if (!ctrl || !chanh || !prop || !nchan)
1937 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001938 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001939 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001940 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001941 struct slim_ich *slc = &ctrl->chans[chan];
1942 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001944 if (slc->state < SLIM_CH_ALLOCATED) {
1945 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 goto err_define_ch;
1947 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001948 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1949 if (prop->ratem != slc->prop.ratem ||
1950 prop->sampleszbits != slc->prop.sampleszbits ||
1951 prop->baser != slc->prop.baser) {
1952 ret = -EISCONN;
1953 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001955 } else if (slc->state > SLIM_CH_DEFINED) {
1956 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001958 } else {
1959 ctrl->chans[chan].prop = *prop;
1960 ret = slim_nextdefine_ch(sb, chan);
1961 if (ret)
1962 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001963 }
1964 if (i < (nchan - 1))
1965 ctrl->chans[chan].nextgrp = chanh[i + 1];
1966 if (i == 0)
1967 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1968 if (i == (nchan - 1))
1969 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 }
1971
1972 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001973 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001974 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001975 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001976 struct slim_ich *slc = &ctrl->chans[chan];
1977 if (slc->state == SLIM_CH_ALLOCATED)
1978 slc->state = SLIM_CH_DEFINED;
1979 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001981 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07001982 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(slim_define_ch);
1986
1987static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
1988{
1989 u32 code = 0;
1990 if (*ctrlw == *subfrml) {
1991 *ctrlw = 8;
1992 *subfrml = 8;
1993 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
1994 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
1995 return 0;
1996 }
1997 if (*subfrml == 6) {
1998 code = 0;
1999 *msgsl = 256;
2000 } else if (*subfrml == 8) {
2001 code = 1;
2002 *msgsl = 192;
2003 } else if (*subfrml == 24) {
2004 code = 2;
2005 *msgsl = 64;
2006 } else { /* 32 */
2007 code = 3;
2008 *msgsl = 48;
2009 }
2010
2011 if (*ctrlw < 8) {
2012 if (*ctrlw >= 6) {
2013 *ctrlw = 6;
2014 code |= 0x14;
2015 } else {
2016 if (*ctrlw == 5)
2017 *ctrlw = 4;
2018 code |= (*ctrlw << 2);
2019 }
2020 } else {
2021 code -= 2;
2022 if (*ctrlw >= 24) {
2023 *ctrlw = 24;
2024 code |= 0x1e;
2025 } else if (*ctrlw >= 16) {
2026 *ctrlw = 16;
2027 code |= 0x1c;
2028 } else if (*ctrlw >= 12) {
2029 *ctrlw = 12;
2030 code |= 0x1a;
2031 } else {
2032 *ctrlw = 8;
2033 code |= 0x18;
2034 }
2035 }
2036
2037 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2038 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2039 return code;
2040}
2041
2042static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
2043 int sz, u32 shft)
2044{
2045 int i;
2046 u32 oldoff;
2047 for (i = 0; i < sz; i++) {
2048 struct slim_ich *slc;
2049 if (ach[i] == NULL)
2050 continue;
2051 slc = ach[i];
2052 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2053 continue;
2054 oldoff = slc->newoff;
2055 slc->newoff += shft;
2056 /* seg. offset must be <= interval */
2057 if (slc->newoff >= slc->newintr)
2058 slc->newoff -= slc->newintr;
2059 }
2060}
2061
2062static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
2063 u32 *ctrlw, u32 *subfrml)
2064{
2065 int coeff1, coeff3;
2066 enum slim_ch_coeff bias;
2067 struct slim_controller *ctrl = sb->ctrl;
2068 int last1 = ctrl->sched.num_cc1 - 1;
2069 int last3 = ctrl->sched.num_cc3 - 1;
2070
2071 /*
2072 * Find first channels with coeff 1 & 3 as starting points for
2073 * scheduling
2074 */
2075 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
2076 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
2077 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2078 continue;
2079 else
2080 break;
2081 }
2082 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
2083 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
2084 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2085 continue;
2086 else
2087 break;
2088 }
2089 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
2090 *ctrlw = 8;
2091 *subfrml = 8;
2092 return 0;
2093 } else if (coeff3 == ctrl->sched.num_cc3)
2094 bias = SLIM_COEFF_1;
2095 else
2096 bias = SLIM_COEFF_3;
2097
2098 /*
2099 * Find last chan in coeff1, 3 list, we will use to know when we
2100 * have done scheduling all coeff1 channels
2101 */
2102 while (last1 >= 0) {
2103 if (ctrl->sched.chc1[last1] != NULL &&
2104 (ctrl->sched.chc1[last1])->state !=
2105 SLIM_CH_PENDING_REMOVAL)
2106 break;
2107 last1--;
2108 }
2109 while (last3 >= 0) {
2110 if (ctrl->sched.chc3[last3] != NULL &&
2111 (ctrl->sched.chc3[last3])->state !=
2112 SLIM_CH_PENDING_REMOVAL)
2113 break;
2114 last3--;
2115 }
2116
2117 if (bias == SLIM_COEFF_1) {
2118 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
2119 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2120 int curexp, finalexp;
2121 u32 curintr, curmaxsl;
2122 int opensl1[2];
2123 int maxctrlw1;
2124
2125 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2126 curexp = (int)expshft - 1;
2127
2128 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2129 curmaxsl = curintr >> 1;
2130 opensl1[0] = opensl1[1] = curmaxsl;
2131
2132 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2133 curintr >>= 1;
2134 curmaxsl >>= 1;
2135
2136 /* update 4K family open slot records */
2137 if (opensl1[1] < opensl1[0])
2138 opensl1[1] -= curmaxsl;
2139 else
2140 opensl1[1] = opensl1[0] - curmaxsl;
2141 opensl1[0] = curmaxsl;
2142 if (opensl1[1] < 0) {
2143 opensl1[0] += opensl1[1];
2144 opensl1[1] = 0;
2145 }
2146 if (opensl1[0] <= 0) {
2147 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2148 __LINE__);
2149 return -EXFULL;
2150 }
2151 curexp++;
2152 /* schedule 4k family channels */
2153
2154 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2155 (int)(slc1->rootexp + expshft))) {
2156 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2157 coeff1++;
2158 slc1 = ctrl->sched.chc1[coeff1];
2159 continue;
2160 }
2161 if (opensl1[1] >= opensl1[0] ||
2162 (finalexp == (int)slc1->rootexp &&
2163 curintr <= 24 &&
2164 opensl1[0] == curmaxsl)) {
2165 opensl1[1] -= slc1->seglen;
2166 slc1->newoff = curmaxsl + opensl1[1];
2167 if (opensl1[1] < 0 &&
2168 opensl1[0] == curmaxsl) {
2169 opensl1[0] += opensl1[1];
2170 opensl1[1] = 0;
2171 if (opensl1[0] < 0) {
2172 dev_dbg(&ctrl->dev,
2173 "reconfig failed:%d\n",
2174 __LINE__);
2175 return -EXFULL;
2176 }
2177 }
2178 } else {
2179 if (slc1->seglen > opensl1[0]) {
2180 dev_dbg(&ctrl->dev,
2181 "reconfig failed:%d\n",
2182 __LINE__);
2183 return -EXFULL;
2184 }
2185 slc1->newoff = opensl1[0] -
2186 slc1->seglen;
2187 opensl1[0] = slc1->newoff;
2188 }
2189 slc1->newintr = curintr;
2190 coeff1++;
2191 slc1 = ctrl->sched.chc1[coeff1];
2192 }
2193 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002194 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002195 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002196 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 if (opensl1[1] > opensl1[0]) {
2198 int temp = opensl1[0];
2199 opensl1[0] = opensl1[1];
2200 opensl1[1] = temp;
2201 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2202 ctrl->sched.num_cc1, curmaxsl);
2203 }
2204 /* choose subframe mode to maximize bw */
2205 maxctrlw1 = opensl1[0];
2206 if (opensl1[0] == curmaxsl)
2207 maxctrlw1 += opensl1[1];
2208 if (curintr >= 24) {
2209 *subfrml = 24;
2210 *ctrlw = maxctrlw1;
2211 } else if (curintr == 12) {
2212 if (maxctrlw1 > opensl1[1] * 4) {
2213 *subfrml = 24;
2214 *ctrlw = maxctrlw1;
2215 } else {
2216 *subfrml = 6;
2217 *ctrlw = opensl1[1];
2218 }
2219 } else {
2220 *subfrml = 6;
2221 *ctrlw = maxctrlw1;
2222 }
2223 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002224 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002225 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2226 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2227 int curexp, finalexp, exp1;
2228 u32 curintr, curmaxsl;
2229 int opensl3[2];
2230 int opensl1[6];
2231 bool opensl1valid = false;
2232 int maxctrlw1, maxctrlw3, i;
2233 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2234 if (last1 >= 0) {
2235 slc1 = ctrl->sched.chc1[coeff1];
2236 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2237 if (exp1 > finalexp)
2238 finalexp = exp1;
2239 }
2240 curexp = (int)expshft - 1;
2241
2242 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2243 curmaxsl = curintr >> 1;
2244 opensl3[0] = opensl3[1] = curmaxsl;
2245
2246 while (coeff1 < ctrl->sched.num_cc1 ||
2247 coeff3 < ctrl->sched.num_cc3 ||
2248 curintr > 32) {
2249 curintr >>= 1;
2250 curmaxsl >>= 1;
2251
2252 /* update 12k family open slot records */
2253 if (opensl3[1] < opensl3[0])
2254 opensl3[1] -= curmaxsl;
2255 else
2256 opensl3[1] = opensl3[0] - curmaxsl;
2257 opensl3[0] = curmaxsl;
2258 if (opensl3[1] < 0) {
2259 opensl3[0] += opensl3[1];
2260 opensl3[1] = 0;
2261 }
2262 if (opensl3[0] <= 0) {
2263 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2264 __LINE__);
2265 return -EXFULL;
2266 }
2267 curexp++;
2268
2269 /* schedule 12k family channels */
2270 while (coeff3 < ctrl->sched.num_cc3 &&
2271 curexp == (int)slc3->rootexp + expshft) {
2272 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2273 coeff3++;
2274 slc3 = ctrl->sched.chc3[coeff3];
2275 continue;
2276 }
2277 opensl1valid = false;
2278 if (opensl3[1] >= opensl3[0] ||
2279 (finalexp == (int)slc3->rootexp &&
2280 curintr <= 32 &&
2281 opensl3[0] == curmaxsl &&
2282 last1 < 0)) {
2283 opensl3[1] -= slc3->seglen;
2284 slc3->newoff = curmaxsl + opensl3[1];
2285 if (opensl3[1] < 0 &&
2286 opensl3[0] == curmaxsl) {
2287 opensl3[0] += opensl3[1];
2288 opensl3[1] = 0;
2289 }
2290 if (opensl3[0] < 0) {
2291 dev_dbg(&ctrl->dev,
2292 "reconfig failed:%d\n",
2293 __LINE__);
2294 return -EXFULL;
2295 }
2296 } else {
2297 if (slc3->seglen > opensl3[0]) {
2298 dev_dbg(&ctrl->dev,
2299 "reconfig failed:%d\n",
2300 __LINE__);
2301 return -EXFULL;
2302 }
2303 slc3->newoff = opensl3[0] -
2304 slc3->seglen;
2305 opensl3[0] = slc3->newoff;
2306 }
2307 slc3->newintr = curintr;
2308 coeff3++;
2309 slc3 = ctrl->sched.chc3[coeff3];
2310 }
2311 /* update 4k openslot records */
2312 if (opensl1valid == false) {
2313 for (i = 0; i < 3; i++) {
2314 opensl1[i * 2] = opensl3[0];
2315 opensl1[(i * 2) + 1] = opensl3[1];
2316 }
2317 } else {
2318 int opensl1p[6];
2319 memcpy(opensl1p, opensl1, sizeof(opensl1));
2320 for (i = 0; i < 3; i++) {
2321 if (opensl1p[i] < opensl1p[i + 3])
2322 opensl1[(i * 2) + 1] =
2323 opensl1p[i];
2324 else
2325 opensl1[(i * 2) + 1] =
2326 opensl1p[i + 3];
2327 }
2328 for (i = 0; i < 3; i++) {
2329 opensl1[(i * 2) + 1] -= curmaxsl;
2330 opensl1[i * 2] = curmaxsl;
2331 if (opensl1[(i * 2) + 1] < 0) {
2332 opensl1[i * 2] +=
2333 opensl1[(i * 2) + 1];
2334 opensl1[(i * 2) + 1] = 0;
2335 }
2336 if (opensl1[i * 2] < 0) {
2337 dev_dbg(&ctrl->dev,
2338 "reconfig failed:%d\n",
2339 __LINE__);
2340 return -EXFULL;
2341 }
2342 }
2343 }
2344 /* schedule 4k family channels */
2345 while (coeff1 < ctrl->sched.num_cc1 &&
2346 curexp == (int)slc1->rootexp + expshft) {
2347 /* searchorder effective when opensl valid */
2348 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2349 int maxopensl = 0;
2350 int maxi = 0;
2351 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2352 coeff1++;
2353 slc1 = ctrl->sched.chc1[coeff1];
2354 continue;
2355 }
2356 opensl1valid = true;
2357 for (i = 0; i < 6; i++) {
2358 if (opensl1[srcho[i]] > maxopensl) {
2359 maxopensl = opensl1[srcho[i]];
2360 maxi = srcho[i];
2361 }
2362 }
2363 opensl1[maxi] -= slc1->seglen;
2364 slc1->newoff = (curmaxsl * maxi) +
2365 opensl1[maxi];
2366 if (opensl1[maxi] < 0) {
2367 if (((maxi & 1) == 1) &&
2368 (opensl1[maxi - 1] == curmaxsl)) {
2369 opensl1[maxi - 1] +=
2370 opensl1[maxi];
2371 if (opensl3[0] >
2372 opensl1[maxi - 1])
2373 opensl3[0] =
2374 opensl1[maxi - 1];
2375 opensl3[1] = 0;
2376 opensl1[maxi] = 0;
2377 if (opensl1[maxi - 1] < 0) {
2378 dev_dbg(&ctrl->dev,
2379 "reconfig failed:%d\n",
2380 __LINE__);
2381 return -EXFULL;
2382 }
2383 } else {
2384 dev_dbg(&ctrl->dev,
2385 "reconfig failed:%d\n",
2386 __LINE__);
2387 return -EXFULL;
2388 }
2389 } else {
2390 if (opensl3[maxi & 1] > opensl1[maxi])
2391 opensl3[maxi & 1] =
2392 opensl1[maxi];
2393 }
2394 slc1->newintr = curintr * 3;
2395 coeff1++;
2396 slc1 = ctrl->sched.chc1[coeff1];
2397 }
2398 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002399 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002400 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002401 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002402 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2403 if (opensl3[1] > opensl3[0]) {
2404 int temp = opensl3[0];
2405 opensl3[0] = opensl3[1];
2406 opensl3[1] = temp;
2407 temp = opensl1[5];
2408 opensl1[5] = opensl1[4];
2409 opensl1[4] = opensl1[3];
2410 opensl1[3] = opensl1[2];
2411 opensl1[2] = opensl1[1];
2412 opensl1[1] = opensl1[0];
2413 opensl1[0] = temp;
2414 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2415 ctrl->sched.num_cc1, curmaxsl);
2416 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2417 ctrl->sched.num_cc3, curmaxsl);
2418 }
2419 /* subframe mode to maximize BW */
2420 maxctrlw3 = opensl3[0];
2421 maxctrlw1 = opensl1[0];
2422 if (opensl3[0] == curmaxsl)
2423 maxctrlw3 += opensl3[1];
2424 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2425 maxctrlw1 += opensl1[i + 1];
2426 if (curintr >= 32) {
2427 *subfrml = 32;
2428 *ctrlw = maxctrlw3;
2429 } else if (curintr == 16) {
2430 if (maxctrlw3 > (opensl3[1] * 4)) {
2431 *subfrml = 32;
2432 *ctrlw = maxctrlw3;
2433 } else {
2434 *subfrml = 8;
2435 *ctrlw = opensl3[1];
2436 }
2437 } else {
2438 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2439 *subfrml = 24;
2440 *ctrlw = maxctrlw1;
2441 } else {
2442 *subfrml = 8;
2443 *ctrlw = maxctrlw3;
2444 }
2445 }
2446 }
2447 return 0;
2448}
2449
2450#ifdef DEBUG
2451static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2452 u32 subfrml, u32 clkgear)
2453{
2454 int sl, i;
2455 int cc1 = 0;
2456 int cc3 = 0;
2457 struct slim_ich *slc = NULL;
2458 if (!ctrl->sched.slots)
2459 return 0;
2460 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2461 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2462 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2463 for (i = 0; i < ctrlw; i++)
2464 ctrl->sched.slots[sl + i] = 33;
2465 }
2466 while (cc1 < ctrl->sched.num_cc1) {
2467 slc = ctrl->sched.chc1[cc1];
2468 if (slc == NULL) {
2469 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2470 cc1);
2471 return -EIO;
2472 }
2473 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2474 (slc - ctrl->chans), slc->newoff,
2475 slc->newintr, slc->seglen);
2476
2477 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2478 for (sl = slc->newoff;
2479 sl < SLIM_SL_PER_SUPERFRAME;
2480 sl += slc->newintr) {
2481 for (i = 0; i < slc->seglen; i++) {
2482 if (ctrl->sched.slots[sl + i])
2483 return -EXFULL;
2484 ctrl->sched.slots[sl + i] = cc1 + 1;
2485 }
2486 }
2487 }
2488 cc1++;
2489 }
2490 while (cc3 < ctrl->sched.num_cc3) {
2491 slc = ctrl->sched.chc3[cc3];
2492 if (slc == NULL) {
2493 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2494 cc3);
2495 return -EIO;
2496 }
2497 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2498 (slc - ctrl->chans), slc->newoff,
2499 slc->newintr, slc->seglen);
2500 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2501 for (sl = slc->newoff;
2502 sl < SLIM_SL_PER_SUPERFRAME;
2503 sl += slc->newintr) {
2504 for (i = 0; i < slc->seglen; i++) {
2505 if (ctrl->sched.slots[sl + i])
2506 return -EXFULL;
2507 ctrl->sched.slots[sl + i] = cc3 + 1;
2508 }
2509 }
2510 }
2511 cc3++;
2512 }
2513
2514 return 0;
2515}
2516#else
2517static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2518 u32 subfrml, u32 clkgear)
2519{
2520 return 0;
2521}
2522#endif
2523
2524static void slim_sort_chan_grp(struct slim_controller *ctrl,
2525 struct slim_ich *slc)
2526{
2527 u8 last = (u8)-1;
2528 u8 second = 0;
2529
2530 for (; last > 0; last--) {
2531 struct slim_ich *slc1 = slc;
2532 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002533 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534 slc2 = &ctrl->chans[next];
2535 for (second = 1; second <= last && slc2 &&
2536 (slc2->state == SLIM_CH_ACTIVE ||
2537 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2538 if (slc1->newoff > slc2->newoff) {
2539 u32 temp = slc2->newoff;
2540 slc2->newoff = slc1->newoff;
2541 slc1->newoff = temp;
2542 }
2543 if (slc2->nextgrp & SLIM_END_GRP) {
2544 last = second;
2545 break;
2546 }
2547 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002548 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002549 slc2 = &ctrl->chans[next];
2550 }
2551 if (slc2 == NULL)
2552 last = second - 1;
2553 }
2554}
2555
2556
2557static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2558{
2559 u32 msgsl = 0;
2560 u32 ctrlw = 0;
2561 u32 subfrml = 0;
2562 int ret = -EIO;
2563 struct slim_controller *ctrl = sb->ctrl;
2564 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2565 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2566 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2567 *clkgear = SLIM_MAX_CLK_GEAR;
2568
2569 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2570 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2571 ctrl->sched.usedslots,
2572 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002573 /*
2574 * If number of slots are 0, that means channels are inactive.
2575 * It is very likely that the manager will call clock pause very soon.
2576 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2577 * minimum amount of time.
2578 */
2579 if (ctrl->sched.usedslots != 0) {
2580 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2581 *clkgear -= 1;
2582 usedsl *= 2;
2583 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584 }
2585
2586 /*
2587 * Try scheduling data channels at current clock gear, if all channels
2588 * can be scheduled, or reserved BW can't be satisfied, increase clock
2589 * gear and try again
2590 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002591 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2593
2594 if (ret == 0) {
2595 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002596 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002597 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002598 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002599 continue;
2600 else
2601 break;
2602 }
2603 }
2604 if (ret == 0) {
2605 int i;
2606 /* Sort channel-groups */
2607 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2608 struct slim_ich *slc = ctrl->sched.chc1[i];
2609 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2610 continue;
2611 if ((slc->nextgrp & SLIM_START_GRP) &&
2612 !(slc->nextgrp & SLIM_END_GRP)) {
2613 slim_sort_chan_grp(ctrl, slc);
2614 }
2615 }
2616 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2617 struct slim_ich *slc = ctrl->sched.chc3[i];
2618 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2619 continue;
2620 if ((slc->nextgrp & SLIM_START_GRP) &&
2621 !(slc->nextgrp & SLIM_END_GRP)) {
2622 slim_sort_chan_grp(ctrl, slc);
2623 }
2624 }
2625
2626 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2627 }
2628
2629 return ret;
2630}
2631
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002632static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2633{
2634 struct slim_ich **arr;
2635 int len, i;
2636 if (coeff == SLIM_COEFF_1) {
2637 arr = ctrl->sched.chc1;
2638 len = ctrl->sched.num_cc1;
2639 } else {
2640 arr = ctrl->sched.chc3;
2641 len = ctrl->sched.num_cc3;
2642 }
2643 for (i = 0; i < len; i++) {
2644 struct slim_ich *slc = arr[i];
2645 if (slc->state == SLIM_CH_ACTIVE ||
2646 slc->state == SLIM_CH_SUSPENDED)
2647 slc->offset = slc->newoff;
2648 slc->interval = slc->newintr;
2649 }
2650}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002651static void slim_chan_changes(struct slim_device *sb, bool revert)
2652{
2653 struct slim_controller *ctrl = sb->ctrl;
2654 while (!list_empty(&sb->mark_define)) {
2655 struct slim_ich *slc;
2656 struct slim_pending_ch *pch =
2657 list_entry(sb->mark_define.next,
2658 struct slim_pending_ch, pending);
2659 slc = &ctrl->chans[pch->chan];
2660 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002661 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2662 u32 sl = slc->seglen << slc->rootexp;
2663 if (slc->coeff == SLIM_COEFF_3)
2664 sl *= 3;
2665 ctrl->sched.usedslots -= sl;
2666 slim_remove_ch(ctrl, slc);
2667 slc->state = SLIM_CH_DEFINED;
2668 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002671 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672 }
2673 list_del_init(&pch->pending);
2674 kfree(pch);
2675 }
2676
2677 while (!list_empty(&sb->mark_removal)) {
2678 struct slim_pending_ch *pch =
2679 list_entry(sb->mark_removal.next,
2680 struct slim_pending_ch, pending);
2681 struct slim_ich *slc = &ctrl->chans[pch->chan];
2682 u32 sl = slc->seglen << slc->rootexp;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002683 if (revert || slc->def > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002684 if (slc->coeff == SLIM_COEFF_3)
2685 sl *= 3;
2686 ctrl->sched.usedslots += sl;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002687 if (revert)
2688 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002689 slc->state = SLIM_CH_ACTIVE;
2690 } else
2691 slim_remove_ch(ctrl, slc);
2692 list_del_init(&pch->pending);
2693 kfree(pch);
2694 }
2695
2696 while (!list_empty(&sb->mark_suspend)) {
2697 struct slim_pending_ch *pch =
2698 list_entry(sb->mark_suspend.next,
2699 struct slim_pending_ch, pending);
2700 struct slim_ich *slc = &ctrl->chans[pch->chan];
2701 if (revert)
2702 slc->state = SLIM_CH_ACTIVE;
2703 list_del_init(&pch->pending);
2704 kfree(pch);
2705 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002706 /* Change already active channel if reconfig succeeded */
2707 if (!revert) {
2708 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2709 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2710 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002711}
2712
2713/*
2714 * slim_reconfigure_now: Request reconfiguration now.
2715 * @sb: client handle
2716 * This API does what commit flag in other scheduling APIs do.
2717 * -EXFULL is returned if there is no space in TDM to reserve the
2718 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2719 * progress.
2720 */
2721int slim_reconfigure_now(struct slim_device *sb)
2722{
2723 u8 i;
2724 u8 wbuf[4];
2725 u32 clkgear, subframe;
2726 u32 curexp;
2727 int ret;
2728 struct slim_controller *ctrl = sb->ctrl;
2729 u32 expshft;
2730 u32 segdist;
2731 struct slim_pending_ch *pch;
2732
Sagar Dharia80a55e12012-08-16 16:43:58 -06002733 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002734 /*
2735 * If there are no pending changes from this client, avoid sending
2736 * the reconfiguration sequence
2737 */
2738 if (sb->pending_msgsl == sb->cur_msgsl &&
2739 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002740 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002741 struct list_head *pos, *next;
2742 list_for_each_safe(pos, next, &sb->mark_removal) {
2743 struct slim_ich *slc;
2744 pch = list_entry(pos, struct slim_pending_ch, pending);
2745 slc = &ctrl->chans[pch->chan];
2746 if (slc->def > 0)
2747 slc->def--;
2748 /* Disconnect source port to free it up */
2749 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2750 slc->srch = 0;
Sagar Dhariae8f6c9a2013-02-22 19:06:39 -07002751 /*
2752 * If controller overrides BW allocation,
2753 * delete this in remove channel itself
2754 */
2755 if (slc->def != 0 && !ctrl->allocbw) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002756 list_del(&pch->pending);
2757 kfree(pch);
2758 }
2759 }
2760 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002761 mutex_unlock(&ctrl->sched.m_reconf);
2762 pr_info("SLIM_CL: skip reconfig sequence");
2763 return 0;
2764 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002765 }
2766
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2768 list_for_each_entry(pch, &sb->mark_define, pending) {
2769 struct slim_ich *slc = &ctrl->chans[pch->chan];
2770 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002771 if (slc->state < SLIM_CH_ACTIVE)
2772 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002773 }
2774
2775 list_for_each_entry(pch, &sb->mark_removal, pending) {
2776 struct slim_ich *slc = &ctrl->chans[pch->chan];
2777 u32 sl = slc->seglen << slc->rootexp;
2778 if (slc->coeff == SLIM_COEFF_3)
2779 sl *= 3;
2780 ctrl->sched.usedslots -= sl;
2781 slc->state = SLIM_CH_PENDING_REMOVAL;
2782 }
2783 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2784 struct slim_ich *slc = &ctrl->chans[pch->chan];
2785 slc->state = SLIM_CH_SUSPENDED;
2786 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002787
Sagar Dharia4aec9232012-07-24 23:44:26 -06002788 /*
2789 * Controller can override default channel scheduling algorithm.
2790 * (e.g. if controller needs to use fixed channel scheduling based
2791 * on number of channels)
2792 */
2793 if (ctrl->allocbw)
2794 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2795 else
2796 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002797
2798 if (!ret) {
2799 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2800 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2801 NULL, NULL, 0, 3, NULL, 0, NULL);
2802 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2803 }
2804
2805 if (!ret && subframe != ctrl->sched.subfrmcode) {
2806 wbuf[0] = (u8)(subframe & 0xFF);
2807 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2808 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2809 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2810 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2811 (int)wbuf[0], ret);
2812 }
2813 if (!ret && clkgear != ctrl->clkgear) {
2814 wbuf[0] = (u8)(clkgear & 0xFF);
2815 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2816 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2817 NULL, wbuf, 1, 4, NULL, 0, NULL);
2818 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2819 (int)wbuf[0], ret);
2820 }
2821 if (ret)
2822 goto revert_reconfig;
2823
2824 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2825 /* activate/remove channel */
2826 list_for_each_entry(pch, &sb->mark_define, pending) {
2827 struct slim_ich *slc = &ctrl->chans[pch->chan];
2828 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002829 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002830 wbuf[1] = slc->prrate;
2831 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2832 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2833 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2834 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2835 /* Right now, channel link bit is not supported */
2836 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2837 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2838 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2839 NULL, 0, NULL);
2840 if (ret)
2841 goto revert_reconfig;
2842
2843 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2844 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2845 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2846 NULL, 0, NULL);
2847 if (ret)
2848 goto revert_reconfig;
2849 }
2850
2851 list_for_each_entry(pch, &sb->mark_removal, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002852 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002853 dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002854 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002855 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2856 SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
2857 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2858 NULL, 0, NULL);
2859 if (ret)
2860 goto revert_reconfig;
2861 }
2862 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002863 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002864 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002865 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002866 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2867 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2868 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2869 NULL, 0, NULL);
2870 if (ret)
2871 goto revert_reconfig;
2872 }
2873
2874 /* Define CC1 channel */
2875 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2876 struct slim_ich *slc = ctrl->sched.chc1[i];
2877 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2878 continue;
2879 curexp = slc->rootexp + expshft;
2880 segdist = (slc->newoff << curexp) & 0x1FF;
2881 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002882 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2883 slc->newintr, slc->interval, segdist);
2884 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2885 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002887 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002888 slc->newintr != slc->interval ||
2889 slc->newoff != slc->offset) {
2890 segdist |= 0x200;
2891 segdist >>= curexp;
2892 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002893 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002894 wbuf[1] = (u8)(segdist & 0xFF);
2895 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2896 (slc->prop.prot << 4);
2897 wbuf[3] = slc->seglen;
2898 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2899 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2900 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2901 7, NULL, 0, NULL);
2902 if (ret)
2903 goto revert_reconfig;
2904 }
2905 }
2906
2907 /* Define CC3 channels */
2908 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2909 struct slim_ich *slc = ctrl->sched.chc3[i];
2910 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2911 continue;
2912 curexp = slc->rootexp + expshft;
2913 segdist = (slc->newoff << curexp) & 0x1FF;
2914 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002915 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2916 slc->newintr, slc->interval, segdist);
2917 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2918 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002920 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002921 slc->newintr != slc->interval ||
2922 slc->newoff != slc->offset) {
2923 segdist |= 0x200;
2924 segdist >>= curexp;
2925 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002926 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002927 wbuf[1] = (u8)(segdist & 0xFF);
2928 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2929 (slc->prop.prot << 4);
2930 wbuf[3] = (u8)(slc->seglen);
2931 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2932 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2933 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2934 7, NULL, 0, NULL);
2935 if (ret)
2936 goto revert_reconfig;
2937 }
2938 }
2939 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2940 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2941 NULL, 0, 3, NULL, 0, NULL);
2942 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2943 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002944 ctrl->sched.subfrmcode = subframe;
2945 ctrl->clkgear = clkgear;
2946 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2947 sb->cur_msgsl = sb->pending_msgsl;
2948 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002949 mutex_unlock(&ctrl->sched.m_reconf);
2950 return 0;
2951 }
2952
2953revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002954 /* Revert channel changes */
2955 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002956 mutex_unlock(&ctrl->sched.m_reconf);
2957 return ret;
2958}
2959EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2960
2961static int add_pending_ch(struct list_head *listh, u8 chan)
2962{
2963 struct slim_pending_ch *pch;
2964 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2965 if (!pch)
2966 return -ENOMEM;
2967 pch->chan = chan;
2968 list_add_tail(&pch->pending, listh);
2969 return 0;
2970}
2971
2972/*
2973 * slim_control_ch: Channel control API.
2974 * @sb: client handle
2975 * @chanh: group or channel handle to be controlled
2976 * @chctrl: Control command (activate/suspend/remove)
2977 * @commit: flag to indicate whether the control should take effect right-away.
2978 * This API activates, removes or suspends a channel (or group of channels)
2979 * chanh indicates the channel or group handle (returned by the define_ch API).
2980 * Reconfiguration may be time-consuming since it can change all other active
2981 * channel allocations on the bus, change in clock gear used by the slimbus,
2982 * and change in the control space width used for messaging.
2983 * commit makes sure that multiple channels can be activated/deactivated before
2984 * reconfiguration is started.
2985 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
2986 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
2987 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002988 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002989 */
2990int slim_control_ch(struct slim_device *sb, u16 chanh,
2991 enum slim_ch_control chctrl, bool commit)
2992{
2993 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002994 int ret = 0;
2995 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06002996 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06002997 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002998 struct slim_ich *slc = &ctrl->chans[chan];
2999 if (!(slc->nextgrp & SLIM_START_GRP))
3000 return -EINVAL;
3001
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003002 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003003 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05303004 struct slim_pending_ch *pch;
3005 u8 add_mark_removal = true;
3006
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003007 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003008 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
3009 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003010 if (slc->state < SLIM_CH_DEFINED) {
3011 ret = -ENOTCONN;
3012 break;
3013 }
3014 if (chctrl == SLIM_CH_SUSPEND) {
3015 ret = add_pending_ch(&sb->mark_suspend, chan);
3016 if (ret)
3017 break;
3018 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06003019 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003020 ret = -EISCONN;
3021 break;
3022 }
3023 ret = add_pending_ch(&sb->mark_define, chan);
3024 if (ret)
3025 break;
3026 } else {
3027 if (slc->state < SLIM_CH_ACTIVE) {
3028 ret = -ENOTCONN;
3029 break;
3030 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05303031 /* If channel removal request comes when pending
3032 * in the mark_define, remove it from the define
3033 * list instead of adding it to removal list
3034 */
3035 if (!list_empty(&sb->mark_define)) {
3036 struct list_head *pos, *next;
3037 list_for_each_safe(pos, next,
3038 &sb->mark_define) {
3039 pch = list_entry(pos,
3040 struct slim_pending_ch,
3041 pending);
3042 if (pch->chan == slc->chan) {
3043 list_del(&pch->pending);
3044 kfree(pch);
3045 add_mark_removal = false;
3046 break;
3047 }
3048 }
3049 }
3050 if (add_mark_removal == true) {
3051 ret = add_pending_ch(&sb->mark_removal, chan);
3052 if (ret)
3053 break;
3054 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003055 }
3056
Sagar Dhariab886e042012-10-17 22:41:57 -06003057 nchan++;
3058 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06003059 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06003060 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003061 if (!ret && commit == true)
3062 ret = slim_reconfigure_now(sb);
3063 mutex_unlock(&sb->sldev_reconf);
3064 return ret;
3065}
3066EXPORT_SYMBOL_GPL(slim_control_ch);
3067
3068/*
3069 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
3070 * @sb: client handle
3071 * @bw_bps: message bandwidth in bits per second to be requested
3072 * @commit: indicates whether the reconfiguration needs to be acted upon.
3073 * This API call can be grouped with slim_control_ch API call with only one of
3074 * the APIs specifying the commit flag to avoid reconfiguration being called too
3075 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
3076 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
3077 * is already in progress.
3078 */
3079int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
3080{
3081 struct slim_controller *ctrl = sb->ctrl;
3082 int ret = 0;
3083 int sl;
3084 mutex_lock(&sb->sldev_reconf);
3085 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
3086 sl = SLIM_SL_PER_SUPERFRAME;
3087 else {
3088 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
3089 (ctrl->a_framer->rootfreq/2 - 1)) /
3090 (ctrl->a_framer->rootfreq/2);
3091 }
3092 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
3093 sb->cur_msgsl);
3094 sb->pending_msgsl = sl;
3095 if (commit == true)
3096 ret = slim_reconfigure_now(sb);
3097 mutex_unlock(&sb->sldev_reconf);
3098 return ret;
3099}
3100EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
3101
Sagar Dharia33f34442011-08-08 16:22:03 -06003102/*
3103 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
3104 * paused or woken up out of clock pause
3105 * or woken up from clock pause
3106 * @ctrl: controller requesting bus to be paused or woken up
3107 * @wakeup: Wakeup this controller from clock pause.
3108 * @restart: Restart time value per spec used for clock pause. This value
3109 * isn't used when controller is to be woken up.
3110 * This API executes clock pause reconfiguration sequence if wakeup is false.
3111 * If wakeup is true, controller's wakeup is called
3112 * Slimbus clock is idle and can be disabled by the controller later.
3113 */
3114int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
3115{
3116 int ret = 0;
3117 int i;
3118
3119 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
3120 return -EINVAL;
3121 mutex_lock(&ctrl->m_ctrl);
3122 if (wakeup) {
3123 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
3124 mutex_unlock(&ctrl->m_ctrl);
3125 return 0;
3126 }
3127 wait_for_completion(&ctrl->pause_comp);
3128 /*
3129 * Slimbus framework will call controller wakeup
3130 * Controller should make sure that it sets active framer
3131 * out of clock pause by doing appropriate setting
3132 */
3133 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3134 ret = ctrl->wakeup(ctrl);
3135 if (!ret)
3136 ctrl->clk_state = SLIM_CLK_ACTIVE;
3137 mutex_unlock(&ctrl->m_ctrl);
3138 return ret;
3139 } else {
3140 switch (ctrl->clk_state) {
3141 case SLIM_CLK_ENTERING_PAUSE:
3142 case SLIM_CLK_PAUSE_FAILED:
3143 /*
3144 * If controller is already trying to enter clock pause,
3145 * let it finish.
3146 * In case of error, retry
3147 * In both cases, previous clock pause has signalled
3148 * completion.
3149 */
3150 wait_for_completion(&ctrl->pause_comp);
3151 /* retry upon failure */
3152 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3153 ctrl->clk_state = SLIM_CLK_ACTIVE;
3154 break;
3155 } else {
3156 mutex_unlock(&ctrl->m_ctrl);
3157 /*
3158 * Signal completion so that wakeup can wait on
3159 * it.
3160 */
3161 complete(&ctrl->pause_comp);
3162 return 0;
3163 }
3164 break;
3165 case SLIM_CLK_PAUSED:
3166 /* already paused */
3167 mutex_unlock(&ctrl->m_ctrl);
3168 return 0;
3169 case SLIM_CLK_ACTIVE:
3170 default:
3171 break;
3172 }
3173 }
3174 /* Pending response for a message */
3175 for (i = 0; i < ctrl->last_tid; i++) {
3176 if (ctrl->txnt[i]) {
3177 ret = -EBUSY;
Sagar Dharia33beca02012-10-22 16:21:46 -06003178 pr_info("slim_clk_pause: txn-rsp for %d pending", i);
Sagar Dharia33f34442011-08-08 16:22:03 -06003179 mutex_unlock(&ctrl->m_ctrl);
3180 return -EBUSY;
3181 }
3182 }
3183 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3184 mutex_unlock(&ctrl->m_ctrl);
3185
3186 mutex_lock(&ctrl->sched.m_reconf);
3187 /* Data channels active */
3188 if (ctrl->sched.usedslots) {
Sagar Dharia33beca02012-10-22 16:21:46 -06003189 pr_info("slim_clk_pause: data channel active");
Sagar Dharia33f34442011-08-08 16:22:03 -06003190 ret = -EBUSY;
3191 goto clk_pause_ret;
3192 }
3193
3194 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003195 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3196 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003197 if (ret)
3198 goto clk_pause_ret;
3199
3200 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003201 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3202 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3203 if (ret)
3204 goto clk_pause_ret;
3205
3206 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3207 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3208 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003209 if (ret)
3210 goto clk_pause_ret;
3211
3212clk_pause_ret:
3213 if (ret)
3214 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3215 else
3216 ctrl->clk_state = SLIM_CLK_PAUSED;
3217 complete(&ctrl->pause_comp);
3218 mutex_unlock(&ctrl->sched.m_reconf);
3219 return ret;
3220}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003221EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003222
3223MODULE_LICENSE("GPL v2");
3224MODULE_VERSION("0.1");
3225MODULE_DESCRIPTION("Slimbus module");
3226MODULE_ALIAS("platform:slimbus");