Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * HMC425A and similar Gain Amplifiers
0004  *
0005  * Copyright 2020 Analog Devices Inc.
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/err.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/iio/iio.h>
0012 #include <linux/iio/sysfs.h>
0013 #include <linux/kernel.h>
0014 #include <linux/mod_devicetable.h>
0015 #include <linux/module.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/property.h>
0018 #include <linux/slab.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/sysfs.h>
0021 
0022 enum hmc425a_type {
0023     ID_HMC425A,
0024 };
0025 
0026 struct hmc425a_chip_info {
0027     const char          *name;
0028     const struct iio_chan_spec  *channels;
0029     unsigned int            num_channels;
0030     unsigned int            num_gpios;
0031     int             gain_min;
0032     int             gain_max;
0033     int             default_gain;
0034 };
0035 
0036 struct hmc425a_state {
0037     struct  regulator *reg;
0038     struct  mutex lock; /* protect sensor state */
0039     struct  hmc425a_chip_info *chip_info;
0040     struct  gpio_descs *gpios;
0041     enum    hmc425a_type type;
0042     u32 gain;
0043 };
0044 
0045 static int hmc425a_write(struct iio_dev *indio_dev, u32 value)
0046 {
0047     struct hmc425a_state *st = iio_priv(indio_dev);
0048     DECLARE_BITMAP(values, BITS_PER_TYPE(value));
0049 
0050     values[0] = value;
0051 
0052     gpiod_set_array_value_cansleep(st->gpios->ndescs, st->gpios->desc,
0053                        NULL, values);
0054     return 0;
0055 }
0056 
0057 static int hmc425a_read_raw(struct iio_dev *indio_dev,
0058                 struct iio_chan_spec const *chan, int *val,
0059                 int *val2, long m)
0060 {
0061     struct hmc425a_state *st = iio_priv(indio_dev);
0062     int code, gain = 0;
0063     int ret;
0064 
0065     mutex_lock(&st->lock);
0066     switch (m) {
0067     case IIO_CHAN_INFO_HARDWAREGAIN:
0068         code = st->gain;
0069 
0070         switch (st->type) {
0071         case ID_HMC425A:
0072             gain = ~code * -500;
0073             break;
0074         }
0075 
0076         *val = gain / 1000;
0077         *val2 = (gain % 1000) * 1000;
0078 
0079         ret = IIO_VAL_INT_PLUS_MICRO_DB;
0080         break;
0081     default:
0082         ret = -EINVAL;
0083     }
0084     mutex_unlock(&st->lock);
0085 
0086     return ret;
0087 };
0088 
0089 static int hmc425a_write_raw(struct iio_dev *indio_dev,
0090                  struct iio_chan_spec const *chan, int val,
0091                  int val2, long mask)
0092 {
0093     struct hmc425a_state *st = iio_priv(indio_dev);
0094     struct hmc425a_chip_info *inf = st->chip_info;
0095     int code = 0, gain;
0096     int ret;
0097 
0098     if (val < 0)
0099         gain = (val * 1000) - (val2 / 1000);
0100     else
0101         gain = (val * 1000) + (val2 / 1000);
0102 
0103     if (gain > inf->gain_max || gain < inf->gain_min)
0104         return -EINVAL;
0105 
0106     switch (st->type) {
0107     case ID_HMC425A:
0108         code = ~((abs(gain) / 500) & 0x3F);
0109         break;
0110     }
0111 
0112     mutex_lock(&st->lock);
0113     switch (mask) {
0114     case IIO_CHAN_INFO_HARDWAREGAIN:
0115         st->gain = code;
0116 
0117         ret = hmc425a_write(indio_dev, st->gain);
0118         break;
0119     default:
0120         ret = -EINVAL;
0121     }
0122     mutex_unlock(&st->lock);
0123 
0124     return ret;
0125 }
0126 
0127 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev,
0128                      struct iio_chan_spec const *chan,
0129                      long mask)
0130 {
0131     switch (mask) {
0132     case IIO_CHAN_INFO_HARDWAREGAIN:
0133         return IIO_VAL_INT_PLUS_MICRO_DB;
0134     default:
0135         return -EINVAL;
0136     }
0137 }
0138 
0139 static const struct iio_info hmc425a_info = {
0140     .read_raw = &hmc425a_read_raw,
0141     .write_raw = &hmc425a_write_raw,
0142     .write_raw_get_fmt = &hmc425a_write_raw_get_fmt,
0143 };
0144 
0145 #define HMC425A_CHAN(_channel)                      \
0146 {                                   \
0147     .type = IIO_VOLTAGE,                        \
0148     .output = 1,                            \
0149     .indexed = 1,                           \
0150     .channel = _channel,                        \
0151     .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN),      \
0152 }
0153 
0154 static const struct iio_chan_spec hmc425a_channels[] = {
0155     HMC425A_CHAN(0),
0156 };
0157 
0158 /* Match table for of_platform binding */
0159 static const struct of_device_id hmc425a_of_match[] = {
0160     { .compatible = "adi,hmc425a", .data = (void *)ID_HMC425A },
0161     {},
0162 };
0163 MODULE_DEVICE_TABLE(of, hmc425a_of_match);
0164 
0165 static void hmc425a_reg_disable(void *data)
0166 {
0167     struct hmc425a_state *st = data;
0168 
0169     regulator_disable(st->reg);
0170 }
0171 
0172 static struct hmc425a_chip_info hmc425a_chip_info_tbl[] = {
0173     [ID_HMC425A] = {
0174         .name = "hmc425a",
0175         .channels = hmc425a_channels,
0176         .num_channels = ARRAY_SIZE(hmc425a_channels),
0177         .num_gpios = 6,
0178         .gain_min = -31500,
0179         .gain_max = 0,
0180         .default_gain = -0x40, /* set default gain -31.5db*/
0181     },
0182 };
0183 
0184 static int hmc425a_probe(struct platform_device *pdev)
0185 {
0186     struct iio_dev *indio_dev;
0187     struct hmc425a_state *st;
0188     int ret;
0189 
0190     indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
0191     if (!indio_dev)
0192         return -ENOMEM;
0193 
0194     st = iio_priv(indio_dev);
0195     st->type = (uintptr_t)device_get_match_data(&pdev->dev);
0196 
0197     st->chip_info = &hmc425a_chip_info_tbl[st->type];
0198     indio_dev->num_channels = st->chip_info->num_channels;
0199     indio_dev->channels = st->chip_info->channels;
0200     indio_dev->name = st->chip_info->name;
0201     st->gain = st->chip_info->default_gain;
0202 
0203     st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW);
0204     if (IS_ERR(st->gpios))
0205         return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios),
0206                      "failed to get gpios\n");
0207 
0208     if (st->gpios->ndescs != st->chip_info->num_gpios) {
0209         dev_err(&pdev->dev, "%d GPIOs needed to operate\n",
0210             st->chip_info->num_gpios);
0211         return -ENODEV;
0212     }
0213 
0214     st->reg = devm_regulator_get(&pdev->dev, "vcc-supply");
0215     if (IS_ERR(st->reg))
0216         return PTR_ERR(st->reg);
0217 
0218     ret = regulator_enable(st->reg);
0219     if (ret)
0220         return ret;
0221     ret = devm_add_action_or_reset(&pdev->dev, hmc425a_reg_disable, st);
0222     if (ret)
0223         return ret;
0224 
0225     mutex_init(&st->lock);
0226 
0227     indio_dev->info = &hmc425a_info;
0228     indio_dev->modes = INDIO_DIRECT_MODE;
0229 
0230     return devm_iio_device_register(&pdev->dev, indio_dev);
0231 }
0232 
0233 static struct platform_driver hmc425a_driver = {
0234     .driver = {
0235         .name = KBUILD_MODNAME,
0236         .of_match_table = hmc425a_of_match,
0237     },
0238     .probe = hmc425a_probe,
0239 };
0240 module_platform_driver(hmc425a_driver);
0241 
0242 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
0243 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers");
0244 MODULE_LICENSE("GPL v2");