]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/firmware/ti_sci.c
MAINTAINERS: Update MAX77802 PMIC entry
[mirror_ubuntu-artful-kernel.git] / drivers / firmware / ti_sci.c
1 /*
2 * Texas Instruments System Control Interface Protocol Driver
3 *
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5 * Nishanth Menon
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #define pr_fmt(fmt) "%s: " fmt, __func__
18
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/export.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/mailbox_client.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29 #include <linux/soc/ti/ti-msgmgr.h>
30 #include <linux/soc/ti/ti_sci_protocol.h>
31 #include <linux/reboot.h>
32
33 #include "ti_sci.h"
34
35 /* List of all TI SCI devices active in system */
36 static LIST_HEAD(ti_sci_list);
37 /* Protection for the entire list */
38 static DEFINE_MUTEX(ti_sci_list_mutex);
39
40 /**
41 * struct ti_sci_xfer - Structure representing a message flow
42 * @tx_message: Transmit message
43 * @rx_len: Receive message length
44 * @xfer_buf: Preallocated buffer to store receive message
45 * Since we work with request-ACK protocol, we can
46 * reuse the same buffer for the rx path as we
47 * use for the tx path.
48 * @done: completion event
49 */
50 struct ti_sci_xfer {
51 struct ti_msgmgr_message tx_message;
52 u8 rx_len;
53 u8 *xfer_buf;
54 struct completion done;
55 };
56
57 /**
58 * struct ti_sci_xfers_info - Structure to manage transfer information
59 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
60 * Messages.
61 * @xfer_block: Preallocated Message array
62 * @xfer_alloc_table: Bitmap table for allocated messages.
63 * Index of this bitmap table is also used for message
64 * sequence identifier.
65 * @xfer_lock: Protection for message allocation
66 */
67 struct ti_sci_xfers_info {
68 struct semaphore sem_xfer_count;
69 struct ti_sci_xfer *xfer_block;
70 unsigned long *xfer_alloc_table;
71 /* protect transfer allocation */
72 spinlock_t xfer_lock;
73 };
74
75 /**
76 * struct ti_sci_desc - Description of SoC integration
77 * @host_id: Host identifier representing the compute entity
78 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
79 * @max_msgs: Maximum number of messages that can be pending
80 * simultaneously in the system
81 * @max_msg_size: Maximum size of data per message that can be handled.
82 */
83 struct ti_sci_desc {
84 u8 host_id;
85 int max_rx_timeout_ms;
86 int max_msgs;
87 int max_msg_size;
88 };
89
90 /**
91 * struct ti_sci_info - Structure representing a TI SCI instance
92 * @dev: Device pointer
93 * @desc: SoC description for this instance
94 * @nb: Reboot Notifier block
95 * @d: Debugfs file entry
96 * @debug_region: Memory region where the debug message are available
97 * @debug_region_size: Debug region size
98 * @debug_buffer: Buffer allocated to copy debug messages.
99 * @handle: Instance of TI SCI handle to send to clients.
100 * @cl: Mailbox Client
101 * @chan_tx: Transmit mailbox channel
102 * @chan_rx: Receive mailbox channel
103 * @minfo: Message info
104 * @node: list head
105 * @users: Number of users of this instance
106 */
107 struct ti_sci_info {
108 struct device *dev;
109 struct notifier_block nb;
110 const struct ti_sci_desc *desc;
111 struct dentry *d;
112 void __iomem *debug_region;
113 char *debug_buffer;
114 size_t debug_region_size;
115 struct ti_sci_handle handle;
116 struct mbox_client cl;
117 struct mbox_chan *chan_tx;
118 struct mbox_chan *chan_rx;
119 struct ti_sci_xfers_info minfo;
120 struct list_head node;
121 /* protected by ti_sci_list_mutex */
122 int users;
123
124 };
125
126 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
127 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
128 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
129
130 #ifdef CONFIG_DEBUG_FS
131
132 /**
133 * ti_sci_debug_show() - Helper to dump the debug log
134 * @s: sequence file pointer
135 * @unused: unused.
136 *
137 * Return: 0
138 */
139 static int ti_sci_debug_show(struct seq_file *s, void *unused)
140 {
141 struct ti_sci_info *info = s->private;
142
143 memcpy_fromio(info->debug_buffer, info->debug_region,
144 info->debug_region_size);
145 /*
146 * We don't trust firmware to leave NULL terminated last byte (hence
147 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
148 * specific data format for debug messages, We just present the data
149 * in the buffer as is - we expect the messages to be self explanatory.
150 */
151 seq_puts(s, info->debug_buffer);
152 return 0;
153 }
154
155 /**
156 * ti_sci_debug_open() - debug file open
157 * @inode: inode pointer
158 * @file: file pointer
159 *
160 * Return: result of single_open
161 */
162 static int ti_sci_debug_open(struct inode *inode, struct file *file)
163 {
164 return single_open(file, ti_sci_debug_show, inode->i_private);
165 }
166
167 /* log file operations */
168 static const struct file_operations ti_sci_debug_fops = {
169 .open = ti_sci_debug_open,
170 .read = seq_read,
171 .llseek = seq_lseek,
172 .release = single_release,
173 };
174
175 /**
176 * ti_sci_debugfs_create() - Create log debug file
177 * @pdev: platform device pointer
178 * @info: Pointer to SCI entity information
179 *
180 * Return: 0 if all went fine, else corresponding error.
181 */
182 static int ti_sci_debugfs_create(struct platform_device *pdev,
183 struct ti_sci_info *info)
184 {
185 struct device *dev = &pdev->dev;
186 struct resource *res;
187 char debug_name[50] = "ti_sci_debug@";
188
189 /* Debug region is optional */
190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
191 "debug_messages");
192 info->debug_region = devm_ioremap_resource(dev, res);
193 if (IS_ERR(info->debug_region))
194 return 0;
195 info->debug_region_size = resource_size(res);
196
197 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
198 sizeof(char), GFP_KERNEL);
199 if (!info->debug_buffer)
200 return -ENOMEM;
201 /* Setup NULL termination */
202 info->debug_buffer[info->debug_region_size] = 0;
203
204 info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
205 sizeof(debug_name)),
206 0444, NULL, info, &ti_sci_debug_fops);
207 if (IS_ERR(info->d))
208 return PTR_ERR(info->d);
209
210 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
211 info->debug_region, info->debug_region_size, res);
212 return 0;
213 }
214
215 /**
216 * ti_sci_debugfs_destroy() - clean up log debug file
217 * @pdev: platform device pointer
218 * @info: Pointer to SCI entity information
219 */
220 static void ti_sci_debugfs_destroy(struct platform_device *pdev,
221 struct ti_sci_info *info)
222 {
223 if (IS_ERR(info->debug_region))
224 return;
225
226 debugfs_remove(info->d);
227 }
228 #else /* CONFIG_DEBUG_FS */
229 static inline int ti_sci_debugfs_create(struct platform_device *dev,
230 struct ti_sci_info *info)
231 {
232 return 0;
233 }
234
235 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
236 struct ti_sci_info *info)
237 {
238 }
239 #endif /* CONFIG_DEBUG_FS */
240
241 /**
242 * ti_sci_dump_header_dbg() - Helper to dump a message header.
243 * @dev: Device pointer corresponding to the SCI entity
244 * @hdr: pointer to header.
245 */
246 static inline void ti_sci_dump_header_dbg(struct device *dev,
247 struct ti_sci_msg_hdr *hdr)
248 {
249 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
250 hdr->type, hdr->host, hdr->seq, hdr->flags);
251 }
252
253 /**
254 * ti_sci_rx_callback() - mailbox client callback for receive messages
255 * @cl: client pointer
256 * @m: mailbox message
257 *
258 * Processes one received message to appropriate transfer information and
259 * signals completion of the transfer.
260 *
261 * NOTE: This function will be invoked in IRQ context, hence should be
262 * as optimal as possible.
263 */
264 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
265 {
266 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
267 struct device *dev = info->dev;
268 struct ti_sci_xfers_info *minfo = &info->minfo;
269 struct ti_msgmgr_message *mbox_msg = m;
270 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
271 struct ti_sci_xfer *xfer;
272 u8 xfer_id;
273
274 xfer_id = hdr->seq;
275
276 /*
277 * Are we even expecting this?
278 * NOTE: barriers were implicit in locks used for modifying the bitmap
279 */
280 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
281 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
282 return;
283 }
284
285 xfer = &minfo->xfer_block[xfer_id];
286
287 /* Is the message of valid length? */
288 if (mbox_msg->len > info->desc->max_msg_size) {
289 dev_err(dev, "Unable to handle %d xfer(max %d)\n",
290 mbox_msg->len, info->desc->max_msg_size);
291 ti_sci_dump_header_dbg(dev, hdr);
292 return;
293 }
294 if (mbox_msg->len < xfer->rx_len) {
295 dev_err(dev, "Recv xfer %d < expected %d length\n",
296 mbox_msg->len, xfer->rx_len);
297 ti_sci_dump_header_dbg(dev, hdr);
298 return;
299 }
300
301 ti_sci_dump_header_dbg(dev, hdr);
302 /* Take a copy to the rx buffer.. */
303 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
304 complete(&xfer->done);
305 }
306
307 /**
308 * ti_sci_get_one_xfer() - Allocate one message
309 * @info: Pointer to SCI entity information
310 * @msg_type: Message type
311 * @msg_flags: Flag to set for the message
312 * @tx_message_size: transmit message size
313 * @rx_message_size: receive message size
314 *
315 * Helper function which is used by various command functions that are
316 * exposed to clients of this driver for allocating a message traffic event.
317 *
318 * This function can sleep depending on pending requests already in the system
319 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
320 * of internal data structures.
321 *
322 * Return: 0 if all went fine, else corresponding error.
323 */
324 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
325 u16 msg_type, u32 msg_flags,
326 size_t tx_message_size,
327 size_t rx_message_size)
328 {
329 struct ti_sci_xfers_info *minfo = &info->minfo;
330 struct ti_sci_xfer *xfer;
331 struct ti_sci_msg_hdr *hdr;
332 unsigned long flags;
333 unsigned long bit_pos;
334 u8 xfer_id;
335 int ret;
336 int timeout;
337
338 /* Ensure we have sane transfer sizes */
339 if (rx_message_size > info->desc->max_msg_size ||
340 tx_message_size > info->desc->max_msg_size ||
341 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
342 return ERR_PTR(-ERANGE);
343
344 /*
345 * Ensure we have only controlled number of pending messages.
346 * Ideally, we might just have to wait a single message, be
347 * conservative and wait 5 times that..
348 */
349 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
350 ret = down_timeout(&minfo->sem_xfer_count, timeout);
351 if (ret < 0)
352 return ERR_PTR(ret);
353
354 /* Keep the locked section as small as possible */
355 spin_lock_irqsave(&minfo->xfer_lock, flags);
356 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
357 info->desc->max_msgs);
358 set_bit(bit_pos, minfo->xfer_alloc_table);
359 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
360
361 /*
362 * We already ensured in probe that we can have max messages that can
363 * fit in hdr.seq - NOTE: this improves access latencies
364 * to predictable O(1) access, BUT, it opens us to risk if
365 * remote misbehaves with corrupted message sequence responses.
366 * If that happens, we are going to be messed up anyways..
367 */
368 xfer_id = (u8)bit_pos;
369
370 xfer = &minfo->xfer_block[xfer_id];
371
372 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
373 xfer->tx_message.len = tx_message_size;
374 xfer->rx_len = (u8)rx_message_size;
375
376 reinit_completion(&xfer->done);
377
378 hdr->seq = xfer_id;
379 hdr->type = msg_type;
380 hdr->host = info->desc->host_id;
381 hdr->flags = msg_flags;
382
383 return xfer;
384 }
385
386 /**
387 * ti_sci_put_one_xfer() - Release a message
388 * @minfo: transfer info pointer
389 * @xfer: message that was reserved by ti_sci_get_one_xfer
390 *
391 * This holds a spinlock to maintain integrity of internal data structures.
392 */
393 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
394 struct ti_sci_xfer *xfer)
395 {
396 unsigned long flags;
397 struct ti_sci_msg_hdr *hdr;
398 u8 xfer_id;
399
400 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
401 xfer_id = hdr->seq;
402
403 /*
404 * Keep the locked section as small as possible
405 * NOTE: we might escape with smp_mb and no lock here..
406 * but just be conservative and symmetric.
407 */
408 spin_lock_irqsave(&minfo->xfer_lock, flags);
409 clear_bit(xfer_id, minfo->xfer_alloc_table);
410 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
411
412 /* Increment the count for the next user to get through */
413 up(&minfo->sem_xfer_count);
414 }
415
416 /**
417 * ti_sci_do_xfer() - Do one transfer
418 * @info: Pointer to SCI entity information
419 * @xfer: Transfer to initiate and wait for response
420 *
421 * Return: -ETIMEDOUT in case of no response, if transmit error,
422 * return corresponding error, else if all goes well,
423 * return 0.
424 */
425 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
426 struct ti_sci_xfer *xfer)
427 {
428 int ret;
429 int timeout;
430 struct device *dev = info->dev;
431
432 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
433 if (ret < 0)
434 return ret;
435
436 ret = 0;
437
438 /* And we wait for the response. */
439 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
440 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
441 dev_err(dev, "Mbox timedout in resp(caller: %pF)\n",
442 (void *)_RET_IP_);
443 ret = -ETIMEDOUT;
444 }
445 /*
446 * NOTE: we might prefer not to need the mailbox ticker to manage the
447 * transfer queueing since the protocol layer queues things by itself.
448 * Unfortunately, we have to kick the mailbox framework after we have
449 * received our message.
450 */
451 mbox_client_txdone(info->chan_tx, ret);
452
453 return ret;
454 }
455
456 /**
457 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
458 * @info: Pointer to SCI entity information
459 *
460 * Updates the SCI information in the internal data structure.
461 *
462 * Return: 0 if all went fine, else return appropriate error.
463 */
464 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
465 {
466 struct device *dev = info->dev;
467 struct ti_sci_handle *handle = &info->handle;
468 struct ti_sci_version_info *ver = &handle->version;
469 struct ti_sci_msg_resp_version *rev_info;
470 struct ti_sci_xfer *xfer;
471 int ret;
472
473 /* No need to setup flags since it is expected to respond */
474 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
475 0x0, sizeof(struct ti_sci_msg_hdr),
476 sizeof(*rev_info));
477 if (IS_ERR(xfer)) {
478 ret = PTR_ERR(xfer);
479 dev_err(dev, "Message alloc failed(%d)\n", ret);
480 return ret;
481 }
482
483 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
484
485 ret = ti_sci_do_xfer(info, xfer);
486 if (ret) {
487 dev_err(dev, "Mbox send fail %d\n", ret);
488 goto fail;
489 }
490
491 ver->abi_major = rev_info->abi_major;
492 ver->abi_minor = rev_info->abi_minor;
493 ver->firmware_revision = rev_info->firmware_revision;
494 strncpy(ver->firmware_description, rev_info->firmware_description,
495 sizeof(ver->firmware_description));
496
497 fail:
498 ti_sci_put_one_xfer(&info->minfo, xfer);
499 return ret;
500 }
501
502 /**
503 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
504 * @r: pointer to response buffer
505 *
506 * Return: true if the response was an ACK, else returns false.
507 */
508 static inline bool ti_sci_is_response_ack(void *r)
509 {
510 struct ti_sci_msg_hdr *hdr = r;
511
512 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
513 }
514
515 /**
516 * ti_sci_set_device_state() - Set device state helper
517 * @handle: pointer to TI SCI handle
518 * @id: Device identifier
519 * @flags: flags to setup for the device
520 * @state: State to move the device to
521 *
522 * Return: 0 if all went well, else returns appropriate error value.
523 */
524 static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
525 u32 id, u32 flags, u8 state)
526 {
527 struct ti_sci_info *info;
528 struct ti_sci_msg_req_set_device_state *req;
529 struct ti_sci_msg_hdr *resp;
530 struct ti_sci_xfer *xfer;
531 struct device *dev;
532 int ret = 0;
533
534 if (IS_ERR(handle))
535 return PTR_ERR(handle);
536 if (!handle)
537 return -EINVAL;
538
539 info = handle_to_ti_sci_info(handle);
540 dev = info->dev;
541
542 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
543 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
544 sizeof(*req), sizeof(*resp));
545 if (IS_ERR(xfer)) {
546 ret = PTR_ERR(xfer);
547 dev_err(dev, "Message alloc failed(%d)\n", ret);
548 return ret;
549 }
550 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
551 req->id = id;
552 req->state = state;
553
554 ret = ti_sci_do_xfer(info, xfer);
555 if (ret) {
556 dev_err(dev, "Mbox send fail %d\n", ret);
557 goto fail;
558 }
559
560 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
561
562 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
563
564 fail:
565 ti_sci_put_one_xfer(&info->minfo, xfer);
566
567 return ret;
568 }
569
570 /**
571 * ti_sci_get_device_state() - Get device state helper
572 * @handle: Handle to the device
573 * @id: Device Identifier
574 * @clcnt: Pointer to Context Loss Count
575 * @resets: pointer to resets
576 * @p_state: pointer to p_state
577 * @c_state: pointer to c_state
578 *
579 * Return: 0 if all went fine, else return appropriate error.
580 */
581 static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
582 u32 id, u32 *clcnt, u32 *resets,
583 u8 *p_state, u8 *c_state)
584 {
585 struct ti_sci_info *info;
586 struct ti_sci_msg_req_get_device_state *req;
587 struct ti_sci_msg_resp_get_device_state *resp;
588 struct ti_sci_xfer *xfer;
589 struct device *dev;
590 int ret = 0;
591
592 if (IS_ERR(handle))
593 return PTR_ERR(handle);
594 if (!handle)
595 return -EINVAL;
596
597 if (!clcnt && !resets && !p_state && !c_state)
598 return -EINVAL;
599
600 info = handle_to_ti_sci_info(handle);
601 dev = info->dev;
602
603 /* Response is expected, so need of any flags */
604 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
605 0, sizeof(*req), sizeof(*resp));
606 if (IS_ERR(xfer)) {
607 ret = PTR_ERR(xfer);
608 dev_err(dev, "Message alloc failed(%d)\n", ret);
609 return ret;
610 }
611 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
612 req->id = id;
613
614 ret = ti_sci_do_xfer(info, xfer);
615 if (ret) {
616 dev_err(dev, "Mbox send fail %d\n", ret);
617 goto fail;
618 }
619
620 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
621 if (!ti_sci_is_response_ack(resp)) {
622 ret = -ENODEV;
623 goto fail;
624 }
625
626 if (clcnt)
627 *clcnt = resp->context_loss_count;
628 if (resets)
629 *resets = resp->resets;
630 if (p_state)
631 *p_state = resp->programmed_state;
632 if (c_state)
633 *c_state = resp->current_state;
634 fail:
635 ti_sci_put_one_xfer(&info->minfo, xfer);
636
637 return ret;
638 }
639
640 /**
641 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
642 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
643 * @id: Device Identifier
644 *
645 * Request for the device - NOTE: the client MUST maintain integrity of
646 * usage count by balancing get_device with put_device. No refcounting is
647 * managed by driver for that purpose.
648 *
649 * NOTE: The request is for exclusive access for the processor.
650 *
651 * Return: 0 if all went fine, else return appropriate error.
652 */
653 static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
654 {
655 return ti_sci_set_device_state(handle, id,
656 MSG_FLAG_DEVICE_EXCLUSIVE,
657 MSG_DEVICE_SW_STATE_ON);
658 }
659
660 /**
661 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
662 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
663 * @id: Device Identifier
664 *
665 * Request for the device - NOTE: the client MUST maintain integrity of
666 * usage count by balancing get_device with put_device. No refcounting is
667 * managed by driver for that purpose.
668 *
669 * Return: 0 if all went fine, else return appropriate error.
670 */
671 static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
672 {
673 return ti_sci_set_device_state(handle, id,
674 MSG_FLAG_DEVICE_EXCLUSIVE,
675 MSG_DEVICE_SW_STATE_RETENTION);
676 }
677
678 /**
679 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
680 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
681 * @id: Device Identifier
682 *
683 * Request for the device - NOTE: the client MUST maintain integrity of
684 * usage count by balancing get_device with put_device. No refcounting is
685 * managed by driver for that purpose.
686 *
687 * Return: 0 if all went fine, else return appropriate error.
688 */
689 static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
690 {
691 return ti_sci_set_device_state(handle, id,
692 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
693 }
694
695 /**
696 * ti_sci_cmd_dev_is_valid() - Is the device valid
697 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
698 * @id: Device Identifier
699 *
700 * Return: 0 if all went fine and the device ID is valid, else return
701 * appropriate error.
702 */
703 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
704 {
705 u8 unused;
706
707 /* check the device state which will also tell us if the ID is valid */
708 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
709 }
710
711 /**
712 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
713 * @handle: Pointer to TISCI handle
714 * @id: Device Identifier
715 * @count: Pointer to Context Loss counter to populate
716 *
717 * Return: 0 if all went fine, else return appropriate error.
718 */
719 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
720 u32 *count)
721 {
722 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
723 }
724
725 /**
726 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
727 * @handle: Pointer to TISCI handle
728 * @id: Device Identifier
729 * @r_state: true if requested to be idle
730 *
731 * Return: 0 if all went fine, else return appropriate error.
732 */
733 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
734 bool *r_state)
735 {
736 int ret;
737 u8 state;
738
739 if (!r_state)
740 return -EINVAL;
741
742 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
743 if (ret)
744 return ret;
745
746 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
747
748 return 0;
749 }
750
751 /**
752 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
753 * @handle: Pointer to TISCI handle
754 * @id: Device Identifier
755 * @r_state: true if requested to be stopped
756 * @curr_state: true if currently stopped.
757 *
758 * Return: 0 if all went fine, else return appropriate error.
759 */
760 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
761 bool *r_state, bool *curr_state)
762 {
763 int ret;
764 u8 p_state, c_state;
765
766 if (!r_state && !curr_state)
767 return -EINVAL;
768
769 ret =
770 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
771 if (ret)
772 return ret;
773
774 if (r_state)
775 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
776 if (curr_state)
777 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
778
779 return 0;
780 }
781
782 /**
783 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
784 * @handle: Pointer to TISCI handle
785 * @id: Device Identifier
786 * @r_state: true if requested to be ON
787 * @curr_state: true if currently ON and active
788 *
789 * Return: 0 if all went fine, else return appropriate error.
790 */
791 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
792 bool *r_state, bool *curr_state)
793 {
794 int ret;
795 u8 p_state, c_state;
796
797 if (!r_state && !curr_state)
798 return -EINVAL;
799
800 ret =
801 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
802 if (ret)
803 return ret;
804
805 if (r_state)
806 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
807 if (curr_state)
808 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
809
810 return 0;
811 }
812
813 /**
814 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
815 * @handle: Pointer to TISCI handle
816 * @id: Device Identifier
817 * @curr_state: true if currently transitioning.
818 *
819 * Return: 0 if all went fine, else return appropriate error.
820 */
821 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
822 bool *curr_state)
823 {
824 int ret;
825 u8 state;
826
827 if (!curr_state)
828 return -EINVAL;
829
830 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
831 if (ret)
832 return ret;
833
834 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
835
836 return 0;
837 }
838
839 /**
840 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
841 * by TISCI
842 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
843 * @id: Device Identifier
844 * @reset_state: Device specific reset bit field
845 *
846 * Return: 0 if all went fine, else return appropriate error.
847 */
848 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
849 u32 id, u32 reset_state)
850 {
851 struct ti_sci_info *info;
852 struct ti_sci_msg_req_set_device_resets *req;
853 struct ti_sci_msg_hdr *resp;
854 struct ti_sci_xfer *xfer;
855 struct device *dev;
856 int ret = 0;
857
858 if (IS_ERR(handle))
859 return PTR_ERR(handle);
860 if (!handle)
861 return -EINVAL;
862
863 info = handle_to_ti_sci_info(handle);
864 dev = info->dev;
865
866 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
867 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
868 sizeof(*req), sizeof(*resp));
869 if (IS_ERR(xfer)) {
870 ret = PTR_ERR(xfer);
871 dev_err(dev, "Message alloc failed(%d)\n", ret);
872 return ret;
873 }
874 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
875 req->id = id;
876 req->resets = reset_state;
877
878 ret = ti_sci_do_xfer(info, xfer);
879 if (ret) {
880 dev_err(dev, "Mbox send fail %d\n", ret);
881 goto fail;
882 }
883
884 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
885
886 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
887
888 fail:
889 ti_sci_put_one_xfer(&info->minfo, xfer);
890
891 return ret;
892 }
893
894 /**
895 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
896 * by TISCI
897 * @handle: Pointer to TISCI handle
898 * @id: Device Identifier
899 * @reset_state: Pointer to reset state to populate
900 *
901 * Return: 0 if all went fine, else return appropriate error.
902 */
903 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
904 u32 id, u32 *reset_state)
905 {
906 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
907 NULL);
908 }
909
910 /**
911 * ti_sci_set_clock_state() - Set clock state helper
912 * @handle: pointer to TI SCI handle
913 * @dev_id: Device identifier this request is for
914 * @clk_id: Clock identifier for the device for this request.
915 * Each device has it's own set of clock inputs. This indexes
916 * which clock input to modify.
917 * @flags: Header flags as needed
918 * @state: State to request for the clock.
919 *
920 * Return: 0 if all went well, else returns appropriate error value.
921 */
922 static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
923 u32 dev_id, u8 clk_id,
924 u32 flags, u8 state)
925 {
926 struct ti_sci_info *info;
927 struct ti_sci_msg_req_set_clock_state *req;
928 struct ti_sci_msg_hdr *resp;
929 struct ti_sci_xfer *xfer;
930 struct device *dev;
931 int ret = 0;
932
933 if (IS_ERR(handle))
934 return PTR_ERR(handle);
935 if (!handle)
936 return -EINVAL;
937
938 info = handle_to_ti_sci_info(handle);
939 dev = info->dev;
940
941 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
942 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
943 sizeof(*req), sizeof(*resp));
944 if (IS_ERR(xfer)) {
945 ret = PTR_ERR(xfer);
946 dev_err(dev, "Message alloc failed(%d)\n", ret);
947 return ret;
948 }
949 req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
950 req->dev_id = dev_id;
951 req->clk_id = clk_id;
952 req->request_state = state;
953
954 ret = ti_sci_do_xfer(info, xfer);
955 if (ret) {
956 dev_err(dev, "Mbox send fail %d\n", ret);
957 goto fail;
958 }
959
960 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
961
962 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
963
964 fail:
965 ti_sci_put_one_xfer(&info->minfo, xfer);
966
967 return ret;
968 }
969
970 /**
971 * ti_sci_cmd_get_clock_state() - Get clock state helper
972 * @handle: pointer to TI SCI handle
973 * @dev_id: Device identifier this request is for
974 * @clk_id: Clock identifier for the device for this request.
975 * Each device has it's own set of clock inputs. This indexes
976 * which clock input to modify.
977 * @programmed_state: State requested for clock to move to
978 * @current_state: State that the clock is currently in
979 *
980 * Return: 0 if all went well, else returns appropriate error value.
981 */
982 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
983 u32 dev_id, u8 clk_id,
984 u8 *programmed_state, u8 *current_state)
985 {
986 struct ti_sci_info *info;
987 struct ti_sci_msg_req_get_clock_state *req;
988 struct ti_sci_msg_resp_get_clock_state *resp;
989 struct ti_sci_xfer *xfer;
990 struct device *dev;
991 int ret = 0;
992
993 if (IS_ERR(handle))
994 return PTR_ERR(handle);
995 if (!handle)
996 return -EINVAL;
997
998 if (!programmed_state && !current_state)
999 return -EINVAL;
1000
1001 info = handle_to_ti_sci_info(handle);
1002 dev = info->dev;
1003
1004 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1005 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1006 sizeof(*req), sizeof(*resp));
1007 if (IS_ERR(xfer)) {
1008 ret = PTR_ERR(xfer);
1009 dev_err(dev, "Message alloc failed(%d)\n", ret);
1010 return ret;
1011 }
1012 req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1013 req->dev_id = dev_id;
1014 req->clk_id = clk_id;
1015
1016 ret = ti_sci_do_xfer(info, xfer);
1017 if (ret) {
1018 dev_err(dev, "Mbox send fail %d\n", ret);
1019 goto fail;
1020 }
1021
1022 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1023
1024 if (!ti_sci_is_response_ack(resp)) {
1025 ret = -ENODEV;
1026 goto fail;
1027 }
1028
1029 if (programmed_state)
1030 *programmed_state = resp->programmed_state;
1031 if (current_state)
1032 *current_state = resp->current_state;
1033
1034 fail:
1035 ti_sci_put_one_xfer(&info->minfo, xfer);
1036
1037 return ret;
1038 }
1039
1040 /**
1041 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1042 * @handle: pointer to TI SCI handle
1043 * @dev_id: Device identifier this request is for
1044 * @clk_id: Clock identifier for the device for this request.
1045 * Each device has it's own set of clock inputs. This indexes
1046 * which clock input to modify.
1047 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1048 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1049 * @enable_input_term: 'true' if input termination is desired, else 'false'
1050 *
1051 * Return: 0 if all went well, else returns appropriate error value.
1052 */
1053 static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1054 u8 clk_id, bool needs_ssc, bool can_change_freq,
1055 bool enable_input_term)
1056 {
1057 u32 flags = 0;
1058
1059 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1060 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1061 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1062
1063 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1064 MSG_CLOCK_SW_STATE_REQ);
1065 }
1066
1067 /**
1068 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1069 * @handle: pointer to TI SCI handle
1070 * @dev_id: Device identifier this request is for
1071 * @clk_id: Clock identifier for the device for this request.
1072 * Each device has it's own set of clock inputs. This indexes
1073 * which clock input to modify.
1074 *
1075 * NOTE: This clock must have been requested by get_clock previously.
1076 *
1077 * Return: 0 if all went well, else returns appropriate error value.
1078 */
1079 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1080 u32 dev_id, u8 clk_id)
1081 {
1082 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1083 MSG_CLOCK_SW_STATE_UNREQ);
1084 }
1085
1086 /**
1087 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1088 * @handle: pointer to TI SCI handle
1089 * @dev_id: Device identifier this request is for
1090 * @clk_id: Clock identifier for the device for this request.
1091 * Each device has it's own set of clock inputs. This indexes
1092 * which clock input to modify.
1093 *
1094 * NOTE: This clock must have been requested by get_clock previously.
1095 *
1096 * Return: 0 if all went well, else returns appropriate error value.
1097 */
1098 static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1099 u32 dev_id, u8 clk_id)
1100 {
1101 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1102 MSG_CLOCK_SW_STATE_AUTO);
1103 }
1104
1105 /**
1106 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1107 * @handle: pointer to TI SCI handle
1108 * @dev_id: Device identifier this request is for
1109 * @clk_id: Clock identifier for the device for this request.
1110 * Each device has it's own set of clock inputs. This indexes
1111 * which clock input to modify.
1112 * @req_state: state indicating if the clock is auto managed
1113 *
1114 * Return: 0 if all went well, else returns appropriate error value.
1115 */
1116 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1117 u32 dev_id, u8 clk_id, bool *req_state)
1118 {
1119 u8 state = 0;
1120 int ret;
1121
1122 if (!req_state)
1123 return -EINVAL;
1124
1125 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1126 if (ret)
1127 return ret;
1128
1129 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1130 return 0;
1131 }
1132
1133 /**
1134 * ti_sci_cmd_clk_is_on() - Is the clock ON
1135 * @handle: pointer to TI SCI handle
1136 * @dev_id: Device identifier this request is for
1137 * @clk_id: Clock identifier for the device for this request.
1138 * Each device has it's own set of clock inputs. This indexes
1139 * which clock input to modify.
1140 * @req_state: state indicating if the clock is managed by us and enabled
1141 * @curr_state: state indicating if the clock is ready for operation
1142 *
1143 * Return: 0 if all went well, else returns appropriate error value.
1144 */
1145 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1146 u8 clk_id, bool *req_state, bool *curr_state)
1147 {
1148 u8 c_state = 0, r_state = 0;
1149 int ret;
1150
1151 if (!req_state && !curr_state)
1152 return -EINVAL;
1153
1154 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1155 &r_state, &c_state);
1156 if (ret)
1157 return ret;
1158
1159 if (req_state)
1160 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1161 if (curr_state)
1162 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1163 return 0;
1164 }
1165
1166 /**
1167 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1168 * @handle: pointer to TI SCI handle
1169 * @dev_id: Device identifier this request is for
1170 * @clk_id: Clock identifier for the device for this request.
1171 * Each device has it's own set of clock inputs. This indexes
1172 * which clock input to modify.
1173 * @req_state: state indicating if the clock is managed by us and disabled
1174 * @curr_state: state indicating if the clock is NOT ready for operation
1175 *
1176 * Return: 0 if all went well, else returns appropriate error value.
1177 */
1178 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1179 u8 clk_id, bool *req_state, bool *curr_state)
1180 {
1181 u8 c_state = 0, r_state = 0;
1182 int ret;
1183
1184 if (!req_state && !curr_state)
1185 return -EINVAL;
1186
1187 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1188 &r_state, &c_state);
1189 if (ret)
1190 return ret;
1191
1192 if (req_state)
1193 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1194 if (curr_state)
1195 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1196 return 0;
1197 }
1198
1199 /**
1200 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1201 * @handle: pointer to TI SCI handle
1202 * @dev_id: Device identifier this request is for
1203 * @clk_id: Clock identifier for the device for this request.
1204 * Each device has it's own set of clock inputs. This indexes
1205 * which clock input to modify.
1206 * @parent_id: Parent clock identifier to set
1207 *
1208 * Return: 0 if all went well, else returns appropriate error value.
1209 */
1210 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1211 u32 dev_id, u8 clk_id, u8 parent_id)
1212 {
1213 struct ti_sci_info *info;
1214 struct ti_sci_msg_req_set_clock_parent *req;
1215 struct ti_sci_msg_hdr *resp;
1216 struct ti_sci_xfer *xfer;
1217 struct device *dev;
1218 int ret = 0;
1219
1220 if (IS_ERR(handle))
1221 return PTR_ERR(handle);
1222 if (!handle)
1223 return -EINVAL;
1224
1225 info = handle_to_ti_sci_info(handle);
1226 dev = info->dev;
1227
1228 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1229 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1230 sizeof(*req), sizeof(*resp));
1231 if (IS_ERR(xfer)) {
1232 ret = PTR_ERR(xfer);
1233 dev_err(dev, "Message alloc failed(%d)\n", ret);
1234 return ret;
1235 }
1236 req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1237 req->dev_id = dev_id;
1238 req->clk_id = clk_id;
1239 req->parent_id = parent_id;
1240
1241 ret = ti_sci_do_xfer(info, xfer);
1242 if (ret) {
1243 dev_err(dev, "Mbox send fail %d\n", ret);
1244 goto fail;
1245 }
1246
1247 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1248
1249 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1250
1251 fail:
1252 ti_sci_put_one_xfer(&info->minfo, xfer);
1253
1254 return ret;
1255 }
1256
1257 /**
1258 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1259 * @handle: pointer to TI SCI handle
1260 * @dev_id: Device identifier this request is for
1261 * @clk_id: Clock identifier for the device for this request.
1262 * Each device has it's own set of clock inputs. This indexes
1263 * which clock input to modify.
1264 * @parent_id: Current clock parent
1265 *
1266 * Return: 0 if all went well, else returns appropriate error value.
1267 */
1268 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1269 u32 dev_id, u8 clk_id, u8 *parent_id)
1270 {
1271 struct ti_sci_info *info;
1272 struct ti_sci_msg_req_get_clock_parent *req;
1273 struct ti_sci_msg_resp_get_clock_parent *resp;
1274 struct ti_sci_xfer *xfer;
1275 struct device *dev;
1276 int ret = 0;
1277
1278 if (IS_ERR(handle))
1279 return PTR_ERR(handle);
1280 if (!handle || !parent_id)
1281 return -EINVAL;
1282
1283 info = handle_to_ti_sci_info(handle);
1284 dev = info->dev;
1285
1286 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1287 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1288 sizeof(*req), sizeof(*resp));
1289 if (IS_ERR(xfer)) {
1290 ret = PTR_ERR(xfer);
1291 dev_err(dev, "Message alloc failed(%d)\n", ret);
1292 return ret;
1293 }
1294 req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1295 req->dev_id = dev_id;
1296 req->clk_id = clk_id;
1297
1298 ret = ti_sci_do_xfer(info, xfer);
1299 if (ret) {
1300 dev_err(dev, "Mbox send fail %d\n", ret);
1301 goto fail;
1302 }
1303
1304 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1305
1306 if (!ti_sci_is_response_ack(resp))
1307 ret = -ENODEV;
1308 else
1309 *parent_id = resp->parent_id;
1310
1311 fail:
1312 ti_sci_put_one_xfer(&info->minfo, xfer);
1313
1314 return ret;
1315 }
1316
1317 /**
1318 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1319 * @handle: pointer to TI SCI handle
1320 * @dev_id: Device identifier this request is for
1321 * @clk_id: Clock identifier for the device for this request.
1322 * Each device has it's own set of clock inputs. This indexes
1323 * which clock input to modify.
1324 * @num_parents: Returns he number of parents to the current clock.
1325 *
1326 * Return: 0 if all went well, else returns appropriate error value.
1327 */
1328 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1329 u32 dev_id, u8 clk_id,
1330 u8 *num_parents)
1331 {
1332 struct ti_sci_info *info;
1333 struct ti_sci_msg_req_get_clock_num_parents *req;
1334 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1335 struct ti_sci_xfer *xfer;
1336 struct device *dev;
1337 int ret = 0;
1338
1339 if (IS_ERR(handle))
1340 return PTR_ERR(handle);
1341 if (!handle || !num_parents)
1342 return -EINVAL;
1343
1344 info = handle_to_ti_sci_info(handle);
1345 dev = info->dev;
1346
1347 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1348 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1349 sizeof(*req), sizeof(*resp));
1350 if (IS_ERR(xfer)) {
1351 ret = PTR_ERR(xfer);
1352 dev_err(dev, "Message alloc failed(%d)\n", ret);
1353 return ret;
1354 }
1355 req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1356 req->dev_id = dev_id;
1357 req->clk_id = clk_id;
1358
1359 ret = ti_sci_do_xfer(info, xfer);
1360 if (ret) {
1361 dev_err(dev, "Mbox send fail %d\n", ret);
1362 goto fail;
1363 }
1364
1365 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1366
1367 if (!ti_sci_is_response_ack(resp))
1368 ret = -ENODEV;
1369 else
1370 *num_parents = resp->num_parents;
1371
1372 fail:
1373 ti_sci_put_one_xfer(&info->minfo, xfer);
1374
1375 return ret;
1376 }
1377
1378 /**
1379 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1380 * @handle: pointer to TI SCI handle
1381 * @dev_id: Device identifier this request is for
1382 * @clk_id: Clock identifier for the device for this request.
1383 * Each device has it's own set of clock inputs. This indexes
1384 * which clock input to modify.
1385 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1386 * allowable programmed frequency and does not account for clock
1387 * tolerances and jitter.
1388 * @target_freq: The target clock frequency in Hz. A frequency will be
1389 * processed as close to this target frequency as possible.
1390 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1391 * allowable programmed frequency and does not account for clock
1392 * tolerances and jitter.
1393 * @match_freq: Frequency match in Hz response.
1394 *
1395 * Return: 0 if all went well, else returns appropriate error value.
1396 */
1397 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1398 u32 dev_id, u8 clk_id, u64 min_freq,
1399 u64 target_freq, u64 max_freq,
1400 u64 *match_freq)
1401 {
1402 struct ti_sci_info *info;
1403 struct ti_sci_msg_req_query_clock_freq *req;
1404 struct ti_sci_msg_resp_query_clock_freq *resp;
1405 struct ti_sci_xfer *xfer;
1406 struct device *dev;
1407 int ret = 0;
1408
1409 if (IS_ERR(handle))
1410 return PTR_ERR(handle);
1411 if (!handle || !match_freq)
1412 return -EINVAL;
1413
1414 info = handle_to_ti_sci_info(handle);
1415 dev = info->dev;
1416
1417 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1418 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1419 sizeof(*req), sizeof(*resp));
1420 if (IS_ERR(xfer)) {
1421 ret = PTR_ERR(xfer);
1422 dev_err(dev, "Message alloc failed(%d)\n", ret);
1423 return ret;
1424 }
1425 req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1426 req->dev_id = dev_id;
1427 req->clk_id = clk_id;
1428 req->min_freq_hz = min_freq;
1429 req->target_freq_hz = target_freq;
1430 req->max_freq_hz = max_freq;
1431
1432 ret = ti_sci_do_xfer(info, xfer);
1433 if (ret) {
1434 dev_err(dev, "Mbox send fail %d\n", ret);
1435 goto fail;
1436 }
1437
1438 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1439
1440 if (!ti_sci_is_response_ack(resp))
1441 ret = -ENODEV;
1442 else
1443 *match_freq = resp->freq_hz;
1444
1445 fail:
1446 ti_sci_put_one_xfer(&info->minfo, xfer);
1447
1448 return ret;
1449 }
1450
1451 /**
1452 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1453 * @handle: pointer to TI SCI handle
1454 * @dev_id: Device identifier this request is for
1455 * @clk_id: Clock identifier for the device for this request.
1456 * Each device has it's own set of clock inputs. This indexes
1457 * which clock input to modify.
1458 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1459 * allowable programmed frequency and does not account for clock
1460 * tolerances and jitter.
1461 * @target_freq: The target clock frequency in Hz. A frequency will be
1462 * processed as close to this target frequency as possible.
1463 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1464 * allowable programmed frequency and does not account for clock
1465 * tolerances and jitter.
1466 *
1467 * Return: 0 if all went well, else returns appropriate error value.
1468 */
1469 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1470 u32 dev_id, u8 clk_id, u64 min_freq,
1471 u64 target_freq, u64 max_freq)
1472 {
1473 struct ti_sci_info *info;
1474 struct ti_sci_msg_req_set_clock_freq *req;
1475 struct ti_sci_msg_hdr *resp;
1476 struct ti_sci_xfer *xfer;
1477 struct device *dev;
1478 int ret = 0;
1479
1480 if (IS_ERR(handle))
1481 return PTR_ERR(handle);
1482 if (!handle)
1483 return -EINVAL;
1484
1485 info = handle_to_ti_sci_info(handle);
1486 dev = info->dev;
1487
1488 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1489 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1490 sizeof(*req), sizeof(*resp));
1491 if (IS_ERR(xfer)) {
1492 ret = PTR_ERR(xfer);
1493 dev_err(dev, "Message alloc failed(%d)\n", ret);
1494 return ret;
1495 }
1496 req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1497 req->dev_id = dev_id;
1498 req->clk_id = clk_id;
1499 req->min_freq_hz = min_freq;
1500 req->target_freq_hz = target_freq;
1501 req->max_freq_hz = max_freq;
1502
1503 ret = ti_sci_do_xfer(info, xfer);
1504 if (ret) {
1505 dev_err(dev, "Mbox send fail %d\n", ret);
1506 goto fail;
1507 }
1508
1509 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1510
1511 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1512
1513 fail:
1514 ti_sci_put_one_xfer(&info->minfo, xfer);
1515
1516 return ret;
1517 }
1518
1519 /**
1520 * ti_sci_cmd_clk_get_freq() - Get current frequency
1521 * @handle: pointer to TI SCI handle
1522 * @dev_id: Device identifier this request is for
1523 * @clk_id: Clock identifier for the device for this request.
1524 * Each device has it's own set of clock inputs. This indexes
1525 * which clock input to modify.
1526 * @freq: Currently frequency in Hz
1527 *
1528 * Return: 0 if all went well, else returns appropriate error value.
1529 */
1530 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1531 u32 dev_id, u8 clk_id, u64 *freq)
1532 {
1533 struct ti_sci_info *info;
1534 struct ti_sci_msg_req_get_clock_freq *req;
1535 struct ti_sci_msg_resp_get_clock_freq *resp;
1536 struct ti_sci_xfer *xfer;
1537 struct device *dev;
1538 int ret = 0;
1539
1540 if (IS_ERR(handle))
1541 return PTR_ERR(handle);
1542 if (!handle || !freq)
1543 return -EINVAL;
1544
1545 info = handle_to_ti_sci_info(handle);
1546 dev = info->dev;
1547
1548 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1549 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1550 sizeof(*req), sizeof(*resp));
1551 if (IS_ERR(xfer)) {
1552 ret = PTR_ERR(xfer);
1553 dev_err(dev, "Message alloc failed(%d)\n", ret);
1554 return ret;
1555 }
1556 req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1557 req->dev_id = dev_id;
1558 req->clk_id = clk_id;
1559
1560 ret = ti_sci_do_xfer(info, xfer);
1561 if (ret) {
1562 dev_err(dev, "Mbox send fail %d\n", ret);
1563 goto fail;
1564 }
1565
1566 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1567
1568 if (!ti_sci_is_response_ack(resp))
1569 ret = -ENODEV;
1570 else
1571 *freq = resp->freq_hz;
1572
1573 fail:
1574 ti_sci_put_one_xfer(&info->minfo, xfer);
1575
1576 return ret;
1577 }
1578
1579 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1580 {
1581 struct ti_sci_info *info;
1582 struct ti_sci_msg_req_reboot *req;
1583 struct ti_sci_msg_hdr *resp;
1584 struct ti_sci_xfer *xfer;
1585 struct device *dev;
1586 int ret = 0;
1587
1588 if (IS_ERR(handle))
1589 return PTR_ERR(handle);
1590 if (!handle)
1591 return -EINVAL;
1592
1593 info = handle_to_ti_sci_info(handle);
1594 dev = info->dev;
1595
1596 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1597 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1598 sizeof(*req), sizeof(*resp));
1599 if (IS_ERR(xfer)) {
1600 ret = PTR_ERR(xfer);
1601 dev_err(dev, "Message alloc failed(%d)\n", ret);
1602 return ret;
1603 }
1604 req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1605
1606 ret = ti_sci_do_xfer(info, xfer);
1607 if (ret) {
1608 dev_err(dev, "Mbox send fail %d\n", ret);
1609 goto fail;
1610 }
1611
1612 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1613
1614 if (!ti_sci_is_response_ack(resp))
1615 ret = -ENODEV;
1616 else
1617 ret = 0;
1618
1619 fail:
1620 ti_sci_put_one_xfer(&info->minfo, xfer);
1621
1622 return ret;
1623 }
1624
1625 /*
1626 * ti_sci_setup_ops() - Setup the operations structures
1627 * @info: pointer to TISCI pointer
1628 */
1629 static void ti_sci_setup_ops(struct ti_sci_info *info)
1630 {
1631 struct ti_sci_ops *ops = &info->handle.ops;
1632 struct ti_sci_core_ops *core_ops = &ops->core_ops;
1633 struct ti_sci_dev_ops *dops = &ops->dev_ops;
1634 struct ti_sci_clk_ops *cops = &ops->clk_ops;
1635
1636 core_ops->reboot_device = ti_sci_cmd_core_reboot;
1637
1638 dops->get_device = ti_sci_cmd_get_device;
1639 dops->idle_device = ti_sci_cmd_idle_device;
1640 dops->put_device = ti_sci_cmd_put_device;
1641
1642 dops->is_valid = ti_sci_cmd_dev_is_valid;
1643 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
1644 dops->is_idle = ti_sci_cmd_dev_is_idle;
1645 dops->is_stop = ti_sci_cmd_dev_is_stop;
1646 dops->is_on = ti_sci_cmd_dev_is_on;
1647 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
1648 dops->set_device_resets = ti_sci_cmd_set_device_resets;
1649 dops->get_device_resets = ti_sci_cmd_get_device_resets;
1650
1651 cops->get_clock = ti_sci_cmd_get_clock;
1652 cops->idle_clock = ti_sci_cmd_idle_clock;
1653 cops->put_clock = ti_sci_cmd_put_clock;
1654 cops->is_auto = ti_sci_cmd_clk_is_auto;
1655 cops->is_on = ti_sci_cmd_clk_is_on;
1656 cops->is_off = ti_sci_cmd_clk_is_off;
1657
1658 cops->set_parent = ti_sci_cmd_clk_set_parent;
1659 cops->get_parent = ti_sci_cmd_clk_get_parent;
1660 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
1661
1662 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
1663 cops->set_freq = ti_sci_cmd_clk_set_freq;
1664 cops->get_freq = ti_sci_cmd_clk_get_freq;
1665 }
1666
1667 /**
1668 * ti_sci_get_handle() - Get the TI SCI handle for a device
1669 * @dev: Pointer to device for which we want SCI handle
1670 *
1671 * NOTE: The function does not track individual clients of the framework
1672 * and is expected to be maintained by caller of TI SCI protocol library.
1673 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1674 * Return: pointer to handle if successful, else:
1675 * -EPROBE_DEFER if the instance is not ready
1676 * -ENODEV if the required node handler is missing
1677 * -EINVAL if invalid conditions are encountered.
1678 */
1679 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
1680 {
1681 struct device_node *ti_sci_np;
1682 struct list_head *p;
1683 struct ti_sci_handle *handle = NULL;
1684 struct ti_sci_info *info;
1685
1686 if (!dev) {
1687 pr_err("I need a device pointer\n");
1688 return ERR_PTR(-EINVAL);
1689 }
1690 ti_sci_np = of_get_parent(dev->of_node);
1691 if (!ti_sci_np) {
1692 dev_err(dev, "No OF information\n");
1693 return ERR_PTR(-EINVAL);
1694 }
1695
1696 mutex_lock(&ti_sci_list_mutex);
1697 list_for_each(p, &ti_sci_list) {
1698 info = list_entry(p, struct ti_sci_info, node);
1699 if (ti_sci_np == info->dev->of_node) {
1700 handle = &info->handle;
1701 info->users++;
1702 break;
1703 }
1704 }
1705 mutex_unlock(&ti_sci_list_mutex);
1706 of_node_put(ti_sci_np);
1707
1708 if (!handle)
1709 return ERR_PTR(-EPROBE_DEFER);
1710
1711 return handle;
1712 }
1713 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
1714
1715 /**
1716 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1717 * @handle: Handle acquired by ti_sci_get_handle
1718 *
1719 * NOTE: The function does not track individual clients of the framework
1720 * and is expected to be maintained by caller of TI SCI protocol library.
1721 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1722 *
1723 * Return: 0 is successfully released
1724 * if an error pointer was passed, it returns the error value back,
1725 * if null was passed, it returns -EINVAL;
1726 */
1727 int ti_sci_put_handle(const struct ti_sci_handle *handle)
1728 {
1729 struct ti_sci_info *info;
1730
1731 if (IS_ERR(handle))
1732 return PTR_ERR(handle);
1733 if (!handle)
1734 return -EINVAL;
1735
1736 info = handle_to_ti_sci_info(handle);
1737 mutex_lock(&ti_sci_list_mutex);
1738 if (!WARN_ON(!info->users))
1739 info->users--;
1740 mutex_unlock(&ti_sci_list_mutex);
1741
1742 return 0;
1743 }
1744 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
1745
1746 static void devm_ti_sci_release(struct device *dev, void *res)
1747 {
1748 const struct ti_sci_handle **ptr = res;
1749 const struct ti_sci_handle *handle = *ptr;
1750 int ret;
1751
1752 ret = ti_sci_put_handle(handle);
1753 if (ret)
1754 dev_err(dev, "failed to put handle %d\n", ret);
1755 }
1756
1757 /**
1758 * devm_ti_sci_get_handle() - Managed get handle
1759 * @dev: device for which we want SCI handle for.
1760 *
1761 * NOTE: This releases the handle once the device resources are
1762 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1763 * The function does not track individual clients of the framework
1764 * and is expected to be maintained by caller of TI SCI protocol library.
1765 *
1766 * Return: 0 if all went fine, else corresponding error.
1767 */
1768 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
1769 {
1770 const struct ti_sci_handle **ptr;
1771 const struct ti_sci_handle *handle;
1772
1773 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
1774 if (!ptr)
1775 return ERR_PTR(-ENOMEM);
1776 handle = ti_sci_get_handle(dev);
1777
1778 if (!IS_ERR(handle)) {
1779 *ptr = handle;
1780 devres_add(dev, ptr);
1781 } else {
1782 devres_free(ptr);
1783 }
1784
1785 return handle;
1786 }
1787 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
1788
1789 static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
1790 void *cmd)
1791 {
1792 struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
1793 const struct ti_sci_handle *handle = &info->handle;
1794
1795 ti_sci_cmd_core_reboot(handle);
1796
1797 /* call fail OR pass, we should not be here in the first place */
1798 return NOTIFY_BAD;
1799 }
1800
1801 /* Description for K2G */
1802 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
1803 .host_id = 2,
1804 /* Conservative duration */
1805 .max_rx_timeout_ms = 1000,
1806 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1807 .max_msgs = 20,
1808 .max_msg_size = 64,
1809 };
1810
1811 static const struct of_device_id ti_sci_of_match[] = {
1812 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
1813 { /* Sentinel */ },
1814 };
1815 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
1816
1817 static int ti_sci_probe(struct platform_device *pdev)
1818 {
1819 struct device *dev = &pdev->dev;
1820 const struct of_device_id *of_id;
1821 const struct ti_sci_desc *desc;
1822 struct ti_sci_xfer *xfer;
1823 struct ti_sci_info *info = NULL;
1824 struct ti_sci_xfers_info *minfo;
1825 struct mbox_client *cl;
1826 int ret = -EINVAL;
1827 int i;
1828 int reboot = 0;
1829
1830 of_id = of_match_device(ti_sci_of_match, dev);
1831 if (!of_id) {
1832 dev_err(dev, "OF data missing\n");
1833 return -EINVAL;
1834 }
1835 desc = of_id->data;
1836
1837 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1838 if (!info)
1839 return -ENOMEM;
1840
1841 info->dev = dev;
1842 info->desc = desc;
1843 reboot = of_property_read_bool(dev->of_node,
1844 "ti,system-reboot-controller");
1845 INIT_LIST_HEAD(&info->node);
1846 minfo = &info->minfo;
1847
1848 /*
1849 * Pre-allocate messages
1850 * NEVER allocate more than what we can indicate in hdr.seq
1851 * if we have data description bug, force a fix..
1852 */
1853 if (WARN_ON(desc->max_msgs >=
1854 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
1855 return -EINVAL;
1856
1857 minfo->xfer_block = devm_kcalloc(dev,
1858 desc->max_msgs,
1859 sizeof(*minfo->xfer_block),
1860 GFP_KERNEL);
1861 if (!minfo->xfer_block)
1862 return -ENOMEM;
1863
1864 minfo->xfer_alloc_table = devm_kzalloc(dev,
1865 BITS_TO_LONGS(desc->max_msgs)
1866 * sizeof(unsigned long),
1867 GFP_KERNEL);
1868 if (!minfo->xfer_alloc_table)
1869 return -ENOMEM;
1870 bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
1871
1872 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1873 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
1874 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
1875 GFP_KERNEL);
1876 if (!xfer->xfer_buf)
1877 return -ENOMEM;
1878
1879 xfer->tx_message.buf = xfer->xfer_buf;
1880 init_completion(&xfer->done);
1881 }
1882
1883 ret = ti_sci_debugfs_create(pdev, info);
1884 if (ret)
1885 dev_warn(dev, "Failed to create debug file\n");
1886
1887 platform_set_drvdata(pdev, info);
1888
1889 cl = &info->cl;
1890 cl->dev = dev;
1891 cl->tx_block = false;
1892 cl->rx_callback = ti_sci_rx_callback;
1893 cl->knows_txdone = true;
1894
1895 spin_lock_init(&minfo->xfer_lock);
1896 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
1897
1898 info->chan_rx = mbox_request_channel_byname(cl, "rx");
1899 if (IS_ERR(info->chan_rx)) {
1900 ret = PTR_ERR(info->chan_rx);
1901 goto out;
1902 }
1903
1904 info->chan_tx = mbox_request_channel_byname(cl, "tx");
1905 if (IS_ERR(info->chan_tx)) {
1906 ret = PTR_ERR(info->chan_tx);
1907 goto out;
1908 }
1909 ret = ti_sci_cmd_get_revision(info);
1910 if (ret) {
1911 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
1912 goto out;
1913 }
1914
1915 ti_sci_setup_ops(info);
1916
1917 if (reboot) {
1918 info->nb.notifier_call = tisci_reboot_handler;
1919 info->nb.priority = 128;
1920
1921 ret = register_restart_handler(&info->nb);
1922 if (ret) {
1923 dev_err(dev, "reboot registration fail(%d)\n", ret);
1924 return ret;
1925 }
1926 }
1927
1928 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1929 info->handle.version.abi_major, info->handle.version.abi_minor,
1930 info->handle.version.firmware_revision,
1931 info->handle.version.firmware_description);
1932
1933 mutex_lock(&ti_sci_list_mutex);
1934 list_add_tail(&info->node, &ti_sci_list);
1935 mutex_unlock(&ti_sci_list_mutex);
1936
1937 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1938 out:
1939 if (!IS_ERR(info->chan_tx))
1940 mbox_free_channel(info->chan_tx);
1941 if (!IS_ERR(info->chan_rx))
1942 mbox_free_channel(info->chan_rx);
1943 debugfs_remove(info->d);
1944 return ret;
1945 }
1946
1947 static int ti_sci_remove(struct platform_device *pdev)
1948 {
1949 struct ti_sci_info *info;
1950 struct device *dev = &pdev->dev;
1951 int ret = 0;
1952
1953 of_platform_depopulate(dev);
1954
1955 info = platform_get_drvdata(pdev);
1956
1957 if (info->nb.notifier_call)
1958 unregister_restart_handler(&info->nb);
1959
1960 mutex_lock(&ti_sci_list_mutex);
1961 if (info->users)
1962 ret = -EBUSY;
1963 else
1964 list_del(&info->node);
1965 mutex_unlock(&ti_sci_list_mutex);
1966
1967 if (!ret) {
1968 ti_sci_debugfs_destroy(pdev, info);
1969
1970 /* Safe to free channels since no more users */
1971 mbox_free_channel(info->chan_tx);
1972 mbox_free_channel(info->chan_rx);
1973 }
1974
1975 return ret;
1976 }
1977
1978 static struct platform_driver ti_sci_driver = {
1979 .probe = ti_sci_probe,
1980 .remove = ti_sci_remove,
1981 .driver = {
1982 .name = "ti-sci",
1983 .of_match_table = of_match_ptr(ti_sci_of_match),
1984 },
1985 };
1986 module_platform_driver(ti_sci_driver);
1987
1988 MODULE_LICENSE("GPL v2");
1989 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1990 MODULE_AUTHOR("Nishanth Menon");
1991 MODULE_ALIAS("platform:ti-sci");