0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/acpi.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/delay.h>
0015 #include <linux/nfc.h>
0016 #include <linux/of.h>
0017
0018 #include "st-nci.h"
0019
0020 #define DRIVER_DESC "NCI NFC driver for ST_NCI"
0021
0022
0023 #define ST_NCI_FRAME_HEADROOM 1
0024 #define ST_NCI_FRAME_TAILROOM 0
0025
0026 #define ST_NCI_I2C_MIN_SIZE 4
0027 #define ST_NCI_I2C_MAX_SIZE 250
0028
0029 #define ST_NCI_DRIVER_NAME "st_nci"
0030 #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
0031
0032 struct st_nci_i2c_phy {
0033 struct i2c_client *i2c_dev;
0034 struct llt_ndlc *ndlc;
0035
0036 bool irq_active;
0037
0038 struct gpio_desc *gpiod_reset;
0039
0040 struct st_nci_se_status se_status;
0041 };
0042
0043 static int st_nci_i2c_enable(void *phy_id)
0044 {
0045 struct st_nci_i2c_phy *phy = phy_id;
0046
0047 gpiod_set_value(phy->gpiod_reset, 0);
0048 usleep_range(10000, 15000);
0049 gpiod_set_value(phy->gpiod_reset, 1);
0050 usleep_range(80000, 85000);
0051
0052 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
0053 enable_irq(phy->i2c_dev->irq);
0054 phy->irq_active = true;
0055 }
0056
0057 return 0;
0058 }
0059
0060 static void st_nci_i2c_disable(void *phy_id)
0061 {
0062 struct st_nci_i2c_phy *phy = phy_id;
0063
0064 disable_irq_nosync(phy->i2c_dev->irq);
0065 phy->irq_active = false;
0066 }
0067
0068
0069
0070
0071
0072
0073 static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
0074 {
0075 int r;
0076 struct st_nci_i2c_phy *phy = phy_id;
0077 struct i2c_client *client = phy->i2c_dev;
0078
0079 if (phy->ndlc->hard_fault != 0)
0080 return phy->ndlc->hard_fault;
0081
0082 r = i2c_master_send(client, skb->data, skb->len);
0083 if (r < 0) {
0084 usleep_range(1000, 4000);
0085 r = i2c_master_send(client, skb->data, skb->len);
0086 }
0087
0088 if (r >= 0) {
0089 if (r != skb->len)
0090 r = -EREMOTEIO;
0091 else
0092 r = 0;
0093 }
0094
0095 return r;
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
0107 struct sk_buff **skb)
0108 {
0109 int r;
0110 u8 len;
0111 u8 buf[ST_NCI_I2C_MAX_SIZE];
0112 struct i2c_client *client = phy->i2c_dev;
0113
0114 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
0115 if (r < 0) {
0116 usleep_range(1000, 4000);
0117 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
0118 }
0119
0120 if (r != ST_NCI_I2C_MIN_SIZE)
0121 return -EREMOTEIO;
0122
0123 len = be16_to_cpu(*(__be16 *) (buf + 2));
0124 if (len > ST_NCI_I2C_MAX_SIZE) {
0125 nfc_err(&client->dev, "invalid frame len\n");
0126 return -EBADMSG;
0127 }
0128
0129 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
0130 if (*skb == NULL)
0131 return -ENOMEM;
0132
0133 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
0134 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
0135 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
0136
0137 if (!len)
0138 return 0;
0139
0140 r = i2c_master_recv(client, buf, len);
0141 if (r != len) {
0142 kfree_skb(*skb);
0143 return -EREMOTEIO;
0144 }
0145
0146 skb_put(*skb, len);
0147 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
0148
0149 return 0;
0150 }
0151
0152
0153
0154
0155
0156
0157 static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
0158 {
0159 struct st_nci_i2c_phy *phy = phy_id;
0160 struct sk_buff *skb = NULL;
0161 int r;
0162
0163 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
0164 WARN_ON_ONCE(1);
0165 return IRQ_NONE;
0166 }
0167
0168 if (phy->ndlc->hard_fault)
0169 return IRQ_HANDLED;
0170
0171 if (!phy->ndlc->powered) {
0172 st_nci_i2c_disable(phy);
0173 return IRQ_HANDLED;
0174 }
0175
0176 r = st_nci_i2c_read(phy, &skb);
0177 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
0178 return IRQ_HANDLED;
0179
0180 ndlc_recv(phy->ndlc, skb);
0181
0182 return IRQ_HANDLED;
0183 }
0184
0185 static const struct nfc_phy_ops i2c_phy_ops = {
0186 .write = st_nci_i2c_write,
0187 .enable = st_nci_i2c_enable,
0188 .disable = st_nci_i2c_disable,
0189 };
0190
0191 static const struct acpi_gpio_params reset_gpios = { 1, 0, false };
0192
0193 static const struct acpi_gpio_mapping acpi_st_nci_gpios[] = {
0194 { "reset-gpios", &reset_gpios, 1 },
0195 {},
0196 };
0197
0198 static int st_nci_i2c_probe(struct i2c_client *client,
0199 const struct i2c_device_id *id)
0200 {
0201 struct device *dev = &client->dev;
0202 struct st_nci_i2c_phy *phy;
0203 int r;
0204
0205 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0206 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
0207 return -ENODEV;
0208 }
0209
0210 phy = devm_kzalloc(dev, sizeof(struct st_nci_i2c_phy), GFP_KERNEL);
0211 if (!phy)
0212 return -ENOMEM;
0213
0214 phy->i2c_dev = client;
0215
0216 i2c_set_clientdata(client, phy);
0217
0218 r = devm_acpi_dev_add_driver_gpios(dev, acpi_st_nci_gpios);
0219 if (r)
0220 dev_dbg(dev, "Unable to add GPIO mapping table\n");
0221
0222
0223 phy->gpiod_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
0224 if (IS_ERR(phy->gpiod_reset)) {
0225 nfc_err(dev, "Unable to get RESET GPIO\n");
0226 return -ENODEV;
0227 }
0228
0229 phy->se_status.is_ese_present =
0230 device_property_read_bool(dev, "ese-present");
0231 phy->se_status.is_uicc_present =
0232 device_property_read_bool(dev, "uicc-present");
0233
0234 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
0235 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
0236 &phy->ndlc, &phy->se_status);
0237 if (r < 0) {
0238 nfc_err(&client->dev, "Unable to register ndlc layer\n");
0239 return r;
0240 }
0241
0242 phy->irq_active = true;
0243 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
0244 st_nci_irq_thread_fn,
0245 IRQF_ONESHOT,
0246 ST_NCI_DRIVER_NAME, phy);
0247 if (r < 0)
0248 nfc_err(&client->dev, "Unable to register IRQ handler\n");
0249
0250 return r;
0251 }
0252
0253 static int st_nci_i2c_remove(struct i2c_client *client)
0254 {
0255 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
0256
0257 ndlc_remove(phy->ndlc);
0258
0259 return 0;
0260 }
0261
0262 static const struct i2c_device_id st_nci_i2c_id_table[] = {
0263 {ST_NCI_DRIVER_NAME, 0},
0264 {}
0265 };
0266 MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
0267
0268 static const struct acpi_device_id st_nci_i2c_acpi_match[] __maybe_unused = {
0269 {"SMO2101"},
0270 {"SMO2102"},
0271 {}
0272 };
0273 MODULE_DEVICE_TABLE(acpi, st_nci_i2c_acpi_match);
0274
0275 static const struct of_device_id of_st_nci_i2c_match[] __maybe_unused = {
0276 { .compatible = "st,st21nfcb-i2c", },
0277 { .compatible = "st,st21nfcb_i2c", },
0278 { .compatible = "st,st21nfcc-i2c", },
0279 {}
0280 };
0281 MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
0282
0283 static struct i2c_driver st_nci_i2c_driver = {
0284 .driver = {
0285 .name = ST_NCI_I2C_DRIVER_NAME,
0286 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
0287 .acpi_match_table = ACPI_PTR(st_nci_i2c_acpi_match),
0288 },
0289 .probe = st_nci_i2c_probe,
0290 .id_table = st_nci_i2c_id_table,
0291 .remove = st_nci_i2c_remove,
0292 };
0293 module_i2c_driver(st_nci_i2c_driver);
0294
0295 MODULE_LICENSE("GPL");
0296 MODULE_DESCRIPTION(DRIVER_DESC);