1 /*
2  * IIO accel core driver for Freescale MMA7455L 3-axis 10-bit accelerometer
3  * Copyright 2015 Joachim Eastwood <manabian@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * UNSUPPORTED hardware features:
10  *  - 8-bit mode with different scales
11  *  - INT1/INT2 interrupts
12  *  - Offset calibration
13  *  - Events
14  */
15 
16 #include <linux/delay.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 
26 #include "mma7455.h"
27 
28 #define MMA7455_REG_XOUTL		0x00
29 #define MMA7455_REG_XOUTH		0x01
30 #define MMA7455_REG_YOUTL		0x02
31 #define MMA7455_REG_YOUTH		0x03
32 #define MMA7455_REG_ZOUTL		0x04
33 #define MMA7455_REG_ZOUTH		0x05
34 #define MMA7455_REG_STATUS		0x09
35 #define  MMA7455_STATUS_DRDY		BIT(0)
36 #define MMA7455_REG_WHOAMI		0x0f
37 #define  MMA7455_WHOAMI_ID		0x55
38 #define MMA7455_REG_MCTL		0x16
39 #define  MMA7455_MCTL_MODE_STANDBY	0x00
40 #define  MMA7455_MCTL_MODE_MEASURE	0x01
41 #define MMA7455_REG_CTL1		0x18
42 #define  MMA7455_CTL1_DFBW_MASK		BIT(7)
43 #define  MMA7455_CTL1_DFBW_125HZ	BIT(7)
44 #define  MMA7455_CTL1_DFBW_62_5HZ	0
45 #define MMA7455_REG_TW			0x1e
46 
47 /*
48  * When MMA7455 is used in 10-bit it has a fullscale of -8g
49  * corresponding to raw value -512. The userspace interface
50  * uses m/s^2 and we declare micro units.
51  * So scale factor is given by:
52  *       g * 8 * 1e6 / 512 = 153228.90625, with g = 9.80665
53  */
54 #define MMA7455_10BIT_SCALE	153229
55 
56 struct mma7455_data {
57 	struct regmap *regmap;
58 	/*
59 	 * Used to reorganize data.  Will ensure correct alignment of
60 	 * the timestamp if present
61 	 */
62 	struct {
63 		__le16 channels[3];
64 		s64 ts __aligned(8);
65 	} scan;
66 };
67 
mma7455_drdy(struct mma7455_data * mma7455)68 static int mma7455_drdy(struct mma7455_data *mma7455)
69 {
70 	struct device *dev = regmap_get_device(mma7455->regmap);
71 	unsigned int reg;
72 	int tries = 3;
73 	int ret;
74 
75 	while (tries-- > 0) {
76 		ret = regmap_read(mma7455->regmap, MMA7455_REG_STATUS, &reg);
77 		if (ret)
78 			return ret;
79 
80 		if (reg & MMA7455_STATUS_DRDY)
81 			return 0;
82 
83 		msleep(20);
84 	}
85 
86 	dev_warn(dev, "data not ready\n");
87 
88 	return -EIO;
89 }
90 
mma7455_trigger_handler(int irq,void * p)91 static irqreturn_t mma7455_trigger_handler(int irq, void *p)
92 {
93 	struct iio_poll_func *pf = p;
94 	struct iio_dev *indio_dev = pf->indio_dev;
95 	struct mma7455_data *mma7455 = iio_priv(indio_dev);
96 	int ret;
97 
98 	ret = mma7455_drdy(mma7455);
99 	if (ret)
100 		goto done;
101 
102 	ret = regmap_bulk_read(mma7455->regmap, MMA7455_REG_XOUTL,
103 			       mma7455->scan.channels,
104 			       sizeof(mma7455->scan.channels));
105 	if (ret)
106 		goto done;
107 
108 	iio_push_to_buffers_with_timestamp(indio_dev, &mma7455->scan,
109 					   iio_get_time_ns(indio_dev));
110 
111 done:
112 	iio_trigger_notify_done(indio_dev->trig);
113 
114 	return IRQ_HANDLED;
115 }
116 
mma7455_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)117 static int mma7455_read_raw(struct iio_dev *indio_dev,
118 			    struct iio_chan_spec const *chan,
119 			    int *val, int *val2, long mask)
120 {
121 	struct mma7455_data *mma7455 = iio_priv(indio_dev);
122 	unsigned int reg;
123 	__le16 data;
124 	int ret;
125 
126 	switch (mask) {
127 	case IIO_CHAN_INFO_RAW:
128 		if (iio_buffer_enabled(indio_dev))
129 			return -EBUSY;
130 
131 		ret = mma7455_drdy(mma7455);
132 		if (ret)
133 			return ret;
134 
135 		ret = regmap_bulk_read(mma7455->regmap, chan->address, &data,
136 				       sizeof(data));
137 		if (ret)
138 			return ret;
139 
140 		*val = sign_extend32(le16_to_cpu(data), 9);
141 
142 		return IIO_VAL_INT;
143 
144 	case IIO_CHAN_INFO_SCALE:
145 		*val = 0;
146 		*val2 = MMA7455_10BIT_SCALE;
147 
148 		return IIO_VAL_INT_PLUS_MICRO;
149 
150 	case IIO_CHAN_INFO_SAMP_FREQ:
151 		ret = regmap_read(mma7455->regmap, MMA7455_REG_CTL1, &reg);
152 		if (ret)
153 			return ret;
154 
155 		if (reg & MMA7455_CTL1_DFBW_MASK)
156 			*val = 250;
157 		else
158 			*val = 125;
159 
160 		return IIO_VAL_INT;
161 	}
162 
163 	return -EINVAL;
164 }
165 
mma7455_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)166 static int mma7455_write_raw(struct iio_dev *indio_dev,
167 			     struct iio_chan_spec const *chan,
168 			     int val, int val2, long mask)
169 {
170 	struct mma7455_data *mma7455 = iio_priv(indio_dev);
171 	int i;
172 
173 	switch (mask) {
174 	case IIO_CHAN_INFO_SAMP_FREQ:
175 		if (val == 250 && val2 == 0)
176 			i = MMA7455_CTL1_DFBW_125HZ;
177 		else if (val == 125 && val2 == 0)
178 			i = MMA7455_CTL1_DFBW_62_5HZ;
179 		else
180 			return -EINVAL;
181 
182 		return regmap_update_bits(mma7455->regmap, MMA7455_REG_CTL1,
183 					  MMA7455_CTL1_DFBW_MASK, i);
184 
185 	case IIO_CHAN_INFO_SCALE:
186 		/* In 10-bit mode there is only one scale available */
187 		if (val == 0 && val2 == MMA7455_10BIT_SCALE)
188 			return 0;
189 		break;
190 	}
191 
192 	return -EINVAL;
193 }
194 
195 static IIO_CONST_ATTR(sampling_frequency_available, "125 250");
196 
197 static struct attribute *mma7455_attributes[] = {
198 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
199 	NULL
200 };
201 
202 static const struct attribute_group mma7455_group = {
203 	.attrs = mma7455_attributes,
204 };
205 
206 static const struct iio_info mma7455_info = {
207 	.attrs = &mma7455_group,
208 	.read_raw = mma7455_read_raw,
209 	.write_raw = mma7455_write_raw,
210 };
211 
212 #define MMA7455_CHANNEL(axis, idx) { \
213 	.type = IIO_ACCEL, \
214 	.modified = 1, \
215 	.address = MMA7455_REG_##axis##OUTL,\
216 	.channel2 = IIO_MOD_##axis, \
217 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
218 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
219 				    BIT(IIO_CHAN_INFO_SCALE), \
220 	.scan_index = idx, \
221 	.scan_type = { \
222 		.sign = 's', \
223 		.realbits = 10, \
224 		.storagebits = 16, \
225 		.endianness = IIO_LE, \
226 	}, \
227 }
228 
229 static const struct iio_chan_spec mma7455_channels[] = {
230 	MMA7455_CHANNEL(X, 0),
231 	MMA7455_CHANNEL(Y, 1),
232 	MMA7455_CHANNEL(Z, 2),
233 	IIO_CHAN_SOFT_TIMESTAMP(3),
234 };
235 
236 static const unsigned long mma7455_scan_masks[] = {0x7, 0};
237 
238 const struct regmap_config mma7455_core_regmap = {
239 	.reg_bits = 8,
240 	.val_bits = 8,
241 	.max_register = MMA7455_REG_TW,
242 };
243 EXPORT_SYMBOL_GPL(mma7455_core_regmap);
244 
mma7455_core_probe(struct device * dev,struct regmap * regmap,const char * name)245 int mma7455_core_probe(struct device *dev, struct regmap *regmap,
246 		       const char *name)
247 {
248 	struct mma7455_data *mma7455;
249 	struct iio_dev *indio_dev;
250 	unsigned int reg;
251 	int ret;
252 
253 	ret = regmap_read(regmap, MMA7455_REG_WHOAMI, &reg);
254 	if (ret) {
255 		dev_err(dev, "unable to read reg\n");
256 		return ret;
257 	}
258 
259 	if (reg != MMA7455_WHOAMI_ID) {
260 		dev_err(dev, "device id mismatch\n");
261 		return -ENODEV;
262 	}
263 
264 	indio_dev = devm_iio_device_alloc(dev, sizeof(*mma7455));
265 	if (!indio_dev)
266 		return -ENOMEM;
267 
268 	dev_set_drvdata(dev, indio_dev);
269 	mma7455 = iio_priv(indio_dev);
270 	mma7455->regmap = regmap;
271 
272 	indio_dev->info = &mma7455_info;
273 	indio_dev->name = name;
274 	indio_dev->dev.parent = dev;
275 	indio_dev->modes = INDIO_DIRECT_MODE;
276 	indio_dev->channels = mma7455_channels;
277 	indio_dev->num_channels = ARRAY_SIZE(mma7455_channels);
278 	indio_dev->available_scan_masks = mma7455_scan_masks;
279 
280 	regmap_write(mma7455->regmap, MMA7455_REG_MCTL,
281 		     MMA7455_MCTL_MODE_MEASURE);
282 
283 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
284 					 mma7455_trigger_handler, NULL);
285 	if (ret) {
286 		dev_err(dev, "unable to setup triggered buffer\n");
287 		return ret;
288 	}
289 
290 	ret = iio_device_register(indio_dev);
291 	if (ret) {
292 		dev_err(dev, "unable to register device\n");
293 		iio_triggered_buffer_cleanup(indio_dev);
294 		return ret;
295 	}
296 
297 	return 0;
298 }
299 EXPORT_SYMBOL_GPL(mma7455_core_probe);
300 
mma7455_core_remove(struct device * dev)301 int mma7455_core_remove(struct device *dev)
302 {
303 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
304 	struct mma7455_data *mma7455 = iio_priv(indio_dev);
305 
306 	iio_device_unregister(indio_dev);
307 	iio_triggered_buffer_cleanup(indio_dev);
308 
309 	regmap_write(mma7455->regmap, MMA7455_REG_MCTL,
310 		     MMA7455_MCTL_MODE_STANDBY);
311 
312 	return 0;
313 }
314 EXPORT_SYMBOL_GPL(mma7455_core_remove);
315 
316 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
317 MODULE_DESCRIPTION("Freescale MMA7455L core accelerometer driver");
318 MODULE_LICENSE("GPL v2");
319