1 // SPDX-License-Identifier: GPL-2.0-only
3 * kxsd9.c simple support for the Kionix KXSD9 3D
6 * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org>
8 * The i2c interface is very similar, so shouldn't be a problem once
9 * I have a suitable wire made up.
11 * TODO: Support the motion detector
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/sysfs.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/triggered_buffer.h>
28 #include <linux/iio/trigger_consumer.h>
32 #define KXSD9_REG_X 0x00
33 #define KXSD9_REG_Y 0x02
34 #define KXSD9_REG_Z 0x04
35 #define KXSD9_REG_AUX 0x06
36 #define KXSD9_REG_RESET 0x0a
37 #define KXSD9_REG_CTRL_C 0x0c
39 #define KXSD9_CTRL_C_FS_MASK 0x03
40 #define KXSD9_CTRL_C_FS_8G 0x00
41 #define KXSD9_CTRL_C_FS_6G 0x01
42 #define KXSD9_CTRL_C_FS_4G 0x02
43 #define KXSD9_CTRL_C_FS_2G 0x03
44 #define KXSD9_CTRL_C_MOT_LAT BIT(3)
45 #define KXSD9_CTRL_C_MOT_LEV BIT(4)
46 #define KXSD9_CTRL_C_LP_MASK 0xe0
47 #define KXSD9_CTRL_C_LP_NONE 0x00
48 #define KXSD9_CTRL_C_LP_2000HZC BIT(5)
49 #define KXSD9_CTRL_C_LP_2000HZB BIT(6)
50 #define KXSD9_CTRL_C_LP_2000HZA (BIT(5)|BIT(6))
51 #define KXSD9_CTRL_C_LP_1000HZ BIT(7)
52 #define KXSD9_CTRL_C_LP_500HZ (BIT(7)|BIT(5))
53 #define KXSD9_CTRL_C_LP_100HZ (BIT(7)|BIT(6))
54 #define KXSD9_CTRL_C_LP_50HZ (BIT(7)|BIT(6)|BIT(5))
56 #define KXSD9_REG_CTRL_B 0x0d
58 #define KXSD9_CTRL_B_CLK_HLD BIT(7)
59 #define KXSD9_CTRL_B_ENABLE BIT(6)
60 #define KXSD9_CTRL_B_ST BIT(5) /* Self-test */
62 #define KXSD9_REG_CTRL_A 0x0e
65 * struct kxsd9_state - device related storage
66 * @dev: pointer to the parent device
67 * @map: regmap to the device
68 * @orientation: mounting matrix, flipped axis etc
69 * @regs: regulators for this device, VDD and IOVDD
70 * @scale: the current scaling setting
75 struct iio_mount_matrix orientation
;
76 struct regulator_bulk_data regs
[2];
80 #define KXSD9_SCALE_2G "0.011978"
81 #define KXSD9_SCALE_4G "0.023927"
82 #define KXSD9_SCALE_6G "0.035934"
83 #define KXSD9_SCALE_8G "0.047853"
86 static const int kxsd9_micro_scales
[4] = { 47853, 35934, 23927, 11978 };
88 #define KXSD9_ZERO_G_OFFSET -2048
93 static const char kxsd9_reg_vdd
[] = "vdd";
94 static const char kxsd9_reg_iovdd
[] = "iovdd";
96 static int kxsd9_write_scale(struct iio_dev
*indio_dev
, int micro
)
99 struct kxsd9_state
*st
= iio_priv(indio_dev
);
100 bool foundit
= false;
102 for (i
= 0; i
< 4; i
++)
103 if (micro
== kxsd9_micro_scales
[i
]) {
110 ret
= regmap_update_bits(st
->map
,
112 KXSD9_CTRL_C_FS_MASK
,
117 /* Cached scale when the sensor is powered down */
124 static IIO_CONST_ATTR(accel_scale_available
,
130 static struct attribute
*kxsd9_attributes
[] = {
131 &iio_const_attr_accel_scale_available
.dev_attr
.attr
,
135 static int kxsd9_write_raw(struct iio_dev
*indio_dev
,
136 struct iio_chan_spec
const *chan
,
142 struct kxsd9_state
*st
= iio_priv(indio_dev
);
144 pm_runtime_get_sync(st
->dev
);
146 if (mask
== IIO_CHAN_INFO_SCALE
) {
147 /* Check no integer component */
150 ret
= kxsd9_write_scale(indio_dev
, val2
);
153 pm_runtime_mark_last_busy(st
->dev
);
154 pm_runtime_put_autosuspend(st
->dev
);
159 static int kxsd9_read_raw(struct iio_dev
*indio_dev
,
160 struct iio_chan_spec
const *chan
,
161 int *val
, int *val2
, long mask
)
164 struct kxsd9_state
*st
= iio_priv(indio_dev
);
169 pm_runtime_get_sync(st
->dev
);
172 case IIO_CHAN_INFO_RAW
:
173 ret
= regmap_bulk_read(st
->map
, chan
->address
, &raw_val
,
177 nval
= be16_to_cpu(raw_val
);
178 /* Only 12 bits are valid */
183 case IIO_CHAN_INFO_OFFSET
:
184 /* This has a bias of -2048 */
185 *val
= KXSD9_ZERO_G_OFFSET
;
188 case IIO_CHAN_INFO_SCALE
:
189 ret
= regmap_read(st
->map
,
195 *val2
= kxsd9_micro_scales
[regval
& KXSD9_CTRL_C_FS_MASK
];
196 ret
= IIO_VAL_INT_PLUS_MICRO
;
201 pm_runtime_mark_last_busy(st
->dev
);
202 pm_runtime_put_autosuspend(st
->dev
);
207 static irqreturn_t
kxsd9_trigger_handler(int irq
, void *p
)
209 const struct iio_poll_func
*pf
= p
;
210 struct iio_dev
*indio_dev
= pf
->indio_dev
;
211 struct kxsd9_state
*st
= iio_priv(indio_dev
);
213 * Ensure correct positioning and alignment of timestamp.
214 * No need to zero initialize as all elements written.
222 ret
= regmap_bulk_read(st
->map
,
225 sizeof(hw_values
.chan
));
227 dev_err(st
->dev
, "error reading data: %d\n", ret
);
231 iio_push_to_buffers_with_timestamp(indio_dev
,
233 iio_get_time_ns(indio_dev
));
235 iio_trigger_notify_done(indio_dev
->trig
);
240 static int kxsd9_buffer_preenable(struct iio_dev
*indio_dev
)
242 struct kxsd9_state
*st
= iio_priv(indio_dev
);
244 pm_runtime_get_sync(st
->dev
);
249 static int kxsd9_buffer_postdisable(struct iio_dev
*indio_dev
)
251 struct kxsd9_state
*st
= iio_priv(indio_dev
);
253 pm_runtime_mark_last_busy(st
->dev
);
254 pm_runtime_put_autosuspend(st
->dev
);
259 static const struct iio_buffer_setup_ops kxsd9_buffer_setup_ops
= {
260 .preenable
= kxsd9_buffer_preenable
,
261 .postdisable
= kxsd9_buffer_postdisable
,
264 static const struct iio_mount_matrix
*
265 kxsd9_get_mount_matrix(const struct iio_dev
*indio_dev
,
266 const struct iio_chan_spec
*chan
)
268 struct kxsd9_state
*st
= iio_priv(indio_dev
);
270 return &st
->orientation
;
273 static const struct iio_chan_spec_ext_info kxsd9_ext_info
[] = {
274 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, kxsd9_get_mount_matrix
),
278 #define KXSD9_ACCEL_CHAN(axis, index) \
282 .channel2 = IIO_MOD_##axis, \
283 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
284 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
285 BIT(IIO_CHAN_INFO_OFFSET), \
286 .ext_info = kxsd9_ext_info, \
287 .address = KXSD9_REG_##axis, \
288 .scan_index = index, \
294 .endianness = IIO_BE, \
298 static const struct iio_chan_spec kxsd9_channels
[] = {
299 KXSD9_ACCEL_CHAN(X
, 0),
300 KXSD9_ACCEL_CHAN(Y
, 1),
301 KXSD9_ACCEL_CHAN(Z
, 2),
304 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
306 .address
= KXSD9_REG_AUX
,
313 .endianness
= IIO_BE
,
316 IIO_CHAN_SOFT_TIMESTAMP(4),
319 static const struct attribute_group kxsd9_attribute_group
= {
320 .attrs
= kxsd9_attributes
,
323 static int kxsd9_power_up(struct kxsd9_state
*st
)
327 /* Enable the regulators */
328 ret
= regulator_bulk_enable(ARRAY_SIZE(st
->regs
), st
->regs
);
330 dev_err(st
->dev
, "Cannot enable regulators\n");
335 ret
= regmap_write(st
->map
,
337 KXSD9_CTRL_B_ENABLE
);
342 * Set 1000Hz LPF, 2g fullscale, motion wakeup threshold 1g,
345 ret
= regmap_write(st
->map
,
347 KXSD9_CTRL_C_LP_1000HZ
|
348 KXSD9_CTRL_C_MOT_LEV
|
349 KXSD9_CTRL_C_MOT_LAT
|
355 * Power-up time depends on the LPF setting, but typ 15.9 ms, let's
356 * set 20 ms to allow for some slack.
363 static int kxsd9_power_down(struct kxsd9_state
*st
)
368 * Set into low power mode - since there may be more users of the
369 * regulators this is the first step of the power saving: it will
370 * make sure we conserve power even if there are others users on the
373 ret
= regmap_clear_bits(st
->map
, KXSD9_REG_CTRL_B
, KXSD9_CTRL_B_ENABLE
);
377 /* Disable the regulators */
378 ret
= regulator_bulk_disable(ARRAY_SIZE(st
->regs
), st
->regs
);
380 dev_err(st
->dev
, "Cannot disable regulators\n");
387 static const struct iio_info kxsd9_info
= {
388 .read_raw
= &kxsd9_read_raw
,
389 .write_raw
= &kxsd9_write_raw
,
390 .attrs
= &kxsd9_attribute_group
,
393 /* Four channels apart from timestamp, scan mask = 0x0f */
394 static const unsigned long kxsd9_scan_masks
[] = { 0xf, 0 };
396 int kxsd9_common_probe(struct device
*dev
,
400 struct iio_dev
*indio_dev
;
401 struct kxsd9_state
*st
;
404 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
408 st
= iio_priv(indio_dev
);
412 indio_dev
->channels
= kxsd9_channels
;
413 indio_dev
->num_channels
= ARRAY_SIZE(kxsd9_channels
);
414 indio_dev
->name
= name
;
415 indio_dev
->info
= &kxsd9_info
;
416 indio_dev
->modes
= INDIO_DIRECT_MODE
;
417 indio_dev
->available_scan_masks
= kxsd9_scan_masks
;
419 /* Read the mounting matrix, if present */
420 ret
= iio_read_mount_matrix(dev
, &st
->orientation
);
424 /* Fetch and turn on regulators */
425 st
->regs
[0].supply
= kxsd9_reg_vdd
;
426 st
->regs
[1].supply
= kxsd9_reg_iovdd
;
427 ret
= devm_regulator_bulk_get(dev
,
428 ARRAY_SIZE(st
->regs
),
431 dev_err(dev
, "Cannot get regulators\n");
434 /* Default scaling */
435 st
->scale
= KXSD9_CTRL_C_FS_2G
;
439 ret
= iio_triggered_buffer_setup(indio_dev
,
440 iio_pollfunc_store_time
,
441 kxsd9_trigger_handler
,
442 &kxsd9_buffer_setup_ops
);
444 dev_err(dev
, "triggered buffer setup failed\n");
448 ret
= iio_device_register(indio_dev
);
450 goto err_cleanup_buffer
;
452 dev_set_drvdata(dev
, indio_dev
);
454 /* Enable runtime PM */
455 pm_runtime_get_noresume(dev
);
456 pm_runtime_set_active(dev
);
457 pm_runtime_enable(dev
);
459 * Set autosuspend to two orders of magnitude larger than the
460 * start-up time. 20ms start-up time means 2000ms autosuspend,
463 pm_runtime_set_autosuspend_delay(dev
, 2000);
464 pm_runtime_use_autosuspend(dev
);
470 iio_triggered_buffer_cleanup(indio_dev
);
472 kxsd9_power_down(st
);
476 EXPORT_SYMBOL_NS(kxsd9_common_probe
, IIO_KXSD9
);
478 void kxsd9_common_remove(struct device
*dev
)
480 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
481 struct kxsd9_state
*st
= iio_priv(indio_dev
);
483 iio_triggered_buffer_cleanup(indio_dev
);
484 iio_device_unregister(indio_dev
);
485 pm_runtime_get_sync(dev
);
486 pm_runtime_put_noidle(dev
);
487 pm_runtime_disable(dev
);
488 kxsd9_power_down(st
);
490 EXPORT_SYMBOL_NS(kxsd9_common_remove
, IIO_KXSD9
);
492 static int kxsd9_runtime_suspend(struct device
*dev
)
494 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
495 struct kxsd9_state
*st
= iio_priv(indio_dev
);
497 return kxsd9_power_down(st
);
500 static int kxsd9_runtime_resume(struct device
*dev
)
502 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
503 struct kxsd9_state
*st
= iio_priv(indio_dev
);
505 return kxsd9_power_up(st
);
508 EXPORT_NS_RUNTIME_DEV_PM_OPS(kxsd9_dev_pm_ops
, kxsd9_runtime_suspend
,
509 kxsd9_runtime_resume
, NULL
, IIO_KXSD9
);
511 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
512 MODULE_DESCRIPTION("Kionix KXSD9 driver");
513 MODULE_LICENSE("GPL v2");