blob: e0a512e4f0e6ad6c4c87422b15368ef20e9924fb [file] [log] [blame]
Christophe Ricard35630df2014-05-25 22:35:38 +02001/*
Christophe Ricarded06aeef2015-06-09 22:26:05 +02002 * I2C Link Layer for ST NCI NFC controller familly based Driver
3 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
Christophe Ricard35630df2014-05-25 22:35:38 +02004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Christophe Ricard35630df2014-05-25 22:35:38 +020020#include <linux/module.h>
21#include <linux/i2c.h>
22#include <linux/gpio.h>
23#include <linux/of_irq.h>
24#include <linux/of_gpio.h>
Christophe Ricard35630df2014-05-25 22:35:38 +020025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/nfc.h>
Christophe Ricard76b733d2015-08-14 22:33:30 +020028#include <linux/platform_data/st-nci.h>
Christophe Ricard35630df2014-05-25 22:35:38 +020029
Christophe Ricarde67e7e52015-10-25 22:54:17 +010030#include "st-nci.h"
Christophe Ricard35630df2014-05-25 22:35:38 +020031
Christophe Ricard30458aa2015-08-14 22:33:31 +020032#define DRIVER_DESC "NCI NFC driver for ST_NCI"
Christophe Ricard35630df2014-05-25 22:35:38 +020033
34/* ndlc header */
Christophe Ricard064d0042015-10-25 22:54:44 +010035#define ST_NCI_FRAME_HEADROOM 1
Christophe Ricard30458aa2015-08-14 22:33:31 +020036#define ST_NCI_FRAME_TAILROOM 0
Christophe Ricard35630df2014-05-25 22:35:38 +020037
Christophe Ricarded06aeef2015-06-09 22:26:05 +020038#define ST_NCI_I2C_MIN_SIZE 4 /* PCB(1) + NCI Packet header(3) */
39#define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
Christophe Ricard35630df2014-05-25 22:35:38 +020040
Christophe Ricarded06aeef2015-06-09 22:26:05 +020041#define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
Christophe Ricard35630df2014-05-25 22:35:38 +020042
Christophe Ricard35630df2014-05-25 22:35:38 +020043
Christophe Ricarded06aeef2015-06-09 22:26:05 +020044struct st_nci_i2c_phy {
Christophe Ricard35630df2014-05-25 22:35:38 +020045 struct i2c_client *i2c_dev;
46 struct llt_ndlc *ndlc;
47
Christophe Ricardbb2496c2015-10-25 22:54:48 +010048 bool irq_active;
49
Christophe Ricard35630df2014-05-25 22:35:38 +020050 unsigned int gpio_reset;
51 unsigned int irq_polarity;
Christophe Ricard3648dc62015-10-25 22:54:39 +010052
53 struct st_nci_se_status se_status;
Christophe Ricard35630df2014-05-25 22:35:38 +020054};
55
Christophe Ricarded06aeef2015-06-09 22:26:05 +020056static int st_nci_i2c_enable(void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +020057{
Christophe Ricarded06aeef2015-06-09 22:26:05 +020058 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020059
60 gpio_set_value(phy->gpio_reset, 0);
61 usleep_range(10000, 15000);
62 gpio_set_value(phy->gpio_reset, 1);
Christophe Ricard35630df2014-05-25 22:35:38 +020063 usleep_range(80000, 85000);
64
Christophe Ricardbb2496c2015-10-25 22:54:48 +010065 if (phy->ndlc->powered == 0 && phy->irq_active == 0) {
Christophe Ricard05f09392015-06-06 13:16:51 +020066 enable_irq(phy->i2c_dev->irq);
Christophe Ricardbb2496c2015-10-25 22:54:48 +010067 phy->irq_active = true;
68 }
Christophe Ricard05f09392015-06-06 13:16:51 +020069
Christophe Ricard35630df2014-05-25 22:35:38 +020070 return 0;
71}
72
Christophe Ricarded06aeef2015-06-09 22:26:05 +020073static void st_nci_i2c_disable(void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +020074{
Christophe Ricarded06aeef2015-06-09 22:26:05 +020075 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020076
Christophe Ricard05f09392015-06-06 13:16:51 +020077 disable_irq_nosync(phy->i2c_dev->irq);
Christophe Ricardbb2496c2015-10-25 22:54:48 +010078 phy->irq_active = false;
Christophe Ricard35630df2014-05-25 22:35:38 +020079}
80
Christophe Ricard35630df2014-05-25 22:35:38 +020081/*
82 * Writing a frame must not return the number of written bytes.
83 * It must return either zero for success, or <0 for error.
84 * In addition, it must not alter the skb
85 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +020086static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
Christophe Ricard35630df2014-05-25 22:35:38 +020087{
88 int r = -1;
Christophe Ricarded06aeef2015-06-09 22:26:05 +020089 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +020090 struct i2c_client *client = phy->i2c_dev;
91
Christophe Ricard4294e322014-09-13 10:28:47 +020092 if (phy->ndlc->hard_fault != 0)
93 return phy->ndlc->hard_fault;
Christophe Ricard35630df2014-05-25 22:35:38 +020094
95 r = i2c_master_send(client, skb->data, skb->len);
Christophe Ricardd4a41d12015-03-31 08:02:15 +020096 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +020097 usleep_range(1000, 4000);
98 r = i2c_master_send(client, skb->data, skb->len);
99 }
100
101 if (r >= 0) {
102 if (r != skb->len)
103 r = -EREMOTEIO;
104 else
105 r = 0;
106 }
107
Christophe Ricard35630df2014-05-25 22:35:38 +0200108 return r;
109}
110
111/*
112 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
113 * returns:
Christophe Ricarde7723b32015-08-14 22:33:32 +0200114 * 0 : if received frame is complete
Christophe Ricard35630df2014-05-25 22:35:38 +0200115 * -EREMOTEIO : i2c read error (fatal)
116 * -EBADMSG : frame was incorrect and discarded
Christophe Ricarde7723b32015-08-14 22:33:32 +0200117 * -ENOMEM : cannot allocate skb, frame dropped
Christophe Ricard35630df2014-05-25 22:35:38 +0200118 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200119static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
Christophe Ricard35630df2014-05-25 22:35:38 +0200120 struct sk_buff **skb)
121{
122 int r;
123 u8 len;
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200124 u8 buf[ST_NCI_I2C_MAX_SIZE];
Christophe Ricard35630df2014-05-25 22:35:38 +0200125 struct i2c_client *client = phy->i2c_dev;
126
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200127 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricardd4a41d12015-03-31 08:02:15 +0200128 if (r < 0) { /* Retry, chip was in standby */
Christophe Ricard35630df2014-05-25 22:35:38 +0200129 usleep_range(1000, 4000);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200130 r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricardac633ba2014-09-03 23:30:26 +0200131 }
132
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200133 if (r != ST_NCI_I2C_MIN_SIZE)
Christophe Ricard35630df2014-05-25 22:35:38 +0200134 return -EREMOTEIO;
Christophe Ricard35630df2014-05-25 22:35:38 +0200135
136 len = be16_to_cpu(*(__be16 *) (buf + 2));
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200137 if (len > ST_NCI_I2C_MAX_SIZE) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200138 nfc_err(&client->dev, "invalid frame len\n");
139 return -EBADMSG;
140 }
141
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200142 *skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
Christophe Ricard35630df2014-05-25 22:35:38 +0200143 if (*skb == NULL)
144 return -ENOMEM;
145
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200146 skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
147 skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
148 memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
Christophe Ricard35630df2014-05-25 22:35:38 +0200149
150 if (!len)
151 return 0;
152
153 r = i2c_master_recv(client, buf, len);
154 if (r != len) {
155 kfree_skb(*skb);
156 return -EREMOTEIO;
157 }
158
159 skb_put(*skb, len);
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200160 memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
Christophe Ricard35630df2014-05-25 22:35:38 +0200161
Christophe Ricard35630df2014-05-25 22:35:38 +0200162 return 0;
163}
164
165/*
166 * Reads an ndlc frame from the chip.
167 *
Christophe Ricard30458aa2015-08-14 22:33:31 +0200168 * On ST_NCI, IRQ goes in idle state when read starts.
Christophe Ricard35630df2014-05-25 22:35:38 +0200169 */
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200170static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
Christophe Ricard35630df2014-05-25 22:35:38 +0200171{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200172 struct st_nci_i2c_phy *phy = phy_id;
Christophe Ricard35630df2014-05-25 22:35:38 +0200173 struct i2c_client *client;
174 struct sk_buff *skb = NULL;
175 int r;
176
Christophe Ricard72744962015-01-25 23:33:29 +0100177 if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
Christophe Ricard35630df2014-05-25 22:35:38 +0200178 WARN_ON_ONCE(1);
179 return IRQ_NONE;
180 }
181
182 client = phy->i2c_dev;
183 dev_dbg(&client->dev, "IRQ\n");
184
Christophe Ricard4294e322014-09-13 10:28:47 +0200185 if (phy->ndlc->hard_fault)
Christophe Ricard35630df2014-05-25 22:35:38 +0200186 return IRQ_HANDLED;
187
Christophe Ricard183fe2d2015-06-06 13:16:50 +0200188 if (!phy->ndlc->powered) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200189 st_nci_i2c_disable(phy);
Christophe Ricard35630df2014-05-25 22:35:38 +0200190 return IRQ_HANDLED;
191 }
192
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200193 r = st_nci_i2c_read(phy, &skb);
Christophe Ricard4294e322014-09-13 10:28:47 +0200194 if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
Christophe Ricard35630df2014-05-25 22:35:38 +0200195 return IRQ_HANDLED;
Christophe Ricard35630df2014-05-25 22:35:38 +0200196
197 ndlc_recv(phy->ndlc, skb);
198
199 return IRQ_HANDLED;
200}
201
202static struct nfc_phy_ops i2c_phy_ops = {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200203 .write = st_nci_i2c_write,
204 .enable = st_nci_i2c_enable,
205 .disable = st_nci_i2c_disable,
Christophe Ricard35630df2014-05-25 22:35:38 +0200206};
207
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200208static int st_nci_i2c_of_request_resources(struct i2c_client *client)
Christophe Ricard35630df2014-05-25 22:35:38 +0200209{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200210 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200211 struct device_node *pp;
212 int gpio;
213 int r;
214
215 pp = client->dev.of_node;
216 if (!pp)
217 return -ENODEV;
218
219 /* Get GPIO from device tree */
220 gpio = of_get_named_gpio(pp, "reset-gpios", 0);
221 if (gpio < 0) {
222 nfc_err(&client->dev,
223 "Failed to retrieve reset-gpios from device tree\n");
224 return gpio;
225 }
226
227 /* GPIO request and configuration */
Christophe Ricard56ee6452014-07-28 18:11:33 +0200228 r = devm_gpio_request_one(&client->dev, gpio,
229 GPIOF_OUT_INIT_HIGH, "clf_reset");
Christophe Ricard35630df2014-05-25 22:35:38 +0200230 if (r) {
231 nfc_err(&client->dev, "Failed to request reset pin\n");
Christophe Ricard18159622014-12-02 21:27:56 +0100232 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200233 }
Christophe Ricard35630df2014-05-25 22:35:38 +0200234 phy->gpio_reset = gpio;
235
Christophe Ricarda80d0cb2014-11-13 00:30:26 +0100236 phy->irq_polarity = irq_get_trigger_type(client->irq);
Christophe Ricard35630df2014-05-25 22:35:38 +0200237
Christophe Ricard3648dc62015-10-25 22:54:39 +0100238 phy->se_status.is_ese_present =
239 of_property_read_bool(pp, "ese-present");
240 phy->se_status.is_uicc_present =
241 of_property_read_bool(pp, "uicc-present");
242
Christophe Ricard35630df2014-05-25 22:35:38 +0200243 return 0;
244}
Christophe Ricard35630df2014-05-25 22:35:38 +0200245
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200246static int st_nci_i2c_request_resources(struct i2c_client *client)
Christophe Ricard35630df2014-05-25 22:35:38 +0200247{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200248 struct st_nci_nfc_platform_data *pdata;
249 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200250 int r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200251
252 pdata = client->dev.platform_data;
253 if (pdata == NULL) {
254 nfc_err(&client->dev, "No platform data\n");
255 return -EINVAL;
256 }
257
258 /* store for later use */
Christophe Ricard35630df2014-05-25 22:35:38 +0200259 phy->gpio_reset = pdata->gpio_reset;
260 phy->irq_polarity = pdata->irq_polarity;
261
Christophe Ricard56ee6452014-07-28 18:11:33 +0200262 r = devm_gpio_request_one(&client->dev,
263 phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
Christophe Ricard35630df2014-05-25 22:35:38 +0200264 if (r) {
265 pr_err("%s : reset gpio_request failed\n", __FILE__);
Christophe Ricard18159622014-12-02 21:27:56 +0100266 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200267 }
268
Christophe Ricard3648dc62015-10-25 22:54:39 +0100269 phy->se_status.is_ese_present = pdata->is_ese_present;
270 phy->se_status.is_uicc_present = pdata->is_uicc_present;
271
Christophe Ricard35630df2014-05-25 22:35:38 +0200272 return 0;
273}
274
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200275static int st_nci_i2c_probe(struct i2c_client *client,
Christophe Ricard35630df2014-05-25 22:35:38 +0200276 const struct i2c_device_id *id)
277{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200278 struct st_nci_i2c_phy *phy;
279 struct st_nci_nfc_platform_data *pdata;
Christophe Ricard35630df2014-05-25 22:35:38 +0200280 int r;
281
282 dev_dbg(&client->dev, "%s\n", __func__);
283 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
284
285 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
286 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
287 return -ENODEV;
288 }
289
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200290 phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
Christophe Ricard35630df2014-05-25 22:35:38 +0200291 GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -0700292 if (!phy)
Christophe Ricard35630df2014-05-25 22:35:38 +0200293 return -ENOMEM;
Christophe Ricard35630df2014-05-25 22:35:38 +0200294
295 phy->i2c_dev = client;
296
297 i2c_set_clientdata(client, phy);
298
299 pdata = client->dev.platform_data;
300 if (!pdata && client->dev.of_node) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200301 r = st_nci_i2c_of_request_resources(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200302 if (r) {
303 nfc_err(&client->dev, "No platform data\n");
304 return r;
305 }
306 } else if (pdata) {
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200307 r = st_nci_i2c_request_resources(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200308 if (r) {
309 nfc_err(&client->dev,
310 "Cannot get platform resources\n");
311 return r;
312 }
313 } else {
314 nfc_err(&client->dev,
Christophe Ricard30458aa2015-08-14 22:33:31 +0200315 "st_nci platform resources not available\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200316 return -ENODEV;
317 }
318
Christophe Ricard72744962015-01-25 23:33:29 +0100319 r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
Christophe Ricard30458aa2015-08-14 22:33:31 +0200320 ST_NCI_FRAME_HEADROOM, ST_NCI_FRAME_TAILROOM,
Christophe Ricard3648dc62015-10-25 22:54:39 +0100321 &phy->ndlc, &phy->se_status);
Christophe Ricard72744962015-01-25 23:33:29 +0100322 if (r < 0) {
323 nfc_err(&client->dev, "Unable to register ndlc layer\n");
324 return r;
325 }
326
Christophe Ricardbb2496c2015-10-25 22:54:48 +0100327 phy->irq_active = true;
Christophe Ricard35630df2014-05-25 22:35:38 +0200328 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200329 st_nci_irq_thread_fn,
Christophe Ricard35630df2014-05-25 22:35:38 +0200330 phy->irq_polarity | IRQF_ONESHOT,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200331 ST_NCI_DRIVER_NAME, phy);
Christophe Ricard72744962015-01-25 23:33:29 +0100332 if (r < 0)
Christophe Ricard35630df2014-05-25 22:35:38 +0200333 nfc_err(&client->dev, "Unable to register IRQ handler\n");
Christophe Ricard35630df2014-05-25 22:35:38 +0200334
Christophe Ricard72744962015-01-25 23:33:29 +0100335 return r;
Christophe Ricard35630df2014-05-25 22:35:38 +0200336}
337
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200338static int st_nci_i2c_remove(struct i2c_client *client)
Christophe Ricard35630df2014-05-25 22:35:38 +0200339{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200340 struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
Christophe Ricard35630df2014-05-25 22:35:38 +0200341
342 dev_dbg(&client->dev, "%s\n", __func__);
343
344 ndlc_remove(phy->ndlc);
345
Christophe Ricard35630df2014-05-25 22:35:38 +0200346 return 0;
347}
348
Christophe Ricard32528972015-12-23 23:45:05 +0100349static struct i2c_device_id st_nci_i2c_id_table[] = {
350 {ST_NCI_DRIVER_NAME, 0},
351 {}
352};
353MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
354
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200355static const struct of_device_id of_st_nci_i2c_match[] = {
Christophe Ricard1a94cb62014-12-08 22:08:07 +0100356 { .compatible = "st,st21nfcb-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200357 { .compatible = "st,st21nfcb_i2c", },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200358 { .compatible = "st,st21nfcc-i2c", },
Christophe Ricard35630df2014-05-25 22:35:38 +0200359 {}
360};
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200361MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
Christophe Ricard35630df2014-05-25 22:35:38 +0200362
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200363static struct i2c_driver st_nci_i2c_driver = {
Christophe Ricard35630df2014-05-25 22:35:38 +0200364 .driver = {
365 .owner = THIS_MODULE,
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200366 .name = ST_NCI_I2C_DRIVER_NAME,
367 .of_match_table = of_match_ptr(of_st_nci_i2c_match),
Christophe Ricard35630df2014-05-25 22:35:38 +0200368 },
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200369 .probe = st_nci_i2c_probe,
370 .id_table = st_nci_i2c_id_table,
371 .remove = st_nci_i2c_remove,
Christophe Ricard35630df2014-05-25 22:35:38 +0200372};
373
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200374module_i2c_driver(st_nci_i2c_driver);
Christophe Ricard35630df2014-05-25 22:35:38 +0200375
376MODULE_LICENSE("GPL");
377MODULE_DESCRIPTION(DRIVER_DESC);