Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / dac / m62332.c
blob19031943dabed6bb798261629143285062924af4
1 /*
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
32 struct m62332_data {
33 struct i2c_client *client;
34 struct regulator *vcc;
35 struct mutex mutex;
36 u8 raw[M62332_CHANNELS];
37 #ifdef CONFIG_PM_SLEEP
38 u8 save[M62332_CHANNELS];
39 #endif
42 static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
44 struct m62332_data *data = iio_priv(indio_dev);
45 struct i2c_client *client = data->client;
46 u8 outbuf[2];
47 int res;
49 if (val == data->raw[channel])
50 return 0;
52 outbuf[0] = channel;
53 outbuf[1] = val;
55 mutex_lock(&data->mutex);
57 if (val) {
58 res = regulator_enable(data->vcc);
59 if (res)
60 goto out;
63 res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
64 if (res >= 0 && res != ARRAY_SIZE(outbuf))
65 res = -EIO;
66 if (res < 0)
67 goto out;
69 data->raw[channel] = val;
71 if (!val)
72 regulator_disable(data->vcc);
74 mutex_unlock(&data->mutex);
76 return 0;
78 out:
79 mutex_unlock(&data->mutex);
81 return res;
84 static int m62332_read_raw(struct iio_dev *indio_dev,
85 struct iio_chan_spec const *chan,
86 int *val,
87 int *val2,
88 long mask)
90 struct m62332_data *data = iio_priv(indio_dev);
91 int ret;
93 switch (mask) {
94 case IIO_CHAN_INFO_SCALE:
95 /* Corresponds to Vref / 2^(bits) */
96 ret = regulator_get_voltage(data->vcc);
97 if (ret < 0)
98 return ret;
100 *val = ret / 1000; /* mV */
101 *val2 = 8;
103 return IIO_VAL_FRACTIONAL_LOG2;
104 case IIO_CHAN_INFO_RAW:
105 *val = data->raw[chan->channel];
107 return IIO_VAL_INT;
108 case IIO_CHAN_INFO_OFFSET:
109 *val = 1;
111 return IIO_VAL_INT;
112 default:
113 break;
116 return -EINVAL;
119 static int m62332_write_raw(struct iio_dev *indio_dev,
120 struct iio_chan_spec const *chan, int val, int val2,
121 long mask)
123 switch (mask) {
124 case IIO_CHAN_INFO_RAW:
125 if (val < 0 || val > 255)
126 return -EINVAL;
128 return m62332_set_value(indio_dev, val, chan->channel);
129 default:
130 break;
133 return -EINVAL;
136 #ifdef CONFIG_PM_SLEEP
137 static int m62332_suspend(struct device *dev)
139 struct i2c_client *client = to_i2c_client(dev);
140 struct iio_dev *indio_dev = i2c_get_clientdata(client);
141 struct m62332_data *data = iio_priv(indio_dev);
142 int ret;
144 data->save[0] = data->raw[0];
145 data->save[1] = data->raw[1];
147 ret = m62332_set_value(indio_dev, 0, 0);
148 if (ret < 0)
149 return ret;
151 return m62332_set_value(indio_dev, 0, 1);
154 static int m62332_resume(struct device *dev)
156 struct i2c_client *client = to_i2c_client(dev);
157 struct iio_dev *indio_dev = i2c_get_clientdata(client);
158 struct m62332_data *data = iio_priv(indio_dev);
159 int ret;
161 ret = m62332_set_value(indio_dev, data->save[0], 0);
162 if (ret < 0)
163 return ret;
165 return m62332_set_value(indio_dev, data->save[1], 1);
168 static SIMPLE_DEV_PM_OPS(m62332_pm_ops, m62332_suspend, m62332_resume);
169 #define M62332_PM_OPS (&m62332_pm_ops)
170 #else
171 #define M62332_PM_OPS NULL
172 #endif
174 static const struct iio_info m62332_info = {
175 .read_raw = m62332_read_raw,
176 .write_raw = m62332_write_raw,
179 #define M62332_CHANNEL(chan) { \
180 .type = IIO_VOLTAGE, \
181 .indexed = 1, \
182 .output = 1, \
183 .channel = (chan), \
184 .datasheet_name = "CH" #chan, \
185 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
186 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
187 BIT(IIO_CHAN_INFO_OFFSET), \
190 static const struct iio_chan_spec m62332_channels[M62332_CHANNELS] = {
191 M62332_CHANNEL(0),
192 M62332_CHANNEL(1)
195 static int m62332_probe(struct i2c_client *client,
196 const struct i2c_device_id *id)
198 struct m62332_data *data;
199 struct iio_dev *indio_dev;
200 int ret;
202 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
203 if (!indio_dev)
204 return -ENOMEM;
206 data = iio_priv(indio_dev);
207 i2c_set_clientdata(client, indio_dev);
208 data->client = client;
210 mutex_init(&data->mutex);
212 data->vcc = devm_regulator_get(&client->dev, "VCC");
213 if (IS_ERR(data->vcc))
214 return PTR_ERR(data->vcc);
216 /* establish that the iio_dev is a child of the i2c device */
217 indio_dev->dev.parent = &client->dev;
219 indio_dev->num_channels = ARRAY_SIZE(m62332_channels);
220 indio_dev->channels = m62332_channels;
221 indio_dev->modes = INDIO_DIRECT_MODE;
222 indio_dev->info = &m62332_info;
224 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
225 if (ret < 0)
226 return ret;
228 ret = iio_device_register(indio_dev);
229 if (ret < 0)
230 goto err;
232 return 0;
234 err:
235 iio_map_array_unregister(indio_dev);
237 return ret;
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);
246 m62332_set_value(indio_dev, 0, 0);
247 m62332_set_value(indio_dev, 0, 1);
249 return 0;
252 static const struct i2c_device_id m62332_id[] = {
253 { "m62332", },
256 MODULE_DEVICE_TABLE(i2c, m62332_id);
258 static struct i2c_driver m62332_driver = {
259 .driver = {
260 .name = "m62332",
261 .pm = M62332_PM_OPS,
263 .probe = m62332_probe,
264 .remove = m62332_remove,
265 .id_table = m62332_id,
267 module_i2c_driver(m62332_driver);
269 MODULE_AUTHOR("Dmitry Eremin-Solenikov");
270 MODULE_DESCRIPTION("M62332 8-bit DAC");
271 MODULE_LICENSE("GPL v2");