xref: /openbmc/linux/drivers/iio/adc/ltc2485.c (revision 680ef72a)
1 /*
2  * ltc2485.c - Driver for Linear Technology LTC2485 ADC
3  *
4  * Copyright (C) 2016 Alison Schofield <amsfield22@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Datasheet: http://cds.linear.com/docs/en/datasheet/2485fd.pdf
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 
20 /* Power-on configuration: rejects both 50/60Hz, operates at 1x speed */
21 #define LTC2485_CONFIG_DEFAULT		0
22 
23 struct ltc2485_data {
24 	struct i2c_client	*client;
25 	ktime_t			time_prev;	/* last conversion */
26 };
27 
28 static void ltc2485_wait_conv(struct ltc2485_data *data)
29 {
30 	const unsigned int conv_time = 147;	/* conversion time ms */
31 	unsigned int time_elapsed;
32 
33 	/* delay if conversion time not passed since last read or write */
34 	time_elapsed = ktime_ms_delta(ktime_get(), data->time_prev);
35 
36 	if (time_elapsed < conv_time)
37 		msleep(conv_time - time_elapsed);
38 }
39 
40 static int ltc2485_read(struct ltc2485_data *data, int *val)
41 {
42 	struct i2c_client *client = data->client;
43 	__be32 buf = 0;
44 	int ret;
45 
46 	ltc2485_wait_conv(data);
47 
48 	ret = i2c_master_recv(client, (char *)&buf, 4);
49 	if (ret < 0)  {
50 		dev_err(&client->dev, "i2c_master_recv failed\n");
51 		return ret;
52 	}
53 	data->time_prev = ktime_get();
54 	*val = sign_extend32(be32_to_cpu(buf) >> 6, 24);
55 
56 	return ret;
57 }
58 
59 static int ltc2485_read_raw(struct iio_dev *indio_dev,
60 			    struct iio_chan_spec const *chan,
61 			    int *val, int *val2, long mask)
62 {
63 	struct ltc2485_data *data = iio_priv(indio_dev);
64 	int ret;
65 
66 	if (mask == IIO_CHAN_INFO_RAW) {
67 		ret = ltc2485_read(data, val);
68 		if (ret < 0)
69 			return ret;
70 
71 		return IIO_VAL_INT;
72 
73 	} else if (mask == IIO_CHAN_INFO_SCALE) {
74 		*val = 5000;			/* on board vref millivolts */
75 		*val2 = 25;			/* 25 (24 + sign) data bits */
76 		return IIO_VAL_FRACTIONAL_LOG2;
77 
78 	} else {
79 		return -EINVAL;
80 	}
81 }
82 
83 static const struct iio_chan_spec ltc2485_channel[] = {
84 	{
85 		.type = IIO_VOLTAGE,
86 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE)
88 	},
89 };
90 
91 static const struct iio_info ltc2485_info = {
92 	.read_raw = ltc2485_read_raw,
93 };
94 
95 static int ltc2485_probe(struct i2c_client *client,
96 			 const struct i2c_device_id *id)
97 {
98 	struct iio_dev *indio_dev;
99 	struct ltc2485_data *data;
100 	int ret;
101 
102 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
103 				     I2C_FUNC_SMBUS_WRITE_BYTE))
104 		return -EOPNOTSUPP;
105 
106 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
107 	if (!indio_dev)
108 		return -ENOMEM;
109 
110 	data = iio_priv(indio_dev);
111 	i2c_set_clientdata(client, indio_dev);
112 	data->client = client;
113 
114 	indio_dev->dev.parent = &client->dev;
115 	indio_dev->name = id->name;
116 	indio_dev->info = &ltc2485_info;
117 	indio_dev->modes = INDIO_DIRECT_MODE;
118 	indio_dev->channels = ltc2485_channel;
119 	indio_dev->num_channels = ARRAY_SIZE(ltc2485_channel);
120 
121 	ret = i2c_smbus_write_byte(data->client, LTC2485_CONFIG_DEFAULT);
122 	if (ret < 0)
123 		return ret;
124 
125 	data->time_prev = ktime_get();
126 
127 	return devm_iio_device_register(&client->dev, indio_dev);
128 }
129 
130 static const struct i2c_device_id ltc2485_id[] = {
131 	{ "ltc2485", 0 },
132 	{ }
133 };
134 MODULE_DEVICE_TABLE(i2c, ltc2485_id);
135 
136 static struct i2c_driver ltc2485_driver = {
137 	.driver = {
138 		.name = "ltc2485",
139 	},
140 	.probe = ltc2485_probe,
141 	.id_table = ltc2485_id,
142 };
143 module_i2c_driver(ltc2485_driver);
144 
145 MODULE_AUTHOR("Alison Schofield <amsfield22@gmail.com>");
146 MODULE_DESCRIPTION("Linear Technology LTC2485 ADC driver");
147 MODULE_LICENSE("GPL v2");
148