linux/drivers/iio/dac/mcp4922.c
<<
>>
Prefs
   1/*
   2 * mcp4922.c
   3 *
   4 * Driver for Microchip Digital to Analog Converters.
   5 * Supports MCP4902, MCP4912, and MCP4922.
   6 *
   7 * Copyright (c) 2014 EMAC Inc.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/init.h>
  23#include <linux/spi/spi.h>
  24#include <linux/iio/iio.h>
  25#include <linux/iio/sysfs.h>
  26#include <linux/regulator/consumer.h>
  27#include <linux/bitops.h>
  28
  29#define MCP4922_NUM_CHANNELS    2
  30
  31enum mcp4922_supported_device_ids {
  32        ID_MCP4902,
  33        ID_MCP4912,
  34        ID_MCP4922,
  35};
  36
  37struct mcp4922_state {
  38        struct spi_device *spi;
  39        unsigned int value[MCP4922_NUM_CHANNELS];
  40        unsigned int vref_mv;
  41        struct regulator *vref_reg;
  42        u8 mosi[2] ____cacheline_aligned;
  43};
  44
  45#define MCP4922_CHAN(chan, bits) {                      \
  46        .type = IIO_VOLTAGE,                            \
  47        .output = 1,                                    \
  48        .indexed = 1,                                   \
  49        .channel = chan,                                \
  50        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
  51        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
  52        .scan_type = {                                  \
  53                .sign = 'u',                            \
  54                .realbits = (bits),                     \
  55                .storagebits = 16,                      \
  56                .shift = 12 - (bits),                   \
  57        },                                              \
  58}
  59
  60static int mcp4922_spi_write(struct mcp4922_state *state, u8 addr, u32 val)
  61{
  62        state->mosi[1] = val & 0xff;
  63        state->mosi[0] = (addr == 0) ? 0x00 : 0x80;
  64        state->mosi[0] |= 0x30 | ((val >> 8) & 0x0f);
  65
  66        return spi_write(state->spi, state->mosi, 2);
  67}
  68
  69static int mcp4922_read_raw(struct iio_dev *indio_dev,
  70                struct iio_chan_spec const *chan,
  71                int *val,
  72                int *val2,
  73                long mask)
  74{
  75        struct mcp4922_state *state = iio_priv(indio_dev);
  76
  77        switch (mask) {
  78        case IIO_CHAN_INFO_RAW:
  79                *val = state->value[chan->channel];
  80                return IIO_VAL_INT;
  81        case IIO_CHAN_INFO_SCALE:
  82                *val = state->vref_mv;
  83                *val2 = chan->scan_type.realbits;
  84                return IIO_VAL_FRACTIONAL_LOG2;
  85        default:
  86                return -EINVAL;
  87        }
  88}
  89
  90static int mcp4922_write_raw(struct iio_dev *indio_dev,
  91                struct iio_chan_spec const *chan,
  92                int val,
  93                int val2,
  94                long mask)
  95{
  96        struct mcp4922_state *state = iio_priv(indio_dev);
  97
  98        if (val2 != 0)
  99                return -EINVAL;
 100
 101        switch (mask) {
 102        case IIO_CHAN_INFO_RAW:
 103                if (val > GENMASK(chan->scan_type.realbits-1, 0))
 104                        return -EINVAL;
 105                val <<= chan->scan_type.shift;
 106                state->value[chan->channel] = val;
 107                return mcp4922_spi_write(state, chan->channel, val);
 108        default:
 109                return -EINVAL;
 110        }
 111}
 112
 113static const struct iio_chan_spec mcp4922_channels[3][MCP4922_NUM_CHANNELS] = {
 114        [ID_MCP4902] = { MCP4922_CHAN(0, 8),    MCP4922_CHAN(1, 8) },
 115        [ID_MCP4912] = { MCP4922_CHAN(0, 10),   MCP4922_CHAN(1, 10) },
 116        [ID_MCP4922] = { MCP4922_CHAN(0, 12),   MCP4922_CHAN(1, 12) },
 117};
 118
 119static const struct iio_info mcp4922_info = {
 120        .read_raw = &mcp4922_read_raw,
 121        .write_raw = &mcp4922_write_raw,
 122};
 123
 124static int mcp4922_probe(struct spi_device *spi)
 125{
 126        struct iio_dev *indio_dev;
 127        struct mcp4922_state *state;
 128        const struct spi_device_id *id;
 129        int ret;
 130
 131        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
 132        if (indio_dev == NULL)
 133                return -ENOMEM;
 134
 135        state = iio_priv(indio_dev);
 136        state->spi = spi;
 137        state->vref_reg = devm_regulator_get(&spi->dev, "vref");
 138        if (IS_ERR(state->vref_reg)) {
 139                dev_err(&spi->dev, "Vref regulator not specified\n");
 140                return PTR_ERR(state->vref_reg);
 141        }
 142
 143        ret = regulator_enable(state->vref_reg);
 144        if (ret) {
 145                dev_err(&spi->dev, "Failed to enable vref regulator: %d\n",
 146                                ret);
 147                return ret;
 148        }
 149
 150        ret = regulator_get_voltage(state->vref_reg);
 151        if (ret < 0) {
 152                dev_err(&spi->dev, "Failed to read vref regulator: %d\n",
 153                                ret);
 154                goto error_disable_reg;
 155        }
 156        state->vref_mv = ret / 1000;
 157
 158        spi_set_drvdata(spi, indio_dev);
 159        id = spi_get_device_id(spi);
 160        indio_dev->dev.parent = &spi->dev;
 161        indio_dev->info = &mcp4922_info;
 162        indio_dev->modes = INDIO_DIRECT_MODE;
 163        indio_dev->channels = mcp4922_channels[id->driver_data];
 164        indio_dev->num_channels = MCP4922_NUM_CHANNELS;
 165        indio_dev->name = id->name;
 166
 167        ret = iio_device_register(indio_dev);
 168        if (ret) {
 169                dev_err(&spi->dev, "Failed to register iio device: %d\n",
 170                                ret);
 171                goto error_disable_reg;
 172        }
 173
 174        return 0;
 175
 176error_disable_reg:
 177        regulator_disable(state->vref_reg);
 178
 179        return ret;
 180}
 181
 182static int mcp4922_remove(struct spi_device *spi)
 183{
 184        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 185        struct mcp4922_state *state;
 186
 187        iio_device_unregister(indio_dev);
 188        state = iio_priv(indio_dev);
 189        regulator_disable(state->vref_reg);
 190
 191        return 0;
 192}
 193
 194static const struct spi_device_id mcp4922_id[] = {
 195        {"mcp4902", ID_MCP4902},
 196        {"mcp4912", ID_MCP4912},
 197        {"mcp4922", ID_MCP4922},
 198        {}
 199};
 200MODULE_DEVICE_TABLE(spi, mcp4922_id);
 201
 202static struct spi_driver mcp4922_driver = {
 203        .driver = {
 204                   .name = "mcp4922",
 205                   },
 206        .probe = mcp4922_probe,
 207        .remove = mcp4922_remove,
 208        .id_table = mcp4922_id,
 209};
 210module_spi_driver(mcp4922_driver);
 211
 212MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");
 213MODULE_DESCRIPTION("Microchip MCP4902, MCP4912, MCP4922 DAC");
 214MODULE_LICENSE("GPL v2");
 215