| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* BM1390 ROHM pressure sensor |
| 4 |
* |
| 5 |
* Copyright (c) 2023, ROHM Semiconductor. |
| 6 |
* https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/pressure/bm1390glv-z-e.pdf |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/bitfield.h> |
| 10 |
#include <linux/bits.h> |
| 11 |
#include <linux/cleanup.h> |
| 12 |
#include <linux/device.h> |
| 13 |
#include <linux/i2c.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
#include <linux/regulator/consumer.h> |
| 17 |
|
| 18 |
#include <linux/iio/iio.h> |
| 19 |
#include <linux/iio/trigger.h> |
| 20 |
#include <linux/iio/trigger_consumer.h> |
| 21 |
#include <linux/iio/triggered_buffer.h> |
| 22 |
|
| 23 |
#define BM1390_REG_MANUFACT_ID 0x0f |
| 24 |
#define BM1390_REG_PART_ID 0x10 |
| 25 |
#define BM1390_REG_POWER 0x12 |
| 26 |
#define BM1390_MASK_POWER BIT(0) |
| 27 |
#define BM1390_POWER_ON BM1390_MASK_POWER |
| 28 |
#define BM1390_POWER_OFF 0x00 |
| 29 |
#define BM1390_REG_RESET 0x13 |
| 30 |
#define BM1390_MASK_RESET BIT(0) |
| 31 |
#define BM1390_RESET_RELEASE BM1390_MASK_RESET |
| 32 |
#define BM1390_RESET 0x00 |
| 33 |
#define BM1390_REG_MODE_CTRL 0x14 |
| 34 |
#define BM1390_MASK_MEAS_MODE GENMASK(1, 0) |
| 35 |
#define BM1390_MASK_DRDY_EN BIT(4) |
| 36 |
#define BM1390_MASK_WMI_EN BIT(2) |
| 37 |
#define BM1390_MASK_AVE_NUM GENMASK(7, 5) |
| 38 |
|
| 39 |
/* |
| 40 |
* Data-sheet states that when the IIR is used, the AVE_NUM must be set to |
| 41 |
* value 110b |
| 42 |
*/ |
| 43 |
#define BM1390_IIR_AVE_NUM 0x06 |
| 44 |
#define BM1390_REG_FIFO_CTRL 0x15 |
| 45 |
#define BM1390_MASK_IIR_MODE GENMASK(1, 0) |
| 46 |
#define BM1390_IIR_MODE_OFF 0x0 |
| 47 |
#define BM1390_IIR_MODE_WEAK 0x1 |
| 48 |
#define BM1390_IIR_MODE_MID 0x2 |
| 49 |
#define BM1390_IIR_MODE_STRONG 0x3 |
| 50 |
|
| 51 |
#define BM1390_MASK_FIFO_LEN BIT(6) |
| 52 |
#define BM1390_MASK_FIFO_EN BIT(7) |
| 53 |
#define BM1390_WMI_MIN 2 |
| 54 |
#define BM1390_WMI_MAX 3 |
| 55 |
|
| 56 |
#define BM1390_REG_FIFO_LVL 0x18 |
| 57 |
#define BM1390_MASK_FIFO_LVL GENMASK(2, 0) |
| 58 |
#define BM1390_REG_STATUS 0x19 |
| 59 |
#define BM1390_REG_PRESSURE_BASE 0x1a |
| 60 |
#define BM1390_REG_TEMP_HI 0x1d |
| 61 |
#define BM1390_REG_TEMP_LO 0x1e |
| 62 |
#define BM1390_MAX_REGISTER BM1390_REG_TEMP_LO |
| 63 |
|
| 64 |
#define BM1390_ID 0x34 |
| 65 |
|
| 66 |
/* Regmap configs */ |
| 67 |
static const struct regmap_range bm1390_volatile_ranges[] = { |
| 68 |
{ |
| 69 |
.range_min = BM1390_REG_STATUS, |
| 70 |
.range_max = BM1390_REG_STATUS, |
| 71 |
}, |
| 72 |
{ |
| 73 |
.range_min = BM1390_REG_FIFO_LVL, |
| 74 |
.range_max = BM1390_REG_TEMP_LO, |
| 75 |
}, |
| 76 |
}; |
| 77 |
|
| 78 |
static const struct regmap_access_table bm1390_volatile_regs = { |
| 79 |
.yes_ranges = &bm1390_volatile_ranges[0], |
| 80 |
.n_yes_ranges = ARRAY_SIZE(bm1390_volatile_ranges), |
| 81 |
}; |
| 82 |
|
| 83 |
static const struct regmap_range bm1390_precious_ranges[] = { |
| 84 |
{ |
| 85 |
.range_min = BM1390_REG_STATUS, |
| 86 |
.range_max = BM1390_REG_STATUS, |
| 87 |
}, |
| 88 |
}; |
| 89 |
|
| 90 |
static const struct regmap_access_table bm1390_precious_regs = { |
| 91 |
.yes_ranges = &bm1390_precious_ranges[0], |
| 92 |
.n_yes_ranges = ARRAY_SIZE(bm1390_precious_ranges), |
| 93 |
}; |
| 94 |
|
| 95 |
static const struct regmap_range bm1390_read_only_ranges[] = { |
| 96 |
{ |
| 97 |
.range_min = BM1390_REG_MANUFACT_ID, |
| 98 |
.range_max = BM1390_REG_PART_ID, |
| 99 |
}, { |
| 100 |
.range_min = BM1390_REG_FIFO_LVL, |
| 101 |
.range_max = BM1390_REG_TEMP_LO, |
| 102 |
}, |
| 103 |
}; |
| 104 |
|
| 105 |
static const struct regmap_access_table bm1390_ro_regs = { |
| 106 |
.no_ranges = &bm1390_read_only_ranges[0], |
| 107 |
.n_no_ranges = ARRAY_SIZE(bm1390_read_only_ranges), |
| 108 |
}; |
| 109 |
|
| 110 |
static const struct regmap_range bm1390_noinc_read_ranges[] = { |
| 111 |
{ |
| 112 |
.range_min = BM1390_REG_PRESSURE_BASE, |
| 113 |
.range_max = BM1390_REG_TEMP_LO, |
| 114 |
}, |
| 115 |
}; |
| 116 |
|
| 117 |
static const struct regmap_access_table bm1390_nir_regs = { |
| 118 |
.yes_ranges = &bm1390_noinc_read_ranges[0], |
| 119 |
.n_yes_ranges = ARRAY_SIZE(bm1390_noinc_read_ranges), |
| 120 |
}; |
| 121 |
|
| 122 |
static const struct regmap_config bm1390_regmap = { |
| 123 |
.reg_bits = 8, |
| 124 |
.val_bits = 8, |
| 125 |
.volatile_table = &bm1390_volatile_regs, |
| 126 |
.wr_table = &bm1390_ro_regs, |
| 127 |
.rd_noinc_table = &bm1390_nir_regs, |
| 128 |
.precious_table = &bm1390_precious_regs, |
| 129 |
.max_register = BM1390_MAX_REGISTER, |
| 130 |
.cache_type = REGCACHE_RBTREE, |
| 131 |
.disable_locking = true, |
| 132 |
}; |
| 133 |
|
| 134 |
enum { |
| 135 |
BM1390_STATE_SAMPLE, |
| 136 |
BM1390_STATE_FIFO, |
| 137 |
}; |
| 138 |
|
| 139 |
struct bm1390_data_buf { |
| 140 |
u32 pressure; |
| 141 |
__be16 temp; |
| 142 |
aligned_s64 ts; |
| 143 |
}; |
| 144 |
|
| 145 |
/* BM1390 has FIFO for 4 pressure samples */ |
| 146 |
#define BM1390_FIFO_LENGTH 4 |
| 147 |
|
| 148 |
struct bm1390_data { |
| 149 |
s64 timestamp, old_timestamp; |
| 150 |
struct iio_trigger *trig; |
| 151 |
struct regmap *regmap; |
| 152 |
struct device *dev; |
| 153 |
struct bm1390_data_buf buf; |
| 154 |
int irq; |
| 155 |
unsigned int state; |
| 156 |
bool trigger_enabled; |
| 157 |
u8 watermark; |
| 158 |
|
| 159 |
/* Prevent accessing sensor during FIFO read sequence */ |
| 160 |
struct mutex mutex; |
| 161 |
}; |
| 162 |
|
| 163 |
enum { |
| 164 |
BM1390_CHAN_PRESSURE, |
| 165 |
BM1390_CHAN_TEMP, |
| 166 |
}; |
| 167 |
|
| 168 |
static const struct iio_chan_spec bm1390_channels[] = { |
| 169 |
{ |
| 170 |
.type = IIO_PRESSURE, |
| 171 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 172 |
/* |
| 173 |
* When IIR is used, we must fix amount of averaged samples. |
| 174 |
* Thus we don't allow setting oversampling ratio. |
| 175 |
*/ |
| 176 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 177 |
.scan_index = BM1390_CHAN_PRESSURE, |
| 178 |
.scan_type = { |
| 179 |
.sign = 'u', |
| 180 |
.realbits = 22, |
| 181 |
.storagebits = 32, |
| 182 |
.endianness = IIO_LE, |
| 183 |
}, |
| 184 |
}, |
| 185 |
{ |
| 186 |
.type = IIO_TEMP, |
| 187 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 188 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
| 189 |
.scan_index = BM1390_CHAN_TEMP, |
| 190 |
.scan_type = { |
| 191 |
.sign = 's', |
| 192 |
.realbits = 16, |
| 193 |
.storagebits = 16, |
| 194 |
.endianness = IIO_BE, |
| 195 |
}, |
| 196 |
}, |
| 197 |
IIO_CHAN_SOFT_TIMESTAMP(2), |
| 198 |
}; |
| 199 |
|
| 200 |
/* |
| 201 |
* We can't skip reading the pressure because the watermark IRQ is acked |
| 202 |
* only when the pressure data is read from the FIFO. |
| 203 |
*/ |
| 204 |
static const unsigned long bm1390_scan_masks[] = { |
| 205 |
BIT(BM1390_CHAN_PRESSURE), |
| 206 |
BIT(BM1390_CHAN_PRESSURE) | BIT(BM1390_CHAN_TEMP), |
| 207 |
0 |
| 208 |
}; |
| 209 |
|
| 210 |
static int bm1390_read_temp(struct bm1390_data *data, int *temp) |
| 211 |
{ |
| 212 |
__be16 temp_raw; |
| 213 |
int ret; |
| 214 |
|
| 215 |
ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp_raw, |
| 216 |
sizeof(temp_raw)); |
| 217 |
if (ret) |
| 218 |
return ret; |
| 219 |
|
| 220 |
*temp = be16_to_cpu(temp_raw); |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
static int bm1390_pressure_read(struct bm1390_data *data, u32 *pressure) |
| 226 |
{ |
| 227 |
/* Pressure data is in 3 8-bit registers */ |
| 228 |
u8 raw[3]; |
| 229 |
int ret; |
| 230 |
|
| 231 |
ret = regmap_bulk_read(data->regmap, BM1390_REG_PRESSURE_BASE, |
| 232 |
raw, sizeof(raw)); |
| 233 |
if (ret < 0) |
| 234 |
return ret; |
| 235 |
|
| 236 |
*pressure = (u32)(raw[2] >> 2 | raw[1] << 6 | raw[0] << 14); |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
/* The enum values map directly to register bits */ |
| 242 |
enum bm1390_meas_mode { |
| 243 |
BM1390_MEAS_MODE_STOP = 0x0, |
| 244 |
BM1390_MEAS_MODE_1SHOT = 0x1, |
| 245 |
BM1390_MEAS_MODE_CONTINUOUS = 0x2, |
| 246 |
}; |
| 247 |
|
| 248 |
static int bm1390_meas_set(struct bm1390_data *data, enum bm1390_meas_mode mode) |
| 249 |
{ |
| 250 |
return regmap_update_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 251 |
BM1390_MASK_MEAS_MODE, mode); |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* If the trigger is not used we just wait until the measurement has |
| 256 |
* completed. The data-sheet says maximum measurement cycle (regardless |
| 257 |
* the AVE_NUM) is 200 mS so let's just sleep at least that long. If speed |
| 258 |
* is needed the trigger should be used. |
| 259 |
*/ |
| 260 |
#define BM1390_MAX_MEAS_TIME_MS 205 |
| 261 |
|
| 262 |
static int bm1390_read_data(struct bm1390_data *data, |
| 263 |
struct iio_chan_spec const *chan, int *val, int *val2) |
| 264 |
{ |
| 265 |
int ret, warn; |
| 266 |
|
| 267 |
guard(mutex)(&data->mutex); |
| 268 |
/* |
| 269 |
* We use 'continuous mode' even for raw read because according to the |
| 270 |
* data-sheet an one-shot mode can't be used with IIR filter. |
| 271 |
*/ |
| 272 |
ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); |
| 273 |
if (ret) |
| 274 |
return ret; |
| 275 |
|
| 276 |
switch (chan->type) { |
| 277 |
case IIO_PRESSURE: |
| 278 |
msleep(BM1390_MAX_MEAS_TIME_MS); |
| 279 |
ret = bm1390_pressure_read(data, val); |
| 280 |
break; |
| 281 |
case IIO_TEMP: |
| 282 |
msleep(BM1390_MAX_MEAS_TIME_MS); |
| 283 |
ret = bm1390_read_temp(data, val); |
| 284 |
break; |
| 285 |
default: |
| 286 |
ret = -EINVAL; |
| 287 |
} |
| 288 |
warn = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); |
| 289 |
if (warn) |
| 290 |
dev_warn(data->dev, "Failed to stop measurement (%d)\n", warn); |
| 291 |
|
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
static int bm1390_read_raw(struct iio_dev *idev, |
| 296 |
struct iio_chan_spec const *chan, |
| 297 |
int *val, int *val2, long mask) |
| 298 |
{ |
| 299 |
struct bm1390_data *data = iio_priv(idev); |
| 300 |
int ret; |
| 301 |
|
| 302 |
switch (mask) { |
| 303 |
case IIO_CHAN_INFO_SCALE: |
| 304 |
if (chan->type == IIO_TEMP) { |
| 305 |
*val = 31; |
| 306 |
*val2 = 250000; |
| 307 |
|
| 308 |
return IIO_VAL_INT_PLUS_MICRO; |
| 309 |
} else if (chan->type == IIO_PRESSURE) { |
| 310 |
/* |
| 311 |
* pressure in hPa is register value divided by 2048. |
| 312 |
* This means kPa is 1/20480 times the register value, |
| 313 |
*/ |
| 314 |
*val = 1; |
| 315 |
*val2 = 2048; |
| 316 |
|
| 317 |
return IIO_VAL_FRACTIONAL; |
| 318 |
} |
| 319 |
|
| 320 |
return -EINVAL; |
| 321 |
case IIO_CHAN_INFO_RAW: |
| 322 |
if (!iio_device_claim_direct(idev)) |
| 323 |
return -EBUSY; |
| 324 |
|
| 325 |
ret = bm1390_read_data(data, chan, val, val2); |
| 326 |
iio_device_release_direct(idev); |
| 327 |
if (ret) |
| 328 |
return ret; |
| 329 |
|
| 330 |
return IIO_VAL_INT; |
| 331 |
default: |
| 332 |
return -EINVAL; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
static int __bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples, |
| 337 |
s64 timestamp) |
| 338 |
{ |
| 339 |
/* BM1390_FIFO_LENGTH is small so we shouldn't run out of stack */ |
| 340 |
struct bm1390_data_buf buffer[BM1390_FIFO_LENGTH]; |
| 341 |
struct bm1390_data *data = iio_priv(idev); |
| 342 |
int smp_lvl, ret, i, warn, dummy; |
| 343 |
u64 sample_period; |
| 344 |
__be16 temp = 0; |
| 345 |
|
| 346 |
ret = regmap_read(data->regmap, BM1390_REG_FIFO_LVL, &smp_lvl); |
| 347 |
if (ret) |
| 348 |
return ret; |
| 349 |
|
| 350 |
smp_lvl = FIELD_GET(BM1390_MASK_FIFO_LVL, smp_lvl); |
| 351 |
if (!smp_lvl) |
| 352 |
return 0; |
| 353 |
|
| 354 |
if (smp_lvl > BM1390_FIFO_LENGTH) { |
| 355 |
/* |
| 356 |
* The fifo holds maximum of 4 samples so valid values |
| 357 |
* should be 0, 1, 2, 3, 4 - rest are probably bit errors |
| 358 |
* in I2C line. Don't overflow if this happens. |
| 359 |
*/ |
| 360 |
dev_err(data->dev, "bad FIFO level %d\n", smp_lvl); |
| 361 |
smp_lvl = BM1390_FIFO_LENGTH; |
| 362 |
} |
| 363 |
|
| 364 |
sample_period = timestamp - data->old_timestamp; |
| 365 |
do_div(sample_period, smp_lvl); |
| 366 |
|
| 367 |
if (samples && smp_lvl > samples) |
| 368 |
smp_lvl = samples; |
| 369 |
|
| 370 |
|
| 371 |
/* |
| 372 |
* After some testing it appears that the temperature is not readable |
| 373 |
* until the FIFO access has been done after the WMI. Thus, we need |
| 374 |
* to read the all pressure values to memory and read the temperature |
| 375 |
* only after that. |
| 376 |
*/ |
| 377 |
for (i = 0; i < smp_lvl; i++) { |
| 378 |
/* |
| 379 |
* When we start reading data from the FIFO the sensor goes to |
| 380 |
* special FIFO reading mode. If any other register is accessed |
| 381 |
* during the FIFO read, samples can be dropped. Prevent access |
| 382 |
* until FIFO_LVL is read. We have mutex locked and we do also |
| 383 |
* go performing reading of FIFO_LVL even if this read fails. |
| 384 |
*/ |
| 385 |
if (test_bit(BM1390_CHAN_PRESSURE, idev->active_scan_mask)) { |
| 386 |
ret = bm1390_pressure_read(data, &buffer[i].pressure); |
| 387 |
if (ret) |
| 388 |
break; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* Old timestamp is either the previous sample IRQ time, |
| 393 |
* previous flush-time or, if this was first sample, the enable |
| 394 |
* time. When we add a sample period to that we should get the |
| 395 |
* best approximation of the time-stamp we are handling. |
| 396 |
* |
| 397 |
* Idea is to always keep the "old_timestamp" matching the |
| 398 |
* timestamp which we are currently handling. |
| 399 |
*/ |
| 400 |
data->old_timestamp += sample_period; |
| 401 |
buffer[i].ts = data->old_timestamp; |
| 402 |
} |
| 403 |
/* Reading the FIFO_LVL closes the FIFO access sequence */ |
| 404 |
warn = regmap_read(data->regmap, BM1390_REG_FIFO_LVL, &dummy); |
| 405 |
if (warn) |
| 406 |
dev_warn(data->dev, "Closing FIFO sequence failed\n"); |
| 407 |
|
| 408 |
if (ret) |
| 409 |
return ret; |
| 410 |
|
| 411 |
if (test_bit(BM1390_CHAN_TEMP, idev->active_scan_mask)) { |
| 412 |
ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp, |
| 413 |
sizeof(temp)); |
| 414 |
if (ret) |
| 415 |
return ret; |
| 416 |
} |
| 417 |
|
| 418 |
for (i = 0; i < smp_lvl; i++) { |
| 419 |
buffer[i].temp = temp; |
| 420 |
iio_push_to_buffers(idev, &buffer[i]); |
| 421 |
} |
| 422 |
|
| 423 |
return smp_lvl; |
| 424 |
} |
| 425 |
|
| 426 |
static int bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples) |
| 427 |
{ |
| 428 |
struct bm1390_data *data = iio_priv(idev); |
| 429 |
s64 timestamp; |
| 430 |
int ret; |
| 431 |
|
| 432 |
/* |
| 433 |
* If fifo_flush is being called from IRQ handler we know the stored |
| 434 |
* timestamp is fairly accurate for the last stored sample. If we are |
| 435 |
* called as a result of a read operation from userspace and hence |
| 436 |
* before the watermark interrupt was triggered, take a timestamp |
| 437 |
* now. We can fall anywhere in between two samples so the error in this |
| 438 |
* case is at most one sample period. |
| 439 |
* We need to have the IRQ disabled or we risk of messing-up |
| 440 |
* the timestamps. If we are ran from IRQ, then the |
| 441 |
* IRQF_ONESHOT has us covered - but if we are ran by the |
| 442 |
* user-space read we need to disable the IRQ to be on a safe |
| 443 |
* side. We do this usng synchronous disable so that if the |
| 444 |
* IRQ thread is being ran on other CPU we wait for it to be |
| 445 |
* finished. |
| 446 |
*/ |
| 447 |
|
| 448 |
timestamp = iio_get_time_ns(idev); |
| 449 |
mutex_lock(&data->mutex); |
| 450 |
ret = __bm1390_fifo_flush(idev, samples, timestamp); |
| 451 |
mutex_unlock(&data->mutex); |
| 452 |
|
| 453 |
return ret; |
| 454 |
} |
| 455 |
|
| 456 |
static int bm1390_set_watermark(struct iio_dev *idev, unsigned int val) |
| 457 |
{ |
| 458 |
struct bm1390_data *data = iio_priv(idev); |
| 459 |
|
| 460 |
if (val < BM1390_WMI_MIN || val > BM1390_WMI_MAX) |
| 461 |
return -EINVAL; |
| 462 |
|
| 463 |
mutex_lock(&data->mutex); |
| 464 |
data->watermark = val; |
| 465 |
mutex_unlock(&data->mutex); |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
static const struct iio_info bm1390_noirq_info = { |
| 471 |
.read_raw = &bm1390_read_raw, |
| 472 |
}; |
| 473 |
|
| 474 |
static const struct iio_info bm1390_info = { |
| 475 |
.read_raw = &bm1390_read_raw, |
| 476 |
.hwfifo_set_watermark = bm1390_set_watermark, |
| 477 |
.hwfifo_flush_to_buffer = bm1390_fifo_flush, |
| 478 |
}; |
| 479 |
|
| 480 |
static int bm1390_chip_init(struct bm1390_data *data) |
| 481 |
{ |
| 482 |
int ret; |
| 483 |
|
| 484 |
ret = regmap_write_bits(data->regmap, BM1390_REG_POWER, |
| 485 |
BM1390_MASK_POWER, BM1390_POWER_ON); |
| 486 |
if (ret) |
| 487 |
return ret; |
| 488 |
|
| 489 |
msleep(1); |
| 490 |
|
| 491 |
ret = regmap_write_bits(data->regmap, BM1390_REG_RESET, |
| 492 |
BM1390_MASK_RESET, BM1390_RESET); |
| 493 |
if (ret) |
| 494 |
return ret; |
| 495 |
|
| 496 |
msleep(1); |
| 497 |
|
| 498 |
ret = regmap_write_bits(data->regmap, BM1390_REG_RESET, |
| 499 |
BM1390_MASK_RESET, BM1390_RESET_RELEASE); |
| 500 |
if (ret) |
| 501 |
return ret; |
| 502 |
|
| 503 |
msleep(1); |
| 504 |
|
| 505 |
ret = regmap_reinit_cache(data->regmap, &bm1390_regmap); |
| 506 |
if (ret) { |
| 507 |
dev_err(data->dev, "Failed to reinit reg cache\n"); |
| 508 |
return ret; |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* Default to use IIR filter in "middle" mode. Also the AVE_NUM must |
| 513 |
* be fixed when IIR is in use. |
| 514 |
*/ |
| 515 |
ret = regmap_update_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 516 |
BM1390_MASK_AVE_NUM, BM1390_IIR_AVE_NUM); |
| 517 |
if (ret) |
| 518 |
return ret; |
| 519 |
|
| 520 |
return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL, |
| 521 |
BM1390_MASK_IIR_MODE, BM1390_IIR_MODE_MID); |
| 522 |
} |
| 523 |
|
| 524 |
static int bm1390_fifo_set_wmi(struct bm1390_data *data) |
| 525 |
{ |
| 526 |
u8 regval; |
| 527 |
|
| 528 |
regval = FIELD_PREP(BM1390_MASK_FIFO_LEN, |
| 529 |
data->watermark - BM1390_WMI_MIN); |
| 530 |
|
| 531 |
return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL, |
| 532 |
BM1390_MASK_FIFO_LEN, regval); |
| 533 |
} |
| 534 |
|
| 535 |
static int bm1390_fifo_enable(struct iio_dev *idev) |
| 536 |
{ |
| 537 |
struct bm1390_data *data = iio_priv(idev); |
| 538 |
int ret; |
| 539 |
|
| 540 |
/* We can't do buffered stuff without IRQ as we never get WMI */ |
| 541 |
if (data->irq <= 0) |
| 542 |
return -EINVAL; |
| 543 |
|
| 544 |
guard(mutex)(&data->mutex); |
| 545 |
|
| 546 |
if (data->trigger_enabled) |
| 547 |
return -EBUSY; |
| 548 |
|
| 549 |
/* Update watermark to HW */ |
| 550 |
ret = bm1390_fifo_set_wmi(data); |
| 551 |
if (ret) |
| 552 |
return ret; |
| 553 |
|
| 554 |
/* Enable WMI_IRQ */ |
| 555 |
ret = regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 556 |
BM1390_MASK_WMI_EN); |
| 557 |
if (ret) |
| 558 |
return ret; |
| 559 |
|
| 560 |
/* Enable FIFO */ |
| 561 |
ret = regmap_set_bits(data->regmap, BM1390_REG_FIFO_CTRL, |
| 562 |
BM1390_MASK_FIFO_EN); |
| 563 |
if (ret) |
| 564 |
return ret; |
| 565 |
|
| 566 |
data->state = BM1390_STATE_FIFO; |
| 567 |
|
| 568 |
data->old_timestamp = iio_get_time_ns(idev); |
| 569 |
|
| 570 |
return bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); |
| 571 |
} |
| 572 |
|
| 573 |
static int bm1390_fifo_disable(struct iio_dev *idev) |
| 574 |
{ |
| 575 |
struct bm1390_data *data = iio_priv(idev); |
| 576 |
int ret; |
| 577 |
|
| 578 |
msleep(1); |
| 579 |
|
| 580 |
guard(mutex)(&data->mutex); |
| 581 |
ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); |
| 582 |
if (ret) |
| 583 |
return ret; |
| 584 |
|
| 585 |
/* Disable FIFO */ |
| 586 |
ret = regmap_clear_bits(data->regmap, BM1390_REG_FIFO_CTRL, |
| 587 |
BM1390_MASK_FIFO_EN); |
| 588 |
if (ret) |
| 589 |
return ret; |
| 590 |
|
| 591 |
data->state = BM1390_STATE_SAMPLE; |
| 592 |
|
| 593 |
/* Disable WMI_IRQ */ |
| 594 |
return regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 595 |
BM1390_MASK_WMI_EN); |
| 596 |
} |
| 597 |
|
| 598 |
static int bm1390_buffer_postenable(struct iio_dev *idev) |
| 599 |
{ |
| 600 |
/* |
| 601 |
* If we use data-ready trigger, then the IRQ masks should be handled by |
| 602 |
* trigger enable and the hardware buffer is not used but we just update |
| 603 |
* results to the IIO FIFO when data-ready triggers. |
| 604 |
*/ |
| 605 |
if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED) |
| 606 |
return 0; |
| 607 |
|
| 608 |
return bm1390_fifo_enable(idev); |
| 609 |
} |
| 610 |
|
| 611 |
static int bm1390_buffer_predisable(struct iio_dev *idev) |
| 612 |
{ |
| 613 |
if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED) |
| 614 |
return 0; |
| 615 |
|
| 616 |
return bm1390_fifo_disable(idev); |
| 617 |
} |
| 618 |
|
| 619 |
static const struct iio_buffer_setup_ops bm1390_buffer_ops = { |
| 620 |
.postenable = bm1390_buffer_postenable, |
| 621 |
.predisable = bm1390_buffer_predisable, |
| 622 |
}; |
| 623 |
|
| 624 |
static irqreturn_t bm1390_trigger_handler(int irq, void *p) |
| 625 |
{ |
| 626 |
struct iio_poll_func *pf = p; |
| 627 |
struct iio_dev *idev = pf->indio_dev; |
| 628 |
struct bm1390_data *data = iio_priv(idev); |
| 629 |
int ret, status; |
| 630 |
|
| 631 |
/* DRDY is acked by reading status reg */ |
| 632 |
ret = regmap_read(data->regmap, BM1390_REG_STATUS, &status); |
| 633 |
if (ret || !status) |
| 634 |
return IRQ_NONE; |
| 635 |
|
| 636 |
dev_dbg(data->dev, "DRDY trig status 0x%x\n", status); |
| 637 |
|
| 638 |
if (test_bit(BM1390_CHAN_PRESSURE, idev->active_scan_mask)) { |
| 639 |
ret = bm1390_pressure_read(data, &data->buf.pressure); |
| 640 |
if (ret) { |
| 641 |
dev_warn(data->dev, "sample read failed %d\n", ret); |
| 642 |
return IRQ_NONE; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
if (test_bit(BM1390_CHAN_TEMP, idev->active_scan_mask)) { |
| 647 |
ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, |
| 648 |
&data->buf.temp, sizeof(data->buf.temp)); |
| 649 |
if (ret) { |
| 650 |
dev_warn(data->dev, "temp read failed %d\n", ret); |
| 651 |
return IRQ_HANDLED; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
iio_push_to_buffers_with_ts(idev, &data->buf, sizeof(data->buf), |
| 656 |
data->timestamp); |
| 657 |
iio_trigger_notify_done(idev->trig); |
| 658 |
|
| 659 |
return IRQ_HANDLED; |
| 660 |
} |
| 661 |
|
| 662 |
/* Get timestamps and wake the thread if we need to read data */ |
| 663 |
static irqreturn_t bm1390_irq_handler(int irq, void *private) |
| 664 |
{ |
| 665 |
struct iio_dev *idev = private; |
| 666 |
struct bm1390_data *data = iio_priv(idev); |
| 667 |
|
| 668 |
data->timestamp = iio_get_time_ns(idev); |
| 669 |
|
| 670 |
if (data->state == BM1390_STATE_FIFO || data->trigger_enabled) |
| 671 |
return IRQ_WAKE_THREAD; |
| 672 |
|
| 673 |
return IRQ_NONE; |
| 674 |
} |
| 675 |
|
| 676 |
static irqreturn_t bm1390_irq_thread_handler(int irq, void *private) |
| 677 |
{ |
| 678 |
struct iio_dev *idev = private; |
| 679 |
struct bm1390_data *data = iio_priv(idev); |
| 680 |
|
| 681 |
guard(mutex)(&data->mutex); |
| 682 |
|
| 683 |
if (data->trigger_enabled) { |
| 684 |
iio_trigger_poll_nested(data->trig); |
| 685 |
return IRQ_HANDLED; |
| 686 |
} |
| 687 |
|
| 688 |
if (data->state == BM1390_STATE_FIFO) { |
| 689 |
int ok; |
| 690 |
|
| 691 |
ok = __bm1390_fifo_flush(idev, BM1390_FIFO_LENGTH, |
| 692 |
data->timestamp); |
| 693 |
if (ok > 0) |
| 694 |
return IRQ_HANDLED; |
| 695 |
} |
| 696 |
|
| 697 |
return IRQ_NONE; |
| 698 |
} |
| 699 |
|
| 700 |
static int bm1390_set_drdy_irq(struct bm1390_data *data, bool en) |
| 701 |
{ |
| 702 |
if (en) |
| 703 |
return regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 704 |
BM1390_MASK_DRDY_EN); |
| 705 |
return regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL, |
| 706 |
BM1390_MASK_DRDY_EN); |
| 707 |
} |
| 708 |
|
| 709 |
static int bm1390_trigger_set_state(struct iio_trigger *trig, |
| 710 |
bool state) |
| 711 |
{ |
| 712 |
struct bm1390_data *data = iio_trigger_get_drvdata(trig); |
| 713 |
int ret; |
| 714 |
|
| 715 |
guard(mutex)(&data->mutex); |
| 716 |
|
| 717 |
if (data->trigger_enabled == state) |
| 718 |
return 0; |
| 719 |
|
| 720 |
if (data->state == BM1390_STATE_FIFO) { |
| 721 |
dev_warn(data->dev, "Can't set trigger when FIFO enabled\n"); |
| 722 |
return -EBUSY; |
| 723 |
} |
| 724 |
|
| 725 |
data->trigger_enabled = state; |
| 726 |
|
| 727 |
if (state) { |
| 728 |
ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); |
| 729 |
if (ret) |
| 730 |
return ret; |
| 731 |
} else { |
| 732 |
int dummy; |
| 733 |
|
| 734 |
ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); |
| 735 |
if (ret) |
| 736 |
return ret; |
| 737 |
|
| 738 |
/* |
| 739 |
* We need to read the status register in order to ACK the |
| 740 |
* data-ready which may have been generated just before we |
| 741 |
* disabled the measurement. |
| 742 |
*/ |
| 743 |
ret = regmap_read(data->regmap, BM1390_REG_STATUS, &dummy); |
| 744 |
if (ret) |
| 745 |
dev_warn(data->dev, "status read failed\n"); |
| 746 |
} |
| 747 |
|
| 748 |
return bm1390_set_drdy_irq(data, state); |
| 749 |
} |
| 750 |
|
| 751 |
static const struct iio_trigger_ops bm1390_trigger_ops = { |
| 752 |
.set_trigger_state = bm1390_trigger_set_state, |
| 753 |
}; |
| 754 |
|
| 755 |
static int bm1390_setup_buffer(struct bm1390_data *data, struct iio_dev *idev) |
| 756 |
{ |
| 757 |
int ret; |
| 758 |
|
| 759 |
ret = devm_iio_triggered_buffer_setup(data->dev, idev, |
| 760 |
&iio_pollfunc_store_time, |
| 761 |
&bm1390_trigger_handler, |
| 762 |
&bm1390_buffer_ops); |
| 763 |
|
| 764 |
if (ret) |
| 765 |
return dev_err_probe(data->dev, ret, |
| 766 |
"iio_triggered_buffer_setup FAIL\n"); |
| 767 |
|
| 768 |
idev->available_scan_masks = bm1390_scan_masks; |
| 769 |
|
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
static int bm1390_setup_trigger(struct bm1390_data *data, struct iio_dev *idev, |
| 774 |
int irq) |
| 775 |
{ |
| 776 |
struct iio_trigger *itrig; |
| 777 |
char *name; |
| 778 |
int ret; |
| 779 |
|
| 780 |
itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d", idev->name, |
| 781 |
iio_device_id(idev)); |
| 782 |
if (!itrig) |
| 783 |
return -ENOMEM; |
| 784 |
|
| 785 |
data->trig = itrig; |
| 786 |
|
| 787 |
itrig->ops = &bm1390_trigger_ops; |
| 788 |
iio_trigger_set_drvdata(itrig, data); |
| 789 |
|
| 790 |
name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bm1390", |
| 791 |
dev_name(data->dev)); |
| 792 |
if (name == NULL) |
| 793 |
return -ENOMEM; |
| 794 |
|
| 795 |
ret = devm_request_threaded_irq(data->dev, irq, bm1390_irq_handler, |
| 796 |
&bm1390_irq_thread_handler, |
| 797 |
IRQF_ONESHOT, name, idev); |
| 798 |
if (ret) |
| 799 |
return dev_err_probe(data->dev, ret, "Could not request IRQ\n"); |
| 800 |
|
| 801 |
|
| 802 |
ret = devm_iio_trigger_register(data->dev, itrig); |
| 803 |
if (ret) |
| 804 |
return dev_err_probe(data->dev, ret, |
| 805 |
"Trigger registration failed\n"); |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
static int bm1390_probe(struct i2c_client *i2c) |
| 811 |
{ |
| 812 |
struct bm1390_data *data; |
| 813 |
struct regmap *regmap; |
| 814 |
struct iio_dev *idev; |
| 815 |
struct device *dev; |
| 816 |
unsigned int part_id; |
| 817 |
int ret; |
| 818 |
|
| 819 |
dev = &i2c->dev; |
| 820 |
|
| 821 |
regmap = devm_regmap_init_i2c(i2c, &bm1390_regmap); |
| 822 |
if (IS_ERR(regmap)) |
| 823 |
return dev_err_probe(dev, PTR_ERR(regmap), |
| 824 |
"Failed to initialize Regmap\n"); |
| 825 |
|
| 826 |
ret = devm_regulator_get_enable(dev, "vdd"); |
| 827 |
if (ret) |
| 828 |
return dev_err_probe(dev, ret, "Failed to get regulator\n"); |
| 829 |
|
| 830 |
ret = regmap_read(regmap, BM1390_REG_PART_ID, &part_id); |
| 831 |
if (ret) |
| 832 |
return dev_err_probe(dev, ret, "Failed to access sensor\n"); |
| 833 |
|
| 834 |
if (part_id != BM1390_ID) |
| 835 |
dev_warn(dev, "unknown device 0x%x\n", part_id); |
| 836 |
|
| 837 |
idev = devm_iio_device_alloc(dev, sizeof(*data)); |
| 838 |
if (!idev) |
| 839 |
return -ENOMEM; |
| 840 |
|
| 841 |
data = iio_priv(idev); |
| 842 |
data->regmap = regmap; |
| 843 |
data->dev = dev; |
| 844 |
data->irq = i2c->irq; |
| 845 |
/* |
| 846 |
* For now we just allow BM1390_WMI_MIN to BM1390_WMI_MAX and |
| 847 |
* discard every other configuration when triggered mode is not used. |
| 848 |
*/ |
| 849 |
data->watermark = BM1390_WMI_MAX; |
| 850 |
mutex_init(&data->mutex); |
| 851 |
|
| 852 |
idev->channels = bm1390_channels; |
| 853 |
idev->num_channels = ARRAY_SIZE(bm1390_channels); |
| 854 |
idev->name = "bm1390"; |
| 855 |
idev->modes = INDIO_DIRECT_MODE; |
| 856 |
|
| 857 |
ret = bm1390_chip_init(data); |
| 858 |
if (ret) |
| 859 |
return dev_err_probe(dev, ret, "sensor init failed\n"); |
| 860 |
|
| 861 |
ret = bm1390_setup_buffer(data, idev); |
| 862 |
if (ret) |
| 863 |
return ret; |
| 864 |
|
| 865 |
/* No trigger if we don't have IRQ for data-ready and WMI */ |
| 866 |
if (i2c->irq > 0) { |
| 867 |
idev->info = &bm1390_info; |
| 868 |
idev->modes |= INDIO_BUFFER_SOFTWARE; |
| 869 |
ret = bm1390_setup_trigger(data, idev, i2c->irq); |
| 870 |
if (ret) |
| 871 |
return ret; |
| 872 |
} else { |
| 873 |
idev->info = &bm1390_noirq_info; |
| 874 |
} |
| 875 |
|
| 876 |
ret = devm_iio_device_register(dev, idev); |
| 877 |
if (ret < 0) |
| 878 |
return dev_err_probe(dev, ret, |
| 879 |
"Unable to register iio device\n"); |
| 880 |
|
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
static const struct of_device_id bm1390_of_match[] = { |
| 885 |
{ .compatible = "rohm,bm1390glv-z" }, |
| 886 |
{ } |
| 887 |
}; |
| 888 |
MODULE_DEVICE_TABLE(of, bm1390_of_match); |
| 889 |
|
| 890 |
static const struct i2c_device_id bm1390_id[] = { |
| 891 |
{ "bm1390glv-z", }, |
| 892 |
{ } |
| 893 |
}; |
| 894 |
MODULE_DEVICE_TABLE(i2c, bm1390_id); |
| 895 |
|
| 896 |
static struct i2c_driver bm1390_driver = { |
| 897 |
.driver = { |
| 898 |
.name = "bm1390", |
| 899 |
.of_match_table = bm1390_of_match, |
| 900 |
/* |
| 901 |
* Probing explicitly requires a few millisecond of sleep. |
| 902 |
* Enabling the VDD regulator may include ramp up rates. |
| 903 |
*/ |
| 904 |
.probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 905 |
}, |
| 906 |
.probe = bm1390_probe, |
| 907 |
.id_table = bm1390_id, |
| 908 |
}; |
| 909 |
module_i2c_driver(bm1390_driver); |
| 910 |
|
| 911 |
MODULE_AUTHOR("Matti Vaittinen <[email protected]>"); |
| 912 |
MODULE_DESCRIPTION("Driver for ROHM BM1390 pressure sensor"); |
| 913 |
MODULE_LICENSE("GPL"); |