xref: /openbmc/linux/drivers/nfc/s3fwrn5/i2c.c (revision 340f42f7ff0b87a92e69b50706a6c872da756c89)
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 
9*340f42f7SStephan Gerhold #include <linux/clk.h>
10c04c674fSRobert Baldyga #include <linux/i2c.h>
11c04c674fSRobert Baldyga #include <linux/gpio.h>
12c04c674fSRobert Baldyga #include <linux/delay.h>
13c04c674fSRobert Baldyga #include <linux/of_gpio.h>
14c04c674fSRobert Baldyga #include <linux/of_irq.h>
15c04c674fSRobert Baldyga #include <linux/module.h>
16c04c674fSRobert Baldyga 
17c04c674fSRobert Baldyga #include <net/nfc/nfc.h>
18c04c674fSRobert Baldyga 
19b3799d59SBongsu Jeon #include "phy_common.h"
20c04c674fSRobert Baldyga 
21c04c674fSRobert Baldyga #define S3FWRN5_I2C_DRIVER_NAME "s3fwrn5_i2c"
22c04c674fSRobert Baldyga 
23c04c674fSRobert Baldyga struct s3fwrn5_i2c_phy {
24b3799d59SBongsu Jeon 	struct phy_common common;
25c04c674fSRobert Baldyga 	struct i2c_client *i2c_dev;
26*340f42f7SStephan Gerhold 	struct clk *clk;
27c04c674fSRobert Baldyga 
28c04c674fSRobert Baldyga 	unsigned int irq_skip:1;
29c04c674fSRobert Baldyga };
30c04c674fSRobert Baldyga 
31c04c674fSRobert Baldyga static void s3fwrn5_i2c_set_mode(void *phy_id, enum s3fwrn5_mode mode)
32c04c674fSRobert Baldyga {
33c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = phy_id;
34c04c674fSRobert Baldyga 
35b3799d59SBongsu Jeon 	mutex_lock(&phy->common.mutex);
36c04c674fSRobert Baldyga 
37b3799d59SBongsu Jeon 	if (s3fwrn5_phy_power_ctrl(&phy->common, mode) == false)
38c04c674fSRobert Baldyga 		goto out;
39c04c674fSRobert Baldyga 
40c04c674fSRobert Baldyga 	phy->irq_skip = true;
41c04c674fSRobert Baldyga 
42c04c674fSRobert Baldyga out:
43b3799d59SBongsu Jeon 	mutex_unlock(&phy->common.mutex);
44c04c674fSRobert Baldyga }
45c04c674fSRobert Baldyga 
46c04c674fSRobert Baldyga static int s3fwrn5_i2c_write(void *phy_id, struct sk_buff *skb)
47c04c674fSRobert Baldyga {
48c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = phy_id;
49c04c674fSRobert Baldyga 	int ret;
50c04c674fSRobert Baldyga 
51b3799d59SBongsu Jeon 	mutex_lock(&phy->common.mutex);
52c04c674fSRobert Baldyga 
53c04c674fSRobert Baldyga 	phy->irq_skip = false;
54c04c674fSRobert Baldyga 
55c04c674fSRobert Baldyga 	ret = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
56c04c674fSRobert Baldyga 	if (ret == -EREMOTEIO) {
57c04c674fSRobert Baldyga 		/* Retry, chip was in standby */
58c04c674fSRobert Baldyga 		usleep_range(110000, 120000);
59c04c674fSRobert Baldyga 		ret  = i2c_master_send(phy->i2c_dev, skb->data, skb->len);
60c04c674fSRobert Baldyga 	}
61c04c674fSRobert Baldyga 
62b3799d59SBongsu Jeon 	mutex_unlock(&phy->common.mutex);
63c04c674fSRobert Baldyga 
64c04c674fSRobert Baldyga 	if (ret < 0)
65c04c674fSRobert Baldyga 		return ret;
66c04c674fSRobert Baldyga 
67c04c674fSRobert Baldyga 	if (ret != skb->len)
68c04c674fSRobert Baldyga 		return -EREMOTEIO;
69c04c674fSRobert Baldyga 
70c04c674fSRobert Baldyga 	return 0;
71c04c674fSRobert Baldyga }
72c04c674fSRobert Baldyga 
73c6b5df0dSJulia Lawall static const struct s3fwrn5_phy_ops i2c_phy_ops = {
74b3799d59SBongsu Jeon 	.set_wake = s3fwrn5_phy_set_wake,
75c04c674fSRobert Baldyga 	.set_mode = s3fwrn5_i2c_set_mode,
76b3799d59SBongsu Jeon 	.get_mode = s3fwrn5_phy_get_mode,
77c04c674fSRobert Baldyga 	.write = s3fwrn5_i2c_write,
78c04c674fSRobert Baldyga };
79c04c674fSRobert Baldyga 
80c04c674fSRobert Baldyga static int s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy *phy)
81c04c674fSRobert Baldyga {
82c04c674fSRobert Baldyga 	struct sk_buff *skb;
83c04c674fSRobert Baldyga 	size_t hdr_size;
84c04c674fSRobert Baldyga 	size_t data_len;
85c04c674fSRobert Baldyga 	char hdr[4];
86c04c674fSRobert Baldyga 	int ret;
87c04c674fSRobert Baldyga 
88b3799d59SBongsu Jeon 	hdr_size = (phy->common.mode == S3FWRN5_MODE_NCI) ?
89c04c674fSRobert Baldyga 		NCI_CTRL_HDR_SIZE : S3FWRN5_FW_HDR_SIZE;
90c04c674fSRobert Baldyga 	ret = i2c_master_recv(phy->i2c_dev, hdr, hdr_size);
91c04c674fSRobert Baldyga 	if (ret < 0)
92c04c674fSRobert Baldyga 		return ret;
93c04c674fSRobert Baldyga 
94c04c674fSRobert Baldyga 	if (ret < hdr_size)
95c04c674fSRobert Baldyga 		return -EBADMSG;
96c04c674fSRobert Baldyga 
97b3799d59SBongsu Jeon 	data_len = (phy->common.mode == S3FWRN5_MODE_NCI) ?
98c04c674fSRobert Baldyga 		((struct nci_ctrl_hdr *)hdr)->plen :
99c04c674fSRobert Baldyga 		((struct s3fwrn5_fw_header *)hdr)->len;
100c04c674fSRobert Baldyga 
101c04c674fSRobert Baldyga 	skb = alloc_skb(hdr_size + data_len, GFP_KERNEL);
102c04c674fSRobert Baldyga 	if (!skb)
103c04c674fSRobert Baldyga 		return -ENOMEM;
104c04c674fSRobert Baldyga 
10559ae1d12SJohannes Berg 	skb_put_data(skb, hdr, hdr_size);
106c04c674fSRobert Baldyga 
107c04c674fSRobert Baldyga 	if (data_len == 0)
108c04c674fSRobert Baldyga 		goto out;
109c04c674fSRobert Baldyga 
110c04c674fSRobert Baldyga 	ret = i2c_master_recv(phy->i2c_dev, skb_put(skb, data_len), data_len);
111c04c674fSRobert Baldyga 	if (ret != data_len) {
112c04c674fSRobert Baldyga 		kfree_skb(skb);
113c04c674fSRobert Baldyga 		return -EBADMSG;
114c04c674fSRobert Baldyga 	}
115c04c674fSRobert Baldyga 
116c04c674fSRobert Baldyga out:
117b3799d59SBongsu Jeon 	return s3fwrn5_recv_frame(phy->common.ndev, skb, phy->common.mode);
118c04c674fSRobert Baldyga }
119c04c674fSRobert Baldyga 
120c04c674fSRobert Baldyga static irqreturn_t s3fwrn5_i2c_irq_thread_fn(int irq, void *phy_id)
121c04c674fSRobert Baldyga {
122c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = phy_id;
123c04c674fSRobert Baldyga 
124b3799d59SBongsu Jeon 	if (!phy || !phy->common.ndev) {
125c04c674fSRobert Baldyga 		WARN_ON_ONCE(1);
126c04c674fSRobert Baldyga 		return IRQ_NONE;
127c04c674fSRobert Baldyga 	}
128c04c674fSRobert Baldyga 
129b3799d59SBongsu Jeon 	mutex_lock(&phy->common.mutex);
130c04c674fSRobert Baldyga 
131c04c674fSRobert Baldyga 	if (phy->irq_skip)
132c04c674fSRobert Baldyga 		goto out;
133c04c674fSRobert Baldyga 
134b3799d59SBongsu Jeon 	switch (phy->common.mode) {
135c04c674fSRobert Baldyga 	case S3FWRN5_MODE_NCI:
136c04c674fSRobert Baldyga 	case S3FWRN5_MODE_FW:
137e52e4130SKrzysztof Kozlowski 		s3fwrn5_i2c_read(phy);
138c04c674fSRobert Baldyga 		break;
139c04c674fSRobert Baldyga 	case S3FWRN5_MODE_COLD:
140c04c674fSRobert Baldyga 		break;
141c04c674fSRobert Baldyga 	}
142c04c674fSRobert Baldyga 
143c04c674fSRobert Baldyga out:
144b3799d59SBongsu Jeon 	mutex_unlock(&phy->common.mutex);
145c04c674fSRobert Baldyga 
146c04c674fSRobert Baldyga 	return IRQ_HANDLED;
147c04c674fSRobert Baldyga }
148c04c674fSRobert Baldyga 
149c04c674fSRobert Baldyga static int s3fwrn5_i2c_parse_dt(struct i2c_client *client)
150c04c674fSRobert Baldyga {
151c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
152c04c674fSRobert Baldyga 	struct device_node *np = client->dev.of_node;
153c04c674fSRobert Baldyga 
154c04c674fSRobert Baldyga 	if (!np)
155c04c674fSRobert Baldyga 		return -ENODEV;
156c04c674fSRobert Baldyga 
157b3799d59SBongsu Jeon 	phy->common.gpio_en = of_get_named_gpio(np, "en-gpios", 0);
158b3799d59SBongsu Jeon 	if (!gpio_is_valid(phy->common.gpio_en)) {
1591995c4ccSKrzysztof Kozlowski 		/* Support also deprecated property */
160b3799d59SBongsu Jeon 		phy->common.gpio_en = of_get_named_gpio(np,
161b3799d59SBongsu Jeon 							"s3fwrn5,en-gpios",
162b3799d59SBongsu Jeon 							0);
163b3799d59SBongsu Jeon 		if (!gpio_is_valid(phy->common.gpio_en))
164c04c674fSRobert Baldyga 			return -ENODEV;
1651995c4ccSKrzysztof Kozlowski 	}
166c04c674fSRobert Baldyga 
167b3799d59SBongsu Jeon 	phy->common.gpio_fw_wake = of_get_named_gpio(np, "wake-gpios", 0);
168b3799d59SBongsu Jeon 	if (!gpio_is_valid(phy->common.gpio_fw_wake)) {
1691995c4ccSKrzysztof Kozlowski 		/* Support also deprecated property */
170b3799d59SBongsu Jeon 		phy->common.gpio_fw_wake = of_get_named_gpio(np,
171b3799d59SBongsu Jeon 							     "s3fwrn5,fw-gpios",
172b3799d59SBongsu Jeon 							     0);
173b3799d59SBongsu Jeon 		if (!gpio_is_valid(phy->common.gpio_fw_wake))
174c04c674fSRobert Baldyga 			return -ENODEV;
1751995c4ccSKrzysztof Kozlowski 	}
176c04c674fSRobert Baldyga 
177c04c674fSRobert Baldyga 	return 0;
178c04c674fSRobert Baldyga }
179c04c674fSRobert Baldyga 
180c04c674fSRobert Baldyga static int s3fwrn5_i2c_probe(struct i2c_client *client,
181c04c674fSRobert Baldyga 				  const struct i2c_device_id *id)
182c04c674fSRobert Baldyga {
183c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy;
184c04c674fSRobert Baldyga 	int ret;
185c04c674fSRobert Baldyga 
186c04c674fSRobert Baldyga 	phy = devm_kzalloc(&client->dev, sizeof(*phy), GFP_KERNEL);
187c04c674fSRobert Baldyga 	if (!phy)
188c04c674fSRobert Baldyga 		return -ENOMEM;
189c04c674fSRobert Baldyga 
190b3799d59SBongsu Jeon 	mutex_init(&phy->common.mutex);
191b3799d59SBongsu Jeon 	phy->common.mode = S3FWRN5_MODE_COLD;
192c04c674fSRobert Baldyga 	phy->irq_skip = true;
193c04c674fSRobert Baldyga 
194c04c674fSRobert Baldyga 	phy->i2c_dev = client;
195c04c674fSRobert Baldyga 	i2c_set_clientdata(client, phy);
196c04c674fSRobert Baldyga 
197c04c674fSRobert Baldyga 	ret = s3fwrn5_i2c_parse_dt(client);
198c04c674fSRobert Baldyga 	if (ret < 0)
199c04c674fSRobert Baldyga 		return ret;
200c04c674fSRobert Baldyga 
201b3799d59SBongsu Jeon 	ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->common.gpio_en,
202c04c674fSRobert Baldyga 				    GPIOF_OUT_INIT_HIGH, "s3fwrn5_en");
203c04c674fSRobert Baldyga 	if (ret < 0)
204c04c674fSRobert Baldyga 		return ret;
205c04c674fSRobert Baldyga 
206b3799d59SBongsu Jeon 	ret = devm_gpio_request_one(&phy->i2c_dev->dev,
207b3799d59SBongsu Jeon 				    phy->common.gpio_fw_wake,
208c04c674fSRobert Baldyga 				    GPIOF_OUT_INIT_LOW, "s3fwrn5_fw_wake");
209c04c674fSRobert Baldyga 	if (ret < 0)
210c04c674fSRobert Baldyga 		return ret;
211c04c674fSRobert Baldyga 
212*340f42f7SStephan Gerhold 	phy->clk = devm_clk_get_optional(&client->dev, NULL);
213*340f42f7SStephan Gerhold 	if (IS_ERR(phy->clk))
214*340f42f7SStephan Gerhold 		return dev_err_probe(&client->dev, PTR_ERR(phy->clk),
215*340f42f7SStephan Gerhold 				     "failed to get clock\n");
216*340f42f7SStephan Gerhold 
217*340f42f7SStephan Gerhold 	/*
218*340f42f7SStephan Gerhold 	 * S3FWRN5 depends on a clock input ("XI" pin) to function properly.
219*340f42f7SStephan Gerhold 	 * Depending on the hardware configuration this could be an always-on
220*340f42f7SStephan Gerhold 	 * oscillator or some external clock that must be explicitly enabled.
221*340f42f7SStephan Gerhold 	 * Make sure the clock is running before starting S3FWRN5.
222*340f42f7SStephan Gerhold 	 */
223*340f42f7SStephan Gerhold 	ret = clk_prepare_enable(phy->clk);
224*340f42f7SStephan Gerhold 	if (ret < 0) {
225*340f42f7SStephan Gerhold 		dev_err(&client->dev, "failed to enable clock: %d\n", ret);
226*340f42f7SStephan Gerhold 		return ret;
227*340f42f7SStephan Gerhold 	}
228*340f42f7SStephan Gerhold 
229b3799d59SBongsu Jeon 	ret = s3fwrn5_probe(&phy->common.ndev, phy, &phy->i2c_dev->dev,
230b3799d59SBongsu Jeon 			    &i2c_phy_ops);
231c04c674fSRobert Baldyga 	if (ret < 0)
232*340f42f7SStephan Gerhold 		goto disable_clk;
233c04c674fSRobert Baldyga 
234d7e7f02fSAxel Lin 	ret = devm_request_threaded_irq(&client->dev, phy->i2c_dev->irq, NULL,
235e0a64d1dSKrzysztof Kozlowski 		s3fwrn5_i2c_irq_thread_fn, IRQF_ONESHOT,
236c04c674fSRobert Baldyga 		S3FWRN5_I2C_DRIVER_NAME, phy);
237c04c674fSRobert Baldyga 	if (ret)
238*340f42f7SStephan Gerhold 		goto s3fwrn5_remove;
239c04c674fSRobert Baldyga 
240*340f42f7SStephan Gerhold 	return 0;
241*340f42f7SStephan Gerhold 
242*340f42f7SStephan Gerhold s3fwrn5_remove:
243*340f42f7SStephan Gerhold 	s3fwrn5_remove(phy->common.ndev);
244*340f42f7SStephan Gerhold disable_clk:
245*340f42f7SStephan Gerhold 	clk_disable_unprepare(phy->clk);
246c04c674fSRobert Baldyga 	return ret;
247c04c674fSRobert Baldyga }
248c04c674fSRobert Baldyga 
249c04c674fSRobert Baldyga static int s3fwrn5_i2c_remove(struct i2c_client *client)
250c04c674fSRobert Baldyga {
251c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
252c04c674fSRobert Baldyga 
253b3799d59SBongsu Jeon 	s3fwrn5_remove(phy->common.ndev);
254*340f42f7SStephan Gerhold 	clk_disable_unprepare(phy->clk);
255c04c674fSRobert Baldyga 
256c04c674fSRobert Baldyga 	return 0;
257c04c674fSRobert Baldyga }
258c04c674fSRobert Baldyga 
25981251cc5SArvind Yadav static const struct i2c_device_id s3fwrn5_i2c_id_table[] = {
260c04c674fSRobert Baldyga 	{S3FWRN5_I2C_DRIVER_NAME, 0},
261c04c674fSRobert Baldyga 	{}
262c04c674fSRobert Baldyga };
263c04c674fSRobert Baldyga MODULE_DEVICE_TABLE(i2c, s3fwrn5_i2c_id_table);
264c04c674fSRobert Baldyga 
265c04c674fSRobert Baldyga static const struct of_device_id of_s3fwrn5_i2c_match[] = {
266c04c674fSRobert Baldyga 	{ .compatible = "samsung,s3fwrn5-i2c", },
267c04c674fSRobert Baldyga 	{}
268c04c674fSRobert Baldyga };
269c04c674fSRobert Baldyga MODULE_DEVICE_TABLE(of, of_s3fwrn5_i2c_match);
270c04c674fSRobert Baldyga 
271c04c674fSRobert Baldyga static struct i2c_driver s3fwrn5_i2c_driver = {
272c04c674fSRobert Baldyga 	.driver = {
273c04c674fSRobert Baldyga 		.name = S3FWRN5_I2C_DRIVER_NAME,
274c04c674fSRobert Baldyga 		.of_match_table = of_match_ptr(of_s3fwrn5_i2c_match),
275c04c674fSRobert Baldyga 	},
276c04c674fSRobert Baldyga 	.probe = s3fwrn5_i2c_probe,
277c04c674fSRobert Baldyga 	.remove = s3fwrn5_i2c_remove,
278c04c674fSRobert Baldyga 	.id_table = s3fwrn5_i2c_id_table,
279c04c674fSRobert Baldyga };
280c04c674fSRobert Baldyga 
281c04c674fSRobert Baldyga module_i2c_driver(s3fwrn5_i2c_driver);
282c04c674fSRobert Baldyga 
283c04c674fSRobert Baldyga MODULE_LICENSE("GPL");
284c04c674fSRobert Baldyga MODULE_DESCRIPTION("I2C driver for Samsung S3FWRN5");
285c04c674fSRobert Baldyga MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");
286