xref: /openbmc/linux/drivers/nfc/s3fwrn5/i2c.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
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 
9340f42f7SStephan 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;
26340f42f7SStephan Gerhold 	struct clk *clk;
27c04c674fSRobert Baldyga 
28c04c674fSRobert Baldyga 	unsigned int irq_skip:1;
29c04c674fSRobert Baldyga };
30c04c674fSRobert Baldyga 
s3fwrn5_i2c_set_mode(void * phy_id,enum s3fwrn5_mode mode)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 
s3fwrn5_i2c_write(void * phy_id,struct sk_buff * skb)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 
s3fwrn5_i2c_read(struct s3fwrn5_i2c_phy * phy)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 
s3fwrn5_i2c_irq_thread_fn(int irq,void * phy_id)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 
s3fwrn5_i2c_parse_dt(struct i2c_client * client)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 
s3fwrn5_i2c_probe(struct i2c_client * client)1800fc00c08SUwe Kleine-König static int s3fwrn5_i2c_probe(struct i2c_client *client)
181c04c674fSRobert Baldyga {
182c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy;
183c04c674fSRobert Baldyga 	int ret;
184c04c674fSRobert Baldyga 
185c04c674fSRobert Baldyga 	phy = devm_kzalloc(&client->dev, sizeof(*phy), GFP_KERNEL);
186c04c674fSRobert Baldyga 	if (!phy)
187c04c674fSRobert Baldyga 		return -ENOMEM;
188c04c674fSRobert Baldyga 
189b3799d59SBongsu Jeon 	mutex_init(&phy->common.mutex);
190b3799d59SBongsu Jeon 	phy->common.mode = S3FWRN5_MODE_COLD;
191c04c674fSRobert Baldyga 	phy->irq_skip = true;
192c04c674fSRobert Baldyga 
193c04c674fSRobert Baldyga 	phy->i2c_dev = client;
194c04c674fSRobert Baldyga 	i2c_set_clientdata(client, phy);
195c04c674fSRobert Baldyga 
196c04c674fSRobert Baldyga 	ret = s3fwrn5_i2c_parse_dt(client);
197c04c674fSRobert Baldyga 	if (ret < 0)
198c04c674fSRobert Baldyga 		return ret;
199c04c674fSRobert Baldyga 
200b3799d59SBongsu Jeon 	ret = devm_gpio_request_one(&phy->i2c_dev->dev, phy->common.gpio_en,
201c04c674fSRobert Baldyga 				    GPIOF_OUT_INIT_HIGH, "s3fwrn5_en");
202c04c674fSRobert Baldyga 	if (ret < 0)
203c04c674fSRobert Baldyga 		return ret;
204c04c674fSRobert Baldyga 
205b3799d59SBongsu Jeon 	ret = devm_gpio_request_one(&phy->i2c_dev->dev,
206b3799d59SBongsu Jeon 				    phy->common.gpio_fw_wake,
207c04c674fSRobert Baldyga 				    GPIOF_OUT_INIT_LOW, "s3fwrn5_fw_wake");
208c04c674fSRobert Baldyga 	if (ret < 0)
209c04c674fSRobert Baldyga 		return ret;
210c04c674fSRobert Baldyga 
211340f42f7SStephan Gerhold 	/*
212340f42f7SStephan Gerhold 	 * S3FWRN5 depends on a clock input ("XI" pin) to function properly.
213340f42f7SStephan Gerhold 	 * Depending on the hardware configuration this could be an always-on
214340f42f7SStephan Gerhold 	 * oscillator or some external clock that must be explicitly enabled.
215340f42f7SStephan Gerhold 	 * Make sure the clock is running before starting S3FWRN5.
216340f42f7SStephan Gerhold 	 */
217f8f797f3SDmitry Torokhov 	phy->clk = devm_clk_get_optional_enabled(&client->dev, NULL);
218f8f797f3SDmitry Torokhov 	if (IS_ERR(phy->clk))
219f8f797f3SDmitry Torokhov 		return dev_err_probe(&client->dev, PTR_ERR(phy->clk),
220f8f797f3SDmitry Torokhov 				     "failed to get clock\n");
221340f42f7SStephan Gerhold 
222b3799d59SBongsu Jeon 	ret = s3fwrn5_probe(&phy->common.ndev, phy, &phy->i2c_dev->dev,
223b3799d59SBongsu Jeon 			    &i2c_phy_ops);
224c04c674fSRobert Baldyga 	if (ret < 0)
225f8f797f3SDmitry Torokhov 		return ret;
226c04c674fSRobert Baldyga 
227d7e7f02fSAxel Lin 	ret = devm_request_threaded_irq(&client->dev, phy->i2c_dev->irq, NULL,
228e0a64d1dSKrzysztof Kozlowski 		s3fwrn5_i2c_irq_thread_fn, IRQF_ONESHOT,
229c04c674fSRobert Baldyga 		S3FWRN5_I2C_DRIVER_NAME, phy);
230c04c674fSRobert Baldyga 	if (ret)
231340f42f7SStephan Gerhold 		goto s3fwrn5_remove;
232c04c674fSRobert Baldyga 
233340f42f7SStephan Gerhold 	return 0;
234340f42f7SStephan Gerhold 
235340f42f7SStephan Gerhold s3fwrn5_remove:
236340f42f7SStephan Gerhold 	s3fwrn5_remove(phy->common.ndev);
237c04c674fSRobert Baldyga 	return ret;
238c04c674fSRobert Baldyga }
239c04c674fSRobert Baldyga 
s3fwrn5_i2c_remove(struct i2c_client * client)240ed5c2f5fSUwe Kleine-König static void s3fwrn5_i2c_remove(struct i2c_client *client)
241c04c674fSRobert Baldyga {
242c04c674fSRobert Baldyga 	struct s3fwrn5_i2c_phy *phy = i2c_get_clientdata(client);
243c04c674fSRobert Baldyga 
244b3799d59SBongsu Jeon 	s3fwrn5_remove(phy->common.ndev);
245c04c674fSRobert Baldyga }
246c04c674fSRobert Baldyga 
24781251cc5SArvind Yadav static const struct i2c_device_id s3fwrn5_i2c_id_table[] = {
248c04c674fSRobert Baldyga 	{S3FWRN5_I2C_DRIVER_NAME, 0},
249c04c674fSRobert Baldyga 	{}
250c04c674fSRobert Baldyga };
251c04c674fSRobert Baldyga MODULE_DEVICE_TABLE(i2c, s3fwrn5_i2c_id_table);
252c04c674fSRobert Baldyga 
2535edc9426SKrzysztof Kozlowski static const struct of_device_id of_s3fwrn5_i2c_match[] __maybe_unused = {
254c04c674fSRobert Baldyga 	{ .compatible = "samsung,s3fwrn5-i2c", },
255c04c674fSRobert Baldyga 	{}
256c04c674fSRobert Baldyga };
257c04c674fSRobert Baldyga MODULE_DEVICE_TABLE(of, of_s3fwrn5_i2c_match);
258c04c674fSRobert Baldyga 
259c04c674fSRobert Baldyga static struct i2c_driver s3fwrn5_i2c_driver = {
260c04c674fSRobert Baldyga 	.driver = {
261c04c674fSRobert Baldyga 		.name = S3FWRN5_I2C_DRIVER_NAME,
262c04c674fSRobert Baldyga 		.of_match_table = of_match_ptr(of_s3fwrn5_i2c_match),
263c04c674fSRobert Baldyga 	},
264*efc3001fSUwe Kleine-König 	.probe = s3fwrn5_i2c_probe,
265c04c674fSRobert Baldyga 	.remove = s3fwrn5_i2c_remove,
266c04c674fSRobert Baldyga 	.id_table = s3fwrn5_i2c_id_table,
267c04c674fSRobert Baldyga };
268c04c674fSRobert Baldyga 
269c04c674fSRobert Baldyga module_i2c_driver(s3fwrn5_i2c_driver);
270c04c674fSRobert Baldyga 
271c04c674fSRobert Baldyga MODULE_LICENSE("GPL");
272c04c674fSRobert Baldyga MODULE_DESCRIPTION("I2C driver for Samsung S3FWRN5");
273c04c674fSRobert Baldyga MODULE_AUTHOR("Robert Baldyga <r.baldyga@samsung.com>");
274