Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * adcxx.c
0004  *
0005  * The adcxx4s is an AD converter family from National Semiconductor (NS).
0006  *
0007  * Copyright (c) 2008 Marc Pignat <marc.pignat@hevs.ch>
0008  *
0009  * The adcxx4s communicates with a host processor via an SPI/Microwire Bus
0010  * interface. This driver supports the whole family of devices with name
0011  * ADC<bb><c>S<sss>, where
0012  * * bb is the resolution in number of bits (8, 10, 12)
0013  * * c is the number of channels (1, 2, 4, 8)
0014  * * sss is the maximum conversion speed (021 for 200 kSPS, 051 for 500 kSPS
0015  *   and 101 for 1 MSPS)
0016  *
0017  * Complete datasheets are available at National's website here:
0018  * http://www.national.com/ds/DC/ADC<bb><c>S<sss>.pdf
0019  *
0020  * Handling of 8, 10 and 12 bits converters are the same, the
0021  * unavailable bits are 0 :)
0022  */
0023 
0024 #include <linux/init.h>
0025 #include <linux/module.h>
0026 #include <linux/kernel.h>
0027 #include <linux/slab.h>
0028 #include <linux/device.h>
0029 #include <linux/err.h>
0030 #include <linux/sysfs.h>
0031 #include <linux/hwmon.h>
0032 #include <linux/hwmon-sysfs.h>
0033 #include <linux/mutex.h>
0034 #include <linux/mod_devicetable.h>
0035 #include <linux/spi/spi.h>
0036 
0037 #define DRVNAME     "adcxx"
0038 
0039 struct adcxx {
0040     struct device *hwmon_dev;
0041     struct mutex lock;
0042     u32 channels;
0043     u32 reference; /* in millivolts */
0044 };
0045 
0046 /* sysfs hook function */
0047 static ssize_t adcxx_show(struct device *dev,
0048               struct device_attribute *devattr, char *buf)
0049 {
0050     struct spi_device *spi = to_spi_device(dev);
0051     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0052     struct adcxx *adc = spi_get_drvdata(spi);
0053     u8 tx_buf[2];
0054     u8 rx_buf[2];
0055     int status;
0056     u32 value;
0057 
0058     if (mutex_lock_interruptible(&adc->lock))
0059         return -ERESTARTSYS;
0060 
0061     if (adc->channels == 1) {
0062         status = spi_read(spi, rx_buf, sizeof(rx_buf));
0063     } else {
0064         tx_buf[0] = attr->index << 3; /* other bits are don't care */
0065         status = spi_write_then_read(spi, tx_buf, sizeof(tx_buf),
0066                         rx_buf, sizeof(rx_buf));
0067     }
0068     if (status < 0) {
0069         dev_warn(dev, "SPI synch. transfer failed with status %d\n",
0070                 status);
0071         goto out;
0072     }
0073 
0074     value = (rx_buf[0] << 8) + rx_buf[1];
0075     dev_dbg(dev, "raw value = 0x%x\n", value);
0076 
0077     value = value * adc->reference >> 12;
0078     status = sprintf(buf, "%d\n", value);
0079 out:
0080     mutex_unlock(&adc->lock);
0081     return status;
0082 }
0083 
0084 static ssize_t adcxx_min_show(struct device *dev,
0085                   struct device_attribute *devattr, char *buf)
0086 {
0087     /* The minimum reference is 0 for this chip family */
0088     return sprintf(buf, "0\n");
0089 }
0090 
0091 static ssize_t adcxx_max_show(struct device *dev,
0092                   struct device_attribute *devattr, char *buf)
0093 {
0094     struct spi_device *spi = to_spi_device(dev);
0095     struct adcxx *adc = spi_get_drvdata(spi);
0096     u32 reference;
0097 
0098     if (mutex_lock_interruptible(&adc->lock))
0099         return -ERESTARTSYS;
0100 
0101     reference = adc->reference;
0102 
0103     mutex_unlock(&adc->lock);
0104 
0105     return sprintf(buf, "%d\n", reference);
0106 }
0107 
0108 static ssize_t adcxx_max_store(struct device *dev,
0109                    struct device_attribute *devattr,
0110                    const char *buf, size_t count)
0111 {
0112     struct spi_device *spi = to_spi_device(dev);
0113     struct adcxx *adc = spi_get_drvdata(spi);
0114     unsigned long value;
0115 
0116     if (kstrtoul(buf, 10, &value))
0117         return -EINVAL;
0118 
0119     if (mutex_lock_interruptible(&adc->lock))
0120         return -ERESTARTSYS;
0121 
0122     adc->reference = value;
0123 
0124     mutex_unlock(&adc->lock);
0125 
0126     return count;
0127 }
0128 
0129 static ssize_t adcxx_name_show(struct device *dev,
0130                    struct device_attribute *devattr, char *buf)
0131 {
0132     return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
0133 }
0134 
0135 static struct sensor_device_attribute ad_input[] = {
0136     SENSOR_ATTR_RO(name, adcxx_name, 0),
0137     SENSOR_ATTR_RO(in_min, adcxx_min, 0),
0138     SENSOR_ATTR_RW(in_max, adcxx_max, 0),
0139     SENSOR_ATTR_RO(in0_input, adcxx, 0),
0140     SENSOR_ATTR_RO(in1_input, adcxx, 1),
0141     SENSOR_ATTR_RO(in2_input, adcxx, 2),
0142     SENSOR_ATTR_RO(in3_input, adcxx, 3),
0143     SENSOR_ATTR_RO(in4_input, adcxx, 4),
0144     SENSOR_ATTR_RO(in5_input, adcxx, 5),
0145     SENSOR_ATTR_RO(in6_input, adcxx, 6),
0146     SENSOR_ATTR_RO(in7_input, adcxx, 7),
0147 };
0148 
0149 /*----------------------------------------------------------------------*/
0150 
0151 static int adcxx_probe(struct spi_device *spi)
0152 {
0153     int channels = spi_get_device_id(spi)->driver_data;
0154     struct adcxx *adc;
0155     int status;
0156     int i;
0157 
0158     adc = devm_kzalloc(&spi->dev, sizeof(*adc), GFP_KERNEL);
0159     if (!adc)
0160         return -ENOMEM;
0161 
0162     /* set a default value for the reference */
0163     adc->reference = 3300;
0164     adc->channels = channels;
0165     mutex_init(&adc->lock);
0166 
0167     mutex_lock(&adc->lock);
0168 
0169     spi_set_drvdata(spi, adc);
0170 
0171     for (i = 0; i < 3 + adc->channels; i++) {
0172         status = device_create_file(&spi->dev, &ad_input[i].dev_attr);
0173         if (status) {
0174             dev_err(&spi->dev, "device_create_file failed.\n");
0175             goto out_err;
0176         }
0177     }
0178 
0179     adc->hwmon_dev = hwmon_device_register(&spi->dev);
0180     if (IS_ERR(adc->hwmon_dev)) {
0181         dev_err(&spi->dev, "hwmon_device_register failed.\n");
0182         status = PTR_ERR(adc->hwmon_dev);
0183         goto out_err;
0184     }
0185 
0186     mutex_unlock(&adc->lock);
0187     return 0;
0188 
0189 out_err:
0190     for (i--; i >= 0; i--)
0191         device_remove_file(&spi->dev, &ad_input[i].dev_attr);
0192 
0193     mutex_unlock(&adc->lock);
0194     return status;
0195 }
0196 
0197 static void adcxx_remove(struct spi_device *spi)
0198 {
0199     struct adcxx *adc = spi_get_drvdata(spi);
0200     int i;
0201 
0202     mutex_lock(&adc->lock);
0203     hwmon_device_unregister(adc->hwmon_dev);
0204     for (i = 0; i < 3 + adc->channels; i++)
0205         device_remove_file(&spi->dev, &ad_input[i].dev_attr);
0206 
0207     mutex_unlock(&adc->lock);
0208 }
0209 
0210 static const struct spi_device_id adcxx_ids[] = {
0211     { "adcxx1s", 1 },
0212     { "adcxx2s", 2 },
0213     { "adcxx4s", 4 },
0214     { "adcxx8s", 8 },
0215     { },
0216 };
0217 MODULE_DEVICE_TABLE(spi, adcxx_ids);
0218 
0219 static struct spi_driver adcxx_driver = {
0220     .driver = {
0221         .name   = "adcxx",
0222     },
0223     .id_table = adcxx_ids,
0224     .probe  = adcxx_probe,
0225     .remove = adcxx_remove,
0226 };
0227 
0228 module_spi_driver(adcxx_driver);
0229 
0230 MODULE_AUTHOR("Marc Pignat");
0231 MODULE_DESCRIPTION("National Semiconductor adcxx8sxxx Linux driver");
0232 MODULE_LICENSE("GPL");