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