2 * m62332.c - Support for Mitsubishi m62332 DAC
4 * Copyright (c) 2014 Dmitry Eremin-Solenikov
6 * Based on max517 driver:
7 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
23 #include <linux/err.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/driver.h>
28 #include <linux/regulator/consumer.h>
30 #define M62332_CHANNELS 2
33 struct i2c_client *client;
35 struct regulator *vcc;
37 u8 raw[M62332_CHANNELS];
38 #ifdef CONFIG_PM_SLEEP
39 u8 save[M62332_CHANNELS];
43 static int m62332_set_value(struct iio_dev *indio_dev,
46 struct m62332_data *data = iio_priv(indio_dev);
47 struct i2c_client *client = data->client;
51 if (val == data->raw[channel])
57 mutex_lock(&data->mutex);
60 res = regulator_enable(data->vcc);
65 res = i2c_master_send(client, outbuf, 2);
66 if (res >= 0 && res != 2)
71 data->raw[channel] = val;
74 regulator_disable(data->vcc);
76 mutex_unlock(&data->mutex);
81 mutex_unlock(&data->mutex);
86 static int m62332_read_raw(struct iio_dev *indio_dev,
87 struct iio_chan_spec const *chan,
92 struct m62332_data *data = iio_priv(indio_dev);
95 case IIO_CHAN_INFO_SCALE:
96 /* Corresponds to Vref / 2^(bits) */
99 return IIO_VAL_FRACTIONAL_LOG2;
100 case IIO_CHAN_INFO_RAW:
101 *val = data->raw[chan->channel];
103 case IIO_CHAN_INFO_OFFSET:
112 static int m62332_write_raw(struct iio_dev *indio_dev,
113 struct iio_chan_spec const *chan, int val, int val2, long mask)
118 case IIO_CHAN_INFO_RAW:
119 if (val < 0 || val > 255)
122 ret = m62332_set_value(indio_dev, val, chan->channel);
132 #ifdef CONFIG_PM_SLEEP
133 static int m62332_suspend(struct device *dev)
135 struct i2c_client *client = to_i2c_client(dev);
136 struct iio_dev *indio_dev = i2c_get_clientdata(client);
137 struct m62332_data *data = iio_priv(indio_dev);
140 data->save[0] = data->raw[0];
141 data->save[1] = data->raw[1];
143 ret = m62332_set_value(indio_dev, 0, 0);
147 return m62332_set_value(indio_dev, 0, 1);
150 static int m62332_resume(struct device *dev)
152 struct i2c_client *client = to_i2c_client(dev);
153 struct iio_dev *indio_dev = i2c_get_clientdata(client);
154 struct m62332_data *data = iio_priv(indio_dev);
157 ret = m62332_set_value(indio_dev, data->save[0], 0);
161 return m62332_set_value(indio_dev, data->save[1], 1);
164 static SIMPLE_DEV_PM_OPS(m62332_pm_ops, m62332_suspend, m62332_resume);
165 #define M62332_PM_OPS (&m62332_pm_ops)
167 #define M62332_PM_OPS NULL
170 static const struct iio_info m62332_info = {
171 .read_raw = m62332_read_raw,
172 .write_raw = m62332_write_raw,
173 .driver_module = THIS_MODULE,
176 #define M62332_CHANNEL(chan) { \
177 .type = IIO_VOLTAGE, \
181 .datasheet_name = "CH" #chan, \
182 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
183 BIT(IIO_CHAN_INFO_SCALE) | \
184 BIT(IIO_CHAN_INFO_OFFSET), \
187 static const struct iio_chan_spec m62332_channels[M62332_CHANNELS] = {
192 static int m62332_probe(struct i2c_client *client,
193 const struct i2c_device_id *id)
195 struct m62332_data *data;
196 struct iio_dev *indio_dev;
199 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
202 data = iio_priv(indio_dev);
203 i2c_set_clientdata(client, indio_dev);
204 data->client = client;
206 mutex_init(&data->mutex);
208 data->vcc = devm_regulator_get(&client->dev, "VCC");
209 if (IS_ERR(data->vcc))
210 return PTR_ERR(data->vcc);
212 /* establish that the iio_dev is a child of the i2c device */
213 indio_dev->dev.parent = &client->dev;
215 indio_dev->num_channels = M62332_CHANNELS;
216 indio_dev->channels = m62332_channels;
217 indio_dev->modes = INDIO_DIRECT_MODE;
218 indio_dev->info = &m62332_info;
220 ret = regulator_get_voltage(data->vcc);
223 data->vref_mv = ret / 1000; /* mV */
225 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
229 ret = iio_device_register(indio_dev);
236 iio_map_array_unregister(indio_dev);
240 static int m62332_remove(struct i2c_client *client)
242 struct iio_dev *indio_dev = i2c_get_clientdata(client);
244 iio_device_unregister(indio_dev);
245 iio_map_array_unregister(indio_dev);
250 static const struct i2c_device_id m62332_id[] = {
254 MODULE_DEVICE_TABLE(i2c, m62332_id);
256 static struct i2c_driver m62332_driver = {
261 .probe = m62332_probe,
262 .remove = m62332_remove,
263 .id_table = m62332_id,
265 module_i2c_driver(m62332_driver);
267 MODULE_AUTHOR("Dmitry Eremin-Solenikov");
268 MODULE_DESCRIPTION("M62332 8-bit DAC");
269 MODULE_LICENSE("GPL v2");