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