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