2 * kxsd9.c simple support for the Kionix KXSD9 3D
5 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
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.
11 * The i2c interface is very similar, so shouldn't be a problem once
12 * I have a suitable wire made up.
14 * TODO: Support the motion detector
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>
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
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))
59 #define KXSD9_REG_CTRL_B 0x0d
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 */
65 #define KXSD9_REG_CTRL_A 0x0e
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
78 struct iio_mount_matrix orientation
;
79 struct regulator_bulk_data regs
[2];
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"
89 static const int kxsd9_micro_scales
[4] = { 47853, 35934, 23927, 11978 };
91 #define KXSD9_ZERO_G_OFFSET -2048
96 static const char kxsd9_reg_vdd
[] = "vdd";
97 static const char kxsd9_reg_iovdd
[] = "iovdd";
99 static int kxsd9_write_scale(struct iio_dev
*indio_dev
, int micro
)
102 struct kxsd9_state
*st
= iio_priv(indio_dev
);
103 bool foundit
= false;
105 for (i
= 0; i
< 4; i
++)
106 if (micro
== kxsd9_micro_scales
[i
]) {
113 ret
= regmap_update_bits(st
->map
,
115 KXSD9_CTRL_C_FS_MASK
,
120 /* Cached scale when the sensor is powered down */
127 static IIO_CONST_ATTR(accel_scale_available
,
133 static struct attribute
*kxsd9_attributes
[] = {
134 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
138 static int kxsd9_write_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *chan
,
145 struct kxsd9_state
*st
= iio_priv(indio_dev
);
147 pm_runtime_get_sync(st
->dev
);
149 if (mask
== IIO_CHAN_INFO_SCALE
) {
150 /* Check no integer component */
153 ret
= kxsd9_write_scale(indio_dev
, val2
);
156 pm_runtime_mark_last_busy(st
->dev
);
157 pm_runtime_put_autosuspend(st
->dev
);
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
)
167 struct kxsd9_state
*st
= iio_priv(indio_dev
);
172 pm_runtime_get_sync(st
->dev
);
175 case IIO_CHAN_INFO_RAW
:
176 ret
= regmap_bulk_read(st
->map
, chan
->address
, &raw_val
,
180 nval
= be16_to_cpu(raw_val
);
181 /* Only 12 bits are valid */
186 case IIO_CHAN_INFO_OFFSET
:
187 /* This has a bias of -2048 */
188 *val
= KXSD9_ZERO_G_OFFSET
;
191 case IIO_CHAN_INFO_SCALE
:
192 ret
= regmap_read(st
->map
,
198 *val2
= kxsd9_micro_scales
[regval
& KXSD9_CTRL_C_FS_MASK
];
199 ret
= IIO_VAL_INT_PLUS_MICRO
;
204 pm_runtime_mark_last_busy(st
->dev
);
205 pm_runtime_put_autosuspend(st
->dev
);
210 static irqreturn_t
kxsd9_trigger_handler(int irq
, void *p
)
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
);
216 /* 4 * 16bit values AND timestamp */
219 ret
= regmap_bulk_read(st
->map
,
225 "error reading data\n");
229 iio_push_to_buffers_with_timestamp(indio_dev
,
231 iio_get_time_ns(indio_dev
));
232 iio_trigger_notify_done(indio_dev
->trig
);
237 static int kxsd9_buffer_preenable(struct iio_dev
*indio_dev
)
239 struct kxsd9_state
*st
= iio_priv(indio_dev
);
241 pm_runtime_get_sync(st
->dev
);
246 static int kxsd9_buffer_postdisable(struct iio_dev
*indio_dev
)
248 struct kxsd9_state
*st
= iio_priv(indio_dev
);
250 pm_runtime_mark_last_busy(st
->dev
);
251 pm_runtime_put_autosuspend(st
->dev
);
256 static const struct iio_buffer_setup_ops kxsd9_buffer_setup_ops
= {
257 .preenable
= kxsd9_buffer_preenable
,
258 .postenable
= iio_triggered_buffer_postenable
,
259 .predisable
= iio_triggered_buffer_predisable
,
260 .postdisable
= kxsd9_buffer_postdisable
,
263 static const struct iio_mount_matrix
*
264 kxsd9_get_mount_matrix(const struct iio_dev
*indio_dev
,
265 const struct iio_chan_spec
*chan
)
267 struct kxsd9_state
*st
= iio_priv(indio_dev
);
269 return &st
->orientation
;
272 static const struct iio_chan_spec_ext_info kxsd9_ext_info
[] = {
273 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, kxsd9_get_mount_matrix
),
277 #define KXSD9_ACCEL_CHAN(axis, index) \
281 .channel2 = IIO_MOD_##axis, \
282 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
283 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
284 BIT(IIO_CHAN_INFO_OFFSET), \
285 .ext_info = kxsd9_ext_info, \
286 .address = KXSD9_REG_##axis, \
287 .scan_index = index, \
293 .endianness = IIO_BE, \
297 static const struct iio_chan_spec kxsd9_channels
[] = {
298 KXSD9_ACCEL_CHAN(X
, 0),
299 KXSD9_ACCEL_CHAN(Y
, 1),
300 KXSD9_ACCEL_CHAN(Z
, 2),
303 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
305 .address
= KXSD9_REG_AUX
,
312 .endianness
= IIO_BE
,
315 IIO_CHAN_SOFT_TIMESTAMP(4),
318 static const struct attribute_group kxsd9_attribute_group
= {
319 .attrs
= kxsd9_attributes
,
322 static int kxsd9_power_up(struct kxsd9_state
*st
)
326 /* Enable the regulators */
327 ret
= regulator_bulk_enable(ARRAY_SIZE(st
->regs
), st
->regs
);
329 dev_err(st
->dev
, "Cannot enable regulators\n");
334 ret
= regmap_write(st
->map
,
336 KXSD9_CTRL_B_ENABLE
);
341 * Set 1000Hz LPF, 2g fullscale, motion wakeup threshold 1g,
344 ret
= regmap_write(st
->map
,
346 KXSD9_CTRL_C_LP_1000HZ
|
347 KXSD9_CTRL_C_MOT_LEV
|
348 KXSD9_CTRL_C_MOT_LAT
|
354 * Power-up time depends on the LPF setting, but typ 15.9 ms, let's
355 * set 20 ms to allow for some slack.
362 static int kxsd9_power_down(struct kxsd9_state
*st
)
367 * Set into low power mode - since there may be more users of the
368 * regulators this is the first step of the power saving: it will
369 * make sure we conserve power even if there are others users on the
372 ret
= regmap_update_bits(st
->map
,
379 /* Disable the regulators */
380 ret
= regulator_bulk_disable(ARRAY_SIZE(st
->regs
), st
->regs
);
382 dev_err(st
->dev
, "Cannot disable regulators\n");
389 static const struct iio_info kxsd9_info
= {
390 .read_raw
= &kxsd9_read_raw
,
391 .write_raw
= &kxsd9_write_raw
,
392 .attrs
= &kxsd9_attribute_group
,
393 .driver_module
= THIS_MODULE
,
396 /* Four channels apart from timestamp, scan mask = 0x0f */
397 static const unsigned long kxsd9_scan_masks
[] = { 0xf, 0 };
399 int kxsd9_common_probe(struct device
*dev
,
403 struct iio_dev
*indio_dev
;
404 struct kxsd9_state
*st
;
407 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
411 st
= iio_priv(indio_dev
);
415 indio_dev
->channels
= kxsd9_channels
;
416 indio_dev
->num_channels
= ARRAY_SIZE(kxsd9_channels
);
417 indio_dev
->name
= name
;
418 indio_dev
->dev
.parent
= dev
;
419 indio_dev
->info
= &kxsd9_info
;
420 indio_dev
->modes
= INDIO_DIRECT_MODE
;
421 indio_dev
->available_scan_masks
= kxsd9_scan_masks
;
423 /* Read the mounting matrix, if present */
424 ret
= of_iio_read_mount_matrix(dev
,
430 /* Fetch and turn on regulators */
431 st
->regs
[0].supply
= kxsd9_reg_vdd
;
432 st
->regs
[1].supply
= kxsd9_reg_iovdd
;
433 ret
= devm_regulator_bulk_get(dev
,
434 ARRAY_SIZE(st
->regs
),
437 dev_err(dev
, "Cannot get regulators\n");
440 /* Default scaling */
441 st
->scale
= KXSD9_CTRL_C_FS_2G
;
445 ret
= iio_triggered_buffer_setup(indio_dev
,
446 iio_pollfunc_store_time
,
447 kxsd9_trigger_handler
,
448 &kxsd9_buffer_setup_ops
);
450 dev_err(dev
, "triggered buffer setup failed\n");
454 ret
= iio_device_register(indio_dev
);
456 goto err_cleanup_buffer
;
458 dev_set_drvdata(dev
, indio_dev
);
460 /* Enable runtime PM */
461 pm_runtime_get_noresume(dev
);
462 pm_runtime_set_active(dev
);
463 pm_runtime_enable(dev
);
465 * Set autosuspend to two orders of magnitude larger than the
466 * start-up time. 20ms start-up time means 2000ms autosuspend,
469 pm_runtime_set_autosuspend_delay(dev
, 2000);
470 pm_runtime_use_autosuspend(dev
);
476 iio_triggered_buffer_cleanup(indio_dev
);
478 kxsd9_power_down(st
);
482 EXPORT_SYMBOL(kxsd9_common_probe
);
484 int kxsd9_common_remove(struct device
*dev
)
486 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
487 struct kxsd9_state
*st
= iio_priv(indio_dev
);
489 iio_triggered_buffer_cleanup(indio_dev
);
490 iio_device_unregister(indio_dev
);
491 pm_runtime_get_sync(dev
);
492 pm_runtime_put_noidle(dev
);
493 pm_runtime_disable(dev
);
494 kxsd9_power_down(st
);
498 EXPORT_SYMBOL(kxsd9_common_remove
);
501 static int kxsd9_runtime_suspend(struct device
*dev
)
503 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
504 struct kxsd9_state
*st
= iio_priv(indio_dev
);
506 return kxsd9_power_down(st
);
509 static int kxsd9_runtime_resume(struct device
*dev
)
511 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
512 struct kxsd9_state
*st
= iio_priv(indio_dev
);
514 return kxsd9_power_up(st
);
516 #endif /* CONFIG_PM */
518 const struct dev_pm_ops kxsd9_dev_pm_ops
= {
519 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
520 pm_runtime_force_resume
)
521 SET_RUNTIME_PM_OPS(kxsd9_runtime_suspend
,
522 kxsd9_runtime_resume
, NULL
)
524 EXPORT_SYMBOL(kxsd9_dev_pm_ops
);
526 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
527 MODULE_DESCRIPTION("Kionix KXSD9 driver");
528 MODULE_LICENSE("GPL v2");