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