1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2014 Philippe Reynes
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/iio/iio.h>
11 #include <linux/regulator/consumer.h>
13 #define MAX5821_MAX_DAC_CHANNELS 2
16 #define MAX5821_LOAD_DAC_A_IN_REG_B 0x00
17 #define MAX5821_LOAD_DAC_B_IN_REG_A 0x10
18 #define MAX5821_EXTENDED_COMMAND_MODE 0xf0
19 #define MAX5821_READ_DAC_A_COMMAND 0xf1
20 #define MAX5821_READ_DAC_B_COMMAND 0xf2
22 #define MAX5821_EXTENDED_POWER_UP 0x00
23 #define MAX5821_EXTENDED_POWER_DOWN_MODE0 0x01
24 #define MAX5821_EXTENDED_POWER_DOWN_MODE1 0x02
25 #define MAX5821_EXTENDED_POWER_DOWN_MODE2 0x03
26 #define MAX5821_EXTENDED_DAC_A 0x04
27 #define MAX5821_EXTENDED_DAC_B 0x08
29 enum max5821_device_ids
{
34 struct i2c_client
*client
;
35 struct regulator
*vref_reg
;
36 unsigned short vref_mv
;
37 bool powerdown
[MAX5821_MAX_DAC_CHANNELS
];
38 u8 powerdown_mode
[MAX5821_MAX_DAC_CHANNELS
];
42 static const char * const max5821_powerdown_modes
[] = {
51 MAX5821_100KOHM_TO_GND
54 static int max5821_get_powerdown_mode(struct iio_dev
*indio_dev
,
55 const struct iio_chan_spec
*chan
)
57 struct max5821_data
*st
= iio_priv(indio_dev
);
59 return st
->powerdown_mode
[chan
->channel
];
62 static int max5821_set_powerdown_mode(struct iio_dev
*indio_dev
,
63 const struct iio_chan_spec
*chan
,
66 struct max5821_data
*st
= iio_priv(indio_dev
);
68 st
->powerdown_mode
[chan
->channel
] = mode
;
73 static const struct iio_enum max5821_powerdown_mode_enum
= {
74 .items
= max5821_powerdown_modes
,
75 .num_items
= ARRAY_SIZE(max5821_powerdown_modes
),
76 .get
= max5821_get_powerdown_mode
,
77 .set
= max5821_set_powerdown_mode
,
80 static ssize_t
max5821_read_dac_powerdown(struct iio_dev
*indio_dev
,
82 const struct iio_chan_spec
*chan
,
85 struct max5821_data
*st
= iio_priv(indio_dev
);
87 return sprintf(buf
, "%d\n", st
->powerdown
[chan
->channel
]);
90 static int max5821_sync_powerdown_mode(struct max5821_data
*data
,
91 const struct iio_chan_spec
*chan
)
95 outbuf
[0] = MAX5821_EXTENDED_COMMAND_MODE
;
97 if (chan
->channel
== 0)
98 outbuf
[1] = MAX5821_EXTENDED_DAC_A
;
100 outbuf
[1] = MAX5821_EXTENDED_DAC_B
;
102 if (data
->powerdown
[chan
->channel
])
103 outbuf
[1] |= data
->powerdown_mode
[chan
->channel
] + 1;
105 outbuf
[1] |= MAX5821_EXTENDED_POWER_UP
;
107 return i2c_master_send(data
->client
, outbuf
, 2);
110 static ssize_t
max5821_write_dac_powerdown(struct iio_dev
*indio_dev
,
112 const struct iio_chan_spec
*chan
,
113 const char *buf
, size_t len
)
115 struct max5821_data
*data
= iio_priv(indio_dev
);
119 ret
= strtobool(buf
, &powerdown
);
123 data
->powerdown
[chan
->channel
] = powerdown
;
125 ret
= max5821_sync_powerdown_mode(data
, chan
);
132 static const struct iio_chan_spec_ext_info max5821_ext_info
[] = {
135 .read
= max5821_read_dac_powerdown
,
136 .write
= max5821_write_dac_powerdown
,
137 .shared
= IIO_SEPARATE
,
139 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &max5821_powerdown_mode_enum
),
140 IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum
),
144 #define MAX5821_CHANNEL(chan) { \
145 .type = IIO_VOLTAGE, \
149 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
150 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \
151 .ext_info = max5821_ext_info, \
154 static const struct iio_chan_spec max5821_channels
[] = {
159 static const u8 max5821_read_dac_command
[] = {
160 MAX5821_READ_DAC_A_COMMAND
,
161 MAX5821_READ_DAC_B_COMMAND
164 static const u8 max5821_load_dac_command
[] = {
165 MAX5821_LOAD_DAC_A_IN_REG_B
,
166 MAX5821_LOAD_DAC_B_IN_REG_A
169 static int max5821_get_value(struct iio_dev
*indio_dev
,
170 int *val
, int channel
)
172 struct max5821_data
*data
= iio_priv(indio_dev
);
173 struct i2c_client
*client
= data
->client
;
178 if ((channel
!= 0) && (channel
!= 1))
181 outbuf
[0] = max5821_read_dac_command
[channel
];
183 mutex_lock(&data
->lock
);
185 ret
= i2c_master_send(client
, outbuf
, 1);
187 mutex_unlock(&data
->lock
);
189 } else if (ret
!= 1) {
190 mutex_unlock(&data
->lock
);
194 ret
= i2c_master_recv(client
, inbuf
, 2);
196 mutex_unlock(&data
->lock
);
198 } else if (ret
!= 2) {
199 mutex_unlock(&data
->lock
);
203 mutex_unlock(&data
->lock
);
205 *val
= ((inbuf
[0] & 0x0f) << 6) | (inbuf
[1] >> 2);
210 static int max5821_set_value(struct iio_dev
*indio_dev
,
211 int val
, int channel
)
213 struct max5821_data
*data
= iio_priv(indio_dev
);
214 struct i2c_client
*client
= data
->client
;
218 if ((val
< 0) || (val
> 1023))
221 if ((channel
!= 0) && (channel
!= 1))
224 outbuf
[0] = max5821_load_dac_command
[channel
];
225 outbuf
[0] |= val
>> 6;
226 outbuf
[1] = (val
& 0x3f) << 2;
228 ret
= i2c_master_send(client
, outbuf
, 2);
237 static int max5821_read_raw(struct iio_dev
*indio_dev
,
238 struct iio_chan_spec
const *chan
,
239 int *val
, int *val2
, long mask
)
241 struct max5821_data
*data
= iio_priv(indio_dev
);
244 case IIO_CHAN_INFO_RAW
:
245 return max5821_get_value(indio_dev
, val
, chan
->channel
);
246 case IIO_CHAN_INFO_SCALE
:
247 *val
= data
->vref_mv
;
249 return IIO_VAL_FRACTIONAL_LOG2
;
255 static int max5821_write_raw(struct iio_dev
*indio_dev
,
256 struct iio_chan_spec
const *chan
,
257 int val
, int val2
, long mask
)
263 case IIO_CHAN_INFO_RAW
:
264 return max5821_set_value(indio_dev
, val
, chan
->channel
);
270 static int __maybe_unused
max5821_suspend(struct device
*dev
)
272 u8 outbuf
[2] = { MAX5821_EXTENDED_COMMAND_MODE
,
273 MAX5821_EXTENDED_DAC_A
|
274 MAX5821_EXTENDED_DAC_B
|
275 MAX5821_EXTENDED_POWER_DOWN_MODE2
};
277 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
280 static int __maybe_unused
max5821_resume(struct device
*dev
)
282 u8 outbuf
[2] = { MAX5821_EXTENDED_COMMAND_MODE
,
283 MAX5821_EXTENDED_DAC_A
|
284 MAX5821_EXTENDED_DAC_B
|
285 MAX5821_EXTENDED_POWER_UP
};
287 return i2c_master_send(to_i2c_client(dev
), outbuf
, 2);
290 static SIMPLE_DEV_PM_OPS(max5821_pm_ops
, max5821_suspend
, max5821_resume
);
292 static const struct iio_info max5821_info
= {
293 .read_raw
= max5821_read_raw
,
294 .write_raw
= max5821_write_raw
,
297 static int max5821_probe(struct i2c_client
*client
,
298 const struct i2c_device_id
*id
)
300 struct max5821_data
*data
;
301 struct iio_dev
*indio_dev
;
305 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
308 data
= iio_priv(indio_dev
);
309 i2c_set_clientdata(client
, indio_dev
);
310 data
->client
= client
;
311 mutex_init(&data
->lock
);
313 /* max5821 start in powerdown mode 100Kohm to ground */
314 for (tmp
= 0; tmp
< MAX5821_MAX_DAC_CHANNELS
; tmp
++) {
315 data
->powerdown
[tmp
] = true;
316 data
->powerdown_mode
[tmp
] = MAX5821_100KOHM_TO_GND
;
319 data
->vref_reg
= devm_regulator_get(&client
->dev
, "vref");
320 if (IS_ERR(data
->vref_reg
)) {
321 ret
= PTR_ERR(data
->vref_reg
);
322 dev_err(&client
->dev
,
323 "Failed to get vref regulator: %d\n", ret
);
327 ret
= regulator_enable(data
->vref_reg
);
329 dev_err(&client
->dev
,
330 "Failed to enable vref regulator: %d\n", ret
);
334 ret
= regulator_get_voltage(data
->vref_reg
);
336 dev_err(&client
->dev
,
337 "Failed to get voltage on regulator: %d\n", ret
);
338 goto error_disable_reg
;
341 data
->vref_mv
= ret
/ 1000;
343 indio_dev
->name
= id
->name
;
344 indio_dev
->num_channels
= ARRAY_SIZE(max5821_channels
);
345 indio_dev
->channels
= max5821_channels
;
346 indio_dev
->modes
= INDIO_DIRECT_MODE
;
347 indio_dev
->info
= &max5821_info
;
349 return iio_device_register(indio_dev
);
352 regulator_disable(data
->vref_reg
);
359 static int max5821_remove(struct i2c_client
*client
)
361 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
362 struct max5821_data
*data
= iio_priv(indio_dev
);
364 iio_device_unregister(indio_dev
);
365 regulator_disable(data
->vref_reg
);
370 static const struct i2c_device_id max5821_id
[] = {
371 { "max5821", ID_MAX5821
},
374 MODULE_DEVICE_TABLE(i2c
, max5821_id
);
376 static const struct of_device_id max5821_of_match
[] = {
377 { .compatible
= "maxim,max5821" },
380 MODULE_DEVICE_TABLE(of
, max5821_of_match
);
382 static struct i2c_driver max5821_driver
= {
385 .of_match_table
= max5821_of_match
,
386 .pm
= &max5821_pm_ops
,
388 .probe
= max5821_probe
,
389 .remove
= max5821_remove
,
390 .id_table
= max5821_id
,
392 module_i2c_driver(max5821_driver
);
394 MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
395 MODULE_DESCRIPTION("MAX5821 DAC");
396 MODULE_LICENSE("GPL v2");