1c05dc2ccSPeter Rosin /* 2c05dc2ccSPeter Rosin * Industrial I/O driver for Microchip digital potentiometers 3c05dc2ccSPeter Rosin * Copyright (c) 2015 Axentia Technologies AB 4c05dc2ccSPeter Rosin * Author: Peter Rosin <peda@axentia.se> 5c05dc2ccSPeter Rosin * 6c05dc2ccSPeter Rosin * Datasheet: http://www.microchip.com/downloads/en/DeviceDoc/22096b.pdf 7c05dc2ccSPeter Rosin * 8c05dc2ccSPeter Rosin * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address 9c05dc2ccSPeter Rosin * mcp4531 1 129 5, 10, 50, 100 010111x 10c05dc2ccSPeter Rosin * mcp4532 1 129 5, 10, 50, 100 01011xx 11*294ea6f3SFlorian Vaussard * mcp4541 1 129 5, 10, 50, 100 010111x 12*294ea6f3SFlorian Vaussard * mcp4542 1 129 5, 10, 50, 100 01011xx 13c05dc2ccSPeter Rosin * mcp4551 1 257 5, 10, 50, 100 010111x 14c05dc2ccSPeter Rosin * mcp4552 1 257 5, 10, 50, 100 01011xx 15*294ea6f3SFlorian Vaussard * mcp4561 1 257 5, 10, 50, 100 010111x 16*294ea6f3SFlorian Vaussard * mcp4562 1 257 5, 10, 50, 100 01011xx 17c05dc2ccSPeter Rosin * mcp4631 2 129 5, 10, 50, 100 0101xxx 18c05dc2ccSPeter Rosin * mcp4632 2 129 5, 10, 50, 100 01011xx 19*294ea6f3SFlorian Vaussard * mcp4641 2 129 5, 10, 50, 100 0101xxx 20*294ea6f3SFlorian Vaussard * mcp4642 2 129 5, 10, 50, 100 01011xx 21c05dc2ccSPeter Rosin * mcp4651 2 257 5, 10, 50, 100 0101xxx 22c05dc2ccSPeter Rosin * mcp4652 2 257 5, 10, 50, 100 01011xx 23*294ea6f3SFlorian Vaussard * mcp4661 2 257 5, 10, 50, 100 0101xxx 24*294ea6f3SFlorian Vaussard * mcp4662 2 257 5, 10, 50, 100 01011xx 25c05dc2ccSPeter Rosin * 26c05dc2ccSPeter Rosin * This program is free software; you can redistribute it and/or modify it 27c05dc2ccSPeter Rosin * under the terms of the GNU General Public License version 2 as published by 28c05dc2ccSPeter Rosin * the Free Software Foundation. 29c05dc2ccSPeter Rosin */ 30c05dc2ccSPeter Rosin 31c05dc2ccSPeter Rosin #include <linux/module.h> 32c05dc2ccSPeter Rosin #include <linux/i2c.h> 33c05dc2ccSPeter Rosin #include <linux/err.h> 34c05dc2ccSPeter Rosin 35c05dc2ccSPeter Rosin #include <linux/iio/iio.h> 36c05dc2ccSPeter Rosin 37c05dc2ccSPeter Rosin struct mcp4531_cfg { 38c05dc2ccSPeter Rosin int wipers; 39c05dc2ccSPeter Rosin int max_pos; 40c05dc2ccSPeter Rosin int kohms; 41c05dc2ccSPeter Rosin }; 42c05dc2ccSPeter Rosin 43c05dc2ccSPeter Rosin enum mcp4531_type { 44c05dc2ccSPeter Rosin MCP453x_502, 45c05dc2ccSPeter Rosin MCP453x_103, 46c05dc2ccSPeter Rosin MCP453x_503, 47c05dc2ccSPeter Rosin MCP453x_104, 48*294ea6f3SFlorian Vaussard MCP454x_502, 49*294ea6f3SFlorian Vaussard MCP454x_103, 50*294ea6f3SFlorian Vaussard MCP454x_503, 51*294ea6f3SFlorian Vaussard MCP454x_104, 52c05dc2ccSPeter Rosin MCP455x_502, 53c05dc2ccSPeter Rosin MCP455x_103, 54c05dc2ccSPeter Rosin MCP455x_503, 55c05dc2ccSPeter Rosin MCP455x_104, 56*294ea6f3SFlorian Vaussard MCP456x_502, 57*294ea6f3SFlorian Vaussard MCP456x_103, 58*294ea6f3SFlorian Vaussard MCP456x_503, 59*294ea6f3SFlorian Vaussard MCP456x_104, 60c05dc2ccSPeter Rosin MCP463x_502, 61c05dc2ccSPeter Rosin MCP463x_103, 62c05dc2ccSPeter Rosin MCP463x_503, 63c05dc2ccSPeter Rosin MCP463x_104, 64*294ea6f3SFlorian Vaussard MCP464x_502, 65*294ea6f3SFlorian Vaussard MCP464x_103, 66*294ea6f3SFlorian Vaussard MCP464x_503, 67*294ea6f3SFlorian Vaussard MCP464x_104, 68c05dc2ccSPeter Rosin MCP465x_502, 69c05dc2ccSPeter Rosin MCP465x_103, 70c05dc2ccSPeter Rosin MCP465x_503, 71c05dc2ccSPeter Rosin MCP465x_104, 72*294ea6f3SFlorian Vaussard MCP466x_502, 73*294ea6f3SFlorian Vaussard MCP466x_103, 74*294ea6f3SFlorian Vaussard MCP466x_503, 75*294ea6f3SFlorian Vaussard MCP466x_104, 76c05dc2ccSPeter Rosin }; 77c05dc2ccSPeter Rosin 78c05dc2ccSPeter Rosin static const struct mcp4531_cfg mcp4531_cfg[] = { 79c05dc2ccSPeter Rosin [MCP453x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, }, 80c05dc2ccSPeter Rosin [MCP453x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, }, 81c05dc2ccSPeter Rosin [MCP453x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, }, 82c05dc2ccSPeter Rosin [MCP453x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, }, 83*294ea6f3SFlorian Vaussard [MCP454x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, }, 84*294ea6f3SFlorian Vaussard [MCP454x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, }, 85*294ea6f3SFlorian Vaussard [MCP454x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, }, 86*294ea6f3SFlorian Vaussard [MCP454x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, }, 87c05dc2ccSPeter Rosin [MCP455x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, }, 88c05dc2ccSPeter Rosin [MCP455x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, }, 89c05dc2ccSPeter Rosin [MCP455x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, }, 90c05dc2ccSPeter Rosin [MCP455x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, }, 91*294ea6f3SFlorian Vaussard [MCP456x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, }, 92*294ea6f3SFlorian Vaussard [MCP456x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, }, 93*294ea6f3SFlorian Vaussard [MCP456x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, }, 94*294ea6f3SFlorian Vaussard [MCP456x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, }, 95c05dc2ccSPeter Rosin [MCP463x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, }, 96c05dc2ccSPeter Rosin [MCP463x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, }, 97c05dc2ccSPeter Rosin [MCP463x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, }, 98c05dc2ccSPeter Rosin [MCP463x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, }, 99*294ea6f3SFlorian Vaussard [MCP464x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, }, 100*294ea6f3SFlorian Vaussard [MCP464x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, }, 101*294ea6f3SFlorian Vaussard [MCP464x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, }, 102*294ea6f3SFlorian Vaussard [MCP464x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, }, 103c05dc2ccSPeter Rosin [MCP465x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, }, 104c05dc2ccSPeter Rosin [MCP465x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, }, 105c05dc2ccSPeter Rosin [MCP465x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, }, 106c05dc2ccSPeter Rosin [MCP465x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, }, 107*294ea6f3SFlorian Vaussard [MCP466x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, }, 108*294ea6f3SFlorian Vaussard [MCP466x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, }, 109*294ea6f3SFlorian Vaussard [MCP466x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, }, 110*294ea6f3SFlorian Vaussard [MCP466x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, }, 111c05dc2ccSPeter Rosin }; 112c05dc2ccSPeter Rosin 113c05dc2ccSPeter Rosin #define MCP4531_WRITE (0 << 2) 114c05dc2ccSPeter Rosin #define MCP4531_INCR (1 << 2) 115c05dc2ccSPeter Rosin #define MCP4531_DECR (2 << 2) 116c05dc2ccSPeter Rosin #define MCP4531_READ (3 << 2) 117c05dc2ccSPeter Rosin 118c05dc2ccSPeter Rosin #define MCP4531_WIPER_SHIFT (4) 119c05dc2ccSPeter Rosin 120c05dc2ccSPeter Rosin struct mcp4531_data { 121c05dc2ccSPeter Rosin struct i2c_client *client; 12291307cbeSSlawomir Stepien const struct mcp4531_cfg *cfg; 123c05dc2ccSPeter Rosin }; 124c05dc2ccSPeter Rosin 125c05dc2ccSPeter Rosin #define MCP4531_CHANNEL(ch) { \ 126c05dc2ccSPeter Rosin .type = IIO_RESISTANCE, \ 127c05dc2ccSPeter Rosin .indexed = 1, \ 128c05dc2ccSPeter Rosin .output = 1, \ 129c05dc2ccSPeter Rosin .channel = (ch), \ 130c05dc2ccSPeter Rosin .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 131c05dc2ccSPeter Rosin .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 132c05dc2ccSPeter Rosin } 133c05dc2ccSPeter Rosin 134c05dc2ccSPeter Rosin static const struct iio_chan_spec mcp4531_channels[] = { 135c05dc2ccSPeter Rosin MCP4531_CHANNEL(0), 136c05dc2ccSPeter Rosin MCP4531_CHANNEL(1), 137c05dc2ccSPeter Rosin }; 138c05dc2ccSPeter Rosin 139c05dc2ccSPeter Rosin static int mcp4531_read_raw(struct iio_dev *indio_dev, 140c05dc2ccSPeter Rosin struct iio_chan_spec const *chan, 141c05dc2ccSPeter Rosin int *val, int *val2, long mask) 142c05dc2ccSPeter Rosin { 143c05dc2ccSPeter Rosin struct mcp4531_data *data = iio_priv(indio_dev); 144c05dc2ccSPeter Rosin int address = chan->channel << MCP4531_WIPER_SHIFT; 145c05dc2ccSPeter Rosin s32 ret; 146c05dc2ccSPeter Rosin 147c05dc2ccSPeter Rosin switch (mask) { 148c05dc2ccSPeter Rosin case IIO_CHAN_INFO_RAW: 149c05dc2ccSPeter Rosin ret = i2c_smbus_read_word_swapped(data->client, 150c05dc2ccSPeter Rosin MCP4531_READ | address); 151c05dc2ccSPeter Rosin if (ret < 0) 152c05dc2ccSPeter Rosin return ret; 153c05dc2ccSPeter Rosin *val = ret; 154c05dc2ccSPeter Rosin return IIO_VAL_INT; 155c05dc2ccSPeter Rosin case IIO_CHAN_INFO_SCALE: 15691307cbeSSlawomir Stepien *val = 1000 * data->cfg->kohms; 15791307cbeSSlawomir Stepien *val2 = data->cfg->max_pos; 158c05dc2ccSPeter Rosin return IIO_VAL_FRACTIONAL; 159c05dc2ccSPeter Rosin } 160c05dc2ccSPeter Rosin 161c05dc2ccSPeter Rosin return -EINVAL; 162c05dc2ccSPeter Rosin } 163c05dc2ccSPeter Rosin 164c05dc2ccSPeter Rosin static int mcp4531_write_raw(struct iio_dev *indio_dev, 165c05dc2ccSPeter Rosin struct iio_chan_spec const *chan, 166c05dc2ccSPeter Rosin int val, int val2, long mask) 167c05dc2ccSPeter Rosin { 168c05dc2ccSPeter Rosin struct mcp4531_data *data = iio_priv(indio_dev); 169c05dc2ccSPeter Rosin int address = chan->channel << MCP4531_WIPER_SHIFT; 170c05dc2ccSPeter Rosin 171c05dc2ccSPeter Rosin switch (mask) { 172c05dc2ccSPeter Rosin case IIO_CHAN_INFO_RAW: 17391307cbeSSlawomir Stepien if (val > data->cfg->max_pos || val < 0) 174c05dc2ccSPeter Rosin return -EINVAL; 175c05dc2ccSPeter Rosin break; 176c05dc2ccSPeter Rosin default: 177c05dc2ccSPeter Rosin return -EINVAL; 178c05dc2ccSPeter Rosin } 179c05dc2ccSPeter Rosin 180c05dc2ccSPeter Rosin return i2c_smbus_write_byte_data(data->client, 181c05dc2ccSPeter Rosin MCP4531_WRITE | address | (val >> 8), 182c05dc2ccSPeter Rosin val & 0xff); 183c05dc2ccSPeter Rosin } 184c05dc2ccSPeter Rosin 185c05dc2ccSPeter Rosin static const struct iio_info mcp4531_info = { 186c05dc2ccSPeter Rosin .read_raw = mcp4531_read_raw, 187c05dc2ccSPeter Rosin .write_raw = mcp4531_write_raw, 188c05dc2ccSPeter Rosin .driver_module = THIS_MODULE, 189c05dc2ccSPeter Rosin }; 190c05dc2ccSPeter Rosin 191c05dc2ccSPeter Rosin static int mcp4531_probe(struct i2c_client *client, 192c05dc2ccSPeter Rosin const struct i2c_device_id *id) 193c05dc2ccSPeter Rosin { 194c05dc2ccSPeter Rosin struct device *dev = &client->dev; 195c05dc2ccSPeter Rosin struct mcp4531_data *data; 196c05dc2ccSPeter Rosin struct iio_dev *indio_dev; 197c05dc2ccSPeter Rosin 198c05dc2ccSPeter Rosin if (!i2c_check_functionality(client->adapter, 199c05dc2ccSPeter Rosin I2C_FUNC_SMBUS_WORD_DATA)) { 200c05dc2ccSPeter Rosin dev_err(dev, "SMBUS Word Data not supported\n"); 201f8d9d3b4SMatt Ranostay return -EOPNOTSUPP; 202c05dc2ccSPeter Rosin } 203c05dc2ccSPeter Rosin 204c05dc2ccSPeter Rosin indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 205c05dc2ccSPeter Rosin if (!indio_dev) 206c05dc2ccSPeter Rosin return -ENOMEM; 207c05dc2ccSPeter Rosin data = iio_priv(indio_dev); 208c05dc2ccSPeter Rosin i2c_set_clientdata(client, indio_dev); 209c05dc2ccSPeter Rosin data->client = client; 21091307cbeSSlawomir Stepien data->cfg = &mcp4531_cfg[id->driver_data]; 211c05dc2ccSPeter Rosin 212c05dc2ccSPeter Rosin indio_dev->dev.parent = dev; 213c05dc2ccSPeter Rosin indio_dev->info = &mcp4531_info; 214c05dc2ccSPeter Rosin indio_dev->channels = mcp4531_channels; 21591307cbeSSlawomir Stepien indio_dev->num_channels = data->cfg->wipers; 216c05dc2ccSPeter Rosin indio_dev->name = client->name; 217c05dc2ccSPeter Rosin 218c05dc2ccSPeter Rosin return devm_iio_device_register(dev, indio_dev); 219c05dc2ccSPeter Rosin } 220c05dc2ccSPeter Rosin 221c05dc2ccSPeter Rosin static const struct i2c_device_id mcp4531_id[] = { 222c05dc2ccSPeter Rosin { "mcp4531-502", MCP453x_502 }, 223c05dc2ccSPeter Rosin { "mcp4531-103", MCP453x_103 }, 224c05dc2ccSPeter Rosin { "mcp4531-503", MCP453x_503 }, 225c05dc2ccSPeter Rosin { "mcp4531-104", MCP453x_104 }, 226c05dc2ccSPeter Rosin { "mcp4532-502", MCP453x_502 }, 227c05dc2ccSPeter Rosin { "mcp4532-103", MCP453x_103 }, 228c05dc2ccSPeter Rosin { "mcp4532-503", MCP453x_503 }, 229c05dc2ccSPeter Rosin { "mcp4532-104", MCP453x_104 }, 230*294ea6f3SFlorian Vaussard { "mcp4541-502", MCP454x_502 }, 231*294ea6f3SFlorian Vaussard { "mcp4541-103", MCP454x_103 }, 232*294ea6f3SFlorian Vaussard { "mcp4541-503", MCP454x_503 }, 233*294ea6f3SFlorian Vaussard { "mcp4541-104", MCP454x_104 }, 234*294ea6f3SFlorian Vaussard { "mcp4542-502", MCP454x_502 }, 235*294ea6f3SFlorian Vaussard { "mcp4542-103", MCP454x_103 }, 236*294ea6f3SFlorian Vaussard { "mcp4542-503", MCP454x_503 }, 237*294ea6f3SFlorian Vaussard { "mcp4542-104", MCP454x_104 }, 238c05dc2ccSPeter Rosin { "mcp4551-502", MCP455x_502 }, 239c05dc2ccSPeter Rosin { "mcp4551-103", MCP455x_103 }, 240c05dc2ccSPeter Rosin { "mcp4551-503", MCP455x_503 }, 241c05dc2ccSPeter Rosin { "mcp4551-104", MCP455x_104 }, 242c05dc2ccSPeter Rosin { "mcp4552-502", MCP455x_502 }, 243c05dc2ccSPeter Rosin { "mcp4552-103", MCP455x_103 }, 244c05dc2ccSPeter Rosin { "mcp4552-503", MCP455x_503 }, 245c05dc2ccSPeter Rosin { "mcp4552-104", MCP455x_104 }, 246*294ea6f3SFlorian Vaussard { "mcp4561-502", MCP456x_502 }, 247*294ea6f3SFlorian Vaussard { "mcp4561-103", MCP456x_103 }, 248*294ea6f3SFlorian Vaussard { "mcp4561-503", MCP456x_503 }, 249*294ea6f3SFlorian Vaussard { "mcp4561-104", MCP456x_104 }, 250*294ea6f3SFlorian Vaussard { "mcp4562-502", MCP456x_502 }, 251*294ea6f3SFlorian Vaussard { "mcp4562-103", MCP456x_103 }, 252*294ea6f3SFlorian Vaussard { "mcp4562-503", MCP456x_503 }, 253*294ea6f3SFlorian Vaussard { "mcp4562-104", MCP456x_104 }, 254c05dc2ccSPeter Rosin { "mcp4631-502", MCP463x_502 }, 255c05dc2ccSPeter Rosin { "mcp4631-103", MCP463x_103 }, 256c05dc2ccSPeter Rosin { "mcp4631-503", MCP463x_503 }, 257c05dc2ccSPeter Rosin { "mcp4631-104", MCP463x_104 }, 258c05dc2ccSPeter Rosin { "mcp4632-502", MCP463x_502 }, 259c05dc2ccSPeter Rosin { "mcp4632-103", MCP463x_103 }, 260c05dc2ccSPeter Rosin { "mcp4632-503", MCP463x_503 }, 261c05dc2ccSPeter Rosin { "mcp4632-104", MCP463x_104 }, 262*294ea6f3SFlorian Vaussard { "mcp4641-502", MCP464x_502 }, 263*294ea6f3SFlorian Vaussard { "mcp4641-103", MCP464x_103 }, 264*294ea6f3SFlorian Vaussard { "mcp4641-503", MCP464x_503 }, 265*294ea6f3SFlorian Vaussard { "mcp4641-104", MCP464x_104 }, 266*294ea6f3SFlorian Vaussard { "mcp4642-502", MCP464x_502 }, 267*294ea6f3SFlorian Vaussard { "mcp4642-103", MCP464x_103 }, 268*294ea6f3SFlorian Vaussard { "mcp4642-503", MCP464x_503 }, 269*294ea6f3SFlorian Vaussard { "mcp4642-104", MCP464x_104 }, 270c05dc2ccSPeter Rosin { "mcp4651-502", MCP465x_502 }, 271c05dc2ccSPeter Rosin { "mcp4651-103", MCP465x_103 }, 272c05dc2ccSPeter Rosin { "mcp4651-503", MCP465x_503 }, 273c05dc2ccSPeter Rosin { "mcp4651-104", MCP465x_104 }, 274c05dc2ccSPeter Rosin { "mcp4652-502", MCP465x_502 }, 275c05dc2ccSPeter Rosin { "mcp4652-103", MCP465x_103 }, 276c05dc2ccSPeter Rosin { "mcp4652-503", MCP465x_503 }, 277c05dc2ccSPeter Rosin { "mcp4652-104", MCP465x_104 }, 278*294ea6f3SFlorian Vaussard { "mcp4661-502", MCP466x_502 }, 279*294ea6f3SFlorian Vaussard { "mcp4661-103", MCP466x_103 }, 280*294ea6f3SFlorian Vaussard { "mcp4661-503", MCP466x_503 }, 281*294ea6f3SFlorian Vaussard { "mcp4661-104", MCP466x_104 }, 282*294ea6f3SFlorian Vaussard { "mcp4662-502", MCP466x_502 }, 283*294ea6f3SFlorian Vaussard { "mcp4662-103", MCP466x_103 }, 284*294ea6f3SFlorian Vaussard { "mcp4662-503", MCP466x_503 }, 285*294ea6f3SFlorian Vaussard { "mcp4662-104", MCP466x_104 }, 286c05dc2ccSPeter Rosin {} 287c05dc2ccSPeter Rosin }; 288c05dc2ccSPeter Rosin MODULE_DEVICE_TABLE(i2c, mcp4531_id); 289c05dc2ccSPeter Rosin 290c05dc2ccSPeter Rosin static struct i2c_driver mcp4531_driver = { 291c05dc2ccSPeter Rosin .driver = { 292c05dc2ccSPeter Rosin .name = "mcp4531", 293c05dc2ccSPeter Rosin }, 294c05dc2ccSPeter Rosin .probe = mcp4531_probe, 295c05dc2ccSPeter Rosin .id_table = mcp4531_id, 296c05dc2ccSPeter Rosin }; 297c05dc2ccSPeter Rosin 298c05dc2ccSPeter Rosin module_i2c_driver(mcp4531_driver); 299c05dc2ccSPeter Rosin 300c05dc2ccSPeter Rosin MODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); 301c05dc2ccSPeter Rosin MODULE_DESCRIPTION("MCP4531 digital potentiometer"); 302c05dc2ccSPeter Rosin MODULE_LICENSE("GPL"); 303