1 // SPDX-License-Identifier: GPL-2.0+
3 * Mellanox register access driver
5 * Copyright (C) 2018 Mellanox Technologies
6 * Copyright (C) 2018 Vadim Pasternak <vadimp@mellanox.com>
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/platform_data/mlxreg.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 /* Attribute parameters. */
19 #define MLXREG_IO_ATT_SIZE 10
20 #define MLXREG_IO_ATT_NUM 96
23 * struct mlxreg_io_priv_data - driver's private data:
25 * @pdev: platform device;
26 * @pdata: platform data;
27 * @hwmon: hwmon device;
28 * @mlxreg_io_attr: sysfs attributes array;
29 * @mlxreg_io_dev_attr: sysfs sensor device attribute array;
30 * @group: sysfs attribute group;
31 * @groups: list of sysfs attribute group for hwmon registration;
32 * @regsize: size of a register value;
33 * @io_lock: user access locking;
35 struct mlxreg_io_priv_data
{
36 struct platform_device
*pdev
;
37 struct mlxreg_core_platform_data
*pdata
;
39 struct attribute
*mlxreg_io_attr
[MLXREG_IO_ATT_NUM
+ 1];
40 struct sensor_device_attribute mlxreg_io_dev_attr
[MLXREG_IO_ATT_NUM
];
41 struct attribute_group group
;
42 const struct attribute_group
*groups
[2];
44 struct mutex io_lock
; /* Protects user access. */
48 mlxreg_io_get_reg(void *regmap
, struct mlxreg_core_data
*data
, u32 in_val
,
49 bool rw_flag
, int regsize
, u32
*regval
)
53 ret
= regmap_read(regmap
, data
->reg
, regval
);
58 * There are four kinds of attributes: single bit, full register's
59 * bits, bit sequence, bits in few registers For the first kind field
60 * mask indicates which bits are not related and field bit is set zero.
61 * For the second kind field mask is set to zero and field bit is set
62 * with all bits one. No special handling for such kind of attributes -
63 * pass value as is. For the third kind, the field mask indicates which
64 * bits are related and the field bit is set to the first bit number
65 * (from 1 to 32) is the bit sequence. For the fourth kind - the number
66 * of registers which should be read for getting an attribute are
67 * specified through 'data->regnum' field.
72 /* For show: expose effective bit value as 0 or 1. */
73 *regval
= !!(*regval
& ~data
->mask
);
75 /* For store: set effective bit value. */
76 *regval
&= data
->mask
;
78 *regval
|= ~data
->mask
;
80 } else if (data
->mask
) {
83 /* For show: mask and shift right. */
84 *regval
= ror32(*regval
& data
->mask
, (data
->bit
- 1));
86 /* For store: shift to the position and mask. */
87 in_val
= rol32(in_val
, data
->bit
- 1) & data
->mask
;
88 /* Clear relevant bits and set them to new value. */
89 *regval
= (*regval
& ~data
->mask
) | in_val
;
93 * Some attributes could occupied few registers in case regmap
94 * bit size is 8 or 16. Compose such attributes from 'regnum'
95 * registers. Such attributes contain read-only data.
97 for (i
= 1; i
< data
->regnum
; i
++) {
98 ret
= regmap_read(regmap
, data
->reg
+ i
, &val
);
102 *regval
|= rol32(val
, regsize
* i
* 8);
111 mlxreg_io_attr_show(struct device
*dev
, struct device_attribute
*attr
,
114 struct mlxreg_io_priv_data
*priv
= dev_get_drvdata(dev
);
115 int index
= to_sensor_dev_attr(attr
)->index
;
116 struct mlxreg_core_data
*data
= priv
->pdata
->data
+ index
;
120 mutex_lock(&priv
->io_lock
);
122 ret
= mlxreg_io_get_reg(priv
->pdata
->regmap
, data
, 0, true,
123 priv
->regsize
, ®val
);
127 mutex_unlock(&priv
->io_lock
);
129 return sprintf(buf
, "%u\n", regval
);
132 mutex_unlock(&priv
->io_lock
);
137 mlxreg_io_attr_store(struct device
*dev
, struct device_attribute
*attr
,
138 const char *buf
, size_t len
)
140 struct mlxreg_io_priv_data
*priv
= dev_get_drvdata(dev
);
141 int index
= to_sensor_dev_attr(attr
)->index
;
142 struct mlxreg_core_data
*data
= priv
->pdata
->data
+ index
;
143 u32 input_val
, regval
;
146 if (len
> MLXREG_IO_ATT_SIZE
)
149 /* Convert buffer to input value. */
150 ret
= kstrtou32(buf
, 0, &input_val
);
154 mutex_lock(&priv
->io_lock
);
156 ret
= mlxreg_io_get_reg(priv
->pdata
->regmap
, data
, input_val
, false,
157 priv
->regsize
, ®val
);
161 ret
= regmap_write(priv
->pdata
->regmap
, data
->reg
, regval
);
165 mutex_unlock(&priv
->io_lock
);
170 mutex_unlock(&priv
->io_lock
);
171 dev_err(&priv
->pdev
->dev
, "Bus access error\n");
175 static struct device_attribute mlxreg_io_devattr_rw
= {
176 .show
= mlxreg_io_attr_show
,
177 .store
= mlxreg_io_attr_store
,
180 static int mlxreg_io_attr_init(struct mlxreg_io_priv_data
*priv
)
184 priv
->group
.attrs
= devm_kcalloc(&priv
->pdev
->dev
,
185 priv
->pdata
->counter
,
186 sizeof(struct attribute
*),
188 if (!priv
->group
.attrs
)
191 for (i
= 0; i
< priv
->pdata
->counter
; i
++) {
192 priv
->mlxreg_io_attr
[i
] =
193 &priv
->mlxreg_io_dev_attr
[i
].dev_attr
.attr
;
194 memcpy(&priv
->mlxreg_io_dev_attr
[i
].dev_attr
,
195 &mlxreg_io_devattr_rw
, sizeof(struct device_attribute
));
197 /* Set attribute name as a label. */
198 priv
->mlxreg_io_attr
[i
]->name
=
199 devm_kasprintf(&priv
->pdev
->dev
, GFP_KERNEL
,
200 priv
->pdata
->data
[i
].label
);
202 if (!priv
->mlxreg_io_attr
[i
]->name
) {
203 dev_err(&priv
->pdev
->dev
, "Memory allocation failed for sysfs attribute %d.\n",
208 priv
->mlxreg_io_dev_attr
[i
].dev_attr
.attr
.mode
=
209 priv
->pdata
->data
[i
].mode
;
210 priv
->mlxreg_io_dev_attr
[i
].dev_attr
.attr
.name
=
211 priv
->mlxreg_io_attr
[i
]->name
;
212 priv
->mlxreg_io_dev_attr
[i
].index
= i
;
213 sysfs_attr_init(&priv
->mlxreg_io_dev_attr
[i
].dev_attr
.attr
);
216 priv
->group
.attrs
= priv
->mlxreg_io_attr
;
217 priv
->groups
[0] = &priv
->group
;
218 priv
->groups
[1] = NULL
;
223 static int mlxreg_io_probe(struct platform_device
*pdev
)
225 struct mlxreg_io_priv_data
*priv
;
228 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
232 priv
->pdata
= dev_get_platdata(&pdev
->dev
);
234 dev_err(&pdev
->dev
, "Failed to get platform data.\n");
239 priv
->regsize
= regmap_get_val_bytes(priv
->pdata
->regmap
);
240 if (priv
->regsize
< 0)
241 return priv
->regsize
;
243 err
= mlxreg_io_attr_init(priv
);
245 dev_err(&priv
->pdev
->dev
, "Failed to allocate attributes: %d\n",
250 priv
->hwmon
= devm_hwmon_device_register_with_groups(&pdev
->dev
,
254 if (IS_ERR(priv
->hwmon
)) {
255 dev_err(&pdev
->dev
, "Failed to register hwmon device %ld\n",
256 PTR_ERR(priv
->hwmon
));
257 return PTR_ERR(priv
->hwmon
);
260 mutex_init(&priv
->io_lock
);
261 dev_set_drvdata(&pdev
->dev
, priv
);
266 static void mlxreg_io_remove(struct platform_device
*pdev
)
268 struct mlxreg_io_priv_data
*priv
= dev_get_drvdata(&pdev
->dev
);
270 mutex_destroy(&priv
->io_lock
);
273 static struct platform_driver mlxreg_io_driver
= {
277 .probe
= mlxreg_io_probe
,
278 .remove
= mlxreg_io_remove
,
281 module_platform_driver(mlxreg_io_driver
);
283 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
284 MODULE_DESCRIPTION("Mellanox regmap I/O access driver");
285 MODULE_LICENSE("GPL");
286 MODULE_ALIAS("platform:mlxreg-io");