0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/regulator/consumer.h>
0012 #include <linux/err.h>
0013 #include <linux/delay.h>
0014 #include <linux/iio/iio.h>
0015 #include <linux/iio/driver.h>
0016 #include <linux/iio/machine.h>
0017 #include <linux/iio/consumer.h>
0018
0019 #define DS4422_MAX_DAC_CHANNELS 2
0020 #define DS4424_MAX_DAC_CHANNELS 4
0021
0022 #define DS4424_DAC_ADDR(chan) ((chan) + 0xf8)
0023 #define DS4424_SOURCE_I 1
0024 #define DS4424_SINK_I 0
0025
0026 #define DS4424_CHANNEL(chan) { \
0027 .type = IIO_CURRENT, \
0028 .indexed = 1, \
0029 .output = 1, \
0030 .channel = chan, \
0031 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
0032 }
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 union ds4424_raw_data {
0043 struct {
0044 u8 dx:7;
0045 u8 source_bit:1;
0046 };
0047 u8 bits;
0048 };
0049
0050 enum ds4424_device_ids {
0051 ID_DS4422,
0052 ID_DS4424,
0053 };
0054
0055 struct ds4424_data {
0056 struct i2c_client *client;
0057 struct mutex lock;
0058 uint8_t save[DS4424_MAX_DAC_CHANNELS];
0059 struct regulator *vcc_reg;
0060 uint8_t raw[DS4424_MAX_DAC_CHANNELS];
0061 };
0062
0063 static const struct iio_chan_spec ds4424_channels[] = {
0064 DS4424_CHANNEL(0),
0065 DS4424_CHANNEL(1),
0066 DS4424_CHANNEL(2),
0067 DS4424_CHANNEL(3),
0068 };
0069
0070 static int ds4424_get_value(struct iio_dev *indio_dev,
0071 int *val, int channel)
0072 {
0073 struct ds4424_data *data = iio_priv(indio_dev);
0074 int ret;
0075
0076 mutex_lock(&data->lock);
0077 ret = i2c_smbus_read_byte_data(data->client, DS4424_DAC_ADDR(channel));
0078 if (ret < 0)
0079 goto fail;
0080
0081 *val = ret;
0082
0083 fail:
0084 mutex_unlock(&data->lock);
0085 return ret;
0086 }
0087
0088 static int ds4424_set_value(struct iio_dev *indio_dev,
0089 int val, struct iio_chan_spec const *chan)
0090 {
0091 struct ds4424_data *data = iio_priv(indio_dev);
0092 int ret;
0093
0094 mutex_lock(&data->lock);
0095 ret = i2c_smbus_write_byte_data(data->client,
0096 DS4424_DAC_ADDR(chan->channel), val);
0097 if (ret < 0)
0098 goto fail;
0099
0100 data->raw[chan->channel] = val;
0101
0102 fail:
0103 mutex_unlock(&data->lock);
0104 return ret;
0105 }
0106
0107 static int ds4424_read_raw(struct iio_dev *indio_dev,
0108 struct iio_chan_spec const *chan,
0109 int *val, int *val2, long mask)
0110 {
0111 union ds4424_raw_data raw;
0112 int ret;
0113
0114 switch (mask) {
0115 case IIO_CHAN_INFO_RAW:
0116 ret = ds4424_get_value(indio_dev, val, chan->channel);
0117 if (ret < 0) {
0118 pr_err("%s : ds4424_get_value returned %d\n",
0119 __func__, ret);
0120 return ret;
0121 }
0122 raw.bits = *val;
0123 *val = raw.dx;
0124 if (raw.source_bit == DS4424_SINK_I)
0125 *val = -*val;
0126 return IIO_VAL_INT;
0127
0128 default:
0129 return -EINVAL;
0130 }
0131 }
0132
0133 static int ds4424_write_raw(struct iio_dev *indio_dev,
0134 struct iio_chan_spec const *chan,
0135 int val, int val2, long mask)
0136 {
0137 union ds4424_raw_data raw;
0138
0139 if (val2 != 0)
0140 return -EINVAL;
0141
0142 switch (mask) {
0143 case IIO_CHAN_INFO_RAW:
0144 if (val < S8_MIN || val > S8_MAX)
0145 return -EINVAL;
0146
0147 if (val > 0) {
0148 raw.source_bit = DS4424_SOURCE_I;
0149 raw.dx = val;
0150 } else {
0151 raw.source_bit = DS4424_SINK_I;
0152 raw.dx = -val;
0153 }
0154
0155 return ds4424_set_value(indio_dev, raw.bits, chan);
0156
0157 default:
0158 return -EINVAL;
0159 }
0160 }
0161
0162 static int ds4424_verify_chip(struct iio_dev *indio_dev)
0163 {
0164 int ret, val;
0165
0166 ret = ds4424_get_value(indio_dev, &val, 0);
0167 if (ret < 0)
0168 dev_err(&indio_dev->dev,
0169 "%s failed. ret: %d\n", __func__, ret);
0170
0171 return ret;
0172 }
0173
0174 static int ds4424_suspend(struct device *dev)
0175 {
0176 struct i2c_client *client = to_i2c_client(dev);
0177 struct iio_dev *indio_dev = i2c_get_clientdata(client);
0178 struct ds4424_data *data = iio_priv(indio_dev);
0179 int ret = 0;
0180 int i;
0181
0182 for (i = 0; i < indio_dev->num_channels; i++) {
0183 data->save[i] = data->raw[i];
0184 ret = ds4424_set_value(indio_dev, 0,
0185 &indio_dev->channels[i]);
0186 if (ret < 0)
0187 return ret;
0188 }
0189 return ret;
0190 }
0191
0192 static int ds4424_resume(struct device *dev)
0193 {
0194 struct i2c_client *client = to_i2c_client(dev);
0195 struct iio_dev *indio_dev = i2c_get_clientdata(client);
0196 struct ds4424_data *data = iio_priv(indio_dev);
0197 int ret = 0;
0198 int i;
0199
0200 for (i = 0; i < indio_dev->num_channels; i++) {
0201 ret = ds4424_set_value(indio_dev, data->save[i],
0202 &indio_dev->channels[i]);
0203 if (ret < 0)
0204 return ret;
0205 }
0206 return ret;
0207 }
0208
0209 static DEFINE_SIMPLE_DEV_PM_OPS(ds4424_pm_ops, ds4424_suspend, ds4424_resume);
0210
0211 static const struct iio_info ds4424_info = {
0212 .read_raw = ds4424_read_raw,
0213 .write_raw = ds4424_write_raw,
0214 };
0215
0216 static int ds4424_probe(struct i2c_client *client,
0217 const struct i2c_device_id *id)
0218 {
0219 struct ds4424_data *data;
0220 struct iio_dev *indio_dev;
0221 int ret;
0222
0223 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0224 if (!indio_dev) {
0225 dev_err(&client->dev, "iio dev alloc failed.\n");
0226 return -ENOMEM;
0227 }
0228
0229 data = iio_priv(indio_dev);
0230 i2c_set_clientdata(client, indio_dev);
0231 data->client = client;
0232 indio_dev->name = id->name;
0233
0234 data->vcc_reg = devm_regulator_get(&client->dev, "vcc");
0235 if (IS_ERR(data->vcc_reg))
0236 return dev_err_probe(&client->dev, PTR_ERR(data->vcc_reg),
0237 "Failed to get vcc-supply regulator.\n");
0238
0239 mutex_init(&data->lock);
0240 ret = regulator_enable(data->vcc_reg);
0241 if (ret < 0) {
0242 dev_err(&client->dev,
0243 "Unable to enable the regulator.\n");
0244 return ret;
0245 }
0246
0247 usleep_range(1000, 1200);
0248 ret = ds4424_verify_chip(indio_dev);
0249 if (ret < 0)
0250 goto fail;
0251
0252 switch (id->driver_data) {
0253 case ID_DS4422:
0254 indio_dev->num_channels = DS4422_MAX_DAC_CHANNELS;
0255 break;
0256 case ID_DS4424:
0257 indio_dev->num_channels = DS4424_MAX_DAC_CHANNELS;
0258 break;
0259 default:
0260 dev_err(&client->dev,
0261 "ds4424: Invalid chip id.\n");
0262 ret = -ENXIO;
0263 goto fail;
0264 }
0265
0266 indio_dev->channels = ds4424_channels;
0267 indio_dev->modes = INDIO_DIRECT_MODE;
0268 indio_dev->info = &ds4424_info;
0269
0270 ret = iio_device_register(indio_dev);
0271 if (ret < 0) {
0272 dev_err(&client->dev,
0273 "iio_device_register failed. ret: %d\n", ret);
0274 goto fail;
0275 }
0276
0277 return ret;
0278
0279 fail:
0280 regulator_disable(data->vcc_reg);
0281 return ret;
0282 }
0283
0284 static int ds4424_remove(struct i2c_client *client)
0285 {
0286 struct iio_dev *indio_dev = i2c_get_clientdata(client);
0287 struct ds4424_data *data = iio_priv(indio_dev);
0288
0289 iio_device_unregister(indio_dev);
0290 regulator_disable(data->vcc_reg);
0291
0292 return 0;
0293 }
0294
0295 static const struct i2c_device_id ds4424_id[] = {
0296 { "ds4422", ID_DS4422 },
0297 { "ds4424", ID_DS4424 },
0298 { }
0299 };
0300
0301 MODULE_DEVICE_TABLE(i2c, ds4424_id);
0302
0303 static const struct of_device_id ds4424_of_match[] = {
0304 { .compatible = "maxim,ds4422" },
0305 { .compatible = "maxim,ds4424" },
0306 { },
0307 };
0308
0309 MODULE_DEVICE_TABLE(of, ds4424_of_match);
0310
0311 static struct i2c_driver ds4424_driver = {
0312 .driver = {
0313 .name = "ds4424",
0314 .of_match_table = ds4424_of_match,
0315 .pm = pm_sleep_ptr(&ds4424_pm_ops),
0316 },
0317 .probe = ds4424_probe,
0318 .remove = ds4424_remove,
0319 .id_table = ds4424_id,
0320 };
0321 module_i2c_driver(ds4424_driver);
0322
0323 MODULE_DESCRIPTION("Maxim DS4424 DAC Driver");
0324 MODULE_AUTHOR("Ismail H. Kose <ismail.kose@maximintegrated.com>");
0325 MODULE_AUTHOR("Vishal Sood <vishal.sood@maximintegrated.com>");
0326 MODULE_AUTHOR("David Jung <david.jung@maximintegrated.com>");
0327 MODULE_LICENSE("GPL v2");