WIP FPC-III support
[linux/fpc-iii.git] / drivers / char / tpm / st33zp24 / i2c.c
blob7c617edff4ca2968ef24fc2ae457233fa433e511
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 */
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>
17 #include "../tpm.h"
18 #include "st33zp24.h"
20 #define TPM_DUMMY_BYTE 0xAA
22 struct st33zp24_i2c_phy {
23 struct i2c_client *client;
24 u8 buf[ST33ZP24_BUFSIZE + 1];
25 int io_lpcpd;
29 * write8_reg
30 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
31 * @param: tpm_register, the tpm tis register where the data should be written
32 * @param: tpm_data, the tpm_data to write inside the tpm_register
33 * @param: tpm_size, The length of the data
34 * @return: Returns negative errno, or else the number of bytes written.
36 static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
38 struct st33zp24_i2c_phy *phy = phy_id;
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 } /* write8_reg() */
46 * read8_reg
47 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
48 * @param: tpm_register, the tpm tis register where the data should be read
49 * @param: tpm_data, the TPM response
50 * @param: tpm_size, tpm TPM response size to read.
51 * @return: number of byte read successfully: should be one if success.
53 static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
55 struct st33zp24_i2c_phy *phy = phy_id;
56 u8 status = 0;
57 u8 data;
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 } /* read8_reg() */
67 * st33zp24_i2c_send
68 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
69 * @param: phy_id, the phy description
70 * @param: tpm_register, the tpm tis register where the data should be written
71 * @param: tpm_data, the tpm_data to write inside the tpm_register
72 * @param: tpm_size, the length of the data
73 * @return: number of byte written successfully: should be one if success.
75 static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
76 int tpm_size)
78 return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
79 tpm_size);
83 * st33zp24_i2c_recv
84 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
85 * @param: phy_id, the phy description
86 * @param: tpm_register, the tpm tis register where the data should be read
87 * @param: tpm_data, the TPM response
88 * @param: tpm_size, tpm TPM response size to read.
89 * @return: number of byte read successfully: should be one if success.
91 static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
92 int tpm_size)
94 return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
97 static const struct st33zp24_phy_ops i2c_phy_ops = {
98 .send = st33zp24_i2c_send,
99 .recv = st33zp24_i2c_recv,
102 static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
104 static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
105 { "lpcpd-gpios", &lpcpd_gpios, 1 },
109 static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
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;
118 ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
119 if (ret)
120 return ret;
122 /* Get LPCPD GPIO from ACPI */
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;
129 * lpcpd pin is not specified. This is not an issue as
130 * power management can be also managed by TPM specific
131 * commands. So leave with a success status code.
133 return 0;
136 phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
138 return 0;
141 static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
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;
150 pp = client->dev.of_node;
151 if (!pp) {
152 dev_err(&client->dev, "No platform data\n");
153 return -ENODEV;
156 /* Get GPIO from device tree */
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;
163 * lpcpd pin is not specified. This is not an issue as
164 * power management can be also managed by TPM specific
165 * commands. So leave with a success status code.
167 return 0;
169 /* GPIO request and configuration */
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;
176 phy->io_lpcpd = gpio;
178 return 0;
181 static int st33zp24_i2c_request_resources(struct i2c_client *client)
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;
189 pdata = client->dev.platform_data;
190 if (!pdata) {
191 dev_err(&client->dev, "No platform data\n");
192 return -ENODEV;
195 /* store for late use */
196 phy->io_lpcpd = pdata->io_lpcpd;
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;
208 return 0;
212 * st33zp24_i2c_probe initialize the TPM device
213 * @param: client, the i2c_client description (TPM I2C description).
214 * @param: id, the i2c_device_id struct.
215 * @return: 0 in case of success.
216 * -1 in other case.
218 static int st33zp24_i2c_probe(struct i2c_client *client,
219 const struct i2c_device_id *id)
221 int ret;
222 struct st33zp24_platform_data *pdata;
223 struct st33zp24_i2c_phy *phy;
225 if (!client) {
226 pr_info("%s: i2c client is NULL. Device not accessible.\n",
227 __func__);
228 return -ENODEV;
231 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
232 dev_info(&client->dev, "client not i2c capable\n");
233 return -ENODEV;
236 phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
237 GFP_KERNEL);
238 if (!phy)
239 return -ENOMEM;
241 phy->client = client;
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;
258 return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
259 phy->io_lpcpd);
263 * st33zp24_i2c_remove remove the TPM device
264 * @param: client, the i2c_client description (TPM I2C description).
265 * @return: 0 in case of success.
267 static int st33zp24_i2c_remove(struct i2c_client *client)
269 struct tpm_chip *chip = i2c_get_clientdata(client);
270 int ret;
272 ret = st33zp24_remove(chip);
273 if (ret)
274 return ret;
276 return 0;
279 static const struct i2c_device_id st33zp24_i2c_id[] = {
280 {TPM_ST33_I2C, 0},
283 MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
285 static const struct of_device_id of_st33zp24_i2c_match[] = {
286 { .compatible = "st,st33zp24-i2c", },
289 MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
291 static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
292 {"SMO3324"},
295 MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
297 static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
298 st33zp24_pm_resume);
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),
307 .probe = st33zp24_i2c_probe,
308 .remove = st33zp24_i2c_remove,
309 .id_table = st33zp24_i2c_id
312 module_i2c_driver(st33zp24_i2c_driver);
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");