2 * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009 - 2015 STMicroelectronics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
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>
29 #define TPM_DUMMY_BYTE 0xAA
31 struct st33zp24_i2c_phy
{
32 struct i2c_client
*client
;
33 u8 buf
[TPM_BUFSIZE
+ 1];
39 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
40 * @param: tpm_register, the tpm tis register where the data should be written
41 * @param: tpm_data, the tpm_data to write inside the tpm_register
42 * @param: tpm_size, The length of the data
43 * @return: Returns negative errno, or else the number of bytes written.
45 static int write8_reg(void *phy_id
, u8 tpm_register
, u8
*tpm_data
, int tpm_size
)
47 struct st33zp24_i2c_phy
*phy
= phy_id
;
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);
56 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
57 * @param: tpm_register, the tpm tis register where the data should be read
58 * @param: tpm_data, the TPM response
59 * @param: tpm_size, tpm TPM response size to read.
60 * @return: number of byte read successfully: should be one if success.
62 static int read8_reg(void *phy_id
, u8 tpm_register
, u8
*tpm_data
, int tpm_size
)
64 struct st33zp24_i2c_phy
*phy
= phy_id
;
68 data
= TPM_DUMMY_BYTE
;
69 status
= write8_reg(phy
, tpm_register
, &data
, 1);
71 status
= i2c_master_recv(phy
->client
, tpm_data
, tpm_size
);
77 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
78 * @param: phy_id, the phy description
79 * @param: tpm_register, the tpm tis register where the data should be written
80 * @param: tpm_data, the tpm_data to write inside the tpm_register
81 * @param: tpm_size, the length of the data
82 * @return: number of byte written successfully: should be one if success.
84 static int st33zp24_i2c_send(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
87 return write8_reg(phy_id
, tpm_register
| TPM_WRITE_DIRECTION
, tpm_data
,
93 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
94 * @param: phy_id, the phy description
95 * @param: tpm_register, the tpm tis register where the data should be read
96 * @param: tpm_data, the TPM response
97 * @param: tpm_size, tpm TPM response size to read.
98 * @return: number of byte read successfully: should be one if success.
100 static int st33zp24_i2c_recv(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
103 return read8_reg(phy_id
, tpm_register
, tpm_data
, tpm_size
);
106 static const struct st33zp24_phy_ops i2c_phy_ops
= {
107 .send
= st33zp24_i2c_send
,
108 .recv
= st33zp24_i2c_recv
,
112 static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy
*phy
)
114 struct device_node
*pp
;
115 struct i2c_client
*client
= phy
->client
;
119 pp
= client
->dev
.of_node
;
121 dev_err(&client
->dev
, "No platform data\n");
125 /* Get GPIO from device tree */
126 gpio
= of_get_named_gpio(pp
, "lpcpd-gpios", 0);
128 dev_err(&client
->dev
,
129 "Failed to retrieve lpcpd-gpios from dts.\n");
132 * lpcpd pin is not specified. This is not an issue as
133 * power management can be also managed by TPM specific
134 * commands. So leave with a success status code.
138 /* GPIO request and configuration */
139 ret
= devm_gpio_request_one(&client
->dev
, gpio
,
140 GPIOF_OUT_INIT_HIGH
, "TPM IO LPCPD");
142 dev_err(&client
->dev
, "Failed to request lpcpd pin\n");
145 phy
->io_lpcpd
= gpio
;
150 static int st33zp24_i2c_of_request_resources(struct st33zp24_i2c_phy
*phy
)
156 static int st33zp24_i2c_request_resources(struct i2c_client
*client
,
157 struct st33zp24_i2c_phy
*phy
)
159 struct st33zp24_platform_data
*pdata
;
162 pdata
= client
->dev
.platform_data
;
164 dev_err(&client
->dev
, "No platform data\n");
168 /* store for late use */
169 phy
->io_lpcpd
= pdata
->io_lpcpd
;
171 if (gpio_is_valid(pdata
->io_lpcpd
)) {
172 ret
= devm_gpio_request_one(&client
->dev
,
173 pdata
->io_lpcpd
, GPIOF_OUT_INIT_HIGH
,
176 dev_err(&client
->dev
, "Failed to request lpcpd pin\n");
185 * st33zp24_i2c_probe initialize the TPM device
186 * @param: client, the i2c_client drescription (TPM I2C description).
187 * @param: id, the i2c_device_id struct.
188 * @return: 0 in case of success.
191 static int st33zp24_i2c_probe(struct i2c_client
*client
,
192 const struct i2c_device_id
*id
)
195 struct st33zp24_platform_data
*pdata
;
196 struct st33zp24_i2c_phy
*phy
;
199 pr_info("%s: i2c client is NULL. Device not accessible.\n",
204 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
205 dev_info(&client
->dev
, "client not i2c capable\n");
209 phy
= devm_kzalloc(&client
->dev
, sizeof(struct st33zp24_i2c_phy
),
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
);
221 ret
= st33zp24_i2c_request_resources(client
, phy
);
226 return st33zp24_probe(phy
, &i2c_phy_ops
, &client
->dev
, client
->irq
,
231 * st33zp24_i2c_remove remove the TPM device
232 * @param: client, the i2c_client description (TPM I2C description).
233 * @return: 0 in case of success.
235 static int st33zp24_i2c_remove(struct i2c_client
*client
)
237 struct tpm_chip
*chip
= i2c_get_clientdata(client
);
239 return st33zp24_remove(chip
);
242 static const struct i2c_device_id st33zp24_i2c_id
[] = {
246 MODULE_DEVICE_TABLE(i2c
, st33zp24_i2c_id
);
249 static const struct of_device_id of_st33zp24_i2c_match
[] = {
250 { .compatible
= "st,st33zp24-i2c", },
253 MODULE_DEVICE_TABLE(of
, of_st33zp24_i2c_match
);
256 static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops
, st33zp24_pm_suspend
,
259 static struct i2c_driver st33zp24_i2c_driver
= {
261 .name
= TPM_ST33_I2C
,
262 .pm
= &st33zp24_i2c_ops
,
263 .of_match_table
= of_match_ptr(of_st33zp24_i2c_match
),
265 .probe
= st33zp24_i2c_probe
,
266 .remove
= st33zp24_i2c_remove
,
267 .id_table
= st33zp24_i2c_id
270 module_i2c_driver(st33zp24_i2c_driver
);
272 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
273 MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
274 MODULE_VERSION("1.3.0");
275 MODULE_LICENSE("GPL");