blob: fbd26ecbf4a49212b8d73d35224b7d0299eed64f [file] [log] [blame]
Robert Dolcaa06347c2015-10-22 12:11:42 +03001/* -------------------------------------------------------------------------
2 * Copyright (C) 2014-2016, Intel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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 */
15
16#include <linux/module.h>
17#include <linux/acpi.h>
18#include <linux/i2c.h>
19#include <linux/interrupt.h>
20#include <linux/nfc.h>
21#include <linux/delay.h>
22#include <linux/gpio/consumer.h>
23#include <net/nfc/nfc.h>
24#include <net/nfc/nci_core.h>
25
26#include "fdp.h"
27
28#define FDP_I2C_DRIVER_NAME "fdp_nci_i2c"
29
30#define FDP_DP_POWER_GPIO_NAME "power"
31#define FDP_DP_CLOCK_TYPE_NAME "clock-type"
32#define FDP_DP_CLOCK_FREQ_NAME "clock-freq"
33#define FDP_DP_FW_VSC_CFG_NAME "fw-vsc-cfg"
34
35#define FDP_FRAME_HEADROOM 2
36#define FDP_FRAME_TAILROOM 1
37
38#define FDP_NCI_I2C_MIN_PAYLOAD 5
39#define FDP_NCI_I2C_MAX_PAYLOAD 261
40
41#define FDP_POWER_OFF 0
42#define FDP_POWER_ON 1
43
44#define fdp_nci_i2c_dump_skb(dev, prefix, skb) \
45 print_hex_dump(KERN_DEBUG, prefix": ", DUMP_PREFIX_OFFSET, \
46 16, 1, (skb)->data, (skb)->len, 0)
47
48static void fdp_nci_i2c_reset(struct fdp_i2c_phy *phy)
49{
50 /* Reset RST/WakeUP for at least 100 micro-second */
51 gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_OFF);
52 usleep_range(1000, 4000);
53 gpiod_set_value_cansleep(phy->power_gpio, FDP_POWER_ON);
54 usleep_range(10000, 14000);
55}
56
57static int fdp_nci_i2c_enable(void *phy_id)
58{
59 struct fdp_i2c_phy *phy = phy_id;
60
61 dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
62 fdp_nci_i2c_reset(phy);
63
64 return 0;
65}
66
67static void fdp_nci_i2c_disable(void *phy_id)
68{
69 struct fdp_i2c_phy *phy = phy_id;
70
71 dev_dbg(&phy->i2c_dev->dev, "%s\n", __func__);
72 fdp_nci_i2c_reset(phy);
73}
74
75static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
76{
77 u8 lrc = 0;
78 u16 len, i;
79
80 /* Add length header */
81 len = skb->len;
82 *skb_push(skb, 1) = len & 0xff;
83 *skb_push(skb, 1) = len >> 8;
84
85 /* Compute and add lrc */
86 for (i = 0; i < len + 2; i++)
87 lrc ^= skb->data[i];
88
89 *skb_put(skb, 1) = lrc;
90}
91
92static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
93{
94 skb_pull(skb, FDP_FRAME_HEADROOM);
95 skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
96}
97
98static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
99{
100 struct fdp_i2c_phy *phy = phy_id;
101 struct i2c_client *client = phy->i2c_dev;
102 int r;
103
104 if (phy->hard_fault != 0)
105 return phy->hard_fault;
106
107 fdp_nci_i2c_add_len_lrc(skb);
108 fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
109
110 r = i2c_master_send(client, skb->data, skb->len);
111 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
112 usleep_range(1000, 4000);
113 r = i2c_master_send(client, skb->data, skb->len);
114 }
115
116 if (r < 0 || r != skb->len)
117 dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
118 __func__, r, skb->len);
119
120 if (r >= 0) {
121 if (r != skb->len) {
122 phy->hard_fault = r;
123 r = -EREMOTEIO;
124 } else {
125 r = 0;
126 }
127 }
128
129 fdp_nci_i2c_remove_len_lrc(skb);
130
131 return r;
132}
133
134static struct nfc_phy_ops i2c_phy_ops = {
135 .write = fdp_nci_i2c_write,
136 .enable = fdp_nci_i2c_enable,
137 .disable = fdp_nci_i2c_disable,
138};
139
140static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
141{
142 int r, len;
143 u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
144 u16 i;
145 struct i2c_client *client = phy->i2c_dev;
146
147 *skb = NULL;
148
149 /* Read the length packet and the data packet */
150 for (k = 0; k < 2; k++) {
151
152 len = phy->next_read_size;
153
154 r = i2c_master_recv(client, tmp, len);
155 if (r != len) {
156 dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
157 __func__, r);
158 goto flush;
159 }
160
161 /* Check packet integruty */
162 for (lrc = i = 0; i < r; i++)
163 lrc ^= tmp[i];
164
165 /*
166 * LRC check failed. This may due to transmission error or
167 * desynchronization between driver and FDP. Drop the paquet
168 * and force resynchronization
169 */
170 if (lrc) {
171 dev_dbg(&client->dev, "%s: corrupted packet\n",
172 __func__);
173 phy->next_read_size = 5;
174 goto flush;
175 }
176
177 /* Packet that contains a length */
178 if (tmp[0] == 0 && tmp[1] == 0) {
179 phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
Suren Baghdasaryan6dda7ac2017-08-17 16:30:47 -0700180 /*
181 * Ensure next_read_size does not exceed sizeof(tmp)
182 * for reading that many bytes during next iteration
183 */
184 if (phy->next_read_size > FDP_NCI_I2C_MAX_PAYLOAD) {
185 dev_dbg(&client->dev, "%s: corrupted packet\n",
186 __func__);
187 phy->next_read_size = 5;
188 goto flush;
189 }
Robert Dolcaa06347c2015-10-22 12:11:42 +0300190 } else {
191 phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
192
193 *skb = alloc_skb(len, GFP_KERNEL);
194 if (*skb == NULL) {
195 r = -ENOMEM;
196 goto flush;
197 }
198
199 memcpy(skb_put(*skb, len), tmp, len);
200 fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
201
202 fdp_nci_i2c_remove_len_lrc(*skb);
203 }
204 }
205
206 return 0;
207
208flush:
209 /* Flush the remaining data */
210 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
211 r = -EREMOTEIO;
212
213 return r;
214}
215
216static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
217{
218 struct fdp_i2c_phy *phy = phy_id;
219 struct i2c_client *client;
220 struct sk_buff *skb;
221 int r;
222
Robert Dolcaa06347c2015-10-22 12:11:42 +0300223 if (!phy || irq != phy->i2c_dev->irq) {
224 WARN_ON_ONCE(1);
225 return IRQ_NONE;
226 }
227
Sudip Mukherjee57154f02016-12-20 21:09:04 +0000228 client = phy->i2c_dev;
229 dev_dbg(&client->dev, "%s\n", __func__);
230
Robert Dolcaa06347c2015-10-22 12:11:42 +0300231 r = fdp_nci_i2c_read(phy, &skb);
232
233 if (r == -EREMOTEIO)
234 return IRQ_HANDLED;
235 else if (r == -ENOMEM || r == -EBADMSG)
236 return IRQ_HANDLED;
237
238 if (skb != NULL)
239 fdp_nci_recv_frame(phy->ndev, skb);
240
241 return IRQ_HANDLED;
242}
243
244static void fdp_nci_i2c_read_device_properties(struct device *dev,
245 u8 *clock_type, u32 *clock_freq,
246 u8 **fw_vsc_cfg)
247{
248 int r;
249 u8 len;
250
251 r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
252 if (r) {
253 dev_dbg(dev, "Using default clock type");
254 *clock_type = 0;
255 }
256
257 r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
258 if (r) {
259 dev_dbg(dev, "Using default clock frequency\n");
260 *clock_freq = 26000;
261 }
262
263 if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
264 r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
265 &len);
266
267 if (r || len <= 0)
268 goto vsc_read_err;
269
270 /* Add 1 to the length to inclue the length byte itself */
271 len++;
272
273 *fw_vsc_cfg = devm_kmalloc(dev,
274 len * sizeof(**fw_vsc_cfg),
275 GFP_KERNEL);
276
277 r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
278 *fw_vsc_cfg, len);
279
280 if (r) {
281 devm_kfree(dev, fw_vsc_cfg);
282 goto vsc_read_err;
283 }
284 } else {
285vsc_read_err:
286 dev_dbg(dev, "FW vendor specific commands not present\n");
287 *fw_vsc_cfg = NULL;
288 }
289
290 dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
291 *clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
292}
293
294static int fdp_nci_i2c_probe(struct i2c_client *client,
295 const struct i2c_device_id *id)
296{
297 struct fdp_i2c_phy *phy;
298 struct device *dev = &client->dev;
299 u8 *fw_vsc_cfg;
300 u8 clock_type;
301 u32 clock_freq;
302 int r = 0;
303
304 dev_dbg(dev, "%s\n", __func__);
305
306 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
307 nfc_err(dev, "No I2C_FUNC_I2C support\n");
308 return -ENODEV;
309 }
310
Christophe Ricard0b0a2642015-12-23 23:45:22 +0100311 /* Checking if we have an irq */
312 if (client->irq <= 0) {
313 nfc_err(dev, "IRQ not present\n");
314 return -ENODEV;
315 }
316
Robert Dolcaa06347c2015-10-22 12:11:42 +0300317 phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy),
318 GFP_KERNEL);
319 if (!phy)
320 return -ENOMEM;
321
322 phy->i2c_dev = client;
323 phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
324 i2c_set_clientdata(client, phy);
325
Robert Dolcaa06347c2015-10-22 12:11:42 +0300326 r = request_threaded_irq(client->irq, NULL, fdp_nci_i2c_irq_thread_fn,
327 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
328 FDP_I2C_DRIVER_NAME, phy);
329
330 if (r < 0) {
331 nfc_err(&client->dev, "Unable to register IRQ handler\n");
332 return r;
333 }
334
335 /* Requesting the power gpio */
336 phy->power_gpio = devm_gpiod_get(dev, FDP_DP_POWER_GPIO_NAME,
337 GPIOD_OUT_LOW);
338
339 if (IS_ERR(phy->power_gpio)) {
340 nfc_err(dev, "Power GPIO request failed\n");
341 return PTR_ERR(phy->power_gpio);
342 }
343
344 /* read device properties to get the clock and production settings */
345 fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
346 &fw_vsc_cfg);
347
348 /* Call the NFC specific probe function */
349 r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
350 FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
351 clock_type, clock_freq, fw_vsc_cfg);
352 if (r < 0) {
353 nfc_err(dev, "NCI probing error\n");
354 return r;
355 }
356
357 dev_dbg(dev, "I2C driver loaded\n");
358 return 0;
359}
360
361static int fdp_nci_i2c_remove(struct i2c_client *client)
362{
363 struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
364
365 dev_dbg(&client->dev, "%s\n", __func__);
366
367 fdp_nci_remove(phy->ndev);
368 fdp_nci_i2c_disable(phy);
369
370 return 0;
371}
372
373static struct i2c_device_id fdp_nci_i2c_id_table[] = {
374 {"int339a", 0},
375 {}
376};
377MODULE_DEVICE_TABLE(i2c, fdp_nci_i2c_id_table);
378
379static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
380 {"INT339A", 0},
381 {}
382};
383MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
384
385static struct i2c_driver fdp_nci_i2c_driver = {
386 .driver = {
387 .name = FDP_I2C_DRIVER_NAME,
388 .acpi_match_table = ACPI_PTR(fdp_nci_i2c_acpi_match),
389 },
390 .id_table = fdp_nci_i2c_id_table,
391 .probe = fdp_nci_i2c_probe,
392 .remove = fdp_nci_i2c_remove,
393};
394module_i2c_driver(fdp_nci_i2c_driver);
395
396MODULE_LICENSE("GPL");
397MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
398MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");