xref: /linux/drivers/nfc/s3fwrn5/i2c.c (revision e7c22eeaff8565d9a8374f320238c251ca31480b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * I2C Link Layer for Samsung S3FWRN5 NCI based Driver
4  *
5  * Copyright (C) 2015 Samsung Electrnoics
6  * Robert Baldyga <r.baldyga@samsung.com>
7  */
8 
9 #include <linux/i2c.h>
10 #include <linux/gpio.h>
11 #include <linux/delay.h>
12 #include <linux/of_gpio.h>
13 #include <linux/of_irq.h>
14 #include <linux/module.h>
15 
16 #include <net/nfc/nfc.h>
17 
18 #include "phy_common.h"
19 
20 #define S3FWRN5_I2C_DRIVER_NAME "s3fwrn5_i2c"
21 
22 struct s3fwrn5_i2c_phy {
23 	struct phy_common common;
24 	struct i2c_client *i2c_dev;
25 
26 	unsigned int irq_skip:1;
27 };
28 
29 static void s3fwrn5_i2c_set_mode(void *phy_id, enum s3fwrn5_mode mode)
30 {
31 	struct s3fwrn5_i2c_phy *phy = phy_id;
32 
33 	mutex_lock(&phy->common.mutex);
34 
35 	if (s3fwrn5_phy_power_ctrl(&phy->common, mode) == false)
36 		goto out;
37 
38 	phy->irq_skip = true;
39 
40 out:
41 	mutex_unlock(&phy->common.mutex);
42 }
43 
44 static int s3fwrn5_i2c_write(void *phy_id, struct sk_buff *skb)
45 {
46 	struct s3fwrn5_i2c_phy *phy = phy_id;
47 	int ret;
48 
49 	mutex_lock(&phy->common.mutex);
50 
51 	phy->irq_skip = false;
52 
53 	ret = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
54 	if (ret == -EREMOTEIO) {
55 		/* Retry, chip was in standby */
56 		usleep_range(110000, 120000);
57 		ret  = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
58 	}
59 
60 	mutex_unlock(&phy->common.mutex);
61 
62 	if (ret < 0)
63 		return ret;
64 
65 	if (ret != skb->len)
66 		return -EREMOTEIO;
67 
68 	return 0;
69 }
70 
71 static const struct s3fwrn5_phy_ops i2c_phy_ops = {
72 	.set_wake = s3fwrn5_phy_set_wake,
73 	.set_mode = s3fwrn5_i2c_set_mode,
74 	.get_mode = s3fwrn5_phy_get_mode,
75 	.write = s3fwrn5_i2c_write,
76 };
77 
78 static int s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy *phy)
79 {
80 	struct sk_buff *skb;
81 	size_t hdr_size;
82 	size_t data_len;
83 	char hdr[4];
84 	int ret;
85 
86 	hdr_size = (phy->common.mode == S3FWRN5_MODE_NCI) ?
87 		NCI_CTRL_HDR_SIZE : S3FWRN5_FW_HDR_SIZE;
88 	ret = i2c_master_recv(phy->i2c_dev, hdr, hdr_size);
89 	if (ret < 0)
90 		return ret;
91 
92 	if (ret < hdr_size)
93 		return -EBADMSG;
94 
95 	data_len = (phy->common.mode == S3FWRN5_MODE_NCI) ?
96 		((struct nci_ctrl_hdr *)hdr)->plen :
97 		((struct s3fwrn5_fw_header *)hdr)->len;
98 
99 	skb = alloc_skb(hdr_size + data_len, GFP_KERNEL);
100 	if (!skb)
101 		return -ENOMEM;
102 
103 	skb_put_data(skb, hdr, hdr_size);
104 
105 	if (data_len == 0)
106 		goto out;
107 
108 	ret = i2c_master_recv(phy->i2c_dev, skb_put(skb, data_len), data_len);
109 	if (ret != data_len) {
110 		kfree_skb(skb);
111 		return -EBADMSG;
112 	}
113 
114 out:
115 	return s3fwrn5_recv_frame(phy->common.ndev, skb, phy->common.mode);
116 }
117 
118 static irqreturn_t s3fwrn5_i2c_irq_thread_fn(int irq, void *phy_id)
119 {
120 	struct s3fwrn5_i2c_phy *phy = phy_id;
121 
122 	if (!phy || !phy->common.ndev) {
123 		WARN_ON_ONCE(1);
124 		return IRQ_NONE;
125 	}
126 
127 	mutex_lock(&phy->common.mutex);
128 
129 	if (phy->irq_skip)
130 		goto out;
131 
132 	switch (phy->common.mode) {
133 	case S3FWRN5_MODE_NCI:
134 	case S3FWRN5_MODE_FW:
135 		s3fwrn5_i2c_read(phy);
136 		break;
137 	case S3FWRN5_MODE_COLD:
138 		break;
139 	}
140 
141 out:
142 	mutex_unlock(&phy->common.mutex);
143 
144 	return IRQ_HANDLED;
145 }
146 
147 static int s3fwrn5_i2c_parse_dt(struct i2c_client *client)
148 {
149 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
150 	struct device_node *np = client->dev.of_node;
151 
152 	if (!np)
153 		return -ENODEV;
154 
155 	phy->common.gpio_en = of_get_named_gpio(np, "en-gpios", 0);
156 	if (!gpio_is_valid(phy->common.gpio_en)) {
157 		/* Support also deprecated property */
158 		phy->common.gpio_en = of_get_named_gpio(np,
159 							"s3fwrn5,en-gpios",
160 							0);
161 		if (!gpio_is_valid(phy->common.gpio_en))
162 			return -ENODEV;
163 	}
164 
165 	phy->common.gpio_fw_wake = of_get_named_gpio(np, "wake-gpios", 0);
166 	if (!gpio_is_valid(phy->common.gpio_fw_wake)) {
167 		/* Support also deprecated property */
168 		phy->common.gpio_fw_wake = of_get_named_gpio(np,
169 							     "s3fwrn5,fw-gpios",
170 							     0);
171 		if (!gpio_is_valid(phy->common.gpio_fw_wake))
172 			return -ENODEV;
173 	}
174 
175 	return 0;
176 }
177 
178 static int s3fwrn5_i2c_probe(struct i2c_client *client,
179 				  const struct i2c_device_id *id)
180 {
181 	struct s3fwrn5_i2c_phy *phy;
182 	int ret;
183 
184 	phy = devm_kzalloc(&client->dev, sizeof(*phy), GFP_KERNEL);
185 	if (!phy)
186 		return -ENOMEM;
187 
188 	mutex_init(&phy->common.mutex);
189 	phy->common.mode = S3FWRN5_MODE_COLD;
190 	phy->irq_skip = true;
191 
192 	phy->i2c_dev = client;
193 	i2c_set_clientdata(client, phy);
194 
195 	ret = s3fwrn5_i2c_parse_dt(client);
196 	if (ret < 0)
197 		return ret;
198 
199 	ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->common.gpio_en,
200 				    GPIOF_OUT_INIT_HIGH, "s3fwrn5_en");
201 	if (ret < 0)
202 		return ret;
203 
204 	ret = devm_gpio_request_one(&phy->i2c_dev->dev,
205 				    phy->common.gpio_fw_wake,
206 				    GPIOF_OUT_INIT_LOW, "s3fwrn5_fw_wake");
207 	if (ret < 0)
208 		return ret;
209 
210 	ret = s3fwrn5_probe(&phy->common.ndev, phy, &phy->i2c_dev->dev,
211 			    &i2c_phy_ops);
212 	if (ret < 0)
213 		return ret;
214 
215 	ret = devm_request_threaded_irq(&client->dev, phy->i2c_dev->irq, NULL,
216 		s3fwrn5_i2c_irq_thread_fn, IRQF_ONESHOT,
217 		S3FWRN5_I2C_DRIVER_NAME, phy);
218 	if (ret)
219 		s3fwrn5_remove(phy->common.ndev);
220 
221 	return ret;
222 }
223 
224 static int s3fwrn5_i2c_remove(struct i2c_client *client)
225 {
226 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
227 
228 	s3fwrn5_remove(phy->common.ndev);
229 
230 	return 0;
231 }
232 
233 static const struct i2c_device_id s3fwrn5_i2c_id_table[] = {
234 	{S3FWRN5_I2C_DRIVER_NAME, 0},
235 	{}
236 };
237 MODULE_DEVICE_TABLE(i2c, s3fwrn5_i2c_id_table);
238 
239 static const struct of_device_id of_s3fwrn5_i2c_match[] = {
240 	{ .compatible = "samsung,s3fwrn5-i2c", },
241 	{}
242 };
243 MODULE_DEVICE_TABLE(of, of_s3fwrn5_i2c_match);
244 
245 static struct i2c_driver s3fwrn5_i2c_driver = {
246 	.driver = {
247 		.name = S3FWRN5_I2C_DRIVER_NAME,
248 		.of_match_table = of_match_ptr(of_s3fwrn5_i2c_match),
249 	},
250 	.probe = s3fwrn5_i2c_probe,
251 	.remove = s3fwrn5_i2c_remove,
252 	.id_table = s3fwrn5_i2c_id_table,
253 };
254 
255 module_i2c_driver(s3fwrn5_i2c_driver);
256 
257 MODULE_LICENSE("GPL");
258 MODULE_DESCRIPTION("I2C driver for Samsung S3FWRN5");
259 MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");
260