0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/bitops.h>
0010 #include <linux/device.h>
0011 #include <linux/hwmon.h>
0012 #include <linux/hwmon-sysfs.h>
0013 #include <linux/module.h>
0014 #include <linux/of_device.h>
0015 #include <linux/platform_data/mlxreg.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/regmap.h>
0018
0019
0020 #define MLXREG_IO_ATT_SIZE 10
0021 #define MLXREG_IO_ATT_NUM 96
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 struct mlxreg_io_priv_data {
0037 struct platform_device *pdev;
0038 struct mlxreg_core_platform_data *pdata;
0039 struct device *hwmon;
0040 struct attribute *mlxreg_io_attr[MLXREG_IO_ATT_NUM + 1];
0041 struct sensor_device_attribute mlxreg_io_dev_attr[MLXREG_IO_ATT_NUM];
0042 struct attribute_group group;
0043 const struct attribute_group *groups[2];
0044 int regsize;
0045 struct mutex io_lock;
0046 };
0047
0048 static int
0049 mlxreg_io_get_reg(void *regmap, struct mlxreg_core_data *data, u32 in_val,
0050 bool rw_flag, int regsize, u32 *regval)
0051 {
0052 int i, val, ret;
0053
0054 ret = regmap_read(regmap, data->reg, regval);
0055 if (ret)
0056 goto access_error;
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 if (!data->bit) {
0071
0072 if (rw_flag) {
0073
0074 *regval = !!(*regval & ~data->mask);
0075 } else {
0076
0077 *regval &= data->mask;
0078 if (in_val)
0079 *regval |= ~data->mask;
0080 }
0081 } else if (data->mask) {
0082
0083 if (rw_flag) {
0084
0085 *regval = ror32(*regval & data->mask, (data->bit - 1));
0086 } else {
0087
0088 in_val = rol32(in_val, data->bit - 1) & data->mask;
0089
0090 *regval = (*regval & ~data->mask) | in_val;
0091 }
0092 } else {
0093
0094
0095
0096
0097
0098 for (i = 1; i < data->regnum; i++) {
0099 ret = regmap_read(regmap, data->reg + i, &val);
0100 if (ret)
0101 goto access_error;
0102
0103 *regval |= rol32(val, regsize * i * 8);
0104 }
0105 }
0106
0107 access_error:
0108 return ret;
0109 }
0110
0111 static ssize_t
0112 mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
0113 char *buf)
0114 {
0115 struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
0116 int index = to_sensor_dev_attr(attr)->index;
0117 struct mlxreg_core_data *data = priv->pdata->data + index;
0118 u32 regval = 0;
0119 int ret;
0120
0121 mutex_lock(&priv->io_lock);
0122
0123 ret = mlxreg_io_get_reg(priv->pdata->regmap, data, 0, true,
0124 priv->regsize, ®val);
0125 if (ret)
0126 goto access_error;
0127
0128 mutex_unlock(&priv->io_lock);
0129
0130 return sprintf(buf, "%u\n", regval);
0131
0132 access_error:
0133 mutex_unlock(&priv->io_lock);
0134 return ret;
0135 }
0136
0137 static ssize_t
0138 mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
0139 const char *buf, size_t len)
0140 {
0141 struct mlxreg_io_priv_data *priv = dev_get_drvdata(dev);
0142 int index = to_sensor_dev_attr(attr)->index;
0143 struct mlxreg_core_data *data = priv->pdata->data + index;
0144 u32 input_val, regval;
0145 int ret;
0146
0147 if (len > MLXREG_IO_ATT_SIZE)
0148 return -EINVAL;
0149
0150
0151 ret = kstrtou32(buf, 0, &input_val);
0152 if (ret)
0153 return ret;
0154
0155 mutex_lock(&priv->io_lock);
0156
0157 ret = mlxreg_io_get_reg(priv->pdata->regmap, data, input_val, false,
0158 priv->regsize, ®val);
0159 if (ret)
0160 goto access_error;
0161
0162 ret = regmap_write(priv->pdata->regmap, data->reg, regval);
0163 if (ret)
0164 goto access_error;
0165
0166 mutex_unlock(&priv->io_lock);
0167
0168 return len;
0169
0170 access_error:
0171 mutex_unlock(&priv->io_lock);
0172 dev_err(&priv->pdev->dev, "Bus access error\n");
0173 return ret;
0174 }
0175
0176 static struct device_attribute mlxreg_io_devattr_rw = {
0177 .show = mlxreg_io_attr_show,
0178 .store = mlxreg_io_attr_store,
0179 };
0180
0181 static int mlxreg_io_attr_init(struct mlxreg_io_priv_data *priv)
0182 {
0183 int i;
0184
0185 priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
0186 priv->pdata->counter,
0187 sizeof(struct attribute *),
0188 GFP_KERNEL);
0189 if (!priv->group.attrs)
0190 return -ENOMEM;
0191
0192 for (i = 0; i < priv->pdata->counter; i++) {
0193 priv->mlxreg_io_attr[i] =
0194 &priv->mlxreg_io_dev_attr[i].dev_attr.attr;
0195 memcpy(&priv->mlxreg_io_dev_attr[i].dev_attr,
0196 &mlxreg_io_devattr_rw, sizeof(struct device_attribute));
0197
0198
0199 priv->mlxreg_io_attr[i]->name =
0200 devm_kasprintf(&priv->pdev->dev, GFP_KERNEL,
0201 priv->pdata->data[i].label);
0202
0203 if (!priv->mlxreg_io_attr[i]->name) {
0204 dev_err(&priv->pdev->dev, "Memory allocation failed for sysfs attribute %d.\n",
0205 i + 1);
0206 return -ENOMEM;
0207 }
0208
0209 priv->mlxreg_io_dev_attr[i].dev_attr.attr.mode =
0210 priv->pdata->data[i].mode;
0211 priv->mlxreg_io_dev_attr[i].dev_attr.attr.name =
0212 priv->mlxreg_io_attr[i]->name;
0213 priv->mlxreg_io_dev_attr[i].index = i;
0214 sysfs_attr_init(&priv->mlxreg_io_dev_attr[i].dev_attr.attr);
0215 }
0216
0217 priv->group.attrs = priv->mlxreg_io_attr;
0218 priv->groups[0] = &priv->group;
0219 priv->groups[1] = NULL;
0220
0221 return 0;
0222 }
0223
0224 static int mlxreg_io_probe(struct platform_device *pdev)
0225 {
0226 struct mlxreg_io_priv_data *priv;
0227 int err;
0228
0229 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0230 if (!priv)
0231 return -ENOMEM;
0232
0233 priv->pdata = dev_get_platdata(&pdev->dev);
0234 if (!priv->pdata) {
0235 dev_err(&pdev->dev, "Failed to get platform data.\n");
0236 return -EINVAL;
0237 }
0238
0239 priv->pdev = pdev;
0240 priv->regsize = regmap_get_val_bytes(priv->pdata->regmap);
0241 if (priv->regsize < 0)
0242 return priv->regsize;
0243
0244 err = mlxreg_io_attr_init(priv);
0245 if (err) {
0246 dev_err(&priv->pdev->dev, "Failed to allocate attributes: %d\n",
0247 err);
0248 return err;
0249 }
0250
0251 priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
0252 "mlxreg_io",
0253 priv,
0254 priv->groups);
0255 if (IS_ERR(priv->hwmon)) {
0256 dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
0257 PTR_ERR(priv->hwmon));
0258 return PTR_ERR(priv->hwmon);
0259 }
0260
0261 mutex_init(&priv->io_lock);
0262 dev_set_drvdata(&pdev->dev, priv);
0263
0264 return 0;
0265 }
0266
0267 static int mlxreg_io_remove(struct platform_device *pdev)
0268 {
0269 struct mlxreg_io_priv_data *priv = dev_get_drvdata(&pdev->dev);
0270
0271 mutex_destroy(&priv->io_lock);
0272
0273 return 0;
0274 }
0275
0276 static struct platform_driver mlxreg_io_driver = {
0277 .driver = {
0278 .name = "mlxreg-io",
0279 },
0280 .probe = mlxreg_io_probe,
0281 .remove = mlxreg_io_remove,
0282 };
0283
0284 module_platform_driver(mlxreg_io_driver);
0285
0286 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
0287 MODULE_DESCRIPTION("Mellanox regmap I/O access driver");
0288 MODULE_LICENSE("GPL");
0289 MODULE_ALIAS("platform:mlxreg-io");