| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Murata ZPA2326 pressure and temperature sensor IIO driver |
| 4 |
* |
| 5 |
* Copyright (c) 2016 Parrot S.A. |
| 6 |
* |
| 7 |
* Author: Gregor Boirie <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* DOC: ZPA2326 theory of operations |
| 12 |
* |
| 13 |
* This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO |
| 14 |
* modes. |
| 15 |
* A internal hardware trigger is also implemented to dispatch registered IIO |
| 16 |
* trigger consumers upon "sample ready" interrupts. |
| 17 |
* |
| 18 |
* ZPA2326 hardware supports 2 sampling mode: one shot and continuous. |
| 19 |
* |
| 20 |
* A complete one shot sampling cycle gets device out of low power mode, |
| 21 |
* performs pressure and temperature measurements, then automatically switches |
| 22 |
* back to low power mode. It is meant for on demand sampling with optimal power |
| 23 |
* saving at the cost of lower sampling rate and higher software overhead. |
| 24 |
* This is a natural candidate for IIO read_raw hook implementation |
| 25 |
* (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to |
| 26 |
* ensure explicit synchronization with external trigger events |
| 27 |
* (%INDIO_BUFFER_TRIGGERED). |
| 28 |
* |
| 29 |
* The continuous mode works according to a periodic hardware measurement |
| 30 |
* process continuously pushing samples into an internal hardware FIFO (for |
| 31 |
* pressure samples only). Measurement cycle completion may be signaled by a |
| 32 |
* "sample ready" interrupt. |
| 33 |
* Typical software sequence of operations : |
| 34 |
* - get device out of low power mode, |
| 35 |
* - setup hardware sampling period, |
| 36 |
* - at end of period, upon data ready interrupt: pop pressure samples out of |
| 37 |
* hardware FIFO and fetch temperature sample |
| 38 |
* - when no longer needed, stop sampling process by putting device into |
| 39 |
* low power mode. |
| 40 |
* This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree |
| 41 |
* declares a valid interrupt line. In this case, the internal hardware trigger |
| 42 |
* drives acquisition. |
| 43 |
* |
| 44 |
* Note that hardware sampling frequency is taken into account only when |
| 45 |
* internal hardware trigger is attached as the highest sampling rate seems to |
| 46 |
* be the most energy efficient. |
| 47 |
* |
| 48 |
* TODO: |
| 49 |
* preset pressure threshold crossing / IIO events ; |
| 50 |
* differential pressure sampling ; |
| 51 |
* hardware samples averaging. |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <linux/module.h> |
| 55 |
#include <linux/kernel.h> |
| 56 |
#include <linux/delay.h> |
| 57 |
#include <linux/interrupt.h> |
| 58 |
#include <linux/regulator/consumer.h> |
| 59 |
#include <linux/pm_runtime.h> |
| 60 |
#include <linux/regmap.h> |
| 61 |
#include <linux/iio/iio.h> |
| 62 |
#include <linux/iio/sysfs.h> |
| 63 |
#include <linux/iio/buffer.h> |
| 64 |
#include <linux/iio/trigger.h> |
| 65 |
#include <linux/iio/trigger_consumer.h> |
| 66 |
#include <linux/iio/triggered_buffer.h> |
| 67 |
#include <linux/unaligned.h> |
| 68 |
#include "zpa2326.h" |
| 69 |
|
| 70 |
/* 200 ms should be enough for the longest conversion time in one-shot mode. */ |
| 71 |
#define ZPA2326_CONVERSION_JIFFIES (HZ / 5) |
| 72 |
|
| 73 |
/* There should be a 1 ms delay (Tpup) after getting out of reset. */ |
| 74 |
#define ZPA2326_TPUP_USEC_MIN (1000) |
| 75 |
#define ZPA2326_TPUP_USEC_MAX (2000) |
| 76 |
|
| 77 |
/** |
| 78 |
* struct zpa2326_frequency - Hardware sampling frequency descriptor |
| 79 |
* @hz : Frequency in Hertz. |
| 80 |
* @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG. |
| 81 |
*/ |
| 82 |
struct zpa2326_frequency { |
| 83 |
int hz; |
| 84 |
u16 odr; |
| 85 |
}; |
| 86 |
|
| 87 |
/* |
| 88 |
* Keep these in strict ascending order: last array entry is expected to |
| 89 |
* correspond to the highest sampling frequency. |
| 90 |
*/ |
| 91 |
static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = { |
| 92 |
{ .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT }, |
| 93 |
{ .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT }, |
| 94 |
{ .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT }, |
| 95 |
{ .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT }, |
| 96 |
}; |
| 97 |
|
| 98 |
/* Return the highest hardware sampling frequency available. */ |
| 99 |
static const struct zpa2326_frequency *zpa2326_highest_frequency(void) |
| 100 |
{ |
| 101 |
return &zpa2326_sampling_frequencies[ |
| 102 |
ARRAY_SIZE(zpa2326_sampling_frequencies) - 1]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* struct zpa2326_private - Per-device internal private state |
| 107 |
* @timestamp: Buffered samples ready datum. |
| 108 |
* @regmap: Underlying I2C / SPI bus adapter used to abstract slave register |
| 109 |
* accesses. |
| 110 |
* @result: Allows sampling logic to get completion status of operations |
| 111 |
* that interrupt handlers perform asynchronously. |
| 112 |
* @data_ready: Interrupt handler uses this to wake user context up at sampling |
| 113 |
* operation completion. |
| 114 |
* @trigger: Optional hardware / interrupt driven trigger used to notify |
| 115 |
* external devices a new sample is ready. |
| 116 |
* @waken: Flag indicating whether or not device has just been powered on. |
| 117 |
* @irq: Optional interrupt line: negative or zero if not declared into |
| 118 |
* DT, in which case sampling logic keeps polling status register |
| 119 |
* to detect completion. |
| 120 |
* @frequency: Current hardware sampling frequency. |
| 121 |
* @vref: Power / voltage reference. |
| 122 |
* @vdd: Power supply. |
| 123 |
*/ |
| 124 |
struct zpa2326_private { |
| 125 |
s64 timestamp; |
| 126 |
struct regmap *regmap; |
| 127 |
int result; |
| 128 |
struct completion data_ready; |
| 129 |
struct iio_trigger *trigger; |
| 130 |
bool waken; |
| 131 |
int irq; |
| 132 |
const struct zpa2326_frequency *frequency; |
| 133 |
struct regulator *vref; |
| 134 |
struct regulator *vdd; |
| 135 |
}; |
| 136 |
|
| 137 |
#define zpa2326_err(idev, fmt, ...) \ |
| 138 |
dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__) |
| 139 |
|
| 140 |
#define zpa2326_warn(idev, fmt, ...) \ |
| 141 |
dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__) |
| 142 |
|
| 143 |
#define zpa2326_dbg(idev, fmt, ...) \ |
| 144 |
dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__) |
| 145 |
|
| 146 |
bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg) |
| 147 |
{ |
| 148 |
switch (reg) { |
| 149 |
case ZPA2326_REF_P_XL_REG: |
| 150 |
case ZPA2326_REF_P_L_REG: |
| 151 |
case ZPA2326_REF_P_H_REG: |
| 152 |
case ZPA2326_RES_CONF_REG: |
| 153 |
case ZPA2326_CTRL_REG0_REG: |
| 154 |
case ZPA2326_CTRL_REG1_REG: |
| 155 |
case ZPA2326_CTRL_REG2_REG: |
| 156 |
case ZPA2326_CTRL_REG3_REG: |
| 157 |
case ZPA2326_THS_P_LOW_REG: |
| 158 |
case ZPA2326_THS_P_HIGH_REG: |
| 159 |
return true; |
| 160 |
|
| 161 |
default: |
| 162 |
return false; |
| 163 |
} |
| 164 |
} |
| 165 |
EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_writeable, "IIO_ZPA2326"); |
| 166 |
|
| 167 |
bool zpa2326_isreg_readable(struct device *dev, unsigned int reg) |
| 168 |
{ |
| 169 |
switch (reg) { |
| 170 |
case ZPA2326_REF_P_XL_REG: |
| 171 |
case ZPA2326_REF_P_L_REG: |
| 172 |
case ZPA2326_REF_P_H_REG: |
| 173 |
case ZPA2326_DEVICE_ID_REG: |
| 174 |
case ZPA2326_RES_CONF_REG: |
| 175 |
case ZPA2326_CTRL_REG0_REG: |
| 176 |
case ZPA2326_CTRL_REG1_REG: |
| 177 |
case ZPA2326_CTRL_REG2_REG: |
| 178 |
case ZPA2326_CTRL_REG3_REG: |
| 179 |
case ZPA2326_INT_SOURCE_REG: |
| 180 |
case ZPA2326_THS_P_LOW_REG: |
| 181 |
case ZPA2326_THS_P_HIGH_REG: |
| 182 |
case ZPA2326_STATUS_REG: |
| 183 |
case ZPA2326_PRESS_OUT_XL_REG: |
| 184 |
case ZPA2326_PRESS_OUT_L_REG: |
| 185 |
case ZPA2326_PRESS_OUT_H_REG: |
| 186 |
case ZPA2326_TEMP_OUT_L_REG: |
| 187 |
case ZPA2326_TEMP_OUT_H_REG: |
| 188 |
return true; |
| 189 |
|
| 190 |
default: |
| 191 |
return false; |
| 192 |
} |
| 193 |
} |
| 194 |
EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_readable, "IIO_ZPA2326"); |
| 195 |
|
| 196 |
bool zpa2326_isreg_precious(struct device *dev, unsigned int reg) |
| 197 |
{ |
| 198 |
switch (reg) { |
| 199 |
case ZPA2326_INT_SOURCE_REG: |
| 200 |
case ZPA2326_PRESS_OUT_H_REG: |
| 201 |
return true; |
| 202 |
|
| 203 |
default: |
| 204 |
return false; |
| 205 |
} |
| 206 |
} |
| 207 |
EXPORT_SYMBOL_NS_GPL(zpa2326_isreg_precious, "IIO_ZPA2326"); |
| 208 |
|
| 209 |
/** |
| 210 |
* zpa2326_enable_device() - Enable device, i.e. get out of low power mode. |
| 211 |
* @indio_dev: The IIO device associated with the hardware to enable. |
| 212 |
* |
| 213 |
* Required to access complete register space and to perform any sampling |
| 214 |
* or control operations. |
| 215 |
* |
| 216 |
* Return: Zero when successful, a negative error code otherwise. |
| 217 |
*/ |
| 218 |
static int zpa2326_enable_device(const struct iio_dev *indio_dev) |
| 219 |
{ |
| 220 |
int err; |
| 221 |
|
| 222 |
err = regmap_write(((struct zpa2326_private *) |
| 223 |
iio_priv(indio_dev))->regmap, |
| 224 |
ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE); |
| 225 |
if (err) { |
| 226 |
zpa2326_err(indio_dev, "failed to enable device (%d)", err); |
| 227 |
return err; |
| 228 |
} |
| 229 |
|
| 230 |
zpa2326_dbg(indio_dev, "enabled"); |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* zpa2326_sleep() - Disable device, i.e. switch to low power mode. |
| 237 |
* @indio_dev: The IIO device associated with the hardware to disable. |
| 238 |
* |
| 239 |
* Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be |
| 240 |
* accessed once device is in the disabled state. |
| 241 |
* |
| 242 |
* Return: Zero when successful, a negative error code otherwise. |
| 243 |
*/ |
| 244 |
static int zpa2326_sleep(const struct iio_dev *indio_dev) |
| 245 |
{ |
| 246 |
int err; |
| 247 |
|
| 248 |
err = regmap_write(((struct zpa2326_private *) |
| 249 |
iio_priv(indio_dev))->regmap, |
| 250 |
ZPA2326_CTRL_REG0_REG, 0); |
| 251 |
if (err) { |
| 252 |
zpa2326_err(indio_dev, "failed to sleep (%d)", err); |
| 253 |
return err; |
| 254 |
} |
| 255 |
|
| 256 |
zpa2326_dbg(indio_dev, "sleeping"); |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* zpa2326_reset_device() - Reset device to default hardware state. |
| 263 |
* @indio_dev: The IIO device associated with the hardware to reset. |
| 264 |
* |
| 265 |
* Disable sampling and empty hardware FIFO. |
| 266 |
* Device must be enabled before reset, i.e. not in low power mode. |
| 267 |
* |
| 268 |
* Return: Zero when successful, a negative error code otherwise. |
| 269 |
*/ |
| 270 |
static int zpa2326_reset_device(const struct iio_dev *indio_dev) |
| 271 |
{ |
| 272 |
int err; |
| 273 |
|
| 274 |
err = regmap_write(((struct zpa2326_private *) |
| 275 |
iio_priv(indio_dev))->regmap, |
| 276 |
ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET); |
| 277 |
if (err) { |
| 278 |
zpa2326_err(indio_dev, "failed to reset device (%d)", err); |
| 279 |
return err; |
| 280 |
} |
| 281 |
|
| 282 |
usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX); |
| 283 |
|
| 284 |
zpa2326_dbg(indio_dev, "reset"); |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot |
| 291 |
* mode. |
| 292 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 293 |
* |
| 294 |
* Device must have been previously enabled and configured for one shot mode. |
| 295 |
* Device will be switched back to low power mode at end of cycle. |
| 296 |
* |
| 297 |
* Return: Zero when successful, a negative error code otherwise. |
| 298 |
*/ |
| 299 |
static int zpa2326_start_oneshot(const struct iio_dev *indio_dev) |
| 300 |
{ |
| 301 |
int err; |
| 302 |
|
| 303 |
err = regmap_write(((struct zpa2326_private *) |
| 304 |
iio_priv(indio_dev))->regmap, |
| 305 |
ZPA2326_CTRL_REG0_REG, |
| 306 |
ZPA2326_CTRL_REG0_ENABLE | |
| 307 |
ZPA2326_CTRL_REG0_ONE_SHOT); |
| 308 |
if (err) { |
| 309 |
zpa2326_err(indio_dev, "failed to start one shot cycle (%d)", |
| 310 |
err); |
| 311 |
return err; |
| 312 |
} |
| 313 |
|
| 314 |
zpa2326_dbg(indio_dev, "one shot cycle started"); |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* zpa2326_power_on() - Power on device to allow subsequent configuration. |
| 321 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 322 |
* @private: Internal private state related to @indio_dev. |
| 323 |
* |
| 324 |
* Sampling will be disabled, preventing strange things from happening in our |
| 325 |
* back. Hardware FIFO content will be cleared. |
| 326 |
* When successful, device will be left in the enabled state to allow further |
| 327 |
* configuration. |
| 328 |
* |
| 329 |
* Return: Zero when successful, a negative error code otherwise. |
| 330 |
*/ |
| 331 |
static int zpa2326_power_on(const struct iio_dev *indio_dev, |
| 332 |
const struct zpa2326_private *private) |
| 333 |
{ |
| 334 |
int err; |
| 335 |
|
| 336 |
err = regulator_enable(private->vref); |
| 337 |
if (err) |
| 338 |
return err; |
| 339 |
|
| 340 |
err = regulator_enable(private->vdd); |
| 341 |
if (err) |
| 342 |
goto vref; |
| 343 |
|
| 344 |
zpa2326_dbg(indio_dev, "powered on"); |
| 345 |
|
| 346 |
err = zpa2326_enable_device(indio_dev); |
| 347 |
if (err) |
| 348 |
goto vdd; |
| 349 |
|
| 350 |
err = zpa2326_reset_device(indio_dev); |
| 351 |
if (err) |
| 352 |
goto sleep; |
| 353 |
|
| 354 |
return 0; |
| 355 |
|
| 356 |
sleep: |
| 357 |
zpa2326_sleep(indio_dev); |
| 358 |
vdd: |
| 359 |
regulator_disable(private->vdd); |
| 360 |
vref: |
| 361 |
regulator_disable(private->vref); |
| 362 |
|
| 363 |
zpa2326_dbg(indio_dev, "powered off"); |
| 364 |
|
| 365 |
return err; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* zpa2326_power_off() - Power off device, i.e. disable attached power |
| 370 |
* regulators. |
| 371 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 372 |
* @private: Internal private state related to @indio_dev. |
| 373 |
* |
| 374 |
* Return: Zero when successful, a negative error code otherwise. |
| 375 |
*/ |
| 376 |
static void zpa2326_power_off(const struct iio_dev *indio_dev, |
| 377 |
const struct zpa2326_private *private) |
| 378 |
{ |
| 379 |
regulator_disable(private->vdd); |
| 380 |
regulator_disable(private->vref); |
| 381 |
|
| 382 |
zpa2326_dbg(indio_dev, "powered off"); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* zpa2326_config_oneshot() - Setup device for one shot / on demand mode. |
| 387 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 388 |
* @irq: Optional interrupt line the hardware uses to notify new data |
| 389 |
* samples are ready. Negative or zero values indicate no interrupts |
| 390 |
* are available, meaning polling is required. |
| 391 |
* |
| 392 |
* Output Data Rate is configured for the highest possible rate so that |
| 393 |
* conversion time and power consumption are reduced to a minimum. |
| 394 |
* Note that hardware internal averaging machinery (not implemented in this |
| 395 |
* driver) is not applicable in this mode. |
| 396 |
* |
| 397 |
* Device must have been previously enabled before calling |
| 398 |
* zpa2326_config_oneshot(). |
| 399 |
* |
| 400 |
* Return: Zero when successful, a negative error code otherwise. |
| 401 |
*/ |
| 402 |
static int zpa2326_config_oneshot(const struct iio_dev *indio_dev, |
| 403 |
int irq) |
| 404 |
{ |
| 405 |
struct regmap *regs = ((struct zpa2326_private *) |
| 406 |
iio_priv(indio_dev))->regmap; |
| 407 |
const struct zpa2326_frequency *freq = zpa2326_highest_frequency(); |
| 408 |
int err; |
| 409 |
|
| 410 |
/* Setup highest available Output Data Rate for one shot mode. */ |
| 411 |
err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr); |
| 412 |
if (err) |
| 413 |
return err; |
| 414 |
|
| 415 |
if (irq > 0) { |
| 416 |
/* Request interrupt when new sample is available. */ |
| 417 |
err = regmap_write(regs, ZPA2326_CTRL_REG1_REG, |
| 418 |
(u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY); |
| 419 |
|
| 420 |
if (err) { |
| 421 |
dev_err(indio_dev->dev.parent, |
| 422 |
"failed to setup one shot mode (%d)", err); |
| 423 |
return err; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz); |
| 428 |
|
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO. |
| 434 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 435 |
* @min_count: Number of samples present within hardware FIFO. |
| 436 |
* |
| 437 |
* @min_count argument is a hint corresponding to the known minimum number of |
| 438 |
* samples currently living in the FIFO. This allows to reduce the number of bus |
| 439 |
* accesses by skipping status register read operation as long as we know for |
| 440 |
* sure there are still entries left. |
| 441 |
* |
| 442 |
* Return: Zero when successful, a negative error code otherwise. |
| 443 |
*/ |
| 444 |
static int zpa2326_clear_fifo(const struct iio_dev *indio_dev, |
| 445 |
unsigned int min_count) |
| 446 |
{ |
| 447 |
struct regmap *regs = ((struct zpa2326_private *) |
| 448 |
iio_priv(indio_dev))->regmap; |
| 449 |
int err; |
| 450 |
unsigned int val; |
| 451 |
|
| 452 |
if (!min_count) { |
| 453 |
/* |
| 454 |
* No hint: read status register to determine whether FIFO is |
| 455 |
* empty or not. |
| 456 |
*/ |
| 457 |
err = regmap_read(regs, ZPA2326_STATUS_REG, &val); |
| 458 |
|
| 459 |
if (err < 0) |
| 460 |
goto err; |
| 461 |
|
| 462 |
if (val & ZPA2326_STATUS_FIFO_E) |
| 463 |
/* Fifo is empty: nothing to trash. */ |
| 464 |
return 0; |
| 465 |
} |
| 466 |
|
| 467 |
/* Clear FIFO. */ |
| 468 |
do { |
| 469 |
/* |
| 470 |
* A single fetch from pressure MSB register is enough to pop |
| 471 |
* values out of FIFO. |
| 472 |
*/ |
| 473 |
err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val); |
| 474 |
if (err < 0) |
| 475 |
goto err; |
| 476 |
|
| 477 |
if (min_count) { |
| 478 |
/* |
| 479 |
* We know for sure there are at least min_count entries |
| 480 |
* left in FIFO. Skip status register read. |
| 481 |
*/ |
| 482 |
min_count--; |
| 483 |
continue; |
| 484 |
} |
| 485 |
|
| 486 |
err = regmap_read(regs, ZPA2326_STATUS_REG, &val); |
| 487 |
if (err < 0) |
| 488 |
goto err; |
| 489 |
|
| 490 |
} while (!(val & ZPA2326_STATUS_FIFO_E)); |
| 491 |
|
| 492 |
zpa2326_dbg(indio_dev, "FIFO cleared"); |
| 493 |
|
| 494 |
return 0; |
| 495 |
|
| 496 |
err: |
| 497 |
zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err); |
| 498 |
|
| 499 |
return err; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from |
| 504 |
* hardware FIFO. |
| 505 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 506 |
* @pressure: Sampled pressure output. |
| 507 |
* |
| 508 |
* Note that ZPA2326 hardware FIFO stores pressure samples only. |
| 509 |
* |
| 510 |
* Return: Zero when successful, a negative error code otherwise. |
| 511 |
*/ |
| 512 |
static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev, |
| 513 |
u32 *pressure) |
| 514 |
{ |
| 515 |
struct regmap *regs = ((struct zpa2326_private *) |
| 516 |
iio_priv(indio_dev))->regmap; |
| 517 |
unsigned int val; |
| 518 |
int err; |
| 519 |
int cleared = -1; |
| 520 |
|
| 521 |
err = regmap_read(regs, ZPA2326_STATUS_REG, &val); |
| 522 |
if (err < 0) |
| 523 |
return err; |
| 524 |
|
| 525 |
*pressure = 0; |
| 526 |
|
| 527 |
if (val & ZPA2326_STATUS_P_OR) { |
| 528 |
/* |
| 529 |
* Fifo overrun : first sample dequeued from FIFO is the |
| 530 |
* newest. |
| 531 |
*/ |
| 532 |
zpa2326_warn(indio_dev, "FIFO overflow"); |
| 533 |
|
| 534 |
err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure, |
| 535 |
3); |
| 536 |
if (err) |
| 537 |
return err; |
| 538 |
|
| 539 |
#define ZPA2326_FIFO_DEPTH (16U) |
| 540 |
/* Hardware FIFO may hold no more than 16 pressure samples. */ |
| 541 |
return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1); |
| 542 |
} |
| 543 |
|
| 544 |
/* |
| 545 |
* Fifo has not overflown : retrieve newest sample. We need to pop |
| 546 |
* values out until FIFO is empty : last fetched pressure is the newest. |
| 547 |
* In nominal cases, we should find a single queued sample only. |
| 548 |
*/ |
| 549 |
do { |
| 550 |
err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure, |
| 551 |
3); |
| 552 |
if (err) |
| 553 |
return err; |
| 554 |
|
| 555 |
err = regmap_read(regs, ZPA2326_STATUS_REG, &val); |
| 556 |
if (err < 0) |
| 557 |
return err; |
| 558 |
|
| 559 |
cleared++; |
| 560 |
} while (!(val & ZPA2326_STATUS_FIFO_E)); |
| 561 |
|
| 562 |
if (cleared) |
| 563 |
/* |
| 564 |
* Samples were pushed by hardware during previous rounds but we |
| 565 |
* didn't consume them fast enough: inform user. |
| 566 |
*/ |
| 567 |
zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared); |
| 568 |
|
| 569 |
return 0; |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer. |
| 574 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 575 |
* @private: Internal private state related to @indio_dev. |
| 576 |
* |
| 577 |
* Return: Zero when successful, a negative error code otherwise. |
| 578 |
*/ |
| 579 |
static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev, |
| 580 |
const struct zpa2326_private *private) |
| 581 |
{ |
| 582 |
struct { |
| 583 |
u32 pressure; |
| 584 |
u16 temperature; |
| 585 |
aligned_s64 timestamp; |
| 586 |
} sample = { }; |
| 587 |
int err; |
| 588 |
|
| 589 |
if (test_bit(0, indio_dev->active_scan_mask)) { |
| 590 |
/* Get current pressure from hardware FIFO. */ |
| 591 |
err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure); |
| 592 |
if (err) { |
| 593 |
zpa2326_warn(indio_dev, "failed to fetch pressure (%d)", |
| 594 |
err); |
| 595 |
return err; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
if (test_bit(1, indio_dev->active_scan_mask)) { |
| 600 |
/* Get current temperature. */ |
| 601 |
err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG, |
| 602 |
&sample.temperature, 2); |
| 603 |
if (err) { |
| 604 |
zpa2326_warn(indio_dev, |
| 605 |
"failed to fetch temperature (%d)", err); |
| 606 |
return err; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/* |
| 611 |
* Now push samples using timestamp stored either : |
| 612 |
* - by hardware interrupt handler if interrupt is available: see |
| 613 |
* zpa2326_handle_irq(), |
| 614 |
* - or oneshot completion polling machinery : see |
| 615 |
* zpa2326_trigger_handler(). |
| 616 |
*/ |
| 617 |
zpa2326_dbg(indio_dev, "filling raw samples buffer"); |
| 618 |
|
| 619 |
iio_push_to_buffers_with_ts(indio_dev, &sample, sizeof(sample), |
| 620 |
private->timestamp); |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
#ifdef CONFIG_PM |
| 626 |
static int zpa2326_runtime_suspend(struct device *parent) |
| 627 |
{ |
| 628 |
const struct iio_dev *indio_dev = dev_get_drvdata(parent); |
| 629 |
|
| 630 |
if (pm_runtime_autosuspend_expiration(parent)) |
| 631 |
/* Userspace changed autosuspend delay. */ |
| 632 |
return -EAGAIN; |
| 633 |
|
| 634 |
zpa2326_power_off(indio_dev, iio_priv(indio_dev)); |
| 635 |
|
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static int zpa2326_runtime_resume(struct device *parent) |
| 640 |
{ |
| 641 |
const struct iio_dev *indio_dev = dev_get_drvdata(parent); |
| 642 |
|
| 643 |
return zpa2326_power_on(indio_dev, iio_priv(indio_dev)); |
| 644 |
} |
| 645 |
|
| 646 |
const struct dev_pm_ops zpa2326_pm_ops = { |
| 647 |
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 648 |
pm_runtime_force_resume) |
| 649 |
SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume, |
| 650 |
NULL) |
| 651 |
}; |
| 652 |
EXPORT_SYMBOL_NS_GPL(zpa2326_pm_ops, "IIO_ZPA2326"); |
| 653 |
|
| 654 |
/** |
| 655 |
* zpa2326_resume() - Request the PM layer to power supply the device. |
| 656 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 657 |
* |
| 658 |
* Return: |
| 659 |
* < 0 - a negative error code meaning failure ; |
| 660 |
* 0 - success, device has just been powered up ; |
| 661 |
* 1 - success, device was already powered. |
| 662 |
*/ |
| 663 |
static int zpa2326_resume(const struct iio_dev *indio_dev) |
| 664 |
{ |
| 665 |
int err; |
| 666 |
|
| 667 |
err = pm_runtime_get_sync(indio_dev->dev.parent); |
| 668 |
if (err < 0) { |
| 669 |
pm_runtime_put(indio_dev->dev.parent); |
| 670 |
return err; |
| 671 |
} |
| 672 |
|
| 673 |
if (err > 0) { |
| 674 |
/* |
| 675 |
* Device was already power supplied: get it out of low power |
| 676 |
* mode and inform caller. |
| 677 |
*/ |
| 678 |
zpa2326_enable_device(indio_dev); |
| 679 |
return 1; |
| 680 |
} |
| 681 |
|
| 682 |
/* Inform caller device has just been brought back to life. */ |
| 683 |
return 0; |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* zpa2326_suspend() - Schedule a power down using autosuspend feature of PM |
| 688 |
* layer. |
| 689 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 690 |
* |
| 691 |
* Device is switched to low power mode at first to save power even when |
| 692 |
* attached regulator is a "dummy" one. |
| 693 |
*/ |
| 694 |
static void zpa2326_suspend(struct iio_dev *indio_dev) |
| 695 |
{ |
| 696 |
struct device *parent = indio_dev->dev.parent; |
| 697 |
|
| 698 |
zpa2326_sleep(indio_dev); |
| 699 |
|
| 700 |
pm_runtime_put_autosuspend(parent); |
| 701 |
} |
| 702 |
|
| 703 |
static void zpa2326_init_runtime(struct device *parent) |
| 704 |
{ |
| 705 |
pm_runtime_get_noresume(parent); |
| 706 |
pm_runtime_set_active(parent); |
| 707 |
pm_runtime_enable(parent); |
| 708 |
pm_runtime_set_autosuspend_delay(parent, 1000); |
| 709 |
pm_runtime_use_autosuspend(parent); |
| 710 |
pm_runtime_put_autosuspend(parent); |
| 711 |
} |
| 712 |
|
| 713 |
static void zpa2326_fini_runtime(struct device *parent) |
| 714 |
{ |
| 715 |
pm_runtime_disable(parent); |
| 716 |
pm_runtime_set_suspended(parent); |
| 717 |
} |
| 718 |
#else /* !CONFIG_PM */ |
| 719 |
static int zpa2326_resume(const struct iio_dev *indio_dev) |
| 720 |
{ |
| 721 |
zpa2326_enable_device(indio_dev); |
| 722 |
|
| 723 |
return 0; |
| 724 |
} |
| 725 |
|
| 726 |
static void zpa2326_suspend(struct iio_dev *indio_dev) |
| 727 |
{ |
| 728 |
zpa2326_sleep(indio_dev); |
| 729 |
} |
| 730 |
|
| 731 |
#define zpa2326_init_runtime(_parent) |
| 732 |
#define zpa2326_fini_runtime(_parent) |
| 733 |
#endif /* !CONFIG_PM */ |
| 734 |
|
| 735 |
/** |
| 736 |
* zpa2326_handle_irq() - Process hardware interrupts. |
| 737 |
* @irq: Interrupt line the hardware uses to notify new data has arrived. |
| 738 |
* @data: The IIO device associated with the sampling hardware. |
| 739 |
* |
| 740 |
* Timestamp buffered samples as soon as possible then schedule threaded bottom |
| 741 |
* half. |
| 742 |
* |
| 743 |
* Return: Always successful. |
| 744 |
*/ |
| 745 |
static irqreturn_t zpa2326_handle_irq(int irq, void *data) |
| 746 |
{ |
| 747 |
struct iio_dev *indio_dev = data; |
| 748 |
|
| 749 |
if (iio_buffer_enabled(indio_dev)) { |
| 750 |
/* Timestamping needed for buffered sampling only. */ |
| 751 |
((struct zpa2326_private *) |
| 752 |
iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev); |
| 753 |
} |
| 754 |
|
| 755 |
return IRQ_WAKE_THREAD; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* zpa2326_handle_threaded_irq() - Interrupt bottom-half handler. |
| 760 |
* @irq: Interrupt line the hardware uses to notify new data has arrived. |
| 761 |
* @data: The IIO device associated with the sampling hardware. |
| 762 |
* |
| 763 |
* Mainly ensures interrupt is caused by a real "new sample available" |
| 764 |
* condition. This relies upon the ability to perform blocking / sleeping bus |
| 765 |
* accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is |
| 766 |
* called from within a thread, i.e. not called from hard interrupt context. |
| 767 |
* |
| 768 |
* When device is using its own internal hardware trigger in continuous sampling |
| 769 |
* mode, data are available into hardware FIFO once interrupt has occurred. All |
| 770 |
* we have to do is to dispatch the trigger, which in turn will fetch data and |
| 771 |
* fill IIO buffer. |
| 772 |
* |
| 773 |
* When not using its own internal hardware trigger, the device has been |
| 774 |
* configured in one-shot mode either by an external trigger or the IIO read_raw |
| 775 |
* hook. This means one of the latter is currently waiting for sampling |
| 776 |
* completion, in which case we must simply wake it up. |
| 777 |
* |
| 778 |
* See zpa2326_trigger_handler(). |
| 779 |
* |
| 780 |
* Return: |
| 781 |
* %IRQ_NONE - no consistent interrupt happened ; |
| 782 |
* %IRQ_HANDLED - there was new samples available. |
| 783 |
*/ |
| 784 |
static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data) |
| 785 |
{ |
| 786 |
struct iio_dev *indio_dev = data; |
| 787 |
struct zpa2326_private *priv = iio_priv(indio_dev); |
| 788 |
unsigned int val; |
| 789 |
bool cont; |
| 790 |
irqreturn_t ret = IRQ_NONE; |
| 791 |
|
| 792 |
/* |
| 793 |
* Are we using our own internal trigger in triggered buffer mode, i.e., |
| 794 |
* currently working in continuous sampling mode ? |
| 795 |
*/ |
| 796 |
cont = (iio_buffer_enabled(indio_dev) && |
| 797 |
iio_trigger_using_own(indio_dev)); |
| 798 |
|
| 799 |
/* |
| 800 |
* Device works according to a level interrupt scheme: reading interrupt |
| 801 |
* status de-asserts interrupt line. |
| 802 |
*/ |
| 803 |
priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val); |
| 804 |
if (priv->result < 0) { |
| 805 |
if (cont) |
| 806 |
return IRQ_NONE; |
| 807 |
|
| 808 |
goto complete; |
| 809 |
} |
| 810 |
|
| 811 |
/* Data ready is the only interrupt source we requested. */ |
| 812 |
if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) { |
| 813 |
/* |
| 814 |
* Interrupt happened but no new sample available: likely caused |
| 815 |
* by spurious interrupts, in which case, returning IRQ_NONE |
| 816 |
* allows to benefit from the generic spurious interrupts |
| 817 |
* handling. |
| 818 |
*/ |
| 819 |
zpa2326_warn(indio_dev, "unexpected interrupt status %02x", |
| 820 |
val); |
| 821 |
|
| 822 |
if (cont) |
| 823 |
return IRQ_NONE; |
| 824 |
|
| 825 |
priv->result = -ENODATA; |
| 826 |
goto complete; |
| 827 |
} |
| 828 |
|
| 829 |
/* New sample available: dispatch internal trigger consumers. */ |
| 830 |
iio_trigger_poll_nested(priv->trigger); |
| 831 |
|
| 832 |
if (cont) |
| 833 |
/* |
| 834 |
* Internal hardware trigger has been scheduled above : it will |
| 835 |
* fetch data on its own. |
| 836 |
*/ |
| 837 |
return IRQ_HANDLED; |
| 838 |
|
| 839 |
ret = IRQ_HANDLED; |
| 840 |
|
| 841 |
complete: |
| 842 |
/* |
| 843 |
* Wake up direct or externaly triggered buffer mode waiters: see |
| 844 |
* zpa2326_sample_oneshot() and zpa2326_trigger_handler(). |
| 845 |
*/ |
| 846 |
complete(&priv->data_ready); |
| 847 |
|
| 848 |
return ret; |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt. |
| 853 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 854 |
* @private: Internal private state related to @indio_dev. |
| 855 |
* |
| 856 |
* Return: Zero when successful, a negative error code otherwise. |
| 857 |
*/ |
| 858 |
static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev, |
| 859 |
struct zpa2326_private *private) |
| 860 |
{ |
| 861 |
unsigned int val; |
| 862 |
long time_left; |
| 863 |
|
| 864 |
zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt"); |
| 865 |
|
| 866 |
time_left = wait_for_completion_interruptible_timeout( |
| 867 |
&private->data_ready, ZPA2326_CONVERSION_JIFFIES); |
| 868 |
if (time_left > 0) |
| 869 |
/* |
| 870 |
* Interrupt handler completed before timeout: return operation |
| 871 |
* status. |
| 872 |
*/ |
| 873 |
return private->result; |
| 874 |
|
| 875 |
/* Clear all interrupts just to be sure. */ |
| 876 |
regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val); |
| 877 |
|
| 878 |
if (!time_left) { |
| 879 |
/* Timed out. */ |
| 880 |
zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)", |
| 881 |
time_left); |
| 882 |
return -ETIME; |
| 883 |
} |
| 884 |
|
| 885 |
zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled"); |
| 886 |
return -ERESTARTSYS; |
| 887 |
} |
| 888 |
|
| 889 |
static int zpa2326_init_managed_irq(struct device *parent, |
| 890 |
struct iio_dev *indio_dev, |
| 891 |
struct zpa2326_private *private, |
| 892 |
int irq) |
| 893 |
{ |
| 894 |
int err; |
| 895 |
|
| 896 |
private->irq = irq; |
| 897 |
|
| 898 |
if (irq <= 0) { |
| 899 |
/* |
| 900 |
* Platform declared no interrupt line: device will be polled |
| 901 |
* for data availability. |
| 902 |
*/ |
| 903 |
dev_info(parent, "no interrupt found, running in polling mode"); |
| 904 |
return 0; |
| 905 |
} |
| 906 |
|
| 907 |
init_completion(&private->data_ready); |
| 908 |
|
| 909 |
/* Request handler to be scheduled into threaded interrupt context. */ |
| 910 |
err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq, |
| 911 |
zpa2326_handle_threaded_irq, |
| 912 |
IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 913 |
dev_name(parent), indio_dev); |
| 914 |
if (err) { |
| 915 |
dev_err(parent, "failed to request interrupt %d (%d)", irq, |
| 916 |
err); |
| 917 |
return err; |
| 918 |
} |
| 919 |
|
| 920 |
dev_info(parent, "using interrupt %d", irq); |
| 921 |
|
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
/** |
| 926 |
* zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready. |
| 927 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 928 |
* |
| 929 |
* Loop over registers content to detect end of sampling cycle. Used when DT |
| 930 |
* declared no valid interrupt lines. |
| 931 |
* |
| 932 |
* Return: Zero when successful, a negative error code otherwise. |
| 933 |
*/ |
| 934 |
static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev) |
| 935 |
{ |
| 936 |
unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES; |
| 937 |
struct regmap *regs = ((struct zpa2326_private *) |
| 938 |
iio_priv(indio_dev))->regmap; |
| 939 |
unsigned int val; |
| 940 |
int err; |
| 941 |
|
| 942 |
zpa2326_dbg(indio_dev, "polling for one shot completion"); |
| 943 |
|
| 944 |
/* |
| 945 |
* At least, 100 ms is needed for the device to complete its one-shot |
| 946 |
* cycle. |
| 947 |
*/ |
| 948 |
if (msleep_interruptible(100)) |
| 949 |
return -ERESTARTSYS; |
| 950 |
|
| 951 |
/* Poll for conversion completion in hardware. */ |
| 952 |
while (true) { |
| 953 |
err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val); |
| 954 |
if (err < 0) |
| 955 |
goto err; |
| 956 |
|
| 957 |
if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT)) |
| 958 |
/* One-shot bit self clears at conversion end. */ |
| 959 |
break; |
| 960 |
|
| 961 |
if (time_after(jiffies, tmout)) { |
| 962 |
/* Prevent from waiting forever : let's time out. */ |
| 963 |
err = -ETIME; |
| 964 |
goto err; |
| 965 |
} |
| 966 |
|
| 967 |
usleep_range(10000, 20000); |
| 968 |
} |
| 969 |
|
| 970 |
/* |
| 971 |
* In oneshot mode, pressure sample availability guarantees that |
| 972 |
* temperature conversion has also completed : just check pressure |
| 973 |
* status bit to keep things simple. |
| 974 |
*/ |
| 975 |
err = regmap_read(regs, ZPA2326_STATUS_REG, &val); |
| 976 |
if (err < 0) |
| 977 |
goto err; |
| 978 |
|
| 979 |
if (!(val & ZPA2326_STATUS_P_DA)) { |
| 980 |
/* No sample available. */ |
| 981 |
err = -ENODATA; |
| 982 |
goto err; |
| 983 |
} |
| 984 |
|
| 985 |
return 0; |
| 986 |
|
| 987 |
err: |
| 988 |
zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err); |
| 989 |
|
| 990 |
return err; |
| 991 |
} |
| 992 |
|
| 993 |
/** |
| 994 |
* zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU |
| 995 |
* endianness. |
| 996 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 997 |
* @type: Type of measurement / channel to fetch from. |
| 998 |
* @value: Sample output. |
| 999 |
* |
| 1000 |
* Return: Zero when successful, a negative error code otherwise. |
| 1001 |
*/ |
| 1002 |
static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev, |
| 1003 |
enum iio_chan_type type, |
| 1004 |
int *value) |
| 1005 |
{ |
| 1006 |
struct regmap *regs = ((struct zpa2326_private *) |
| 1007 |
iio_priv(indio_dev))->regmap; |
| 1008 |
int err; |
| 1009 |
u8 v[3]; |
| 1010 |
|
| 1011 |
switch (type) { |
| 1012 |
case IIO_PRESSURE: |
| 1013 |
zpa2326_dbg(indio_dev, "fetching raw pressure sample"); |
| 1014 |
|
| 1015 |
err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, v, sizeof(v)); |
| 1016 |
if (err) { |
| 1017 |
zpa2326_warn(indio_dev, "failed to fetch pressure (%d)", |
| 1018 |
err); |
| 1019 |
return err; |
| 1020 |
} |
| 1021 |
|
| 1022 |
*value = get_unaligned_le24(&v[0]); |
| 1023 |
|
| 1024 |
return IIO_VAL_INT; |
| 1025 |
|
| 1026 |
case IIO_TEMP: |
| 1027 |
zpa2326_dbg(indio_dev, "fetching raw temperature sample"); |
| 1028 |
|
| 1029 |
err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2); |
| 1030 |
if (err) { |
| 1031 |
zpa2326_warn(indio_dev, |
| 1032 |
"failed to fetch temperature (%d)", err); |
| 1033 |
return err; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* Temperature is a 16 bits wide little-endian signed int. */ |
| 1037 |
*value = (int)le16_to_cpup((__le16 *)value); |
| 1038 |
|
| 1039 |
return IIO_VAL_INT; |
| 1040 |
|
| 1041 |
default: |
| 1042 |
return -EINVAL; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle. |
| 1048 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 1049 |
* @type: Type of measurement / channel to fetch from. |
| 1050 |
* @value: Sample output. |
| 1051 |
* |
| 1052 |
* Return: Zero when successful, a negative error code otherwise. |
| 1053 |
*/ |
| 1054 |
static int zpa2326_sample_oneshot(struct iio_dev *indio_dev, |
| 1055 |
enum iio_chan_type type, |
| 1056 |
int *value) |
| 1057 |
{ |
| 1058 |
int ret; |
| 1059 |
struct zpa2326_private *priv; |
| 1060 |
|
| 1061 |
if (!iio_device_claim_direct(indio_dev)) |
| 1062 |
return -EBUSY; |
| 1063 |
|
| 1064 |
ret = zpa2326_resume(indio_dev); |
| 1065 |
if (ret < 0) |
| 1066 |
goto release; |
| 1067 |
|
| 1068 |
priv = iio_priv(indio_dev); |
| 1069 |
|
| 1070 |
if (ret > 0) { |
| 1071 |
/* |
| 1072 |
* We were already power supplied. Just clear hardware FIFO to |
| 1073 |
* get rid of samples acquired during previous rounds (if any). |
| 1074 |
* Sampling operation always generates both temperature and |
| 1075 |
* pressure samples. The latter are always enqueued into |
| 1076 |
* hardware FIFO. This may lead to situations were pressure |
| 1077 |
* samples still sit into FIFO when previous cycle(s) fetched |
| 1078 |
* temperature data only. |
| 1079 |
* Hence, we need to clear hardware FIFO content to prevent from |
| 1080 |
* getting outdated values at the end of current cycle. |
| 1081 |
*/ |
| 1082 |
if (type == IIO_PRESSURE) { |
| 1083 |
ret = zpa2326_clear_fifo(indio_dev, 0); |
| 1084 |
if (ret) |
| 1085 |
goto suspend; |
| 1086 |
} |
| 1087 |
} else { |
| 1088 |
/* |
| 1089 |
* We have just been power supplied, i.e. device is in default |
| 1090 |
* "out of reset" state, meaning we need to reconfigure it |
| 1091 |
* entirely. |
| 1092 |
*/ |
| 1093 |
ret = zpa2326_config_oneshot(indio_dev, priv->irq); |
| 1094 |
if (ret) |
| 1095 |
goto suspend; |
| 1096 |
} |
| 1097 |
|
| 1098 |
/* Start a sampling cycle in oneshot mode. */ |
| 1099 |
ret = zpa2326_start_oneshot(indio_dev); |
| 1100 |
if (ret) |
| 1101 |
goto suspend; |
| 1102 |
|
| 1103 |
/* Wait for sampling cycle to complete. */ |
| 1104 |
if (priv->irq > 0) |
| 1105 |
ret = zpa2326_wait_oneshot_completion(indio_dev, priv); |
| 1106 |
else |
| 1107 |
ret = zpa2326_poll_oneshot_completion(indio_dev); |
| 1108 |
|
| 1109 |
if (ret) |
| 1110 |
goto suspend; |
| 1111 |
|
| 1112 |
/* Retrieve raw sample value and convert it to CPU endianness. */ |
| 1113 |
ret = zpa2326_fetch_raw_sample(indio_dev, type, value); |
| 1114 |
|
| 1115 |
suspend: |
| 1116 |
zpa2326_suspend(indio_dev); |
| 1117 |
release: |
| 1118 |
iio_device_release_direct(indio_dev); |
| 1119 |
|
| 1120 |
return ret; |
| 1121 |
} |
| 1122 |
|
| 1123 |
/** |
| 1124 |
* zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one |
| 1125 |
* shot mode. |
| 1126 |
* @irq: The software interrupt assigned to @data |
| 1127 |
* @data: The IIO poll function dispatched by external trigger our device is |
| 1128 |
* attached to. |
| 1129 |
* |
| 1130 |
* Bottom-half handler called by the IIO trigger to which our device is |
| 1131 |
* currently attached. Allows us to synchronize this device buffered sampling |
| 1132 |
* either with external events (such as timer expiration, external device sample |
| 1133 |
* ready, etc...) or with its own interrupt (internal hardware trigger). |
| 1134 |
* |
| 1135 |
* When using an external trigger, basically run the same sequence of operations |
| 1136 |
* as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO |
| 1137 |
* is not cleared since already done at buffering enable time and samples |
| 1138 |
* dequeueing always retrieves the most recent value. |
| 1139 |
* |
| 1140 |
* Otherwise, when internal hardware trigger has dispatched us, just fetch data |
| 1141 |
* from hardware FIFO. |
| 1142 |
* |
| 1143 |
* Fetched data will pushed unprocessed to IIO buffer since samples conversion |
| 1144 |
* is delegated to userspace in buffered mode (endianness, etc...). |
| 1145 |
* |
| 1146 |
* Return: |
| 1147 |
* %IRQ_NONE - no consistent interrupt happened ; |
| 1148 |
* %IRQ_HANDLED - there was new samples available. |
| 1149 |
*/ |
| 1150 |
static irqreturn_t zpa2326_trigger_handler(int irq, void *data) |
| 1151 |
{ |
| 1152 |
struct iio_dev *indio_dev = ((struct iio_poll_func *) |
| 1153 |
data)->indio_dev; |
| 1154 |
struct zpa2326_private *priv = iio_priv(indio_dev); |
| 1155 |
bool cont; |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* We have been dispatched, meaning we are in triggered buffer mode. |
| 1159 |
* Using our own internal trigger implies we are currently in continuous |
| 1160 |
* hardware sampling mode. |
| 1161 |
*/ |
| 1162 |
cont = iio_trigger_using_own(indio_dev); |
| 1163 |
|
| 1164 |
if (!cont) { |
| 1165 |
/* On demand sampling : start a one shot cycle. */ |
| 1166 |
if (zpa2326_start_oneshot(indio_dev)) |
| 1167 |
goto out; |
| 1168 |
|
| 1169 |
/* Wait for sampling cycle to complete. */ |
| 1170 |
if (priv->irq <= 0) { |
| 1171 |
/* No interrupt available: poll for completion. */ |
| 1172 |
if (zpa2326_poll_oneshot_completion(indio_dev)) |
| 1173 |
goto out; |
| 1174 |
|
| 1175 |
/* Only timestamp sample once it is ready. */ |
| 1176 |
priv->timestamp = iio_get_time_ns(indio_dev); |
| 1177 |
} else { |
| 1178 |
/* Interrupt handlers will timestamp for us. */ |
| 1179 |
if (zpa2326_wait_oneshot_completion(indio_dev, priv)) |
| 1180 |
goto out; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* Enqueue to IIO buffer / userspace. */ |
| 1185 |
zpa2326_fill_sample_buffer(indio_dev, priv); |
| 1186 |
|
| 1187 |
out: |
| 1188 |
if (!cont) |
| 1189 |
/* Don't switch to low power if sampling continuously. */ |
| 1190 |
zpa2326_sleep(indio_dev); |
| 1191 |
|
| 1192 |
/* Inform attached trigger we are done. */ |
| 1193 |
iio_trigger_notify_done(indio_dev->trig); |
| 1194 |
|
| 1195 |
return IRQ_HANDLED; |
| 1196 |
} |
| 1197 |
|
| 1198 |
/** |
| 1199 |
* zpa2326_preenable_buffer() - Prepare device for configuring triggered |
| 1200 |
* sampling |
| 1201 |
* modes. |
| 1202 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 1203 |
* |
| 1204 |
* Basically power up device. |
| 1205 |
* Called with IIO device's lock held. |
| 1206 |
* |
| 1207 |
* Return: Zero when successful, a negative error code otherwise. |
| 1208 |
*/ |
| 1209 |
static int zpa2326_preenable_buffer(struct iio_dev *indio_dev) |
| 1210 |
{ |
| 1211 |
int ret = zpa2326_resume(indio_dev); |
| 1212 |
|
| 1213 |
if (ret < 0) |
| 1214 |
return ret; |
| 1215 |
|
| 1216 |
/* Tell zpa2326_postenable_buffer() if we have just been powered on. */ |
| 1217 |
((struct zpa2326_private *) |
| 1218 |
iio_priv(indio_dev))->waken = iio_priv(indio_dev); |
| 1219 |
|
| 1220 |
return 0; |
| 1221 |
} |
| 1222 |
|
| 1223 |
/** |
| 1224 |
* zpa2326_postenable_buffer() - Configure device for triggered sampling. |
| 1225 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 1226 |
* |
| 1227 |
* Basically setup one-shot mode if plugging external trigger. |
| 1228 |
* Otherwise, let internal trigger configure continuous sampling : |
| 1229 |
* see zpa2326_set_trigger_state(). |
| 1230 |
* |
| 1231 |
* If an error is returned, IIO layer will call our postdisable hook for us, |
| 1232 |
* i.e. no need to explicitly power device off here. |
| 1233 |
* Called with IIO device's lock held. |
| 1234 |
* |
| 1235 |
* Called with IIO device's lock held. |
| 1236 |
* |
| 1237 |
* Return: Zero when successful, a negative error code otherwise. |
| 1238 |
*/ |
| 1239 |
static int zpa2326_postenable_buffer(struct iio_dev *indio_dev) |
| 1240 |
{ |
| 1241 |
const struct zpa2326_private *priv = iio_priv(indio_dev); |
| 1242 |
int err; |
| 1243 |
|
| 1244 |
if (!priv->waken) { |
| 1245 |
/* |
| 1246 |
* We were already power supplied. Just clear hardware FIFO to |
| 1247 |
* get rid of samples acquired during previous rounds (if any). |
| 1248 |
*/ |
| 1249 |
err = zpa2326_clear_fifo(indio_dev, 0); |
| 1250 |
if (err) { |
| 1251 |
zpa2326_err(indio_dev, |
| 1252 |
"failed to enable buffering (%d)", err); |
| 1253 |
return err; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
if (!iio_trigger_using_own(indio_dev) && priv->waken) { |
| 1258 |
/* |
| 1259 |
* We are using an external trigger and we have just been |
| 1260 |
* powered up: reconfigure one-shot mode. |
| 1261 |
*/ |
| 1262 |
err = zpa2326_config_oneshot(indio_dev, priv->irq); |
| 1263 |
if (err) { |
| 1264 |
zpa2326_err(indio_dev, |
| 1265 |
"failed to enable buffering (%d)", err); |
| 1266 |
return err; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
return 0; |
| 1271 |
} |
| 1272 |
|
| 1273 |
static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev) |
| 1274 |
{ |
| 1275 |
zpa2326_suspend(indio_dev); |
| 1276 |
|
| 1277 |
return 0; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = { |
| 1281 |
.preenable = zpa2326_preenable_buffer, |
| 1282 |
.postenable = zpa2326_postenable_buffer, |
| 1283 |
.postdisable = zpa2326_postdisable_buffer |
| 1284 |
}; |
| 1285 |
|
| 1286 |
/** |
| 1287 |
* zpa2326_set_trigger_state() - Start / stop continuous sampling. |
| 1288 |
* @trig: The trigger being attached to IIO device associated with the sampling |
| 1289 |
* hardware. |
| 1290 |
* @state: Tell whether to start (true) or stop (false) |
| 1291 |
* |
| 1292 |
* Basically enable / disable hardware continuous sampling mode. |
| 1293 |
* |
| 1294 |
* Called with IIO device's lock held at postenable() or predisable() time. |
| 1295 |
* |
| 1296 |
* Return: Zero when successful, a negative error code otherwise. |
| 1297 |
*/ |
| 1298 |
static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state) |
| 1299 |
{ |
| 1300 |
const struct iio_dev *indio_dev = dev_get_drvdata( |
| 1301 |
trig->dev.parent); |
| 1302 |
const struct zpa2326_private *priv = iio_priv(indio_dev); |
| 1303 |
int err; |
| 1304 |
|
| 1305 |
if (!state) { |
| 1306 |
/* |
| 1307 |
* Switch trigger off : in case of failure, interrupt is left |
| 1308 |
* disabled in order to prevent handler from accessing released |
| 1309 |
* resources. |
| 1310 |
*/ |
| 1311 |
unsigned int val; |
| 1312 |
|
| 1313 |
/* |
| 1314 |
* As device is working in continuous mode, handlers may be |
| 1315 |
* accessing resources we are currently freeing... |
| 1316 |
* Prevent this by disabling interrupt handlers and ensure |
| 1317 |
* the device will generate no more interrupts unless explicitly |
| 1318 |
* required to, i.e. by restoring back to default one shot mode. |
| 1319 |
*/ |
| 1320 |
disable_irq(priv->irq); |
| 1321 |
|
| 1322 |
/* |
| 1323 |
* Disable continuous sampling mode to restore settings for |
| 1324 |
* one shot / direct sampling operations. |
| 1325 |
*/ |
| 1326 |
err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG, |
| 1327 |
zpa2326_highest_frequency()->odr); |
| 1328 |
if (err) |
| 1329 |
return err; |
| 1330 |
|
| 1331 |
/* |
| 1332 |
* Now that device won't generate interrupts on its own, |
| 1333 |
* acknowledge any currently active interrupts (may happen on |
| 1334 |
* rare occasions while stopping continuous mode). |
| 1335 |
*/ |
| 1336 |
err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val); |
| 1337 |
if (err < 0) |
| 1338 |
return err; |
| 1339 |
|
| 1340 |
/* |
| 1341 |
* Re-enable interrupts only if we can guarantee the device will |
| 1342 |
* generate no more interrupts to prevent handlers from |
| 1343 |
* accessing released resources. |
| 1344 |
*/ |
| 1345 |
enable_irq(priv->irq); |
| 1346 |
|
| 1347 |
zpa2326_dbg(indio_dev, "continuous mode stopped"); |
| 1348 |
} else { |
| 1349 |
/* |
| 1350 |
* Switch trigger on : start continuous sampling at required |
| 1351 |
* frequency. |
| 1352 |
*/ |
| 1353 |
|
| 1354 |
if (priv->waken) { |
| 1355 |
/* Enable interrupt if getting out of reset. */ |
| 1356 |
err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG, |
| 1357 |
(u8) |
| 1358 |
~ZPA2326_CTRL_REG1_MASK_DATA_READY); |
| 1359 |
if (err) |
| 1360 |
return err; |
| 1361 |
} |
| 1362 |
|
| 1363 |
/* Enable continuous sampling at specified frequency. */ |
| 1364 |
err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG, |
| 1365 |
ZPA2326_CTRL_REG3_ENABLE_MEAS | |
| 1366 |
priv->frequency->odr); |
| 1367 |
if (err) |
| 1368 |
return err; |
| 1369 |
|
| 1370 |
zpa2326_dbg(indio_dev, "continuous mode setup @%dHz", |
| 1371 |
priv->frequency->hz); |
| 1372 |
} |
| 1373 |
|
| 1374 |
return 0; |
| 1375 |
} |
| 1376 |
|
| 1377 |
static const struct iio_trigger_ops zpa2326_trigger_ops = { |
| 1378 |
.set_trigger_state = zpa2326_set_trigger_state, |
| 1379 |
}; |
| 1380 |
|
| 1381 |
/** |
| 1382 |
* zpa2326_init_managed_trigger() - Create interrupt driven / hardware trigger |
| 1383 |
* allowing to notify external devices a new sample is |
| 1384 |
* ready. |
| 1385 |
* @parent: Hardware sampling device @indio_dev is a child of. |
| 1386 |
* @indio_dev: The IIO device associated with the sampling hardware. |
| 1387 |
* @private: Internal private state related to @indio_dev. |
| 1388 |
* @irq: Optional interrupt line the hardware uses to notify new data |
| 1389 |
* samples are ready. Negative or zero values indicate no interrupts |
| 1390 |
* are available, meaning polling is required. |
| 1391 |
* |
| 1392 |
* Only relevant when DT declares a valid interrupt line. |
| 1393 |
* |
| 1394 |
* Return: Zero when successful, a negative error code otherwise. |
| 1395 |
*/ |
| 1396 |
static int zpa2326_init_managed_trigger(struct device *parent, |
| 1397 |
struct iio_dev *indio_dev, |
| 1398 |
struct zpa2326_private *private, |
| 1399 |
int irq) |
| 1400 |
{ |
| 1401 |
struct iio_trigger *trigger; |
| 1402 |
int ret; |
| 1403 |
|
| 1404 |
if (irq <= 0) |
| 1405 |
return 0; |
| 1406 |
|
| 1407 |
trigger = devm_iio_trigger_alloc(parent, "%s-dev%d", |
| 1408 |
indio_dev->name, |
| 1409 |
iio_device_id(indio_dev)); |
| 1410 |
if (!trigger) |
| 1411 |
return -ENOMEM; |
| 1412 |
|
| 1413 |
/* Basic setup. */ |
| 1414 |
trigger->ops = &zpa2326_trigger_ops; |
| 1415 |
|
| 1416 |
private->trigger = trigger; |
| 1417 |
|
| 1418 |
/* Register to triggers space. */ |
| 1419 |
ret = devm_iio_trigger_register(parent, trigger); |
| 1420 |
if (ret) |
| 1421 |
dev_err(parent, "failed to register hardware trigger (%d)", |
| 1422 |
ret); |
| 1423 |
|
| 1424 |
return ret; |
| 1425 |
} |
| 1426 |
|
| 1427 |
static int zpa2326_get_frequency(const struct iio_dev *indio_dev) |
| 1428 |
{ |
| 1429 |
return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz; |
| 1430 |
} |
| 1431 |
|
| 1432 |
static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz) |
| 1433 |
{ |
| 1434 |
struct zpa2326_private *priv = iio_priv(indio_dev); |
| 1435 |
int freq; |
| 1436 |
|
| 1437 |
/* Check if requested frequency is supported. */ |
| 1438 |
for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++) |
| 1439 |
if (zpa2326_sampling_frequencies[freq].hz == hz) |
| 1440 |
break; |
| 1441 |
if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies)) |
| 1442 |
return -EINVAL; |
| 1443 |
|
| 1444 |
/* Don't allow changing frequency if buffered sampling is ongoing. */ |
| 1445 |
if (!iio_device_claim_direct(indio_dev)) |
| 1446 |
return -EBUSY; |
| 1447 |
|
| 1448 |
priv->frequency = &zpa2326_sampling_frequencies[freq]; |
| 1449 |
|
| 1450 |
iio_device_release_direct(indio_dev); |
| 1451 |
|
| 1452 |
return 0; |
| 1453 |
} |
| 1454 |
|
| 1455 |
/* Expose supported hardware sampling frequencies (Hz) through sysfs. */ |
| 1456 |
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23"); |
| 1457 |
|
| 1458 |
static struct attribute *zpa2326_attributes[] = { |
| 1459 |
&iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 1460 |
NULL |
| 1461 |
}; |
| 1462 |
|
| 1463 |
static const struct attribute_group zpa2326_attribute_group = { |
| 1464 |
.attrs = zpa2326_attributes, |
| 1465 |
}; |
| 1466 |
|
| 1467 |
static int zpa2326_read_raw(struct iio_dev *indio_dev, |
| 1468 |
struct iio_chan_spec const *chan, |
| 1469 |
int *val, |
| 1470 |
int *val2, |
| 1471 |
long mask) |
| 1472 |
{ |
| 1473 |
switch (mask) { |
| 1474 |
case IIO_CHAN_INFO_RAW: |
| 1475 |
return zpa2326_sample_oneshot(indio_dev, chan->type, val); |
| 1476 |
|
| 1477 |
case IIO_CHAN_INFO_SCALE: |
| 1478 |
switch (chan->type) { |
| 1479 |
case IIO_PRESSURE: |
| 1480 |
/* |
| 1481 |
* Pressure resolution is 1/64 Pascal. Scale to kPascal |
| 1482 |
* as required by IIO ABI. |
| 1483 |
*/ |
| 1484 |
*val = 1; |
| 1485 |
*val2 = 64000; |
| 1486 |
return IIO_VAL_FRACTIONAL; |
| 1487 |
|
| 1488 |
case IIO_TEMP: |
| 1489 |
/* |
| 1490 |
* Temperature follows the equation: |
| 1491 |
* Temp[degC] = Tempcode * 0.00649 - 176.83 |
| 1492 |
* where: |
| 1493 |
* Tempcode is composed the raw sampled 16 bits. |
| 1494 |
* |
| 1495 |
* Hence, to produce a temperature in milli-degrees |
| 1496 |
* Celsius according to IIO ABI, we need to apply the |
| 1497 |
* following equation to raw samples: |
| 1498 |
* Temp[milli degC] = (Tempcode + Offset) * Scale |
| 1499 |
* where: |
| 1500 |
* Offset = -176.83 / 0.00649 |
| 1501 |
* Scale = 0.00649 * 1000 |
| 1502 |
*/ |
| 1503 |
*val = 6; |
| 1504 |
*val2 = 490000; |
| 1505 |
return IIO_VAL_INT_PLUS_MICRO; |
| 1506 |
|
| 1507 |
default: |
| 1508 |
return -EINVAL; |
| 1509 |
} |
| 1510 |
|
| 1511 |
case IIO_CHAN_INFO_OFFSET: |
| 1512 |
switch (chan->type) { |
| 1513 |
case IIO_TEMP: |
| 1514 |
*val = -17683000; |
| 1515 |
*val2 = 649; |
| 1516 |
return IIO_VAL_FRACTIONAL; |
| 1517 |
|
| 1518 |
default: |
| 1519 |
return -EINVAL; |
| 1520 |
} |
| 1521 |
|
| 1522 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 1523 |
*val = zpa2326_get_frequency(indio_dev); |
| 1524 |
return IIO_VAL_INT; |
| 1525 |
|
| 1526 |
default: |
| 1527 |
return -EINVAL; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
static int zpa2326_write_raw(struct iio_dev *indio_dev, |
| 1532 |
const struct iio_chan_spec *chan, |
| 1533 |
int val, |
| 1534 |
int val2, |
| 1535 |
long mask) |
| 1536 |
{ |
| 1537 |
if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2) |
| 1538 |
return -EINVAL; |
| 1539 |
|
| 1540 |
return zpa2326_set_frequency(indio_dev, val); |
| 1541 |
} |
| 1542 |
|
| 1543 |
static const struct iio_chan_spec zpa2326_channels[] = { |
| 1544 |
[0] = { |
| 1545 |
.type = IIO_PRESSURE, |
| 1546 |
.scan_index = 0, |
| 1547 |
.scan_type = { |
| 1548 |
.sign = 'u', |
| 1549 |
.realbits = 24, |
| 1550 |
.storagebits = 32, |
| 1551 |
.endianness = IIO_LE, |
| 1552 |
}, |
| 1553 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 1554 |
BIT(IIO_CHAN_INFO_SCALE), |
| 1555 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
| 1556 |
}, |
| 1557 |
[1] = { |
| 1558 |
.type = IIO_TEMP, |
| 1559 |
.scan_index = 1, |
| 1560 |
.scan_type = { |
| 1561 |
.sign = 's', |
| 1562 |
.realbits = 16, |
| 1563 |
.storagebits = 16, |
| 1564 |
.endianness = IIO_LE, |
| 1565 |
}, |
| 1566 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 1567 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 1568 |
BIT(IIO_CHAN_INFO_OFFSET), |
| 1569 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
| 1570 |
}, |
| 1571 |
[2] = IIO_CHAN_SOFT_TIMESTAMP(2), |
| 1572 |
}; |
| 1573 |
|
| 1574 |
static const struct iio_info zpa2326_info = { |
| 1575 |
.attrs = &zpa2326_attribute_group, |
| 1576 |
.read_raw = zpa2326_read_raw, |
| 1577 |
.write_raw = zpa2326_write_raw, |
| 1578 |
}; |
| 1579 |
|
| 1580 |
static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device, |
| 1581 |
const char *name, |
| 1582 |
struct regmap *regmap) |
| 1583 |
{ |
| 1584 |
struct iio_dev *indio_dev; |
| 1585 |
|
| 1586 |
/* Allocate space to hold IIO device internal state. */ |
| 1587 |
indio_dev = devm_iio_device_alloc(device, |
| 1588 |
sizeof(struct zpa2326_private)); |
| 1589 |
if (!indio_dev) |
| 1590 |
return NULL; |
| 1591 |
|
| 1592 |
/* Setup for userspace synchronous on demand sampling. */ |
| 1593 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1594 |
indio_dev->channels = zpa2326_channels; |
| 1595 |
indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels); |
| 1596 |
indio_dev->name = name; |
| 1597 |
indio_dev->info = &zpa2326_info; |
| 1598 |
|
| 1599 |
return indio_dev; |
| 1600 |
} |
| 1601 |
|
| 1602 |
int zpa2326_probe(struct device *parent, |
| 1603 |
const char *name, |
| 1604 |
int irq, |
| 1605 |
unsigned int hwid, |
| 1606 |
struct regmap *regmap) |
| 1607 |
{ |
| 1608 |
struct iio_dev *indio_dev; |
| 1609 |
struct zpa2326_private *priv; |
| 1610 |
int err; |
| 1611 |
unsigned int id; |
| 1612 |
|
| 1613 |
indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap); |
| 1614 |
if (!indio_dev) |
| 1615 |
return -ENOMEM; |
| 1616 |
|
| 1617 |
priv = iio_priv(indio_dev); |
| 1618 |
|
| 1619 |
priv->vref = devm_regulator_get(parent, "vref"); |
| 1620 |
if (IS_ERR(priv->vref)) |
| 1621 |
return PTR_ERR(priv->vref); |
| 1622 |
|
| 1623 |
priv->vdd = devm_regulator_get(parent, "vdd"); |
| 1624 |
if (IS_ERR(priv->vdd)) |
| 1625 |
return PTR_ERR(priv->vdd); |
| 1626 |
|
| 1627 |
/* Set default hardware sampling frequency to highest rate supported. */ |
| 1628 |
priv->frequency = zpa2326_highest_frequency(); |
| 1629 |
|
| 1630 |
/* |
| 1631 |
* Plug device's underlying bus abstraction : this MUST be set before |
| 1632 |
* registering interrupt handlers since an interrupt might happen if |
| 1633 |
* power up sequence is not properly applied. |
| 1634 |
*/ |
| 1635 |
priv->regmap = regmap; |
| 1636 |
|
| 1637 |
err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL, |
| 1638 |
zpa2326_trigger_handler, |
| 1639 |
&zpa2326_buffer_setup_ops); |
| 1640 |
if (err) |
| 1641 |
return err; |
| 1642 |
|
| 1643 |
err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq); |
| 1644 |
if (err) |
| 1645 |
return err; |
| 1646 |
|
| 1647 |
err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq); |
| 1648 |
if (err) |
| 1649 |
return err; |
| 1650 |
|
| 1651 |
/* Power up to check device ID and perform initial hardware setup. */ |
| 1652 |
err = zpa2326_power_on(indio_dev, priv); |
| 1653 |
if (err) |
| 1654 |
return err; |
| 1655 |
|
| 1656 |
/* Read id register to check we are talking to the right slave. */ |
| 1657 |
err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id); |
| 1658 |
if (err) |
| 1659 |
goto sleep; |
| 1660 |
|
| 1661 |
if (id != hwid) { |
| 1662 |
dev_err(parent, "found device with unexpected id %02x", id); |
| 1663 |
err = -ENODEV; |
| 1664 |
goto sleep; |
| 1665 |
} |
| 1666 |
|
| 1667 |
err = zpa2326_config_oneshot(indio_dev, irq); |
| 1668 |
if (err) |
| 1669 |
goto sleep; |
| 1670 |
|
| 1671 |
/* Setup done : go sleeping. Device will be awaken upon user request. */ |
| 1672 |
err = zpa2326_sleep(indio_dev); |
| 1673 |
if (err) |
| 1674 |
goto poweroff; |
| 1675 |
|
| 1676 |
dev_set_drvdata(parent, indio_dev); |
| 1677 |
|
| 1678 |
zpa2326_init_runtime(parent); |
| 1679 |
|
| 1680 |
err = iio_device_register(indio_dev); |
| 1681 |
if (err) { |
| 1682 |
zpa2326_fini_runtime(parent); |
| 1683 |
goto poweroff; |
| 1684 |
} |
| 1685 |
|
| 1686 |
return 0; |
| 1687 |
|
| 1688 |
sleep: |
| 1689 |
/* Put to sleep just in case power regulators are "dummy" ones. */ |
| 1690 |
zpa2326_sleep(indio_dev); |
| 1691 |
poweroff: |
| 1692 |
zpa2326_power_off(indio_dev, priv); |
| 1693 |
|
| 1694 |
return err; |
| 1695 |
} |
| 1696 |
EXPORT_SYMBOL_NS_GPL(zpa2326_probe, "IIO_ZPA2326"); |
| 1697 |
|
| 1698 |
void zpa2326_remove(const struct device *parent) |
| 1699 |
{ |
| 1700 |
struct iio_dev *indio_dev = dev_get_drvdata(parent); |
| 1701 |
|
| 1702 |
iio_device_unregister(indio_dev); |
| 1703 |
zpa2326_fini_runtime(indio_dev->dev.parent); |
| 1704 |
zpa2326_sleep(indio_dev); |
| 1705 |
zpa2326_power_off(indio_dev, iio_priv(indio_dev)); |
| 1706 |
} |
| 1707 |
EXPORT_SYMBOL_NS_GPL(zpa2326_remove, "IIO_ZPA2326"); |
| 1708 |
|
| 1709 |
MODULE_AUTHOR("Gregor Boirie <[email protected]>"); |
| 1710 |
MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor"); |
| 1711 |
MODULE_LICENSE("GPL v2"); |