blob: c320e4620c290f533b38c3d251d7c642221217b2 [file] [log] [blame]
Sagar Dharia29f35f02011-10-01 20:37:50 -06001/* Copyright (c) 2011-2012, Code Aurora Forum. 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
236#define slim_ctrl_attr_gr NULL
237
238static void slim_ctrl_release(struct device *dev)
239{
240 struct slim_controller *ctrl = to_slim_controller(dev);
241
242 complete(&ctrl->dev_released);
243}
244
245static struct device_type slim_ctrl_type = {
246 .groups = slim_ctrl_attr_gr,
247 .release = slim_ctrl_release,
248};
249
250static struct slim_controller *slim_ctrl_get(struct slim_controller *ctrl)
251{
252 if (!ctrl || !get_device(&ctrl->dev))
253 return NULL;
254
255 return ctrl;
256}
257
258static void slim_ctrl_put(struct slim_controller *ctrl)
259{
260 if (ctrl)
261 put_device(&ctrl->dev);
262}
263
264#define slim_device_attr_gr NULL
265#define slim_device_uevent NULL
266static void slim_dev_release(struct device *dev)
267{
268 struct slim_device *sbdev = to_slim_device(dev);
269 slim_ctrl_put(sbdev->ctrl);
270}
271
272static struct device_type slim_dev_type = {
273 .groups = slim_device_attr_gr,
274 .uevent = slim_device_uevent,
275 .release = slim_dev_release,
276};
277
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600278static void slim_report_present(struct work_struct *work)
279{
280 u8 laddr;
281 int ret;
282 struct slim_driver *sbdrv;
283 struct slim_device *sbdev =
284 container_of(work, struct slim_device, wd);
285 if (sbdev->notified || !sbdev->dev.driver)
286 return;
287 ret = slim_get_logical_addr(sbdev, sbdev->e_addr, 6, &laddr);
288 sbdrv = to_slim_driver(sbdev->dev.driver);
289 if (!ret && sbdrv->device_up) {
290 sbdev->notified = true;
291 sbdrv->device_up(sbdev);
292 }
293}
294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295/*
296 * slim_add_device: Add a new device without register board info.
297 * @ctrl: Controller to which this device is to be added to.
298 * Called when device doesn't have an explicit client-driver to be probed, or
299 * the client-driver is a module installed dynamically.
300 */
301int slim_add_device(struct slim_controller *ctrl, struct slim_device *sbdev)
302{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 sbdev->dev.bus = &slimbus_type;
304 sbdev->dev.parent = ctrl->dev.parent;
305 sbdev->dev.type = &slim_dev_type;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600306 sbdev->dev.driver = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 sbdev->ctrl = ctrl;
308 slim_ctrl_get(ctrl);
309 dev_set_name(&sbdev->dev, "%s", sbdev->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 mutex_init(&sbdev->sldev_reconf);
311 INIT_LIST_HEAD(&sbdev->mark_define);
312 INIT_LIST_HEAD(&sbdev->mark_suspend);
313 INIT_LIST_HEAD(&sbdev->mark_removal);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600314 INIT_WORK(&sbdev->wd, slim_report_present);
315 mutex_lock(&ctrl->m_ctrl);
316 list_add_tail(&sbdev->dev_list, &ctrl->devs);
317 mutex_unlock(&ctrl->m_ctrl);
318 /* probe slave on this controller */
319 return device_register(&sbdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320}
321EXPORT_SYMBOL_GPL(slim_add_device);
322
323struct sbi_boardinfo {
324 struct list_head list;
325 struct slim_boardinfo board_info;
326};
327
328static LIST_HEAD(board_list);
329static LIST_HEAD(slim_ctrl_list);
330static DEFINE_MUTEX(board_lock);
331
332/* If controller is not present, only add to boards list */
333static void slim_match_ctrl_to_boardinfo(struct slim_controller *ctrl,
334 struct slim_boardinfo *bi)
335{
336 int ret;
337 if (ctrl->nr != bi->bus_num)
338 return;
339
340 ret = slim_add_device(ctrl, bi->slim_slave);
341 if (ret != 0)
342 dev_err(ctrl->dev.parent, "can't create new device for %s\n",
343 bi->slim_slave->name);
344}
345
346/*
347 * slim_register_board_info: Board-initialization routine.
348 * @info: List of all devices on all controllers present on the board.
349 * @n: number of entries.
350 * API enumerates respective devices on corresponding controller.
351 * Called from board-init function.
352 */
353int slim_register_board_info(struct slim_boardinfo const *info, unsigned n)
354{
355 struct sbi_boardinfo *bi;
356 int i;
357
358 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
359 if (!bi)
360 return -ENOMEM;
361
362 for (i = 0; i < n; i++, bi++, info++) {
363 struct slim_controller *ctrl;
364
365 memcpy(&bi->board_info, info, sizeof(*info));
366 mutex_lock(&board_lock);
367 list_add_tail(&bi->list, &board_list);
368 list_for_each_entry(ctrl, &slim_ctrl_list, list)
369 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
370 mutex_unlock(&board_lock);
371 }
372 return 0;
373}
374EXPORT_SYMBOL_GPL(slim_register_board_info);
375
376/*
Sagar Dhariaa6627e02012-08-28 12:20:49 -0600377 * slim_ctrl_add_boarddevs: Add devices registered by board-info
378 * @ctrl: Controller to which these devices are to be added to.
379 * This API is called by controller when it is up and running.
380 * If devices on a controller were registered before controller,
381 * this will make sure that they get probed when controller is up.
382 */
383void slim_ctrl_add_boarddevs(struct slim_controller *ctrl)
384{
385 struct sbi_boardinfo *bi;
386 mutex_lock(&board_lock);
387 list_add_tail(&ctrl->list, &slim_ctrl_list);
388 list_for_each_entry(bi, &board_list, list)
389 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
390 mutex_unlock(&board_lock);
391}
392EXPORT_SYMBOL_GPL(slim_ctrl_add_boarddevs);
393
394/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 * slim_busnum_to_ctrl: Map bus number to controller
396 * @busnum: Bus number
397 * Returns controller representing this bus number
398 */
399struct slim_controller *slim_busnum_to_ctrl(u32 bus_num)
400{
401 struct slim_controller *ctrl;
402 mutex_lock(&board_lock);
403 list_for_each_entry(ctrl, &slim_ctrl_list, list)
404 if (bus_num == ctrl->nr) {
405 mutex_unlock(&board_lock);
406 return ctrl;
407 }
408 mutex_unlock(&board_lock);
409 return NULL;
410}
411EXPORT_SYMBOL_GPL(slim_busnum_to_ctrl);
412
413static int slim_register_controller(struct slim_controller *ctrl)
414{
415 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 /* Can't register until after driver model init */
418 if (WARN_ON(!slimbus_type.p)) {
419 ret = -EAGAIN;
420 goto out_list;
421 }
422
423 dev_set_name(&ctrl->dev, "sb-%d", ctrl->nr);
424 ctrl->dev.bus = &slimbus_type;
425 ctrl->dev.type = &slim_ctrl_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 ctrl->num_dev = 0;
Sagar Dharia98a7ecb2011-07-25 15:25:35 -0600427 if (!ctrl->min_cg)
428 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
429 if (!ctrl->max_cg)
430 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 mutex_init(&ctrl->m_ctrl);
432 mutex_init(&ctrl->sched.m_reconf);
433 ret = device_register(&ctrl->dev);
434 if (ret)
435 goto out_list;
436
437 dev_dbg(&ctrl->dev, "Bus [%s] registered:dev:%x\n", ctrl->name,
438 (u32)&ctrl->dev);
439
440 if (ctrl->nports) {
441 ctrl->ports = kzalloc(ctrl->nports * sizeof(struct slim_port),
442 GFP_KERNEL);
443 if (!ctrl->ports) {
444 ret = -ENOMEM;
445 goto err_port_failed;
446 }
447 }
448 if (ctrl->nchans) {
449 ctrl->chans = kzalloc(ctrl->nchans * sizeof(struct slim_ich),
450 GFP_KERNEL);
451 if (!ctrl->chans) {
452 ret = -ENOMEM;
453 goto err_chan_failed;
454 }
455
456 ctrl->sched.chc1 =
457 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
458 GFP_KERNEL);
459 if (!ctrl->sched.chc1) {
460 kfree(ctrl->chans);
461 ret = -ENOMEM;
462 goto err_chan_failed;
463 }
464 ctrl->sched.chc3 =
465 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
466 GFP_KERNEL);
467 if (!ctrl->sched.chc3) {
468 kfree(ctrl->sched.chc1);
469 kfree(ctrl->chans);
470 ret = -ENOMEM;
471 goto err_chan_failed;
472 }
473 }
474#ifdef DEBUG
475 ctrl->sched.slots = kzalloc(SLIM_SL_PER_SUPERFRAME, GFP_KERNEL);
476#endif
Sagar Dharia33f34442011-08-08 16:22:03 -0600477 init_completion(&ctrl->pause_comp);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600478
479 INIT_LIST_HEAD(&ctrl->devs);
480 ctrl->wq = create_singlethread_workqueue(dev_name(&ctrl->dev));
481 if (!ctrl->wq)
482 goto err_workq_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
484 return 0;
485
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600486err_workq_failed:
487 kfree(ctrl->sched.chc3);
488 kfree(ctrl->sched.chc1);
489 kfree(ctrl->chans);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490err_chan_failed:
491 kfree(ctrl->ports);
492err_port_failed:
493 device_unregister(&ctrl->dev);
494out_list:
495 mutex_lock(&slim_lock);
496 idr_remove(&ctrl_idr, ctrl->nr);
497 mutex_unlock(&slim_lock);
498 return ret;
499}
500
501/* slim_remove_device: Remove the effect of slim_add_device() */
502void slim_remove_device(struct slim_device *sbdev)
503{
504 device_unregister(&sbdev->dev);
505}
506EXPORT_SYMBOL_GPL(slim_remove_device);
507
508static void slim_ctrl_remove_device(struct slim_controller *ctrl,
509 struct slim_boardinfo *bi)
510{
511 if (ctrl->nr == bi->bus_num)
512 slim_remove_device(bi->slim_slave);
513}
514
515/*
516 * slim_del_controller: Controller tear-down.
517 * Controller added with the above API is teared down using this API.
518 */
519int slim_del_controller(struct slim_controller *ctrl)
520{
521 struct slim_controller *found;
522 struct sbi_boardinfo *bi;
523
524 /* First make sure that this bus was added */
525 mutex_lock(&slim_lock);
526 found = idr_find(&ctrl_idr, ctrl->nr);
527 mutex_unlock(&slim_lock);
528 if (found != ctrl)
529 return -EINVAL;
530
531 /* Remove all clients */
532 mutex_lock(&board_lock);
533 list_for_each_entry(bi, &board_list, list)
534 slim_ctrl_remove_device(ctrl, &bi->board_info);
535 mutex_unlock(&board_lock);
536
537 init_completion(&ctrl->dev_released);
538 device_unregister(&ctrl->dev);
539
540 wait_for_completion(&ctrl->dev_released);
541 list_del(&ctrl->list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600542 destroy_workqueue(ctrl->wq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 /* free bus id */
544 mutex_lock(&slim_lock);
545 idr_remove(&ctrl_idr, ctrl->nr);
546 mutex_unlock(&slim_lock);
547
548 kfree(ctrl->sched.chc1);
549 kfree(ctrl->sched.chc3);
550#ifdef DEBUG
551 kfree(ctrl->sched.slots);
552#endif
553 kfree(ctrl->chans);
554 kfree(ctrl->ports);
555
556 return 0;
557}
558EXPORT_SYMBOL_GPL(slim_del_controller);
559
560/*
561 * slim_add_numbered_controller: Controller bring-up.
562 * @ctrl: Controller to be registered.
563 * A controller is registered with the framework using this API. ctrl->nr is the
564 * desired number with which slimbus framework registers the controller.
565 * Function will return -EBUSY if the number is in use.
566 */
567int slim_add_numbered_controller(struct slim_controller *ctrl)
568{
569 int id;
570 int status;
571
572 if (ctrl->nr & ~MAX_ID_MASK)
573 return -EINVAL;
574
575retry:
576 if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0)
577 return -ENOMEM;
578
579 mutex_lock(&slim_lock);
580 status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
581 if (status == 0 && id != ctrl->nr) {
582 status = -EAGAIN;
583 idr_remove(&ctrl_idr, id);
584 }
585 mutex_unlock(&slim_lock);
586 if (status == -EAGAIN)
587 goto retry;
588
589 if (status == 0)
590 status = slim_register_controller(ctrl);
591 return status;
592}
593EXPORT_SYMBOL_GPL(slim_add_numbered_controller);
594
595/*
596 * slim_msg_response: Deliver Message response received from a device to the
597 * framework.
598 * @ctrl: Controller handle
599 * @reply: Reply received from the device
600 * @len: Length of the reply
601 * @tid: Transaction ID received with which framework can associate reply.
602 * Called by controller to inform framework about the response received.
603 * This helps in making the API asynchronous, and controller-driver doesn't need
604 * to manage 1 more table other than the one managed by framework mapping TID
605 * with buffers
606 */
607void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
608{
609 int i;
610 struct slim_msg_txn *txn;
611
612 mutex_lock(&ctrl->m_ctrl);
613 txn = ctrl->txnt[tid];
614 if (txn == NULL) {
615 dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
616 tid, len);
617 mutex_unlock(&ctrl->m_ctrl);
618 return;
619 }
620 for (i = 0; i < len; i++)
621 txn->rbuf[i] = reply[i];
622 if (txn->comp)
623 complete(txn->comp);
624 ctrl->txnt[tid] = NULL;
625 mutex_unlock(&ctrl->m_ctrl);
626 kfree(txn);
627}
628EXPORT_SYMBOL_GPL(slim_msg_response);
629
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600630static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
632 struct completion *comp, u8 la, u8 *tid)
633{
634 u8 i = 0;
635 int ret = 0;
636 struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
637 GFP_KERNEL);
638 if (!txn)
639 return -ENOMEM;
640 if (tid) {
641 mutex_lock(&ctrl->m_ctrl);
642 for (i = 0; i < ctrl->last_tid; i++) {
643 if (ctrl->txnt[i] == NULL)
644 break;
645 }
646 if (i >= ctrl->last_tid) {
647 if (ctrl->last_tid == 255) {
648 mutex_unlock(&ctrl->m_ctrl);
649 kfree(txn);
650 return -ENOMEM;
651 }
652 ctrl->txnt = krealloc(ctrl->txnt,
653 (i + 1) * sizeof(struct slim_msg_txn *),
654 GFP_KERNEL);
655 if (!ctrl->txnt) {
656 mutex_unlock(&ctrl->m_ctrl);
657 kfree(txn);
658 return -ENOMEM;
659 }
660 ctrl->last_tid++;
661 }
662 ctrl->txnt[i] = txn;
663 mutex_unlock(&ctrl->m_ctrl);
664 txn->tid = i;
665 *tid = i;
666 }
667 txn->mc = mc;
668 txn->mt = mt;
669 txn->dt = dt;
670 txn->ec = ec;
671 txn->la = la;
672 txn->rbuf = rbuf;
673 txn->wbuf = wbuf;
674 txn->rl = mlen;
675 txn->len = len;
676 txn->comp = comp;
677
678 ret = ctrl->xfer_msg(ctrl, txn);
679 if (!tid)
680 kfree(txn);
681 return ret;
682}
683
684static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600685 u8 e_len, u8 *entry)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686{
687 u8 i;
688 for (i = 0; i < ctrl->num_dev; i++) {
689 if (ctrl->addrt[i].valid &&
690 memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600691 *entry = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 return 0;
693 }
694 }
695 return -ENXIO;
696}
697
698/*
699 * slim_assign_laddr: Assign logical address to a device enumerated.
700 * @ctrl: Controller with which device is enumerated.
701 * @e_addr: 6-byte elemental address of the device.
702 * @e_len: buffer length for e_addr
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600703 * @laddr: Return logical address (if valid flag is false)
704 * @valid: true if laddr holds a valid address that controller wants to
705 * set for this enumeration address. Otherwise framework sets index into
706 * address table as logical address.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 * Called by controller in response to REPORT_PRESENT. Framework will assign
708 * a logical address to this enumeration address.
709 * Function returns -EXFULL to indicate that all logical addresses are already
710 * taken.
711 */
712int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600713 u8 e_len, u8 *laddr, bool valid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714{
715 int ret;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600716 u8 i = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600717 bool exists = false;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600718 struct slim_device *sbdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 mutex_lock(&ctrl->m_ctrl);
720 /* already assigned */
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600721 if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
722 *laddr = ctrl->addrt[i].laddr;
723 exists = true;
724 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 if (ctrl->num_dev >= 254) {
726 ret = -EXFULL;
727 goto ret_assigned_laddr;
728 }
729 for (i = 0; i < ctrl->num_dev; i++) {
730 if (ctrl->addrt[i].valid == false)
731 break;
732 }
733 if (i == ctrl->num_dev) {
734 ctrl->addrt = krealloc(ctrl->addrt,
735 (ctrl->num_dev + 1) *
736 sizeof(struct slim_addrt),
737 GFP_KERNEL);
738 if (!ctrl->addrt) {
739 ret = -ENOMEM;
740 goto ret_assigned_laddr;
741 }
742 ctrl->num_dev++;
743 }
744 memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
745 ctrl->addrt[i].valid = true;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600746 /* Preferred address is index into table */
747 if (!valid)
748 *laddr = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 }
750
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600751 ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
752 *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 if (ret) {
754 ctrl->addrt[i].valid = false;
755 goto ret_assigned_laddr;
756 }
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600757 ctrl->addrt[i].laddr = *laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600759 dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760ret_assigned_laddr:
761 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600762 if (exists || ret)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600763 return ret;
764
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600765 pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600766 e_addr[1], e_addr[2]);
767 mutex_lock(&ctrl->m_ctrl);
768 list_for_each_entry(sbdev, &ctrl->devs, dev_list) {
769 if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
770 struct slim_driver *sbdrv;
Sagar Dharia33c84e62012-10-30 21:12:09 -0600771 sbdev->laddr = *laddr;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600772 if (sbdev->dev.driver) {
773 sbdrv = to_slim_driver(sbdev->dev.driver);
774 if (sbdrv->device_up)
775 queue_work(ctrl->wq, &sbdev->wd);
776 }
777 break;
778 }
779 }
780 mutex_unlock(&ctrl->m_ctrl);
781 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782}
783EXPORT_SYMBOL_GPL(slim_assign_laddr);
784
785/*
786 * slim_get_logical_addr: Return the logical address of a slimbus device.
787 * @sb: client handle requesting the adddress.
788 * @e_addr: Elemental address of the device.
789 * @e_len: Length of e_addr
790 * @laddr: output buffer to store the address
791 * context: can sleep
792 * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
793 * the device with this elemental address is not found.
794 */
795int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
796 u8 e_len, u8 *laddr)
797{
798 int ret = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600799 u8 entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800 struct slim_controller *ctrl = sb->ctrl;
801 if (!ctrl || !laddr || !e_addr || e_len != 6)
802 return -EINVAL;
803 mutex_lock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600804 ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
805 if (!ret)
806 *laddr = ctrl->addrt[entry].laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600808 if (ret == -ENXIO && ctrl->get_laddr) {
809 ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
810 if (!ret)
811 ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
812 true);
813 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 return ret;
815}
816EXPORT_SYMBOL_GPL(slim_get_logical_addr);
817
818static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
819 u8 *rbuf, const u8 *wbuf, u8 len)
820{
821 if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
822 return -EINVAL;
823 switch (oper) {
824 case SLIM_MSG_MC_REQUEST_VALUE:
825 case SLIM_MSG_MC_REQUEST_INFORMATION:
826 if (rbuf == NULL)
827 return -EINVAL;
828 return 0;
829 case SLIM_MSG_MC_CHANGE_VALUE:
830 case SLIM_MSG_MC_CLEAR_INFORMATION:
831 if (wbuf == NULL)
832 return -EINVAL;
833 return 0;
834 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
835 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
836 if (rbuf == NULL || wbuf == NULL)
837 return -EINVAL;
838 return 0;
839 default:
840 return -EINVAL;
841 }
842}
843
844static u16 slim_slicecodefromsize(u32 req)
845{
846 u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
847 if (req >= 8)
848 return 0;
849 else
850 return codetosize[req];
851}
852
853static u16 slim_slicesize(u32 code)
854{
855 u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
856 if (code == 0)
857 code = 1;
858 if (code > 16)
859 code = 16;
860 return sizetocode[code - 1];
861}
862
863
864/* Message APIs Unicast message APIs used by slimbus slave drivers */
865
866/*
867 * Message API access routines.
868 * @sb: client handle requesting elemental message reads, writes.
869 * @msg: Input structure for start-offset, number of bytes to read.
870 * @rbuf: data buffer to be filled with values read.
871 * @len: data buffer size
872 * @wbuf: data buffer containing value/information to be written
873 * context: can sleep
874 * Returns:
875 * -EINVAL: Invalid parameters
876 * -ETIMEDOUT: If controller could not complete the request. This may happen if
877 * the bus lines are not clocked, controller is not powered-on, slave with
878 * given address is not enumerated/responding.
879 */
880int slim_request_val_element(struct slim_device *sb,
881 struct slim_ele_access *msg, u8 *buf, u8 len)
882{
883 struct slim_controller *ctrl = sb->ctrl;
884 if (!ctrl)
885 return -EINVAL;
886 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_VALUE, buf,
887 NULL, len);
888}
889EXPORT_SYMBOL_GPL(slim_request_val_element);
890
891int slim_request_inf_element(struct slim_device *sb,
892 struct slim_ele_access *msg, u8 *buf, u8 len)
893{
894 struct slim_controller *ctrl = sb->ctrl;
895 if (!ctrl)
896 return -EINVAL;
897 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_INFORMATION,
898 buf, NULL, len);
899}
900EXPORT_SYMBOL_GPL(slim_request_inf_element);
901
902int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
903 const u8 *buf, u8 len)
904{
905 struct slim_controller *ctrl = sb->ctrl;
906 if (!ctrl)
907 return -EINVAL;
908 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CHANGE_VALUE, NULL, buf,
909 len);
910}
911EXPORT_SYMBOL_GPL(slim_change_val_element);
912
913int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
914 u8 *buf, u8 len)
915{
916 struct slim_controller *ctrl = sb->ctrl;
917 if (!ctrl)
918 return -EINVAL;
919 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
920 buf, len);
921}
922EXPORT_SYMBOL_GPL(slim_clear_inf_element);
923
924int slim_request_change_val_element(struct slim_device *sb,
925 struct slim_ele_access *msg, u8 *rbuf,
926 const u8 *wbuf, u8 len)
927{
928 struct slim_controller *ctrl = sb->ctrl;
929 if (!ctrl)
930 return -EINVAL;
931 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
932 rbuf, wbuf, len);
933}
934EXPORT_SYMBOL_GPL(slim_request_change_val_element);
935
936int slim_request_clear_inf_element(struct slim_device *sb,
937 struct slim_ele_access *msg, u8 *rbuf,
938 const u8 *wbuf, u8 len)
939{
940 struct slim_controller *ctrl = sb->ctrl;
941 if (!ctrl)
942 return -EINVAL;
943 return slim_xfer_msg(ctrl, sb, msg,
944 SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
945 rbuf, wbuf, len);
946}
947EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
948
949/*
950 * Broadcast message API:
951 * call this API directly with sbdev = NULL.
952 * For broadcast reads, make sure that buffers are big-enough to incorporate
953 * replies from all logical addresses.
954 * All controllers may not support broadcast
955 */
956int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600957 struct slim_ele_access *msg, u16 mc, u8 *rbuf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958 const u8 *wbuf, u8 len)
959{
960 DECLARE_COMPLETION_ONSTACK(complete);
961 int ret;
962 u16 sl, cur;
963 u16 ec;
964 u8 tid, mlen = 6;
965
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
967 if (ret)
968 goto xfer_err;
969
970 sl = slim_slicesize(len);
971 dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
972 msg->start_offset, len, mc, sl);
973
974 cur = slim_slicecodefromsize(sl);
975 ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
976
977 if (wbuf)
978 mlen += len;
979 if (rbuf) {
980 mlen++;
981 if (!msg->comp)
982 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
983 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
984 &complete, sbdev->laddr, &tid);
985 else
986 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
987 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
988 msg->comp, sbdev->laddr, &tid);
989 /* sync read */
Sagar Dhariacd0a2522011-08-31 18:29:31 -0600990 if (!ret && !msg->comp) {
991 ret = wait_for_completion_timeout(&complete, HZ);
992 if (!ret) {
993 struct slim_msg_txn *txn;
994 dev_err(&ctrl->dev, "slimbus Read timed out");
995 mutex_lock(&ctrl->m_ctrl);
996 txn = ctrl->txnt[tid];
997 /* Invalidate the transaction */
998 ctrl->txnt[tid] = NULL;
999 mutex_unlock(&ctrl->m_ctrl);
1000 kfree(txn);
1001 ret = -ETIMEDOUT;
1002 } else
1003 ret = 0;
Sagar Dharia53a9f792012-09-04 19:56:18 -06001004 } else if (ret < 0 && !msg->comp) {
1005 struct slim_msg_txn *txn;
1006 dev_err(&ctrl->dev, "slimbus Read error");
1007 mutex_lock(&ctrl->m_ctrl);
1008 txn = ctrl->txnt[tid];
1009 /* Invalidate the transaction */
1010 ctrl->txnt[tid] = NULL;
1011 mutex_unlock(&ctrl->m_ctrl);
1012 kfree(txn);
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001013 }
1014
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 } else
1016 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
1017 SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1018 NULL, sbdev->laddr, NULL);
1019xfer_err:
1020 return ret;
1021}
1022EXPORT_SYMBOL_GPL(slim_xfer_msg);
1023
1024/*
1025 * slim_alloc_mgrports: Allocate port on manager side.
1026 * @sb: device/client handle.
1027 * @req: Port request type.
1028 * @nports: Number of ports requested
1029 * @rh: output buffer to store the port handles
1030 * @hsz: size of buffer storing handles
1031 * context: can sleep
1032 * This port will be typically used by SW. e.g. client driver wants to receive
1033 * some data from audio codec HW using a data channel.
1034 * Port allocated using this API will be used to receive the data.
1035 * If half-duplex ports are requested, two adjacent ports are allocated for
1036 * 1 half-duplex port. So the handle-buffer size should be twice the number
1037 * of half-duplex ports to be allocated.
1038 * -EDQUOT is returned if all ports are in use.
1039 */
1040int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1041 int nports, u32 *rh, int hsz)
1042{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001043 int i, j;
1044 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 int nphysp = nports;
1046 struct slim_controller *ctrl = sb->ctrl;
1047
1048 if (!rh || !ctrl)
1049 return -EINVAL;
1050 if (req == SLIM_REQ_HALF_DUP)
1051 nphysp *= 2;
1052 if (hsz/sizeof(u32) < nphysp)
1053 return -EINVAL;
1054 mutex_lock(&ctrl->m_ctrl);
1055
1056 for (i = 0; i < ctrl->nports; i++) {
1057 bool multiok = true;
1058 if (ctrl->ports[i].state != SLIM_P_FREE)
1059 continue;
1060 /* Start half duplex channel at even port */
1061 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1062 continue;
1063 /* Allocate ports contiguously for multi-ch */
1064 if (ctrl->nports < (i + nphysp)) {
1065 i = ctrl->nports;
1066 break;
1067 }
1068 if (req == SLIM_REQ_MULTI_CH) {
1069 multiok = true;
1070 for (j = i; j < i + nphysp; j++) {
1071 if (ctrl->ports[j].state != SLIM_P_FREE) {
1072 multiok = false;
1073 break;
1074 }
1075 }
1076 if (!multiok)
1077 continue;
1078 }
1079 break;
1080 }
1081 if (i >= ctrl->nports)
1082 ret = -EDQUOT;
1083 for (j = i; j < i + nphysp; j++) {
1084 ctrl->ports[j].state = SLIM_P_UNCFG;
1085 ctrl->ports[j].req = req;
1086 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1087 ctrl->ports[j].flow = SLIM_SINK;
1088 else
1089 ctrl->ports[j].flow = SLIM_SRC;
1090 ret = ctrl->config_port(ctrl, j);
1091 if (ret) {
1092 for (; j >= i; j--)
1093 ctrl->ports[j].state = SLIM_P_FREE;
1094 goto alloc_err;
1095 }
1096 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1097 }
1098alloc_err:
1099 mutex_unlock(&ctrl->m_ctrl);
1100 return ret;
1101}
1102EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1103
1104/* Deallocate the port(s) allocated using the API above */
1105int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1106{
1107 int i;
1108 struct slim_controller *ctrl = sb->ctrl;
1109
1110 if (!ctrl || !hdl)
1111 return -EINVAL;
1112
1113 mutex_lock(&ctrl->m_ctrl);
1114
1115 for (i = 0; i < nports; i++) {
1116 u8 pn;
1117 pn = SLIM_HDL_TO_PORT(hdl[i]);
1118 if (ctrl->ports[pn].state == SLIM_P_CFG) {
1119 int j;
1120 dev_err(&ctrl->dev, "Can't dealloc connected port:%d",
1121 i);
1122 for (j = i - 1; j >= 0; j--) {
1123 pn = SLIM_HDL_TO_PORT(hdl[j]);
1124 ctrl->ports[pn].state = SLIM_P_UNCFG;
1125 }
1126 mutex_unlock(&ctrl->m_ctrl);
1127 return -EISCONN;
1128 }
1129 ctrl->ports[pn].state = SLIM_P_FREE;
1130 }
1131 mutex_unlock(&ctrl->m_ctrl);
1132 return 0;
1133}
1134EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1135
1136/*
1137 * slim_get_slaveport: Get slave port handle
1138 * @la: slave device logical address.
1139 * @idx: port index at slave
1140 * @rh: return handle
1141 * @flw: Flow type (source or destination)
1142 * This API only returns a slave port's representation as expected by slimbus
1143 * driver. This port is not managed by the slimbus driver. Caller is expected
1144 * to have visibility of this port since it's a device-port.
1145 */
1146int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1147{
1148 if (rh == NULL)
1149 return -EINVAL;
1150 *rh = SLIM_PORT_HDL(la, flw, idx);
1151 return 0;
1152}
1153EXPORT_SYMBOL_GPL(slim_get_slaveport);
1154
1155static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1156 enum slim_port_flow flow)
1157{
1158 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001159 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160 u8 buf[2];
1161 u32 la = SLIM_HDL_TO_LA(ph);
1162 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1163
1164 if (flow == SLIM_SRC)
1165 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1166 else
1167 mc = SLIM_MSG_MC_CONNECT_SINK;
1168 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001169 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 if (la == SLIM_LA_MANAGER)
1171 ctrl->ports[pn].flow = flow;
1172 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1173 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1174 NULL);
1175 if (!ret && la == SLIM_LA_MANAGER)
1176 ctrl->ports[pn].state = SLIM_P_CFG;
1177 return ret;
1178}
1179
1180static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1181{
1182 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001183 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 u32 la = SLIM_HDL_TO_LA(ph);
1185 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1186
1187 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1188 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1189 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1190 NULL, la, NULL);
1191 if (ret)
1192 return ret;
1193 if (la == SLIM_LA_MANAGER)
1194 ctrl->ports[pn].state = SLIM_P_UNCFG;
1195 return 0;
1196}
1197
1198/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001199 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001201 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001203 * Per slimbus specification, a channel may have 1 source port.
1204 * Channel specified in chanh needs to be allocated first.
1205 * Returns -EALREADY if source is already configured for this channel.
1206 * Returns -ENOTCONN if channel is not allocated
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001208int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1209{
1210 struct slim_controller *ctrl = sb->ctrl;
1211 int ret;
1212 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1213 struct slim_ich *slc = &ctrl->chans[chan];
1214 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
1215
1216 if (flow != SLIM_SRC)
1217 return -EINVAL;
1218
Sagar Dhariad2959352012-12-01 15:43:01 -07001219 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001220
1221 if (slc->state == SLIM_CH_FREE) {
1222 ret = -ENOTCONN;
1223 goto connect_src_err;
1224 }
1225 /*
1226 * Once channel is removed, its ports can be considered disconnected
1227 * So its ports can be reassigned. Source port is zeroed
1228 * when channel is deallocated.
1229 */
1230 if (slc->srch) {
1231 ret = -EALREADY;
1232 goto connect_src_err;
1233 }
1234
1235 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1236
1237 if (!ret)
1238 slc->srch = srch;
1239
1240connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001241 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001242 return ret;
1243}
1244EXPORT_SYMBOL_GPL(slim_connect_src);
1245
1246/*
1247 * slim_connect_sink: Connect sink port(s) to channel.
1248 * @sb: client handle
1249 * @sinkh: sink handle(s) to be connected to this channel
1250 * @nsink: number of sinks
1251 * @chanh: Channel with which the ports need to be associated with.
1252 * Per slimbus specification, a channel may have multiple sink-ports.
1253 * Channel specified in chanh needs to be allocated first.
1254 * Returns -EALREADY if sink is already configured for this channel.
1255 * Returns -ENOTCONN if channel is not allocated
1256 */
1257int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258{
1259 struct slim_controller *ctrl = sb->ctrl;
1260 int j;
1261 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001262 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 struct slim_ich *slc = &ctrl->chans[chan];
1264
Sagar Dharia29f35f02011-10-01 20:37:50 -06001265 if (!sinkh || !nsink)
1266 return -EINVAL;
1267
Sagar Dhariad2959352012-12-01 15:43:01 -07001268 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269
1270 /*
1271 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001272 * So its ports can be reassigned. Sink ports are freed when channel
1273 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001275 if (slc->state == SLIM_CH_FREE) {
1276 ret = -ENOTCONN;
1277 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001279
Sagar Dharia29f35f02011-10-01 20:37:50 -06001280 for (j = 0; j < nsink; j++) {
1281 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
1282 if (flow != SLIM_SINK)
1283 ret = -EINVAL;
1284 else
1285 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001287 for (j = j - 1; j >= 0; j--)
1288 disconnect_port_ch(ctrl, sinkh[j]);
1289 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 }
1291 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001292
1293 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1294 GFP_KERNEL);
1295 if (!slc->sinkh) {
1296 ret = -ENOMEM;
1297 for (j = 0; j < nsink; j++)
1298 disconnect_port_ch(ctrl, sinkh[j]);
1299 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 }
1301
Sagar Dharia29f35f02011-10-01 20:37:50 -06001302 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1303 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304
Sagar Dharia29f35f02011-10-01 20:37:50 -06001305connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001306 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 return ret;
1308}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001309EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310
1311/*
1312 * slim_disconnect_ports: Disconnect port(s) from channel
1313 * @sb: client handle
1314 * @ph: ports to be disconnected
1315 * @nph: number of ports.
1316 * Disconnects ports from a channel.
1317 */
1318int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1319{
1320 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001321 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001322
Sagar Dhariad2959352012-12-01 15:43:01 -07001323 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 for (i = 0; i < nph; i++)
1326 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001327 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 return 0;
1329}
1330EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1331
1332/*
1333 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1334 * @sb: client handle
1335 * @ph: port-handle
1336 * @iobuf: buffer to be transferred or populated
1337 * @len: buffer size.
1338 * @comp: completion signal to indicate transfer done or error.
1339 * context: can sleep
1340 * Returns number of bytes transferred/received if used synchronously.
1341 * Will return 0 if used asynchronously.
1342 * Client will call slim_port_get_xfer_status to get error and/or number of
1343 * bytes transferred if used asynchronously.
1344 */
1345int slim_port_xfer(struct slim_device *sb, u32 ph, u8 *iobuf, u32 len,
1346 struct completion *comp)
1347{
1348 struct slim_controller *ctrl = sb->ctrl;
1349 u8 pn = SLIM_HDL_TO_PORT(ph);
1350 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1351 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1352}
1353EXPORT_SYMBOL_GPL(slim_port_xfer);
1354
1355/*
1356 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1357 * after completion is done.
1358 * @sb: client handle
1359 * @ph: port-handle
1360 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1361 * @done_len: Number of bytes transferred.
1362 * This can be called when port_xfer complition is signalled.
1363 * The API will return port transfer error (underflow/overflow/disconnect)
1364 * and/or done_len will reflect number of bytes transferred. Note that
1365 * done_len may be valid even if port error (overflow/underflow) has happened.
1366 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1367 * client has not supplied more data to be transferred, done_len will indicate
1368 * number of bytes transferred with underflow error. To avoid frequent underflow
1369 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1370 * channel has data to be transferred even if client is not ready to transfer
1371 * data all the time. done_buf will indicate address of the last buffer
1372 * processed from the multiple transfers.
1373 */
1374enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
1375 u8 **done_buf, u32 *done_len)
1376{
1377 struct slim_controller *ctrl = sb->ctrl;
1378 u8 pn = SLIM_HDL_TO_PORT(ph);
1379 u32 la = SLIM_HDL_TO_LA(ph);
1380 enum slim_port_err err;
1381 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1382 /*
1383 * Framework only has insight into ports managed by ported device
1384 * used by the manager and not slave
1385 */
1386 if (la != SLIM_LA_MANAGER) {
1387 if (done_buf)
1388 *done_buf = NULL;
1389 if (done_len)
1390 *done_len = 0;
1391 return SLIM_P_NOT_OWNED;
1392 }
1393 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1394 if (err == SLIM_P_INPROGRESS)
1395 err = ctrl->ports[pn].err;
1396 return err;
1397}
1398EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1399
1400static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1401{
1402 struct slim_ich **arr;
1403 int i, j;
1404 int *len;
1405 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001406 /* Channel is already active and other end is transmitting data */
1407 if (slc->state >= SLIM_CH_ACTIVE)
1408 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 if (slc->coeff == SLIM_COEFF_1) {
1410 arr = ctrl->sched.chc1;
1411 len = &ctrl->sched.num_cc1;
1412 } else {
1413 arr = ctrl->sched.chc3;
1414 len = &ctrl->sched.num_cc3;
1415 sl *= 3;
1416 }
1417
1418 *len += 1;
1419
1420 /* Insert the channel based on rootexp and seglen */
1421 for (i = 0; i < *len - 1; i++) {
1422 /*
1423 * Primary key: exp low to high.
1424 * Secondary key: seglen: high to low
1425 */
1426 if ((slc->rootexp > arr[i]->rootexp) ||
1427 ((slc->rootexp == arr[i]->rootexp) &&
1428 (slc->seglen < arr[i]->seglen)))
1429 continue;
1430 else
1431 break;
1432 }
1433 for (j = *len - 1; j > i; j--)
1434 arr[j] = arr[j - 1];
1435 arr[i] = slc;
1436 ctrl->sched.usedslots += sl;
1437
1438 return;
1439}
1440
1441static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1442{
1443 struct slim_ich **arr;
1444 int i;
1445 u32 la, ph;
1446 int *len;
1447 if (slc->coeff == SLIM_COEFF_1) {
1448 arr = ctrl->sched.chc1;
1449 len = &ctrl->sched.num_cc1;
1450 } else {
1451 arr = ctrl->sched.chc3;
1452 len = &ctrl->sched.num_cc3;
1453 }
1454
1455 for (i = 0; i < *len; i++) {
1456 if (arr[i] == slc)
1457 break;
1458 }
1459 if (i >= *len)
1460 return -EXFULL;
1461 for (; i < *len - 1; i++)
1462 arr[i] = arr[i + 1];
1463 *len -= 1;
1464 arr[*len] = NULL;
1465
1466 slc->state = SLIM_CH_ALLOCATED;
1467 slc->newintr = 0;
1468 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001469 for (i = 0; i < slc->nsink; i++) {
1470 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001471 la = SLIM_HDL_TO_LA(ph);
1472 /*
1473 * For ports managed by manager's ported device, no need to send
1474 * disconnect. It is client's responsibility to call disconnect
1475 * on ports owned by the slave device
1476 */
1477 if (la == SLIM_LA_MANAGER)
1478 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1479 }
1480
Sagar Dharia29f35f02011-10-01 20:37:50 -06001481 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 la = SLIM_HDL_TO_LA(ph);
1483 if (la == SLIM_LA_MANAGER)
1484 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1485
Sagar Dharia29f35f02011-10-01 20:37:50 -06001486 kfree(slc->sinkh);
1487 slc->sinkh = NULL;
1488 slc->srch = 0;
1489 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 return 0;
1491}
1492
1493static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1494{
1495 u32 rate = 0, rate4k = 0, rate11k = 0;
1496 u32 exp = 0;
1497 u32 pr = 0;
1498 bool exact = true;
1499 bool done = false;
1500 enum slim_ch_rate ratefam;
1501
1502 if (prop->prot >= SLIM_PUSH)
1503 return 0;
1504 if (prop->baser == SLIM_RATE_1HZ) {
1505 rate = prop->ratem / 4000;
1506 rate4k = rate;
1507 if (rate * 4000 == prop->ratem)
1508 ratefam = SLIM_RATE_4000HZ;
1509 else {
1510 rate = prop->ratem / 11025;
1511 rate11k = rate;
1512 if (rate * 11025 == prop->ratem)
1513 ratefam = SLIM_RATE_11025HZ;
1514 else
1515 ratefam = SLIM_RATE_1HZ;
1516 }
1517 } else {
1518 ratefam = prop->baser;
1519 rate = prop->ratem;
1520 }
1521 if (ratefam == SLIM_RATE_1HZ) {
1522 exact = false;
1523 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1524 rate = rate4k + 1;
1525 ratefam = SLIM_RATE_4000HZ;
1526 } else {
1527 rate = rate11k + 1;
1528 ratefam = SLIM_RATE_11025HZ;
1529 }
1530 }
1531 /* covert rate to coeff-exp */
1532 while (!done) {
1533 while ((rate & 0x1) != 0x1) {
1534 rate >>= 1;
1535 exp++;
1536 }
1537 if (rate > 3) {
1538 /* roundup if not exact */
1539 rate++;
1540 exact = false;
1541 } else
1542 done = true;
1543 }
1544 if (ratefam == SLIM_RATE_4000HZ) {
1545 if (rate == 1)
1546 pr = 0x10;
1547 else {
1548 pr = 0;
1549 exp++;
1550 }
1551 } else {
1552 pr = 8;
1553 exp++;
1554 }
1555 if (exp <= 7) {
1556 pr |= exp;
1557 if (exact)
1558 pr |= 0x80;
1559 } else
1560 pr = 0;
1561 return pr;
1562}
1563
1564static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1565{
1566 struct slim_controller *ctrl = sb->ctrl;
1567 u32 chrate = 0;
1568 u32 exp = 0;
1569 u32 coeff = 0;
1570 bool exact = true;
1571 bool done = false;
1572 int ret = 0;
1573 struct slim_ich *slc = &ctrl->chans[chan];
1574 struct slim_ch *prop = &slc->prop;
1575
1576 slc->prrate = slim_calc_prrate(ctrl, prop);
1577 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1578 if (prop->baser == SLIM_RATE_4000HZ)
1579 chrate = 4000 * prop->ratem;
1580 else if (prop->baser == SLIM_RATE_11025HZ)
1581 chrate = 11025 * prop->ratem;
1582 else
1583 chrate = prop->ratem;
1584 /* max allowed sample freq = 768 seg/frame */
1585 if (chrate > 3600000)
1586 return -EDQUOT;
1587 if (prop->baser == SLIM_RATE_4000HZ &&
1588 ctrl->a_framer->superfreq == 4000)
1589 coeff = prop->ratem;
1590 else if (prop->baser == SLIM_RATE_11025HZ &&
1591 ctrl->a_framer->superfreq == 3675)
1592 coeff = 3 * prop->ratem;
1593 else {
1594 u32 tempr = 0;
1595 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1596 coeff = tempr / ctrl->a_framer->rootfreq;
1597 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1598 coeff++;
1599 exact = false;
1600 }
1601 }
1602
1603 /* convert coeff to coeff-exponent */
1604 exp = 0;
1605 while (!done) {
1606 while ((coeff & 0x1) != 0x1) {
1607 coeff >>= 1;
1608 exp++;
1609 }
1610 if (coeff > 3) {
1611 coeff++;
1612 exact = false;
1613 } else
1614 done = true;
1615 }
1616 if (prop->prot == SLIM_HARD_ISO && !exact)
1617 return -EPROTONOSUPPORT;
1618 else if (prop->prot == SLIM_AUTO_ISO) {
1619 if (exact)
1620 prop->prot = SLIM_HARD_ISO;
1621 else {
1622 /* Push-Pull not supported for now */
1623 return -EPROTONOSUPPORT;
1624 }
1625 }
1626 slc->rootexp = exp;
1627 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1628 if (prop->prot != SLIM_HARD_ISO)
1629 slc->seglen++;
1630 if (prop->prot >= SLIM_EXT_SMPLX)
1631 slc->seglen++;
1632 /* convert coeff to enum */
1633 if (coeff == 1) {
1634 if (exp > 9)
1635 ret = -EIO;
1636 coeff = SLIM_COEFF_1;
1637 } else {
1638 if (exp > 8)
1639 ret = -EIO;
1640 coeff = SLIM_COEFF_3;
1641 }
1642 slc->coeff = coeff;
1643
1644 return ret;
1645}
1646
1647/*
1648 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1649 * @sb: client handle.
1650 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001651 * Slimbus channels are limited to 256 per specification.
1652 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 * Although slimbus specification supports 256 channels, a controller may not
1654 * support that many channels.
1655 */
1656int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1657{
1658 struct slim_controller *ctrl = sb->ctrl;
1659 u16 i;
1660
1661 if (!ctrl)
1662 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001663 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664 for (i = 0; i < ctrl->nchans; i++) {
1665 if (ctrl->chans[i].state == SLIM_CH_FREE)
1666 break;
1667 }
1668 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001669 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 return -EXFULL;
1671 }
1672 *chanh = i;
1673 ctrl->chans[i].nextgrp = 0;
1674 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001675 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676
Sagar Dhariad2959352012-12-01 15:43:01 -07001677 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678 return 0;
1679}
1680EXPORT_SYMBOL_GPL(slim_alloc_ch);
1681
1682/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001683 * slim_query_ch: Get reference-counted handle for a channel number. Every
1684 * channel is reference counted by upto one as producer and the others as
1685 * consumer)
1686 * @sb: client handle
1687 * @chan: slimbus channel number
1688 * @chanh: return channel handle
1689 * If request channel number is not in use, it is allocated, and reference
1690 * count is set to one. If the channel was was already allocated, this API
1691 * will return handle to that channel and reference count is incremented.
1692 * -EXFULL is returned if all channels are in use
1693 */
1694int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1695{
1696 struct slim_controller *ctrl = sb->ctrl;
1697 u16 i, j;
1698 int ret = 0;
1699 if (!ctrl || !chanh)
1700 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001701 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001702 /* start with modulo number */
1703 i = ch % ctrl->nchans;
1704
1705 for (j = 0; j < ctrl->nchans; j++) {
1706 if (ctrl->chans[i].chan == ch) {
1707 *chanh = i;
1708 ctrl->chans[i].ref++;
1709 if (ctrl->chans[i].state == SLIM_CH_FREE)
1710 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1711 goto query_out;
1712 }
1713 i = (i + 1) % ctrl->nchans;
1714 }
1715
1716 /* Channel not in table yet */
1717 ret = -EXFULL;
1718 for (j = 0; j < ctrl->nchans; j++) {
1719 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1720 ctrl->chans[i].state =
1721 SLIM_CH_ALLOCATED;
1722 *chanh = i;
1723 ctrl->chans[i].ref++;
1724 ctrl->chans[i].chan = ch;
1725 ctrl->chans[i].nextgrp = 0;
1726 ret = 0;
1727 break;
1728 }
1729 i = (i + 1) % ctrl->nchans;
1730 }
1731query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001732 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001733 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1734 ch, i, ctrl->chans[i].ref, ret);
1735 return ret;
1736}
1737EXPORT_SYMBOL_GPL(slim_query_ch);
1738
1739/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 * slim_dealloc_ch: Deallocate channel allocated using the API above
1741 * -EISCONN is returned if the channel is tried to be deallocated without
1742 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001743 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1744 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001745 */
1746int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1747{
1748 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001749 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750 struct slim_ich *slc = &ctrl->chans[chan];
1751 if (!ctrl)
1752 return -EINVAL;
1753
Sagar Dhariad2959352012-12-01 15:43:01 -07001754 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001755 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001756 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001757 return -ENOTCONN;
1758 }
1759 if (slc->ref > 1) {
1760 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001761 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001762 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1763 slc->chan, chanh, slc->ref);
1764 return 0;
1765 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001766 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1767 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001768 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 return -EISCONN;
1770 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001771 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001773 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001774 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1775 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 return 0;
1777}
1778EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1779
1780/*
1781 * slim_get_ch_state: Channel state.
1782 * This API returns the channel's state (active, suspended, inactive etc)
1783 */
1784enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1785{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001786 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 struct slim_ich *slc = &sb->ctrl->chans[chan];
1788 return slc->state;
1789}
1790EXPORT_SYMBOL_GPL(slim_get_ch_state);
1791
1792/*
1793 * slim_define_ch: Define a channel.This API defines channel parameters for a
1794 * given channel.
1795 * @sb: client handle.
1796 * @prop: slim_ch structure with channel parameters desired to be used.
1797 * @chanh: list of channels to be defined.
1798 * @nchan: number of channels in a group (1 if grp is false)
1799 * @grp: Are the channels grouped
1800 * @grph: return group handle if grouping of channels is desired.
1801 * Channels can be grouped if multiple channels use same parameters
1802 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1803 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001804 * -EISCONN is returned if channel is already used with different parameters.
1805 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 */
1807int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1808 u8 nchan, bool grp, u16 *grph)
1809{
1810 struct slim_controller *ctrl = sb->ctrl;
1811 int i, ret = 0;
1812
1813 if (!ctrl || !chanh || !prop || !nchan)
1814 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001815 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001817 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001818 struct slim_ich *slc = &ctrl->chans[chan];
1819 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001821 if (slc->state < SLIM_CH_ALLOCATED) {
1822 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 goto err_define_ch;
1824 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001825 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1826 if (prop->ratem != slc->prop.ratem ||
1827 prop->sampleszbits != slc->prop.sampleszbits ||
1828 prop->baser != slc->prop.baser) {
1829 ret = -EISCONN;
1830 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001831 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001832 } else if (slc->state > SLIM_CH_DEFINED) {
1833 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001834 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001835 } else {
1836 ctrl->chans[chan].prop = *prop;
1837 ret = slim_nextdefine_ch(sb, chan);
1838 if (ret)
1839 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 }
1841 if (i < (nchan - 1))
1842 ctrl->chans[chan].nextgrp = chanh[i + 1];
1843 if (i == 0)
1844 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1845 if (i == (nchan - 1))
1846 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001847 }
1848
1849 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001850 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001851 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001852 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001853 struct slim_ich *slc = &ctrl->chans[chan];
1854 if (slc->state == SLIM_CH_ALLOCATED)
1855 slc->state = SLIM_CH_DEFINED;
1856 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001858 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07001859 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 return ret;
1861}
1862EXPORT_SYMBOL_GPL(slim_define_ch);
1863
1864static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
1865{
1866 u32 code = 0;
1867 if (*ctrlw == *subfrml) {
1868 *ctrlw = 8;
1869 *subfrml = 8;
1870 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
1871 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
1872 return 0;
1873 }
1874 if (*subfrml == 6) {
1875 code = 0;
1876 *msgsl = 256;
1877 } else if (*subfrml == 8) {
1878 code = 1;
1879 *msgsl = 192;
1880 } else if (*subfrml == 24) {
1881 code = 2;
1882 *msgsl = 64;
1883 } else { /* 32 */
1884 code = 3;
1885 *msgsl = 48;
1886 }
1887
1888 if (*ctrlw < 8) {
1889 if (*ctrlw >= 6) {
1890 *ctrlw = 6;
1891 code |= 0x14;
1892 } else {
1893 if (*ctrlw == 5)
1894 *ctrlw = 4;
1895 code |= (*ctrlw << 2);
1896 }
1897 } else {
1898 code -= 2;
1899 if (*ctrlw >= 24) {
1900 *ctrlw = 24;
1901 code |= 0x1e;
1902 } else if (*ctrlw >= 16) {
1903 *ctrlw = 16;
1904 code |= 0x1c;
1905 } else if (*ctrlw >= 12) {
1906 *ctrlw = 12;
1907 code |= 0x1a;
1908 } else {
1909 *ctrlw = 8;
1910 code |= 0x18;
1911 }
1912 }
1913
1914 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
1915 SLIM_GDE_SLOTS_PER_SUPERFRAME;
1916 return code;
1917}
1918
1919static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
1920 int sz, u32 shft)
1921{
1922 int i;
1923 u32 oldoff;
1924 for (i = 0; i < sz; i++) {
1925 struct slim_ich *slc;
1926 if (ach[i] == NULL)
1927 continue;
1928 slc = ach[i];
1929 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1930 continue;
1931 oldoff = slc->newoff;
1932 slc->newoff += shft;
1933 /* seg. offset must be <= interval */
1934 if (slc->newoff >= slc->newintr)
1935 slc->newoff -= slc->newintr;
1936 }
1937}
1938
1939static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
1940 u32 *ctrlw, u32 *subfrml)
1941{
1942 int coeff1, coeff3;
1943 enum slim_ch_coeff bias;
1944 struct slim_controller *ctrl = sb->ctrl;
1945 int last1 = ctrl->sched.num_cc1 - 1;
1946 int last3 = ctrl->sched.num_cc3 - 1;
1947
1948 /*
1949 * Find first channels with coeff 1 & 3 as starting points for
1950 * scheduling
1951 */
1952 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
1953 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
1954 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1955 continue;
1956 else
1957 break;
1958 }
1959 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
1960 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
1961 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1962 continue;
1963 else
1964 break;
1965 }
1966 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
1967 *ctrlw = 8;
1968 *subfrml = 8;
1969 return 0;
1970 } else if (coeff3 == ctrl->sched.num_cc3)
1971 bias = SLIM_COEFF_1;
1972 else
1973 bias = SLIM_COEFF_3;
1974
1975 /*
1976 * Find last chan in coeff1, 3 list, we will use to know when we
1977 * have done scheduling all coeff1 channels
1978 */
1979 while (last1 >= 0) {
1980 if (ctrl->sched.chc1[last1] != NULL &&
1981 (ctrl->sched.chc1[last1])->state !=
1982 SLIM_CH_PENDING_REMOVAL)
1983 break;
1984 last1--;
1985 }
1986 while (last3 >= 0) {
1987 if (ctrl->sched.chc3[last3] != NULL &&
1988 (ctrl->sched.chc3[last3])->state !=
1989 SLIM_CH_PENDING_REMOVAL)
1990 break;
1991 last3--;
1992 }
1993
1994 if (bias == SLIM_COEFF_1) {
1995 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
1996 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
1997 int curexp, finalexp;
1998 u32 curintr, curmaxsl;
1999 int opensl1[2];
2000 int maxctrlw1;
2001
2002 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2003 curexp = (int)expshft - 1;
2004
2005 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2006 curmaxsl = curintr >> 1;
2007 opensl1[0] = opensl1[1] = curmaxsl;
2008
2009 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2010 curintr >>= 1;
2011 curmaxsl >>= 1;
2012
2013 /* update 4K family open slot records */
2014 if (opensl1[1] < opensl1[0])
2015 opensl1[1] -= curmaxsl;
2016 else
2017 opensl1[1] = opensl1[0] - curmaxsl;
2018 opensl1[0] = curmaxsl;
2019 if (opensl1[1] < 0) {
2020 opensl1[0] += opensl1[1];
2021 opensl1[1] = 0;
2022 }
2023 if (opensl1[0] <= 0) {
2024 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2025 __LINE__);
2026 return -EXFULL;
2027 }
2028 curexp++;
2029 /* schedule 4k family channels */
2030
2031 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2032 (int)(slc1->rootexp + expshft))) {
2033 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2034 coeff1++;
2035 slc1 = ctrl->sched.chc1[coeff1];
2036 continue;
2037 }
2038 if (opensl1[1] >= opensl1[0] ||
2039 (finalexp == (int)slc1->rootexp &&
2040 curintr <= 24 &&
2041 opensl1[0] == curmaxsl)) {
2042 opensl1[1] -= slc1->seglen;
2043 slc1->newoff = curmaxsl + opensl1[1];
2044 if (opensl1[1] < 0 &&
2045 opensl1[0] == curmaxsl) {
2046 opensl1[0] += opensl1[1];
2047 opensl1[1] = 0;
2048 if (opensl1[0] < 0) {
2049 dev_dbg(&ctrl->dev,
2050 "reconfig failed:%d\n",
2051 __LINE__);
2052 return -EXFULL;
2053 }
2054 }
2055 } else {
2056 if (slc1->seglen > opensl1[0]) {
2057 dev_dbg(&ctrl->dev,
2058 "reconfig failed:%d\n",
2059 __LINE__);
2060 return -EXFULL;
2061 }
2062 slc1->newoff = opensl1[0] -
2063 slc1->seglen;
2064 opensl1[0] = slc1->newoff;
2065 }
2066 slc1->newintr = curintr;
2067 coeff1++;
2068 slc1 = ctrl->sched.chc1[coeff1];
2069 }
2070 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002071 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002072 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002073 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002074 if (opensl1[1] > opensl1[0]) {
2075 int temp = opensl1[0];
2076 opensl1[0] = opensl1[1];
2077 opensl1[1] = temp;
2078 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2079 ctrl->sched.num_cc1, curmaxsl);
2080 }
2081 /* choose subframe mode to maximize bw */
2082 maxctrlw1 = opensl1[0];
2083 if (opensl1[0] == curmaxsl)
2084 maxctrlw1 += opensl1[1];
2085 if (curintr >= 24) {
2086 *subfrml = 24;
2087 *ctrlw = maxctrlw1;
2088 } else if (curintr == 12) {
2089 if (maxctrlw1 > opensl1[1] * 4) {
2090 *subfrml = 24;
2091 *ctrlw = maxctrlw1;
2092 } else {
2093 *subfrml = 6;
2094 *ctrlw = opensl1[1];
2095 }
2096 } else {
2097 *subfrml = 6;
2098 *ctrlw = maxctrlw1;
2099 }
2100 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002101 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2103 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2104 int curexp, finalexp, exp1;
2105 u32 curintr, curmaxsl;
2106 int opensl3[2];
2107 int opensl1[6];
2108 bool opensl1valid = false;
2109 int maxctrlw1, maxctrlw3, i;
2110 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2111 if (last1 >= 0) {
2112 slc1 = ctrl->sched.chc1[coeff1];
2113 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2114 if (exp1 > finalexp)
2115 finalexp = exp1;
2116 }
2117 curexp = (int)expshft - 1;
2118
2119 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2120 curmaxsl = curintr >> 1;
2121 opensl3[0] = opensl3[1] = curmaxsl;
2122
2123 while (coeff1 < ctrl->sched.num_cc1 ||
2124 coeff3 < ctrl->sched.num_cc3 ||
2125 curintr > 32) {
2126 curintr >>= 1;
2127 curmaxsl >>= 1;
2128
2129 /* update 12k family open slot records */
2130 if (opensl3[1] < opensl3[0])
2131 opensl3[1] -= curmaxsl;
2132 else
2133 opensl3[1] = opensl3[0] - curmaxsl;
2134 opensl3[0] = curmaxsl;
2135 if (opensl3[1] < 0) {
2136 opensl3[0] += opensl3[1];
2137 opensl3[1] = 0;
2138 }
2139 if (opensl3[0] <= 0) {
2140 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2141 __LINE__);
2142 return -EXFULL;
2143 }
2144 curexp++;
2145
2146 /* schedule 12k family channels */
2147 while (coeff3 < ctrl->sched.num_cc3 &&
2148 curexp == (int)slc3->rootexp + expshft) {
2149 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2150 coeff3++;
2151 slc3 = ctrl->sched.chc3[coeff3];
2152 continue;
2153 }
2154 opensl1valid = false;
2155 if (opensl3[1] >= opensl3[0] ||
2156 (finalexp == (int)slc3->rootexp &&
2157 curintr <= 32 &&
2158 opensl3[0] == curmaxsl &&
2159 last1 < 0)) {
2160 opensl3[1] -= slc3->seglen;
2161 slc3->newoff = curmaxsl + opensl3[1];
2162 if (opensl3[1] < 0 &&
2163 opensl3[0] == curmaxsl) {
2164 opensl3[0] += opensl3[1];
2165 opensl3[1] = 0;
2166 }
2167 if (opensl3[0] < 0) {
2168 dev_dbg(&ctrl->dev,
2169 "reconfig failed:%d\n",
2170 __LINE__);
2171 return -EXFULL;
2172 }
2173 } else {
2174 if (slc3->seglen > opensl3[0]) {
2175 dev_dbg(&ctrl->dev,
2176 "reconfig failed:%d\n",
2177 __LINE__);
2178 return -EXFULL;
2179 }
2180 slc3->newoff = opensl3[0] -
2181 slc3->seglen;
2182 opensl3[0] = slc3->newoff;
2183 }
2184 slc3->newintr = curintr;
2185 coeff3++;
2186 slc3 = ctrl->sched.chc3[coeff3];
2187 }
2188 /* update 4k openslot records */
2189 if (opensl1valid == false) {
2190 for (i = 0; i < 3; i++) {
2191 opensl1[i * 2] = opensl3[0];
2192 opensl1[(i * 2) + 1] = opensl3[1];
2193 }
2194 } else {
2195 int opensl1p[6];
2196 memcpy(opensl1p, opensl1, sizeof(opensl1));
2197 for (i = 0; i < 3; i++) {
2198 if (opensl1p[i] < opensl1p[i + 3])
2199 opensl1[(i * 2) + 1] =
2200 opensl1p[i];
2201 else
2202 opensl1[(i * 2) + 1] =
2203 opensl1p[i + 3];
2204 }
2205 for (i = 0; i < 3; i++) {
2206 opensl1[(i * 2) + 1] -= curmaxsl;
2207 opensl1[i * 2] = curmaxsl;
2208 if (opensl1[(i * 2) + 1] < 0) {
2209 opensl1[i * 2] +=
2210 opensl1[(i * 2) + 1];
2211 opensl1[(i * 2) + 1] = 0;
2212 }
2213 if (opensl1[i * 2] < 0) {
2214 dev_dbg(&ctrl->dev,
2215 "reconfig failed:%d\n",
2216 __LINE__);
2217 return -EXFULL;
2218 }
2219 }
2220 }
2221 /* schedule 4k family channels */
2222 while (coeff1 < ctrl->sched.num_cc1 &&
2223 curexp == (int)slc1->rootexp + expshft) {
2224 /* searchorder effective when opensl valid */
2225 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2226 int maxopensl = 0;
2227 int maxi = 0;
2228 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2229 coeff1++;
2230 slc1 = ctrl->sched.chc1[coeff1];
2231 continue;
2232 }
2233 opensl1valid = true;
2234 for (i = 0; i < 6; i++) {
2235 if (opensl1[srcho[i]] > maxopensl) {
2236 maxopensl = opensl1[srcho[i]];
2237 maxi = srcho[i];
2238 }
2239 }
2240 opensl1[maxi] -= slc1->seglen;
2241 slc1->newoff = (curmaxsl * maxi) +
2242 opensl1[maxi];
2243 if (opensl1[maxi] < 0) {
2244 if (((maxi & 1) == 1) &&
2245 (opensl1[maxi - 1] == curmaxsl)) {
2246 opensl1[maxi - 1] +=
2247 opensl1[maxi];
2248 if (opensl3[0] >
2249 opensl1[maxi - 1])
2250 opensl3[0] =
2251 opensl1[maxi - 1];
2252 opensl3[1] = 0;
2253 opensl1[maxi] = 0;
2254 if (opensl1[maxi - 1] < 0) {
2255 dev_dbg(&ctrl->dev,
2256 "reconfig failed:%d\n",
2257 __LINE__);
2258 return -EXFULL;
2259 }
2260 } else {
2261 dev_dbg(&ctrl->dev,
2262 "reconfig failed:%d\n",
2263 __LINE__);
2264 return -EXFULL;
2265 }
2266 } else {
2267 if (opensl3[maxi & 1] > opensl1[maxi])
2268 opensl3[maxi & 1] =
2269 opensl1[maxi];
2270 }
2271 slc1->newintr = curintr * 3;
2272 coeff1++;
2273 slc1 = ctrl->sched.chc1[coeff1];
2274 }
2275 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002276 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002277 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002278 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2280 if (opensl3[1] > opensl3[0]) {
2281 int temp = opensl3[0];
2282 opensl3[0] = opensl3[1];
2283 opensl3[1] = temp;
2284 temp = opensl1[5];
2285 opensl1[5] = opensl1[4];
2286 opensl1[4] = opensl1[3];
2287 opensl1[3] = opensl1[2];
2288 opensl1[2] = opensl1[1];
2289 opensl1[1] = opensl1[0];
2290 opensl1[0] = temp;
2291 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2292 ctrl->sched.num_cc1, curmaxsl);
2293 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2294 ctrl->sched.num_cc3, curmaxsl);
2295 }
2296 /* subframe mode to maximize BW */
2297 maxctrlw3 = opensl3[0];
2298 maxctrlw1 = opensl1[0];
2299 if (opensl3[0] == curmaxsl)
2300 maxctrlw3 += opensl3[1];
2301 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2302 maxctrlw1 += opensl1[i + 1];
2303 if (curintr >= 32) {
2304 *subfrml = 32;
2305 *ctrlw = maxctrlw3;
2306 } else if (curintr == 16) {
2307 if (maxctrlw3 > (opensl3[1] * 4)) {
2308 *subfrml = 32;
2309 *ctrlw = maxctrlw3;
2310 } else {
2311 *subfrml = 8;
2312 *ctrlw = opensl3[1];
2313 }
2314 } else {
2315 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2316 *subfrml = 24;
2317 *ctrlw = maxctrlw1;
2318 } else {
2319 *subfrml = 8;
2320 *ctrlw = maxctrlw3;
2321 }
2322 }
2323 }
2324 return 0;
2325}
2326
2327#ifdef DEBUG
2328static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2329 u32 subfrml, u32 clkgear)
2330{
2331 int sl, i;
2332 int cc1 = 0;
2333 int cc3 = 0;
2334 struct slim_ich *slc = NULL;
2335 if (!ctrl->sched.slots)
2336 return 0;
2337 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2338 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2339 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2340 for (i = 0; i < ctrlw; i++)
2341 ctrl->sched.slots[sl + i] = 33;
2342 }
2343 while (cc1 < ctrl->sched.num_cc1) {
2344 slc = ctrl->sched.chc1[cc1];
2345 if (slc == NULL) {
2346 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2347 cc1);
2348 return -EIO;
2349 }
2350 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2351 (slc - ctrl->chans), slc->newoff,
2352 slc->newintr, slc->seglen);
2353
2354 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2355 for (sl = slc->newoff;
2356 sl < SLIM_SL_PER_SUPERFRAME;
2357 sl += slc->newintr) {
2358 for (i = 0; i < slc->seglen; i++) {
2359 if (ctrl->sched.slots[sl + i])
2360 return -EXFULL;
2361 ctrl->sched.slots[sl + i] = cc1 + 1;
2362 }
2363 }
2364 }
2365 cc1++;
2366 }
2367 while (cc3 < ctrl->sched.num_cc3) {
2368 slc = ctrl->sched.chc3[cc3];
2369 if (slc == NULL) {
2370 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2371 cc3);
2372 return -EIO;
2373 }
2374 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2375 (slc - ctrl->chans), slc->newoff,
2376 slc->newintr, slc->seglen);
2377 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2378 for (sl = slc->newoff;
2379 sl < SLIM_SL_PER_SUPERFRAME;
2380 sl += slc->newintr) {
2381 for (i = 0; i < slc->seglen; i++) {
2382 if (ctrl->sched.slots[sl + i])
2383 return -EXFULL;
2384 ctrl->sched.slots[sl + i] = cc3 + 1;
2385 }
2386 }
2387 }
2388 cc3++;
2389 }
2390
2391 return 0;
2392}
2393#else
2394static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2395 u32 subfrml, u32 clkgear)
2396{
2397 return 0;
2398}
2399#endif
2400
2401static void slim_sort_chan_grp(struct slim_controller *ctrl,
2402 struct slim_ich *slc)
2403{
2404 u8 last = (u8)-1;
2405 u8 second = 0;
2406
2407 for (; last > 0; last--) {
2408 struct slim_ich *slc1 = slc;
2409 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002410 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002411 slc2 = &ctrl->chans[next];
2412 for (second = 1; second <= last && slc2 &&
2413 (slc2->state == SLIM_CH_ACTIVE ||
2414 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2415 if (slc1->newoff > slc2->newoff) {
2416 u32 temp = slc2->newoff;
2417 slc2->newoff = slc1->newoff;
2418 slc1->newoff = temp;
2419 }
2420 if (slc2->nextgrp & SLIM_END_GRP) {
2421 last = second;
2422 break;
2423 }
2424 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002425 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426 slc2 = &ctrl->chans[next];
2427 }
2428 if (slc2 == NULL)
2429 last = second - 1;
2430 }
2431}
2432
2433
2434static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2435{
2436 u32 msgsl = 0;
2437 u32 ctrlw = 0;
2438 u32 subfrml = 0;
2439 int ret = -EIO;
2440 struct slim_controller *ctrl = sb->ctrl;
2441 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2442 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2443 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2444 *clkgear = SLIM_MAX_CLK_GEAR;
2445
2446 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2447 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2448 ctrl->sched.usedslots,
2449 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002450 /*
2451 * If number of slots are 0, that means channels are inactive.
2452 * It is very likely that the manager will call clock pause very soon.
2453 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2454 * minimum amount of time.
2455 */
2456 if (ctrl->sched.usedslots != 0) {
2457 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2458 *clkgear -= 1;
2459 usedsl *= 2;
2460 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461 }
2462
2463 /*
2464 * Try scheduling data channels at current clock gear, if all channels
2465 * can be scheduled, or reserved BW can't be satisfied, increase clock
2466 * gear and try again
2467 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002468 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2470
2471 if (ret == 0) {
2472 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002473 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002475 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 continue;
2477 else
2478 break;
2479 }
2480 }
2481 if (ret == 0) {
2482 int i;
2483 /* Sort channel-groups */
2484 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2485 struct slim_ich *slc = ctrl->sched.chc1[i];
2486 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2487 continue;
2488 if ((slc->nextgrp & SLIM_START_GRP) &&
2489 !(slc->nextgrp & SLIM_END_GRP)) {
2490 slim_sort_chan_grp(ctrl, slc);
2491 }
2492 }
2493 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2494 struct slim_ich *slc = ctrl->sched.chc3[i];
2495 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2496 continue;
2497 if ((slc->nextgrp & SLIM_START_GRP) &&
2498 !(slc->nextgrp & SLIM_END_GRP)) {
2499 slim_sort_chan_grp(ctrl, slc);
2500 }
2501 }
2502
2503 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2504 }
2505
2506 return ret;
2507}
2508
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002509static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2510{
2511 struct slim_ich **arr;
2512 int len, i;
2513 if (coeff == SLIM_COEFF_1) {
2514 arr = ctrl->sched.chc1;
2515 len = ctrl->sched.num_cc1;
2516 } else {
2517 arr = ctrl->sched.chc3;
2518 len = ctrl->sched.num_cc3;
2519 }
2520 for (i = 0; i < len; i++) {
2521 struct slim_ich *slc = arr[i];
2522 if (slc->state == SLIM_CH_ACTIVE ||
2523 slc->state == SLIM_CH_SUSPENDED)
2524 slc->offset = slc->newoff;
2525 slc->interval = slc->newintr;
2526 }
2527}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528static void slim_chan_changes(struct slim_device *sb, bool revert)
2529{
2530 struct slim_controller *ctrl = sb->ctrl;
2531 while (!list_empty(&sb->mark_define)) {
2532 struct slim_ich *slc;
2533 struct slim_pending_ch *pch =
2534 list_entry(sb->mark_define.next,
2535 struct slim_pending_ch, pending);
2536 slc = &ctrl->chans[pch->chan];
2537 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002538 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2539 u32 sl = slc->seglen << slc->rootexp;
2540 if (slc->coeff == SLIM_COEFF_3)
2541 sl *= 3;
2542 ctrl->sched.usedslots -= sl;
2543 slim_remove_ch(ctrl, slc);
2544 slc->state = SLIM_CH_DEFINED;
2545 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002547 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002548 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002549 }
2550 list_del_init(&pch->pending);
2551 kfree(pch);
2552 }
2553
2554 while (!list_empty(&sb->mark_removal)) {
2555 struct slim_pending_ch *pch =
2556 list_entry(sb->mark_removal.next,
2557 struct slim_pending_ch, pending);
2558 struct slim_ich *slc = &ctrl->chans[pch->chan];
2559 u32 sl = slc->seglen << slc->rootexp;
2560 if (revert) {
2561 if (slc->coeff == SLIM_COEFF_3)
2562 sl *= 3;
2563 ctrl->sched.usedslots += sl;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002564 slc->def = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002565 slc->state = SLIM_CH_ACTIVE;
2566 } else
2567 slim_remove_ch(ctrl, slc);
2568 list_del_init(&pch->pending);
2569 kfree(pch);
2570 }
2571
2572 while (!list_empty(&sb->mark_suspend)) {
2573 struct slim_pending_ch *pch =
2574 list_entry(sb->mark_suspend.next,
2575 struct slim_pending_ch, pending);
2576 struct slim_ich *slc = &ctrl->chans[pch->chan];
2577 if (revert)
2578 slc->state = SLIM_CH_ACTIVE;
2579 list_del_init(&pch->pending);
2580 kfree(pch);
2581 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002582 /* Change already active channel if reconfig succeeded */
2583 if (!revert) {
2584 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2585 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2586 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587}
2588
2589/*
2590 * slim_reconfigure_now: Request reconfiguration now.
2591 * @sb: client handle
2592 * This API does what commit flag in other scheduling APIs do.
2593 * -EXFULL is returned if there is no space in TDM to reserve the
2594 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2595 * progress.
2596 */
2597int slim_reconfigure_now(struct slim_device *sb)
2598{
2599 u8 i;
2600 u8 wbuf[4];
2601 u32 clkgear, subframe;
2602 u32 curexp;
2603 int ret;
2604 struct slim_controller *ctrl = sb->ctrl;
2605 u32 expshft;
2606 u32 segdist;
2607 struct slim_pending_ch *pch;
2608
Sagar Dharia80a55e12012-08-16 16:43:58 -06002609 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002610 /*
2611 * If there are no pending changes from this client, avoid sending
2612 * the reconfiguration sequence
2613 */
2614 if (sb->pending_msgsl == sb->cur_msgsl &&
2615 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002616 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002617 struct list_head *pos, *next;
2618 list_for_each_safe(pos, next, &sb->mark_removal) {
2619 struct slim_ich *slc;
2620 pch = list_entry(pos, struct slim_pending_ch, pending);
2621 slc = &ctrl->chans[pch->chan];
2622 if (slc->def > 0)
2623 slc->def--;
2624 /* Disconnect source port to free it up */
2625 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2626 slc->srch = 0;
2627 if (slc->def != 0) {
2628 list_del(&pch->pending);
2629 kfree(pch);
2630 }
2631 }
2632 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002633 mutex_unlock(&ctrl->sched.m_reconf);
2634 pr_info("SLIM_CL: skip reconfig sequence");
2635 return 0;
2636 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002637 }
2638
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002639 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2640 list_for_each_entry(pch, &sb->mark_define, pending) {
2641 struct slim_ich *slc = &ctrl->chans[pch->chan];
2642 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002643 if (slc->state < SLIM_CH_ACTIVE)
2644 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002645 }
2646
2647 list_for_each_entry(pch, &sb->mark_removal, pending) {
2648 struct slim_ich *slc = &ctrl->chans[pch->chan];
2649 u32 sl = slc->seglen << slc->rootexp;
2650 if (slc->coeff == SLIM_COEFF_3)
2651 sl *= 3;
2652 ctrl->sched.usedslots -= sl;
2653 slc->state = SLIM_CH_PENDING_REMOVAL;
2654 }
2655 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2656 struct slim_ich *slc = &ctrl->chans[pch->chan];
2657 slc->state = SLIM_CH_SUSPENDED;
2658 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659
Sagar Dharia4aec9232012-07-24 23:44:26 -06002660 /*
2661 * Controller can override default channel scheduling algorithm.
2662 * (e.g. if controller needs to use fixed channel scheduling based
2663 * on number of channels)
2664 */
2665 if (ctrl->allocbw)
2666 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2667 else
2668 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669
2670 if (!ret) {
2671 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2672 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2673 NULL, NULL, 0, 3, NULL, 0, NULL);
2674 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2675 }
2676
2677 if (!ret && subframe != ctrl->sched.subfrmcode) {
2678 wbuf[0] = (u8)(subframe & 0xFF);
2679 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2680 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2681 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2682 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2683 (int)wbuf[0], ret);
2684 }
2685 if (!ret && clkgear != ctrl->clkgear) {
2686 wbuf[0] = (u8)(clkgear & 0xFF);
2687 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2688 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2689 NULL, wbuf, 1, 4, NULL, 0, NULL);
2690 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2691 (int)wbuf[0], ret);
2692 }
2693 if (ret)
2694 goto revert_reconfig;
2695
2696 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2697 /* activate/remove channel */
2698 list_for_each_entry(pch, &sb->mark_define, pending) {
2699 struct slim_ich *slc = &ctrl->chans[pch->chan];
2700 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002701 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002702 wbuf[1] = slc->prrate;
2703 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2704 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2705 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2706 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2707 /* Right now, channel link bit is not supported */
2708 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2709 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2710 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2711 NULL, 0, NULL);
2712 if (ret)
2713 goto revert_reconfig;
2714
2715 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2716 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2717 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2718 NULL, 0, NULL);
2719 if (ret)
2720 goto revert_reconfig;
2721 }
2722
2723 list_for_each_entry(pch, &sb->mark_removal, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002724 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 dev_dbg(&ctrl->dev, "remove chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002726 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002727 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2728 SLIM_MSG_MC_NEXT_REMOVE_CHANNEL, 0,
2729 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2730 NULL, 0, NULL);
2731 if (ret)
2732 goto revert_reconfig;
2733 }
2734 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002735 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002736 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002737 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002738 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2739 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2740 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2741 NULL, 0, NULL);
2742 if (ret)
2743 goto revert_reconfig;
2744 }
2745
2746 /* Define CC1 channel */
2747 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2748 struct slim_ich *slc = ctrl->sched.chc1[i];
2749 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2750 continue;
2751 curexp = slc->rootexp + expshft;
2752 segdist = (slc->newoff << curexp) & 0x1FF;
2753 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002754 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2755 slc->newintr, slc->interval, segdist);
2756 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2757 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002758
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002759 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002760 slc->newintr != slc->interval ||
2761 slc->newoff != slc->offset) {
2762 segdist |= 0x200;
2763 segdist >>= curexp;
2764 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002765 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002766 wbuf[1] = (u8)(segdist & 0xFF);
2767 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2768 (slc->prop.prot << 4);
2769 wbuf[3] = slc->seglen;
2770 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2771 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2772 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2773 7, NULL, 0, NULL);
2774 if (ret)
2775 goto revert_reconfig;
2776 }
2777 }
2778
2779 /* Define CC3 channels */
2780 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2781 struct slim_ich *slc = ctrl->sched.chc3[i];
2782 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2783 continue;
2784 curexp = slc->rootexp + expshft;
2785 segdist = (slc->newoff << curexp) & 0x1FF;
2786 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002787 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2788 slc->newintr, slc->interval, segdist);
2789 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2790 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002791
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002792 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002793 slc->newintr != slc->interval ||
2794 slc->newoff != slc->offset) {
2795 segdist |= 0x200;
2796 segdist >>= curexp;
2797 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002798 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002799 wbuf[1] = (u8)(segdist & 0xFF);
2800 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2801 (slc->prop.prot << 4);
2802 wbuf[3] = (u8)(slc->seglen);
2803 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2804 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2805 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2806 7, NULL, 0, NULL);
2807 if (ret)
2808 goto revert_reconfig;
2809 }
2810 }
2811 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2812 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2813 NULL, 0, 3, NULL, 0, NULL);
2814 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2815 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002816 ctrl->sched.subfrmcode = subframe;
2817 ctrl->clkgear = clkgear;
2818 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2819 sb->cur_msgsl = sb->pending_msgsl;
2820 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002821 mutex_unlock(&ctrl->sched.m_reconf);
2822 return 0;
2823 }
2824
2825revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002826 /* Revert channel changes */
2827 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002828 mutex_unlock(&ctrl->sched.m_reconf);
2829 return ret;
2830}
2831EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2832
2833static int add_pending_ch(struct list_head *listh, u8 chan)
2834{
2835 struct slim_pending_ch *pch;
2836 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2837 if (!pch)
2838 return -ENOMEM;
2839 pch->chan = chan;
2840 list_add_tail(&pch->pending, listh);
2841 return 0;
2842}
2843
2844/*
2845 * slim_control_ch: Channel control API.
2846 * @sb: client handle
2847 * @chanh: group or channel handle to be controlled
2848 * @chctrl: Control command (activate/suspend/remove)
2849 * @commit: flag to indicate whether the control should take effect right-away.
2850 * This API activates, removes or suspends a channel (or group of channels)
2851 * chanh indicates the channel or group handle (returned by the define_ch API).
2852 * Reconfiguration may be time-consuming since it can change all other active
2853 * channel allocations on the bus, change in clock gear used by the slimbus,
2854 * and change in the control space width used for messaging.
2855 * commit makes sure that multiple channels can be activated/deactivated before
2856 * reconfiguration is started.
2857 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
2858 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
2859 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002860 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002861 */
2862int slim_control_ch(struct slim_device *sb, u16 chanh,
2863 enum slim_ch_control chctrl, bool commit)
2864{
2865 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002866 int ret = 0;
2867 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06002868 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06002869 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002870 struct slim_ich *slc = &ctrl->chans[chan];
2871 if (!(slc->nextgrp & SLIM_START_GRP))
2872 return -EINVAL;
2873
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002874 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002875 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05302876 struct slim_pending_ch *pch;
2877 u8 add_mark_removal = true;
2878
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002879 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002880 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
2881 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 if (slc->state < SLIM_CH_DEFINED) {
2883 ret = -ENOTCONN;
2884 break;
2885 }
2886 if (chctrl == SLIM_CH_SUSPEND) {
2887 ret = add_pending_ch(&sb->mark_suspend, chan);
2888 if (ret)
2889 break;
2890 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002891 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002892 ret = -EISCONN;
2893 break;
2894 }
2895 ret = add_pending_ch(&sb->mark_define, chan);
2896 if (ret)
2897 break;
2898 } else {
2899 if (slc->state < SLIM_CH_ACTIVE) {
2900 ret = -ENOTCONN;
2901 break;
2902 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05302903 /* If channel removal request comes when pending
2904 * in the mark_define, remove it from the define
2905 * list instead of adding it to removal list
2906 */
2907 if (!list_empty(&sb->mark_define)) {
2908 struct list_head *pos, *next;
2909 list_for_each_safe(pos, next,
2910 &sb->mark_define) {
2911 pch = list_entry(pos,
2912 struct slim_pending_ch,
2913 pending);
2914 if (pch->chan == slc->chan) {
2915 list_del(&pch->pending);
2916 kfree(pch);
2917 add_mark_removal = false;
2918 break;
2919 }
2920 }
2921 }
2922 if (add_mark_removal == true) {
2923 ret = add_pending_ch(&sb->mark_removal, chan);
2924 if (ret)
2925 break;
2926 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002927 }
2928
Sagar Dhariab886e042012-10-17 22:41:57 -06002929 nchan++;
2930 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06002931 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06002932 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002933 if (!ret && commit == true)
2934 ret = slim_reconfigure_now(sb);
2935 mutex_unlock(&sb->sldev_reconf);
2936 return ret;
2937}
2938EXPORT_SYMBOL_GPL(slim_control_ch);
2939
2940/*
2941 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
2942 * @sb: client handle
2943 * @bw_bps: message bandwidth in bits per second to be requested
2944 * @commit: indicates whether the reconfiguration needs to be acted upon.
2945 * This API call can be grouped with slim_control_ch API call with only one of
2946 * the APIs specifying the commit flag to avoid reconfiguration being called too
2947 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
2948 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
2949 * is already in progress.
2950 */
2951int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
2952{
2953 struct slim_controller *ctrl = sb->ctrl;
2954 int ret = 0;
2955 int sl;
2956 mutex_lock(&sb->sldev_reconf);
2957 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
2958 sl = SLIM_SL_PER_SUPERFRAME;
2959 else {
2960 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
2961 (ctrl->a_framer->rootfreq/2 - 1)) /
2962 (ctrl->a_framer->rootfreq/2);
2963 }
2964 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
2965 sb->cur_msgsl);
2966 sb->pending_msgsl = sl;
2967 if (commit == true)
2968 ret = slim_reconfigure_now(sb);
2969 mutex_unlock(&sb->sldev_reconf);
2970 return ret;
2971}
2972EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
2973
Sagar Dharia33f34442011-08-08 16:22:03 -06002974/*
2975 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
2976 * paused or woken up out of clock pause
2977 * or woken up from clock pause
2978 * @ctrl: controller requesting bus to be paused or woken up
2979 * @wakeup: Wakeup this controller from clock pause.
2980 * @restart: Restart time value per spec used for clock pause. This value
2981 * isn't used when controller is to be woken up.
2982 * This API executes clock pause reconfiguration sequence if wakeup is false.
2983 * If wakeup is true, controller's wakeup is called
2984 * Slimbus clock is idle and can be disabled by the controller later.
2985 */
2986int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
2987{
2988 int ret = 0;
2989 int i;
2990
2991 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
2992 return -EINVAL;
2993 mutex_lock(&ctrl->m_ctrl);
2994 if (wakeup) {
2995 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
2996 mutex_unlock(&ctrl->m_ctrl);
2997 return 0;
2998 }
2999 wait_for_completion(&ctrl->pause_comp);
3000 /*
3001 * Slimbus framework will call controller wakeup
3002 * Controller should make sure that it sets active framer
3003 * out of clock pause by doing appropriate setting
3004 */
3005 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3006 ret = ctrl->wakeup(ctrl);
3007 if (!ret)
3008 ctrl->clk_state = SLIM_CLK_ACTIVE;
3009 mutex_unlock(&ctrl->m_ctrl);
3010 return ret;
3011 } else {
3012 switch (ctrl->clk_state) {
3013 case SLIM_CLK_ENTERING_PAUSE:
3014 case SLIM_CLK_PAUSE_FAILED:
3015 /*
3016 * If controller is already trying to enter clock pause,
3017 * let it finish.
3018 * In case of error, retry
3019 * In both cases, previous clock pause has signalled
3020 * completion.
3021 */
3022 wait_for_completion(&ctrl->pause_comp);
3023 /* retry upon failure */
3024 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3025 ctrl->clk_state = SLIM_CLK_ACTIVE;
3026 break;
3027 } else {
3028 mutex_unlock(&ctrl->m_ctrl);
3029 /*
3030 * Signal completion so that wakeup can wait on
3031 * it.
3032 */
3033 complete(&ctrl->pause_comp);
3034 return 0;
3035 }
3036 break;
3037 case SLIM_CLK_PAUSED:
3038 /* already paused */
3039 mutex_unlock(&ctrl->m_ctrl);
3040 return 0;
3041 case SLIM_CLK_ACTIVE:
3042 default:
3043 break;
3044 }
3045 }
3046 /* Pending response for a message */
3047 for (i = 0; i < ctrl->last_tid; i++) {
3048 if (ctrl->txnt[i]) {
3049 ret = -EBUSY;
3050 mutex_unlock(&ctrl->m_ctrl);
3051 return -EBUSY;
3052 }
3053 }
3054 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3055 mutex_unlock(&ctrl->m_ctrl);
3056
3057 mutex_lock(&ctrl->sched.m_reconf);
3058 /* Data channels active */
3059 if (ctrl->sched.usedslots) {
3060 ret = -EBUSY;
3061 goto clk_pause_ret;
3062 }
3063
3064 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003065 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3066 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003067 if (ret)
3068 goto clk_pause_ret;
3069
3070 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003071 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3072 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3073 if (ret)
3074 goto clk_pause_ret;
3075
3076 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3077 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3078 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003079 if (ret)
3080 goto clk_pause_ret;
3081
3082clk_pause_ret:
3083 if (ret)
3084 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3085 else
3086 ctrl->clk_state = SLIM_CLK_PAUSED;
3087 complete(&ctrl->pause_comp);
3088 mutex_unlock(&ctrl->sched.m_reconf);
3089 return ret;
3090}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003091EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003092
3093MODULE_LICENSE("GPL v2");
3094MODULE_VERSION("0.1");
3095MODULE_DESCRIPTION("Slimbus module");
3096MODULE_ALIAS("platform:slimbus");