Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / adc / ltc2497.c
blob1adddf5a88a94b0a2ae4284e1cf4033ce11fdc2c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC
5 * Copyright (C) 2017 Analog Devices Inc.
7 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf
8 */
10 #include <linux/i2c.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/driver.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
16 #include "ltc2497.h"
18 struct ltc2497_driverdata {
19 /* this must be the first member */
20 struct ltc2497core_driverdata common_ddata;
21 struct i2c_client *client;
23 * DMA (thus cache coherency maintenance) requires the
24 * transfer buffers to live in their own cache lines.
26 __be32 buf ____cacheline_aligned;
29 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
30 u8 address, int *val)
32 struct ltc2497_driverdata *st =
33 container_of(ddata, struct ltc2497_driverdata, common_ddata);
34 int ret;
36 if (val) {
37 ret = i2c_master_recv(st->client, (char *)&st->buf, 3);
38 if (ret < 0) {
39 dev_err(&st->client->dev, "i2c_master_recv failed\n");
40 return ret;
43 *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
46 ret = i2c_smbus_write_byte(st->client,
47 LTC2497_ENABLE | address);
48 if (ret)
49 dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
50 ERR_PTR(ret));
51 return ret;
54 static int ltc2497_probe(struct i2c_client *client,
55 const struct i2c_device_id *id)
57 struct iio_dev *indio_dev;
58 struct ltc2497_driverdata *st;
59 struct device *dev = &client->dev;
61 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
62 I2C_FUNC_SMBUS_WRITE_BYTE))
63 return -EOPNOTSUPP;
65 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
66 if (!indio_dev)
67 return -ENOMEM;
69 st = iio_priv(indio_dev);
70 i2c_set_clientdata(client, indio_dev);
71 st->client = client;
72 st->common_ddata.result_and_measure = ltc2497_result_and_measure;
74 return ltc2497core_probe(dev, indio_dev);
77 static int ltc2497_remove(struct i2c_client *client)
79 struct iio_dev *indio_dev = i2c_get_clientdata(client);
81 ltc2497core_remove(indio_dev);
83 return 0;
86 static const struct i2c_device_id ltc2497_id[] = {
87 { "ltc2497", 0 },
88 { }
90 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
92 static const struct of_device_id ltc2497_of_match[] = {
93 { .compatible = "lltc,ltc2497", },
94 {},
96 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
98 static struct i2c_driver ltc2497_driver = {
99 .driver = {
100 .name = "ltc2497",
101 .of_match_table = ltc2497_of_match,
103 .probe = ltc2497_probe,
104 .remove = ltc2497_remove,
105 .id_table = ltc2497_id,
107 module_i2c_driver(ltc2497_driver);
109 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
110 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
111 MODULE_LICENSE("GPL v2");