1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2020 Samsung Electronics Co., Ltd. 4 * Copyright 2020 Google LLC. 5 * Copyright 2024 Linaro Ltd. 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/bitmap.h> 10 #include <linux/bitops.h> 11 #include <linux/cleanup.h> 12 #include <linux/container_of.h> 13 #include <linux/delay.h> 14 #include <linux/device.h> 15 #include <linux/find.h> 16 #include <linux/firmware/samsung/exynos-acpm-protocol.h> 17 #include <linux/io.h> 18 #include <linux/iopoll.h> 19 #include <linux/ktime.h> 20 #include <linux/mailbox/exynos-message.h> 21 #include <linux/mailbox_client.h> 22 #include <linux/module.h> 23 #include <linux/mutex.h> 24 #include <linux/math.h> 25 #include <linux/of.h> 26 #include <linux/of_address.h> 27 #include <linux/of_platform.h> 28 #include <linux/platform_device.h> 29 #include <linux/slab.h> 30 #include <linux/types.h> 31 32 #include "exynos-acpm.h" 33 #include "exynos-acpm-dvfs.h" 34 #include "exynos-acpm-pmic.h" 35 #include "exynos-acpm-tmu.h" 36 37 #define ACPM_PROTOCOL_SEQNUM GENMASK(21, 16) 38 39 #define ACPM_POLL_TIMEOUT_US (100 * USEC_PER_MSEC) 40 #define ACPM_TX_TIMEOUT_US 500000 41 42 #define ACPM_GS101_INITDATA_BASE 0xa000 43 44 /** 45 * struct acpm_shmem - shared memory configuration information. 46 * @reserved: unused fields. 47 * @chans: offset to array of struct acpm_chan_shmem. 48 * @reserved1: unused fields. 49 * @num_chans: number of channels. 50 */ 51 struct acpm_shmem { 52 u32 reserved[2]; 53 u32 chans; 54 u32 reserved1[3]; 55 u32 num_chans; 56 }; 57 58 /** 59 * struct acpm_chan_shmem - descriptor of a shared memory channel. 60 * 61 * @id: channel ID. 62 * @reserved: unused fields. 63 * @rx_rear: rear pointer of APM RX queue (TX for AP). 64 * @rx_front: front pointer of APM RX queue (TX for AP). 65 * @rx_base: base address of APM RX queue (TX for AP). 66 * @reserved1: unused fields. 67 * @tx_rear: rear pointer of APM TX queue (RX for AP). 68 * @tx_front: front pointer of APM TX queue (RX for AP). 69 * @tx_base: base address of APM TX queue (RX for AP). 70 * @qlen: queue length. Applies to both TX/RX queues. 71 * @mlen: message length. Applies to both TX/RX queues. 72 * @reserved2: unused fields. 73 * @poll_completion: true when the channel works on polling. 74 */ 75 struct acpm_chan_shmem { 76 u32 id; 77 u32 reserved[3]; 78 u32 rx_rear; 79 u32 rx_front; 80 u32 rx_base; 81 u32 reserved1[3]; 82 u32 tx_rear; 83 u32 tx_front; 84 u32 tx_base; 85 u32 qlen; 86 u32 mlen; 87 u32 reserved2[2]; 88 u32 poll_completion; 89 }; 90 91 /** 92 * struct acpm_queue - exynos acpm queue. 93 * 94 * @rear: rear address of the queue. 95 * @front: front address of the queue. 96 * @base: base address of the queue. 97 */ 98 struct acpm_queue { 99 void __iomem *rear; 100 void __iomem *front; 101 void __iomem *base; 102 }; 103 104 /** 105 * struct acpm_rx_data - RX queue data. 106 * 107 * @cmd: pointer to where the data shall be saved. 108 * @cmdcnt: allocated capacity of the @cmd buffer in 32-bit words. 109 * @rxcnt: expected length of the response in 32-bit words. 110 * @completed: flag indicating if the firmware response has been fully 111 * processed. 112 */ 113 struct acpm_rx_data { 114 u32 *cmd __counted_by_ptr(cmdcnt); 115 size_t cmdcnt; 116 size_t rxcnt; 117 bool completed; 118 }; 119 120 #define ACPM_SEQNUM_MAX 64 121 122 /** 123 * struct acpm_chan - driver internal representation of a channel. 124 * @cl: mailbox client. 125 * @chan: mailbox channel. 126 * @acpm: pointer to driver private data. 127 * @tx: TX queue. The enqueue is done by the host. 128 * - front index is written by the host. 129 * - rear index is written by the firmware. 130 * 131 * @rx: RX queue. The enqueue is done by the firmware. 132 * - front index is written by the firmware. 133 * - rear index is written by the host. 134 * @tx_lock: protects TX queue. 135 * @rx_lock: protects RX queue. 136 * @qlen: queue length. Applies to both TX/RX queues. 137 * @mlen: message length. Applies to both TX/RX queues. 138 * @seqnum: sequence number of the last message enqueued on TX queue. 139 * @id: channel ID. 140 * @poll_completion: indicates if the transfer needs to be polled for 141 * completion or interrupt mode is used. 142 * @bitmap_seqnum: bitmap that tracks the messages on the TX/RX queues. 143 * @rx_data: internal buffer used to drain the RX queue. 144 */ 145 struct acpm_chan { 146 struct mbox_client cl; 147 struct mbox_chan *chan; 148 struct acpm_info *acpm; 149 struct acpm_queue tx; 150 struct acpm_queue rx; 151 struct mutex tx_lock; 152 struct mutex rx_lock; 153 154 unsigned int qlen; 155 unsigned int mlen; 156 u8 seqnum; 157 u8 id; 158 bool poll_completion; 159 160 DECLARE_BITMAP(bitmap_seqnum, ACPM_SEQNUM_MAX - 1); 161 struct acpm_rx_data rx_data[ACPM_SEQNUM_MAX]; 162 }; 163 164 /** 165 * struct acpm_info - driver's private data. 166 * @shmem: pointer to the SRAM configuration data. 167 * @sram_base: base address of SRAM. 168 * @chans: pointer to the ACPM channel parameters retrieved from SRAM. 169 * @dev: pointer to the exynos-acpm device. 170 * @handle: instance of acpm_handle to send to clients. 171 * @num_chans: number of channels available for this controller. 172 */ 173 struct acpm_info { 174 struct acpm_shmem __iomem *shmem; 175 void __iomem *sram_base; 176 struct acpm_chan *chans; 177 struct device *dev; 178 struct acpm_handle handle; 179 u32 num_chans; 180 }; 181 182 /** 183 * struct acpm_match_data - of_device_id data. 184 * @initdata_base: offset in SRAM where the channels configuration resides. 185 * @acpm_clk_dev_name: base name for the ACPM clocks device that we're registering. 186 */ 187 struct acpm_match_data { 188 loff_t initdata_base; 189 const char *acpm_clk_dev_name; 190 }; 191 192 #define client_to_acpm_chan(c) container_of(c, struct acpm_chan, cl) 193 #define handle_to_acpm_info(h) container_of(h, struct acpm_info, handle) 194 195 /** 196 * acpm_get_saved_rx() - get the response if it was already saved. 197 * @achan: ACPM channel info. 198 * @xfer: reference to the transfer to get response for. 199 * @tx_seqnum: xfer TX sequence number. 200 */ 201 static void acpm_get_saved_rx(struct acpm_chan *achan, 202 const struct acpm_xfer *xfer, u32 tx_seqnum) 203 { 204 const struct acpm_rx_data *rx_data = &achan->rx_data[tx_seqnum - 1]; 205 u32 rx_seqnum; 206 207 if (!rx_data->rxcnt) 208 return; 209 210 rx_seqnum = FIELD_GET(ACPM_PROTOCOL_SEQNUM, rx_data->cmd[0]); 211 212 if (rx_seqnum == tx_seqnum) 213 memcpy(xfer->rxd, rx_data->cmd, xfer->rxcnt * sizeof(*xfer->rxd)); 214 } 215 216 /** 217 * acpm_get_rx() - get response from RX queue. 218 * @achan: ACPM channel info. 219 * @xfer: reference to the transfer to get response for. 220 * @native_match: pointer to a boolean set to true if the thread natively 221 * processed its own sequence number during this call. 222 * 223 * Return: 0 on success, -errno otherwise. 224 */ 225 static int acpm_get_rx(struct acpm_chan *achan, const struct acpm_xfer *xfer, 226 bool *native_match) 227 { 228 u32 rx_front, rx_seqnum, tx_seqnum, seqnum; 229 const void __iomem *base, *addr; 230 struct acpm_rx_data *rx_data; 231 u32 i, val, mlen; 232 233 *native_match = false; 234 235 guard(mutex)(&achan->rx_lock); 236 237 rx_front = readl(achan->rx.front); 238 i = readl(achan->rx.rear); 239 240 tx_seqnum = FIELD_GET(ACPM_PROTOCOL_SEQNUM, xfer->txd[0]); 241 242 if (i == rx_front) 243 return 0; 244 245 base = achan->rx.base; 246 mlen = achan->mlen; 247 248 /* Drain RX queue. */ 249 do { 250 /* Read RX seqnum. */ 251 addr = base + mlen * i; 252 val = readl(addr); 253 254 rx_seqnum = FIELD_GET(ACPM_PROTOCOL_SEQNUM, val); 255 if (!rx_seqnum) 256 return -EIO; 257 /* 258 * mssg seqnum starts with value 1, whereas the driver considers 259 * the first mssg at index 0. 260 */ 261 seqnum = rx_seqnum - 1; 262 rx_data = &achan->rx_data[seqnum]; 263 264 if (rx_data->rxcnt) { 265 if (rx_seqnum == tx_seqnum) { 266 __ioread32_copy(xfer->rxd, addr, xfer->rxcnt); 267 /* 268 * Signal completion to the polling thread. 269 * Pairs with smp_load_acquire() in polling 270 * loop. 271 */ 272 smp_store_release(&rx_data->completed, true); 273 *native_match = true; 274 } else { 275 /* 276 * The RX data corresponds to another request. 277 * Save the data to drain the queue, but don't 278 * clear yet the bitmap. It will be cleared 279 * after the response is copied to the request. 280 */ 281 __ioread32_copy(rx_data->cmd, addr, 282 rx_data->rxcnt); 283 /* 284 * Signal completion to the polling thread. 285 * Pairs with smp_load_acquire() in polling 286 * loop. 287 */ 288 smp_store_release(&rx_data->completed, true); 289 } 290 } else { 291 /* 292 * Signal completion to the polling thread. 293 * Pairs with smp_load_acquire() in polling loop. 294 */ 295 smp_store_release(&rx_data->completed, true); 296 if (rx_seqnum == tx_seqnum) 297 *native_match = true; 298 } 299 300 i = (i + 1) % achan->qlen; 301 } while (i != rx_front); 302 303 /* We saved all responses, mark RX empty. */ 304 writel(rx_front, achan->rx.rear); 305 306 return 0; 307 } 308 309 /** 310 * acpm_dequeue_by_polling() - RX dequeue by polling. 311 * @achan: ACPM channel info. 312 * @xfer: reference to the transfer being waited for. 313 * 314 * Return: 0 on success, -errno otherwise. 315 */ 316 static int acpm_dequeue_by_polling(struct acpm_chan *achan, 317 const struct acpm_xfer *xfer) 318 { 319 struct device *dev = achan->acpm->dev; 320 bool native_match; 321 ktime_t timeout; 322 u32 seqnum; 323 int ret; 324 325 seqnum = FIELD_GET(ACPM_PROTOCOL_SEQNUM, xfer->txd[0]); 326 327 timeout = ktime_add_us(ktime_get(), ACPM_POLL_TIMEOUT_US); 328 do { 329 ret = acpm_get_rx(achan, xfer, &native_match); 330 if (ret) 331 return ret; 332 333 /* 334 * Safely check if our specific transaction has been processed. 335 * smp_load_acquire prevents the CPU from speculatively 336 * executing subsequent instructions before the transaction is 337 * synchronized. 338 */ 339 if (smp_load_acquire(&achan->rx_data[seqnum - 1].completed)) { 340 /* Retrieve payload if another thread cached it for us */ 341 if (!native_match) 342 acpm_get_saved_rx(achan, xfer, seqnum); 343 344 /* Relinquish ownership of the sequence slot */ 345 clear_bit_unlock(seqnum - 1, achan->bitmap_seqnum); 346 return 0; 347 } 348 349 /* Determined experimentally. */ 350 udelay(20); 351 } while (ktime_before(ktime_get(), timeout)); 352 353 dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx.\n", 354 achan->id, seqnum, achan->bitmap_seqnum[0]); 355 356 return -ETIME; 357 } 358 359 /** 360 * acpm_wait_for_queue_slots() - wait for queue slots. 361 * 362 * @achan: ACPM channel info. 363 * @next_tx_front: next front index of the TX queue. 364 * 365 * Return: 0 on success, -errno otherwise. 366 */ 367 static int acpm_wait_for_queue_slots(struct acpm_chan *achan, u32 next_tx_front) 368 { 369 u32 val, ret; 370 371 /* 372 * Wait for RX front to keep up with TX front. Make sure there's at 373 * least one element between them. 374 */ 375 ret = readl_poll_timeout(achan->rx.front, val, next_tx_front != val, 0, 376 ACPM_TX_TIMEOUT_US); 377 if (ret) { 378 dev_err(achan->acpm->dev, "RX front can not keep up with TX front.\n"); 379 return ret; 380 } 381 382 ret = readl_poll_timeout(achan->tx.rear, val, next_tx_front != val, 0, 383 ACPM_TX_TIMEOUT_US); 384 if (ret) 385 dev_err(achan->acpm->dev, "TX queue is full.\n"); 386 387 return ret; 388 } 389 390 /** 391 * acpm_prepare_xfer() - prepare a transfer before writing the message to the 392 * TX queue. 393 * @achan: ACPM channel info. 394 * @xfer: reference to the transfer being prepared. 395 * 396 * Return: 0 on success, -errno otherwise. 397 */ 398 static int acpm_prepare_xfer(struct acpm_chan *achan, 399 const struct acpm_xfer *xfer) 400 { 401 struct acpm_rx_data *rx_data; 402 u32 *txd = (u32 *)xfer->txd; 403 unsigned long size = ACPM_SEQNUM_MAX - 1; 404 unsigned long bit = achan->seqnum; 405 406 bit = find_next_zero_bit(achan->bitmap_seqnum, size, bit); 407 if (bit >= size) { 408 bit = find_first_zero_bit(achan->bitmap_seqnum, size); 409 if (bit >= size) { 410 dev_err_ratelimited(achan->acpm->dev, 411 "ACPM sequence number pool exhausted\n"); 412 return -EBUSY; 413 } 414 } 415 416 /* 417 * Execute the atomic set to formally claim the bit and establish 418 * LKMM Acquire semantics against the RX thread's clear_bit_unlock(). 419 * A loop is unnecessary because allocations are strictly serialized 420 * by tx_lock. 421 */ 422 if (WARN_ON_ONCE(test_and_set_bit_lock(bit, achan->bitmap_seqnum))) 423 return -EIO; 424 425 /* Flag the index based on seqnum. (seqnum: 1~63, bitmap: 0~62) */ 426 achan->seqnum = bit + 1; 427 txd[0] |= FIELD_PREP(ACPM_PROTOCOL_SEQNUM, achan->seqnum); 428 429 /* Clear data for upcoming responses */ 430 rx_data = &achan->rx_data[bit]; 431 rx_data->completed = false; 432 memset(rx_data->cmd, 0, sizeof(*rx_data->cmd) * rx_data->cmdcnt); 433 /* zero means no response expected */ 434 rx_data->rxcnt = xfer->rxcnt; 435 436 return 0; 437 } 438 439 /** 440 * acpm_wait_for_message_response - an helper to group all possible ways of 441 * waiting for a synchronous message response. 442 * 443 * @achan: ACPM channel info. 444 * @xfer: reference to the transfer being waited for. 445 * 446 * Return: 0 on success, -errno otherwise. 447 */ 448 static int acpm_wait_for_message_response(struct acpm_chan *achan, 449 const struct acpm_xfer *xfer) 450 { 451 /* Just polling mode supported for now. */ 452 return acpm_dequeue_by_polling(achan, xfer); 453 } 454 455 /** 456 * acpm_do_xfer() - do one transfer. 457 * @handle: pointer to the acpm handle. 458 * @xfer: transfer to initiate and wait for response. 459 * 460 * Return: 0 on success, -errno otherwise. 461 */ 462 int acpm_do_xfer(struct acpm_handle *handle, const struct acpm_xfer *xfer) 463 { 464 struct acpm_info *acpm = handle_to_acpm_info(handle); 465 struct exynos_mbox_msg msg; 466 struct acpm_chan *achan; 467 u32 idx, tx_front; 468 int ret; 469 470 if (xfer->acpm_chan_id >= acpm->num_chans) 471 return -EINVAL; 472 473 achan = &acpm->chans[xfer->acpm_chan_id]; 474 475 if (!xfer->txd || 476 (xfer->txcnt * sizeof(*xfer->txd) > achan->mlen) || 477 (xfer->rxcnt * sizeof(*xfer->rxd) > achan->mlen)) 478 return -EINVAL; 479 480 if (!achan->poll_completion) { 481 dev_err(achan->acpm->dev, "Interrupt mode not supported\n"); 482 return -EOPNOTSUPP; 483 } 484 485 msg.chan_id = xfer->acpm_chan_id; 486 msg.chan_type = EXYNOS_MBOX_CHAN_TYPE_DOORBELL; 487 488 scoped_guard(mutex, &achan->tx_lock) { 489 tx_front = readl(achan->tx.front); 490 idx = (tx_front + 1) % achan->qlen; 491 492 ret = acpm_wait_for_queue_slots(achan, idx); 493 if (ret) 494 return ret; 495 496 ret = acpm_prepare_xfer(achan, xfer); 497 if (ret) 498 return ret; 499 500 /* Write TX command. */ 501 __iowrite32_copy(achan->tx.base + achan->mlen * tx_front, 502 xfer->txd, xfer->txcnt); 503 504 /* Advance TX front. */ 505 writel(idx, achan->tx.front); 506 507 ret = mbox_send_message(achan->chan, (void *)&msg); 508 if (ret < 0) 509 return ret; 510 511 mbox_client_txdone(achan->chan, 0); 512 } 513 514 return acpm_wait_for_message_response(achan, xfer); 515 } 516 517 /** 518 * acpm_set_xfer() - initialize an ACPM IPC transfer structure. 519 * @xfer: pointer to the ACPM transfer structure that is being initialized. 520 * @cmd: pointer to the buffer containing the command to be transmitted 521 * to the ACPM firmware. 522 * @cmdcnt: length of the command in 32-bit words. 523 * @acpm_chan_id: mailbox channel identifier. 524 * @response: boolean flag indicating whether the kernel expects the ACPM 525 * firmware to send a reply to this specific command. 526 */ 527 void acpm_set_xfer(struct acpm_xfer *xfer, u32 *cmd, size_t cmdcnt, 528 unsigned int acpm_chan_id, bool response) 529 { 530 xfer->acpm_chan_id = acpm_chan_id; 531 xfer->txcnt = cmdcnt; 532 xfer->txd = cmd; 533 534 if (response) { 535 xfer->rxcnt = cmdcnt; 536 xfer->rxd = cmd; 537 } else { 538 xfer->rxcnt = 0; 539 xfer->rxd = NULL; 540 } 541 } 542 543 /** 544 * acpm_chan_shmem_get_params() - get channel parameters and addresses of the 545 * TX/RX queues. 546 * @achan: ACPM channel info. 547 * @chan_shmem: __iomem pointer to a channel described in shared memory. 548 */ 549 static void acpm_chan_shmem_get_params(struct acpm_chan *achan, 550 struct acpm_chan_shmem __iomem *chan_shmem) 551 { 552 void __iomem *base = achan->acpm->sram_base; 553 struct acpm_queue *rx = &achan->rx; 554 struct acpm_queue *tx = &achan->tx; 555 556 achan->mlen = readl(&chan_shmem->mlen); 557 achan->poll_completion = readl(&chan_shmem->poll_completion); 558 achan->id = readl(&chan_shmem->id); 559 achan->qlen = readl(&chan_shmem->qlen); 560 561 tx->base = base + readl(&chan_shmem->rx_base); 562 tx->rear = base + readl(&chan_shmem->rx_rear); 563 tx->front = base + readl(&chan_shmem->rx_front); 564 565 rx->base = base + readl(&chan_shmem->tx_base); 566 rx->rear = base + readl(&chan_shmem->tx_rear); 567 rx->front = base + readl(&chan_shmem->tx_front); 568 569 dev_vdbg(achan->acpm->dev, "ID = %d poll = %d, mlen = %d, qlen = %d\n", 570 achan->id, achan->poll_completion, achan->mlen, achan->qlen); 571 } 572 573 /** 574 * acpm_achan_alloc_cmds() - allocate buffers for retrieving data from the ACPM 575 * firmware. 576 * @achan: ACPM channel info. 577 * 578 * Return: 0 on success, -errno otherwise. 579 */ 580 static int acpm_achan_alloc_cmds(struct acpm_chan *achan) 581 { 582 struct device *dev = achan->acpm->dev; 583 struct acpm_rx_data *rx_data; 584 size_t cmd_size, cmdcnt; 585 int i; 586 587 if (achan->mlen == 0) 588 return 0; 589 590 cmd_size = sizeof(*(achan->rx_data[0].cmd)); 591 cmdcnt = DIV_ROUND_UP_ULL(achan->mlen, cmd_size); 592 593 for (i = 0; i < ACPM_SEQNUM_MAX; i++) { 594 rx_data = &achan->rx_data[i]; 595 rx_data->cmdcnt = cmdcnt; 596 rx_data->cmd = devm_kcalloc(dev, cmdcnt, cmd_size, GFP_KERNEL); 597 if (!rx_data->cmd) 598 return -ENOMEM; 599 } 600 601 return 0; 602 } 603 604 /** 605 * acpm_free_mbox_chans() - free mailbox channels. 606 * @data: pointer to driver data. 607 */ 608 static void acpm_free_mbox_chans(void *data) 609 { 610 struct acpm_info *acpm = data; 611 int i; 612 613 for (i = 0; i < acpm->num_chans; i++) 614 if (!IS_ERR_OR_NULL(acpm->chans[i].chan)) 615 mbox_free_channel(acpm->chans[i].chan); 616 } 617 618 /** 619 * acpm_channels_init() - initialize channels based on the configuration data in 620 * the shared memory. 621 * @acpm: pointer to driver data. 622 * 623 * Return: 0 on success, -errno otherwise. 624 */ 625 static int acpm_channels_init(struct acpm_info *acpm) 626 { 627 struct acpm_shmem __iomem *shmem = acpm->shmem; 628 struct acpm_chan_shmem __iomem *chans_shmem; 629 struct device *dev = acpm->dev; 630 int i, ret; 631 632 acpm->num_chans = readl(&shmem->num_chans); 633 acpm->chans = devm_kcalloc(dev, acpm->num_chans, sizeof(*acpm->chans), 634 GFP_KERNEL); 635 if (!acpm->chans) 636 return -ENOMEM; 637 638 ret = devm_add_action_or_reset(dev, acpm_free_mbox_chans, acpm); 639 if (ret) 640 return dev_err_probe(dev, ret, "Failed to add mbox free action.\n"); 641 642 chans_shmem = acpm->sram_base + readl(&shmem->chans); 643 644 for (i = 0; i < acpm->num_chans; i++) { 645 struct acpm_chan_shmem __iomem *chan_shmem = &chans_shmem[i]; 646 struct acpm_chan *achan = &acpm->chans[i]; 647 struct mbox_client *cl = &achan->cl; 648 649 achan->acpm = acpm; 650 651 acpm_chan_shmem_get_params(achan, chan_shmem); 652 653 ret = acpm_achan_alloc_cmds(achan); 654 if (ret) 655 return ret; 656 657 mutex_init(&achan->rx_lock); 658 mutex_init(&achan->tx_lock); 659 660 cl->dev = dev; 661 662 achan->chan = mbox_request_channel(cl, 0); 663 if (IS_ERR(achan->chan)) 664 return PTR_ERR(achan->chan); 665 } 666 667 return 0; 668 } 669 670 static void acpm_clk_pdev_unregister(void *data) 671 { 672 platform_device_unregister(data); 673 } 674 675 static const struct acpm_ops exynos_acpm_driver_ops = { 676 .dvfs = { 677 .set_rate = acpm_dvfs_set_rate, 678 .get_rate = acpm_dvfs_get_rate, 679 }, 680 681 .pmic = { 682 .read_reg = acpm_pmic_read_reg, 683 .bulk_read = acpm_pmic_bulk_read, 684 .write_reg = acpm_pmic_write_reg, 685 .bulk_write = acpm_pmic_bulk_write, 686 .update_reg = acpm_pmic_update_reg, 687 }, 688 689 .tmu = { 690 .init = acpm_tmu_init, 691 .read_temp = acpm_tmu_read_temp, 692 .set_threshold = acpm_tmu_set_threshold, 693 .set_interrupt_enable = acpm_tmu_set_interrupt_enable, 694 .tz_control = acpm_tmu_tz_control, 695 .clear_tz_irq = acpm_tmu_clear_tz_irq, 696 .suspend = acpm_tmu_suspend, 697 .resume = acpm_tmu_resume, 698 }, 699 }; 700 701 static int acpm_probe(struct platform_device *pdev) 702 { 703 const struct acpm_match_data *match_data; 704 struct platform_device *acpm_clk_pdev; 705 struct device *dev = &pdev->dev; 706 struct device_node *shmem; 707 struct acpm_info *acpm; 708 resource_size_t size; 709 struct resource res; 710 int ret; 711 712 acpm = devm_kzalloc(dev, sizeof(*acpm), GFP_KERNEL); 713 if (!acpm) 714 return -ENOMEM; 715 716 shmem = of_parse_phandle(dev->of_node, "shmem", 0); 717 ret = of_address_to_resource(shmem, 0, &res); 718 of_node_put(shmem); 719 if (ret) 720 return dev_err_probe(dev, ret, 721 "Failed to get shared memory.\n"); 722 723 size = resource_size(&res); 724 acpm->sram_base = devm_ioremap(dev, res.start, size); 725 if (!acpm->sram_base) 726 return dev_err_probe(dev, -ENOMEM, 727 "Failed to ioremap shared memory.\n"); 728 729 match_data = of_device_get_match_data(dev); 730 if (!match_data) 731 return dev_err_probe(dev, -EINVAL, 732 "Failed to get match data.\n"); 733 734 acpm->shmem = acpm->sram_base + match_data->initdata_base; 735 acpm->dev = dev; 736 737 ret = acpm_channels_init(acpm); 738 if (ret) 739 return ret; 740 741 acpm->handle.ops = &exynos_acpm_driver_ops; 742 743 platform_set_drvdata(pdev, acpm); 744 745 acpm_clk_pdev = platform_device_register_data(dev, 746 match_data->acpm_clk_dev_name, 747 PLATFORM_DEVID_NONE, NULL, 0); 748 if (IS_ERR(acpm_clk_pdev)) 749 return dev_err_probe(dev, PTR_ERR(acpm_clk_pdev), 750 "Failed to register ACPM clocks device.\n"); 751 752 ret = devm_add_action_or_reset(dev, acpm_clk_pdev_unregister, 753 acpm_clk_pdev); 754 if (ret) 755 return dev_err_probe(dev, ret, "Failed to add devm action.\n"); 756 757 return devm_of_platform_populate(dev); 758 } 759 760 /** 761 * acpm_handle_put() - release the handle acquired by acpm_get_by_phandle. 762 * @handle: Handle acquired by acpm_get_by_phandle. 763 */ 764 static void acpm_handle_put(struct acpm_handle *handle) 765 { 766 struct acpm_info *acpm = handle_to_acpm_info(handle); 767 struct device *dev = acpm->dev; 768 769 module_put(dev->driver->owner); 770 /* Drop reference taken with of_find_device_by_node(). */ 771 put_device(dev); 772 } 773 774 /** 775 * devm_acpm_release() - devres release method. 776 * @dev: pointer to device. 777 * @res: pointer to resource. 778 */ 779 static void devm_acpm_release(struct device *dev, void *res) 780 { 781 acpm_handle_put(*(struct acpm_handle **)res); 782 } 783 784 /** 785 * acpm_get_by_node() - get the ACPM handle using node pointer. 786 * @dev: device pointer requesting ACPM handle. 787 * @np: ACPM device tree node. 788 * 789 * Return: pointer to handle on success, ERR_PTR(-errno) otherwise. 790 * 791 * Note: handle CANNOT be pointer to const 792 */ 793 static struct acpm_handle *acpm_get_by_node(struct device *dev, 794 struct device_node *np) 795 { 796 struct platform_device *pdev; 797 struct device_link *link; 798 struct acpm_info *acpm; 799 800 pdev = of_find_device_by_node(np); 801 if (!pdev) 802 return ERR_PTR(-EPROBE_DEFER); 803 804 acpm = platform_get_drvdata(pdev); 805 if (!acpm) { 806 platform_device_put(pdev); 807 return ERR_PTR(-EPROBE_DEFER); 808 } 809 810 if (!try_module_get(pdev->dev.driver->owner)) { 811 platform_device_put(pdev); 812 return ERR_PTR(-EPROBE_DEFER); 813 } 814 815 link = device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_SUPPLIER); 816 if (!link) { 817 dev_err(&pdev->dev, 818 "Failed to create device link to consumer %s.\n", 819 dev_name(dev)); 820 platform_device_put(pdev); 821 module_put(pdev->dev.driver->owner); 822 return ERR_PTR(-EINVAL); 823 } 824 825 return &acpm->handle; 826 } 827 828 /** 829 * devm_acpm_get_by_node() - managed get handle using node pointer. 830 * @dev: device pointer requesting ACPM handle. 831 * @np: ACPM device tree node. 832 * 833 * Return: pointer to handle on success, ERR_PTR(-errno) otherwise. 834 */ 835 struct acpm_handle *devm_acpm_get_by_node(struct device *dev, 836 struct device_node *np) 837 { 838 struct acpm_handle **ptr, *handle; 839 840 ptr = devres_alloc(devm_acpm_release, sizeof(*ptr), GFP_KERNEL); 841 if (!ptr) 842 return ERR_PTR(-ENOMEM); 843 844 handle = acpm_get_by_node(dev, np); 845 if (!IS_ERR(handle)) { 846 *ptr = handle; 847 devres_add(dev, ptr); 848 } else { 849 devres_free(ptr); 850 } 851 852 return handle; 853 } 854 EXPORT_SYMBOL_GPL(devm_acpm_get_by_node); 855 856 /** 857 * devm_acpm_get_by_phandle - Resource managed lookup of the standardized 858 * "samsung,acpm-ipc" handle. 859 * @dev: consumer device 860 * 861 * Return: pointer to handle on success, ERR_PTR(-errno) otherwise. 862 */ 863 struct acpm_handle *devm_acpm_get_by_phandle(struct device *dev) 864 { 865 struct acpm_handle *handle; 866 struct device_node *np; 867 868 np = of_parse_phandle(dev->of_node, "samsung,acpm-ipc", 0); 869 if (!np) 870 return ERR_PTR(-ENODEV); 871 872 handle = devm_acpm_get_by_node(dev, np); 873 of_node_put(np); 874 875 return handle; 876 } 877 EXPORT_SYMBOL_GPL(devm_acpm_get_by_phandle); 878 879 static const struct acpm_match_data acpm_gs101 = { 880 .initdata_base = ACPM_GS101_INITDATA_BASE, 881 .acpm_clk_dev_name = "gs101-acpm-clk", 882 }; 883 884 static const struct of_device_id acpm_match[] = { 885 { 886 .compatible = "google,gs101-acpm-ipc", 887 .data = &acpm_gs101, 888 }, 889 {}, 890 }; 891 MODULE_DEVICE_TABLE(of, acpm_match); 892 893 static struct platform_driver acpm_driver = { 894 .probe = acpm_probe, 895 .driver = { 896 .name = "exynos-acpm-protocol", 897 .of_match_table = acpm_match, 898 }, 899 }; 900 module_platform_driver(acpm_driver); 901 902 MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@linaro.org>"); 903 MODULE_DESCRIPTION("Samsung Exynos ACPM mailbox protocol driver"); 904 MODULE_LICENSE("GPL"); 905