Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 1 | /* |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 2 | * mcp4725.c - Support for Microchip MCP4725/6 |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net> |
| 5 | * |
| 6 | * Based on max517 by Roland Stigge <stigge@antcom.de> |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of version 2 of |
| 9 | * the GNU General Public License. See the file COPYING in the main |
| 10 | * directory of this archive for more details. |
| 11 | * |
| 12 | * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC) |
| 13 | * (7-bit I2C slave address 0x60, the three LSBs can be configured in |
| 14 | * hardware) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/err.h> |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 20 | #include <linux/delay.h> |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 21 | #include <linux/regulator/consumer.h> |
Javier Martinez Canillas | 8414af1 | 2017-03-15 01:44:52 -0300 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Tomas Novotny | 88e39a8 | 2016-10-18 19:43:09 +0200 | [diff] [blame] | 23 | #include <linux/of.h> |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 24 | |
| 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/sysfs.h> |
| 27 | |
| 28 | #include <linux/iio/dac/mcp4725.h> |
| 29 | |
| 30 | #define MCP4725_DRV_NAME "mcp4725" |
| 31 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 32 | #define MCP472X_REF_VDD 0x00 |
| 33 | #define MCP472X_REF_VREF_UNBUFFERED 0x02 |
| 34 | #define MCP472X_REF_VREF_BUFFERED 0x03 |
| 35 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 36 | struct mcp4725_data { |
| 37 | struct i2c_client *client; |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 38 | int id; |
| 39 | unsigned ref_mode; |
| 40 | bool vref_buffered; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 41 | u16 dac_value; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 42 | bool powerdown; |
| 43 | unsigned powerdown_mode; |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 44 | struct regulator *vdd_reg; |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 45 | struct regulator *vref_reg; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 48 | static int mcp4725_suspend(struct device *dev) |
| 49 | { |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 50 | struct mcp4725_data *data = iio_priv(i2c_get_clientdata( |
| 51 | to_i2c_client(dev))); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 52 | u8 outbuf[2]; |
| 53 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 54 | outbuf[0] = (data->powerdown_mode + 1) << 4; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 55 | outbuf[1] = 0; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 56 | data->powerdown = true; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 57 | |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 58 | return i2c_master_send(data->client, outbuf, 2); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int mcp4725_resume(struct device *dev) |
| 62 | { |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 63 | struct mcp4725_data *data = iio_priv(i2c_get_clientdata( |
| 64 | to_i2c_client(dev))); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 65 | u8 outbuf[2]; |
| 66 | |
| 67 | /* restore previous DAC value */ |
| 68 | outbuf[0] = (data->dac_value >> 8) & 0xf; |
| 69 | outbuf[1] = data->dac_value & 0xff; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 70 | data->powerdown = false; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 71 | |
Peter Meerwald | a97dd06 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 72 | return i2c_master_send(data->client, outbuf, 2); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 75 | #ifdef CONFIG_PM_SLEEP |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 76 | static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume); |
| 77 | #define MCP4725_PM_OPS (&mcp4725_pm_ops) |
| 78 | #else |
| 79 | #define MCP4725_PM_OPS NULL |
| 80 | #endif |
| 81 | |
Jonathan Cameron | 4fd313d | 2013-07-12 20:54:00 +0100 | [diff] [blame] | 82 | static ssize_t mcp4725_store_eeprom(struct device *dev, |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 83 | struct device_attribute *attr, const char *buf, size_t len) |
| 84 | { |
| 85 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 86 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 87 | int tries = 20; |
| 88 | u8 inoutbuf[3]; |
| 89 | bool state; |
| 90 | int ret; |
| 91 | |
| 92 | ret = strtobool(buf, &state); |
| 93 | if (ret < 0) |
| 94 | return ret; |
| 95 | |
| 96 | if (!state) |
| 97 | return 0; |
| 98 | |
| 99 | inoutbuf[0] = 0x60; /* write EEPROM */ |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 100 | inoutbuf[0] |= data->ref_mode << 3; |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 101 | inoutbuf[1] = data->dac_value >> 4; |
| 102 | inoutbuf[2] = (data->dac_value & 0xf) << 4; |
| 103 | |
| 104 | ret = i2c_master_send(data->client, inoutbuf, 3); |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | else if (ret != 3) |
| 108 | return -EIO; |
| 109 | |
| 110 | /* wait for write complete, takes up to 50ms */ |
| 111 | while (tries--) { |
| 112 | msleep(20); |
| 113 | ret = i2c_master_recv(data->client, inoutbuf, 3); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | else if (ret != 3) |
| 117 | return -EIO; |
| 118 | |
| 119 | if (inoutbuf[0] & 0x80) |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | if (tries < 0) { |
| 124 | dev_err(&data->client->dev, |
| 125 | "mcp4725_store_eeprom() failed, incomplete\n"); |
| 126 | return -EIO; |
| 127 | } |
| 128 | |
| 129 | return len; |
| 130 | } |
| 131 | |
| 132 | static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0); |
| 133 | |
| 134 | static struct attribute *mcp4725_attributes[] = { |
| 135 | &iio_dev_attr_store_eeprom.dev_attr.attr, |
| 136 | NULL, |
| 137 | }; |
| 138 | |
| 139 | static const struct attribute_group mcp4725_attribute_group = { |
| 140 | .attrs = mcp4725_attributes, |
| 141 | }; |
| 142 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 143 | static const char * const mcp4725_powerdown_modes[] = { |
| 144 | "1kohm_to_gnd", |
| 145 | "100kohm_to_gnd", |
| 146 | "500kohm_to_gnd" |
| 147 | }; |
| 148 | |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 149 | static const char * const mcp4726_powerdown_modes[] = { |
| 150 | "1kohm_to_gnd", |
| 151 | "125kohm_to_gnd", |
| 152 | "640kohm_to_gnd" |
| 153 | }; |
| 154 | |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 155 | static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev, |
| 156 | const struct iio_chan_spec *chan) |
| 157 | { |
| 158 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 159 | |
| 160 | return data->powerdown_mode; |
| 161 | } |
| 162 | |
| 163 | static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev, |
| 164 | const struct iio_chan_spec *chan, unsigned mode) |
| 165 | { |
| 166 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 167 | |
| 168 | data->powerdown_mode = mode; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev, |
| 174 | uintptr_t private, const struct iio_chan_spec *chan, char *buf) |
| 175 | { |
| 176 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 177 | |
| 178 | return sprintf(buf, "%d\n", data->powerdown); |
| 179 | } |
| 180 | |
| 181 | static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev, |
| 182 | uintptr_t private, const struct iio_chan_spec *chan, |
| 183 | const char *buf, size_t len) |
| 184 | { |
| 185 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 186 | bool state; |
| 187 | int ret; |
| 188 | |
| 189 | ret = strtobool(buf, &state); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | if (state) |
| 194 | ret = mcp4725_suspend(&data->client->dev); |
| 195 | else |
| 196 | ret = mcp4725_resume(&data->client->dev); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
| 200 | return len; |
| 201 | } |
| 202 | |
Javier Martinez Canillas | 8414af1 | 2017-03-15 01:44:52 -0300 | [diff] [blame] | 203 | enum chip_id { |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 204 | MCP4725, |
| 205 | MCP4726, |
| 206 | }; |
| 207 | |
| 208 | static const struct iio_enum mcp472x_powerdown_mode_enum[] = { |
| 209 | [MCP4725] = { |
| 210 | .items = mcp4725_powerdown_modes, |
| 211 | .num_items = ARRAY_SIZE(mcp4725_powerdown_modes), |
| 212 | .get = mcp4725_get_powerdown_mode, |
| 213 | .set = mcp4725_set_powerdown_mode, |
| 214 | }, |
| 215 | [MCP4726] = { |
| 216 | .items = mcp4726_powerdown_modes, |
| 217 | .num_items = ARRAY_SIZE(mcp4726_powerdown_modes), |
| 218 | .get = mcp4725_get_powerdown_mode, |
| 219 | .set = mcp4725_set_powerdown_mode, |
| 220 | }, |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = { |
| 224 | { |
| 225 | .name = "powerdown", |
| 226 | .read = mcp4725_read_powerdown, |
| 227 | .write = mcp4725_write_powerdown, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 228 | .shared = IIO_SEPARATE, |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 229 | }, |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 230 | IIO_ENUM("powerdown_mode", IIO_SEPARATE, |
| 231 | &mcp472x_powerdown_mode_enum[MCP4725]), |
| 232 | IIO_ENUM_AVAILABLE("powerdown_mode", |
| 233 | &mcp472x_powerdown_mode_enum[MCP4725]), |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 234 | { }, |
| 235 | }; |
| 236 | |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 237 | static const struct iio_chan_spec_ext_info mcp4726_ext_info[] = { |
| 238 | { |
| 239 | .name = "powerdown", |
| 240 | .read = mcp4725_read_powerdown, |
| 241 | .write = mcp4725_write_powerdown, |
| 242 | .shared = IIO_SEPARATE, |
| 243 | }, |
| 244 | IIO_ENUM("powerdown_mode", IIO_SEPARATE, |
| 245 | &mcp472x_powerdown_mode_enum[MCP4726]), |
| 246 | IIO_ENUM_AVAILABLE("powerdown_mode", |
| 247 | &mcp472x_powerdown_mode_enum[MCP4726]), |
| 248 | { }, |
| 249 | }; |
| 250 | |
| 251 | static const struct iio_chan_spec mcp472x_channel[] = { |
| 252 | [MCP4725] = { |
| 253 | .type = IIO_VOLTAGE, |
| 254 | .indexed = 1, |
| 255 | .output = 1, |
| 256 | .channel = 0, |
| 257 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 258 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 259 | .ext_info = mcp4725_ext_info, |
| 260 | }, |
| 261 | [MCP4726] = { |
| 262 | .type = IIO_VOLTAGE, |
| 263 | .indexed = 1, |
| 264 | .output = 1, |
| 265 | .channel = 0, |
| 266 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 267 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 268 | .ext_info = mcp4726_ext_info, |
| 269 | }, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static int mcp4725_set_value(struct iio_dev *indio_dev, int val) |
| 273 | { |
| 274 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 275 | u8 outbuf[2]; |
| 276 | int ret; |
| 277 | |
| 278 | if (val >= (1 << 12) || val < 0) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | outbuf[0] = (val >> 8) & 0xf; |
| 282 | outbuf[1] = val & 0xff; |
| 283 | |
| 284 | ret = i2c_master_send(data->client, outbuf, 2); |
| 285 | if (ret < 0) |
| 286 | return ret; |
| 287 | else if (ret != 2) |
| 288 | return -EIO; |
| 289 | else |
| 290 | return 0; |
| 291 | } |
| 292 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 293 | static int mcp4726_set_cfg(struct iio_dev *indio_dev) |
| 294 | { |
| 295 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 296 | u8 outbuf[3]; |
| 297 | int ret; |
| 298 | |
| 299 | outbuf[0] = 0x40; |
| 300 | outbuf[0] |= data->ref_mode << 3; |
| 301 | if (data->powerdown) |
| 302 | outbuf[0] |= data->powerdown << 1; |
| 303 | outbuf[1] = data->dac_value >> 4; |
| 304 | outbuf[2] = (data->dac_value & 0xf) << 4; |
| 305 | |
| 306 | ret = i2c_master_send(data->client, outbuf, 3); |
| 307 | if (ret < 0) |
| 308 | return ret; |
| 309 | else if (ret != 3) |
| 310 | return -EIO; |
| 311 | else |
| 312 | return 0; |
| 313 | } |
| 314 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 315 | static int mcp4725_read_raw(struct iio_dev *indio_dev, |
| 316 | struct iio_chan_spec const *chan, |
| 317 | int *val, int *val2, long mask) |
| 318 | { |
| 319 | struct mcp4725_data *data = iio_priv(indio_dev); |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 320 | int ret; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 321 | |
| 322 | switch (mask) { |
| 323 | case IIO_CHAN_INFO_RAW: |
| 324 | *val = data->dac_value; |
| 325 | return IIO_VAL_INT; |
| 326 | case IIO_CHAN_INFO_SCALE: |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 327 | if (data->ref_mode == MCP472X_REF_VDD) |
| 328 | ret = regulator_get_voltage(data->vdd_reg); |
| 329 | else |
| 330 | ret = regulator_get_voltage(data->vref_reg); |
| 331 | |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | return ret; |
| 334 | |
| 335 | *val = ret / 1000; |
Lars-Peter Clausen | b117f96 | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 336 | *val2 = 12; |
| 337 | return IIO_VAL_FRACTIONAL_LOG2; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 338 | } |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | static int mcp4725_write_raw(struct iio_dev *indio_dev, |
| 343 | struct iio_chan_spec const *chan, |
| 344 | int val, int val2, long mask) |
| 345 | { |
| 346 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 347 | int ret; |
| 348 | |
| 349 | switch (mask) { |
| 350 | case IIO_CHAN_INFO_RAW: |
| 351 | ret = mcp4725_set_value(indio_dev, val); |
| 352 | data->dac_value = val; |
| 353 | break; |
| 354 | default: |
| 355 | ret = -EINVAL; |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static const struct iio_info mcp4725_info = { |
| 363 | .read_raw = mcp4725_read_raw, |
| 364 | .write_raw = mcp4725_write_raw, |
Peter Meerwald | f17b77d | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 365 | .attrs = &mcp4725_attribute_group, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 366 | .driver_module = THIS_MODULE, |
| 367 | }; |
| 368 | |
Tomas Novotny | 88e39a8 | 2016-10-18 19:43:09 +0200 | [diff] [blame] | 369 | #ifdef CONFIG_OF |
| 370 | static int mcp4725_probe_dt(struct device *dev, |
| 371 | struct mcp4725_platform_data *pdata) |
| 372 | { |
| 373 | struct device_node *np = dev->of_node; |
| 374 | |
| 375 | if (!np) |
| 376 | return -ENODEV; |
| 377 | |
| 378 | /* check if is the vref-supply defined */ |
| 379 | pdata->use_vref = of_property_read_bool(np, "vref-supply"); |
| 380 | pdata->vref_buffered = |
| 381 | of_property_read_bool(np, "microchip,vref-buffered"); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | #else |
| 386 | static int mcp4725_probe_dt(struct device *dev, |
| 387 | struct mcp4725_platform_data *platform_data) |
| 388 | { |
| 389 | return -ENODEV; |
| 390 | } |
| 391 | #endif |
| 392 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 393 | static int mcp4725_probe(struct i2c_client *client, |
| 394 | const struct i2c_device_id *id) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 395 | { |
| 396 | struct mcp4725_data *data; |
| 397 | struct iio_dev *indio_dev; |
Tomas Novotny | 88e39a8 | 2016-10-18 19:43:09 +0200 | [diff] [blame] | 398 | struct mcp4725_platform_data *pdata, pdata_dt; |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 399 | u8 inbuf[4]; |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 400 | u8 pd; |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 401 | u8 ref; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 402 | int err; |
| 403 | |
Peter Meerwald | b6ff86f | 2013-07-29 23:18:00 +0100 | [diff] [blame] | 404 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 405 | if (indio_dev == NULL) |
| 406 | return -ENOMEM; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 407 | data = iio_priv(indio_dev); |
| 408 | i2c_set_clientdata(client, indio_dev); |
| 409 | data->client = client; |
Javier Martinez Canillas | 8414af1 | 2017-03-15 01:44:52 -0300 | [diff] [blame] | 410 | if (client->dev.of_node) |
| 411 | data->id = (enum chip_id)of_device_get_match_data(&client->dev); |
| 412 | else |
| 413 | data->id = id->driver_data; |
Tomas Novotny | 88e39a8 | 2016-10-18 19:43:09 +0200 | [diff] [blame] | 414 | pdata = dev_get_platdata(&client->dev); |
| 415 | |
| 416 | if (!pdata) { |
| 417 | err = mcp4725_probe_dt(&client->dev, &pdata_dt); |
| 418 | if (err) { |
| 419 | dev_err(&client->dev, |
| 420 | "invalid platform or devicetree data"); |
| 421 | return err; |
| 422 | } |
| 423 | pdata = &pdata_dt; |
| 424 | } |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 425 | |
| 426 | if (data->id == MCP4725 && pdata->use_vref) { |
| 427 | dev_err(&client->dev, |
| 428 | "external reference is unavailable on MCP4725"); |
| 429 | return -EINVAL; |
| 430 | } |
| 431 | |
| 432 | if (!pdata->use_vref && pdata->vref_buffered) { |
| 433 | dev_err(&client->dev, |
| 434 | "buffering is unavailable on the internal reference"); |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | if (!pdata->use_vref) |
| 439 | data->ref_mode = MCP472X_REF_VDD; |
| 440 | else |
| 441 | data->ref_mode = pdata->vref_buffered ? |
| 442 | MCP472X_REF_VREF_BUFFERED : |
| 443 | MCP472X_REF_VREF_UNBUFFERED; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 444 | |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 445 | data->vdd_reg = devm_regulator_get(&client->dev, "vdd"); |
| 446 | if (IS_ERR(data->vdd_reg)) |
| 447 | return PTR_ERR(data->vdd_reg); |
| 448 | |
| 449 | err = regulator_enable(data->vdd_reg); |
| 450 | if (err) |
| 451 | return err; |
| 452 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 453 | if (pdata->use_vref) { |
| 454 | data->vref_reg = devm_regulator_get(&client->dev, "vref"); |
| 455 | if (IS_ERR(data->vref_reg)) { |
Julia Lawall | 30df2d1 | 2016-10-22 22:41:32 +0200 | [diff] [blame] | 456 | err = PTR_ERR(data->vref_reg); |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 457 | goto err_disable_vdd_reg; |
| 458 | } |
| 459 | |
| 460 | err = regulator_enable(data->vref_reg); |
| 461 | if (err) |
| 462 | goto err_disable_vdd_reg; |
| 463 | } |
| 464 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 465 | indio_dev->dev.parent = &client->dev; |
Yong Li | 97a249e | 2016-01-06 09:09:43 +0800 | [diff] [blame] | 466 | indio_dev->name = id->name; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 467 | indio_dev->info = &mcp4725_info; |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 468 | indio_dev->channels = &mcp472x_channel[id->driver_data]; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 469 | indio_dev->num_channels = 1; |
| 470 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 471 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 472 | /* read current DAC value and settings */ |
| 473 | err = i2c_master_recv(client, inbuf, data->id == MCP4725 ? 3 : 4); |
| 474 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 475 | if (err < 0) { |
| 476 | dev_err(&client->dev, "failed to read DAC value"); |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 477 | goto err_disable_vref_reg; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 478 | } |
Peter Meerwald | 155f1b4 | 2013-06-23 22:30:00 +0100 | [diff] [blame] | 479 | pd = (inbuf[0] >> 1) & 0x3; |
| 480 | data->powerdown = pd > 0 ? true : false; |
Tomas Novotny | 6a31c22 | 2016-10-18 19:43:06 +0200 | [diff] [blame] | 481 | data->powerdown_mode = pd ? pd - 1 : 2; /* largest resistor to gnd */ |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 482 | data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4); |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 483 | if (data->id == MCP4726) |
| 484 | ref = (inbuf[3] >> 3) & 0x3; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 485 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 486 | if (data->id == MCP4726 && ref != data->ref_mode) { |
| 487 | dev_info(&client->dev, |
| 488 | "voltage reference mode differs (conf: %u, eeprom: %u), setting %u", |
| 489 | data->ref_mode, ref, data->ref_mode); |
| 490 | err = mcp4726_set_cfg(indio_dev); |
| 491 | if (err < 0) |
| 492 | goto err_disable_vref_reg; |
| 493 | } |
| 494 | |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 495 | err = iio_device_register(indio_dev); |
| 496 | if (err) |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 497 | goto err_disable_vref_reg; |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 498 | |
| 499 | return 0; |
| 500 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 501 | err_disable_vref_reg: |
| 502 | if (data->vref_reg) |
| 503 | regulator_disable(data->vref_reg); |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 504 | |
| 505 | err_disable_vdd_reg: |
| 506 | regulator_disable(data->vdd_reg); |
| 507 | |
| 508 | return err; |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 511 | static int mcp4725_remove(struct i2c_client *client) |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 512 | { |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 513 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 514 | struct mcp4725_data *data = iio_priv(indio_dev); |
| 515 | |
| 516 | iio_device_unregister(indio_dev); |
| 517 | |
Tomas Novotny | 29157c6 | 2016-10-18 19:43:08 +0200 | [diff] [blame] | 518 | if (data->vref_reg) |
| 519 | regulator_disable(data->vref_reg); |
Tomas Novotny | b440f1d | 2016-10-11 15:57:40 +0200 | [diff] [blame] | 520 | regulator_disable(data->vdd_reg); |
| 521 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static const struct i2c_device_id mcp4725_id[] = { |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 526 | { "mcp4725", MCP4725 }, |
| 527 | { "mcp4726", MCP4726 }, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 528 | { } |
| 529 | }; |
| 530 | MODULE_DEVICE_TABLE(i2c, mcp4725_id); |
| 531 | |
Javier Martinez Canillas | 8414af1 | 2017-03-15 01:44:52 -0300 | [diff] [blame] | 532 | #ifdef CONFIG_OF |
| 533 | static const struct of_device_id mcp4725_of_match[] = { |
| 534 | { |
| 535 | .compatible = "microchip,mcp4725", |
| 536 | .data = (void *)MCP4725 |
| 537 | }, |
| 538 | { |
| 539 | .compatible = "microchip,mcp4726", |
| 540 | .data = (void *)MCP4726 |
| 541 | }, |
| 542 | { } |
| 543 | }; |
| 544 | MODULE_DEVICE_TABLE(of, mcp4725_of_match); |
| 545 | #endif |
| 546 | |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 547 | static struct i2c_driver mcp4725_driver = { |
| 548 | .driver = { |
| 549 | .name = MCP4725_DRV_NAME, |
Javier Martinez Canillas | 8414af1 | 2017-03-15 01:44:52 -0300 | [diff] [blame] | 550 | .of_match_table = of_match_ptr(mcp4725_of_match), |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 551 | .pm = MCP4725_PM_OPS, |
| 552 | }, |
| 553 | .probe = mcp4725_probe, |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 554 | .remove = mcp4725_remove, |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 555 | .id_table = mcp4725_id, |
| 556 | }; |
| 557 | module_i2c_driver(mcp4725_driver); |
| 558 | |
| 559 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
Akinobu Mita | 35f7396 | 2015-12-26 21:17:22 +0900 | [diff] [blame] | 560 | MODULE_DESCRIPTION("MCP4725/6 12-bit DAC"); |
Peter Meerwald | cf35ad6 | 2012-06-08 18:06:45 +0200 | [diff] [blame] | 561 | MODULE_LICENSE("GPL"); |