iio: dac: ltc2632: add support for LTC2636 family
[linux-2.6-microblaze.git] / drivers / iio / dac / ltc2632.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LTC2632 Digital to analog convertors spi driver
4  *
5  * Copyright 2017 Maxime Roussin-BĂ©langer
6  * expanded by Silvan Murer <silvan.murer@gmail.com>
7  */
8
9 #include <linux/device.h>
10 #include <linux/spi/spi.h>
11 #include <linux/module.h>
12 #include <linux/iio/iio.h>
13 #include <linux/regulator/consumer.h>
14
15 #define LTC2632_ADDR_DAC0                       0x0
16 #define LTC2632_ADDR_DAC1                       0x1
17
18 #define LTC2632_CMD_WRITE_INPUT_N               0x0
19 #define LTC2632_CMD_UPDATE_DAC_N                0x1
20 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL    0x2
21 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_N      0x3
22 #define LTC2632_CMD_POWERDOWN_DAC_N             0x4
23 #define LTC2632_CMD_POWERDOWN_CHIP              0x5
24 #define LTC2632_CMD_INTERNAL_REFER              0x6
25 #define LTC2632_CMD_EXTERNAL_REFER              0x7
26
27 /**
28  * struct ltc2632_chip_info - chip specific information
29  * @channels:           channel spec for the DAC
30  * @vref_mv:            internal reference voltage
31  */
32 struct ltc2632_chip_info {
33         const struct iio_chan_spec *channels;
34         const size_t num_channels;
35         const int vref_mv;
36 };
37
38 /**
39  * struct ltc2632_state - driver instance specific data
40  * @spi_dev:                    pointer to the spi_device struct
41  * @powerdown_cache_mask        used to show current channel powerdown state
42  * @vref_mv                     used reference voltage (internal or external)
43  * @vref_reg            regulator for the reference voltage
44  */
45 struct ltc2632_state {
46         struct spi_device *spi_dev;
47         unsigned int powerdown_cache_mask;
48         int vref_mv;
49         struct regulator *vref_reg;
50 };
51
52 enum ltc2632_supported_device_ids {
53         ID_LTC2632L12,
54         ID_LTC2632L10,
55         ID_LTC2632L8,
56         ID_LTC2632H12,
57         ID_LTC2632H10,
58         ID_LTC2632H8,
59         ID_LTC2636L12,
60         ID_LTC2636L10,
61         ID_LTC2636L8,
62         ID_LTC2636H12,
63         ID_LTC2636H10,
64         ID_LTC2636H8,
65 };
66
67 static int ltc2632_spi_write(struct spi_device *spi,
68                              u8 cmd, u8 addr, u16 val, u8 shift)
69 {
70         u32 data;
71         u8 msg[3];
72
73         /*
74          * The input shift register is 24 bits wide.
75          * The next four are the command bits, C3 to C0,
76          * followed by the 4-bit DAC address, A3 to A0, and then the
77          * 12-, 10-, 8-bit data-word. The data-word comprises the 12-,
78          * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits.
79          */
80         data = (cmd << 20) | (addr << 16) | (val << shift);
81         msg[0] = data >> 16;
82         msg[1] = data >> 8;
83         msg[2] = data;
84
85         return spi_write(spi, msg, sizeof(msg));
86 }
87
88 static int ltc2632_read_raw(struct iio_dev *indio_dev,
89                             struct iio_chan_spec const *chan,
90                             int *val,
91                             int *val2,
92                             long m)
93 {
94         const struct ltc2632_state *st = iio_priv(indio_dev);
95
96         switch (m) {
97         case IIO_CHAN_INFO_SCALE:
98                 *val = st->vref_mv;
99                 *val2 = chan->scan_type.realbits;
100                 return IIO_VAL_FRACTIONAL_LOG2;
101         }
102         return -EINVAL;
103 }
104
105 static int ltc2632_write_raw(struct iio_dev *indio_dev,
106                              struct iio_chan_spec const *chan,
107                              int val,
108                              int val2,
109                              long mask)
110 {
111         struct ltc2632_state *st = iio_priv(indio_dev);
112
113         switch (mask) {
114         case IIO_CHAN_INFO_RAW:
115                 if (val >= (1 << chan->scan_type.realbits) || val < 0)
116                         return -EINVAL;
117
118                 return ltc2632_spi_write(st->spi_dev,
119                                          LTC2632_CMD_WRITE_INPUT_N_UPDATE_N,
120                                          chan->address, val,
121                                          chan->scan_type.shift);
122         default:
123                 return -EINVAL;
124         }
125 }
126
127 static ssize_t ltc2632_read_dac_powerdown(struct iio_dev *indio_dev,
128                                           uintptr_t private,
129                                           const struct iio_chan_spec *chan,
130                                           char *buf)
131 {
132         struct ltc2632_state *st = iio_priv(indio_dev);
133
134         return sprintf(buf, "%d\n",
135                        !!(st->powerdown_cache_mask & (1 << chan->channel)));
136 }
137
138 static ssize_t ltc2632_write_dac_powerdown(struct iio_dev *indio_dev,
139                                            uintptr_t private,
140                                            const struct iio_chan_spec *chan,
141                                            const char *buf,
142                                            size_t len)
143 {
144         bool pwr_down;
145         int ret;
146         struct ltc2632_state *st = iio_priv(indio_dev);
147
148         ret = strtobool(buf, &pwr_down);
149         if (ret)
150                 return ret;
151
152         if (pwr_down)
153                 st->powerdown_cache_mask |= (1 << chan->channel);
154         else
155                 st->powerdown_cache_mask &= ~(1 << chan->channel);
156
157         ret = ltc2632_spi_write(st->spi_dev,
158                                 LTC2632_CMD_POWERDOWN_DAC_N,
159                                 chan->channel, 0, 0);
160
161         return ret ? ret : len;
162 }
163
164 static const struct iio_info ltc2632_info = {
165         .write_raw      = ltc2632_write_raw,
166         .read_raw       = ltc2632_read_raw,
167 };
168
169 static const struct iio_chan_spec_ext_info ltc2632_ext_info[] = {
170         {
171                 .name = "powerdown",
172                 .read = ltc2632_read_dac_powerdown,
173                 .write = ltc2632_write_dac_powerdown,
174                 .shared = IIO_SEPARATE,
175         },
176         { },
177 };
178
179 #define LTC2632_CHANNEL(_chan, _bits) { \
180                 .type = IIO_VOLTAGE, \
181                 .indexed = 1, \
182                 .output = 1, \
183                 .channel = (_chan), \
184                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
185                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
186                 .address = (_chan), \
187                 .scan_type = { \
188                         .realbits       = (_bits), \
189                         .shift          = 16 - (_bits), \
190                 }, \
191                 .ext_info = ltc2632_ext_info, \
192 }
193
194 #define DECLARE_LTC2632_CHANNELS(_name, _bits) \
195         const struct iio_chan_spec _name ## _channels[] = { \
196                 LTC2632_CHANNEL(0, _bits), \
197                 LTC2632_CHANNEL(1, _bits), \
198                 LTC2632_CHANNEL(2, _bits), \
199                 LTC2632_CHANNEL(3, _bits), \
200                 LTC2632_CHANNEL(4, _bits), \
201                 LTC2632_CHANNEL(5, _bits), \
202                 LTC2632_CHANNEL(6, _bits), \
203                 LTC2632_CHANNEL(7, _bits), \
204         }
205
206 static DECLARE_LTC2632_CHANNELS(ltc2632x12, 12);
207 static DECLARE_LTC2632_CHANNELS(ltc2632x10, 10);
208 static DECLARE_LTC2632_CHANNELS(ltc2632x8, 8);
209
210 static const struct ltc2632_chip_info ltc2632_chip_info_tbl[] = {
211         [ID_LTC2632L12] = {
212                 .channels       = ltc2632x12_channels,
213                 .num_channels   = 2,
214                 .vref_mv        = 2500,
215         },
216         [ID_LTC2632L10] = {
217                 .channels       = ltc2632x10_channels,
218                 .num_channels   = 2,
219                 .vref_mv        = 2500,
220         },
221         [ID_LTC2632L8] =  {
222                 .channels       = ltc2632x8_channels,
223                 .num_channels   = 2,
224                 .vref_mv        = 2500,
225         },
226         [ID_LTC2632H12] = {
227                 .channels       = ltc2632x12_channels,
228                 .num_channels   = 2,
229                 .vref_mv        = 4096,
230         },
231         [ID_LTC2632H10] = {
232                 .channels       = ltc2632x10_channels,
233                 .num_channels   = 2,
234                 .vref_mv        = 4096,
235         },
236         [ID_LTC2632H8] =  {
237                 .channels       = ltc2632x8_channels,
238                 .num_channels   = 2,
239                 .vref_mv        = 4096,
240         },
241         [ID_LTC2636L12] = {
242                 .channels       = ltc2632x12_channels,
243                 .num_channels   = 8,
244                 .vref_mv        = 2500,
245         },
246         [ID_LTC2636L10] = {
247                 .channels       = ltc2632x10_channels,
248                 .num_channels   = 8,
249                 .vref_mv        = 2500,
250         },
251         [ID_LTC2636L8] =  {
252                 .channels       = ltc2632x8_channels,
253                 .num_channels   = 8,
254                 .vref_mv        = 2500,
255         },
256         [ID_LTC2636H12] = {
257                 .channels       = ltc2632x12_channels,
258                 .num_channels   = 8,
259                 .vref_mv        = 4096,
260         },
261         [ID_LTC2636H10] = {
262                 .channels       = ltc2632x10_channels,
263                 .num_channels   = 8,
264                 .vref_mv        = 4096,
265         },
266         [ID_LTC2636H8] =  {
267                 .channels       = ltc2632x8_channels,
268                 .num_channels   = 8,
269                 .vref_mv        = 4096,
270         },
271 };
272
273 static int ltc2632_probe(struct spi_device *spi)
274 {
275         struct ltc2632_state *st;
276         struct iio_dev *indio_dev;
277         struct ltc2632_chip_info *chip_info;
278         int ret;
279
280         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
281         if (!indio_dev)
282                 return -ENOMEM;
283
284         st = iio_priv(indio_dev);
285
286         spi_set_drvdata(spi, indio_dev);
287         st->spi_dev = spi;
288
289         chip_info = (struct ltc2632_chip_info *)
290                         spi_get_device_id(spi)->driver_data;
291
292         st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref");
293         if (PTR_ERR(st->vref_reg) == -ENODEV) {
294                 /* use internal reference voltage */
295                 st->vref_reg = NULL;
296                 st->vref_mv = chip_info->vref_mv;
297
298                 ret = ltc2632_spi_write(spi, LTC2632_CMD_INTERNAL_REFER,
299                                 0, 0, 0);
300                 if (ret) {
301                         dev_err(&spi->dev,
302                                 "Set internal reference command failed, %d\n",
303                                 ret);
304                         return ret;
305                 }
306         } else if (IS_ERR(st->vref_reg)) {
307                 dev_err(&spi->dev,
308                                 "Error getting voltage reference regulator\n");
309                 return PTR_ERR(st->vref_reg);
310         } else {
311                 /* use external reference voltage */
312                 ret = regulator_enable(st->vref_reg);
313                 if (ret) {
314                         dev_err(&spi->dev,
315                                 "enable reference regulator failed, %d\n",
316                                 ret);
317                         return ret;
318                 }
319                 st->vref_mv = regulator_get_voltage(st->vref_reg) / 1000;
320
321                 ret = ltc2632_spi_write(spi, LTC2632_CMD_EXTERNAL_REFER,
322                                 0, 0, 0);
323                 if (ret) {
324                         dev_err(&spi->dev,
325                                 "Set external reference command failed, %d\n",
326                                 ret);
327                         return ret;
328                 }
329         }
330
331         indio_dev->dev.parent = &spi->dev;
332         indio_dev->name = dev_of_node(&spi->dev) ? dev_of_node(&spi->dev)->name
333                                                  : spi_get_device_id(spi)->name;
334         indio_dev->info = &ltc2632_info;
335         indio_dev->modes = INDIO_DIRECT_MODE;
336         indio_dev->channels = chip_info->channels;
337         indio_dev->num_channels = chip_info->num_channels;
338
339         return iio_device_register(indio_dev);
340 }
341
342 static int ltc2632_remove(struct spi_device *spi)
343 {
344         struct iio_dev *indio_dev = spi_get_drvdata(spi);
345         struct ltc2632_state *st = iio_priv(indio_dev);
346
347         iio_device_unregister(indio_dev);
348
349         if (st->vref_reg)
350                 regulator_disable(st->vref_reg);
351
352         return 0;
353 }
354
355 static const struct spi_device_id ltc2632_id[] = {
356         { "ltc2632-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L12] },
357         { "ltc2632-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L10] },
358         { "ltc2632-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632L8] },
359         { "ltc2632-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H12] },
360         { "ltc2632-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H10] },
361         { "ltc2632-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H8] },
362         { "ltc2636-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L12] },
363         { "ltc2636-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L10] },
364         { "ltc2636-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L8] },
365         { "ltc2636-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H12] },
366         { "ltc2636-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H10] },
367         { "ltc2636-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636H8] },
368         {}
369 };
370 MODULE_DEVICE_TABLE(spi, ltc2632_id);
371
372 static const struct of_device_id ltc2632_of_match[] = {
373         {
374                 .compatible = "lltc,ltc2632-l12",
375                 .data = &ltc2632_chip_info_tbl[ID_LTC2632L12]
376         }, {
377                 .compatible = "lltc,ltc2632-l10",
378                 .data = &ltc2632_chip_info_tbl[ID_LTC2632L10]
379         }, {
380                 .compatible = "lltc,ltc2632-l8",
381                 .data = &ltc2632_chip_info_tbl[ID_LTC2632L8]
382         }, {
383                 .compatible = "lltc,ltc2632-h12",
384                 .data = &ltc2632_chip_info_tbl[ID_LTC2632H12]
385         }, {
386                 .compatible = "lltc,ltc2632-h10",
387                 .data = &ltc2632_chip_info_tbl[ID_LTC2632H10]
388         }, {
389                 .compatible = "lltc,ltc2632-h8",
390                 .data = &ltc2632_chip_info_tbl[ID_LTC2632H8]
391         }, {
392                 .compatible = "lltc,ltc2636-l12",
393                 .data = &ltc2632_chip_info_tbl[ID_LTC2636L12]
394         }, {
395                 .compatible = "lltc,ltc2636-l10",
396                 .data = &ltc2632_chip_info_tbl[ID_LTC2636L10]
397         }, {
398                 .compatible = "lltc,ltc2636-l8",
399                 .data = &ltc2632_chip_info_tbl[ID_LTC2636L8]
400         }, {
401                 .compatible = "lltc,ltc2636-h12",
402                 .data = &ltc2632_chip_info_tbl[ID_LTC2636H12]
403         }, {
404                 .compatible = "lltc,ltc2636-h10",
405                 .data = &ltc2632_chip_info_tbl[ID_LTC2636H10]
406         }, {
407                 .compatible = "lltc,ltc2636-h8",
408                 .data = &ltc2632_chip_info_tbl[ID_LTC2636H8]
409         },
410         {}
411 };
412 MODULE_DEVICE_TABLE(of, ltc2632_of_match);
413
414 static struct spi_driver ltc2632_driver = {
415         .driver         = {
416                 .name   = "ltc2632",
417                 .of_match_table = of_match_ptr(ltc2632_of_match),
418         },
419         .probe          = ltc2632_probe,
420         .remove         = ltc2632_remove,
421         .id_table       = ltc2632_id,
422 };
423 module_spi_driver(ltc2632_driver);
424
425 MODULE_AUTHOR("Maxime Roussin-Belanger <maxime.roussinbelanger@gmail.com>");
426 MODULE_DESCRIPTION("LTC2632 DAC SPI driver");
427 MODULE_LICENSE("GPL v2");