Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Mellanox register access driver
0004  *
0005  * Copyright (C) 2018 Mellanox Technologies
0006  * Copyright (C) 2018 Vadim Pasternak <vadimp@mellanox.com>
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 /* Attribute parameters. */
0020 #define MLXREG_IO_ATT_SIZE  10
0021 #define MLXREG_IO_ATT_NUM   96
0022 
0023 /**
0024  * struct mlxreg_io_priv_data - driver's private data:
0025  *
0026  * @pdev: platform device;
0027  * @pdata: platform data;
0028  * @hwmon: hwmon device;
0029  * @mlxreg_io_attr: sysfs attributes array;
0030  * @mlxreg_io_dev_attr: sysfs sensor device attribute array;
0031  * @group: sysfs attribute group;
0032  * @groups: list of sysfs attribute group for hwmon registration;
0033  * @regsize: size of a register value;
0034  * @io_lock: user access locking;
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; /* Protects user access. */
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      * There are four kinds of attributes: single bit, full register's
0060      * bits, bit sequence, bits in few registers For the first kind field
0061      * mask indicates which bits are not related and field bit is set zero.
0062      * For the second kind field mask is set to zero and field bit is set
0063      * with all bits one. No special handling for such kind of attributes -
0064      * pass value as is. For the third kind, the field mask indicates which
0065      * bits are related and the field bit is set to the first bit number
0066      * (from 1 to 32) is the bit sequence. For the fourth kind - the number
0067      * of registers which should be read for getting an attribute are
0068      * specified through 'data->regnum' field.
0069      */
0070     if (!data->bit) {
0071         /* Single bit. */
0072         if (rw_flag) {
0073             /* For show: expose effective bit value as 0 or 1. */
0074             *regval = !!(*regval & ~data->mask);
0075         } else {
0076             /* For store: set effective bit value. */
0077             *regval &= data->mask;
0078             if (in_val)
0079                 *regval |= ~data->mask;
0080         }
0081     } else if (data->mask) {
0082         /* Bit sequence. */
0083         if (rw_flag) {
0084             /* For show: mask and shift right. */
0085             *regval = ror32(*regval & data->mask, (data->bit - 1));
0086         } else {
0087             /* For store: shift to the position and mask. */
0088             in_val = rol32(in_val, data->bit - 1) & data->mask;
0089             /* Clear relevant bits and set them to new value. */
0090             *regval = (*regval & ~data->mask) | in_val;
0091         }
0092     } else {
0093         /*
0094          * Some attributes could occupied few registers in case regmap
0095          * bit size is 8 or 16. Compose such attributes from 'regnum'
0096          * registers. Such attributes contain read-only data.
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, &regval);
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     /* Convert buffer to input value. */
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, &regval);
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         /* Set attribute name as a label. */
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");