1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Marvell NFC-over-I2C driver: I2C interface related functions 4 * 5 * Copyright (C) 2015, Marvell International Ltd. 6 */ 7 8 #include <linux/module.h> 9 #include <linux/interrupt.h> 10 #include <linux/i2c.h> 11 #include <linux/nfc.h> 12 #include <linux/delay.h> 13 #include <linux/of_irq.h> 14 #include <net/nfc/nci.h> 15 #include <net/nfc/nci_core.h> 16 #include "nfcmrvl.h" 17 18 struct nfcmrvl_i2c_drv_data { 19 unsigned long flags; 20 struct device *dev; 21 struct i2c_client *i2c; 22 struct nfcmrvl_private *priv; 23 }; 24 25 static int nfcmrvl_i2c_read(struct nfcmrvl_i2c_drv_data *drv_data, 26 struct sk_buff **skb) 27 { 28 int ret; 29 struct nci_ctrl_hdr nci_hdr; 30 31 /* Read NCI header to know the payload size */ 32 ret = i2c_master_recv(drv_data->i2c, (u8 *)&nci_hdr, NCI_CTRL_HDR_SIZE); 33 if (ret != NCI_CTRL_HDR_SIZE) { 34 nfc_err(&drv_data->i2c->dev, "cannot read NCI header\n"); 35 return -EBADMSG; 36 } 37 38 *skb = nci_skb_alloc(drv_data->priv->ndev, 39 nci_hdr.plen + NCI_CTRL_HDR_SIZE, GFP_KERNEL); 40 if (!*skb) 41 return -ENOMEM; 42 43 /* Copy NCI header into the SKB */ 44 skb_put_data(*skb, &nci_hdr, NCI_CTRL_HDR_SIZE); 45 46 if (nci_hdr.plen) { 47 /* Read the NCI payload */ 48 ret = i2c_master_recv(drv_data->i2c, 49 skb_put(*skb, nci_hdr.plen), 50 nci_hdr.plen); 51 52 if (ret != nci_hdr.plen) { 53 nfc_err(&drv_data->i2c->dev, 54 "Invalid frame payload length: %u (expected %u)\n", 55 ret, nci_hdr.plen); 56 kfree_skb(*skb); 57 return -EBADMSG; 58 } 59 } 60 61 return 0; 62 } 63 64 static irqreturn_t nfcmrvl_i2c_int_irq_thread_fn(int irq, void *drv_data_ptr) 65 { 66 struct nfcmrvl_i2c_drv_data *drv_data = drv_data_ptr; 67 struct sk_buff *skb = NULL; 68 int ret; 69 70 if (!drv_data->priv) 71 return IRQ_HANDLED; 72 73 if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags)) 74 return IRQ_HANDLED; 75 76 ret = nfcmrvl_i2c_read(drv_data, &skb); 77 78 switch (ret) { 79 case -EREMOTEIO: 80 set_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags); 81 break; 82 case -ENOMEM: 83 case -EBADMSG: 84 nfc_err(&drv_data->i2c->dev, "read failed %d\n", ret); 85 break; 86 default: 87 if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0) 88 nfc_err(&drv_data->i2c->dev, "corrupted RX packet\n"); 89 break; 90 } 91 return IRQ_HANDLED; 92 } 93 94 static int nfcmrvl_i2c_nci_open(struct nfcmrvl_private *priv) 95 { 96 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data; 97 98 if (!drv_data) 99 return -ENODEV; 100 101 return 0; 102 } 103 104 static int nfcmrvl_i2c_nci_close(struct nfcmrvl_private *priv) 105 { 106 return 0; 107 } 108 109 static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv, 110 struct sk_buff *skb) 111 { 112 struct nfcmrvl_i2c_drv_data *drv_data = priv->drv_data; 113 int ret; 114 115 if (test_bit(NFCMRVL_PHY_ERROR, &priv->flags)) 116 return -EREMOTEIO; 117 118 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len); 119 120 /* Retry if chip was in standby */ 121 if (ret == -EREMOTEIO) { 122 nfc_info(drv_data->dev, "chip may sleep, retry\n"); 123 usleep_range(6000, 10000); 124 ret = i2c_master_send(drv_data->i2c, skb->data, skb->len); 125 } 126 127 if (ret >= 0) { 128 if (ret != skb->len) { 129 nfc_err(drv_data->dev, 130 "Invalid length sent: %u (expected %u)\n", 131 ret, skb->len); 132 ret = -EREMOTEIO; 133 } else 134 ret = 0; 135 } 136 137 if (ret) { 138 kfree_skb(skb); 139 return ret; 140 } 141 142 consume_skb(skb); 143 return 0; 144 } 145 146 static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv, 147 const void *param) 148 { 149 } 150 151 static const struct nfcmrvl_if_ops i2c_ops = { 152 .nci_open = nfcmrvl_i2c_nci_open, 153 .nci_close = nfcmrvl_i2c_nci_close, 154 .nci_send = nfcmrvl_i2c_nci_send, 155 .nci_update_config = nfcmrvl_i2c_nci_update_config, 156 }; 157 158 static int nfcmrvl_i2c_parse_dt(struct device_node *node, 159 struct nfcmrvl_platform_data *pdata) 160 { 161 int ret; 162 163 ret = nfcmrvl_parse_dt(node, pdata); 164 if (ret < 0) { 165 pr_err("Failed to get generic entries\n"); 166 return ret; 167 } 168 169 if (of_find_property(node, "i2c-int-falling", NULL)) 170 pdata->irq_polarity = IRQF_TRIGGER_FALLING; 171 else 172 pdata->irq_polarity = IRQF_TRIGGER_RISING; 173 174 ret = irq_of_parse_and_map(node, 0); 175 if (!ret) { 176 pr_err("Unable to get irq\n"); 177 return -EINVAL; 178 } 179 pdata->irq = ret; 180 181 return 0; 182 } 183 184 static int nfcmrvl_i2c_probe(struct i2c_client *client) 185 { 186 const struct nfcmrvl_platform_data *pdata; 187 struct nfcmrvl_i2c_drv_data *drv_data; 188 struct nfcmrvl_platform_data config; 189 int ret; 190 191 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 192 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); 193 return -ENODEV; 194 } 195 196 drv_data = devm_kzalloc(&client->dev, sizeof(*drv_data), GFP_KERNEL); 197 if (!drv_data) 198 return -ENOMEM; 199 200 drv_data->i2c = client; 201 drv_data->dev = &client->dev; 202 drv_data->priv = NULL; 203 204 i2c_set_clientdata(client, drv_data); 205 206 pdata = client->dev.platform_data; 207 208 if (!pdata && client->dev.of_node) 209 if (nfcmrvl_i2c_parse_dt(client->dev.of_node, &config) == 0) 210 pdata = &config; 211 212 if (!pdata) 213 return -EINVAL; 214 215 /* Request the read IRQ */ 216 ret = devm_request_threaded_irq(&drv_data->i2c->dev, pdata->irq, 217 NULL, nfcmrvl_i2c_int_irq_thread_fn, 218 pdata->irq_polarity | IRQF_ONESHOT, 219 "nfcmrvl_i2c_int", drv_data); 220 if (ret < 0) { 221 nfc_err(&drv_data->i2c->dev, 222 "Unable to register IRQ handler\n"); 223 return ret; 224 } 225 226 drv_data->priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_I2C, 227 drv_data, &i2c_ops, 228 &drv_data->i2c->dev, pdata); 229 230 if (IS_ERR(drv_data->priv)) 231 return PTR_ERR(drv_data->priv); 232 233 drv_data->priv->support_fw_dnld = true; 234 235 return 0; 236 } 237 238 static void nfcmrvl_i2c_remove(struct i2c_client *client) 239 { 240 struct nfcmrvl_i2c_drv_data *drv_data = i2c_get_clientdata(client); 241 242 nfcmrvl_nci_unregister_dev(drv_data->priv); 243 } 244 245 246 static const struct of_device_id of_nfcmrvl_i2c_match[] __maybe_unused = { 247 { .compatible = "marvell,nfc-i2c", }, 248 {}, 249 }; 250 MODULE_DEVICE_TABLE(of, of_nfcmrvl_i2c_match); 251 252 static const struct i2c_device_id nfcmrvl_i2c_id_table[] = { 253 { "nfcmrvl_i2c", 0 }, 254 {} 255 }; 256 MODULE_DEVICE_TABLE(i2c, nfcmrvl_i2c_id_table); 257 258 static struct i2c_driver nfcmrvl_i2c_driver = { 259 .probe_new = nfcmrvl_i2c_probe, 260 .id_table = nfcmrvl_i2c_id_table, 261 .remove = nfcmrvl_i2c_remove, 262 .driver = { 263 .name = "nfcmrvl_i2c", 264 .of_match_table = of_match_ptr(of_nfcmrvl_i2c_match), 265 }, 266 }; 267 268 module_i2c_driver(nfcmrvl_i2c_driver); 269 270 MODULE_AUTHOR("Marvell International Ltd."); 271 MODULE_DESCRIPTION("Marvell NFC-over-I2C driver"); 272 MODULE_LICENSE("GPL v2"); 273