PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / iio / dac / max517.c
blobde76e6a34c1ea9db7922a0147b09bf8703bdd845
1 /*
2 * max517.c - Support for Maxim MAX517, MAX518 and MAX519
4 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/dac/max517.h>
32 #define MAX517_DRV_NAME "max517"
34 /* Commands */
35 #define COMMAND_CHANNEL0 0x00
36 #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
37 #define COMMAND_PD 0x08 /* Power Down */
39 enum max517_device_ids {
40 ID_MAX517,
41 ID_MAX518,
42 ID_MAX519,
45 struct max517_data {
46 struct i2c_client *client;
47 unsigned short vref_mv[2];
51 * channel: bit 0: channel 1
52 * bit 1: channel 2
53 * (this way, it's possible to set both channels at once)
55 static int max517_set_value(struct iio_dev *indio_dev,
56 long val, int channel)
58 struct max517_data *data = iio_priv(indio_dev);
59 struct i2c_client *client = data->client;
60 u8 outbuf[2];
61 int res;
63 if (val < 0 || val > 255)
64 return -EINVAL;
66 outbuf[0] = channel;
67 outbuf[1] = val;
69 res = i2c_master_send(client, outbuf, 2);
70 if (res < 0)
71 return res;
72 else if (res != 2)
73 return -EIO;
74 else
75 return 0;
78 static int max517_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan,
80 int *val,
81 int *val2,
82 long m)
84 struct max517_data *data = iio_priv(indio_dev);
86 switch (m) {
87 case IIO_CHAN_INFO_SCALE:
88 /* Corresponds to Vref / 2^(bits) */
89 *val = data->vref_mv[chan->channel];
90 *val2 = 8;
91 return IIO_VAL_FRACTIONAL_LOG2;
92 default:
93 break;
95 return -EINVAL;
98 static int max517_write_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan, int val, int val2, long mask)
101 int ret;
103 switch (mask) {
104 case IIO_CHAN_INFO_RAW:
105 ret = max517_set_value(indio_dev, val, chan->channel);
106 break;
107 default:
108 ret = -EINVAL;
109 break;
112 return ret;
115 #ifdef CONFIG_PM_SLEEP
116 static int max517_suspend(struct device *dev)
118 u8 outbuf = COMMAND_PD;
120 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
123 static int max517_resume(struct device *dev)
125 u8 outbuf = 0;
127 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
130 static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
131 #define MAX517_PM_OPS (&max517_pm_ops)
132 #else
133 #define MAX517_PM_OPS NULL
134 #endif
136 static const struct iio_info max517_info = {
137 .read_raw = max517_read_raw,
138 .write_raw = max517_write_raw,
139 .driver_module = THIS_MODULE,
142 #define MAX517_CHANNEL(chan) { \
143 .type = IIO_VOLTAGE, \
144 .indexed = 1, \
145 .output = 1, \
146 .channel = (chan), \
147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
148 BIT(IIO_CHAN_INFO_SCALE), \
151 static const struct iio_chan_spec max517_channels[] = {
152 MAX517_CHANNEL(0),
153 MAX517_CHANNEL(1)
156 static int max517_probe(struct i2c_client *client,
157 const struct i2c_device_id *id)
159 struct max517_data *data;
160 struct iio_dev *indio_dev;
161 struct max517_platform_data *platform_data = client->dev.platform_data;
163 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
164 if (!indio_dev)
165 return -ENOMEM;
166 data = iio_priv(indio_dev);
167 i2c_set_clientdata(client, indio_dev);
168 data->client = client;
170 /* establish that the iio_dev is a child of the i2c device */
171 indio_dev->dev.parent = &client->dev;
173 /* reduced channel set for MAX517 */
174 if (id->driver_data == ID_MAX517)
175 indio_dev->num_channels = 1;
176 else
177 indio_dev->num_channels = 2;
178 indio_dev->channels = max517_channels;
179 indio_dev->modes = INDIO_DIRECT_MODE;
180 indio_dev->info = &max517_info;
183 * Reference voltage on MAX518 and default is 5V, else take vref_mv
184 * from platform_data
186 if (id->driver_data == ID_MAX518 || !platform_data) {
187 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
188 } else {
189 data->vref_mv[0] = platform_data->vref_mv[0];
190 data->vref_mv[1] = platform_data->vref_mv[1];
193 return iio_device_register(indio_dev);
196 static int max517_remove(struct i2c_client *client)
198 iio_device_unregister(i2c_get_clientdata(client));
199 return 0;
202 static const struct i2c_device_id max517_id[] = {
203 { "max517", ID_MAX517 },
204 { "max518", ID_MAX518 },
205 { "max519", ID_MAX519 },
208 MODULE_DEVICE_TABLE(i2c, max517_id);
210 static struct i2c_driver max517_driver = {
211 .driver = {
212 .name = MAX517_DRV_NAME,
213 .pm = MAX517_PM_OPS,
215 .probe = max517_probe,
216 .remove = max517_remove,
217 .id_table = max517_id,
219 module_i2c_driver(max517_driver);
221 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
222 MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
223 MODULE_LICENSE("GPL");