xref: /openbmc/linux/drivers/firmware/ti_sci.c (revision e69a3553)
11e0a6014SLokesh Vutla // SPDX-License-Identifier: GPL-2.0
2aa276781SNishanth Menon /*
3aa276781SNishanth Menon  * Texas Instruments System Control Interface Protocol Driver
4aa276781SNishanth Menon  *
5aa276781SNishanth Menon  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
6aa276781SNishanth Menon  *	Nishanth Menon
7aa276781SNishanth Menon  */
8aa276781SNishanth Menon 
9aa276781SNishanth Menon #define pr_fmt(fmt) "%s: " fmt, __func__
10aa276781SNishanth Menon 
11aa276781SNishanth Menon #include <linux/bitmap.h>
12aa276781SNishanth Menon #include <linux/debugfs.h>
13aa276781SNishanth Menon #include <linux/export.h>
14aa276781SNishanth Menon #include <linux/io.h>
15aa276781SNishanth Menon #include <linux/kernel.h>
16aa276781SNishanth Menon #include <linux/mailbox_client.h>
17aa276781SNishanth Menon #include <linux/module.h>
18aa276781SNishanth Menon #include <linux/of_device.h>
19aa276781SNishanth Menon #include <linux/semaphore.h>
20aa276781SNishanth Menon #include <linux/slab.h>
21aa276781SNishanth Menon #include <linux/soc/ti/ti-msgmgr.h>
22aa276781SNishanth Menon #include <linux/soc/ti/ti_sci_protocol.h>
23912cffb4SNishanth Menon #include <linux/reboot.h>
24aa276781SNishanth Menon 
25aa276781SNishanth Menon #include "ti_sci.h"
26aa276781SNishanth Menon 
27aa276781SNishanth Menon /* List of all TI SCI devices active in system */
28aa276781SNishanth Menon static LIST_HEAD(ti_sci_list);
29aa276781SNishanth Menon /* Protection for the entire list */
30aa276781SNishanth Menon static DEFINE_MUTEX(ti_sci_list_mutex);
31aa276781SNishanth Menon 
32aa276781SNishanth Menon /**
33aa276781SNishanth Menon  * struct ti_sci_xfer - Structure representing a message flow
34aa276781SNishanth Menon  * @tx_message:	Transmit message
35aa276781SNishanth Menon  * @rx_len:	Receive message length
36aa276781SNishanth Menon  * @xfer_buf:	Preallocated buffer to store receive message
37aa276781SNishanth Menon  *		Since we work with request-ACK protocol, we can
38aa276781SNishanth Menon  *		reuse the same buffer for the rx path as we
39aa276781SNishanth Menon  *		use for the tx path.
40aa276781SNishanth Menon  * @done:	completion event
41aa276781SNishanth Menon  */
42aa276781SNishanth Menon struct ti_sci_xfer {
43aa276781SNishanth Menon 	struct ti_msgmgr_message tx_message;
44aa276781SNishanth Menon 	u8 rx_len;
45aa276781SNishanth Menon 	u8 *xfer_buf;
46aa276781SNishanth Menon 	struct completion done;
47aa276781SNishanth Menon };
48aa276781SNishanth Menon 
49aa276781SNishanth Menon /**
50aa276781SNishanth Menon  * struct ti_sci_xfers_info - Structure to manage transfer information
51aa276781SNishanth Menon  * @sem_xfer_count:	Counting Semaphore for managing max simultaneous
52aa276781SNishanth Menon  *			Messages.
53aa276781SNishanth Menon  * @xfer_block:		Preallocated Message array
54aa276781SNishanth Menon  * @xfer_alloc_table:	Bitmap table for allocated messages.
55aa276781SNishanth Menon  *			Index of this bitmap table is also used for message
56aa276781SNishanth Menon  *			sequence identifier.
57aa276781SNishanth Menon  * @xfer_lock:		Protection for message allocation
58aa276781SNishanth Menon  */
59aa276781SNishanth Menon struct ti_sci_xfers_info {
60aa276781SNishanth Menon 	struct semaphore sem_xfer_count;
61aa276781SNishanth Menon 	struct ti_sci_xfer *xfer_block;
62aa276781SNishanth Menon 	unsigned long *xfer_alloc_table;
63aa276781SNishanth Menon 	/* protect transfer allocation */
64aa276781SNishanth Menon 	spinlock_t xfer_lock;
65aa276781SNishanth Menon };
66aa276781SNishanth Menon 
67aa276781SNishanth Menon /**
68aa276781SNishanth Menon  * struct ti_sci_desc - Description of SoC integration
69e69a3553SNishanth Menon  * @default_host_id:	Host identifier representing the compute entity
70aa276781SNishanth Menon  * @max_rx_timeout_ms:	Timeout for communication with SoC (in Milliseconds)
71aa276781SNishanth Menon  * @max_msgs: Maximum number of messages that can be pending
72aa276781SNishanth Menon  *		  simultaneously in the system
73aa276781SNishanth Menon  * @max_msg_size: Maximum size of data per message that can be handled.
74aa276781SNishanth Menon  */
75aa276781SNishanth Menon struct ti_sci_desc {
76e69a3553SNishanth Menon 	u8 default_host_id;
77aa276781SNishanth Menon 	int max_rx_timeout_ms;
78aa276781SNishanth Menon 	int max_msgs;
79aa276781SNishanth Menon 	int max_msg_size;
80aa276781SNishanth Menon };
81aa276781SNishanth Menon 
82aa276781SNishanth Menon /**
83aa276781SNishanth Menon  * struct ti_sci_info - Structure representing a TI SCI instance
84aa276781SNishanth Menon  * @dev:	Device pointer
85aa276781SNishanth Menon  * @desc:	SoC description for this instance
86912cffb4SNishanth Menon  * @nb:	Reboot Notifier block
87aa276781SNishanth Menon  * @d:		Debugfs file entry
88aa276781SNishanth Menon  * @debug_region: Memory region where the debug message are available
89aa276781SNishanth Menon  * @debug_region_size: Debug region size
90aa276781SNishanth Menon  * @debug_buffer: Buffer allocated to copy debug messages.
91aa276781SNishanth Menon  * @handle:	Instance of TI SCI handle to send to clients.
92aa276781SNishanth Menon  * @cl:		Mailbox Client
93aa276781SNishanth Menon  * @chan_tx:	Transmit mailbox channel
94aa276781SNishanth Menon  * @chan_rx:	Receive mailbox channel
95aa276781SNishanth Menon  * @minfo:	Message info
96aa276781SNishanth Menon  * @node:	list head
97e69a3553SNishanth Menon  * @host_id:	Host ID
98aa276781SNishanth Menon  * @users:	Number of users of this instance
99aa276781SNishanth Menon  */
100aa276781SNishanth Menon struct ti_sci_info {
101aa276781SNishanth Menon 	struct device *dev;
102912cffb4SNishanth Menon 	struct notifier_block nb;
103aa276781SNishanth Menon 	const struct ti_sci_desc *desc;
104aa276781SNishanth Menon 	struct dentry *d;
105aa276781SNishanth Menon 	void __iomem *debug_region;
106aa276781SNishanth Menon 	char *debug_buffer;
107aa276781SNishanth Menon 	size_t debug_region_size;
108aa276781SNishanth Menon 	struct ti_sci_handle handle;
109aa276781SNishanth Menon 	struct mbox_client cl;
110aa276781SNishanth Menon 	struct mbox_chan *chan_tx;
111aa276781SNishanth Menon 	struct mbox_chan *chan_rx;
112aa276781SNishanth Menon 	struct ti_sci_xfers_info minfo;
113aa276781SNishanth Menon 	struct list_head node;
114e69a3553SNishanth Menon 	u8 host_id;
115aa276781SNishanth Menon 	/* protected by ti_sci_list_mutex */
116aa276781SNishanth Menon 	int users;
117912cffb4SNishanth Menon 
118aa276781SNishanth Menon };
119aa276781SNishanth Menon 
120aa276781SNishanth Menon #define cl_to_ti_sci_info(c)	container_of(c, struct ti_sci_info, cl)
121aa276781SNishanth Menon #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
122912cffb4SNishanth Menon #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
123aa276781SNishanth Menon 
124aa276781SNishanth Menon #ifdef CONFIG_DEBUG_FS
125aa276781SNishanth Menon 
126aa276781SNishanth Menon /**
127aa276781SNishanth Menon  * ti_sci_debug_show() - Helper to dump the debug log
128aa276781SNishanth Menon  * @s:	sequence file pointer
129aa276781SNishanth Menon  * @unused:	unused.
130aa276781SNishanth Menon  *
131aa276781SNishanth Menon  * Return: 0
132aa276781SNishanth Menon  */
133aa276781SNishanth Menon static int ti_sci_debug_show(struct seq_file *s, void *unused)
134aa276781SNishanth Menon {
135aa276781SNishanth Menon 	struct ti_sci_info *info = s->private;
136aa276781SNishanth Menon 
137aa276781SNishanth Menon 	memcpy_fromio(info->debug_buffer, info->debug_region,
138aa276781SNishanth Menon 		      info->debug_region_size);
139aa276781SNishanth Menon 	/*
140aa276781SNishanth Menon 	 * We don't trust firmware to leave NULL terminated last byte (hence
141aa276781SNishanth Menon 	 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142aa276781SNishanth Menon 	 * specific data format for debug messages, We just present the data
143aa276781SNishanth Menon 	 * in the buffer as is - we expect the messages to be self explanatory.
144aa276781SNishanth Menon 	 */
145aa276781SNishanth Menon 	seq_puts(s, info->debug_buffer);
146aa276781SNishanth Menon 	return 0;
147aa276781SNishanth Menon }
148aa276781SNishanth Menon 
149aa276781SNishanth Menon /**
150aa276781SNishanth Menon  * ti_sci_debug_open() - debug file open
151aa276781SNishanth Menon  * @inode:	inode pointer
152aa276781SNishanth Menon  * @file:	file pointer
153aa276781SNishanth Menon  *
154aa276781SNishanth Menon  * Return: result of single_open
155aa276781SNishanth Menon  */
156aa276781SNishanth Menon static int ti_sci_debug_open(struct inode *inode, struct file *file)
157aa276781SNishanth Menon {
158aa276781SNishanth Menon 	return single_open(file, ti_sci_debug_show, inode->i_private);
159aa276781SNishanth Menon }
160aa276781SNishanth Menon 
161aa276781SNishanth Menon /* log file operations */
162aa276781SNishanth Menon static const struct file_operations ti_sci_debug_fops = {
163aa276781SNishanth Menon 	.open = ti_sci_debug_open,
164aa276781SNishanth Menon 	.read = seq_read,
165aa276781SNishanth Menon 	.llseek = seq_lseek,
166aa276781SNishanth Menon 	.release = single_release,
167aa276781SNishanth Menon };
168aa276781SNishanth Menon 
169aa276781SNishanth Menon /**
170aa276781SNishanth Menon  * ti_sci_debugfs_create() - Create log debug file
171aa276781SNishanth Menon  * @pdev:	platform device pointer
172aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
173aa276781SNishanth Menon  *
174aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
175aa276781SNishanth Menon  */
176aa276781SNishanth Menon static int ti_sci_debugfs_create(struct platform_device *pdev,
177aa276781SNishanth Menon 				 struct ti_sci_info *info)
178aa276781SNishanth Menon {
179aa276781SNishanth Menon 	struct device *dev = &pdev->dev;
180aa276781SNishanth Menon 	struct resource *res;
181aa276781SNishanth Menon 	char debug_name[50] = "ti_sci_debug@";
182aa276781SNishanth Menon 
183aa276781SNishanth Menon 	/* Debug region is optional */
184aa276781SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
185aa276781SNishanth Menon 					   "debug_messages");
186aa276781SNishanth Menon 	info->debug_region = devm_ioremap_resource(dev, res);
187aa276781SNishanth Menon 	if (IS_ERR(info->debug_region))
188aa276781SNishanth Menon 		return 0;
189aa276781SNishanth Menon 	info->debug_region_size = resource_size(res);
190aa276781SNishanth Menon 
191aa276781SNishanth Menon 	info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
192aa276781SNishanth Menon 					  sizeof(char), GFP_KERNEL);
193aa276781SNishanth Menon 	if (!info->debug_buffer)
194aa276781SNishanth Menon 		return -ENOMEM;
195aa276781SNishanth Menon 	/* Setup NULL termination */
196aa276781SNishanth Menon 	info->debug_buffer[info->debug_region_size] = 0;
197aa276781SNishanth Menon 
198aa276781SNishanth Menon 	info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
19976cefef8SArnd Bergmann 					      sizeof(debug_name) -
20076cefef8SArnd Bergmann 					      sizeof("ti_sci_debug@")),
201aa276781SNishanth Menon 				      0444, NULL, info, &ti_sci_debug_fops);
202aa276781SNishanth Menon 	if (IS_ERR(info->d))
203aa276781SNishanth Menon 		return PTR_ERR(info->d);
204aa276781SNishanth Menon 
205aa276781SNishanth Menon 	dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
206aa276781SNishanth Menon 		info->debug_region, info->debug_region_size, res);
207aa276781SNishanth Menon 	return 0;
208aa276781SNishanth Menon }
209aa276781SNishanth Menon 
210aa276781SNishanth Menon /**
211aa276781SNishanth Menon  * ti_sci_debugfs_destroy() - clean up log debug file
212aa276781SNishanth Menon  * @pdev:	platform device pointer
213aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
214aa276781SNishanth Menon  */
215aa276781SNishanth Menon static void ti_sci_debugfs_destroy(struct platform_device *pdev,
216aa276781SNishanth Menon 				   struct ti_sci_info *info)
217aa276781SNishanth Menon {
218aa276781SNishanth Menon 	if (IS_ERR(info->debug_region))
219aa276781SNishanth Menon 		return;
220aa276781SNishanth Menon 
221aa276781SNishanth Menon 	debugfs_remove(info->d);
222aa276781SNishanth Menon }
223aa276781SNishanth Menon #else /* CONFIG_DEBUG_FS */
224aa276781SNishanth Menon static inline int ti_sci_debugfs_create(struct platform_device *dev,
225aa276781SNishanth Menon 					struct ti_sci_info *info)
226aa276781SNishanth Menon {
227aa276781SNishanth Menon 	return 0;
228aa276781SNishanth Menon }
229aa276781SNishanth Menon 
230aa276781SNishanth Menon static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
231aa276781SNishanth Menon 					  struct ti_sci_info *info)
232aa276781SNishanth Menon {
233aa276781SNishanth Menon }
234aa276781SNishanth Menon #endif /* CONFIG_DEBUG_FS */
235aa276781SNishanth Menon 
236aa276781SNishanth Menon /**
237aa276781SNishanth Menon  * ti_sci_dump_header_dbg() - Helper to dump a message header.
238aa276781SNishanth Menon  * @dev:	Device pointer corresponding to the SCI entity
239aa276781SNishanth Menon  * @hdr:	pointer to header.
240aa276781SNishanth Menon  */
241aa276781SNishanth Menon static inline void ti_sci_dump_header_dbg(struct device *dev,
242aa276781SNishanth Menon 					  struct ti_sci_msg_hdr *hdr)
243aa276781SNishanth Menon {
244aa276781SNishanth Menon 	dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
245aa276781SNishanth Menon 		hdr->type, hdr->host, hdr->seq, hdr->flags);
246aa276781SNishanth Menon }
247aa276781SNishanth Menon 
248aa276781SNishanth Menon /**
249aa276781SNishanth Menon  * ti_sci_rx_callback() - mailbox client callback for receive messages
250aa276781SNishanth Menon  * @cl:	client pointer
251aa276781SNishanth Menon  * @m:	mailbox message
252aa276781SNishanth Menon  *
253aa276781SNishanth Menon  * Processes one received message to appropriate transfer information and
254aa276781SNishanth Menon  * signals completion of the transfer.
255aa276781SNishanth Menon  *
256aa276781SNishanth Menon  * NOTE: This function will be invoked in IRQ context, hence should be
257aa276781SNishanth Menon  * as optimal as possible.
258aa276781SNishanth Menon  */
259aa276781SNishanth Menon static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
260aa276781SNishanth Menon {
261aa276781SNishanth Menon 	struct ti_sci_info *info = cl_to_ti_sci_info(cl);
262aa276781SNishanth Menon 	struct device *dev = info->dev;
263aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo = &info->minfo;
264aa276781SNishanth Menon 	struct ti_msgmgr_message *mbox_msg = m;
265aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
266aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
267aa276781SNishanth Menon 	u8 xfer_id;
268aa276781SNishanth Menon 
269aa276781SNishanth Menon 	xfer_id = hdr->seq;
270aa276781SNishanth Menon 
271aa276781SNishanth Menon 	/*
272aa276781SNishanth Menon 	 * Are we even expecting this?
273aa276781SNishanth Menon 	 * NOTE: barriers were implicit in locks used for modifying the bitmap
274aa276781SNishanth Menon 	 */
275aa276781SNishanth Menon 	if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
276aa276781SNishanth Menon 		dev_err(dev, "Message for %d is not expected!\n", xfer_id);
277aa276781SNishanth Menon 		return;
278aa276781SNishanth Menon 	}
279aa276781SNishanth Menon 
280aa276781SNishanth Menon 	xfer = &minfo->xfer_block[xfer_id];
281aa276781SNishanth Menon 
282aa276781SNishanth Menon 	/* Is the message of valid length? */
283aa276781SNishanth Menon 	if (mbox_msg->len > info->desc->max_msg_size) {
284bd0fa74eSNishanth Menon 		dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
285aa276781SNishanth Menon 			mbox_msg->len, info->desc->max_msg_size);
286aa276781SNishanth Menon 		ti_sci_dump_header_dbg(dev, hdr);
287aa276781SNishanth Menon 		return;
288aa276781SNishanth Menon 	}
289aa276781SNishanth Menon 	if (mbox_msg->len < xfer->rx_len) {
290bd0fa74eSNishanth Menon 		dev_err(dev, "Recv xfer %zu < expected %d length\n",
291aa276781SNishanth Menon 			mbox_msg->len, xfer->rx_len);
292aa276781SNishanth Menon 		ti_sci_dump_header_dbg(dev, hdr);
293aa276781SNishanth Menon 		return;
294aa276781SNishanth Menon 	}
295aa276781SNishanth Menon 
296aa276781SNishanth Menon 	ti_sci_dump_header_dbg(dev, hdr);
297aa276781SNishanth Menon 	/* Take a copy to the rx buffer.. */
298aa276781SNishanth Menon 	memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
299aa276781SNishanth Menon 	complete(&xfer->done);
300aa276781SNishanth Menon }
301aa276781SNishanth Menon 
302aa276781SNishanth Menon /**
303aa276781SNishanth Menon  * ti_sci_get_one_xfer() - Allocate one message
304aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
305aa276781SNishanth Menon  * @msg_type:	Message type
306aa276781SNishanth Menon  * @msg_flags:	Flag to set for the message
307aa276781SNishanth Menon  * @tx_message_size: transmit message size
308aa276781SNishanth Menon  * @rx_message_size: receive message size
309aa276781SNishanth Menon  *
310aa276781SNishanth Menon  * Helper function which is used by various command functions that are
311aa276781SNishanth Menon  * exposed to clients of this driver for allocating a message traffic event.
312aa276781SNishanth Menon  *
313aa276781SNishanth Menon  * This function can sleep depending on pending requests already in the system
314aa276781SNishanth Menon  * for the SCI entity. Further, this also holds a spinlock to maintain integrity
315aa276781SNishanth Menon  * of internal data structures.
316aa276781SNishanth Menon  *
317aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
318aa276781SNishanth Menon  */
319aa276781SNishanth Menon static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
320aa276781SNishanth Menon 					       u16 msg_type, u32 msg_flags,
321aa276781SNishanth Menon 					       size_t tx_message_size,
322aa276781SNishanth Menon 					       size_t rx_message_size)
323aa276781SNishanth Menon {
324aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo = &info->minfo;
325aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
326aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr;
327aa276781SNishanth Menon 	unsigned long flags;
328aa276781SNishanth Menon 	unsigned long bit_pos;
329aa276781SNishanth Menon 	u8 xfer_id;
330aa276781SNishanth Menon 	int ret;
331aa276781SNishanth Menon 	int timeout;
332aa276781SNishanth Menon 
333aa276781SNishanth Menon 	/* Ensure we have sane transfer sizes */
334aa276781SNishanth Menon 	if (rx_message_size > info->desc->max_msg_size ||
335aa276781SNishanth Menon 	    tx_message_size > info->desc->max_msg_size ||
336aa276781SNishanth Menon 	    rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
337aa276781SNishanth Menon 		return ERR_PTR(-ERANGE);
338aa276781SNishanth Menon 
339aa276781SNishanth Menon 	/*
340aa276781SNishanth Menon 	 * Ensure we have only controlled number of pending messages.
341aa276781SNishanth Menon 	 * Ideally, we might just have to wait a single message, be
342aa276781SNishanth Menon 	 * conservative and wait 5 times that..
343aa276781SNishanth Menon 	 */
344aa276781SNishanth Menon 	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
345aa276781SNishanth Menon 	ret = down_timeout(&minfo->sem_xfer_count, timeout);
346aa276781SNishanth Menon 	if (ret < 0)
347aa276781SNishanth Menon 		return ERR_PTR(ret);
348aa276781SNishanth Menon 
349aa276781SNishanth Menon 	/* Keep the locked section as small as possible */
350aa276781SNishanth Menon 	spin_lock_irqsave(&minfo->xfer_lock, flags);
351aa276781SNishanth Menon 	bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
352aa276781SNishanth Menon 				      info->desc->max_msgs);
353aa276781SNishanth Menon 	set_bit(bit_pos, minfo->xfer_alloc_table);
354aa276781SNishanth Menon 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
355aa276781SNishanth Menon 
356aa276781SNishanth Menon 	/*
357aa276781SNishanth Menon 	 * We already ensured in probe that we can have max messages that can
358aa276781SNishanth Menon 	 * fit in  hdr.seq - NOTE: this improves access latencies
359aa276781SNishanth Menon 	 * to predictable O(1) access, BUT, it opens us to risk if
360aa276781SNishanth Menon 	 * remote misbehaves with corrupted message sequence responses.
361aa276781SNishanth Menon 	 * If that happens, we are going to be messed up anyways..
362aa276781SNishanth Menon 	 */
363aa276781SNishanth Menon 	xfer_id = (u8)bit_pos;
364aa276781SNishanth Menon 
365aa276781SNishanth Menon 	xfer = &minfo->xfer_block[xfer_id];
366aa276781SNishanth Menon 
367aa276781SNishanth Menon 	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368aa276781SNishanth Menon 	xfer->tx_message.len = tx_message_size;
369aa276781SNishanth Menon 	xfer->rx_len = (u8)rx_message_size;
370aa276781SNishanth Menon 
371aa276781SNishanth Menon 	reinit_completion(&xfer->done);
372aa276781SNishanth Menon 
373aa276781SNishanth Menon 	hdr->seq = xfer_id;
374aa276781SNishanth Menon 	hdr->type = msg_type;
375e69a3553SNishanth Menon 	hdr->host = info->host_id;
376aa276781SNishanth Menon 	hdr->flags = msg_flags;
377aa276781SNishanth Menon 
378aa276781SNishanth Menon 	return xfer;
379aa276781SNishanth Menon }
380aa276781SNishanth Menon 
381aa276781SNishanth Menon /**
382aa276781SNishanth Menon  * ti_sci_put_one_xfer() - Release a message
383aa276781SNishanth Menon  * @minfo:	transfer info pointer
384aa276781SNishanth Menon  * @xfer:	message that was reserved by ti_sci_get_one_xfer
385aa276781SNishanth Menon  *
386aa276781SNishanth Menon  * This holds a spinlock to maintain integrity of internal data structures.
387aa276781SNishanth Menon  */
388aa276781SNishanth Menon static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
389aa276781SNishanth Menon 				struct ti_sci_xfer *xfer)
390aa276781SNishanth Menon {
391aa276781SNishanth Menon 	unsigned long flags;
392aa276781SNishanth Menon 	struct ti_sci_msg_hdr *hdr;
393aa276781SNishanth Menon 	u8 xfer_id;
394aa276781SNishanth Menon 
395aa276781SNishanth Menon 	hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
396aa276781SNishanth Menon 	xfer_id = hdr->seq;
397aa276781SNishanth Menon 
398aa276781SNishanth Menon 	/*
399aa276781SNishanth Menon 	 * Keep the locked section as small as possible
400aa276781SNishanth Menon 	 * NOTE: we might escape with smp_mb and no lock here..
401aa276781SNishanth Menon 	 * but just be conservative and symmetric.
402aa276781SNishanth Menon 	 */
403aa276781SNishanth Menon 	spin_lock_irqsave(&minfo->xfer_lock, flags);
404aa276781SNishanth Menon 	clear_bit(xfer_id, minfo->xfer_alloc_table);
405aa276781SNishanth Menon 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
406aa276781SNishanth Menon 
407aa276781SNishanth Menon 	/* Increment the count for the next user to get through */
408aa276781SNishanth Menon 	up(&minfo->sem_xfer_count);
409aa276781SNishanth Menon }
410aa276781SNishanth Menon 
411aa276781SNishanth Menon /**
412aa276781SNishanth Menon  * ti_sci_do_xfer() - Do one transfer
413aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
414aa276781SNishanth Menon  * @xfer:	Transfer to initiate and wait for response
415aa276781SNishanth Menon  *
416aa276781SNishanth Menon  * Return: -ETIMEDOUT in case of no response, if transmit error,
417aa276781SNishanth Menon  *	   return corresponding error, else if all goes well,
418aa276781SNishanth Menon  *	   return 0.
419aa276781SNishanth Menon  */
420aa276781SNishanth Menon static inline int ti_sci_do_xfer(struct ti_sci_info *info,
421aa276781SNishanth Menon 				 struct ti_sci_xfer *xfer)
422aa276781SNishanth Menon {
423aa276781SNishanth Menon 	int ret;
424aa276781SNishanth Menon 	int timeout;
425aa276781SNishanth Menon 	struct device *dev = info->dev;
426aa276781SNishanth Menon 
427aa276781SNishanth Menon 	ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
428aa276781SNishanth Menon 	if (ret < 0)
429aa276781SNishanth Menon 		return ret;
430aa276781SNishanth Menon 
431aa276781SNishanth Menon 	ret = 0;
432aa276781SNishanth Menon 
433aa276781SNishanth Menon 	/* And we wait for the response. */
434aa276781SNishanth Menon 	timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
435aa276781SNishanth Menon 	if (!wait_for_completion_timeout(&xfer->done, timeout)) {
436595f3a9dSHelge Deller 		dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
437aa276781SNishanth Menon 			(void *)_RET_IP_);
438aa276781SNishanth Menon 		ret = -ETIMEDOUT;
439aa276781SNishanth Menon 	}
440aa276781SNishanth Menon 	/*
441aa276781SNishanth Menon 	 * NOTE: we might prefer not to need the mailbox ticker to manage the
442aa276781SNishanth Menon 	 * transfer queueing since the protocol layer queues things by itself.
443aa276781SNishanth Menon 	 * Unfortunately, we have to kick the mailbox framework after we have
444aa276781SNishanth Menon 	 * received our message.
445aa276781SNishanth Menon 	 */
446aa276781SNishanth Menon 	mbox_client_txdone(info->chan_tx, ret);
447aa276781SNishanth Menon 
448aa276781SNishanth Menon 	return ret;
449aa276781SNishanth Menon }
450aa276781SNishanth Menon 
451aa276781SNishanth Menon /**
452aa276781SNishanth Menon  * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
453aa276781SNishanth Menon  * @info:	Pointer to SCI entity information
454aa276781SNishanth Menon  *
455aa276781SNishanth Menon  * Updates the SCI information in the internal data structure.
456aa276781SNishanth Menon  *
457aa276781SNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
458aa276781SNishanth Menon  */
459aa276781SNishanth Menon static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
460aa276781SNishanth Menon {
461aa276781SNishanth Menon 	struct device *dev = info->dev;
462aa276781SNishanth Menon 	struct ti_sci_handle *handle = &info->handle;
463aa276781SNishanth Menon 	struct ti_sci_version_info *ver = &handle->version;
464aa276781SNishanth Menon 	struct ti_sci_msg_resp_version *rev_info;
465aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
466aa276781SNishanth Menon 	int ret;
467aa276781SNishanth Menon 
468aa276781SNishanth Menon 	/* No need to setup flags since it is expected to respond */
469aa276781SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
470aa276781SNishanth Menon 				   0x0, sizeof(struct ti_sci_msg_hdr),
471aa276781SNishanth Menon 				   sizeof(*rev_info));
472aa276781SNishanth Menon 	if (IS_ERR(xfer)) {
473aa276781SNishanth Menon 		ret = PTR_ERR(xfer);
474aa276781SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
475aa276781SNishanth Menon 		return ret;
476aa276781SNishanth Menon 	}
477aa276781SNishanth Menon 
478aa276781SNishanth Menon 	rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
479aa276781SNishanth Menon 
480aa276781SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
481aa276781SNishanth Menon 	if (ret) {
482aa276781SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
483aa276781SNishanth Menon 		goto fail;
484aa276781SNishanth Menon 	}
485aa276781SNishanth Menon 
486aa276781SNishanth Menon 	ver->abi_major = rev_info->abi_major;
487aa276781SNishanth Menon 	ver->abi_minor = rev_info->abi_minor;
488aa276781SNishanth Menon 	ver->firmware_revision = rev_info->firmware_revision;
489aa276781SNishanth Menon 	strncpy(ver->firmware_description, rev_info->firmware_description,
490aa276781SNishanth Menon 		sizeof(ver->firmware_description));
491aa276781SNishanth Menon 
492aa276781SNishanth Menon fail:
493aa276781SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
494aa276781SNishanth Menon 	return ret;
495aa276781SNishanth Menon }
496aa276781SNishanth Menon 
497aa276781SNishanth Menon /**
4989e7d756dSNishanth Menon  * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
4999e7d756dSNishanth Menon  * @r:	pointer to response buffer
5009e7d756dSNishanth Menon  *
5019e7d756dSNishanth Menon  * Return: true if the response was an ACK, else returns false.
5029e7d756dSNishanth Menon  */
5039e7d756dSNishanth Menon static inline bool ti_sci_is_response_ack(void *r)
5049e7d756dSNishanth Menon {
5059e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *hdr = r;
5069e7d756dSNishanth Menon 
5079e7d756dSNishanth Menon 	return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
5089e7d756dSNishanth Menon }
5099e7d756dSNishanth Menon 
5109e7d756dSNishanth Menon /**
5119e7d756dSNishanth Menon  * ti_sci_set_device_state() - Set device state helper
5129e7d756dSNishanth Menon  * @handle:	pointer to TI SCI handle
5139e7d756dSNishanth Menon  * @id:		Device identifier
5149e7d756dSNishanth Menon  * @flags:	flags to setup for the device
5159e7d756dSNishanth Menon  * @state:	State to move the device to
5169e7d756dSNishanth Menon  *
5179e7d756dSNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
5189e7d756dSNishanth Menon  */
5199e7d756dSNishanth Menon static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
5209e7d756dSNishanth Menon 				   u32 id, u32 flags, u8 state)
5219e7d756dSNishanth Menon {
5229e7d756dSNishanth Menon 	struct ti_sci_info *info;
5239e7d756dSNishanth Menon 	struct ti_sci_msg_req_set_device_state *req;
5249e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *resp;
5259e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
5269e7d756dSNishanth Menon 	struct device *dev;
5279e7d756dSNishanth Menon 	int ret = 0;
5289e7d756dSNishanth Menon 
5299e7d756dSNishanth Menon 	if (IS_ERR(handle))
5309e7d756dSNishanth Menon 		return PTR_ERR(handle);
5319e7d756dSNishanth Menon 	if (!handle)
5329e7d756dSNishanth Menon 		return -EINVAL;
5339e7d756dSNishanth Menon 
5349e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
5359e7d756dSNishanth Menon 	dev = info->dev;
5369e7d756dSNishanth Menon 
5379e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
5389e7d756dSNishanth Menon 				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
5399e7d756dSNishanth Menon 				   sizeof(*req), sizeof(*resp));
5409e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
5419e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
5429e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
5439e7d756dSNishanth Menon 		return ret;
5449e7d756dSNishanth Menon 	}
5459e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
5469e7d756dSNishanth Menon 	req->id = id;
5479e7d756dSNishanth Menon 	req->state = state;
5489e7d756dSNishanth Menon 
5499e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
5509e7d756dSNishanth Menon 	if (ret) {
5519e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
5529e7d756dSNishanth Menon 		goto fail;
5539e7d756dSNishanth Menon 	}
5549e7d756dSNishanth Menon 
5559e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
5569e7d756dSNishanth Menon 
5579e7d756dSNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
5589e7d756dSNishanth Menon 
5599e7d756dSNishanth Menon fail:
5609e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
5619e7d756dSNishanth Menon 
5629e7d756dSNishanth Menon 	return ret;
5639e7d756dSNishanth Menon }
5649e7d756dSNishanth Menon 
5659e7d756dSNishanth Menon /**
5669e7d756dSNishanth Menon  * ti_sci_get_device_state() - Get device state helper
5679e7d756dSNishanth Menon  * @handle:	Handle to the device
5689e7d756dSNishanth Menon  * @id:		Device Identifier
5699e7d756dSNishanth Menon  * @clcnt:	Pointer to Context Loss Count
5709e7d756dSNishanth Menon  * @resets:	pointer to resets
5719e7d756dSNishanth Menon  * @p_state:	pointer to p_state
5729e7d756dSNishanth Menon  * @c_state:	pointer to c_state
5739e7d756dSNishanth Menon  *
5749e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
5759e7d756dSNishanth Menon  */
5769e7d756dSNishanth Menon static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
5779e7d756dSNishanth Menon 				   u32 id,  u32 *clcnt,  u32 *resets,
5789e7d756dSNishanth Menon 				    u8 *p_state,  u8 *c_state)
5799e7d756dSNishanth Menon {
5809e7d756dSNishanth Menon 	struct ti_sci_info *info;
5819e7d756dSNishanth Menon 	struct ti_sci_msg_req_get_device_state *req;
5829e7d756dSNishanth Menon 	struct ti_sci_msg_resp_get_device_state *resp;
5839e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
5849e7d756dSNishanth Menon 	struct device *dev;
5859e7d756dSNishanth Menon 	int ret = 0;
5869e7d756dSNishanth Menon 
5879e7d756dSNishanth Menon 	if (IS_ERR(handle))
5889e7d756dSNishanth Menon 		return PTR_ERR(handle);
5899e7d756dSNishanth Menon 	if (!handle)
5909e7d756dSNishanth Menon 		return -EINVAL;
5919e7d756dSNishanth Menon 
5929e7d756dSNishanth Menon 	if (!clcnt && !resets && !p_state && !c_state)
5939e7d756dSNishanth Menon 		return -EINVAL;
5949e7d756dSNishanth Menon 
5959e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
5969e7d756dSNishanth Menon 	dev = info->dev;
5979e7d756dSNishanth Menon 
5989e7d756dSNishanth Menon 	/* Response is expected, so need of any flags */
5999e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
6009e7d756dSNishanth Menon 				   0, sizeof(*req), sizeof(*resp));
6019e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
6029e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
6039e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
6049e7d756dSNishanth Menon 		return ret;
6059e7d756dSNishanth Menon 	}
6069e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
6079e7d756dSNishanth Menon 	req->id = id;
6089e7d756dSNishanth Menon 
6099e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
6109e7d756dSNishanth Menon 	if (ret) {
6119e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
6129e7d756dSNishanth Menon 		goto fail;
6139e7d756dSNishanth Menon 	}
6149e7d756dSNishanth Menon 
6159e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
6169e7d756dSNishanth Menon 	if (!ti_sci_is_response_ack(resp)) {
6179e7d756dSNishanth Menon 		ret = -ENODEV;
6189e7d756dSNishanth Menon 		goto fail;
6199e7d756dSNishanth Menon 	}
6209e7d756dSNishanth Menon 
6219e7d756dSNishanth Menon 	if (clcnt)
6229e7d756dSNishanth Menon 		*clcnt = resp->context_loss_count;
6239e7d756dSNishanth Menon 	if (resets)
6249e7d756dSNishanth Menon 		*resets = resp->resets;
6259e7d756dSNishanth Menon 	if (p_state)
6269e7d756dSNishanth Menon 		*p_state = resp->programmed_state;
6279e7d756dSNishanth Menon 	if (c_state)
6289e7d756dSNishanth Menon 		*c_state = resp->current_state;
6299e7d756dSNishanth Menon fail:
6309e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
6319e7d756dSNishanth Menon 
6329e7d756dSNishanth Menon 	return ret;
6339e7d756dSNishanth Menon }
6349e7d756dSNishanth Menon 
6359e7d756dSNishanth Menon /**
6369e7d756dSNishanth Menon  * ti_sci_cmd_get_device() - command to request for device managed by TISCI
6379e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6389e7d756dSNishanth Menon  * @id:		Device Identifier
6399e7d756dSNishanth Menon  *
6409e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
6419e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
6429e7d756dSNishanth Menon  * managed by driver for that purpose.
6439e7d756dSNishanth Menon  *
6449e7d756dSNishanth Menon  * NOTE: The request is for exclusive access for the processor.
6459e7d756dSNishanth Menon  *
6469e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
6479e7d756dSNishanth Menon  */
6489e7d756dSNishanth Menon static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
6499e7d756dSNishanth Menon {
6509e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
6519e7d756dSNishanth Menon 				       MSG_FLAG_DEVICE_EXCLUSIVE,
6529e7d756dSNishanth Menon 				       MSG_DEVICE_SW_STATE_ON);
6539e7d756dSNishanth Menon }
6549e7d756dSNishanth Menon 
6559e7d756dSNishanth Menon /**
6569e7d756dSNishanth Menon  * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
6579e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6589e7d756dSNishanth Menon  * @id:		Device Identifier
6599e7d756dSNishanth Menon  *
6609e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
6619e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
6629e7d756dSNishanth Menon  * managed by driver for that purpose.
6639e7d756dSNishanth Menon  *
6649e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
6659e7d756dSNishanth Menon  */
6669e7d756dSNishanth Menon static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
6679e7d756dSNishanth Menon {
6689e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
6699e7d756dSNishanth Menon 				       MSG_FLAG_DEVICE_EXCLUSIVE,
6709e7d756dSNishanth Menon 				       MSG_DEVICE_SW_STATE_RETENTION);
6719e7d756dSNishanth Menon }
6729e7d756dSNishanth Menon 
6739e7d756dSNishanth Menon /**
6749e7d756dSNishanth Menon  * ti_sci_cmd_put_device() - command to release a device managed by TISCI
6759e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6769e7d756dSNishanth Menon  * @id:		Device Identifier
6779e7d756dSNishanth Menon  *
6789e7d756dSNishanth Menon  * Request for the device - NOTE: the client MUST maintain integrity of
6799e7d756dSNishanth Menon  * usage count by balancing get_device with put_device. No refcounting is
6809e7d756dSNishanth Menon  * managed by driver for that purpose.
6819e7d756dSNishanth Menon  *
6829e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
6839e7d756dSNishanth Menon  */
6849e7d756dSNishanth Menon static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
6859e7d756dSNishanth Menon {
6869e7d756dSNishanth Menon 	return ti_sci_set_device_state(handle, id,
6879e7d756dSNishanth Menon 				       0, MSG_DEVICE_SW_STATE_AUTO_OFF);
6889e7d756dSNishanth Menon }
6899e7d756dSNishanth Menon 
6909e7d756dSNishanth Menon /**
6919e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_valid() - Is the device valid
6929e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
6939e7d756dSNishanth Menon  * @id:		Device Identifier
6949e7d756dSNishanth Menon  *
6959e7d756dSNishanth Menon  * Return: 0 if all went fine and the device ID is valid, else return
6969e7d756dSNishanth Menon  * appropriate error.
6979e7d756dSNishanth Menon  */
6989e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
6999e7d756dSNishanth Menon {
7009e7d756dSNishanth Menon 	u8 unused;
7019e7d756dSNishanth Menon 
7029e7d756dSNishanth Menon 	/* check the device state which will also tell us if the ID is valid */
7039e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
7049e7d756dSNishanth Menon }
7059e7d756dSNishanth Menon 
7069e7d756dSNishanth Menon /**
7079e7d756dSNishanth Menon  * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
7089e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7099e7d756dSNishanth Menon  * @id:		Device Identifier
7109e7d756dSNishanth Menon  * @count:	Pointer to Context Loss counter to populate
7119e7d756dSNishanth Menon  *
7129e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7139e7d756dSNishanth Menon  */
7149e7d756dSNishanth Menon static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
7159e7d756dSNishanth Menon 				    u32 *count)
7169e7d756dSNishanth Menon {
7179e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
7189e7d756dSNishanth Menon }
7199e7d756dSNishanth Menon 
7209e7d756dSNishanth Menon /**
7219e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
7229e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7239e7d756dSNishanth Menon  * @id:		Device Identifier
7249e7d756dSNishanth Menon  * @r_state:	true if requested to be idle
7259e7d756dSNishanth Menon  *
7269e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7279e7d756dSNishanth Menon  */
7289e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
7299e7d756dSNishanth Menon 				  bool *r_state)
7309e7d756dSNishanth Menon {
7319e7d756dSNishanth Menon 	int ret;
7329e7d756dSNishanth Menon 	u8 state;
7339e7d756dSNishanth Menon 
7349e7d756dSNishanth Menon 	if (!r_state)
7359e7d756dSNishanth Menon 		return -EINVAL;
7369e7d756dSNishanth Menon 
7379e7d756dSNishanth Menon 	ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
7389e7d756dSNishanth Menon 	if (ret)
7399e7d756dSNishanth Menon 		return ret;
7409e7d756dSNishanth Menon 
7419e7d756dSNishanth Menon 	*r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
7429e7d756dSNishanth Menon 
7439e7d756dSNishanth Menon 	return 0;
7449e7d756dSNishanth Menon }
7459e7d756dSNishanth Menon 
7469e7d756dSNishanth Menon /**
7479e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
7489e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7499e7d756dSNishanth Menon  * @id:		Device Identifier
7509e7d756dSNishanth Menon  * @r_state:	true if requested to be stopped
7519e7d756dSNishanth Menon  * @curr_state:	true if currently stopped.
7529e7d756dSNishanth Menon  *
7539e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7549e7d756dSNishanth Menon  */
7559e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
7569e7d756dSNishanth Menon 				  bool *r_state,  bool *curr_state)
7579e7d756dSNishanth Menon {
7589e7d756dSNishanth Menon 	int ret;
7599e7d756dSNishanth Menon 	u8 p_state, c_state;
7609e7d756dSNishanth Menon 
7619e7d756dSNishanth Menon 	if (!r_state && !curr_state)
7629e7d756dSNishanth Menon 		return -EINVAL;
7639e7d756dSNishanth Menon 
7649e7d756dSNishanth Menon 	ret =
7659e7d756dSNishanth Menon 	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
7669e7d756dSNishanth Menon 	if (ret)
7679e7d756dSNishanth Menon 		return ret;
7689e7d756dSNishanth Menon 
7699e7d756dSNishanth Menon 	if (r_state)
7709e7d756dSNishanth Menon 		*r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
7719e7d756dSNishanth Menon 	if (curr_state)
7729e7d756dSNishanth Menon 		*curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
7739e7d756dSNishanth Menon 
7749e7d756dSNishanth Menon 	return 0;
7759e7d756dSNishanth Menon }
7769e7d756dSNishanth Menon 
7779e7d756dSNishanth Menon /**
7789e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
7799e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
7809e7d756dSNishanth Menon  * @id:		Device Identifier
7819e7d756dSNishanth Menon  * @r_state:	true if requested to be ON
7829e7d756dSNishanth Menon  * @curr_state:	true if currently ON and active
7839e7d756dSNishanth Menon  *
7849e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
7859e7d756dSNishanth Menon  */
7869e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
7879e7d756dSNishanth Menon 				bool *r_state,  bool *curr_state)
7889e7d756dSNishanth Menon {
7899e7d756dSNishanth Menon 	int ret;
7909e7d756dSNishanth Menon 	u8 p_state, c_state;
7919e7d756dSNishanth Menon 
7929e7d756dSNishanth Menon 	if (!r_state && !curr_state)
7939e7d756dSNishanth Menon 		return -EINVAL;
7949e7d756dSNishanth Menon 
7959e7d756dSNishanth Menon 	ret =
7969e7d756dSNishanth Menon 	    ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
7979e7d756dSNishanth Menon 	if (ret)
7989e7d756dSNishanth Menon 		return ret;
7999e7d756dSNishanth Menon 
8009e7d756dSNishanth Menon 	if (r_state)
8019e7d756dSNishanth Menon 		*r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
8029e7d756dSNishanth Menon 	if (curr_state)
8039e7d756dSNishanth Menon 		*curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
8049e7d756dSNishanth Menon 
8059e7d756dSNishanth Menon 	return 0;
8069e7d756dSNishanth Menon }
8079e7d756dSNishanth Menon 
8089e7d756dSNishanth Menon /**
8099e7d756dSNishanth Menon  * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
8109e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle
8119e7d756dSNishanth Menon  * @id:		Device Identifier
8129e7d756dSNishanth Menon  * @curr_state:	true if currently transitioning.
8139e7d756dSNishanth Menon  *
8149e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8159e7d756dSNishanth Menon  */
8169e7d756dSNishanth Menon static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
8179e7d756dSNishanth Menon 				   bool *curr_state)
8189e7d756dSNishanth Menon {
8199e7d756dSNishanth Menon 	int ret;
8209e7d756dSNishanth Menon 	u8 state;
8219e7d756dSNishanth Menon 
8229e7d756dSNishanth Menon 	if (!curr_state)
8239e7d756dSNishanth Menon 		return -EINVAL;
8249e7d756dSNishanth Menon 
8259e7d756dSNishanth Menon 	ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
8269e7d756dSNishanth Menon 	if (ret)
8279e7d756dSNishanth Menon 		return ret;
8289e7d756dSNishanth Menon 
8299e7d756dSNishanth Menon 	*curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
8309e7d756dSNishanth Menon 
8319e7d756dSNishanth Menon 	return 0;
8329e7d756dSNishanth Menon }
8339e7d756dSNishanth Menon 
8349e7d756dSNishanth Menon /**
8359e7d756dSNishanth Menon  * ti_sci_cmd_set_device_resets() - command to set resets for device managed
8369e7d756dSNishanth Menon  *				    by TISCI
8379e7d756dSNishanth Menon  * @handle:	Pointer to TISCI handle as retrieved by *ti_sci_get_handle
8389e7d756dSNishanth Menon  * @id:		Device Identifier
8399e7d756dSNishanth Menon  * @reset_state: Device specific reset bit field
8409e7d756dSNishanth Menon  *
8419e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8429e7d756dSNishanth Menon  */
8439e7d756dSNishanth Menon static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
8449e7d756dSNishanth Menon 					u32 id, u32 reset_state)
8459e7d756dSNishanth Menon {
8469e7d756dSNishanth Menon 	struct ti_sci_info *info;
8479e7d756dSNishanth Menon 	struct ti_sci_msg_req_set_device_resets *req;
8489e7d756dSNishanth Menon 	struct ti_sci_msg_hdr *resp;
8499e7d756dSNishanth Menon 	struct ti_sci_xfer *xfer;
8509e7d756dSNishanth Menon 	struct device *dev;
8519e7d756dSNishanth Menon 	int ret = 0;
8529e7d756dSNishanth Menon 
8539e7d756dSNishanth Menon 	if (IS_ERR(handle))
8549e7d756dSNishanth Menon 		return PTR_ERR(handle);
8559e7d756dSNishanth Menon 	if (!handle)
8569e7d756dSNishanth Menon 		return -EINVAL;
8579e7d756dSNishanth Menon 
8589e7d756dSNishanth Menon 	info = handle_to_ti_sci_info(handle);
8599e7d756dSNishanth Menon 	dev = info->dev;
8609e7d756dSNishanth Menon 
8619e7d756dSNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
8629e7d756dSNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
8639e7d756dSNishanth Menon 				   sizeof(*req), sizeof(*resp));
8649e7d756dSNishanth Menon 	if (IS_ERR(xfer)) {
8659e7d756dSNishanth Menon 		ret = PTR_ERR(xfer);
8669e7d756dSNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
8679e7d756dSNishanth Menon 		return ret;
8689e7d756dSNishanth Menon 	}
8699e7d756dSNishanth Menon 	req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
8709e7d756dSNishanth Menon 	req->id = id;
8719e7d756dSNishanth Menon 	req->resets = reset_state;
8729e7d756dSNishanth Menon 
8739e7d756dSNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
8749e7d756dSNishanth Menon 	if (ret) {
8759e7d756dSNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
8769e7d756dSNishanth Menon 		goto fail;
8779e7d756dSNishanth Menon 	}
8789e7d756dSNishanth Menon 
8799e7d756dSNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
8809e7d756dSNishanth Menon 
8819e7d756dSNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
8829e7d756dSNishanth Menon 
8839e7d756dSNishanth Menon fail:
8849e7d756dSNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
8859e7d756dSNishanth Menon 
8869e7d756dSNishanth Menon 	return ret;
8879e7d756dSNishanth Menon }
8889e7d756dSNishanth Menon 
8899e7d756dSNishanth Menon /**
8909e7d756dSNishanth Menon  * ti_sci_cmd_get_device_resets() - Get reset state for device managed
8919e7d756dSNishanth Menon  *				    by TISCI
8929e7d756dSNishanth Menon  * @handle:		Pointer to TISCI handle
8939e7d756dSNishanth Menon  * @id:			Device Identifier
8949e7d756dSNishanth Menon  * @reset_state:	Pointer to reset state to populate
8959e7d756dSNishanth Menon  *
8969e7d756dSNishanth Menon  * Return: 0 if all went fine, else return appropriate error.
8979e7d756dSNishanth Menon  */
8989e7d756dSNishanth Menon static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
8999e7d756dSNishanth Menon 					u32 id, u32 *reset_state)
9009e7d756dSNishanth Menon {
9019e7d756dSNishanth Menon 	return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
9029e7d756dSNishanth Menon 				       NULL);
9039e7d756dSNishanth Menon }
9049e7d756dSNishanth Menon 
9059f723220SNishanth Menon /**
9069f723220SNishanth Menon  * ti_sci_set_clock_state() - Set clock state helper
9079f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
9089f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
9099f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
9109f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
9119f723220SNishanth Menon  *		which clock input to modify.
9129f723220SNishanth Menon  * @flags:	Header flags as needed
9139f723220SNishanth Menon  * @state:	State to request for the clock.
9149f723220SNishanth Menon  *
9159f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
9169f723220SNishanth Menon  */
9179f723220SNishanth Menon static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
9189f723220SNishanth Menon 				  u32 dev_id, u8 clk_id,
9199f723220SNishanth Menon 				  u32 flags, u8 state)
9209f723220SNishanth Menon {
9219f723220SNishanth Menon 	struct ti_sci_info *info;
9229f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_state *req;
9239f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
9249f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
9259f723220SNishanth Menon 	struct device *dev;
9269f723220SNishanth Menon 	int ret = 0;
9279f723220SNishanth Menon 
9289f723220SNishanth Menon 	if (IS_ERR(handle))
9299f723220SNishanth Menon 		return PTR_ERR(handle);
9309f723220SNishanth Menon 	if (!handle)
9319f723220SNishanth Menon 		return -EINVAL;
9329f723220SNishanth Menon 
9339f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
9349f723220SNishanth Menon 	dev = info->dev;
9359f723220SNishanth Menon 
9369f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
9379f723220SNishanth Menon 				   flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
9389f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
9399f723220SNishanth Menon 	if (IS_ERR(xfer)) {
9409f723220SNishanth Menon 		ret = PTR_ERR(xfer);
9419f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
9429f723220SNishanth Menon 		return ret;
9439f723220SNishanth Menon 	}
9449f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
9459f723220SNishanth Menon 	req->dev_id = dev_id;
9469f723220SNishanth Menon 	req->clk_id = clk_id;
9479f723220SNishanth Menon 	req->request_state = state;
9489f723220SNishanth Menon 
9499f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
9509f723220SNishanth Menon 	if (ret) {
9519f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
9529f723220SNishanth Menon 		goto fail;
9539f723220SNishanth Menon 	}
9549f723220SNishanth Menon 
9559f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
9569f723220SNishanth Menon 
9579f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
9589f723220SNishanth Menon 
9599f723220SNishanth Menon fail:
9609f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
9619f723220SNishanth Menon 
9629f723220SNishanth Menon 	return ret;
9639f723220SNishanth Menon }
9649f723220SNishanth Menon 
9659f723220SNishanth Menon /**
9669f723220SNishanth Menon  * ti_sci_cmd_get_clock_state() - Get clock state helper
9679f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
9689f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
9699f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
9709f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
9719f723220SNishanth Menon  *		which clock input to modify.
9729f723220SNishanth Menon  * @programmed_state:	State requested for clock to move to
9739f723220SNishanth Menon  * @current_state:	State that the clock is currently in
9749f723220SNishanth Menon  *
9759f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
9769f723220SNishanth Menon  */
9779f723220SNishanth Menon static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
9789f723220SNishanth Menon 				      u32 dev_id, u8 clk_id,
9799f723220SNishanth Menon 				      u8 *programmed_state, u8 *current_state)
9809f723220SNishanth Menon {
9819f723220SNishanth Menon 	struct ti_sci_info *info;
9829f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_state *req;
9839f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_state *resp;
9849f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
9859f723220SNishanth Menon 	struct device *dev;
9869f723220SNishanth Menon 	int ret = 0;
9879f723220SNishanth Menon 
9889f723220SNishanth Menon 	if (IS_ERR(handle))
9899f723220SNishanth Menon 		return PTR_ERR(handle);
9909f723220SNishanth Menon 	if (!handle)
9919f723220SNishanth Menon 		return -EINVAL;
9929f723220SNishanth Menon 
9939f723220SNishanth Menon 	if (!programmed_state && !current_state)
9949f723220SNishanth Menon 		return -EINVAL;
9959f723220SNishanth Menon 
9969f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
9979f723220SNishanth Menon 	dev = info->dev;
9989f723220SNishanth Menon 
9999f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
10009f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
10019f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
10029f723220SNishanth Menon 	if (IS_ERR(xfer)) {
10039f723220SNishanth Menon 		ret = PTR_ERR(xfer);
10049f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
10059f723220SNishanth Menon 		return ret;
10069f723220SNishanth Menon 	}
10079f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
10089f723220SNishanth Menon 	req->dev_id = dev_id;
10099f723220SNishanth Menon 	req->clk_id = clk_id;
10109f723220SNishanth Menon 
10119f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
10129f723220SNishanth Menon 	if (ret) {
10139f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
10149f723220SNishanth Menon 		goto fail;
10159f723220SNishanth Menon 	}
10169f723220SNishanth Menon 
10179f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
10189f723220SNishanth Menon 
10199f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp)) {
10209f723220SNishanth Menon 		ret = -ENODEV;
10219f723220SNishanth Menon 		goto fail;
10229f723220SNishanth Menon 	}
10239f723220SNishanth Menon 
10249f723220SNishanth Menon 	if (programmed_state)
10259f723220SNishanth Menon 		*programmed_state = resp->programmed_state;
10269f723220SNishanth Menon 	if (current_state)
10279f723220SNishanth Menon 		*current_state = resp->current_state;
10289f723220SNishanth Menon 
10299f723220SNishanth Menon fail:
10309f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
10319f723220SNishanth Menon 
10329f723220SNishanth Menon 	return ret;
10339f723220SNishanth Menon }
10349f723220SNishanth Menon 
10359f723220SNishanth Menon /**
10369f723220SNishanth Menon  * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
10379f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
10389f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
10399f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
10409f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
10419f723220SNishanth Menon  *		which clock input to modify.
10429f723220SNishanth Menon  * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
10439f723220SNishanth Menon  * @can_change_freq: 'true' if frequency change is desired, else 'false'
10449f723220SNishanth Menon  * @enable_input_term: 'true' if input termination is desired, else 'false'
10459f723220SNishanth Menon  *
10469f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
10479f723220SNishanth Menon  */
10489f723220SNishanth Menon static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
10499f723220SNishanth Menon 				u8 clk_id, bool needs_ssc, bool can_change_freq,
10509f723220SNishanth Menon 				bool enable_input_term)
10519f723220SNishanth Menon {
10529f723220SNishanth Menon 	u32 flags = 0;
10539f723220SNishanth Menon 
10549f723220SNishanth Menon 	flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
10559f723220SNishanth Menon 	flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
10569f723220SNishanth Menon 	flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
10579f723220SNishanth Menon 
10589f723220SNishanth Menon 	return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
10599f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_REQ);
10609f723220SNishanth Menon }
10619f723220SNishanth Menon 
10629f723220SNishanth Menon /**
10639f723220SNishanth Menon  * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
10649f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
10659f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
10669f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
10679f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
10689f723220SNishanth Menon  *		which clock input to modify.
10699f723220SNishanth Menon  *
10709f723220SNishanth Menon  * NOTE: This clock must have been requested by get_clock previously.
10719f723220SNishanth Menon  *
10729f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
10739f723220SNishanth Menon  */
10749f723220SNishanth Menon static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
10759f723220SNishanth Menon 				 u32 dev_id, u8 clk_id)
10769f723220SNishanth Menon {
10779f723220SNishanth Menon 	return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
10789f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_UNREQ);
10799f723220SNishanth Menon }
10809f723220SNishanth Menon 
10819f723220SNishanth Menon /**
10829f723220SNishanth Menon  * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
10839f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
10849f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
10859f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
10869f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
10879f723220SNishanth Menon  *		which clock input to modify.
10889f723220SNishanth Menon  *
10899f723220SNishanth Menon  * NOTE: This clock must have been requested by get_clock previously.
10909f723220SNishanth Menon  *
10919f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
10929f723220SNishanth Menon  */
10939f723220SNishanth Menon static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
10949f723220SNishanth Menon 				u32 dev_id, u8 clk_id)
10959f723220SNishanth Menon {
10969f723220SNishanth Menon 	return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
10979f723220SNishanth Menon 				      MSG_CLOCK_SW_STATE_AUTO);
10989f723220SNishanth Menon }
10999f723220SNishanth Menon 
11009f723220SNishanth Menon /**
11019f723220SNishanth Menon  * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
11029f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11039f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11049f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11059f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11069f723220SNishanth Menon  *		which clock input to modify.
11079f723220SNishanth Menon  * @req_state: state indicating if the clock is auto managed
11089f723220SNishanth Menon  *
11099f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11109f723220SNishanth Menon  */
11119f723220SNishanth Menon static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
11129f723220SNishanth Menon 				  u32 dev_id, u8 clk_id, bool *req_state)
11139f723220SNishanth Menon {
11149f723220SNishanth Menon 	u8 state = 0;
11159f723220SNishanth Menon 	int ret;
11169f723220SNishanth Menon 
11179f723220SNishanth Menon 	if (!req_state)
11189f723220SNishanth Menon 		return -EINVAL;
11199f723220SNishanth Menon 
11209f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
11219f723220SNishanth Menon 	if (ret)
11229f723220SNishanth Menon 		return ret;
11239f723220SNishanth Menon 
11249f723220SNishanth Menon 	*req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
11259f723220SNishanth Menon 	return 0;
11269f723220SNishanth Menon }
11279f723220SNishanth Menon 
11289f723220SNishanth Menon /**
11299f723220SNishanth Menon  * ti_sci_cmd_clk_is_on() - Is the clock ON
11309f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11319f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11329f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11339f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11349f723220SNishanth Menon  *		which clock input to modify.
11359f723220SNishanth Menon  * @req_state: state indicating if the clock is managed by us and enabled
11369f723220SNishanth Menon  * @curr_state: state indicating if the clock is ready for operation
11379f723220SNishanth Menon  *
11389f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11399f723220SNishanth Menon  */
11409f723220SNishanth Menon static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
11419f723220SNishanth Menon 				u8 clk_id, bool *req_state, bool *curr_state)
11429f723220SNishanth Menon {
11439f723220SNishanth Menon 	u8 c_state = 0, r_state = 0;
11449f723220SNishanth Menon 	int ret;
11459f723220SNishanth Menon 
11469f723220SNishanth Menon 	if (!req_state && !curr_state)
11479f723220SNishanth Menon 		return -EINVAL;
11489f723220SNishanth Menon 
11499f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
11509f723220SNishanth Menon 					 &r_state, &c_state);
11519f723220SNishanth Menon 	if (ret)
11529f723220SNishanth Menon 		return ret;
11539f723220SNishanth Menon 
11549f723220SNishanth Menon 	if (req_state)
11559f723220SNishanth Menon 		*req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
11569f723220SNishanth Menon 	if (curr_state)
11579f723220SNishanth Menon 		*curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
11589f723220SNishanth Menon 	return 0;
11599f723220SNishanth Menon }
11609f723220SNishanth Menon 
11619f723220SNishanth Menon /**
11629f723220SNishanth Menon  * ti_sci_cmd_clk_is_off() - Is the clock OFF
11639f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11649f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11659f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11669f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
11679f723220SNishanth Menon  *		which clock input to modify.
11689f723220SNishanth Menon  * @req_state: state indicating if the clock is managed by us and disabled
11699f723220SNishanth Menon  * @curr_state: state indicating if the clock is NOT ready for operation
11709f723220SNishanth Menon  *
11719f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
11729f723220SNishanth Menon  */
11739f723220SNishanth Menon static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
11749f723220SNishanth Menon 				 u8 clk_id, bool *req_state, bool *curr_state)
11759f723220SNishanth Menon {
11769f723220SNishanth Menon 	u8 c_state = 0, r_state = 0;
11779f723220SNishanth Menon 	int ret;
11789f723220SNishanth Menon 
11799f723220SNishanth Menon 	if (!req_state && !curr_state)
11809f723220SNishanth Menon 		return -EINVAL;
11819f723220SNishanth Menon 
11829f723220SNishanth Menon 	ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
11839f723220SNishanth Menon 					 &r_state, &c_state);
11849f723220SNishanth Menon 	if (ret)
11859f723220SNishanth Menon 		return ret;
11869f723220SNishanth Menon 
11879f723220SNishanth Menon 	if (req_state)
11889f723220SNishanth Menon 		*req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
11899f723220SNishanth Menon 	if (curr_state)
11909f723220SNishanth Menon 		*curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
11919f723220SNishanth Menon 	return 0;
11929f723220SNishanth Menon }
11939f723220SNishanth Menon 
11949f723220SNishanth Menon /**
11959f723220SNishanth Menon  * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
11969f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
11979f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
11989f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
11999f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
12009f723220SNishanth Menon  *		which clock input to modify.
12019f723220SNishanth Menon  * @parent_id:	Parent clock identifier to set
12029f723220SNishanth Menon  *
12039f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
12049f723220SNishanth Menon  */
12059f723220SNishanth Menon static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
12069f723220SNishanth Menon 				     u32 dev_id, u8 clk_id, u8 parent_id)
12079f723220SNishanth Menon {
12089f723220SNishanth Menon 	struct ti_sci_info *info;
12099f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_parent *req;
12109f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
12119f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
12129f723220SNishanth Menon 	struct device *dev;
12139f723220SNishanth Menon 	int ret = 0;
12149f723220SNishanth Menon 
12159f723220SNishanth Menon 	if (IS_ERR(handle))
12169f723220SNishanth Menon 		return PTR_ERR(handle);
12179f723220SNishanth Menon 	if (!handle)
12189f723220SNishanth Menon 		return -EINVAL;
12199f723220SNishanth Menon 
12209f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
12219f723220SNishanth Menon 	dev = info->dev;
12229f723220SNishanth Menon 
12239f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
12249f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
12259f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
12269f723220SNishanth Menon 	if (IS_ERR(xfer)) {
12279f723220SNishanth Menon 		ret = PTR_ERR(xfer);
12289f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
12299f723220SNishanth Menon 		return ret;
12309f723220SNishanth Menon 	}
12319f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
12329f723220SNishanth Menon 	req->dev_id = dev_id;
12339f723220SNishanth Menon 	req->clk_id = clk_id;
12349f723220SNishanth Menon 	req->parent_id = parent_id;
12359f723220SNishanth Menon 
12369f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
12379f723220SNishanth Menon 	if (ret) {
12389f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
12399f723220SNishanth Menon 		goto fail;
12409f723220SNishanth Menon 	}
12419f723220SNishanth Menon 
12429f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
12439f723220SNishanth Menon 
12449f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
12459f723220SNishanth Menon 
12469f723220SNishanth Menon fail:
12479f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
12489f723220SNishanth Menon 
12499f723220SNishanth Menon 	return ret;
12509f723220SNishanth Menon }
12519f723220SNishanth Menon 
12529f723220SNishanth Menon /**
12539f723220SNishanth Menon  * ti_sci_cmd_clk_get_parent() - Get current parent clock source
12549f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
12559f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
12569f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
12579f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
12589f723220SNishanth Menon  *		which clock input to modify.
12599f723220SNishanth Menon  * @parent_id:	Current clock parent
12609f723220SNishanth Menon  *
12619f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
12629f723220SNishanth Menon  */
12639f723220SNishanth Menon static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
12649f723220SNishanth Menon 				     u32 dev_id, u8 clk_id, u8 *parent_id)
12659f723220SNishanth Menon {
12669f723220SNishanth Menon 	struct ti_sci_info *info;
12679f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_parent *req;
12689f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_parent *resp;
12699f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
12709f723220SNishanth Menon 	struct device *dev;
12719f723220SNishanth Menon 	int ret = 0;
12729f723220SNishanth Menon 
12739f723220SNishanth Menon 	if (IS_ERR(handle))
12749f723220SNishanth Menon 		return PTR_ERR(handle);
12759f723220SNishanth Menon 	if (!handle || !parent_id)
12769f723220SNishanth Menon 		return -EINVAL;
12779f723220SNishanth Menon 
12789f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
12799f723220SNishanth Menon 	dev = info->dev;
12809f723220SNishanth Menon 
12819f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
12829f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
12839f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
12849f723220SNishanth Menon 	if (IS_ERR(xfer)) {
12859f723220SNishanth Menon 		ret = PTR_ERR(xfer);
12869f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
12879f723220SNishanth Menon 		return ret;
12889f723220SNishanth Menon 	}
12899f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
12909f723220SNishanth Menon 	req->dev_id = dev_id;
12919f723220SNishanth Menon 	req->clk_id = clk_id;
12929f723220SNishanth Menon 
12939f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
12949f723220SNishanth Menon 	if (ret) {
12959f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
12969f723220SNishanth Menon 		goto fail;
12979f723220SNishanth Menon 	}
12989f723220SNishanth Menon 
12999f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
13009f723220SNishanth Menon 
13019f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
13029f723220SNishanth Menon 		ret = -ENODEV;
13039f723220SNishanth Menon 	else
13049f723220SNishanth Menon 		*parent_id = resp->parent_id;
13059f723220SNishanth Menon 
13069f723220SNishanth Menon fail:
13079f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
13089f723220SNishanth Menon 
13099f723220SNishanth Menon 	return ret;
13109f723220SNishanth Menon }
13119f723220SNishanth Menon 
13129f723220SNishanth Menon /**
13139f723220SNishanth Menon  * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
13149f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
13159f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
13169f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
13179f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
13189f723220SNishanth Menon  *		which clock input to modify.
13199f723220SNishanth Menon  * @num_parents: Returns he number of parents to the current clock.
13209f723220SNishanth Menon  *
13219f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
13229f723220SNishanth Menon  */
13239f723220SNishanth Menon static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
13249f723220SNishanth Menon 					  u32 dev_id, u8 clk_id,
13259f723220SNishanth Menon 					  u8 *num_parents)
13269f723220SNishanth Menon {
13279f723220SNishanth Menon 	struct ti_sci_info *info;
13289f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_num_parents *req;
13299f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_num_parents *resp;
13309f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
13319f723220SNishanth Menon 	struct device *dev;
13329f723220SNishanth Menon 	int ret = 0;
13339f723220SNishanth Menon 
13349f723220SNishanth Menon 	if (IS_ERR(handle))
13359f723220SNishanth Menon 		return PTR_ERR(handle);
13369f723220SNishanth Menon 	if (!handle || !num_parents)
13379f723220SNishanth Menon 		return -EINVAL;
13389f723220SNishanth Menon 
13399f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
13409f723220SNishanth Menon 	dev = info->dev;
13419f723220SNishanth Menon 
13429f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
13439f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
13449f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
13459f723220SNishanth Menon 	if (IS_ERR(xfer)) {
13469f723220SNishanth Menon 		ret = PTR_ERR(xfer);
13479f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
13489f723220SNishanth Menon 		return ret;
13499f723220SNishanth Menon 	}
13509f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
13519f723220SNishanth Menon 	req->dev_id = dev_id;
13529f723220SNishanth Menon 	req->clk_id = clk_id;
13539f723220SNishanth Menon 
13549f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
13559f723220SNishanth Menon 	if (ret) {
13569f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
13579f723220SNishanth Menon 		goto fail;
13589f723220SNishanth Menon 	}
13599f723220SNishanth Menon 
13609f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
13619f723220SNishanth Menon 
13629f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
13639f723220SNishanth Menon 		ret = -ENODEV;
13649f723220SNishanth Menon 	else
13659f723220SNishanth Menon 		*num_parents = resp->num_parents;
13669f723220SNishanth Menon 
13679f723220SNishanth Menon fail:
13689f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
13699f723220SNishanth Menon 
13709f723220SNishanth Menon 	return ret;
13719f723220SNishanth Menon }
13729f723220SNishanth Menon 
13739f723220SNishanth Menon /**
13749f723220SNishanth Menon  * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
13759f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
13769f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
13779f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
13789f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
13799f723220SNishanth Menon  *		which clock input to modify.
13809f723220SNishanth Menon  * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
13819f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
13829f723220SNishanth Menon  *		tolerances and jitter.
13839f723220SNishanth Menon  * @target_freq: The target clock frequency in Hz. A frequency will be
13849f723220SNishanth Menon  *		processed as close to this target frequency as possible.
13859f723220SNishanth Menon  * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
13869f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
13879f723220SNishanth Menon  *		tolerances and jitter.
13889f723220SNishanth Menon  * @match_freq:	Frequency match in Hz response.
13899f723220SNishanth Menon  *
13909f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
13919f723220SNishanth Menon  */
13929f723220SNishanth Menon static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
13939f723220SNishanth Menon 					 u32 dev_id, u8 clk_id, u64 min_freq,
13949f723220SNishanth Menon 					 u64 target_freq, u64 max_freq,
13959f723220SNishanth Menon 					 u64 *match_freq)
13969f723220SNishanth Menon {
13979f723220SNishanth Menon 	struct ti_sci_info *info;
13989f723220SNishanth Menon 	struct ti_sci_msg_req_query_clock_freq *req;
13999f723220SNishanth Menon 	struct ti_sci_msg_resp_query_clock_freq *resp;
14009f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
14019f723220SNishanth Menon 	struct device *dev;
14029f723220SNishanth Menon 	int ret = 0;
14039f723220SNishanth Menon 
14049f723220SNishanth Menon 	if (IS_ERR(handle))
14059f723220SNishanth Menon 		return PTR_ERR(handle);
14069f723220SNishanth Menon 	if (!handle || !match_freq)
14079f723220SNishanth Menon 		return -EINVAL;
14089f723220SNishanth Menon 
14099f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
14109f723220SNishanth Menon 	dev = info->dev;
14119f723220SNishanth Menon 
14129f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
14139f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
14149f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
14159f723220SNishanth Menon 	if (IS_ERR(xfer)) {
14169f723220SNishanth Menon 		ret = PTR_ERR(xfer);
14179f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
14189f723220SNishanth Menon 		return ret;
14199f723220SNishanth Menon 	}
14209f723220SNishanth Menon 	req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
14219f723220SNishanth Menon 	req->dev_id = dev_id;
14229f723220SNishanth Menon 	req->clk_id = clk_id;
14239f723220SNishanth Menon 	req->min_freq_hz = min_freq;
14249f723220SNishanth Menon 	req->target_freq_hz = target_freq;
14259f723220SNishanth Menon 	req->max_freq_hz = max_freq;
14269f723220SNishanth Menon 
14279f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
14289f723220SNishanth Menon 	if (ret) {
14299f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
14309f723220SNishanth Menon 		goto fail;
14319f723220SNishanth Menon 	}
14329f723220SNishanth Menon 
14339f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
14349f723220SNishanth Menon 
14359f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
14369f723220SNishanth Menon 		ret = -ENODEV;
14379f723220SNishanth Menon 	else
14389f723220SNishanth Menon 		*match_freq = resp->freq_hz;
14399f723220SNishanth Menon 
14409f723220SNishanth Menon fail:
14419f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
14429f723220SNishanth Menon 
14439f723220SNishanth Menon 	return ret;
14449f723220SNishanth Menon }
14459f723220SNishanth Menon 
14469f723220SNishanth Menon /**
14479f723220SNishanth Menon  * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
14489f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
14499f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
14509f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
14519f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
14529f723220SNishanth Menon  *		which clock input to modify.
14539f723220SNishanth Menon  * @min_freq:	The minimum allowable frequency in Hz. This is the minimum
14549f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
14559f723220SNishanth Menon  *		tolerances and jitter.
14569f723220SNishanth Menon  * @target_freq: The target clock frequency in Hz. A frequency will be
14579f723220SNishanth Menon  *		processed as close to this target frequency as possible.
14589f723220SNishanth Menon  * @max_freq:	The maximum allowable frequency in Hz. This is the maximum
14599f723220SNishanth Menon  *		allowable programmed frequency and does not account for clock
14609f723220SNishanth Menon  *		tolerances and jitter.
14619f723220SNishanth Menon  *
14629f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
14639f723220SNishanth Menon  */
14649f723220SNishanth Menon static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
14659f723220SNishanth Menon 				   u32 dev_id, u8 clk_id, u64 min_freq,
14669f723220SNishanth Menon 				   u64 target_freq, u64 max_freq)
14679f723220SNishanth Menon {
14689f723220SNishanth Menon 	struct ti_sci_info *info;
14699f723220SNishanth Menon 	struct ti_sci_msg_req_set_clock_freq *req;
14709f723220SNishanth Menon 	struct ti_sci_msg_hdr *resp;
14719f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
14729f723220SNishanth Menon 	struct device *dev;
14739f723220SNishanth Menon 	int ret = 0;
14749f723220SNishanth Menon 
14759f723220SNishanth Menon 	if (IS_ERR(handle))
14769f723220SNishanth Menon 		return PTR_ERR(handle);
14779f723220SNishanth Menon 	if (!handle)
14789f723220SNishanth Menon 		return -EINVAL;
14799f723220SNishanth Menon 
14809f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
14819f723220SNishanth Menon 	dev = info->dev;
14829f723220SNishanth Menon 
14839f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
14849f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
14859f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
14869f723220SNishanth Menon 	if (IS_ERR(xfer)) {
14879f723220SNishanth Menon 		ret = PTR_ERR(xfer);
14889f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
14899f723220SNishanth Menon 		return ret;
14909f723220SNishanth Menon 	}
14919f723220SNishanth Menon 	req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
14929f723220SNishanth Menon 	req->dev_id = dev_id;
14939f723220SNishanth Menon 	req->clk_id = clk_id;
14949f723220SNishanth Menon 	req->min_freq_hz = min_freq;
14959f723220SNishanth Menon 	req->target_freq_hz = target_freq;
14969f723220SNishanth Menon 	req->max_freq_hz = max_freq;
14979f723220SNishanth Menon 
14989f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
14999f723220SNishanth Menon 	if (ret) {
15009f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
15019f723220SNishanth Menon 		goto fail;
15029f723220SNishanth Menon 	}
15039f723220SNishanth Menon 
15049f723220SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
15059f723220SNishanth Menon 
15069f723220SNishanth Menon 	ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
15079f723220SNishanth Menon 
15089f723220SNishanth Menon fail:
15099f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
15109f723220SNishanth Menon 
15119f723220SNishanth Menon 	return ret;
15129f723220SNishanth Menon }
15139f723220SNishanth Menon 
15149f723220SNishanth Menon /**
15159f723220SNishanth Menon  * ti_sci_cmd_clk_get_freq() - Get current frequency
15169f723220SNishanth Menon  * @handle:	pointer to TI SCI handle
15179f723220SNishanth Menon  * @dev_id:	Device identifier this request is for
15189f723220SNishanth Menon  * @clk_id:	Clock identifier for the device for this request.
15199f723220SNishanth Menon  *		Each device has it's own set of clock inputs. This indexes
15209f723220SNishanth Menon  *		which clock input to modify.
15219f723220SNishanth Menon  * @freq:	Currently frequency in Hz
15229f723220SNishanth Menon  *
15239f723220SNishanth Menon  * Return: 0 if all went well, else returns appropriate error value.
15249f723220SNishanth Menon  */
15259f723220SNishanth Menon static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
15269f723220SNishanth Menon 				   u32 dev_id, u8 clk_id, u64 *freq)
15279f723220SNishanth Menon {
15289f723220SNishanth Menon 	struct ti_sci_info *info;
15299f723220SNishanth Menon 	struct ti_sci_msg_req_get_clock_freq *req;
15309f723220SNishanth Menon 	struct ti_sci_msg_resp_get_clock_freq *resp;
15319f723220SNishanth Menon 	struct ti_sci_xfer *xfer;
15329f723220SNishanth Menon 	struct device *dev;
15339f723220SNishanth Menon 	int ret = 0;
15349f723220SNishanth Menon 
15359f723220SNishanth Menon 	if (IS_ERR(handle))
15369f723220SNishanth Menon 		return PTR_ERR(handle);
15379f723220SNishanth Menon 	if (!handle || !freq)
15389f723220SNishanth Menon 		return -EINVAL;
15399f723220SNishanth Menon 
15409f723220SNishanth Menon 	info = handle_to_ti_sci_info(handle);
15419f723220SNishanth Menon 	dev = info->dev;
15429f723220SNishanth Menon 
15439f723220SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
15449f723220SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
15459f723220SNishanth Menon 				   sizeof(*req), sizeof(*resp));
15469f723220SNishanth Menon 	if (IS_ERR(xfer)) {
15479f723220SNishanth Menon 		ret = PTR_ERR(xfer);
15489f723220SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
15499f723220SNishanth Menon 		return ret;
15509f723220SNishanth Menon 	}
15519f723220SNishanth Menon 	req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
15529f723220SNishanth Menon 	req->dev_id = dev_id;
15539f723220SNishanth Menon 	req->clk_id = clk_id;
15549f723220SNishanth Menon 
15559f723220SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
15569f723220SNishanth Menon 	if (ret) {
15579f723220SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
15589f723220SNishanth Menon 		goto fail;
15599f723220SNishanth Menon 	}
15609f723220SNishanth Menon 
15619f723220SNishanth Menon 	resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
15629f723220SNishanth Menon 
15639f723220SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
15649f723220SNishanth Menon 		ret = -ENODEV;
15659f723220SNishanth Menon 	else
15669f723220SNishanth Menon 		*freq = resp->freq_hz;
15679f723220SNishanth Menon 
15689f723220SNishanth Menon fail:
15699f723220SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
15709f723220SNishanth Menon 
15719f723220SNishanth Menon 	return ret;
15729f723220SNishanth Menon }
15739f723220SNishanth Menon 
1574912cffb4SNishanth Menon static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1575912cffb4SNishanth Menon {
1576912cffb4SNishanth Menon 	struct ti_sci_info *info;
1577912cffb4SNishanth Menon 	struct ti_sci_msg_req_reboot *req;
1578912cffb4SNishanth Menon 	struct ti_sci_msg_hdr *resp;
1579912cffb4SNishanth Menon 	struct ti_sci_xfer *xfer;
1580912cffb4SNishanth Menon 	struct device *dev;
1581912cffb4SNishanth Menon 	int ret = 0;
1582912cffb4SNishanth Menon 
1583912cffb4SNishanth Menon 	if (IS_ERR(handle))
1584912cffb4SNishanth Menon 		return PTR_ERR(handle);
1585912cffb4SNishanth Menon 	if (!handle)
1586912cffb4SNishanth Menon 		return -EINVAL;
1587912cffb4SNishanth Menon 
1588912cffb4SNishanth Menon 	info = handle_to_ti_sci_info(handle);
1589912cffb4SNishanth Menon 	dev = info->dev;
1590912cffb4SNishanth Menon 
1591912cffb4SNishanth Menon 	xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1592912cffb4SNishanth Menon 				   TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1593912cffb4SNishanth Menon 				   sizeof(*req), sizeof(*resp));
1594912cffb4SNishanth Menon 	if (IS_ERR(xfer)) {
1595912cffb4SNishanth Menon 		ret = PTR_ERR(xfer);
1596912cffb4SNishanth Menon 		dev_err(dev, "Message alloc failed(%d)\n", ret);
1597912cffb4SNishanth Menon 		return ret;
1598912cffb4SNishanth Menon 	}
1599912cffb4SNishanth Menon 	req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1600912cffb4SNishanth Menon 
1601912cffb4SNishanth Menon 	ret = ti_sci_do_xfer(info, xfer);
1602912cffb4SNishanth Menon 	if (ret) {
1603912cffb4SNishanth Menon 		dev_err(dev, "Mbox send fail %d\n", ret);
1604912cffb4SNishanth Menon 		goto fail;
1605912cffb4SNishanth Menon 	}
1606912cffb4SNishanth Menon 
1607912cffb4SNishanth Menon 	resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1608912cffb4SNishanth Menon 
1609912cffb4SNishanth Menon 	if (!ti_sci_is_response_ack(resp))
1610912cffb4SNishanth Menon 		ret = -ENODEV;
1611912cffb4SNishanth Menon 	else
1612912cffb4SNishanth Menon 		ret = 0;
1613912cffb4SNishanth Menon 
1614912cffb4SNishanth Menon fail:
1615912cffb4SNishanth Menon 	ti_sci_put_one_xfer(&info->minfo, xfer);
1616912cffb4SNishanth Menon 
1617912cffb4SNishanth Menon 	return ret;
1618912cffb4SNishanth Menon }
1619912cffb4SNishanth Menon 
16209e7d756dSNishanth Menon /*
16219e7d756dSNishanth Menon  * ti_sci_setup_ops() - Setup the operations structures
16229e7d756dSNishanth Menon  * @info:	pointer to TISCI pointer
16239e7d756dSNishanth Menon  */
16249e7d756dSNishanth Menon static void ti_sci_setup_ops(struct ti_sci_info *info)
16259e7d756dSNishanth Menon {
16269e7d756dSNishanth Menon 	struct ti_sci_ops *ops = &info->handle.ops;
1627912cffb4SNishanth Menon 	struct ti_sci_core_ops *core_ops = &ops->core_ops;
16289e7d756dSNishanth Menon 	struct ti_sci_dev_ops *dops = &ops->dev_ops;
16299f723220SNishanth Menon 	struct ti_sci_clk_ops *cops = &ops->clk_ops;
16309e7d756dSNishanth Menon 
1631912cffb4SNishanth Menon 	core_ops->reboot_device = ti_sci_cmd_core_reboot;
1632912cffb4SNishanth Menon 
16339e7d756dSNishanth Menon 	dops->get_device = ti_sci_cmd_get_device;
16349e7d756dSNishanth Menon 	dops->idle_device = ti_sci_cmd_idle_device;
16359e7d756dSNishanth Menon 	dops->put_device = ti_sci_cmd_put_device;
16369e7d756dSNishanth Menon 
16379e7d756dSNishanth Menon 	dops->is_valid = ti_sci_cmd_dev_is_valid;
16389e7d756dSNishanth Menon 	dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
16399e7d756dSNishanth Menon 	dops->is_idle = ti_sci_cmd_dev_is_idle;
16409e7d756dSNishanth Menon 	dops->is_stop = ti_sci_cmd_dev_is_stop;
16419e7d756dSNishanth Menon 	dops->is_on = ti_sci_cmd_dev_is_on;
16429e7d756dSNishanth Menon 	dops->is_transitioning = ti_sci_cmd_dev_is_trans;
16439e7d756dSNishanth Menon 	dops->set_device_resets = ti_sci_cmd_set_device_resets;
16449e7d756dSNishanth Menon 	dops->get_device_resets = ti_sci_cmd_get_device_resets;
16459f723220SNishanth Menon 
16469f723220SNishanth Menon 	cops->get_clock = ti_sci_cmd_get_clock;
16479f723220SNishanth Menon 	cops->idle_clock = ti_sci_cmd_idle_clock;
16489f723220SNishanth Menon 	cops->put_clock = ti_sci_cmd_put_clock;
16499f723220SNishanth Menon 	cops->is_auto = ti_sci_cmd_clk_is_auto;
16509f723220SNishanth Menon 	cops->is_on = ti_sci_cmd_clk_is_on;
16519f723220SNishanth Menon 	cops->is_off = ti_sci_cmd_clk_is_off;
16529f723220SNishanth Menon 
16539f723220SNishanth Menon 	cops->set_parent = ti_sci_cmd_clk_set_parent;
16549f723220SNishanth Menon 	cops->get_parent = ti_sci_cmd_clk_get_parent;
16559f723220SNishanth Menon 	cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
16569f723220SNishanth Menon 
16579f723220SNishanth Menon 	cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
16589f723220SNishanth Menon 	cops->set_freq = ti_sci_cmd_clk_set_freq;
16599f723220SNishanth Menon 	cops->get_freq = ti_sci_cmd_clk_get_freq;
16609e7d756dSNishanth Menon }
16619e7d756dSNishanth Menon 
16629e7d756dSNishanth Menon /**
1663aa276781SNishanth Menon  * ti_sci_get_handle() - Get the TI SCI handle for a device
1664aa276781SNishanth Menon  * @dev:	Pointer to device for which we want SCI handle
1665aa276781SNishanth Menon  *
1666aa276781SNishanth Menon  * NOTE: The function does not track individual clients of the framework
1667aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
1668aa276781SNishanth Menon  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1669aa276781SNishanth Menon  * Return: pointer to handle if successful, else:
1670aa276781SNishanth Menon  * -EPROBE_DEFER if the instance is not ready
1671aa276781SNishanth Menon  * -ENODEV if the required node handler is missing
1672aa276781SNishanth Menon  * -EINVAL if invalid conditions are encountered.
1673aa276781SNishanth Menon  */
1674aa276781SNishanth Menon const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
1675aa276781SNishanth Menon {
1676aa276781SNishanth Menon 	struct device_node *ti_sci_np;
1677aa276781SNishanth Menon 	struct list_head *p;
1678aa276781SNishanth Menon 	struct ti_sci_handle *handle = NULL;
1679aa276781SNishanth Menon 	struct ti_sci_info *info;
1680aa276781SNishanth Menon 
1681aa276781SNishanth Menon 	if (!dev) {
1682aa276781SNishanth Menon 		pr_err("I need a device pointer\n");
1683aa276781SNishanth Menon 		return ERR_PTR(-EINVAL);
1684aa276781SNishanth Menon 	}
1685aa276781SNishanth Menon 	ti_sci_np = of_get_parent(dev->of_node);
1686aa276781SNishanth Menon 	if (!ti_sci_np) {
1687aa276781SNishanth Menon 		dev_err(dev, "No OF information\n");
1688aa276781SNishanth Menon 		return ERR_PTR(-EINVAL);
1689aa276781SNishanth Menon 	}
1690aa276781SNishanth Menon 
1691aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
1692aa276781SNishanth Menon 	list_for_each(p, &ti_sci_list) {
1693aa276781SNishanth Menon 		info = list_entry(p, struct ti_sci_info, node);
1694aa276781SNishanth Menon 		if (ti_sci_np == info->dev->of_node) {
1695aa276781SNishanth Menon 			handle = &info->handle;
1696aa276781SNishanth Menon 			info->users++;
1697aa276781SNishanth Menon 			break;
1698aa276781SNishanth Menon 		}
1699aa276781SNishanth Menon 	}
1700aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
1701aa276781SNishanth Menon 	of_node_put(ti_sci_np);
1702aa276781SNishanth Menon 
1703aa276781SNishanth Menon 	if (!handle)
1704aa276781SNishanth Menon 		return ERR_PTR(-EPROBE_DEFER);
1705aa276781SNishanth Menon 
1706aa276781SNishanth Menon 	return handle;
1707aa276781SNishanth Menon }
1708aa276781SNishanth Menon EXPORT_SYMBOL_GPL(ti_sci_get_handle);
1709aa276781SNishanth Menon 
1710aa276781SNishanth Menon /**
1711aa276781SNishanth Menon  * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1712aa276781SNishanth Menon  * @handle:	Handle acquired by ti_sci_get_handle
1713aa276781SNishanth Menon  *
1714aa276781SNishanth Menon  * NOTE: The function does not track individual clients of the framework
1715aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
1716aa276781SNishanth Menon  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1717aa276781SNishanth Menon  *
1718aa276781SNishanth Menon  * Return: 0 is successfully released
1719aa276781SNishanth Menon  * if an error pointer was passed, it returns the error value back,
1720aa276781SNishanth Menon  * if null was passed, it returns -EINVAL;
1721aa276781SNishanth Menon  */
1722aa276781SNishanth Menon int ti_sci_put_handle(const struct ti_sci_handle *handle)
1723aa276781SNishanth Menon {
1724aa276781SNishanth Menon 	struct ti_sci_info *info;
1725aa276781SNishanth Menon 
1726aa276781SNishanth Menon 	if (IS_ERR(handle))
1727aa276781SNishanth Menon 		return PTR_ERR(handle);
1728aa276781SNishanth Menon 	if (!handle)
1729aa276781SNishanth Menon 		return -EINVAL;
1730aa276781SNishanth Menon 
1731aa276781SNishanth Menon 	info = handle_to_ti_sci_info(handle);
1732aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
1733aa276781SNishanth Menon 	if (!WARN_ON(!info->users))
1734aa276781SNishanth Menon 		info->users--;
1735aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
1736aa276781SNishanth Menon 
1737aa276781SNishanth Menon 	return 0;
1738aa276781SNishanth Menon }
1739aa276781SNishanth Menon EXPORT_SYMBOL_GPL(ti_sci_put_handle);
1740aa276781SNishanth Menon 
1741aa276781SNishanth Menon static void devm_ti_sci_release(struct device *dev, void *res)
1742aa276781SNishanth Menon {
1743aa276781SNishanth Menon 	const struct ti_sci_handle **ptr = res;
1744aa276781SNishanth Menon 	const struct ti_sci_handle *handle = *ptr;
1745aa276781SNishanth Menon 	int ret;
1746aa276781SNishanth Menon 
1747aa276781SNishanth Menon 	ret = ti_sci_put_handle(handle);
1748aa276781SNishanth Menon 	if (ret)
1749aa276781SNishanth Menon 		dev_err(dev, "failed to put handle %d\n", ret);
1750aa276781SNishanth Menon }
1751aa276781SNishanth Menon 
1752aa276781SNishanth Menon /**
1753aa276781SNishanth Menon  * devm_ti_sci_get_handle() - Managed get handle
1754aa276781SNishanth Menon  * @dev:	device for which we want SCI handle for.
1755aa276781SNishanth Menon  *
1756aa276781SNishanth Menon  * NOTE: This releases the handle once the device resources are
1757aa276781SNishanth Menon  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1758aa276781SNishanth Menon  * The function does not track individual clients of the framework
1759aa276781SNishanth Menon  * and is expected to be maintained by caller of TI SCI protocol library.
1760aa276781SNishanth Menon  *
1761aa276781SNishanth Menon  * Return: 0 if all went fine, else corresponding error.
1762aa276781SNishanth Menon  */
1763aa276781SNishanth Menon const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
1764aa276781SNishanth Menon {
1765aa276781SNishanth Menon 	const struct ti_sci_handle **ptr;
1766aa276781SNishanth Menon 	const struct ti_sci_handle *handle;
1767aa276781SNishanth Menon 
1768aa276781SNishanth Menon 	ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
1769aa276781SNishanth Menon 	if (!ptr)
1770aa276781SNishanth Menon 		return ERR_PTR(-ENOMEM);
1771aa276781SNishanth Menon 	handle = ti_sci_get_handle(dev);
1772aa276781SNishanth Menon 
1773aa276781SNishanth Menon 	if (!IS_ERR(handle)) {
1774aa276781SNishanth Menon 		*ptr = handle;
1775aa276781SNishanth Menon 		devres_add(dev, ptr);
1776aa276781SNishanth Menon 	} else {
1777aa276781SNishanth Menon 		devres_free(ptr);
1778aa276781SNishanth Menon 	}
1779aa276781SNishanth Menon 
1780aa276781SNishanth Menon 	return handle;
1781aa276781SNishanth Menon }
1782aa276781SNishanth Menon EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
1783aa276781SNishanth Menon 
1784912cffb4SNishanth Menon static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
1785912cffb4SNishanth Menon 				void *cmd)
1786912cffb4SNishanth Menon {
1787912cffb4SNishanth Menon 	struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
1788912cffb4SNishanth Menon 	const struct ti_sci_handle *handle = &info->handle;
1789912cffb4SNishanth Menon 
1790912cffb4SNishanth Menon 	ti_sci_cmd_core_reboot(handle);
1791912cffb4SNishanth Menon 
1792912cffb4SNishanth Menon 	/* call fail OR pass, we should not be here in the first place */
1793912cffb4SNishanth Menon 	return NOTIFY_BAD;
1794912cffb4SNishanth Menon }
1795912cffb4SNishanth Menon 
1796aa276781SNishanth Menon /* Description for K2G */
1797aa276781SNishanth Menon static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
1798e69a3553SNishanth Menon 	.default_host_id = 2,
1799aa276781SNishanth Menon 	/* Conservative duration */
1800aa276781SNishanth Menon 	.max_rx_timeout_ms = 1000,
1801aa276781SNishanth Menon 	/* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1802aa276781SNishanth Menon 	.max_msgs = 20,
1803aa276781SNishanth Menon 	.max_msg_size = 64,
1804aa276781SNishanth Menon };
1805aa276781SNishanth Menon 
1806aa276781SNishanth Menon static const struct of_device_id ti_sci_of_match[] = {
1807aa276781SNishanth Menon 	{.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
1808aa276781SNishanth Menon 	{ /* Sentinel */ },
1809aa276781SNishanth Menon };
1810aa276781SNishanth Menon MODULE_DEVICE_TABLE(of, ti_sci_of_match);
1811aa276781SNishanth Menon 
1812aa276781SNishanth Menon static int ti_sci_probe(struct platform_device *pdev)
1813aa276781SNishanth Menon {
1814aa276781SNishanth Menon 	struct device *dev = &pdev->dev;
1815aa276781SNishanth Menon 	const struct of_device_id *of_id;
1816aa276781SNishanth Menon 	const struct ti_sci_desc *desc;
1817aa276781SNishanth Menon 	struct ti_sci_xfer *xfer;
1818aa276781SNishanth Menon 	struct ti_sci_info *info = NULL;
1819aa276781SNishanth Menon 	struct ti_sci_xfers_info *minfo;
1820aa276781SNishanth Menon 	struct mbox_client *cl;
1821aa276781SNishanth Menon 	int ret = -EINVAL;
1822aa276781SNishanth Menon 	int i;
1823912cffb4SNishanth Menon 	int reboot = 0;
1824e69a3553SNishanth Menon 	u32 h_id;
1825aa276781SNishanth Menon 
1826aa276781SNishanth Menon 	of_id = of_match_device(ti_sci_of_match, dev);
1827aa276781SNishanth Menon 	if (!of_id) {
1828aa276781SNishanth Menon 		dev_err(dev, "OF data missing\n");
1829aa276781SNishanth Menon 		return -EINVAL;
1830aa276781SNishanth Menon 	}
1831aa276781SNishanth Menon 	desc = of_id->data;
1832aa276781SNishanth Menon 
1833aa276781SNishanth Menon 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1834aa276781SNishanth Menon 	if (!info)
1835aa276781SNishanth Menon 		return -ENOMEM;
1836aa276781SNishanth Menon 
1837aa276781SNishanth Menon 	info->dev = dev;
1838aa276781SNishanth Menon 	info->desc = desc;
1839e69a3553SNishanth Menon 	ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
1840e69a3553SNishanth Menon 	/* if the property is not present in DT, use a default from desc */
1841e69a3553SNishanth Menon 	if (ret < 0) {
1842e69a3553SNishanth Menon 		info->host_id = info->desc->default_host_id;
1843e69a3553SNishanth Menon 	} else {
1844e69a3553SNishanth Menon 		if (!h_id) {
1845e69a3553SNishanth Menon 			dev_warn(dev, "Host ID 0 is reserved for firmware\n");
1846e69a3553SNishanth Menon 			info->host_id = info->desc->default_host_id;
1847e69a3553SNishanth Menon 		} else {
1848e69a3553SNishanth Menon 			info->host_id = h_id;
1849e69a3553SNishanth Menon 		}
1850e69a3553SNishanth Menon 	}
1851e69a3553SNishanth Menon 
1852912cffb4SNishanth Menon 	reboot = of_property_read_bool(dev->of_node,
1853912cffb4SNishanth Menon 				       "ti,system-reboot-controller");
1854aa276781SNishanth Menon 	INIT_LIST_HEAD(&info->node);
1855aa276781SNishanth Menon 	minfo = &info->minfo;
1856aa276781SNishanth Menon 
1857aa276781SNishanth Menon 	/*
1858aa276781SNishanth Menon 	 * Pre-allocate messages
1859aa276781SNishanth Menon 	 * NEVER allocate more than what we can indicate in hdr.seq
1860aa276781SNishanth Menon 	 * if we have data description bug, force a fix..
1861aa276781SNishanth Menon 	 */
1862aa276781SNishanth Menon 	if (WARN_ON(desc->max_msgs >=
1863aa276781SNishanth Menon 		    1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
1864aa276781SNishanth Menon 		return -EINVAL;
1865aa276781SNishanth Menon 
1866aa276781SNishanth Menon 	minfo->xfer_block = devm_kcalloc(dev,
1867aa276781SNishanth Menon 					 desc->max_msgs,
1868aa276781SNishanth Menon 					 sizeof(*minfo->xfer_block),
1869aa276781SNishanth Menon 					 GFP_KERNEL);
1870aa276781SNishanth Menon 	if (!minfo->xfer_block)
1871aa276781SNishanth Menon 		return -ENOMEM;
1872aa276781SNishanth Menon 
1873a86854d0SKees Cook 	minfo->xfer_alloc_table = devm_kcalloc(dev,
1874a86854d0SKees Cook 					       BITS_TO_LONGS(desc->max_msgs),
1875a86854d0SKees Cook 					       sizeof(unsigned long),
1876aa276781SNishanth Menon 					       GFP_KERNEL);
1877aa276781SNishanth Menon 	if (!minfo->xfer_alloc_table)
1878aa276781SNishanth Menon 		return -ENOMEM;
1879aa276781SNishanth Menon 	bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
1880aa276781SNishanth Menon 
1881aa276781SNishanth Menon 	/* Pre-initialize the buffer pointer to pre-allocated buffers */
1882aa276781SNishanth Menon 	for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
1883aa276781SNishanth Menon 		xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
1884aa276781SNishanth Menon 					      GFP_KERNEL);
1885aa276781SNishanth Menon 		if (!xfer->xfer_buf)
1886aa276781SNishanth Menon 			return -ENOMEM;
1887aa276781SNishanth Menon 
1888aa276781SNishanth Menon 		xfer->tx_message.buf = xfer->xfer_buf;
1889aa276781SNishanth Menon 		init_completion(&xfer->done);
1890aa276781SNishanth Menon 	}
1891aa276781SNishanth Menon 
1892aa276781SNishanth Menon 	ret = ti_sci_debugfs_create(pdev, info);
1893aa276781SNishanth Menon 	if (ret)
1894aa276781SNishanth Menon 		dev_warn(dev, "Failed to create debug file\n");
1895aa276781SNishanth Menon 
1896aa276781SNishanth Menon 	platform_set_drvdata(pdev, info);
1897aa276781SNishanth Menon 
1898aa276781SNishanth Menon 	cl = &info->cl;
1899aa276781SNishanth Menon 	cl->dev = dev;
1900aa276781SNishanth Menon 	cl->tx_block = false;
1901aa276781SNishanth Menon 	cl->rx_callback = ti_sci_rx_callback;
1902aa276781SNishanth Menon 	cl->knows_txdone = true;
1903aa276781SNishanth Menon 
1904aa276781SNishanth Menon 	spin_lock_init(&minfo->xfer_lock);
1905aa276781SNishanth Menon 	sema_init(&minfo->sem_xfer_count, desc->max_msgs);
1906aa276781SNishanth Menon 
1907aa276781SNishanth Menon 	info->chan_rx = mbox_request_channel_byname(cl, "rx");
1908aa276781SNishanth Menon 	if (IS_ERR(info->chan_rx)) {
1909aa276781SNishanth Menon 		ret = PTR_ERR(info->chan_rx);
1910aa276781SNishanth Menon 		goto out;
1911aa276781SNishanth Menon 	}
1912aa276781SNishanth Menon 
1913aa276781SNishanth Menon 	info->chan_tx = mbox_request_channel_byname(cl, "tx");
1914aa276781SNishanth Menon 	if (IS_ERR(info->chan_tx)) {
1915aa276781SNishanth Menon 		ret = PTR_ERR(info->chan_tx);
1916aa276781SNishanth Menon 		goto out;
1917aa276781SNishanth Menon 	}
1918aa276781SNishanth Menon 	ret = ti_sci_cmd_get_revision(info);
1919aa276781SNishanth Menon 	if (ret) {
1920aa276781SNishanth Menon 		dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
1921aa276781SNishanth Menon 		goto out;
1922aa276781SNishanth Menon 	}
1923aa276781SNishanth Menon 
19249e7d756dSNishanth Menon 	ti_sci_setup_ops(info);
19259e7d756dSNishanth Menon 
1926912cffb4SNishanth Menon 	if (reboot) {
1927912cffb4SNishanth Menon 		info->nb.notifier_call = tisci_reboot_handler;
1928912cffb4SNishanth Menon 		info->nb.priority = 128;
1929912cffb4SNishanth Menon 
1930912cffb4SNishanth Menon 		ret = register_restart_handler(&info->nb);
1931912cffb4SNishanth Menon 		if (ret) {
1932912cffb4SNishanth Menon 			dev_err(dev, "reboot registration fail(%d)\n", ret);
1933912cffb4SNishanth Menon 			return ret;
1934912cffb4SNishanth Menon 		}
1935912cffb4SNishanth Menon 	}
1936912cffb4SNishanth Menon 
1937aa276781SNishanth Menon 	dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1938aa276781SNishanth Menon 		 info->handle.version.abi_major, info->handle.version.abi_minor,
1939aa276781SNishanth Menon 		 info->handle.version.firmware_revision,
1940aa276781SNishanth Menon 		 info->handle.version.firmware_description);
1941aa276781SNishanth Menon 
1942aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
1943aa276781SNishanth Menon 	list_add_tail(&info->node, &ti_sci_list);
1944aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
1945aa276781SNishanth Menon 
1946aa276781SNishanth Menon 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
1947aa276781SNishanth Menon out:
1948aa276781SNishanth Menon 	if (!IS_ERR(info->chan_tx))
1949aa276781SNishanth Menon 		mbox_free_channel(info->chan_tx);
1950aa276781SNishanth Menon 	if (!IS_ERR(info->chan_rx))
1951aa276781SNishanth Menon 		mbox_free_channel(info->chan_rx);
1952aa276781SNishanth Menon 	debugfs_remove(info->d);
1953aa276781SNishanth Menon 	return ret;
1954aa276781SNishanth Menon }
1955aa276781SNishanth Menon 
1956aa276781SNishanth Menon static int ti_sci_remove(struct platform_device *pdev)
1957aa276781SNishanth Menon {
1958aa276781SNishanth Menon 	struct ti_sci_info *info;
1959aa276781SNishanth Menon 	struct device *dev = &pdev->dev;
1960aa276781SNishanth Menon 	int ret = 0;
1961aa276781SNishanth Menon 
1962aa276781SNishanth Menon 	of_platform_depopulate(dev);
1963aa276781SNishanth Menon 
1964aa276781SNishanth Menon 	info = platform_get_drvdata(pdev);
1965aa276781SNishanth Menon 
1966912cffb4SNishanth Menon 	if (info->nb.notifier_call)
1967912cffb4SNishanth Menon 		unregister_restart_handler(&info->nb);
1968912cffb4SNishanth Menon 
1969aa276781SNishanth Menon 	mutex_lock(&ti_sci_list_mutex);
1970aa276781SNishanth Menon 	if (info->users)
1971aa276781SNishanth Menon 		ret = -EBUSY;
1972aa276781SNishanth Menon 	else
1973aa276781SNishanth Menon 		list_del(&info->node);
1974aa276781SNishanth Menon 	mutex_unlock(&ti_sci_list_mutex);
1975aa276781SNishanth Menon 
1976aa276781SNishanth Menon 	if (!ret) {
1977aa276781SNishanth Menon 		ti_sci_debugfs_destroy(pdev, info);
1978aa276781SNishanth Menon 
1979aa276781SNishanth Menon 		/* Safe to free channels since no more users */
1980aa276781SNishanth Menon 		mbox_free_channel(info->chan_tx);
1981aa276781SNishanth Menon 		mbox_free_channel(info->chan_rx);
1982aa276781SNishanth Menon 	}
1983aa276781SNishanth Menon 
1984aa276781SNishanth Menon 	return ret;
1985aa276781SNishanth Menon }
1986aa276781SNishanth Menon 
1987aa276781SNishanth Menon static struct platform_driver ti_sci_driver = {
1988aa276781SNishanth Menon 	.probe = ti_sci_probe,
1989aa276781SNishanth Menon 	.remove = ti_sci_remove,
1990aa276781SNishanth Menon 	.driver = {
1991aa276781SNishanth Menon 		   .name = "ti-sci",
1992aa276781SNishanth Menon 		   .of_match_table = of_match_ptr(ti_sci_of_match),
1993aa276781SNishanth Menon 	},
1994aa276781SNishanth Menon };
1995aa276781SNishanth Menon module_platform_driver(ti_sci_driver);
1996aa276781SNishanth Menon 
1997aa276781SNishanth Menon MODULE_LICENSE("GPL v2");
1998aa276781SNishanth Menon MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1999aa276781SNishanth Menon MODULE_AUTHOR("Nishanth Menon");
2000aa276781SNishanth Menon MODULE_ALIAS("platform:ti-sci");
2001