4 * Driver for Microchip Digital to Analog Converters.
5 * Supports MCP4902, MCP4912, and MCP4922.
7 * Copyright (c) 2014 EMAC Inc.
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.
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.
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>
29 #define MCP4922_NUM_CHANNELS 2
31 enum mcp4922_supported_device_ids
{
37 struct mcp4922_state
{
38 struct spi_device
*spi
;
39 unsigned int value
[MCP4922_NUM_CHANNELS
];
41 struct regulator
*vref_reg
;
42 u8 mosi
[2] ____cacheline_aligned
;
45 #define MCP4922_CHAN(chan, bits) { \
46 .type = IIO_VOLTAGE, \
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
56 .shift = 12 - (bits), \
60 static int mcp4922_spi_write(struct mcp4922_state
*state
, u8 addr
, u32 val
)
62 state
->mosi
[1] = val
& 0xff;
63 state
->mosi
[0] = (addr
== 0) ? 0x00 : 0x80;
64 state
->mosi
[0] |= 0x30 | ((val
>> 8) & 0x0f);
66 return spi_write(state
->spi
, state
->mosi
, 2);
69 static int mcp4922_read_raw(struct iio_dev
*indio_dev
,
70 struct iio_chan_spec
const *chan
,
75 struct mcp4922_state
*state
= iio_priv(indio_dev
);
78 case IIO_CHAN_INFO_RAW
:
79 *val
= state
->value
[chan
->channel
];
81 case IIO_CHAN_INFO_SCALE
:
82 *val
= state
->vref_mv
;
83 *val2
= chan
->scan_type
.realbits
;
84 return IIO_VAL_FRACTIONAL_LOG2
;
90 static int mcp4922_write_raw(struct iio_dev
*indio_dev
,
91 struct iio_chan_spec
const *chan
,
96 struct mcp4922_state
*state
= iio_priv(indio_dev
);
103 case IIO_CHAN_INFO_RAW
:
104 if (val
< 0 || val
> GENMASK(chan
->scan_type
.realbits
- 1, 0))
106 val
<<= chan
->scan_type
.shift
;
108 ret
= mcp4922_spi_write(state
, chan
->channel
, val
);
110 state
->value
[chan
->channel
] = val
;
118 static const struct iio_chan_spec mcp4922_channels
[3][MCP4922_NUM_CHANNELS
] = {
119 [ID_MCP4902
] = { MCP4922_CHAN(0, 8), MCP4922_CHAN(1, 8) },
120 [ID_MCP4912
] = { MCP4922_CHAN(0, 10), MCP4922_CHAN(1, 10) },
121 [ID_MCP4922
] = { MCP4922_CHAN(0, 12), MCP4922_CHAN(1, 12) },
124 static const struct iio_info mcp4922_info
= {
125 .read_raw
= &mcp4922_read_raw
,
126 .write_raw
= &mcp4922_write_raw
,
129 static int mcp4922_probe(struct spi_device
*spi
)
131 struct iio_dev
*indio_dev
;
132 struct mcp4922_state
*state
;
133 const struct spi_device_id
*id
;
136 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
137 if (indio_dev
== NULL
)
140 state
= iio_priv(indio_dev
);
142 state
->vref_reg
= devm_regulator_get(&spi
->dev
, "vref");
143 if (IS_ERR(state
->vref_reg
)) {
144 dev_err(&spi
->dev
, "Vref regulator not specified\n");
145 return PTR_ERR(state
->vref_reg
);
148 ret
= regulator_enable(state
->vref_reg
);
150 dev_err(&spi
->dev
, "Failed to enable vref regulator: %d\n",
155 ret
= regulator_get_voltage(state
->vref_reg
);
157 dev_err(&spi
->dev
, "Failed to read vref regulator: %d\n",
159 goto error_disable_reg
;
161 state
->vref_mv
= ret
/ 1000;
163 spi_set_drvdata(spi
, indio_dev
);
164 id
= spi_get_device_id(spi
);
165 indio_dev
->dev
.parent
= &spi
->dev
;
166 indio_dev
->info
= &mcp4922_info
;
167 indio_dev
->modes
= INDIO_DIRECT_MODE
;
168 indio_dev
->channels
= mcp4922_channels
[id
->driver_data
];
169 indio_dev
->num_channels
= MCP4922_NUM_CHANNELS
;
170 indio_dev
->name
= id
->name
;
172 ret
= iio_device_register(indio_dev
);
174 dev_err(&spi
->dev
, "Failed to register iio device: %d\n",
176 goto error_disable_reg
;
182 regulator_disable(state
->vref_reg
);
187 static int mcp4922_remove(struct spi_device
*spi
)
189 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
190 struct mcp4922_state
*state
;
192 iio_device_unregister(indio_dev
);
193 state
= iio_priv(indio_dev
);
194 regulator_disable(state
->vref_reg
);
199 static const struct spi_device_id mcp4922_id
[] = {
200 {"mcp4902", ID_MCP4902
},
201 {"mcp4912", ID_MCP4912
},
202 {"mcp4922", ID_MCP4922
},
205 MODULE_DEVICE_TABLE(spi
, mcp4922_id
);
207 static struct spi_driver mcp4922_driver
= {
211 .probe
= mcp4922_probe
,
212 .remove
= mcp4922_remove
,
213 .id_table
= mcp4922_id
,
215 module_spi_driver(mcp4922_driver
);
217 MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");
218 MODULE_DESCRIPTION("Microchip MCP4902, MCP4912, MCP4922 DAC");
219 MODULE_LICENSE("GPL v2");