PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/char/tpm/st33zp24/i2c.c

https://gitlab.com/deepcypher/linux
C | 314 lines | 205 code | 44 blank | 65 comment | 22 complexity | 9c7dfb4c676f66705a558f47dde6cd5f MD5 | raw file
  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. */
  6. #include <linux/module.h>
  7. #include <linux/i2c.h>
  8. #include <linux/gpio.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/of_gpio.h>
  12. #include <linux/acpi.h>
  13. #include <linux/tpm.h>
  14. #include <linux/platform_data/st33zp24.h>
  15. #include "../tpm.h"
  16. #include "st33zp24.h"
  17. #define TPM_DUMMY_BYTE 0xAA
  18. struct st33zp24_i2c_phy {
  19. struct i2c_client *client;
  20. u8 buf[ST33ZP24_BUFSIZE + 1];
  21. int io_lpcpd;
  22. };
  23. /*
  24. * write8_reg
  25. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  26. * @param: tpm_register, the tpm tis register where the data should be written
  27. * @param: tpm_data, the tpm_data to write inside the tpm_register
  28. * @param: tpm_size, The length of the data
  29. * @return: Returns negative errno, or else the number of bytes written.
  30. */
  31. static int write8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  32. {
  33. struct st33zp24_i2c_phy *phy = phy_id;
  34. phy->buf[0] = tpm_register;
  35. memcpy(phy->buf + 1, tpm_data, tpm_size);
  36. return i2c_master_send(phy->client, phy->buf, tpm_size + 1);
  37. } /* write8_reg() */
  38. /*
  39. * read8_reg
  40. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  41. * @param: tpm_register, the tpm tis register where the data should be read
  42. * @param: tpm_data, the TPM response
  43. * @param: tpm_size, tpm TPM response size to read.
  44. * @return: number of byte read successfully: should be one if success.
  45. */
  46. static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
  47. {
  48. struct st33zp24_i2c_phy *phy = phy_id;
  49. u8 status = 0;
  50. u8 data;
  51. data = TPM_DUMMY_BYTE;
  52. status = write8_reg(phy, tpm_register, &data, 1);
  53. if (status == 2)
  54. status = i2c_master_recv(phy->client, tpm_data, tpm_size);
  55. return status;
  56. } /* read8_reg() */
  57. /*
  58. * st33zp24_i2c_send
  59. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  60. * @param: phy_id, the phy description
  61. * @param: tpm_register, the tpm tis register where the data should be written
  62. * @param: tpm_data, the tpm_data to write inside the tpm_register
  63. * @param: tpm_size, the length of the data
  64. * @return: number of byte written successfully: should be one if success.
  65. */
  66. static int st33zp24_i2c_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
  67. int tpm_size)
  68. {
  69. return write8_reg(phy_id, tpm_register | TPM_WRITE_DIRECTION, tpm_data,
  70. tpm_size);
  71. }
  72. /*
  73. * st33zp24_i2c_recv
  74. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  75. * @param: phy_id, the phy description
  76. * @param: tpm_register, the tpm tis register where the data should be read
  77. * @param: tpm_data, the TPM response
  78. * @param: tpm_size, tpm TPM response size to read.
  79. * @return: number of byte read successfully: should be one if success.
  80. */
  81. static int st33zp24_i2c_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
  82. int tpm_size)
  83. {
  84. return read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
  85. }
  86. static const struct st33zp24_phy_ops i2c_phy_ops = {
  87. .send = st33zp24_i2c_send,
  88. .recv = st33zp24_i2c_recv,
  89. };
  90. static const struct acpi_gpio_params lpcpd_gpios = { 1, 0, false };
  91. static const struct acpi_gpio_mapping acpi_st33zp24_gpios[] = {
  92. { "lpcpd-gpios", &lpcpd_gpios, 1 },
  93. {},
  94. };
  95. static int st33zp24_i2c_acpi_request_resources(struct i2c_client *client)
  96. {
  97. struct tpm_chip *chip = i2c_get_clientdata(client);
  98. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  99. struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
  100. struct gpio_desc *gpiod_lpcpd;
  101. struct device *dev = &client->dev;
  102. int ret;
  103. ret = devm_acpi_dev_add_driver_gpios(dev, acpi_st33zp24_gpios);
  104. if (ret)
  105. return ret;
  106. /* Get LPCPD GPIO from ACPI */
  107. gpiod_lpcpd = devm_gpiod_get(dev, "lpcpd", GPIOD_OUT_HIGH);
  108. if (IS_ERR(gpiod_lpcpd)) {
  109. dev_err(&client->dev,
  110. "Failed to retrieve lpcpd-gpios from acpi.\n");
  111. phy->io_lpcpd = -1;
  112. /*
  113. * lpcpd pin is not specified. This is not an issue as
  114. * power management can be also managed by TPM specific
  115. * commands. So leave with a success status code.
  116. */
  117. return 0;
  118. }
  119. phy->io_lpcpd = desc_to_gpio(gpiod_lpcpd);
  120. return 0;
  121. }
  122. static int st33zp24_i2c_of_request_resources(struct i2c_client *client)
  123. {
  124. struct tpm_chip *chip = i2c_get_clientdata(client);
  125. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  126. struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
  127. struct device_node *pp;
  128. int gpio;
  129. int ret;
  130. pp = client->dev.of_node;
  131. if (!pp) {
  132. dev_err(&client->dev, "No platform data\n");
  133. return -ENODEV;
  134. }
  135. /* Get GPIO from device tree */
  136. gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
  137. if (gpio < 0) {
  138. dev_err(&client->dev,
  139. "Failed to retrieve lpcpd-gpios from dts.\n");
  140. phy->io_lpcpd = -1;
  141. /*
  142. * lpcpd pin is not specified. This is not an issue as
  143. * power management can be also managed by TPM specific
  144. * commands. So leave with a success status code.
  145. */
  146. return 0;
  147. }
  148. /* GPIO request and configuration */
  149. ret = devm_gpio_request_one(&client->dev, gpio,
  150. GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
  151. if (ret) {
  152. dev_err(&client->dev, "Failed to request lpcpd pin\n");
  153. return -ENODEV;
  154. }
  155. phy->io_lpcpd = gpio;
  156. return 0;
  157. }
  158. static int st33zp24_i2c_request_resources(struct i2c_client *client)
  159. {
  160. struct tpm_chip *chip = i2c_get_clientdata(client);
  161. struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
  162. struct st33zp24_i2c_phy *phy = tpm_dev->phy_id;
  163. struct st33zp24_platform_data *pdata;
  164. int ret;
  165. pdata = client->dev.platform_data;
  166. if (!pdata) {
  167. dev_err(&client->dev, "No platform data\n");
  168. return -ENODEV;
  169. }
  170. /* store for late use */
  171. phy->io_lpcpd = pdata->io_lpcpd;
  172. if (gpio_is_valid(pdata->io_lpcpd)) {
  173. ret = devm_gpio_request_one(&client->dev,
  174. pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
  175. "TPM IO_LPCPD");
  176. if (ret) {
  177. dev_err(&client->dev, "Failed to request lpcpd pin\n");
  178. return ret;
  179. }
  180. }
  181. return 0;
  182. }
  183. /*
  184. * st33zp24_i2c_probe initialize the TPM device
  185. * @param: client, the i2c_client description (TPM I2C description).
  186. * @param: id, the i2c_device_id struct.
  187. * @return: 0 in case of success.
  188. * -1 in other case.
  189. */
  190. static int st33zp24_i2c_probe(struct i2c_client *client,
  191. const struct i2c_device_id *id)
  192. {
  193. int ret;
  194. struct st33zp24_platform_data *pdata;
  195. struct st33zp24_i2c_phy *phy;
  196. if (!client) {
  197. pr_info("%s: i2c client is NULL. Device not accessible.\n",
  198. __func__);
  199. return -ENODEV;
  200. }
  201. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  202. dev_info(&client->dev, "client not i2c capable\n");
  203. return -ENODEV;
  204. }
  205. phy = devm_kzalloc(&client->dev, sizeof(struct st33zp24_i2c_phy),
  206. GFP_KERNEL);
  207. if (!phy)
  208. return -ENOMEM;
  209. phy->client = client;
  210. pdata = client->dev.platform_data;
  211. if (!pdata && client->dev.of_node) {
  212. ret = st33zp24_i2c_of_request_resources(client);
  213. if (ret)
  214. return ret;
  215. } else if (pdata) {
  216. ret = st33zp24_i2c_request_resources(client);
  217. if (ret)
  218. return ret;
  219. } else if (ACPI_HANDLE(&client->dev)) {
  220. ret = st33zp24_i2c_acpi_request_resources(client);
  221. if (ret)
  222. return ret;
  223. }
  224. return st33zp24_probe(phy, &i2c_phy_ops, &client->dev, client->irq,
  225. phy->io_lpcpd);
  226. }
  227. /*
  228. * st33zp24_i2c_remove remove the TPM device
  229. * @param: client, the i2c_client description (TPM I2C description).
  230. * @return: 0 in case of success.
  231. */
  232. static int st33zp24_i2c_remove(struct i2c_client *client)
  233. {
  234. struct tpm_chip *chip = i2c_get_clientdata(client);
  235. st33zp24_remove(chip);
  236. return 0;
  237. }
  238. static const struct i2c_device_id st33zp24_i2c_id[] = {
  239. {TPM_ST33_I2C, 0},
  240. {}
  241. };
  242. MODULE_DEVICE_TABLE(i2c, st33zp24_i2c_id);
  243. static const struct of_device_id of_st33zp24_i2c_match[] = {
  244. { .compatible = "st,st33zp24-i2c", },
  245. {}
  246. };
  247. MODULE_DEVICE_TABLE(of, of_st33zp24_i2c_match);
  248. static const struct acpi_device_id st33zp24_i2c_acpi_match[] = {
  249. {"SMO3324"},
  250. {}
  251. };
  252. MODULE_DEVICE_TABLE(acpi, st33zp24_i2c_acpi_match);
  253. static SIMPLE_DEV_PM_OPS(st33zp24_i2c_ops, st33zp24_pm_suspend,
  254. st33zp24_pm_resume);
  255. static struct i2c_driver st33zp24_i2c_driver = {
  256. .driver = {
  257. .name = TPM_ST33_I2C,
  258. .pm = &st33zp24_i2c_ops,
  259. .of_match_table = of_match_ptr(of_st33zp24_i2c_match),
  260. .acpi_match_table = ACPI_PTR(st33zp24_i2c_acpi_match),
  261. },
  262. .probe = st33zp24_i2c_probe,
  263. .remove = st33zp24_i2c_remove,
  264. .id_table = st33zp24_i2c_id
  265. };
  266. module_i2c_driver(st33zp24_i2c_driver);
  267. MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
  268. MODULE_DESCRIPTION("STM TPM 1.2 I2C ST33 Driver");
  269. MODULE_VERSION("1.3.0");
  270. MODULE_LICENSE("GPL");