Lines Matching refs:nvec

33 #include "nvec.h"
80 .name = "nvec-kbd",
83 .name = "nvec-mouse",
86 .name = "nvec-power",
90 .name = "nvec-power",
94 .name = "nvec-paz00",
99 * nvec_register_notifier - Register a notifier with nvec
100 * @nvec: A &struct nvec_chip
104 * Registers a notifier with @nvec. The notifier will be added to an atomic
108 int nvec_register_notifier(struct nvec_chip *nvec, struct notifier_block *nb,
111 return atomic_notifier_chain_register(&nvec->notifier_list, nb);
116 * nvec_unregister_notifier - Unregister a notifier with nvec
117 * @nvec: A &struct nvec_chip
120 * Unregisters a notifier with @nvec. The notifier will be removed from the
123 int nvec_unregister_notifier(struct nvec_chip *nvec, struct notifier_block *nb)
125 return atomic_notifier_chain_unregister(&nvec->notifier_list, nb);
138 struct nvec_chip *nvec = container_of(nb, struct nvec_chip,
145 dev_warn(nvec->dev, "unhandled msg type %ld\n", event_type);
154 * @nvec: A &struct nvec_chip
158 * @nvec. The result shall be passed to nvec_msg_free() if no longer
166 static struct nvec_msg *nvec_msg_alloc(struct nvec_chip *nvec,
172 if (atomic_xchg(&nvec->msg_pool[i].used, 1) == 0) {
173 dev_vdbg(nvec->dev, "INFO: Allocate %i\n", i);
174 return &nvec->msg_pool[i];
178 dev_err(nvec->dev, "could not allocate %s buffer\n",
186 * @nvec: A &struct nvec_chip
187 * @msg: A message (must be allocated by nvec_msg_alloc() and belong to @nvec)
191 void nvec_msg_free(struct nvec_chip *nvec, struct nvec_msg *msg)
193 if (msg != &nvec->tx_scratch)
194 dev_vdbg(nvec->dev, "INFO: Free %ti\n", msg - nvec->msg_pool);
231 * @nvec: A &struct nvec_chip
236 static void nvec_gpio_set_value(struct nvec_chip *nvec, int value)
238 dev_dbg(nvec->dev, "GPIO changed from %u to %u\n",
239 gpiod_get_value(nvec->gpiod), value);
240 gpiod_set_value(nvec->gpiod, value);
245 * @nvec: An nvec_chip instance
253 * occurred, the nvec driver may print an error.
255 int nvec_write_async(struct nvec_chip *nvec, const unsigned char *data,
261 msg = nvec_msg_alloc(nvec, NVEC_MSG_TX);
270 spin_lock_irqsave(&nvec->tx_lock, flags);
271 list_add_tail(&msg->node, &nvec->tx_data);
272 spin_unlock_irqrestore(&nvec->tx_lock, flags);
274 schedule_work(&nvec->tx_work);
281 * nvec_write_sync - Write a message to nvec and read the response
282 * @nvec: An &struct nvec_chip
297 int nvec_write_sync(struct nvec_chip *nvec,
301 mutex_lock(&nvec->sync_write_mutex);
304 nvec->sync_write_pending = (data[1] << 8) + data[0];
306 if (nvec_write_async(nvec, data, size) < 0) {
307 mutex_unlock(&nvec->sync_write_mutex);
311 dev_dbg(nvec->dev, "nvec_sync_write: 0x%04x\n",
312 nvec->sync_write_pending);
313 if (!(wait_for_completion_timeout(&nvec->sync_write,
315 dev_warn(nvec->dev,
317 mutex_unlock(&nvec->sync_write_mutex);
321 dev_dbg(nvec->dev, "nvec_sync_write: pong!\n");
323 *msg = nvec->last_sync_msg;
325 mutex_unlock(&nvec->sync_write_mutex);
333 * @nvec: nvec handle
338 static void nvec_toggle_global_events(struct nvec_chip *nvec, bool state)
342 nvec_write_async(nvec, global_events, 3);
376 struct nvec_chip *nvec = container_of(work, struct nvec_chip, tx_work);
381 spin_lock_irqsave(&nvec->tx_lock, flags);
382 while (!list_empty(&nvec->tx_data)) {
383 msg = list_first_entry(&nvec->tx_data, struct nvec_msg, node);
384 spin_unlock_irqrestore(&nvec->tx_lock, flags);
385 nvec_gpio_set_value(nvec, 0);
386 err = wait_for_completion_interruptible_timeout(&nvec->ec_transfer,
390 dev_warn(nvec->dev, "timeout waiting for ec transfer\n");
391 nvec_gpio_set_value(nvec, 1);
395 spin_lock_irqsave(&nvec->tx_lock, flags);
399 nvec_msg_free(nvec, msg);
402 spin_unlock_irqrestore(&nvec->tx_lock, flags);
407 * @nvec: A &struct nvec_chip
408 * @msg: A message received by @nvec
413 static int parse_msg(struct nvec_chip *nvec, struct nvec_msg *msg)
416 dev_err(nvec->dev, "ec responded %*ph\n", 4, msg->data);
425 atomic_notifier_call_chain(&nvec->notifier_list, msg->data[0] & 0x8f,
440 struct nvec_chip *nvec = container_of(work, struct nvec_chip, rx_work);
444 spin_lock_irqsave(&nvec->rx_lock, flags);
445 while (!list_empty(&nvec->rx_data)) {
446 msg = list_first_entry(&nvec->rx_data, struct nvec_msg, node);
448 spin_unlock_irqrestore(&nvec->rx_lock, flags);
450 if (nvec->sync_write_pending ==
452 dev_dbg(nvec->dev, "sync write completed!\n");
453 nvec->sync_write_pending = 0;
454 nvec->last_sync_msg = msg;
455 complete(&nvec->sync_write);
457 parse_msg(nvec, msg);
458 nvec_msg_free(nvec, msg);
460 spin_lock_irqsave(&nvec->rx_lock, flags);
462 spin_unlock_irqrestore(&nvec->rx_lock, flags);
467 * @nvec: A &struct nvec_chip
471 static void nvec_tx_completed(struct nvec_chip *nvec)
474 if (nvec->tx->pos != nvec->tx->size) {
475 dev_err(nvec->dev, "premature END_TRANS, resending\n");
476 nvec->tx->pos = 0;
477 nvec_gpio_set_value(nvec, 0);
479 nvec->state = 0;
485 * @nvec: A &struct nvec_chip
489 static void nvec_rx_completed(struct nvec_chip *nvec)
491 if (nvec->rx->pos != nvec_msg_size(nvec->rx)) {
492 dev_err(nvec->dev, "RX incomplete: Expected %u bytes, got %u\n",
493 (uint)nvec_msg_size(nvec->rx),
494 (uint)nvec->rx->pos);
496 nvec_msg_free(nvec, nvec->rx);
497 nvec->state = 0;
500 if (nvec->rx->data[0] == NVEC_BAT)
501 complete(&nvec->ec_transfer);
506 spin_lock(&nvec->rx_lock);
512 list_add_tail(&nvec->rx->node, &nvec->rx_data);
514 spin_unlock(&nvec->rx_lock);
516 nvec->state = 0;
518 if (!nvec_msg_is_event(nvec->rx))
519 complete(&nvec->ec_transfer);
521 schedule_work(&nvec->rx_work);
526 * @nvec: The nvec device
530 static void nvec_invalid_flags(struct nvec_chip *nvec, unsigned int status,
533 dev_err(nvec->dev, "unexpected status flags 0x%02x during state %i\n",
534 status, nvec->state);
536 nvec->state = 0;
540 * nvec_tx_set - Set the message to transfer (nvec->tx)
541 * @nvec: A &struct nvec_chip
543 * Gets the first entry from the tx_data list of @nvec and sets the
547 static void nvec_tx_set(struct nvec_chip *nvec)
549 spin_lock(&nvec->tx_lock);
550 if (list_empty(&nvec->tx_data)) {
551 dev_err(nvec->dev, "empty tx - sending no-op\n");
552 memcpy(nvec->tx_scratch.data, "\x02\x07\x02", 3);
553 nvec->tx_scratch.size = 3;
554 nvec->tx_scratch.pos = 0;
555 nvec->tx = &nvec->tx_scratch;
556 list_add_tail(&nvec->tx->node, &nvec->tx_data);
558 nvec->tx = list_first_entry(&nvec->tx_data, struct nvec_msg,
560 nvec->tx->pos = 0;
562 spin_unlock(&nvec->tx_lock);
564 dev_dbg(nvec->dev, "Sending message of length %u, command 0x%x\n",
565 (uint)nvec->tx->size, nvec->tx->data[1]);
571 * @dev: The nvec device
583 struct nvec_chip *nvec = dev;
584 unsigned int state = nvec->state;
586 status = readl(nvec->base + I2C_SL_STATUS);
590 dev_err(nvec->dev, "unexpected irq mask %lx\n", status);
594 dev_err(nvec->dev, "Spurious IRQ\n");
600 received = readl(nvec->base + I2C_SL_RCVD);
602 writel(0, nvec->base + I2C_SL_RCVD);
606 nvec->state = 0;
608 switch (nvec->state) {
611 nvec_invalid_flags(nvec, status, false);
615 nvec_invalid_flags(nvec, status, true);
617 nvec->rx = nvec_msg_alloc(nvec, NVEC_MSG_RX);
619 if (unlikely(!nvec->rx)) {
620 nvec->state = 0;
623 nvec->rx->data[0] = received;
624 nvec->rx->pos = 1;
625 nvec->state = 2;
631 if (nvec->rx->data[0] != 0x01) {
632 dev_err(nvec->dev,
634 nvec->state = 0;
637 nvec_msg_free(nvec, nvec->rx);
638 nvec->state = 3;
639 nvec_tx_set(nvec);
640 to_send = nvec->tx->data[0];
641 nvec->tx->pos = 1;
643 nvec->rx->data[1] = received;
644 nvec->rx->pos = 2;
645 nvec->state = 4;
647 nvec_invalid_flags(nvec, status, true);
652 nvec_tx_completed(nvec);
654 nvec_invalid_flags(nvec, status, true);
655 } else if (nvec->tx && nvec->tx->pos < nvec->tx->size) {
656 to_send = nvec->tx->data[nvec->tx->pos++];
658 dev_err(nvec->dev,
660 nvec->tx,
661 (uint)(nvec->tx ? nvec->tx->pos : 0),
662 (uint)(nvec->tx ? nvec->tx->size : 0));
663 nvec->state = 0;
668 nvec_rx_completed(nvec);
670 nvec_invalid_flags(nvec, status, true);
671 else if (nvec->rx && nvec->rx->pos < NVEC_MSG_SIZE)
672 nvec->rx->data[nvec->rx->pos++] = received;
674 dev_err(nvec->dev,
676 nvec->rx, nvec->rx ? nvec->rx->pos : 0,
680 nvec->state = 0;
685 if (received != nvec->i2c_addr)
686 dev_err(nvec->dev,
688 received, nvec->i2c_addr);
689 nvec->state = 1;
694 writel(to_send, nvec->base + I2C_SL_RCVD);
698 nvec_gpio_set_value(nvec, 1);
700 dev_dbg(nvec->dev,
723 static void tegra_init_i2c_slave(struct nvec_chip *nvec)
727 clk_prepare_enable(nvec->i2c_clk);
729 reset_control_assert(nvec->rst);
731 reset_control_deassert(nvec->rst);
735 writel(val, nvec->base + I2C_CNFG);
737 clk_set_rate(nvec->i2c_clk, 8 * 80000);
739 writel(I2C_SL_NEWSL, nvec->base + I2C_SL_CNFG);
740 writel(0x1E, nvec->base + I2C_SL_DELAY_COUNT);
742 writel(nvec->i2c_addr >> 1, nvec->base + I2C_SL_ADDR1);
743 writel(0, nvec->base + I2C_SL_ADDR2);
745 enable_irq(nvec->irq);
749 static void nvec_disable_i2c_slave(struct nvec_chip *nvec)
751 disable_irq(nvec->irq);
752 writel(I2C_SL_NEWSL | I2C_SL_NACK, nvec->base + I2C_SL_CNFG);
753 clk_disable_unprepare(nvec->i2c_clk);
770 struct nvec_chip *nvec;
782 nvec = devm_kzalloc(dev, sizeof(struct nvec_chip), GFP_KERNEL);
783 if (!nvec)
786 platform_set_drvdata(pdev, nvec);
787 nvec->dev = dev;
789 if (of_property_read_u32(dev->of_node, "slave-addr", &nvec->i2c_addr)) {
798 nvec->irq = platform_get_irq(pdev, 0);
799 if (nvec->irq < 0)
808 nvec->rst = devm_reset_control_get_exclusive(dev, "i2c");
809 if (IS_ERR(nvec->rst)) {
811 return PTR_ERR(nvec->rst);
814 nvec->base = base;
815 nvec->i2c_clk = i2c_clk;
816 nvec->rx = &nvec->msg_pool[0];
818 ATOMIC_INIT_NOTIFIER_HEAD(&nvec->notifier_list);
820 init_completion(&nvec->sync_write);
821 init_completion(&nvec->ec_transfer);
822 mutex_init(&nvec->sync_write_mutex);
823 spin_lock_init(&nvec->tx_lock);
824 spin_lock_init(&nvec->rx_lock);
825 INIT_LIST_HEAD(&nvec->rx_data);
826 INIT_LIST_HEAD(&nvec->tx_data);
827 INIT_WORK(&nvec->rx_work, nvec_dispatch);
828 INIT_WORK(&nvec->tx_work, nvec_request_master);
830 nvec->gpiod = devm_gpiod_get(dev, "request", GPIOD_OUT_HIGH);
831 if (IS_ERR(nvec->gpiod)) {
833 return PTR_ERR(nvec->gpiod);
836 err = devm_request_irq(dev, nvec->irq, nvec_interrupt, 0,
837 "nvec", nvec);
842 disable_irq(nvec->irq);
844 tegra_init_i2c_slave(nvec);
847 nvec_toggle_global_events(nvec, true);
849 nvec->nvec_status_notifier.notifier_call = nvec_status_notifier;
850 nvec_register_notifier(nvec, &nvec->nvec_status_notifier, 0);
852 nvec_power_handle = nvec;
856 err = nvec_write_sync(nvec, get_firmware_version, 2, &msg);
864 nvec_msg_free(nvec, msg);
873 nvec_write_async(nvec, unmute_speakers, 4);
877 nvec_write_async(nvec, enable_event, 7);
881 nvec_write_async(nvec, enable_event, 7);
888 struct nvec_chip *nvec = platform_get_drvdata(pdev);
890 nvec_toggle_global_events(nvec, false);
891 mfd_remove_devices(nvec->dev);
892 nvec_unregister_notifier(nvec, &nvec->nvec_status_notifier);
893 cancel_work_sync(&nvec->rx_work);
894 cancel_work_sync(&nvec->tx_work);
895 /* FIXME: needs check whether nvec is responsible for power off */
903 struct nvec_chip *nvec = dev_get_drvdata(dev);
907 dev_dbg(nvec->dev, "suspending\n");
910 nvec_toggle_global_events(nvec, false);
912 err = nvec_write_sync(nvec, ap_suspend, sizeof(ap_suspend), &msg);
914 nvec_msg_free(nvec, msg);
916 nvec_disable_i2c_slave(nvec);
923 struct nvec_chip *nvec = dev_get_drvdata(dev);
925 dev_dbg(nvec->dev, "resuming\n");
926 tegra_init_i2c_slave(nvec);
927 nvec_toggle_global_events(nvec, true);
937 { .compatible = "nvidia,nvec", },
946 .name = "nvec",
954 MODULE_ALIAS("platform:nvec");