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