1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * iio/dac/max5821.c 4 * Copyright (C) 2014 Philippe Reynes 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/i2c.h> 10 #include <linux/iio/iio.h> 11 #include <linux/regulator/consumer.h> 12 13 #define MAX5821_MAX_DAC_CHANNELS 2 14 15 /* command bytes */ 16 #define MAX5821_LOAD_DAC_A_IN_REG_B 0x00 17 #define MAX5821_LOAD_DAC_B_IN_REG_A 0x10 18 #define MAX5821_EXTENDED_COMMAND_MODE 0xf0 19 #define MAX5821_READ_DAC_A_COMMAND 0xf1 20 #define MAX5821_READ_DAC_B_COMMAND 0xf2 21 22 #define MAX5821_EXTENDED_POWER_UP 0x00 23 #define MAX5821_EXTENDED_POWER_DOWN_MODE0 0x01 24 #define MAX5821_EXTENDED_POWER_DOWN_MODE1 0x02 25 #define MAX5821_EXTENDED_POWER_DOWN_MODE2 0x03 26 #define MAX5821_EXTENDED_DAC_A 0x04 27 #define MAX5821_EXTENDED_DAC_B 0x08 28 29 enum max5821_device_ids { 30 ID_MAX5821, 31 }; 32 33 struct max5821_data { 34 struct i2c_client *client; 35 struct regulator *vref_reg; 36 unsigned short vref_mv; 37 bool powerdown[MAX5821_MAX_DAC_CHANNELS]; 38 u8 powerdown_mode[MAX5821_MAX_DAC_CHANNELS]; 39 struct mutex lock; 40 }; 41 42 static const char * const max5821_powerdown_modes[] = { 43 "three_state", 44 "1kohm_to_gnd", 45 "100kohm_to_gnd", 46 }; 47 48 enum { 49 MAX5821_THREE_STATE, 50 MAX5821_1KOHM_TO_GND, 51 MAX5821_100KOHM_TO_GND 52 }; 53 54 static int max5821_get_powerdown_mode(struct iio_dev *indio_dev, 55 const struct iio_chan_spec *chan) 56 { 57 struct max5821_data *st = iio_priv(indio_dev); 58 59 return st->powerdown_mode[chan->channel]; 60 } 61 62 static int max5821_set_powerdown_mode(struct iio_dev *indio_dev, 63 const struct iio_chan_spec *chan, 64 unsigned int mode) 65 { 66 struct max5821_data *st = iio_priv(indio_dev); 67 68 st->powerdown_mode[chan->channel] = mode; 69 70 return 0; 71 } 72 73 static const struct iio_enum max5821_powerdown_mode_enum = { 74 .items = max5821_powerdown_modes, 75 .num_items = ARRAY_SIZE(max5821_powerdown_modes), 76 .get = max5821_get_powerdown_mode, 77 .set = max5821_set_powerdown_mode, 78 }; 79 80 static ssize_t max5821_read_dac_powerdown(struct iio_dev *indio_dev, 81 uintptr_t private, 82 const struct iio_chan_spec *chan, 83 char *buf) 84 { 85 struct max5821_data *st = iio_priv(indio_dev); 86 87 return sysfs_emit(buf, "%d\n", st->powerdown[chan->channel]); 88 } 89 90 static int max5821_sync_powerdown_mode(struct max5821_data *data, 91 const struct iio_chan_spec *chan) 92 { 93 u8 outbuf[2]; 94 95 outbuf[0] = MAX5821_EXTENDED_COMMAND_MODE; 96 97 if (chan->channel == 0) 98 outbuf[1] = MAX5821_EXTENDED_DAC_A; 99 else 100 outbuf[1] = MAX5821_EXTENDED_DAC_B; 101 102 if (data->powerdown[chan->channel]) 103 outbuf[1] |= data->powerdown_mode[chan->channel] + 1; 104 else 105 outbuf[1] |= MAX5821_EXTENDED_POWER_UP; 106 107 return i2c_master_send(data->client, outbuf, 2); 108 } 109 110 static ssize_t max5821_write_dac_powerdown(struct iio_dev *indio_dev, 111 uintptr_t private, 112 const struct iio_chan_spec *chan, 113 const char *buf, size_t len) 114 { 115 struct max5821_data *data = iio_priv(indio_dev); 116 bool powerdown; 117 int ret; 118 119 ret = strtobool(buf, &powerdown); 120 if (ret) 121 return ret; 122 123 data->powerdown[chan->channel] = powerdown; 124 125 ret = max5821_sync_powerdown_mode(data, chan); 126 if (ret < 0) 127 return ret; 128 129 return len; 130 } 131 132 static const struct iio_chan_spec_ext_info max5821_ext_info[] = { 133 { 134 .name = "powerdown", 135 .read = max5821_read_dac_powerdown, 136 .write = max5821_write_dac_powerdown, 137 .shared = IIO_SEPARATE, 138 }, 139 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &max5821_powerdown_mode_enum), 140 IIO_ENUM_AVAILABLE("powerdown_mode", &max5821_powerdown_mode_enum), 141 { }, 142 }; 143 144 #define MAX5821_CHANNEL(chan) { \ 145 .type = IIO_VOLTAGE, \ 146 .indexed = 1, \ 147 .output = 1, \ 148 .channel = (chan), \ 149 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 150 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \ 151 .ext_info = max5821_ext_info, \ 152 } 153 154 static const struct iio_chan_spec max5821_channels[] = { 155 MAX5821_CHANNEL(0), 156 MAX5821_CHANNEL(1) 157 }; 158 159 static const u8 max5821_read_dac_command[] = { 160 MAX5821_READ_DAC_A_COMMAND, 161 MAX5821_READ_DAC_B_COMMAND 162 }; 163 164 static const u8 max5821_load_dac_command[] = { 165 MAX5821_LOAD_DAC_A_IN_REG_B, 166 MAX5821_LOAD_DAC_B_IN_REG_A 167 }; 168 169 static int max5821_get_value(struct iio_dev *indio_dev, 170 int *val, int channel) 171 { 172 struct max5821_data *data = iio_priv(indio_dev); 173 struct i2c_client *client = data->client; 174 u8 outbuf[1]; 175 u8 inbuf[2]; 176 int ret; 177 178 if ((channel != 0) && (channel != 1)) 179 return -EINVAL; 180 181 outbuf[0] = max5821_read_dac_command[channel]; 182 183 mutex_lock(&data->lock); 184 185 ret = i2c_master_send(client, outbuf, 1); 186 if (ret < 0) { 187 mutex_unlock(&data->lock); 188 return ret; 189 } else if (ret != 1) { 190 mutex_unlock(&data->lock); 191 return -EIO; 192 } 193 194 ret = i2c_master_recv(client, inbuf, 2); 195 if (ret < 0) { 196 mutex_unlock(&data->lock); 197 return ret; 198 } else if (ret != 2) { 199 mutex_unlock(&data->lock); 200 return -EIO; 201 } 202 203 mutex_unlock(&data->lock); 204 205 *val = ((inbuf[0] & 0x0f) << 6) | (inbuf[1] >> 2); 206 207 return IIO_VAL_INT; 208 } 209 210 static int max5821_set_value(struct iio_dev *indio_dev, 211 int val, int channel) 212 { 213 struct max5821_data *data = iio_priv(indio_dev); 214 struct i2c_client *client = data->client; 215 u8 outbuf[2]; 216 int ret; 217 218 if ((val < 0) || (val > 1023)) 219 return -EINVAL; 220 221 if ((channel != 0) && (channel != 1)) 222 return -EINVAL; 223 224 outbuf[0] = max5821_load_dac_command[channel]; 225 outbuf[0] |= val >> 6; 226 outbuf[1] = (val & 0x3f) << 2; 227 228 ret = i2c_master_send(client, outbuf, 2); 229 if (ret < 0) 230 return ret; 231 else if (ret != 2) 232 return -EIO; 233 else 234 return 0; 235 } 236 237 static int max5821_read_raw(struct iio_dev *indio_dev, 238 struct iio_chan_spec const *chan, 239 int *val, int *val2, long mask) 240 { 241 struct max5821_data *data = iio_priv(indio_dev); 242 243 switch (mask) { 244 case IIO_CHAN_INFO_RAW: 245 return max5821_get_value(indio_dev, val, chan->channel); 246 case IIO_CHAN_INFO_SCALE: 247 *val = data->vref_mv; 248 *val2 = 10; 249 return IIO_VAL_FRACTIONAL_LOG2; 250 default: 251 return -EINVAL; 252 } 253 } 254 255 static int max5821_write_raw(struct iio_dev *indio_dev, 256 struct iio_chan_spec const *chan, 257 int val, int val2, long mask) 258 { 259 if (val2 != 0) 260 return -EINVAL; 261 262 switch (mask) { 263 case IIO_CHAN_INFO_RAW: 264 return max5821_set_value(indio_dev, val, chan->channel); 265 default: 266 return -EINVAL; 267 } 268 } 269 270 static int __maybe_unused max5821_suspend(struct device *dev) 271 { 272 u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE, 273 MAX5821_EXTENDED_DAC_A | 274 MAX5821_EXTENDED_DAC_B | 275 MAX5821_EXTENDED_POWER_DOWN_MODE2 }; 276 277 return i2c_master_send(to_i2c_client(dev), outbuf, 2); 278 } 279 280 static int __maybe_unused max5821_resume(struct device *dev) 281 { 282 u8 outbuf[2] = { MAX5821_EXTENDED_COMMAND_MODE, 283 MAX5821_EXTENDED_DAC_A | 284 MAX5821_EXTENDED_DAC_B | 285 MAX5821_EXTENDED_POWER_UP }; 286 287 return i2c_master_send(to_i2c_client(dev), outbuf, 2); 288 } 289 290 static SIMPLE_DEV_PM_OPS(max5821_pm_ops, max5821_suspend, max5821_resume); 291 292 static const struct iio_info max5821_info = { 293 .read_raw = max5821_read_raw, 294 .write_raw = max5821_write_raw, 295 }; 296 297 static int max5821_probe(struct i2c_client *client, 298 const struct i2c_device_id *id) 299 { 300 struct max5821_data *data; 301 struct iio_dev *indio_dev; 302 u32 tmp; 303 int ret; 304 305 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 306 if (!indio_dev) 307 return -ENOMEM; 308 data = iio_priv(indio_dev); 309 i2c_set_clientdata(client, indio_dev); 310 data->client = client; 311 mutex_init(&data->lock); 312 313 /* max5821 start in powerdown mode 100Kohm to ground */ 314 for (tmp = 0; tmp < MAX5821_MAX_DAC_CHANNELS; tmp++) { 315 data->powerdown[tmp] = true; 316 data->powerdown_mode[tmp] = MAX5821_100KOHM_TO_GND; 317 } 318 319 data->vref_reg = devm_regulator_get(&client->dev, "vref"); 320 if (IS_ERR(data->vref_reg)) { 321 ret = PTR_ERR(data->vref_reg); 322 dev_err(&client->dev, 323 "Failed to get vref regulator: %d\n", ret); 324 goto error_free_reg; 325 } 326 327 ret = regulator_enable(data->vref_reg); 328 if (ret) { 329 dev_err(&client->dev, 330 "Failed to enable vref regulator: %d\n", ret); 331 goto error_free_reg; 332 } 333 334 ret = regulator_get_voltage(data->vref_reg); 335 if (ret < 0) { 336 dev_err(&client->dev, 337 "Failed to get voltage on regulator: %d\n", ret); 338 goto error_disable_reg; 339 } 340 341 data->vref_mv = ret / 1000; 342 343 indio_dev->name = id->name; 344 indio_dev->num_channels = ARRAY_SIZE(max5821_channels); 345 indio_dev->channels = max5821_channels; 346 indio_dev->modes = INDIO_DIRECT_MODE; 347 indio_dev->info = &max5821_info; 348 349 return iio_device_register(indio_dev); 350 351 error_disable_reg: 352 regulator_disable(data->vref_reg); 353 354 error_free_reg: 355 356 return ret; 357 } 358 359 static int max5821_remove(struct i2c_client *client) 360 { 361 struct iio_dev *indio_dev = i2c_get_clientdata(client); 362 struct max5821_data *data = iio_priv(indio_dev); 363 364 iio_device_unregister(indio_dev); 365 regulator_disable(data->vref_reg); 366 367 return 0; 368 } 369 370 static const struct i2c_device_id max5821_id[] = { 371 { "max5821", ID_MAX5821 }, 372 { } 373 }; 374 MODULE_DEVICE_TABLE(i2c, max5821_id); 375 376 static const struct of_device_id max5821_of_match[] = { 377 { .compatible = "maxim,max5821" }, 378 { } 379 }; 380 MODULE_DEVICE_TABLE(of, max5821_of_match); 381 382 static struct i2c_driver max5821_driver = { 383 .driver = { 384 .name = "max5821", 385 .of_match_table = max5821_of_match, 386 .pm = &max5821_pm_ops, 387 }, 388 .probe = max5821_probe, 389 .remove = max5821_remove, 390 .id_table = max5821_id, 391 }; 392 module_i2c_driver(max5821_driver); 393 394 MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>"); 395 MODULE_DESCRIPTION("MAX5821 DAC"); 396 MODULE_LICENSE("GPL v2"); 397