Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 1 | /* |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 2 | * I2C Link Layer for ST NCI NFC controller familly based Driver |
| 3 | * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved. |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/of_irq.h> |
| 24 | #include <linux/of_gpio.h> |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/nfc.h> |
Christophe Ricard | 76b733d | 2015-08-14 22:33:30 +0200 | [diff] [blame] | 28 | #include <linux/platform_data/st-nci.h> |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 29 | |
Christophe Ricard | e67e7e5 | 2015-10-25 22:54:17 +0100 | [diff] [blame] | 30 | #include "st-nci.h" |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 31 | |
Christophe Ricard | 30458aa | 2015-08-14 22:33:31 +0200 | [diff] [blame] | 32 | #define DRIVER_DESC "NCI NFC driver for ST_NCI" |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 33 | |
| 34 | /* ndlc header */ |
Christophe Ricard | 064d004 | 2015-10-25 22:54:44 +0100 | [diff] [blame] | 35 | #define ST_NCI_FRAME_HEADROOM 1 |
Christophe Ricard | 30458aa | 2015-08-14 22:33:31 +0200 | [diff] [blame] | 36 | #define ST_NCI_FRAME_TAILROOM 0 |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 37 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 38 | #define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */ |
| 39 | #define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */ |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 40 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 41 | #define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c" |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 42 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 43 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 44 | struct st_nci_i2c_phy { |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 45 | struct i2c_client *i2c_dev; |
| 46 | struct llt_ndlc *ndlc; |
| 47 | |
Christophe Ricard | bb2496c | 2015-10-25 22:54:48 +0100 | [diff] [blame] | 48 | bool irq_active; |
| 49 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 50 | unsigned int gpio_reset; |
| 51 | unsigned int irq_polarity; |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 52 | |
| 53 | struct st_nci_se_status se_status; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 56 | static int st_nci_i2c_enable(void *phy_id) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 57 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 58 | struct st_nci_i2c_phy *phy = phy_id; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 59 | |
| 60 | gpio_set_value(phy->gpio_reset, 0); |
| 61 | usleep_range(10000, 15000); |
| 62 | gpio_set_value(phy->gpio_reset, 1); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 63 | usleep_range(80000, 85000); |
| 64 | |
Christophe Ricard | bb2496c | 2015-10-25 22:54:48 +0100 | [diff] [blame] | 65 | if (phy->ndlc->powered == 0 && phy->irq_active == 0) { |
Christophe Ricard | 05f0939 | 2015-06-06 13:16:51 +0200 | [diff] [blame] | 66 | enable_irq(phy->i2c_dev->irq); |
Christophe Ricard | bb2496c | 2015-10-25 22:54:48 +0100 | [diff] [blame] | 67 | phy->irq_active = true; |
| 68 | } |
Christophe Ricard | 05f0939 | 2015-06-06 13:16:51 +0200 | [diff] [blame] | 69 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 73 | static void st_nci_i2c_disable(void *phy_id) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 74 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 75 | struct st_nci_i2c_phy *phy = phy_id; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 76 | |
Christophe Ricard | 05f0939 | 2015-06-06 13:16:51 +0200 | [diff] [blame] | 77 | disable_irq_nosync(phy->i2c_dev->irq); |
Christophe Ricard | bb2496c | 2015-10-25 22:54:48 +0100 | [diff] [blame] | 78 | phy->irq_active = false; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Writing a frame must not return the number of written bytes. |
| 83 | * It must return either zero for success, or <0 for error. |
| 84 | * In addition, it must not alter the skb |
| 85 | */ |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 86 | static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 87 | { |
| 88 | int r = -1; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 89 | struct st_nci_i2c_phy *phy = phy_id; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 90 | struct i2c_client *client = phy->i2c_dev; |
| 91 | |
Christophe Ricard | 4294e32 | 2014-09-13 10:28:47 +0200 | [diff] [blame] | 92 | if (phy->ndlc->hard_fault != 0) |
| 93 | return phy->ndlc->hard_fault; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 94 | |
| 95 | r = i2c_master_send(client, skb->data, skb->len); |
Christophe Ricard | d4a41d1 | 2015-03-31 08:02:15 +0200 | [diff] [blame] | 96 | if (r < 0) { /* Retry, chip was in standby */ |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 97 | usleep_range(1000, 4000); |
| 98 | r = i2c_master_send(client, skb->data, skb->len); |
| 99 | } |
| 100 | |
| 101 | if (r >= 0) { |
| 102 | if (r != skb->len) |
| 103 | r = -EREMOTEIO; |
| 104 | else |
| 105 | r = 0; |
| 106 | } |
| 107 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 108 | return r; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Reads an ndlc frame and returns it in a newly allocated sk_buff. |
| 113 | * returns: |
Christophe Ricard | e7723b3 | 2015-08-14 22:33:32 +0200 | [diff] [blame] | 114 | * 0 : if received frame is complete |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 115 | * -EREMOTEIO : i2c read error (fatal) |
| 116 | * -EBADMSG : frame was incorrect and discarded |
Christophe Ricard | e7723b3 | 2015-08-14 22:33:32 +0200 | [diff] [blame] | 117 | * -ENOMEM : cannot allocate skb, frame dropped |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 118 | */ |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 119 | static int st_nci_i2c_read(struct st_nci_i2c_phy *phy, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 120 | struct sk_buff **skb) |
| 121 | { |
| 122 | int r; |
| 123 | u8 len; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 124 | u8 buf[ST_NCI_I2C_MAX_SIZE]; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 125 | struct i2c_client *client = phy->i2c_dev; |
| 126 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 127 | r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE); |
Christophe Ricard | d4a41d1 | 2015-03-31 08:02:15 +0200 | [diff] [blame] | 128 | if (r < 0) { /* Retry, chip was in standby */ |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 129 | usleep_range(1000, 4000); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 130 | r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE); |
Christophe Ricard | ac633ba | 2014-09-03 23:30:26 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 133 | if (r != ST_NCI_I2C_MIN_SIZE) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 134 | return -EREMOTEIO; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 135 | |
| 136 | len = be16_to_cpu(*(__be16 *) (buf + 2)); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 137 | if (len > ST_NCI_I2C_MAX_SIZE) { |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 138 | nfc_err(&client->dev, "invalid frame len\n"); |
| 139 | return -EBADMSG; |
| 140 | } |
| 141 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 142 | *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 143 | if (*skb == NULL) |
| 144 | return -ENOMEM; |
| 145 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 146 | skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE); |
| 147 | skb_put(*skb, ST_NCI_I2C_MIN_SIZE); |
| 148 | memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 149 | |
| 150 | if (!len) |
| 151 | return 0; |
| 152 | |
| 153 | r = i2c_master_recv(client, buf, len); |
| 154 | if (r != len) { |
| 155 | kfree_skb(*skb); |
| 156 | return -EREMOTEIO; |
| 157 | } |
| 158 | |
| 159 | skb_put(*skb, len); |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 160 | memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 161 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Reads an ndlc frame from the chip. |
| 167 | * |
Christophe Ricard | 30458aa | 2015-08-14 22:33:31 +0200 | [diff] [blame] | 168 | * On ST_NCI, IRQ goes in idle state when read starts. |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 169 | */ |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 170 | static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 171 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 172 | struct st_nci_i2c_phy *phy = phy_id; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 173 | struct i2c_client *client; |
| 174 | struct sk_buff *skb = NULL; |
| 175 | int r; |
| 176 | |
Christophe Ricard | 7274496 | 2015-01-25 23:33:29 +0100 | [diff] [blame] | 177 | if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) { |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 178 | WARN_ON_ONCE(1); |
| 179 | return IRQ_NONE; |
| 180 | } |
| 181 | |
| 182 | client = phy->i2c_dev; |
| 183 | dev_dbg(&client->dev, "IRQ\n"); |
| 184 | |
Christophe Ricard | 4294e32 | 2014-09-13 10:28:47 +0200 | [diff] [blame] | 185 | if (phy->ndlc->hard_fault) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 186 | return IRQ_HANDLED; |
| 187 | |
Christophe Ricard | 183fe2d | 2015-06-06 13:16:50 +0200 | [diff] [blame] | 188 | if (!phy->ndlc->powered) { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 189 | st_nci_i2c_disable(phy); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 190 | return IRQ_HANDLED; |
| 191 | } |
| 192 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 193 | r = st_nci_i2c_read(phy, &skb); |
Christophe Ricard | 4294e32 | 2014-09-13 10:28:47 +0200 | [diff] [blame] | 194 | if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 195 | return IRQ_HANDLED; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 196 | |
| 197 | ndlc_recv(phy->ndlc, skb); |
| 198 | |
| 199 | return IRQ_HANDLED; |
| 200 | } |
| 201 | |
| 202 | static struct nfc_phy_ops i2c_phy_ops = { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 203 | .write = st_nci_i2c_write, |
| 204 | .enable = st_nci_i2c_enable, |
| 205 | .disable = st_nci_i2c_disable, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 208 | static int st_nci_i2c_of_request_resources(struct i2c_client *client) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 209 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 210 | struct st_nci_i2c_phy *phy = i2c_get_clientdata(client); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 211 | struct device_node *pp; |
| 212 | int gpio; |
| 213 | int r; |
| 214 | |
| 215 | pp = client->dev.of_node; |
| 216 | if (!pp) |
| 217 | return -ENODEV; |
| 218 | |
| 219 | /* Get GPIO from device tree */ |
| 220 | gpio = of_get_named_gpio(pp, "reset-gpios", 0); |
| 221 | if (gpio < 0) { |
| 222 | nfc_err(&client->dev, |
| 223 | "Failed to retrieve reset-gpios from device tree\n"); |
| 224 | return gpio; |
| 225 | } |
| 226 | |
| 227 | /* GPIO request and configuration */ |
Christophe Ricard | 56ee645 | 2014-07-28 18:11:33 +0200 | [diff] [blame] | 228 | r = devm_gpio_request_one(&client->dev, gpio, |
| 229 | GPIOF_OUT_INIT_HIGH, "clf_reset"); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 230 | if (r) { |
| 231 | nfc_err(&client->dev, "Failed to request reset pin\n"); |
Christophe Ricard | 1815962 | 2014-12-02 21:27:56 +0100 | [diff] [blame] | 232 | return r; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 233 | } |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 234 | phy->gpio_reset = gpio; |
| 235 | |
Christophe Ricard | a80d0cb | 2014-11-13 00:30:26 +0100 | [diff] [blame] | 236 | phy->irq_polarity = irq_get_trigger_type(client->irq); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 237 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 238 | phy->se_status.is_ese_present = |
| 239 | of_property_read_bool(pp, "ese-present"); |
| 240 | phy->se_status.is_uicc_present = |
| 241 | of_property_read_bool(pp, "uicc-present"); |
| 242 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 245 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 246 | static int st_nci_i2c_request_resources(struct i2c_client *client) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 247 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 248 | struct st_nci_nfc_platform_data *pdata; |
| 249 | struct st_nci_i2c_phy *phy = i2c_get_clientdata(client); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 250 | int r; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 251 | |
| 252 | pdata = client->dev.platform_data; |
| 253 | if (pdata == NULL) { |
| 254 | nfc_err(&client->dev, "No platform data\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | /* store for later use */ |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 259 | phy->gpio_reset = pdata->gpio_reset; |
| 260 | phy->irq_polarity = pdata->irq_polarity; |
| 261 | |
Christophe Ricard | 56ee645 | 2014-07-28 18:11:33 +0200 | [diff] [blame] | 262 | r = devm_gpio_request_one(&client->dev, |
| 263 | phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset"); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 264 | if (r) { |
| 265 | pr_err("%s : reset gpio_request failed\n", __FILE__); |
Christophe Ricard | 1815962 | 2014-12-02 21:27:56 +0100 | [diff] [blame] | 266 | return r; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 269 | phy->se_status.is_ese_present = pdata->is_ese_present; |
| 270 | phy->se_status.is_uicc_present = pdata->is_uicc_present; |
| 271 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 275 | static int st_nci_i2c_probe(struct i2c_client *client, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 276 | const struct i2c_device_id *id) |
| 277 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 278 | struct st_nci_i2c_phy *phy; |
| 279 | struct st_nci_nfc_platform_data *pdata; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 280 | int r; |
| 281 | |
| 282 | dev_dbg(&client->dev, "%s\n", __func__); |
| 283 | dev_dbg(&client->dev, "IRQ: %d\n", client->irq); |
| 284 | |
| 285 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 286 | nfc_err(&client->dev, "Need I2C_FUNC_I2C\n"); |
| 287 | return -ENODEV; |
| 288 | } |
| 289 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 290 | phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy), |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 291 | GFP_KERNEL); |
Joe Perches | 3590ebc | 2015-04-07 00:17:00 -0700 | [diff] [blame] | 292 | if (!phy) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 293 | return -ENOMEM; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 294 | |
| 295 | phy->i2c_dev = client; |
| 296 | |
| 297 | i2c_set_clientdata(client, phy); |
| 298 | |
| 299 | pdata = client->dev.platform_data; |
| 300 | if (!pdata && client->dev.of_node) { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 301 | r = st_nci_i2c_of_request_resources(client); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 302 | if (r) { |
| 303 | nfc_err(&client->dev, "No platform data\n"); |
| 304 | return r; |
| 305 | } |
| 306 | } else if (pdata) { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 307 | r = st_nci_i2c_request_resources(client); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 308 | if (r) { |
| 309 | nfc_err(&client->dev, |
| 310 | "Cannot get platform resources\n"); |
| 311 | return r; |
| 312 | } |
| 313 | } else { |
| 314 | nfc_err(&client->dev, |
Christophe Ricard | 30458aa | 2015-08-14 22:33:31 +0200 | [diff] [blame] | 315 | "st_nci platform resources not available\n"); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 316 | return -ENODEV; |
| 317 | } |
| 318 | |
Christophe Ricard | 7274496 | 2015-01-25 23:33:29 +0100 | [diff] [blame] | 319 | r = ndlc_probe(phy, &i2c_phy_ops, &client->dev, |
Christophe Ricard | 30458aa | 2015-08-14 22:33:31 +0200 | [diff] [blame] | 320 | ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM, |
Christophe Ricard | 3648dc6 | 2015-10-25 22:54:39 +0100 | [diff] [blame] | 321 | &phy->ndlc, &phy->se_status); |
Christophe Ricard | 7274496 | 2015-01-25 23:33:29 +0100 | [diff] [blame] | 322 | if (r < 0) { |
| 323 | nfc_err(&client->dev, "Unable to register ndlc layer\n"); |
| 324 | return r; |
| 325 | } |
| 326 | |
Christophe Ricard | bb2496c | 2015-10-25 22:54:48 +0100 | [diff] [blame] | 327 | phy->irq_active = true; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 328 | r = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 329 | st_nci_irq_thread_fn, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 330 | phy->irq_polarity | IRQF_ONESHOT, |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 331 | ST_NCI_DRIVER_NAME, phy); |
Christophe Ricard | 7274496 | 2015-01-25 23:33:29 +0100 | [diff] [blame] | 332 | if (r < 0) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 333 | nfc_err(&client->dev, "Unable to register IRQ handler\n"); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 334 | |
Christophe Ricard | 7274496 | 2015-01-25 23:33:29 +0100 | [diff] [blame] | 335 | return r; |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 338 | static int st_nci_i2c_remove(struct i2c_client *client) |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 339 | { |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 340 | struct st_nci_i2c_phy *phy = i2c_get_clientdata(client); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 341 | |
| 342 | dev_dbg(&client->dev, "%s\n", __func__); |
| 343 | |
| 344 | ndlc_remove(phy->ndlc); |
| 345 | |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
Christophe Ricard | 3252897 | 2015-12-23 23:45:05 +0100 | [diff] [blame] | 349 | static struct i2c_device_id st_nci_i2c_id_table[] = { |
| 350 | {ST_NCI_DRIVER_NAME, 0}, |
| 351 | {} |
| 352 | }; |
| 353 | MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table); |
| 354 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 355 | static const struct of_device_id of_st_nci_i2c_match[] = { |
Christophe Ricard | 1a94cb6 | 2014-12-08 22:08:07 +0100 | [diff] [blame] | 356 | { .compatible = "st,st21nfcb-i2c", }, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 357 | { .compatible = "st,st21nfcb_i2c", }, |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 358 | { .compatible = "st,st21nfcc-i2c", }, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 359 | {} |
| 360 | }; |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 361 | MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 362 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 363 | static struct i2c_driver st_nci_i2c_driver = { |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 364 | .driver = { |
| 365 | .owner = THIS_MODULE, |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 366 | .name = ST_NCI_I2C_DRIVER_NAME, |
| 367 | .of_match_table = of_match_ptr(of_st_nci_i2c_match), |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 368 | }, |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 369 | .probe = st_nci_i2c_probe, |
| 370 | .id_table = st_nci_i2c_id_table, |
| 371 | .remove = st_nci_i2c_remove, |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 372 | }; |
| 373 | |
Christophe Ricard | ed06aeef | 2015-06-09 22:26:05 +0200 | [diff] [blame] | 374 | module_i2c_driver(st_nci_i2c_driver); |
Christophe Ricard | 35630df | 2014-05-25 22:35:38 +0200 | [diff] [blame] | 375 | |
| 376 | MODULE_LICENSE("GPL"); |
| 377 | MODULE_DESCRIPTION(DRIVER_DESC); |