i2c.c 9.0 KB

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