atmel-sha204a.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip / Atmel SHA204A (I2C) driver.
  4. *
  5. * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/errno.h>
  11. #include <linux/i2c.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include "atmel-i2c.h"
  19. static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
  20. void *areq, int status)
  21. {
  22. struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
  23. struct hwrng *rng = areq;
  24. if (status)
  25. dev_warn_ratelimited(&i2c_priv->client->dev,
  26. "i2c transaction failed (%d)\n",
  27. status);
  28. rng->priv = (unsigned long)work_data;
  29. atomic_dec(&i2c_priv->tfm_count);
  30. }
  31. static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
  32. size_t max)
  33. {
  34. struct atmel_i2c_client_priv *i2c_priv;
  35. struct atmel_i2c_work_data *work_data;
  36. i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
  37. /* keep maximum 1 asynchronous read in flight at any time */
  38. if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
  39. return 0;
  40. if (rng->priv) {
  41. work_data = (struct atmel_i2c_work_data *)rng->priv;
  42. max = min(sizeof(work_data->cmd.data), max);
  43. memcpy(data, &work_data->cmd.data, max);
  44. rng->priv = 0;
  45. } else {
  46. work_data = kmalloc(sizeof(*work_data), GFP_ATOMIC);
  47. if (!work_data)
  48. return -ENOMEM;
  49. work_data->ctx = i2c_priv;
  50. work_data->client = i2c_priv->client;
  51. max = 0;
  52. }
  53. atmel_i2c_init_random_cmd(&work_data->cmd);
  54. atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
  55. return max;
  56. }
  57. static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
  58. bool wait)
  59. {
  60. struct atmel_i2c_client_priv *i2c_priv;
  61. struct atmel_i2c_cmd cmd;
  62. int ret;
  63. if (!wait)
  64. return atmel_sha204a_rng_read_nonblocking(rng, data, max);
  65. i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
  66. atmel_i2c_init_random_cmd(&cmd);
  67. ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
  68. if (ret)
  69. return ret;
  70. max = min(sizeof(cmd.data), max);
  71. memcpy(data, cmd.data, max);
  72. return max;
  73. }
  74. static int atmel_sha204a_probe(struct i2c_client *client,
  75. const struct i2c_device_id *id)
  76. {
  77. struct atmel_i2c_client_priv *i2c_priv;
  78. int ret;
  79. ret = atmel_i2c_probe(client, id);
  80. if (ret)
  81. return ret;
  82. i2c_priv = i2c_get_clientdata(client);
  83. memset(&i2c_priv->hwrng, 0, sizeof(i2c_priv->hwrng));
  84. i2c_priv->hwrng.name = dev_name(&client->dev);
  85. i2c_priv->hwrng.read = atmel_sha204a_rng_read;
  86. i2c_priv->hwrng.quality = 1024;
  87. ret = devm_hwrng_register(&client->dev, &i2c_priv->hwrng);
  88. if (ret)
  89. dev_warn(&client->dev, "failed to register RNG (%d)\n", ret);
  90. return ret;
  91. }
  92. static int atmel_sha204a_remove(struct i2c_client *client)
  93. {
  94. struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
  95. if (atomic_read(&i2c_priv->tfm_count)) {
  96. dev_err(&client->dev, "Device is busy\n");
  97. return -EBUSY;
  98. }
  99. if (i2c_priv->hwrng.priv)
  100. kfree((void *)i2c_priv->hwrng.priv);
  101. return 0;
  102. }
  103. static const struct of_device_id atmel_sha204a_dt_ids[] = {
  104. { .compatible = "atmel,atsha204a", },
  105. { /* sentinel */ }
  106. };
  107. MODULE_DEVICE_TABLE(of, atmel_sha204a_dt_ids);
  108. static const struct i2c_device_id atmel_sha204a_id[] = {
  109. { "atsha204a", 0 },
  110. { /* sentinel */ }
  111. };
  112. MODULE_DEVICE_TABLE(i2c, atmel_sha204a_id);
  113. static struct i2c_driver atmel_sha204a_driver = {
  114. .probe = atmel_sha204a_probe,
  115. .remove = atmel_sha204a_remove,
  116. .id_table = atmel_sha204a_id,
  117. .driver.name = "atmel-sha204a",
  118. .driver.of_match_table = of_match_ptr(atmel_sha204a_dt_ids),
  119. };
  120. static int __init atmel_sha204a_init(void)
  121. {
  122. return i2c_add_driver(&atmel_sha204a_driver);
  123. }
  124. static void __exit atmel_sha204a_exit(void)
  125. {
  126. flush_scheduled_work();
  127. i2c_del_driver(&atmel_sha204a_driver);
  128. }
  129. module_init(atmel_sha204a_init);
  130. module_exit(atmel_sha204a_exit);
  131. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  132. MODULE_LICENSE("GPL v2");