1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2016, Intel Corporation
5 * -------------------------------------------------------------------------
8 #include <linux/module.h>
9 #include <linux/acpi.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/nfc.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <net/nfc/nfc.h>
16 #include <net/nfc/nci_core.h>
20 #define FDP_I2C_DRIVER_NAME "fdp_nci_i2c"
22 #define FDP_DP_CLOCK_TYPE_NAME "clock-type"
23 #define FDP_DP_CLOCK_FREQ_NAME "clock-freq"
24 #define FDP_DP_FW_VSC_CFG_NAME "fw-vsc-cfg"
26 #define FDP_FRAME_HEADROOM 2
27 #define FDP_FRAME_TAILROOM 1
29 #define FDP_NCI_I2C_MIN_PAYLOAD 5
30 #define FDP_NCI_I2C_MAX_PAYLOAD 261
32 #define FDP_POWER_OFF 0
33 #define FDP_POWER_ON 1
35 #define fdp_nci_i2c_dump_skb(dev, prefix, skb) \
36 print_hex_dump(KERN_DEBUG, prefix": ", DUMP_PREFIX_OFFSET, \
37 16, 1, (skb)->data, (skb)->len, 0)
39 static void fdp_nci_i2c_reset(struct fdp_i2c_phy *phy)
41 /* Reset RST/WakeUP for at least 100 micro-second */
42 gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_OFF);
43 usleep_range(1000, 4000);
44 gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_ON);
45 usleep_range(10000, 14000);
48 static int fdp_nci_i2c_enable(void *phy_id)
50 struct fdp_i2c_phy *phy = phy_id;
52 fdp_nci_i2c_reset(phy);
57 static void fdp_nci_i2c_disable(void *phy_id)
59 struct fdp_i2c_phy *phy = phy_id;
61 fdp_nci_i2c_reset(phy);
64 static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
69 /* Add length header */
71 *(u8 *)skb_push(skb, 1) = len & 0xff;
72 *(u8 *)skb_push(skb, 1) = len >> 8;
74 /* Compute and add lrc */
75 for (i = 0; i < len + 2; i++)
81 static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
83 skb_pull(skb, FDP_FRAME_HEADROOM);
84 skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
87 static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
89 struct fdp_i2c_phy *phy = phy_id;
90 struct i2c_client *client = phy->i2c_dev;
93 if (phy->hard_fault != 0)
94 return phy->hard_fault;
96 fdp_nci_i2c_add_len_lrc(skb);
97 fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
99 r = i2c_master_send(client, skb->data, skb->len);
100 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
101 usleep_range(1000, 4000);
102 r = i2c_master_send(client, skb->data, skb->len);
105 if (r < 0 || r != skb->len)
106 dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
107 __func__, r, skb->len);
118 fdp_nci_i2c_remove_len_lrc(skb);
123 static struct nfc_phy_ops i2c_phy_ops = {
124 .write = fdp_nci_i2c_write,
125 .enable = fdp_nci_i2c_enable,
126 .disable = fdp_nci_i2c_disable,
129 static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
132 u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
134 struct i2c_client *client = phy->i2c_dev;
138 /* Read the length packet and the data packet */
139 for (k = 0; k < 2; k++) {
141 len = phy->next_read_size;
143 r = i2c_master_recv(client, tmp, len);
145 dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
150 /* Check packet integruty */
151 for (lrc = i = 0; i < r; i++)
155 * LRC check failed. This may due to transmission error or
156 * desynchronization between driver and FDP. Drop the paquet
157 * and force resynchronization
160 dev_dbg(&client->dev, "%s: corrupted packet\n",
162 phy->next_read_size = 5;
166 /* Packet that contains a length */
167 if (tmp[0] == 0 && tmp[1] == 0) {
168 phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
170 phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
172 *skb = alloc_skb(len, GFP_KERNEL);
178 skb_put_data(*skb, tmp, len);
179 fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
181 fdp_nci_i2c_remove_len_lrc(*skb);
188 /* Flush the remaining data */
189 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
195 static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
197 struct fdp_i2c_phy *phy = phy_id;
201 if (!phy || irq != phy->i2c_dev->irq) {
206 r = fdp_nci_i2c_read(phy, &skb);
210 else if (r == -ENOMEM || r == -EBADMSG)
214 nci_recv_frame(phy->ndev, skb);
219 static void fdp_nci_i2c_read_device_properties(struct device *dev,
220 u8 *clock_type, u32 *clock_freq,
226 r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
228 dev_dbg(dev, "Using default clock type");
232 r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
234 dev_dbg(dev, "Using default clock frequency\n");
238 if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
239 r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
245 /* Add 1 to the length to inclue the length byte itself */
248 *fw_vsc_cfg = devm_kmalloc_array(dev,
249 len, sizeof(**fw_vsc_cfg),
255 r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
259 devm_kfree(dev, *fw_vsc_cfg);
264 dev_dbg(dev, "FW vendor specific commands not present\n");
269 dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
270 *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
273 static const struct acpi_gpio_params power_gpios = { 0, 0, false };
275 static const struct acpi_gpio_mapping acpi_fdp_gpios[] = {
276 { "power-gpios", &power_gpios, 1 },
280 static int fdp_nci_i2c_probe(struct i2c_client *client)
282 struct fdp_i2c_phy *phy;
283 struct device *dev = &client->dev;
289 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
290 nfc_err(dev, "No I2C_FUNC_I2C support\n");
294 /* Checking if we have an irq */
295 if (client->irq <= 0) {
296 nfc_err(dev, "IRQ not present\n");
300 phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy), GFP_KERNEL);
304 phy->i2c_dev = client;
305 phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
306 i2c_set_clientdata(client, phy);
308 r = devm_request_threaded_irq(dev, client->irq,
309 NULL, fdp_nci_i2c_irq_thread_fn,
310 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
311 FDP_I2C_DRIVER_NAME, phy);
314 nfc_err(&client->dev, "Unable to register IRQ handler\n");
318 r = devm_acpi_dev_add_driver_gpios(dev, acpi_fdp_gpios);
320 dev_dbg(dev, "Unable to add GPIO mapping table\n");
322 /* Requesting the power gpio */
323 phy->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
324 if (IS_ERR(phy->power_gpio)) {
325 nfc_err(dev, "Power GPIO request failed\n");
326 return PTR_ERR(phy->power_gpio);
329 /* read device properties to get the clock and production settings */
330 fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
333 /* Call the NFC specific probe function */
334 r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
335 FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
336 clock_type, clock_freq, fw_vsc_cfg);
338 nfc_err(dev, "NCI probing error\n");
342 dev_dbg(dev, "I2C driver loaded\n");
346 static int fdp_nci_i2c_remove(struct i2c_client *client)
348 struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
350 fdp_nci_remove(phy->ndev);
351 fdp_nci_i2c_disable(phy);
356 static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
360 MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
362 static struct i2c_driver fdp_nci_i2c_driver = {
364 .name = FDP_I2C_DRIVER_NAME,
365 .acpi_match_table = ACPI_PTR(fdp_nci_i2c_acpi_match),
367 .probe_new = fdp_nci_i2c_probe,
368 .remove = fdp_nci_i2c_remove,
370 module_i2c_driver(fdp_nci_i2c_driver);
372 MODULE_LICENSE("GPL");
373 MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
374 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");