1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip / Atmel SHA204A (I2C) driver.
5 * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/scatterlist.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include "atmel-i2c.h"
21 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
22 void *areq, int status)
24 struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
25 struct hwrng *rng = areq;
28 dev_warn_ratelimited(&i2c_priv->client->dev,
29 "i2c transaction failed (%d)\n",
32 rng->priv = (unsigned long)work_data;
33 atomic_dec(&i2c_priv->tfm_count);
36 static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
39 struct atmel_i2c_client_priv *i2c_priv;
40 struct atmel_i2c_work_data *work_data;
42 i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
44 /* keep maximum 1 asynchronous read in flight at any time */
45 if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
49 work_data = (struct atmel_i2c_work_data *)rng->priv;
50 max = min(sizeof(work_data->cmd.data), max);
51 memcpy(data, &work_data->cmd.data, max);
54 work_data = kmalloc(sizeof(*work_data), GFP_ATOMIC);
58 work_data->ctx = i2c_priv;
59 work_data->client = i2c_priv->client;
64 atmel_i2c_init_random_cmd(&work_data->cmd);
65 atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
70 static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
73 struct atmel_i2c_client_priv *i2c_priv;
74 struct atmel_i2c_cmd cmd;
78 return atmel_sha204a_rng_read_nonblocking(rng, data, max);
80 i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
82 atmel_i2c_init_random_cmd(&cmd);
84 ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
88 max = min(sizeof(cmd.data), max);
89 memcpy(data, cmd.data, max);
94 static int atmel_sha204a_probe(struct i2c_client *client)
96 struct atmel_i2c_client_priv *i2c_priv;
99 ret = atmel_i2c_probe(client);
103 i2c_priv = i2c_get_clientdata(client);
105 memset(&i2c_priv->hwrng, 0, sizeof(i2c_priv->hwrng));
107 i2c_priv->hwrng.name = dev_name(&client->dev);
108 i2c_priv->hwrng.read = atmel_sha204a_rng_read;
110 ret = devm_hwrng_register(&client->dev, &i2c_priv->hwrng);
112 dev_warn(&client->dev, "failed to register RNG (%d)\n", ret);
117 static void atmel_sha204a_remove(struct i2c_client *client)
119 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
121 if (atomic_read(&i2c_priv->tfm_count)) {
122 dev_emerg(&client->dev, "Device is busy, will remove it anyhow\n");
126 kfree((void *)i2c_priv->hwrng.priv);
129 static const struct of_device_id atmel_sha204a_dt_ids[] = {
130 { .compatible = "atmel,atsha204", },
131 { .compatible = "atmel,atsha204a", },
134 MODULE_DEVICE_TABLE(of, atmel_sha204a_dt_ids);
136 static const struct i2c_device_id atmel_sha204a_id[] = {
141 MODULE_DEVICE_TABLE(i2c, atmel_sha204a_id);
143 static struct i2c_driver atmel_sha204a_driver = {
144 .probe_new = atmel_sha204a_probe,
145 .remove = atmel_sha204a_remove,
146 .id_table = atmel_sha204a_id,
148 .driver.name = "atmel-sha204a",
149 .driver.of_match_table = of_match_ptr(atmel_sha204a_dt_ids),
152 static int __init atmel_sha204a_init(void)
154 return i2c_add_driver(&atmel_sha204a_driver);
157 static void __exit atmel_sha204a_exit(void)
159 atmel_i2c_flush_queue();
160 i2c_del_driver(&atmel_sha204a_driver);
163 module_init(atmel_sha204a_init);
164 module_exit(atmel_sha204a_exit);
166 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
167 MODULE_LICENSE("GPL v2");