i2c.c 8.9 KB
Newer Older
1
/*
2 3
 * I2C Link Layer for ST NCI NFC controller familly based Driver
 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/nfc.h>
28
#include <linux/platform_data/st_nci.h>
29 30 31 32 33 34 35 36 37

#include "ndlc.h"

#define DRIVER_DESC "NCI NFC driver for ST21NFCB"

/* ndlc header */
#define ST21NFCB_FRAME_HEADROOM	1
#define ST21NFCB_FRAME_TAILROOM 0

38 39
#define ST_NCI_I2C_MIN_SIZE 4   /* PCB(1) + NCI Packet header(3) */
#define ST_NCI_I2C_MAX_SIZE 250 /* req 4.2.1 */
40

41
#define ST_NCI_I2C_DRIVER_NAME "st_nci_i2c"
42

43 44
static struct i2c_device_id st_nci_i2c_id_table[] = {
	{ST_NCI_DRIVER_NAME, 0},
45 46
	{}
};
47
MODULE_DEVICE_TABLE(i2c, st_nci_i2c_id_table);
48

49
struct st_nci_i2c_phy {
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	struct i2c_client *i2c_dev;
	struct llt_ndlc *ndlc;

	unsigned int gpio_reset;
	unsigned int irq_polarity;
};

#define I2C_DUMP_SKB(info, skb)					\
do {								\
	pr_debug("%s:\n", info);				\
	print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET,	\
		       16, 1, (skb)->data, (skb)->len, 0);	\
} while (0)

64
static int st_nci_i2c_enable(void *phy_id)
65
{
66
	struct st_nci_i2c_phy *phy = phy_id;
67 68 69 70 71 72

	gpio_set_value(phy->gpio_reset, 0);
	usleep_range(10000, 15000);
	gpio_set_value(phy->gpio_reset, 1);
	usleep_range(80000, 85000);

73 74 75
	if (phy->ndlc->powered == 0)
		enable_irq(phy->i2c_dev->irq);

76 77 78
	return 0;
}

79
static void st_nci_i2c_disable(void *phy_id)
80
{
81
	struct st_nci_i2c_phy *phy = phy_id;
82

83
	disable_irq_nosync(phy->i2c_dev->irq);
84 85 86 87 88 89 90
}

/*
 * Writing a frame must not return the number of written bytes.
 * It must return either zero for success, or <0 for error.
 * In addition, it must not alter the skb
 */
91
static int st_nci_i2c_write(void *phy_id, struct sk_buff *skb)
92 93
{
	int r = -1;
94
	struct st_nci_i2c_phy *phy = phy_id;
95 96
	struct i2c_client *client = phy->i2c_dev;

97
	I2C_DUMP_SKB("st_nci_i2c_write", skb);
98

99 100
	if (phy->ndlc->hard_fault != 0)
		return phy->ndlc->hard_fault;
101 102

	r = i2c_master_send(client, skb->data, skb->len);
103
	if (r < 0) {  /* Retry, chip was in standby */
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		usleep_range(1000, 4000);
		r = i2c_master_send(client, skb->data, skb->len);
	}

	if (r >= 0) {
		if (r != skb->len)
			r = -EREMOTEIO;
		else
			r = 0;
	}

	return r;
}

/*
 * Reads an ndlc frame and returns it in a newly allocated sk_buff.
 * returns:
 * frame size : if received frame is complete (find ST21NFCB_SOF_EOF at
 * end of read)
 * -EAGAIN : if received frame is incomplete (not find ST21NFCB_SOF_EOF
 * at end of read)
 * -EREMOTEIO : i2c read error (fatal)
 * -EBADMSG : frame was incorrect and discarded
127
 * (value returned from st_nci_i2c_repack)
128 129 130
 * -EIO : if no ST21NFCB_SOF_EOF is found after reaching
 * the read length end sequence
 */
131
static int st_nci_i2c_read(struct st_nci_i2c_phy *phy,
132 133 134 135
				 struct sk_buff **skb)
{
	int r;
	u8 len;
136
	u8 buf[ST_NCI_I2C_MAX_SIZE];
137 138
	struct i2c_client *client = phy->i2c_dev;

139
	r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
140
	if (r < 0) {  /* Retry, chip was in standby */
141
		usleep_range(1000, 4000);
142
		r = i2c_master_recv(client, buf, ST_NCI_I2C_MIN_SIZE);
143 144
	}

145
	if (r != ST_NCI_I2C_MIN_SIZE)
146 147 148
		return -EREMOTEIO;

	len = be16_to_cpu(*(__be16 *) (buf + 2));
149
	if (len > ST_NCI_I2C_MAX_SIZE) {
150 151 152 153
		nfc_err(&client->dev, "invalid frame len\n");
		return -EBADMSG;
	}

154
	*skb = alloc_skb(ST_NCI_I2C_MIN_SIZE + len, GFP_KERNEL);
155 156 157
	if (*skb == NULL)
		return -ENOMEM;

158 159 160
	skb_reserve(*skb, ST_NCI_I2C_MIN_SIZE);
	skb_put(*skb, ST_NCI_I2C_MIN_SIZE);
	memcpy((*skb)->data, buf, ST_NCI_I2C_MIN_SIZE);
161 162 163 164 165 166 167 168 169 170 171

	if (!len)
		return 0;

	r = i2c_master_recv(client, buf, len);
	if (r != len) {
		kfree_skb(*skb);
		return -EREMOTEIO;
	}

	skb_put(*skb, len);
172
	memcpy((*skb)->data + ST_NCI_I2C_MIN_SIZE, buf, len);
173 174 175 176 177 178 179 180 181 182 183

	I2C_DUMP_SKB("i2c frame read", *skb);

	return 0;
}

/*
 * Reads an ndlc frame from the chip.
 *
 * On ST21NFCB, IRQ goes in idle state when read starts.
 */
184
static irqreturn_t st_nci_irq_thread_fn(int irq, void *phy_id)
185
{
186
	struct st_nci_i2c_phy *phy = phy_id;
187 188 189 190
	struct i2c_client *client;
	struct sk_buff *skb = NULL;
	int r;

191
	if (!phy || !phy->ndlc || irq != phy->i2c_dev->irq) {
192 193 194 195 196 197 198
		WARN_ON_ONCE(1);
		return IRQ_NONE;
	}

	client = phy->i2c_dev;
	dev_dbg(&client->dev, "IRQ\n");

199
	if (phy->ndlc->hard_fault)
200 201
		return IRQ_HANDLED;

202
	if (!phy->ndlc->powered) {
203
		st_nci_i2c_disable(phy);
204 205 206
		return IRQ_HANDLED;
	}

207
	r = st_nci_i2c_read(phy, &skb);
208
	if (r == -EREMOTEIO || r == -ENOMEM || r == -EBADMSG)
209 210 211 212 213 214 215 216
		return IRQ_HANDLED;

	ndlc_recv(phy->ndlc, skb);

	return IRQ_HANDLED;
}

static struct nfc_phy_ops i2c_phy_ops = {
217 218 219
	.write = st_nci_i2c_write,
	.enable = st_nci_i2c_enable,
	.disable = st_nci_i2c_disable,
220 221 222
};

#ifdef CONFIG_OF
223
static int st_nci_i2c_of_request_resources(struct i2c_client *client)
224
{
225
	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	struct device_node *pp;
	int gpio;
	int r;

	pp = client->dev.of_node;
	if (!pp)
		return -ENODEV;

	/* Get GPIO from device tree */
	gpio = of_get_named_gpio(pp, "reset-gpios", 0);
	if (gpio < 0) {
		nfc_err(&client->dev,
			"Failed to retrieve reset-gpios from device tree\n");
		return gpio;
	}

	/* GPIO request and configuration */
243 244
	r = devm_gpio_request_one(&client->dev, gpio,
				GPIOF_OUT_INIT_HIGH, "clf_reset");
245 246
	if (r) {
		nfc_err(&client->dev, "Failed to request reset pin\n");
247
		return r;
248 249 250
	}
	phy->gpio_reset = gpio;

251
	phy->irq_polarity = irq_get_trigger_type(client->irq);
252 253 254 255

	return 0;
}
#else
256
static int st_nci_i2c_of_request_resources(struct i2c_client *client)
257 258 259 260 261
{
	return -ENODEV;
}
#endif

262
static int st_nci_i2c_request_resources(struct i2c_client *client)
263
{
264 265
	struct st_nci_nfc_platform_data *pdata;
	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
266 267 268 269 270 271 272 273 274 275 276 277
	int r;

	pdata = client->dev.platform_data;
	if (pdata == NULL) {
		nfc_err(&client->dev, "No platform data\n");
		return -EINVAL;
	}

	/* store for later use */
	phy->gpio_reset = pdata->gpio_reset;
	phy->irq_polarity = pdata->irq_polarity;

278 279
	r = devm_gpio_request_one(&client->dev,
			phy->gpio_reset, GPIOF_OUT_INIT_HIGH, "clf_reset");
280 281
	if (r) {
		pr_err("%s : reset gpio_request failed\n", __FILE__);
282
		return r;
283 284 285 286 287
	}

	return 0;
}

288
static int st_nci_i2c_probe(struct i2c_client *client,
289 290
				  const struct i2c_device_id *id)
{
291 292
	struct st_nci_i2c_phy *phy;
	struct st_nci_nfc_platform_data *pdata;
293 294 295 296 297 298 299 300 301 302
	int r;

	dev_dbg(&client->dev, "%s\n", __func__);
	dev_dbg(&client->dev, "IRQ: %d\n", client->irq);

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
		return -ENODEV;
	}

303
	phy = devm_kzalloc(&client->dev, sizeof(struct st_nci_i2c_phy),
304
			   GFP_KERNEL);
J
Joe Perches 已提交
305
	if (!phy)
306 307 308 309 310 311 312 313
		return -ENOMEM;

	phy->i2c_dev = client;

	i2c_set_clientdata(client, phy);

	pdata = client->dev.platform_data;
	if (!pdata && client->dev.of_node) {
314
		r = st_nci_i2c_of_request_resources(client);
315 316 317 318 319
		if (r) {
			nfc_err(&client->dev, "No platform data\n");
			return r;
		}
	} else if (pdata) {
320
		r = st_nci_i2c_request_resources(client);
321 322 323 324 325 326 327 328 329 330 331
		if (r) {
			nfc_err(&client->dev,
				"Cannot get platform resources\n");
			return r;
		}
	} else {
		nfc_err(&client->dev,
			"st21nfcb platform resources not available\n");
		return -ENODEV;
	}

332 333 334 335 336 337 338 339
	r = ndlc_probe(phy, &i2c_phy_ops, &client->dev,
			ST21NFCB_FRAME_HEADROOM, ST21NFCB_FRAME_TAILROOM,
			&phy->ndlc);
	if (r < 0) {
		nfc_err(&client->dev, "Unable to register ndlc layer\n");
		return r;
	}

340
	r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
341
				st_nci_irq_thread_fn,
342
				phy->irq_polarity | IRQF_ONESHOT,
343
				ST_NCI_DRIVER_NAME, phy);
344
	if (r < 0)
345 346
		nfc_err(&client->dev, "Unable to register IRQ handler\n");

347
	return r;
348 349
}

350
static int st_nci_i2c_remove(struct i2c_client *client)
351
{
352
	struct st_nci_i2c_phy *phy = i2c_get_clientdata(client);
353 354 355 356 357 358 359 360

	dev_dbg(&client->dev, "%s\n", __func__);

	ndlc_remove(phy->ndlc);

	return 0;
}

361
#ifdef CONFIG_OF
362
static const struct of_device_id of_st_nci_i2c_match[] = {
363
	{ .compatible = "st,st21nfcb-i2c", },
364
	{ .compatible = "st,st21nfcb_i2c", },
365
	{ .compatible = "st,st21nfcc-i2c", },
366 367
	{}
};
368
MODULE_DEVICE_TABLE(of, of_st_nci_i2c_match);
369
#endif
370

371
static struct i2c_driver st_nci_i2c_driver = {
372 373
	.driver = {
		.owner = THIS_MODULE,
374 375
		.name = ST_NCI_I2C_DRIVER_NAME,
		.of_match_table = of_match_ptr(of_st_nci_i2c_match),
376
	},
377 378 379
	.probe = st_nci_i2c_probe,
	.id_table = st_nci_i2c_id_table,
	.remove = st_nci_i2c_remove,
380 381
};

382
module_i2c_driver(st_nci_i2c_driver);
383 384 385

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);