blob: bec03995c6f23bb196c9884e5da5c880ea62f947 [file] [log] [blame]
Sagar Dharia33beca02012-10-22 16:21:46 -06001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/completion.h>
18#include <linux/idr.h>
19#include <linux/pm_runtime.h>
20#include <linux/slimbus/slimbus.h>
21
22#define SLIM_PORT_HDL(la, f, p) ((la)<<24 | (f) << 16 | (p))
23
24#define SLIM_HDL_TO_LA(hdl) ((u32)((hdl) & 0xFF000000) >> 24)
25#define SLIM_HDL_TO_FLOW(hdl) (((u32)(hdl) & 0xFF0000) >> 16)
26#define SLIM_HDL_TO_PORT(hdl) ((u32)(hdl) & 0xFF)
27
Sagar Dharia29f35f02011-10-01 20:37:50 -060028#define SLIM_HDL_TO_CHIDX(hdl) ((u16)(hdl) & 0xFF)
Sagar Dhariab886e042012-10-17 22:41:57 -060029#define SLIM_GRP_TO_NCHAN(hdl) ((u16)(hdl >> 8) & 0xFF)
Sagar Dharia29f35f02011-10-01 20:37:50 -060030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031#define SLIM_SLAVE_PORT(p, la) (((la)<<16) | (p))
32#define SLIM_MGR_PORT(p) ((0xFF << 16) | (p))
33#define SLIM_LA_MANAGER 0xFF
34
35#define SLIM_START_GRP (1 << 8)
36#define SLIM_END_GRP (1 << 9)
37
38#define SLIM_MAX_INTR_COEFF_3 (SLIM_SL_PER_SUPERFRAME/3)
39#define SLIM_MAX_INTR_COEFF_1 SLIM_SL_PER_SUPERFRAME
40
41static DEFINE_MUTEX(slim_lock);
42static DEFINE_IDR(ctrl_idr);
43static struct device_type slim_dev_type;
44static struct device_type slim_ctrl_type;
45
46static const struct slim_device_id *slim_match(const struct slim_device_id *id,
47 const struct slim_device *slim_dev)
48{
49 while (id->name[0]) {
50 if (strncmp(slim_dev->name, id->name, SLIMBUS_NAME_SIZE) == 0)
51 return id;
52 id++;
53 }
54 return NULL;
55}
56
57static int slim_device_match(struct device *dev, struct device_driver *driver)
58{
59 struct slim_device *slim_dev;
60 struct slim_driver *drv = to_slim_driver(driver);
61
62 if (dev->type == &slim_dev_type)
63 slim_dev = to_slim_device(dev);
64 else
65 return 0;
66 if (drv->id_table)
67 return slim_match(drv->id_table, slim_dev) != NULL;
68
69 if (driver->name)
70 return strncmp(slim_dev->name, driver->name, SLIMBUS_NAME_SIZE)
71 == 0;
72 return 0;
73}
74
75#ifdef CONFIG_PM_SLEEP
76static int slim_legacy_suspend(struct device *dev, pm_message_t mesg)
77{
78 struct slim_device *slim_dev = NULL;
79 struct slim_driver *driver;
80 if (dev->type == &slim_dev_type)
81 slim_dev = to_slim_device(dev);
82
83 if (!slim_dev || !dev->driver)
84 return 0;
85
86 driver = to_slim_driver(dev->driver);
87 if (!driver->suspend)
88 return 0;
89
90 return driver->suspend(slim_dev, mesg);
91}
92
93static int slim_legacy_resume(struct device *dev)
94{
95 struct slim_device *slim_dev = NULL;
96 struct slim_driver *driver;
97 if (dev->type == &slim_dev_type)
98 slim_dev = to_slim_device(dev);
99
100 if (!slim_dev || !dev->driver)
101 return 0;
102
103 driver = to_slim_driver(dev->driver);
104 if (!driver->resume)
105 return 0;
106
107 return driver->resume(slim_dev);
108}
109
110static int slim_pm_suspend(struct device *dev)
111{
112 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
113
114 if (pm)
115 return pm_generic_suspend(dev);
116 else
117 return slim_legacy_suspend(dev, PMSG_SUSPEND);
118}
119
120static int slim_pm_resume(struct device *dev)
121{
122 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
123
124 if (pm)
125 return pm_generic_resume(dev);
126 else
127 return slim_legacy_resume(dev);
128}
129
130#else
131#define slim_pm_suspend NULL
132#define slim_pm_resume NULL
133#endif
134
135static const struct dev_pm_ops slimbus_pm = {
136 .suspend = slim_pm_suspend,
137 .resume = slim_pm_resume,
138 SET_RUNTIME_PM_OPS(
139 pm_generic_suspend,
140 pm_generic_resume,
141 pm_generic_runtime_idle
142 )
143};
144struct bus_type slimbus_type = {
145 .name = "slimbus",
146 .match = slim_device_match,
147 .pm = &slimbus_pm,
148};
149EXPORT_SYMBOL_GPL(slimbus_type);
150
151struct device slimbus_dev = {
152 .init_name = "slimbus",
153};
154
155static void __exit slimbus_exit(void)
156{
157 device_unregister(&slimbus_dev);
158 bus_unregister(&slimbus_type);
159}
160
161static int __init slimbus_init(void)
162{
163 int retval;
164
165 retval = bus_register(&slimbus_type);
166 if (!retval)
167 retval = device_register(&slimbus_dev);
168
169 if (retval)
170 bus_unregister(&slimbus_type);
171
172 return retval;
173}
174postcore_initcall(slimbus_init);
175module_exit(slimbus_exit);
176
177static int slim_drv_probe(struct device *dev)
178{
179 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600180 struct slim_device *sbdev = to_slim_device(dev);
181 struct slim_controller *ctrl = sbdev->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600183 if (sdrv->probe) {
184 int ret;
185 ret = sdrv->probe(sbdev);
186 if (ret)
187 return ret;
188 if (sdrv->device_up)
189 queue_work(ctrl->wq, &sbdev->wd);
190 return 0;
191 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 return -ENODEV;
193}
194
195static int slim_drv_remove(struct device *dev)
196{
197 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600198 struct slim_device *sbdev = to_slim_device(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600200 sbdev->notified = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 if (sdrv->remove)
202 return sdrv->remove(to_slim_device(dev));
203 return -ENODEV;
204}
205
206static void slim_drv_shutdown(struct device *dev)
207{
208 const struct slim_driver *sdrv = to_slim_driver(dev->driver);
209
210 if (sdrv->shutdown)
211 sdrv->shutdown(to_slim_device(dev));
212}
213
214/*
215 * slim_driver_register: Client driver registration with slimbus
216 * @drv:Client driver to be associated with client-device.
217 * This API will register the client driver with the slimbus
218 * It is called from the driver's module-init function.
219 */
220int slim_driver_register(struct slim_driver *drv)
221{
222 drv->driver.bus = &slimbus_type;
223 if (drv->probe)
224 drv->driver.probe = slim_drv_probe;
225
226 if (drv->remove)
227 drv->driver.remove = slim_drv_remove;
228
229 if (drv->shutdown)
230 drv->driver.shutdown = slim_drv_shutdown;
231
232 return driver_register(&drv->driver);
233}
234EXPORT_SYMBOL_GPL(slim_driver_register);
235
Sagar Dharia371bfa22013-01-22 17:51:41 -0700236/*
237 * slim_driver_unregister: Undo effects of slim_driver_register
238 * @drv: Client driver to be unregistered
239 */
240void slim_driver_unregister(struct slim_driver *drv)
241{
242 if (drv)
243 driver_unregister(&drv->driver);
244}
245EXPORT_SYMBOL_GPL(slim_driver_unregister);
246
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247#define slim_ctrl_attr_gr NULL
248
249static void slim_ctrl_release(struct device *dev)
250{
251 struct slim_controller *ctrl = to_slim_controller(dev);
252
253 complete(&ctrl->dev_released);
254}
255
256static struct device_type slim_ctrl_type = {
257 .groups = slim_ctrl_attr_gr,
258 .release = slim_ctrl_release,
259};
260
261static struct slim_controller *slim_ctrl_get(struct slim_controller *ctrl)
262{
263 if (!ctrl || !get_device(&ctrl->dev))
264 return NULL;
265
266 return ctrl;
267}
268
269static void slim_ctrl_put(struct slim_controller *ctrl)
270{
271 if (ctrl)
272 put_device(&ctrl->dev);
273}
274
275#define slim_device_attr_gr NULL
276#define slim_device_uevent NULL
277static void slim_dev_release(struct device *dev)
278{
279 struct slim_device *sbdev = to_slim_device(dev);
280 slim_ctrl_put(sbdev->ctrl);
281}
282
283static struct device_type slim_dev_type = {
284 .groups = slim_device_attr_gr,
285 .uevent = slim_device_uevent,
286 .release = slim_dev_release,
287};
288
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600289static void slim_report_present(struct work_struct *work)
290{
291 u8 laddr;
292 int ret;
293 struct slim_driver *sbdrv;
294 struct slim_device *sbdev =
295 container_of(work, struct slim_device, wd);
296 if (sbdev->notified || !sbdev->dev.driver)
297 return;
298 ret = slim_get_logical_addr(sbdev, sbdev->e_addr, 6, &laddr);
299 sbdrv = to_slim_driver(sbdev->dev.driver);
300 if (!ret && sbdrv->device_up) {
301 sbdev->notified = true;
302 sbdrv->device_up(sbdev);
303 }
304}
305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306/*
307 * slim_add_device: Add a new device without register board info.
308 * @ctrl: Controller to which this device is to be added to.
309 * Called when device doesn't have an explicit client-driver to be probed, or
310 * the client-driver is a module installed dynamically.
311 */
312int slim_add_device(struct slim_controller *ctrl, struct slim_device *sbdev)
313{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314 sbdev->dev.bus = &slimbus_type;
315 sbdev->dev.parent = ctrl->dev.parent;
316 sbdev->dev.type = &slim_dev_type;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600317 sbdev->dev.driver = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 sbdev->ctrl = ctrl;
319 slim_ctrl_get(ctrl);
320 dev_set_name(&sbdev->dev, "%s", sbdev->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 mutex_init(&sbdev->sldev_reconf);
322 INIT_LIST_HEAD(&sbdev->mark_define);
323 INIT_LIST_HEAD(&sbdev->mark_suspend);
324 INIT_LIST_HEAD(&sbdev->mark_removal);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600325 INIT_WORK(&sbdev->wd, slim_report_present);
326 mutex_lock(&ctrl->m_ctrl);
327 list_add_tail(&sbdev->dev_list, &ctrl->devs);
328 mutex_unlock(&ctrl->m_ctrl);
329 /* probe slave on this controller */
330 return device_register(&sbdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331}
332EXPORT_SYMBOL_GPL(slim_add_device);
333
334struct sbi_boardinfo {
335 struct list_head list;
336 struct slim_boardinfo board_info;
337};
338
339static LIST_HEAD(board_list);
340static LIST_HEAD(slim_ctrl_list);
341static DEFINE_MUTEX(board_lock);
342
343/* If controller is not present, only add to boards list */
344static void slim_match_ctrl_to_boardinfo(struct slim_controller *ctrl,
345 struct slim_boardinfo *bi)
346{
347 int ret;
348 if (ctrl->nr != bi->bus_num)
349 return;
350
351 ret = slim_add_device(ctrl, bi->slim_slave);
352 if (ret != 0)
353 dev_err(ctrl->dev.parent, "can't create new device for %s\n",
354 bi->slim_slave->name);
355}
356
357/*
358 * slim_register_board_info: Board-initialization routine.
359 * @info: List of all devices on all controllers present on the board.
360 * @n: number of entries.
361 * API enumerates respective devices on corresponding controller.
362 * Called from board-init function.
363 */
364int slim_register_board_info(struct slim_boardinfo const *info, unsigned n)
365{
366 struct sbi_boardinfo *bi;
367 int i;
368
369 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
370 if (!bi)
371 return -ENOMEM;
372
373 for (i = 0; i < n; i++, bi++, info++) {
374 struct slim_controller *ctrl;
375
376 memcpy(&bi->board_info, info, sizeof(*info));
377 mutex_lock(&board_lock);
378 list_add_tail(&bi->list, &board_list);
379 list_for_each_entry(ctrl, &slim_ctrl_list, list)
380 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
381 mutex_unlock(&board_lock);
382 }
383 return 0;
384}
385EXPORT_SYMBOL_GPL(slim_register_board_info);
386
387/*
Sagar Dhariaa6627e02012-08-28 12:20:49 -0600388 * slim_ctrl_add_boarddevs: Add devices registered by board-info
389 * @ctrl: Controller to which these devices are to be added to.
390 * This API is called by controller when it is up and running.
391 * If devices on a controller were registered before controller,
392 * this will make sure that they get probed when controller is up.
393 */
394void slim_ctrl_add_boarddevs(struct slim_controller *ctrl)
395{
396 struct sbi_boardinfo *bi;
397 mutex_lock(&board_lock);
398 list_add_tail(&ctrl->list, &slim_ctrl_list);
399 list_for_each_entry(bi, &board_list, list)
400 slim_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
401 mutex_unlock(&board_lock);
402}
403EXPORT_SYMBOL_GPL(slim_ctrl_add_boarddevs);
404
405/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 * slim_busnum_to_ctrl: Map bus number to controller
407 * @busnum: Bus number
408 * Returns controller representing this bus number
409 */
410struct slim_controller *slim_busnum_to_ctrl(u32 bus_num)
411{
412 struct slim_controller *ctrl;
413 mutex_lock(&board_lock);
414 list_for_each_entry(ctrl, &slim_ctrl_list, list)
415 if (bus_num == ctrl->nr) {
416 mutex_unlock(&board_lock);
417 return ctrl;
418 }
419 mutex_unlock(&board_lock);
420 return NULL;
421}
422EXPORT_SYMBOL_GPL(slim_busnum_to_ctrl);
423
424static int slim_register_controller(struct slim_controller *ctrl)
425{
426 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
428 /* Can't register until after driver model init */
429 if (WARN_ON(!slimbus_type.p)) {
430 ret = -EAGAIN;
431 goto out_list;
432 }
433
434 dev_set_name(&ctrl->dev, "sb-%d", ctrl->nr);
435 ctrl->dev.bus = &slimbus_type;
436 ctrl->dev.type = &slim_ctrl_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437 ctrl->num_dev = 0;
Sagar Dharia98a7ecb2011-07-25 15:25:35 -0600438 if (!ctrl->min_cg)
439 ctrl->min_cg = SLIM_MIN_CLK_GEAR;
440 if (!ctrl->max_cg)
441 ctrl->max_cg = SLIM_MAX_CLK_GEAR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442 mutex_init(&ctrl->m_ctrl);
443 mutex_init(&ctrl->sched.m_reconf);
444 ret = device_register(&ctrl->dev);
445 if (ret)
446 goto out_list;
447
448 dev_dbg(&ctrl->dev, "Bus [%s] registered:dev:%x\n", ctrl->name,
449 (u32)&ctrl->dev);
450
451 if (ctrl->nports) {
452 ctrl->ports = kzalloc(ctrl->nports * sizeof(struct slim_port),
453 GFP_KERNEL);
454 if (!ctrl->ports) {
455 ret = -ENOMEM;
456 goto err_port_failed;
457 }
458 }
459 if (ctrl->nchans) {
460 ctrl->chans = kzalloc(ctrl->nchans * sizeof(struct slim_ich),
461 GFP_KERNEL);
462 if (!ctrl->chans) {
463 ret = -ENOMEM;
464 goto err_chan_failed;
465 }
466
467 ctrl->sched.chc1 =
468 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
469 GFP_KERNEL);
470 if (!ctrl->sched.chc1) {
471 kfree(ctrl->chans);
472 ret = -ENOMEM;
473 goto err_chan_failed;
474 }
475 ctrl->sched.chc3 =
476 kzalloc(ctrl->nchans * sizeof(struct slim_ich *),
477 GFP_KERNEL);
478 if (!ctrl->sched.chc3) {
479 kfree(ctrl->sched.chc1);
480 kfree(ctrl->chans);
481 ret = -ENOMEM;
482 goto err_chan_failed;
483 }
484 }
485#ifdef DEBUG
486 ctrl->sched.slots = kzalloc(SLIM_SL_PER_SUPERFRAME, GFP_KERNEL);
487#endif
Sagar Dharia33f34442011-08-08 16:22:03 -0600488 init_completion(&ctrl->pause_comp);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600489
490 INIT_LIST_HEAD(&ctrl->devs);
491 ctrl->wq = create_singlethread_workqueue(dev_name(&ctrl->dev));
492 if (!ctrl->wq)
493 goto err_workq_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494
495 return 0;
496
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600497err_workq_failed:
498 kfree(ctrl->sched.chc3);
499 kfree(ctrl->sched.chc1);
500 kfree(ctrl->chans);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501err_chan_failed:
502 kfree(ctrl->ports);
503err_port_failed:
504 device_unregister(&ctrl->dev);
505out_list:
506 mutex_lock(&slim_lock);
507 idr_remove(&ctrl_idr, ctrl->nr);
508 mutex_unlock(&slim_lock);
509 return ret;
510}
511
512/* slim_remove_device: Remove the effect of slim_add_device() */
513void slim_remove_device(struct slim_device *sbdev)
514{
515 device_unregister(&sbdev->dev);
516}
517EXPORT_SYMBOL_GPL(slim_remove_device);
518
519static void slim_ctrl_remove_device(struct slim_controller *ctrl,
520 struct slim_boardinfo *bi)
521{
522 if (ctrl->nr == bi->bus_num)
523 slim_remove_device(bi->slim_slave);
524}
525
526/*
527 * slim_del_controller: Controller tear-down.
528 * Controller added with the above API is teared down using this API.
529 */
530int slim_del_controller(struct slim_controller *ctrl)
531{
532 struct slim_controller *found;
533 struct sbi_boardinfo *bi;
534
535 /* First make sure that this bus was added */
536 mutex_lock(&slim_lock);
537 found = idr_find(&ctrl_idr, ctrl->nr);
538 mutex_unlock(&slim_lock);
539 if (found != ctrl)
540 return -EINVAL;
541
542 /* Remove all clients */
543 mutex_lock(&board_lock);
544 list_for_each_entry(bi, &board_list, list)
545 slim_ctrl_remove_device(ctrl, &bi->board_info);
546 mutex_unlock(&board_lock);
547
548 init_completion(&ctrl->dev_released);
549 device_unregister(&ctrl->dev);
550
551 wait_for_completion(&ctrl->dev_released);
552 list_del(&ctrl->list);
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600553 destroy_workqueue(ctrl->wq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 /* free bus id */
555 mutex_lock(&slim_lock);
556 idr_remove(&ctrl_idr, ctrl->nr);
557 mutex_unlock(&slim_lock);
558
559 kfree(ctrl->sched.chc1);
560 kfree(ctrl->sched.chc3);
561#ifdef DEBUG
562 kfree(ctrl->sched.slots);
563#endif
564 kfree(ctrl->chans);
565 kfree(ctrl->ports);
566
567 return 0;
568}
569EXPORT_SYMBOL_GPL(slim_del_controller);
570
571/*
572 * slim_add_numbered_controller: Controller bring-up.
573 * @ctrl: Controller to be registered.
574 * A controller is registered with the framework using this API. ctrl->nr is the
575 * desired number with which slimbus framework registers the controller.
576 * Function will return -EBUSY if the number is in use.
577 */
578int slim_add_numbered_controller(struct slim_controller *ctrl)
579{
580 int id;
581 int status;
582
583 if (ctrl->nr & ~MAX_ID_MASK)
584 return -EINVAL;
585
586retry:
587 if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0)
588 return -ENOMEM;
589
590 mutex_lock(&slim_lock);
591 status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
592 if (status == 0 && id != ctrl->nr) {
593 status = -EAGAIN;
594 idr_remove(&ctrl_idr, id);
595 }
596 mutex_unlock(&slim_lock);
597 if (status == -EAGAIN)
598 goto retry;
599
600 if (status == 0)
601 status = slim_register_controller(ctrl);
602 return status;
603}
604EXPORT_SYMBOL_GPL(slim_add_numbered_controller);
605
606/*
607 * slim_msg_response: Deliver Message response received from a device to the
608 * framework.
609 * @ctrl: Controller handle
610 * @reply: Reply received from the device
611 * @len: Length of the reply
612 * @tid: Transaction ID received with which framework can associate reply.
613 * Called by controller to inform framework about the response received.
614 * This helps in making the API asynchronous, and controller-driver doesn't need
615 * to manage 1 more table other than the one managed by framework mapping TID
616 * with buffers
617 */
618void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
619{
620 int i;
621 struct slim_msg_txn *txn;
622
623 mutex_lock(&ctrl->m_ctrl);
624 txn = ctrl->txnt[tid];
625 if (txn == NULL) {
626 dev_err(&ctrl->dev, "Got response to invalid TID:%d, len:%d",
627 tid, len);
628 mutex_unlock(&ctrl->m_ctrl);
629 return;
630 }
631 for (i = 0; i < len; i++)
632 txn->rbuf[i] = reply[i];
633 if (txn->comp)
634 complete(txn->comp);
635 ctrl->txnt[tid] = NULL;
636 mutex_unlock(&ctrl->m_ctrl);
637 kfree(txn);
638}
639EXPORT_SYMBOL_GPL(slim_msg_response);
640
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600641static int slim_processtxn(struct slim_controller *ctrl, u8 dt, u16 mc, u16 ec,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 u8 mt, u8 *rbuf, const u8 *wbuf, u8 len, u8 mlen,
643 struct completion *comp, u8 la, u8 *tid)
644{
645 u8 i = 0;
646 int ret = 0;
647 struct slim_msg_txn *txn = kmalloc(sizeof(struct slim_msg_txn),
648 GFP_KERNEL);
649 if (!txn)
650 return -ENOMEM;
651 if (tid) {
652 mutex_lock(&ctrl->m_ctrl);
653 for (i = 0; i < ctrl->last_tid; i++) {
654 if (ctrl->txnt[i] == NULL)
655 break;
656 }
657 if (i >= ctrl->last_tid) {
658 if (ctrl->last_tid == 255) {
659 mutex_unlock(&ctrl->m_ctrl);
660 kfree(txn);
661 return -ENOMEM;
662 }
663 ctrl->txnt = krealloc(ctrl->txnt,
664 (i + 1) * sizeof(struct slim_msg_txn *),
665 GFP_KERNEL);
666 if (!ctrl->txnt) {
667 mutex_unlock(&ctrl->m_ctrl);
668 kfree(txn);
669 return -ENOMEM;
670 }
671 ctrl->last_tid++;
672 }
673 ctrl->txnt[i] = txn;
674 mutex_unlock(&ctrl->m_ctrl);
675 txn->tid = i;
676 *tid = i;
677 }
678 txn->mc = mc;
679 txn->mt = mt;
680 txn->dt = dt;
681 txn->ec = ec;
682 txn->la = la;
683 txn->rbuf = rbuf;
684 txn->wbuf = wbuf;
685 txn->rl = mlen;
686 txn->len = len;
687 txn->comp = comp;
688
689 ret = ctrl->xfer_msg(ctrl, txn);
690 if (!tid)
691 kfree(txn);
692 return ret;
693}
694
695static int ctrl_getlogical_addr(struct slim_controller *ctrl, const u8 *eaddr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600696 u8 e_len, u8 *entry)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697{
698 u8 i;
699 for (i = 0; i < ctrl->num_dev; i++) {
700 if (ctrl->addrt[i].valid &&
701 memcmp(ctrl->addrt[i].eaddr, eaddr, e_len) == 0) {
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600702 *entry = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 return 0;
704 }
705 }
706 return -ENXIO;
707}
708
709/*
710 * slim_assign_laddr: Assign logical address to a device enumerated.
711 * @ctrl: Controller with which device is enumerated.
712 * @e_addr: 6-byte elemental address of the device.
713 * @e_len: buffer length for e_addr
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600714 * @laddr: Return logical address (if valid flag is false)
715 * @valid: true if laddr holds a valid address that controller wants to
716 * set for this enumeration address. Otherwise framework sets index into
717 * address table as logical address.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 * Called by controller in response to REPORT_PRESENT. Framework will assign
719 * a logical address to this enumeration address.
720 * Function returns -EXFULL to indicate that all logical addresses are already
721 * taken.
722 */
723int slim_assign_laddr(struct slim_controller *ctrl, const u8 *e_addr,
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600724 u8 e_len, u8 *laddr, bool valid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725{
726 int ret;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600727 u8 i = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600728 bool exists = false;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600729 struct slim_device *sbdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 mutex_lock(&ctrl->m_ctrl);
731 /* already assigned */
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600732 if (ctrl_getlogical_addr(ctrl, e_addr, e_len, &i) == 0) {
733 *laddr = ctrl->addrt[i].laddr;
734 exists = true;
735 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 if (ctrl->num_dev >= 254) {
737 ret = -EXFULL;
738 goto ret_assigned_laddr;
739 }
740 for (i = 0; i < ctrl->num_dev; i++) {
741 if (ctrl->addrt[i].valid == false)
742 break;
743 }
744 if (i == ctrl->num_dev) {
745 ctrl->addrt = krealloc(ctrl->addrt,
746 (ctrl->num_dev + 1) *
747 sizeof(struct slim_addrt),
748 GFP_KERNEL);
749 if (!ctrl->addrt) {
750 ret = -ENOMEM;
751 goto ret_assigned_laddr;
752 }
753 ctrl->num_dev++;
754 }
755 memcpy(ctrl->addrt[i].eaddr, e_addr, e_len);
756 ctrl->addrt[i].valid = true;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600757 /* Preferred address is index into table */
758 if (!valid)
759 *laddr = i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 }
761
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600762 ret = ctrl->set_laddr(ctrl, (const u8 *)&ctrl->addrt[i].eaddr, 6,
763 *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 if (ret) {
765 ctrl->addrt[i].valid = false;
766 goto ret_assigned_laddr;
767 }
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600768 ctrl->addrt[i].laddr = *laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600770 dev_dbg(&ctrl->dev, "setting slimbus l-addr:%x\n", *laddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771ret_assigned_laddr:
772 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600773 if (exists || ret)
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600774 return ret;
775
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600776 pr_info("slimbus:%d laddr:0x%x, EAPC:0x%x:0x%x", ctrl->nr, *laddr,
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600777 e_addr[1], e_addr[2]);
778 mutex_lock(&ctrl->m_ctrl);
779 list_for_each_entry(sbdev, &ctrl->devs, dev_list) {
780 if (memcmp(sbdev->e_addr, e_addr, 6) == 0) {
781 struct slim_driver *sbdrv;
Sagar Dharia33c84e62012-10-30 21:12:09 -0600782 sbdev->laddr = *laddr;
Sagar Dharia0e7a1ef2012-07-23 19:38:57 -0600783 if (sbdev->dev.driver) {
784 sbdrv = to_slim_driver(sbdev->dev.driver);
785 if (sbdrv->device_up)
786 queue_work(ctrl->wq, &sbdev->wd);
787 }
788 break;
789 }
790 }
791 mutex_unlock(&ctrl->m_ctrl);
792 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793}
794EXPORT_SYMBOL_GPL(slim_assign_laddr);
795
796/*
797 * slim_get_logical_addr: Return the logical address of a slimbus device.
798 * @sb: client handle requesting the adddress.
799 * @e_addr: Elemental address of the device.
800 * @e_len: Length of e_addr
801 * @laddr: output buffer to store the address
802 * context: can sleep
803 * -EINVAL is returned in case of invalid parameters, and -ENXIO is returned if
804 * the device with this elemental address is not found.
805 */
806int slim_get_logical_addr(struct slim_device *sb, const u8 *e_addr,
807 u8 e_len, u8 *laddr)
808{
809 int ret = 0;
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600810 u8 entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 struct slim_controller *ctrl = sb->ctrl;
812 if (!ctrl || !laddr || !e_addr || e_len != 6)
813 return -EINVAL;
814 mutex_lock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600815 ret = ctrl_getlogical_addr(ctrl, e_addr, e_len, &entry);
816 if (!ret)
817 *laddr = ctrl->addrt[entry].laddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 mutex_unlock(&ctrl->m_ctrl);
Sagar Dhariaf0b9c752012-09-09 17:32:46 -0600819 if (ret == -ENXIO && ctrl->get_laddr) {
820 ret = ctrl->get_laddr(ctrl, e_addr, e_len, laddr);
821 if (!ret)
822 ret = slim_assign_laddr(ctrl, e_addr, e_len, laddr,
823 true);
824 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 return ret;
826}
827EXPORT_SYMBOL_GPL(slim_get_logical_addr);
828
829static int slim_ele_access_sanity(struct slim_ele_access *msg, int oper,
830 u8 *rbuf, const u8 *wbuf, u8 len)
831{
832 if (!msg || msg->num_bytes > 16 || msg->start_offset + len > 0xC00)
833 return -EINVAL;
834 switch (oper) {
835 case SLIM_MSG_MC_REQUEST_VALUE:
836 case SLIM_MSG_MC_REQUEST_INFORMATION:
837 if (rbuf == NULL)
838 return -EINVAL;
839 return 0;
840 case SLIM_MSG_MC_CHANGE_VALUE:
841 case SLIM_MSG_MC_CLEAR_INFORMATION:
842 if (wbuf == NULL)
843 return -EINVAL;
844 return 0;
845 case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
846 case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
847 if (rbuf == NULL || wbuf == NULL)
848 return -EINVAL;
849 return 0;
850 default:
851 return -EINVAL;
852 }
853}
854
855static u16 slim_slicecodefromsize(u32 req)
856{
857 u8 codetosize[8] = {1, 2, 3, 4, 6, 8, 12, 16};
858 if (req >= 8)
859 return 0;
860 else
861 return codetosize[req];
862}
863
864static u16 slim_slicesize(u32 code)
865{
866 u8 sizetocode[16] = {0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7};
867 if (code == 0)
868 code = 1;
869 if (code > 16)
870 code = 16;
871 return sizetocode[code - 1];
872}
873
874
875/* Message APIs Unicast message APIs used by slimbus slave drivers */
876
877/*
878 * Message API access routines.
879 * @sb: client handle requesting elemental message reads, writes.
880 * @msg: Input structure for start-offset, number of bytes to read.
881 * @rbuf: data buffer to be filled with values read.
882 * @len: data buffer size
883 * @wbuf: data buffer containing value/information to be written
884 * context: can sleep
885 * Returns:
886 * -EINVAL: Invalid parameters
887 * -ETIMEDOUT: If controller could not complete the request. This may happen if
888 * the bus lines are not clocked, controller is not powered-on, slave with
889 * given address is not enumerated/responding.
890 */
891int slim_request_val_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_VALUE, buf,
898 NULL, len);
899}
900EXPORT_SYMBOL_GPL(slim_request_val_element);
901
902int slim_request_inf_element(struct slim_device *sb,
903 struct slim_ele_access *msg, 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_REQUEST_INFORMATION,
909 buf, NULL, len);
910}
911EXPORT_SYMBOL_GPL(slim_request_inf_element);
912
913int slim_change_val_element(struct slim_device *sb, struct slim_ele_access *msg,
914 const 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_CHANGE_VALUE, NULL, buf,
920 len);
921}
922EXPORT_SYMBOL_GPL(slim_change_val_element);
923
924int slim_clear_inf_element(struct slim_device *sb, struct slim_ele_access *msg,
925 u8 *buf, u8 len)
926{
927 struct slim_controller *ctrl = sb->ctrl;
928 if (!ctrl)
929 return -EINVAL;
930 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_CLEAR_INFORMATION, NULL,
931 buf, len);
932}
933EXPORT_SYMBOL_GPL(slim_clear_inf_element);
934
935int slim_request_change_val_element(struct slim_device *sb,
936 struct slim_ele_access *msg, u8 *rbuf,
937 const u8 *wbuf, u8 len)
938{
939 struct slim_controller *ctrl = sb->ctrl;
940 if (!ctrl)
941 return -EINVAL;
942 return slim_xfer_msg(ctrl, sb, msg, SLIM_MSG_MC_REQUEST_CHANGE_VALUE,
943 rbuf, wbuf, len);
944}
945EXPORT_SYMBOL_GPL(slim_request_change_val_element);
946
947int slim_request_clear_inf_element(struct slim_device *sb,
948 struct slim_ele_access *msg, u8 *rbuf,
949 const u8 *wbuf, u8 len)
950{
951 struct slim_controller *ctrl = sb->ctrl;
952 if (!ctrl)
953 return -EINVAL;
954 return slim_xfer_msg(ctrl, sb, msg,
955 SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION,
956 rbuf, wbuf, len);
957}
958EXPORT_SYMBOL_GPL(slim_request_clear_inf_element);
959
960/*
961 * Broadcast message API:
962 * call this API directly with sbdev = NULL.
963 * For broadcast reads, make sure that buffers are big-enough to incorporate
964 * replies from all logical addresses.
965 * All controllers may not support broadcast
966 */
967int slim_xfer_msg(struct slim_controller *ctrl, struct slim_device *sbdev,
Sagar Dharia45ee38a2011-08-03 17:01:31 -0600968 struct slim_ele_access *msg, u16 mc, u8 *rbuf,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969 const u8 *wbuf, u8 len)
970{
971 DECLARE_COMPLETION_ONSTACK(complete);
972 int ret;
973 u16 sl, cur;
974 u16 ec;
975 u8 tid, mlen = 6;
976
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 ret = slim_ele_access_sanity(msg, mc, rbuf, wbuf, len);
978 if (ret)
979 goto xfer_err;
980
981 sl = slim_slicesize(len);
982 dev_dbg(&ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
983 msg->start_offset, len, mc, sl);
984
985 cur = slim_slicecodefromsize(sl);
986 ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
987
988 if (wbuf)
989 mlen += len;
990 if (rbuf) {
991 mlen++;
992 if (!msg->comp)
993 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
994 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
995 &complete, sbdev->laddr, &tid);
996 else
997 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR,
998 mc, ec, SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
999 msg->comp, sbdev->laddr, &tid);
1000 /* sync read */
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001001 if (!ret && !msg->comp) {
1002 ret = wait_for_completion_timeout(&complete, HZ);
1003 if (!ret) {
1004 struct slim_msg_txn *txn;
1005 dev_err(&ctrl->dev, "slimbus Read timed out");
1006 mutex_lock(&ctrl->m_ctrl);
1007 txn = ctrl->txnt[tid];
1008 /* Invalidate the transaction */
1009 ctrl->txnt[tid] = NULL;
1010 mutex_unlock(&ctrl->m_ctrl);
1011 kfree(txn);
1012 ret = -ETIMEDOUT;
1013 } else
1014 ret = 0;
Sagar Dharia53a9f792012-09-04 19:56:18 -06001015 } else if (ret < 0 && !msg->comp) {
1016 struct slim_msg_txn *txn;
1017 dev_err(&ctrl->dev, "slimbus Read error");
1018 mutex_lock(&ctrl->m_ctrl);
1019 txn = ctrl->txnt[tid];
1020 /* Invalidate the transaction */
1021 ctrl->txnt[tid] = NULL;
1022 mutex_unlock(&ctrl->m_ctrl);
1023 kfree(txn);
Sagar Dhariacd0a2522011-08-31 18:29:31 -06001024 }
1025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 } else
1027 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, ec,
1028 SLIM_MSG_MT_CORE, rbuf, wbuf, len, mlen,
1029 NULL, sbdev->laddr, NULL);
1030xfer_err:
1031 return ret;
1032}
1033EXPORT_SYMBOL_GPL(slim_xfer_msg);
1034
1035/*
1036 * slim_alloc_mgrports: Allocate port on manager side.
1037 * @sb: device/client handle.
1038 * @req: Port request type.
1039 * @nports: Number of ports requested
1040 * @rh: output buffer to store the port handles
1041 * @hsz: size of buffer storing handles
1042 * context: can sleep
1043 * This port will be typically used by SW. e.g. client driver wants to receive
1044 * some data from audio codec HW using a data channel.
1045 * Port allocated using this API will be used to receive the data.
1046 * If half-duplex ports are requested, two adjacent ports are allocated for
1047 * 1 half-duplex port. So the handle-buffer size should be twice the number
1048 * of half-duplex ports to be allocated.
1049 * -EDQUOT is returned if all ports are in use.
1050 */
1051int slim_alloc_mgrports(struct slim_device *sb, enum slim_port_req req,
1052 int nports, u32 *rh, int hsz)
1053{
Sagar Dharia4d364c22011-10-04 12:47:21 -06001054 int i, j;
1055 int ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 int nphysp = nports;
1057 struct slim_controller *ctrl = sb->ctrl;
1058
1059 if (!rh || !ctrl)
1060 return -EINVAL;
1061 if (req == SLIM_REQ_HALF_DUP)
1062 nphysp *= 2;
1063 if (hsz/sizeof(u32) < nphysp)
1064 return -EINVAL;
1065 mutex_lock(&ctrl->m_ctrl);
1066
1067 for (i = 0; i < ctrl->nports; i++) {
1068 bool multiok = true;
1069 if (ctrl->ports[i].state != SLIM_P_FREE)
1070 continue;
1071 /* Start half duplex channel at even port */
1072 if (req == SLIM_REQ_HALF_DUP && (i % 2))
1073 continue;
1074 /* Allocate ports contiguously for multi-ch */
1075 if (ctrl->nports < (i + nphysp)) {
1076 i = ctrl->nports;
1077 break;
1078 }
1079 if (req == SLIM_REQ_MULTI_CH) {
1080 multiok = true;
1081 for (j = i; j < i + nphysp; j++) {
1082 if (ctrl->ports[j].state != SLIM_P_FREE) {
1083 multiok = false;
1084 break;
1085 }
1086 }
1087 if (!multiok)
1088 continue;
1089 }
1090 break;
1091 }
1092 if (i >= ctrl->nports)
1093 ret = -EDQUOT;
1094 for (j = i; j < i + nphysp; j++) {
1095 ctrl->ports[j].state = SLIM_P_UNCFG;
1096 ctrl->ports[j].req = req;
1097 if (req == SLIM_REQ_HALF_DUP && (j % 2))
1098 ctrl->ports[j].flow = SLIM_SINK;
1099 else
1100 ctrl->ports[j].flow = SLIM_SRC;
1101 ret = ctrl->config_port(ctrl, j);
1102 if (ret) {
1103 for (; j >= i; j--)
1104 ctrl->ports[j].state = SLIM_P_FREE;
1105 goto alloc_err;
1106 }
1107 *rh++ = SLIM_PORT_HDL(SLIM_LA_MANAGER, 0, j);
1108 }
1109alloc_err:
1110 mutex_unlock(&ctrl->m_ctrl);
1111 return ret;
1112}
1113EXPORT_SYMBOL_GPL(slim_alloc_mgrports);
1114
1115/* Deallocate the port(s) allocated using the API above */
1116int slim_dealloc_mgrports(struct slim_device *sb, u32 *hdl, int nports)
1117{
1118 int i;
1119 struct slim_controller *ctrl = sb->ctrl;
1120
1121 if (!ctrl || !hdl)
1122 return -EINVAL;
1123
1124 mutex_lock(&ctrl->m_ctrl);
1125
1126 for (i = 0; i < nports; i++) {
1127 u8 pn;
1128 pn = SLIM_HDL_TO_PORT(hdl[i]);
1129 if (ctrl->ports[pn].state == SLIM_P_CFG) {
1130 int j;
1131 dev_err(&ctrl->dev, "Can't dealloc connected port:%d",
1132 i);
1133 for (j = i - 1; j >= 0; j--) {
1134 pn = SLIM_HDL_TO_PORT(hdl[j]);
1135 ctrl->ports[pn].state = SLIM_P_UNCFG;
1136 }
1137 mutex_unlock(&ctrl->m_ctrl);
1138 return -EISCONN;
1139 }
1140 ctrl->ports[pn].state = SLIM_P_FREE;
1141 }
1142 mutex_unlock(&ctrl->m_ctrl);
1143 return 0;
1144}
1145EXPORT_SYMBOL_GPL(slim_dealloc_mgrports);
1146
1147/*
1148 * slim_get_slaveport: Get slave port handle
1149 * @la: slave device logical address.
1150 * @idx: port index at slave
1151 * @rh: return handle
1152 * @flw: Flow type (source or destination)
1153 * This API only returns a slave port's representation as expected by slimbus
1154 * driver. This port is not managed by the slimbus driver. Caller is expected
1155 * to have visibility of this port since it's a device-port.
1156 */
1157int slim_get_slaveport(u8 la, int idx, u32 *rh, enum slim_port_flow flw)
1158{
1159 if (rh == NULL)
1160 return -EINVAL;
1161 *rh = SLIM_PORT_HDL(la, flw, idx);
1162 return 0;
1163}
1164EXPORT_SYMBOL_GPL(slim_get_slaveport);
1165
1166static int connect_port_ch(struct slim_controller *ctrl, u8 ch, u32 ph,
1167 enum slim_port_flow flow)
1168{
1169 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001170 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 u8 buf[2];
1172 u32 la = SLIM_HDL_TO_LA(ph);
1173 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1174
1175 if (flow == SLIM_SRC)
1176 mc = SLIM_MSG_MC_CONNECT_SOURCE;
1177 else
1178 mc = SLIM_MSG_MC_CONNECT_SINK;
1179 buf[0] = pn;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001180 buf[1] = ctrl->chans[ch].chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 if (la == SLIM_LA_MANAGER)
1182 ctrl->ports[pn].flow = flow;
1183 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1184 SLIM_MSG_MT_CORE, NULL, buf, 2, 6, NULL, la,
1185 NULL);
1186 if (!ret && la == SLIM_LA_MANAGER)
1187 ctrl->ports[pn].state = SLIM_P_CFG;
1188 return ret;
1189}
1190
1191static int disconnect_port_ch(struct slim_controller *ctrl, u32 ph)
1192{
1193 int ret;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001194 u16 mc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 u32 la = SLIM_HDL_TO_LA(ph);
1196 u8 pn = (u8)SLIM_HDL_TO_PORT(ph);
1197
1198 mc = SLIM_MSG_MC_DISCONNECT_PORT;
1199 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_LOGICALADDR, mc, 0,
1200 SLIM_MSG_MT_CORE, NULL, &pn, 1, 5,
1201 NULL, la, NULL);
1202 if (ret)
1203 return ret;
1204 if (la == SLIM_LA_MANAGER)
1205 ctrl->ports[pn].state = SLIM_P_UNCFG;
1206 return 0;
1207}
1208
1209/*
Sagar Dharia29f35f02011-10-01 20:37:50 -06001210 * slim_connect_src: Connect source port to channel.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 * @sb: client handle
Sagar Dharia29f35f02011-10-01 20:37:50 -06001212 * @srch: source handle to be connected to this channel
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001213 * @chanh: Channel with which the ports need to be associated with.
Sagar Dharia29f35f02011-10-01 20:37:50 -06001214 * Per slimbus specification, a channel may have 1 source port.
1215 * Channel specified in chanh needs to be allocated first.
1216 * Returns -EALREADY if source is already configured for this channel.
1217 * Returns -ENOTCONN if channel is not allocated
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001219int slim_connect_src(struct slim_device *sb, u32 srch, u16 chanh)
1220{
1221 struct slim_controller *ctrl = sb->ctrl;
1222 int ret;
1223 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
1224 struct slim_ich *slc = &ctrl->chans[chan];
1225 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(srch);
1226
1227 if (flow != SLIM_SRC)
1228 return -EINVAL;
1229
Sagar Dhariad2959352012-12-01 15:43:01 -07001230 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001231
1232 if (slc->state == SLIM_CH_FREE) {
1233 ret = -ENOTCONN;
1234 goto connect_src_err;
1235 }
1236 /*
1237 * Once channel is removed, its ports can be considered disconnected
1238 * So its ports can be reassigned. Source port is zeroed
1239 * when channel is deallocated.
1240 */
1241 if (slc->srch) {
1242 ret = -EALREADY;
1243 goto connect_src_err;
1244 }
1245
1246 ret = connect_port_ch(ctrl, chan, srch, SLIM_SRC);
1247
1248 if (!ret)
1249 slc->srch = srch;
1250
1251connect_src_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001252 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia29f35f02011-10-01 20:37:50 -06001253 return ret;
1254}
1255EXPORT_SYMBOL_GPL(slim_connect_src);
1256
1257/*
1258 * slim_connect_sink: Connect sink port(s) to channel.
1259 * @sb: client handle
1260 * @sinkh: sink handle(s) to be connected to this channel
1261 * @nsink: number of sinks
1262 * @chanh: Channel with which the ports need to be associated with.
1263 * Per slimbus specification, a channel may have multiple sink-ports.
1264 * Channel specified in chanh needs to be allocated first.
1265 * Returns -EALREADY if sink is already configured for this channel.
1266 * Returns -ENOTCONN if channel is not allocated
1267 */
1268int slim_connect_sink(struct slim_device *sb, u32 *sinkh, int nsink, u16 chanh)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269{
1270 struct slim_controller *ctrl = sb->ctrl;
1271 int j;
1272 int ret = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001273 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274 struct slim_ich *slc = &ctrl->chans[chan];
1275
Sagar Dharia29f35f02011-10-01 20:37:50 -06001276 if (!sinkh || !nsink)
1277 return -EINVAL;
1278
Sagar Dhariad2959352012-12-01 15:43:01 -07001279 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280
1281 /*
1282 * Once channel is removed, its ports can be considered disconnected
Sagar Dharia29f35f02011-10-01 20:37:50 -06001283 * So its ports can be reassigned. Sink ports are freed when channel
1284 * is deallocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 */
Sagar Dharia29f35f02011-10-01 20:37:50 -06001286 if (slc->state == SLIM_CH_FREE) {
1287 ret = -ENOTCONN;
1288 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 }
Sagar Dharia33f34442011-08-08 16:22:03 -06001290
Sagar Dharia29f35f02011-10-01 20:37:50 -06001291 for (j = 0; j < nsink; j++) {
1292 enum slim_port_flow flow = SLIM_HDL_TO_FLOW(sinkh[j]);
1293 if (flow != SLIM_SINK)
1294 ret = -EINVAL;
1295 else
1296 ret = connect_port_ch(ctrl, chan, sinkh[j], SLIM_SINK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 if (ret) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001298 for (j = j - 1; j >= 0; j--)
1299 disconnect_port_ch(ctrl, sinkh[j]);
1300 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 }
1302 }
Sagar Dharia29f35f02011-10-01 20:37:50 -06001303
1304 slc->sinkh = krealloc(slc->sinkh, (sizeof(u32) * (slc->nsink + nsink)),
1305 GFP_KERNEL);
1306 if (!slc->sinkh) {
1307 ret = -ENOMEM;
1308 for (j = 0; j < nsink; j++)
1309 disconnect_port_ch(ctrl, sinkh[j]);
1310 goto connect_sink_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 }
1312
Sagar Dharia29f35f02011-10-01 20:37:50 -06001313 memcpy(slc->sinkh + slc->nsink, sinkh, (sizeof(u32) * nsink));
1314 slc->nsink += nsink;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315
Sagar Dharia29f35f02011-10-01 20:37:50 -06001316connect_sink_err:
Sagar Dhariad2959352012-12-01 15:43:01 -07001317 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 return ret;
1319}
Sagar Dharia29f35f02011-10-01 20:37:50 -06001320EXPORT_SYMBOL_GPL(slim_connect_sink);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321
1322/*
1323 * slim_disconnect_ports: Disconnect port(s) from channel
1324 * @sb: client handle
1325 * @ph: ports to be disconnected
1326 * @nph: number of ports.
1327 * Disconnects ports from a channel.
1328 */
1329int slim_disconnect_ports(struct slim_device *sb, u32 *ph, int nph)
1330{
1331 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia45ee38a2011-08-03 17:01:31 -06001332 int i;
Sagar Dharia33f34442011-08-08 16:22:03 -06001333
Sagar Dhariad2959352012-12-01 15:43:01 -07001334 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia33f34442011-08-08 16:22:03 -06001335
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336 for (i = 0; i < nph; i++)
1337 disconnect_port_ch(ctrl, ph[i]);
Sagar Dhariad2959352012-12-01 15:43:01 -07001338 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 return 0;
1340}
1341EXPORT_SYMBOL_GPL(slim_disconnect_ports);
1342
1343/*
1344 * slim_port_xfer: Schedule buffer to be transferred/received using port-handle.
1345 * @sb: client handle
1346 * @ph: port-handle
1347 * @iobuf: buffer to be transferred or populated
1348 * @len: buffer size.
1349 * @comp: completion signal to indicate transfer done or error.
1350 * context: can sleep
1351 * Returns number of bytes transferred/received if used synchronously.
1352 * Will return 0 if used asynchronously.
1353 * Client will call slim_port_get_xfer_status to get error and/or number of
1354 * bytes transferred if used asynchronously.
1355 */
1356int slim_port_xfer(struct slim_device *sb, u32 ph, u8 *iobuf, u32 len,
1357 struct completion *comp)
1358{
1359 struct slim_controller *ctrl = sb->ctrl;
1360 u8 pn = SLIM_HDL_TO_PORT(ph);
1361 dev_dbg(&ctrl->dev, "port xfer: num:%d", pn);
1362 return ctrl->port_xfer(ctrl, pn, iobuf, len, comp);
1363}
1364EXPORT_SYMBOL_GPL(slim_port_xfer);
1365
1366/*
1367 * slim_port_get_xfer_status: Poll for port transfers, or get transfer status
1368 * after completion is done.
1369 * @sb: client handle
1370 * @ph: port-handle
1371 * @done_buf: return pointer (iobuf from slim_port_xfer) which is processed.
1372 * @done_len: Number of bytes transferred.
1373 * This can be called when port_xfer complition is signalled.
1374 * The API will return port transfer error (underflow/overflow/disconnect)
1375 * and/or done_len will reflect number of bytes transferred. Note that
1376 * done_len may be valid even if port error (overflow/underflow) has happened.
1377 * e.g. If the transfer was scheduled with a few bytes to be transferred and
1378 * client has not supplied more data to be transferred, done_len will indicate
1379 * number of bytes transferred with underflow error. To avoid frequent underflow
1380 * errors, multiple transfers can be queued (e.g. ping-pong buffers) so that
1381 * channel has data to be transferred even if client is not ready to transfer
1382 * data all the time. done_buf will indicate address of the last buffer
1383 * processed from the multiple transfers.
1384 */
1385enum slim_port_err slim_port_get_xfer_status(struct slim_device *sb, u32 ph,
1386 u8 **done_buf, u32 *done_len)
1387{
1388 struct slim_controller *ctrl = sb->ctrl;
1389 u8 pn = SLIM_HDL_TO_PORT(ph);
1390 u32 la = SLIM_HDL_TO_LA(ph);
1391 enum slim_port_err err;
1392 dev_dbg(&ctrl->dev, "get status port num:%d", pn);
1393 /*
1394 * Framework only has insight into ports managed by ported device
1395 * used by the manager and not slave
1396 */
1397 if (la != SLIM_LA_MANAGER) {
1398 if (done_buf)
1399 *done_buf = NULL;
1400 if (done_len)
1401 *done_len = 0;
1402 return SLIM_P_NOT_OWNED;
1403 }
1404 err = ctrl->port_xfer_status(ctrl, pn, done_buf, done_len);
1405 if (err == SLIM_P_INPROGRESS)
1406 err = ctrl->ports[pn].err;
1407 return err;
1408}
1409EXPORT_SYMBOL_GPL(slim_port_get_xfer_status);
1410
1411static void slim_add_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1412{
1413 struct slim_ich **arr;
1414 int i, j;
1415 int *len;
1416 int sl = slc->seglen << slc->rootexp;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001417 /* Channel is already active and other end is transmitting data */
1418 if (slc->state >= SLIM_CH_ACTIVE)
1419 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 if (slc->coeff == SLIM_COEFF_1) {
1421 arr = ctrl->sched.chc1;
1422 len = &ctrl->sched.num_cc1;
1423 } else {
1424 arr = ctrl->sched.chc3;
1425 len = &ctrl->sched.num_cc3;
1426 sl *= 3;
1427 }
1428
1429 *len += 1;
1430
1431 /* Insert the channel based on rootexp and seglen */
1432 for (i = 0; i < *len - 1; i++) {
1433 /*
1434 * Primary key: exp low to high.
1435 * Secondary key: seglen: high to low
1436 */
1437 if ((slc->rootexp > arr[i]->rootexp) ||
1438 ((slc->rootexp == arr[i]->rootexp) &&
1439 (slc->seglen < arr[i]->seglen)))
1440 continue;
1441 else
1442 break;
1443 }
1444 for (j = *len - 1; j > i; j--)
1445 arr[j] = arr[j - 1];
1446 arr[i] = slc;
1447 ctrl->sched.usedslots += sl;
1448
1449 return;
1450}
1451
1452static int slim_remove_ch(struct slim_controller *ctrl, struct slim_ich *slc)
1453{
1454 struct slim_ich **arr;
1455 int i;
1456 u32 la, ph;
1457 int *len;
1458 if (slc->coeff == SLIM_COEFF_1) {
1459 arr = ctrl->sched.chc1;
1460 len = &ctrl->sched.num_cc1;
1461 } else {
1462 arr = ctrl->sched.chc3;
1463 len = &ctrl->sched.num_cc3;
1464 }
1465
1466 for (i = 0; i < *len; i++) {
1467 if (arr[i] == slc)
1468 break;
1469 }
1470 if (i >= *len)
1471 return -EXFULL;
1472 for (; i < *len - 1; i++)
1473 arr[i] = arr[i + 1];
1474 *len -= 1;
1475 arr[*len] = NULL;
1476
1477 slc->state = SLIM_CH_ALLOCATED;
1478 slc->newintr = 0;
1479 slc->newoff = 0;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001480 for (i = 0; i < slc->nsink; i++) {
1481 ph = slc->sinkh[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 la = SLIM_HDL_TO_LA(ph);
1483 /*
1484 * For ports managed by manager's ported device, no need to send
1485 * disconnect. It is client's responsibility to call disconnect
1486 * on ports owned by the slave device
1487 */
1488 if (la == SLIM_LA_MANAGER)
1489 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1490 }
1491
Sagar Dharia29f35f02011-10-01 20:37:50 -06001492 ph = slc->srch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493 la = SLIM_HDL_TO_LA(ph);
1494 if (la == SLIM_LA_MANAGER)
1495 ctrl->ports[SLIM_HDL_TO_PORT(ph)].state = SLIM_P_UNCFG;
1496
Sagar Dharia29f35f02011-10-01 20:37:50 -06001497 kfree(slc->sinkh);
1498 slc->sinkh = NULL;
1499 slc->srch = 0;
1500 slc->nsink = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001501 return 0;
1502}
1503
1504static u32 slim_calc_prrate(struct slim_controller *ctrl, struct slim_ch *prop)
1505{
1506 u32 rate = 0, rate4k = 0, rate11k = 0;
1507 u32 exp = 0;
1508 u32 pr = 0;
1509 bool exact = true;
1510 bool done = false;
1511 enum slim_ch_rate ratefam;
1512
1513 if (prop->prot >= SLIM_PUSH)
1514 return 0;
1515 if (prop->baser == SLIM_RATE_1HZ) {
1516 rate = prop->ratem / 4000;
1517 rate4k = rate;
1518 if (rate * 4000 == prop->ratem)
1519 ratefam = SLIM_RATE_4000HZ;
1520 else {
1521 rate = prop->ratem / 11025;
1522 rate11k = rate;
1523 if (rate * 11025 == prop->ratem)
1524 ratefam = SLIM_RATE_11025HZ;
1525 else
1526 ratefam = SLIM_RATE_1HZ;
1527 }
1528 } else {
1529 ratefam = prop->baser;
1530 rate = prop->ratem;
1531 }
1532 if (ratefam == SLIM_RATE_1HZ) {
1533 exact = false;
1534 if ((rate4k + 1) * 4000 < (rate11k + 1) * 11025) {
1535 rate = rate4k + 1;
1536 ratefam = SLIM_RATE_4000HZ;
1537 } else {
1538 rate = rate11k + 1;
1539 ratefam = SLIM_RATE_11025HZ;
1540 }
1541 }
1542 /* covert rate to coeff-exp */
1543 while (!done) {
1544 while ((rate & 0x1) != 0x1) {
1545 rate >>= 1;
1546 exp++;
1547 }
1548 if (rate > 3) {
1549 /* roundup if not exact */
1550 rate++;
1551 exact = false;
1552 } else
1553 done = true;
1554 }
1555 if (ratefam == SLIM_RATE_4000HZ) {
1556 if (rate == 1)
1557 pr = 0x10;
1558 else {
1559 pr = 0;
1560 exp++;
1561 }
1562 } else {
1563 pr = 8;
1564 exp++;
1565 }
1566 if (exp <= 7) {
1567 pr |= exp;
1568 if (exact)
1569 pr |= 0x80;
1570 } else
1571 pr = 0;
1572 return pr;
1573}
1574
1575static int slim_nextdefine_ch(struct slim_device *sb, u8 chan)
1576{
1577 struct slim_controller *ctrl = sb->ctrl;
1578 u32 chrate = 0;
1579 u32 exp = 0;
1580 u32 coeff = 0;
1581 bool exact = true;
1582 bool done = false;
1583 int ret = 0;
1584 struct slim_ich *slc = &ctrl->chans[chan];
1585 struct slim_ch *prop = &slc->prop;
1586
1587 slc->prrate = slim_calc_prrate(ctrl, prop);
1588 dev_dbg(&ctrl->dev, "ch:%d, chan PR rate:%x\n", chan, slc->prrate);
1589 if (prop->baser == SLIM_RATE_4000HZ)
1590 chrate = 4000 * prop->ratem;
1591 else if (prop->baser == SLIM_RATE_11025HZ)
1592 chrate = 11025 * prop->ratem;
1593 else
1594 chrate = prop->ratem;
1595 /* max allowed sample freq = 768 seg/frame */
1596 if (chrate > 3600000)
1597 return -EDQUOT;
1598 if (prop->baser == SLIM_RATE_4000HZ &&
1599 ctrl->a_framer->superfreq == 4000)
1600 coeff = prop->ratem;
1601 else if (prop->baser == SLIM_RATE_11025HZ &&
1602 ctrl->a_framer->superfreq == 3675)
1603 coeff = 3 * prop->ratem;
1604 else {
1605 u32 tempr = 0;
1606 tempr = chrate * SLIM_CL_PER_SUPERFRAME_DIV8;
1607 coeff = tempr / ctrl->a_framer->rootfreq;
1608 if (coeff * ctrl->a_framer->rootfreq != tempr) {
1609 coeff++;
1610 exact = false;
1611 }
1612 }
1613
1614 /* convert coeff to coeff-exponent */
1615 exp = 0;
1616 while (!done) {
1617 while ((coeff & 0x1) != 0x1) {
1618 coeff >>= 1;
1619 exp++;
1620 }
1621 if (coeff > 3) {
1622 coeff++;
1623 exact = false;
1624 } else
1625 done = true;
1626 }
1627 if (prop->prot == SLIM_HARD_ISO && !exact)
1628 return -EPROTONOSUPPORT;
1629 else if (prop->prot == SLIM_AUTO_ISO) {
1630 if (exact)
1631 prop->prot = SLIM_HARD_ISO;
1632 else {
1633 /* Push-Pull not supported for now */
1634 return -EPROTONOSUPPORT;
1635 }
1636 }
1637 slc->rootexp = exp;
1638 slc->seglen = prop->sampleszbits/SLIM_CL_PER_SL;
1639 if (prop->prot != SLIM_HARD_ISO)
1640 slc->seglen++;
1641 if (prop->prot >= SLIM_EXT_SMPLX)
1642 slc->seglen++;
1643 /* convert coeff to enum */
1644 if (coeff == 1) {
1645 if (exp > 9)
1646 ret = -EIO;
1647 coeff = SLIM_COEFF_1;
1648 } else {
1649 if (exp > 8)
1650 ret = -EIO;
1651 coeff = SLIM_COEFF_3;
1652 }
1653 slc->coeff = coeff;
1654
1655 return ret;
1656}
1657
1658/*
1659 * slim_alloc_ch: Allocate a slimbus channel and return its handle.
1660 * @sb: client handle.
1661 * @chanh: return channel handle
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001662 * Slimbus channels are limited to 256 per specification.
1663 * -EXFULL is returned if all channels are in use.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664 * Although slimbus specification supports 256 channels, a controller may not
1665 * support that many channels.
1666 */
1667int slim_alloc_ch(struct slim_device *sb, u16 *chanh)
1668{
1669 struct slim_controller *ctrl = sb->ctrl;
1670 u16 i;
1671
1672 if (!ctrl)
1673 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001674 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 for (i = 0; i < ctrl->nchans; i++) {
1676 if (ctrl->chans[i].state == SLIM_CH_FREE)
1677 break;
1678 }
1679 if (i >= ctrl->nchans) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001680 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001681 return -EXFULL;
1682 }
1683 *chanh = i;
1684 ctrl->chans[i].nextgrp = 0;
1685 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001686 ctrl->chans[i].chan = (u8)(ctrl->reserved + i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001687
Sagar Dhariad2959352012-12-01 15:43:01 -07001688 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689 return 0;
1690}
1691EXPORT_SYMBOL_GPL(slim_alloc_ch);
1692
1693/*
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001694 * slim_query_ch: Get reference-counted handle for a channel number. Every
1695 * channel is reference counted by upto one as producer and the others as
1696 * consumer)
1697 * @sb: client handle
1698 * @chan: slimbus channel number
1699 * @chanh: return channel handle
1700 * If request channel number is not in use, it is allocated, and reference
1701 * count is set to one. If the channel was was already allocated, this API
1702 * will return handle to that channel and reference count is incremented.
1703 * -EXFULL is returned if all channels are in use
1704 */
1705int slim_query_ch(struct slim_device *sb, u8 ch, u16 *chanh)
1706{
1707 struct slim_controller *ctrl = sb->ctrl;
1708 u16 i, j;
1709 int ret = 0;
1710 if (!ctrl || !chanh)
1711 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001712 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001713 /* start with modulo number */
1714 i = ch % ctrl->nchans;
1715
1716 for (j = 0; j < ctrl->nchans; j++) {
1717 if (ctrl->chans[i].chan == ch) {
1718 *chanh = i;
1719 ctrl->chans[i].ref++;
1720 if (ctrl->chans[i].state == SLIM_CH_FREE)
1721 ctrl->chans[i].state = SLIM_CH_ALLOCATED;
1722 goto query_out;
1723 }
1724 i = (i + 1) % ctrl->nchans;
1725 }
1726
1727 /* Channel not in table yet */
1728 ret = -EXFULL;
1729 for (j = 0; j < ctrl->nchans; j++) {
1730 if (ctrl->chans[i].state == SLIM_CH_FREE) {
1731 ctrl->chans[i].state =
1732 SLIM_CH_ALLOCATED;
1733 *chanh = i;
1734 ctrl->chans[i].ref++;
1735 ctrl->chans[i].chan = ch;
1736 ctrl->chans[i].nextgrp = 0;
1737 ret = 0;
1738 break;
1739 }
1740 i = (i + 1) % ctrl->nchans;
1741 }
1742query_out:
Sagar Dhariad2959352012-12-01 15:43:01 -07001743 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001744 dev_dbg(&ctrl->dev, "query ch:%d,hdl:%d,ref:%d,ret:%d",
1745 ch, i, ctrl->chans[i].ref, ret);
1746 return ret;
1747}
1748EXPORT_SYMBOL_GPL(slim_query_ch);
1749
1750/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751 * slim_dealloc_ch: Deallocate channel allocated using the API above
1752 * -EISCONN is returned if the channel is tried to be deallocated without
1753 * being removed first.
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001754 * -ENOTCONN is returned if deallocation is tried on a channel that's not
1755 * allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001756 */
1757int slim_dealloc_ch(struct slim_device *sb, u16 chanh)
1758{
1759 struct slim_controller *ctrl = sb->ctrl;
Sagar Dharia29f35f02011-10-01 20:37:50 -06001760 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001761 struct slim_ich *slc = &ctrl->chans[chan];
1762 if (!ctrl)
1763 return -EINVAL;
1764
Sagar Dhariad2959352012-12-01 15:43:01 -07001765 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001766 if (slc->state == SLIM_CH_FREE) {
Sagar Dhariad2959352012-12-01 15:43:01 -07001767 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001768 return -ENOTCONN;
1769 }
1770 if (slc->ref > 1) {
1771 slc->ref--;
Sagar Dhariad2959352012-12-01 15:43:01 -07001772 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001773 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1774 slc->chan, chanh, slc->ref);
1775 return 0;
1776 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 if (slc->state >= SLIM_CH_PENDING_ACTIVE) {
1778 dev_err(&ctrl->dev, "Channel:%d should be removed first", chan);
Sagar Dhariad2959352012-12-01 15:43:01 -07001779 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001780 return -EISCONN;
1781 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001782 slc->ref--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001783 slc->state = SLIM_CH_FREE;
Sagar Dhariad2959352012-12-01 15:43:01 -07001784 mutex_unlock(&ctrl->sched.m_reconf);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001785 dev_dbg(&ctrl->dev, "remove chan:%d,hdl:%d,ref:%d",
1786 slc->chan, chanh, slc->ref);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 return 0;
1788}
1789EXPORT_SYMBOL_GPL(slim_dealloc_ch);
1790
1791/*
1792 * slim_get_ch_state: Channel state.
1793 * This API returns the channel's state (active, suspended, inactive etc)
1794 */
1795enum slim_ch_state slim_get_ch_state(struct slim_device *sb, u16 chanh)
1796{
Sagar Dharia29f35f02011-10-01 20:37:50 -06001797 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798 struct slim_ich *slc = &sb->ctrl->chans[chan];
1799 return slc->state;
1800}
1801EXPORT_SYMBOL_GPL(slim_get_ch_state);
1802
1803/*
1804 * slim_define_ch: Define a channel.This API defines channel parameters for a
1805 * given channel.
1806 * @sb: client handle.
1807 * @prop: slim_ch structure with channel parameters desired to be used.
1808 * @chanh: list of channels to be defined.
1809 * @nchan: number of channels in a group (1 if grp is false)
1810 * @grp: Are the channels grouped
1811 * @grph: return group handle if grouping of channels is desired.
1812 * Channels can be grouped if multiple channels use same parameters
1813 * (e.g. 5.1 audio has 6 channels with same parameters. They will all be grouped
1814 * and given 1 handle for simplicity and avoid repeatedly calling the API)
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001815 * -EISCONN is returned if channel is already used with different parameters.
1816 * -ENXIO is returned if the channel is not yet allocated.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001817 */
1818int slim_define_ch(struct slim_device *sb, struct slim_ch *prop, u16 *chanh,
1819 u8 nchan, bool grp, u16 *grph)
1820{
1821 struct slim_controller *ctrl = sb->ctrl;
1822 int i, ret = 0;
1823
1824 if (!ctrl || !chanh || !prop || !nchan)
1825 return -EINVAL;
Sagar Dhariad2959352012-12-01 15:43:01 -07001826 mutex_lock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001828 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001829 struct slim_ich *slc = &ctrl->chans[chan];
1830 dev_dbg(&ctrl->dev, "define_ch: ch:%d, state:%d", chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001831 (int)ctrl->chans[chan].state);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001832 if (slc->state < SLIM_CH_ALLOCATED) {
1833 ret = -ENXIO;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001834 goto err_define_ch;
1835 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001836 if (slc->state >= SLIM_CH_DEFINED && slc->ref >= 2) {
1837 if (prop->ratem != slc->prop.ratem ||
1838 prop->sampleszbits != slc->prop.sampleszbits ||
1839 prop->baser != slc->prop.baser) {
1840 ret = -EISCONN;
1841 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 }
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001843 } else if (slc->state > SLIM_CH_DEFINED) {
1844 ret = -EISCONN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845 goto err_define_ch;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001846 } else {
1847 ctrl->chans[chan].prop = *prop;
1848 ret = slim_nextdefine_ch(sb, chan);
1849 if (ret)
1850 goto err_define_ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001851 }
1852 if (i < (nchan - 1))
1853 ctrl->chans[chan].nextgrp = chanh[i + 1];
1854 if (i == 0)
1855 ctrl->chans[chan].nextgrp |= SLIM_START_GRP;
1856 if (i == (nchan - 1))
1857 ctrl->chans[chan].nextgrp |= SLIM_END_GRP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 }
1859
1860 if (grp)
Sagar Dhariab886e042012-10-17 22:41:57 -06001861 *grph = ((nchan << 8) | SLIM_HDL_TO_CHIDX(chanh[0]));
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001862 for (i = 0; i < nchan; i++) {
Sagar Dharia29f35f02011-10-01 20:37:50 -06001863 u8 chan = SLIM_HDL_TO_CHIDX(chanh[i]);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001864 struct slim_ich *slc = &ctrl->chans[chan];
1865 if (slc->state == SLIM_CH_ALLOCATED)
1866 slc->state = SLIM_CH_DEFINED;
1867 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868err_define_ch:
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06001869 dev_dbg(&ctrl->dev, "define_ch: ch:%d, ret:%d", *chanh, ret);
Sagar Dhariad2959352012-12-01 15:43:01 -07001870 mutex_unlock(&ctrl->sched.m_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871 return ret;
1872}
1873EXPORT_SYMBOL_GPL(slim_define_ch);
1874
1875static u32 getsubfrmcoding(u32 *ctrlw, u32 *subfrml, u32 *msgsl)
1876{
1877 u32 code = 0;
1878 if (*ctrlw == *subfrml) {
1879 *ctrlw = 8;
1880 *subfrml = 8;
1881 *msgsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME
1882 - SLIM_GDE_SLOTS_PER_SUPERFRAME;
1883 return 0;
1884 }
1885 if (*subfrml == 6) {
1886 code = 0;
1887 *msgsl = 256;
1888 } else if (*subfrml == 8) {
1889 code = 1;
1890 *msgsl = 192;
1891 } else if (*subfrml == 24) {
1892 code = 2;
1893 *msgsl = 64;
1894 } else { /* 32 */
1895 code = 3;
1896 *msgsl = 48;
1897 }
1898
1899 if (*ctrlw < 8) {
1900 if (*ctrlw >= 6) {
1901 *ctrlw = 6;
1902 code |= 0x14;
1903 } else {
1904 if (*ctrlw == 5)
1905 *ctrlw = 4;
1906 code |= (*ctrlw << 2);
1907 }
1908 } else {
1909 code -= 2;
1910 if (*ctrlw >= 24) {
1911 *ctrlw = 24;
1912 code |= 0x1e;
1913 } else if (*ctrlw >= 16) {
1914 *ctrlw = 16;
1915 code |= 0x1c;
1916 } else if (*ctrlw >= 12) {
1917 *ctrlw = 12;
1918 code |= 0x1a;
1919 } else {
1920 *ctrlw = 8;
1921 code |= 0x18;
1922 }
1923 }
1924
1925 *msgsl = (*msgsl * *ctrlw) - SLIM_FRM_SLOTS_PER_SUPERFRAME -
1926 SLIM_GDE_SLOTS_PER_SUPERFRAME;
1927 return code;
1928}
1929
1930static void shiftsegoffsets(struct slim_controller *ctrl, struct slim_ich **ach,
1931 int sz, u32 shft)
1932{
1933 int i;
1934 u32 oldoff;
1935 for (i = 0; i < sz; i++) {
1936 struct slim_ich *slc;
1937 if (ach[i] == NULL)
1938 continue;
1939 slc = ach[i];
1940 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1941 continue;
1942 oldoff = slc->newoff;
1943 slc->newoff += shft;
1944 /* seg. offset must be <= interval */
1945 if (slc->newoff >= slc->newintr)
1946 slc->newoff -= slc->newintr;
1947 }
1948}
1949
1950static int slim_sched_chans(struct slim_device *sb, u32 clkgear,
1951 u32 *ctrlw, u32 *subfrml)
1952{
1953 int coeff1, coeff3;
1954 enum slim_ch_coeff bias;
1955 struct slim_controller *ctrl = sb->ctrl;
1956 int last1 = ctrl->sched.num_cc1 - 1;
1957 int last3 = ctrl->sched.num_cc3 - 1;
1958
1959 /*
1960 * Find first channels with coeff 1 & 3 as starting points for
1961 * scheduling
1962 */
1963 for (coeff3 = 0; coeff3 < ctrl->sched.num_cc3; coeff3++) {
1964 struct slim_ich *slc = ctrl->sched.chc3[coeff3];
1965 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1966 continue;
1967 else
1968 break;
1969 }
1970 for (coeff1 = 0; coeff1 < ctrl->sched.num_cc1; coeff1++) {
1971 struct slim_ich *slc = ctrl->sched.chc1[coeff1];
1972 if (slc->state == SLIM_CH_PENDING_REMOVAL)
1973 continue;
1974 else
1975 break;
1976 }
1977 if (coeff3 == ctrl->sched.num_cc3 && coeff1 == ctrl->sched.num_cc1) {
1978 *ctrlw = 8;
1979 *subfrml = 8;
1980 return 0;
1981 } else if (coeff3 == ctrl->sched.num_cc3)
1982 bias = SLIM_COEFF_1;
1983 else
1984 bias = SLIM_COEFF_3;
1985
1986 /*
1987 * Find last chan in coeff1, 3 list, we will use to know when we
1988 * have done scheduling all coeff1 channels
1989 */
1990 while (last1 >= 0) {
1991 if (ctrl->sched.chc1[last1] != NULL &&
1992 (ctrl->sched.chc1[last1])->state !=
1993 SLIM_CH_PENDING_REMOVAL)
1994 break;
1995 last1--;
1996 }
1997 while (last3 >= 0) {
1998 if (ctrl->sched.chc3[last3] != NULL &&
1999 (ctrl->sched.chc3[last3])->state !=
2000 SLIM_CH_PENDING_REMOVAL)
2001 break;
2002 last3--;
2003 }
2004
2005 if (bias == SLIM_COEFF_1) {
2006 struct slim_ich *slc1 = ctrl->sched.chc1[coeff1];
2007 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2008 int curexp, finalexp;
2009 u32 curintr, curmaxsl;
2010 int opensl1[2];
2011 int maxctrlw1;
2012
2013 finalexp = (ctrl->sched.chc1[last1])->rootexp;
2014 curexp = (int)expshft - 1;
2015
2016 curintr = (SLIM_MAX_INTR_COEFF_1 * 2) >> (curexp + 1);
2017 curmaxsl = curintr >> 1;
2018 opensl1[0] = opensl1[1] = curmaxsl;
2019
2020 while ((coeff1 < ctrl->sched.num_cc1) || (curintr > 24)) {
2021 curintr >>= 1;
2022 curmaxsl >>= 1;
2023
2024 /* update 4K family open slot records */
2025 if (opensl1[1] < opensl1[0])
2026 opensl1[1] -= curmaxsl;
2027 else
2028 opensl1[1] = opensl1[0] - curmaxsl;
2029 opensl1[0] = curmaxsl;
2030 if (opensl1[1] < 0) {
2031 opensl1[0] += opensl1[1];
2032 opensl1[1] = 0;
2033 }
2034 if (opensl1[0] <= 0) {
2035 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2036 __LINE__);
2037 return -EXFULL;
2038 }
2039 curexp++;
2040 /* schedule 4k family channels */
2041
2042 while ((coeff1 < ctrl->sched.num_cc1) && (curexp ==
2043 (int)(slc1->rootexp + expshft))) {
2044 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2045 coeff1++;
2046 slc1 = ctrl->sched.chc1[coeff1];
2047 continue;
2048 }
2049 if (opensl1[1] >= opensl1[0] ||
2050 (finalexp == (int)slc1->rootexp &&
2051 curintr <= 24 &&
2052 opensl1[0] == curmaxsl)) {
2053 opensl1[1] -= slc1->seglen;
2054 slc1->newoff = curmaxsl + opensl1[1];
2055 if (opensl1[1] < 0 &&
2056 opensl1[0] == curmaxsl) {
2057 opensl1[0] += opensl1[1];
2058 opensl1[1] = 0;
2059 if (opensl1[0] < 0) {
2060 dev_dbg(&ctrl->dev,
2061 "reconfig failed:%d\n",
2062 __LINE__);
2063 return -EXFULL;
2064 }
2065 }
2066 } else {
2067 if (slc1->seglen > opensl1[0]) {
2068 dev_dbg(&ctrl->dev,
2069 "reconfig failed:%d\n",
2070 __LINE__);
2071 return -EXFULL;
2072 }
2073 slc1->newoff = opensl1[0] -
2074 slc1->seglen;
2075 opensl1[0] = slc1->newoff;
2076 }
2077 slc1->newintr = curintr;
2078 coeff1++;
2079 slc1 = ctrl->sched.chc1[coeff1];
2080 }
2081 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002082 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002083 if (opensl1[1] <= 0 && opensl1[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002084 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 if (opensl1[1] > opensl1[0]) {
2086 int temp = opensl1[0];
2087 opensl1[0] = opensl1[1];
2088 opensl1[1] = temp;
2089 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2090 ctrl->sched.num_cc1, curmaxsl);
2091 }
2092 /* choose subframe mode to maximize bw */
2093 maxctrlw1 = opensl1[0];
2094 if (opensl1[0] == curmaxsl)
2095 maxctrlw1 += opensl1[1];
2096 if (curintr >= 24) {
2097 *subfrml = 24;
2098 *ctrlw = maxctrlw1;
2099 } else if (curintr == 12) {
2100 if (maxctrlw1 > opensl1[1] * 4) {
2101 *subfrml = 24;
2102 *ctrlw = maxctrlw1;
2103 } else {
2104 *subfrml = 6;
2105 *ctrlw = opensl1[1];
2106 }
2107 } else {
2108 *subfrml = 6;
2109 *ctrlw = maxctrlw1;
2110 }
2111 } else {
Jordan Crouse9bb8aca2011-11-23 11:41:20 -07002112 struct slim_ich *slc1 = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002113 struct slim_ich *slc3 = ctrl->sched.chc3[coeff3];
2114 u32 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2115 int curexp, finalexp, exp1;
2116 u32 curintr, curmaxsl;
2117 int opensl3[2];
2118 int opensl1[6];
2119 bool opensl1valid = false;
2120 int maxctrlw1, maxctrlw3, i;
2121 finalexp = (ctrl->sched.chc3[last3])->rootexp;
2122 if (last1 >= 0) {
2123 slc1 = ctrl->sched.chc1[coeff1];
2124 exp1 = (ctrl->sched.chc1[last1])->rootexp;
2125 if (exp1 > finalexp)
2126 finalexp = exp1;
2127 }
2128 curexp = (int)expshft - 1;
2129
2130 curintr = (SLIM_MAX_INTR_COEFF_3 * 2) >> (curexp + 1);
2131 curmaxsl = curintr >> 1;
2132 opensl3[0] = opensl3[1] = curmaxsl;
2133
2134 while (coeff1 < ctrl->sched.num_cc1 ||
2135 coeff3 < ctrl->sched.num_cc3 ||
2136 curintr > 32) {
2137 curintr >>= 1;
2138 curmaxsl >>= 1;
2139
2140 /* update 12k family open slot records */
2141 if (opensl3[1] < opensl3[0])
2142 opensl3[1] -= curmaxsl;
2143 else
2144 opensl3[1] = opensl3[0] - curmaxsl;
2145 opensl3[0] = curmaxsl;
2146 if (opensl3[1] < 0) {
2147 opensl3[0] += opensl3[1];
2148 opensl3[1] = 0;
2149 }
2150 if (opensl3[0] <= 0) {
2151 dev_dbg(&ctrl->dev, "reconfig failed:%d\n",
2152 __LINE__);
2153 return -EXFULL;
2154 }
2155 curexp++;
2156
2157 /* schedule 12k family channels */
2158 while (coeff3 < ctrl->sched.num_cc3 &&
2159 curexp == (int)slc3->rootexp + expshft) {
2160 if (slc3->state == SLIM_CH_PENDING_REMOVAL) {
2161 coeff3++;
2162 slc3 = ctrl->sched.chc3[coeff3];
2163 continue;
2164 }
2165 opensl1valid = false;
2166 if (opensl3[1] >= opensl3[0] ||
2167 (finalexp == (int)slc3->rootexp &&
2168 curintr <= 32 &&
2169 opensl3[0] == curmaxsl &&
2170 last1 < 0)) {
2171 opensl3[1] -= slc3->seglen;
2172 slc3->newoff = curmaxsl + opensl3[1];
2173 if (opensl3[1] < 0 &&
2174 opensl3[0] == curmaxsl) {
2175 opensl3[0] += opensl3[1];
2176 opensl3[1] = 0;
2177 }
2178 if (opensl3[0] < 0) {
2179 dev_dbg(&ctrl->dev,
2180 "reconfig failed:%d\n",
2181 __LINE__);
2182 return -EXFULL;
2183 }
2184 } else {
2185 if (slc3->seglen > opensl3[0]) {
2186 dev_dbg(&ctrl->dev,
2187 "reconfig failed:%d\n",
2188 __LINE__);
2189 return -EXFULL;
2190 }
2191 slc3->newoff = opensl3[0] -
2192 slc3->seglen;
2193 opensl3[0] = slc3->newoff;
2194 }
2195 slc3->newintr = curintr;
2196 coeff3++;
2197 slc3 = ctrl->sched.chc3[coeff3];
2198 }
2199 /* update 4k openslot records */
2200 if (opensl1valid == false) {
2201 for (i = 0; i < 3; i++) {
2202 opensl1[i * 2] = opensl3[0];
2203 opensl1[(i * 2) + 1] = opensl3[1];
2204 }
2205 } else {
2206 int opensl1p[6];
2207 memcpy(opensl1p, opensl1, sizeof(opensl1));
2208 for (i = 0; i < 3; i++) {
2209 if (opensl1p[i] < opensl1p[i + 3])
2210 opensl1[(i * 2) + 1] =
2211 opensl1p[i];
2212 else
2213 opensl1[(i * 2) + 1] =
2214 opensl1p[i + 3];
2215 }
2216 for (i = 0; i < 3; i++) {
2217 opensl1[(i * 2) + 1] -= curmaxsl;
2218 opensl1[i * 2] = curmaxsl;
2219 if (opensl1[(i * 2) + 1] < 0) {
2220 opensl1[i * 2] +=
2221 opensl1[(i * 2) + 1];
2222 opensl1[(i * 2) + 1] = 0;
2223 }
2224 if (opensl1[i * 2] < 0) {
2225 dev_dbg(&ctrl->dev,
2226 "reconfig failed:%d\n",
2227 __LINE__);
2228 return -EXFULL;
2229 }
2230 }
2231 }
2232 /* schedule 4k family channels */
2233 while (coeff1 < ctrl->sched.num_cc1 &&
2234 curexp == (int)slc1->rootexp + expshft) {
2235 /* searchorder effective when opensl valid */
2236 static const int srcho[] = { 5, 2, 4, 1, 3, 0 };
2237 int maxopensl = 0;
2238 int maxi = 0;
2239 if (slc1->state == SLIM_CH_PENDING_REMOVAL) {
2240 coeff1++;
2241 slc1 = ctrl->sched.chc1[coeff1];
2242 continue;
2243 }
2244 opensl1valid = true;
2245 for (i = 0; i < 6; i++) {
2246 if (opensl1[srcho[i]] > maxopensl) {
2247 maxopensl = opensl1[srcho[i]];
2248 maxi = srcho[i];
2249 }
2250 }
2251 opensl1[maxi] -= slc1->seglen;
2252 slc1->newoff = (curmaxsl * maxi) +
2253 opensl1[maxi];
2254 if (opensl1[maxi] < 0) {
2255 if (((maxi & 1) == 1) &&
2256 (opensl1[maxi - 1] == curmaxsl)) {
2257 opensl1[maxi - 1] +=
2258 opensl1[maxi];
2259 if (opensl3[0] >
2260 opensl1[maxi - 1])
2261 opensl3[0] =
2262 opensl1[maxi - 1];
2263 opensl3[1] = 0;
2264 opensl1[maxi] = 0;
2265 if (opensl1[maxi - 1] < 0) {
2266 dev_dbg(&ctrl->dev,
2267 "reconfig failed:%d\n",
2268 __LINE__);
2269 return -EXFULL;
2270 }
2271 } else {
2272 dev_dbg(&ctrl->dev,
2273 "reconfig failed:%d\n",
2274 __LINE__);
2275 return -EXFULL;
2276 }
2277 } else {
2278 if (opensl3[maxi & 1] > opensl1[maxi])
2279 opensl3[maxi & 1] =
2280 opensl1[maxi];
2281 }
2282 slc1->newintr = curintr * 3;
2283 coeff1++;
2284 slc1 = ctrl->sched.chc1[coeff1];
2285 }
2286 }
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002287 /* Leave some slots for messaging space */
Sagar Dharia90a06cc2012-06-25 12:44:02 -06002288 if (opensl3[1] <= 0 && opensl3[0] <= 0)
Sagar Dhariaa00f61f2012-04-21 15:10:08 -06002289 return -EXFULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290 /* swap 1st and 2nd bucket if 2nd bucket has more open slots */
2291 if (opensl3[1] > opensl3[0]) {
2292 int temp = opensl3[0];
2293 opensl3[0] = opensl3[1];
2294 opensl3[1] = temp;
2295 temp = opensl1[5];
2296 opensl1[5] = opensl1[4];
2297 opensl1[4] = opensl1[3];
2298 opensl1[3] = opensl1[2];
2299 opensl1[2] = opensl1[1];
2300 opensl1[1] = opensl1[0];
2301 opensl1[0] = temp;
2302 shiftsegoffsets(ctrl, ctrl->sched.chc1,
2303 ctrl->sched.num_cc1, curmaxsl);
2304 shiftsegoffsets(ctrl, ctrl->sched.chc3,
2305 ctrl->sched.num_cc3, curmaxsl);
2306 }
2307 /* subframe mode to maximize BW */
2308 maxctrlw3 = opensl3[0];
2309 maxctrlw1 = opensl1[0];
2310 if (opensl3[0] == curmaxsl)
2311 maxctrlw3 += opensl3[1];
2312 for (i = 0; i < 5 && opensl1[i] == curmaxsl; i++)
2313 maxctrlw1 += opensl1[i + 1];
2314 if (curintr >= 32) {
2315 *subfrml = 32;
2316 *ctrlw = maxctrlw3;
2317 } else if (curintr == 16) {
2318 if (maxctrlw3 > (opensl3[1] * 4)) {
2319 *subfrml = 32;
2320 *ctrlw = maxctrlw3;
2321 } else {
2322 *subfrml = 8;
2323 *ctrlw = opensl3[1];
2324 }
2325 } else {
2326 if ((maxctrlw1 * 8) >= (maxctrlw3 * 24)) {
2327 *subfrml = 24;
2328 *ctrlw = maxctrlw1;
2329 } else {
2330 *subfrml = 8;
2331 *ctrlw = maxctrlw3;
2332 }
2333 }
2334 }
2335 return 0;
2336}
2337
2338#ifdef DEBUG
2339static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2340 u32 subfrml, u32 clkgear)
2341{
2342 int sl, i;
2343 int cc1 = 0;
2344 int cc3 = 0;
2345 struct slim_ich *slc = NULL;
2346 if (!ctrl->sched.slots)
2347 return 0;
2348 memset(ctrl->sched.slots, 0, SLIM_SL_PER_SUPERFRAME);
2349 dev_dbg(&ctrl->dev, "Clock gear is:%d\n", clkgear);
2350 for (sl = 0; sl < SLIM_SL_PER_SUPERFRAME; sl += subfrml) {
2351 for (i = 0; i < ctrlw; i++)
2352 ctrl->sched.slots[sl + i] = 33;
2353 }
2354 while (cc1 < ctrl->sched.num_cc1) {
2355 slc = ctrl->sched.chc1[cc1];
2356 if (slc == NULL) {
2357 dev_err(&ctrl->dev, "SLC1 null in verify: chan%d\n",
2358 cc1);
2359 return -EIO;
2360 }
2361 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2362 (slc - ctrl->chans), slc->newoff,
2363 slc->newintr, slc->seglen);
2364
2365 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2366 for (sl = slc->newoff;
2367 sl < SLIM_SL_PER_SUPERFRAME;
2368 sl += slc->newintr) {
2369 for (i = 0; i < slc->seglen; i++) {
2370 if (ctrl->sched.slots[sl + i])
2371 return -EXFULL;
2372 ctrl->sched.slots[sl + i] = cc1 + 1;
2373 }
2374 }
2375 }
2376 cc1++;
2377 }
2378 while (cc3 < ctrl->sched.num_cc3) {
2379 slc = ctrl->sched.chc3[cc3];
2380 if (slc == NULL) {
2381 dev_err(&ctrl->dev, "SLC3 null in verify: chan%d\n",
2382 cc3);
2383 return -EIO;
2384 }
2385 dev_dbg(&ctrl->dev, "chan:%d, offset:%d, intr:%d, seglen:%d\n",
2386 (slc - ctrl->chans), slc->newoff,
2387 slc->newintr, slc->seglen);
2388 if (slc->state != SLIM_CH_PENDING_REMOVAL) {
2389 for (sl = slc->newoff;
2390 sl < SLIM_SL_PER_SUPERFRAME;
2391 sl += slc->newintr) {
2392 for (i = 0; i < slc->seglen; i++) {
2393 if (ctrl->sched.slots[sl + i])
2394 return -EXFULL;
2395 ctrl->sched.slots[sl + i] = cc3 + 1;
2396 }
2397 }
2398 }
2399 cc3++;
2400 }
2401
2402 return 0;
2403}
2404#else
2405static int slim_verifychansched(struct slim_controller *ctrl, u32 ctrlw,
2406 u32 subfrml, u32 clkgear)
2407{
2408 return 0;
2409}
2410#endif
2411
2412static void slim_sort_chan_grp(struct slim_controller *ctrl,
2413 struct slim_ich *slc)
2414{
2415 u8 last = (u8)-1;
2416 u8 second = 0;
2417
2418 for (; last > 0; last--) {
2419 struct slim_ich *slc1 = slc;
2420 struct slim_ich *slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002421 u8 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 slc2 = &ctrl->chans[next];
2423 for (second = 1; second <= last && slc2 &&
2424 (slc2->state == SLIM_CH_ACTIVE ||
2425 slc2->state == SLIM_CH_PENDING_ACTIVE); second++) {
2426 if (slc1->newoff > slc2->newoff) {
2427 u32 temp = slc2->newoff;
2428 slc2->newoff = slc1->newoff;
2429 slc1->newoff = temp;
2430 }
2431 if (slc2->nextgrp & SLIM_END_GRP) {
2432 last = second;
2433 break;
2434 }
2435 slc1 = slc2;
Sagar Dharia29f35f02011-10-01 20:37:50 -06002436 next = SLIM_HDL_TO_CHIDX(slc1->nextgrp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002437 slc2 = &ctrl->chans[next];
2438 }
2439 if (slc2 == NULL)
2440 last = second - 1;
2441 }
2442}
2443
2444
2445static int slim_allocbw(struct slim_device *sb, int *subfrmc, int *clkgear)
2446{
2447 u32 msgsl = 0;
2448 u32 ctrlw = 0;
2449 u32 subfrml = 0;
2450 int ret = -EIO;
2451 struct slim_controller *ctrl = sb->ctrl;
2452 u32 usedsl = ctrl->sched.usedslots + ctrl->sched.pending_msgsl;
2453 u32 availsl = SLIM_SL_PER_SUPERFRAME - SLIM_FRM_SLOTS_PER_SUPERFRAME -
2454 SLIM_GDE_SLOTS_PER_SUPERFRAME;
2455 *clkgear = SLIM_MAX_CLK_GEAR;
2456
2457 dev_dbg(&ctrl->dev, "used sl:%u, availlable sl:%u\n", usedsl, availsl);
2458 dev_dbg(&ctrl->dev, "pending:chan sl:%u, :msg sl:%u, clkgear:%u\n",
2459 ctrl->sched.usedslots,
2460 ctrl->sched.pending_msgsl, *clkgear);
Sagar Dharia33f34442011-08-08 16:22:03 -06002461 /*
2462 * If number of slots are 0, that means channels are inactive.
2463 * It is very likely that the manager will call clock pause very soon.
2464 * By making sure that bus is in MAX_GEAR, clk pause sequence will take
2465 * minimum amount of time.
2466 */
2467 if (ctrl->sched.usedslots != 0) {
2468 while ((usedsl * 2 <= availsl) && (*clkgear > ctrl->min_cg)) {
2469 *clkgear -= 1;
2470 usedsl *= 2;
2471 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472 }
2473
2474 /*
2475 * Try scheduling data channels at current clock gear, if all channels
2476 * can be scheduled, or reserved BW can't be satisfied, increase clock
2477 * gear and try again
2478 */
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002479 for (; *clkgear <= ctrl->max_cg; (*clkgear)++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002480 ret = slim_sched_chans(sb, *clkgear, &ctrlw, &subfrml);
2481
2482 if (ret == 0) {
2483 *subfrmc = getsubfrmcoding(&ctrlw, &subfrml, &msgsl);
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002484 if ((msgsl >> (ctrl->max_cg - *clkgear) <
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002485 ctrl->sched.pending_msgsl) &&
Sagar Dharia98a7ecb2011-07-25 15:25:35 -06002486 (*clkgear < ctrl->max_cg))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487 continue;
2488 else
2489 break;
2490 }
2491 }
2492 if (ret == 0) {
2493 int i;
2494 /* Sort channel-groups */
2495 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2496 struct slim_ich *slc = ctrl->sched.chc1[i];
2497 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2498 continue;
2499 if ((slc->nextgrp & SLIM_START_GRP) &&
2500 !(slc->nextgrp & SLIM_END_GRP)) {
2501 slim_sort_chan_grp(ctrl, slc);
2502 }
2503 }
2504 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2505 struct slim_ich *slc = ctrl->sched.chc3[i];
2506 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2507 continue;
2508 if ((slc->nextgrp & SLIM_START_GRP) &&
2509 !(slc->nextgrp & SLIM_END_GRP)) {
2510 slim_sort_chan_grp(ctrl, slc);
2511 }
2512 }
2513
2514 ret = slim_verifychansched(ctrl, ctrlw, subfrml, *clkgear);
2515 }
2516
2517 return ret;
2518}
2519
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002520static void slim_change_existing_chans(struct slim_controller *ctrl, int coeff)
2521{
2522 struct slim_ich **arr;
2523 int len, i;
2524 if (coeff == SLIM_COEFF_1) {
2525 arr = ctrl->sched.chc1;
2526 len = ctrl->sched.num_cc1;
2527 } else {
2528 arr = ctrl->sched.chc3;
2529 len = ctrl->sched.num_cc3;
2530 }
2531 for (i = 0; i < len; i++) {
2532 struct slim_ich *slc = arr[i];
2533 if (slc->state == SLIM_CH_ACTIVE ||
2534 slc->state == SLIM_CH_SUSPENDED)
2535 slc->offset = slc->newoff;
2536 slc->interval = slc->newintr;
2537 }
2538}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539static void slim_chan_changes(struct slim_device *sb, bool revert)
2540{
2541 struct slim_controller *ctrl = sb->ctrl;
2542 while (!list_empty(&sb->mark_define)) {
2543 struct slim_ich *slc;
2544 struct slim_pending_ch *pch =
2545 list_entry(sb->mark_define.next,
2546 struct slim_pending_ch, pending);
2547 slc = &ctrl->chans[pch->chan];
2548 if (revert) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002549 if (slc->state == SLIM_CH_PENDING_ACTIVE) {
2550 u32 sl = slc->seglen << slc->rootexp;
2551 if (slc->coeff == SLIM_COEFF_3)
2552 sl *= 3;
2553 ctrl->sched.usedslots -= sl;
2554 slim_remove_ch(ctrl, slc);
2555 slc->state = SLIM_CH_DEFINED;
2556 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002557 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558 slc->state = SLIM_CH_ACTIVE;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002559 slc->def++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002560 }
2561 list_del_init(&pch->pending);
2562 kfree(pch);
2563 }
2564
2565 while (!list_empty(&sb->mark_removal)) {
2566 struct slim_pending_ch *pch =
2567 list_entry(sb->mark_removal.next,
2568 struct slim_pending_ch, pending);
2569 struct slim_ich *slc = &ctrl->chans[pch->chan];
2570 u32 sl = slc->seglen << slc->rootexp;
2571 if (revert) {
2572 if (slc->coeff == SLIM_COEFF_3)
2573 sl *= 3;
2574 ctrl->sched.usedslots += sl;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002575 slc->def = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 slc->state = SLIM_CH_ACTIVE;
2577 } else
2578 slim_remove_ch(ctrl, slc);
2579 list_del_init(&pch->pending);
2580 kfree(pch);
2581 }
2582
2583 while (!list_empty(&sb->mark_suspend)) {
2584 struct slim_pending_ch *pch =
2585 list_entry(sb->mark_suspend.next,
2586 struct slim_pending_ch, pending);
2587 struct slim_ich *slc = &ctrl->chans[pch->chan];
2588 if (revert)
2589 slc->state = SLIM_CH_ACTIVE;
2590 list_del_init(&pch->pending);
2591 kfree(pch);
2592 }
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002593 /* Change already active channel if reconfig succeeded */
2594 if (!revert) {
2595 slim_change_existing_chans(ctrl, SLIM_COEFF_1);
2596 slim_change_existing_chans(ctrl, SLIM_COEFF_3);
2597 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598}
2599
2600/*
2601 * slim_reconfigure_now: Request reconfiguration now.
2602 * @sb: client handle
2603 * This API does what commit flag in other scheduling APIs do.
2604 * -EXFULL is returned if there is no space in TDM to reserve the
2605 * bandwidth. -EBUSY is returned if reconfiguration request is already in
2606 * progress.
2607 */
2608int slim_reconfigure_now(struct slim_device *sb)
2609{
2610 u8 i;
2611 u8 wbuf[4];
2612 u32 clkgear, subframe;
2613 u32 curexp;
2614 int ret;
2615 struct slim_controller *ctrl = sb->ctrl;
2616 u32 expshft;
2617 u32 segdist;
2618 struct slim_pending_ch *pch;
2619
Sagar Dharia80a55e12012-08-16 16:43:58 -06002620 mutex_lock(&ctrl->sched.m_reconf);
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002621 /*
2622 * If there are no pending changes from this client, avoid sending
2623 * the reconfiguration sequence
2624 */
2625 if (sb->pending_msgsl == sb->cur_msgsl &&
2626 list_empty(&sb->mark_define) &&
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002627 list_empty(&sb->mark_suspend)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002628 struct list_head *pos, *next;
2629 list_for_each_safe(pos, next, &sb->mark_removal) {
2630 struct slim_ich *slc;
2631 pch = list_entry(pos, struct slim_pending_ch, pending);
2632 slc = &ctrl->chans[pch->chan];
2633 if (slc->def > 0)
2634 slc->def--;
2635 /* Disconnect source port to free it up */
2636 if (SLIM_HDL_TO_LA(slc->srch) == sb->laddr)
2637 slc->srch = 0;
2638 if (slc->def != 0) {
2639 list_del(&pch->pending);
2640 kfree(pch);
2641 }
2642 }
2643 if (list_empty(&sb->mark_removal)) {
Sagar Dharia80a55e12012-08-16 16:43:58 -06002644 mutex_unlock(&ctrl->sched.m_reconf);
2645 pr_info("SLIM_CL: skip reconfig sequence");
2646 return 0;
2647 }
Sagar Dharia6e728bd2012-07-26 16:56:44 -06002648 }
2649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002650 ctrl->sched.pending_msgsl += sb->pending_msgsl - sb->cur_msgsl;
2651 list_for_each_entry(pch, &sb->mark_define, pending) {
2652 struct slim_ich *slc = &ctrl->chans[pch->chan];
2653 slim_add_ch(ctrl, slc);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002654 if (slc->state < SLIM_CH_ACTIVE)
2655 slc->state = SLIM_CH_PENDING_ACTIVE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 }
2657
2658 list_for_each_entry(pch, &sb->mark_removal, pending) {
2659 struct slim_ich *slc = &ctrl->chans[pch->chan];
2660 u32 sl = slc->seglen << slc->rootexp;
2661 if (slc->coeff == SLIM_COEFF_3)
2662 sl *= 3;
2663 ctrl->sched.usedslots -= sl;
2664 slc->state = SLIM_CH_PENDING_REMOVAL;
2665 }
2666 list_for_each_entry(pch, &sb->mark_suspend, pending) {
2667 struct slim_ich *slc = &ctrl->chans[pch->chan];
2668 slc->state = SLIM_CH_SUSPENDED;
2669 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670
Sagar Dharia4aec9232012-07-24 23:44:26 -06002671 /*
2672 * Controller can override default channel scheduling algorithm.
2673 * (e.g. if controller needs to use fixed channel scheduling based
2674 * on number of channels)
2675 */
2676 if (ctrl->allocbw)
2677 ret = ctrl->allocbw(sb, &subframe, &clkgear);
2678 else
2679 ret = slim_allocbw(sb, &subframe, &clkgear);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680
2681 if (!ret) {
2682 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2683 SLIM_MSG_MC_BEGIN_RECONFIGURATION, 0, SLIM_MSG_MT_CORE,
2684 NULL, NULL, 0, 3, NULL, 0, NULL);
2685 dev_dbg(&ctrl->dev, "sending begin_reconfig:ret:%d\n", ret);
2686 }
2687
2688 if (!ret && subframe != ctrl->sched.subfrmcode) {
2689 wbuf[0] = (u8)(subframe & 0xFF);
2690 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2691 SLIM_MSG_MC_NEXT_SUBFRAME_MODE, 0, SLIM_MSG_MT_CORE,
2692 NULL, (u8 *)&subframe, 1, 4, NULL, 0, NULL);
2693 dev_dbg(&ctrl->dev, "sending subframe:%d,ret:%d\n",
2694 (int)wbuf[0], ret);
2695 }
2696 if (!ret && clkgear != ctrl->clkgear) {
2697 wbuf[0] = (u8)(clkgear & 0xFF);
2698 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2699 SLIM_MSG_MC_NEXT_CLOCK_GEAR, 0, SLIM_MSG_MT_CORE,
2700 NULL, wbuf, 1, 4, NULL, 0, NULL);
2701 dev_dbg(&ctrl->dev, "sending clkgear:%d,ret:%d\n",
2702 (int)wbuf[0], ret);
2703 }
2704 if (ret)
2705 goto revert_reconfig;
2706
2707 expshft = SLIM_MAX_CLK_GEAR - clkgear;
2708 /* activate/remove channel */
2709 list_for_each_entry(pch, &sb->mark_define, pending) {
2710 struct slim_ich *slc = &ctrl->chans[pch->chan];
2711 /* Define content */
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002712 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002713 wbuf[1] = slc->prrate;
2714 wbuf[2] = slc->prop.dataf | (slc->prop.auxf << 4);
2715 wbuf[3] = slc->prop.sampleszbits / SLIM_CL_PER_SL;
2716 dev_dbg(&ctrl->dev, "define content, activate:%x, %x, %x, %x\n",
2717 wbuf[0], wbuf[1], wbuf[2], wbuf[3]);
2718 /* Right now, channel link bit is not supported */
2719 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2720 SLIM_MSG_MC_NEXT_DEFINE_CONTENT, 0,
2721 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 4, 7,
2722 NULL, 0, NULL);
2723 if (ret)
2724 goto revert_reconfig;
2725
2726 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2727 SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL, 0,
2728 SLIM_MSG_MT_CORE, NULL, (u8 *)&wbuf, 1, 4,
2729 NULL, 0, NULL);
2730 if (ret)
2731 goto revert_reconfig;
2732 }
2733
2734 list_for_each_entry(pch, &sb->mark_removal, 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, "remove 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_REMOVE_CHANNEL, 0,
2740 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2741 NULL, 0, NULL);
2742 if (ret)
2743 goto revert_reconfig;
2744 }
2745 list_for_each_entry(pch, &sb->mark_suspend, pending) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002746 struct slim_ich *slc = &ctrl->chans[pch->chan];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002747 dev_dbg(&ctrl->dev, "suspend chan:%x\n", pch->chan);
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002748 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002749 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2750 SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL, 0,
2751 SLIM_MSG_MT_CORE, NULL, wbuf, 1, 4,
2752 NULL, 0, NULL);
2753 if (ret)
2754 goto revert_reconfig;
2755 }
2756
2757 /* Define CC1 channel */
2758 for (i = 0; i < ctrl->sched.num_cc1; i++) {
2759 struct slim_ich *slc = ctrl->sched.chc1[i];
2760 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2761 continue;
2762 curexp = slc->rootexp + expshft;
2763 segdist = (slc->newoff << curexp) & 0x1FF;
2764 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002765 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2766 slc->newintr, slc->interval, segdist);
2767 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2768 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002769
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002770 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002771 slc->newintr != slc->interval ||
2772 slc->newoff != slc->offset) {
2773 segdist |= 0x200;
2774 segdist >>= curexp;
2775 segdist |= (slc->newoff << (curexp + 1)) & 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002776 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002777 wbuf[1] = (u8)(segdist & 0xFF);
2778 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2779 (slc->prop.prot << 4);
2780 wbuf[3] = slc->seglen;
2781 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2782 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2783 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2784 7, NULL, 0, NULL);
2785 if (ret)
2786 goto revert_reconfig;
2787 }
2788 }
2789
2790 /* Define CC3 channels */
2791 for (i = 0; i < ctrl->sched.num_cc3; i++) {
2792 struct slim_ich *slc = ctrl->sched.chc3[i];
2793 if (slc->state == SLIM_CH_PENDING_REMOVAL)
2794 continue;
2795 curexp = slc->rootexp + expshft;
2796 segdist = (slc->newoff << curexp) & 0x1FF;
2797 expshft = SLIM_MAX_CLK_GEAR - clkgear;
Sagar Dhariaa0f6b672011-08-13 17:36:55 -06002798 dev_dbg(&ctrl->dev, "new-intr:%d, old-intr:%d, dist:%d\n",
2799 slc->newintr, slc->interval, segdist);
2800 dev_dbg(&ctrl->dev, "new-off:%d, old-off:%d\n",
2801 slc->newoff, slc->offset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002802
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002803 if (slc->state < SLIM_CH_ACTIVE || slc->def < slc->ref ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002804 slc->newintr != slc->interval ||
2805 slc->newoff != slc->offset) {
2806 segdist |= 0x200;
2807 segdist >>= curexp;
2808 segdist |= 0xC00;
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002809 wbuf[0] = slc->chan;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002810 wbuf[1] = (u8)(segdist & 0xFF);
2811 wbuf[2] = (u8)((segdist & 0xF00) >> 8) |
2812 (slc->prop.prot << 4);
2813 wbuf[3] = (u8)(slc->seglen);
2814 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2815 SLIM_MSG_MC_NEXT_DEFINE_CHANNEL, 0,
2816 SLIM_MSG_MT_CORE, NULL, (u8 *)wbuf, 4,
2817 7, NULL, 0, NULL);
2818 if (ret)
2819 goto revert_reconfig;
2820 }
2821 }
2822 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
2823 SLIM_MSG_MC_RECONFIGURE_NOW, 0, SLIM_MSG_MT_CORE, NULL,
2824 NULL, 0, 3, NULL, 0, NULL);
2825 dev_dbg(&ctrl->dev, "reconfig now:ret:%d\n", ret);
2826 if (!ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002827 ctrl->sched.subfrmcode = subframe;
2828 ctrl->clkgear = clkgear;
2829 ctrl->sched.msgsl = ctrl->sched.pending_msgsl;
2830 sb->cur_msgsl = sb->pending_msgsl;
2831 slim_chan_changes(sb, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002832 mutex_unlock(&ctrl->sched.m_reconf);
2833 return 0;
2834 }
2835
2836revert_reconfig:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002837 /* Revert channel changes */
2838 slim_chan_changes(sb, true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002839 mutex_unlock(&ctrl->sched.m_reconf);
2840 return ret;
2841}
2842EXPORT_SYMBOL_GPL(slim_reconfigure_now);
2843
2844static int add_pending_ch(struct list_head *listh, u8 chan)
2845{
2846 struct slim_pending_ch *pch;
2847 pch = kmalloc(sizeof(struct slim_pending_ch), GFP_KERNEL);
2848 if (!pch)
2849 return -ENOMEM;
2850 pch->chan = chan;
2851 list_add_tail(&pch->pending, listh);
2852 return 0;
2853}
2854
2855/*
2856 * slim_control_ch: Channel control API.
2857 * @sb: client handle
2858 * @chanh: group or channel handle to be controlled
2859 * @chctrl: Control command (activate/suspend/remove)
2860 * @commit: flag to indicate whether the control should take effect right-away.
2861 * This API activates, removes or suspends a channel (or group of channels)
2862 * chanh indicates the channel or group handle (returned by the define_ch API).
2863 * Reconfiguration may be time-consuming since it can change all other active
2864 * channel allocations on the bus, change in clock gear used by the slimbus,
2865 * and change in the control space width used for messaging.
2866 * commit makes sure that multiple channels can be activated/deactivated before
2867 * reconfiguration is started.
2868 * -EXFULL is returned if there is no space in TDM to reserve the bandwidth.
2869 * -EISCONN/-ENOTCONN is returned if the channel is already connected or not
2870 * yet defined.
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002871 * -EINVAL is returned if individual control of a grouped-channel is attempted.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002872 */
2873int slim_control_ch(struct slim_device *sb, u16 chanh,
2874 enum slim_ch_control chctrl, bool commit)
2875{
2876 struct slim_controller *ctrl = sb->ctrl;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002877 int ret = 0;
2878 /* Get rid of the group flag in MSB if any */
Sagar Dharia29f35f02011-10-01 20:37:50 -06002879 u8 chan = SLIM_HDL_TO_CHIDX(chanh);
Sagar Dhariab886e042012-10-17 22:41:57 -06002880 u8 nchan = 0;
Sagar Dharia2e7026a2012-02-21 17:48:14 -07002881 struct slim_ich *slc = &ctrl->chans[chan];
2882 if (!(slc->nextgrp & SLIM_START_GRP))
2883 return -EINVAL;
2884
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002885 mutex_lock(&sb->sldev_reconf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886 do {
Kiran Gunda3dad0212012-10-09 13:30:13 +05302887 struct slim_pending_ch *pch;
2888 u8 add_mark_removal = true;
2889
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002890 slc = &ctrl->chans[chan];
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002891 dev_dbg(&ctrl->dev, "chan:%d,ctrl:%d,def:%d", chan, chctrl,
2892 slc->def);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002893 if (slc->state < SLIM_CH_DEFINED) {
2894 ret = -ENOTCONN;
2895 break;
2896 }
2897 if (chctrl == SLIM_CH_SUSPEND) {
2898 ret = add_pending_ch(&sb->mark_suspend, chan);
2899 if (ret)
2900 break;
2901 } else if (chctrl == SLIM_CH_ACTIVATE) {
Sagar Dharia4ec2ff42011-09-26 10:20:17 -06002902 if (slc->state > SLIM_CH_ACTIVE) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002903 ret = -EISCONN;
2904 break;
2905 }
2906 ret = add_pending_ch(&sb->mark_define, chan);
2907 if (ret)
2908 break;
2909 } else {
2910 if (slc->state < SLIM_CH_ACTIVE) {
2911 ret = -ENOTCONN;
2912 break;
2913 }
Kiran Gunda3dad0212012-10-09 13:30:13 +05302914 /* If channel removal request comes when pending
2915 * in the mark_define, remove it from the define
2916 * list instead of adding it to removal list
2917 */
2918 if (!list_empty(&sb->mark_define)) {
2919 struct list_head *pos, *next;
2920 list_for_each_safe(pos, next,
2921 &sb->mark_define) {
2922 pch = list_entry(pos,
2923 struct slim_pending_ch,
2924 pending);
2925 if (pch->chan == slc->chan) {
2926 list_del(&pch->pending);
2927 kfree(pch);
2928 add_mark_removal = false;
2929 break;
2930 }
2931 }
2932 }
2933 if (add_mark_removal == true) {
2934 ret = add_pending_ch(&sb->mark_removal, chan);
2935 if (ret)
2936 break;
2937 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002938 }
2939
Sagar Dhariab886e042012-10-17 22:41:57 -06002940 nchan++;
2941 if (nchan < SLIM_GRP_TO_NCHAN(chanh))
Sagar Dharia29f35f02011-10-01 20:37:50 -06002942 chan = SLIM_HDL_TO_CHIDX(slc->nextgrp);
Sagar Dhariab886e042012-10-17 22:41:57 -06002943 } while (nchan < SLIM_GRP_TO_NCHAN(chanh));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002944 if (!ret && commit == true)
2945 ret = slim_reconfigure_now(sb);
2946 mutex_unlock(&sb->sldev_reconf);
2947 return ret;
2948}
2949EXPORT_SYMBOL_GPL(slim_control_ch);
2950
2951/*
2952 * slim_reservemsg_bw: Request to reserve bandwidth for messages.
2953 * @sb: client handle
2954 * @bw_bps: message bandwidth in bits per second to be requested
2955 * @commit: indicates whether the reconfiguration needs to be acted upon.
2956 * This API call can be grouped with slim_control_ch API call with only one of
2957 * the APIs specifying the commit flag to avoid reconfiguration being called too
2958 * frequently. -EXFULL is returned if there is no space in TDM to reserve the
2959 * bandwidth. -EBUSY is returned if reconfiguration is requested, but a request
2960 * is already in progress.
2961 */
2962int slim_reservemsg_bw(struct slim_device *sb, u32 bw_bps, bool commit)
2963{
2964 struct slim_controller *ctrl = sb->ctrl;
2965 int ret = 0;
2966 int sl;
2967 mutex_lock(&sb->sldev_reconf);
2968 if ((bw_bps >> 3) >= ctrl->a_framer->rootfreq)
2969 sl = SLIM_SL_PER_SUPERFRAME;
2970 else {
2971 sl = (bw_bps * (SLIM_CL_PER_SUPERFRAME_DIV8/SLIM_CL_PER_SL/2) +
2972 (ctrl->a_framer->rootfreq/2 - 1)) /
2973 (ctrl->a_framer->rootfreq/2);
2974 }
2975 dev_dbg(&ctrl->dev, "request:bw:%d, slots:%d, current:%d\n", bw_bps, sl,
2976 sb->cur_msgsl);
2977 sb->pending_msgsl = sl;
2978 if (commit == true)
2979 ret = slim_reconfigure_now(sb);
2980 mutex_unlock(&sb->sldev_reconf);
2981 return ret;
2982}
2983EXPORT_SYMBOL_GPL(slim_reservemsg_bw);
2984
Sagar Dharia33f34442011-08-08 16:22:03 -06002985/*
2986 * slim_ctrl_clk_pause: Called by slimbus controller to request clock to be
2987 * paused or woken up out of clock pause
2988 * or woken up from clock pause
2989 * @ctrl: controller requesting bus to be paused or woken up
2990 * @wakeup: Wakeup this controller from clock pause.
2991 * @restart: Restart time value per spec used for clock pause. This value
2992 * isn't used when controller is to be woken up.
2993 * This API executes clock pause reconfiguration sequence if wakeup is false.
2994 * If wakeup is true, controller's wakeup is called
2995 * Slimbus clock is idle and can be disabled by the controller later.
2996 */
2997int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
2998{
2999 int ret = 0;
3000 int i;
3001
3002 if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
3003 return -EINVAL;
3004 mutex_lock(&ctrl->m_ctrl);
3005 if (wakeup) {
3006 if (ctrl->clk_state == SLIM_CLK_ACTIVE) {
3007 mutex_unlock(&ctrl->m_ctrl);
3008 return 0;
3009 }
3010 wait_for_completion(&ctrl->pause_comp);
3011 /*
3012 * Slimbus framework will call controller wakeup
3013 * Controller should make sure that it sets active framer
3014 * out of clock pause by doing appropriate setting
3015 */
3016 if (ctrl->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
3017 ret = ctrl->wakeup(ctrl);
3018 if (!ret)
3019 ctrl->clk_state = SLIM_CLK_ACTIVE;
3020 mutex_unlock(&ctrl->m_ctrl);
3021 return ret;
3022 } else {
3023 switch (ctrl->clk_state) {
3024 case SLIM_CLK_ENTERING_PAUSE:
3025 case SLIM_CLK_PAUSE_FAILED:
3026 /*
3027 * If controller is already trying to enter clock pause,
3028 * let it finish.
3029 * In case of error, retry
3030 * In both cases, previous clock pause has signalled
3031 * completion.
3032 */
3033 wait_for_completion(&ctrl->pause_comp);
3034 /* retry upon failure */
3035 if (ctrl->clk_state == SLIM_CLK_PAUSE_FAILED) {
3036 ctrl->clk_state = SLIM_CLK_ACTIVE;
3037 break;
3038 } else {
3039 mutex_unlock(&ctrl->m_ctrl);
3040 /*
3041 * Signal completion so that wakeup can wait on
3042 * it.
3043 */
3044 complete(&ctrl->pause_comp);
3045 return 0;
3046 }
3047 break;
3048 case SLIM_CLK_PAUSED:
3049 /* already paused */
3050 mutex_unlock(&ctrl->m_ctrl);
3051 return 0;
3052 case SLIM_CLK_ACTIVE:
3053 default:
3054 break;
3055 }
3056 }
3057 /* Pending response for a message */
3058 for (i = 0; i < ctrl->last_tid; i++) {
3059 if (ctrl->txnt[i]) {
3060 ret = -EBUSY;
Sagar Dharia33beca02012-10-22 16:21:46 -06003061 pr_info("slim_clk_pause: txn-rsp for %d pending", i);
Sagar Dharia33f34442011-08-08 16:22:03 -06003062 mutex_unlock(&ctrl->m_ctrl);
3063 return -EBUSY;
3064 }
3065 }
3066 ctrl->clk_state = SLIM_CLK_ENTERING_PAUSE;
3067 mutex_unlock(&ctrl->m_ctrl);
3068
3069 mutex_lock(&ctrl->sched.m_reconf);
3070 /* Data channels active */
3071 if (ctrl->sched.usedslots) {
Sagar Dharia33beca02012-10-22 16:21:46 -06003072 pr_info("slim_clk_pause: data channel active");
Sagar Dharia33f34442011-08-08 16:22:03 -06003073 ret = -EBUSY;
3074 goto clk_pause_ret;
3075 }
3076
3077 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003078 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_BEGIN_RECONFIGURATION,
3079 0, SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003080 if (ret)
3081 goto clk_pause_ret;
3082
3083 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
Sagar Dharia45ee38a2011-08-03 17:01:31 -06003084 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_NEXT_PAUSE_CLOCK, 0,
3085 SLIM_MSG_MT_CORE, NULL, &restart, 1, 4, NULL, 0, NULL);
3086 if (ret)
3087 goto clk_pause_ret;
3088
3089 ret = slim_processtxn(ctrl, SLIM_MSG_DEST_BROADCAST,
3090 SLIM_MSG_CLK_PAUSE_SEQ_FLG | SLIM_MSG_MC_RECONFIGURE_NOW, 0,
3091 SLIM_MSG_MT_CORE, NULL, NULL, 0, 3, NULL, 0, NULL);
Sagar Dharia33f34442011-08-08 16:22:03 -06003092 if (ret)
3093 goto clk_pause_ret;
3094
3095clk_pause_ret:
3096 if (ret)
3097 ctrl->clk_state = SLIM_CLK_PAUSE_FAILED;
3098 else
3099 ctrl->clk_state = SLIM_CLK_PAUSED;
3100 complete(&ctrl->pause_comp);
3101 mutex_unlock(&ctrl->sched.m_reconf);
3102 return ret;
3103}
Sagar Dharia88821fb2012-07-24 23:04:32 -06003104EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003105
3106MODULE_LICENSE("GPL v2");
3107MODULE_VERSION("0.1");
3108MODULE_DESCRIPTION("Slimbus module");
3109MODULE_ALIAS("platform:slimbus");