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