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