Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 1 | /* |
| 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 Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/err.h> |
| 26 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/sysfs.h> |
Lars-Peter Clausen | dbdc025 | 2012-06-04 11:36:28 +0200 | [diff] [blame] | 29 | #include <linux/iio/dac/max517.h> |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 30 | |
| 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 | |
| 38 | enum max517_device_ids { |
| 39 | ID_MAX517, |
| 40 | ID_MAX518, |
| 41 | ID_MAX519, |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 42 | ID_MAX520, |
| 43 | ID_MAX521, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct max517_data { |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 47 | struct i2c_client *client; |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 48 | unsigned short vref_mv[8]; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 49 | }; |
| 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 Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 56 | static int max517_set_value(struct iio_dev *indio_dev, |
| 57 | long val, int channel) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 58 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 59 | struct max517_data *data = iio_priv(indio_dev); |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 60 | struct i2c_client *client = data->client; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 61 | u8 outbuf[2]; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 62 | int res; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 63 | |
| 64 | if (val < 0 || val > 255) |
| 65 | return -EINVAL; |
| 66 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 67 | outbuf[0] = channel; |
| 68 | outbuf[1] = val; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 69 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 70 | res = i2c_master_send(client, outbuf, 2); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 71 | if (res < 0) |
| 72 | return res; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 73 | else if (res != 2) |
| 74 | return -EIO; |
| 75 | else |
| 76 | return 0; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 79 | static 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 Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 84 | { |
Jonathan Cameron | 638e59f | 2011-10-06 17:14:38 +0100 | [diff] [blame] | 85 | struct max517_data *data = iio_priv(indio_dev); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 86 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 87 | switch (m) { |
| 88 | case IIO_CHAN_INFO_SCALE: |
| 89 | /* Corresponds to Vref / 2^(bits) */ |
Lars-Peter Clausen | 998f129 | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 90 | *val = data->vref_mv[chan->channel]; |
| 91 | *val2 = 8; |
| 92 | return IIO_VAL_FRACTIONAL_LOG2; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | return -EINVAL; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 99 | static int max517_write_raw(struct iio_dev *indio_dev, |
| 100 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 101 | { |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 102 | 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 Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 114 | } |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 115 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 116 | #ifdef CONFIG_PM_SLEEP |
| 117 | static int max517_suspend(struct device *dev) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 118 | { |
| 119 | u8 outbuf = COMMAND_PD; |
| 120 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 121 | return i2c_master_send(to_i2c_client(dev), &outbuf, 1); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 124 | static int max517_resume(struct device *dev) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 125 | { |
| 126 | u8 outbuf = 0; |
| 127 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 128 | return i2c_master_send(to_i2c_client(dev), &outbuf, 1); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 131 | static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume); |
| 132 | #define MAX517_PM_OPS (&max517_pm_ops) |
| 133 | #else |
| 134 | #define MAX517_PM_OPS NULL |
| 135 | #endif |
| 136 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 137 | static const struct iio_info max517_info = { |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 138 | .read_raw = max517_read_raw, |
| 139 | .write_raw = max517_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 140 | .driver_module = THIS_MODULE, |
| 141 | }; |
| 142 | |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 143 | #define MAX517_CHANNEL(chan) { \ |
| 144 | .type = IIO_VOLTAGE, \ |
| 145 | .indexed = 1, \ |
| 146 | .output = 1, \ |
| 147 | .channel = (chan), \ |
Jonathan Cameron | 040b837 | 2013-02-27 19:28:22 +0000 | [diff] [blame] | 148 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 149 | BIT(IIO_CHAN_INFO_SCALE), \ |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static const struct iio_chan_spec max517_channels[] = { |
| 153 | MAX517_CHANNEL(0), |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 154 | MAX517_CHANNEL(1), |
| 155 | MAX517_CHANNEL(2), |
| 156 | MAX517_CHANNEL(3), |
| 157 | MAX517_CHANNEL(4), |
| 158 | MAX517_CHANNEL(5), |
| 159 | MAX517_CHANNEL(6), |
| 160 | MAX517_CHANNEL(7), |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 163 | static int max517_probe(struct i2c_client *client, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 164 | const struct i2c_device_id *id) |
| 165 | { |
| 166 | struct max517_data *data; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 167 | struct iio_dev *indio_dev; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 168 | struct max517_platform_data *platform_data = client->dev.platform_data; |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 169 | int chan; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 170 | |
Sachin Kamat | cc566fd | 2013-08-19 12:38:00 +0100 | [diff] [blame] | 171 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 172 | if (!indio_dev) |
| 173 | return -ENOMEM; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 174 | data = iio_priv(indio_dev); |
| 175 | i2c_set_clientdata(client, indio_dev); |
Roland Stigge | 826514b | 2011-01-29 16:36:46 +0100 | [diff] [blame] | 176 | data->client = client; |
| 177 | |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 178 | /* establish that the iio_dev is a child of the i2c device */ |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 179 | indio_dev->dev.parent = &client->dev; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 180 | |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 181 | switch (id->driver_data) { |
| 182 | case ID_MAX521: |
| 183 | indio_dev->num_channels = 8; |
| 184 | break; |
| 185 | case ID_MAX520: |
| 186 | indio_dev->num_channels = 4; |
| 187 | break; |
| 188 | case ID_MAX519: |
| 189 | case ID_MAX518: |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 190 | indio_dev->num_channels = 2; |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 191 | break; |
| 192 | default: /* single channel for MAX517 */ |
| 193 | indio_dev->num_channels = 1; |
| 194 | break; |
| 195 | } |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 196 | indio_dev->channels = max517_channels; |
Jonathan Cameron | cada11f | 2011-06-27 13:07:35 +0100 | [diff] [blame] | 197 | indio_dev->modes = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 4cd8d87 | 2012-06-04 11:36:20 +0200 | [diff] [blame] | 198 | indio_dev->info = &max517_info; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Reference voltage on MAX518 and default is 5V, else take vref_mv |
| 202 | * from platform_data |
| 203 | */ |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 204 | for (chan = 0; chan < indio_dev->num_channels; chan++) { |
| 205 | if (id->driver_data == ID_MAX518 || !platform_data) |
| 206 | data->vref_mv[chan] = 5000; /* mV */ |
| 207 | else |
| 208 | data->vref_mv[chan] = platform_data->vref_mv[chan]; |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Sachin Kamat | 345d4f92 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 211 | return iio_device_register(indio_dev); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 214 | static int max517_remove(struct i2c_client *client) |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 215 | { |
Peter Meerwald | e8ccc11 | 2012-05-02 01:13:33 +0200 | [diff] [blame] | 216 | iio_device_unregister(i2c_get_clientdata(client)); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static const struct i2c_device_id max517_id[] = { |
| 221 | { "max517", ID_MAX517 }, |
| 222 | { "max518", ID_MAX518 }, |
| 223 | { "max519", ID_MAX519 }, |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 224 | { "max520", ID_MAX520 }, |
| 225 | { "max521", ID_MAX521 }, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 226 | { } |
| 227 | }; |
| 228 | MODULE_DEVICE_TABLE(i2c, max517_id); |
| 229 | |
| 230 | static struct i2c_driver max517_driver = { |
| 231 | .driver = { |
| 232 | .name = MAX517_DRV_NAME, |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 233 | .pm = MAX517_PM_OPS, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 234 | }, |
| 235 | .probe = max517_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 236 | .remove = max517_remove, |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 237 | .id_table = max517_id, |
| 238 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 239 | module_i2c_driver(max517_driver); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 240 | |
| 241 | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); |
Antonio Fiol | a878a1a | 2015-03-28 09:07:14 +0100 | [diff] [blame] | 242 | MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC"); |
Roland Stigge | 6ddc5fb | 2011-01-12 11:41:59 +0100 | [diff] [blame] | 243 | MODULE_LICENSE("GPL"); |