Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Industrial I/O driver for Microchip digital potentiometers |
| 4 | * Copyright (c) 2018 Axentia Technologies AB |
| 5 | * Author: Peter Rosin <peda@axentia.se> |
| 6 | * |
| 7 | * Datasheet: http://www.microchip.com/downloads/en/DeviceDoc/22147a.pdf |
| 8 | * |
| 9 | * DEVID #Wipers #Positions Resistor Opts (kOhm) |
| 10 | * mcp4017 1 128 5, 10, 50, 100 |
| 11 | * mcp4018 1 128 5, 10, 50, 100 |
| 12 | * mcp4019 1 128 5, 10, 50, 100 |
| 13 | */ |
| 14 | |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | |
| 22 | #define MCP4018_WIPER_MAX 127 |
| 23 | |
| 24 | struct mcp4018_cfg { |
| 25 | int kohms; |
| 26 | }; |
| 27 | |
| 28 | enum mcp4018_type { |
| 29 | MCP4018_502, |
| 30 | MCP4018_103, |
| 31 | MCP4018_503, |
| 32 | MCP4018_104, |
| 33 | }; |
| 34 | |
| 35 | static const struct mcp4018_cfg mcp4018_cfg[] = { |
| 36 | [MCP4018_502] = { .kohms = 5, }, |
| 37 | [MCP4018_103] = { .kohms = 10, }, |
| 38 | [MCP4018_503] = { .kohms = 50, }, |
| 39 | [MCP4018_104] = { .kohms = 100, }, |
| 40 | }; |
| 41 | |
| 42 | struct mcp4018_data { |
| 43 | struct i2c_client *client; |
| 44 | const struct mcp4018_cfg *cfg; |
| 45 | }; |
| 46 | |
| 47 | static const struct iio_chan_spec mcp4018_channel = { |
| 48 | .type = IIO_RESISTANCE, |
| 49 | .indexed = 1, |
| 50 | .output = 1, |
| 51 | .channel = 0, |
| 52 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 53 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 54 | }; |
| 55 | |
| 56 | static int mcp4018_read_raw(struct iio_dev *indio_dev, |
| 57 | struct iio_chan_spec const *chan, |
| 58 | int *val, int *val2, long mask) |
| 59 | { |
| 60 | struct mcp4018_data *data = iio_priv(indio_dev); |
| 61 | s32 ret; |
| 62 | |
| 63 | switch (mask) { |
| 64 | case IIO_CHAN_INFO_RAW: |
| 65 | ret = i2c_smbus_read_byte(data->client); |
| 66 | if (ret < 0) |
| 67 | return ret; |
| 68 | *val = ret; |
| 69 | return IIO_VAL_INT; |
| 70 | case IIO_CHAN_INFO_SCALE: |
| 71 | *val = 1000 * data->cfg->kohms; |
| 72 | *val2 = MCP4018_WIPER_MAX; |
| 73 | return IIO_VAL_FRACTIONAL; |
| 74 | } |
| 75 | |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | static int mcp4018_write_raw(struct iio_dev *indio_dev, |
| 80 | struct iio_chan_spec const *chan, |
| 81 | int val, int val2, long mask) |
| 82 | { |
| 83 | struct mcp4018_data *data = iio_priv(indio_dev); |
| 84 | |
| 85 | switch (mask) { |
| 86 | case IIO_CHAN_INFO_RAW: |
| 87 | if (val > MCP4018_WIPER_MAX || val < 0) |
| 88 | return -EINVAL; |
| 89 | break; |
| 90 | default: |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | return i2c_smbus_write_byte(data->client, val); |
| 95 | } |
| 96 | |
| 97 | static const struct iio_info mcp4018_info = { |
| 98 | .read_raw = mcp4018_read_raw, |
| 99 | .write_raw = mcp4018_write_raw, |
| 100 | }; |
| 101 | |
Peter Rosin | 0f6f400 | 2018-04-11 14:49:14 +0200 | [diff] [blame] | 102 | static const struct i2c_device_id mcp4018_id[] = { |
| 103 | { "mcp4017-502", MCP4018_502 }, |
| 104 | { "mcp4017-103", MCP4018_103 }, |
| 105 | { "mcp4017-503", MCP4018_503 }, |
| 106 | { "mcp4017-104", MCP4018_104 }, |
| 107 | { "mcp4018-502", MCP4018_502 }, |
| 108 | { "mcp4018-103", MCP4018_103 }, |
| 109 | { "mcp4018-503", MCP4018_503 }, |
| 110 | { "mcp4018-104", MCP4018_104 }, |
| 111 | { "mcp4019-502", MCP4018_502 }, |
| 112 | { "mcp4019-103", MCP4018_103 }, |
| 113 | { "mcp4019-503", MCP4018_503 }, |
| 114 | { "mcp4019-104", MCP4018_104 }, |
| 115 | {} |
| 116 | }; |
| 117 | MODULE_DEVICE_TABLE(i2c, mcp4018_id); |
| 118 | |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 119 | #ifdef CONFIG_OF |
| 120 | |
| 121 | #define MCP4018_COMPATIBLE(of_compatible, cfg) { \ |
| 122 | .compatible = of_compatible, \ |
| 123 | .data = &mcp4018_cfg[cfg], \ |
| 124 | } |
| 125 | |
| 126 | static const struct of_device_id mcp4018_of_match[] = { |
| 127 | MCP4018_COMPATIBLE("microchip,mcp4017-502", MCP4018_502), |
| 128 | MCP4018_COMPATIBLE("microchip,mcp4017-103", MCP4018_103), |
| 129 | MCP4018_COMPATIBLE("microchip,mcp4017-503", MCP4018_503), |
| 130 | MCP4018_COMPATIBLE("microchip,mcp4017-104", MCP4018_104), |
| 131 | MCP4018_COMPATIBLE("microchip,mcp4018-502", MCP4018_502), |
| 132 | MCP4018_COMPATIBLE("microchip,mcp4018-103", MCP4018_103), |
| 133 | MCP4018_COMPATIBLE("microchip,mcp4018-503", MCP4018_503), |
| 134 | MCP4018_COMPATIBLE("microchip,mcp4018-104", MCP4018_104), |
| 135 | MCP4018_COMPATIBLE("microchip,mcp4019-502", MCP4018_502), |
| 136 | MCP4018_COMPATIBLE("microchip,mcp4019-103", MCP4018_103), |
| 137 | MCP4018_COMPATIBLE("microchip,mcp4019-503", MCP4018_503), |
| 138 | MCP4018_COMPATIBLE("microchip,mcp4019-104", MCP4018_104), |
| 139 | { /* sentinel */ } |
| 140 | }; |
| 141 | MODULE_DEVICE_TABLE(of, mcp4018_of_match); |
| 142 | |
| 143 | #endif |
| 144 | |
Peter Rosin | 0f6f400 | 2018-04-11 14:49:14 +0200 | [diff] [blame] | 145 | static int mcp4018_probe(struct i2c_client *client) |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 146 | { |
| 147 | struct device *dev = &client->dev; |
| 148 | struct mcp4018_data *data; |
| 149 | struct iio_dev *indio_dev; |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 150 | |
| 151 | if (!i2c_check_functionality(client->adapter, |
| 152 | I2C_FUNC_SMBUS_BYTE)) { |
| 153 | dev_err(dev, "SMBUS Byte transfers not supported\n"); |
| 154 | return -EOPNOTSUPP; |
| 155 | } |
| 156 | |
| 157 | indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
| 158 | if (!indio_dev) |
| 159 | return -ENOMEM; |
| 160 | data = iio_priv(indio_dev); |
| 161 | i2c_set_clientdata(client, indio_dev); |
| 162 | data->client = client; |
| 163 | |
Julia Lawall | ce7c637 | 2018-05-21 11:49:10 +0200 | [diff] [blame] | 164 | data->cfg = of_device_get_match_data(dev); |
| 165 | if (!data->cfg) |
Peter Rosin | 0f6f400 | 2018-04-11 14:49:14 +0200 | [diff] [blame] | 166 | data->cfg = &mcp4018_cfg[i2c_match_id(mcp4018_id, client)->driver_data]; |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 167 | |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 168 | indio_dev->info = &mcp4018_info; |
| 169 | indio_dev->channels = &mcp4018_channel; |
| 170 | indio_dev->num_channels = 1; |
| 171 | indio_dev->name = client->name; |
| 172 | |
| 173 | return devm_iio_device_register(dev, indio_dev); |
| 174 | } |
| 175 | |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 176 | static struct i2c_driver mcp4018_driver = { |
| 177 | .driver = { |
| 178 | .name = "mcp4018", |
| 179 | .of_match_table = of_match_ptr(mcp4018_of_match), |
| 180 | }, |
Peter Rosin | 0f6f400 | 2018-04-11 14:49:14 +0200 | [diff] [blame] | 181 | .probe_new = mcp4018_probe, |
Peter Rosin | 5214ad6 | 2018-03-08 20:58:30 +0100 | [diff] [blame] | 182 | .id_table = mcp4018_id, |
| 183 | }; |
| 184 | |
| 185 | module_i2c_driver(mcp4018_driver); |
| 186 | |
| 187 | MODULE_AUTHOR("Peter Rosin <peda@axentia.se>"); |
| 188 | MODULE_DESCRIPTION("MCP4018 digital potentiometer"); |
Peter Rosin | b9ea8c3 | 2018-08-20 12:01:10 +0200 | [diff] [blame] | 189 | MODULE_LICENSE("GPL v2"); |