18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HCI based Driver for Inside Secure microread NFC Chip - i2c layer
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Intel Corporation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/gpio.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/nfc.h>
188c2ecf20Sopenharmony_ci#include <net/nfc/hci.h>
198c2ecf20Sopenharmony_ci#include <net/nfc/llc.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "microread.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define MICROREAD_I2C_DRIVER_NAME "microread"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define MICROREAD_I2C_FRAME_HEADROOM 1
268c2ecf20Sopenharmony_ci#define MICROREAD_I2C_FRAME_TAILROOM 1
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* framing in HCI mode */
298c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_LEN		1
308c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_CRC		1
318c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_LEN_CRC	(MICROREAD_I2C_LLC_LEN + \
328c2ecf20Sopenharmony_ci					MICROREAD_I2C_LLC_CRC)
338c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_MIN_SIZE	(1 + MICROREAD_I2C_LLC_LEN_CRC)
348c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_MAX_PAYLOAD	29
358c2ecf20Sopenharmony_ci#define MICROREAD_I2C_LLC_MAX_SIZE	(MICROREAD_I2C_LLC_LEN_CRC + 1 + \
368c2ecf20Sopenharmony_ci					MICROREAD_I2C_LLC_MAX_PAYLOAD)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct microread_i2c_phy {
398c2ecf20Sopenharmony_ci	struct i2c_client *i2c_dev;
408c2ecf20Sopenharmony_ci	struct nfc_hci_dev *hdev;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	int hard_fault;		/*
438c2ecf20Sopenharmony_ci				 * < 0 if hardware error occured (e.g. i2c err)
448c2ecf20Sopenharmony_ci				 * and prevents normal operation.
458c2ecf20Sopenharmony_ci				 */
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define I2C_DUMP_SKB(info, skb)					\
498c2ecf20Sopenharmony_cido {								\
508c2ecf20Sopenharmony_ci	pr_debug("%s:\n", info);				\
518c2ecf20Sopenharmony_ci	print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET,	\
528c2ecf20Sopenharmony_ci		       16, 1, (skb)->data, (skb)->len, 0);	\
538c2ecf20Sopenharmony_ci} while (0)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic void microread_i2c_add_len_crc(struct sk_buff *skb)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	int i;
588c2ecf20Sopenharmony_ci	u8 crc = 0;
598c2ecf20Sopenharmony_ci	int len;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	len = skb->len;
628c2ecf20Sopenharmony_ci	*(u8 *)skb_push(skb, 1) = len;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	for (i = 0; i < skb->len; i++)
658c2ecf20Sopenharmony_ci		crc = crc ^ skb->data[i];
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	skb_put_u8(skb, crc);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void microread_i2c_remove_len_crc(struct sk_buff *skb)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	skb_pull(skb, MICROREAD_I2C_FRAME_HEADROOM);
738c2ecf20Sopenharmony_ci	skb_trim(skb, MICROREAD_I2C_FRAME_TAILROOM);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int check_crc(struct sk_buff *skb)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	int i;
798c2ecf20Sopenharmony_ci	u8 crc = 0;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	for (i = 0; i < skb->len - 1; i++)
828c2ecf20Sopenharmony_ci		crc = crc ^ skb->data[i];
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (crc != skb->data[skb->len-1]) {
858c2ecf20Sopenharmony_ci		pr_err("CRC error 0x%x != 0x%x\n", crc, skb->data[skb->len-1]);
868c2ecf20Sopenharmony_ci		pr_info("%s: BAD CRC\n", __func__);
878c2ecf20Sopenharmony_ci		return -EPERM;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int microread_i2c_enable(void *phy_id)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void microread_i2c_disable(void *phy_id)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	return;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int microread_i2c_write(void *phy_id, struct sk_buff *skb)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	int r;
1068c2ecf20Sopenharmony_ci	struct microread_i2c_phy *phy = phy_id;
1078c2ecf20Sopenharmony_ci	struct i2c_client *client = phy->i2c_dev;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (phy->hard_fault != 0)
1108c2ecf20Sopenharmony_ci		return phy->hard_fault;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	usleep_range(3000, 6000);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	microread_i2c_add_len_crc(skb);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	I2C_DUMP_SKB("i2c frame written", skb);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	r = i2c_master_send(client, skb->data, skb->len);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (r == -EREMOTEIO) {	/* Retry, chip was in standby */
1218c2ecf20Sopenharmony_ci		usleep_range(6000, 10000);
1228c2ecf20Sopenharmony_ci		r = i2c_master_send(client, skb->data, skb->len);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (r >= 0) {
1268c2ecf20Sopenharmony_ci		if (r != skb->len)
1278c2ecf20Sopenharmony_ci			r = -EREMOTEIO;
1288c2ecf20Sopenharmony_ci		else
1298c2ecf20Sopenharmony_ci			r = 0;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	microread_i2c_remove_len_crc(skb);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return r;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int microread_i2c_read(struct microread_i2c_phy *phy,
1398c2ecf20Sopenharmony_ci			      struct sk_buff **skb)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	int r;
1428c2ecf20Sopenharmony_ci	u8 len;
1438c2ecf20Sopenharmony_ci	u8 tmp[MICROREAD_I2C_LLC_MAX_SIZE - 1];
1448c2ecf20Sopenharmony_ci	struct i2c_client *client = phy->i2c_dev;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	r = i2c_master_recv(client, &len, 1);
1478c2ecf20Sopenharmony_ci	if (r != 1) {
1488c2ecf20Sopenharmony_ci		nfc_err(&client->dev, "cannot read len byte\n");
1498c2ecf20Sopenharmony_ci		return -EREMOTEIO;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if ((len < MICROREAD_I2C_LLC_MIN_SIZE) ||
1538c2ecf20Sopenharmony_ci	    (len > MICROREAD_I2C_LLC_MAX_SIZE)) {
1548c2ecf20Sopenharmony_ci		nfc_err(&client->dev, "invalid len byte\n");
1558c2ecf20Sopenharmony_ci		r = -EBADMSG;
1568c2ecf20Sopenharmony_ci		goto flush;
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	*skb = alloc_skb(1 + len, GFP_KERNEL);
1608c2ecf20Sopenharmony_ci	if (*skb == NULL) {
1618c2ecf20Sopenharmony_ci		r = -ENOMEM;
1628c2ecf20Sopenharmony_ci		goto flush;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	skb_put_u8(*skb, len);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	r = i2c_master_recv(client, skb_put(*skb, len), len);
1688c2ecf20Sopenharmony_ci	if (r != len) {
1698c2ecf20Sopenharmony_ci		kfree_skb(*skb);
1708c2ecf20Sopenharmony_ci		return -EREMOTEIO;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	I2C_DUMP_SKB("cc frame read", *skb);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	r = check_crc(*skb);
1768c2ecf20Sopenharmony_ci	if (r != 0) {
1778c2ecf20Sopenharmony_ci		kfree_skb(*skb);
1788c2ecf20Sopenharmony_ci		r = -EBADMSG;
1798c2ecf20Sopenharmony_ci		goto flush;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	skb_pull(*skb, 1);
1838c2ecf20Sopenharmony_ci	skb_trim(*skb, (*skb)->len - MICROREAD_I2C_FRAME_TAILROOM);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	usleep_range(3000, 6000);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return 0;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ciflush:
1908c2ecf20Sopenharmony_ci	if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
1918c2ecf20Sopenharmony_ci		r = -EREMOTEIO;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	usleep_range(3000, 6000);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return r;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic irqreturn_t microread_i2c_irq_thread_fn(int irq, void *phy_id)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct microread_i2c_phy *phy = phy_id;
2018c2ecf20Sopenharmony_ci	struct sk_buff *skb = NULL;
2028c2ecf20Sopenharmony_ci	int r;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!phy || irq != phy->i2c_dev->irq) {
2058c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
2068c2ecf20Sopenharmony_ci		return IRQ_NONE;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (phy->hard_fault != 0)
2108c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	r = microread_i2c_read(phy, &skb);
2138c2ecf20Sopenharmony_ci	if (r == -EREMOTEIO) {
2148c2ecf20Sopenharmony_ci		phy->hard_fault = r;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		nfc_hci_recv_frame(phy->hdev, NULL);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
2198c2ecf20Sopenharmony_ci	} else if ((r == -ENOMEM) || (r == -EBADMSG)) {
2208c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	nfc_hci_recv_frame(phy->hdev, skb);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic struct nfc_phy_ops i2c_phy_ops = {
2298c2ecf20Sopenharmony_ci	.write = microread_i2c_write,
2308c2ecf20Sopenharmony_ci	.enable = microread_i2c_enable,
2318c2ecf20Sopenharmony_ci	.disable = microread_i2c_disable,
2328c2ecf20Sopenharmony_ci};
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int microread_i2c_probe(struct i2c_client *client,
2358c2ecf20Sopenharmony_ci			       const struct i2c_device_id *id)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct microread_i2c_phy *phy;
2388c2ecf20Sopenharmony_ci	int r;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "client %p\n", client);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	phy = devm_kzalloc(&client->dev, sizeof(struct microread_i2c_phy),
2438c2ecf20Sopenharmony_ci			   GFP_KERNEL);
2448c2ecf20Sopenharmony_ci	if (!phy)
2458c2ecf20Sopenharmony_ci		return -ENOMEM;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, phy);
2488c2ecf20Sopenharmony_ci	phy->i2c_dev = client;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	r = request_threaded_irq(client->irq, NULL, microread_i2c_irq_thread_fn,
2518c2ecf20Sopenharmony_ci				 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2528c2ecf20Sopenharmony_ci				 MICROREAD_I2C_DRIVER_NAME, phy);
2538c2ecf20Sopenharmony_ci	if (r) {
2548c2ecf20Sopenharmony_ci		nfc_err(&client->dev, "Unable to register IRQ handler\n");
2558c2ecf20Sopenharmony_ci		return r;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	r = microread_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
2598c2ecf20Sopenharmony_ci			    MICROREAD_I2C_FRAME_HEADROOM,
2608c2ecf20Sopenharmony_ci			    MICROREAD_I2C_FRAME_TAILROOM,
2618c2ecf20Sopenharmony_ci			    MICROREAD_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
2628c2ecf20Sopenharmony_ci	if (r < 0)
2638c2ecf20Sopenharmony_ci		goto err_irq;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	nfc_info(&client->dev, "Probed\n");
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cierr_irq:
2708c2ecf20Sopenharmony_ci	free_irq(client->irq, phy);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return r;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic int microread_i2c_remove(struct i2c_client *client)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct microread_i2c_phy *phy = i2c_get_clientdata(client);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	microread_remove(phy->hdev);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	free_irq(client->irq, phy);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return 0;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic const struct i2c_device_id microread_i2c_id[] = {
2878c2ecf20Sopenharmony_ci	{ MICROREAD_I2C_DRIVER_NAME, 0},
2888c2ecf20Sopenharmony_ci	{ }
2898c2ecf20Sopenharmony_ci};
2908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, microread_i2c_id);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic struct i2c_driver microread_i2c_driver = {
2938c2ecf20Sopenharmony_ci	.driver = {
2948c2ecf20Sopenharmony_ci		.name = MICROREAD_I2C_DRIVER_NAME,
2958c2ecf20Sopenharmony_ci	},
2968c2ecf20Sopenharmony_ci	.probe		= microread_i2c_probe,
2978c2ecf20Sopenharmony_ci	.remove		= microread_i2c_remove,
2988c2ecf20Sopenharmony_ci	.id_table	= microread_i2c_id,
2998c2ecf20Sopenharmony_ci};
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cimodule_i2c_driver(microread_i2c_driver);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3048c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
305