Merge tag 'drm-misc-fixes-2022-12-08' of git://anongit.freedesktop.org/drm/drm-misc...
[platform/kernel/linux-starfive.git] / drivers / platform / mellanox / mlxreg-io.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Mellanox register access driver
4  *
5  * Copyright (C) 2018 Mellanox Technologies
6  * Copyright (C) 2018 Vadim Pasternak <vadimp@mellanox.com>
7  */
8
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_data/mlxreg.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18
19 /* Attribute parameters. */
20 #define MLXREG_IO_ATT_SIZE      10
21 #define MLXREG_IO_ATT_NUM       96
22
23 /**
24  * struct mlxreg_io_priv_data - driver's private data:
25  *
26  * @pdev: platform device;
27  * @pdata: platform data;
28  * @hwmon: hwmon device;
29  * @mlxreg_io_attr: sysfs attributes array;
30  * @mlxreg_io_dev_attr: sysfs sensor device attribute array;
31  * @group: sysfs attribute group;
32  * @groups: list of sysfs attribute group for hwmon registration;
33  * @regsize: size of a register value;
34  * @io_lock: user access locking;
35  */
36 struct mlxreg_io_priv_data {
37         struct platform_device *pdev;
38         struct mlxreg_core_platform_data *pdata;
39         struct device *hwmon;
40         struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1];
41         struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM];
42         struct attribute_group group;
43         const struct attribute_group *groups[2];
44         int regsize;
45         struct mutex io_lock; /* Protects user access. */
46 };
47
48 static int
49 mlxreg_io_get_reg(void *regmap, struct mlxreg_core_data *data, u32 in_val,
50                   bool rw_flag, int regsize, u32 *regval)
51 {
52         int i, val, ret;
53
54         ret = regmap_read(regmap, data->reg, regval);
55         if (ret)
56                 goto access_error;
57
58         /*
59          * There are four kinds of attributes: single bit, full register's
60          * bits, bit sequence, bits in few registers For the first kind field
61          * mask indicates which bits are not related and field bit is set zero.
62          * For the second kind field mask is set to zero and field bit is set
63          * with all bits one. No special handling for such kind of attributes -
64          * pass value as is. For the third kind, the field mask indicates which
65          * bits are related and the field bit is set to the first bit number
66          * (from 1 to 32) is the bit sequence. For the fourth kind - the number
67          * of registers which should be read for getting an attribute are
68          * specified through 'data->regnum' field.
69          */
70         if (!data->bit) {
71                 /* Single bit. */
72                 if (rw_flag) {
73                         /* For show: expose effective bit value as 0 or 1. */
74                         *regval = !!(*regval & ~data->mask);
75                 } else {
76                         /* For store: set effective bit value. */
77                         *regval &= data->mask;
78                         if (in_val)
79                                 *regval |= ~data->mask;
80                 }
81         } else if (data->mask) {
82                 /* Bit sequence. */
83                 if (rw_flag) {
84                         /* For show: mask and shift right. */
85                         *regval = ror32(*regval & data->mask, (data->bit - 1));
86                 } else {
87                         /* For store: shift to the position and mask. */
88                         in_val = rol32(in_val, data->bit - 1) & data->mask;
89                         /* Clear relevant bits and set them to new value. */
90                         *regval = (*regval & ~data->mask) | in_val;
91                 }
92         } else {
93                 /*
94                  * Some attributes could occupied few registers in case regmap
95                  * bit size is 8 or 16. Compose such attributes from 'regnum'
96                  * registers. Such attributes contain read-only data.
97                  */
98                 for (i = 1; i < data->regnum; i++) {
99                         ret = regmap_read(regmap, data->reg + i, &val);
100                         if (ret)
101                                 goto access_error;
102
103                         *regval |= rol32(val, regsize * i * 8);
104                 }
105         }
106
107 access_error:
108         return ret;
109 }
110
111 static ssize_t
112 mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
113                     char *buf)
114 {
115         struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
116         int index = to_sensor_dev_attr(attr)->index;
117         struct mlxreg_core_data *data = priv->pdata->data + index;
118         u32 regval = 0;
119         int ret;
120
121         mutex_lock(&priv->io_lock);
122
123         ret = mlxreg_io_get_reg(priv->pdata->regmap, data, 0, true,
124                                 priv->regsize, &regval);
125         if (ret)
126                 goto access_error;
127
128         mutex_unlock(&priv->io_lock);
129
130         return sprintf(buf, "%u\n", regval);
131
132 access_error:
133         mutex_unlock(&priv->io_lock);
134         return ret;
135 }
136
137 static ssize_t
138 mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
139                      const char *buf, size_t len)
140 {
141         struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
142         int index = to_sensor_dev_attr(attr)->index;
143         struct mlxreg_core_data *data = priv->pdata->data + index;
144         u32 input_val, regval;
145         int ret;
146
147         if (len > MLXREG_IO_ATT_SIZE)
148                 return -EINVAL;
149
150         /* Convert buffer to input value. */
151         ret = kstrtou32(buf, 0, &input_val);
152         if (ret)
153                 return ret;
154
155         mutex_lock(&priv->io_lock);
156
157         ret = mlxreg_io_get_reg(priv->pdata->regmap, data, input_val, false,
158                                 priv->regsize, &regval);
159         if (ret)
160                 goto access_error;
161
162         ret = regmap_write(priv->pdata->regmap, data->reg, regval);
163         if (ret)
164                 goto access_error;
165
166         mutex_unlock(&priv->io_lock);
167
168         return len;
169
170 access_error:
171         mutex_unlock(&priv->io_lock);
172         dev_err(&priv->pdev->dev, "Bus access error\n");
173         return ret;
174 }
175
176 static struct device_attribute mlxreg_io_devattr_rw = {
177         .show   = mlxreg_io_attr_show,
178         .store  = mlxreg_io_attr_store,
179 };
180
181 static int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv)
182 {
183         int i;
184
185         priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
186                                          priv->pdata->counter,
187                                          sizeof(struct attribute *),
188                                          GFP_KERNEL);
189         if (!priv->group.attrs)
190                 return -ENOMEM;
191
192         for (i = 0; i < priv->pdata->counter; i++) {
193                 priv->mlxreg_io_attr[i] =
194                                 &priv->mlxreg_io_dev_attr[i].dev_attr.attr;
195                 memcpy(&priv->mlxreg_io_dev_attr[i].dev_attr,
196                        &mlxreg_io_devattr_rw, sizeof(struct device_attribute));
197
198                 /* Set attribute name as a label. */
199                 priv->mlxreg_io_attr[i]->name =
200                                 devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
201                                                priv->pdata->data[i].label);
202
203                 if (!priv->mlxreg_io_attr[i]->name) {
204                         dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n",
205                                 i + 1);
206                         return -ENOMEM;
207                 }
208
209                 priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode =
210                                                 priv->pdata->data[i].mode;
211                 priv->mlxreg_io_dev_attr[i].dev_attr.attr.name =
212                                         priv->mlxreg_io_attr[i]->name;
213                 priv->mlxreg_io_dev_attr[i].index = i;
214                 sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr);
215         }
216
217         priv->group.attrs = priv->mlxreg_io_attr;
218         priv->groups[0] = &priv->group;
219         priv->groups[1] = NULL;
220
221         return 0;
222 }
223
224 static int mlxreg_io_probe(struct platform_device *pdev)
225 {
226         struct mlxreg_io_priv_data *priv;
227         int err;
228
229         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
230         if (!priv)
231                 return -ENOMEM;
232
233         priv->pdata = dev_get_platdata(&pdev->dev);
234         if (!priv->pdata) {
235                 dev_err(&pdev->dev, "Failed to get platform data.\n");
236                 return -EINVAL;
237         }
238
239         priv->pdev = pdev;
240         priv->regsize = regmap_get_val_bytes(priv->pdata->regmap);
241         if (priv->regsize < 0)
242                 return priv->regsize;
243
244         err = mlxreg_io_attr_init(priv);
245         if (err) {
246                 dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n",
247                         err);
248                 return err;
249         }
250
251         priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
252                                                              "mlxreg_io",
253                                                               priv,
254                                                               priv->groups);
255         if (IS_ERR(priv->hwmon)) {
256                 dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
257                         PTR_ERR(priv->hwmon));
258                 return PTR_ERR(priv->hwmon);
259         }
260
261         mutex_init(&priv->io_lock);
262         dev_set_drvdata(&pdev->dev, priv);
263
264         return 0;
265 }
266
267 static int mlxreg_io_remove(struct platform_device *pdev)
268 {
269         struct mlxreg_io_priv_data *priv = dev_get_drvdata(&pdev->dev);
270
271         mutex_destroy(&priv->io_lock);
272
273         return 0;
274 }
275
276 static struct platform_driver mlxreg_io_driver = {
277         .driver = {
278             .name = "mlxreg-io",
279         },
280         .probe = mlxreg_io_probe,
281         .remove = mlxreg_io_remove,
282 };
283
284 module_platform_driver(mlxreg_io_driver);
285
286 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
287 MODULE_DESCRIPTION("Mellanox regmap I/O access driver");
288 MODULE_LICENSE("GPL");
289 MODULE_ALIAS("platform:mlxreg-io");