146fe7771SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
268957303SChristophe Ricard /*
368957303SChristophe Ricard * I2C Link Layer for ST21NFCA HCI based Driver
468957303SChristophe Ricard * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
568957303SChristophe Ricard */
668957303SChristophe Ricard
768957303SChristophe Ricard #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
868957303SChristophe Ricard
968957303SChristophe Ricard #include <linux/crc-ccitt.h>
1068957303SChristophe Ricard #include <linux/module.h>
1168957303SChristophe Ricard #include <linux/i2c.h>
12dfa8070dSChristophe Ricard #include <linux/gpio/consumer.h>
13c44cb2edSChristophe Ricard #include <linux/of_irq.h>
14c44cb2edSChristophe Ricard #include <linux/of_gpio.h>
15dfa8070dSChristophe Ricard #include <linux/acpi.h>
1668957303SChristophe Ricard #include <linux/interrupt.h>
1768957303SChristophe Ricard #include <linux/delay.h>
1868957303SChristophe Ricard #include <linux/nfc.h>
1968957303SChristophe Ricard #include <linux/firmware.h>
2079557b33SAndy Shevchenko
2168957303SChristophe Ricard #include <net/nfc/hci.h>
2268957303SChristophe Ricard #include <net/nfc/llc.h>
2368957303SChristophe Ricard #include <net/nfc/nfc.h>
2468957303SChristophe Ricard
2568957303SChristophe Ricard #include "st21nfca.h"
2668957303SChristophe Ricard
2768957303SChristophe Ricard /*
2868957303SChristophe Ricard * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
2968957303SChristophe Ricard * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
3068957303SChristophe Ricard * called byte stuffing has been introduced.
3168957303SChristophe Ricard *
3268957303SChristophe Ricard * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
3368957303SChristophe Ricard * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
3468957303SChristophe Ricard * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
3568957303SChristophe Ricard */
3668957303SChristophe Ricard #define ST21NFCA_SOF_EOF 0x7e
3768957303SChristophe Ricard #define ST21NFCA_BYTE_STUFFING_MASK 0x20
3868957303SChristophe Ricard #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
3968957303SChristophe Ricard
40e1fb97b9SChristophe Ricard /* SOF + 00 */
4168957303SChristophe Ricard #define ST21NFCA_FRAME_HEADROOM 2
4268957303SChristophe Ricard
43e1fb97b9SChristophe Ricard /* 2 bytes crc + EOF */
44e1fb97b9SChristophe Ricard #define ST21NFCA_FRAME_TAILROOM 3
45c97ffdbfSChristophe Ricard #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
46c97ffdbfSChristophe Ricard buf[1] == 0)
4768957303SChristophe Ricard
4879557b33SAndy Shevchenko #define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
4968957303SChristophe Ricard #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
5068957303SChristophe Ricard
5168957303SChristophe Ricard struct st21nfca_i2c_phy {
5268957303SChristophe Ricard struct i2c_client *i2c_dev;
5368957303SChristophe Ricard struct nfc_hci_dev *hdev;
5468957303SChristophe Ricard
558d3c50e2SAndy Shevchenko struct gpio_desc *gpiod_ena;
562130fb97SChristophe Ricard struct st21nfca_se_status se_status;
572130fb97SChristophe Ricard
5868957303SChristophe Ricard struct sk_buff *pending_skb;
5968957303SChristophe Ricard int current_read_len;
6068957303SChristophe Ricard /*
6168957303SChristophe Ricard * crc might have fail because i2c macro
6268957303SChristophe Ricard * is disable due to other interface activity
6368957303SChristophe Ricard */
6468957303SChristophe Ricard int crc_trials;
6568957303SChristophe Ricard
6668957303SChristophe Ricard int powered;
6768957303SChristophe Ricard int run_mode;
6868957303SChristophe Ricard
6968957303SChristophe Ricard /*
7068957303SChristophe Ricard * < 0 if hardware error occured (e.g. i2c err)
7168957303SChristophe Ricard * and prevents normal operation.
7268957303SChristophe Ricard */
7368957303SChristophe Ricard int hard_fault;
74a3c5d8fbSChristophe Ricard struct mutex phy_lock;
7568957303SChristophe Ricard };
7657dc828aSChristophe Ricard
770f20ae9bSKrzysztof Kozlowski static const u8 len_seq[] = { 16, 24, 12, 29 };
780f20ae9bSKrzysztof Kozlowski static const u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
7968957303SChristophe Ricard
8068957303SChristophe Ricard #define I2C_DUMP_SKB(info, skb) \
8168957303SChristophe Ricard do { \
8268957303SChristophe Ricard pr_debug("%s:\n", info); \
8368957303SChristophe Ricard print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
8468957303SChristophe Ricard 16, 1, (skb)->data, (skb)->len, 0); \
8568957303SChristophe Ricard } while (0)
8668957303SChristophe Ricard
8718d2c624SChristophe Ricard /*
8818d2c624SChristophe Ricard * In order to get the CLF in a known state we generate an internal reboot
8918d2c624SChristophe Ricard * using a proprietary command.
9018d2c624SChristophe Ricard * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
9118d2c624SChristophe Ricard * fill buffer.
9218d2c624SChristophe Ricard */
st21nfca_hci_platform_init(struct st21nfca_i2c_phy * phy)9318d2c624SChristophe Ricard static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
9468957303SChristophe Ricard {
95c5b0c370SChristophe Ricard u16 wait_reboot[] = { 50, 300, 1000 };
9668957303SChristophe Ricard char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
9768957303SChristophe Ricard u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
9868957303SChristophe Ricard int i, r = -1;
9968957303SChristophe Ricard
10018d2c624SChristophe Ricard for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
10168957303SChristophe Ricard r = i2c_master_send(phy->i2c_dev, reboot_cmd,
10268957303SChristophe Ricard sizeof(reboot_cmd));
10318d2c624SChristophe Ricard if (r < 0)
10418d2c624SChristophe Ricard msleep(wait_reboot[i]);
10518d2c624SChristophe Ricard }
10618d2c624SChristophe Ricard if (r < 0)
10718d2c624SChristophe Ricard return r;
10868957303SChristophe Ricard
10918d2c624SChristophe Ricard /* CLF is spending about 20ms to do an internal reboot */
11018d2c624SChristophe Ricard msleep(20);
11118d2c624SChristophe Ricard r = -1;
11218d2c624SChristophe Ricard for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
11318d2c624SChristophe Ricard r = i2c_master_recv(phy->i2c_dev, tmp,
11418d2c624SChristophe Ricard ST21NFCA_HCI_LLC_MAX_SIZE);
11518d2c624SChristophe Ricard if (r < 0)
11618d2c624SChristophe Ricard msleep(wait_reboot[i]);
11718d2c624SChristophe Ricard }
11818d2c624SChristophe Ricard if (r < 0)
11918d2c624SChristophe Ricard return r;
12018d2c624SChristophe Ricard
12118d2c624SChristophe Ricard for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
12218d2c624SChristophe Ricard tmp[i] == ST21NFCA_SOF_EOF; i++)
12318d2c624SChristophe Ricard ;
12418d2c624SChristophe Ricard
12518d2c624SChristophe Ricard if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
12618d2c624SChristophe Ricard return -ENODEV;
12718d2c624SChristophe Ricard
12818d2c624SChristophe Ricard usleep_range(1000, 1500);
12918d2c624SChristophe Ricard return 0;
13068957303SChristophe Ricard }
13168957303SChristophe Ricard
st21nfca_hci_i2c_enable(void * phy_id)13268957303SChristophe Ricard static int st21nfca_hci_i2c_enable(void *phy_id)
13368957303SChristophe Ricard {
13468957303SChristophe Ricard struct st21nfca_i2c_phy *phy = phy_id;
13568957303SChristophe Ricard
1368d3c50e2SAndy Shevchenko gpiod_set_value(phy->gpiod_ena, 1);
13768957303SChristophe Ricard phy->powered = 1;
13868957303SChristophe Ricard phy->run_mode = ST21NFCA_HCI_MODE;
13968957303SChristophe Ricard
14068957303SChristophe Ricard usleep_range(10000, 15000);
14168957303SChristophe Ricard
14268957303SChristophe Ricard return 0;
14368957303SChristophe Ricard }
14468957303SChristophe Ricard
st21nfca_hci_i2c_disable(void * phy_id)14568957303SChristophe Ricard static void st21nfca_hci_i2c_disable(void *phy_id)
14668957303SChristophe Ricard {
14768957303SChristophe Ricard struct st21nfca_i2c_phy *phy = phy_id;
14868957303SChristophe Ricard
1498d3c50e2SAndy Shevchenko gpiod_set_value(phy->gpiod_ena, 0);
15068957303SChristophe Ricard
15168957303SChristophe Ricard phy->powered = 0;
15268957303SChristophe Ricard }
15368957303SChristophe Ricard
st21nfca_hci_add_len_crc(struct sk_buff * skb)154e1fb97b9SChristophe Ricard static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
15568957303SChristophe Ricard {
15668957303SChristophe Ricard u16 crc;
15768957303SChristophe Ricard u8 tmp;
15868957303SChristophe Ricard
159d58ff351SJohannes Berg *(u8 *)skb_push(skb, 1) = 0;
16068957303SChristophe Ricard
16168957303SChristophe Ricard crc = crc_ccitt(0xffff, skb->data, skb->len);
16268957303SChristophe Ricard crc = ~crc;
16368957303SChristophe Ricard
16468957303SChristophe Ricard tmp = crc & 0x00ff;
165634fef61SJohannes Berg skb_put_u8(skb, tmp);
16668957303SChristophe Ricard
16768957303SChristophe Ricard tmp = (crc >> 8) & 0x00ff;
168634fef61SJohannes Berg skb_put_u8(skb, tmp);
16968957303SChristophe Ricard }
17068957303SChristophe Ricard
st21nfca_hci_remove_len_crc(struct sk_buff * skb)171e1fb97b9SChristophe Ricard static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
17268957303SChristophe Ricard {
17368957303SChristophe Ricard skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
174e1fb97b9SChristophe Ricard skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
17568957303SChristophe Ricard }
17668957303SChristophe Ricard
17768957303SChristophe Ricard /*
17868957303SChristophe Ricard * Writing a frame must not return the number of written bytes.
17968957303SChristophe Ricard * It must return either zero for success, or <0 for error.
18068957303SChristophe Ricard * In addition, it must not alter the skb
18168957303SChristophe Ricard */
st21nfca_hci_i2c_write(void * phy_id,struct sk_buff * skb)18268957303SChristophe Ricard static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
18368957303SChristophe Ricard {
184e1fb97b9SChristophe Ricard int r = -1, i, j;
18568957303SChristophe Ricard struct st21nfca_i2c_phy *phy = phy_id;
18668957303SChristophe Ricard struct i2c_client *client = phy->i2c_dev;
18768957303SChristophe Ricard u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
18868957303SChristophe Ricard
18968957303SChristophe Ricard I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
19068957303SChristophe Ricard
19168957303SChristophe Ricard if (phy->hard_fault != 0)
19268957303SChristophe Ricard return phy->hard_fault;
19368957303SChristophe Ricard
19468957303SChristophe Ricard /*
19568957303SChristophe Ricard * Compute CRC before byte stuffing computation on frame
19668957303SChristophe Ricard * Note st21nfca_hci_add_len_crc is doing a byte stuffing
19768957303SChristophe Ricard * on its own value
19868957303SChristophe Ricard */
199e1fb97b9SChristophe Ricard st21nfca_hci_add_len_crc(skb);
20068957303SChristophe Ricard
20168957303SChristophe Ricard /* add ST21NFCA_SOF_EOF on tail */
202634fef61SJohannes Berg skb_put_u8(skb, ST21NFCA_SOF_EOF);
20368957303SChristophe Ricard /* add ST21NFCA_SOF_EOF on head */
204d58ff351SJohannes Berg *(u8 *)skb_push(skb, 1) = ST21NFCA_SOF_EOF;
20568957303SChristophe Ricard
20668957303SChristophe Ricard /*
20768957303SChristophe Ricard * Compute byte stuffing
20868957303SChristophe Ricard * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
20968957303SChristophe Ricard * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
21068957303SChristophe Ricard * xor byte with ST21NFCA_BYTE_STUFFING_MASK
21168957303SChristophe Ricard */
21268957303SChristophe Ricard tmp[0] = skb->data[0];
21368957303SChristophe Ricard for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
21468957303SChristophe Ricard if (skb->data[i] == ST21NFCA_SOF_EOF
21568957303SChristophe Ricard || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
21668957303SChristophe Ricard tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
21768957303SChristophe Ricard j++;
21868957303SChristophe Ricard tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
21968957303SChristophe Ricard } else {
22068957303SChristophe Ricard tmp[j] = skb->data[i];
22168957303SChristophe Ricard }
22268957303SChristophe Ricard }
22368957303SChristophe Ricard tmp[j] = skb->data[i];
22468957303SChristophe Ricard j++;
22568957303SChristophe Ricard
22668957303SChristophe Ricard /*
22768957303SChristophe Ricard * Manage sleep mode
22868957303SChristophe Ricard * Try 3 times to send data with delay between each
22968957303SChristophe Ricard */
230a3c5d8fbSChristophe Ricard mutex_lock(&phy->phy_lock);
23168957303SChristophe Ricard for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
23268957303SChristophe Ricard r = i2c_master_send(client, tmp, j);
23368957303SChristophe Ricard if (r < 0)
23468957303SChristophe Ricard msleep(wait_tab[i]);
23568957303SChristophe Ricard }
236a3c5d8fbSChristophe Ricard mutex_unlock(&phy->phy_lock);
23768957303SChristophe Ricard
23868957303SChristophe Ricard if (r >= 0) {
23968957303SChristophe Ricard if (r != j)
24068957303SChristophe Ricard r = -EREMOTEIO;
24168957303SChristophe Ricard else
24268957303SChristophe Ricard r = 0;
24368957303SChristophe Ricard }
24468957303SChristophe Ricard
245e1fb97b9SChristophe Ricard st21nfca_hci_remove_len_crc(skb);
24668957303SChristophe Ricard
24768957303SChristophe Ricard return r;
24868957303SChristophe Ricard }
24968957303SChristophe Ricard
get_frame_size(u8 * buf,int buflen)25068957303SChristophe Ricard static int get_frame_size(u8 *buf, int buflen)
25168957303SChristophe Ricard {
25268957303SChristophe Ricard int len = 0;
2533e6df919SChristophe Ricard
25468957303SChristophe Ricard if (buf[len + 1] == ST21NFCA_SOF_EOF)
25568957303SChristophe Ricard return 0;
25668957303SChristophe Ricard
25768957303SChristophe Ricard for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
25868957303SChristophe Ricard ;
25968957303SChristophe Ricard
26068957303SChristophe Ricard return len;
26168957303SChristophe Ricard }
26268957303SChristophe Ricard
check_crc(u8 * buf,int buflen)26368957303SChristophe Ricard static int check_crc(u8 *buf, int buflen)
26468957303SChristophe Ricard {
26568957303SChristophe Ricard u16 crc;
26668957303SChristophe Ricard
26768957303SChristophe Ricard crc = crc_ccitt(0xffff, buf, buflen - 2);
26868957303SChristophe Ricard crc = ~crc;
26968957303SChristophe Ricard
27068957303SChristophe Ricard if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
27168957303SChristophe Ricard pr_err(ST21NFCA_HCI_DRIVER_NAME
27268957303SChristophe Ricard ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
27368957303SChristophe Ricard buf[buflen - 2]);
27468957303SChristophe Ricard
27568957303SChristophe Ricard pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
27668957303SChristophe Ricard print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
27768957303SChristophe Ricard 16, 2, buf, buflen, false);
27868957303SChristophe Ricard return -EPERM;
27968957303SChristophe Ricard }
28068957303SChristophe Ricard return 0;
28168957303SChristophe Ricard }
28268957303SChristophe Ricard
28368957303SChristophe Ricard /*
28468957303SChristophe Ricard * Prepare received data for upper layer.
28568957303SChristophe Ricard * Received data include byte stuffing, crc and sof/eof
28668957303SChristophe Ricard * which is not usable by hci part.
28768957303SChristophe Ricard * returns:
28868957303SChristophe Ricard * frame size without sof/eof, header and byte stuffing
28968957303SChristophe Ricard * -EBADMSG : frame was incorrect and discarded
29068957303SChristophe Ricard */
st21nfca_hci_i2c_repack(struct sk_buff * skb)29168957303SChristophe Ricard static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
29268957303SChristophe Ricard {
29368957303SChristophe Ricard int i, j, r, size;
2943e6df919SChristophe Ricard
29568957303SChristophe Ricard if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
29668957303SChristophe Ricard return -EBADMSG;
29768957303SChristophe Ricard
29868957303SChristophe Ricard size = get_frame_size(skb->data, skb->len);
29968957303SChristophe Ricard if (size > 0) {
30068957303SChristophe Ricard skb_trim(skb, size);
30168957303SChristophe Ricard /* remove ST21NFCA byte stuffing for upper layer */
30268957303SChristophe Ricard for (i = 1, j = 0; i < skb->len; i++) {
3033096e25aSChristophe Ricard if (skb->data[i + j] ==
30468957303SChristophe Ricard (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
3053096e25aSChristophe Ricard skb->data[i] = skb->data[i + j + 1]
3063096e25aSChristophe Ricard | ST21NFCA_BYTE_STUFFING_MASK;
30768957303SChristophe Ricard i++;
30868957303SChristophe Ricard j++;
30968957303SChristophe Ricard }
31068957303SChristophe Ricard skb->data[i] = skb->data[i + j];
31168957303SChristophe Ricard }
31268957303SChristophe Ricard /* remove byte stuffing useless byte */
31368957303SChristophe Ricard skb_trim(skb, i - j);
31468957303SChristophe Ricard /* remove ST21NFCA_SOF_EOF from head */
31568957303SChristophe Ricard skb_pull(skb, 1);
31668957303SChristophe Ricard
31768957303SChristophe Ricard r = check_crc(skb->data, skb->len);
318314fbde9SColin Ian King if (r != 0)
31968957303SChristophe Ricard return -EBADMSG;
32068957303SChristophe Ricard
32168957303SChristophe Ricard /* remove headbyte */
32268957303SChristophe Ricard skb_pull(skb, 1);
32368957303SChristophe Ricard /* remove crc. Byte Stuffing is already removed here */
32468957303SChristophe Ricard skb_trim(skb, skb->len - 2);
32568957303SChristophe Ricard return skb->len;
32668957303SChristophe Ricard }
32768957303SChristophe Ricard return 0;
32868957303SChristophe Ricard }
32968957303SChristophe Ricard
33068957303SChristophe Ricard /*
33168957303SChristophe Ricard * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
33268957303SChristophe Ricard * that i2c bus will be flushed and that next read will start on a new frame.
33368957303SChristophe Ricard * returned skb contains only LLC header and payload.
33468957303SChristophe Ricard * returns:
33568957303SChristophe Ricard * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
33668957303SChristophe Ricard * end of read)
33768957303SChristophe Ricard * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
33868957303SChristophe Ricard * at end of read)
33968957303SChristophe Ricard * -EREMOTEIO : i2c read error (fatal)
34068957303SChristophe Ricard * -EBADMSG : frame was incorrect and discarded
34168957303SChristophe Ricard * (value returned from st21nfca_hci_i2c_repack)
34268957303SChristophe Ricard * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
34368957303SChristophe Ricard * the read length end sequence
34468957303SChristophe Ricard */
st21nfca_hci_i2c_read(struct st21nfca_i2c_phy * phy,struct sk_buff * skb)34568957303SChristophe Ricard static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
34668957303SChristophe Ricard struct sk_buff *skb)
34768957303SChristophe Ricard {
34868957303SChristophe Ricard int r, i;
34968957303SChristophe Ricard u8 len;
350c97ffdbfSChristophe Ricard u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
35168957303SChristophe Ricard struct i2c_client *client = phy->i2c_dev;
35268957303SChristophe Ricard
35368957303SChristophe Ricard if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
35468957303SChristophe Ricard len = len_seq[phy->current_read_len];
35568957303SChristophe Ricard
35668957303SChristophe Ricard /*
35768957303SChristophe Ricard * Add retry mecanism
35868957303SChristophe Ricard * Operation on I2C interface may fail in case of operation on
35968957303SChristophe Ricard * RF or SWP interface
36068957303SChristophe Ricard */
36168957303SChristophe Ricard r = 0;
362a3c5d8fbSChristophe Ricard mutex_lock(&phy->phy_lock);
36368957303SChristophe Ricard for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
364c97ffdbfSChristophe Ricard r = i2c_master_recv(client, buf, len);
36568957303SChristophe Ricard if (r < 0)
36668957303SChristophe Ricard msleep(wait_tab[i]);
36768957303SChristophe Ricard }
368a3c5d8fbSChristophe Ricard mutex_unlock(&phy->phy_lock);
36968957303SChristophe Ricard
37068957303SChristophe Ricard if (r != len) {
37168957303SChristophe Ricard phy->current_read_len = 0;
37268957303SChristophe Ricard return -EREMOTEIO;
37368957303SChristophe Ricard }
37468957303SChristophe Ricard
375c97ffdbfSChristophe Ricard /*
376c97ffdbfSChristophe Ricard * The first read sequence does not start with SOF.
377c97ffdbfSChristophe Ricard * Data is corrupeted so we drop it.
378c97ffdbfSChristophe Ricard */
3798e9466ccSChristophe Ricard if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
380c97ffdbfSChristophe Ricard skb_trim(skb, 0);
381c97ffdbfSChristophe Ricard phy->current_read_len = 0;
382c97ffdbfSChristophe Ricard return -EIO;
3838e9466ccSChristophe Ricard } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
384c97ffdbfSChristophe Ricard /*
385c97ffdbfSChristophe Ricard * Previous frame transmission was interrupted and
386c97ffdbfSChristophe Ricard * the frame got repeated.
387c97ffdbfSChristophe Ricard * Received frame start with ST21NFCA_SOF_EOF + 00.
388c97ffdbfSChristophe Ricard */
389c97ffdbfSChristophe Ricard skb_trim(skb, 0);
390c97ffdbfSChristophe Ricard phy->current_read_len = 0;
391c97ffdbfSChristophe Ricard }
392c97ffdbfSChristophe Ricard
39359ae1d12SJohannes Berg skb_put_data(skb, buf, len);
394c97ffdbfSChristophe Ricard
395c97ffdbfSChristophe Ricard if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
39668957303SChristophe Ricard phy->current_read_len = 0;
39768957303SChristophe Ricard return st21nfca_hci_i2c_repack(skb);
39868957303SChristophe Ricard }
39968957303SChristophe Ricard phy->current_read_len++;
40068957303SChristophe Ricard return -EAGAIN;
40168957303SChristophe Ricard }
40268957303SChristophe Ricard return -EIO;
40368957303SChristophe Ricard }
40468957303SChristophe Ricard
40568957303SChristophe Ricard /*
40668957303SChristophe Ricard * Reads an shdlc frame from the chip. This is not as straightforward as it
40768957303SChristophe Ricard * seems. The frame format is data-crc, and corruption can occur anywhere
40868957303SChristophe Ricard * while transiting on i2c bus, such that we could read an invalid data.
40968957303SChristophe Ricard * The tricky case is when we read a corrupted data or crc. We must detect
41068957303SChristophe Ricard * this here in order to determine that data can be transmitted to the hci
41168957303SChristophe Ricard * core. This is the reason why we check the crc here.
41268957303SChristophe Ricard * The CLF will repeat a frame until we send a RR on that frame.
41368957303SChristophe Ricard *
41468957303SChristophe Ricard * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
41568957303SChristophe Ricard * available in the incoming data, other IRQ might come. Every IRQ will trigger
41668957303SChristophe Ricard * a read sequence with different length and will fill the current frame.
41768957303SChristophe Ricard * The reception is complete once we reach a ST21NFCA_SOF_EOF.
41868957303SChristophe Ricard */
st21nfca_hci_irq_thread_fn(int irq,void * phy_id)41968957303SChristophe Ricard static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
42068957303SChristophe Ricard {
42168957303SChristophe Ricard struct st21nfca_i2c_phy *phy = phy_id;
42268957303SChristophe Ricard
42368957303SChristophe Ricard int r;
42468957303SChristophe Ricard
42568957303SChristophe Ricard if (!phy || irq != phy->i2c_dev->irq) {
42668957303SChristophe Ricard WARN_ON_ONCE(1);
42768957303SChristophe Ricard return IRQ_NONE;
42868957303SChristophe Ricard }
42968957303SChristophe Ricard
43068957303SChristophe Ricard if (phy->hard_fault != 0)
43168957303SChristophe Ricard return IRQ_HANDLED;
43268957303SChristophe Ricard
43368957303SChristophe Ricard r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
43468957303SChristophe Ricard if (r == -EREMOTEIO) {
43568957303SChristophe Ricard phy->hard_fault = r;
43668957303SChristophe Ricard
43768957303SChristophe Ricard nfc_hci_recv_frame(phy->hdev, NULL);
43868957303SChristophe Ricard
43968957303SChristophe Ricard return IRQ_HANDLED;
44068957303SChristophe Ricard } else if (r == -EAGAIN || r == -EIO) {
44168957303SChristophe Ricard return IRQ_HANDLED;
44268957303SChristophe Ricard } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
44368957303SChristophe Ricard /*
44468957303SChristophe Ricard * With ST21NFCA, only one interface (I2C, RF or SWP)
44568957303SChristophe Ricard * may be active at a time.
44668957303SChristophe Ricard * Having incorrect crc is usually due to i2c macrocell
44768957303SChristophe Ricard * deactivation in the middle of a transmission.
44868957303SChristophe Ricard * It may generate corrupted data on i2c.
44968957303SChristophe Ricard * We give sometime to get i2c back.
45068957303SChristophe Ricard * The complete frame will be repeated.
45168957303SChristophe Ricard */
45268957303SChristophe Ricard msleep(wait_tab[phy->crc_trials]);
45368957303SChristophe Ricard phy->crc_trials++;
45468957303SChristophe Ricard phy->current_read_len = 0;
4550c942b00SChristophe Ricard kfree_skb(phy->pending_skb);
45668957303SChristophe Ricard } else if (r > 0) {
45768957303SChristophe Ricard /*
45868957303SChristophe Ricard * We succeeded to read data from the CLF and
45968957303SChristophe Ricard * data is valid.
46068957303SChristophe Ricard * Reset counter.
46168957303SChristophe Ricard */
46268957303SChristophe Ricard nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
46368957303SChristophe Ricard phy->crc_trials = 0;
464cf577344SChristophe Ricard } else {
465cf577344SChristophe Ricard kfree_skb(phy->pending_skb);
46668957303SChristophe Ricard }
46768957303SChristophe Ricard
46868957303SChristophe Ricard phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
46968957303SChristophe Ricard if (phy->pending_skb == NULL) {
47068957303SChristophe Ricard phy->hard_fault = -ENOMEM;
47168957303SChristophe Ricard nfc_hci_recv_frame(phy->hdev, NULL);
47268957303SChristophe Ricard }
47368957303SChristophe Ricard
47468957303SChristophe Ricard return IRQ_HANDLED;
47568957303SChristophe Ricard }
47668957303SChristophe Ricard
4777a5e98daSKrzysztof Kozlowski static const struct nfc_phy_ops i2c_phy_ops = {
47868957303SChristophe Ricard .write = st21nfca_hci_i2c_write,
47968957303SChristophe Ricard .enable = st21nfca_hci_i2c_enable,
48068957303SChristophe Ricard .disable = st21nfca_hci_i2c_disable,
48168957303SChristophe Ricard };
48268957303SChristophe Ricard
483394671e7SAndy Shevchenko static const struct acpi_gpio_params enable_gpios = { 1, 0, false };
484394671e7SAndy Shevchenko
485394671e7SAndy Shevchenko static const struct acpi_gpio_mapping acpi_st21nfca_gpios[] = {
486394671e7SAndy Shevchenko { "enable-gpios", &enable_gpios, 1 },
487394671e7SAndy Shevchenko {},
488394671e7SAndy Shevchenko };
489394671e7SAndy Shevchenko
st21nfca_hci_i2c_probe(struct i2c_client * client)4901fa08273SUwe Kleine-König static int st21nfca_hci_i2c_probe(struct i2c_client *client)
49168957303SChristophe Ricard {
492d31748beSAndy Shevchenko struct device *dev = &client->dev;
49368957303SChristophe Ricard struct st21nfca_i2c_phy *phy;
494fcb45e6aSChristophe Ricard int r;
49568957303SChristophe Ricard
49668957303SChristophe Ricard if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
49768957303SChristophe Ricard nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
49868957303SChristophe Ricard return -ENODEV;
49968957303SChristophe Ricard }
50068957303SChristophe Ricard
50168957303SChristophe Ricard phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
50268957303SChristophe Ricard GFP_KERNEL);
5032b702832SChristophe Ricard if (!phy)
50468957303SChristophe Ricard return -ENOMEM;
50568957303SChristophe Ricard
50668957303SChristophe Ricard phy->i2c_dev = client;
507fcb45e6aSChristophe Ricard phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
508fcb45e6aSChristophe Ricard if (phy->pending_skb == NULL)
509fcb45e6aSChristophe Ricard return -ENOMEM;
51068957303SChristophe Ricard
511fcb45e6aSChristophe Ricard phy->current_read_len = 0;
512fcb45e6aSChristophe Ricard phy->crc_trials = 0;
513a3c5d8fbSChristophe Ricard mutex_init(&phy->phy_lock);
51468957303SChristophe Ricard i2c_set_clientdata(client, phy);
51568957303SChristophe Ricard
516d31748beSAndy Shevchenko r = devm_acpi_dev_add_driver_gpios(dev, acpi_st21nfca_gpios);
517d31748beSAndy Shevchenko if (r)
518d31748beSAndy Shevchenko dev_dbg(dev, "Unable to add GPIO mapping table\n");
519d31748beSAndy Shevchenko
520d31748beSAndy Shevchenko /* Get EN GPIO from resource provider */
521d31748beSAndy Shevchenko phy->gpiod_ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
522d31748beSAndy Shevchenko if (IS_ERR(phy->gpiod_ena)) {
523d31748beSAndy Shevchenko nfc_err(dev, "Unable to get ENABLE GPIO\n");
5241b9dadbaSWei Yongjun r = PTR_ERR(phy->gpiod_ena);
5251b9dadbaSWei Yongjun goto out_free;
526fcb45e6aSChristophe Ricard }
527fcb45e6aSChristophe Ricard
528682fd618SAndy Shevchenko phy->se_status.is_ese_present =
529682fd618SAndy Shevchenko device_property_read_bool(&client->dev, "ese-present");
530682fd618SAndy Shevchenko phy->se_status.is_uicc_present =
531682fd618SAndy Shevchenko device_property_read_bool(&client->dev, "uicc-present");
532682fd618SAndy Shevchenko
53318d2c624SChristophe Ricard r = st21nfca_hci_platform_init(phy);
53418d2c624SChristophe Ricard if (r < 0) {
53518d2c624SChristophe Ricard nfc_err(&client->dev, "Unable to reboot st21nfca\n");
5361b9dadbaSWei Yongjun goto out_free;
53718d2c624SChristophe Ricard }
53818d2c624SChristophe Ricard
53968957303SChristophe Ricard r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
54068957303SChristophe Ricard st21nfca_hci_irq_thread_fn,
5418e7836d0SAndy Shevchenko IRQF_ONESHOT,
54268957303SChristophe Ricard ST21NFCA_HCI_DRIVER_NAME, phy);
54368957303SChristophe Ricard if (r < 0) {
54468957303SChristophe Ricard nfc_err(&client->dev, "Unable to register IRQ handler\n");
5451b9dadbaSWei Yongjun goto out_free;
54668957303SChristophe Ricard }
54768957303SChristophe Ricard
5481b9dadbaSWei Yongjun r = st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
5492130fb97SChristophe Ricard ST21NFCA_FRAME_HEADROOM,
5502130fb97SChristophe Ricard ST21NFCA_FRAME_TAILROOM,
5512130fb97SChristophe Ricard ST21NFCA_HCI_LLC_MAX_PAYLOAD,
5522130fb97SChristophe Ricard &phy->hdev,
5532130fb97SChristophe Ricard &phy->se_status);
5541b9dadbaSWei Yongjun if (r)
5551b9dadbaSWei Yongjun goto out_free;
5561b9dadbaSWei Yongjun
5571b9dadbaSWei Yongjun return 0;
5581b9dadbaSWei Yongjun
5591b9dadbaSWei Yongjun out_free:
5601b9dadbaSWei Yongjun kfree_skb(phy->pending_skb);
5611b9dadbaSWei Yongjun return r;
56268957303SChristophe Ricard }
56368957303SChristophe Ricard
st21nfca_hci_i2c_remove(struct i2c_client * client)564ed5c2f5fSUwe Kleine-König static void st21nfca_hci_i2c_remove(struct i2c_client *client)
56568957303SChristophe Ricard {
56668957303SChristophe Ricard struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
56768957303SChristophe Ricard
56868957303SChristophe Ricard st21nfca_hci_remove(phy->hdev);
56968957303SChristophe Ricard
57068957303SChristophe Ricard if (phy->powered)
57168957303SChristophe Ricard st21nfca_hci_i2c_disable(phy);
5721b9dadbaSWei Yongjun kfree_skb(phy->pending_skb);
57368957303SChristophe Ricard }
57468957303SChristophe Ricard
575a122ffd0SArvind Yadav static const struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
57639db2d21SChristophe Ricard {ST21NFCA_HCI_DRIVER_NAME, 0},
57739db2d21SChristophe Ricard {}
57839db2d21SChristophe Ricard };
57939db2d21SChristophe Ricard MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
58039db2d21SChristophe Ricard
58180627802SKrzysztof Kozlowski static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] __maybe_unused = {
582dfa8070dSChristophe Ricard {"SMO2100", 0},
583dfa8070dSChristophe Ricard {}
584dfa8070dSChristophe Ricard };
585dfa8070dSChristophe Ricard MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match);
586dfa8070dSChristophe Ricard
58780627802SKrzysztof Kozlowski static const struct of_device_id of_st21nfca_i2c_match[] __maybe_unused = {
5886b5fba4eSChristophe Ricard { .compatible = "st,st21nfca-i2c", },
589c44cb2edSChristophe Ricard { .compatible = "st,st21nfca_i2c", },
590c44cb2edSChristophe Ricard {}
591c44cb2edSChristophe Ricard };
59217e40107SChristophe Ricard MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
593c44cb2edSChristophe Ricard
59468957303SChristophe Ricard static struct i2c_driver st21nfca_hci_i2c_driver = {
59568957303SChristophe Ricard .driver = {
59668957303SChristophe Ricard .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
597c44cb2edSChristophe Ricard .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
598dfa8070dSChristophe Ricard .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match),
59968957303SChristophe Ricard },
600*efc3001fSUwe Kleine-König .probe = st21nfca_hci_i2c_probe,
60168957303SChristophe Ricard .id_table = st21nfca_hci_i2c_id_table,
60268957303SChristophe Ricard .remove = st21nfca_hci_i2c_remove,
60368957303SChristophe Ricard };
60468957303SChristophe Ricard module_i2c_driver(st21nfca_hci_i2c_driver);
60568957303SChristophe Ricard
60668957303SChristophe Ricard MODULE_LICENSE("GPL");
60768957303SChristophe Ricard MODULE_DESCRIPTION(DRIVER_DESC);
608