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