Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * kxsd9.c simple support for the Kionix KXSD9 3D |
| 3 | * accelerometer. |
| 4 | * |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 5 | * Copyright (c) 2008-2009 Jonathan Cameron <jic23@kernel.org> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 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 |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/kernel.h> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 19 | #include <linux/sysfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 22 | #include <linux/regmap.h> |
Linus Walleij | 11adc2b | 2016-09-01 11:44:45 +0200 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 24 | #include <linux/delay.h> |
| 25 | #include <linux/regulator/consumer.h> |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/sysfs.h> |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 29 | #include <linux/iio/buffer.h> |
| 30 | #include <linux/iio/triggered_buffer.h> |
| 31 | #include <linux/iio/trigger_consumer.h> |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 32 | |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 33 | #include "kxsd9.h" |
| 34 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 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 | |
Linus Walleij | 11adc2b | 2016-09-01 11:44:45 +0200 | [diff] [blame] | 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)) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 58 | |
| 59 | #define KXSD9_REG_CTRL_B 0x0d |
Linus Walleij | 11adc2b | 2016-09-01 11:44:45 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 65 | #define KXSD9_REG_CTRL_A 0x0e |
| 66 | |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 67 | /** |
| 68 | * struct kxsd9_state - device related storage |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 69 | * @dev: pointer to the parent device |
Linus Walleij | dc6ac05 | 2016-09-01 11:44:42 +0200 | [diff] [blame] | 70 | * @map: regmap to the device |
Linus Walleij | 1288400 | 2016-09-01 11:44:49 +0200 | [diff] [blame] | 71 | * @orientation: mounting matrix, flipped axis etc |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 72 | * @regs: regulators for this device, VDD and IOVDD |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 73 | * @scale: the current scaling setting |
Linus Walleij | dc6ac05 | 2016-09-01 11:44:42 +0200 | [diff] [blame] | 74 | */ |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 75 | struct kxsd9_state { |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 76 | struct device *dev; |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 77 | struct regmap *map; |
Linus Walleij | 1288400 | 2016-09-01 11:44:49 +0200 | [diff] [blame] | 78 | struct iio_mount_matrix orientation; |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 79 | struct regulator_bulk_data regs[2]; |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 80 | u8 scale; |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 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 | |
Linus Walleij | 84e2f6f | 2016-09-01 11:44:43 +0200 | [diff] [blame] | 91 | #define KXSD9_ZERO_G_OFFSET -2048 |
| 92 | |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Regulator names |
| 95 | */ |
| 96 | static const char kxsd9_reg_vdd[] = "vdd"; |
| 97 | static const char kxsd9_reg_iovdd[] = "iovdd"; |
| 98 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 99 | static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 100 | { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 101 | int ret, i; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 102 | struct kxsd9_state *st = iio_priv(indio_dev); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 103 | bool foundit = false; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 104 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 105 | for (i = 0; i < 4; i++) |
| 106 | if (micro == kxsd9_micro_scales[i]) { |
| 107 | foundit = true; |
| 108 | break; |
| 109 | } |
| 110 | if (!foundit) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 111 | return -EINVAL; |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 112 | |
Linus Walleij | 11adc2b | 2016-09-01 11:44:45 +0200 | [diff] [blame] | 113 | ret = regmap_update_bits(st->map, |
| 114 | KXSD9_REG_CTRL_C, |
| 115 | KXSD9_CTRL_C_FS_MASK, |
| 116 | i); |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 117 | if (ret < 0) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 118 | goto error_ret; |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 119 | |
| 120 | /* Cached scale when the sensor is powered down */ |
| 121 | st->scale = i; |
| 122 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 123 | error_ret: |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 124 | return ret; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 125 | } |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 126 | |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 127 | static IIO_CONST_ATTR(accel_scale_available, |
| 128 | KXSD9_SCALE_2G " " |
| 129 | KXSD9_SCALE_4G " " |
| 130 | KXSD9_SCALE_6G " " |
| 131 | KXSD9_SCALE_8G); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 132 | |
| 133 | static struct attribute *kxsd9_attributes[] = { |
Jonathan Cameron | f3fb001 | 2010-05-04 14:42:58 +0100 | [diff] [blame] | 134 | &iio_const_attr_accel_scale_available.dev_attr.attr, |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 135 | NULL, |
| 136 | }; |
| 137 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 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; |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 145 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 146 | |
| 147 | pm_runtime_get_sync(st->dev); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 148 | |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 149 | if (mask == IIO_CHAN_INFO_SCALE) { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 150 | /* Check no integer component */ |
| 151 | if (val) |
| 152 | return -EINVAL; |
| 153 | ret = kxsd9_write_scale(indio_dev, val2); |
| 154 | } |
| 155 | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 156 | pm_runtime_mark_last_busy(st->dev); |
| 157 | pm_runtime_put_autosuspend(st->dev); |
| 158 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 159 | return ret; |
| 160 | } |
| 161 | |
| 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); |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 168 | unsigned int regval; |
Linus Walleij | 84e2f6f | 2016-09-01 11:44:43 +0200 | [diff] [blame] | 169 | __be16 raw_val; |
| 170 | u16 nval; |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 171 | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 172 | pm_runtime_get_sync(st->dev); |
| 173 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 174 | switch (mask) { |
Jonathan Cameron | 31313fc | 2012-04-15 17:41:17 +0100 | [diff] [blame] | 175 | case IIO_CHAN_INFO_RAW: |
Linus Walleij | 84e2f6f | 2016-09-01 11:44:43 +0200 | [diff] [blame] | 176 | ret = regmap_bulk_read(st->map, chan->address, &raw_val, |
| 177 | sizeof(raw_val)); |
| 178 | if (ret) |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 179 | goto error_ret; |
Linus Walleij | 84e2f6f | 2016-09-01 11:44:43 +0200 | [diff] [blame] | 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; |
Linus Walleij | 7ac61a0 | 2016-08-16 15:33:28 +0200 | [diff] [blame] | 189 | ret = IIO_VAL_INT; |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 190 | break; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 191 | case IIO_CHAN_INFO_SCALE: |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 192 | ret = regmap_read(st->map, |
| 193 | KXSD9_REG_CTRL_C, |
| 194 | ®val); |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 195 | if (ret < 0) |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 196 | goto error_ret; |
Linus Walleij | 307fe9d | 2016-09-01 11:44:35 +0200 | [diff] [blame] | 197 | *val = 0; |
Linus Walleij | 11adc2b | 2016-09-01 11:44:45 +0200 | [diff] [blame] | 198 | *val2 = kxsd9_micro_scales[regval & KXSD9_CTRL_C_FS_MASK]; |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 199 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 200 | break; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 201 | } |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 202 | |
| 203 | error_ret: |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 204 | pm_runtime_mark_last_busy(st->dev); |
| 205 | pm_runtime_put_autosuspend(st->dev); |
| 206 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 207 | return ret; |
| 208 | }; |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 209 | |
| 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 | int ret; |
| 216 | /* 4 * 16bit values AND timestamp */ |
| 217 | __be16 hw_values[8]; |
| 218 | |
| 219 | ret = regmap_bulk_read(st->map, |
| 220 | KXSD9_REG_X, |
| 221 | &hw_values, |
| 222 | 8); |
| 223 | if (ret) { |
| 224 | dev_err(st->dev, |
| 225 | "error reading data\n"); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | iio_push_to_buffers_with_timestamp(indio_dev, |
| 230 | hw_values, |
| 231 | iio_get_time_ns(indio_dev)); |
| 232 | iio_trigger_notify_done(indio_dev->trig); |
| 233 | |
| 234 | return IRQ_HANDLED; |
| 235 | } |
| 236 | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 237 | static int kxsd9_buffer_preenable(struct iio_dev *indio_dev) |
| 238 | { |
| 239 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 240 | |
| 241 | pm_runtime_get_sync(st->dev); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int kxsd9_buffer_postdisable(struct iio_dev *indio_dev) |
| 247 | { |
| 248 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 249 | |
| 250 | pm_runtime_mark_last_busy(st->dev); |
| 251 | pm_runtime_put_autosuspend(st->dev); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 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, |
| 261 | }; |
| 262 | |
Linus Walleij | 1288400 | 2016-09-01 11:44:49 +0200 | [diff] [blame] | 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) |
| 266 | { |
| 267 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 268 | |
| 269 | return &st->orientation; |
| 270 | } |
| 271 | |
| 272 | static const struct iio_chan_spec_ext_info kxsd9_ext_info[] = { |
| 273 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxsd9_get_mount_matrix), |
| 274 | { }, |
| 275 | }; |
| 276 | |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 277 | #define KXSD9_ACCEL_CHAN(axis, index) \ |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 278 | { \ |
| 279 | .type = IIO_ACCEL, \ |
| 280 | .modified = 1, \ |
| 281 | .channel2 = IIO_MOD_##axis, \ |
Jonathan Cameron | 2f6bb53 | 2013-02-27 19:03:36 +0000 | [diff] [blame] | 282 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
Linus Walleij | 84e2f6f | 2016-09-01 11:44:43 +0200 | [diff] [blame] | 283 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 284 | BIT(IIO_CHAN_INFO_OFFSET), \ |
Linus Walleij | 1288400 | 2016-09-01 11:44:49 +0200 | [diff] [blame] | 285 | .ext_info = kxsd9_ext_info, \ |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 286 | .address = KXSD9_REG_##axis, \ |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 287 | .scan_index = index, \ |
| 288 | .scan_type = { \ |
| 289 | .sign = 'u', \ |
| 290 | .realbits = 12, \ |
| 291 | .storagebits = 16, \ |
| 292 | .shift = 4, \ |
| 293 | .endianness = IIO_BE, \ |
| 294 | }, \ |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame] | 297 | static const struct iio_chan_spec kxsd9_channels[] = { |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 298 | KXSD9_ACCEL_CHAN(X, 0), |
| 299 | KXSD9_ACCEL_CHAN(Y, 1), |
| 300 | KXSD9_ACCEL_CHAN(Z, 2), |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 301 | { |
Jonathan Cameron | 6835cb6 | 2011-09-27 09:56:41 +0100 | [diff] [blame] | 302 | .type = IIO_VOLTAGE, |
Jonathan Cameron | 2f6bb53 | 2013-02-27 19:03:36 +0000 | [diff] [blame] | 303 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 304 | .indexed = 1, |
| 305 | .address = KXSD9_REG_AUX, |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 306 | .scan_index = 3, |
| 307 | .scan_type = { |
| 308 | .sign = 'u', |
| 309 | .realbits = 12, |
| 310 | .storagebits = 16, |
| 311 | .shift = 4, |
| 312 | .endianness = IIO_BE, |
| 313 | }, |
| 314 | }, |
| 315 | IIO_CHAN_SOFT_TIMESTAMP(4), |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 316 | }; |
| 317 | |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 318 | static const struct attribute_group kxsd9_attribute_group = { |
| 319 | .attrs = kxsd9_attributes, |
| 320 | }; |
| 321 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 322 | static int kxsd9_power_up(struct kxsd9_state *st) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 323 | { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 324 | int ret; |
| 325 | |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 326 | /* Enable the regulators */ |
| 327 | ret = regulator_bulk_enable(ARRAY_SIZE(st->regs), st->regs); |
| 328 | if (ret) { |
| 329 | dev_err(st->dev, "Cannot enable regulators\n"); |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | /* Power up */ |
| 334 | ret = regmap_write(st->map, |
| 335 | KXSD9_REG_CTRL_B, |
| 336 | KXSD9_CTRL_B_ENABLE); |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 337 | if (ret) |
| 338 | return ret; |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * Set 1000Hz LPF, 2g fullscale, motion wakeup threshold 1g, |
| 342 | * latched wakeup |
| 343 | */ |
| 344 | ret = regmap_write(st->map, |
| 345 | KXSD9_REG_CTRL_C, |
| 346 | KXSD9_CTRL_C_LP_1000HZ | |
| 347 | KXSD9_CTRL_C_MOT_LEV | |
| 348 | KXSD9_CTRL_C_MOT_LAT | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 349 | st->scale); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 350 | if (ret) |
| 351 | return ret; |
| 352 | |
| 353 | /* |
| 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. |
| 356 | */ |
| 357 | msleep(20); |
| 358 | |
| 359 | return 0; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 360 | }; |
| 361 | |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 362 | static int kxsd9_power_down(struct kxsd9_state *st) |
| 363 | { |
| 364 | int ret; |
| 365 | |
| 366 | /* |
| 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 |
| 370 | * regulators. |
| 371 | */ |
| 372 | ret = regmap_update_bits(st->map, |
| 373 | KXSD9_REG_CTRL_B, |
| 374 | KXSD9_CTRL_B_ENABLE, |
| 375 | 0); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | |
| 379 | /* Disable the regulators */ |
| 380 | ret = regulator_bulk_disable(ARRAY_SIZE(st->regs), st->regs); |
| 381 | if (ret) { |
| 382 | dev_err(st->dev, "Cannot disable regulators\n"); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 389 | static const struct iio_info kxsd9_info = { |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 390 | .read_raw = &kxsd9_read_raw, |
| 391 | .write_raw = &kxsd9_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 392 | .attrs = &kxsd9_attribute_group, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 393 | }; |
| 394 | |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 395 | /* Four channels apart from timestamp, scan mask = 0x0f */ |
| 396 | static const unsigned long kxsd9_scan_masks[] = { 0xf, 0 }; |
| 397 | |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 398 | int kxsd9_common_probe(struct device *dev, |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 399 | struct regmap *map, |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 400 | const char *name) |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 401 | { |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 402 | struct iio_dev *indio_dev; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 403 | struct kxsd9_state *st; |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 404 | int ret; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 405 | |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 406 | indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
Sachin Kamat | 4ee3093 | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 407 | if (!indio_dev) |
| 408 | return -ENOMEM; |
| 409 | |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 410 | st = iio_priv(indio_dev); |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 411 | st->dev = dev; |
Linus Walleij | 0d1fb2d | 2016-09-01 11:44:40 +0200 | [diff] [blame] | 412 | st->map = map; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 413 | |
Jonathan Cameron | d34dbee | 2011-08-12 17:47:52 +0100 | [diff] [blame] | 414 | indio_dev->channels = kxsd9_channels; |
| 415 | indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels); |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 416 | indio_dev->name = name; |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 417 | indio_dev->dev.parent = dev; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 418 | indio_dev->info = &kxsd9_info; |
| 419 | indio_dev->modes = INDIO_DIRECT_MODE; |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 420 | indio_dev->available_scan_masks = kxsd9_scan_masks; |
Jonathan Cameron | ed0c012 | 2011-06-27 13:17:56 +0100 | [diff] [blame] | 421 | |
Linus Walleij | 1288400 | 2016-09-01 11:44:49 +0200 | [diff] [blame] | 422 | /* Read the mounting matrix, if present */ |
| 423 | ret = of_iio_read_mount_matrix(dev, |
| 424 | "mount-matrix", |
| 425 | &st->orientation); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 429 | /* Fetch and turn on regulators */ |
| 430 | st->regs[0].supply = kxsd9_reg_vdd; |
| 431 | st->regs[1].supply = kxsd9_reg_iovdd; |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 432 | ret = devm_regulator_bulk_get(dev, |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 433 | ARRAY_SIZE(st->regs), |
| 434 | st->regs); |
| 435 | if (ret) { |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 436 | dev_err(dev, "Cannot get regulators\n"); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 437 | return ret; |
| 438 | } |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 439 | /* Default scaling */ |
| 440 | st->scale = KXSD9_CTRL_C_FS_2G; |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 441 | |
anish kumar | 3fd47d4 | 2011-06-27 13:08:00 +0100 | [diff] [blame] | 442 | kxsd9_power_up(st); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 443 | |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 444 | ret = iio_triggered_buffer_setup(indio_dev, |
| 445 | iio_pollfunc_store_time, |
| 446 | kxsd9_trigger_handler, |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 447 | &kxsd9_buffer_setup_ops); |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 448 | if (ret) { |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 449 | dev_err(dev, "triggered buffer setup failed\n"); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 450 | goto err_power_down; |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 453 | ret = iio_device_register(indio_dev); |
| 454 | if (ret) |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 455 | goto err_cleanup_buffer; |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 456 | |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 457 | dev_set_drvdata(dev, indio_dev); |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 458 | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 459 | /* Enable runtime PM */ |
| 460 | pm_runtime_get_noresume(dev); |
| 461 | pm_runtime_set_active(dev); |
| 462 | pm_runtime_enable(dev); |
| 463 | /* |
| 464 | * Set autosuspend to two orders of magnitude larger than the |
| 465 | * start-up time. 20ms start-up time means 2000ms autosuspend, |
| 466 | * i.e. 2 seconds. |
| 467 | */ |
| 468 | pm_runtime_set_autosuspend_delay(dev, 2000); |
| 469 | pm_runtime_use_autosuspend(dev); |
| 470 | pm_runtime_put(dev); |
| 471 | |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 472 | return 0; |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 473 | |
| 474 | err_cleanup_buffer: |
| 475 | iio_triggered_buffer_cleanup(indio_dev); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 476 | err_power_down: |
| 477 | kxsd9_power_down(st); |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 478 | |
| 479 | return ret; |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 480 | } |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 481 | EXPORT_SYMBOL(kxsd9_common_probe); |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 482 | |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 483 | int kxsd9_common_remove(struct device *dev) |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 484 | { |
Linus Walleij | 79383aa | 2016-09-01 11:44:47 +0200 | [diff] [blame] | 485 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 486 | struct kxsd9_state *st = iio_priv(indio_dev); |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 487 | |
Linus Walleij | 0427a10 | 2016-09-01 11:44:44 +0200 | [diff] [blame] | 488 | iio_triggered_buffer_cleanup(indio_dev); |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 489 | iio_device_unregister(indio_dev); |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 490 | pm_runtime_get_sync(dev); |
| 491 | pm_runtime_put_noidle(dev); |
| 492 | pm_runtime_disable(dev); |
Linus Walleij | 2bb4a02 | 2016-09-01 11:44:46 +0200 | [diff] [blame] | 493 | kxsd9_power_down(st); |
Linus Walleij | 154021a | 2016-09-01 11:44:37 +0200 | [diff] [blame] | 494 | |
Linus Walleij | 9f90797 | 2016-09-01 11:44:36 +0200 | [diff] [blame] | 495 | return 0; |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 496 | } |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 497 | EXPORT_SYMBOL(kxsd9_common_remove); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 498 | |
Linus Walleij | 9a9a369 | 2016-09-01 11:44:48 +0200 | [diff] [blame] | 499 | #ifdef CONFIG_PM |
| 500 | static int kxsd9_runtime_suspend(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_down(st); |
| 506 | } |
| 507 | |
| 508 | static int kxsd9_runtime_resume(struct device *dev) |
| 509 | { |
| 510 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 511 | struct kxsd9_state *st = iio_priv(indio_dev); |
| 512 | |
| 513 | return kxsd9_power_up(st); |
| 514 | } |
| 515 | #endif /* CONFIG_PM */ |
| 516 | |
| 517 | const struct dev_pm_ops kxsd9_dev_pm_ops = { |
| 518 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 519 | pm_runtime_force_resume) |
| 520 | SET_RUNTIME_PM_OPS(kxsd9_runtime_suspend, |
| 521 | kxsd9_runtime_resume, NULL) |
| 522 | }; |
| 523 | EXPORT_SYMBOL(kxsd9_dev_pm_ops); |
| 524 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 525 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Linus Walleij | bf96f6e | 2016-09-01 11:44:38 +0200 | [diff] [blame] | 526 | MODULE_DESCRIPTION("Kionix KXSD9 driver"); |
Jonathan Cameron | e435bc1 | 2009-08-18 18:06:23 +0100 | [diff] [blame] | 527 | MODULE_LICENSE("GPL v2"); |