1 /*
2  * kxsd9.c	simple support for the Kionix KXSD9 3D
3  *		accelerometer.
4  *
5  * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * The i2c interface is very similar, so shouldn't be a problem once
12  * I have a suitable wire made up.
13  *
14  * TODO:	Support the motion detector
15  */
16 
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/sysfs.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/regmap.h>
23 #include <linux/bitops.h>
24 #include <linux/delay.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include <linux/iio/trigger_consumer.h>
32 
33 #include "kxsd9.h"
34 
35 #define KXSD9_REG_X		0x00
36 #define KXSD9_REG_Y		0x02
37 #define KXSD9_REG_Z		0x04
38 #define KXSD9_REG_AUX		0x06
39 #define KXSD9_REG_RESET		0x0a
40 #define KXSD9_REG_CTRL_C	0x0c
41 
42 #define KXSD9_CTRL_C_FS_MASK	0x03
43 #define KXSD9_CTRL_C_FS_8G	0x00
44 #define KXSD9_CTRL_C_FS_6G	0x01
45 #define KXSD9_CTRL_C_FS_4G	0x02
46 #define KXSD9_CTRL_C_FS_2G	0x03
47 #define KXSD9_CTRL_C_MOT_LAT	BIT(3)
48 #define KXSD9_CTRL_C_MOT_LEV	BIT(4)
49 #define KXSD9_CTRL_C_LP_MASK	0xe0
50 #define KXSD9_CTRL_C_LP_NONE	0x00
51 #define KXSD9_CTRL_C_LP_2000HZC	BIT(5)
52 #define KXSD9_CTRL_C_LP_2000HZB	BIT(6)
53 #define KXSD9_CTRL_C_LP_2000HZA	(BIT(5)|BIT(6))
54 #define KXSD9_CTRL_C_LP_1000HZ	BIT(7)
55 #define KXSD9_CTRL_C_LP_500HZ	(BIT(7)|BIT(5))
56 #define KXSD9_CTRL_C_LP_100HZ	(BIT(7)|BIT(6))
57 #define KXSD9_CTRL_C_LP_50HZ	(BIT(7)|BIT(6)|BIT(5))
58 
59 #define KXSD9_REG_CTRL_B	0x0d
60 
61 #define KXSD9_CTRL_B_CLK_HLD	BIT(7)
62 #define KXSD9_CTRL_B_ENABLE	BIT(6)
63 #define KXSD9_CTRL_B_ST		BIT(5) /* Self-test */
64 
65 #define KXSD9_REG_CTRL_A	0x0e
66 
67 /**
68  * struct kxsd9_state - device related storage
69  * @dev: pointer to the parent device
70  * @map: regmap to the device
71  * @orientation: mounting matrix, flipped axis etc
72  * @regs: regulators for this device, VDD and IOVDD
73  * @scale: the current scaling setting
74  */
75 struct kxsd9_state {
76 	struct device *dev;
77 	struct regmap *map;
78 	struct iio_mount_matrix orientation;
79 	struct regulator_bulk_data regs[2];
80 	u8 scale;
81 };
82 
83 #define KXSD9_SCALE_2G "0.011978"
84 #define KXSD9_SCALE_4G "0.023927"
85 #define KXSD9_SCALE_6G "0.035934"
86 #define KXSD9_SCALE_8G "0.047853"
87 
88 /* reverse order */
89 static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
90 
91 #define KXSD9_ZERO_G_OFFSET -2048
92 
93 /*
94  * Regulator names
95  */
96 static const char kxsd9_reg_vdd[] = "vdd";
97 static const char kxsd9_reg_iovdd[] = "iovdd";
98 
kxsd9_write_scale(struct iio_dev * indio_dev,int micro)99 static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
100 {
101 	int ret, i;
102 	struct kxsd9_state *st = iio_priv(indio_dev);
103 	bool foundit = false;
104 
105 	for (i = 0; i < 4; i++)
106 		if (micro == kxsd9_micro_scales[i]) {
107 			foundit = true;
108 			break;
109 		}
110 	if (!foundit)
111 		return -EINVAL;
112 
113 	ret = regmap_update_bits(st->map,
114 				 KXSD9_REG_CTRL_C,
115 				 KXSD9_CTRL_C_FS_MASK,
116 				 i);
117 	if (ret < 0)
118 		goto error_ret;
119 
120 	/* Cached scale when the sensor is powered down */
121 	st->scale = i;
122 
123 error_ret:
124 	return ret;
125 }
126 
127 static IIO_CONST_ATTR(accel_scale_available,
128 		KXSD9_SCALE_2G " "
129 		KXSD9_SCALE_4G " "
130 		KXSD9_SCALE_6G " "
131 		KXSD9_SCALE_8G);
132 
133 static struct attribute *kxsd9_attributes[] = {
134 	&iio_const_attr_accel_scale_available.dev_attr.attr,
135 	NULL,
136 };
137 
kxsd9_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)138 static int kxsd9_write_raw(struct iio_dev *indio_dev,
139 			   struct iio_chan_spec const *chan,
140 			   int val,
141 			   int val2,
142 			   long mask)
143 {
144 	int ret = -EINVAL;
145 	struct kxsd9_state *st = iio_priv(indio_dev);
146 
147 	pm_runtime_get_sync(st->dev);
148 
149 	if (mask == IIO_CHAN_INFO_SCALE) {
150 		/* Check no integer component */
151 		if (val)
152 			return -EINVAL;
153 		ret = kxsd9_write_scale(indio_dev, val2);
154 	}
155 
156 	pm_runtime_mark_last_busy(st->dev);
157 	pm_runtime_put_autosuspend(st->dev);
158 
159 	return ret;
160 }
161 
kxsd9_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)162 static int kxsd9_read_raw(struct iio_dev *indio_dev,
163 			  struct iio_chan_spec const *chan,
164 			  int *val, int *val2, long mask)
165 {
166 	int ret = -EINVAL;
167 	struct kxsd9_state *st = iio_priv(indio_dev);
168 	unsigned int regval;
169 	__be16 raw_val;
170 	u16 nval;
171 
172 	pm_runtime_get_sync(st->dev);
173 
174 	switch (mask) {
175 	case IIO_CHAN_INFO_RAW:
176 		ret = regmap_bulk_read(st->map, chan->address, &raw_val,
177 				       sizeof(raw_val));
178 		if (ret)
179 			goto error_ret;
180 		nval = be16_to_cpu(raw_val);
181 		/* Only 12 bits are valid */
182 		nval >>= 4;
183 		*val = nval;
184 		ret = IIO_VAL_INT;
185 		break;
186 	case IIO_CHAN_INFO_OFFSET:
187 		/* This has a bias of -2048 */
188 		*val = KXSD9_ZERO_G_OFFSET;
189 		ret = IIO_VAL_INT;
190 		break;
191 	case IIO_CHAN_INFO_SCALE:
192 		ret = regmap_read(st->map,
193 				  KXSD9_REG_CTRL_C,
194 				  &regval);
195 		if (ret < 0)
196 			goto error_ret;
197 		*val = 0;
198 		*val2 = kxsd9_micro_scales[regval & KXSD9_CTRL_C_FS_MASK];
199 		ret = IIO_VAL_INT_PLUS_MICRO;
200 		break;
201 	}
202 
203 error_ret:
204 	pm_runtime_mark_last_busy(st->dev);
205 	pm_runtime_put_autosuspend(st->dev);
206 
207 	return ret;
208 };
209 
kxsd9_trigger_handler(int irq,void * p)210 static irqreturn_t kxsd9_trigger_handler(int irq, void *p)
211 {
212 	const struct iio_poll_func *pf = p;
213 	struct iio_dev *indio_dev = pf->indio_dev;
214 	struct kxsd9_state *st = iio_priv(indio_dev);
215 	/*
216 	 * Ensure correct positioning and alignment of timestamp.
217 	 * No need to zero initialize as all elements written.
218 	 */
219 	struct {
220 		__be16 chan[4];
221 		s64 ts __aligned(8);
222 	} hw_values;
223 	int ret;
224 
225 	ret = regmap_bulk_read(st->map,
226 			       KXSD9_REG_X,
227 			       hw_values.chan,
228 			       sizeof(hw_values.chan));
229 	if (ret) {
230 		dev_err(st->dev, "error reading data: %d\n", ret);
231 		goto out;
232 	}
233 
234 	iio_push_to_buffers_with_timestamp(indio_dev,
235 					   &hw_values,
236 					   iio_get_time_ns(indio_dev));
237 out:
238 	iio_trigger_notify_done(indio_dev->trig);
239 
240 	return IRQ_HANDLED;
241 }
242 
kxsd9_buffer_preenable(struct iio_dev * indio_dev)243 static int kxsd9_buffer_preenable(struct iio_dev *indio_dev)
244 {
245 	struct kxsd9_state *st = iio_priv(indio_dev);
246 
247 	pm_runtime_get_sync(st->dev);
248 
249 	return 0;
250 }
251 
kxsd9_buffer_postdisable(struct iio_dev * indio_dev)252 static int kxsd9_buffer_postdisable(struct iio_dev *indio_dev)
253 {
254 	struct kxsd9_state *st = iio_priv(indio_dev);
255 
256 	pm_runtime_mark_last_busy(st->dev);
257 	pm_runtime_put_autosuspend(st->dev);
258 
259 	return 0;
260 }
261 
262 static const struct iio_buffer_setup_ops kxsd9_buffer_setup_ops = {
263 	.preenable = kxsd9_buffer_preenable,
264 	.postenable = iio_triggered_buffer_postenable,
265 	.predisable = iio_triggered_buffer_predisable,
266 	.postdisable = kxsd9_buffer_postdisable,
267 };
268 
269 static const struct iio_mount_matrix *
kxsd9_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)270 kxsd9_get_mount_matrix(const struct iio_dev *indio_dev,
271 		       const struct iio_chan_spec *chan)
272 {
273 	struct kxsd9_state *st = iio_priv(indio_dev);
274 
275 	return &st->orientation;
276 }
277 
278 static const struct iio_chan_spec_ext_info kxsd9_ext_info[] = {
279 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxsd9_get_mount_matrix),
280 	{ },
281 };
282 
283 #define KXSD9_ACCEL_CHAN(axis, index)						\
284 	{								\
285 		.type = IIO_ACCEL,					\
286 		.modified = 1,						\
287 		.channel2 = IIO_MOD_##axis,				\
288 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
289 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
290 					BIT(IIO_CHAN_INFO_OFFSET),	\
291 		.ext_info = kxsd9_ext_info,				\
292 		.address = KXSD9_REG_##axis,				\
293 		.scan_index = index,					\
294 		.scan_type = {                                          \
295 			.sign = 'u',					\
296 			.realbits = 12,					\
297 			.storagebits = 16,				\
298 			.shift = 4,					\
299 			.endianness = IIO_BE,				\
300 		},							\
301 	}
302 
303 static const struct iio_chan_spec kxsd9_channels[] = {
304 	KXSD9_ACCEL_CHAN(X, 0),
305 	KXSD9_ACCEL_CHAN(Y, 1),
306 	KXSD9_ACCEL_CHAN(Z, 2),
307 	{
308 		.type = IIO_VOLTAGE,
309 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
310 		.indexed = 1,
311 		.address = KXSD9_REG_AUX,
312 		.scan_index = 3,
313 		.scan_type = {
314 			.sign = 'u',
315 			.realbits = 12,
316 			.storagebits = 16,
317 			.shift = 4,
318 			.endianness = IIO_BE,
319 		},
320 	},
321 	IIO_CHAN_SOFT_TIMESTAMP(4),
322 };
323 
324 static const struct attribute_group kxsd9_attribute_group = {
325 	.attrs = kxsd9_attributes,
326 };
327 
kxsd9_power_up(struct kxsd9_state * st)328 static int kxsd9_power_up(struct kxsd9_state *st)
329 {
330 	int ret;
331 
332 	/* Enable the regulators */
333 	ret = regulator_bulk_enable(ARRAY_SIZE(st->regs), st->regs);
334 	if (ret) {
335 		dev_err(st->dev, "Cannot enable regulators\n");
336 		return ret;
337 	}
338 
339 	/* Power up */
340 	ret = regmap_write(st->map,
341 			   KXSD9_REG_CTRL_B,
342 			   KXSD9_CTRL_B_ENABLE);
343 	if (ret)
344 		return ret;
345 
346 	/*
347 	 * Set 1000Hz LPF, 2g fullscale, motion wakeup threshold 1g,
348 	 * latched wakeup
349 	 */
350 	ret = regmap_write(st->map,
351 			   KXSD9_REG_CTRL_C,
352 			   KXSD9_CTRL_C_LP_1000HZ |
353 			   KXSD9_CTRL_C_MOT_LEV	|
354 			   KXSD9_CTRL_C_MOT_LAT |
355 			   st->scale);
356 	if (ret)
357 		return ret;
358 
359 	/*
360 	 * Power-up time depends on the LPF setting, but typ 15.9 ms, let's
361 	 * set 20 ms to allow for some slack.
362 	 */
363 	msleep(20);
364 
365 	return 0;
366 };
367 
kxsd9_power_down(struct kxsd9_state * st)368 static int kxsd9_power_down(struct kxsd9_state *st)
369 {
370 	int ret;
371 
372 	/*
373 	 * Set into low power mode - since there may be more users of the
374 	 * regulators this is the first step of the power saving: it will
375 	 * make sure we conserve power even if there are others users on the
376 	 * regulators.
377 	 */
378 	ret = regmap_update_bits(st->map,
379 				 KXSD9_REG_CTRL_B,
380 				 KXSD9_CTRL_B_ENABLE,
381 				 0);
382 	if (ret)
383 		return ret;
384 
385 	/* Disable the regulators */
386 	ret = regulator_bulk_disable(ARRAY_SIZE(st->regs), st->regs);
387 	if (ret) {
388 		dev_err(st->dev, "Cannot disable regulators\n");
389 		return ret;
390 	}
391 
392 	return 0;
393 }
394 
395 static const struct iio_info kxsd9_info = {
396 	.read_raw = &kxsd9_read_raw,
397 	.write_raw = &kxsd9_write_raw,
398 	.attrs = &kxsd9_attribute_group,
399 };
400 
401 /* Four channels apart from timestamp, scan mask = 0x0f */
402 static const unsigned long kxsd9_scan_masks[] = { 0xf, 0 };
403 
kxsd9_common_probe(struct device * dev,struct regmap * map,const char * name)404 int kxsd9_common_probe(struct device *dev,
405 		       struct regmap *map,
406 		       const char *name)
407 {
408 	struct iio_dev *indio_dev;
409 	struct kxsd9_state *st;
410 	int ret;
411 
412 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
413 	if (!indio_dev)
414 		return -ENOMEM;
415 
416 	st = iio_priv(indio_dev);
417 	st->dev = dev;
418 	st->map = map;
419 
420 	indio_dev->channels = kxsd9_channels;
421 	indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
422 	indio_dev->name = name;
423 	indio_dev->dev.parent = dev;
424 	indio_dev->info = &kxsd9_info;
425 	indio_dev->modes = INDIO_DIRECT_MODE;
426 	indio_dev->available_scan_masks = kxsd9_scan_masks;
427 
428 	/* Read the mounting matrix, if present */
429 	ret = of_iio_read_mount_matrix(dev,
430 				       "mount-matrix",
431 				       &st->orientation);
432 	if (ret)
433 		return ret;
434 
435 	/* Fetch and turn on regulators */
436 	st->regs[0].supply = kxsd9_reg_vdd;
437 	st->regs[1].supply = kxsd9_reg_iovdd;
438 	ret = devm_regulator_bulk_get(dev,
439 				      ARRAY_SIZE(st->regs),
440 				      st->regs);
441 	if (ret) {
442 		dev_err(dev, "Cannot get regulators\n");
443 		return ret;
444 	}
445 	/* Default scaling */
446 	st->scale = KXSD9_CTRL_C_FS_2G;
447 
448 	kxsd9_power_up(st);
449 
450 	ret = iio_triggered_buffer_setup(indio_dev,
451 					 iio_pollfunc_store_time,
452 					 kxsd9_trigger_handler,
453 					 &kxsd9_buffer_setup_ops);
454 	if (ret) {
455 		dev_err(dev, "triggered buffer setup failed\n");
456 		goto err_power_down;
457 	}
458 
459 	ret = iio_device_register(indio_dev);
460 	if (ret)
461 		goto err_cleanup_buffer;
462 
463 	dev_set_drvdata(dev, indio_dev);
464 
465 	/* Enable runtime PM */
466 	pm_runtime_get_noresume(dev);
467 	pm_runtime_set_active(dev);
468 	pm_runtime_enable(dev);
469 	/*
470 	 * Set autosuspend to two orders of magnitude larger than the
471 	 * start-up time. 20ms start-up time means 2000ms autosuspend,
472 	 * i.e. 2 seconds.
473 	 */
474 	pm_runtime_set_autosuspend_delay(dev, 2000);
475 	pm_runtime_use_autosuspend(dev);
476 	pm_runtime_put(dev);
477 
478 	return 0;
479 
480 err_cleanup_buffer:
481 	iio_triggered_buffer_cleanup(indio_dev);
482 err_power_down:
483 	kxsd9_power_down(st);
484 
485 	return ret;
486 }
487 EXPORT_SYMBOL(kxsd9_common_probe);
488 
kxsd9_common_remove(struct device * dev)489 int kxsd9_common_remove(struct device *dev)
490 {
491 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
492 	struct kxsd9_state *st = iio_priv(indio_dev);
493 
494 	iio_triggered_buffer_cleanup(indio_dev);
495 	iio_device_unregister(indio_dev);
496 	pm_runtime_get_sync(dev);
497 	pm_runtime_put_noidle(dev);
498 	pm_runtime_disable(dev);
499 	kxsd9_power_down(st);
500 
501 	return 0;
502 }
503 EXPORT_SYMBOL(kxsd9_common_remove);
504 
505 #ifdef CONFIG_PM
kxsd9_runtime_suspend(struct device * dev)506 static int kxsd9_runtime_suspend(struct device *dev)
507 {
508 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
509 	struct kxsd9_state *st = iio_priv(indio_dev);
510 
511 	return kxsd9_power_down(st);
512 }
513 
kxsd9_runtime_resume(struct device * dev)514 static int kxsd9_runtime_resume(struct device *dev)
515 {
516 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
517 	struct kxsd9_state *st = iio_priv(indio_dev);
518 
519 	return kxsd9_power_up(st);
520 }
521 #endif /* CONFIG_PM */
522 
523 const struct dev_pm_ops kxsd9_dev_pm_ops = {
524 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
525 				pm_runtime_force_resume)
526 	SET_RUNTIME_PM_OPS(kxsd9_runtime_suspend,
527 			   kxsd9_runtime_resume, NULL)
528 };
529 EXPORT_SYMBOL(kxsd9_dev_pm_ops);
530 
531 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
532 MODULE_DESCRIPTION("Kionix KXSD9 driver");
533 MODULE_LICENSE("GPL v2");
534