1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/i2c.h>
21#include <linux/gpio.h>
22#include <linux/of_irq.h>
23#include <linux/of_gpio.h>
24#include <linux/tpm.h>
25#include <linux/platform_data/st33zp24.h>
26
27#include "st33zp24.h"
28
29#define TPM_DUMMY_BYTE 0xAA
30
31struct st33zp24_i2c_phy {
32 struct i2c_client *client;
33 u8 buf[TPM_BUFSIZE + 1];
34 int io_lpcpd;
35};
36
37
38
39
40
41
42
43
44
45static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
46{
47 struct st33zp24_i2c_phy *phy = phy_id;
48
49 phy->buf[0] = tpm_register;
50 memcpy(phy->buf + 1, tpm_data, tpm_size);
51 return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
52}
53
54
55
56
57
58
59
60
61
62static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
63{
64 struct st33zp24_i2c_phy *phy = phy_id;
65 u8 status = 0;
66 u8 data;
67
68 data = TPM_DUMMY_BYTE;
69 status = write8_reg(phy, tpm_register, &data, 1);
70 if (status == 2)
71 status = i2c_master_recv(phy->client, tpm_data, tpm_size);
72 return status;
73}
74
75
76
77
78
79
80
81
82
83
84static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
85 int tpm_size)
86{
87 return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
88 tpm_size);
89}
90
91
92
93
94
95
96
97
98
99
100static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
101 int tpm_size)
102{
103 return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
104}
105
106static const struct st33zp24_phy_ops i2c_phy_ops = {
107 .send = st33zp24_i2c_send,
108 .recv = st33zp24_i2c_recv,
109};
110
111#ifdef CONFIG_OF
112static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy)
113{
114 struct device_node *pp;
115 struct i2c_client *client = phy->client;
116 int gpio;
117 int ret;
118
119 pp = client->dev.of_node;
120 if (!pp) {
121 dev_err(&client->dev, "No platform data\n");
122 return -ENODEV;
123 }
124
125
126 gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
127 if (gpio < 0) {
128 dev_err(&client->dev,
129 "Failed to retrieve lpcpd-gpios from dts.\n");
130 phy->io_lpcpd = -1;
131
132
133
134
135
136 return 0;
137 }
138
139 ret = devm_gpio_request_one(&client->dev, gpio,
140 GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
141 if (ret) {
142 dev_err(&client->dev, "Failed to request lpcpd pin\n");
143 return -ENODEV;
144 }
145 phy->io_lpcpd = gpio;
146
147 return 0;
148}
149#else
150static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy *phy)
151{
152 return -ENODEV;
153}
154#endif
155
156static int st33zp24_i2c_request_resources(struct i2c_client *client,
157 struct st33zp24_i2c_phy *phy)
158{
159 struct st33zp24_platform_data *pdata;
160 int ret;
161
162 pdata = client->dev.platform_data;
163 if (!pdata) {
164 dev_err(&client->dev, "No platform data\n");
165 return -ENODEV;
166 }
167
168
169 phy->io_lpcpd = pdata->io_lpcpd;
170
171 if (gpio_is_valid(pdata->io_lpcpd)) {
172 ret = devm_gpio_request_one(&client->dev,
173 pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
174 "TPM IO_LPCPD");
175 if (ret) {
176 dev_err(&client->dev, "Failed to request lpcpd pin\n");
177 return ret;
178 }
179 }
180
181 return 0;
182}
183
184
185
186
187
188
189
190
191static int st33zp24_i2c_probe(struct i2c_client *client,
192 const struct i2c_device_id *id)
193{
194 int ret;
195 struct st33zp24_platform_data *pdata;
196 struct st33zp24_i2c_phy *phy;
197
198 if (!client) {
199 pr_info("%s: i2c client is NULL. Device not accessible.\n",
200 __func__);
201 return -ENODEV;
202 }
203
204 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
205 dev_info(&client->dev, "client not i2c capable\n");
206 return -ENODEV;
207 }
208
209 phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
210 GFP_KERNEL);
211 if (!phy)
212 return -ENOMEM;
213
214 phy->client = client;
215 pdata = client->dev.platform_data;
216 if (!pdata && client->dev.of_node) {
217 ret = st33zp24_i2c_of_request_resources(phy);
218 if (ret)
219 return ret;
220 } else if (pdata) {
221 ret = st33zp24_i2c_request_resources(client, phy);
222 if (ret)
223 return ret;
224 }
225
226 return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
227 phy->io_lpcpd);
228}
229
230
231
232
233
234
235static int st33zp24_i2c_remove(struct i2c_client *client)
236{
237 struct tpm_chip *chip = i2c_get_clientdata(client);
238
239 return st33zp24_remove(chip);
240}
241
242static const struct i2c_device_id st33zp24_i2c_id[] = {
243 {TPM_ST33_I2C, 0},
244 {}
245};
246MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
247
248#ifdef CONFIG_OF
249static const struct of_device_id of_st33zp24_i2c_match[] = {
250 { .compatible = "st,st33zp24-i2c", },
251 {}
252};
253MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
254#endif
255
256static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
257 st33zp24_pm_resume);
258
259static struct i2c_driver st33zp24_i2c_driver = {
260 .driver = {
261 .name = TPM_ST33_I2C,
262 .pm = &st33zp24_i2c_ops,
263 .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
264 },
265 .probe = st33zp24_i2c_probe,
266 .remove = st33zp24_i2c_remove,
267 .id_table = st33zp24_i2c_id
268};
269
270module_i2c_driver(st33zp24_i2c_driver);
271
272MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
273MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
274MODULE_VERSION("1.3.0");
275MODULE_LICENSE("GPL");
276