blob: 451d10e323cff1ff6d3a033e21b4129ad6ffe3c7 [file] [log] [blame]
Roland Stigge6ddc5fb2011-01-12 11:41:59 +01001/*
2 * max517.c - Support for Maxim MAX517, MAX518 and MAX519
3 *
4 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21#include <linux/module.h>
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010022#include <linux/slab.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/err.h>
26
Jonathan Cameron06458e22012-04-25 15:54:58 +010027#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
Lars-Peter Clausendbdc0252012-06-04 11:36:28 +020029#include <linux/iio/dac/max517.h>
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010030
31#define MAX517_DRV_NAME "max517"
32
33/* Commands */
34#define COMMAND_CHANNEL0 0x00
35#define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
36#define COMMAND_PD 0x08 /* Power Down */
37
38enum max517_device_ids {
39 ID_MAX517,
40 ID_MAX518,
41 ID_MAX519,
Antonio Fiola878a1a2015-03-28 09:07:14 +010042 ID_MAX520,
43 ID_MAX521,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010044};
45
46struct max517_data {
Roland Stigge826514b2011-01-29 16:36:46 +010047 struct i2c_client *client;
Antonio Fiola878a1a2015-03-28 09:07:14 +010048 unsigned short vref_mv[8];
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010049};
50
51/*
52 * channel: bit 0: channel 1
53 * bit 1: channel 2
54 * (this way, it's possible to set both channels at once)
55 */
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020056static int max517_set_value(struct iio_dev *indio_dev,
57 long val, int channel)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010058{
Jonathan Cameron638e59f2011-10-06 17:14:38 +010059 struct max517_data *data = iio_priv(indio_dev);
Roland Stigge826514b2011-01-29 16:36:46 +010060 struct i2c_client *client = data->client;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020061 u8 outbuf[2];
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010062 int res;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010063
64 if (val < 0 || val > 255)
65 return -EINVAL;
66
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020067 outbuf[0] = channel;
68 outbuf[1] = val;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010069
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020070 res = i2c_master_send(client, outbuf, 2);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010071 if (res < 0)
72 return res;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020073 else if (res != 2)
74 return -EIO;
75 else
76 return 0;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010077}
78
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020079static int max517_read_raw(struct iio_dev *indio_dev,
80 struct iio_chan_spec const *chan,
81 int *val,
82 int *val2,
83 long m)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010084{
Jonathan Cameron638e59f2011-10-06 17:14:38 +010085 struct max517_data *data = iio_priv(indio_dev);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010086
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020087 switch (m) {
88 case IIO_CHAN_INFO_SCALE:
89 /* Corresponds to Vref / 2^(bits) */
Lars-Peter Clausen998f1292013-09-28 10:31:00 +010090 *val = data->vref_mv[chan->channel];
91 *val2 = 8;
92 return IIO_VAL_FRACTIONAL_LOG2;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020093 default:
94 break;
95 }
96 return -EINVAL;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +010097}
98
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +020099static int max517_write_raw(struct iio_dev *indio_dev,
100 struct iio_chan_spec const *chan, int val, int val2, long mask)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100101{
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200102 int ret;
103
104 switch (mask) {
105 case IIO_CHAN_INFO_RAW:
106 ret = max517_set_value(indio_dev, val, chan->channel);
107 break;
108 default:
109 ret = -EINVAL;
110 break;
111 }
112
113 return ret;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100114}
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100115
Marcus Folkessonfe8a5422018-08-11 10:47:09 +0200116static int __maybe_unused max517_suspend(struct device *dev)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100117{
118 u8 outbuf = COMMAND_PD;
119
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100120 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100121}
122
Marcus Folkessonfe8a5422018-08-11 10:47:09 +0200123static int __maybe_unused max517_resume(struct device *dev)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100124{
125 u8 outbuf = 0;
126
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100127 return i2c_master_send(to_i2c_client(dev), &outbuf, 1);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100128}
129
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100130static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100131
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100132static const struct iio_info max517_info = {
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200133 .read_raw = max517_read_raw,
134 .write_raw = max517_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100135};
136
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200137#define MAX517_CHANNEL(chan) { \
138 .type = IIO_VOLTAGE, \
139 .indexed = 1, \
140 .output = 1, \
141 .channel = (chan), \
Jonathan Cameron040b8372013-02-27 19:28:22 +0000142 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
143 BIT(IIO_CHAN_INFO_SCALE), \
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200144}
145
146static const struct iio_chan_spec max517_channels[] = {
147 MAX517_CHANNEL(0),
Antonio Fiola878a1a2015-03-28 09:07:14 +0100148 MAX517_CHANNEL(1),
149 MAX517_CHANNEL(2),
150 MAX517_CHANNEL(3),
151 MAX517_CHANNEL(4),
152 MAX517_CHANNEL(5),
153 MAX517_CHANNEL(6),
154 MAX517_CHANNEL(7),
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100155};
156
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800157static int max517_probe(struct i2c_client *client,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100158 const struct i2c_device_id *id)
159{
160 struct max517_data *data;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100161 struct iio_dev *indio_dev;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100162 struct max517_platform_data *platform_data = client->dev.platform_data;
Antonio Fiola878a1a2015-03-28 09:07:14 +0100163 int chan;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100164
Sachin Kamatcc566fd2013-08-19 12:38:00 +0100165 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
166 if (!indio_dev)
167 return -ENOMEM;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100168 data = iio_priv(indio_dev);
169 i2c_set_clientdata(client, indio_dev);
Roland Stigge826514b2011-01-29 16:36:46 +0100170 data->client = client;
171
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100172 /* establish that the iio_dev is a child of the i2c device */
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100173 indio_dev->dev.parent = &client->dev;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100174
Antonio Fiola878a1a2015-03-28 09:07:14 +0100175 switch (id->driver_data) {
176 case ID_MAX521:
177 indio_dev->num_channels = 8;
178 break;
179 case ID_MAX520:
180 indio_dev->num_channels = 4;
181 break;
182 case ID_MAX519:
183 case ID_MAX518:
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200184 indio_dev->num_channels = 2;
Antonio Fiola878a1a2015-03-28 09:07:14 +0100185 break;
186 default: /* single channel for MAX517 */
187 indio_dev->num_channels = 1;
188 break;
189 }
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200190 indio_dev->channels = max517_channels;
Jonathan Cameroncada11f2011-06-27 13:07:35 +0100191 indio_dev->modes = INDIO_DIRECT_MODE;
Lars-Peter Clausen4cd8d872012-06-04 11:36:20 +0200192 indio_dev->info = &max517_info;
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100193
194 /*
195 * Reference voltage on MAX518 and default is 5V, else take vref_mv
196 * from platform_data
197 */
Antonio Fiola878a1a2015-03-28 09:07:14 +0100198 for (chan = 0; chan < indio_dev->num_channels; chan++) {
199 if (id->driver_data == ID_MAX518 || !platform_data)
200 data->vref_mv[chan] = 5000; /* mV */
201 else
202 data->vref_mv[chan] = platform_data->vref_mv[chan];
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100203 }
204
Sachin Kamat345d4f922013-10-24 12:53:00 +0100205 return iio_device_register(indio_dev);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100206}
207
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800208static int max517_remove(struct i2c_client *client)
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100209{
Peter Meerwalde8ccc112012-05-02 01:13:33 +0200210 iio_device_unregister(i2c_get_clientdata(client));
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100211 return 0;
212}
213
214static const struct i2c_device_id max517_id[] = {
215 { "max517", ID_MAX517 },
216 { "max518", ID_MAX518 },
217 { "max519", ID_MAX519 },
Antonio Fiola878a1a2015-03-28 09:07:14 +0100218 { "max520", ID_MAX520 },
219 { "max521", ID_MAX521 },
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100220 { }
221};
222MODULE_DEVICE_TABLE(i2c, max517_id);
223
224static struct i2c_driver max517_driver = {
225 .driver = {
226 .name = MAX517_DRV_NAME,
Marcus Folkessonfe8a5422018-08-11 10:47:09 +0200227 .pm = &max517_pm_ops,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100228 },
229 .probe = max517_probe,
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800230 .remove = max517_remove,
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100231 .id_table = max517_id,
232};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100233module_i2c_driver(max517_driver);
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100234
235MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
Antonio Fiola878a1a2015-03-28 09:07:14 +0100236MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
Roland Stigge6ddc5fb2011-01-12 11:41:59 +0100237MODULE_LICENSE("GPL");