1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * m62332.c - Support for Mitsubishi m62332 DAC
5 * Copyright (c) 2014 Dmitry Eremin-Solenikov
7 * Based on max517 driver:
8 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/err.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/driver.h>
19 #include <linux/regulator/consumer.h>
21 #define M62332_CHANNELS 2
24 struct i2c_client
*client
;
25 struct regulator
*vcc
;
27 u8 raw
[M62332_CHANNELS
];
28 #ifdef CONFIG_PM_SLEEP
29 u8 save
[M62332_CHANNELS
];
33 static int m62332_set_value(struct iio_dev
*indio_dev
, u8 val
, int channel
)
35 struct m62332_data
*data
= iio_priv(indio_dev
);
36 struct i2c_client
*client
= data
->client
;
40 if (val
== data
->raw
[channel
])
46 mutex_lock(&data
->mutex
);
49 res
= regulator_enable(data
->vcc
);
54 res
= i2c_master_send(client
, outbuf
, ARRAY_SIZE(outbuf
));
55 if (res
>= 0 && res
!= ARRAY_SIZE(outbuf
))
60 data
->raw
[channel
] = val
;
63 regulator_disable(data
->vcc
);
65 mutex_unlock(&data
->mutex
);
70 mutex_unlock(&data
->mutex
);
75 static int m62332_read_raw(struct iio_dev
*indio_dev
,
76 struct iio_chan_spec
const *chan
,
81 struct m62332_data
*data
= iio_priv(indio_dev
);
85 case IIO_CHAN_INFO_SCALE
:
86 /* Corresponds to Vref / 2^(bits) */
87 ret
= regulator_get_voltage(data
->vcc
);
91 *val
= ret
/ 1000; /* mV */
94 return IIO_VAL_FRACTIONAL_LOG2
;
95 case IIO_CHAN_INFO_RAW
:
96 *val
= data
->raw
[chan
->channel
];
99 case IIO_CHAN_INFO_OFFSET
:
110 static int m62332_write_raw(struct iio_dev
*indio_dev
,
111 struct iio_chan_spec
const *chan
, int val
, int val2
,
115 case IIO_CHAN_INFO_RAW
:
116 if (val
< 0 || val
> 255)
119 return m62332_set_value(indio_dev
, val
, chan
->channel
);
127 #ifdef CONFIG_PM_SLEEP
128 static int m62332_suspend(struct device
*dev
)
130 struct i2c_client
*client
= to_i2c_client(dev
);
131 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
132 struct m62332_data
*data
= iio_priv(indio_dev
);
135 data
->save
[0] = data
->raw
[0];
136 data
->save
[1] = data
->raw
[1];
138 ret
= m62332_set_value(indio_dev
, 0, 0);
142 return m62332_set_value(indio_dev
, 0, 1);
145 static int m62332_resume(struct device
*dev
)
147 struct i2c_client
*client
= to_i2c_client(dev
);
148 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
149 struct m62332_data
*data
= iio_priv(indio_dev
);
152 ret
= m62332_set_value(indio_dev
, data
->save
[0], 0);
156 return m62332_set_value(indio_dev
, data
->save
[1], 1);
159 static SIMPLE_DEV_PM_OPS(m62332_pm_ops
, m62332_suspend
, m62332_resume
);
160 #define M62332_PM_OPS (&m62332_pm_ops)
162 #define M62332_PM_OPS NULL
165 static const struct iio_info m62332_info
= {
166 .read_raw
= m62332_read_raw
,
167 .write_raw
= m62332_write_raw
,
170 #define M62332_CHANNEL(chan) { \
171 .type = IIO_VOLTAGE, \
175 .datasheet_name = "CH" #chan, \
176 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
177 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
178 BIT(IIO_CHAN_INFO_OFFSET), \
181 static const struct iio_chan_spec m62332_channels
[M62332_CHANNELS
] = {
186 static int m62332_probe(struct i2c_client
*client
,
187 const struct i2c_device_id
*id
)
189 struct m62332_data
*data
;
190 struct iio_dev
*indio_dev
;
193 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
197 data
= iio_priv(indio_dev
);
198 i2c_set_clientdata(client
, indio_dev
);
199 data
->client
= client
;
201 mutex_init(&data
->mutex
);
203 data
->vcc
= devm_regulator_get(&client
->dev
, "VCC");
204 if (IS_ERR(data
->vcc
))
205 return PTR_ERR(data
->vcc
);
207 indio_dev
->num_channels
= ARRAY_SIZE(m62332_channels
);
208 indio_dev
->channels
= m62332_channels
;
209 indio_dev
->modes
= INDIO_DIRECT_MODE
;
210 indio_dev
->info
= &m62332_info
;
212 ret
= iio_map_array_register(indio_dev
, client
->dev
.platform_data
);
216 ret
= iio_device_register(indio_dev
);
223 iio_map_array_unregister(indio_dev
);
228 static int m62332_remove(struct i2c_client
*client
)
230 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
232 iio_device_unregister(indio_dev
);
233 iio_map_array_unregister(indio_dev
);
234 m62332_set_value(indio_dev
, 0, 0);
235 m62332_set_value(indio_dev
, 0, 1);
240 static const struct i2c_device_id m62332_id
[] = {
244 MODULE_DEVICE_TABLE(i2c
, m62332_id
);
246 static struct i2c_driver m62332_driver
= {
251 .probe
= m62332_probe
,
252 .remove
= m62332_remove
,
253 .id_table
= m62332_id
,
255 module_i2c_driver(m62332_driver
);
257 MODULE_AUTHOR("Dmitry Eremin-Solenikov");
258 MODULE_DESCRIPTION("M62332 8-bit DAC");
259 MODULE_LICENSE("GPL v2");