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