xref: /linux/drivers/char/tpm/st33zp24/i2c.c (revision 2da68a77)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
4  * Copyright (C) 2009 - 2016 STMicroelectronics
5  */
6 
7 #include <linux/module.h>
8 #include <linux/i2c.h>
9 #include <linux/of.h>
10 #include <linux/acpi.h>
11 #include <linux/tpm.h>
12 
13 #include "../tpm.h"
14 #include "st33zp24.h"
15 
16 #define TPM_DUMMY_BYTE			0xAA
17 
18 struct st33zp24_i2c_phy {
19 	struct i2c_client *client;
20 	u8 buf[ST33ZP24_BUFSIZE + 1];
21 };
22 
23 /*
24  * write8_reg
25  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
26  * @param: tpm_register, the tpm tis register where the data should be written
27  * @param: tpm_data, the tpm_data to write inside the tpm_register
28  * @param: tpm_size, The length of the data
29  * @return: Returns negative errno, or else the number of bytes written.
30  */
31 static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
32 {
33 	struct st33zp24_i2c_phy *phy = phy_id;
34 
35 	phy->buf[0] = tpm_register;
36 	memcpy(phy->buf + 1, tpm_data, tpm_size);
37 	return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
38 } /* write8_reg() */
39 
40 /*
41  * read8_reg
42  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
43  * @param: tpm_register, the tpm tis register where the data should be read
44  * @param: tpm_data, the TPM response
45  * @param: tpm_size, tpm TPM response size to read.
46  * @return: number of byte read successfully: should be one if success.
47  */
48 static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
49 {
50 	struct st33zp24_i2c_phy *phy = phy_id;
51 	u8 status = 0;
52 	u8 data;
53 
54 	data = TPM_DUMMY_BYTE;
55 	status = write8_reg(phy, tpm_register, &data, 1);
56 	if (status == 2)
57 		status = i2c_master_recv(phy->client, tpm_data, tpm_size);
58 	return status;
59 } /* read8_reg() */
60 
61 /*
62  * st33zp24_i2c_send
63  * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
64  * @param: phy_id, the phy description
65  * @param: tpm_register, the tpm tis register where the data should be written
66  * @param: tpm_data, the tpm_data to write inside the tpm_register
67  * @param: tpm_size, the length of the data
68  * @return: number of byte written successfully: should be one if success.
69  */
70 static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
71 			     int tpm_size)
72 {
73 	return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
74 			  tpm_size);
75 }
76 
77 /*
78  * st33zp24_i2c_recv
79  * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
80  * @param: phy_id, the phy description
81  * @param: tpm_register, the tpm tis register where the data should be read
82  * @param: tpm_data, the TPM response
83  * @param: tpm_size, tpm TPM response size to read.
84  * @return: number of byte read successfully: should be one if success.
85  */
86 static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
87 			     int tpm_size)
88 {
89 	return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
90 }
91 
92 static const struct st33zp24_phy_ops i2c_phy_ops = {
93 	.send = st33zp24_i2c_send,
94 	.recv = st33zp24_i2c_recv,
95 };
96 
97 /*
98  * st33zp24_i2c_probe initialize the TPM device
99  * @param: client, the i2c_client description (TPM I2C description).
100  * @param: id, the i2c_device_id struct.
101  * @return: 0 in case of success.
102  *	 -1 in other case.
103  */
104 static int st33zp24_i2c_probe(struct i2c_client *client,
105 			      const struct i2c_device_id *id)
106 {
107 	struct st33zp24_i2c_phy *phy;
108 
109 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
110 		dev_info(&client->dev, "client not i2c capable\n");
111 		return -ENODEV;
112 	}
113 
114 	phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
115 			   GFP_KERNEL);
116 	if (!phy)
117 		return -ENOMEM;
118 
119 	phy->client = client;
120 
121 	return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq);
122 }
123 
124 /*
125  * st33zp24_i2c_remove remove the TPM device
126  * @param: client, the i2c_client description (TPM I2C description).
127  * @return: 0 in case of success.
128  */
129 static void st33zp24_i2c_remove(struct i2c_client *client)
130 {
131 	struct tpm_chip *chip = i2c_get_clientdata(client);
132 
133 	st33zp24_remove(chip);
134 }
135 
136 static const struct i2c_device_id st33zp24_i2c_id[] = {
137 	{TPM_ST33_I2C, 0},
138 	{}
139 };
140 MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
141 
142 static const struct of_device_id of_st33zp24_i2c_match[] = {
143 	{ .compatible = "st,st33zp24-i2c", },
144 	{}
145 };
146 MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
147 
148 static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
149 	{"SMO3324"},
150 	{}
151 };
152 MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
153 
154 static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
155 			 st33zp24_pm_resume);
156 
157 static struct i2c_driver st33zp24_i2c_driver = {
158 	.driver = {
159 		.name = TPM_ST33_I2C,
160 		.pm = &st33zp24_i2c_ops,
161 		.of_match_table = of_match_ptr(of_st33zp24_i2c_match),
162 		.acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
163 	},
164 	.probe = st33zp24_i2c_probe,
165 	.remove = st33zp24_i2c_remove,
166 	.id_table = st33zp24_i2c_id
167 };
168 
169 module_i2c_driver(st33zp24_i2c_driver);
170 
171 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
172 MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
173 MODULE_VERSION("1.3.0");
174 MODULE_LICENSE("GPL");
175