18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci /******************************************************************************
38c2ecf20Sopenharmony_ci * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501/NPCT6XX,
48c2ecf20Sopenharmony_ci * based on the TCG TPM Interface Spec version 1.2.
58c2ecf20Sopenharmony_ci * Specifications at www.trustedcomputinggroup.org
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2011, Nuvoton Technology Corporation.
88c2ecf20Sopenharmony_ci *  Dan Morav <dan.morav@nuvoton.com>
98c2ecf20Sopenharmony_ci * Copyright (C) 2013, Obsidian Research Corp.
108c2ecf20Sopenharmony_ci *  Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Nuvoton contact information: APC.Support@nuvoton.com
138c2ecf20Sopenharmony_ci *****************************************************************************/
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
208c2ecf20Sopenharmony_ci#include <linux/wait.h>
218c2ecf20Sopenharmony_ci#include <linux/i2c.h>
228c2ecf20Sopenharmony_ci#include <linux/of_device.h>
238c2ecf20Sopenharmony_ci#include "tpm.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* I2C interface offsets */
268c2ecf20Sopenharmony_ci#define TPM_STS			0x00
278c2ecf20Sopenharmony_ci#define TPM_BURST_COUNT		0x01
288c2ecf20Sopenharmony_ci#define TPM_DATA_FIFO_W		0x20
298c2ecf20Sopenharmony_ci#define TPM_DATA_FIFO_R		0x40
308c2ecf20Sopenharmony_ci#define TPM_VID_DID_RID		0x60
318c2ecf20Sopenharmony_ci#define TPM_I2C_RETRIES		5
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * I2C bus device maximum buffer size w/o counting I2C address or command
348c2ecf20Sopenharmony_ci * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define TPM_I2C_MAX_BUF_SIZE           32
378c2ecf20Sopenharmony_ci#define TPM_I2C_RETRY_COUNT            32
388c2ecf20Sopenharmony_ci#define TPM_I2C_BUS_DELAY              1000      	/* usec */
398c2ecf20Sopenharmony_ci#define TPM_I2C_RETRY_DELAY_SHORT      (2 * 1000)	/* usec */
408c2ecf20Sopenharmony_ci#define TPM_I2C_RETRY_DELAY_LONG       (10 * 1000) 	/* usec */
418c2ecf20Sopenharmony_ci#define TPM_I2C_DELAY_RANGE            300		/* usec */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define OF_IS_TPM2 ((void *)1)
448c2ecf20Sopenharmony_ci#define I2C_IS_TPM2 1
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct priv_data {
478c2ecf20Sopenharmony_ci	int irq;
488c2ecf20Sopenharmony_ci	unsigned int intrs;
498c2ecf20Sopenharmony_ci	wait_queue_head_t read_queue;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
538c2ecf20Sopenharmony_ci				u8 *data)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	s32 status;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
588c2ecf20Sopenharmony_ci	dev_dbg(&client->dev,
598c2ecf20Sopenharmony_ci		"%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
608c2ecf20Sopenharmony_ci		offset, size, (int)size, data, status);
618c2ecf20Sopenharmony_ci	return status;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
658c2ecf20Sopenharmony_ci				 u8 *data)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	s32 status;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
708c2ecf20Sopenharmony_ci	dev_dbg(&client->dev,
718c2ecf20Sopenharmony_ci		"%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
728c2ecf20Sopenharmony_ci		offset, size, (int)size, data, status);
738c2ecf20Sopenharmony_ci	return status;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define TPM_STS_VALID          0x80
778c2ecf20Sopenharmony_ci#define TPM_STS_COMMAND_READY  0x40
788c2ecf20Sopenharmony_ci#define TPM_STS_GO             0x20
798c2ecf20Sopenharmony_ci#define TPM_STS_DATA_AVAIL     0x10
808c2ecf20Sopenharmony_ci#define TPM_STS_EXPECT         0x08
818c2ecf20Sopenharmony_ci#define TPM_STS_RESPONSE_RETRY 0x02
828c2ecf20Sopenharmony_ci#define TPM_STS_ERR_VAL        0x07    /* bit2...bit0 reads always 0 */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define TPM_I2C_SHORT_TIMEOUT  750     /* ms */
858c2ecf20Sopenharmony_ci#define TPM_I2C_LONG_TIMEOUT   2000    /* 2 sec */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* read TPM_STS register */
888c2ecf20Sopenharmony_cistatic u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(chip->dev.parent);
918c2ecf20Sopenharmony_ci	s32 status;
928c2ecf20Sopenharmony_ci	u8 data;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
958c2ecf20Sopenharmony_ci	if (status <= 0) {
968c2ecf20Sopenharmony_ci		dev_err(&chip->dev, "%s() error return %d\n", __func__,
978c2ecf20Sopenharmony_ci			status);
988c2ecf20Sopenharmony_ci		data = TPM_STS_ERR_VAL;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return data;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* write byte to TPM_STS register */
1058c2ecf20Sopenharmony_cistatic s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	s32 status;
1088c2ecf20Sopenharmony_ci	int i;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* this causes the current command to be aborted */
1118c2ecf20Sopenharmony_ci	for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
1128c2ecf20Sopenharmony_ci		status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
1138c2ecf20Sopenharmony_ci		if (status < 0)
1148c2ecf20Sopenharmony_ci			usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
1158c2ecf20Sopenharmony_ci				     + TPM_I2C_DELAY_RANGE);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci	return status;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* write commandReady to TPM_STS register */
1218c2ecf20Sopenharmony_cistatic void i2c_nuvoton_ready(struct tpm_chip *chip)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(chip->dev.parent);
1248c2ecf20Sopenharmony_ci	s32 status;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* this causes the current command to be aborted */
1278c2ecf20Sopenharmony_ci	status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
1288c2ecf20Sopenharmony_ci	if (status < 0)
1298c2ecf20Sopenharmony_ci		dev_err(&chip->dev,
1308c2ecf20Sopenharmony_ci			"%s() fail to write TPM_STS.commandReady\n", __func__);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* read burstCount field from TPM_STS register
1348c2ecf20Sopenharmony_ci * return -1 on fail to read */
1358c2ecf20Sopenharmony_cistatic int i2c_nuvoton_get_burstcount(struct i2c_client *client,
1368c2ecf20Sopenharmony_ci				      struct tpm_chip *chip)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	unsigned long stop = jiffies + chip->timeout_d;
1398c2ecf20Sopenharmony_ci	s32 status;
1408c2ecf20Sopenharmony_ci	int burst_count = -1;
1418c2ecf20Sopenharmony_ci	u8 data;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* wait for burstcount to be non-zero */
1448c2ecf20Sopenharmony_ci	do {
1458c2ecf20Sopenharmony_ci		/* in I2C burstCount is 1 byte */
1468c2ecf20Sopenharmony_ci		status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
1478c2ecf20Sopenharmony_ci					      &data);
1488c2ecf20Sopenharmony_ci		if (status > 0 && data > 0) {
1498c2ecf20Sopenharmony_ci			burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
1508c2ecf20Sopenharmony_ci			break;
1518c2ecf20Sopenharmony_ci		}
1528c2ecf20Sopenharmony_ci		usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
1538c2ecf20Sopenharmony_ci			     + TPM_I2C_DELAY_RANGE);
1548c2ecf20Sopenharmony_ci	} while (time_before(jiffies, stop));
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return burst_count;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * WPCT301/NPCT501/NPCT6XX SINT# supports only dataAvail
1618c2ecf20Sopenharmony_ci * any call to this function which is not waiting for dataAvail will
1628c2ecf20Sopenharmony_ci * set queue to NULL to avoid waiting for interrupt
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	u8 status = i2c_nuvoton_read_status(chip);
1678c2ecf20Sopenharmony_ci	return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
1718c2ecf20Sopenharmony_ci				     u32 timeout, wait_queue_head_t *queue)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	if ((chip->flags & TPM_CHIP_FLAG_IRQ) && queue) {
1748c2ecf20Sopenharmony_ci		s32 rc;
1758c2ecf20Sopenharmony_ci		struct priv_data *priv = dev_get_drvdata(&chip->dev);
1768c2ecf20Sopenharmony_ci		unsigned int cur_intrs = priv->intrs;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		enable_irq(priv->irq);
1798c2ecf20Sopenharmony_ci		rc = wait_event_interruptible_timeout(*queue,
1808c2ecf20Sopenharmony_ci						      cur_intrs != priv->intrs,
1818c2ecf20Sopenharmony_ci						      timeout);
1828c2ecf20Sopenharmony_ci		if (rc > 0)
1838c2ecf20Sopenharmony_ci			return 0;
1848c2ecf20Sopenharmony_ci		/* At this point we know that the SINT pin is asserted, so we
1858c2ecf20Sopenharmony_ci		 * do not need to do i2c_nuvoton_check_status */
1868c2ecf20Sopenharmony_ci	} else {
1878c2ecf20Sopenharmony_ci		unsigned long ten_msec, stop;
1888c2ecf20Sopenharmony_ci		bool status_valid;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		/* check current status */
1918c2ecf20Sopenharmony_ci		status_valid = i2c_nuvoton_check_status(chip, mask, value);
1928c2ecf20Sopenharmony_ci		if (status_valid)
1938c2ecf20Sopenharmony_ci			return 0;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		/* use polling to wait for the event */
1968c2ecf20Sopenharmony_ci		ten_msec = jiffies + usecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
1978c2ecf20Sopenharmony_ci		stop = jiffies + timeout;
1988c2ecf20Sopenharmony_ci		do {
1998c2ecf20Sopenharmony_ci			if (time_before(jiffies, ten_msec))
2008c2ecf20Sopenharmony_ci				usleep_range(TPM_I2C_RETRY_DELAY_SHORT,
2018c2ecf20Sopenharmony_ci					     TPM_I2C_RETRY_DELAY_SHORT
2028c2ecf20Sopenharmony_ci					     + TPM_I2C_DELAY_RANGE);
2038c2ecf20Sopenharmony_ci			else
2048c2ecf20Sopenharmony_ci				usleep_range(TPM_I2C_RETRY_DELAY_LONG,
2058c2ecf20Sopenharmony_ci					     TPM_I2C_RETRY_DELAY_LONG
2068c2ecf20Sopenharmony_ci					     + TPM_I2C_DELAY_RANGE);
2078c2ecf20Sopenharmony_ci			status_valid = i2c_nuvoton_check_status(chip, mask,
2088c2ecf20Sopenharmony_ci								value);
2098c2ecf20Sopenharmony_ci			if (status_valid)
2108c2ecf20Sopenharmony_ci				return 0;
2118c2ecf20Sopenharmony_ci		} while (time_before(jiffies, stop));
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci	dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
2148c2ecf20Sopenharmony_ci		value);
2158c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/* wait for dataAvail field to be set in the TPM_STS register */
2198c2ecf20Sopenharmony_cistatic int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
2208c2ecf20Sopenharmony_ci					   wait_queue_head_t *queue)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return i2c_nuvoton_wait_for_stat(chip,
2238c2ecf20Sopenharmony_ci					 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
2248c2ecf20Sopenharmony_ci					 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
2258c2ecf20Sopenharmony_ci					 timeout, queue);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/* Read @count bytes into @buf from TPM_RD_FIFO register */
2298c2ecf20Sopenharmony_cistatic int i2c_nuvoton_recv_data(struct i2c_client *client,
2308c2ecf20Sopenharmony_ci				 struct tpm_chip *chip, u8 *buf, size_t count)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	struct priv_data *priv = dev_get_drvdata(&chip->dev);
2338c2ecf20Sopenharmony_ci	s32 rc;
2348c2ecf20Sopenharmony_ci	int burst_count, bytes2read, size = 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	while (size < count &&
2378c2ecf20Sopenharmony_ci	       i2c_nuvoton_wait_for_data_avail(chip,
2388c2ecf20Sopenharmony_ci					       chip->timeout_c,
2398c2ecf20Sopenharmony_ci					       &priv->read_queue) == 0) {
2408c2ecf20Sopenharmony_ci		burst_count = i2c_nuvoton_get_burstcount(client, chip);
2418c2ecf20Sopenharmony_ci		if (burst_count < 0) {
2428c2ecf20Sopenharmony_ci			dev_err(&chip->dev,
2438c2ecf20Sopenharmony_ci				"%s() fail to read burstCount=%d\n", __func__,
2448c2ecf20Sopenharmony_ci				burst_count);
2458c2ecf20Sopenharmony_ci			return -EIO;
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci		bytes2read = min_t(size_t, burst_count, count - size);
2488c2ecf20Sopenharmony_ci		rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
2498c2ecf20Sopenharmony_ci					  bytes2read, &buf[size]);
2508c2ecf20Sopenharmony_ci		if (rc < 0) {
2518c2ecf20Sopenharmony_ci			dev_err(&chip->dev,
2528c2ecf20Sopenharmony_ci				"%s() fail on i2c_nuvoton_read_buf()=%d\n",
2538c2ecf20Sopenharmony_ci				__func__, rc);
2548c2ecf20Sopenharmony_ci			return -EIO;
2558c2ecf20Sopenharmony_ci		}
2568c2ecf20Sopenharmony_ci		dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
2578c2ecf20Sopenharmony_ci		size += bytes2read;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	return size;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/* Read TPM command results */
2648c2ecf20Sopenharmony_cistatic int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	struct priv_data *priv = dev_get_drvdata(&chip->dev);
2678c2ecf20Sopenharmony_ci	struct device *dev = chip->dev.parent;
2688c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2698c2ecf20Sopenharmony_ci	s32 rc;
2708c2ecf20Sopenharmony_ci	int status;
2718c2ecf20Sopenharmony_ci	int burst_count;
2728c2ecf20Sopenharmony_ci	int retries;
2738c2ecf20Sopenharmony_ci	int size = 0;
2748c2ecf20Sopenharmony_ci	u32 expected;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (count < TPM_HEADER_SIZE) {
2778c2ecf20Sopenharmony_ci		i2c_nuvoton_ready(chip);    /* return to idle */
2788c2ecf20Sopenharmony_ci		dev_err(dev, "%s() count < header size\n", __func__);
2798c2ecf20Sopenharmony_ci		return -EIO;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci	for (retries = 0; retries < TPM_I2C_RETRIES; retries++) {
2828c2ecf20Sopenharmony_ci		if (retries > 0) {
2838c2ecf20Sopenharmony_ci			/* if this is not the first trial, set responseRetry */
2848c2ecf20Sopenharmony_ci			i2c_nuvoton_write_status(client,
2858c2ecf20Sopenharmony_ci						 TPM_STS_RESPONSE_RETRY);
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		/*
2888c2ecf20Sopenharmony_ci		 * read first available (> 10 bytes), including:
2898c2ecf20Sopenharmony_ci		 * tag, paramsize, and result
2908c2ecf20Sopenharmony_ci		 */
2918c2ecf20Sopenharmony_ci		status = i2c_nuvoton_wait_for_data_avail(
2928c2ecf20Sopenharmony_ci			chip, chip->timeout_c, &priv->read_queue);
2938c2ecf20Sopenharmony_ci		if (status != 0) {
2948c2ecf20Sopenharmony_ci			dev_err(dev, "%s() timeout on dataAvail\n", __func__);
2958c2ecf20Sopenharmony_ci			size = -ETIMEDOUT;
2968c2ecf20Sopenharmony_ci			continue;
2978c2ecf20Sopenharmony_ci		}
2988c2ecf20Sopenharmony_ci		burst_count = i2c_nuvoton_get_burstcount(client, chip);
2998c2ecf20Sopenharmony_ci		if (burst_count < 0) {
3008c2ecf20Sopenharmony_ci			dev_err(dev, "%s() fail to get burstCount\n", __func__);
3018c2ecf20Sopenharmony_ci			size = -EIO;
3028c2ecf20Sopenharmony_ci			continue;
3038c2ecf20Sopenharmony_ci		}
3048c2ecf20Sopenharmony_ci		size = i2c_nuvoton_recv_data(client, chip, buf,
3058c2ecf20Sopenharmony_ci					     burst_count);
3068c2ecf20Sopenharmony_ci		if (size < TPM_HEADER_SIZE) {
3078c2ecf20Sopenharmony_ci			dev_err(dev, "%s() fail to read header\n", __func__);
3088c2ecf20Sopenharmony_ci			size = -EIO;
3098c2ecf20Sopenharmony_ci			continue;
3108c2ecf20Sopenharmony_ci		}
3118c2ecf20Sopenharmony_ci		/*
3128c2ecf20Sopenharmony_ci		 * convert number of expected bytes field from big endian 32 bit
3138c2ecf20Sopenharmony_ci		 * to machine native
3148c2ecf20Sopenharmony_ci		 */
3158c2ecf20Sopenharmony_ci		expected = be32_to_cpu(*(__be32 *) (buf + 2));
3168c2ecf20Sopenharmony_ci		if (expected > count || expected < size) {
3178c2ecf20Sopenharmony_ci			dev_err(dev, "%s() expected > count\n", __func__);
3188c2ecf20Sopenharmony_ci			size = -EIO;
3198c2ecf20Sopenharmony_ci			continue;
3208c2ecf20Sopenharmony_ci		}
3218c2ecf20Sopenharmony_ci		rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
3228c2ecf20Sopenharmony_ci					   expected - size);
3238c2ecf20Sopenharmony_ci		size += rc;
3248c2ecf20Sopenharmony_ci		if (rc < 0 || size < expected) {
3258c2ecf20Sopenharmony_ci			dev_err(dev, "%s() fail to read remainder of result\n",
3268c2ecf20Sopenharmony_ci				__func__);
3278c2ecf20Sopenharmony_ci			size = -EIO;
3288c2ecf20Sopenharmony_ci			continue;
3298c2ecf20Sopenharmony_ci		}
3308c2ecf20Sopenharmony_ci		if (i2c_nuvoton_wait_for_stat(
3318c2ecf20Sopenharmony_ci			    chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
3328c2ecf20Sopenharmony_ci			    TPM_STS_VALID, chip->timeout_c,
3338c2ecf20Sopenharmony_ci			    NULL)) {
3348c2ecf20Sopenharmony_ci			dev_err(dev, "%s() error left over data\n", __func__);
3358c2ecf20Sopenharmony_ci			size = -ETIMEDOUT;
3368c2ecf20Sopenharmony_ci			continue;
3378c2ecf20Sopenharmony_ci		}
3388c2ecf20Sopenharmony_ci		break;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci	i2c_nuvoton_ready(chip);
3418c2ecf20Sopenharmony_ci	dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
3428c2ecf20Sopenharmony_ci	return size;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/*
3468c2ecf20Sopenharmony_ci * Send TPM command.
3478c2ecf20Sopenharmony_ci *
3488c2ecf20Sopenharmony_ci * If interrupts are used (signaled by an irq set in the vendor structure)
3498c2ecf20Sopenharmony_ci * tpm.c can skip polling for the data to be available as the interrupt is
3508c2ecf20Sopenharmony_ci * waited for here
3518c2ecf20Sopenharmony_ci */
3528c2ecf20Sopenharmony_cistatic int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct priv_data *priv = dev_get_drvdata(&chip->dev);
3558c2ecf20Sopenharmony_ci	struct device *dev = chip->dev.parent;
3568c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
3578c2ecf20Sopenharmony_ci	u32 ordinal;
3588c2ecf20Sopenharmony_ci	unsigned long duration;
3598c2ecf20Sopenharmony_ci	size_t count = 0;
3608c2ecf20Sopenharmony_ci	int burst_count, bytes2write, retries, rc = -EIO;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	for (retries = 0; retries < TPM_RETRY; retries++) {
3638c2ecf20Sopenharmony_ci		i2c_nuvoton_ready(chip);
3648c2ecf20Sopenharmony_ci		if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
3658c2ecf20Sopenharmony_ci					      TPM_STS_COMMAND_READY,
3668c2ecf20Sopenharmony_ci					      chip->timeout_b, NULL)) {
3678c2ecf20Sopenharmony_ci			dev_err(dev, "%s() timeout on commandReady\n",
3688c2ecf20Sopenharmony_ci				__func__);
3698c2ecf20Sopenharmony_ci			rc = -EIO;
3708c2ecf20Sopenharmony_ci			continue;
3718c2ecf20Sopenharmony_ci		}
3728c2ecf20Sopenharmony_ci		rc = 0;
3738c2ecf20Sopenharmony_ci		while (count < len - 1) {
3748c2ecf20Sopenharmony_ci			burst_count = i2c_nuvoton_get_burstcount(client,
3758c2ecf20Sopenharmony_ci								 chip);
3768c2ecf20Sopenharmony_ci			if (burst_count < 0) {
3778c2ecf20Sopenharmony_ci				dev_err(dev, "%s() fail get burstCount\n",
3788c2ecf20Sopenharmony_ci					__func__);
3798c2ecf20Sopenharmony_ci				rc = -EIO;
3808c2ecf20Sopenharmony_ci				break;
3818c2ecf20Sopenharmony_ci			}
3828c2ecf20Sopenharmony_ci			bytes2write = min_t(size_t, burst_count,
3838c2ecf20Sopenharmony_ci					    len - 1 - count);
3848c2ecf20Sopenharmony_ci			rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
3858c2ecf20Sopenharmony_ci						   bytes2write, &buf[count]);
3868c2ecf20Sopenharmony_ci			if (rc < 0) {
3878c2ecf20Sopenharmony_ci				dev_err(dev, "%s() fail i2cWriteBuf\n",
3888c2ecf20Sopenharmony_ci					__func__);
3898c2ecf20Sopenharmony_ci				break;
3908c2ecf20Sopenharmony_ci			}
3918c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s(%d):", __func__, bytes2write);
3928c2ecf20Sopenharmony_ci			count += bytes2write;
3938c2ecf20Sopenharmony_ci			rc = i2c_nuvoton_wait_for_stat(chip,
3948c2ecf20Sopenharmony_ci						       TPM_STS_VALID |
3958c2ecf20Sopenharmony_ci						       TPM_STS_EXPECT,
3968c2ecf20Sopenharmony_ci						       TPM_STS_VALID |
3978c2ecf20Sopenharmony_ci						       TPM_STS_EXPECT,
3988c2ecf20Sopenharmony_ci						       chip->timeout_c,
3998c2ecf20Sopenharmony_ci						       NULL);
4008c2ecf20Sopenharmony_ci			if (rc < 0) {
4018c2ecf20Sopenharmony_ci				dev_err(dev, "%s() timeout on Expect\n",
4028c2ecf20Sopenharmony_ci					__func__);
4038c2ecf20Sopenharmony_ci				rc = -ETIMEDOUT;
4048c2ecf20Sopenharmony_ci				break;
4058c2ecf20Sopenharmony_ci			}
4068c2ecf20Sopenharmony_ci		}
4078c2ecf20Sopenharmony_ci		if (rc < 0)
4088c2ecf20Sopenharmony_ci			continue;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci		/* write last byte */
4118c2ecf20Sopenharmony_ci		rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
4128c2ecf20Sopenharmony_ci					   &buf[count]);
4138c2ecf20Sopenharmony_ci		if (rc < 0) {
4148c2ecf20Sopenharmony_ci			dev_err(dev, "%s() fail to write last byte\n",
4158c2ecf20Sopenharmony_ci				__func__);
4168c2ecf20Sopenharmony_ci			rc = -EIO;
4178c2ecf20Sopenharmony_ci			continue;
4188c2ecf20Sopenharmony_ci		}
4198c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
4208c2ecf20Sopenharmony_ci		rc = i2c_nuvoton_wait_for_stat(chip,
4218c2ecf20Sopenharmony_ci					       TPM_STS_VALID | TPM_STS_EXPECT,
4228c2ecf20Sopenharmony_ci					       TPM_STS_VALID,
4238c2ecf20Sopenharmony_ci					       chip->timeout_c, NULL);
4248c2ecf20Sopenharmony_ci		if (rc) {
4258c2ecf20Sopenharmony_ci			dev_err(dev, "%s() timeout on Expect to clear\n",
4268c2ecf20Sopenharmony_ci				__func__);
4278c2ecf20Sopenharmony_ci			rc = -ETIMEDOUT;
4288c2ecf20Sopenharmony_ci			continue;
4298c2ecf20Sopenharmony_ci		}
4308c2ecf20Sopenharmony_ci		break;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci	if (rc < 0) {
4338c2ecf20Sopenharmony_ci		/* retries == TPM_RETRY */
4348c2ecf20Sopenharmony_ci		i2c_nuvoton_ready(chip);
4358c2ecf20Sopenharmony_ci		return rc;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci	/* execute the TPM command */
4388c2ecf20Sopenharmony_ci	rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
4398c2ecf20Sopenharmony_ci	if (rc < 0) {
4408c2ecf20Sopenharmony_ci		dev_err(dev, "%s() fail to write Go\n", __func__);
4418c2ecf20Sopenharmony_ci		i2c_nuvoton_ready(chip);
4428c2ecf20Sopenharmony_ci		return rc;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
4458c2ecf20Sopenharmony_ci	duration = tpm_calc_ordinal_duration(chip, ordinal);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	rc = i2c_nuvoton_wait_for_data_avail(chip, duration, &priv->read_queue);
4488c2ecf20Sopenharmony_ci	if (rc) {
4498c2ecf20Sopenharmony_ci		dev_err(dev, "%s() timeout command duration %ld\n",
4508c2ecf20Sopenharmony_ci			__func__, duration);
4518c2ecf20Sopenharmony_ci		i2c_nuvoton_ready(chip);
4528c2ecf20Sopenharmony_ci		return rc;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s() -> %zd\n", __func__, len);
4568c2ecf20Sopenharmony_ci	return 0;
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	return (status == TPM_STS_COMMAND_READY);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic const struct tpm_class_ops tpm_i2c = {
4658c2ecf20Sopenharmony_ci	.flags = TPM_OPS_AUTO_STARTUP,
4668c2ecf20Sopenharmony_ci	.status = i2c_nuvoton_read_status,
4678c2ecf20Sopenharmony_ci	.recv = i2c_nuvoton_recv,
4688c2ecf20Sopenharmony_ci	.send = i2c_nuvoton_send,
4698c2ecf20Sopenharmony_ci	.cancel = i2c_nuvoton_ready,
4708c2ecf20Sopenharmony_ci	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
4718c2ecf20Sopenharmony_ci	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
4728c2ecf20Sopenharmony_ci	.req_canceled = i2c_nuvoton_req_canceled,
4738c2ecf20Sopenharmony_ci};
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci/* The only purpose for the handler is to signal to any waiting threads that
4768c2ecf20Sopenharmony_ci * the interrupt is currently being asserted. The driver does not do any
4778c2ecf20Sopenharmony_ci * processing triggered by interrupts, and the chip provides no way to mask at
4788c2ecf20Sopenharmony_ci * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
4798c2ecf20Sopenharmony_ci * this means it cannot be shared. */
4808c2ecf20Sopenharmony_cistatic irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	struct tpm_chip *chip = dev_id;
4838c2ecf20Sopenharmony_ci	struct priv_data *priv = dev_get_drvdata(&chip->dev);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	priv->intrs++;
4868c2ecf20Sopenharmony_ci	wake_up(&priv->read_queue);
4878c2ecf20Sopenharmony_ci	disable_irq_nosync(priv->irq);
4888c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_cistatic int get_vid(struct i2c_client *client, u32 *res)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
4948c2ecf20Sopenharmony_ci	u32 temp;
4958c2ecf20Sopenharmony_ci	s32 rc;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
4988c2ecf20Sopenharmony_ci		return -ENODEV;
4998c2ecf20Sopenharmony_ci	rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
5008c2ecf20Sopenharmony_ci	if (rc < 0)
5018c2ecf20Sopenharmony_ci		return rc;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	/* check WPCT301 values - ignore RID */
5048c2ecf20Sopenharmony_ci	if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
5058c2ecf20Sopenharmony_ci		/*
5068c2ecf20Sopenharmony_ci		 * f/w rev 2.81 has an issue where the VID_DID_RID is not
5078c2ecf20Sopenharmony_ci		 * reporting the right value. so give it another chance at
5088c2ecf20Sopenharmony_ci		 * offset 0x20 (FIFO_W).
5098c2ecf20Sopenharmony_ci		 */
5108c2ecf20Sopenharmony_ci		rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
5118c2ecf20Sopenharmony_ci					  (u8 *) (&temp));
5128c2ecf20Sopenharmony_ci		if (rc < 0)
5138c2ecf20Sopenharmony_ci			return rc;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci		/* check WPCT301 values - ignore RID */
5168c2ecf20Sopenharmony_ci		if (memcmp(&temp, vid_did_rid_value,
5178c2ecf20Sopenharmony_ci			   sizeof(vid_did_rid_value)))
5188c2ecf20Sopenharmony_ci			return -ENODEV;
5198c2ecf20Sopenharmony_ci	}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	*res = temp;
5228c2ecf20Sopenharmony_ci	return 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int i2c_nuvoton_probe(struct i2c_client *client,
5268c2ecf20Sopenharmony_ci			     const struct i2c_device_id *id)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	int rc;
5298c2ecf20Sopenharmony_ci	struct tpm_chip *chip;
5308c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
5318c2ecf20Sopenharmony_ci	struct priv_data *priv;
5328c2ecf20Sopenharmony_ci	u32 vid = 0;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	rc = get_vid(client, &vid);
5358c2ecf20Sopenharmony_ci	if (rc)
5368c2ecf20Sopenharmony_ci		return rc;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
5398c2ecf20Sopenharmony_ci		 (u8) (vid >> 16), (u8) (vid >> 24));
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	chip = tpmm_chip_alloc(dev, &tpm_i2c);
5428c2ecf20Sopenharmony_ci	if (IS_ERR(chip))
5438c2ecf20Sopenharmony_ci		return PTR_ERR(chip);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
5468c2ecf20Sopenharmony_ci	if (!priv)
5478c2ecf20Sopenharmony_ci		return -ENOMEM;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (dev->of_node) {
5508c2ecf20Sopenharmony_ci		const struct of_device_id *of_id;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		of_id = of_match_device(dev->driver->of_match_table, dev);
5538c2ecf20Sopenharmony_ci		if (of_id && of_id->data == OF_IS_TPM2)
5548c2ecf20Sopenharmony_ci			chip->flags |= TPM_CHIP_FLAG_TPM2;
5558c2ecf20Sopenharmony_ci	} else
5568c2ecf20Sopenharmony_ci		if (id->driver_data == I2C_IS_TPM2)
5578c2ecf20Sopenharmony_ci			chip->flags |= TPM_CHIP_FLAG_TPM2;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	init_waitqueue_head(&priv->read_queue);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	/* Default timeouts */
5628c2ecf20Sopenharmony_ci	chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
5638c2ecf20Sopenharmony_ci	chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
5648c2ecf20Sopenharmony_ci	chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
5658c2ecf20Sopenharmony_ci	chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	dev_set_drvdata(&chip->dev, priv);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/*
5708c2ecf20Sopenharmony_ci	 * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
5718c2ecf20Sopenharmony_ci	 *   TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
5728c2ecf20Sopenharmony_ci	 * The IRQ should be set in the i2c_board_info (which is done
5738c2ecf20Sopenharmony_ci	 * automatically in of_i2c_register_devices, for device tree users */
5748c2ecf20Sopenharmony_ci	priv->irq = client->irq;
5758c2ecf20Sopenharmony_ci	if (client->irq) {
5768c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s() priv->irq\n", __func__);
5778c2ecf20Sopenharmony_ci		rc = devm_request_irq(dev, client->irq,
5788c2ecf20Sopenharmony_ci				      i2c_nuvoton_int_handler,
5798c2ecf20Sopenharmony_ci				      IRQF_TRIGGER_LOW,
5808c2ecf20Sopenharmony_ci				      dev_name(&chip->dev),
5818c2ecf20Sopenharmony_ci				      chip);
5828c2ecf20Sopenharmony_ci		if (rc) {
5838c2ecf20Sopenharmony_ci			dev_err(dev, "%s() Unable to request irq: %d for use\n",
5848c2ecf20Sopenharmony_ci				__func__, priv->irq);
5858c2ecf20Sopenharmony_ci			priv->irq = 0;
5868c2ecf20Sopenharmony_ci		} else {
5878c2ecf20Sopenharmony_ci			chip->flags |= TPM_CHIP_FLAG_IRQ;
5888c2ecf20Sopenharmony_ci			/* Clear any pending interrupt */
5898c2ecf20Sopenharmony_ci			i2c_nuvoton_ready(chip);
5908c2ecf20Sopenharmony_ci			/* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
5918c2ecf20Sopenharmony_ci			rc = i2c_nuvoton_wait_for_stat(chip,
5928c2ecf20Sopenharmony_ci						       TPM_STS_COMMAND_READY,
5938c2ecf20Sopenharmony_ci						       TPM_STS_COMMAND_READY,
5948c2ecf20Sopenharmony_ci						       chip->timeout_b,
5958c2ecf20Sopenharmony_ci						       NULL);
5968c2ecf20Sopenharmony_ci			if (rc == 0) {
5978c2ecf20Sopenharmony_ci				/*
5988c2ecf20Sopenharmony_ci				 * TIS is in ready state
5998c2ecf20Sopenharmony_ci				 * write dummy byte to enter reception state
6008c2ecf20Sopenharmony_ci				 * TPM_DATA_FIFO_W <- rc (0)
6018c2ecf20Sopenharmony_ci				 */
6028c2ecf20Sopenharmony_ci				rc = i2c_nuvoton_write_buf(client,
6038c2ecf20Sopenharmony_ci							   TPM_DATA_FIFO_W,
6048c2ecf20Sopenharmony_ci							   1, (u8 *) (&rc));
6058c2ecf20Sopenharmony_ci				if (rc < 0)
6068c2ecf20Sopenharmony_ci					return rc;
6078c2ecf20Sopenharmony_ci				/* TPM_STS <- 0x40 (commandReady) */
6088c2ecf20Sopenharmony_ci				i2c_nuvoton_ready(chip);
6098c2ecf20Sopenharmony_ci			} else {
6108c2ecf20Sopenharmony_ci				/*
6118c2ecf20Sopenharmony_ci				 * timeout_b reached - command was
6128c2ecf20Sopenharmony_ci				 * aborted. TIS should now be in idle state -
6138c2ecf20Sopenharmony_ci				 * only TPM_STS_VALID should be set
6148c2ecf20Sopenharmony_ci				 */
6158c2ecf20Sopenharmony_ci				if (i2c_nuvoton_read_status(chip) !=
6168c2ecf20Sopenharmony_ci				    TPM_STS_VALID)
6178c2ecf20Sopenharmony_ci					return -EIO;
6188c2ecf20Sopenharmony_ci			}
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	return tpm_chip_register(chip);
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic int i2c_nuvoton_remove(struct i2c_client *client)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct tpm_chip *chip = i2c_get_clientdata(client);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	tpm_chip_unregister(chip);
6308c2ecf20Sopenharmony_ci	return 0;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic const struct i2c_device_id i2c_nuvoton_id[] = {
6348c2ecf20Sopenharmony_ci	{"tpm_i2c_nuvoton"},
6358c2ecf20Sopenharmony_ci	{"tpm2_i2c_nuvoton", .driver_data = I2C_IS_TPM2},
6368c2ecf20Sopenharmony_ci	{}
6378c2ecf20Sopenharmony_ci};
6388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
6418c2ecf20Sopenharmony_cistatic const struct of_device_id i2c_nuvoton_of_match[] = {
6428c2ecf20Sopenharmony_ci	{.compatible = "nuvoton,npct501"},
6438c2ecf20Sopenharmony_ci	{.compatible = "winbond,wpct301"},
6448c2ecf20Sopenharmony_ci	{.compatible = "nuvoton,npct601", .data = OF_IS_TPM2},
6458c2ecf20Sopenharmony_ci	{},
6468c2ecf20Sopenharmony_ci};
6478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
6488c2ecf20Sopenharmony_ci#endif
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic struct i2c_driver i2c_nuvoton_driver = {
6538c2ecf20Sopenharmony_ci	.id_table = i2c_nuvoton_id,
6548c2ecf20Sopenharmony_ci	.probe = i2c_nuvoton_probe,
6558c2ecf20Sopenharmony_ci	.remove = i2c_nuvoton_remove,
6568c2ecf20Sopenharmony_ci	.driver = {
6578c2ecf20Sopenharmony_ci		.name = "tpm_i2c_nuvoton",
6588c2ecf20Sopenharmony_ci		.pm = &i2c_nuvoton_pm_ops,
6598c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(i2c_nuvoton_of_match),
6608c2ecf20Sopenharmony_ci	},
6618c2ecf20Sopenharmony_ci};
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cimodule_i2c_driver(i2c_nuvoton_driver);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
6668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
6678c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
668