linux/drivers/iio/dac/ad5624r_spi.c
<<
>>
Prefs
   1/*
   2 * AD5624R, AD5644R, AD5664R Digital to analog convertors spi driver
   3 *
   4 * Copyright 2010-2011 Analog Devices Inc.
   5 *
   6 * Licensed under the GPL-2.
   7 */
   8
   9#include <linux/interrupt.h>
  10#include <linux/fs.h>
  11#include <linux/device.h>
  12#include <linux/kernel.h>
  13#include <linux/spi/spi.h>
  14#include <linux/slab.h>
  15#include <linux/sysfs.h>
  16#include <linux/regulator/consumer.h>
  17#include <linux/module.h>
  18
  19#include <linux/iio/iio.h>
  20#include <linux/iio/sysfs.h>
  21
  22#include "ad5624r.h"
  23
  24static int ad5624r_spi_write(struct spi_device *spi,
  25                             u8 cmd, u8 addr, u16 val, u8 shift)
  26{
  27        u32 data;
  28        u8 msg[3];
  29
  30        /*
  31         * The input shift register is 24 bits wide. The first two bits are
  32         * don't care bits. The next three are the command bits, C2 to C0,
  33         * followed by the 3-bit DAC address, A2 to A0, and then the
  34         * 16-, 14-, 12-bit data-word. The data-word comprises the 16-,
  35         * 14-, 12-bit input code followed by 0, 2, or 4 don't care bits,
  36         * for the AD5664R, AD5644R, and AD5624R, respectively.
  37         */
  38        data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << shift);
  39        msg[0] = data >> 16;
  40        msg[1] = data >> 8;
  41        msg[2] = data;
  42
  43        return spi_write(spi, msg, 3);
  44}
  45
  46static int ad5624r_read_raw(struct iio_dev *indio_dev,
  47                           struct iio_chan_spec const *chan,
  48                           int *val,
  49                           int *val2,
  50                           long m)
  51{
  52        struct ad5624r_state *st = iio_priv(indio_dev);
  53
  54        switch (m) {
  55        case IIO_CHAN_INFO_SCALE:
  56                *val = st->vref_mv;
  57                *val2 = chan->scan_type.realbits;
  58                return IIO_VAL_FRACTIONAL_LOG2;
  59        }
  60        return -EINVAL;
  61}
  62
  63static int ad5624r_write_raw(struct iio_dev *indio_dev,
  64                               struct iio_chan_spec const *chan,
  65                               int val,
  66                               int val2,
  67                               long mask)
  68{
  69        struct ad5624r_state *st = iio_priv(indio_dev);
  70
  71        switch (mask) {
  72        case IIO_CHAN_INFO_RAW:
  73                if (val >= (1 << chan->scan_type.realbits) || val < 0)
  74                        return -EINVAL;
  75
  76                return ad5624r_spi_write(st->us,
  77                                AD5624R_CMD_WRITE_INPUT_N_UPDATE_N,
  78                                chan->address, val,
  79                                chan->scan_type.shift);
  80        default:
  81                return -EINVAL;
  82        }
  83}
  84
  85static const char * const ad5624r_powerdown_modes[] = {
  86        "1kohm_to_gnd",
  87        "100kohm_to_gnd",
  88        "three_state"
  89};
  90
  91static int ad5624r_get_powerdown_mode(struct iio_dev *indio_dev,
  92        const struct iio_chan_spec *chan)
  93{
  94        struct ad5624r_state *st = iio_priv(indio_dev);
  95
  96        return st->pwr_down_mode;
  97}
  98
  99static int ad5624r_set_powerdown_mode(struct iio_dev *indio_dev,
 100        const struct iio_chan_spec *chan, unsigned int mode)
 101{
 102        struct ad5624r_state *st = iio_priv(indio_dev);
 103
 104        st->pwr_down_mode = mode;
 105
 106        return 0;
 107}
 108
 109static const struct iio_enum ad5624r_powerdown_mode_enum = {
 110        .items = ad5624r_powerdown_modes,
 111        .num_items = ARRAY_SIZE(ad5624r_powerdown_modes),
 112        .get = ad5624r_get_powerdown_mode,
 113        .set = ad5624r_set_powerdown_mode,
 114};
 115
 116static ssize_t ad5624r_read_dac_powerdown(struct iio_dev *indio_dev,
 117        uintptr_t private, const struct iio_chan_spec *chan, char *buf)
 118{
 119        struct ad5624r_state *st = iio_priv(indio_dev);
 120
 121        return sprintf(buf, "%d\n",
 122                        !!(st->pwr_down_mask & (1 << chan->channel)));
 123}
 124
 125static ssize_t ad5624r_write_dac_powerdown(struct iio_dev *indio_dev,
 126        uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
 127        size_t len)
 128{
 129        bool pwr_down;
 130        int ret;
 131        struct ad5624r_state *st = iio_priv(indio_dev);
 132
 133        ret = strtobool(buf, &pwr_down);
 134        if (ret)
 135                return ret;
 136
 137        if (pwr_down)
 138                st->pwr_down_mask |= (1 << chan->channel);
 139        else
 140                st->pwr_down_mask &= ~(1 << chan->channel);
 141
 142        ret = ad5624r_spi_write(st->us, AD5624R_CMD_POWERDOWN_DAC, 0,
 143                                (st->pwr_down_mode << 4) |
 144                                st->pwr_down_mask, 16);
 145
 146        return ret ? ret : len;
 147}
 148
 149static const struct iio_info ad5624r_info = {
 150        .write_raw = ad5624r_write_raw,
 151        .read_raw = ad5624r_read_raw,
 152};
 153
 154static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
 155        {
 156                .name = "powerdown",
 157                .read = ad5624r_read_dac_powerdown,
 158                .write = ad5624r_write_dac_powerdown,
 159                .shared = IIO_SEPARATE,
 160        },
 161        IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE,
 162                 &ad5624r_powerdown_mode_enum),
 163        IIO_ENUM_AVAILABLE("powerdown_mode", &ad5624r_powerdown_mode_enum),
 164        { },
 165};
 166
 167#define AD5624R_CHANNEL(_chan, _bits) { \
 168        .type = IIO_VOLTAGE, \
 169        .indexed = 1, \
 170        .output = 1, \
 171        .channel = (_chan), \
 172        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 173        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 174        .address = (_chan), \
 175        .scan_type = { \
 176                .sign = 'u', \
 177                .realbits = (_bits), \
 178                .storagebits = 16, \
 179                .shift = 16 - (_bits), \
 180        }, \
 181        .ext_info = ad5624r_ext_info, \
 182}
 183
 184#define DECLARE_AD5624R_CHANNELS(_name, _bits) \
 185        const struct iio_chan_spec _name##_channels[] = { \
 186                AD5624R_CHANNEL(0, _bits), \
 187                AD5624R_CHANNEL(1, _bits), \
 188                AD5624R_CHANNEL(2, _bits), \
 189                AD5624R_CHANNEL(3, _bits), \
 190}
 191
 192static DECLARE_AD5624R_CHANNELS(ad5624r, 12);
 193static DECLARE_AD5624R_CHANNELS(ad5644r, 14);
 194static DECLARE_AD5624R_CHANNELS(ad5664r, 16);
 195
 196static const struct ad5624r_chip_info ad5624r_chip_info_tbl[] = {
 197        [ID_AD5624R3] = {
 198                .channels = ad5624r_channels,
 199                .int_vref_mv = 1250,
 200        },
 201        [ID_AD5624R5] = {
 202                .channels = ad5624r_channels,
 203                .int_vref_mv = 2500,
 204        },
 205        [ID_AD5644R3] = {
 206                .channels = ad5644r_channels,
 207                .int_vref_mv = 1250,
 208        },
 209        [ID_AD5644R5] = {
 210                .channels = ad5644r_channels,
 211                .int_vref_mv = 2500,
 212        },
 213        [ID_AD5664R3] = {
 214                .channels = ad5664r_channels,
 215                .int_vref_mv = 1250,
 216        },
 217        [ID_AD5664R5] = {
 218                .channels = ad5664r_channels,
 219                .int_vref_mv = 2500,
 220        },
 221};
 222
 223static int ad5624r_probe(struct spi_device *spi)
 224{
 225        struct ad5624r_state *st;
 226        struct iio_dev *indio_dev;
 227        int ret, voltage_uv = 0;
 228
 229        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 230        if (!indio_dev)
 231                return -ENOMEM;
 232        st = iio_priv(indio_dev);
 233        st->reg = devm_regulator_get(&spi->dev, "vcc");
 234        if (!IS_ERR(st->reg)) {
 235                ret = regulator_enable(st->reg);
 236                if (ret)
 237                        return ret;
 238
 239                ret = regulator_get_voltage(st->reg);
 240                if (ret < 0)
 241                        goto error_disable_reg;
 242
 243                voltage_uv = ret;
 244        }
 245
 246        spi_set_drvdata(spi, indio_dev);
 247        st->chip_info =
 248                &ad5624r_chip_info_tbl[spi_get_device_id(spi)->driver_data];
 249
 250        if (voltage_uv)
 251                st->vref_mv = voltage_uv / 1000;
 252        else
 253                st->vref_mv = st->chip_info->int_vref_mv;
 254
 255        st->us = spi;
 256
 257        indio_dev->dev.parent = &spi->dev;
 258        indio_dev->name = spi_get_device_id(spi)->name;
 259        indio_dev->info = &ad5624r_info;
 260        indio_dev->modes = INDIO_DIRECT_MODE;
 261        indio_dev->channels = st->chip_info->channels;
 262        indio_dev->num_channels = AD5624R_DAC_CHANNELS;
 263
 264        ret = ad5624r_spi_write(spi, AD5624R_CMD_INTERNAL_REFER_SETUP, 0,
 265                                !!voltage_uv, 16);
 266        if (ret)
 267                goto error_disable_reg;
 268
 269        ret = iio_device_register(indio_dev);
 270        if (ret)
 271                goto error_disable_reg;
 272
 273        return 0;
 274
 275error_disable_reg:
 276        if (!IS_ERR(st->reg))
 277                regulator_disable(st->reg);
 278
 279        return ret;
 280}
 281
 282static int ad5624r_remove(struct spi_device *spi)
 283{
 284        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 285        struct ad5624r_state *st = iio_priv(indio_dev);
 286
 287        iio_device_unregister(indio_dev);
 288        if (!IS_ERR(st->reg))
 289                regulator_disable(st->reg);
 290
 291        return 0;
 292}
 293
 294static const struct spi_device_id ad5624r_id[] = {
 295        {"ad5624r3", ID_AD5624R3},
 296        {"ad5644r3", ID_AD5644R3},
 297        {"ad5664r3", ID_AD5664R3},
 298        {"ad5624r5", ID_AD5624R5},
 299        {"ad5644r5", ID_AD5644R5},
 300        {"ad5664r5", ID_AD5664R5},
 301        {}
 302};
 303MODULE_DEVICE_TABLE(spi, ad5624r_id);
 304
 305static struct spi_driver ad5624r_driver = {
 306        .driver = {
 307                   .name = "ad5624r",
 308                   },
 309        .probe = ad5624r_probe,
 310        .remove = ad5624r_remove,
 311        .id_table = ad5624r_id,
 312};
 313module_spi_driver(ad5624r_driver);
 314
 315MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 316MODULE_DESCRIPTION("Analog Devices AD5624/44/64R DAC spi driver");
 317MODULE_LICENSE("GPL v2");
 318