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