Lines Matching +full:spi +full:- +full:crc
1 // SPDX-License-Identifier: GPL-2.0
3 * MacBook (Pro) SPI keyboard and touchpad driver
5 * Copyright (c) 2015-2018 Federico Lorenzi
6 * Copyright (c) 2017-2018 Ronald Tschalär
11 * MacBook8 and newer can be driven either by USB or SPI. However the USB
15 * * UIEN ("USB Interface Enable"): If invoked with argument 1, disables SPI
18 * * SIEN ("SPI Interface Enable"): If invoked with argument 1, disables USB
19 * and enables SPI. If invoked with argument 0, disables SPI.
20 * * SIST ("SPI Interface Status"): Returns 1 if SPI is enabled, 0 otherwise.
21 * * ISOL: Resets the four GPIO pins used for SPI. Intended to be invoked with
26 * SPI-based Protocol
27 * ------------------
40 * fixed, 256-byte size). In that case it will be split over multiple,
55 #include <linux/spi/spi.h>
85 ((KBD_BL_LEVEL_MAX - KBD_BL_LEVEL_MIN) * KBD_BL_LEVEL_SCALE / 255U)
102 …emap Fn key ([0] = no-remap; 1 = left-ctrl, 2 = left-shift, 3 = left-alt, 4 = left-meta, 6 = right…
106 MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. ([0] = disabled,…
114 * struct keyboard_protocol - keyboard message.
118 * @modifiers: bit-set of modifier/control keys pressed
120 * @keys_pressed: the (non-modifier) keys currently pressed
122 * @crc16: crc over the whole message struct (message header +
135 * struct tp_finger - single trackpad finger structure, le16-aligned
150 * @crc16: on last finger: crc over the whole message struct
172 * struct touchpad_protocol - touchpad message.
176 * @clicked: 1 if a button-click was detected, 0 otherwise
194 * struct command_protocol_tp_info - get touchpad info.
197 * @crc16: crc over the whole message struct (message header +
205 * struct touchpad_info_protocol - touchpad info response.
213 * @crc16: crc over the whole message struct (message header +
225 * struct command_protocol_mt_init - initialize multitouch.
229 * @crc16: crc over the whole message struct (message header +
238 * struct command_protocol_capsl - toggle caps-lock led
243 * @crc16: crc over the whole message struct (message header +
253 * struct command_protocol_bl - set keyboard backlight brightness
259 * @crc16: crc over the whole message struct (message header +
270 * struct message - a complete spi message.
272 * Each message begins with fixed header, followed by a message-type specific
273 * payload, and ends with a 16-bit crc. Because of the varying lengths of the
274 * payload, the crc is defined at the end of each payload struct, rather than
287 * structure (after re-assembly in case of being split over
288 * multiple spi-packets), minus the trailing crc. The total size
294 * @tp_info_command: Touchpad info (CRC)
296 * @capsl_command: Toggle caps-lock LED
322 * struct spi_packet - a complete spi packet; always 256 bytes. This carries
342 * @crc16: crc over this whole structure minus this @crc16 field. This
343 * covers just this packet, even on multi-packet messages (in
344 * contrast to the crc in the message).
357 u64 spi_cs_delay; /* cs-to-clk delay in us */
358 u64 reset_a2r_usec; /* active-to-receive delay? */
371 struct spi_device *spi; member
512 .tp_info = { -5087, -182, 5579, 6089 },
516 .tp_info = { -6243, -170, 6749, 7685 },
520 .tp_info = { -7456, -163, 7976, 9283 },
551 struct spi_message *msg = &applespi->rd_m; in applespi_setup_read_txfrs()
552 struct spi_transfer *dl_t = &applespi->dl_t; in applespi_setup_read_txfrs()
553 struct spi_transfer *rd_t = &applespi->rd_t; in applespi_setup_read_txfrs()
558 dl_t->delay.value = applespi->spi_settings.spi_cs_delay; in applespi_setup_read_txfrs()
559 dl_t->delay.unit = SPI_DELAY_UNIT_USECS; in applespi_setup_read_txfrs()
561 rd_t->rx_buf = applespi->rx_buffer; in applespi_setup_read_txfrs()
562 rd_t->len = APPLESPI_PACKET_SIZE; in applespi_setup_read_txfrs()
571 struct spi_message *msg = &applespi->wr_m; in applespi_setup_write_txfrs()
572 struct spi_transfer *wt_t = &applespi->ww_t; in applespi_setup_write_txfrs()
573 struct spi_transfer *dl_t = &applespi->wd_t; in applespi_setup_write_txfrs()
574 struct spi_transfer *wr_t = &applespi->wr_t; in applespi_setup_write_txfrs()
575 struct spi_transfer *st_t = &applespi->st_t; in applespi_setup_write_txfrs()
584 * asserting cs. But the current spi API doesn't support this, so we in applespi_setup_write_txfrs()
588 wt_t->delay.value = SPI_RW_CHG_DELAY_US; in applespi_setup_write_txfrs()
589 wt_t->delay.unit = SPI_DELAY_UNIT_USECS; in applespi_setup_write_txfrs()
590 wt_t->cs_change = 1; in applespi_setup_write_txfrs()
592 dl_t->delay.value = applespi->spi_settings.spi_cs_delay; in applespi_setup_write_txfrs()
593 dl_t->delay.unit = SPI_DELAY_UNIT_USECS; in applespi_setup_write_txfrs()
595 wr_t->tx_buf = applespi->tx_buffer; in applespi_setup_write_txfrs()
596 wr_t->len = APPLESPI_PACKET_SIZE; in applespi_setup_write_txfrs()
597 wr_t->delay.value = SPI_RW_CHG_DELAY_US; in applespi_setup_write_txfrs()
598 wr_t->delay.unit = SPI_DELAY_UNIT_USECS; in applespi_setup_write_txfrs()
600 st_t->rx_buf = applespi->tx_status; in applespi_setup_write_txfrs()
601 st_t->len = APPLESPI_STATUS_SIZE; in applespi_setup_write_txfrs()
613 message->complete = complete; in applespi_async()
614 message->context = applespi; in applespi_async()
616 return spi_async(applespi->spi, message); in applespi_async()
625 dev_warn(&applespi->spi->dev, "Error writing to device: %d\n", in applespi_check_write_status()
630 if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) { in applespi_check_write_status()
631 dev_warn(&applespi->spi->dev, "Error writing to device: %*ph\n", in applespi_check_write_status()
632 APPLESPI_STATUS_SIZE, applespi->tx_status); in applespi_check_write_status()
641 struct acpi_device *adev = ACPI_COMPANION(&applespi->spi->dev); in applespi_get_spi_settings()
643 struct spi_settings *settings = &applespi->spi_settings; in applespi_get_spi_settings()
646 settings->spi_cs_delay = *(u64 *)o->buffer.pointer; in applespi_get_spi_settings()
648 dev_warn(&applespi->spi->dev, in applespi_get_spi_settings()
652 settings->reset_a2r_usec = *(u64 *)o->buffer.pointer; in applespi_get_spi_settings()
654 dev_warn(&applespi->spi->dev, in applespi_get_spi_settings()
658 settings->reset_rec_usec = *(u64 *)o->buffer.pointer; in applespi_get_spi_settings()
660 dev_warn(&applespi->spi->dev, in applespi_get_spi_settings()
663 dev_dbg(&applespi->spi->dev, in applespi_get_spi_settings()
664 "SPI settings: spi_cs_delay=%llu reset_a2r_usec=%llu reset_rec_usec=%llu\n", in applespi_get_spi_settings()
665 settings->spi_cs_delay, settings->reset_a2r_usec, in applespi_get_spi_settings()
666 settings->reset_rec_usec); in applespi_get_spi_settings()
679 spin_lock_init(&applespi->cmd_msg_lock); in applespi_setup_spi()
680 init_waitqueue_head(&applespi->drain_complete); in applespi_setup_spi()
690 /* check if SPI is already enabled, so we can skip the delay below */ in applespi_enable_spi()
691 acpi_sts = acpi_evaluate_integer(applespi->sist, NULL, NULL, in applespi_enable_spi()
696 /* SIEN(1) will enable SPI communication */ in applespi_enable_spi()
697 acpi_sts = acpi_execute_simple_method(applespi->sien, NULL, 1); in applespi_enable_spi()
699 dev_err(&applespi->spi->dev, "SIEN failed: %s\n", in applespi_enable_spi()
701 return -ENODEV; in applespi_enable_spi()
705 * Allow the SPI interface to come up before returning. Without this in applespi_enable_spi()
706 * delay, the SPI commands to enable multitouch mode may not reach in applespi_enable_spi()
722 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_msg_complete()
725 applespi->read_active = false; in applespi_msg_complete()
727 applespi->write_active = false; in applespi_msg_complete()
729 if (applespi->drain && !applespi->write_active) in applespi_msg_complete()
730 wake_up_all(&applespi->drain_complete); in applespi_msg_complete()
733 applespi->cmd_msg_queued = 0; in applespi_msg_complete()
737 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_msg_complete()
743 enum applespi_evt_type evt_type = applespi->cmd_evt_type; in applespi_async_write_complete()
746 applespi->tx_buffer, in applespi_async_write_complete()
749 applespi->tx_status, in applespi_async_write_complete()
754 if (!applespi_check_write_status(applespi, applespi->wr_m.status)) { in applespi_async_write_complete()
765 u16 crc; in applespi_send_cmd_msg() local
767 struct spi_packet *packet = (struct spi_packet *)applespi->tx_buffer; in applespi_send_cmd_msg()
768 struct message *message = (struct message *)packet->data; in applespi_send_cmd_msg()
773 if (applespi->drain) in applespi_send_cmd_msg()
777 if (applespi->cmd_msg_queued) { in applespi_send_cmd_msg()
778 if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000) in applespi_send_cmd_msg()
781 dev_warn(&applespi->spi->dev, "Command %d timed out\n", in applespi_send_cmd_msg()
782 applespi->cmd_evt_type); in applespi_send_cmd_msg()
784 applespi->cmd_msg_queued = 0; in applespi_send_cmd_msg()
785 applespi->write_active = false; in applespi_send_cmd_msg()
792 if (applespi->want_tp_info_cmd) { in applespi_send_cmd_msg()
793 applespi->want_tp_info_cmd = false; in applespi_send_cmd_msg()
794 applespi->want_mt_init_cmd = true; in applespi_send_cmd_msg()
795 applespi->cmd_evt_type = ET_CMD_TP_INI; in applespi_send_cmd_msg()
800 message->type = cpu_to_le16(0x1020); in applespi_send_cmd_msg()
801 msg_len = sizeof(message->tp_info_command); in applespi_send_cmd_msg()
803 message->zero = 0x02; in applespi_send_cmd_msg()
804 message->rsp_buf_len = cpu_to_le16(0x0200); in applespi_send_cmd_msg()
806 } else if (applespi->want_mt_init_cmd) { in applespi_send_cmd_msg()
807 applespi->want_mt_init_cmd = false; in applespi_send_cmd_msg()
808 applespi->cmd_evt_type = ET_CMD_TP_INI; in applespi_send_cmd_msg()
813 message->type = cpu_to_le16(0x0252); in applespi_send_cmd_msg()
814 msg_len = sizeof(message->init_mt_command); in applespi_send_cmd_msg()
816 message->init_mt_command.cmd = cpu_to_le16(0x0102); in applespi_send_cmd_msg()
818 /* do we need caps-lock command? */ in applespi_send_cmd_msg()
819 } else if (applespi->want_cl_led_on != applespi->have_cl_led_on) { in applespi_send_cmd_msg()
820 applespi->have_cl_led_on = applespi->want_cl_led_on; in applespi_send_cmd_msg()
821 applespi->cmd_evt_type = ET_CMD_CL; in applespi_send_cmd_msg()
826 message->type = cpu_to_le16(0x0151); in applespi_send_cmd_msg()
827 msg_len = sizeof(message->capsl_command); in applespi_send_cmd_msg()
829 message->capsl_command.unknown = 0x01; in applespi_send_cmd_msg()
830 message->capsl_command.led = applespi->have_cl_led_on ? 2 : 0; in applespi_send_cmd_msg()
833 } else if (applespi->want_bl_level != applespi->have_bl_level) { in applespi_send_cmd_msg()
834 applespi->have_bl_level = applespi->want_bl_level; in applespi_send_cmd_msg()
835 applespi->cmd_evt_type = ET_CMD_BL; in applespi_send_cmd_msg()
840 message->type = cpu_to_le16(0xB051); in applespi_send_cmd_msg()
841 msg_len = sizeof(message->bl_command); in applespi_send_cmd_msg()
843 message->bl_command.const1 = cpu_to_le16(0x01B0); in applespi_send_cmd_msg()
844 message->bl_command.level = in applespi_send_cmd_msg()
845 cpu_to_le16(applespi->have_bl_level); in applespi_send_cmd_msg()
847 if (applespi->have_bl_level > 0) in applespi_send_cmd_msg()
848 message->bl_command.const2 = cpu_to_le16(0x01F4); in applespi_send_cmd_msg()
850 message->bl_command.const2 = cpu_to_le16(0x0001); in applespi_send_cmd_msg()
852 /* everything's up-to-date */ in applespi_send_cmd_msg()
858 packet->flags = PACKET_TYPE_WRITE; in applespi_send_cmd_msg()
859 packet->device = device; in applespi_send_cmd_msg()
860 packet->length = cpu_to_le16(MSG_HEADER_SIZE + msg_len); in applespi_send_cmd_msg()
862 message->counter = applespi->cmd_msg_cntr++ % (U8_MAX + 1); in applespi_send_cmd_msg()
864 message->length = cpu_to_le16(msg_len - 2); in applespi_send_cmd_msg()
865 if (!message->rsp_buf_len) in applespi_send_cmd_msg()
866 message->rsp_buf_len = message->length; in applespi_send_cmd_msg()
868 crc = crc16(0, (u8 *)message, le16_to_cpu(packet->length) - 2); in applespi_send_cmd_msg()
869 put_unaligned_le16(crc, &message->data[msg_len - 2]); in applespi_send_cmd_msg()
871 crc = crc16(0, (u8 *)packet, sizeof(*packet) - 2); in applespi_send_cmd_msg()
872 packet->crc16 = cpu_to_le16(crc); in applespi_send_cmd_msg()
875 sts = applespi_async(applespi, &applespi->wr_m, in applespi_send_cmd_msg()
878 dev_warn(&applespi->spi->dev, in applespi_send_cmd_msg()
883 applespi->cmd_msg_queued = ktime_get_coarse(); in applespi_send_cmd_msg()
884 applespi->write_active = true; in applespi_send_cmd_msg()
893 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_init()
896 applespi->want_mt_init_cmd = true; in applespi_init()
898 applespi->want_tp_info_cmd = true; in applespi_init()
901 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_init()
910 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_set_capsl_led()
912 applespi->want_cl_led_on = capslock_on; in applespi_set_capsl_led()
915 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_set_capsl_led()
927 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_set_bl_level()
930 applespi->want_bl_level = value; in applespi_set_bl_level()
937 applespi->want_bl_level = in applespi_set_bl_level()
944 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_set_bl_level()
954 applespi_set_capsl_led(applespi, !!test_bit(LED_CAPSL, dev->led)); in applespi_event()
958 return -EINVAL; in applespi_event()
962 /* convert 16-bit little endian to signed integer */
971 applespi->tp_dim_min_x = min(applespi->tp_dim_min_x, in applespi_debug_update_dimensions()
972 le16_to_int(f->abs_x)); in applespi_debug_update_dimensions()
973 applespi->tp_dim_max_x = max(applespi->tp_dim_max_x, in applespi_debug_update_dimensions()
974 le16_to_int(f->abs_x)); in applespi_debug_update_dimensions()
975 applespi->tp_dim_min_y = min(applespi->tp_dim_min_y, in applespi_debug_update_dimensions()
976 le16_to_int(f->abs_y)); in applespi_debug_update_dimensions()
977 applespi->tp_dim_max_y = max(applespi->tp_dim_max_y, in applespi_debug_update_dimensions()
978 le16_to_int(f->abs_y)); in applespi_debug_update_dimensions()
983 struct applespi_data *applespi = inode->i_private; in applespi_tp_dim_open()
985 file->private_data = applespi; in applespi_tp_dim_open()
987 snprintf(applespi->tp_dim_val, sizeof(applespi->tp_dim_val), in applespi_tp_dim_open()
989 applespi->touchpad_input_dev->id.product, in applespi_tp_dim_open()
990 applespi->tp_dim_min_x, applespi->tp_dim_min_y, in applespi_tp_dim_open()
991 applespi->tp_dim_max_x - applespi->tp_dim_min_x, in applespi_tp_dim_open()
992 applespi->tp_dim_max_y - applespi->tp_dim_min_y); in applespi_tp_dim_open()
1000 struct applespi_data *applespi = file->private_data; in applespi_tp_dim_read()
1002 return simple_read_from_buffer(buf, len, off, applespi->tp_dim_val, in applespi_tp_dim_read()
1003 strlen(applespi->tp_dim_val)); in applespi_tp_dim_read()
1021 le16_to_int(f->touch_major) << 1); in report_finger_data()
1023 le16_to_int(f->touch_minor) << 1); in report_finger_data()
1025 le16_to_int(f->tool_major) << 1); in report_finger_data()
1027 le16_to_int(f->tool_minor) << 1); in report_finger_data()
1029 MAX_FINGER_ORIENTATION - le16_to_int(f->orientation)); in report_finger_data()
1030 input_report_abs(input, ABS_MT_POSITION_X, pos->x); in report_finger_data()
1031 input_report_abs(input, ABS_MT_POSITION_Y, pos->y); in report_finger_data()
1039 const struct applespi_tp_info *tp_info = &applespi->tp_info; in report_tp_state()
1043 input = smp_load_acquire(&applespi->touchpad_input_dev); in report_tp_state()
1049 for (i = 0; i < t->number_of_fingers; i++) { in report_tp_state()
1050 f = &t->fingers[i]; in report_tp_state()
1051 if (le16_to_int(f->touch_major) == 0) in report_tp_state()
1053 applespi->pos[n].x = le16_to_int(f->abs_x); in report_tp_state()
1054 applespi->pos[n].y = tp_info->y_min + tp_info->y_max - in report_tp_state()
1055 le16_to_int(f->abs_y); in report_tp_state()
1058 if (applespi->debug_tp_dim) in report_tp_state()
1062 input_mt_assign_slots(input, applespi->slots, applespi->pos, n, 0); in report_tp_state()
1065 report_finger_data(input, applespi->slots[i], in report_tp_state()
1066 &applespi->pos[i], &t->fingers[i]); in report_tp_state()
1069 input_report_key(input, BTN_LEFT, t->clicked); in report_tp_state()
1079 for (trans = table; trans->from; trans++) in applespi_find_translation()
1080 if (trans->from == key) in applespi_find_translation()
1093 if (trans->flags & APPLE_FLAG_FKEY) in applespi_translate_fn_key()
1100 key = trans->to; in applespi_translate_fn_key()
1112 key = trans->to; in applespi_translate_iso_layout()
1132 u8 bit = BIT((fnremap - 1) & 0x07); in applespi_remap_fn_key()
1135 !applespi_controlcodes[fnremap - 1]) in applespi_remap_fn_key()
1138 tmp = keyboard_protocol->fn_pressed; in applespi_remap_fn_key()
1139 keyboard_protocol->fn_pressed = !!(keyboard_protocol->modifiers & bit); in applespi_remap_fn_key()
1141 keyboard_protocol->modifiers |= bit; in applespi_remap_fn_key()
1143 keyboard_protocol->modifiers &= ~bit; in applespi_remap_fn_key()
1158 if (!memchr_inv(keyboard_protocol->keys_pressed, 1, MAX_ROLLOVER)) in applespi_handle_keyboard_event()
1166 if (memchr(keyboard_protocol->keys_pressed, in applespi_handle_keyboard_event()
1167 applespi->last_keys_pressed[i], MAX_ROLLOVER)) in applespi_handle_keyboard_event()
1170 key = applespi_code_to_key(applespi->last_keys_pressed[i], in applespi_handle_keyboard_event()
1171 applespi->last_keys_fn_pressed[i]); in applespi_handle_keyboard_event()
1172 input_report_key(applespi->keyboard_input_dev, key, 0); in applespi_handle_keyboard_event()
1173 applespi->last_keys_fn_pressed[i] = 0; in applespi_handle_keyboard_event()
1178 if (keyboard_protocol->keys_pressed[i] < in applespi_handle_keyboard_event()
1180 keyboard_protocol->keys_pressed[i] > 0) { in applespi_handle_keyboard_event()
1182 keyboard_protocol->keys_pressed[i], in applespi_handle_keyboard_event()
1183 keyboard_protocol->fn_pressed); in applespi_handle_keyboard_event()
1184 input_report_key(applespi->keyboard_input_dev, key, 1); in applespi_handle_keyboard_event()
1185 applespi->last_keys_fn_pressed[i] = in applespi_handle_keyboard_event()
1186 keyboard_protocol->fn_pressed; in applespi_handle_keyboard_event()
1192 if (keyboard_protocol->modifiers & BIT(i)) in applespi_handle_keyboard_event()
1193 input_report_key(applespi->keyboard_input_dev, in applespi_handle_keyboard_event()
1196 input_report_key(applespi->keyboard_input_dev, in applespi_handle_keyboard_event()
1201 if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed) in applespi_handle_keyboard_event()
1202 input_report_key(applespi->keyboard_input_dev, KEY_FN, 1); in applespi_handle_keyboard_event()
1203 else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed) in applespi_handle_keyboard_event()
1204 input_report_key(applespi->keyboard_input_dev, KEY_FN, 0); in applespi_handle_keyboard_event()
1205 applespi->last_fn_pressed = keyboard_protocol->fn_pressed; in applespi_handle_keyboard_event()
1208 input_sync(applespi->keyboard_input_dev); in applespi_handle_keyboard_event()
1209 memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed, in applespi_handle_keyboard_event()
1210 sizeof(applespi->last_keys_pressed)); in applespi_handle_keyboard_event()
1217 for (info = applespi_tp_models; info->model; info++) { in applespi_find_touchpad_info()
1218 if (info->model == model) in applespi_find_touchpad_info()
1219 return &info->tp_info; in applespi_find_touchpad_info()
1234 tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no); in applespi_register_touchpad_device()
1236 dev_warn(&applespi->spi->dev, in applespi_register_touchpad_device()
1237 "Unknown touchpad model %x - falling back to MB8 touchpad\n", in applespi_register_touchpad_device()
1238 rcvd_tp_info->model_no); in applespi_register_touchpad_device()
1242 applespi->tp_info = *tp_info; in applespi_register_touchpad_device()
1249 dev_info(&applespi->spi->dev, in applespi_register_touchpad_device()
1251 applespi->tp_info.x_min = x; in applespi_register_touchpad_device()
1252 applespi->tp_info.y_min = y; in applespi_register_touchpad_device()
1253 applespi->tp_info.x_max = x + w; in applespi_register_touchpad_device()
1254 applespi->tp_info.y_max = y + h; in applespi_register_touchpad_device()
1256 dev_warn(&applespi->spi->dev, in applespi_register_touchpad_device()
1265 applespi->tp_info.x_min, in applespi_register_touchpad_device()
1266 applespi->tp_info.y_min, in applespi_register_touchpad_device()
1267 applespi->tp_info.x_max - applespi->tp_info.x_min, in applespi_register_touchpad_device()
1268 applespi->tp_info.y_max - applespi->tp_info.y_min); in applespi_register_touchpad_device()
1272 touchpad_input_dev = devm_input_allocate_device(&applespi->spi->dev); in applespi_register_touchpad_device()
1274 dev_err(&applespi->spi->dev, in applespi_register_touchpad_device()
1276 return -ENOMEM; in applespi_register_touchpad_device()
1279 touchpad_input_dev->name = "Apple SPI Touchpad"; in applespi_register_touchpad_device()
1280 touchpad_input_dev->phys = "applespi/input1"; in applespi_register_touchpad_device()
1281 touchpad_input_dev->dev.parent = &applespi->spi->dev; in applespi_register_touchpad_device()
1282 touchpad_input_dev->id.bustype = BUS_SPI; in applespi_register_touchpad_device()
1283 touchpad_input_dev->id.vendor = SYNAPTICS_VENDOR_ID; in applespi_register_touchpad_device()
1284 touchpad_input_dev->id.product = in applespi_register_touchpad_device()
1285 rcvd_tp_info->model_no << 8 | rcvd_tp_info->model_flags; in applespi_register_touchpad_device()
1291 __set_bit(INPUT_PROP_POINTER, touchpad_input_dev->propbit); in applespi_register_touchpad_device()
1292 __set_bit(INPUT_PROP_BUTTONPAD, touchpad_input_dev->propbit); in applespi_register_touchpad_device()
1308 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION, in applespi_register_touchpad_device()
1313 applespi->tp_info.x_min, applespi->tp_info.x_max, in applespi_register_touchpad_device()
1316 applespi->tp_info.y_min, applespi->tp_info.y_max, in applespi_register_touchpad_device()
1327 dev_err(&applespi->spi->dev, in applespi_register_touchpad_device()
1335 dev_err(&applespi->spi->dev, in applespi_register_touchpad_device()
1340 /* touchpad_input_dev is read async in spi callback */ in applespi_register_touchpad_device()
1341 smp_store_release(&applespi->touchpad_input_dev, touchpad_input_dev); in applespi_register_touchpad_device()
1351 applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info); in applespi_worker()
1358 if (packet->device == PACKET_DEV_INFO && in applespi_handle_cmd_response()
1359 le16_to_cpu(message->type) == 0x1020) { in applespi_handle_cmd_response()
1364 applespi->rcvd_tp_info = message->tp_info; in applespi_handle_cmd_response()
1365 schedule_work(&applespi->work); in applespi_handle_cmd_response()
1369 if (le16_to_cpu(message->length) != 0x0000) { in applespi_handle_cmd_response()
1370 dev_warn_ratelimited(&applespi->spi->dev, in applespi_handle_cmd_response()
1372 le16_to_cpu(message->length)); in applespi_handle_cmd_response()
1376 if (packet->device == PACKET_DEV_TPAD && in applespi_handle_cmd_response()
1377 le16_to_cpu(message->type) == 0x0252 && in applespi_handle_cmd_response()
1378 le16_to_cpu(message->rsp_buf_len) == 0x0002) in applespi_handle_cmd_response()
1379 dev_info(&applespi->spi->dev, "modeswitch done.\n"); in applespi_handle_cmd_response()
1385 u16 crc; in applespi_verify_crc() local
1387 crc = crc16(0, buffer, buflen); in applespi_verify_crc()
1388 if (crc) { in applespi_verify_crc()
1389 dev_warn_ratelimited(&applespi->spi->dev, in applespi_verify_crc()
1390 "Received corrupted packet (crc mismatch)\n"); in applespi_verify_crc()
1404 if (packet->flags == PACKET_TYPE_READ && in applespi_debug_print_read_packet()
1405 packet->device == PACKET_DEV_KEYB) in applespi_debug_print_read_packet()
1407 else if (packet->flags == PACKET_TYPE_READ && in applespi_debug_print_read_packet()
1408 packet->device == PACKET_DEV_TPAD) in applespi_debug_print_read_packet()
1410 else if (packet->flags == PACKET_TYPE_WRITE) in applespi_debug_print_read_packet()
1411 evt_type = applespi->cmd_evt_type; in applespi_debug_print_read_packet()
1415 applespi_get_trace_fun(evt_type)(evt_type, PT_READ, applespi->rx_buffer, in applespi_debug_print_read_packet()
1429 if (!applespi_verify_crc(applespi, applespi->rx_buffer, in applespi_got_data()
1433 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_got_data()
1435 if (applespi->drain) { in applespi_got_data()
1436 applespi->read_active = false; in applespi_got_data()
1437 applespi->write_active = false; in applespi_got_data()
1439 wake_up_all(&applespi->drain_complete); in applespi_got_data()
1442 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_got_data()
1447 packet = (struct spi_packet *)applespi->rx_buffer; in applespi_got_data()
1451 off = le16_to_cpu(packet->offset); in applespi_got_data()
1452 rem = le16_to_cpu(packet->remaining); in applespi_got_data()
1453 len = le16_to_cpu(packet->length); in applespi_got_data()
1455 if (len > sizeof(packet->data)) { in applespi_got_data()
1456 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1462 /* handle multi-packet messages */ in applespi_got_data()
1464 if (off != applespi->saved_msg_len) { in applespi_got_data()
1465 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1467 off, applespi->saved_msg_len); in applespi_got_data()
1472 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1479 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1485 memcpy(applespi->msg_buf + off, &packet->data, len); in applespi_got_data()
1486 applespi->saved_msg_len += len; in applespi_got_data()
1491 message = (struct message *)applespi->msg_buf; in applespi_got_data()
1492 msg_len = applespi->saved_msg_len; in applespi_got_data()
1494 message = (struct message *)&packet->data; in applespi_got_data()
1498 /* got complete message - verify */ in applespi_got_data()
1502 if (le16_to_cpu(message->length) != msg_len - MSG_HEADER_SIZE - 2) { in applespi_got_data()
1503 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1504 "Received corrupted packet (invalid message length %u - expected %u)\n", in applespi_got_data()
1505 le16_to_cpu(message->length), in applespi_got_data()
1506 msg_len - MSG_HEADER_SIZE - 2); in applespi_got_data()
1511 if (packet->flags == PACKET_TYPE_READ && in applespi_got_data()
1512 packet->device == PACKET_DEV_KEYB) { in applespi_got_data()
1513 applespi_handle_keyboard_event(applespi, &message->keyboard); in applespi_got_data()
1515 } else if (packet->flags == PACKET_TYPE_READ && in applespi_got_data()
1516 packet->device == PACKET_DEV_TPAD) { in applespi_got_data()
1520 tp = &message->touchpad; in applespi_got_data()
1521 tp_len = struct_size(tp, fingers, tp->number_of_fingers); in applespi_got_data()
1523 if (le16_to_cpu(message->length) + 2 != tp_len) { in applespi_got_data()
1524 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1525 "Received corrupted packet (invalid message length %u - num-fingers %u, tp-len %zu)\n", in applespi_got_data()
1526 le16_to_cpu(message->length), in applespi_got_data()
1527 tp->number_of_fingers, tp_len); in applespi_got_data()
1531 if (tp->number_of_fingers > MAX_FINGERS) { in applespi_got_data()
1532 dev_warn_ratelimited(&applespi->spi->dev, in applespi_got_data()
1534 tp->number_of_fingers, in applespi_got_data()
1536 tp->number_of_fingers = MAX_FINGERS; in applespi_got_data()
1541 } else if (packet->flags == PACKET_TYPE_WRITE) { in applespi_got_data()
1546 applespi->saved_msg_len = 0; in applespi_got_data()
1548 applespi_msg_complete(applespi, packet->flags == PACKET_TYPE_WRITE, in applespi_got_data()
1556 if (applespi->rd_m.status < 0) { in applespi_async_read_complete()
1557 dev_warn(&applespi->spi->dev, "Error reading from device: %d\n", in applespi_async_read_complete()
1558 applespi->rd_m.status); in applespi_async_read_complete()
1569 acpi_finish_gpe(NULL, applespi->gpe); in applespi_async_read_complete()
1580 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_notify()
1582 if (!applespi->suspended) { in applespi_notify()
1583 sts = applespi_async(applespi, &applespi->rd_m, in applespi_notify()
1586 dev_warn(&applespi->spi->dev, in applespi_notify()
1590 applespi->read_active = true; in applespi_notify()
1593 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_notify()
1608 dev_warn(&applespi->spi->dev, in applespi_get_saved_bl_level()
1612 return sts != EFI_SUCCESS ? -ENODEV : efi_data; in applespi_get_saved_bl_level()
1630 dev_warn(&applespi->spi->dev, in applespi_save_bl_level()
1634 static int applespi_probe(struct spi_device *spi) in applespi_probe() argument
1637 acpi_handle spi_handle = ACPI_HANDLE(&spi->dev); in applespi_probe()
1646 dev_info(&spi->dev, "USB interface already enabled\n"); in applespi_probe()
1647 return -ENODEV; in applespi_probe()
1651 applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL); in applespi_probe()
1653 return -ENOMEM; in applespi_probe()
1655 applespi->spi = spi; in applespi_probe()
1657 INIT_WORK(&applespi->work, applespi_worker); in applespi_probe()
1660 spi_set_drvdata(spi, applespi); in applespi_probe()
1663 applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE, in applespi_probe()
1665 applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE, in applespi_probe()
1667 applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE, in applespi_probe()
1669 applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG, in applespi_probe()
1673 if (!applespi->tx_buffer || !applespi->tx_status || in applespi_probe()
1674 !applespi->rx_buffer || !applespi->msg_buf) in applespi_probe()
1675 return -ENOMEM; in applespi_probe()
1677 /* set up our spi messages */ in applespi_probe()
1682 acpi_sts = acpi_get_handle(spi_handle, "SIEN", &applespi->sien); in applespi_probe()
1684 dev_err(&applespi->spi->dev, in applespi_probe()
1687 return -ENODEV; in applespi_probe()
1690 acpi_sts = acpi_get_handle(spi_handle, "SIST", &applespi->sist); in applespi_probe()
1692 dev_err(&applespi->spi->dev, in applespi_probe()
1695 return -ENODEV; in applespi_probe()
1698 /* switch on the SPI interface */ in applespi_probe()
1708 applespi->keyboard_input_dev = devm_input_allocate_device(&spi->dev); in applespi_probe()
1710 if (!applespi->keyboard_input_dev) in applespi_probe()
1711 return -ENOMEM; in applespi_probe()
1713 applespi->keyboard_input_dev->name = "Apple SPI Keyboard"; in applespi_probe()
1714 applespi->keyboard_input_dev->phys = "applespi/input0"; in applespi_probe()
1715 applespi->keyboard_input_dev->dev.parent = &spi->dev; in applespi_probe()
1716 applespi->keyboard_input_dev->id.bustype = BUS_SPI; in applespi_probe()
1718 applespi->keyboard_input_dev->evbit[0] = in applespi_probe()
1720 applespi->keyboard_input_dev->ledbit[0] = BIT_MASK(LED_CAPSL); in applespi_probe()
1722 input_set_drvdata(applespi->keyboard_input_dev, applespi); in applespi_probe()
1723 applespi->keyboard_input_dev->event = applespi_event; in applespi_probe()
1727 input_set_capability(applespi->keyboard_input_dev, in applespi_probe()
1732 input_set_capability(applespi->keyboard_input_dev, in applespi_probe()
1737 input_set_capability(applespi->keyboard_input_dev, in applespi_probe()
1740 input_set_capability(applespi->keyboard_input_dev, EV_KEY, KEY_FN); in applespi_probe()
1742 sts = input_register_device(applespi->keyboard_input_dev); in applespi_probe()
1744 dev_err(&applespi->spi->dev, in applespi_probe()
1746 return -ENODEV; in applespi_probe()
1755 dev_err(&applespi->spi->dev, in applespi_probe()
1756 "Failed to obtain GPE for SPI slave device: %s\n", in applespi_probe()
1758 return -ENODEV; in applespi_probe()
1760 applespi->gpe = (int)gpe; in applespi_probe()
1762 acpi_sts = acpi_install_gpe_handler(NULL, applespi->gpe, in applespi_probe()
1766 dev_err(&applespi->spi->dev, in applespi_probe()
1768 applespi->gpe, acpi_format_exception(acpi_sts)); in applespi_probe()
1769 return -ENODEV; in applespi_probe()
1772 applespi->suspended = false; in applespi_probe()
1774 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe); in applespi_probe()
1776 dev_err(&applespi->spi->dev, in applespi_probe()
1778 applespi->gpe, acpi_format_exception(acpi_sts)); in applespi_probe()
1779 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify); in applespi_probe()
1780 return -ENODEV; in applespi_probe()
1791 device_wakeup_enable(&spi->dev); in applespi_probe()
1793 /* set up keyboard-backlight */ in applespi_probe()
1796 applespi_set_bl_level(&applespi->backlight_info, sts); in applespi_probe()
1798 applespi->backlight_info.name = "spi::kbd_backlight"; in applespi_probe()
1799 applespi->backlight_info.default_trigger = "kbd-backlight"; in applespi_probe()
1800 applespi->backlight_info.brightness_set = applespi_set_bl_level; in applespi_probe()
1802 sts = devm_led_classdev_register(&spi->dev, &applespi->backlight_info); in applespi_probe()
1804 dev_warn(&applespi->spi->dev, in applespi_probe()
1809 applespi->debugfs_root = debugfs_create_dir("applespi", NULL); in applespi_probe()
1811 debugfs_create_bool("enable_tp_dim", 0600, applespi->debugfs_root, in applespi_probe()
1812 &applespi->debug_tp_dim); in applespi_probe()
1814 debugfs_create_file("tp_dim", 0400, applespi->debugfs_root, applespi, in applespi_probe()
1824 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_drain_writes()
1826 applespi->drain = true; in applespi_drain_writes()
1827 wait_event_lock_irq(applespi->drain_complete, !applespi->write_active, in applespi_drain_writes()
1828 applespi->cmd_msg_lock); in applespi_drain_writes()
1830 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_drain_writes()
1837 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_drain_reads()
1839 wait_event_lock_irq(applespi->drain_complete, !applespi->read_active, in applespi_drain_reads()
1840 applespi->cmd_msg_lock); in applespi_drain_reads()
1842 applespi->suspended = true; in applespi_drain_reads()
1844 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_drain_reads()
1847 static void applespi_remove(struct spi_device *spi) in applespi_remove() argument
1849 struct applespi_data *applespi = spi_get_drvdata(spi); in applespi_remove()
1853 acpi_disable_gpe(NULL, applespi->gpe); in applespi_remove()
1854 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify); in applespi_remove()
1855 device_wakeup_disable(&spi->dev); in applespi_remove()
1859 debugfs_remove_recursive(applespi->debugfs_root); in applespi_remove()
1862 static void applespi_shutdown(struct spi_device *spi) in applespi_shutdown() argument
1864 struct applespi_data *applespi = spi_get_drvdata(spi); in applespi_shutdown()
1866 applespi_save_bl_level(applespi, applespi->have_bl_level); in applespi_shutdown()
1871 struct spi_device *spi = to_spi_device(dev); in applespi_poweroff_late() local
1872 struct applespi_data *applespi = spi_get_drvdata(spi); in applespi_poweroff_late()
1874 applespi_save_bl_level(applespi, applespi->have_bl_level); in applespi_poweroff_late()
1881 struct spi_device *spi = to_spi_device(dev); in applespi_suspend() local
1882 struct applespi_data *applespi = spi_get_drvdata(spi); in applespi_suspend()
1886 /* turn off caps-lock - it'll stay on otherwise */ in applespi_suspend()
1889 dev_warn(&applespi->spi->dev, in applespi_suspend()
1890 "Failed to turn off caps-lock led (%d)\n", sts); in applespi_suspend()
1895 acpi_sts = acpi_disable_gpe(NULL, applespi->gpe); in applespi_suspend()
1897 dev_err(&applespi->spi->dev, in applespi_suspend()
1899 applespi->gpe, acpi_format_exception(acpi_sts)); in applespi_suspend()
1908 struct spi_device *spi = to_spi_device(dev); in applespi_resume() local
1909 struct applespi_data *applespi = spi_get_drvdata(spi); in applespi_resume()
1914 spin_lock_irqsave(&applespi->cmd_msg_lock, flags); in applespi_resume()
1916 applespi->drain = false; in applespi_resume()
1917 applespi->have_cl_led_on = false; in applespi_resume()
1918 applespi->have_bl_level = 0; in applespi_resume()
1919 applespi->cmd_msg_queued = 0; in applespi_resume()
1920 applespi->read_active = false; in applespi_resume()
1921 applespi->write_active = false; in applespi_resume()
1923 applespi->suspended = false; in applespi_resume()
1925 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags); in applespi_resume()
1927 /* switch on the SPI interface */ in applespi_resume()
1930 /* re-enable the interrupt */ in applespi_resume()
1931 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe); in applespi_resume()
1933 dev_err(&applespi->spi->dev, in applespi_resume()
1934 "Failed to re-enable GPE handler for GPE %d: %s\n", in applespi_resume()
1935 applespi->gpe, acpi_format_exception(acpi_sts)); in applespi_resume()
1968 MODULE_DESCRIPTION("MacBook(Pro) SPI Keyboard/Touchpad driver");