1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max517.c - Support for Maxim MAX517, MAX518 and MAX519
5 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/jiffies.h>
11 #include <linux/i2c.h>
12 #include <linux/err.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/dac/max517.h>
18 #define MAX517_DRV_NAME "max517"
21 #define COMMAND_CHANNEL0 0x00
22 #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
23 #define COMMAND_PD 0x08 /* Power Down */
25 enum max517_device_ids
{
34 struct i2c_client
*client
;
35 unsigned short vref_mv
[8];
39 * channel: bit 0: channel 1
41 * (this way, it's possible to set both channels at once)
43 static int max517_set_value(struct iio_dev
*indio_dev
,
44 long val
, int channel
)
46 struct max517_data
*data
= iio_priv(indio_dev
);
47 struct i2c_client
*client
= data
->client
;
51 if (val
< 0 || val
> 255)
57 res
= i2c_master_send(client
, outbuf
, 2);
66 static int max517_read_raw(struct iio_dev
*indio_dev
,
67 struct iio_chan_spec
const *chan
,
72 struct max517_data
*data
= iio_priv(indio_dev
);
75 case IIO_CHAN_INFO_SCALE
:
76 /* Corresponds to Vref / 2^(bits) */
77 *val
= data
->vref_mv
[chan
->channel
];
79 return IIO_VAL_FRACTIONAL_LOG2
;
86 static int max517_write_raw(struct iio_dev
*indio_dev
,
87 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
92 case IIO_CHAN_INFO_RAW
:
93 ret
= max517_set_value(indio_dev
, val
, chan
->channel
);
103 static int __maybe_unused
max517_suspend(struct device
*dev
)
105 u8 outbuf
= COMMAND_PD
;
107 return i2c_master_send(to_i2c_client(dev
), &outbuf
, 1);
110 static int __maybe_unused
max517_resume(struct device
*dev
)
114 return i2c_master_send(to_i2c_client(dev
), &outbuf
, 1);
117 static SIMPLE_DEV_PM_OPS(max517_pm_ops
, max517_suspend
, max517_resume
);
119 static const struct iio_info max517_info
= {
120 .read_raw
= max517_read_raw
,
121 .write_raw
= max517_write_raw
,
124 #define MAX517_CHANNEL(chan) { \
125 .type = IIO_VOLTAGE, \
129 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
130 BIT(IIO_CHAN_INFO_SCALE), \
133 static const struct iio_chan_spec max517_channels
[] = {
144 static int max517_probe(struct i2c_client
*client
,
145 const struct i2c_device_id
*id
)
147 struct max517_data
*data
;
148 struct iio_dev
*indio_dev
;
149 struct max517_platform_data
*platform_data
= client
->dev
.platform_data
;
152 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
155 data
= iio_priv(indio_dev
);
156 i2c_set_clientdata(client
, indio_dev
);
157 data
->client
= client
;
159 /* establish that the iio_dev is a child of the i2c device */
160 indio_dev
->dev
.parent
= &client
->dev
;
162 switch (id
->driver_data
) {
164 indio_dev
->num_channels
= 8;
167 indio_dev
->num_channels
= 4;
171 indio_dev
->num_channels
= 2;
173 default: /* single channel for MAX517 */
174 indio_dev
->num_channels
= 1;
177 indio_dev
->channels
= max517_channels
;
178 indio_dev
->modes
= INDIO_DIRECT_MODE
;
179 indio_dev
->info
= &max517_info
;
182 * Reference voltage on MAX518 and default is 5V, else take vref_mv
185 for (chan
= 0; chan
< indio_dev
->num_channels
; chan
++) {
186 if (id
->driver_data
== ID_MAX518
|| !platform_data
)
187 data
->vref_mv
[chan
] = 5000; /* mV */
189 data
->vref_mv
[chan
] = platform_data
->vref_mv
[chan
];
192 return iio_device_register(indio_dev
);
195 static int max517_remove(struct i2c_client
*client
)
197 iio_device_unregister(i2c_get_clientdata(client
));
201 static const struct i2c_device_id max517_id
[] = {
202 { "max517", ID_MAX517
},
203 { "max518", ID_MAX518
},
204 { "max519", ID_MAX519
},
205 { "max520", ID_MAX520
},
206 { "max521", ID_MAX521
},
209 MODULE_DEVICE_TABLE(i2c
, max517_id
);
211 static struct i2c_driver max517_driver
= {
213 .name
= MAX517_DRV_NAME
,
214 .pm
= &max517_pm_ops
,
216 .probe
= max517_probe
,
217 .remove
= max517_remove
,
218 .id_table
= max517_id
,
220 module_i2c_driver(max517_driver
);
222 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
223 MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
224 MODULE_LICENSE("GPL");