xref: /openbmc/linux/drivers/nfc/fdp/i2c.c (revision e40573a4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3  * Copyright (C) 2014-2016, Intel Corporation
4  *
5  * -------------------------------------------------------------------------
6  */
7 
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>
17 
18 #include "fdp.h"
19 
20 #define FDP_I2C_DRIVER_NAME	"fdp_nci_i2c"
21 
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"
25 
26 #define FDP_FRAME_HEADROOM	2
27 #define FDP_FRAME_TAILROOM	1
28 
29 #define FDP_NCI_I2C_MIN_PAYLOAD	5
30 #define FDP_NCI_I2C_MAX_PAYLOAD	261
31 
32 #define FDP_POWER_OFF		0
33 #define FDP_POWER_ON		1
34 
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)
38 
39 static void fdp_nci_i2c_reset(const struct fdp_i2c_phy *phy)
40 {
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);
46 }
47 
48 static int fdp_nci_i2c_enable(void *phy_id)
49 {
50 	const struct fdp_i2c_phy *phy = phy_id;
51 
52 	fdp_nci_i2c_reset(phy);
53 
54 	return 0;
55 }
56 
57 static void fdp_nci_i2c_disable(void *phy_id)
58 {
59 	const struct fdp_i2c_phy *phy = phy_id;
60 
61 	fdp_nci_i2c_reset(phy);
62 }
63 
64 static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
65 {
66 	u8 lrc = 0;
67 	u16 len, i;
68 
69 	/* Add length header */
70 	len = skb->len;
71 	*(u8 *)skb_push(skb, 1) = len & 0xff;
72 	*(u8 *)skb_push(skb, 1) = len >> 8;
73 
74 	/* Compute and add lrc */
75 	for (i = 0; i < len + 2; i++)
76 		lrc ^= skb->data[i];
77 
78 	skb_put_u8(skb, lrc);
79 }
80 
81 static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
82 {
83 	skb_pull(skb, FDP_FRAME_HEADROOM);
84 	skb_trim(skb, skb->len - FDP_FRAME_TAILROOM);
85 }
86 
87 static int fdp_nci_i2c_write(void *phy_id, struct sk_buff *skb)
88 {
89 	struct fdp_i2c_phy *phy = phy_id;
90 	struct i2c_client *client = phy->i2c_dev;
91 	int r;
92 
93 	if (phy->hard_fault != 0)
94 		return phy->hard_fault;
95 
96 	fdp_nci_i2c_add_len_lrc(skb);
97 	fdp_nci_i2c_dump_skb(&client->dev, "fdp_wr", skb);
98 
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);
103 	}
104 
105 	if (r < 0 || r != skb->len)
106 		dev_dbg(&client->dev, "%s: error err=%d len=%d\n",
107 			__func__, r, skb->len);
108 
109 	if (r >= 0) {
110 		if (r != skb->len) {
111 			phy->hard_fault = r;
112 			r = -EREMOTEIO;
113 		} else {
114 			r = 0;
115 		}
116 	}
117 
118 	fdp_nci_i2c_remove_len_lrc(skb);
119 
120 	return r;
121 }
122 
123 static const 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,
127 };
128 
129 static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb)
130 {
131 	int r, len;
132 	u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD], lrc, k;
133 	u16 i;
134 	struct i2c_client *client = phy->i2c_dev;
135 
136 	*skb = NULL;
137 
138 	/* Read the length packet and the data packet */
139 	for (k = 0; k < 2; k++) {
140 
141 		len = phy->next_read_size;
142 
143 		r = i2c_master_recv(client, tmp, len);
144 		if (r != len) {
145 			dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
146 				__func__, r);
147 			goto flush;
148 		}
149 
150 		/* Check packet integruty */
151 		for (lrc = i = 0; i < r; i++)
152 			lrc ^= tmp[i];
153 
154 		/*
155 		 * LRC check failed. This may due to transmission error or
156 		 * desynchronization between driver and FDP. Drop the packet
157 		 * and force resynchronization
158 		 */
159 		if (lrc) {
160 			dev_dbg(&client->dev, "%s: corrupted packet\n",
161 				__func__);
162 			phy->next_read_size = 5;
163 			goto flush;
164 		}
165 
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;
169 		} else {
170 			phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
171 
172 			*skb = alloc_skb(len, GFP_KERNEL);
173 			if (*skb == NULL) {
174 				r = -ENOMEM;
175 				goto flush;
176 			}
177 
178 			skb_put_data(*skb, tmp, len);
179 			fdp_nci_i2c_dump_skb(&client->dev, "fdp_rd", *skb);
180 
181 			fdp_nci_i2c_remove_len_lrc(*skb);
182 		}
183 	}
184 
185 	return 0;
186 
187 flush:
188 	/* Flush the remaining data */
189 	if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
190 		r = -EREMOTEIO;
191 
192 	return r;
193 }
194 
195 static irqreturn_t fdp_nci_i2c_irq_thread_fn(int irq, void *phy_id)
196 {
197 	struct fdp_i2c_phy *phy = phy_id;
198 	struct sk_buff *skb;
199 	int r;
200 
201 	if (!phy || irq != phy->i2c_dev->irq) {
202 		WARN_ON_ONCE(1);
203 		return IRQ_NONE;
204 	}
205 
206 	r = fdp_nci_i2c_read(phy, &skb);
207 
208 	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
209 		return IRQ_HANDLED;
210 
211 	if (skb != NULL)
212 		nci_recv_frame(phy->ndev, skb);
213 
214 	return IRQ_HANDLED;
215 }
216 
217 static void fdp_nci_i2c_read_device_properties(struct device *dev,
218 					       u8 *clock_type, u32 *clock_freq,
219 					       u8 **fw_vsc_cfg)
220 {
221 	int r;
222 	u8 len;
223 
224 	r = device_property_read_u8(dev, FDP_DP_CLOCK_TYPE_NAME, clock_type);
225 	if (r) {
226 		dev_dbg(dev, "Using default clock type");
227 		*clock_type = 0;
228 	}
229 
230 	r = device_property_read_u32(dev, FDP_DP_CLOCK_FREQ_NAME, clock_freq);
231 	if (r) {
232 		dev_dbg(dev, "Using default clock frequency\n");
233 		*clock_freq = 26000;
234 	}
235 
236 	if (device_property_present(dev, FDP_DP_FW_VSC_CFG_NAME)) {
237 		r = device_property_read_u8(dev, FDP_DP_FW_VSC_CFG_NAME,
238 					    &len);
239 
240 		if (r || len <= 0)
241 			goto vsc_read_err;
242 
243 		/* Add 1 to the length to inclue the length byte itself */
244 		len++;
245 
246 		*fw_vsc_cfg = devm_kmalloc_array(dev,
247 					   len, sizeof(**fw_vsc_cfg),
248 					   GFP_KERNEL);
249 
250 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
251 						  *fw_vsc_cfg, len);
252 
253 		if (r) {
254 			devm_kfree(dev, *fw_vsc_cfg);
255 			goto vsc_read_err;
256 		}
257 	} else {
258 vsc_read_err:
259 		dev_dbg(dev, "FW vendor specific commands not present\n");
260 		*fw_vsc_cfg = NULL;
261 	}
262 
263 	dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
264 		*clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
265 }
266 
267 static const struct acpi_gpio_params power_gpios = { 0, 0, false };
268 
269 static const struct acpi_gpio_mapping acpi_fdp_gpios[] = {
270 	{ "power-gpios", &power_gpios, 1 },
271 	{},
272 };
273 
274 static int fdp_nci_i2c_probe(struct i2c_client *client)
275 {
276 	struct fdp_i2c_phy *phy;
277 	struct device *dev = &client->dev;
278 	u8 *fw_vsc_cfg;
279 	u8 clock_type;
280 	u32 clock_freq;
281 	int r = 0;
282 
283 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
284 		nfc_err(dev, "No I2C_FUNC_I2C support\n");
285 		return -ENODEV;
286 	}
287 
288 	/* Checking if we have an irq */
289 	if (client->irq <= 0) {
290 		nfc_err(dev, "IRQ not present\n");
291 		return -ENODEV;
292 	}
293 
294 	phy = devm_kzalloc(dev, sizeof(struct fdp_i2c_phy), GFP_KERNEL);
295 	if (!phy)
296 		return -ENOMEM;
297 
298 	phy->i2c_dev = client;
299 	phy->next_read_size = FDP_NCI_I2C_MIN_PAYLOAD;
300 	i2c_set_clientdata(client, phy);
301 
302 	r = devm_request_threaded_irq(dev, client->irq,
303 				      NULL, fdp_nci_i2c_irq_thread_fn,
304 				      IRQF_TRIGGER_RISING | IRQF_ONESHOT,
305 				      FDP_I2C_DRIVER_NAME, phy);
306 
307 	if (r < 0) {
308 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
309 		return r;
310 	}
311 
312 	r = devm_acpi_dev_add_driver_gpios(dev, acpi_fdp_gpios);
313 	if (r)
314 		dev_dbg(dev, "Unable to add GPIO mapping table\n");
315 
316 	/* Requesting the power gpio */
317 	phy->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
318 	if (IS_ERR(phy->power_gpio)) {
319 		nfc_err(dev, "Power GPIO request failed\n");
320 		return PTR_ERR(phy->power_gpio);
321 	}
322 
323 	/* read device properties to get the clock and production settings */
324 	fdp_nci_i2c_read_device_properties(dev, &clock_type, &clock_freq,
325 					   &fw_vsc_cfg);
326 
327 	/* Call the NFC specific probe function */
328 	r = fdp_nci_probe(phy, &i2c_phy_ops, &phy->ndev,
329 			  FDP_FRAME_HEADROOM, FDP_FRAME_TAILROOM,
330 			  clock_type, clock_freq, fw_vsc_cfg);
331 	if (r < 0) {
332 		nfc_err(dev, "NCI probing error\n");
333 		return r;
334 	}
335 
336 	return 0;
337 }
338 
339 static int fdp_nci_i2c_remove(struct i2c_client *client)
340 {
341 	struct fdp_i2c_phy *phy = i2c_get_clientdata(client);
342 
343 	fdp_nci_remove(phy->ndev);
344 	fdp_nci_i2c_disable(phy);
345 
346 	return 0;
347 }
348 
349 static const struct acpi_device_id fdp_nci_i2c_acpi_match[] = {
350 	{"INT339A", 0},
351 	{}
352 };
353 MODULE_DEVICE_TABLE(acpi, fdp_nci_i2c_acpi_match);
354 
355 static struct i2c_driver fdp_nci_i2c_driver = {
356 	.driver = {
357 		   .name = FDP_I2C_DRIVER_NAME,
358 		   .acpi_match_table = fdp_nci_i2c_acpi_match,
359 		  },
360 	.probe_new = fdp_nci_i2c_probe,
361 	.remove = fdp_nci_i2c_remove,
362 };
363 module_i2c_driver(fdp_nci_i2c_driver);
364 
365 MODULE_LICENSE("GPL");
366 MODULE_DESCRIPTION("I2C driver for Intel Fields Peak NFC controller");
367 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
368